more header changes for amd64 port; the pc64 building infrastructure
[dragonfly/port-amd64.git] / sys / platform / pc64 / amd64 / tls.c
blobe4408adf88cdfdc604574b17228464b4e86d5e5f
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/pc64/amd64/tls.c,v 1.1 2007/09/23 04:29:31 yanyh Exp $
35 * $DragonFly: src/sys/platform/pc64/amd64/tls.c,v 1.1 2007/09/23 04:29:31 yanyh Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/sysent.h>
44 #include <sys/sysctl.h>
45 #include <sys/tls.h>
46 #include <sys/reg.h>
47 #include <sys/thread2.h>
49 #include <machine/cpu.h>
50 #include <machine/clock.h>
51 #include <machine/specialreg.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 * (struct tls_info *info, int infosize, int which)
67 int
68 sys_set_tls_area(struct set_tls_area_args *uap)
70 struct tls_info info;
71 struct segment_descriptor *desc;
72 int error;
73 int i;
76 * Sanity checks
78 i = uap->which;
79 if (i < 0 || i >= NGTLS)
80 return (ERANGE);
81 if (uap->infosize < 0)
82 return (EINVAL);
85 * Maintain forwards compatibility with future extensions.
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));
94 if (error)
95 return (error);
96 if (info.size < -1)
97 return (EINVAL);
98 if (info.size > (1 << 20))
99 info.size = (info.size + PAGE_MASK) & ~PAGE_MASK;
102 * Load the descriptor. A critical section is required in case
103 * an interrupt thread comes along and switches us out and then back
104 * in.
106 desc = &curthread->td_tls.tls[i];
107 crit_enter();
108 if (info.size == 0) {
109 bzero(desc, sizeof(*desc));
110 } else {
111 desc->sd_lobase = (intptr_t)info.base;
112 desc->sd_hibase = (intptr_t)info.base >> 24;
113 desc->sd_def32 = 1;
114 desc->sd_type = SDT_MEMRWA;
115 desc->sd_dpl = SEL_UPL;
116 desc->sd_xx = 0;
117 desc->sd_p = 1;
118 if (info.size == -1) {
120 * A descriptor size of -1 is a hack to map the
121 * whole address space. This type of mapping is
122 * required for direct-tls accesses of variable
123 * data, e.g. %gs:OFFSET where OFFSET is negative.
125 desc->sd_lolimit = -1;
126 desc->sd_hilimit = -1;
127 desc->sd_gran = 1;
128 } else if (info.size >= (1 << 20)) {
130 * A descriptor size greater then 1MB requires page
131 * granularity (the lo+hilimit field is only 20 bits)
133 desc->sd_lolimit = info.size >> PAGE_SHIFT;
134 desc->sd_hilimit = info.size >> (PAGE_SHIFT + 16);
135 desc->sd_gran = 1;
136 } else {
138 * Otherwise a byte-granular size is supported.
140 desc->sd_lolimit = info.size;
141 desc->sd_hilimit = info.size >> 16;
142 desc->sd_gran = 0;
145 crit_exit();
146 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
147 set_user_TLS();
148 return(0);
152 * Return the specified TLS descriptor to userland.
154 * Returns the value userland needs to load into %gs representing the
155 * TLS descriptor or -1 on error.
157 * (struct tls_info *info, int infosize, int which)
160 sys_get_tls_area(struct get_tls_area_args *uap)
162 struct tls_info info;
163 struct segment_descriptor *desc;
164 int error;
165 int i;
168 * Sanity checks
170 i = uap->which;
171 if (i < 0 || i >= NGTLS)
172 return (ERANGE);
173 if (uap->infosize < 0)
174 return (EINVAL);
177 * unpack the descriptor, ENOENT is returned for any descriptor
178 * which has not been loaded. uap->info may be NULL.
180 desc = &curthread->td_tls.tls[i];
181 if (desc->sd_p) {
182 if (uap->info && uap->infosize > 0) {
183 bzero(&info, sizeof(info));
184 info.base = (void *)(intptr_t)
185 ((desc->sd_hibase << 24) | desc->sd_lobase);
186 info.size = (desc->sd_hilimit << 16) | desc->sd_lolimit;
187 if (desc->sd_gran)
188 info.size <<= PAGE_SHIFT;
189 error = copyout(&info, uap->info,
190 min(sizeof(info), uap->infosize));
191 } else {
192 error = 0;
194 uap->sysmsg_result = GSEL(GTLS_START + i, SEL_UPL);
195 } else {
196 error = ENOENT;
198 return(error);
202 * This function is a NOP because the TLS segments are proactively copied
203 * by vmspace_ctl() when we switch to the (emulated) user process.
205 void
206 set_user_TLS(void)