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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
34 * $FreeBSD: src/sys/i386/i386/sys_machdep.c,v 1.47.2.3 2002/10/07 17:20:00 jhb Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/malloc.h>
42 #include <sys/thread.h>
45 #include <sys/memrange.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
55 #include <machine/cpu.h>
56 #include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
57 #include <machine/sysarch.h>
58 #include <machine/smp.h>
59 #include <machine/globaldata.h> /* mdcpu */
61 #include <vm/vm_kern.h> /* for kernel_map */
63 #include <sys/thread2.h>
64 #include <sys/mplock2.h>
67 #define LD_PER_PAGE 512
68 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
69 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
73 static int ki386_get_ldt(struct lwp *, char *, int *);
74 static int ki386_set_ldt(struct lwp *, char *, int *);
75 static int ki386_get_ioperm(struct lwp *, char *);
76 static int ki386_set_ioperm(struct lwp *, char *);
77 static int check_descs(union descriptor *, int);
78 int i386_extend_pcb(struct lwp *);
81 * sysarch_args(int op, char *params)
86 sys_sysarch(struct sysarch_args *uap)
88 struct lwp *lp = curthread->td_lwp;
95 error = ki386_get_ldt(lp, uap->parms, &uap->sysmsg_result);
98 error = ki386_set_ldt(lp, uap->parms, &uap->sysmsg_result);
100 case I386_GET_IOPERM:
101 error = ki386_get_ioperm(lp, uap->parms);
103 case I386_SET_IOPERM:
104 error = ki386_set_ioperm(lp, uap->parms);
107 error = vm86_sysarch(lp, uap->parms);
118 i386_extend_pcb(struct lwp *lp)
123 struct soft_segment_descriptor ssd = {
124 0, /* segment base address (overwritten) */
125 ctob(IOPAGES + 1) - 1, /* length */
126 SDT_SYS386TSS, /* segment type */
127 0, /* priority level */
128 1, /* descriptor present */
130 0, /* default 32 size */
134 ext = (struct pcb_ext *)kmem_alloc(&kernel_map, ctob(IOPAGES+1));
137 bzero(ext, sizeof(struct pcb_ext));
138 ext->ext_tss.tss_esp0 = (unsigned)((char *)lp->lwp_thread->td_pcb - 16);
139 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
141 * The last byte of the i/o map must be followed by an 0xff byte.
142 * We arbitrarily allocate 16 bytes here, to keep the starting
143 * address on a doubleword boundary.
145 offset = PAGE_SIZE - 16;
146 ext->ext_tss.tss_ioopt =
147 (offset - offsetof(struct pcb_ext, ext_tss)) << 16;
148 ext->ext_iomap = (caddr_t)ext + offset;
149 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
151 addr = (u_long *)ext->ext_vm86.vm86_intmap;
152 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
155 ssd.ssd_base = (unsigned)&ext->ext_tss;
156 ssd.ssd_limit -= offsetof(struct pcb_ext, ext_tss);
157 ssdtosd(&ssd, &ext->ext_tssd);
160 * Put the new TSS where the switch code can find it. Do
161 * a forced switch to ourself to activate it.
164 lp->lwp_thread->td_pcb->pcb_ext = ext;
165 lp->lwp_thread->td_switch(lp->lwp_thread);
172 ki386_set_ioperm(struct lwp *lp, char *args)
175 struct i386_ioperm_args ua;
178 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
181 if ((error = priv_check_cred(lp->lwp_thread->td_ucred, PRIV_ROOT, 0)) != 0)
187 * While this is restricted to root, we should probably figure out
188 * whether any other driver is using this i/o address, as so not to
189 * cause confusion. This probably requires a global 'usage registry'.
192 if (lp->lwp_thread->td_pcb->pcb_ext == 0)
193 if ((error = i386_extend_pcb(lp)) != 0)
195 iomap = (char *)lp->lwp_thread->td_pcb->pcb_ext->ext_iomap;
197 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
200 for (i = ua.start; i < ua.start + ua.length; i++) {
202 iomap[i >> 3] &= ~(1 << (i & 7));
204 iomap[i >> 3] |= (1 << (i & 7));
210 ki386_get_ioperm(struct lwp *lp, char *args)
213 struct i386_ioperm_args ua;
216 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
218 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
221 if (lp->lwp_thread->td_pcb->pcb_ext == 0) {
226 iomap = (char *)lp->lwp_thread->td_pcb->pcb_ext->ext_iomap;
229 state = (iomap[i >> 3] >> (i & 7)) & 1;
233 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
234 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
240 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
245 * Update the TLS entries for the process. Used by assembly, do not staticize.
247 * Must be called from a critical section (else an interrupt thread preemption
248 * may cause %gs to fault). Normally called from the low level swtch.s code.
255 struct thread *td = curthread;
257 int off = GTLS_START + mycpu->gd_cpuid * NGDT;
258 for (i = 0; i < NGTLS; ++i)
259 gdt[off + i].sd = td->td_tls.tls[i];
264 set_user_ldt_cpusync(void *arg)
270 * Update the GDT entry pointing to the LDT to point to the LDT of the
271 * current process. Used by assembly, do not staticize.
273 * Must be called from a critical section (else an interrupt thread preemption
274 * may cause %gs to fault). Normally called from the low level swtch.s code.
277 set_user_ldt(struct pcb *pcb)
279 struct pcb_ldt *pcb_ldt;
281 if (pcb != curthread->td_pcb)
284 pcb_ldt = pcb->pcb_ldt;
285 gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
286 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
287 mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
291 user_ldt_alloc(struct pcb *pcb, int len)
293 struct pcb_ldt *pcb_ldt, *new_ldt;
295 new_ldt = kmalloc(sizeof(struct pcb_ldt), M_SUBPROC, M_WAITOK);
297 new_ldt->ldt_len = len = NEW_MAX_LD(len);
298 new_ldt->ldt_base = (caddr_t)kmem_alloc(&kernel_map,
299 len * sizeof(union descriptor));
300 if (new_ldt->ldt_base == NULL) {
301 kfree(new_ldt, M_SUBPROC);
304 new_ldt->ldt_refcnt = 1;
305 new_ldt->ldt_active = 0;
307 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
308 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
309 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
311 if ((pcb_ldt = pcb->pcb_ldt)) {
312 if (len > pcb_ldt->ldt_len)
313 len = pcb_ldt->ldt_len;
314 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
315 len * sizeof(union descriptor));
317 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
323 user_ldt_free(struct pcb *pcb)
325 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
331 if (pcb == curthread->td_pcb) {
333 mdcpu->gd_currentldt = _default_ldt;
338 if (--pcb_ldt->ldt_refcnt == 0) {
339 kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
340 pcb_ldt->ldt_len * sizeof(union descriptor));
341 kfree(pcb_ldt, M_SUBPROC);
346 ki386_get_ldt(struct lwp *lwp, char *args, int *res)
349 struct pcb *pcb = lwp->lwp_thread->td_pcb;
350 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
351 unsigned int nldt, num;
352 union descriptor *lp;
353 struct i386_ldt_args ua, *uap = &ua;
355 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
359 kprintf("ki386_get_ldt: start=%d num=%d descs=%p\n",
360 uap->start, uap->num, (void *)uap->descs);
366 nldt = (unsigned int)pcb_ldt->ldt_len;
367 num = min(uap->num, nldt);
368 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
370 nldt = (unsigned int)(NELEM(ldt));
371 num = min(uap->num, nldt);
372 lp = &ldt[uap->start];
376 * note: uap->(args), num, and nldt are unsigned. nldt and num
377 * are limited in scope, but uap->start can be anything.
379 if (uap->start > nldt || uap->start + num > nldt) {
384 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
392 ki386_set_ldt(struct lwp *lp, char *args, int *res)
396 struct pcb *pcb = lp->lwp_thread->td_pcb;
397 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
398 union descriptor *descs;
400 struct i386_ldt_args ua, *uap = &ua;
402 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
406 kprintf("ki386_set_ldt: start=%d num=%d descs=%p\n",
407 uap->start, uap->num, (void *)uap->descs);
410 /* verify range of descriptors to modify */
411 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
416 largest_ld = uap->start + uap->num - 1;
417 if (largest_ld >= MAX_LD)
420 /* allocate user ldt */
421 if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
422 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
426 pcb_ldt->ldt_sd = new_ldt->ldt_sd;
427 kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
428 pcb_ldt->ldt_len * sizeof(union descriptor));
429 pcb_ldt->ldt_base = new_ldt->ldt_base;
430 pcb_ldt->ldt_len = new_ldt->ldt_len;
431 kfree(new_ldt, M_SUBPROC);
433 pcb->pcb_ldt = pcb_ldt = new_ldt;
436 * Since the LDT may be shared, we must signal other cpus to
437 * reload it. XXX we need to track which cpus might be
438 * using the shared ldt and only signal those.
440 lwkt_cpusync_simple(-1, set_user_ldt_cpusync, pcb);
443 descs_size = uap->num * sizeof(union descriptor);
444 descs = (union descriptor *)kmem_alloc(&kernel_map, descs_size);
447 error = copyin(&uap->descs[0], descs, descs_size);
449 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
452 /* Check descriptors for access violations */
453 error = check_descs(descs, uap->num);
455 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
460 * Fill in the actual ldt entries. Since %fs or %gs might point to
461 * one of these entries a critical section is required to prevent an
462 * interrupt thread from preempting us, switch back, and faulting
463 * on the load of %fs due to a half-formed descriptor.
467 &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
468 uap->num * sizeof(union descriptor));
472 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
477 check_descs(union descriptor *descs, int num)
481 /* Check descriptors for access violations */
482 for (i = 0; i < num; i++) {
483 union descriptor *dp;
486 switch (dp->sd.sd_type) {
487 case SDT_SYSNULL: /* system null */
490 case SDT_SYS286TSS: /* system 286 TSS available */
491 case SDT_SYSLDT: /* system local descriptor table */
492 case SDT_SYS286BSY: /* system 286 TSS busy */
493 case SDT_SYSTASKGT: /* system task gate */
494 case SDT_SYS286IGT: /* system 286 interrupt gate */
495 case SDT_SYS286TGT: /* system 286 trap gate */
496 case SDT_SYSNULL2: /* undefined by Intel */
497 case SDT_SYS386TSS: /* system 386 TSS available */
498 case SDT_SYSNULL3: /* undefined by Intel */
499 case SDT_SYS386BSY: /* system 386 TSS busy */
500 case SDT_SYSNULL4: /* undefined by Intel */
501 case SDT_SYS386IGT: /* system 386 interrupt gate */
502 case SDT_SYS386TGT: /* system 386 trap gate */
503 case SDT_SYS286CGT: /* system 286 call gate */
504 case SDT_SYS386CGT: /* system 386 call gate */
505 /* I can't think of any reason to allow a user proc
506 * to create a segment of these types. They are
511 /* memory segment types */
512 case SDT_MEMEC: /* memory execute only conforming */
513 case SDT_MEMEAC: /* memory execute only accessed conforming */
514 case SDT_MEMERC: /* memory execute read conforming */
515 case SDT_MEMERAC: /* memory execute read accessed conforming */
516 /* Must be "present" if executable and conforming. */
517 if (dp->sd.sd_p == 0)
520 case SDT_MEMRO: /* memory read only */
521 case SDT_MEMROA: /* memory read only accessed */
522 case SDT_MEMRW: /* memory read write */
523 case SDT_MEMRWA: /* memory read write accessed */
524 case SDT_MEMROD: /* memory read only expand dwn limit */
525 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
526 case SDT_MEMRWD: /* memory read write expand dwn limit */
527 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
528 case SDT_MEME: /* memory execute only */
529 case SDT_MEMEA: /* memory execute only accessed */
530 case SDT_MEMER: /* memory execute read */
531 case SDT_MEMERA: /* memory execute read accessed */
538 /* Only user (ring-3) descriptors may be present. */
539 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
546 * Called when /dev/io is opened
551 curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
556 * Called when /dev/io is closed
561 curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;