xref: /titanic_51/usr/src/lib/libc/sparc/threads/machdep.c (revision 678453a8ed49104d8adad58f3ba591bdc39883e8)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "thr_uberdata.h"
31 #include <procfs.h>
32 #include <setjmp.h>
33 #include <sys/fsr.h>
34 #include "sigjmp_struct.h"
35 
36 extern int getlwpstatus(thread_t, lwpstatus_t *);
37 extern int putlwpregs(thread_t, prgregset_t);
38 
39 int
40 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
41 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
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 	uintptr_t stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
48 	    SA(MINFRAME);
49 
50 	/* clear the context and the top stack frame */
51 	(void) _memset(ucp, 0, sizeof (*ucp));
52 	(void) _memset((void *)stack, 0, SA(MINFRAME));
53 
54 	/* fill in registers of interest */
55 	ucp->uc_flags |= UC_CPU;
56 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
57 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
58 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
59 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
60 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
61 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
62 
63 	return (0);
64 }
65 
66 /*
67  * Machine-dependent startup code for a newly-created thread.
68  */
69 void *
70 _thr_setup(ulwp_t *self)
71 {
72 	extern void _setfsr(greg_t *);
73 
74 	if (self->ul_fpuenv.fpu_en)
75 		_setfsr(&self->ul_fpuenv.fsr);
76 
77 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
78 	self->ul_ustack.ss_size = self->ul_stksiz;
79 	self->ul_ustack.ss_flags = 0;
80 	(void) _private_setustack(&self->ul_ustack);
81 
82 	update_sched(self);
83 	tls_setup();
84 
85 	/* signals have been deferred until now */
86 	sigon(self);
87 
88 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
89 		return (NULL);	/* cancelled by pthread_create() */
90 	return (self->ul_startpc(self->ul_startarg));
91 }
92 
93 void
94 _fpinherit(ulwp_t *ulwp)
95 {
96 	extern void _getfsr(greg_t *);
97 	int fpu_enabled;
98 
99 #ifdef __sparcv9
100 	extern greg_t _getfprs();
101 	fpu_enabled = _getfprs() & FPRS_FEF;
102 #else
103 	extern psw_t _getpsr();
104 	fpu_enabled = _getpsr() & PSR_EF;
105 #endif /* __sparcv9 */
106 
107 	if (fpu_enabled) {
108 		_getfsr(&ulwp->ul_fpuenv.fsr);
109 		ulwp->ul_fpuenv.fpu_en = 1;
110 	} else {
111 		ulwp->ul_fpuenv.fpu_en = 0;
112 	}
113 }
114 
115 void
116 getgregs(ulwp_t *ulwp, gregset_t rs)
117 {
118 	lwpstatus_t status;
119 
120 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
121 		rs[REG_PC] = status.pr_reg[R_PC];
122 		rs[REG_O6] = status.pr_reg[R_O6];
123 		rs[REG_O7] = status.pr_reg[R_O7];
124 		rs[REG_G1] = status.pr_reg[R_G1];
125 		rs[REG_G2] = status.pr_reg[R_G2];
126 		rs[REG_G3] = status.pr_reg[R_G3];
127 		rs[REG_G4] = status.pr_reg[R_G4];
128 	} else {
129 		rs[REG_PC] = 0;
130 		rs[REG_O6] = 0;
131 		rs[REG_O7] = 0;
132 		rs[REG_G1] = 0;
133 		rs[REG_G2] = 0;
134 		rs[REG_G3] = 0;
135 		rs[REG_G4] = 0;
136 	}
137 }
138 
139 void
140 setgregs(ulwp_t *ulwp, gregset_t rs)
141 {
142 	lwpstatus_t status;
143 
144 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
145 		status.pr_reg[R_PC] = rs[REG_PC];
146 		status.pr_reg[R_O6] = rs[REG_O6];
147 		status.pr_reg[R_O7] = rs[REG_O7];
148 		status.pr_reg[R_G1] = rs[REG_G1];
149 		status.pr_reg[R_G2] = rs[REG_G2];
150 		status.pr_reg[R_G3] = rs[REG_G3];
151 		status.pr_reg[R_G4] = rs[REG_G4];
152 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
153 	}
154 }
155 
156 int
157 __csigsetjmp(sigjmp_buf env, int savemask)
158 {
159 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
160 	ulwp_t *self = curthread;
161 
162 	/*
163 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
164 	 */
165 	bp->sjs_flags = JB_FRAMEPTR;
166 	bp->sjs_uclink = self->ul_siglink;
167 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
168 		bp->sjs_stack = self->ul_ustack;
169 	else {
170 		bp->sjs_stack.ss_sp =
171 		    (void *)(self->ul_stktop - self->ul_stksiz);
172 		bp->sjs_stack.ss_size = self->ul_stksiz;
173 		bp->sjs_stack.ss_flags = 0;
174 	}
175 	if (savemask) {
176 		bp->sjs_flags |= JB_SAVEMASK;
177 		enter_critical(self);
178 		bp->sjs_sigmask = self->ul_sigmask;
179 		exit_critical(self);
180 	}
181 
182 	return (0);
183 }
184