AMD64 - Sync AMD64 support from Jordan Gordeev's svn repository and
[dragonfly.git] / sys / platform / pc64 / amd64 / tls.c
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  * $DragonFly: src/sys/platform/pc64/amd64/tls.c,v 1.4 2008/08/29 17:07: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/reg.h>
46 #include <sys/globaldata.h>
47
48 #include <sys/thread2.h>
49
50 #include <machine/cpu.h>
51 #include <machine/clock.h>
52 #include <machine/specialreg.h>
53 #include <machine/segments.h>
54 #include <machine/md_var.h>
55 #include <machine/pcb_ext.h>            /* pcb.h included via sys/user.h */
56 #include <machine/globaldata.h>         /* CPU_prvspace */
57 #include <machine/smp.h>
58 #include <machine/pcb.h>
59
60 /*
61  * set a TLS descriptor.  For AMD64 descriptor 0 identifies %fs and
62  * descriptor 1 identifies %gs, and 0 is returned in sysmsg_result.
63  * 
64  * Returns the value userland needs to load into %gs representing the 
65  * TLS descriptor or -1 on error.
66  *
67  * (int which, struct tls_info *info, size_t infosize)
68  */
69 int
70 sys_set_tls_area(struct set_tls_area_args *uap)
71 {
72         struct tls_info info;
73         int error;
74         int i;
75
76         /*
77          * Sanity checks
78          *
79          * which 0 == %fs, which 1 == %gs
80          */
81         i = uap->which;
82         if (i < 0 || i > 1)
83                 return (ERANGE);
84         if (uap->infosize < 0)
85                 return (EINVAL);
86
87         /*
88          * Maintain forwards compatibility with future extensions.
89          */
90         if (uap->infosize != sizeof(info)) {
91                 bzero(&info, sizeof(info));
92                 error = copyin(uap->info, &info, 
93                                 min(sizeof(info), uap->infosize));
94         } else {
95                 error = copyin(uap->info, &info, sizeof(info));
96         }
97         if (error)
98                 return (error);
99         if (info.size < -1)
100                 return (EINVAL);
101
102         /*
103          * For AMD64 we can only adjust FSBASE and GSBASE
104          */
105         curthread->td_tls.info[i] = info;
106         set_user_TLS();
107         uap->sysmsg_result = 0; /* segment descriptor $0 */
108         return(0);
109 }
110         
111 /*
112  * Return the specified TLS descriptor to userland.
113  *
114  * Returns the value userland needs to load into %gs representing the 
115  * TLS descriptor or -1 on error.
116  *
117  * (int which, struct tls_info *info, size_t infosize)
118  */
119 int
120 sys_get_tls_area(struct get_tls_area_args *uap)
121 {
122         struct tls_info info;
123         int error;
124         int i;
125
126         /*
127          * Sanity checks
128          */
129         i = uap->which;
130         if (i < 0 || i > 1)
131                 return (ERANGE);
132         if (uap->infosize < 0)
133                 return (EINVAL);
134
135         info = curthread->td_tls.info[i];
136
137         error = copyout(&info, uap->info, min(sizeof(info), uap->infosize));
138         return(error);
139 }
140
141 /*
142  * Install the TLS
143  */
144 void
145 set_user_TLS(void)
146 {
147         struct mdglobaldata *gd = mdcpu;
148         thread_t td = gd->mi.gd_curthread;
149
150         td->td_pcb->pcb_fsbase = (register_t)td->td_tls.info[0].base;
151         td->td_pcb->pcb_gsbase = (register_t)td->td_tls.info[1].base;
152         if (gd->gd_user_fs != td->td_pcb->pcb_fsbase) {
153                 gd->gd_user_fs = td->td_pcb->pcb_fsbase;
154                 wrmsr(MSR_FSBASE, gd->gd_user_fs);
155         }
156         if (gd->gd_user_gs != td->td_pcb->pcb_gsbase) {
157                 gd->gd_user_gs = td->td_pcb->pcb_gsbase;
158                 wrmsr(MSR_KGSBASE, gd->gd_user_gs);
159         }
160 }
161