| Commit | Line | Data |
|---|---|---|
| d7f50089 | 1 | /* |
| c8fe38ae | 2 | * Copyright (c) 2003,2004,2008 The DragonFly Project. All rights reserved. |
| d7f50089 YY |
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. | |
| d7f50089 YY |
33 | */ |
| 34 | ||
| 35 | #include <sys/param.h> | |
| 36 | #include <sys/systm.h> | |
| 37 | #include <sys/sysproto.h> | |
| 38 | #include <sys/kernel.h> | |
| 39 | #include <sys/proc.h> | |
| 40 | #include <sys/sysent.h> | |
| 41 | #include <sys/sysctl.h> | |
| 42 | #include <sys/tls.h> | |
| 43 | #include <sys/reg.h> | |
| c8fe38ae MD |
44 | #include <sys/globaldata.h> |
| 45 | ||
| d7f50089 YY |
46 | #include <sys/thread2.h> |
| 47 | ||
| 48 | #include <machine/cpu.h> | |
| 49 | #include <machine/clock.h> | |
| 50 | #include <machine/specialreg.h> | |
| c8fe38ae | 51 | #include <machine/segments.h> |
| d7f50089 YY |
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> | |
| c8fe38ae | 56 | #include <machine/pcb.h> |
| d7f50089 YY |
57 | |
| 58 | /* | |
| c1543a89 | 59 | * set a TLS descriptor. For x86_64 descriptor 0 identifies %fs and |
| c8fe38ae MD |
60 | * descriptor 1 identifies %gs, and 0 is returned in sysmsg_result. |
| 61 | * | |
| d7f50089 YY |
62 | * Returns the value userland needs to load into %gs representing the |
| 63 | * TLS descriptor or -1 on error. | |
| 64 | * | |
| 2b62f5a7 | 65 | * (int which, struct tls_info *info, size_t infosize) |
| d7f50089 YY |
66 | */ |
| 67 | int | |
| 68 | sys_set_tls_area(struct set_tls_area_args *uap) | |
| 69 | { | |
| 70 | struct tls_info info; | |
| d7f50089 YY |
71 | int error; |
| 72 | int i; | |
| 73 | ||
| 74 | /* | |
| 75 | * Sanity checks | |
| c8fe38ae MD |
76 | * |
| 77 | * which 0 == %fs, which 1 == %gs | |
| d7f50089 YY |
78 | */ |
| 79 | i = uap->which; | |
| c8fe38ae | 80 | if (i < 0 || i > 1) |
| d7f50089 YY |
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); | |
| d7f50089 YY |
99 | |
| 100 | /* | |
| c1543a89 | 101 | * For x86_64 we can only adjust FSBASE and GSBASE |
| d7f50089 | 102 | */ |
| c8fe38ae | 103 | curthread->td_tls.info[i] = info; |
| d7f50089 | 104 | set_user_TLS(); |
| c8fe38ae | 105 | uap->sysmsg_result = 0; /* segment descriptor $0 */ |
| d7f50089 YY |
106 | return(0); |
| 107 | } | |
| 108 | ||
| 109 | /* | |
| 110 | * Return the specified TLS descriptor to userland. | |
| 111 | * | |
| 112 | * Returns the value userland needs to load into %gs representing the | |
| 113 | * TLS descriptor or -1 on error. | |
| 114 | * | |
| 2b62f5a7 | 115 | * (int which, struct tls_info *info, size_t infosize) |
| 923e4699 MD |
116 | * |
| 117 | * MPSAFE | |
| d7f50089 YY |
118 | */ |
| 119 | int | |
| 120 | sys_get_tls_area(struct get_tls_area_args *uap) | |
| 121 | { | |
| 122 | struct tls_info info; | |
| d7f50089 YY |
123 | int error; |
| 124 | int i; | |
| 125 | ||
| 126 | /* | |
| 127 | * Sanity checks | |
| 128 | */ | |
| 129 | i = uap->which; | |
| c8fe38ae | 130 | if (i < 0 || i > 1) |
| d7f50089 YY |
131 | return (ERANGE); |
| 132 | if (uap->infosize < 0) | |
| 133 | return (EINVAL); | |
| 134 | ||
| c8fe38ae MD |
135 | info = curthread->td_tls.info[i]; |
| 136 | ||
| 137 | error = copyout(&info, uap->info, min(sizeof(info), uap->infosize)); | |
| d7f50089 YY |
138 | return(error); |
| 139 | } | |
| 140 | ||
| 141 | /* | |
| c8fe38ae | 142 | * Install the TLS |
| 923e4699 MD |
143 | * |
| 144 | * MPSAFE | |
| d7f50089 YY |
145 | */ |
| 146 | void | |
| 147 | set_user_TLS(void) | |
| 148 | { | |
| c8fe38ae MD |
149 | struct mdglobaldata *gd = mdcpu; |
| 150 | thread_t td = gd->mi.gd_curthread; | |
| 151 | ||
| 152 | td->td_pcb->pcb_fsbase = (register_t)td->td_tls.info[0].base; | |
| 153 | td->td_pcb->pcb_gsbase = (register_t)td->td_tls.info[1].base; | |
| 154 | if (gd->gd_user_fs != td->td_pcb->pcb_fsbase) { | |
| 155 | gd->gd_user_fs = td->td_pcb->pcb_fsbase; | |
| 156 | wrmsr(MSR_FSBASE, gd->gd_user_fs); | |
| 157 | } | |
| 158 | if (gd->gd_user_gs != td->td_pcb->pcb_gsbase) { | |
| 159 | gd->gd_user_gs = td->td_pcb->pcb_gsbase; | |
| 160 | wrmsr(MSR_KGSBASE, gd->gd_user_gs); | |
| 161 | } | |
| d7f50089 | 162 | } |
| c8fe38ae | 163 |