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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include "lint.h"
270Sstevel@tonic-gate #include "thr_uberdata.h"
280Sstevel@tonic-gate #include <procfs.h>
290Sstevel@tonic-gate #include <ucontext.h>
300Sstevel@tonic-gate #include <setjmp.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
330Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
340Sstevel@tonic-gate
357657SRoger.Faulkner@Sun.COM /* ARGSUSED2 */
367657SRoger.Faulkner@Sun.COM void *
setup_top_frame(void * stk,size_t stksize,ulwp_t * ulwp)377657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
387657SRoger.Faulkner@Sun.COM {
397657SRoger.Faulkner@Sun.COM uint64_t *stack;
407657SRoger.Faulkner@Sun.COM struct {
417657SRoger.Faulkner@Sun.COM uint64_t rpc;
427657SRoger.Faulkner@Sun.COM uint64_t fp;
437657SRoger.Faulkner@Sun.COM uint64_t pc;
447657SRoger.Faulkner@Sun.COM } frame;
457657SRoger.Faulkner@Sun.COM
467657SRoger.Faulkner@Sun.COM /*
477657SRoger.Faulkner@Sun.COM * Top-of-stack must be rounded down to STACK_ALIGN and
487657SRoger.Faulkner@Sun.COM * there must be a minimum frame.
497657SRoger.Faulkner@Sun.COM */
507657SRoger.Faulkner@Sun.COM stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
517657SRoger.Faulkner@Sun.COM
527657SRoger.Faulkner@Sun.COM /*
537657SRoger.Faulkner@Sun.COM * This will return NULL if the kernel cannot allocate
547657SRoger.Faulkner@Sun.COM * a page for the top page of the stack. This will cause
557657SRoger.Faulkner@Sun.COM * thr_create(), pthread_create() or pthread_attr_setstack()
567657SRoger.Faulkner@Sun.COM * to fail, passing the problem up to the application.
577657SRoger.Faulkner@Sun.COM */
587657SRoger.Faulkner@Sun.COM stack -= 3;
597657SRoger.Faulkner@Sun.COM frame.pc = 0;
607657SRoger.Faulkner@Sun.COM frame.fp = 0;
617657SRoger.Faulkner@Sun.COM frame.rpc = (uint64_t)_lwp_start;
627657SRoger.Faulkner@Sun.COM if (uucopy(&frame, stack, sizeof (frame)) == 0)
637657SRoger.Faulkner@Sun.COM return (stack);
647657SRoger.Faulkner@Sun.COM return (NULL);
657657SRoger.Faulkner@Sun.COM }
667657SRoger.Faulkner@Sun.COM
670Sstevel@tonic-gate int
setup_context(ucontext_t * ucp,void * (* func)(ulwp_t *),ulwp_t * ulwp,caddr_t stk,size_t stksize)680Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
690Sstevel@tonic-gate ulwp_t *ulwp, caddr_t stk, size_t stksize)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate uint64_t *stack;
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* clear the context */
746515Sraf (void) memset(ucp, 0, sizeof (*ucp));
750Sstevel@tonic-gate
760Sstevel@tonic-gate /* setup to store the current thread pointer in %fs */
770Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_FSBASE] = (greg_t)ulwp;
783446Smrj ucp->uc_mcontext.gregs[REG_FS] = 0; /* null selector indicates fsbase */
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* all contexts should have a valid data segment descriptor for %ss */
810Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL;
820Sstevel@tonic-gate
837657SRoger.Faulkner@Sun.COM /*
847657SRoger.Faulkner@Sun.COM * Setup the top stack frame.
857657SRoger.Faulkner@Sun.COM * If this fails, pass the problem up to the application.
867657SRoger.Faulkner@Sun.COM */
877657SRoger.Faulkner@Sun.COM if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
887657SRoger.Faulkner@Sun.COM return (ENOMEM);
890Sstevel@tonic-gate
900Sstevel@tonic-gate /* fill in registers of interest */
910Sstevel@tonic-gate ucp->uc_flags |= UC_CPU;
920Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp;
930Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func;
940Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack;
957657SRoger.Faulkner@Sun.COM ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack + 1);
960Sstevel@tonic-gate
970Sstevel@tonic-gate return (0);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * Machine-dependent startup code for a newly-created thread.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate void *
_thrp_setup(ulwp_t * self)1046812Sraf _thrp_setup(ulwp_t *self)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1070Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz;
1080Sstevel@tonic-gate self->ul_ustack.ss_flags = 0;
1096515Sraf (void) setustack(&self->ul_ustack);
1106247Sraf
1116247Sraf update_sched(self);
1120Sstevel@tonic-gate tls_setup();
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /* signals have been deferred until now */
1150Sstevel@tonic-gate sigon(self);
1160Sstevel@tonic-gate
1176247Sraf if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1186247Sraf return (NULL); /* cancelled by pthread_create() */
1190Sstevel@tonic-gate return (self->ul_startpc(self->ul_startarg));
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate void
_fpinherit(ulwp_t * ulwp)1230Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate ulwp->ul_fpuenv.ftag = 0xffffffff;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate void
getgregs(ulwp_t * ulwp,gregset_t rs)1290Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate lwpstatus_t status;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1340Sstevel@tonic-gate rs[REG_RBX] = status.pr_reg[REG_RBX];
1350Sstevel@tonic-gate rs[REG_R12] = status.pr_reg[REG_R12];
1360Sstevel@tonic-gate rs[REG_R13] = status.pr_reg[REG_R13];
1370Sstevel@tonic-gate rs[REG_R14] = status.pr_reg[REG_R14];
1380Sstevel@tonic-gate rs[REG_R15] = status.pr_reg[REG_R15];
1390Sstevel@tonic-gate rs[REG_RBP] = status.pr_reg[REG_RBP];
1400Sstevel@tonic-gate rs[REG_RSP] = status.pr_reg[REG_RSP];
1410Sstevel@tonic-gate rs[REG_RIP] = status.pr_reg[REG_RIP];
1420Sstevel@tonic-gate } else {
1430Sstevel@tonic-gate rs[REG_RBX] = 0;
1440Sstevel@tonic-gate rs[REG_R12] = 0;
1450Sstevel@tonic-gate rs[REG_R13] = 0;
1460Sstevel@tonic-gate rs[REG_R14] = 0;
1470Sstevel@tonic-gate rs[REG_R15] = 0;
1480Sstevel@tonic-gate rs[REG_RBP] = 0;
1490Sstevel@tonic-gate rs[REG_RSP] = 0;
1500Sstevel@tonic-gate rs[REG_RIP] = 0;
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate void
setgregs(ulwp_t * ulwp,gregset_t rs)1550Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate lwpstatus_t status;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1600Sstevel@tonic-gate status.pr_reg[REG_RBX] = rs[REG_RBX];
1610Sstevel@tonic-gate status.pr_reg[REG_R12] = rs[REG_R12];
1620Sstevel@tonic-gate status.pr_reg[REG_R13] = rs[REG_R13];
1630Sstevel@tonic-gate status.pr_reg[REG_R14] = rs[REG_R14];
1640Sstevel@tonic-gate status.pr_reg[REG_R15] = rs[REG_R15];
1650Sstevel@tonic-gate status.pr_reg[REG_RBP] = rs[REG_RBP];
1660Sstevel@tonic-gate status.pr_reg[REG_RSP] = rs[REG_RSP];
1670Sstevel@tonic-gate status.pr_reg[REG_RIP] = rs[REG_RIP];
1680Sstevel@tonic-gate (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate int
__csigsetjmp(sigjmp_buf env,int savemask,gregset_t rs)1730Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate /* LINTED alignment */
1760Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)env;
1770Sstevel@tonic-gate ulwp_t *self = curthread;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate ucp->uc_link = self->ul_siglink;
1800Sstevel@tonic-gate if (self->ul_ustack.ss_flags & SS_ONSTACK)
1810Sstevel@tonic-gate ucp->uc_stack = self->ul_ustack;
1820Sstevel@tonic-gate else {
1830Sstevel@tonic-gate ucp->uc_stack.ss_sp =
1846247Sraf (void *)(self->ul_stktop - self->ul_stksiz);
1850Sstevel@tonic-gate ucp->uc_stack.ss_size = self->ul_stksiz;
1860Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate ucp->uc_flags = UC_STACK | UC_CPU;
1890Sstevel@tonic-gate if (savemask) {
1900Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK;
1910Sstevel@tonic-gate enter_critical(self);
1920Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask;
1930Sstevel@tonic-gate exit_critical(self);
1940Sstevel@tonic-gate }
1956515Sraf (void) memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t));
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate return (0);
1980Sstevel@tonic-gate }
199*13081SChris.Kiick@Sun.COM
200*13081SChris.Kiick@Sun.COM void
smt_pause(void)201*13081SChris.Kiick@Sun.COM smt_pause(void)
202*13081SChris.Kiick@Sun.COM {
203*13081SChris.Kiick@Sun.COM SMT_PAUSE();
204*13081SChris.Kiick@Sun.COM }
205