10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53446Smrj * Common Development and Distribution License (the "License").
63446Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216247Sraf
220Sstevel@tonic-gate /*
23*13081SChris.Kiick@Sun.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include "thr_uberdata.h"
270Sstevel@tonic-gate #include <procfs.h>
280Sstevel@tonic-gate #include <ucontext.h>
290Sstevel@tonic-gate #include <setjmp.h>
300Sstevel@tonic-gate
3110607SRoger.Faulkner@Sun.COM /*
3210607SRoger.Faulkner@Sun.COM * The i386 ABI says that the stack pointer need be only 4-byte aligned
3310607SRoger.Faulkner@Sun.COM * before a function call (STACK_ALIGN == 4). We use a 16-byte stack
3410607SRoger.Faulkner@Sun.COM * alignment for the benefit of floating point code compiled using sse2.
3510607SRoger.Faulkner@Sun.COM * Even though the i386 ABI doesn't require it, both cc and gcc
3610607SRoger.Faulkner@Sun.COM * assume this alignment on entry to a function and maintain it
3710607SRoger.Faulkner@Sun.COM * for calls made from that function. If the stack is initially
3810607SRoger.Faulkner@Sun.COM * aligned on a 16-byte boundary, it will continue to be so aligned.
3910607SRoger.Faulkner@Sun.COM * If it is not initially so aligned, it will never become so aligned.
4010607SRoger.Faulkner@Sun.COM */
4110607SRoger.Faulkner@Sun.COM #undef STACK_ALIGN
4210607SRoger.Faulkner@Sun.COM #define STACK_ALIGN 16
4310607SRoger.Faulkner@Sun.COM
440Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
450Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
460Sstevel@tonic-gate
477657SRoger.Faulkner@Sun.COM void *
setup_top_frame(void * stk,size_t stksize,ulwp_t * ulwp)487657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
497657SRoger.Faulkner@Sun.COM {
507657SRoger.Faulkner@Sun.COM uint32_t *stack;
517657SRoger.Faulkner@Sun.COM struct {
527657SRoger.Faulkner@Sun.COM uint32_t rpc;
537657SRoger.Faulkner@Sun.COM uint32_t arg;
5410607SRoger.Faulkner@Sun.COM uint32_t pad;
557657SRoger.Faulkner@Sun.COM uint32_t fp;
567657SRoger.Faulkner@Sun.COM uint32_t pc;
577657SRoger.Faulkner@Sun.COM } frame;
587657SRoger.Faulkner@Sun.COM
597657SRoger.Faulkner@Sun.COM /*
607657SRoger.Faulkner@Sun.COM * Top-of-stack must be rounded down to STACK_ALIGN and
6110607SRoger.Faulkner@Sun.COM * there must be a minimum frame. Note: 'frame' is not a true
6210607SRoger.Faulkner@Sun.COM * stack frame (see <sys/frame.h>) but a construction made here to
6310607SRoger.Faulkner@Sun.COM * make it look like _lwp_start called the thread start function
6410607SRoger.Faulkner@Sun.COM * with a 16-byte aligned stack pointer (the address of frame.arg
6510607SRoger.Faulkner@Sun.COM * is the address that muet be aligned on a 16-byte boundary).
667657SRoger.Faulkner@Sun.COM */
677657SRoger.Faulkner@Sun.COM stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
687657SRoger.Faulkner@Sun.COM
697657SRoger.Faulkner@Sun.COM /*
707657SRoger.Faulkner@Sun.COM * This will return NULL if the kernel cannot allocate
717657SRoger.Faulkner@Sun.COM * a page for the top page of the stack. This will cause
727657SRoger.Faulkner@Sun.COM * thr_create(), pthread_create() or pthread_attr_setstack()
737657SRoger.Faulkner@Sun.COM * to fail, passing the problem up to the application.
747657SRoger.Faulkner@Sun.COM */
7510607SRoger.Faulkner@Sun.COM stack -= 5; /* make the address of frame.arg be 16-byte aligned */
767657SRoger.Faulkner@Sun.COM frame.pc = 0;
7710607SRoger.Faulkner@Sun.COM frame.fp = 0; /* initial address for %ebp (see EBP below) */
7810607SRoger.Faulkner@Sun.COM frame.pad = 0;
797657SRoger.Faulkner@Sun.COM frame.arg = (uint32_t)ulwp;
807657SRoger.Faulkner@Sun.COM frame.rpc = (uint32_t)_lwp_start;
817657SRoger.Faulkner@Sun.COM if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
827657SRoger.Faulkner@Sun.COM return (stack);
837657SRoger.Faulkner@Sun.COM return (NULL);
847657SRoger.Faulkner@Sun.COM }
857657SRoger.Faulkner@Sun.COM
860Sstevel@tonic-gate int
setup_context(ucontext_t * ucp,void * (* func)(ulwp_t *),ulwp_t * ulwp,caddr_t stk,size_t stksize)870Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
880Sstevel@tonic-gate ulwp_t *ulwp, caddr_t stk, size_t stksize)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate static int initialized;
910Sstevel@tonic-gate static greg_t fs, es, ds, cs, ss;
920Sstevel@tonic-gate
930Sstevel@tonic-gate uint32_t *stack;
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (!initialized) {
960Sstevel@tonic-gate ucontext_t uc;
970Sstevel@tonic-gate
980Sstevel@tonic-gate /* do this once to load the segment registers */
990Sstevel@tonic-gate uc.uc_flags = UC_CPU;
1006515Sraf (void) __getcontext(&uc);
1010Sstevel@tonic-gate fs = uc.uc_mcontext.gregs[FS];
1020Sstevel@tonic-gate es = uc.uc_mcontext.gregs[ES];
1030Sstevel@tonic-gate ds = uc.uc_mcontext.gregs[DS];
1040Sstevel@tonic-gate cs = uc.uc_mcontext.gregs[CS];
1050Sstevel@tonic-gate ss = uc.uc_mcontext.gregs[SS];
1060Sstevel@tonic-gate initialized = 1;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate /* clear the context and set the segment registers */
1096515Sraf (void) memset(ucp, 0, sizeof (*ucp));
1100Sstevel@tonic-gate ucp->uc_mcontext.gregs[FS] = fs;
1110Sstevel@tonic-gate ucp->uc_mcontext.gregs[ES] = es;
1120Sstevel@tonic-gate ucp->uc_mcontext.gregs[DS] = ds;
1130Sstevel@tonic-gate ucp->uc_mcontext.gregs[CS] = cs;
1140Sstevel@tonic-gate ucp->uc_mcontext.gregs[SS] = ss;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * Yuck.
1180Sstevel@tonic-gate * Use unused kernel pointer field in ucontext
1190Sstevel@tonic-gate * to pass down self pointer and set %gs selector
1200Sstevel@tonic-gate * value so __lwp_create() can setup %gs atomically.
1210Sstevel@tonic-gate * Without this we would need to block all signals
1226812Sraf * and directly call ___lwp_private() in _thrp_setup
1230Sstevel@tonic-gate * on the other side of __lwp_create().
1240Sstevel@tonic-gate */
1250Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
1263446Smrj ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
1270Sstevel@tonic-gate
1287657SRoger.Faulkner@Sun.COM /*
1297657SRoger.Faulkner@Sun.COM * Setup the top stack frame.
1307657SRoger.Faulkner@Sun.COM * If this fails, pass the problem up to the application.
1317657SRoger.Faulkner@Sun.COM */
1327657SRoger.Faulkner@Sun.COM if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
1337657SRoger.Faulkner@Sun.COM return (ENOMEM);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /* fill in registers of interest */
1360Sstevel@tonic-gate ucp->uc_flags |= UC_CPU;
1370Sstevel@tonic-gate ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
1380Sstevel@tonic-gate ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
13910607SRoger.Faulkner@Sun.COM ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 3);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate return (0);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * Machine-dependent startup code for a newly-created thread.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate void *
_thrp_setup(ulwp_t * self)1486812Sraf _thrp_setup(ulwp_t *self)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1510Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz;
1520Sstevel@tonic-gate self->ul_ustack.ss_flags = 0;
1536515Sraf (void) setustack(&self->ul_ustack);
1540Sstevel@tonic-gate
1556247Sraf update_sched(self);
1560Sstevel@tonic-gate tls_setup();
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /* signals have been deferred until now */
1590Sstevel@tonic-gate sigon(self);
1600Sstevel@tonic-gate
1616247Sraf if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1626247Sraf return (NULL); /* cancelled by pthread_create() */
1630Sstevel@tonic-gate return (self->ul_startpc(self->ul_startarg));
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate void
_fpinherit(ulwp_t * ulwp)1670Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate ulwp->ul_fpuenv.ftag = 0xffffffff;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate void
getgregs(ulwp_t * ulwp,gregset_t rs)1730Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate lwpstatus_t status;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1780Sstevel@tonic-gate rs[EIP] = status.pr_reg[EIP];
1790Sstevel@tonic-gate rs[EDI] = status.pr_reg[EDI];
1800Sstevel@tonic-gate rs[ESI] = status.pr_reg[ESI];
1810Sstevel@tonic-gate rs[EBP] = status.pr_reg[EBP];
1820Sstevel@tonic-gate rs[EBX] = status.pr_reg[EBX];
1830Sstevel@tonic-gate rs[UESP] = status.pr_reg[UESP];
1840Sstevel@tonic-gate } else {
1850Sstevel@tonic-gate rs[EIP] = 0;
1860Sstevel@tonic-gate rs[EDI] = 0;
1870Sstevel@tonic-gate rs[ESI] = 0;
1880Sstevel@tonic-gate rs[EBP] = 0;
1890Sstevel@tonic-gate rs[EBX] = 0;
1900Sstevel@tonic-gate rs[UESP] = 0;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate void
setgregs(ulwp_t * ulwp,gregset_t rs)1950Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate lwpstatus_t status;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2000Sstevel@tonic-gate status.pr_reg[EIP] = rs[EIP];
2010Sstevel@tonic-gate status.pr_reg[EDI] = rs[EDI];
2020Sstevel@tonic-gate status.pr_reg[ESI] = rs[ESI];
2030Sstevel@tonic-gate status.pr_reg[EBP] = rs[EBP];
2040Sstevel@tonic-gate status.pr_reg[EBX] = rs[EBX];
2050Sstevel@tonic-gate status.pr_reg[UESP] = rs[UESP];
2060Sstevel@tonic-gate (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate int
__csigsetjmp(greg_t cs,greg_t ss,greg_t gs,greg_t fs,greg_t es,greg_t ds,greg_t edi,greg_t esi,greg_t ebp,greg_t esp,greg_t ebx,greg_t edx,greg_t ecx,greg_t eax,greg_t eip,sigjmp_buf env,int savemask)2110Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
2120Sstevel@tonic-gate greg_t fs, greg_t es, greg_t ds,
2130Sstevel@tonic-gate greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
2140Sstevel@tonic-gate greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
2150Sstevel@tonic-gate sigjmp_buf env, int savemask)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)env;
2180Sstevel@tonic-gate ulwp_t *self = curthread;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate ucp->uc_link = self->ul_siglink;
2210Sstevel@tonic-gate if (self->ul_ustack.ss_flags & SS_ONSTACK)
2220Sstevel@tonic-gate ucp->uc_stack = self->ul_ustack;
2230Sstevel@tonic-gate else {
2240Sstevel@tonic-gate ucp->uc_stack.ss_sp =
2256247Sraf (void *)(self->ul_stktop - self->ul_stksiz);
2260Sstevel@tonic-gate ucp->uc_stack.ss_size = self->ul_stksiz;
2270Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate ucp->uc_flags = UC_STACK | UC_CPU;
2300Sstevel@tonic-gate if (savemask) {
2310Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK;
2320Sstevel@tonic-gate enter_critical(self);
2330Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask;
2340Sstevel@tonic-gate exit_critical(self);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate ucp->uc_mcontext.gregs[GS] = gs;
2370Sstevel@tonic-gate ucp->uc_mcontext.gregs[FS] = fs;
2380Sstevel@tonic-gate ucp->uc_mcontext.gregs[ES] = es;
2390Sstevel@tonic-gate ucp->uc_mcontext.gregs[DS] = ds;
2400Sstevel@tonic-gate ucp->uc_mcontext.gregs[EDI] = edi;
2410Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESI] = esi;
2420Sstevel@tonic-gate ucp->uc_mcontext.gregs[EBP] = ebp;
2430Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESP] = esp + 4;
2440Sstevel@tonic-gate ucp->uc_mcontext.gregs[EBX] = ebx;
2450Sstevel@tonic-gate ucp->uc_mcontext.gregs[EDX] = edx;
2460Sstevel@tonic-gate ucp->uc_mcontext.gregs[ECX] = ecx;
2470Sstevel@tonic-gate ucp->uc_mcontext.gregs[EAX] = eax;
2480Sstevel@tonic-gate ucp->uc_mcontext.gregs[TRAPNO] = 0;
2490Sstevel@tonic-gate ucp->uc_mcontext.gregs[ERR] = 0;
2500Sstevel@tonic-gate ucp->uc_mcontext.gregs[EIP] = eip;
2510Sstevel@tonic-gate ucp->uc_mcontext.gregs[CS] = cs;
2520Sstevel@tonic-gate ucp->uc_mcontext.gregs[EFL] = 0;
2530Sstevel@tonic-gate ucp->uc_mcontext.gregs[UESP] = esp + 4;
2540Sstevel@tonic-gate ucp->uc_mcontext.gregs[SS] = ss;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate return (0);
2570Sstevel@tonic-gate }
258*13081SChris.Kiick@Sun.COM
259*13081SChris.Kiick@Sun.COM void
smt_pause(void)260*13081SChris.Kiick@Sun.COM smt_pause(void)
261*13081SChris.Kiick@Sun.COM {
262*13081SChris.Kiick@Sun.COM SMT_PAUSE();
263*13081SChris.Kiick@Sun.COM }
264