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