2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_capsicum.h"
36 #include "opt_kstack_pages.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
47 #include <sys/sysproto.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
54 #include <machine/cpu.h>
55 #include <machine/pcb.h>
56 #include <machine/pcb_ext.h>
57 #include <machine/proc.h>
58 #include <machine/sysarch.h>
60 #include <security/audit/audit.h>
62 #include <vm/vm_kern.h> /* for kernel_map */
65 #define LD_PER_PAGE 512
66 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
67 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
68 #define NULL_LDT_BASE ((caddr_t)NULL)
71 static void set_user_ldt_rv(struct vmspace *vmsp);
73 static int i386_set_ldt_data(struct thread *, int start, int num,
74 union descriptor *descs);
75 static int i386_ldt_grow(struct thread *td, int len);
78 fill_based_sd(struct segment_descriptor *sdp, uint32_t base)
81 sdp->sd_lobase = base & 0xffffff;
82 sdp->sd_hibase = (base >> 24) & 0xff;
83 sdp->sd_lolimit = 0xffff; /* 4GB limit, wraps around */
84 sdp->sd_hilimit = 0xf;
85 sdp->sd_type = SDT_MEMRWA;
86 sdp->sd_dpl = SEL_UPL;
93 #ifndef _SYS_SYSPROTO_H_
103 register struct sysarch_args *uap;
106 union descriptor *lp;
108 struct i386_ldt_args largs;
109 struct i386_ioperm_args iargs;
110 struct i386_get_xfpustate xfpu;
113 struct segment_descriptor sd, *sdp;
115 AUDIT_ARG_CMD(uap->op);
117 #ifdef CAPABILITY_MODE
119 * When adding new operations, add a new case statement here to
120 * explicitly indicate whether or not the operation is safe to
121 * perform in capability mode.
123 if (IN_CAPABILITY_MODE(td)) {
127 case I386_GET_IOPERM:
128 case I386_GET_FSBASE:
129 case I386_SET_FSBASE:
130 case I386_GET_GSBASE:
131 case I386_SET_GSBASE:
132 case I386_GET_XFPUSTATE:
135 case I386_SET_IOPERM:
138 if (KTRPOINT(td, KTR_CAPFAIL))
139 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
147 case I386_GET_IOPERM:
148 case I386_SET_IOPERM:
149 if ((error = copyin(uap->parms, &kargs.iargs,
150 sizeof(struct i386_ioperm_args))) != 0)
155 if ((error = copyin(uap->parms, &kargs.largs,
156 sizeof(struct i386_ldt_args))) != 0)
158 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
161 case I386_GET_XFPUSTATE:
162 if ((error = copyin(uap->parms, &kargs.xfpu,
163 sizeof(struct i386_get_xfpustate))) != 0)
172 error = i386_get_ldt(td, &kargs.largs);
175 if (kargs.largs.descs != NULL) {
176 lp = (union descriptor *)malloc(
177 kargs.largs.num * sizeof(union descriptor),
179 error = copyin(kargs.largs.descs, lp,
180 kargs.largs.num * sizeof(union descriptor));
182 error = i386_set_ldt(td, &kargs.largs, lp);
185 error = i386_set_ldt(td, &kargs.largs, NULL);
188 case I386_GET_IOPERM:
189 error = i386_get_ioperm(td, &kargs.iargs);
191 error = copyout(&kargs.iargs, uap->parms,
192 sizeof(struct i386_ioperm_args));
194 case I386_SET_IOPERM:
195 error = i386_set_ioperm(td, &kargs.iargs);
198 error = vm86_sysarch(td, uap->parms);
200 case I386_GET_FSBASE:
201 sdp = &td->td_pcb->pcb_fsd;
202 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
203 error = copyout(&base, uap->parms, sizeof(base));
205 case I386_SET_FSBASE:
206 error = copyin(uap->parms, &base, sizeof(base));
209 * Construct a descriptor and store it in the pcb for
210 * the next context switch. Also store it in the gdt
211 * so that the load of tf_fs into %fs will activate it
212 * at return to userland.
214 fill_based_sd(&sd, base);
216 td->td_pcb->pcb_fsd = sd;
217 PCPU_GET(fsgs_gdt)[0] = sd;
219 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
222 case I386_GET_GSBASE:
223 sdp = &td->td_pcb->pcb_gsd;
224 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
225 error = copyout(&base, uap->parms, sizeof(base));
227 case I386_SET_GSBASE:
228 error = copyin(uap->parms, &base, sizeof(base));
231 * Construct a descriptor and store it in the pcb for
232 * the next context switch. Also store it in the gdt
233 * because we have to do a load_gs() right now.
235 fill_based_sd(&sd, base);
237 td->td_pcb->pcb_gsd = sd;
238 PCPU_GET(fsgs_gdt)[1] = sd;
240 load_gs(GSEL(GUGS_SEL, SEL_UPL));
243 case I386_GET_XFPUSTATE:
244 if (kargs.xfpu.len > cpu_max_ext_state_size -
245 sizeof(union savefpu))
248 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
249 kargs.xfpu.addr, kargs.xfpu.len);
259 i386_extend_pcb(struct thread *td)
264 struct soft_segment_descriptor ssd = {
265 0, /* segment base address (overwritten) */
266 ctob(IOPAGES + 1) - 1, /* length */
267 SDT_SYS386TSS, /* segment type */
268 0, /* priority level */
269 1, /* descriptor present */
271 0, /* default 32 size */
275 ext = (struct pcb_ext *)kmem_malloc(kernel_arena, ctob(IOPAGES+1),
277 /* -16 is so we can convert a trapframe into vm86trapframe inplace */
278 ext->ext_tss.tss_esp0 = td->td_kstack + ctob(td->td_kstack_pages) -
279 sizeof(struct pcb) - 16;
280 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
282 * The last byte of the i/o map must be followed by an 0xff byte.
283 * We arbitrarily allocate 16 bytes here, to keep the starting
284 * address on a doubleword boundary.
286 offset = PAGE_SIZE - 16;
287 ext->ext_tss.tss_ioopt =
288 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
289 ext->ext_iomap = (caddr_t)ext + offset;
290 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
292 addr = (u_long *)ext->ext_vm86.vm86_intmap;
293 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
296 ssd.ssd_base = (unsigned)&ext->ext_tss;
297 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
298 ssdtosd(&ssd, &ext->ext_tssd);
300 KASSERT(td == curthread, ("giving TSS to !curthread"));
301 KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
303 /* Switch to the new TSS. */
305 td->td_pcb->pcb_ext = ext;
306 PCPU_SET(private_tss, 1);
307 *PCPU_GET(tss_gdt) = ext->ext_tssd;
308 ltr(GSEL(GPROC0_SEL, SEL_KPL));
315 i386_set_ioperm(td, uap)
317 struct i386_ioperm_args *uap;
322 if ((error = priv_check(td, PRIV_IO)) != 0)
324 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
328 * While this is restricted to root, we should probably figure out
329 * whether any other driver is using this i/o address, as so not to
330 * cause confusion. This probably requires a global 'usage registry'.
333 if (td->td_pcb->pcb_ext == 0)
334 if ((error = i386_extend_pcb(td)) != 0)
336 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
338 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
341 for (i = uap->start; i < uap->start + uap->length; i++) {
343 iomap[i >> 3] &= ~(1 << (i & 7));
345 iomap[i >> 3] |= (1 << (i & 7));
351 i386_get_ioperm(td, uap)
353 struct i386_ioperm_args *uap;
358 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
361 if (td->td_pcb->pcb_ext == 0) {
366 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
369 state = (iomap[i >> 3] >> (i & 7)) & 1;
370 uap->enable = !state;
373 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
374 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
384 * Update the GDT entry pointing to the LDT to point to the LDT of the
385 * current process. Manage dt_lock holding/unholding autonomously.
388 set_user_ldt(struct mdproc *mdp)
390 struct proc_ldt *pldt;
394 if (!mtx_owned(&dt_lock)) {
395 mtx_lock_spin(&dt_lock);
401 gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
403 gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
405 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
406 PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
408 mtx_unlock_spin(&dt_lock);
413 set_user_ldt_rv(struct vmspace *vmsp)
418 if (vmsp != td->td_proc->p_vmspace)
421 set_user_ldt(&td->td_proc->p_md);
426 * dt_lock must be held. Returns with dt_lock held.
429 user_ldt_alloc(struct mdproc *mdp, int len)
431 struct proc_ldt *pldt, *new_ldt;
433 mtx_assert(&dt_lock, MA_OWNED);
434 mtx_unlock_spin(&dt_lock);
435 new_ldt = malloc(sizeof(struct proc_ldt),
436 M_SUBPROC, M_WAITOK);
438 new_ldt->ldt_len = len = NEW_MAX_LD(len);
439 new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
440 len * sizeof(union descriptor), M_WAITOK);
441 new_ldt->ldt_refcnt = 1;
442 new_ldt->ldt_active = 0;
444 mtx_lock_spin(&dt_lock);
445 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
446 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
447 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
449 if ((pldt = mdp->md_ldt) != NULL) {
450 if (len > pldt->ldt_len)
452 bcopy(pldt->ldt_base, new_ldt->ldt_base,
453 len * sizeof(union descriptor));
455 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
461 * Must be called with dt_lock held. Returns with dt_lock unheld.
464 user_ldt_free(struct thread *td)
466 struct mdproc *mdp = &td->td_proc->p_md;
467 struct proc_ldt *pldt;
469 mtx_assert(&dt_lock, MA_OWNED);
470 if ((pldt = mdp->md_ldt) == NULL) {
471 mtx_unlock_spin(&dt_lock);
475 if (td == curthread) {
477 PCPU_SET(currentldt, _default_ldt);
481 user_ldt_deref(pldt);
485 user_ldt_deref(struct proc_ldt *pldt)
488 mtx_assert(&dt_lock, MA_OWNED);
489 if (--pldt->ldt_refcnt == 0) {
490 mtx_unlock_spin(&dt_lock);
491 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
492 pldt->ldt_len * sizeof(union descriptor));
493 free(pldt, M_SUBPROC);
495 mtx_unlock_spin(&dt_lock);
499 * Note for the authors of compat layers (linux, etc): copyout() in
500 * the function below is not a problem since it presents data in
501 * arch-specific format (i.e. i386-specific in this case), not in
502 * the OS-specific one.
505 i386_get_ldt(td, uap)
507 struct i386_ldt_args *uap;
510 struct proc_ldt *pldt;
512 union descriptor *lp;
515 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
516 uap->start, uap->num, (void *)uap->descs);
519 mtx_lock_spin(&dt_lock);
520 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
521 nldt = pldt->ldt_len;
522 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
523 mtx_unlock_spin(&dt_lock);
524 num = min(uap->num, nldt);
526 mtx_unlock_spin(&dt_lock);
527 nldt = sizeof(ldt)/sizeof(ldt[0]);
528 num = min(uap->num, nldt);
529 lp = &ldt[uap->start];
532 if ((uap->start > (unsigned int)nldt) ||
533 ((unsigned int)num > (unsigned int)nldt) ||
534 ((unsigned int)(uap->start + num) > (unsigned int)nldt))
537 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
539 td->td_retval[0] = num;
545 i386_set_ldt(td, uap, descs)
547 struct i386_ldt_args *uap;
548 union descriptor *descs;
552 struct mdproc *mdp = &td->td_proc->p_md;
553 struct proc_ldt *pldt;
554 union descriptor *dp;
557 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
558 uap->start, uap->num, (void *)uap->descs);
562 /* Free descriptors */
563 if (uap->start == 0 && uap->num == 0) {
565 * Treat this as a special case, so userland needn't
566 * know magic number NLDT.
569 uap->num = MAX_LD - NLDT;
573 mtx_lock_spin(&dt_lock);
574 if ((pldt = mdp->md_ldt) == NULL ||
575 uap->start >= pldt->ldt_len) {
576 mtx_unlock_spin(&dt_lock);
579 largest_ld = uap->start + uap->num;
580 if (largest_ld > pldt->ldt_len)
581 largest_ld = pldt->ldt_len;
582 i = largest_ld - uap->start;
583 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
584 sizeof(union descriptor) * i);
585 mtx_unlock_spin(&dt_lock);
589 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
590 /* verify range of descriptors to modify */
591 largest_ld = uap->start + uap->num;
592 if (uap->start >= MAX_LD || largest_ld > MAX_LD) {
597 /* Check descriptors for access violations */
598 for (i = 0; i < uap->num; i++) {
601 switch (dp->sd.sd_type) {
602 case SDT_SYSNULL: /* system null */
605 case SDT_SYS286TSS: /* system 286 TSS available */
606 case SDT_SYSLDT: /* system local descriptor table */
607 case SDT_SYS286BSY: /* system 286 TSS busy */
608 case SDT_SYSTASKGT: /* system task gate */
609 case SDT_SYS286IGT: /* system 286 interrupt gate */
610 case SDT_SYS286TGT: /* system 286 trap gate */
611 case SDT_SYSNULL2: /* undefined by Intel */
612 case SDT_SYS386TSS: /* system 386 TSS available */
613 case SDT_SYSNULL3: /* undefined by Intel */
614 case SDT_SYS386BSY: /* system 386 TSS busy */
615 case SDT_SYSNULL4: /* undefined by Intel */
616 case SDT_SYS386IGT: /* system 386 interrupt gate */
617 case SDT_SYS386TGT: /* system 386 trap gate */
618 case SDT_SYS286CGT: /* system 286 call gate */
619 case SDT_SYS386CGT: /* system 386 call gate */
620 /* I can't think of any reason to allow a user proc
621 * to create a segment of these types. They are
627 /* memory segment types */
628 case SDT_MEMEC: /* memory execute only conforming */
629 case SDT_MEMEAC: /* memory execute only accessed conforming */
630 case SDT_MEMERC: /* memory execute read conforming */
631 case SDT_MEMERAC: /* memory execute read accessed conforming */
632 /* Must be "present" if executable and conforming. */
633 if (dp->sd.sd_p == 0)
636 case SDT_MEMRO: /* memory read only */
637 case SDT_MEMROA: /* memory read only accessed */
638 case SDT_MEMRW: /* memory read write */
639 case SDT_MEMRWA: /* memory read write accessed */
640 case SDT_MEMROD: /* memory read only expand dwn limit */
641 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
642 case SDT_MEMRWD: /* memory read write expand dwn limit */
643 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
644 case SDT_MEME: /* memory execute only */
645 case SDT_MEMEA: /* memory execute only accessed */
646 case SDT_MEMER: /* memory execute read */
647 case SDT_MEMERA: /* memory execute read accessed */
654 /* Only user (ring-3) descriptors may be present. */
655 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
659 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
660 /* Allocate a free slot */
661 mtx_lock_spin(&dt_lock);
662 if ((pldt = mdp->md_ldt) == NULL) {
663 if ((error = i386_ldt_grow(td, NLDT + 1))) {
664 mtx_unlock_spin(&dt_lock);
671 * start scanning a bit up to leave room for NVidia and
672 * Wine, which still user the "Blat" method of allocation.
674 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
675 for (i = NLDT; i < pldt->ldt_len; ++i) {
676 if (dp->sd.sd_type == SDT_SYSNULL)
680 if (i >= pldt->ldt_len) {
681 if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
682 mtx_unlock_spin(&dt_lock);
688 error = i386_set_ldt_data(td, i, 1, descs);
689 mtx_unlock_spin(&dt_lock);
691 largest_ld = uap->start + uap->num;
692 mtx_lock_spin(&dt_lock);
693 if (!(error = i386_ldt_grow(td, largest_ld))) {
694 error = i386_set_ldt_data(td, uap->start, uap->num,
697 mtx_unlock_spin(&dt_lock);
700 td->td_retval[0] = uap->start;
705 i386_set_ldt_data(struct thread *td, int start, int num,
706 union descriptor *descs)
708 struct mdproc *mdp = &td->td_proc->p_md;
709 struct proc_ldt *pldt = mdp->md_ldt;
711 mtx_assert(&dt_lock, MA_OWNED);
715 &((union descriptor *)(pldt->ldt_base))[start],
716 num * sizeof(union descriptor));
721 i386_ldt_grow(struct thread *td, int len)
723 struct mdproc *mdp = &td->td_proc->p_md;
724 struct proc_ldt *new_ldt, *pldt;
725 caddr_t old_ldt_base = NULL_LDT_BASE;
728 mtx_assert(&dt_lock, MA_OWNED);
735 /* Allocate a user ldt. */
736 if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
737 new_ldt = user_ldt_alloc(mdp, len);
743 if (new_ldt->ldt_len <= pldt->ldt_len) {
745 * We just lost the race for allocation, so
746 * free the new object and return.
748 mtx_unlock_spin(&dt_lock);
749 kmem_free(kernel_arena,
750 (vm_offset_t)new_ldt->ldt_base,
751 new_ldt->ldt_len * sizeof(union descriptor));
752 free(new_ldt, M_SUBPROC);
753 mtx_lock_spin(&dt_lock);
758 * We have to substitute the current LDT entry for
759 * curproc with the new one since its size grew.
761 old_ldt_base = pldt->ldt_base;
762 old_ldt_len = pldt->ldt_len;
763 pldt->ldt_sd = new_ldt->ldt_sd;
764 pldt->ldt_base = new_ldt->ldt_base;
765 pldt->ldt_len = new_ldt->ldt_len;
767 mdp->md_ldt = pldt = new_ldt;
770 * Signal other cpus to reload ldt. We need to unlock dt_lock
771 * here because other CPU will contest on it since their
772 * curthreads won't hold the lock and will block when trying
775 mtx_unlock_spin(&dt_lock);
776 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
777 NULL, td->td_proc->p_vmspace);
779 set_user_ldt(&td->td_proc->p_md);
780 mtx_unlock_spin(&dt_lock);
782 if (old_ldt_base != NULL_LDT_BASE) {
783 kmem_free(kernel_arena, (vm_offset_t)old_ldt_base,
784 old_ldt_len * sizeof(union descriptor));
785 free(new_ldt, M_SUBPROC);
787 mtx_lock_spin(&dt_lock);