xref: /onnv-gate/usr/src/uts/intel/ia32/os/sundep.c (revision 749:d7f9da43aeb7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
290Sstevel@tonic-gate /*	All Rights Reserved   */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/user.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate #include <sys/class.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/procfs.h>
430Sstevel@tonic-gate #include <sys/buf.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/cred.h>
460Sstevel@tonic-gate #include <sys/archsystm.h>
470Sstevel@tonic-gate #include <sys/vmparam.h>
480Sstevel@tonic-gate #include <sys/prsystm.h>
490Sstevel@tonic-gate #include <sys/reboot.h>
500Sstevel@tonic-gate #include <sys/uadmin.h>
510Sstevel@tonic-gate #include <sys/vfs.h>
520Sstevel@tonic-gate #include <sys/vnode.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate #include <sys/session.h>
550Sstevel@tonic-gate #include <sys/ucontext.h>
560Sstevel@tonic-gate #include <sys/dnlc.h>
570Sstevel@tonic-gate #include <sys/var.h>
580Sstevel@tonic-gate #include <sys/cmn_err.h>
590Sstevel@tonic-gate #include <sys/debugreg.h>
600Sstevel@tonic-gate #include <sys/thread.h>
610Sstevel@tonic-gate #include <sys/vtrace.h>
620Sstevel@tonic-gate #include <sys/consdev.h>
630Sstevel@tonic-gate #include <sys/psw.h>
640Sstevel@tonic-gate #include <sys/regset.h>
650Sstevel@tonic-gate #include <sys/privregs.h>
660Sstevel@tonic-gate #include <sys/stack.h>
670Sstevel@tonic-gate #include <sys/swap.h>
680Sstevel@tonic-gate #include <vm/hat.h>
690Sstevel@tonic-gate #include <vm/anon.h>
700Sstevel@tonic-gate #include <vm/as.h>
710Sstevel@tonic-gate #include <vm/page.h>
720Sstevel@tonic-gate #include <vm/seg.h>
730Sstevel@tonic-gate #include <vm/seg_kmem.h>
740Sstevel@tonic-gate #include <vm/seg_map.h>
750Sstevel@tonic-gate #include <vm/seg_vn.h>
760Sstevel@tonic-gate #include <sys/exec.h>
770Sstevel@tonic-gate #include <sys/acct.h>
780Sstevel@tonic-gate #include <sys/core.h>
790Sstevel@tonic-gate #include <sys/corectl.h>
800Sstevel@tonic-gate #include <sys/modctl.h>
810Sstevel@tonic-gate #include <sys/tuneable.h>
820Sstevel@tonic-gate #include <c2/audit.h>
830Sstevel@tonic-gate #include <sys/bootconf.h>
840Sstevel@tonic-gate #include <sys/dumphdr.h>
850Sstevel@tonic-gate #include <sys/promif.h>
860Sstevel@tonic-gate #include <sys/systeminfo.h>
870Sstevel@tonic-gate #include <sys/kdi.h>
880Sstevel@tonic-gate #include <sys/contract_impl.h>
890Sstevel@tonic-gate #include <sys/x86_archext.h>
900Sstevel@tonic-gate #include <sys/segments.h>
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Compare the version of boot that boot says it is against
940Sstevel@tonic-gate  * the version of boot the kernel expects.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate int
970Sstevel@tonic-gate check_boot_version(int boots_version)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	if (boots_version == BO_VERSION)
1000Sstevel@tonic-gate 		return (0);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	prom_printf("Wrong boot interface - kernel needs v%d found v%d\n",
1030Sstevel@tonic-gate 	    BO_VERSION, boots_version);
1040Sstevel@tonic-gate 	prom_panic("halting");
1050Sstevel@tonic-gate 	/*NOTREACHED*/
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Process the physical installed list for boot.
1100Sstevel@tonic-gate  * Finds:
1110Sstevel@tonic-gate  * 1) the pfn of the highest installed physical page,
1120Sstevel@tonic-gate  * 2) the number of pages installed
1130Sstevel@tonic-gate  * 3) the number of distinct contiguous regions these pages fall into.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate void
1160Sstevel@tonic-gate installed_top_size(
1170Sstevel@tonic-gate 	struct memlist *list,	/* pointer to start of installed list */
1180Sstevel@tonic-gate 	pfn_t *high_pfn,	/* return ptr for top value */
1190Sstevel@tonic-gate 	pgcnt_t *pgcnt,		/* return ptr for sum of installed pages */
1200Sstevel@tonic-gate 	int	*ranges)	/* return ptr for the count of contig. ranges */
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	pfn_t top = 0;
1230Sstevel@tonic-gate 	pgcnt_t sumpages = 0;
1240Sstevel@tonic-gate 	pfn_t highp;		/* high page in a chunk */
1250Sstevel@tonic-gate 	int cnt = 0;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	for (; list; list = list->next) {
1280Sstevel@tonic-gate 		++cnt;
1290Sstevel@tonic-gate 		highp = (list->address + list->size - 1) >> PAGESHIFT;
1300Sstevel@tonic-gate 		if (top < highp)
1310Sstevel@tonic-gate 			top = highp;
1320Sstevel@tonic-gate 		sumpages += btop(list->size);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	*high_pfn = top;
1360Sstevel@tonic-gate 	*pgcnt = sumpages;
1370Sstevel@tonic-gate 	*ranges = cnt;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Copy in a memory list from boot to kernel, with a filter function
1420Sstevel@tonic-gate  * to remove pages. The filter function can increase the address and/or
1430Sstevel@tonic-gate  * decrease the size to filter out pages.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate void
1460Sstevel@tonic-gate copy_memlist_filter(
1470Sstevel@tonic-gate 	struct memlist *src,
1480Sstevel@tonic-gate 	struct memlist **dstp,
1490Sstevel@tonic-gate 	void (*filter)(uint64_t *, uint64_t *))
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	struct memlist *dst, *prev;
1520Sstevel@tonic-gate 	uint64_t addr;
1530Sstevel@tonic-gate 	uint64_t size;
1540Sstevel@tonic-gate 	uint64_t eaddr;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	dst = *dstp;
1570Sstevel@tonic-gate 	prev = dst;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * Move through the memlist applying a filter against
1610Sstevel@tonic-gate 	 * each range of memory. Note that we may apply the
1620Sstevel@tonic-gate 	 * filter multiple times against each memlist entry.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	for (; src; src = src->next) {
1650Sstevel@tonic-gate 		addr = src->address;
1660Sstevel@tonic-gate 		eaddr = addr + src->size;
1670Sstevel@tonic-gate 		while (addr < eaddr) {
1680Sstevel@tonic-gate 			size = eaddr - addr;
1690Sstevel@tonic-gate 			if (filter != NULL)
1700Sstevel@tonic-gate 				filter(&addr, &size);
1710Sstevel@tonic-gate 			if (size == 0)
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			dst->address = addr;
1740Sstevel@tonic-gate 			dst->size = size;
1750Sstevel@tonic-gate 			dst->next = 0;
1760Sstevel@tonic-gate 			if (prev == dst) {
1770Sstevel@tonic-gate 				dst->prev = 0;
1780Sstevel@tonic-gate 				dst++;
1790Sstevel@tonic-gate 			} else {
1800Sstevel@tonic-gate 				dst->prev = prev;
1810Sstevel@tonic-gate 				prev->next = dst;
1820Sstevel@tonic-gate 				dst++;
1830Sstevel@tonic-gate 				prev++;
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			addr += size;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	*dstp = dst;
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * Kernel setup code, called from startup().
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate void
1960Sstevel@tonic-gate kern_setup1(void)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	proc_t *pp;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	pp = &p0;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	proc_sched = pp;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Initialize process 0 data structures
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	pp->p_stat = SRUN;
2080Sstevel@tonic-gate 	pp->p_flag = SSYS;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	pp->p_pidp = &pid0;
2110Sstevel@tonic-gate 	pp->p_pgidp = &pid0;
2120Sstevel@tonic-gate 	pp->p_sessp = &session0;
2130Sstevel@tonic-gate 	pp->p_tlist = &t0;
2140Sstevel@tonic-gate 	pid0.pid_pglink = pp;
215*749Ssusans 	pid0.pid_pgtail = pp;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * XXX - we asssume that the u-area is zeroed out except for
2190Sstevel@tonic-gate 	 * ttolwp(curthread)->lwp_regs.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	u.u_cmask = (mode_t)CMASK;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	thread_init();		/* init thread_free list */
2240Sstevel@tonic-gate 	pid_init();		/* initialize pid (proc) table */
2250Sstevel@tonic-gate 	contract_init();	/* initialize contracts */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	init_pages_pp_maximum();
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Load a procedure into a thread.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate void
2340Sstevel@tonic-gate thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	caddr_t sp;
2370Sstevel@tonic-gate 	size_t framesz;
2380Sstevel@tonic-gate 	caddr_t argp;
2390Sstevel@tonic-gate 	long *p;
2400Sstevel@tonic-gate 	extern void thread_start();
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Push a "c" call frame onto the stack to represent
2440Sstevel@tonic-gate 	 * the caller of "start".
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	sp = t->t_stk;
2470Sstevel@tonic-gate 	ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0);
2480Sstevel@tonic-gate 	if (len != 0) {
2490Sstevel@tonic-gate 		/*
2500Sstevel@tonic-gate 		 * the object that arg points at is copied into the
2510Sstevel@tonic-gate 		 * caller's frame.
2520Sstevel@tonic-gate 		 */
2530Sstevel@tonic-gate 		framesz = SA(len);
2540Sstevel@tonic-gate 		sp -= framesz;
2550Sstevel@tonic-gate 		ASSERT(sp > t->t_stkbase);
2560Sstevel@tonic-gate 		argp = sp + SA(MINFRAME);
2570Sstevel@tonic-gate 		bcopy(arg, argp, len);
2580Sstevel@tonic-gate 		arg = argp;
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Set up arguments (arg and len) on the caller's stack frame.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	p = (long *)sp;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	*--p = 0;		/* fake call */
2660Sstevel@tonic-gate 	*--p = 0;		/* null frame pointer terminates stack trace */
2670Sstevel@tonic-gate 	*--p = (long)len;
2680Sstevel@tonic-gate 	*--p = (intptr_t)arg;
2690Sstevel@tonic-gate 	*--p = (intptr_t)start;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/*
2720Sstevel@tonic-gate 	 * initialize thread to resume at thread_start() which will
2730Sstevel@tonic-gate 	 * turn around and invoke (*start)(arg, len).
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	t->t_pc = (uintptr_t)thread_start;
2760Sstevel@tonic-gate 	t->t_sp = (uintptr_t)p;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * load user registers into lwp.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate /*ARGSUSED2*/
2850Sstevel@tonic-gate void
2860Sstevel@tonic-gate lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	setgregs(lwp, grp);
2910Sstevel@tonic-gate 	rp->r_ps = PSL_USER;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * For 64-bit lwps, we allow one magic %fs selector value, and one
2950Sstevel@tonic-gate 	 * magic %gs selector to point anywhere in the address space using
2960Sstevel@tonic-gate 	 * %fsbase and %gsbase behind the scenes.  libc uses %fs to point
2970Sstevel@tonic-gate 	 * at the ulwp_t structure.
2980Sstevel@tonic-gate 	 *
2990Sstevel@tonic-gate 	 * For 32-bit lwps, libc wedges its lwp thread pointer into the
3000Sstevel@tonic-gate 	 * ucontext ESP slot (which is otherwise irrelevant to setting a
3010Sstevel@tonic-gate 	 * ucontext) and LWPGS_SEL value into gregs[REG_GS].  This is so
3020Sstevel@tonic-gate 	 * syslwp_create() can atomically setup %gs.
3030Sstevel@tonic-gate 	 *
3040Sstevel@tonic-gate 	 * See setup_context() in libc.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3070Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) {
3080Sstevel@tonic-gate 		if (grp[REG_GS] == LWPGS_SEL)
3090Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate #else
3120Sstevel@tonic-gate 	if (grp[GS] == LWPGS_SEL)
3130Sstevel@tonic-gate 		(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
3140Sstevel@tonic-gate #endif
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
3170Sstevel@tonic-gate 	lwptot(lwp)->t_post_sys = 1;
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * set syscall()'s return values for a lwp.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate void
3240Sstevel@tonic-gate lwp_setrval(klwp_t *lwp, int v1, int v2)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	lwptoregs(lwp)->r_ps &= ~PS_C;
3270Sstevel@tonic-gate 	lwptoregs(lwp)->r_r0 = v1;
3280Sstevel@tonic-gate 	lwptoregs(lwp)->r_r1 = v2;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * set syscall()'s return values for a lwp.
3330Sstevel@tonic-gate  */
3340Sstevel@tonic-gate void
3350Sstevel@tonic-gate lwp_setsp(klwp_t *lwp, caddr_t sp)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	lwptoregs(lwp)->r_sp = (intptr_t)sp;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * Copy regs from parent to child.
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate void
3440Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate #if defined(__amd64)
3470Sstevel@tonic-gate 	clwp->lwp_pcb.pcb_flags |= RUPDATE_PENDING;
3480Sstevel@tonic-gate 	lwptot(clwp)->t_post_sys = 1;
3490Sstevel@tonic-gate #endif
3500Sstevel@tonic-gate 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs));
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * This function is currently unused on x86.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate /*ARGSUSED*/
3570Sstevel@tonic-gate void
3580Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec)
3590Sstevel@tonic-gate {}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * This function is currently unused on x86.
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate void
3650Sstevel@tonic-gate lwp_pcb_exit(void)
3660Sstevel@tonic-gate {}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * Lwp context ops for segment registers.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate  * Every time we come into the kernel (syscall, interrupt or trap
3740Sstevel@tonic-gate  * but not fast-traps) we capture the current values of the user's
3750Sstevel@tonic-gate  * segment registers into the lwp's reg structure. This includes
3760Sstevel@tonic-gate  * lcall for i386 generic system call support since it is handled
3770Sstevel@tonic-gate  * as a segment-not-present trap.
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * Here we save the current values from the lwp regs into the pcb
3800Sstevel@tonic-gate  * and set the RUPDATE_PENDING bit to tell the rest of the kernel
3810Sstevel@tonic-gate  * that the pcb copy of the segment registers is the current one.
3820Sstevel@tonic-gate  * This ensures the lwp's next trip to user land via update_sregs.
3830Sstevel@tonic-gate  * Finally we set t_post_sys to ensure that no system call fast-path's
3840Sstevel@tonic-gate  * its way out of the kernel via sysret.
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * (This means that we need to have interrupts disabled when we test
3870Sstevel@tonic-gate  * t->t_post_sys in the syscall handlers; if the test fails, we need
3880Sstevel@tonic-gate  * to keep interrupts disabled until we return to userland so we can't
3890Sstevel@tonic-gate  * be switched away.)
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * As a result of all this, we don't really have to do a whole lot if
3920Sstevel@tonic-gate  * the thread is just mucking about in the kernel, switching on and
3930Sstevel@tonic-gate  * off the cpu for whatever reason it feels like. And yet we still
3940Sstevel@tonic-gate  * preserve fast syscalls, cause if we -don't- get descheduled,
3950Sstevel@tonic-gate  * we never come here either.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate #define	VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \
3990Sstevel@tonic-gate 	    (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL)
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate void
4020Sstevel@tonic-gate lwp_segregs_save(klwp_t *lwp)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate #if defined(__amd64)
4050Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
4060Sstevel@tonic-gate 	struct regs *rp;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
4090Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) {
4120Sstevel@tonic-gate 		rp = lwptoregs(lwp);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		/*
4150Sstevel@tonic-gate 		 * If there's no update already pending, capture the current
4160Sstevel@tonic-gate 		 * %ds/%es/%fs/%gs values from lwp's regs in case the user
4170Sstevel@tonic-gate 		 * changed them; %fsbase and %gsbase are privileged so the
4180Sstevel@tonic-gate 		 * kernel versions of these registers in pcb_fsbase and
4190Sstevel@tonic-gate 		 * pcb_gsbase are always up-to-date.
4200Sstevel@tonic-gate 		 */
4210Sstevel@tonic-gate 		pcb->pcb_ds = rp->r_ds;
4220Sstevel@tonic-gate 		pcb->pcb_es = rp->r_es;
4230Sstevel@tonic-gate 		pcb->pcb_fs = rp->r_fs;
4240Sstevel@tonic-gate 		pcb->pcb_gs = rp->r_gs;
4250Sstevel@tonic-gate 		pcb->pcb_flags |= RUPDATE_PENDING;
4260Sstevel@tonic-gate 		lwp->lwp_thread->t_post_sys = 1;
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate #endif	/* __amd64 */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc,
4310Sstevel@tonic-gate 	    sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0);
4320Sstevel@tonic-gate 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc,
4330Sstevel@tonic-gate 	    sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * Restore lwp private fs and gs segment descriptors
4380Sstevel@tonic-gate  * on current cpu's GDT.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate static void
4410Sstevel@tonic-gate lwp_segregs_restore(klwp_t *lwp)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	cpu_t *cpu = CPU;
4440Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
4470Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
4500Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate #if defined(__amd64)
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * Make it impossible for a process to change its data model.
4550Sstevel@tonic-gate 	 * We do this by toggling the present bits for the 32 and
4560Sstevel@tonic-gate 	 * 64-bit user code descriptors. That way if a user lwp attempts
4570Sstevel@tonic-gate 	 * to change its data model (by using the wrong code descriptor in
4580Sstevel@tonic-gate 	 * %cs) it will fault immediately. This also allows us to simplify
4590Sstevel@tonic-gate 	 * assertions and checks in the kernel.
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_UCODE].usd_p = 1;
4620Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_U32CODE].usd_p = 0;
4630Sstevel@tonic-gate #endif	/* __amd64 */
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate static void
4690Sstevel@tonic-gate lwp_segregs_restore32(klwp_t *lwp)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	cpu_t *cpu = CPU;
4720Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
4750Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
4780Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
4790Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_UCODE].usd_p = 0;
4800Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_U32CODE].usd_p = 1;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * Add any lwp-associated context handlers to the lwp at the beginning
4870Sstevel@tonic-gate  * of the lwp's useful life.
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * All paths which create lwp's invoke lwp_create(); lwp_create()
4900Sstevel@tonic-gate  * invokes lwp_stk_init() which initializes the stack, sets up
4910Sstevel@tonic-gate  * lwp_regs, and invokes this routine.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * All paths which destroy lwp's invoke lwp_exit() to rip the lwp
4940Sstevel@tonic-gate  * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it
4950Sstevel@tonic-gate  * ends up in thread_free() which invokes freectx(t, 0) before
4960Sstevel@tonic-gate  * invoking lwp_stk_fini().  When the lwp is recycled from death
4970Sstevel@tonic-gate  * row, lwp_stk_fini() is invoked, then thread_free(), and thus
4980Sstevel@tonic-gate  * freectx(t, 0) as before.
4990Sstevel@tonic-gate  *
5000Sstevel@tonic-gate  * In the case of exec, the surviving lwp is thoroughly scrubbed
5010Sstevel@tonic-gate  * clean; exec invokes freectx(t, 1) to destroy associated contexts.
5020Sstevel@tonic-gate  * On the way back to the new image, it invokes setregs() which
5030Sstevel@tonic-gate  * in turn invokes this routine.
5040Sstevel@tonic-gate  */
5050Sstevel@tonic-gate void
5060Sstevel@tonic-gate lwp_installctx(klwp_t *lwp)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	kthread_t *t = lwptot(lwp);
5090Sstevel@tonic-gate 	int thisthread = t == curthread;
5100Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5110Sstevel@tonic-gate 	void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
5120Sstevel@tonic-gate 	    lwp_segregs_restore : lwp_segregs_restore32;
5130Sstevel@tonic-gate #else
5140Sstevel@tonic-gate 	void (*restop)(klwp_t *) = lwp_segregs_restore;
5150Sstevel@tonic-gate #endif
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Install the basic lwp context handlers on each lwp.
5190Sstevel@tonic-gate 	 *
5200Sstevel@tonic-gate 	 * On the amd64 kernel, the context handlers are responsible for
5210Sstevel@tonic-gate 	 * virtualizing %ds, %es, %fs, and %gs to the lwp.  The register
5220Sstevel@tonic-gate 	 * values are only ever changed via sys_rtt when the
5230Sstevel@tonic-gate 	 * RUPDATE_PENDING bit is set.  Only sys_rtt gets to clear the bit.
5240Sstevel@tonic-gate 	 *
5250Sstevel@tonic-gate 	 * On the i386 kernel, the context handlers are responsible for
5260Sstevel@tonic-gate 	 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
5290Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL) == 0);
5300Sstevel@tonic-gate 	if (thisthread)
5310Sstevel@tonic-gate 		kpreempt_disable();
5320Sstevel@tonic-gate 	installctx(t, lwp, lwp_segregs_save, restop,
5330Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL);
5340Sstevel@tonic-gate 	if (thisthread) {
5350Sstevel@tonic-gate 		/*
5360Sstevel@tonic-gate 		 * Since we're the right thread, set the values in the GDT
5370Sstevel@tonic-gate 		 */
5380Sstevel@tonic-gate 		restop(lwp);
5390Sstevel@tonic-gate 		kpreempt_enable();
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * If we have sysenter/sysexit instructions enabled, we need
5440Sstevel@tonic-gate 	 * to ensure that the hardware mechanism is kept up-to-date with the
5450Sstevel@tonic-gate 	 * lwp's kernel stack pointer across context switches.
5460Sstevel@tonic-gate 	 *
5470Sstevel@tonic-gate 	 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
5480Sstevel@tonic-gate 	 * it to the lwp's kernel stack pointer (kstktop).
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	if (x86_feature & X86_SEP) {
5510Sstevel@tonic-gate #if defined(__amd64)
5520Sstevel@tonic-gate 		caddr_t kstktop = (caddr_t)lwp->lwp_regs;
5530Sstevel@tonic-gate #elif defined(__i386)
5540Sstevel@tonic-gate 		caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
5550Sstevel@tonic-gate 		    SA(sizeof (struct regs) + MINFRAME);
5560Sstevel@tonic-gate #endif
5570Sstevel@tonic-gate 		ASSERT(removectx(t, kstktop,
5580Sstevel@tonic-gate 		    sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		if (thisthread)
5610Sstevel@tonic-gate 			kpreempt_disable();
5620Sstevel@tonic-gate 		installctx(t, kstktop,
5630Sstevel@tonic-gate 		    sep_save, sep_restore, NULL, NULL, NULL, NULL);
5640Sstevel@tonic-gate 		if (thisthread) {
5650Sstevel@tonic-gate 			/*
5660Sstevel@tonic-gate 			 * We're the right thread, so set the stack pointer
5670Sstevel@tonic-gate 			 * for the first sysenter instruction to use
5680Sstevel@tonic-gate 			 */
5690Sstevel@tonic-gate 			sep_restore(kstktop);
5700Sstevel@tonic-gate 			kpreempt_enable();
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate  * Clear registers on exec(2).
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate void
5790Sstevel@tonic-gate setregs(uarg_t *args)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	struct regs *rp;
5820Sstevel@tonic-gate 	kthread_t *t = curthread;
5830Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5840Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
5850Sstevel@tonic-gate 	greg_t sp;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Initialize user registers
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	(void) save_syscall_args();	/* copy args from registers first */
5910Sstevel@tonic-gate 	rp = lwptoregs(lwp);
5920Sstevel@tonic-gate 	sp = rp->r_sp;
5930Sstevel@tonic-gate 	bzero(rp, sizeof (*rp));
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	rp->r_ss = UDS_SEL;
5960Sstevel@tonic-gate 	rp->r_sp = sp;
5970Sstevel@tonic-gate 	rp->r_pc = args->entry;
5980Sstevel@tonic-gate 	rp->r_ps = PSL_USER;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate #if defined(__amd64)
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	pcb->pcb_fs = pcb->pcb_gs = 0;
6030Sstevel@tonic-gate 	pcb->pcb_fsbase = pcb->pcb_gsbase = 0;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (ttoproc(t)->p_model == DATAMODEL_NATIVE) {
6060Sstevel@tonic-gate 		cpu_t *cpu;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * Only allow 64-bit user code descriptor to be present.
6120Sstevel@tonic-gate 		 */
6130Sstevel@tonic-gate 		kpreempt_disable();
6140Sstevel@tonic-gate 		cpu = CPU;
6150Sstevel@tonic-gate 		cpu->cpu_gdt[GDT_UCODE].usd_p = 1;
6160Sstevel@tonic-gate 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 0;
6170Sstevel@tonic-gate 		kpreempt_enable();
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * Arrange that the virtualized %fs and %gs GDT descriptors
6210Sstevel@tonic-gate 		 * have a well-defined initial state (present, ring 3
6220Sstevel@tonic-gate 		 * and of type data).
6230Sstevel@tonic-gate 		 */
6240Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * thrptr is either NULL or a value used by DTrace.
6280Sstevel@tonic-gate 		 * 64-bit processes use %fs as their "thread" register.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		if (args->thrptr)
6310Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	} else {
6340Sstevel@tonic-gate 		cpu_t *cpu;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		rp->r_cs = U32CS_SEL;
6370Sstevel@tonic-gate 		rp->r_ds = rp->r_es = UDS_SEL;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * only allow 32-bit user code selector to be present.
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate 		kpreempt_disable();
6430Sstevel@tonic-gate 		cpu = CPU;
6440Sstevel@tonic-gate 		cpu->cpu_gdt[GDT_UCODE].usd_p = 0;
6450Sstevel@tonic-gate 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 1;
6460Sstevel@tonic-gate 		kpreempt_enable();
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * thrptr is either NULL or a value used by DTrace.
6520Sstevel@tonic-gate 		 * 32-bit processes use %gs as their "thread" register.
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 		if (args->thrptr)
6550Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	pcb->pcb_ds = rp->r_ds;
6610Sstevel@tonic-gate 	pcb->pcb_es = rp->r_es;
6620Sstevel@tonic-gate 	pcb->pcb_flags |= RUPDATE_PENDING;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate #elif defined(__i386)
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	rp->r_cs = UCS_SEL;
6670Sstevel@tonic-gate 	rp->r_ds = rp->r_es = UDS_SEL;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
6710Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
6720Sstevel@tonic-gate 	 * and of type data).
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * For %gs we need to reset LWP_GSBASE in pcb and the
6790Sstevel@tonic-gate 	 * per-cpu GDT descriptor. thrptr is either NULL
6800Sstevel@tonic-gate 	 * or a value used by DTrace.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	if (args->thrptr)
6830Sstevel@tonic-gate 		(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
6840Sstevel@tonic-gate #endif
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
6870Sstevel@tonic-gate 	t->t_post_sys = 1;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * Here we initialize minimal fpu state.
6910Sstevel@tonic-gate 	 * The rest is done at the first floating
6920Sstevel@tonic-gate 	 * point instruction that a process executes.
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	pcb->pcb_fpu.fpu_flags = 0;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * Add the lwp context handlers that virtualize segment registers,
6980Sstevel@tonic-gate 	 * and/or system call stacks etc.
6990Sstevel@tonic-gate 	 */
7000Sstevel@tonic-gate 	lwp_installctx(lwp);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate #if !defined(lwp_getdatamodel)
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * Return the datamodel of the given lwp.
7070Sstevel@tonic-gate  */
7080Sstevel@tonic-gate /*ARGSUSED*/
7090Sstevel@tonic-gate model_t
7100Sstevel@tonic-gate lwp_getdatamodel(klwp_t *lwp)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	return (lwp->lwp_procp->p_model);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate #endif	/* !lwp_getdatamodel */
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate #if !defined(get_udatamodel)
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate model_t
7200Sstevel@tonic-gate get_udatamodel(void)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	return (curproc->p_model);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate #endif	/* !get_udatamodel */
726