xref: /titanic_41/usr/src/uts/intel/ia32/os/sundep.c (revision cc1a9a89a73172cc2db053635fab3b1b91691657)
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 2008 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/cpu.h>
66 #include <sys/stack.h>
67 #include <sys/swap.h>
68 #include <vm/hat.h>
69 #include <vm/anon.h>
70 #include <vm/as.h>
71 #include <vm/page.h>
72 #include <vm/seg.h>
73 #include <vm/seg_kmem.h>
74 #include <vm/seg_map.h>
75 #include <vm/seg_vn.h>
76 #include <sys/exec.h>
77 #include <sys/acct.h>
78 #include <sys/core.h>
79 #include <sys/corectl.h>
80 #include <sys/modctl.h>
81 #include <sys/tuneable.h>
82 #include <c2/audit.h>
83 #include <sys/bootconf.h>
84 #include <sys/brand.h>
85 #include <sys/dumphdr.h>
86 #include <sys/promif.h>
87 #include <sys/systeminfo.h>
88 #include <sys/kdi.h>
89 #include <sys/contract_impl.h>
90 #include <sys/x86_archext.h>
91 #include <sys/segments.h>
92 #include <sys/ontrap.h>
93 #include <sys/cpu.h>
94 #ifdef __xpv
95 #include <sys/hypervisor.h>
96 #endif
97 
98 /*
99  * Compare the version of boot that boot says it is against
100  * the version of boot the kernel expects.
101  */
102 int
103 check_boot_version(int boots_version)
104 {
105 	if (boots_version == BO_VERSION)
106 		return (0);
107 
108 	prom_printf("Wrong boot interface - kernel needs v%d found v%d\n",
109 	    BO_VERSION, boots_version);
110 	prom_panic("halting");
111 	/*NOTREACHED*/
112 }
113 
114 /*
115  * Process the physical installed list for boot.
116  * Finds:
117  * 1) the pfn of the highest installed physical page,
118  * 2) the number of pages installed
119  * 3) the number of distinct contiguous regions these pages fall into.
120  */
121 void
122 installed_top_size(
123 	struct memlist *list,	/* pointer to start of installed list */
124 	pfn_t *high_pfn,	/* return ptr for top value */
125 	pgcnt_t *pgcnt,		/* return ptr for sum of installed pages */
126 	int	*ranges)	/* return ptr for the count of contig. ranges */
127 {
128 	pfn_t top = 0;
129 	pgcnt_t sumpages = 0;
130 	pfn_t highp;		/* high page in a chunk */
131 	int cnt = 0;
132 
133 	for (; list; list = list->next) {
134 		++cnt;
135 		highp = (list->address + list->size - 1) >> PAGESHIFT;
136 		if (top < highp)
137 			top = highp;
138 		sumpages += btop(list->size);
139 	}
140 
141 	*high_pfn = top;
142 	*pgcnt = sumpages;
143 	*ranges = cnt;
144 }
145 
146 /*
147  * Copy in a memory list from boot to kernel, with a filter function
148  * to remove pages. The filter function can increase the address and/or
149  * decrease the size to filter out pages.  It will also align addresses and
150  * sizes to PAGESIZE.
151  */
152 void
153 copy_memlist_filter(
154 	struct memlist *src,
155 	struct memlist **dstp,
156 	void (*filter)(uint64_t *, uint64_t *))
157 {
158 	struct memlist *dst, *prev;
159 	uint64_t addr;
160 	uint64_t size;
161 	uint64_t eaddr;
162 
163 	dst = *dstp;
164 	prev = dst;
165 
166 	/*
167 	 * Move through the memlist applying a filter against
168 	 * each range of memory. Note that we may apply the
169 	 * filter multiple times against each memlist entry.
170 	 */
171 	for (; src; src = src->next) {
172 		addr = P2ROUNDUP(src->address, PAGESIZE);
173 		eaddr = P2ALIGN(src->address + src->size, PAGESIZE);
174 		while (addr < eaddr) {
175 			size = eaddr - addr;
176 			if (filter != NULL)
177 				filter(&addr, &size);
178 			if (size == 0)
179 				break;
180 			dst->address = addr;
181 			dst->size = size;
182 			dst->next = 0;
183 			if (prev == dst) {
184 				dst->prev = 0;
185 				dst++;
186 			} else {
187 				dst->prev = prev;
188 				prev->next = dst;
189 				dst++;
190 				prev++;
191 			}
192 			addr += size;
193 		}
194 	}
195 
196 	*dstp = dst;
197 }
198 
199 /*
200  * Kernel setup code, called from startup().
201  */
202 void
203 kern_setup1(void)
204 {
205 	proc_t *pp;
206 
207 	pp = &p0;
208 
209 	proc_sched = pp;
210 
211 	/*
212 	 * Initialize process 0 data structures
213 	 */
214 	pp->p_stat = SRUN;
215 	pp->p_flag = SSYS;
216 
217 	pp->p_pidp = &pid0;
218 	pp->p_pgidp = &pid0;
219 	pp->p_sessp = &session0;
220 	pp->p_tlist = &t0;
221 	pid0.pid_pglink = pp;
222 	pid0.pid_pgtail = pp;
223 
224 	/*
225 	 * XXX - we asssume that the u-area is zeroed out except for
226 	 * ttolwp(curthread)->lwp_regs.
227 	 */
228 	PTOU(curproc)->u_cmask = (mode_t)CMASK;
229 
230 	thread_init();		/* init thread_free list */
231 	pid_init();		/* initialize pid (proc) table */
232 	contract_init();	/* initialize contracts */
233 
234 	init_pages_pp_maximum();
235 }
236 
237 /*
238  * Load a procedure into a thread.
239  */
240 void
241 thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len)
242 {
243 	caddr_t sp;
244 	size_t framesz;
245 	caddr_t argp;
246 	long *p;
247 	extern void thread_start();
248 
249 	/*
250 	 * Push a "c" call frame onto the stack to represent
251 	 * the caller of "start".
252 	 */
253 	sp = t->t_stk;
254 	ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0);
255 	if (len != 0) {
256 		/*
257 		 * the object that arg points at is copied into the
258 		 * caller's frame.
259 		 */
260 		framesz = SA(len);
261 		sp -= framesz;
262 		ASSERT(sp > t->t_stkbase);
263 		argp = sp + SA(MINFRAME);
264 		bcopy(arg, argp, len);
265 		arg = argp;
266 	}
267 	/*
268 	 * Set up arguments (arg and len) on the caller's stack frame.
269 	 */
270 	p = (long *)sp;
271 
272 	*--p = 0;		/* fake call */
273 	*--p = 0;		/* null frame pointer terminates stack trace */
274 	*--p = (long)len;
275 	*--p = (intptr_t)arg;
276 	*--p = (intptr_t)start;
277 
278 	/*
279 	 * initialize thread to resume at thread_start() which will
280 	 * turn around and invoke (*start)(arg, len).
281 	 */
282 	t->t_pc = (uintptr_t)thread_start;
283 	t->t_sp = (uintptr_t)p;
284 
285 	ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0);
286 }
287 
288 /*
289  * load user registers into lwp.
290  */
291 /*ARGSUSED2*/
292 void
293 lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
294 {
295 	struct regs *rp = lwptoregs(lwp);
296 
297 	setgregs(lwp, grp);
298 	rp->r_ps = PSL_USER;
299 
300 	/*
301 	 * For 64-bit lwps, we allow one magic %fs selector value, and one
302 	 * magic %gs selector to point anywhere in the address space using
303 	 * %fsbase and %gsbase behind the scenes.  libc uses %fs to point
304 	 * at the ulwp_t structure.
305 	 *
306 	 * For 32-bit lwps, libc wedges its lwp thread pointer into the
307 	 * ucontext ESP slot (which is otherwise irrelevant to setting a
308 	 * ucontext) and LWPGS_SEL value into gregs[REG_GS].  This is so
309 	 * syslwp_create() can atomically setup %gs.
310 	 *
311 	 * See setup_context() in libc.
312 	 */
313 #ifdef _SYSCALL32_IMPL
314 	if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) {
315 		if (grp[REG_GS] == LWPGS_SEL)
316 			(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
317 	} else {
318 		/*
319 		 * See lwp_setprivate in kernel and setup_context in libc.
320 		 *
321 		 * Currently libc constructs a ucontext from whole cloth for
322 		 * every new (not main) lwp created.  For 64 bit processes
323 		 * %fsbase is directly set to point to current thread pointer.
324 		 * In the past (solaris 10) %fs was also set LWPFS_SEL to
325 		 * indicate %fsbase. Now we use the null GDT selector for
326 		 * this purpose. LWP[FS|GS]_SEL are only intended for 32 bit
327 		 * processes. To ease transition we support older libcs in
328 		 * the newer kernel by forcing %fs or %gs selector to null
329 		 * by calling lwp_setprivate if LWP[FS|GS]_SEL is passed in
330 		 * the ucontext.  This is should be ripped out at some future
331 		 * date.  Another fix would be for libc to do a getcontext
332 		 * and inherit the null %fs/%gs from the current context but
333 		 * that means an extra system call and could hurt performance.
334 		 */
335 		if (grp[REG_FS] == 0x1bb) /* hard code legacy LWPFS_SEL */
336 			(void) lwp_setprivate(lwp, _LWP_FSBASE,
337 			    (uintptr_t)grp[REG_FSBASE]);
338 
339 		if (grp[REG_GS] == 0x1c3) /* hard code legacy LWPGS_SEL */
340 			(void) lwp_setprivate(lwp, _LWP_GSBASE,
341 			    (uintptr_t)grp[REG_GSBASE]);
342 	}
343 #else
344 	if (grp[GS] == LWPGS_SEL)
345 		(void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
346 #endif
347 
348 	lwp->lwp_eosys = JUSTRETURN;
349 	lwptot(lwp)->t_post_sys = 1;
350 }
351 
352 /*
353  * set syscall()'s return values for a lwp.
354  */
355 void
356 lwp_setrval(klwp_t *lwp, int v1, int v2)
357 {
358 	lwptoregs(lwp)->r_ps &= ~PS_C;
359 	lwptoregs(lwp)->r_r0 = v1;
360 	lwptoregs(lwp)->r_r1 = v2;
361 }
362 
363 /*
364  * set syscall()'s return values for a lwp.
365  */
366 void
367 lwp_setsp(klwp_t *lwp, caddr_t sp)
368 {
369 	lwptoregs(lwp)->r_sp = (intptr_t)sp;
370 }
371 
372 /*
373  * Copy regs from parent to child.
374  */
375 void
376 lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
377 {
378 #if defined(__amd64)
379 	struct pcb *pcb = &clwp->lwp_pcb;
380 	struct regs *rp = lwptoregs(lwp);
381 
382 	if (pcb->pcb_rupdate == 0) {
383 		pcb->pcb_ds = rp->r_ds;
384 		pcb->pcb_es = rp->r_es;
385 		pcb->pcb_fs = rp->r_fs;
386 		pcb->pcb_gs = rp->r_gs;
387 		pcb->pcb_rupdate = 1;
388 		lwptot(clwp)->t_post_sys = 1;
389 	}
390 	ASSERT(lwptot(clwp)->t_post_sys);
391 #endif
392 
393 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs));
394 }
395 
396 /*
397  * This function is currently unused on x86.
398  */
399 /*ARGSUSED*/
400 void
401 lwp_freeregs(klwp_t *lwp, int isexec)
402 {}
403 
404 /*
405  * This function is currently unused on x86.
406  */
407 void
408 lwp_pcb_exit(void)
409 {}
410 
411 /*
412  * Lwp context ops for segment registers.
413  */
414 
415 /*
416  * Every time we come into the kernel (syscall, interrupt or trap
417  * but not fast-traps) we capture the current values of the user's
418  * segment registers into the lwp's reg structure. This includes
419  * lcall for i386 generic system call support since it is handled
420  * as a segment-not-present trap.
421  *
422  * Here we save the current values from the lwp regs into the pcb
423  * and set pcb->pcb_rupdate to 1 to tell the rest of the kernel
424  * that the pcb copy of the segment registers is the current one.
425  * This ensures the lwp's next trip to user land via update_sregs.
426  * Finally we set t_post_sys to ensure that no system call fast-path's
427  * its way out of the kernel via sysret.
428  *
429  * (This means that we need to have interrupts disabled when we test
430  * t->t_post_sys in the syscall handlers; if the test fails, we need
431  * to keep interrupts disabled until we return to userland so we can't
432  * be switched away.)
433  *
434  * As a result of all this, we don't really have to do a whole lot if
435  * the thread is just mucking about in the kernel, switching on and
436  * off the cpu for whatever reason it feels like. And yet we still
437  * preserve fast syscalls, cause if we -don't- get descheduled,
438  * we never come here either.
439  */
440 
441 #define	VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \
442 	    (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL)
443 
444 /*ARGSUSED*/
445 void
446 lwp_segregs_save(klwp_t *lwp)
447 {
448 #if defined(__amd64)
449 	pcb_t *pcb = &lwp->lwp_pcb;
450 	struct regs *rp;
451 
452 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
453 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
454 
455 	if (pcb->pcb_rupdate == 0) {
456 		rp = lwptoregs(lwp);
457 
458 		/*
459 		 * If there's no update already pending, capture the current
460 		 * %ds/%es/%fs/%gs values from lwp's regs in case the user
461 		 * changed them; %fsbase and %gsbase are privileged so the
462 		 * kernel versions of these registers in pcb_fsbase and
463 		 * pcb_gsbase are always up-to-date.
464 		 */
465 		pcb->pcb_ds = rp->r_ds;
466 		pcb->pcb_es = rp->r_es;
467 		pcb->pcb_fs = rp->r_fs;
468 		pcb->pcb_gs = rp->r_gs;
469 		pcb->pcb_rupdate = 1;
470 		lwp->lwp_thread->t_post_sys = 1;
471 	}
472 #endif	/* __amd64 */
473 
474 #if !defined(__xpv)	/* XXPV not sure if we can re-read gdt? */
475 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc,
476 	    sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0);
477 	ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc,
478 	    sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0);
479 #endif
480 }
481 
482 #if defined(__amd64)
483 
484 /*
485  * Update the segment registers with new values from the pcb.
486  *
487  * We have to do this carefully, and in the following order,
488  * in case any of the selectors points at a bogus descriptor.
489  * If they do, we'll catch trap with on_trap and return 1.
490  * returns 0 on success.
491  *
492  * This is particularly tricky for %gs.
493  * This routine must be executed under a cli.
494  */
495 int
496 update_sregs(struct regs *rp,  klwp_t *lwp)
497 {
498 	pcb_t *pcb = &lwp->lwp_pcb;
499 	ulong_t	kgsbase;
500 	on_trap_data_t	otd;
501 	int rc = 0;
502 
503 	if (!on_trap(&otd, OT_SEGMENT_ACCESS)) {
504 
505 #if defined(__xpv)
506 		/*
507 		 * On the hyervisor this is easy. The hypercall below will
508 		 * swapgs and load %gs with the user selector. If the user
509 		 * selector is bad the hypervisor will catch the fault and
510 		 * load %gs with the null selector instead. Either way the
511 		 * kernel's gsbase is not damaged.
512 		 */
513 		kgsbase = (ulong_t)CPU;
514 		if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL,
515 		    pcb->pcb_gs) != 0) {
516 				no_trap();
517 				return (1);
518 		}
519 
520 		rp->r_gs = pcb->pcb_gs;
521 		ASSERT((cpu_t *)kgsbase == CPU);
522 
523 #else	/* __xpv */
524 
525 		/*
526 		 * A little more complicated running native.
527 		 */
528 		kgsbase = (ulong_t)CPU;
529 		__set_gs(pcb->pcb_gs);
530 
531 		/*
532 		 * If __set_gs fails it's because the new %gs is a bad %gs,
533 		 * we'll be taking a trap but with the original %gs and %gsbase
534 		 * undamaged (i.e. pointing at curcpu).
535 		 *
536 		 * We've just mucked up the kernel's gsbase.  Oops.  In
537 		 * particular we can't take any traps at all.  Make the newly
538 		 * computed gsbase be the hidden gs via __swapgs, and fix
539 		 * the kernel's gsbase back again. Later, when we return to
540 		 * userland we'll swapgs again restoring gsbase just loaded
541 		 * above.
542 		 */
543 		__swapgs();
544 		rp->r_gs = pcb->pcb_gs;
545 
546 		/*
547 		 * restore kernel's gsbase
548 		 */
549 		wrmsr(MSR_AMD_GSBASE, kgsbase);
550 
551 #endif	/* __xpv */
552 
553 		/*
554 		 * Only override the descriptor base address if
555 		 * r_gs == LWPGS_SEL or if r_gs == NULL. A note on
556 		 * NULL descriptors -- 32-bit programs take faults
557 		 * if they deference NULL descriptors; however,
558 		 * when 64-bit programs load them into %fs or %gs,
559 		 * they DONT fault -- only the base address remains
560 		 * whatever it was from the last load.   Urk.
561 		 *
562 		 * XXX - note that lwp_setprivate now sets %fs/%gs to the
563 		 * null selector for 64 bit processes. Whereas before
564 		 * %fs/%gs were set to LWP(FS|GS)_SEL regardless of
565 		 * the process's data model. For now we check for both
566 		 * values so that the kernel can also support the older
567 		 * libc. This should be ripped out at some point in the
568 		 * future.
569 		 */
570 		if (pcb->pcb_gs == LWPGS_SEL || pcb->pcb_gs == 0) {
571 #if defined(__xpv)
572 			if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER,
573 			    pcb->pcb_gsbase)) {
574 				no_trap();
575 				return (1);
576 			}
577 #else
578 			wrmsr(MSR_AMD_KGSBASE, pcb->pcb_gsbase);
579 #endif
580 		}
581 
582 		__set_ds(pcb->pcb_ds);
583 		rp->r_ds = pcb->pcb_ds;
584 
585 		__set_es(pcb->pcb_es);
586 		rp->r_es = pcb->pcb_es;
587 
588 		__set_fs(pcb->pcb_fs);
589 		rp->r_fs = pcb->pcb_fs;
590 
591 		/*
592 		 * Same as for %gs
593 		 */
594 		if (pcb->pcb_fs == LWPFS_SEL || pcb->pcb_fs == 0) {
595 #if defined(__xpv)
596 			if (HYPERVISOR_set_segment_base(SEGBASE_FS,
597 			    pcb->pcb_fsbase)) {
598 				no_trap();
599 				return (1);
600 			}
601 #else
602 			wrmsr(MSR_AMD_FSBASE, pcb->pcb_fsbase);
603 #endif
604 		}
605 
606 	} else {
607 		cli();
608 		rc = 1;
609 	}
610 	no_trap();
611 	return (rc);
612 }
613 
614 /*
615  * Make sure any stale selectors are cleared from the segment registers
616  * by putting KDS_SEL (the kernel's default %ds gdt selector) into them.
617  * This is necessary because the kernel itself does not use %es, %fs, nor
618  * %ds. (%cs and %ss are necessary, and are set up by the kernel - along with
619  * %gs - to point to the current cpu struct.) If we enter kmdb while in the
620  * kernel and resume with a stale ldt or brandz selector sitting there in a
621  * segment register, kmdb will #gp fault if the stale selector points to,
622  * for example, an ldt in the context of another process.
623  *
624  * WARNING: Intel and AMD chips behave differently when storing
625  * the null selector into %fs and %gs while in long mode. On AMD
626  * chips fsbase and gsbase are not cleared. But on Intel chips, storing
627  * a null selector into %fs or %gs has the side effect of clearing
628  * fsbase or gsbase. For that reason we use KDS_SEL, which has
629  * consistent behavor between AMD and Intel.
630  *
631  * Caller responsible for preventing cpu migration.
632  */
633 void
634 reset_sregs(void)
635 {
636 	ulong_t kgsbase = (ulong_t)CPU;
637 
638 	ASSERT(curthread->t_preempt != 0 || getpil() >= DISP_LEVEL);
639 
640 	cli();
641 	__set_gs(KGS_SEL);
642 
643 	/*
644 	 * restore kernel gsbase
645 	 */
646 #if defined(__xpv)
647 	xen_set_segment_base(SEGBASE_GS_KERNEL, kgsbase);
648 #else
649 	wrmsr(MSR_AMD_GSBASE, kgsbase);
650 #endif
651 
652 	sti();
653 
654 	__set_ds(KDS_SEL);
655 	__set_es(0 | SEL_KPL);	/* selector RPL not ring 0 on hypervisor */
656 	__set_fs(KFS_SEL);
657 }
658 
659 #endif	/* __amd64 */
660 
661 #ifdef _SYSCALL32_IMPL
662 
663 /*
664  * Make it impossible for a process to change its data model.
665  * We do this by toggling the present bits for the 32 and
666  * 64-bit user code descriptors. That way if a user lwp attempts
667  * to change its data model (by using the wrong code descriptor in
668  * %cs) it will fault immediately. This also allows us to simplify
669  * assertions and checks in the kernel.
670  */
671 
672 static void
673 gdt_ucode_model(model_t model)
674 {
675 	kpreempt_disable();
676 	if (model == DATAMODEL_NATIVE) {
677 		gdt_update_usegd(GDT_UCODE, &ucs_on);
678 		gdt_update_usegd(GDT_U32CODE, &ucs32_off);
679 	} else {
680 		gdt_update_usegd(GDT_U32CODE, &ucs32_on);
681 		gdt_update_usegd(GDT_UCODE, &ucs_off);
682 	}
683 	kpreempt_enable();
684 }
685 
686 #endif	/* _SYSCALL32_IMPL */
687 
688 /*
689  * Restore lwp private fs and gs segment descriptors
690  * on current cpu's GDT.
691  */
692 static void
693 lwp_segregs_restore(klwp_t *lwp)
694 {
695 	pcb_t *pcb = &lwp->lwp_pcb;
696 
697 	ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
698 	ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
699 
700 #ifdef	_SYSCALL32_IMPL
701 	gdt_ucode_model(DATAMODEL_NATIVE);
702 #endif
703 
704 	gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc);
705 	gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc);
706 
707 }
708 
709 #ifdef _SYSCALL32_IMPL
710 
711 static void
712 lwp_segregs_restore32(klwp_t *lwp)
713 {
714 	/*LINTED*/
715 	cpu_t *cpu = CPU;
716 	pcb_t *pcb = &lwp->lwp_pcb;
717 
718 	ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_fsdesc));
719 	ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_gsdesc));
720 
721 	gdt_ucode_model(DATAMODEL_ILP32);
722 	gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc);
723 	gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc);
724 }
725 
726 #endif	/* _SYSCALL32_IMPL */
727 
728 /*
729  * If this is a process in a branded zone, then we want it to use the brand
730  * syscall entry points instead of the standard Solaris entry points.  This
731  * routine must be called when a new lwp is created within a branded zone
732  * or when an existing lwp moves into a branded zone via a zone_enter()
733  * operation.
734  */
735 void
736 lwp_attach_brand_hdlrs(klwp_t *lwp)
737 {
738 	kthread_t *t = lwptot(lwp);
739 
740 	ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
741 
742 	ASSERT(removectx(t, NULL, brand_interpositioning_disable,
743 	    brand_interpositioning_enable, NULL, NULL,
744 	    brand_interpositioning_disable, NULL) == 0);
745 	installctx(t, NULL, brand_interpositioning_disable,
746 	    brand_interpositioning_enable, NULL, NULL,
747 	    brand_interpositioning_disable, NULL);
748 
749 	if (t == curthread) {
750 		kpreempt_disable();
751 		brand_interpositioning_enable();
752 		kpreempt_enable();
753 	}
754 }
755 
756 /*
757  * If this is a process in a branded zone, then we want it to disable the
758  * brand syscall entry points.  This routine must be called when the last
759  * lwp in a process is exiting in proc_exit().
760  */
761 void
762 lwp_detach_brand_hdlrs(klwp_t *lwp)
763 {
764 	kthread_t *t = lwptot(lwp);
765 
766 	ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
767 	if (t == curthread)
768 		kpreempt_disable();
769 
770 	/* Remove the original context handlers */
771 	VERIFY(removectx(t, NULL, brand_interpositioning_disable,
772 	    brand_interpositioning_enable, NULL, NULL,
773 	    brand_interpositioning_disable, NULL) != 0);
774 
775 	if (t == curthread) {
776 		/* Cleanup our MSR and IDT entries. */
777 		brand_interpositioning_disable();
778 		kpreempt_enable();
779 	}
780 }
781 
782 /*
783  * Add any lwp-associated context handlers to the lwp at the beginning
784  * of the lwp's useful life.
785  *
786  * All paths which create lwp's invoke lwp_create(); lwp_create()
787  * invokes lwp_stk_init() which initializes the stack, sets up
788  * lwp_regs, and invokes this routine.
789  *
790  * All paths which destroy lwp's invoke lwp_exit() to rip the lwp
791  * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it
792  * ends up in thread_free() which invokes freectx(t, 0) before
793  * invoking lwp_stk_fini().  When the lwp is recycled from death
794  * row, lwp_stk_fini() is invoked, then thread_free(), and thus
795  * freectx(t, 0) as before.
796  *
797  * In the case of exec, the surviving lwp is thoroughly scrubbed
798  * clean; exec invokes freectx(t, 1) to destroy associated contexts.
799  * On the way back to the new image, it invokes setregs() which
800  * in turn invokes this routine.
801  */
802 void
803 lwp_installctx(klwp_t *lwp)
804 {
805 	kthread_t *t = lwptot(lwp);
806 	int thisthread = t == curthread;
807 #ifdef _SYSCALL32_IMPL
808 	void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
809 	    lwp_segregs_restore : lwp_segregs_restore32;
810 #else
811 	void (*restop)(klwp_t *) = lwp_segregs_restore;
812 #endif
813 
814 	/*
815 	 * Install the basic lwp context handlers on each lwp.
816 	 *
817 	 * On the amd64 kernel, the context handlers are responsible for
818 	 * virtualizing %ds, %es, %fs, and %gs to the lwp.  The register
819 	 * values are only ever changed via sys_rtt when the
820 	 * pcb->pcb_rupdate == 1.  Only sys_rtt gets to clear the bit.
821 	 *
822 	 * On the i386 kernel, the context handlers are responsible for
823 	 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
824 	 */
825 	ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
826 	    NULL, NULL, NULL, NULL) == 0);
827 	if (thisthread)
828 		kpreempt_disable();
829 	installctx(t, lwp, lwp_segregs_save, restop,
830 	    NULL, NULL, NULL, NULL);
831 	if (thisthread) {
832 		/*
833 		 * Since we're the right thread, set the values in the GDT
834 		 */
835 		restop(lwp);
836 		kpreempt_enable();
837 	}
838 
839 	/*
840 	 * If we have sysenter/sysexit instructions enabled, we need
841 	 * to ensure that the hardware mechanism is kept up-to-date with the
842 	 * lwp's kernel stack pointer across context switches.
843 	 *
844 	 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
845 	 * it to the lwp's kernel stack pointer (kstktop).
846 	 */
847 	if (x86_feature & X86_SEP) {
848 #if defined(__amd64)
849 		caddr_t kstktop = (caddr_t)lwp->lwp_regs;
850 #elif defined(__i386)
851 		caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
852 		    SA(sizeof (struct regs) + MINFRAME);
853 #endif
854 		ASSERT(removectx(t, kstktop,
855 		    sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
856 
857 		if (thisthread)
858 			kpreempt_disable();
859 		installctx(t, kstktop,
860 		    sep_save, sep_restore, NULL, NULL, NULL, NULL);
861 		if (thisthread) {
862 			/*
863 			 * We're the right thread, so set the stack pointer
864 			 * for the first sysenter instruction to use
865 			 */
866 			sep_restore(kstktop);
867 			kpreempt_enable();
868 		}
869 	}
870 
871 	if (PROC_IS_BRANDED(ttoproc(t)))
872 		lwp_attach_brand_hdlrs(lwp);
873 }
874 
875 /*
876  * Clear registers on exec(2).
877  */
878 void
879 setregs(uarg_t *args)
880 {
881 	struct regs *rp;
882 	kthread_t *t = curthread;
883 	klwp_t *lwp = ttolwp(t);
884 	pcb_t *pcb = &lwp->lwp_pcb;
885 	greg_t sp;
886 
887 	/*
888 	 * Initialize user registers
889 	 */
890 	(void) save_syscall_args();	/* copy args from registers first */
891 	rp = lwptoregs(lwp);
892 	sp = rp->r_sp;
893 	bzero(rp, sizeof (*rp));
894 
895 	rp->r_ss = UDS_SEL;
896 	rp->r_sp = sp;
897 	rp->r_pc = args->entry;
898 	rp->r_ps = PSL_USER;
899 
900 #if defined(__amd64)
901 
902 	pcb->pcb_fs = pcb->pcb_gs = 0;
903 	pcb->pcb_fsbase = pcb->pcb_gsbase = 0;
904 
905 	if (ttoproc(t)->p_model == DATAMODEL_NATIVE) {
906 
907 		rp->r_cs = UCS_SEL;
908 
909 		/*
910 		 * Only allow 64-bit user code descriptor to be present.
911 		 */
912 		gdt_ucode_model(DATAMODEL_NATIVE);
913 
914 		/*
915 		 * Arrange that the virtualized %fs and %gs GDT descriptors
916 		 * have a well-defined initial state (present, ring 3
917 		 * and of type data).
918 		 */
919 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
920 
921 		/*
922 		 * thrptr is either NULL or a value used by DTrace.
923 		 * 64-bit processes use %fs as their "thread" register.
924 		 */
925 		if (args->thrptr)
926 			(void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr);
927 
928 	} else {
929 
930 		rp->r_cs = U32CS_SEL;
931 		rp->r_ds = rp->r_es = UDS_SEL;
932 
933 		/*
934 		 * only allow 32-bit user code selector to be present.
935 		 */
936 		gdt_ucode_model(DATAMODEL_ILP32);
937 
938 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
939 
940 		/*
941 		 * thrptr is either NULL or a value used by DTrace.
942 		 * 32-bit processes use %gs as their "thread" register.
943 		 */
944 		if (args->thrptr)
945 			(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
946 
947 	}
948 
949 	pcb->pcb_ds = rp->r_ds;
950 	pcb->pcb_es = rp->r_es;
951 	pcb->pcb_rupdate = 1;
952 
953 #elif defined(__i386)
954 
955 	rp->r_cs = UCS_SEL;
956 	rp->r_ds = rp->r_es = UDS_SEL;
957 
958 	/*
959 	 * Arrange that the virtualized %fs and %gs GDT descriptors
960 	 * have a well-defined initial state (present, ring 3
961 	 * and of type data).
962 	 */
963 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
964 
965 	/*
966 	 * For %gs we need to reset LWP_GSBASE in pcb and the
967 	 * per-cpu GDT descriptor. thrptr is either NULL
968 	 * or a value used by DTrace.
969 	 */
970 	if (args->thrptr)
971 		(void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
972 #endif
973 
974 	lwp->lwp_eosys = JUSTRETURN;
975 	t->t_post_sys = 1;
976 
977 	/*
978 	 * Here we initialize minimal fpu state.
979 	 * The rest is done at the first floating
980 	 * point instruction that a process executes.
981 	 */
982 	pcb->pcb_fpu.fpu_flags = 0;
983 
984 	/*
985 	 * Add the lwp context handlers that virtualize segment registers,
986 	 * and/or system call stacks etc.
987 	 */
988 	lwp_installctx(lwp);
989 }
990 
991 user_desc_t *
992 cpu_get_gdt(void)
993 {
994 	return (CPU->cpu_gdt);
995 }
996 
997 
998 #if !defined(lwp_getdatamodel)
999 
1000 /*
1001  * Return the datamodel of the given lwp.
1002  */
1003 /*ARGSUSED*/
1004 model_t
1005 lwp_getdatamodel(klwp_t *lwp)
1006 {
1007 	return (lwp->lwp_procp->p_model);
1008 }
1009 
1010 #endif	/* !lwp_getdatamodel */
1011 
1012 #if !defined(get_udatamodel)
1013 
1014 model_t
1015 get_udatamodel(void)
1016 {
1017 	return (curproc->p_model);
1018 }
1019 
1020 #endif	/* !get_udatamodel */
1021