xref: /titanic_50/usr/src/uts/intel/ia32/os/sundep.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
28 /*	All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/sysmacros.h>
35 #include <sys/signal.h>
36 #include <sys/systm.h>
37 #include <sys/user.h>
38 #include <sys/mman.h>
39 #include <sys/class.h>
40 #include <sys/proc.h>
41 #include <sys/procfs.h>
42 #include <sys/buf.h>
43 #include <sys/kmem.h>
44 #include <sys/cred.h>
45 #include <sys/archsystm.h>
46 #include <sys/vmparam.h>
47 #include <sys/prsystm.h>
48 #include <sys/reboot.h>
49 #include <sys/uadmin.h>
50 #include <sys/vfs.h>
51 #include <sys/vnode.h>
52 #include <sys/file.h>
53 #include <sys/session.h>
54 #include <sys/ucontext.h>
55 #include <sys/dnlc.h>
56 #include <sys/var.h>
57 #include <sys/cmn_err.h>
58 #include <sys/debugreg.h>
59 #include <sys/thread.h>
60 #include <sys/vtrace.h>
61 #include <sys/consdev.h>
62 #include <sys/psw.h>
63 #include <sys/regset.h>
64 #include <sys/privregs.h>
65 #include <sys/stack.h>
66 #include <sys/swap.h>
67 #include <vm/hat.h>
68 #include <vm/anon.h>
69 #include <vm/as.h>
70 #include <vm/page.h>
71 #include <vm/seg.h>
72 #include <vm/seg_kmem.h>
73 #include <vm/seg_map.h>
74 #include <vm/seg_vn.h>
75 #include <sys/exec.h>
76 #include <sys/acct.h>
77 #include <sys/core.h>
78 #include <sys/corectl.h>
79 #include <sys/modctl.h>
80 #include <sys/tuneable.h>
81 #include <c2/audit.h>
82 #include <sys/bootconf.h>
83 #include <sys/brand.h>
84 #include <sys/dumphdr.h>
85 #include <sys/promif.h>
86 #include <sys/systeminfo.h>
87 #include <sys/kdi.h>
88 #include <sys/contract_impl.h>
89 #include <sys/x86_archext.h>
90 #include <sys/segments.h>
91 
92 /*
93  * Compare the version of boot that boot says it is against
94  * the version of boot the kernel expects.
95  */
96 int
97 check_boot_version(int boots_version)
98 {
99 	if (boots_version == BO_VERSION)
100 		return (0);
101 
102 	prom_printf("Wrong boot interface - kernel needs v%d found v%d\n",
103 	    BO_VERSION, boots_version);
104 	prom_panic("halting");
105 	/*NOTREACHED*/
106 }
107 
108 /*
109  * Process the physical installed list for boot.
110  * Finds:
111  * 1) the pfn of the highest installed physical page,
112  * 2) the number of pages installed
113  * 3) the number of distinct contiguous regions these pages fall into.
114  */
115 void
116 installed_top_size(
117 	struct memlist *list,	/* pointer to start of installed list */
118 	pfn_t *high_pfn,	/* return ptr for top value */
119 	pgcnt_t *pgcnt,		/* return ptr for sum of installed pages */
120 	int	*ranges)	/* return ptr for the count of contig. ranges */
121 {
122 	pfn_t top = 0;
123 	pgcnt_t sumpages = 0;
124 	pfn_t highp;		/* high page in a chunk */
125 	int cnt = 0;
126 
127 	for (; list; list = list->next) {
128 		++cnt;
129 		highp = (list->address + list->size - 1) >> PAGESHIFT;
130 		if (top < highp)
131 			top = highp;
132 		sumpages += btop(list->size);
133 	}
134 
135 	*high_pfn = top;
136 	*pgcnt = sumpages;
137 	*ranges = cnt;
138 }
139 
140 /*
141  * Copy in a memory list from boot to kernel, with a filter function
142  * to remove pages. The filter function can increase the address and/or
143  * decrease the size to filter out pages.  It will also align addresses and
144  * sizes to PAGESIZE.
145  */
146 void
147 copy_memlist_filter(
148 	struct memlist *src,
149 	struct memlist **dstp,
150 	void (*filter)(uint64_t *, uint64_t *))
151 {
152 	struct memlist *dst, *prev;
153 	uint64_t addr;
154 	uint64_t size;
155 	uint64_t eaddr;
156 
157 	dst = *dstp;
158 	prev = dst;
159 
160 	/*
161 	 * Move through the memlist applying a filter against
162 	 * each range of memory. Note that we may apply the
163 	 * filter multiple times against each memlist entry.
164 	 */
165 	for (; src; src = src->next) {
166 		addr = P2ROUNDUP(src->address, PAGESIZE);
167 		eaddr = P2ALIGN(src->address + src->size, PAGESIZE);
168 		while (addr < eaddr) {
169 			size = eaddr - addr;
170 			if (filter != NULL)
171 				filter(&addr, &size);
172 			if (size == 0)
173 				break;
174 			dst->address = addr;
175 			dst->size = size;
176 			dst->next = 0;
177 			if (prev == dst) {
178 				dst->prev = 0;
179 				dst++;
180 			} else {
181 				dst->prev = prev;
182 				prev->next = dst;
183 				dst++;
184 				prev++;
185 			}
186 			addr += size;
187 		}
188 	}
189 
190 	*dstp = dst;
191 }
192 
193 /*
194  * Kernel setup code, called from startup().
195  */
196 void
197 kern_setup1(void)
198 {
199 	proc_t *pp;
200 
201 	pp = &p0;
202 
203 	proc_sched = pp;
204 
205 	/*
206 	 * Initialize process 0 data structures
207 	 */
208 	pp->p_stat = SRUN;
209 	pp->p_flag = SSYS;
210 
211 	pp->p_pidp = &pid0;
212 	pp->p_pgidp = &pid0;
213 	pp->p_sessp = &session0;
214 	pp->p_tlist = &t0;
215 	pid0.pid_pglink = pp;
216 	pid0.pid_pgtail = pp;
217 
218 	/*
219 	 * XXX - we asssume that the u-area is zeroed out except for
220 	 * ttolwp(curthread)->lwp_regs.
221 	 */
222 	u.u_cmask = (mode_t)CMASK;
223 
224 	thread_init();		/* init thread_free list */
225 	pid_init();		/* initialize pid (proc) table */
226 	contract_init();	/* initialize contracts */
227 
228 	init_pages_pp_maximum();
229 }
230 
231 /*
232  * Load a procedure into a thread.
233  */
234 void
235 thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len)
236 {
237 	caddr_t sp;
238 	size_t framesz;
239 	caddr_t argp;
240 	long *p;
241 	extern void thread_start();
242 
243 	/*
244 	 * Push a "c" call frame onto the stack to represent
245 	 * the caller of "start".
246 	 */
247 	sp = t->t_stk;
248 	ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0);
249 	if (len != 0) {
250 		/*
251 		 * the object that arg points at is copied into the
252 		 * caller's frame.
253 		 */
254 		framesz = SA(len);
255 		sp -= framesz;
256 		ASSERT(sp > t->t_stkbase);
257 		argp = sp + SA(MINFRAME);
258 		bcopy(arg, argp, len);
259 		arg = argp;
260 	}
261 	/*
262 	 * Set up arguments (arg and len) on the caller's stack frame.
263 	 */
264 	p = (long *)sp;
265 
266 	*--p = 0;		/* fake call */
267 	*--p = 0;		/* null frame pointer terminates stack trace */
268 	*--p = (long)len;
269 	*--p = (intptr_t)arg;
270 	*--p = (intptr_t)start;
271 
272 	/*
273 	 * initialize thread to resume at thread_start() which will
274 	 * turn around and invoke (*start)(arg, len).
275 	 */
276 	t->t_pc = (uintptr_t)thread_start;
277 	t->t_sp = (uintptr_t)p;
278 
279 	ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0);
280 }
281 
282 /*
283  * load user registers into lwp.
284  */
285 /*ARGSUSED2*/
286 void
287 lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
288 {
289 	struct regs *rp = lwptoregs(lwp);
290 
291 	setgregs(lwp, grp);
292 	rp->r_ps = PSL_USER;
293 
294 	/*
295 	 * For 64-bit lwps, we allow one magic %fs selector value, and one
296 	 * magic %gs selector to point anywhere in the address space using
297 	 * %fsbase and %gsbase behind the scenes.  libc uses %fs to point
298 	 * at the ulwp_t structure.
299 	 *
300 	 * For 32-bit lwps, libc wedges its lwp thread pointer into the
301 	 * ucontext ESP slot (which is otherwise irrelevant to setting a
302 	 * ucontext) and LWPGS_SEL value into gregs[REG_GS].  This is so
303 	 * syslwp_create() can atomically setup %gs.
304 	 *
305 	 * See setup_context() in libc.
306 	 */
307 #ifdef _SYSCALL32_IMPL
308 	if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) {
309 		if (grp[REG_GS] == LWPGS_SEL)
310 			(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
311 	}
312 #else
313 	if (grp[GS] == LWPGS_SEL)
314 		(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
315 #endif
316 
317 	lwp->lwp_eosys = JUSTRETURN;
318 	lwptot(lwp)->t_post_sys = 1;
319 }
320 
321 /*
322  * set syscall()'s return values for a lwp.
323  */
324 void
325 lwp_setrval(klwp_t *lwp, int v1, int v2)
326 {
327 	lwptoregs(lwp)->r_ps &= ~PS_C;
328 	lwptoregs(lwp)->r_r0 = v1;
329 	lwptoregs(lwp)->r_r1 = v2;
330 }
331 
332 /*
333  * set syscall()'s return values for a lwp.
334  */
335 void
336 lwp_setsp(klwp_t *lwp, caddr_t sp)
337 {
338 	lwptoregs(lwp)->r_sp = (intptr_t)sp;
339 }
340 
341 /*
342  * Copy regs from parent to child.
343  */
344 void
345 lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
346 {
347 #if defined(__amd64)
348 	struct pcb *pcb = &clwp->lwp_pcb;
349 	struct regs *rp = lwptoregs(lwp);
350 
351 	if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) {
352 		pcb->pcb_ds = rp->r_ds;
353 		pcb->pcb_es = rp->r_es;
354 		pcb->pcb_fs = rp->r_fs;
355 		pcb->pcb_gs = rp->r_gs;
356 		pcb->pcb_flags |= RUPDATE_PENDING;
357 		lwptot(clwp)->t_post_sys = 1;
358 	}
359 	ASSERT(lwptot(clwp)->t_post_sys);
360 #endif
361 
362 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs));
363 }
364 
365 /*
366  * This function is currently unused on x86.
367  */
368 /*ARGSUSED*/
369 void
370 lwp_freeregs(klwp_t *lwp, int isexec)
371 {}
372 
373 /*
374  * This function is currently unused on x86.
375  */
376 void
377 lwp_pcb_exit(void)
378 {}
379 
380 /*
381  * Lwp context ops for segment registers.
382  */
383 
384 /*
385  * Every time we come into the kernel (syscall, interrupt or trap
386  * but not fast-traps) we capture the current values of the user's
387  * segment registers into the lwp's reg structure. This includes
388  * lcall for i386 generic system call support since it is handled
389  * as a segment-not-present trap.
390  *
391  * Here we save the current values from the lwp regs into the pcb
392  * and set the RUPDATE_PENDING bit to tell the rest of the kernel
393  * that the pcb copy of the segment registers is the current one.
394  * This ensures the lwp's next trip to user land via update_sregs.
395  * Finally we set t_post_sys to ensure that no system call fast-path's
396  * its way out of the kernel via sysret.
397  *
398  * (This means that we need to have interrupts disabled when we test
399  * t->t_post_sys in the syscall handlers; if the test fails, we need
400  * to keep interrupts disabled until we return to userland so we can't
401  * be switched away.)
402  *
403  * As a result of all this, we don't really have to do a whole lot if
404  * the thread is just mucking about in the kernel, switching on and
405  * off the cpu for whatever reason it feels like. And yet we still
406  * preserve fast syscalls, cause if we -don't- get descheduled,
407  * we never come here either.
408  */
409 
410 #define	VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \
411 	    (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL)
412 
413 void
414 lwp_segregs_save(klwp_t *lwp)
415 {
416 #if defined(__amd64)
417 	pcb_t *pcb = &lwp->lwp_pcb;
418 	struct regs *rp;
419 
420 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
421 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
422 
423 	if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) {
424 		rp = lwptoregs(lwp);
425 
426 		/*
427 		 * If there's no update already pending, capture the current
428 		 * %ds/%es/%fs/%gs values from lwp's regs in case the user
429 		 * changed them; %fsbase and %gsbase are privileged so the
430 		 * kernel versions of these registers in pcb_fsbase and
431 		 * pcb_gsbase are always up-to-date.
432 		 */
433 		pcb->pcb_ds = rp->r_ds;
434 		pcb->pcb_es = rp->r_es;
435 		pcb->pcb_fs = rp->r_fs;
436 		pcb->pcb_gs = rp->r_gs;
437 		pcb->pcb_flags |= RUPDATE_PENDING;
438 		lwp->lwp_thread->t_post_sys = 1;
439 	}
440 #endif	/* __amd64 */
441 
442 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc,
443 	    sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0);
444 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc,
445 	    sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0);
446 }
447 
448 /*
449  * Restore lwp private fs and gs segment descriptors
450  * on current cpu's GDT.
451  */
452 static void
453 lwp_segregs_restore(klwp_t *lwp)
454 {
455 	cpu_t *cpu = CPU;
456 	pcb_t *pcb = &lwp->lwp_pcb;
457 
458 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
459 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
460 
461 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
462 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
463 
464 #if defined(__amd64)
465 	/*
466 	 * Make it impossible for a process to change its data model.
467 	 * We do this by toggling the present bits for the 32 and
468 	 * 64-bit user code descriptors. That way if a user lwp attempts
469 	 * to change its data model (by using the wrong code descriptor in
470 	 * %cs) it will fault immediately. This also allows us to simplify
471 	 * assertions and checks in the kernel.
472 	 */
473 	cpu->cpu_gdt[GDT_UCODE].usd_p = 1;
474 	cpu->cpu_gdt[GDT_U32CODE].usd_p = 0;
475 #endif	/* __amd64 */
476 }
477 
478 #ifdef _SYSCALL32_IMPL
479 
480 static void
481 lwp_segregs_restore32(klwp_t *lwp)
482 {
483 	cpu_t *cpu = CPU;
484 	pcb_t *pcb = &lwp->lwp_pcb;
485 
486 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
487 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
488 
489 	cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
490 	cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
491 	cpu->cpu_gdt[GDT_UCODE].usd_p = 0;
492 	cpu->cpu_gdt[GDT_U32CODE].usd_p = 1;
493 }
494 
495 #endif	/* _SYSCALL32_IMPL */
496 
497 /*
498  * If this is a process in a branded zone, then we want it to use the brand
499  * syscall entry points instead of the standard Solaris entry points.  This
500  * routine must be called when a new lwp is created within a branded zone
501  * or when an existing lwp moves into a branded zone via a zone_enter()
502  * operation.
503  */
504 void
505 lwp_attach_brand_hdlrs(klwp_t *lwp)
506 {
507 	kthread_t *t = lwptot(lwp);
508 
509 	ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
510 	ASSERT(removectx(t, NULL, brand_interpositioning_disable,
511 	    brand_interpositioning_enable, NULL, NULL, NULL, NULL) == 0);
512 
513 	installctx(t, NULL, brand_interpositioning_disable,
514 	    brand_interpositioning_enable, NULL, NULL, NULL, NULL);
515 
516 	if (t == curthread) {
517 		kpreempt_disable();
518 		brand_interpositioning_enable();
519 		kpreempt_enable();
520 	}
521 }
522 
523 /*
524  * Add any lwp-associated context handlers to the lwp at the beginning
525  * of the lwp's useful life.
526  *
527  * All paths which create lwp's invoke lwp_create(); lwp_create()
528  * invokes lwp_stk_init() which initializes the stack, sets up
529  * lwp_regs, and invokes this routine.
530  *
531  * All paths which destroy lwp's invoke lwp_exit() to rip the lwp
532  * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it
533  * ends up in thread_free() which invokes freectx(t, 0) before
534  * invoking lwp_stk_fini().  When the lwp is recycled from death
535  * row, lwp_stk_fini() is invoked, then thread_free(), and thus
536  * freectx(t, 0) as before.
537  *
538  * In the case of exec, the surviving lwp is thoroughly scrubbed
539  * clean; exec invokes freectx(t, 1) to destroy associated contexts.
540  * On the way back to the new image, it invokes setregs() which
541  * in turn invokes this routine.
542  */
543 void
544 lwp_installctx(klwp_t *lwp)
545 {
546 	kthread_t *t = lwptot(lwp);
547 	int thisthread = t == curthread;
548 #ifdef _SYSCALL32_IMPL
549 	void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
550 	    lwp_segregs_restore : lwp_segregs_restore32;
551 #else
552 	void (*restop)(klwp_t *) = lwp_segregs_restore;
553 #endif
554 
555 	/*
556 	 * Install the basic lwp context handlers on each lwp.
557 	 *
558 	 * On the amd64 kernel, the context handlers are responsible for
559 	 * virtualizing %ds, %es, %fs, and %gs to the lwp.  The register
560 	 * values are only ever changed via sys_rtt when the
561 	 * RUPDATE_PENDING bit is set.  Only sys_rtt gets to clear the bit.
562 	 *
563 	 * On the i386 kernel, the context handlers are responsible for
564 	 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
565 	 */
566 	ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
567 	    NULL, NULL, NULL, NULL) == 0);
568 	if (thisthread)
569 		kpreempt_disable();
570 	installctx(t, lwp, lwp_segregs_save, restop,
571 	    NULL, NULL, NULL, NULL);
572 	if (thisthread) {
573 		/*
574 		 * Since we're the right thread, set the values in the GDT
575 		 */
576 		restop(lwp);
577 		kpreempt_enable();
578 	}
579 
580 	/*
581 	 * If we have sysenter/sysexit instructions enabled, we need
582 	 * to ensure that the hardware mechanism is kept up-to-date with the
583 	 * lwp's kernel stack pointer across context switches.
584 	 *
585 	 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
586 	 * it to the lwp's kernel stack pointer (kstktop).
587 	 */
588 	if (x86_feature & X86_SEP) {
589 #if defined(__amd64)
590 		caddr_t kstktop = (caddr_t)lwp->lwp_regs;
591 #elif defined(__i386)
592 		caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
593 		    SA(sizeof (struct regs) + MINFRAME);
594 #endif
595 		ASSERT(removectx(t, kstktop,
596 		    sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
597 
598 		if (thisthread)
599 			kpreempt_disable();
600 		installctx(t, kstktop,
601 		    sep_save, sep_restore, NULL, NULL, NULL, NULL);
602 		if (thisthread) {
603 			/*
604 			 * We're the right thread, so set the stack pointer
605 			 * for the first sysenter instruction to use
606 			 */
607 			sep_restore(kstktop);
608 			kpreempt_enable();
609 		}
610 	}
611 
612 	if (PROC_IS_BRANDED(ttoproc(t)))
613 		lwp_attach_brand_hdlrs(lwp);
614 }
615 
616 /*
617  * Clear registers on exec(2).
618  */
619 void
620 setregs(uarg_t *args)
621 {
622 	struct regs *rp;
623 	kthread_t *t = curthread;
624 	klwp_t *lwp = ttolwp(t);
625 	pcb_t *pcb = &lwp->lwp_pcb;
626 	greg_t sp;
627 
628 	/*
629 	 * Initialize user registers
630 	 */
631 	(void) save_syscall_args();	/* copy args from registers first */
632 	rp = lwptoregs(lwp);
633 	sp = rp->r_sp;
634 	bzero(rp, sizeof (*rp));
635 
636 	rp->r_ss = UDS_SEL;
637 	rp->r_sp = sp;
638 	rp->r_pc = args->entry;
639 	rp->r_ps = PSL_USER;
640 
641 #if defined(__amd64)
642 
643 	pcb->pcb_fs = pcb->pcb_gs = 0;
644 	pcb->pcb_fsbase = pcb->pcb_gsbase = 0;
645 
646 	if (ttoproc(t)->p_model == DATAMODEL_NATIVE) {
647 		cpu_t *cpu;
648 
649 		rp->r_cs = UCS_SEL;
650 
651 		/*
652 		 * Only allow 64-bit user code descriptor to be present.
653 		 */
654 		kpreempt_disable();
655 		cpu = CPU;
656 		cpu->cpu_gdt[GDT_UCODE].usd_p = 1;
657 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 0;
658 		kpreempt_enable();
659 
660 		/*
661 		 * Arrange that the virtualized %fs and %gs GDT descriptors
662 		 * have a well-defined initial state (present, ring 3
663 		 * and of type data).
664 		 */
665 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
666 
667 		/*
668 		 * thrptr is either NULL or a value used by DTrace.
669 		 * 64-bit processes use %fs as their "thread" register.
670 		 */
671 		if (args->thrptr)
672 			(void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr);
673 
674 	} else {
675 		cpu_t *cpu;
676 
677 		rp->r_cs = U32CS_SEL;
678 		rp->r_ds = rp->r_es = UDS_SEL;
679 
680 		/*
681 		 * only allow 32-bit user code selector to be present.
682 		 */
683 		kpreempt_disable();
684 		cpu = CPU;
685 		cpu->cpu_gdt[GDT_UCODE].usd_p = 0;
686 		cpu->cpu_gdt[GDT_U32CODE].usd_p = 1;
687 		kpreempt_enable();
688 
689 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
690 
691 		/*
692 		 * thrptr is either NULL or a value used by DTrace.
693 		 * 32-bit processes use %gs as their "thread" register.
694 		 */
695 		if (args->thrptr)
696 			(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
697 
698 	}
699 
700 
701 	pcb->pcb_ds = rp->r_ds;
702 	pcb->pcb_es = rp->r_es;
703 	pcb->pcb_flags |= RUPDATE_PENDING;
704 
705 #elif defined(__i386)
706 
707 	rp->r_cs = UCS_SEL;
708 	rp->r_ds = rp->r_es = UDS_SEL;
709 
710 	/*
711 	 * Arrange that the virtualized %fs and %gs GDT descriptors
712 	 * have a well-defined initial state (present, ring 3
713 	 * and of type data).
714 	 */
715 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
716 
717 
718 	/*
719 	 * For %gs we need to reset LWP_GSBASE in pcb and the
720 	 * per-cpu GDT descriptor. thrptr is either NULL
721 	 * or a value used by DTrace.
722 	 */
723 	if (args->thrptr)
724 		(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
725 #endif
726 
727 	lwp->lwp_eosys = JUSTRETURN;
728 	t->t_post_sys = 1;
729 
730 	/*
731 	 * Here we initialize minimal fpu state.
732 	 * The rest is done at the first floating
733 	 * point instruction that a process executes.
734 	 */
735 	pcb->pcb_fpu.fpu_flags = 0;
736 
737 	/*
738 	 * Add the lwp context handlers that virtualize segment registers,
739 	 * and/or system call stacks etc.
740 	 */
741 	lwp_installctx(lwp);
742 }
743 
744 user_desc_t *
745 cpu_get_gdt(void)
746 {
747 	return (CPU->cpu_gdt);
748 }
749 
750 
751 #if !defined(lwp_getdatamodel)
752 
753 /*
754  * Return the datamodel of the given lwp.
755  */
756 /*ARGSUSED*/
757 model_t
758 lwp_getdatamodel(klwp_t *lwp)
759 {
760 	return (lwp->lwp_procp->p_model);
761 }
762 
763 #endif	/* !lwp_getdatamodel */
764 
765 #if !defined(get_udatamodel)
766 
767 model_t
768 get_udatamodel(void)
769 {
770 	return (curproc->p_model);
771 }
772 
773 #endif	/* !get_udatamodel */
774