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
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * 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  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
244c28a617SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/disp.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/thread.h>
347c478bd9Sstevel@tonic-gate #include <sys/lwp.h>
357c478bd9Sstevel@tonic-gate #include <sys/segments.h>
367c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate int
lwp_setprivate(klwp_t * lwp,int which,uintptr_t base)407c478bd9Sstevel@tonic-gate lwp_setprivate(klwp_t *lwp, int which, uintptr_t base)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
437c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
447c478bd9Sstevel@tonic-gate 	kthread_t *t = lwptot(lwp);
457c478bd9Sstevel@tonic-gate 	int thisthread = t == curthread;
467c478bd9Sstevel@tonic-gate 	int rval;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if (thisthread)
497c478bd9Sstevel@tonic-gate 		kpreempt_disable();
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	/*
537c478bd9Sstevel@tonic-gate 	 * 32-bit compatibility processes point to the per-cpu GDT segment
547c478bd9Sstevel@tonic-gate 	 * descriptors that are virtualized to the lwp.  That allows 32-bit
557c478bd9Sstevel@tonic-gate 	 * programs to mess with %fs and %gs; in particular it allows
567c478bd9Sstevel@tonic-gate 	 * things like this:
577c478bd9Sstevel@tonic-gate 	 *
587c478bd9Sstevel@tonic-gate 	 *	movw	%gs, %ax
597c478bd9Sstevel@tonic-gate 	 *	...
607c478bd9Sstevel@tonic-gate 	 *	movw	%ax, %gs
617c478bd9Sstevel@tonic-gate 	 *
627c478bd9Sstevel@tonic-gate 	 * to work, which is needed by emulators for legacy application
637c478bd9Sstevel@tonic-gate 	 * environments ..
647c478bd9Sstevel@tonic-gate 	 *
65ae115bc7Smrj 	 * 64-bit processes may also point to a per-cpu GDT segment descriptor
667c478bd9Sstevel@tonic-gate 	 * virtualized to the lwp.  However the descriptor base is forced
677c478bd9Sstevel@tonic-gate 	 * to zero (because we can't express the full 64-bit address range
687c478bd9Sstevel@tonic-gate 	 * in a long mode descriptor), so don't reload segment registers
69ae115bc7Smrj 	 * in a 64-bit program! 64-bit processes must have selector values
70ae115bc7Smrj 	 * of zero for %fs and %gs to use the 64-bit fs_base and gs_base
71ae115bc7Smrj 	 * respectively.
727c478bd9Sstevel@tonic-gate 	 */
734c28a617SRobert Mustacchi 	if (!PCB_NEED_UPDATE_SEGS(pcb)) {
747c478bd9Sstevel@tonic-gate 		pcb->pcb_ds = rp->r_ds;
757c478bd9Sstevel@tonic-gate 		pcb->pcb_es = rp->r_es;
767c478bd9Sstevel@tonic-gate 		pcb->pcb_fs = rp->r_fs;
777c478bd9Sstevel@tonic-gate 		pcb->pcb_gs = rp->r_gs;
784c28a617SRobert Mustacchi 		PCB_SET_UPDATE_SEGS(pcb);
797c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	ASSERT(t->t_post_sys);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	switch (which) {
847c478bd9Sstevel@tonic-gate 	case _LWP_FSBASE:
85ae115bc7Smrj 		if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
867c478bd9Sstevel@tonic-gate 			set_usegd(&pcb->pcb_fsdesc, SDP_LONG, 0, 0,
877c478bd9Sstevel@tonic-gate 			    SDT_MEMRWA, SEL_UPL, SDP_BYTES, SDP_OP32);
88ae115bc7Smrj 			rval = pcb->pcb_fs = 0;	/* null gdt descriptor */
89ae115bc7Smrj 		} else {
907c478bd9Sstevel@tonic-gate 			set_usegd(&pcb->pcb_fsdesc, SDP_SHORT, (void *)base, -1,
917c478bd9Sstevel@tonic-gate 			    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
92ae115bc7Smrj 			rval = pcb->pcb_fs = LWPFS_SEL;
93ae115bc7Smrj 		}
947c478bd9Sstevel@tonic-gate 		if (thisthread)
95843e1988Sjohnlev 			gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc);
96843e1988Sjohnlev 
977c478bd9Sstevel@tonic-gate 		pcb->pcb_fsbase = base;
987c478bd9Sstevel@tonic-gate 		break;
997c478bd9Sstevel@tonic-gate 	case _LWP_GSBASE:
100ae115bc7Smrj 		if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1017c478bd9Sstevel@tonic-gate 			set_usegd(&pcb->pcb_gsdesc, SDP_LONG, 0, 0,
1027c478bd9Sstevel@tonic-gate 			    SDT_MEMRWA, SEL_UPL, SDP_BYTES, SDP_OP32);
103ae115bc7Smrj 			rval = pcb->pcb_gs = 0;	/* null gdt descriptor */
104ae115bc7Smrj 		} else {
1057c478bd9Sstevel@tonic-gate 			set_usegd(&pcb->pcb_gsdesc, SDP_SHORT, (void *)base, -1,
1067c478bd9Sstevel@tonic-gate 			    SDT_MEMRWA, SEL_UPL, SDP_PAGES, SDP_OP32);
107ae115bc7Smrj 			rval = pcb->pcb_gs = LWPGS_SEL;
108ae115bc7Smrj 		}
1097c478bd9Sstevel@tonic-gate 		if (thisthread)
110843e1988Sjohnlev 			gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc);
111843e1988Sjohnlev 
1127c478bd9Sstevel@tonic-gate 		pcb->pcb_gsbase = base;
1137c478bd9Sstevel@tonic-gate 		break;
1147c478bd9Sstevel@tonic-gate 	default:
1157c478bd9Sstevel@tonic-gate 		rval = -1;
1167c478bd9Sstevel@tonic-gate 		break;
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (thisthread)
1207c478bd9Sstevel@tonic-gate 		kpreempt_enable();
1217c478bd9Sstevel@tonic-gate 	return (rval);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int
lwp_getprivate(klwp_t * lwp,int which,uintptr_t base)1257c478bd9Sstevel@tonic-gate lwp_getprivate(klwp_t *lwp, int which, uintptr_t base)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
1287c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
1297c478bd9Sstevel@tonic-gate 	uintptr_t sbase;
1307c478bd9Sstevel@tonic-gate 	int error = 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	ASSERT(lwptot(lwp) == curthread);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	kpreempt_disable();
1357c478bd9Sstevel@tonic-gate 	switch (which) {
1367c478bd9Sstevel@tonic-gate 	case _LWP_FSBASE:
1377c478bd9Sstevel@tonic-gate 		if ((sbase = pcb->pcb_fsbase) != 0) {
138ae115bc7Smrj 			if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1394c28a617SRobert Mustacchi 				if (PCB_NEED_UPDATE_SEGS(pcb)) {
140ae115bc7Smrj 					if (pcb->pcb_fs == 0)
141ae115bc7Smrj 						break;
142ae115bc7Smrj 				} else {
143ae115bc7Smrj 					if (rp->r_fs == 0)
144ae115bc7Smrj 						break;
145ae115bc7Smrj 				}
1467c478bd9Sstevel@tonic-gate 			} else {
1474c28a617SRobert Mustacchi 				if (PCB_NEED_UPDATE_SEGS(pcb)) {
148ae115bc7Smrj 					if (pcb->pcb_fs == LWPFS_SEL)
149ae115bc7Smrj 						break;
150ae115bc7Smrj 				} else {
151ae115bc7Smrj 					if (rp->r_fs == LWPFS_SEL)
152ae115bc7Smrj 						break;
153ae115bc7Smrj 				}
1547c478bd9Sstevel@tonic-gate 			}
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 		error = EINVAL;
1577c478bd9Sstevel@tonic-gate 		break;
1587c478bd9Sstevel@tonic-gate 	case _LWP_GSBASE:
1597c478bd9Sstevel@tonic-gate 		if ((sbase = pcb->pcb_gsbase) != 0) {
160ae115bc7Smrj 			if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1614c28a617SRobert Mustacchi 				if (PCB_NEED_UPDATE_SEGS(pcb)) {
162ae115bc7Smrj 					if (pcb->pcb_gs == 0)
163ae115bc7Smrj 						break;
164ae115bc7Smrj 				} else {
165ae115bc7Smrj 					if (rp->r_gs == 0)
166ae115bc7Smrj 						break;
167ae115bc7Smrj 				}
1687c478bd9Sstevel@tonic-gate 			} else {
1694c28a617SRobert Mustacchi 				if (PCB_NEED_UPDATE_SEGS(pcb)) {
170ae115bc7Smrj 					if (pcb->pcb_gs == LWPGS_SEL)
171ae115bc7Smrj 						break;
172ae115bc7Smrj 				} else {
173ae115bc7Smrj 					if (rp->r_gs == LWPGS_SEL)
174ae115bc7Smrj 						break;
175ae115bc7Smrj 				}
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		error = EINVAL;
1797c478bd9Sstevel@tonic-gate 		break;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	default:
1837c478bd9Sstevel@tonic-gate 		error = ENOTSUP;
1847c478bd9Sstevel@tonic-gate 		break;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	kpreempt_enable();
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (error != 0)
1897c478bd9Sstevel@tonic-gate 		return (error);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1927c478bd9Sstevel@tonic-gate 		if (sulword((void *)base, sbase) == -1)
1937c478bd9Sstevel@tonic-gate 			error = EFAULT;
1947c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1957c478bd9Sstevel@tonic-gate 	} else {
1967c478bd9Sstevel@tonic-gate 		if (suword32((void *)base, (uint32_t)sbase) == -1)
1977c478bd9Sstevel@tonic-gate 			error = EFAULT;
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	return (error);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * libc-private syscall for managing per-lwp %gs and %fs segment base values.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate int
syslwp_private(int cmd,int which,uintptr_t base)2077c478bd9Sstevel@tonic-gate syslwp_private(int cmd, int which, uintptr_t base)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
2107c478bd9Sstevel@tonic-gate 	int res, error;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	switch (cmd) {
2137c478bd9Sstevel@tonic-gate 	case _LWP_SETPRIVATE:
2147c478bd9Sstevel@tonic-gate 		res = lwp_setprivate(lwp, which, base);
2157c478bd9Sstevel@tonic-gate 		return (res < 0 ? set_errno(ENOTSUP) : res);
2167c478bd9Sstevel@tonic-gate 	case _LWP_GETPRIVATE:
2177c478bd9Sstevel@tonic-gate 		error = lwp_getprivate(lwp, which, base);
2187c478bd9Sstevel@tonic-gate 		return (error != 0 ? set_errno(error) : error);
2197c478bd9Sstevel@tonic-gate 	default:
2207c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate }
223