xref: /onnv-gate/usr/src/lib/libc/sparc/threads/machdep.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56247Sraf  * Common Development and Distribution License (the "License").
66247Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216247Sraf 
220Sstevel@tonic-gate /*
236247Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include <procfs.h>
320Sstevel@tonic-gate #include <setjmp.h>
330Sstevel@tonic-gate #include <sys/fsr.h>
340Sstevel@tonic-gate #include "sigjmp_struct.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
370Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
380Sstevel@tonic-gate 
390Sstevel@tonic-gate int
400Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
410Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	/*
440Sstevel@tonic-gate 	 * Top-of-stack must be rounded down to STACK_ALIGN and
450Sstevel@tonic-gate 	 * there must be a minimum frame for the register window.
460Sstevel@tonic-gate 	 */
470Sstevel@tonic-gate 	uintptr_t stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
480Sstevel@tonic-gate 	    SA(MINFRAME);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	/* clear the context and the top stack frame */
516515Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
526515Sraf 	(void) memset((void *)stack, 0, SA(MINFRAME));
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	/* fill in registers of interest */
550Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
560Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
570Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
580Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
590Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
600Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
610Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	return (0);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate void *
70*6812Sraf _thrp_setup(ulwp_t *self)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	extern void _setfsr(greg_t *);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (self->ul_fpuenv.fpu_en)
750Sstevel@tonic-gate 		_setfsr(&self->ul_fpuenv.fsr);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
780Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
790Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
806515Sraf 	(void) setustack(&self->ul_ustack);
816247Sraf 
826247Sraf 	update_sched(self);
830Sstevel@tonic-gate 	tls_setup();
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/* signals have been deferred until now */
860Sstevel@tonic-gate 	sigon(self);
870Sstevel@tonic-gate 
886247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
896247Sraf 		return (NULL);	/* cancelled by pthread_create() */
900Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate void
940Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	extern void _getfsr(greg_t *);
970Sstevel@tonic-gate 	int fpu_enabled;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #ifdef __sparcv9
1000Sstevel@tonic-gate 	extern greg_t _getfprs();
1010Sstevel@tonic-gate 	fpu_enabled = _getfprs() & FPRS_FEF;
1020Sstevel@tonic-gate #else
1030Sstevel@tonic-gate 	extern psw_t _getpsr();
1040Sstevel@tonic-gate 	fpu_enabled = _getpsr() & PSR_EF;
1050Sstevel@tonic-gate #endif /* __sparcv9 */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (fpu_enabled) {
1080Sstevel@tonic-gate 		_getfsr(&ulwp->ul_fpuenv.fsr);
1090Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 1;
1100Sstevel@tonic-gate 	} else {
1110Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 0;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate void
1160Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	lwpstatus_t status;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1210Sstevel@tonic-gate 		rs[REG_PC] = status.pr_reg[R_PC];
1220Sstevel@tonic-gate 		rs[REG_O6] = status.pr_reg[R_O6];
1230Sstevel@tonic-gate 		rs[REG_O7] = status.pr_reg[R_O7];
1240Sstevel@tonic-gate 		rs[REG_G1] = status.pr_reg[R_G1];
1250Sstevel@tonic-gate 		rs[REG_G2] = status.pr_reg[R_G2];
1260Sstevel@tonic-gate 		rs[REG_G3] = status.pr_reg[R_G3];
1270Sstevel@tonic-gate 		rs[REG_G4] = status.pr_reg[R_G4];
1280Sstevel@tonic-gate 	} else {
1290Sstevel@tonic-gate 		rs[REG_PC] = 0;
1300Sstevel@tonic-gate 		rs[REG_O6] = 0;
1310Sstevel@tonic-gate 		rs[REG_O7] = 0;
1320Sstevel@tonic-gate 		rs[REG_G1] = 0;
1330Sstevel@tonic-gate 		rs[REG_G2] = 0;
1340Sstevel@tonic-gate 		rs[REG_G3] = 0;
1350Sstevel@tonic-gate 		rs[REG_G4] = 0;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate void
1400Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	lwpstatus_t status;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1450Sstevel@tonic-gate 		status.pr_reg[R_PC] = rs[REG_PC];
1460Sstevel@tonic-gate 		status.pr_reg[R_O6] = rs[REG_O6];
1470Sstevel@tonic-gate 		status.pr_reg[R_O7] = rs[REG_O7];
1480Sstevel@tonic-gate 		status.pr_reg[R_G1] = rs[REG_G1];
1490Sstevel@tonic-gate 		status.pr_reg[R_G2] = rs[REG_G2];
1500Sstevel@tonic-gate 		status.pr_reg[R_G3] = rs[REG_G3];
1510Sstevel@tonic-gate 		status.pr_reg[R_G4] = rs[REG_G4];
1520Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int
1570Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
1600Sstevel@tonic-gate 	ulwp_t *self = curthread;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	bp->sjs_flags = JB_FRAMEPTR;
1660Sstevel@tonic-gate 	bp->sjs_uclink = self->ul_siglink;
1670Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1680Sstevel@tonic-gate 		bp->sjs_stack = self->ul_ustack;
1690Sstevel@tonic-gate 	else {
1700Sstevel@tonic-gate 		bp->sjs_stack.ss_sp =
1716247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1720Sstevel@tonic-gate 		bp->sjs_stack.ss_size = self->ul_stksiz;
1730Sstevel@tonic-gate 		bp->sjs_stack.ss_flags = 0;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	if (savemask) {
1760Sstevel@tonic-gate 		bp->sjs_flags |= JB_SAVEMASK;
1770Sstevel@tonic-gate 		enter_critical(self);
1780Sstevel@tonic-gate 		bp->sjs_sigmask = self->ul_sigmask;
1790Sstevel@tonic-gate 		exit_critical(self);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (0);
1830Sstevel@tonic-gate }
184