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