xref: /onnv-gate/usr/src/uts/intel/ia32/os/sundep.c (revision 3446)
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
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
280Sstevel@tonic-gate /*	All Rights Reserved   */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/mman.h>
390Sstevel@tonic-gate #include <sys/class.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/procfs.h>
420Sstevel@tonic-gate #include <sys/buf.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/cred.h>
450Sstevel@tonic-gate #include <sys/archsystm.h>
460Sstevel@tonic-gate #include <sys/vmparam.h>
470Sstevel@tonic-gate #include <sys/prsystm.h>
480Sstevel@tonic-gate #include <sys/reboot.h>
490Sstevel@tonic-gate #include <sys/uadmin.h>
500Sstevel@tonic-gate #include <sys/vfs.h>
510Sstevel@tonic-gate #include <sys/vnode.h>
520Sstevel@tonic-gate #include <sys/file.h>
530Sstevel@tonic-gate #include <sys/session.h>
540Sstevel@tonic-gate #include <sys/ucontext.h>
550Sstevel@tonic-gate #include <sys/dnlc.h>
560Sstevel@tonic-gate #include <sys/var.h>
570Sstevel@tonic-gate #include <sys/cmn_err.h>
580Sstevel@tonic-gate #include <sys/debugreg.h>
590Sstevel@tonic-gate #include <sys/thread.h>
600Sstevel@tonic-gate #include <sys/vtrace.h>
610Sstevel@tonic-gate #include <sys/consdev.h>
620Sstevel@tonic-gate #include <sys/psw.h>
630Sstevel@tonic-gate #include <sys/regset.h>
640Sstevel@tonic-gate #include <sys/privregs.h>
650Sstevel@tonic-gate #include <sys/stack.h>
660Sstevel@tonic-gate #include <sys/swap.h>
670Sstevel@tonic-gate #include <vm/hat.h>
680Sstevel@tonic-gate #include <vm/anon.h>
690Sstevel@tonic-gate #include <vm/as.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate #include <vm/seg.h>
720Sstevel@tonic-gate #include <vm/seg_kmem.h>
730Sstevel@tonic-gate #include <vm/seg_map.h>
740Sstevel@tonic-gate #include <vm/seg_vn.h>
750Sstevel@tonic-gate #include <sys/exec.h>
760Sstevel@tonic-gate #include <sys/acct.h>
770Sstevel@tonic-gate #include <sys/core.h>
780Sstevel@tonic-gate #include <sys/corectl.h>
790Sstevel@tonic-gate #include <sys/modctl.h>
800Sstevel@tonic-gate #include <sys/tuneable.h>
810Sstevel@tonic-gate #include <c2/audit.h>
820Sstevel@tonic-gate #include <sys/bootconf.h>
832712Snn35248 #include <sys/brand.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>
91*3446Smrj #include <sys/ontrap.h>
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Compare the version of boot that boot says it is against
950Sstevel@tonic-gate  * the version of boot the kernel expects.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
980Sstevel@tonic-gate check_boot_version(int boots_version)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	if (boots_version == BO_VERSION)
1010Sstevel@tonic-gate 		return (0);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	prom_printf("Wrong boot interface - kernel needs v%d found v%d\n",
1040Sstevel@tonic-gate 	    BO_VERSION, boots_version);
1050Sstevel@tonic-gate 	prom_panic("halting");
1060Sstevel@tonic-gate 	/*NOTREACHED*/
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Process the physical installed list for boot.
1110Sstevel@tonic-gate  * Finds:
1120Sstevel@tonic-gate  * 1) the pfn of the highest installed physical page,
1130Sstevel@tonic-gate  * 2) the number of pages installed
1140Sstevel@tonic-gate  * 3) the number of distinct contiguous regions these pages fall into.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate void
1170Sstevel@tonic-gate installed_top_size(
1180Sstevel@tonic-gate 	struct memlist *list,	/* pointer to start of installed list */
1190Sstevel@tonic-gate 	pfn_t *high_pfn,	/* return ptr for top value */
1200Sstevel@tonic-gate 	pgcnt_t *pgcnt,		/* return ptr for sum of installed pages */
1210Sstevel@tonic-gate 	int	*ranges)	/* return ptr for the count of contig. ranges */
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	pfn_t top = 0;
1240Sstevel@tonic-gate 	pgcnt_t sumpages = 0;
1250Sstevel@tonic-gate 	pfn_t highp;		/* high page in a chunk */
1260Sstevel@tonic-gate 	int cnt = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	for (; list; list = list->next) {
1290Sstevel@tonic-gate 		++cnt;
1300Sstevel@tonic-gate 		highp = (list->address + list->size - 1) >> PAGESHIFT;
1310Sstevel@tonic-gate 		if (top < highp)
1320Sstevel@tonic-gate 			top = highp;
1330Sstevel@tonic-gate 		sumpages += btop(list->size);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	*high_pfn = top;
1370Sstevel@tonic-gate 	*pgcnt = sumpages;
1380Sstevel@tonic-gate 	*ranges = cnt;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Copy in a memory list from boot to kernel, with a filter function
1430Sstevel@tonic-gate  * to remove pages. The filter function can increase the address and/or
144842Smec  * decrease the size to filter out pages.  It will also align addresses and
145842Smec  * sizes to PAGESIZE.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate void
1480Sstevel@tonic-gate copy_memlist_filter(
1490Sstevel@tonic-gate 	struct memlist *src,
1500Sstevel@tonic-gate 	struct memlist **dstp,
1510Sstevel@tonic-gate 	void (*filter)(uint64_t *, uint64_t *))
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	struct memlist *dst, *prev;
1540Sstevel@tonic-gate 	uint64_t addr;
1550Sstevel@tonic-gate 	uint64_t size;
1560Sstevel@tonic-gate 	uint64_t eaddr;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	dst = *dstp;
1590Sstevel@tonic-gate 	prev = dst;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * Move through the memlist applying a filter against
1630Sstevel@tonic-gate 	 * each range of memory. Note that we may apply the
1640Sstevel@tonic-gate 	 * filter multiple times against each memlist entry.
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	for (; src; src = src->next) {
167842Smec 		addr = P2ROUNDUP(src->address, PAGESIZE);
168842Smec 		eaddr = P2ALIGN(src->address + src->size, PAGESIZE);
1690Sstevel@tonic-gate 		while (addr < eaddr) {
1700Sstevel@tonic-gate 			size = eaddr - addr;
1710Sstevel@tonic-gate 			if (filter != NULL)
1720Sstevel@tonic-gate 				filter(&addr, &size);
1730Sstevel@tonic-gate 			if (size == 0)
1740Sstevel@tonic-gate 				break;
1750Sstevel@tonic-gate 			dst->address = addr;
1760Sstevel@tonic-gate 			dst->size = size;
1770Sstevel@tonic-gate 			dst->next = 0;
1780Sstevel@tonic-gate 			if (prev == dst) {
1790Sstevel@tonic-gate 				dst->prev = 0;
1800Sstevel@tonic-gate 				dst++;
1810Sstevel@tonic-gate 			} else {
1820Sstevel@tonic-gate 				dst->prev = prev;
1830Sstevel@tonic-gate 				prev->next = dst;
1840Sstevel@tonic-gate 				dst++;
1850Sstevel@tonic-gate 				prev++;
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			addr += size;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	*dstp = dst;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Kernel setup code, called from startup().
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate void
1980Sstevel@tonic-gate kern_setup1(void)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	proc_t *pp;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	pp = &p0;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	proc_sched = pp;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * Initialize process 0 data structures
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	pp->p_stat = SRUN;
2100Sstevel@tonic-gate 	pp->p_flag = SSYS;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	pp->p_pidp = &pid0;
2130Sstevel@tonic-gate 	pp->p_pgidp = &pid0;
2140Sstevel@tonic-gate 	pp->p_sessp = &session0;
2150Sstevel@tonic-gate 	pp->p_tlist = &t0;
2160Sstevel@tonic-gate 	pid0.pid_pglink = pp;
217749Ssusans 	pid0.pid_pgtail = pp;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * XXX - we asssume that the u-area is zeroed out except for
2210Sstevel@tonic-gate 	 * ttolwp(curthread)->lwp_regs.
2220Sstevel@tonic-gate 	 */
223*3446Smrj 	PTOU(curproc)->u_cmask = (mode_t)CMASK;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	thread_init();		/* init thread_free list */
2260Sstevel@tonic-gate 	pid_init();		/* initialize pid (proc) table */
2270Sstevel@tonic-gate 	contract_init();	/* initialize contracts */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	init_pages_pp_maximum();
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * Load a procedure into a thread.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate void
2360Sstevel@tonic-gate thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	caddr_t sp;
2390Sstevel@tonic-gate 	size_t framesz;
2400Sstevel@tonic-gate 	caddr_t argp;
2410Sstevel@tonic-gate 	long *p;
2420Sstevel@tonic-gate 	extern void thread_start();
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Push a "c" call frame onto the stack to represent
2460Sstevel@tonic-gate 	 * the caller of "start".
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	sp = t->t_stk;
2490Sstevel@tonic-gate 	ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0);
2500Sstevel@tonic-gate 	if (len != 0) {
2510Sstevel@tonic-gate 		/*
2520Sstevel@tonic-gate 		 * the object that arg points at is copied into the
2530Sstevel@tonic-gate 		 * caller's frame.
2540Sstevel@tonic-gate 		 */
2550Sstevel@tonic-gate 		framesz = SA(len);
2560Sstevel@tonic-gate 		sp -= framesz;
2570Sstevel@tonic-gate 		ASSERT(sp > t->t_stkbase);
2580Sstevel@tonic-gate 		argp = sp + SA(MINFRAME);
2590Sstevel@tonic-gate 		bcopy(arg, argp, len);
2600Sstevel@tonic-gate 		arg = argp;
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Set up arguments (arg and len) on the caller's stack frame.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	p = (long *)sp;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	*--p = 0;		/* fake call */
2680Sstevel@tonic-gate 	*--p = 0;		/* null frame pointer terminates stack trace */
2690Sstevel@tonic-gate 	*--p = (long)len;
2700Sstevel@tonic-gate 	*--p = (intptr_t)arg;
2710Sstevel@tonic-gate 	*--p = (intptr_t)start;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * initialize thread to resume at thread_start() which will
2750Sstevel@tonic-gate 	 * turn around and invoke (*start)(arg, len).
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	t->t_pc = (uintptr_t)thread_start;
2780Sstevel@tonic-gate 	t->t_sp = (uintptr_t)p;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * load user registers into lwp.
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate /*ARGSUSED2*/
2870Sstevel@tonic-gate void
2880Sstevel@tonic-gate lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	setgregs(lwp, grp);
2930Sstevel@tonic-gate 	rp->r_ps = PSL_USER;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
296*3446Smrj 	 * For 64-bit lwps, we allow null %fs selector value, and null
297*3446Smrj 	 * %gs selector to point anywhere in the address space using
2980Sstevel@tonic-gate 	 * %fsbase and %gsbase behind the scenes.  libc uses %fs to point
2990Sstevel@tonic-gate 	 * at the ulwp_t structure.
3000Sstevel@tonic-gate 	 *
3010Sstevel@tonic-gate 	 * For 32-bit lwps, libc wedges its lwp thread pointer into the
3020Sstevel@tonic-gate 	 * ucontext ESP slot (which is otherwise irrelevant to setting a
3030Sstevel@tonic-gate 	 * ucontext) and LWPGS_SEL value into gregs[REG_GS].  This is so
3040Sstevel@tonic-gate 	 * syslwp_create() can atomically setup %gs.
3050Sstevel@tonic-gate 	 *
3060Sstevel@tonic-gate 	 * See setup_context() in libc.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3090Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) {
3100Sstevel@tonic-gate 		if (grp[REG_GS] == LWPGS_SEL)
3110Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
312*3446Smrj 	} else {
313*3446Smrj 		/*
314*3446Smrj 		 * See lwp_setprivate in kernel and setup_context in libc.
315*3446Smrj 		 *
316*3446Smrj 		 * Currently libc constructs a ucontext from whole cloth for
317*3446Smrj 		 * every new (not main) lwp created.  For 64 bit processes
318*3446Smrj 		 * %fsbase is directly set to point to current thread pointer.
319*3446Smrj 		 * In the past (solaris 10) %fs was also set LWPFS_SEL to
320*3446Smrj 		 * indicate %fsbase. Now we use the null GDT selector for
321*3446Smrj 		 * this purpose. LWP[FS|GS]_SEL are only intended for 32 bit
322*3446Smrj 		 * processes. To ease transition we support older libcs in
323*3446Smrj 		 * the newer kernel by forcing %fs or %gs selector to null
324*3446Smrj 		 * by calling lwp_setprivate if LWP[FS|GS]_SEL is passed in
325*3446Smrj 		 * the ucontext.  This is should be ripped out at some future
326*3446Smrj 		 * date.  Another fix would be for libc to do a getcontext
327*3446Smrj 		 * and inherit the null %fs/%gs from the current context but
328*3446Smrj 		 * that means an extra system call and could hurt performance.
329*3446Smrj 		 */
330*3446Smrj 		if (grp[REG_FS] == 0x1bb) /* hard code legacy LWPFS_SEL */
331*3446Smrj 		    (void) lwp_setprivate(lwp, _LWP_FSBASE,
332*3446Smrj 		    (uintptr_t)grp[REG_FSBASE]);
333*3446Smrj 
334*3446Smrj 		if (grp[REG_GS] == 0x1c3) /* hard code legacy LWPGS_SEL */
335*3446Smrj 		    (void) lwp_setprivate(lwp, _LWP_GSBASE,
336*3446Smrj 		    (uintptr_t)grp[REG_GSBASE]);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate #else
3390Sstevel@tonic-gate 	if (grp[GS] == LWPGS_SEL)
3400Sstevel@tonic-gate 		(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
3410Sstevel@tonic-gate #endif
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
3440Sstevel@tonic-gate 	lwptot(lwp)->t_post_sys = 1;
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * set syscall()'s return values for a lwp.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate void
3510Sstevel@tonic-gate lwp_setrval(klwp_t *lwp, int v1, int v2)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	lwptoregs(lwp)->r_ps &= ~PS_C;
3540Sstevel@tonic-gate 	lwptoregs(lwp)->r_r0 = v1;
3550Sstevel@tonic-gate 	lwptoregs(lwp)->r_r1 = v2;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * set syscall()'s return values for a lwp.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate void
3620Sstevel@tonic-gate lwp_setsp(klwp_t *lwp, caddr_t sp)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	lwptoregs(lwp)->r_sp = (intptr_t)sp;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate  * Copy regs from parent to child.
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate void
3710Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate #if defined(__amd64)
3742712Snn35248 	struct pcb *pcb = &clwp->lwp_pcb;
3752712Snn35248 	struct regs *rp = lwptoregs(lwp);
3762712Snn35248 
3772712Snn35248 	if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) {
3782712Snn35248 		pcb->pcb_ds = rp->r_ds;
3792712Snn35248 		pcb->pcb_es = rp->r_es;
3802712Snn35248 		pcb->pcb_fs = rp->r_fs;
3812712Snn35248 		pcb->pcb_gs = rp->r_gs;
3822712Snn35248 		pcb->pcb_flags |= RUPDATE_PENDING;
3832712Snn35248 		lwptot(clwp)->t_post_sys = 1;
3842712Snn35248 	}
3852712Snn35248 	ASSERT(lwptot(clwp)->t_post_sys);
3860Sstevel@tonic-gate #endif
3872712Snn35248 
3880Sstevel@tonic-gate 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs));
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * This function is currently unused on x86.
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate /*ARGSUSED*/
3950Sstevel@tonic-gate void
3960Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec)
3970Sstevel@tonic-gate {}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * This function is currently unused on x86.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate void
4030Sstevel@tonic-gate lwp_pcb_exit(void)
4040Sstevel@tonic-gate {}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * Lwp context ops for segment registers.
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * Every time we come into the kernel (syscall, interrupt or trap
4120Sstevel@tonic-gate  * but not fast-traps) we capture the current values of the user's
4130Sstevel@tonic-gate  * segment registers into the lwp's reg structure. This includes
4140Sstevel@tonic-gate  * lcall for i386 generic system call support since it is handled
4150Sstevel@tonic-gate  * as a segment-not-present trap.
4160Sstevel@tonic-gate  *
4170Sstevel@tonic-gate  * Here we save the current values from the lwp regs into the pcb
4180Sstevel@tonic-gate  * and set the RUPDATE_PENDING bit to tell the rest of the kernel
4190Sstevel@tonic-gate  * that the pcb copy of the segment registers is the current one.
4200Sstevel@tonic-gate  * This ensures the lwp's next trip to user land via update_sregs.
4210Sstevel@tonic-gate  * Finally we set t_post_sys to ensure that no system call fast-path's
4220Sstevel@tonic-gate  * its way out of the kernel via sysret.
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * (This means that we need to have interrupts disabled when we test
4250Sstevel@tonic-gate  * t->t_post_sys in the syscall handlers; if the test fails, we need
4260Sstevel@tonic-gate  * to keep interrupts disabled until we return to userland so we can't
4270Sstevel@tonic-gate  * be switched away.)
4280Sstevel@tonic-gate  *
4290Sstevel@tonic-gate  * As a result of all this, we don't really have to do a whole lot if
4300Sstevel@tonic-gate  * the thread is just mucking about in the kernel, switching on and
4310Sstevel@tonic-gate  * off the cpu for whatever reason it feels like. And yet we still
4320Sstevel@tonic-gate  * preserve fast syscalls, cause if we -don't- get descheduled,
4330Sstevel@tonic-gate  * we never come here either.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate #define	VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \
4370Sstevel@tonic-gate 	    (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL)
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate void
4400Sstevel@tonic-gate lwp_segregs_save(klwp_t *lwp)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate #if defined(__amd64)
4430Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
4440Sstevel@tonic-gate 	struct regs *rp;
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 	if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) {
4500Sstevel@tonic-gate 		rp = lwptoregs(lwp);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		/*
4530Sstevel@tonic-gate 		 * If there's no update already pending, capture the current
4540Sstevel@tonic-gate 		 * %ds/%es/%fs/%gs values from lwp's regs in case the user
4550Sstevel@tonic-gate 		 * changed them; %fsbase and %gsbase are privileged so the
4560Sstevel@tonic-gate 		 * kernel versions of these registers in pcb_fsbase and
4570Sstevel@tonic-gate 		 * pcb_gsbase are always up-to-date.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		pcb->pcb_ds = rp->r_ds;
4600Sstevel@tonic-gate 		pcb->pcb_es = rp->r_es;
4610Sstevel@tonic-gate 		pcb->pcb_fs = rp->r_fs;
4620Sstevel@tonic-gate 		pcb->pcb_gs = rp->r_gs;
4630Sstevel@tonic-gate 		pcb->pcb_flags |= RUPDATE_PENDING;
4640Sstevel@tonic-gate 		lwp->lwp_thread->t_post_sys = 1;
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate #endif	/* __amd64 */
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc,
4690Sstevel@tonic-gate 	    sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0);
4700Sstevel@tonic-gate 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc,
4710Sstevel@tonic-gate 	    sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
474*3446Smrj #if defined(__amd64)
475*3446Smrj 
476*3446Smrj /*
477*3446Smrj  * Update the segment registers with new values from the pcb
478*3446Smrj  *
479*3446Smrj  * We have to do this carefully, and in the following order,
480*3446Smrj  * in case any of the selectors points at a bogus descriptor.
481*3446Smrj  * If they do, we'll catch trap with on_trap and return 1.
482*3446Smrj  * returns 0 on success.
483*3446Smrj  *
484*3446Smrj  * This is particularly tricky for %gs.
485*3446Smrj  * This routine must be executed under a cli.
486*3446Smrj  */
487*3446Smrj int
488*3446Smrj update_sregs(struct regs *rp,  klwp_t *lwp)
489*3446Smrj {
490*3446Smrj 	pcb_t *pcb = &lwp->lwp_pcb;
491*3446Smrj 	ulong_t	kgsbase;
492*3446Smrj 	on_trap_data_t	otd;
493*3446Smrj 	int rc = 0;
494*3446Smrj 
495*3446Smrj 	if (!on_trap(&otd, OT_SEGMENT_ACCESS)) {
496*3446Smrj 
497*3446Smrj 		kgsbase = (ulong_t)CPU;
498*3446Smrj 		__set_gs(pcb->pcb_gs);
499*3446Smrj 
500*3446Smrj 		/*
501*3446Smrj 		 * If __set_gs fails it's because the new %gs is a bad %gs,
502*3446Smrj 		 * we'll be taking a trap but with the original %gs and %gsbase
503*3446Smrj 		 * undamaged (i.e. pointing at curcpu).
504*3446Smrj 		 *
505*3446Smrj 		 * We've just mucked up the kernel's gsbase.  Oops.  In
506*3446Smrj 		 * particular we can't take any traps at all.  Make the newly
507*3446Smrj 		 * computed gsbase be the hidden gs via __swapgs , and fix
508*3446Smrj 		 * the kernel's gsbase back again. Later, when we return to
509*3446Smrj 		 * userland we'll swapgs again restoring gsbase just loaded
510*3446Smrj 		 * above.
511*3446Smrj 		 */
512*3446Smrj 		__swapgs();
513*3446Smrj 		rp->r_gs = pcb->pcb_gs;
514*3446Smrj 
515*3446Smrj 		/*
516*3446Smrj 		 * restore kernel's gsbase
517*3446Smrj 		 */
518*3446Smrj 		wrmsr(MSR_AMD_GSBASE, kgsbase);
519*3446Smrj 
520*3446Smrj 		/*
521*3446Smrj 		 * Only override the descriptor base address if
522*3446Smrj 		 * r_gs == LWPGS_SEL or if r_gs == NULL. A note on
523*3446Smrj 		 * NULL descriptors -- 32-bit programs take faults
524*3446Smrj 		 * if they deference NULL descriptors; however,
525*3446Smrj 		 * when 64-bit programs load them into %fs or %gs,
526*3446Smrj 		 * they DONT fault -- only the base address remains
527*3446Smrj 		 * whatever it was from the last load.   Urk.
528*3446Smrj 		 *
529*3446Smrj 		 * XXX - note that lwp_setprivate now sets %fs/%gs to the
530*3446Smrj 		 * null selector for 64 bit processes. Whereas before
531*3446Smrj 		 * %fs/%gs were set to LWP(FS|GS)_SEL regardless of
532*3446Smrj 		 * the process's data model. For now we check for both
533*3446Smrj 		 * values so that the kernel can also support the older
534*3446Smrj 		 * libc. This should be ripped out at some point in the
535*3446Smrj 		 * future.
536*3446Smrj 		 */
537*3446Smrj 		if (pcb->pcb_gs == LWPGS_SEL || pcb->pcb_gs == 0)
538*3446Smrj 			wrmsr(MSR_AMD_KGSBASE, pcb->pcb_gsbase);
539*3446Smrj 
540*3446Smrj 		__set_ds(pcb->pcb_ds);
541*3446Smrj 		rp->r_ds = pcb->pcb_ds;
542*3446Smrj 
543*3446Smrj 		__set_es(pcb->pcb_es);
544*3446Smrj 		rp->r_es = pcb->pcb_es;
545*3446Smrj 
546*3446Smrj 		__set_fs(pcb->pcb_fs);
547*3446Smrj 		rp->r_fs = pcb->pcb_fs;
548*3446Smrj 
549*3446Smrj 		/*
550*3446Smrj 		 * Same as for %gs
551*3446Smrj 		 */
552*3446Smrj 		if (pcb->pcb_fs == LWPFS_SEL || pcb->pcb_fs == 0)
553*3446Smrj 			wrmsr(MSR_AMD_FSBASE, pcb->pcb_fsbase);
554*3446Smrj 
555*3446Smrj 	} else {
556*3446Smrj 		cli();
557*3446Smrj 		rc = 1;
558*3446Smrj 	}
559*3446Smrj 	no_trap();
560*3446Smrj 	return (rc);
561*3446Smrj }
562*3446Smrj #endif	/* __amd64 */
563*3446Smrj 
564*3446Smrj #ifdef _SYSCALL32_IMPL
565*3446Smrj 
566*3446Smrj /*
567*3446Smrj  * Make it impossible for a process to change its data model.
568*3446Smrj  * We do this by toggling the present bits for the 32 and
569*3446Smrj  * 64-bit user code descriptors. That way if a user lwp attempts
570*3446Smrj  * to change its data model (by using the wrong code descriptor in
571*3446Smrj  * %cs) it will fault immediately. This also allows us to simplify
572*3446Smrj  * assertions and checks in the kernel.
573*3446Smrj  */
574*3446Smrj static void
575*3446Smrj gdt_ucode_model(model_t model)
576*3446Smrj {
577*3446Smrj 	cpu_t *cpu;
578*3446Smrj 
579*3446Smrj 	kpreempt_disable();
580*3446Smrj 	cpu = CPU;
581*3446Smrj 	if (model == DATAMODEL_NATIVE) {
582*3446Smrj 		cpu->cpu_gdt[GDT_UCODE].usd_p = 1;
583*3446Smrj 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 0;
584*3446Smrj 	} else {
585*3446Smrj 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 1;
586*3446Smrj 		cpu->cpu_gdt[GDT_UCODE].usd_p = 0;
587*3446Smrj 	}
588*3446Smrj 	kpreempt_enable();
589*3446Smrj }
590*3446Smrj 
591*3446Smrj #endif	/* _SYSCALL32_IMPL */
592*3446Smrj 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * Restore lwp private fs and gs segment descriptors
5950Sstevel@tonic-gate  * on current cpu's GDT.
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate static void
5980Sstevel@tonic-gate lwp_segregs_restore(klwp_t *lwp)
5990Sstevel@tonic-gate {
600*3446Smrj 	pcb_t *pcb = &lwp->lwp_pcb;
6010Sstevel@tonic-gate 	cpu_t *cpu = CPU;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
6040Sstevel@tonic-gate 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
6050Sstevel@tonic-gate 
606*3446Smrj #ifdef	_SYSCALL32_IMPL
607*3446Smrj 	gdt_ucode_model(DATAMODEL_NATIVE);
608*3446Smrj #endif
609*3446Smrj 
6100Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
6110Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static void
6180Sstevel@tonic-gate lwp_segregs_restore32(klwp_t *lwp)
6190Sstevel@tonic-gate {
620*3446Smrj 	/*LINTED*/
6210Sstevel@tonic-gate 	cpu_t *cpu = CPU;
6220Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
6230Sstevel@tonic-gate 
624*3446Smrj 	ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_fsdesc));
625*3446Smrj 	ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_gsdesc));
6260Sstevel@tonic-gate 
627*3446Smrj 	gdt_ucode_model(DATAMODEL_ILP32);
6280Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
6290Sstevel@tonic-gate 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6352712Snn35248  * If this is a process in a branded zone, then we want it to use the brand
6362712Snn35248  * syscall entry points instead of the standard Solaris entry points.  This
6372712Snn35248  * routine must be called when a new lwp is created within a branded zone
6382712Snn35248  * or when an existing lwp moves into a branded zone via a zone_enter()
6392712Snn35248  * operation.
6402712Snn35248  */
6412712Snn35248 void
6422712Snn35248 lwp_attach_brand_hdlrs(klwp_t *lwp)
6432712Snn35248 {
6442712Snn35248 	kthread_t *t = lwptot(lwp);
6452712Snn35248 
6462712Snn35248 	ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
6472712Snn35248 	ASSERT(removectx(t, NULL, brand_interpositioning_disable,
6482712Snn35248 	    brand_interpositioning_enable, NULL, NULL, NULL, NULL) == 0);
6492712Snn35248 
6502712Snn35248 	installctx(t, NULL, brand_interpositioning_disable,
6512712Snn35248 	    brand_interpositioning_enable, NULL, NULL, NULL, NULL);
6522712Snn35248 
6532712Snn35248 	if (t == curthread) {
6542712Snn35248 		kpreempt_disable();
6552712Snn35248 		brand_interpositioning_enable();
6562712Snn35248 		kpreempt_enable();
6572712Snn35248 	}
6582712Snn35248 }
6592712Snn35248 
6602712Snn35248 /*
6610Sstevel@tonic-gate  * Add any lwp-associated context handlers to the lwp at the beginning
6620Sstevel@tonic-gate  * of the lwp's useful life.
6630Sstevel@tonic-gate  *
6640Sstevel@tonic-gate  * All paths which create lwp's invoke lwp_create(); lwp_create()
6650Sstevel@tonic-gate  * invokes lwp_stk_init() which initializes the stack, sets up
6660Sstevel@tonic-gate  * lwp_regs, and invokes this routine.
6670Sstevel@tonic-gate  *
6680Sstevel@tonic-gate  * All paths which destroy lwp's invoke lwp_exit() to rip the lwp
6690Sstevel@tonic-gate  * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it
6700Sstevel@tonic-gate  * ends up in thread_free() which invokes freectx(t, 0) before
6710Sstevel@tonic-gate  * invoking lwp_stk_fini().  When the lwp is recycled from death
6720Sstevel@tonic-gate  * row, lwp_stk_fini() is invoked, then thread_free(), and thus
6730Sstevel@tonic-gate  * freectx(t, 0) as before.
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  * In the case of exec, the surviving lwp is thoroughly scrubbed
6760Sstevel@tonic-gate  * clean; exec invokes freectx(t, 1) to destroy associated contexts.
6770Sstevel@tonic-gate  * On the way back to the new image, it invokes setregs() which
6780Sstevel@tonic-gate  * in turn invokes this routine.
6790Sstevel@tonic-gate  */
6800Sstevel@tonic-gate void
6810Sstevel@tonic-gate lwp_installctx(klwp_t *lwp)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	kthread_t *t = lwptot(lwp);
6840Sstevel@tonic-gate 	int thisthread = t == curthread;
6850Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6860Sstevel@tonic-gate 	void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
6870Sstevel@tonic-gate 	    lwp_segregs_restore : lwp_segregs_restore32;
6880Sstevel@tonic-gate #else
6890Sstevel@tonic-gate 	void (*restop)(klwp_t *) = lwp_segregs_restore;
6900Sstevel@tonic-gate #endif
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Install the basic lwp context handlers on each lwp.
6940Sstevel@tonic-gate 	 *
6950Sstevel@tonic-gate 	 * On the amd64 kernel, the context handlers are responsible for
6960Sstevel@tonic-gate 	 * virtualizing %ds, %es, %fs, and %gs to the lwp.  The register
6970Sstevel@tonic-gate 	 * values are only ever changed via sys_rtt when the
6980Sstevel@tonic-gate 	 * RUPDATE_PENDING bit is set.  Only sys_rtt gets to clear the bit.
6990Sstevel@tonic-gate 	 *
7000Sstevel@tonic-gate 	 * On the i386 kernel, the context handlers are responsible for
7010Sstevel@tonic-gate 	 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
7040Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL) == 0);
7050Sstevel@tonic-gate 	if (thisthread)
7060Sstevel@tonic-gate 		kpreempt_disable();
7070Sstevel@tonic-gate 	installctx(t, lwp, lwp_segregs_save, restop,
7080Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL);
7090Sstevel@tonic-gate 	if (thisthread) {
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * Since we're the right thread, set the values in the GDT
7120Sstevel@tonic-gate 		 */
7130Sstevel@tonic-gate 		restop(lwp);
7140Sstevel@tonic-gate 		kpreempt_enable();
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * If we have sysenter/sysexit instructions enabled, we need
7190Sstevel@tonic-gate 	 * to ensure that the hardware mechanism is kept up-to-date with the
7200Sstevel@tonic-gate 	 * lwp's kernel stack pointer across context switches.
7210Sstevel@tonic-gate 	 *
7220Sstevel@tonic-gate 	 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
7230Sstevel@tonic-gate 	 * it to the lwp's kernel stack pointer (kstktop).
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 	if (x86_feature & X86_SEP) {
7260Sstevel@tonic-gate #if defined(__amd64)
7270Sstevel@tonic-gate 		caddr_t kstktop = (caddr_t)lwp->lwp_regs;
7280Sstevel@tonic-gate #elif defined(__i386)
7290Sstevel@tonic-gate 		caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
7300Sstevel@tonic-gate 		    SA(sizeof (struct regs) + MINFRAME);
7310Sstevel@tonic-gate #endif
7320Sstevel@tonic-gate 		ASSERT(removectx(t, kstktop,
7330Sstevel@tonic-gate 		    sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		if (thisthread)
7360Sstevel@tonic-gate 			kpreempt_disable();
7370Sstevel@tonic-gate 		installctx(t, kstktop,
7380Sstevel@tonic-gate 		    sep_save, sep_restore, NULL, NULL, NULL, NULL);
7390Sstevel@tonic-gate 		if (thisthread) {
7400Sstevel@tonic-gate 			/*
7410Sstevel@tonic-gate 			 * We're the right thread, so set the stack pointer
7420Sstevel@tonic-gate 			 * for the first sysenter instruction to use
7430Sstevel@tonic-gate 			 */
7440Sstevel@tonic-gate 			sep_restore(kstktop);
7450Sstevel@tonic-gate 			kpreempt_enable();
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 	}
7482712Snn35248 
7492712Snn35248 	if (PROC_IS_BRANDED(ttoproc(t)))
7502712Snn35248 		lwp_attach_brand_hdlrs(lwp);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  * Clear registers on exec(2).
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate void
7570Sstevel@tonic-gate setregs(uarg_t *args)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	struct regs *rp;
7600Sstevel@tonic-gate 	kthread_t *t = curthread;
7610Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
7620Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
7630Sstevel@tonic-gate 	greg_t sp;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Initialize user registers
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	(void) save_syscall_args();	/* copy args from registers first */
7690Sstevel@tonic-gate 	rp = lwptoregs(lwp);
7700Sstevel@tonic-gate 	sp = rp->r_sp;
7710Sstevel@tonic-gate 	bzero(rp, sizeof (*rp));
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	rp->r_ss = UDS_SEL;
7740Sstevel@tonic-gate 	rp->r_sp = sp;
7750Sstevel@tonic-gate 	rp->r_pc = args->entry;
7760Sstevel@tonic-gate 	rp->r_ps = PSL_USER;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate #if defined(__amd64)
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	pcb->pcb_fs = pcb->pcb_gs = 0;
7810Sstevel@tonic-gate 	pcb->pcb_fsbase = pcb->pcb_gsbase = 0;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (ttoproc(t)->p_model == DATAMODEL_NATIVE) {
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		/*
7880Sstevel@tonic-gate 		 * Only allow 64-bit user code descriptor to be present.
7890Sstevel@tonic-gate 		 */
790*3446Smrj 		gdt_ucode_model(DATAMODEL_NATIVE);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		/*
7930Sstevel@tonic-gate 		 * Arrange that the virtualized %fs and %gs GDT descriptors
7940Sstevel@tonic-gate 		 * have a well-defined initial state (present, ring 3
7950Sstevel@tonic-gate 		 * and of type data).
7960Sstevel@tonic-gate 		 */
7970Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		/*
8000Sstevel@tonic-gate 		 * thrptr is either NULL or a value used by DTrace.
8010Sstevel@tonic-gate 		 * 64-bit processes use %fs as their "thread" register.
8020Sstevel@tonic-gate 		 */
8030Sstevel@tonic-gate 		if (args->thrptr)
8040Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	} else {
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		rp->r_cs = U32CS_SEL;
8090Sstevel@tonic-gate 		rp->r_ds = rp->r_es = UDS_SEL;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 		/*
8120Sstevel@tonic-gate 		 * only allow 32-bit user code selector to be present.
8130Sstevel@tonic-gate 		 */
814*3446Smrj 		gdt_ucode_model(DATAMODEL_ILP32);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * thrptr is either NULL or a value used by DTrace.
8200Sstevel@tonic-gate 		 * 32-bit processes use %gs as their "thread" register.
8210Sstevel@tonic-gate 		 */
8220Sstevel@tonic-gate 		if (args->thrptr)
8230Sstevel@tonic-gate 			(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	pcb->pcb_ds = rp->r_ds;
8280Sstevel@tonic-gate 	pcb->pcb_es = rp->r_es;
8290Sstevel@tonic-gate 	pcb->pcb_flags |= RUPDATE_PENDING;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate #elif defined(__i386)
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	rp->r_cs = UCS_SEL;
8340Sstevel@tonic-gate 	rp->r_ds = rp->r_es = UDS_SEL;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
8380Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
8390Sstevel@tonic-gate 	 * and of type data).
8400Sstevel@tonic-gate 	 */
8410Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * For %gs we need to reset LWP_GSBASE in pcb and the
8450Sstevel@tonic-gate 	 * per-cpu GDT descriptor. thrptr is either NULL
8460Sstevel@tonic-gate 	 * or a value used by DTrace.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	if (args->thrptr)
8490Sstevel@tonic-gate 		(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
8500Sstevel@tonic-gate #endif
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
8530Sstevel@tonic-gate 	t->t_post_sys = 1;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * Here we initialize minimal fpu state.
8570Sstevel@tonic-gate 	 * The rest is done at the first floating
8580Sstevel@tonic-gate 	 * point instruction that a process executes.
8590Sstevel@tonic-gate 	 */
8600Sstevel@tonic-gate 	pcb->pcb_fpu.fpu_flags = 0;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/*
8630Sstevel@tonic-gate 	 * Add the lwp context handlers that virtualize segment registers,
8640Sstevel@tonic-gate 	 * and/or system call stacks etc.
8650Sstevel@tonic-gate 	 */
8660Sstevel@tonic-gate 	lwp_installctx(lwp);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate 
8692712Snn35248 user_desc_t *
8702712Snn35248 cpu_get_gdt(void)
8712712Snn35248 {
8722712Snn35248 	return (CPU->cpu_gdt);
8732712Snn35248 }
8742712Snn35248 
8752712Snn35248 
8760Sstevel@tonic-gate #if !defined(lwp_getdatamodel)
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate  * Return the datamodel of the given lwp.
8800Sstevel@tonic-gate  */
8810Sstevel@tonic-gate /*ARGSUSED*/
8820Sstevel@tonic-gate model_t
8830Sstevel@tonic-gate lwp_getdatamodel(klwp_t *lwp)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	return (lwp->lwp_procp->p_model);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate #endif	/* !lwp_getdatamodel */
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate #if !defined(get_udatamodel)
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate model_t
8930Sstevel@tonic-gate get_udatamodel(void)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	return (curproc->p_model);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate #endif	/* !get_udatamodel */
899