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]
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>
36 #include <sys/class.h>
38 #include <sys/procfs.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>
48 #include <sys/vnode.h>
50 #include <sys/session.h>
51 #include <sys/ucontext.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>
60 #include <sys/regset.h>
61 #include <sys/privregs.h>
63 #include <sys/stack.h>
70 #include <vm/seg_kmem.h>
71 #include <vm/seg_map.h>
72 #include <vm/seg_vn.h>
76 #include <sys/corectl.h>
77 #include <sys/modctl.h>
78 #include <sys/tuneable.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>
86 #include <sys/contract_impl.h>
87 #include <sys/x86_archext.h>
88 #include <sys/segments.h>
89 #include <sys/ontrap.h>
93 * Compare the version of boot that boot says it is against
94 * the version of boot the kernel expects.
97 check_boot_version(int boots_version
)
99 if (boots_version
== BO_VERSION
)
102 prom_printf("Wrong boot interface - kernel needs v%d found v%d\n",
103 BO_VERSION
, boots_version
);
104 prom_panic("halting");
109 * Process the physical installed list for boot.
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
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 */
124 pgcnt_t sumpages
= 0;
125 pfn_t highp
; /* high page in a chunk */
128 for (; list
; list
= list
->ml_next
) {
130 highp
= (list
->ml_address
+ list
->ml_size
- 1) >> PAGESHIFT
;
133 sumpages
+= btop(list
->ml_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 */
149 installed_top_size_ex(list
, high_pfn
, pgcnt
, &ranges
);
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
165 struct memlist
**dstp
,
166 void (*filter
)(uint64_t *, uint64_t *))
168 struct memlist
*dst
, *prev
;
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
) {
187 filter(&addr
, &size
);
190 dst
->ml_address
= addr
;
210 * Kernel setup code, called from startup().
222 * Initialize process 0 data structures
229 pp
->p_sessp
= &session0
;
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.
251 thread_load(kthread_t
*t
, void (*start
)(), caddr_t arg
, size_t len
)
257 extern void thread_start();
260 * Push a "c" call frame onto the stack to represent
261 * the caller of "start".
264 ASSERT(((uintptr_t)t
->t_stk
& (STACK_ENTRY_ALIGN
- 1)) == 0);
267 * the object that arg points at is copied into the
272 ASSERT(sp
> t
->t_stkbase
);
273 argp
= sp
+ SA(MINFRAME
);
274 bcopy(arg
, argp
, len
);
278 * Set up arguments (arg and len) on the caller's stack frame.
282 *--p
= 0; /* fake call */
283 *--p
= 0; /* null frame pointer terminates stack trace */
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.
303 lwp_load(klwp_t
*lwp
, gregset_t grp
, uintptr_t thrptr
)
305 struct regs
*rp
= lwptoregs(lwp
);
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
);
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
]);
354 if (grp
[GS
] == LWPGS_SEL
)
355 (void) lwp_setprivate(lwp
, _LWP_GSBASE
, thrptr
);
358 lwp
->lwp_eosys
= JUSTRETURN
;
359 lwptot(lwp
)->t_post_sys
= 1;
363 * set syscall()'s return values for a lwp.
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.
377 lwp_setsp(klwp_t
*lwp
, caddr_t sp
)
379 lwptoregs(lwp
)->r_sp
= (intptr_t)sp
;
383 * Copy regs from parent to child.
386 lwp_forkregs(klwp_t
*lwp
, klwp_t
*clwp
)
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
);
403 bcopy(lwp
->lwp_regs
, clwp
->lwp_regs
, sizeof (struct regs
));
407 * This function is currently unused on x86.
411 lwp_freeregs(klwp_t
*lwp
, int isexec
)
415 * This function is currently unused on x86.
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
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)
456 lwp_segregs_save(klwp_t
*lwp
)
459 pcb_t
*pcb
= &lwp
->lwp_pcb
;
462 ASSERT(VALID_LWP_DESC(&pcb
->pcb_fsdesc
));
463 ASSERT(VALID_LWP_DESC(&pcb
->pcb_gsdesc
));
465 if (pcb
->pcb_rupdate
== 0) {
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;
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);
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
;
513 if (!on_trap(&otd
, OT_SEGMENT_ACCESS
)) {
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
,
530 rp
->r_gs
= pcb
->pcb_gs
;
531 ASSERT((cpu_t
*)kgsbase
== CPU
);
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
554 rp
->r_gs
= pcb
->pcb_gs
;
557 * restore kernel's gsbase
559 wrmsr(MSR_AMD_GSBASE
, kgsbase
);
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
580 if (pcb
->pcb_gs
== LWPGS_SEL
|| pcb
->pcb_gs
== 0) {
582 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER
,
588 wrmsr(MSR_AMD_KGSBASE
, pcb
->pcb_gsbase
);
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
;
604 if (pcb
->pcb_fs
== LWPFS_SEL
|| pcb
->pcb_fs
== 0) {
606 if (HYPERVISOR_set_segment_base(SEGBASE_FS
,
612 wrmsr(MSR_AMD_FSBASE
, pcb
->pcb_fsbase
);
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.
646 ulong_t kgsbase
= (ulong_t
)CPU
;
648 ASSERT(curthread
->t_preempt
!= 0 || getpil() >= DISP_LEVEL
);
654 * restore kernel gsbase
657 xen_set_segment_base(SEGBASE_GS_KERNEL
, kgsbase
);
659 wrmsr(MSR_AMD_GSBASE
, kgsbase
);
665 __set_es(0 | SEL_KPL
); /* selector RPL not ring 0 on hypervisor */
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.
683 gdt_ucode_model(model_t model
)
686 if (model
== DATAMODEL_NATIVE
) {
687 gdt_update_usegd(GDT_UCODE
, &ucs_on
);
688 gdt_update_usegd(GDT_U32CODE
, &ucs32_off
);
690 gdt_update_usegd(GDT_U32CODE
, &ucs32_on
);
691 gdt_update_usegd(GDT_UCODE
, &ucs_off
);
696 #endif /* _SYSCALL32_IMPL */
699 * Restore lwp private fs and gs segment descriptors
700 * on current cpu's GDT.
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
);
714 gdt_update_usegd(GDT_LWPFS
, &pcb
->pcb_fsdesc
);
715 gdt_update_usegd(GDT_LWPGS
, &pcb
->pcb_gsdesc
);
719 #ifdef _SYSCALL32_IMPL
722 lwp_segregs_restore32(klwp_t
*lwp
)
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()
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
) {
761 brand_interpositioning_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().
772 lwp_detach_brand_hdlrs(klwp_t
*lwp
)
774 kthread_t
*t
= lwptot(lwp
);
776 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp
)));
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();
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.
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
;
821 void (*restop
)(klwp_t
*) = lwp_segregs_restore
;
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);
839 installctx(t
, lwp
, lwp_segregs_save
, restop
,
840 NULL
, NULL
, NULL
, NULL
);
843 * Since we're the right thread, set the values in the GDT
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
)) {
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
);
864 ASSERT(removectx(t
, kstktop
,
865 sep_save
, sep_restore
, NULL
, NULL
, NULL
, NULL
) == 0);
869 installctx(t
, kstktop
,
870 sep_save
, sep_restore
, NULL
, NULL
, NULL
, NULL
);
873 * We're the right thread, so set the stack pointer
874 * for the first sysenter instruction to use
876 sep_restore(kstktop
);
881 if (PROC_IS_BRANDED(ttoproc(t
)))
882 lwp_attach_brand_hdlrs(lwp
);
886 * Clear registers on exec(2).
889 setregs(uarg_t
*args
)
892 kthread_t
*t
= curthread
;
893 klwp_t
*lwp
= ttolwp(t
);
894 pcb_t
*pcb
= &lwp
->lwp_pcb
;
898 * Initialize user registers
900 (void) save_syscall_args(); /* copy args from registers first */
903 bzero(rp
, sizeof (*rp
));
907 rp
->r_pc
= args
->entry
;
912 pcb
->pcb_fs
= pcb
->pcb_gs
= 0;
913 pcb
->pcb_fsbase
= pcb
->pcb_gsbase
= 0;
915 if (ttoproc(t
)->p_model
== DATAMODEL_NATIVE
) {
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
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.
936 (void) lwp_setprivate(lwp
, _LWP_FSBASE
, args
->thrptr
);
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.
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)
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
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.
981 (void) lwp_setprivate(lwp
, _LWP_GSBASE
, args
->thrptr
);
984 lwp
->lwp_eosys
= JUSTRETURN
;
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.
1004 return (CPU
->cpu_gdt
);
1008 #if !defined(lwp_getdatamodel)
1011 * Return the datamodel of the given lwp.
1015 lwp_getdatamodel(klwp_t
*lwp
)
1017 return (lwp
->lwp_procp
->p_model
);
1020 #endif /* !lwp_getdatamodel */
1022 #if !defined(get_udatamodel)
1025 get_udatamodel(void)
1027 return (curproc
->p_model
);
1030 #endif /* !get_udatamodel */