xref: /illumos-gate/usr/src/lib/libc/amd64/threads/machdep.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 <ucontext.h>
33 #include <setjmp.h>
34 
35 extern int getlwpstatus(thread_t, lwpstatus_t *);
36 extern int putlwpregs(thread_t, prgregset_t);
37 
38 int
39 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
40 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
41 {
42 	uint64_t *stack;
43 
44 	/* clear the context */
45 	(void) _memset(ucp, 0, sizeof (*ucp));
46 
47 	/* setup to store the current thread pointer in %fs */
48 	ucp->uc_mcontext.gregs[REG_FSBASE] = (greg_t)ulwp;
49 	ulwp->ul_gs = LWPFS_SEL;
50 	ucp->uc_mcontext.gregs[REG_FS] = (greg_t)ulwp->ul_gs;
51 
52 	/* all contexts should have a valid data segment descriptor for %ss */
53 	ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL;
54 
55 	/* top-of-stack must be rounded down to STACK_ALIGN */
56 	stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
57 
58 	/* set up top stack frame */
59 	*--stack = 0;
60 	*--stack = 0;
61 	*--stack = (uint64_t)_lwp_start;
62 
63 	/* fill in registers of interest */
64 	ucp->uc_flags |= UC_CPU;
65 	ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp;
66 	ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func;
67 	ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack;
68 	ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack+1);
69 
70 	return (0);
71 }
72 
73 /*
74  * Machine-dependent startup code for a newly-created thread.
75  */
76 void *
77 _thr_setup(ulwp_t *self)
78 {
79 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
80 	self->ul_ustack.ss_size = self->ul_stksiz;
81 	self->ul_ustack.ss_flags = 0;
82 	(void) _private_setustack(&self->ul_ustack);
83 	tls_setup();
84 
85 	/* signals have been deferred until now */
86 	sigon(self);
87 
88 	return (self->ul_startpc(self->ul_startarg));
89 }
90 
91 void
92 _fpinherit(ulwp_t *ulwp)
93 {
94 	ulwp->ul_fpuenv.ftag = 0xffffffff;
95 }
96 
97 void
98 getgregs(ulwp_t *ulwp, gregset_t rs)
99 {
100 	lwpstatus_t status;
101 
102 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
103 		rs[REG_RBX] = status.pr_reg[REG_RBX];
104 		rs[REG_R12] = status.pr_reg[REG_R12];
105 		rs[REG_R13] = status.pr_reg[REG_R13];
106 		rs[REG_R14] = status.pr_reg[REG_R14];
107 		rs[REG_R15] = status.pr_reg[REG_R15];
108 		rs[REG_RBP] = status.pr_reg[REG_RBP];
109 		rs[REG_RSP] = status.pr_reg[REG_RSP];
110 		rs[REG_RIP] = status.pr_reg[REG_RIP];
111 	} else {
112 		rs[REG_RBX] = 0;
113 		rs[REG_R12] = 0;
114 		rs[REG_R13] = 0;
115 		rs[REG_R14] = 0;
116 		rs[REG_R15] = 0;
117 		rs[REG_RBP] = 0;
118 		rs[REG_RSP] = 0;
119 		rs[REG_RIP] = 0;
120 	}
121 }
122 
123 void
124 setgregs(ulwp_t *ulwp, gregset_t rs)
125 {
126 	lwpstatus_t status;
127 
128 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
129 		status.pr_reg[REG_RBX] = rs[REG_RBX];
130 		status.pr_reg[REG_R12] = rs[REG_R12];
131 		status.pr_reg[REG_R13] = rs[REG_R13];
132 		status.pr_reg[REG_R14] = rs[REG_R14];
133 		status.pr_reg[REG_R15] = rs[REG_R15];
134 		status.pr_reg[REG_RBP] = rs[REG_RBP];
135 		status.pr_reg[REG_RSP] = rs[REG_RSP];
136 		status.pr_reg[REG_RIP] = rs[REG_RIP];
137 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
138 	}
139 }
140 
141 int
142 __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs)
143 {
144 	/* LINTED alignment */
145 	ucontext_t *ucp = (ucontext_t *)env;
146 	ulwp_t *self = curthread;
147 
148 	ucp->uc_link = self->ul_siglink;
149 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
150 		ucp->uc_stack = self->ul_ustack;
151 	else {
152 		ucp->uc_stack.ss_sp =
153 			(void *)(self->ul_stktop - self->ul_stksiz);
154 		ucp->uc_stack.ss_size = self->ul_stksiz;
155 		ucp->uc_stack.ss_flags = 0;
156 	}
157 	ucp->uc_flags = UC_STACK | UC_CPU;
158 	if (savemask) {
159 		ucp->uc_flags |= UC_SIGMASK;
160 		enter_critical(self);
161 		ucp->uc_sigmask = self->ul_sigmask;
162 		exit_critical(self);
163 	}
164 	(void) _memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t));
165 
166 	return (0);
167 }
168