xref: /titanic_44/usr/src/uts/intel/ia32/syscall/lwp_private.c (revision 30a5e8fa1253cb33980ee4514743cf683f584b4e)
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 <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/disp.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cpuvar.h>
33 #include <sys/systm.h>
34 #include <sys/thread.h>
35 #include <sys/lwp.h>
36 #include <sys/segments.h>
37 #include <sys/privregs.h>
38 #include <sys/cmn_err.h>
39 
40 int
41 lwp_setprivate(klwp_t *lwp, int which, uintptr_t base)
42 {
43 	pcb_t *pcb = &lwp->lwp_pcb;
44 	struct regs *rp = lwptoregs(lwp);
45 	kthread_t *t = lwptot(lwp);
46 	int thisthread = t == curthread;
47 	int rval;
48 
49 	if (thisthread)
50 		kpreempt_disable();
51 
52 #if defined(__amd64)
53 
54 	/*
55 	 * 32-bit compatibility processes point to the per-cpu GDT segment
56 	 * descriptors that are virtualized to the lwp.  That allows 32-bit
57 	 * programs to mess with %fs and %gs; in particular it allows
58 	 * things like this:
59 	 *
60 	 *	movw	%gs, %ax
61 	 *	...
62 	 *	movw	%ax, %gs
63 	 *
64 	 * to work, which is needed by emulators for legacy application
65 	 * environments ..
66 	 *
67 	 * 64-bit processes may also point to a per-cpu GDT segment descriptor
68 	 * virtualized to the lwp.  However the descriptor base is forced
69 	 * to zero (because we can't express the full 64-bit address range
70 	 * in a long mode descriptor), so don't reload segment registers
71 	 * in a 64-bit program! 64-bit processes must have selector values
72 	 * of zero for %fs and %gs to use the 64-bit fs_base and gs_base
73 	 * respectively.
74 	 */
75 	if (pcb->pcb_rupdate == 0) {
76 		pcb->pcb_ds = rp->r_ds;
77 		pcb->pcb_es = rp->r_es;
78 		pcb->pcb_fs = rp->r_fs;
79 		pcb->pcb_gs = rp->r_gs;
80 		pcb->pcb_rupdate = 1;
81 		t->t_post_sys = 1;
82 	}
83 	ASSERT(t->t_post_sys);
84 
85 	switch (which) {
86 	case _LWP_FSBASE:
87 		if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
88 			set_usegd(&pcb->pcb_fsdesc, SDP_LONG, 0, 0,
89 			    SDT_MEMRWA, SEL_UPL, SDP_BYTES, SDP_OP32);
90 			rval = pcb->pcb_fs = 0;	/* null gdt descriptor */
91 		} else {
92 			set_usegd(&pcb->pcb_fsdesc, SDP_SHORT, (void *)base, -1,
93 			    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
94 			rval = pcb->pcb_fs = LWPFS_SEL;
95 		}
96 		if (thisthread)
97 			CPU->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
98 		pcb->pcb_fsbase = base;
99 		break;
100 	case _LWP_GSBASE:
101 		if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
102 			set_usegd(&pcb->pcb_gsdesc, SDP_LONG, 0, 0,
103 			    SDT_MEMRWA, SEL_UPL, SDP_BYTES, SDP_OP32);
104 			rval = pcb->pcb_gs = 0;	/* null gdt descriptor */
105 		} else {
106 			set_usegd(&pcb->pcb_gsdesc, SDP_SHORT, (void *)base, -1,
107 			    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
108 			rval = pcb->pcb_gs = LWPGS_SEL;
109 		}
110 		if (thisthread)
111 			CPU->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
112 		pcb->pcb_gsbase = base;
113 		break;
114 	default:
115 		rval = -1;
116 		break;
117 	}
118 
119 #elif defined(__i386)
120 
121 	/*
122 	 * 32-bit processes point to the per-cpu GDT segment
123 	 * descriptors that are virtualized to the lwp.
124 	 */
125 
126 	switch	(which) {
127 	case _LWP_FSBASE:
128 		set_usegd(&pcb->pcb_fsdesc, (void *)base, -1,
129 		    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
130 		if (thisthread)
131 			CPU->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc;
132 		rval = rp->r_fs = LWPFS_SEL;
133 		break;
134 	case _LWP_GSBASE:
135 		set_usegd(&pcb->pcb_gsdesc, (void *)base, -1,
136 		    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
137 		if (thisthread)
138 			CPU->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc;
139 		rval = rp->r_gs = LWPGS_SEL;
140 		break;
141 	default:
142 		rval = -1;
143 		break;
144 	}
145 
146 #endif	/* __i386 */
147 
148 	if (thisthread)
149 		kpreempt_enable();
150 	return (rval);
151 }
152 
153 static int
154 lwp_getprivate(klwp_t *lwp, int which, uintptr_t base)
155 {
156 	pcb_t *pcb = &lwp->lwp_pcb;
157 	struct regs *rp = lwptoregs(lwp);
158 	uintptr_t sbase;
159 	int error = 0;
160 
161 	ASSERT(lwptot(lwp) == curthread);
162 
163 	kpreempt_disable();
164 	switch (which) {
165 #if defined(__amd64)
166 
167 	case _LWP_FSBASE:
168 		if ((sbase = pcb->pcb_fsbase) != 0) {
169 			if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
170 				if (pcb->pcb_rupdate == 1) {
171 					if (pcb->pcb_fs == 0)
172 						break;
173 				} else {
174 					if (rp->r_fs == 0)
175 						break;
176 				}
177 			} else {
178 				if (pcb->pcb_rupdate == 1) {
179 					if (pcb->pcb_fs == LWPFS_SEL)
180 						break;
181 				} else {
182 					if (rp->r_fs == LWPFS_SEL)
183 						break;
184 				}
185 			}
186 		}
187 		error = EINVAL;
188 		break;
189 	case _LWP_GSBASE:
190 		if ((sbase = pcb->pcb_gsbase) != 0) {
191 			if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
192 				if (pcb->pcb_rupdate == 1) {
193 					if (pcb->pcb_gs == 0)
194 						break;
195 				} else {
196 					if (rp->r_gs == 0)
197 						break;
198 				}
199 			} else {
200 				if (pcb->pcb_rupdate == 1) {
201 					if (pcb->pcb_gs == LWPGS_SEL)
202 						break;
203 				} else {
204 					if (rp->r_gs == LWPGS_SEL)
205 						break;
206 				}
207 			}
208 		}
209 		error = EINVAL;
210 		break;
211 
212 #elif defined(__i386)
213 
214 	case _LWP_FSBASE:
215 		if (rp->r_fs == LWPFS_SEL) {
216 			sbase = USEGD_GETBASE(&pcb->pcb_fsdesc);
217 			break;
218 		}
219 		error = EINVAL;
220 		break;
221 	case _LWP_GSBASE:
222 		if (rp->r_gs == LWPGS_SEL) {
223 			sbase = USEGD_GETBASE(&pcb->pcb_gsdesc);
224 			break;
225 		}
226 		error = EINVAL;
227 		break;
228 
229 #endif	/* __i386 */
230 
231 	default:
232 		error = ENOTSUP;
233 		break;
234 	}
235 	kpreempt_enable();
236 
237 	if (error != 0)
238 		return (error);
239 
240 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
241 		if (sulword((void *)base, sbase) == -1)
242 			error = EFAULT;
243 #if defined(_SYSCALL32_IMPL)
244 	} else {
245 		if (suword32((void *)base, (uint32_t)sbase) == -1)
246 			error = EFAULT;
247 #endif
248 	}
249 	return (error);
250 }
251 
252 /*
253  * libc-private syscall for managing per-lwp %gs and %fs segment base values.
254  */
255 int
256 syslwp_private(int cmd, int which, uintptr_t base)
257 {
258 	klwp_t *lwp = ttolwp(curthread);
259 	int res, error;
260 
261 	switch (cmd) {
262 	case _LWP_SETPRIVATE:
263 		res = lwp_setprivate(lwp, which, base);
264 		return (res < 0 ? set_errno(ENOTSUP) : res);
265 	case _LWP_GETPRIVATE:
266 		error = lwp_getprivate(lwp, which, base);
267 		return (error != 0 ? set_errno(error) : error);
268 	default:
269 		return (set_errno(ENOTSUP));
270 	}
271 }
272