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 <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
48 #include <vm/vm_kern.h> /* for kernel_map */
49 #include <vm/vm_extern.h>
51 #include <machine/frame.h>
52 #include <machine/md_var.h>
53 #include <machine/pcb.h>
54 #include <machine/specialreg.h>
55 #include <machine/sysarch.h>
56 #include <machine/tss.h>
57 #include <machine/vmparam.h>
59 #include <security/audit/audit.h>
61 int max_ldt_segment = 1024;
62 #define LD_PER_PAGE 512
63 #define NULL_LDT_BASE ((caddr_t)NULL)
67 static void set_user_ldt_rv(struct vmspace *vmsp);
70 static void user_ldt_derefl(struct proc_ldt *pldt);
72 #ifndef _SYS_SYSPROTO_H_
80 sysarch_ldt(struct thread *td, struct sysarch_args *uap, int uap_space)
82 struct i386_ldt_args *largs, la;
83 struct user_segment_descriptor *lp;
87 * XXXKIB check that the BSM generation code knows to encode
90 AUDIT_ARG_CMD(uap->op);
91 if (uap_space == UIO_USERSPACE) {
92 error = copyin(uap->parms, &la, sizeof(struct i386_ldt_args));
97 largs = (struct i386_ldt_args *)uap->parms;
98 if (largs->num > max_ldt_segment || largs->num <= 0)
103 error = amd64_get_ldt(td, largs);
106 td->td_pcb->pcb_full_iret = 1;
107 if (largs->descs != NULL) {
108 lp = (struct user_segment_descriptor *)
109 kmem_alloc(kernel_map, largs->num *
110 sizeof(struct user_segment_descriptor));
115 error = copyin(largs->descs, lp, largs->num *
116 sizeof(struct user_segment_descriptor));
118 error = amd64_set_ldt(td, largs, lp);
119 kmem_free(kernel_map, (vm_offset_t)lp, largs->num *
120 sizeof(struct user_segment_descriptor));
122 error = amd64_set_ldt(td, largs, NULL);
130 update_gdt_gsbase(struct thread *td, uint32_t base)
132 struct user_segment_descriptor *sd;
136 td->td_pcb->pcb_full_iret = 1;
138 sd = PCPU_GET(gs32p);
139 sd->sd_lobase = base & 0xffffff;
140 sd->sd_hibase = (base >> 24) & 0xff;
145 update_gdt_fsbase(struct thread *td, uint32_t base)
147 struct user_segment_descriptor *sd;
151 td->td_pcb->pcb_full_iret = 1;
153 sd = PCPU_GET(fs32p);
154 sd->sd_lobase = base & 0xffffff;
155 sd->sd_hibase = (base >> 24) & 0xff;
162 register struct sysarch_args *uap;
165 struct pcb *pcb = curthread->td_pcb;
168 struct i386_ioperm_args iargs;
170 if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
171 return (sysarch_ldt(td, uap, UIO_USERSPACE));
173 * XXXKIB check that the BSM generation code knows to encode
176 AUDIT_ARG_CMD(uap->op);
178 case I386_GET_IOPERM:
179 case I386_SET_IOPERM:
180 if ((error = copyin(uap->parms, &iargs,
181 sizeof(struct i386_ioperm_args))) != 0)
189 case I386_GET_IOPERM:
190 error = amd64_get_ioperm(td, &iargs);
192 error = copyout(&iargs, uap->parms,
193 sizeof(struct i386_ioperm_args));
195 case I386_SET_IOPERM:
196 error = amd64_set_ioperm(td, &iargs);
198 case I386_GET_FSBASE:
199 i386base = pcb->pcb_fsbase;
200 error = copyout(&i386base, uap->parms, sizeof(i386base));
202 case I386_SET_FSBASE:
203 error = copyin(uap->parms, &i386base, sizeof(i386base));
205 pcb->pcb_fsbase = i386base;
206 td->td_frame->tf_fs = _ufssel;
207 pcb->pcb_full_iret = 1;
208 update_gdt_fsbase(td, i386base);
211 case I386_GET_GSBASE:
212 i386base = pcb->pcb_gsbase;
213 error = copyout(&i386base, uap->parms, sizeof(i386base));
215 case I386_SET_GSBASE:
216 error = copyin(uap->parms, &i386base, sizeof(i386base));
218 pcb->pcb_gsbase = i386base;
219 pcb->pcb_full_iret = 1;
220 td->td_frame->tf_gs = _ugssel;
221 update_gdt_gsbase(td, i386base);
224 case AMD64_GET_FSBASE:
225 error = copyout(&pcb->pcb_fsbase, uap->parms, sizeof(pcb->pcb_fsbase));
228 case AMD64_SET_FSBASE:
229 error = copyin(uap->parms, &a64base, sizeof(a64base));
231 if (a64base < VM_MAXUSER_ADDRESS) {
232 pcb->pcb_fsbase = a64base;
233 pcb->pcb_full_iret = 1;
234 td->td_frame->tf_fs = _ufssel;
240 case AMD64_GET_GSBASE:
241 error = copyout(&pcb->pcb_gsbase, uap->parms, sizeof(pcb->pcb_gsbase));
244 case AMD64_SET_GSBASE:
245 error = copyin(uap->parms, &a64base, sizeof(a64base));
247 if (a64base < VM_MAXUSER_ADDRESS) {
248 pcb->pcb_gsbase = a64base;
249 pcb->pcb_full_iret = 1;
250 td->td_frame->tf_gs = _ugssel;
264 amd64_set_ioperm(td, uap)
266 struct i386_ioperm_args *uap;
270 struct amd64tss *tssp;
271 struct system_segment_descriptor *tss_sd;
275 if ((error = priv_check(td, PRIV_IO)) != 0)
277 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
279 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
284 * While this is restricted to root, we should probably figure out
285 * whether any other driver is using this i/o address, as so not to
286 * cause confusion. This probably requires a global 'usage registry'.
289 if (pcb->pcb_tssp == NULL) {
290 tssp = (struct amd64tss *)kmem_alloc(kernel_map,
294 iomap = (char *)&tssp[1];
295 addr = (u_long *)iomap;
296 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
299 /* Takes care of tss_rsp0. */
300 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
301 sizeof(struct amd64tss));
302 tssp->tss_iobase = sizeof(*tssp);
303 pcb->pcb_tssp = tssp;
304 tss_sd = PCPU_GET(tss);
305 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
306 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
307 tss_sd->sd_type = SDT_SYSTSS;
308 ltr(GSEL(GPROC0_SEL, SEL_KPL));
309 PCPU_SET(tssp, tssp);
312 iomap = (char *)&pcb->pcb_tssp[1];
313 for (i = uap->start; i < uap->start + uap->length; i++) {
315 iomap[i >> 3] &= ~(1 << (i & 7));
317 iomap[i >> 3] |= (1 << (i & 7));
323 amd64_get_ioperm(td, uap)
325 struct i386_ioperm_args *uap;
330 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
332 if (td->td_pcb->pcb_tssp == NULL) {
337 iomap = (char *)&td->td_pcb->pcb_tssp[1];
340 state = (iomap[i >> 3] >> (i & 7)) & 1;
341 uap->enable = !state;
344 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
345 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
355 * Update the GDT entry pointing to the LDT to point to the LDT of the
359 set_user_ldt(struct mdproc *mdp)
363 *PCPU_GET(ldt) = mdp->md_ldt_sd;
364 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
371 set_user_ldt_rv(struct vmspace *vmsp)
376 if (vmsp != td->td_proc->p_vmspace)
379 set_user_ldt(&td->td_proc->p_md);
385 user_ldt_alloc(struct proc *p, int force)
387 struct proc_ldt *pldt, *new_ldt;
389 struct soft_segment_descriptor sldt;
391 mtx_assert(&dt_lock, MA_OWNED);
393 if (!force && mdp->md_ldt != NULL)
394 return (mdp->md_ldt);
395 mtx_unlock(&dt_lock);
396 new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
397 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
398 max_ldt_segment * sizeof(struct user_segment_descriptor));
399 if (new_ldt->ldt_base == NULL) {
400 FREE(new_ldt, M_SUBPROC);
404 new_ldt->ldt_refcnt = 1;
405 sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
406 sldt.ssd_limit = max_ldt_segment *
407 sizeof(struct user_segment_descriptor) - 1;
408 sldt.ssd_type = SDT_SYSLDT;
409 sldt.ssd_dpl = SEL_KPL;
416 if (pldt != NULL && !force) {
417 kmem_free(kernel_map, (vm_offset_t)new_ldt->ldt_base,
418 max_ldt_segment * sizeof(struct user_segment_descriptor));
419 free(new_ldt, M_SUBPROC);
423 mdp->md_ldt = new_ldt;
425 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
426 sizeof(struct user_segment_descriptor));
427 user_ldt_derefl(pldt);
429 ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
433 return (mdp->md_ldt);
437 user_ldt_free(struct thread *td)
439 struct proc *p = td->td_proc;
440 struct mdproc *mdp = &p->p_md;
441 struct proc_ldt *pldt;
443 mtx_assert(&dt_lock, MA_OWNED);
444 if ((pldt = mdp->md_ldt) == NULL) {
445 mtx_unlock(&dt_lock);
450 bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
452 lldt(GSEL(GNULL_SEL, SEL_KPL));
453 user_ldt_deref(pldt);
457 user_ldt_derefl(struct proc_ldt *pldt)
460 if (--pldt->ldt_refcnt == 0) {
461 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
462 max_ldt_segment * sizeof(struct user_segment_descriptor));
463 free(pldt, M_SUBPROC);
468 user_ldt_deref(struct proc_ldt *pldt)
471 mtx_assert(&dt_lock, MA_OWNED);
472 user_ldt_derefl(pldt);
473 mtx_unlock(&dt_lock);
477 * Note for the authors of compat layers (linux, etc): copyout() in
478 * the function below is not a problem since it presents data in
479 * arch-specific format (i.e. i386-specific in this case), not in
480 * the OS-specific one.
483 amd64_get_ldt(td, uap)
485 struct i386_ldt_args *uap;
488 struct proc_ldt *pldt;
490 struct user_segment_descriptor *lp;
493 printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
494 uap->start, uap->num, (void *)uap->descs);
497 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
498 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
500 num = min(uap->num, max_ldt_segment);
504 if ((uap->start > (unsigned int)max_ldt_segment) ||
505 ((unsigned int)num > (unsigned int)max_ldt_segment) ||
506 ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
509 error = copyout(lp, uap->descs, num *
510 sizeof(struct user_segment_descriptor));
512 td->td_retval[0] = num;
518 amd64_set_ldt(td, uap, descs)
520 struct i386_ldt_args *uap;
521 struct user_segment_descriptor *descs;
525 struct mdproc *mdp = &td->td_proc->p_md;
526 struct proc_ldt *pldt;
527 struct user_segment_descriptor *dp;
531 printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
532 uap->start, uap->num, (void *)uap->descs);
535 td->td_pcb->pcb_full_iret = 1;
538 /* Free descriptors */
539 if (uap->start == 0 && uap->num == 0)
540 uap->num = max_ldt_segment;
543 if ((pldt = mdp->md_ldt) == NULL ||
544 uap->start >= max_ldt_segment)
546 largest_ld = uap->start + uap->num;
547 if (largest_ld > max_ldt_segment)
548 largest_ld = max_ldt_segment;
549 i = largest_ld - uap->start;
551 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
552 [uap->start], sizeof(struct user_segment_descriptor) * i);
553 mtx_unlock(&dt_lock);
557 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
558 /* verify range of descriptors to modify */
559 largest_ld = uap->start + uap->num;
560 if (uap->start >= max_ldt_segment ||
561 uap->num < 0 || largest_ld > max_ldt_segment)
565 /* Check descriptors for access violations */
566 for (i = 0; i < uap->num; i++) {
569 switch (dp->sd_type) {
570 case SDT_SYSNULL: /* system null */
588 /* I can't think of any reason to allow a user proc
589 * to create a segment of these types. They are
595 /* memory segment types */
596 case SDT_MEMEC: /* memory execute only conforming */
597 case SDT_MEMEAC: /* memory execute only accessed conforming */
598 case SDT_MEMERC: /* memory execute read conforming */
599 case SDT_MEMERAC: /* memory execute read accessed conforming */
600 /* Must be "present" if executable and conforming. */
604 case SDT_MEMRO: /* memory read only */
605 case SDT_MEMROA: /* memory read only accessed */
606 case SDT_MEMRW: /* memory read write */
607 case SDT_MEMRWA: /* memory read write accessed */
608 case SDT_MEMROD: /* memory read only expand dwn limit */
609 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
610 case SDT_MEMRWD: /* memory read write expand dwn limit */
611 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
612 case SDT_MEME: /* memory execute only */
613 case SDT_MEMEA: /* memory execute only accessed */
614 case SDT_MEMER: /* memory execute read */
615 case SDT_MEMERA: /* memory execute read accessed */
622 /* Only user (ring-3) descriptors may be present. */
623 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
627 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
628 /* Allocate a free slot */
630 pldt = user_ldt_alloc(p, 0);
632 mtx_unlock(&dt_lock);
637 * start scanning a bit up to leave room for NVidia and
638 * Wine, which still user the "Blat" method of allocation.
641 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
642 for (; i < max_ldt_segment; ++i, ++dp) {
643 if (dp->sd_type == SDT_SYSNULL)
646 if (i >= max_ldt_segment) {
647 mtx_unlock(&dt_lock);
651 error = amd64_set_ldt_data(td, i, 1, descs);
652 mtx_unlock(&dt_lock);
654 largest_ld = uap->start + uap->num;
655 if (largest_ld > max_ldt_segment)
658 if (user_ldt_alloc(p, 0) != NULL) {
659 error = amd64_set_ldt_data(td, uap->start, uap->num,
662 mtx_unlock(&dt_lock);
665 td->td_retval[0] = uap->start;
670 amd64_set_ldt_data(struct thread *td, int start, int num,
671 struct user_segment_descriptor *descs)
673 struct mdproc *mdp = &td->td_proc->p_md;
674 struct proc_ldt *pldt = mdp->md_ldt;
676 mtx_assert(&dt_lock, MA_OWNED);
680 &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
681 num * sizeof(struct user_segment_descriptor));