xref: /titanic_51/usr/src/lib/libc/amd64/threads/machdep.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "lint.h"
29 #include "thr_uberdata.h"
30 #include <procfs.h>
31 #include <ucontext.h>
32 #include <setjmp.h>
33 
34 extern int getlwpstatus(thread_t, lwpstatus_t *);
35 extern int putlwpregs(thread_t, prgregset_t);
36 
37 int
38 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
39 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
40 {
41 	uint64_t *stack;
42 
43 	/* clear the context */
44 	(void) _memset(ucp, 0, sizeof (*ucp));
45 
46 	/* setup to store the current thread pointer in %fs */
47 	ucp->uc_mcontext.gregs[REG_FSBASE] = (greg_t)ulwp;
48 	ucp->uc_mcontext.gregs[REG_FS] = 0; /* null selector indicates fsbase */
49 
50 	/* all contexts should have a valid data segment descriptor for %ss */
51 	ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL;
52 
53 	/* top-of-stack must be rounded down to STACK_ALIGN */
54 	stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
55 
56 	/* set up top stack frame */
57 	*--stack = 0;
58 	*--stack = 0;
59 	*--stack = (uint64_t)_lwp_start;
60 
61 	/* fill in registers of interest */
62 	ucp->uc_flags |= UC_CPU;
63 	ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp;
64 	ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func;
65 	ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack;
66 	ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack+1);
67 
68 	return (0);
69 }
70 
71 /*
72  * Machine-dependent startup code for a newly-created thread.
73  */
74 void *
75 _thr_setup(ulwp_t *self)
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 	tls_setup();
82 
83 	/* signals have been deferred until now */
84 	sigon(self);
85 
86 	return (self->ul_startpc(self->ul_startarg));
87 }
88 
89 void
90 _fpinherit(ulwp_t *ulwp)
91 {
92 	ulwp->ul_fpuenv.ftag = 0xffffffff;
93 }
94 
95 void
96 getgregs(ulwp_t *ulwp, gregset_t rs)
97 {
98 	lwpstatus_t status;
99 
100 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
101 		rs[REG_RBX] = status.pr_reg[REG_RBX];
102 		rs[REG_R12] = status.pr_reg[REG_R12];
103 		rs[REG_R13] = status.pr_reg[REG_R13];
104 		rs[REG_R14] = status.pr_reg[REG_R14];
105 		rs[REG_R15] = status.pr_reg[REG_R15];
106 		rs[REG_RBP] = status.pr_reg[REG_RBP];
107 		rs[REG_RSP] = status.pr_reg[REG_RSP];
108 		rs[REG_RIP] = status.pr_reg[REG_RIP];
109 	} else {
110 		rs[REG_RBX] = 0;
111 		rs[REG_R12] = 0;
112 		rs[REG_R13] = 0;
113 		rs[REG_R14] = 0;
114 		rs[REG_R15] = 0;
115 		rs[REG_RBP] = 0;
116 		rs[REG_RSP] = 0;
117 		rs[REG_RIP] = 0;
118 	}
119 }
120 
121 void
122 setgregs(ulwp_t *ulwp, gregset_t rs)
123 {
124 	lwpstatus_t status;
125 
126 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
127 		status.pr_reg[REG_RBX] = rs[REG_RBX];
128 		status.pr_reg[REG_R12] = rs[REG_R12];
129 		status.pr_reg[REG_R13] = rs[REG_R13];
130 		status.pr_reg[REG_R14] = rs[REG_R14];
131 		status.pr_reg[REG_R15] = rs[REG_R15];
132 		status.pr_reg[REG_RBP] = rs[REG_RBP];
133 		status.pr_reg[REG_RSP] = rs[REG_RSP];
134 		status.pr_reg[REG_RIP] = rs[REG_RIP];
135 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
136 	}
137 }
138 
139 int
140 __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs)
141 {
142 	/* LINTED alignment */
143 	ucontext_t *ucp = (ucontext_t *)env;
144 	ulwp_t *self = curthread;
145 
146 	ucp->uc_link = self->ul_siglink;
147 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
148 		ucp->uc_stack = self->ul_ustack;
149 	else {
150 		ucp->uc_stack.ss_sp =
151 			(void *)(self->ul_stktop - self->ul_stksiz);
152 		ucp->uc_stack.ss_size = self->ul_stksiz;
153 		ucp->uc_stack.ss_flags = 0;
154 	}
155 	ucp->uc_flags = UC_STACK | UC_CPU;
156 	if (savemask) {
157 		ucp->uc_flags |= UC_SIGMASK;
158 		enter_critical(self);
159 		ucp->uc_sigmask = self->ul_sigmask;
160 		exit_critical(self);
161 	}
162 	(void) _memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t));
163 
164 	return (0);
165 }
166