preprocessor cleanup: __xpv
[unleashed.git] / arch / x86 / kernel / os / sundep.c
blobeb5b83d4eb43c7d567ae79c331aff3a8a7d393a6
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
27 /* All Rights Reserved */
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/sysmacros.h>
32 #include <sys/signal.h>
33 #include <sys/systm.h>
34 #include <sys/user.h>
35 #include <sys/mman.h>
36 #include <sys/class.h>
37 #include <sys/proc.h>
38 #include <sys/procfs.h>
39 #include <sys/buf.h>
40 #include <sys/kmem.h>
41 #include <sys/cred.h>
42 #include <sys/archsystm.h>
43 #include <sys/vmparam.h>
44 #include <sys/prsystm.h>
45 #include <sys/reboot.h>
46 #include <sys/uadmin.h>
47 #include <sys/vfs.h>
48 #include <sys/vnode.h>
49 #include <sys/file.h>
50 #include <sys/session.h>
51 #include <sys/ucontext.h>
52 #include <sys/dnlc.h>
53 #include <sys/var.h>
54 #include <sys/cmn_err.h>
55 #include <sys/debugreg.h>
56 #include <sys/thread.h>
57 #include <sys/vtrace.h>
58 #include <sys/consdev.h>
59 #include <sys/psw.h>
60 #include <sys/regset.h>
61 #include <sys/privregs.h>
62 #include <sys/cpu.h>
63 #include <sys/stack.h>
64 #include <sys/swap.h>
65 #include <vm/hat.h>
66 #include <vm/anon.h>
67 #include <vm/as.h>
68 #include <vm/page.h>
69 #include <vm/seg.h>
70 #include <vm/seg_kmem.h>
71 #include <vm/seg_map.h>
72 #include <vm/seg_vn.h>
73 #include <sys/exec.h>
74 #include <sys/acct.h>
75 #include <sys/core.h>
76 #include <sys/corectl.h>
77 #include <sys/modctl.h>
78 #include <sys/tuneable.h>
79 #include <c2/audit.h>
80 #include <sys/bootconf.h>
81 #include <sys/brand.h>
82 #include <sys/dumphdr.h>
83 #include <sys/promif.h>
84 #include <sys/systeminfo.h>
85 #include <sys/kdi.h>
86 #include <sys/contract_impl.h>
87 #include <sys/x86_archext.h>
88 #include <sys/segments.h>
89 #include <sys/ontrap.h>
90 #include <sys/cpu.h>
93 * Compare the version of boot that boot says it is against
94 * the version of boot the kernel expects.
96 int
97 check_boot_version(int boots_version)
99 if (boots_version == BO_VERSION)
100 return (0);
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*/
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 * 4) the number of contiguous memory ranges
116 void
117 installed_top_size_ex(
118 struct memlist *list, /* pointer to start of installed list */
119 pfn_t *high_pfn, /* return ptr for top value */
120 pgcnt_t *pgcnt, /* return ptr for sum of installed pages */
121 int *ranges) /* return ptr for the count of contig. ranges */
123 pfn_t top = 0;
124 pgcnt_t sumpages = 0;
125 pfn_t highp; /* high page in a chunk */
126 int cnt = 0;
128 for (; list; list = list->ml_next) {
129 ++cnt;
130 highp = (list->ml_address + list->ml_size - 1) >> PAGESHIFT;
131 if (top < highp)
132 top = highp;
133 sumpages += btop(list->ml_size);
136 *high_pfn = top;
137 *pgcnt = sumpages;
138 *ranges = cnt;
141 void
142 installed_top_size(
143 struct memlist *list, /* pointer to start of installed list */
144 pfn_t *high_pfn, /* return ptr for top value */
145 pgcnt_t *pgcnt) /* return ptr for sum of installed pages */
147 int ranges;
149 installed_top_size_ex(list, high_pfn, pgcnt, &ranges);
152 void
153 phys_install_has_changed(void)
157 * Copy in a memory list from boot to kernel, with a filter function
158 * to remove pages. The filter function can increase the address and/or
159 * decrease the size to filter out pages. It will also align addresses and
160 * sizes to PAGESIZE.
162 void
163 copy_memlist_filter(
164 struct memlist *src,
165 struct memlist **dstp,
166 void (*filter)(uint64_t *, uint64_t *))
168 struct memlist *dst, *prev;
169 uint64_t addr;
170 uint64_t size;
171 uint64_t eaddr;
173 dst = *dstp;
174 prev = dst;
177 * Move through the memlist applying a filter against
178 * each range of memory. Note that we may apply the
179 * filter multiple times against each memlist entry.
181 for (; src; src = src->ml_next) {
182 addr = P2ROUNDUP(src->ml_address, PAGESIZE);
183 eaddr = P2ALIGN(src->ml_address + src->ml_size, PAGESIZE);
184 while (addr < eaddr) {
185 size = eaddr - addr;
186 if (filter != NULL)
187 filter(&addr, &size);
188 if (size == 0)
189 break;
190 dst->ml_address = addr;
191 dst->ml_size = size;
192 dst->ml_next = 0;
193 if (prev == dst) {
194 dst->ml_prev = 0;
195 dst++;
196 } else {
197 dst->ml_prev = prev;
198 prev->ml_next = dst;
199 dst++;
200 prev++;
202 addr += size;
206 *dstp = dst;
210 * Kernel setup code, called from startup().
212 void
213 kern_setup1(void)
215 proc_t *pp;
217 pp = &p0;
219 proc_sched = pp;
222 * Initialize process 0 data structures
224 pp->p_stat = SRUN;
225 pp->p_flag = SSYS;
227 pp->p_pidp = &pid0;
228 pp->p_pgidp = &pid0;
229 pp->p_sessp = &session0;
230 pp->p_tlist = &t0;
231 pid0.pid_pglink = pp;
232 pid0.pid_pgtail = pp;
235 * XXX - we asssume that the u-area is zeroed out except for
236 * ttolwp(curthread)->lwp_regs.
238 PTOU(curproc)->u_cmask = (mode_t)CMASK;
240 thread_init(); /* init thread_free list */
241 pid_init(); /* initialize pid (proc) table */
242 contract_init(); /* initialize contracts */
244 init_pages_pp_maximum();
248 * Load a procedure into a thread.
250 void
251 thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len)
253 caddr_t sp;
254 size_t framesz;
255 caddr_t argp;
256 long *p;
257 extern void thread_start();
260 * Push a "c" call frame onto the stack to represent
261 * the caller of "start".
263 sp = t->t_stk;
264 ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0);
265 if (len != 0) {
267 * the object that arg points at is copied into the
268 * caller's frame.
270 framesz = SA(len);
271 sp -= framesz;
272 ASSERT(sp > t->t_stkbase);
273 argp = sp + SA(MINFRAME);
274 bcopy(arg, argp, len);
275 arg = argp;
278 * Set up arguments (arg and len) on the caller's stack frame.
280 p = (long *)sp;
282 *--p = 0; /* fake call */
283 *--p = 0; /* null frame pointer terminates stack trace */
284 *--p = (long)len;
285 *--p = (intptr_t)arg;
286 *--p = (intptr_t)start;
289 * initialize thread to resume at thread_start() which will
290 * turn around and invoke (*start)(arg, len).
292 t->t_pc = (uintptr_t)thread_start;
293 t->t_sp = (uintptr_t)p;
295 ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0);
299 * load user registers into lwp.
301 /*ARGSUSED2*/
302 void
303 lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
305 struct regs *rp = lwptoregs(lwp);
307 setgregs(lwp, grp);
308 rp->r_ps = PSL_USER;
311 * For 64-bit lwps, we allow one magic %fs selector value, and one
312 * magic %gs selector to point anywhere in the address space using
313 * %fsbase and %gsbase behind the scenes. libc uses %fs to point
314 * at the ulwp_t structure.
316 * For 32-bit lwps, libc wedges its lwp thread pointer into the
317 * ucontext ESP slot (which is otherwise irrelevant to setting a
318 * ucontext) and LWPGS_SEL value into gregs[REG_GS]. This is so
319 * syslwp_create() can atomically setup %gs.
321 * See setup_context() in libc.
323 #ifdef _SYSCALL32_IMPL
324 if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) {
325 if (grp[REG_GS] == LWPGS_SEL)
326 (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
327 } else {
329 * See lwp_setprivate in kernel and setup_context in libc.
331 * Currently libc constructs a ucontext from whole cloth for
332 * every new (not main) lwp created. For 64 bit processes
333 * %fsbase is directly set to point to current thread pointer.
334 * In the past (solaris 10) %fs was also set LWPFS_SEL to
335 * indicate %fsbase. Now we use the null GDT selector for
336 * this purpose. LWP[FS|GS]_SEL are only intended for 32 bit
337 * processes. To ease transition we support older libcs in
338 * the newer kernel by forcing %fs or %gs selector to null
339 * by calling lwp_setprivate if LWP[FS|GS]_SEL is passed in
340 * the ucontext. This is should be ripped out at some future
341 * date. Another fix would be for libc to do a getcontext
342 * and inherit the null %fs/%gs from the current context but
343 * that means an extra system call and could hurt performance.
345 if (grp[REG_FS] == 0x1bb) /* hard code legacy LWPFS_SEL */
346 (void) lwp_setprivate(lwp, _LWP_FSBASE,
347 (uintptr_t)grp[REG_FSBASE]);
349 if (grp[REG_GS] == 0x1c3) /* hard code legacy LWPGS_SEL */
350 (void) lwp_setprivate(lwp, _LWP_GSBASE,
351 (uintptr_t)grp[REG_GSBASE]);
353 #else
354 if (grp[GS] == LWPGS_SEL)
355 (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr);
356 #endif
358 lwp->lwp_eosys = JUSTRETURN;
359 lwptot(lwp)->t_post_sys = 1;
363 * set syscall()'s return values for a lwp.
365 void
366 lwp_setrval(klwp_t *lwp, int v1, int v2)
368 lwptoregs(lwp)->r_ps &= ~PS_C;
369 lwptoregs(lwp)->r_r0 = v1;
370 lwptoregs(lwp)->r_r1 = v2;
374 * set syscall()'s return values for a lwp.
376 void
377 lwp_setsp(klwp_t *lwp, caddr_t sp)
379 lwptoregs(lwp)->r_sp = (intptr_t)sp;
383 * Copy regs from parent to child.
385 void
386 lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
388 #if defined(__amd64)
389 struct pcb *pcb = &clwp->lwp_pcb;
390 struct regs *rp = lwptoregs(lwp);
392 if (pcb->pcb_rupdate == 0) {
393 pcb->pcb_ds = rp->r_ds;
394 pcb->pcb_es = rp->r_es;
395 pcb->pcb_fs = rp->r_fs;
396 pcb->pcb_gs = rp->r_gs;
397 pcb->pcb_rupdate = 1;
398 lwptot(clwp)->t_post_sys = 1;
400 ASSERT(lwptot(clwp)->t_post_sys);
401 #endif
403 bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs));
407 * This function is currently unused on x86.
409 /*ARGSUSED*/
410 void
411 lwp_freeregs(klwp_t *lwp, int isexec)
415 * This function is currently unused on x86.
417 void
418 lwp_pcb_exit(void)
422 * Lwp context ops for segment registers.
426 * Every time we come into the kernel (syscall, interrupt or trap
427 * but not fast-traps) we capture the current values of the user's
428 * segment registers into the lwp's reg structure. This includes
429 * lcall for i386 generic system call support since it is handled
430 * as a segment-not-present trap.
432 * Here we save the current values from the lwp regs into the pcb
433 * and set pcb->pcb_rupdate to 1 to tell the rest of the kernel
434 * that the pcb copy of the segment registers is the current one.
435 * This ensures the lwp's next trip to user land via update_sregs.
436 * Finally we set t_post_sys to ensure that no system call fast-path's
437 * its way out of the kernel via sysret.
439 * (This means that we need to have interrupts disabled when we test
440 * t->t_post_sys in the syscall handlers; if the test fails, we need
441 * to keep interrupts disabled until we return to userland so we can't
442 * be switched away.)
444 * As a result of all this, we don't really have to do a whole lot if
445 * the thread is just mucking about in the kernel, switching on and
446 * off the cpu for whatever reason it feels like. And yet we still
447 * preserve fast syscalls, cause if we -don't- get descheduled,
448 * we never come here either.
451 #define VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \
452 (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL)
454 /*ARGSUSED*/
455 void
456 lwp_segregs_save(klwp_t *lwp)
458 #if defined(__amd64)
459 pcb_t *pcb = &lwp->lwp_pcb;
460 struct regs *rp;
462 ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
463 ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
465 if (pcb->pcb_rupdate == 0) {
466 rp = lwptoregs(lwp);
469 * If there's no update already pending, capture the current
470 * %ds/%es/%fs/%gs values from lwp's regs in case the user
471 * changed them; %fsbase and %gsbase are privileged so the
472 * kernel versions of these registers in pcb_fsbase and
473 * pcb_gsbase are always up-to-date.
475 pcb->pcb_ds = rp->r_ds;
476 pcb->pcb_es = rp->r_es;
477 pcb->pcb_fs = rp->r_fs;
478 pcb->pcb_gs = rp->r_gs;
479 pcb->pcb_rupdate = 1;
480 lwp->lwp_thread->t_post_sys = 1;
482 #endif /* __amd64 */
484 #if !defined(__xpv) /* XXPV not sure if we can re-read gdt? */
485 ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc,
486 sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0);
487 ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc,
488 sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0);
489 #endif
492 #if defined(__amd64)
495 * Update the segment registers with new values from the pcb.
497 * We have to do this carefully, and in the following order,
498 * in case any of the selectors points at a bogus descriptor.
499 * If they do, we'll catch trap with on_trap and return 1.
500 * returns 0 on success.
502 * This is particularly tricky for %gs.
503 * This routine must be executed under a cli.
506 update_sregs(struct regs *rp, klwp_t *lwp)
508 pcb_t *pcb = &lwp->lwp_pcb;
509 ulong_t kgsbase;
510 on_trap_data_t otd;
511 int rc = 0;
513 if (!on_trap(&otd, OT_SEGMENT_ACCESS)) {
515 #if defined(__xpv)
517 * On the hyervisor this is easy. The hypercall below will
518 * swapgs and load %gs with the user selector. If the user
519 * selector is bad the hypervisor will catch the fault and
520 * load %gs with the null selector instead. Either way the
521 * kernel's gsbase is not damaged.
523 kgsbase = (ulong_t)CPU;
524 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL,
525 pcb->pcb_gs) != 0) {
526 no_trap();
527 return (1);
530 rp->r_gs = pcb->pcb_gs;
531 ASSERT((cpu_t *)kgsbase == CPU);
533 #else /* __xpv */
536 * A little more complicated running native.
538 kgsbase = (ulong_t)CPU;
539 __set_gs(pcb->pcb_gs);
542 * If __set_gs fails it's because the new %gs is a bad %gs,
543 * we'll be taking a trap but with the original %gs and %gsbase
544 * undamaged (i.e. pointing at curcpu).
546 * We've just mucked up the kernel's gsbase. Oops. In
547 * particular we can't take any traps at all. Make the newly
548 * computed gsbase be the hidden gs via __swapgs, and fix
549 * the kernel's gsbase back again. Later, when we return to
550 * userland we'll swapgs again restoring gsbase just loaded
551 * above.
553 __swapgs();
554 rp->r_gs = pcb->pcb_gs;
557 * restore kernel's gsbase
559 wrmsr(MSR_AMD_GSBASE, kgsbase);
561 #endif /* __xpv */
564 * Only override the descriptor base address if
565 * r_gs == LWPGS_SEL or if r_gs == NULL. A note on
566 * NULL descriptors -- 32-bit programs take faults
567 * if they deference NULL descriptors; however,
568 * when 64-bit programs load them into %fs or %gs,
569 * they DONT fault -- only the base address remains
570 * whatever it was from the last load. Urk.
572 * XXX - note that lwp_setprivate now sets %fs/%gs to the
573 * null selector for 64 bit processes. Whereas before
574 * %fs/%gs were set to LWP(FS|GS)_SEL regardless of
575 * the process's data model. For now we check for both
576 * values so that the kernel can also support the older
577 * libc. This should be ripped out at some point in the
578 * future.
580 if (pcb->pcb_gs == LWPGS_SEL || pcb->pcb_gs == 0) {
581 #if defined(__xpv)
582 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER,
583 pcb->pcb_gsbase)) {
584 no_trap();
585 return (1);
587 #else
588 wrmsr(MSR_AMD_KGSBASE, pcb->pcb_gsbase);
589 #endif
592 __set_ds(pcb->pcb_ds);
593 rp->r_ds = pcb->pcb_ds;
595 __set_es(pcb->pcb_es);
596 rp->r_es = pcb->pcb_es;
598 __set_fs(pcb->pcb_fs);
599 rp->r_fs = pcb->pcb_fs;
602 * Same as for %gs
604 if (pcb->pcb_fs == LWPFS_SEL || pcb->pcb_fs == 0) {
605 #if defined(__xpv)
606 if (HYPERVISOR_set_segment_base(SEGBASE_FS,
607 pcb->pcb_fsbase)) {
608 no_trap();
609 return (1);
611 #else
612 wrmsr(MSR_AMD_FSBASE, pcb->pcb_fsbase);
613 #endif
616 } else {
617 cli();
618 rc = 1;
620 no_trap();
621 return (rc);
625 * Make sure any stale selectors are cleared from the segment registers
626 * by putting KDS_SEL (the kernel's default %ds gdt selector) into them.
627 * This is necessary because the kernel itself does not use %es, %fs, nor
628 * %ds. (%cs and %ss are necessary, and are set up by the kernel - along with
629 * %gs - to point to the current cpu struct.) If we enter kmdb while in the
630 * kernel and resume with a stale ldt or brandz selector sitting there in a
631 * segment register, kmdb will #gp fault if the stale selector points to,
632 * for example, an ldt in the context of another process.
634 * WARNING: Intel and AMD chips behave differently when storing
635 * the null selector into %fs and %gs while in long mode. On AMD
636 * chips fsbase and gsbase are not cleared. But on Intel chips, storing
637 * a null selector into %fs or %gs has the side effect of clearing
638 * fsbase or gsbase. For that reason we use KDS_SEL, which has
639 * consistent behavor between AMD and Intel.
641 * Caller responsible for preventing cpu migration.
643 void
644 reset_sregs(void)
646 ulong_t kgsbase = (ulong_t)CPU;
648 ASSERT(curthread->t_preempt != 0 || getpil() >= DISP_LEVEL);
650 cli();
651 __set_gs(KGS_SEL);
654 * restore kernel gsbase
656 #if defined(__xpv)
657 xen_set_segment_base(SEGBASE_GS_KERNEL, kgsbase);
658 #else
659 wrmsr(MSR_AMD_GSBASE, kgsbase);
660 #endif
662 sti();
664 __set_ds(KDS_SEL);
665 __set_es(0 | SEL_KPL); /* selector RPL not ring 0 on hypervisor */
666 __set_fs(KFS_SEL);
669 #endif /* __amd64 */
671 #ifdef _SYSCALL32_IMPL
674 * Make it impossible for a process to change its data model.
675 * We do this by toggling the present bits for the 32 and
676 * 64-bit user code descriptors. That way if a user lwp attempts
677 * to change its data model (by using the wrong code descriptor in
678 * %cs) it will fault immediately. This also allows us to simplify
679 * assertions and checks in the kernel.
682 static void
683 gdt_ucode_model(model_t model)
685 kpreempt_disable();
686 if (model == DATAMODEL_NATIVE) {
687 gdt_update_usegd(GDT_UCODE, &ucs_on);
688 gdt_update_usegd(GDT_U32CODE, &ucs32_off);
689 } else {
690 gdt_update_usegd(GDT_U32CODE, &ucs32_on);
691 gdt_update_usegd(GDT_UCODE, &ucs_off);
693 kpreempt_enable();
696 #endif /* _SYSCALL32_IMPL */
699 * Restore lwp private fs and gs segment descriptors
700 * on current cpu's GDT.
702 static void
703 lwp_segregs_restore(klwp_t *lwp)
705 pcb_t *pcb = &lwp->lwp_pcb;
707 ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc));
708 ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc));
710 #ifdef _SYSCALL32_IMPL
711 gdt_ucode_model(DATAMODEL_NATIVE);
712 #endif
714 gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc);
715 gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc);
719 #ifdef _SYSCALL32_IMPL
721 static void
722 lwp_segregs_restore32(klwp_t *lwp)
724 /*LINTED*/
725 cpu_t *cpu = CPU;
726 pcb_t *pcb = &lwp->lwp_pcb;
728 ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_fsdesc));
729 ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_gsdesc));
731 gdt_ucode_model(DATAMODEL_ILP32);
732 gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc);
733 gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc);
736 #endif /* _SYSCALL32_IMPL */
739 * If this is a process in a branded zone, then we want it to use the brand
740 * syscall entry points instead of the standard Solaris entry points. This
741 * routine must be called when a new lwp is created within a branded zone
742 * or when an existing lwp moves into a branded zone via a zone_enter()
743 * operation.
745 void
746 lwp_attach_brand_hdlrs(klwp_t *lwp)
748 kthread_t *t = lwptot(lwp);
750 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
752 ASSERT(removectx(t, NULL, brand_interpositioning_disable,
753 brand_interpositioning_enable, NULL, NULL,
754 brand_interpositioning_disable, NULL) == 0);
755 installctx(t, NULL, brand_interpositioning_disable,
756 brand_interpositioning_enable, NULL, NULL,
757 brand_interpositioning_disable, NULL);
759 if (t == curthread) {
760 kpreempt_disable();
761 brand_interpositioning_enable();
762 kpreempt_enable();
767 * If this is a process in a branded zone, then we want it to disable the
768 * brand syscall entry points. This routine must be called when the last
769 * lwp in a process is exiting in proc_exit().
771 void
772 lwp_detach_brand_hdlrs(klwp_t *lwp)
774 kthread_t *t = lwptot(lwp);
776 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
777 if (t == curthread)
778 kpreempt_disable();
780 /* Remove the original context handlers */
781 VERIFY(removectx(t, NULL, brand_interpositioning_disable,
782 brand_interpositioning_enable, NULL, NULL,
783 brand_interpositioning_disable, NULL) != 0);
785 if (t == curthread) {
786 /* Cleanup our MSR and IDT entries. */
787 brand_interpositioning_disable();
788 kpreempt_enable();
793 * Add any lwp-associated context handlers to the lwp at the beginning
794 * of the lwp's useful life.
796 * All paths which create lwp's invoke lwp_create(); lwp_create()
797 * invokes lwp_stk_init() which initializes the stack, sets up
798 * lwp_regs, and invokes this routine.
800 * All paths which destroy lwp's invoke lwp_exit() to rip the lwp
801 * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it
802 * ends up in thread_free() which invokes freectx(t, 0) before
803 * invoking lwp_stk_fini(). When the lwp is recycled from death
804 * row, lwp_stk_fini() is invoked, then thread_free(), and thus
805 * freectx(t, 0) as before.
807 * In the case of exec, the surviving lwp is thoroughly scrubbed
808 * clean; exec invokes freectx(t, 1) to destroy associated contexts.
809 * On the way back to the new image, it invokes setregs() which
810 * in turn invokes this routine.
812 void
813 lwp_installctx(klwp_t *lwp)
815 kthread_t *t = lwptot(lwp);
816 int thisthread = t == curthread;
817 #ifdef _SYSCALL32_IMPL
818 void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
819 lwp_segregs_restore : lwp_segregs_restore32;
820 #else
821 void (*restop)(klwp_t *) = lwp_segregs_restore;
822 #endif
825 * Install the basic lwp context handlers on each lwp.
827 * On the amd64 kernel, the context handlers are responsible for
828 * virtualizing %ds, %es, %fs, and %gs to the lwp. The register
829 * values are only ever changed via sys_rtt when the
830 * pcb->pcb_rupdate == 1. Only sys_rtt gets to clear the bit.
832 * On the i386 kernel, the context handlers are responsible for
833 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
835 ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
836 NULL, NULL, NULL, NULL) == 0);
837 if (thisthread)
838 kpreempt_disable();
839 installctx(t, lwp, lwp_segregs_save, restop,
840 NULL, NULL, NULL, NULL);
841 if (thisthread) {
843 * Since we're the right thread, set the values in the GDT
845 restop(lwp);
846 kpreempt_enable();
850 * If we have sysenter/sysexit instructions enabled, we need
851 * to ensure that the hardware mechanism is kept up-to-date with the
852 * lwp's kernel stack pointer across context switches.
854 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
855 * it to the lwp's kernel stack pointer (kstktop).
857 if (is_x86_feature(x86_featureset, X86FSET_SEP)) {
858 #if defined(__amd64)
859 caddr_t kstktop = (caddr_t)lwp->lwp_regs;
860 #elif defined(__i386)
861 caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
862 SA(sizeof (struct regs) + MINFRAME);
863 #endif
864 ASSERT(removectx(t, kstktop,
865 sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
867 if (thisthread)
868 kpreempt_disable();
869 installctx(t, kstktop,
870 sep_save, sep_restore, NULL, NULL, NULL, NULL);
871 if (thisthread) {
873 * We're the right thread, so set the stack pointer
874 * for the first sysenter instruction to use
876 sep_restore(kstktop);
877 kpreempt_enable();
881 if (PROC_IS_BRANDED(ttoproc(t)))
882 lwp_attach_brand_hdlrs(lwp);
886 * Clear registers on exec(2).
888 void
889 setregs(uarg_t *args)
891 struct regs *rp;
892 kthread_t *t = curthread;
893 klwp_t *lwp = ttolwp(t);
894 pcb_t *pcb = &lwp->lwp_pcb;
895 greg_t sp;
898 * Initialize user registers
900 (void) save_syscall_args(); /* copy args from registers first */
901 rp = lwptoregs(lwp);
902 sp = rp->r_sp;
903 bzero(rp, sizeof (*rp));
905 rp->r_ss = UDS_SEL;
906 rp->r_sp = sp;
907 rp->r_pc = args->entry;
908 rp->r_ps = PSL_USER;
910 #if defined(__amd64)
912 pcb->pcb_fs = pcb->pcb_gs = 0;
913 pcb->pcb_fsbase = pcb->pcb_gsbase = 0;
915 if (ttoproc(t)->p_model == DATAMODEL_NATIVE) {
917 rp->r_cs = UCS_SEL;
920 * Only allow 64-bit user code descriptor to be present.
922 gdt_ucode_model(DATAMODEL_NATIVE);
925 * Arrange that the virtualized %fs and %gs GDT descriptors
926 * have a well-defined initial state (present, ring 3
927 * and of type data).
929 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
932 * thrptr is either NULL or a value used by DTrace.
933 * 64-bit processes use %fs as their "thread" register.
935 if (args->thrptr)
936 (void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr);
938 } else {
940 rp->r_cs = U32CS_SEL;
941 rp->r_ds = rp->r_es = UDS_SEL;
944 * only allow 32-bit user code selector to be present.
946 gdt_ucode_model(DATAMODEL_ILP32);
948 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
951 * thrptr is either NULL or a value used by DTrace.
952 * 32-bit processes use %gs as their "thread" register.
954 if (args->thrptr)
955 (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
959 pcb->pcb_ds = rp->r_ds;
960 pcb->pcb_es = rp->r_es;
961 pcb->pcb_rupdate = 1;
963 #elif defined(__i386)
965 rp->r_cs = UCS_SEL;
966 rp->r_ds = rp->r_es = UDS_SEL;
969 * Arrange that the virtualized %fs and %gs GDT descriptors
970 * have a well-defined initial state (present, ring 3
971 * and of type data).
973 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
976 * For %gs we need to reset LWP_GSBASE in pcb and the
977 * per-cpu GDT descriptor. thrptr is either NULL
978 * or a value used by DTrace.
980 if (args->thrptr)
981 (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr);
982 #endif
984 lwp->lwp_eosys = JUSTRETURN;
985 t->t_post_sys = 1;
988 * Here we initialize minimal fpu state.
989 * The rest is done at the first floating
990 * point instruction that a process executes.
992 pcb->pcb_fpu.fpu_flags = 0;
995 * Add the lwp context handlers that virtualize segment registers,
996 * and/or system call stacks etc.
998 lwp_installctx(lwp);
1001 user_desc_t *
1002 cpu_get_gdt(void)
1004 return (CPU->cpu_gdt);
1008 #if !defined(lwp_getdatamodel)
1011 * Return the datamodel of the given lwp.
1013 /*ARGSUSED*/
1014 model_t
1015 lwp_getdatamodel(klwp_t *lwp)
1017 return (lwp->lwp_procp->p_model);
1020 #endif /* !lwp_getdatamodel */
1022 #if !defined(get_udatamodel)
1024 model_t
1025 get_udatamodel(void)
1027 return (curproc->p_model);
1030 #endif /* !get_udatamodel */