xref: /dragonfly/sys/platform/vkernel64/x86_64/tls.c (revision 3d33658b)
1 /*
2  * Copyright (c) 2003,2004,2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by David Xu <davidxu@t2t2.com> and Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysproto.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/sysent.h>
42 #include <sys/sysctl.h>
43 #include <sys/tls.h>
44 #include <sys/reg.h>
45 #include <sys/globaldata.h>
46 
47 #include <machine/cpu.h>
48 #include <machine/clock.h>
49 #include <machine/specialreg.h>
50 #include <machine/segments.h>
51 #include <machine/md_var.h>
52 #include <machine/pcb_ext.h>
53 #include <machine/globaldata.h>		/* CPU_prvspace */
54 #include <machine/smp.h>
55 #include <machine/pcb.h>
56 
57 /*
58  * set a TLS descriptor.  For x86_64 descriptor 0 identifies %fs and
59  * descriptor 1 identifies %gs, and 0 is returned in sysmsg_result.
60  *
61  * Returns the value userland needs to load into %gs representing the
62  * TLS descriptor or -1 on error.
63  *
64  * (int which, struct tls_info *info, size_t infosize)
65  */
66 int
67 sys_set_tls_area(struct set_tls_area_args *uap)
68 {
69 	struct tls_info info;
70 	int error;
71 	int i;
72 
73 	/*
74 	 * Sanity checks
75 	 *
76 	 * which 0 == %fs, which 1 == %gs
77 	 */
78 	i = uap->which;
79 	if (i < 0 || i > 1)
80 		return (ERANGE);
81 	if (uap->infosize < 0)
82 		return (EINVAL);
83 
84 	/*
85 	 * Maintain forwards compatibility with future extensions.
86 	 */
87 	if (uap->infosize != sizeof(info)) {
88 		bzero(&info, sizeof(info));
89 		error = copyin(uap->info, &info,
90 				min(sizeof(info), uap->infosize));
91 	} else {
92 		error = copyin(uap->info, &info, sizeof(info));
93 	}
94 	if (error)
95 		return (error);
96 	if (info.size < -1)
97 		return (EINVAL);
98 
99 	/*
100 	 * For x86_64 we can only adjust FSBASE and GSBASE
101 	 */
102 	curthread->td_tls.info[i] = info;
103 	set_user_TLS();
104 	uap->sysmsg_result = 0;	/* segment descriptor $0 */
105 	return(0);
106 }
107 
108 /*
109  * Return the specified TLS descriptor to userland.
110  *
111  * Returns the value userland needs to load into %gs representing the
112  * TLS descriptor or -1 on error.
113  *
114  * (int which, struct tls_info *info, size_t infosize)
115  */
116 int
117 sys_get_tls_area(struct get_tls_area_args *uap)
118 {
119 	struct tls_info info;
120 	int error;
121 	int i;
122 
123 	/*
124 	 * Sanity checks
125 	 */
126 	i = uap->which;
127 	if (i < 0 || i > 1)
128 		return (ERANGE);
129 	if (uap->infosize < 0)
130 		return (EINVAL);
131 
132 	info = curthread->td_tls.info[i];
133 
134 	error = copyout(&info, uap->info, min(sizeof(info), uap->infosize));
135 	return(error);
136 }
137 
138 /*
139  * This function is a NOP because the TLS segments are proactively copied
140  * by vmspace_ctl() when we switch to the (emulated) user process.
141  */
142 void
143 set_user_TLS(void)
144 {
145 }
146