Modify kern/makesyscall.sh to prefix all kernel system call procedures
[dragonfly.git] / sys / platform / pc32 / i386 / tls.c
... / ...
CommitLineData
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:
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 * $DragonFly: src/sys/platform/pc32/i386/tls.c,v 1.4 2006/06/05 07:26:10 dillon Exp $
35 */
36
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/thread2.h>
46
47#include <machine/cpu.h>
48#include <machine/reg.h>
49#include <machine/clock.h>
50#include <machine/specialreg.h>
51#include <machine/bootinfo.h>
52#include <machine/ipl.h>
53#include <machine/md_var.h>
54#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
55#include <machine/globaldata.h> /* CPU_prvspace */
56#include <machine/smp.h>
57
58/*
59 * set a TLS descriptor and resync the GDT. A descriptor may be cleared
60 * by passing info=NULL and infosize=0. Note that hardware limitations may
61 * cause the size passed in tls_info to be approximated.
62 *
63 * Returns the value userland needs to load into %gs representing the
64 * TLS descriptor or -1 on error.
65 *
66 * (struct tls_info *info, int infosize, int which)
67 */
68int
69sys_sys_set_tls_area(struct sys_set_tls_area_args *uap)
70{
71 struct tls_info info;
72 struct segment_descriptor *desc;
73 int error;
74 int i;
75
76 /*
77 * Sanity checks
78 */
79 i = uap->which;
80 if (i < 0 || i >= NGTLS)
81 return (ERANGE);
82 if (uap->infosize < 0)
83 return (EINVAL);
84
85 /*
86 * Maintain forwards compatibility with future extensions.
87 */
88 if (uap->infosize != sizeof(info)) {
89 bzero(&info, sizeof(info));
90 error = copyin(uap->info, &info,
91 min(sizeof(info), uap->infosize));
92 } else {
93 error = copyin(uap->info, &info, sizeof(info));
94 }
95 if (error)
96 return (error);
97 if (info.size < -1)
98 return (EINVAL);
99 if (info.size > (1 << 20))
100 info.size = (info.size + PAGE_MASK) & ~PAGE_MASK;
101
102 /*
103 * Load the descriptor. A critical section is required in case
104 * an interrupt thread comes along and switches us out and then back
105 * in.
106 */
107 desc = &curthread->td_tls[i];
108 crit_enter();
109 if (info.size == 0) {
110 bzero(desc, sizeof(*desc));
111 } else {
112 desc->sd_lobase = (intptr_t)info.base;
113 desc->sd_hibase = (intptr_t)info.base >> 24;
114 desc->sd_def32 = 1;
115 desc->sd_type = SDT_MEMRWA;
116 desc->sd_dpl = SEL_UPL;
117 desc->sd_xx = 0;
118 desc->sd_p = 1;
119 if (info.size == -1) {
120 /*
121 * A descriptor size of -1 is a hack to map the
122 * whole address space. This type of mapping is
123 * required for direct-tls accesses of variable
124 * data, e.g. %gs:OFFSET where OFFSET is negative.
125 */
126 desc->sd_lolimit = -1;
127 desc->sd_hilimit = -1;
128 desc->sd_gran = 1;
129 } else if (info.size >= (1 << 20)) {
130 /*
131 * A descriptor size greater then 1MB requires page
132 * granularity (the lo+hilimit field is only 20 bits)
133 */
134 desc->sd_lolimit = info.size >> PAGE_SHIFT;
135 desc->sd_hilimit = info.size >> (PAGE_SHIFT + 16);
136 desc->sd_gran = 1;
137 } else {
138 /*
139 * Otherwise a byte-granular size is supported.
140 */
141 desc->sd_lolimit = info.size;
142 desc->sd_hilimit = info.size >> 16;
143 desc->sd_gran = 0;
144 }
145 }
146 crit_exit();
147 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
148 set_user_TLS();
149 return(0);
150}
151
152/*
153 * Return the specified TLS descriptor to userland.
154 *
155 * Returns the value userland needs to load into %gs representing the
156 * TLS descriptor or -1 on error.
157 *
158 * (struct tls_info *info, int infosize, int which)
159 */
160int
161sys_sys_get_tls_area(struct sys_get_tls_area_args *uap)
162{
163 struct tls_info info;
164 struct segment_descriptor *desc;
165 int error;
166 int i;
167
168 /*
169 * Sanity checks
170 */
171 i = uap->which;
172 if (i < 0 || i >= NGTLS)
173 return (ERANGE);
174 if (uap->infosize < 0)
175 return (EINVAL);
176
177 /*
178 * unpack the descriptor, ENOENT is returned for any descriptor
179 * which has not been loaded. uap->info may be NULL.
180 */
181 desc = &curthread->td_tls[i];
182 if (desc->sd_p) {
183 if (uap->info && uap->infosize > 0) {
184 bzero(&info, sizeof(info));
185 info.base = (void *)(intptr_t)
186 ((desc->sd_hibase << 24) | desc->sd_lobase);
187 info.size = (desc->sd_hilimit << 16) | desc->sd_lolimit;
188 if (desc->sd_gran)
189 info.size <<= PAGE_SHIFT;
190 error = copyout(&info, uap->info,
191 min(sizeof(info), uap->infosize));
192 } else {
193 error = 0;
194 }
195 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
196 } else {
197 error = ENOENT;
198 }
199 return(error);
200}
201