1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include "lint.h" 28 #include "thr_uberdata.h" 29 #include <procfs.h> 30 #include <ucontext.h> 31 #include <setjmp.h> 32 33 extern int getlwpstatus(thread_t, lwpstatus_t *); 34 extern int putlwpregs(thread_t, prgregset_t); 35 36 /* ARGSUSED2 */ 37 void * 38 setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp) 39 { 40 uint64_t *stack; 41 struct { 42 uint64_t rpc; 43 uint64_t fp; 44 uint64_t pc; 45 } frame; 46 47 /* 48 * Top-of-stack must be rounded down to STACK_ALIGN and 49 * there must be a minimum frame. 50 */ 51 stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1)); 52 53 /* 54 * This will return NULL if the kernel cannot allocate 55 * a page for the top page of the stack. This will cause 56 * thr_create(), pthread_create() or pthread_attr_setstack() 57 * to fail, passing the problem up to the application. 58 */ 59 stack -= 3; 60 frame.pc = 0; 61 frame.fp = 0; 62 frame.rpc = (uint64_t)_lwp_start; 63 if (uucopy(&frame, stack, sizeof (frame)) == 0) 64 return (stack); 65 return (NULL); 66 } 67 68 int 69 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *), 70 ulwp_t *ulwp, caddr_t stk, size_t stksize) 71 { 72 uint64_t *stack; 73 74 /* clear the context */ 75 (void) memset(ucp, 0, sizeof (*ucp)); 76 77 /* setup to store the current thread pointer in %fs */ 78 ucp->uc_mcontext.gregs[REG_FSBASE] = (greg_t)ulwp; 79 ucp->uc_mcontext.gregs[REG_FS] = 0; /* null selector indicates fsbase */ 80 81 /* all contexts should have a valid data segment descriptor for %ss */ 82 ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL; 83 84 /* 85 * Setup the top stack frame. 86 * If this fails, pass the problem up to the application. 87 */ 88 if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL) 89 return (ENOMEM); 90 91 /* fill in registers of interest */ 92 ucp->uc_flags |= UC_CPU; 93 ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp; 94 ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func; 95 ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack; 96 ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack + 1); 97 98 return (0); 99 } 100 101 /* 102 * Machine-dependent startup code for a newly-created thread. 103 */ 104 void * 105 _thrp_setup(ulwp_t *self) 106 { 107 self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz); 108 self->ul_ustack.ss_size = self->ul_stksiz; 109 self->ul_ustack.ss_flags = 0; 110 (void) setustack(&self->ul_ustack); 111 112 update_sched(self); 113 tls_setup(); 114 115 /* signals have been deferred until now */ 116 sigon(self); 117 118 if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled) 119 return (NULL); /* cancelled by pthread_create() */ 120 return (self->ul_startpc(self->ul_startarg)); 121 } 122 123 void 124 _fpinherit(ulwp_t *ulwp) 125 { 126 ulwp->ul_fpuenv.ftag = 0xffffffff; 127 } 128 129 void 130 getgregs(ulwp_t *ulwp, gregset_t rs) 131 { 132 lwpstatus_t status; 133 134 if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 135 rs[REG_RBX] = status.pr_reg[REG_RBX]; 136 rs[REG_R12] = status.pr_reg[REG_R12]; 137 rs[REG_R13] = status.pr_reg[REG_R13]; 138 rs[REG_R14] = status.pr_reg[REG_R14]; 139 rs[REG_R15] = status.pr_reg[REG_R15]; 140 rs[REG_RBP] = status.pr_reg[REG_RBP]; 141 rs[REG_RSP] = status.pr_reg[REG_RSP]; 142 rs[REG_RIP] = status.pr_reg[REG_RIP]; 143 } else { 144 rs[REG_RBX] = 0; 145 rs[REG_R12] = 0; 146 rs[REG_R13] = 0; 147 rs[REG_R14] = 0; 148 rs[REG_R15] = 0; 149 rs[REG_RBP] = 0; 150 rs[REG_RSP] = 0; 151 rs[REG_RIP] = 0; 152 } 153 } 154 155 void 156 setgregs(ulwp_t *ulwp, gregset_t rs) 157 { 158 lwpstatus_t status; 159 160 if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 161 status.pr_reg[REG_RBX] = rs[REG_RBX]; 162 status.pr_reg[REG_R12] = rs[REG_R12]; 163 status.pr_reg[REG_R13] = rs[REG_R13]; 164 status.pr_reg[REG_R14] = rs[REG_R14]; 165 status.pr_reg[REG_R15] = rs[REG_R15]; 166 status.pr_reg[REG_RBP] = rs[REG_RBP]; 167 status.pr_reg[REG_RSP] = rs[REG_RSP]; 168 status.pr_reg[REG_RIP] = rs[REG_RIP]; 169 (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg); 170 } 171 } 172 173 int 174 __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs) 175 { 176 /* LINTED alignment */ 177 ucontext_t *ucp = (ucontext_t *)env; 178 ulwp_t *self = curthread; 179 180 ucp->uc_link = self->ul_siglink; 181 if (self->ul_ustack.ss_flags & SS_ONSTACK) 182 ucp->uc_stack = self->ul_ustack; 183 else { 184 ucp->uc_stack.ss_sp = 185 (void *)(self->ul_stktop - self->ul_stksiz); 186 ucp->uc_stack.ss_size = self->ul_stksiz; 187 ucp->uc_stack.ss_flags = 0; 188 } 189 ucp->uc_flags = UC_STACK | UC_CPU; 190 if (savemask) { 191 ucp->uc_flags |= UC_SIGMASK; 192 enter_critical(self); 193 ucp->uc_sigmask = self->ul_sigmask; 194 exit_critical(self); 195 } 196 (void) memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t)); 197 198 return (0); 199 } 200