17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21d4204c85Sraf 227c478bd9Sstevel@tonic-gate /* 23*e2c5185aSChristopher Kiick * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include "lint.h" 277c478bd9Sstevel@tonic-gate #include "thr_uberdata.h" 287c478bd9Sstevel@tonic-gate #include <procfs.h> 297c478bd9Sstevel@tonic-gate #include <ucontext.h> 307c478bd9Sstevel@tonic-gate #include <setjmp.h> 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *); 337c478bd9Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t); 347c478bd9Sstevel@tonic-gate 35373d25a2SRoger A. Faulkner /* ARGSUSED2 */ 36373d25a2SRoger A. Faulkner void * 37373d25a2SRoger A. Faulkner setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp) 38373d25a2SRoger A. Faulkner { 39373d25a2SRoger A. Faulkner uint64_t *stack; 40373d25a2SRoger A. Faulkner struct { 41373d25a2SRoger A. Faulkner uint64_t rpc; 42373d25a2SRoger A. Faulkner uint64_t fp; 43373d25a2SRoger A. Faulkner uint64_t pc; 44373d25a2SRoger A. Faulkner } frame; 45373d25a2SRoger A. Faulkner 46373d25a2SRoger A. Faulkner /* 47373d25a2SRoger A. Faulkner * Top-of-stack must be rounded down to STACK_ALIGN and 48373d25a2SRoger A. Faulkner * there must be a minimum frame. 49373d25a2SRoger A. Faulkner */ 50373d25a2SRoger A. Faulkner stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1)); 51373d25a2SRoger A. Faulkner 52373d25a2SRoger A. Faulkner /* 53373d25a2SRoger A. Faulkner * This will return NULL if the kernel cannot allocate 54373d25a2SRoger A. Faulkner * a page for the top page of the stack. This will cause 55373d25a2SRoger A. Faulkner * thr_create(), pthread_create() or pthread_attr_setstack() 56373d25a2SRoger A. Faulkner * to fail, passing the problem up to the application. 57373d25a2SRoger A. Faulkner */ 58373d25a2SRoger A. Faulkner stack -= 3; 59373d25a2SRoger A. Faulkner frame.pc = 0; 60373d25a2SRoger A. Faulkner frame.fp = 0; 61373d25a2SRoger A. Faulkner frame.rpc = (uint64_t)_lwp_start; 62373d25a2SRoger A. Faulkner if (uucopy(&frame, stack, sizeof (frame)) == 0) 63373d25a2SRoger A. Faulkner return (stack); 64373d25a2SRoger A. Faulkner return (NULL); 65373d25a2SRoger A. Faulkner } 66373d25a2SRoger A. Faulkner 677c478bd9Sstevel@tonic-gate int 687c478bd9Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *), 697c478bd9Sstevel@tonic-gate ulwp_t *ulwp, caddr_t stk, size_t stksize) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate uint64_t *stack; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* clear the context */ 748cd45542Sraf (void) memset(ucp, 0, sizeof (*ucp)); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* setup to store the current thread pointer in %fs */ 777c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_FSBASE] = (greg_t)ulwp; 78ae115bc7Smrj ucp->uc_mcontext.gregs[REG_FS] = 0; /* null selector indicates fsbase */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* all contexts should have a valid data segment descriptor for %ss */ 817c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL; 827c478bd9Sstevel@tonic-gate 83373d25a2SRoger A. Faulkner /* 84373d25a2SRoger A. Faulkner * Setup the top stack frame. 85373d25a2SRoger A. Faulkner * If this fails, pass the problem up to the application. 86373d25a2SRoger A. Faulkner */ 87373d25a2SRoger A. Faulkner if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL) 88373d25a2SRoger A. Faulkner return (ENOMEM); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* fill in registers of interest */ 917c478bd9Sstevel@tonic-gate ucp->uc_flags |= UC_CPU; 927c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp; 937c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func; 947c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack; 957c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack + 1); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate return (0); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Machine-dependent startup code for a newly-created thread. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate void * 1047257d1b4Sraf _thrp_setup(ulwp_t *self) 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz); 1077c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz; 1087c478bd9Sstevel@tonic-gate self->ul_ustack.ss_flags = 0; 1098cd45542Sraf (void) setustack(&self->ul_ustack); 110d4204c85Sraf 111d4204c85Sraf update_sched(self); 1127c478bd9Sstevel@tonic-gate tls_setup(); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* signals have been deferred until now */ 1157c478bd9Sstevel@tonic-gate sigon(self); 1167c478bd9Sstevel@tonic-gate 117d4204c85Sraf if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled) 118d4204c85Sraf return (NULL); /* cancelled by pthread_create() */ 1197c478bd9Sstevel@tonic-gate return (self->ul_startpc(self->ul_startarg)); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate void 1237c478bd9Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate ulwp->ul_fpuenv.ftag = 0xffffffff; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate void 1297c478bd9Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate lwpstatus_t status; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 1347c478bd9Sstevel@tonic-gate rs[REG_RBX] = status.pr_reg[REG_RBX]; 1357c478bd9Sstevel@tonic-gate rs[REG_R12] = status.pr_reg[REG_R12]; 1367c478bd9Sstevel@tonic-gate rs[REG_R13] = status.pr_reg[REG_R13]; 1377c478bd9Sstevel@tonic-gate rs[REG_R14] = status.pr_reg[REG_R14]; 1387c478bd9Sstevel@tonic-gate rs[REG_R15] = status.pr_reg[REG_R15]; 1397c478bd9Sstevel@tonic-gate rs[REG_RBP] = status.pr_reg[REG_RBP]; 1407c478bd9Sstevel@tonic-gate rs[REG_RSP] = status.pr_reg[REG_RSP]; 1417c478bd9Sstevel@tonic-gate rs[REG_RIP] = status.pr_reg[REG_RIP]; 1427c478bd9Sstevel@tonic-gate } else { 1437c478bd9Sstevel@tonic-gate rs[REG_RBX] = 0; 1447c478bd9Sstevel@tonic-gate rs[REG_R12] = 0; 1457c478bd9Sstevel@tonic-gate rs[REG_R13] = 0; 1467c478bd9Sstevel@tonic-gate rs[REG_R14] = 0; 1477c478bd9Sstevel@tonic-gate rs[REG_R15] = 0; 1487c478bd9Sstevel@tonic-gate rs[REG_RBP] = 0; 1497c478bd9Sstevel@tonic-gate rs[REG_RSP] = 0; 1507c478bd9Sstevel@tonic-gate rs[REG_RIP] = 0; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate void 1557c478bd9Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs) 1567c478bd9Sstevel@tonic-gate { 1577c478bd9Sstevel@tonic-gate lwpstatus_t status; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 1607c478bd9Sstevel@tonic-gate status.pr_reg[REG_RBX] = rs[REG_RBX]; 1617c478bd9Sstevel@tonic-gate status.pr_reg[REG_R12] = rs[REG_R12]; 1627c478bd9Sstevel@tonic-gate status.pr_reg[REG_R13] = rs[REG_R13]; 1637c478bd9Sstevel@tonic-gate status.pr_reg[REG_R14] = rs[REG_R14]; 1647c478bd9Sstevel@tonic-gate status.pr_reg[REG_R15] = rs[REG_R15]; 1657c478bd9Sstevel@tonic-gate status.pr_reg[REG_RBP] = rs[REG_RBP]; 1667c478bd9Sstevel@tonic-gate status.pr_reg[REG_RSP] = rs[REG_RSP]; 1677c478bd9Sstevel@tonic-gate status.pr_reg[REG_RIP] = rs[REG_RIP]; 1687c478bd9Sstevel@tonic-gate (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate int 1737c478bd9Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs) 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate /* LINTED alignment */ 1767c478bd9Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)env; 1777c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate ucp->uc_link = self->ul_siglink; 1807c478bd9Sstevel@tonic-gate if (self->ul_ustack.ss_flags & SS_ONSTACK) 1817c478bd9Sstevel@tonic-gate ucp->uc_stack = self->ul_ustack; 1827c478bd9Sstevel@tonic-gate else { 1837c478bd9Sstevel@tonic-gate ucp->uc_stack.ss_sp = 1847c478bd9Sstevel@tonic-gate (void *)(self->ul_stktop - self->ul_stksiz); 1857c478bd9Sstevel@tonic-gate ucp->uc_stack.ss_size = self->ul_stksiz; 1867c478bd9Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate ucp->uc_flags = UC_STACK | UC_CPU; 1897c478bd9Sstevel@tonic-gate if (savemask) { 1907c478bd9Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK; 1917c478bd9Sstevel@tonic-gate enter_critical(self); 1927c478bd9Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask; 1937c478bd9Sstevel@tonic-gate exit_critical(self); 1947c478bd9Sstevel@tonic-gate } 1958cd45542Sraf (void) memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t)); 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate return (0); 1987c478bd9Sstevel@tonic-gate } 199*e2c5185aSChristopher Kiick 200*e2c5185aSChristopher Kiick void 201*e2c5185aSChristopher Kiick smt_pause(void) 202*e2c5185aSChristopher Kiick { 203*e2c5185aSChristopher Kiick SMT_PAUSE(); 204*e2c5185aSChristopher Kiick } 205