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