1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "thr_uberdata.h"
30*0Sstevel@tonic-gate #include <procfs.h>
31*0Sstevel@tonic-gate #include <ucontext.h>
32*0Sstevel@tonic-gate #include <setjmp.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
35*0Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate int
38*0Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
39*0Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	static int initialized;
42*0Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	uint32_t *stack;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	if (!initialized) {
47*0Sstevel@tonic-gate 		ucontext_t uc;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 		/* do this once to load the segment registers */
50*0Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
51*0Sstevel@tonic-gate 		(void) __getcontext_syscall(&uc);
52*0Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
53*0Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
54*0Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
55*0Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
56*0Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
57*0Sstevel@tonic-gate 		initialized = 1;
58*0Sstevel@tonic-gate 	}
59*0Sstevel@tonic-gate 	/* clear the context and set the segment registers */
60*0Sstevel@tonic-gate 	(void) _memset(ucp, 0, sizeof (*ucp));
61*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
62*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
63*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
64*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
65*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	/*
68*0Sstevel@tonic-gate 	 * Yuck.
69*0Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
70*0Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
71*0Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
72*0Sstevel@tonic-gate 	 * Without this we would need to block all signals
73*0Sstevel@tonic-gate 	 * and directly call __lwp_setprivate() in _thr_setup
74*0Sstevel@tonic-gate 	 * on the other side of __lwp_create().
75*0Sstevel@tonic-gate 	 */
76*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
77*0Sstevel@tonic-gate 	ulwp->ul_gs = LWPGS_SEL;
78*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = (greg_t)ulwp->ul_gs;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/* top-of-stack must be rounded down to STACK_ALIGN */
81*0Sstevel@tonic-gate 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/* set up top stack frame */
84*0Sstevel@tonic-gate 	*--stack = 0;
85*0Sstevel@tonic-gate 	*--stack = 0;
86*0Sstevel@tonic-gate 	*--stack = (uint32_t)ulwp;
87*0Sstevel@tonic-gate 	*--stack = (uint32_t)_lwp_start;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	/* fill in registers of interest */
90*0Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
91*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
92*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
93*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack+2);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	return (0);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate /*
99*0Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
100*0Sstevel@tonic-gate  */
101*0Sstevel@tonic-gate void *
102*0Sstevel@tonic-gate _thr_setup(ulwp_t *self)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
105*0Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
106*0Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
107*0Sstevel@tonic-gate 	(void) _private_setustack(&self->ul_ustack);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	tls_setup();
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/* signals have been deferred until now */
112*0Sstevel@tonic-gate 	sigon(self);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate void
118*0Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate void
124*0Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	lwpstatus_t status;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
129*0Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
130*0Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
131*0Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
132*0Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
133*0Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
134*0Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
135*0Sstevel@tonic-gate 	} else {
136*0Sstevel@tonic-gate 		rs[EIP] = 0;
137*0Sstevel@tonic-gate 		rs[EDI] = 0;
138*0Sstevel@tonic-gate 		rs[ESI] = 0;
139*0Sstevel@tonic-gate 		rs[EBP] = 0;
140*0Sstevel@tonic-gate 		rs[EBX] = 0;
141*0Sstevel@tonic-gate 		rs[UESP] = 0;
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate void
146*0Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	lwpstatus_t status;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
151*0Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
152*0Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
153*0Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
154*0Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
155*0Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
156*0Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
157*0Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate int
162*0Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
163*0Sstevel@tonic-gate 	greg_t fs, greg_t es, greg_t ds,
164*0Sstevel@tonic-gate 	greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
165*0Sstevel@tonic-gate 	greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
166*0Sstevel@tonic-gate 	sigjmp_buf env, int savemask)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
169*0Sstevel@tonic-gate 	ulwp_t *self = curthread;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
172*0Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
173*0Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
174*0Sstevel@tonic-gate 	else {
175*0Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
176*0Sstevel@tonic-gate 			(void *)(self->ul_stktop - self->ul_stksiz);
177*0Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
178*0Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
181*0Sstevel@tonic-gate 	if (savemask) {
182*0Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
183*0Sstevel@tonic-gate 		enter_critical(self);
184*0Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
185*0Sstevel@tonic-gate 		exit_critical(self);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
188*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
189*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
190*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
191*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
192*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
193*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
194*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
195*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
196*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
197*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
198*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
199*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
200*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
201*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
202*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
203*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
204*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
205*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	return (0);
208*0Sstevel@tonic-gate }
209