xref: /titanic_52/usr/src/lib/libc/amd64/threads/machdep.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 <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 	ucp->uc_mcontext.gregs[REG_FS] = 0; /* null selector indicates fsbase */
50 
51 	/* all contexts should have a valid data segment descriptor for %ss */
52 	ucp->uc_mcontext.gregs[REG_SS] = UDS_SEL;
53 
54 	/* top-of-stack must be rounded down to STACK_ALIGN */
55 	stack = (uint64_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
56 
57 	/* set up top stack frame */
58 	*--stack = 0;
59 	*--stack = 0;
60 	*--stack = (uint64_t)_lwp_start;
61 
62 	/* fill in registers of interest */
63 	ucp->uc_flags |= UC_CPU;
64 	ucp->uc_mcontext.gregs[REG_RDI] = (greg_t)ulwp;
65 	ucp->uc_mcontext.gregs[REG_RIP] = (greg_t)func;
66 	ucp->uc_mcontext.gregs[REG_RSP] = (greg_t)stack;
67 	ucp->uc_mcontext.gregs[REG_RBP] = (greg_t)(stack+1);
68 
69 	return (0);
70 }
71 
72 /*
73  * Machine-dependent startup code for a newly-created thread.
74  */
75 void *
76 _thrp_setup(ulwp_t *self)
77 {
78 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
79 	self->ul_ustack.ss_size = self->ul_stksiz;
80 	self->ul_ustack.ss_flags = 0;
81 	(void) setustack(&self->ul_ustack);
82 
83 	update_sched(self);
84 	tls_setup();
85 
86 	/* signals have been deferred until now */
87 	sigon(self);
88 
89 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
90 		return (NULL);	/* cancelled by pthread_create() */
91 	return (self->ul_startpc(self->ul_startarg));
92 }
93 
94 void
95 _fpinherit(ulwp_t *ulwp)
96 {
97 	ulwp->ul_fpuenv.ftag = 0xffffffff;
98 }
99 
100 void
101 getgregs(ulwp_t *ulwp, gregset_t rs)
102 {
103 	lwpstatus_t status;
104 
105 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
106 		rs[REG_RBX] = status.pr_reg[REG_RBX];
107 		rs[REG_R12] = status.pr_reg[REG_R12];
108 		rs[REG_R13] = status.pr_reg[REG_R13];
109 		rs[REG_R14] = status.pr_reg[REG_R14];
110 		rs[REG_R15] = status.pr_reg[REG_R15];
111 		rs[REG_RBP] = status.pr_reg[REG_RBP];
112 		rs[REG_RSP] = status.pr_reg[REG_RSP];
113 		rs[REG_RIP] = status.pr_reg[REG_RIP];
114 	} else {
115 		rs[REG_RBX] = 0;
116 		rs[REG_R12] = 0;
117 		rs[REG_R13] = 0;
118 		rs[REG_R14] = 0;
119 		rs[REG_R15] = 0;
120 		rs[REG_RBP] = 0;
121 		rs[REG_RSP] = 0;
122 		rs[REG_RIP] = 0;
123 	}
124 }
125 
126 void
127 setgregs(ulwp_t *ulwp, gregset_t rs)
128 {
129 	lwpstatus_t status;
130 
131 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
132 		status.pr_reg[REG_RBX] = rs[REG_RBX];
133 		status.pr_reg[REG_R12] = rs[REG_R12];
134 		status.pr_reg[REG_R13] = rs[REG_R13];
135 		status.pr_reg[REG_R14] = rs[REG_R14];
136 		status.pr_reg[REG_R15] = rs[REG_R15];
137 		status.pr_reg[REG_RBP] = rs[REG_RBP];
138 		status.pr_reg[REG_RSP] = rs[REG_RSP];
139 		status.pr_reg[REG_RIP] = rs[REG_RIP];
140 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
141 	}
142 }
143 
144 int
145 __csigsetjmp(sigjmp_buf env, int savemask, gregset_t rs)
146 {
147 	/* LINTED alignment */
148 	ucontext_t *ucp = (ucontext_t *)env;
149 	ulwp_t *self = curthread;
150 
151 	ucp->uc_link = self->ul_siglink;
152 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
153 		ucp->uc_stack = self->ul_ustack;
154 	else {
155 		ucp->uc_stack.ss_sp =
156 		    (void *)(self->ul_stktop - self->ul_stksiz);
157 		ucp->uc_stack.ss_size = self->ul_stksiz;
158 		ucp->uc_stack.ss_flags = 0;
159 	}
160 	ucp->uc_flags = UC_STACK | UC_CPU;
161 	if (savemask) {
162 		ucp->uc_flags |= UC_SIGMASK;
163 		enter_critical(self);
164 		ucp->uc_sigmask = self->ul_sigmask;
165 		exit_critical(self);
166 	}
167 	(void) memcpy(ucp->uc_mcontext.gregs, rs, _NGREG * sizeof (greg_t));
168 
169 	return (0);
170 }
171