2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1990 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_capsicum.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/capability.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
47 #include <sys/sysproto.h>
52 #include <vm/vm_kern.h> /* for kernel_map */
53 #include <vm/vm_extern.h>
55 #include <machine/frame.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
58 #include <machine/specialreg.h>
59 #include <machine/sysarch.h>
60 #include <machine/tss.h>
61 #include <machine/vmparam.h>
63 #include <security/audit/audit.h>
67 int max_ldt_segment = 1024;
68 SYSCTL_INT(_machdep, OID_AUTO, max_ldt_segment, CTLFLAG_RDTUN,
70 "Maximum number of allowed LDT segments in the single address space");
73 max_ldt_segment_init(void *arg __unused)
76 TUNABLE_INT_FETCH("machdep.max_ldt_segment", &max_ldt_segment);
77 if (max_ldt_segment <= 0)
79 if (max_ldt_segment > MAX_LD)
80 max_ldt_segment = MAX_LD;
82 SYSINIT(maxldt, SI_SUB_VM_CONF, SI_ORDER_ANY, max_ldt_segment_init, NULL);
86 static void set_user_ldt_rv(struct vmspace *vmsp);
89 static void user_ldt_derefl(struct proc_ldt *pldt);
91 #ifndef _SYS_SYSPROTO_H_
99 sysarch_ldt(struct thread *td, struct sysarch_args *uap, int uap_space)
101 struct i386_ldt_args *largs, la;
102 struct user_segment_descriptor *lp;
106 * XXXKIB check that the BSM generation code knows to encode
109 AUDIT_ARG_CMD(uap->op);
110 if (uap_space == UIO_USERSPACE) {
111 error = copyin(uap->parms, &la, sizeof(struct i386_ldt_args));
116 largs = (struct i386_ldt_args *)uap->parms;
120 error = amd64_get_ldt(td, largs);
123 if (largs->descs != NULL && largs->num > max_ldt_segment)
125 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
126 if (largs->descs != NULL) {
127 lp = malloc(largs->num * sizeof(struct
128 user_segment_descriptor), M_TEMP, M_WAITOK);
129 error = copyin(largs->descs, lp, largs->num *
130 sizeof(struct user_segment_descriptor));
132 error = amd64_set_ldt(td, largs, lp);
135 error = amd64_set_ldt(td, largs, NULL);
143 update_gdt_gsbase(struct thread *td, uint32_t base)
145 struct user_segment_descriptor *sd;
149 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
151 sd = PCPU_GET(gs32p);
152 sd->sd_lobase = base & 0xffffff;
153 sd->sd_hibase = (base >> 24) & 0xff;
158 update_gdt_fsbase(struct thread *td, uint32_t base)
160 struct user_segment_descriptor *sd;
164 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
166 sd = PCPU_GET(fs32p);
167 sd->sd_lobase = base & 0xffffff;
168 sd->sd_hibase = (base >> 24) & 0xff;
175 register struct sysarch_args *uap;
178 struct pcb *pcb = curthread->td_pcb;
181 struct i386_ioperm_args iargs;
182 struct i386_get_xfpustate i386xfpu;
183 struct amd64_get_xfpustate a64xfpu;
185 #ifdef CAPABILITY_MODE
187 * When adding new operations, add a new case statement here to
188 * explicitly indicate whether or not the operation is safe to
189 * perform in capability mode.
191 if (IN_CAPABILITY_MODE(td)) {
195 case I386_GET_IOPERM:
196 case I386_GET_FSBASE:
197 case I386_SET_FSBASE:
198 case I386_GET_GSBASE:
199 case I386_SET_GSBASE:
200 case I386_GET_XFPUSTATE:
201 case AMD64_GET_FSBASE:
202 case AMD64_SET_FSBASE:
203 case AMD64_GET_GSBASE:
204 case AMD64_SET_GSBASE:
205 case AMD64_GET_XFPUSTATE:
208 case I386_SET_IOPERM:
211 if (KTRPOINT(td, KTR_CAPFAIL))
212 ktrcapfail(CAPFAIL_SYSCALL, 0, 0);
219 if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
220 return (sysarch_ldt(td, uap, UIO_USERSPACE));
222 * XXXKIB check that the BSM generation code knows to encode
225 AUDIT_ARG_CMD(uap->op);
227 case I386_GET_IOPERM:
228 case I386_SET_IOPERM:
229 if ((error = copyin(uap->parms, &iargs,
230 sizeof(struct i386_ioperm_args))) != 0)
233 case I386_GET_XFPUSTATE:
234 if ((error = copyin(uap->parms, &i386xfpu,
235 sizeof(struct i386_get_xfpustate))) != 0)
237 a64xfpu.addr = (void *)(uintptr_t)i386xfpu.addr;
238 a64xfpu.len = i386xfpu.len;
240 case AMD64_GET_XFPUSTATE:
241 if ((error = copyin(uap->parms, &a64xfpu,
242 sizeof(struct amd64_get_xfpustate))) != 0)
250 case I386_GET_IOPERM:
251 error = amd64_get_ioperm(td, &iargs);
253 error = copyout(&iargs, uap->parms,
254 sizeof(struct i386_ioperm_args));
256 case I386_SET_IOPERM:
257 error = amd64_set_ioperm(td, &iargs);
259 case I386_GET_FSBASE:
260 i386base = pcb->pcb_fsbase;
261 error = copyout(&i386base, uap->parms, sizeof(i386base));
263 case I386_SET_FSBASE:
264 error = copyin(uap->parms, &i386base, sizeof(i386base));
266 pcb->pcb_fsbase = i386base;
267 td->td_frame->tf_fs = _ufssel;
268 update_gdt_fsbase(td, i386base);
271 case I386_GET_GSBASE:
272 i386base = pcb->pcb_gsbase;
273 error = copyout(&i386base, uap->parms, sizeof(i386base));
275 case I386_SET_GSBASE:
276 error = copyin(uap->parms, &i386base, sizeof(i386base));
278 pcb->pcb_gsbase = i386base;
279 td->td_frame->tf_gs = _ugssel;
280 update_gdt_gsbase(td, i386base);
283 case AMD64_GET_FSBASE:
284 error = copyout(&pcb->pcb_fsbase, uap->parms, sizeof(pcb->pcb_fsbase));
287 case AMD64_SET_FSBASE:
288 error = copyin(uap->parms, &a64base, sizeof(a64base));
290 if (a64base < VM_MAXUSER_ADDRESS) {
291 pcb->pcb_fsbase = a64base;
292 set_pcb_flags(pcb, PCB_FULL_IRET);
293 td->td_frame->tf_fs = _ufssel;
299 case AMD64_GET_GSBASE:
300 error = copyout(&pcb->pcb_gsbase, uap->parms, sizeof(pcb->pcb_gsbase));
303 case AMD64_SET_GSBASE:
304 error = copyin(uap->parms, &a64base, sizeof(a64base));
306 if (a64base < VM_MAXUSER_ADDRESS) {
307 pcb->pcb_gsbase = a64base;
308 set_pcb_flags(pcb, PCB_FULL_IRET);
309 td->td_frame->tf_gs = _ugssel;
315 case I386_GET_XFPUSTATE:
316 case AMD64_GET_XFPUSTATE:
317 if (a64xfpu.len > cpu_max_ext_state_size -
318 sizeof(struct savefpu))
321 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
322 a64xfpu.addr, a64xfpu.len);
333 amd64_set_ioperm(td, uap)
335 struct i386_ioperm_args *uap;
339 struct amd64tss *tssp;
340 struct system_segment_descriptor *tss_sd;
344 if ((error = priv_check(td, PRIV_IO)) != 0)
346 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
348 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
353 * While this is restricted to root, we should probably figure out
354 * whether any other driver is using this i/o address, as so not to
355 * cause confusion. This probably requires a global 'usage registry'.
358 if (pcb->pcb_tssp == NULL) {
359 tssp = (struct amd64tss *)kmem_alloc(kernel_map,
363 iomap = (char *)&tssp[1];
364 addr = (u_long *)iomap;
365 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
368 /* Takes care of tss_rsp0. */
369 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
370 sizeof(struct amd64tss));
371 tssp->tss_iobase = sizeof(*tssp);
372 pcb->pcb_tssp = tssp;
373 tss_sd = PCPU_GET(tss);
374 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
375 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
376 tss_sd->sd_type = SDT_SYSTSS;
377 ltr(GSEL(GPROC0_SEL, SEL_KPL));
378 PCPU_SET(tssp, tssp);
381 iomap = (char *)&pcb->pcb_tssp[1];
382 for (i = uap->start; i < uap->start + uap->length; i++) {
384 iomap[i >> 3] &= ~(1 << (i & 7));
386 iomap[i >> 3] |= (1 << (i & 7));
392 amd64_get_ioperm(td, uap)
394 struct i386_ioperm_args *uap;
399 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
401 if (td->td_pcb->pcb_tssp == NULL) {
406 iomap = (char *)&td->td_pcb->pcb_tssp[1];
409 state = (iomap[i >> 3] >> (i & 7)) & 1;
410 uap->enable = !state;
413 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
414 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
424 * Update the GDT entry pointing to the LDT to point to the LDT of the
428 set_user_ldt(struct mdproc *mdp)
432 *PCPU_GET(ldt) = mdp->md_ldt_sd;
433 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
440 set_user_ldt_rv(struct vmspace *vmsp)
445 if (vmsp != td->td_proc->p_vmspace)
448 set_user_ldt(&td->td_proc->p_md);
454 user_ldt_alloc(struct proc *p, int force)
456 struct proc_ldt *pldt, *new_ldt;
458 struct soft_segment_descriptor sldt;
460 mtx_assert(&dt_lock, MA_OWNED);
462 if (!force && mdp->md_ldt != NULL)
463 return (mdp->md_ldt);
464 mtx_unlock(&dt_lock);
465 new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
466 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
467 max_ldt_segment * sizeof(struct user_segment_descriptor));
468 if (new_ldt->ldt_base == NULL) {
469 FREE(new_ldt, M_SUBPROC);
473 new_ldt->ldt_refcnt = 1;
474 sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
475 sldt.ssd_limit = max_ldt_segment *
476 sizeof(struct user_segment_descriptor) - 1;
477 sldt.ssd_type = SDT_SYSLDT;
478 sldt.ssd_dpl = SEL_KPL;
485 if (pldt != NULL && !force) {
486 kmem_free(kernel_map, (vm_offset_t)new_ldt->ldt_base,
487 max_ldt_segment * sizeof(struct user_segment_descriptor));
488 free(new_ldt, M_SUBPROC);
493 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
494 sizeof(struct user_segment_descriptor));
495 user_ldt_derefl(pldt);
497 ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
498 atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
503 return (mdp->md_ldt);
507 user_ldt_free(struct thread *td)
509 struct proc *p = td->td_proc;
510 struct mdproc *mdp = &p->p_md;
511 struct proc_ldt *pldt;
513 mtx_assert(&dt_lock, MA_OWNED);
514 if ((pldt = mdp->md_ldt) == NULL) {
515 mtx_unlock(&dt_lock);
520 bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
522 lldt(GSEL(GNULL_SEL, SEL_KPL));
523 user_ldt_deref(pldt);
527 user_ldt_derefl(struct proc_ldt *pldt)
530 if (--pldt->ldt_refcnt == 0) {
531 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
532 max_ldt_segment * sizeof(struct user_segment_descriptor));
533 free(pldt, M_SUBPROC);
538 user_ldt_deref(struct proc_ldt *pldt)
541 mtx_assert(&dt_lock, MA_OWNED);
542 user_ldt_derefl(pldt);
543 mtx_unlock(&dt_lock);
547 * Note for the authors of compat layers (linux, etc): copyout() in
548 * the function below is not a problem since it presents data in
549 * arch-specific format (i.e. i386-specific in this case), not in
550 * the OS-specific one.
553 amd64_get_ldt(td, uap)
555 struct i386_ldt_args *uap;
558 struct proc_ldt *pldt;
560 struct user_segment_descriptor *lp;
563 printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
564 uap->start, uap->num, (void *)uap->descs);
567 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
568 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
570 num = min(uap->num, max_ldt_segment);
574 if ((uap->start > (unsigned int)max_ldt_segment) ||
575 ((unsigned int)num > (unsigned int)max_ldt_segment) ||
576 ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
579 error = copyout(lp, uap->descs, num *
580 sizeof(struct user_segment_descriptor));
582 td->td_retval[0] = num;
588 amd64_set_ldt(td, uap, descs)
590 struct i386_ldt_args *uap;
591 struct user_segment_descriptor *descs;
595 struct mdproc *mdp = &td->td_proc->p_md;
596 struct proc_ldt *pldt;
597 struct user_segment_descriptor *dp;
601 printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
602 uap->start, uap->num, (void *)uap->descs);
605 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
608 /* Free descriptors */
609 if (uap->start == 0 && uap->num == 0)
610 uap->num = max_ldt_segment;
613 if ((pldt = mdp->md_ldt) == NULL ||
614 uap->start >= max_ldt_segment)
616 largest_ld = uap->start + uap->num;
617 if (largest_ld > max_ldt_segment)
618 largest_ld = max_ldt_segment;
619 i = largest_ld - uap->start;
621 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
622 [uap->start], sizeof(struct user_segment_descriptor) * i);
623 mtx_unlock(&dt_lock);
627 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
628 /* verify range of descriptors to modify */
629 largest_ld = uap->start + uap->num;
630 if (uap->start >= max_ldt_segment ||
631 largest_ld > max_ldt_segment)
635 /* Check descriptors for access violations */
636 for (i = 0; i < uap->num; i++) {
639 switch (dp->sd_type) {
640 case SDT_SYSNULL: /* system null */
658 /* I can't think of any reason to allow a user proc
659 * to create a segment of these types. They are
665 /* memory segment types */
666 case SDT_MEMEC: /* memory execute only conforming */
667 case SDT_MEMEAC: /* memory execute only accessed conforming */
668 case SDT_MEMERC: /* memory execute read conforming */
669 case SDT_MEMERAC: /* memory execute read accessed conforming */
670 /* Must be "present" if executable and conforming. */
674 case SDT_MEMRO: /* memory read only */
675 case SDT_MEMROA: /* memory read only accessed */
676 case SDT_MEMRW: /* memory read write */
677 case SDT_MEMRWA: /* memory read write accessed */
678 case SDT_MEMROD: /* memory read only expand dwn limit */
679 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
680 case SDT_MEMRWD: /* memory read write expand dwn limit */
681 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
682 case SDT_MEME: /* memory execute only */
683 case SDT_MEMEA: /* memory execute only accessed */
684 case SDT_MEMER: /* memory execute read */
685 case SDT_MEMERA: /* memory execute read accessed */
692 /* Only user (ring-3) descriptors may be present. */
693 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
697 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
698 /* Allocate a free slot */
700 pldt = user_ldt_alloc(p, 0);
702 mtx_unlock(&dt_lock);
707 * start scanning a bit up to leave room for NVidia and
708 * Wine, which still user the "Blat" method of allocation.
711 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
712 for (; i < max_ldt_segment; ++i, ++dp) {
713 if (dp->sd_type == SDT_SYSNULL)
716 if (i >= max_ldt_segment) {
717 mtx_unlock(&dt_lock);
721 error = amd64_set_ldt_data(td, i, 1, descs);
722 mtx_unlock(&dt_lock);
724 largest_ld = uap->start + uap->num;
725 if (largest_ld > max_ldt_segment)
728 if (user_ldt_alloc(p, 0) != NULL) {
729 error = amd64_set_ldt_data(td, uap->start, uap->num,
732 mtx_unlock(&dt_lock);
735 td->td_retval[0] = uap->start;
740 amd64_set_ldt_data(struct thread *td, int start, int num,
741 struct user_segment_descriptor *descs)
743 struct mdproc *mdp = &td->td_proc->p_md;
744 struct proc_ldt *pldt = mdp->md_ldt;
746 mtx_assert(&dt_lock, MA_OWNED);
750 &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
751 num * sizeof(struct user_segment_descriptor));