xref: /illumos-gate/usr/src/lib/libc/sparc/threads/machdep.c (revision c3937c08b16d32677a7dc955a81325f2e7a61b5d)
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 	stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp);
76 	if (stack == (uintptr_t)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 	 * Also, if we are running in 64-bit mode (__sparcv9),
190 	 * then bp->sjs_asi and bp->sjs_fprs are already set.
191 	 */
192 	bp->sjs_flags = JB_FRAMEPTR;
193 	bp->sjs_uclink = self->ul_siglink;
194 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
195 		bp->sjs_stack = self->ul_ustack;
196 	else {
197 		bp->sjs_stack.ss_sp =
198 		    (void *)(self->ul_stktop - self->ul_stksiz);
199 		bp->sjs_stack.ss_size = self->ul_stksiz;
200 		bp->sjs_stack.ss_flags = 0;
201 	}
202 	if (savemask) {
203 		bp->sjs_flags |= JB_SAVEMASK;
204 		enter_critical(self);
205 		bp->sjs_sigmask = self->ul_sigmask;
206 		exit_critical(self);
207 	}
208 
209 	return (0);
210 }
211