kernel - Fix some rare pmap races in i386 and x86_64.
[dragonfly.git] / sys / platform / pc32 / i386 / tls.c
blob9451a8b2ac5df04cdcb581b9462029d00adecd8f
1 /*
2 * Copyright (c) 2003,2004 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:
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.
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.
34 * $DragonFly: src/sys/platform/pc32/i386/tls.c,v 1.9 2008/06/29 19:04:01 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/sysent.h>
43 #include <sys/sysctl.h>
44 #include <sys/tls.h>
45 #include <sys/reg.h>
46 #include <sys/thread2.h>
48 #include <machine/cpu.h>
49 #include <machine/clock.h>
50 #include <machine/specialreg.h>
51 #include <machine/bootinfo.h>
52 #include <machine/md_var.h>
53 #include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
54 #include <machine/globaldata.h> /* CPU_prvspace */
55 #include <machine/smp.h>
58 * set a TLS descriptor and resync the GDT. A descriptor may be cleared
59 * by passing info=NULL and infosize=0. Note that hardware limitations may
60 * cause the size passed in tls_info to be approximated.
62 * Returns the value userland needs to load into %gs representing the
63 * TLS descriptor or -1 on error.
65 * (int which, struct tls_info *info, size_t infosize)
67 * MPSAFE
69 int
70 sys_set_tls_area(struct set_tls_area_args *uap)
72 struct tls_info info;
73 struct segment_descriptor *desc;
74 int error;
75 int i;
78 * Sanity checks
80 i = uap->which;
81 if (i < 0 || i >= NGTLS)
82 return (ERANGE);
83 if (uap->infosize < 0)
84 return (EINVAL);
87 * Maintain forwards compatibility with future extensions.
89 if (uap->infosize != sizeof(info)) {
90 bzero(&info, sizeof(info));
91 error = copyin(uap->info, &info,
92 min(sizeof(info), uap->infosize));
93 } else {
94 error = copyin(uap->info, &info, sizeof(info));
96 if (error)
97 return (error);
98 if (info.size < -1)
99 return (EINVAL);
100 if (info.size > (1 << 20))
101 info.size = (info.size + PAGE_MASK) & ~PAGE_MASK;
104 * Load the descriptor. A critical section is required in case
105 * an interrupt thread comes along and switches us out and then back
106 * in.
108 desc = &curthread->td_tls.tls[i];
109 crit_enter();
110 if (info.size == 0) {
111 bzero(desc, sizeof(*desc));
112 } else {
113 desc->sd_lobase = (intptr_t)info.base;
114 desc->sd_hibase = (intptr_t)info.base >> 24;
115 desc->sd_def32 = 1;
116 desc->sd_type = SDT_MEMRWA;
117 desc->sd_dpl = SEL_UPL;
118 desc->sd_xx = 0;
119 desc->sd_p = 1;
120 if (info.size == -1) {
122 * A descriptor size of -1 is a hack to map the
123 * whole address space. This type of mapping is
124 * required for direct-tls accesses of variable
125 * data, e.g. %gs:OFFSET where OFFSET is negative.
127 desc->sd_lolimit = -1;
128 desc->sd_hilimit = -1;
129 desc->sd_gran = 1;
130 } else if (info.size >= (1 << 20)) {
132 * A descriptor size greater then 1MB requires page
133 * granularity (the lo+hilimit field is only 20 bits)
135 desc->sd_lolimit = info.size >> PAGE_SHIFT;
136 desc->sd_hilimit = info.size >> (PAGE_SHIFT + 16);
137 desc->sd_gran = 1;
138 } else {
140 * Otherwise a byte-granular size is supported.
142 desc->sd_lolimit = info.size;
143 desc->sd_hilimit = info.size >> 16;
144 desc->sd_gran = 0;
147 crit_exit();
148 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
149 set_user_TLS();
150 return(0);
154 * Return the specified TLS descriptor to userland.
156 * Returns the value userland needs to load into %gs representing the
157 * TLS descriptor or -1 on error.
159 * (int which, struct tls_info *info, size_t infosize)
161 * MPSAFE
164 sys_get_tls_area(struct get_tls_area_args *uap)
166 struct tls_info info;
167 struct segment_descriptor *desc;
168 int error;
169 int i;
172 * Sanity checks
174 i = uap->which;
175 if (i < 0 || i >= NGTLS)
176 return (ERANGE);
177 if (uap->infosize < 0)
178 return (EINVAL);
181 * unpack the descriptor, ENOENT is returned for any descriptor
182 * which has not been loaded. uap->info may be NULL.
184 desc = &curthread->td_tls.tls[i];
185 if (desc->sd_p) {
186 if (uap->info && uap->infosize > 0) {
187 bzero(&info, sizeof(info));
188 info.base = (void *)(intptr_t)
189 ((desc->sd_hibase << 24) | desc->sd_lobase);
190 info.size = (desc->sd_hilimit << 16) | desc->sd_lolimit;
191 if (desc->sd_gran)
192 info.size <<= PAGE_SHIFT;
193 error = copyout(&info, uap->info,
194 min(sizeof(info), uap->infosize));
195 } else {
196 error = 0;
198 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
199 } else {
200 error = ENOENT;
202 return(error);