xref: /illumos-gate/usr/src/lib/libc/sparc/threads/machdep.c (revision 8cd45542f2a452ca0dab13d8b2d5cfa876ccbebc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d4204c85Sraf  * Common Development and Distribution License (the "License").
6d4204c85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
317c478bd9Sstevel@tonic-gate #include <procfs.h>
327c478bd9Sstevel@tonic-gate #include <setjmp.h>
337c478bd9Sstevel@tonic-gate #include <sys/fsr.h>
347c478bd9Sstevel@tonic-gate #include "sigjmp_struct.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
377c478bd9Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate int
407c478bd9Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
417c478bd9Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	/*
447c478bd9Sstevel@tonic-gate 	 * Top-of-stack must be rounded down to STACK_ALIGN and
457c478bd9Sstevel@tonic-gate 	 * there must be a minimum frame for the register window.
467c478bd9Sstevel@tonic-gate 	 */
477c478bd9Sstevel@tonic-gate 	uintptr_t stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
487c478bd9Sstevel@tonic-gate 	    SA(MINFRAME);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	/* clear the context and the top stack frame */
51*8cd45542Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
52*8cd45542Sraf 	(void) memset((void *)stack, 0, SA(MINFRAME));
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	/* fill in registers of interest */
557c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
567c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
577c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
587c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
597c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
607c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
617c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (0);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate void *
707c478bd9Sstevel@tonic-gate _thr_setup(ulwp_t *self)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	extern void _setfsr(greg_t *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (self->ul_fpuenv.fpu_en)
757c478bd9Sstevel@tonic-gate 		_setfsr(&self->ul_fpuenv.fsr);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
787c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
797c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
80*8cd45542Sraf 	(void) setustack(&self->ul_ustack);
81d4204c85Sraf 
82d4204c85Sraf 	update_sched(self);
837c478bd9Sstevel@tonic-gate 	tls_setup();
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/* signals have been deferred until now */
867c478bd9Sstevel@tonic-gate 	sigon(self);
877c478bd9Sstevel@tonic-gate 
88d4204c85Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
89d4204c85Sraf 		return (NULL);	/* cancelled by pthread_create() */
907c478bd9Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate void
947c478bd9Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	extern void _getfsr(greg_t *);
977c478bd9Sstevel@tonic-gate 	int fpu_enabled;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #ifdef __sparcv9
1007c478bd9Sstevel@tonic-gate 	extern greg_t _getfprs();
1017c478bd9Sstevel@tonic-gate 	fpu_enabled = _getfprs() & FPRS_FEF;
1027c478bd9Sstevel@tonic-gate #else
1037c478bd9Sstevel@tonic-gate 	extern psw_t _getpsr();
1047c478bd9Sstevel@tonic-gate 	fpu_enabled = _getpsr() & PSR_EF;
1057c478bd9Sstevel@tonic-gate #endif /* __sparcv9 */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (fpu_enabled) {
1087c478bd9Sstevel@tonic-gate 		_getfsr(&ulwp->ul_fpuenv.fsr);
1097c478bd9Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 1;
1107c478bd9Sstevel@tonic-gate 	} else {
1117c478bd9Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 0;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate void
1167c478bd9Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1217c478bd9Sstevel@tonic-gate 		rs[REG_PC] = status.pr_reg[R_PC];
1227c478bd9Sstevel@tonic-gate 		rs[REG_O6] = status.pr_reg[R_O6];
1237c478bd9Sstevel@tonic-gate 		rs[REG_O7] = status.pr_reg[R_O7];
1247c478bd9Sstevel@tonic-gate 		rs[REG_G1] = status.pr_reg[R_G1];
1257c478bd9Sstevel@tonic-gate 		rs[REG_G2] = status.pr_reg[R_G2];
1267c478bd9Sstevel@tonic-gate 		rs[REG_G3] = status.pr_reg[R_G3];
1277c478bd9Sstevel@tonic-gate 		rs[REG_G4] = status.pr_reg[R_G4];
1287c478bd9Sstevel@tonic-gate 	} else {
1297c478bd9Sstevel@tonic-gate 		rs[REG_PC] = 0;
1307c478bd9Sstevel@tonic-gate 		rs[REG_O6] = 0;
1317c478bd9Sstevel@tonic-gate 		rs[REG_O7] = 0;
1327c478bd9Sstevel@tonic-gate 		rs[REG_G1] = 0;
1337c478bd9Sstevel@tonic-gate 		rs[REG_G2] = 0;
1347c478bd9Sstevel@tonic-gate 		rs[REG_G3] = 0;
1357c478bd9Sstevel@tonic-gate 		rs[REG_G4] = 0;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate void
1407c478bd9Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1457c478bd9Sstevel@tonic-gate 		status.pr_reg[R_PC] = rs[REG_PC];
1467c478bd9Sstevel@tonic-gate 		status.pr_reg[R_O6] = rs[REG_O6];
1477c478bd9Sstevel@tonic-gate 		status.pr_reg[R_O7] = rs[REG_O7];
1487c478bd9Sstevel@tonic-gate 		status.pr_reg[R_G1] = rs[REG_G1];
1497c478bd9Sstevel@tonic-gate 		status.pr_reg[R_G2] = rs[REG_G2];
1507c478bd9Sstevel@tonic-gate 		status.pr_reg[R_G3] = rs[REG_G3];
1517c478bd9Sstevel@tonic-gate 		status.pr_reg[R_G4] = rs[REG_G4];
1527c478bd9Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
1577c478bd9Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
1607c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	bp->sjs_flags = JB_FRAMEPTR;
1667c478bd9Sstevel@tonic-gate 	bp->sjs_uclink = self->ul_siglink;
1677c478bd9Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1687c478bd9Sstevel@tonic-gate 		bp->sjs_stack = self->ul_ustack;
1697c478bd9Sstevel@tonic-gate 	else {
1707c478bd9Sstevel@tonic-gate 		bp->sjs_stack.ss_sp =
171d4204c85Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1727c478bd9Sstevel@tonic-gate 		bp->sjs_stack.ss_size = self->ul_stksiz;
1737c478bd9Sstevel@tonic-gate 		bp->sjs_stack.ss_flags = 0;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (savemask) {
1767c478bd9Sstevel@tonic-gate 		bp->sjs_flags |= JB_SAVEMASK;
1777c478bd9Sstevel@tonic-gate 		enter_critical(self);
1787c478bd9Sstevel@tonic-gate 		bp->sjs_sigmask = self->ul_sigmask;
1797c478bd9Sstevel@tonic-gate 		exit_critical(self);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (0);
1837c478bd9Sstevel@tonic-gate }
184