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