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