xref: /titanic_41/usr/src/lib/libc/sparc/threads/machdep.c (revision 64d1d4ab72834b7483c7962efc738b568ca8792e)
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 <setjmp.h>
31 #include <sys/fsr.h>
32 #include "sigjmp_struct.h"
33 
34 extern int getlwpstatus(thread_t, lwpstatus_t *);
35 extern int putlwpregs(thread_t, prgregset_t);
36 
37 /* ARGSUSED2 */
38 void *
39 setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
40 {
41 	uintptr_t stack;
42 	char frame[SA(MINFRAME)];
43 
44 	/*
45 	 * Top-of-stack must be rounded down to STACK_ALIGN and
46 	 * there must be a minimum frame for the register window.
47 	 */
48 	stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
49 	    SA(MINFRAME);
50 
51 	/*
52 	 * This will return NULL if the kernel cannot allocate
53 	 * a page for the top page of the stack.  This will cause
54 	 * thr_create(), pthread_create() or pthread_attr_setstack()
55 	 * to fail, passing the problem up to the application.
56 	 */
57 	(void) memset(frame, 0, sizeof (frame));
58 	if (uucopy(frame, (void *)stack, sizeof (frame)) == 0)
59 		return ((void *)stack);
60 	return (NULL);
61 }
62 
63 int
64 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
65 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
66 {
67 	uintptr_t stack;
68 
69 	/* clear the context */
70 	(void) memset(ucp, 0, sizeof (*ucp));
71 
72 	/*
73 	 * Clear the top stack frame.
74 	 * If this fails, pass the problem up to the application.
75 	 */
76 	if ((stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp)) == NULL)
77 		return (ENOMEM);
78 
79 	/* fill in registers of interest */
80 	ucp->uc_flags |= UC_CPU;
81 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
82 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
83 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
84 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
85 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
86 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
87 
88 	return (0);
89 }
90 
91 /*
92  * Machine-dependent startup code for a newly-created thread.
93  */
94 void *
95 _thrp_setup(ulwp_t *self)
96 {
97 	extern void _setfsr(greg_t *);
98 
99 	if (self->ul_fpuenv.fpu_en)
100 		_setfsr(&self->ul_fpuenv.fsr);
101 
102 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
103 	self->ul_ustack.ss_size = self->ul_stksiz;
104 	self->ul_ustack.ss_flags = 0;
105 	(void) setustack(&self->ul_ustack);
106 
107 	update_sched(self);
108 	tls_setup();
109 
110 	/* signals have been deferred until now */
111 	sigon(self);
112 
113 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
114 		return (NULL);	/* cancelled by pthread_create() */
115 	return (self->ul_startpc(self->ul_startarg));
116 }
117 
118 void
119 _fpinherit(ulwp_t *ulwp)
120 {
121 	extern void _getfsr(greg_t *);
122 	int fpu_enabled;
123 
124 #ifdef __sparcv9
125 	extern greg_t _getfprs();
126 	fpu_enabled = _getfprs() & FPRS_FEF;
127 #else
128 	extern psw_t _getpsr();
129 	fpu_enabled = _getpsr() & PSR_EF;
130 #endif /* __sparcv9 */
131 
132 	if (fpu_enabled) {
133 		_getfsr(&ulwp->ul_fpuenv.fsr);
134 		ulwp->ul_fpuenv.fpu_en = 1;
135 	} else {
136 		ulwp->ul_fpuenv.fpu_en = 0;
137 	}
138 }
139 
140 void
141 getgregs(ulwp_t *ulwp, gregset_t rs)
142 {
143 	lwpstatus_t status;
144 
145 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
146 		rs[REG_PC] = status.pr_reg[R_PC];
147 		rs[REG_O6] = status.pr_reg[R_O6];
148 		rs[REG_O7] = status.pr_reg[R_O7];
149 		rs[REG_G1] = status.pr_reg[R_G1];
150 		rs[REG_G2] = status.pr_reg[R_G2];
151 		rs[REG_G3] = status.pr_reg[R_G3];
152 		rs[REG_G4] = status.pr_reg[R_G4];
153 	} else {
154 		rs[REG_PC] = 0;
155 		rs[REG_O6] = 0;
156 		rs[REG_O7] = 0;
157 		rs[REG_G1] = 0;
158 		rs[REG_G2] = 0;
159 		rs[REG_G3] = 0;
160 		rs[REG_G4] = 0;
161 	}
162 }
163 
164 void
165 setgregs(ulwp_t *ulwp, gregset_t rs)
166 {
167 	lwpstatus_t status;
168 
169 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
170 		status.pr_reg[R_PC] = rs[REG_PC];
171 		status.pr_reg[R_O6] = rs[REG_O6];
172 		status.pr_reg[R_O7] = rs[REG_O7];
173 		status.pr_reg[R_G1] = rs[REG_G1];
174 		status.pr_reg[R_G2] = rs[REG_G2];
175 		status.pr_reg[R_G3] = rs[REG_G3];
176 		status.pr_reg[R_G4] = rs[REG_G4];
177 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
178 	}
179 }
180 
181 int
182 __csigsetjmp(sigjmp_buf env, int savemask)
183 {
184 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
185 	ulwp_t *self = curthread;
186 
187 	/*
188 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
189 	 */
190 	bp->sjs_flags = JB_FRAMEPTR;
191 	bp->sjs_uclink = self->ul_siglink;
192 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
193 		bp->sjs_stack = self->ul_ustack;
194 	else {
195 		bp->sjs_stack.ss_sp =
196 		    (void *)(self->ul_stktop - self->ul_stksiz);
197 		bp->sjs_stack.ss_size = self->ul_stksiz;
198 		bp->sjs_stack.ss_flags = 0;
199 	}
200 	if (savemask) {
201 		bp->sjs_flags |= JB_SAVEMASK;
202 		enter_critical(self);
203 		bp->sjs_sigmask = self->ul_sigmask;
204 		exit_critical(self);
205 	}
206 
207 	return (0);
208 }
209