sys_machdep.c revision 1.24 1 /* $NetBSD: sys_machdep.c,v 1.24 2010/07/07 01:20:50 chs Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2007, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.24 2010/07/07 01:20:50 chs Exp $");
34
35 #include "opt_mtrr.h"
36 #include "opt_perfctrs.h"
37 #include "opt_user_ldt.h"
38 #include "opt_vm86.h"
39 #include "opt_xen.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioctl.h>
44 #include <sys/file.h>
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <sys/uio.h>
48 #include <sys/kernel.h>
49 #include <sys/buf.h>
50 #include <sys/signal.h>
51 #include <sys/malloc.h>
52 #include <sys/kmem.h>
53 #include <sys/kauth.h>
54 #include <sys/cpu.h>
55 #include <sys/mount.h>
56 #include <sys/syscallargs.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <machine/cpufunc.h>
61 #include <machine/gdt.h>
62 #include <machine/psl.h>
63 #include <machine/reg.h>
64 #include <machine/sysarch.h>
65 #include <machine/mtrr.h>
66
67 #ifdef __x86_64__
68 /* Need to be checked. */
69 #undef USER_LDT
70 #undef PERFCTRS
71 #undef VM86
72 #undef IOPERM
73 #else
74 #if defined(XEN)
75 #undef IOPERM
76 #else /* defined(XEN) */
77 #define IOPERM
78 #endif /* defined(XEN) */
79 #endif
80
81 #ifdef VM86
82 #include <machine/vm86.h>
83 #endif
84
85 #ifdef PERFCTRS
86 #include <machine/pmc.h>
87 #endif
88
89 extern struct vm_map *kernel_map;
90
91 int x86_get_ioperm(struct lwp *, void *, register_t *);
92 int x86_set_ioperm(struct lwp *, void *, register_t *);
93 int x86_get_mtrr(struct lwp *, void *, register_t *);
94 int x86_set_mtrr(struct lwp *, void *, register_t *);
95 int x86_set_sdbase32(void *, char, lwp_t *, bool);
96 int x86_set_sdbase(void *, char, lwp_t *, bool);
97 int x86_get_sdbase32(void *, char);
98 int x86_get_sdbase(void *, char);
99
100 #ifdef LDT_DEBUG
101 static void x86_print_ldt(int, const struct segment_descriptor *);
102
103 static void
104 x86_print_ldt(int i, const struct segment_descriptor *d)
105 {
106 printf("[%d] lolimit=0x%x, lobase=0x%x, type=%u, dpl=%u, p=%u, "
107 "hilimit=0x%x, xx=%x, def32=%u, gran=%u, hibase=0x%x\n",
108 i, d->sd_lolimit, d->sd_lobase, d->sd_type, d->sd_dpl, d->sd_p,
109 d->sd_hilimit, d->sd_xx, d->sd_def32, d->sd_gran, d->sd_hibase);
110 }
111 #endif
112
113 int
114 x86_get_ldt(struct lwp *l, void *args, register_t *retval)
115 {
116 #ifndef USER_LDT
117 return EINVAL;
118 #else
119 struct x86_get_ldt_args ua;
120 union descriptor *cp;
121 int error;
122
123 if ((error = copyin(args, &ua, sizeof(ua))) != 0)
124 return error;
125
126 if (ua.num < 0 || ua.num > 8192)
127 return EINVAL;
128
129 cp = malloc(ua.num * sizeof(union descriptor), M_TEMP, M_WAITOK);
130 if (cp == NULL)
131 return ENOMEM;
132
133 error = x86_get_ldt1(l, &ua, cp);
134 *retval = ua.num;
135 if (error == 0)
136 error = copyout(cp, ua.desc, ua.num * sizeof(*cp));
137
138 free(cp, M_TEMP);
139 return error;
140 #endif
141 }
142
143 int
144 x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
145 {
146 #ifndef USER_LDT
147 return EINVAL;
148 #else
149 int error;
150 struct proc *p = l->l_proc;
151 pmap_t pmap = p->p_vmspace->vm_map.pmap;
152 int nldt, num;
153 union descriptor *lp;
154
155 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_GET,
156 NULL, NULL, NULL, NULL);
157 if (error)
158 return (error);
159
160 #ifdef LDT_DEBUG
161 printf("x86_get_ldt: start=%d num=%d descs=%p\n", ua->start,
162 ua->num, ua->desc);
163 #endif
164
165 if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
166 ua->start + ua->num > 8192)
167 return (EINVAL);
168
169 mutex_enter(&cpu_lock);
170
171 if (pmap->pm_ldt != NULL) {
172 nldt = pmap->pm_ldt_len / sizeof(*lp);
173 lp = pmap->pm_ldt;
174 } else {
175 nldt = NLDT;
176 lp = ldt;
177 }
178
179 if (ua->start > nldt) {
180 mutex_exit(&cpu_lock);
181 return (EINVAL);
182 }
183
184 lp += ua->start;
185 num = min(ua->num, nldt - ua->start);
186 ua->num = num;
187 #ifdef LDT_DEBUG
188 {
189 int i;
190 for (i = 0; i < num; i++)
191 x86_print_ldt(i, &lp[i].sd);
192 }
193 #endif
194
195 memcpy(cp, lp, num * sizeof(union descriptor));
196 mutex_exit(&cpu_lock);
197
198 return 0;
199 #endif
200 }
201
202 int
203 x86_set_ldt(struct lwp *l, void *args, register_t *retval)
204 {
205 #ifndef USER_LDT
206 return EINVAL;
207 #else
208 struct x86_set_ldt_args ua;
209 union descriptor *descv;
210 int error;
211
212 if ((error = copyin(args, &ua, sizeof(ua))) != 0)
213 return (error);
214
215 if (ua.num < 0 || ua.num > 8192)
216 return EINVAL;
217
218 descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
219 if (descv == NULL)
220 return ENOMEM;
221
222 error = copyin(ua.desc, descv, sizeof (*descv) * ua.num);
223 if (error == 0)
224 error = x86_set_ldt1(l, &ua, descv);
225 *retval = ua.start;
226
227 free(descv, M_TEMP);
228 return error;
229 #endif
230 }
231
232 int
233 x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
234 union descriptor *descv)
235 {
236 #ifndef USER_LDT
237 return EINVAL;
238 #else
239 int error, i, n, old_sel, new_sel;
240 struct proc *p = l->l_proc;
241 pmap_t pmap = p->p_vmspace->vm_map.pmap;
242 size_t old_len, new_len;
243 union descriptor *old_ldt, *new_ldt;
244
245 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_SET,
246 NULL, NULL, NULL, NULL);
247 if (error)
248 return (error);
249
250 if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
251 ua->start + ua->num > 8192)
252 return (EINVAL);
253
254 /* Check descriptors for access violations. */
255 for (i = 0; i < ua->num; i++) {
256 union descriptor *desc = &descv[i];
257
258 switch (desc->sd.sd_type) {
259 case SDT_SYSNULL:
260 desc->sd.sd_p = 0;
261 break;
262 case SDT_SYS286CGT:
263 case SDT_SYS386CGT:
264 /*
265 * Only allow call gates targeting a segment
266 * in the LDT or a user segment in the fixed
267 * part of the gdt. Segments in the LDT are
268 * constrained (below) to be user segments.
269 */
270 if (desc->gd.gd_p != 0 &&
271 !ISLDT(desc->gd.gd_selector) &&
272 ((IDXSEL(desc->gd.gd_selector) >= NGDT) ||
273 (gdt[IDXSEL(desc->gd.gd_selector)].sd.sd_dpl !=
274 SEL_UPL))) {
275 return EACCES;
276 }
277 break;
278 case SDT_MEMEC:
279 case SDT_MEMEAC:
280 case SDT_MEMERC:
281 case SDT_MEMERAC:
282 /* Must be "present" if executable and conforming. */
283 if (desc->sd.sd_p == 0)
284 return EACCES;
285 break;
286 case SDT_MEMRO:
287 case SDT_MEMROA:
288 case SDT_MEMRW:
289 case SDT_MEMRWA:
290 case SDT_MEMROD:
291 case SDT_MEMRODA:
292 case SDT_MEMRWD:
293 case SDT_MEMRWDA:
294 case SDT_MEME:
295 case SDT_MEMEA:
296 case SDT_MEMER:
297 case SDT_MEMERA:
298 break;
299 default:
300 /*
301 * Make sure that unknown descriptor types are
302 * not marked present.
303 */
304 if (desc->sd.sd_p != 0)
305 return EACCES;
306 break;
307 }
308
309 if (desc->sd.sd_p != 0) {
310 /* Only user (ring-3) descriptors may be present. */
311 if (desc->sd.sd_dpl != SEL_UPL)
312 return EACCES;
313 }
314 }
315
316 /*
317 * Install selected changes. We perform a copy, write, swap dance
318 * here to ensure that all updates happen atomically.
319 */
320
321 /* Allocate a new LDT. */
322 for (;;) {
323 new_len = (ua->start + ua->num) * sizeof(union descriptor);
324 new_len = max(new_len, pmap->pm_ldt_len);
325 new_len = max(new_len, NLDT * sizeof(union descriptor));
326 new_len = round_page(new_len);
327 new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
328 new_len, 0, UVM_KMF_WIRED | UVM_KMF_ZERO);
329 mutex_enter(&cpu_lock);
330 if (pmap->pm_ldt_len <= new_len) {
331 break;
332 }
333 mutex_exit(&cpu_lock);
334 uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
335 UVM_KMF_WIRED);
336 }
337
338 /* Copy existing entries, if any. */
339 if (pmap->pm_ldt != NULL) {
340 old_ldt = pmap->pm_ldt;
341 old_len = pmap->pm_ldt_len;
342 old_sel = pmap->pm_ldt_sel;
343 memcpy(new_ldt, old_ldt, old_len);
344 } else {
345 old_ldt = NULL;
346 old_len = 0;
347 old_sel = -1;
348 memcpy(new_ldt, ldt, NLDT * sizeof(union descriptor));
349 }
350
351 /* Apply requested changes. */
352 for (i = 0, n = ua->start; i < ua->num; i++, n++) {
353 new_ldt[n] = descv[i];
354 }
355
356 /* Allocate LDT selector. */
357 new_sel = ldt_alloc(new_ldt, new_len);
358 if (new_sel == -1) {
359 mutex_exit(&cpu_lock);
360 uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
361 UVM_KMF_WIRED);
362 return ENOMEM;
363 }
364
365 /* All changes are now globally visible. Swap in the new LDT. */
366 pmap->pm_ldt = new_ldt;
367 pmap->pm_ldt_len = new_len;
368 pmap->pm_ldt_sel = new_sel;
369
370 /* Switch existing users onto new LDT. */
371 pmap_ldt_sync(pmap);
372
373 /* Free existing LDT (if any). */
374 if (old_ldt != NULL) {
375 ldt_free(old_sel);
376 uvm_km_free(kernel_map, (vaddr_t)old_ldt, old_len,
377 UVM_KMF_WIRED);
378 }
379 mutex_exit(&cpu_lock);
380
381 return error;
382 #endif
383 }
384
385 int
386 x86_iopl(struct lwp *l, void *args, register_t *retval)
387 {
388 int error;
389 struct x86_iopl_args ua;
390 #ifdef XEN
391 int iopl;
392 #else
393 struct trapframe *tf = l->l_md.md_regs;
394 #endif
395
396 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
397 NULL, NULL, NULL, NULL);
398 if (error)
399 return (error);
400
401 if ((error = copyin(args, &ua, sizeof(ua))) != 0)
402 return error;
403
404 #ifdef XEN
405 if (ua.iopl)
406 iopl = SEL_UPL;
407 else
408 iopl = SEL_KPL;
409
410 {
411 struct physdev_op physop;
412 struct pcb *pcb;
413
414 pcb = lwp_getpcb(l);
415 pcb->pcb_iopl = iopl;
416
417 /* Force the change at ring 0. */
418 physop.cmd = PHYSDEVOP_SET_IOPL;
419 physop.u.set_iopl.iopl = iopl;
420 HYPERVISOR_physdev_op(&physop);
421 }
422 #elif defined(__x86_64__)
423 if (ua.iopl)
424 tf->tf_rflags |= PSL_IOPL;
425 else
426 tf->tf_rflags &= ~PSL_IOPL;
427 #else
428 if (ua.iopl)
429 tf->tf_eflags |= PSL_IOPL;
430 else
431 tf->tf_eflags &= ~PSL_IOPL;
432 #endif
433
434 return 0;
435 }
436
437 int
438 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
439 {
440 #ifdef IOPERM
441 int error;
442 struct pcb *pcb = lwp_getpcb(l);
443 struct x86_get_ioperm_args ua;
444 void *dummymap = NULL;
445 void *iomap;
446
447 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
448 NULL, NULL, NULL, NULL);
449 if (error)
450 return (error);
451
452 if ((error = copyin(args, &ua, sizeof(ua))) != 0)
453 return (error);
454
455 iomap = pcb->pcb_iomap;
456 if (iomap == NULL) {
457 iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
458 memset(dummymap, 0xff, IOMAPSIZE);
459 }
460 error = copyout(iomap, ua.iomap, IOMAPSIZE);
461 if (dummymap != NULL) {
462 kmem_free(dummymap, IOMAPSIZE);
463 }
464 return error;
465 #else
466 return EINVAL;
467 #endif
468 }
469
470 int
471 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
472 {
473 #ifdef IOPERM
474 struct cpu_info *ci;
475 int error;
476 struct pcb *pcb = lwp_getpcb(l);
477 struct x86_set_ioperm_args ua;
478 void *new;
479 void *old;
480
481 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
482 NULL, NULL, NULL, NULL);
483 if (error)
484 return (error);
485
486 if ((error = copyin(args, &ua, sizeof(ua))) != 0)
487 return (error);
488
489 new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
490 error = copyin(ua.iomap, new, IOMAPSIZE);
491 if (error) {
492 kmem_free(new, IOMAPSIZE);
493 return error;
494 }
495 old = pcb->pcb_iomap;
496 pcb->pcb_iomap = new;
497 if (old != NULL) {
498 kmem_free(old, IOMAPSIZE);
499 }
500
501 kpreempt_disable();
502 ci = curcpu();
503 memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
504 ci->ci_tss.tss_iobase =
505 ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
506 kpreempt_enable();
507
508 return error;
509 #else
510 return EINVAL;
511 #endif
512 }
513
514 int
515 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
516 {
517 #ifdef MTRR
518 struct x86_get_mtrr_args ua;
519 int error, n;
520
521 if (mtrr_funcs == NULL)
522 return ENOSYS;
523
524 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
525 NULL, NULL, NULL, NULL);
526 if (error)
527 return (error);
528
529 error = copyin(args, &ua, sizeof ua);
530 if (error != 0)
531 return error;
532
533 error = copyin(ua.n, &n, sizeof n);
534 if (error != 0)
535 return error;
536
537 KERNEL_LOCK(1, NULL);
538 error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
539 KERNEL_UNLOCK_ONE(NULL);
540
541 copyout(&n, ua.n, sizeof (int));
542
543 return error;
544 #else
545 return EINVAL;
546 #endif
547 }
548
549 int
550 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
551 {
552 #ifdef MTRR
553 int error, n;
554 struct x86_set_mtrr_args ua;
555
556 if (mtrr_funcs == NULL)
557 return ENOSYS;
558
559 error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
560 NULL, NULL, NULL, NULL);
561 if (error)
562 return (error);
563
564 error = copyin(args, &ua, sizeof ua);
565 if (error != 0)
566 return error;
567
568 error = copyin(ua.n, &n, sizeof n);
569 if (error != 0)
570 return error;
571
572 KERNEL_LOCK(1, NULL);
573 error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
574 if (n != 0)
575 mtrr_commit();
576 KERNEL_UNLOCK_ONE(NULL);
577
578 copyout(&n, ua.n, sizeof n);
579
580 return error;
581 #else
582 return EINVAL;
583 #endif
584 }
585
586 #ifdef __x86_64__
587 #define pcb_fsd pcb_fs
588 #define pcb_gsd pcb_gs
589 #define segment_descriptor mem_segment_descriptor
590 #endif
591
592 int
593 x86_set_sdbase32(void *arg, char which, lwp_t *l, bool direct)
594 {
595 struct trapframe *tf = l->l_md.md_regs;
596 union descriptor usd;
597 struct pcb *pcb;
598 uint32_t base;
599 int error;
600
601 if (direct) {
602 base = (vaddr_t)arg;
603 } else {
604 error = copyin(arg, &base, sizeof(base));
605 if (error != 0)
606 return error;
607 }
608
609 memset(&usd, 0, sizeof(usd));
610 usd.sd.sd_lobase = base & 0xffffff;
611 usd.sd.sd_hibase = (base >> 24) & 0xff;
612 usd.sd.sd_lolimit = 0xffff;
613 usd.sd.sd_hilimit = 0xf;
614 usd.sd.sd_type = SDT_MEMRWA;
615 usd.sd.sd_dpl = SEL_UPL;
616 usd.sd.sd_p = 1;
617 usd.sd.sd_def32 = 1;
618 usd.sd.sd_gran = 1;
619
620 pcb = lwp_getpcb(l);
621 kpreempt_disable();
622 if (which == 'f') {
623 memcpy(&pcb->pcb_fsd, &usd.sd,
624 sizeof(struct segment_descriptor));
625 if (l == curlwp) {
626 update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &usd);
627 #ifdef __x86_64__
628 setfs(GSEL(GUFS_SEL, SEL_UPL));
629 #endif
630 }
631 tf->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
632 } else /* which == 'g' */ {
633 memcpy(&pcb->pcb_gsd, &usd.sd,
634 sizeof(struct segment_descriptor));
635 if (l == curlwp) {
636 update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &usd);
637 #ifdef __x86_64__
638 #ifndef XEN
639 setusergs(GSEL(GUGS_SEL, SEL_UPL));
640 #else
641 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL,
642 GSEL(GUGS_SEL, SEL_UPL));
643 #endif
644 #endif
645 }
646 tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
647 }
648 kpreempt_enable();
649 return 0;
650 }
651
652 int
653 x86_set_sdbase(void *arg, char which, lwp_t *l, bool direct)
654 {
655 #ifdef i386
656 return x86_set_sdbase32(arg, which, l, direct);
657 #else
658 struct pcb *pcb;
659 vaddr_t base;
660 int error;
661
662 if (l->l_proc->p_flag & PK_32) {
663 return x86_set_sdbase32(arg, which, l, direct);
664 }
665
666 if (direct) {
667 base = (vaddr_t)arg;
668 } else {
669 error = copyin(arg, &base, sizeof(base));
670 if (error != 0)
671 return error;
672 }
673
674 if (base >= VM_MAXUSER_ADDRESS)
675 return EINVAL;
676
677 if (error) {
678 return error;
679 }
680
681 pcb = lwp_getpcb(l);
682
683 kpreempt_disable();
684 switch(which) {
685 case 'f':
686 pcb->pcb_fs = base;
687 if (l == curlwp)
688 wrmsr(MSR_FSBASE, pcb->pcb_fs);
689 break;
690 case 'g':
691 pcb->pcb_gs = base;
692 if (l == curlwp)
693 wrmsr(MSR_KERNELGSBASE, pcb->pcb_gs);
694 break;
695 default:
696 panic("x86_get_sdbase");
697 }
698 kpreempt_enable();
699
700 return error;
701 #endif
702 }
703
704 int
705 x86_get_sdbase32(void *arg, char which)
706 {
707 struct segment_descriptor *sd;
708 uint32_t base;
709
710 switch (which) {
711 case 'f':
712 sd = (void *)&curpcb->pcb_fsd;
713 break;
714 case 'g':
715 sd = (void *)&curpcb->pcb_gsd;
716 break;
717 default:
718 panic("x86_get_sdbase");
719 }
720
721 base = sd->sd_hibase << 24 | sd->sd_lobase;
722 return copyout(&base, arg, sizeof(base));
723 }
724
725 int
726 x86_get_sdbase(void *arg, char which)
727 {
728 #ifdef i386
729 return x86_get_sdbase32(arg, which);
730 #else
731 vaddr_t base;
732 struct pcb *pcb;
733
734 if (curproc->p_flag & PK_32) {
735 return x86_get_sdbase32(arg, which);
736 }
737
738 pcb = lwp_getpcb(curlwp);
739
740 switch(which) {
741 case 'f':
742 base = pcb->pcb_fs;
743 break;
744 case 'g':
745 base = pcb->pcb_gs;
746 break;
747 default:
748 panic("x86_get_sdbase");
749 }
750
751 return copyout(&base, arg, sizeof(base));
752 #endif
753 }
754
755 int
756 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
757 {
758 /* {
759 syscallarg(int) op;
760 syscallarg(void *) parms;
761 } */
762 int error = 0;
763
764 switch(SCARG(uap, op)) {
765 case X86_IOPL:
766 error = x86_iopl(l, SCARG(uap, parms), retval);
767 break;
768
769 case X86_GET_LDT:
770 error = x86_get_ldt(l, SCARG(uap, parms), retval);
771 break;
772
773 case X86_SET_LDT:
774 error = x86_set_ldt(l, SCARG(uap, parms), retval);
775 break;
776
777 case X86_GET_IOPERM:
778 error = x86_get_ioperm(l, SCARG(uap, parms), retval);
779 break;
780
781 case X86_SET_IOPERM:
782 error = x86_set_ioperm(l, SCARG(uap, parms), retval);
783 break;
784
785 case X86_GET_MTRR:
786 error = x86_get_mtrr(l, SCARG(uap, parms), retval);
787 break;
788 case X86_SET_MTRR:
789 error = x86_set_mtrr(l, SCARG(uap, parms), retval);
790 break;
791
792 #ifdef VM86
793 case X86_VM86:
794 error = x86_vm86(l, SCARG(uap, parms), retval);
795 break;
796 case X86_OLD_VM86:
797 error = compat_16_x86_vm86(l, SCARG(uap, parms), retval);
798 break;
799 #endif
800
801 #ifdef PERFCTRS
802 case X86_PMC_INFO:
803 KERNEL_LOCK(1, NULL);
804 error = pmc_info(l, SCARG(uap, parms), retval);
805 KERNEL_UNLOCK_ONE(NULL);
806 break;
807
808 case X86_PMC_STARTSTOP:
809 KERNEL_LOCK(1, NULL);
810 error = pmc_startstop(l, SCARG(uap, parms), retval);
811 KERNEL_UNLOCK_ONE(NULL);
812 break;
813
814 case X86_PMC_READ:
815 KERNEL_LOCK(1, NULL);
816 error = pmc_read(l, SCARG(uap, parms), retval);
817 KERNEL_UNLOCK_ONE(NULL);
818 break;
819 #endif
820
821 case X86_SET_FSBASE:
822 error = x86_set_sdbase(SCARG(uap, parms), 'f', curlwp, false);
823 break;
824
825 case X86_SET_GSBASE:
826 error = x86_set_sdbase(SCARG(uap, parms), 'g', curlwp, false);
827 break;
828
829 case X86_GET_FSBASE:
830 error = x86_get_sdbase(SCARG(uap, parms), 'f');
831 break;
832
833 case X86_GET_GSBASE:
834 error = x86_get_sdbase(SCARG(uap, parms), 'g');
835 break;
836
837 default:
838 error = EINVAL;
839 break;
840 }
841 return (error);
842 }
843
844 int
845 cpu_lwp_setprivate(lwp_t *l, void *addr)
846 {
847
848 #ifdef __x86_64__
849 if ((l->l_proc->p_flag & PK_32) == 0) {
850 return x86_set_sdbase(addr, 'f', l, true);
851 }
852 #endif
853 return x86_set_sdbase(addr, 'g', l, true);
854 }
855