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 $
35 * $DragonFly: src/sys/i386/i386/Attic/sys_machdep.c,v 1.14 2004/04/22 03:39:43 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/malloc.h>
43 #include <sys/thread.h>
45 #include <sys/thread.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
55 #include <machine/cpu.h>
56 #include <machine/ipl.h>
57 #include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
58 #include <machine/sysarch.h>
60 #include <machine/smp.h>
62 #include <machine/globaldata.h> /* mdcpu */
64 #include <vm/vm_kern.h> /* for kernel_map */
65 #include <sys/thread2.h>
68 #define LD_PER_PAGE 512
69 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
70 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
74 static int i386_get_ldt (struct proc *, char *, int *);
75 static int i386_set_ldt (struct proc *, char *, int *);
76 static int i386_get_ioperm (struct proc *, char *);
77 static int i386_set_ioperm (struct proc *, char *);
78 int i386_extend_pcb (struct proc *);
81 * sysarch_args(int op, char *params)
85 sysarch(struct sysarch_args *uap)
87 struct proc *p = curproc;
92 error = i386_get_ldt(p, uap->parms, &uap->sysmsg_result);
96 error = i386_set_ldt(p, uap->parms, &uap->sysmsg_result);
99 error = i386_get_ioperm(p, uap->parms);
101 case I386_SET_IOPERM:
102 error = i386_set_ioperm(p, uap->parms);
105 error = vm86_sysarch(p, uap->parms);
115 i386_extend_pcb(struct proc *p)
120 struct soft_segment_descriptor ssd = {
121 0, /* segment base address (overwritten) */
122 ctob(IOPAGES + 1) - 1, /* length */
123 SDT_SYS386TSS, /* segment type */
124 0, /* priority level */
125 1, /* descriptor present */
127 0, /* default 32 size */
131 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
134 p->p_thread->td_pcb->pcb_ext = ext;
135 bzero(ext, sizeof(struct pcb_ext));
136 ext->ext_tss.tss_esp0 = (unsigned)((char *)p->p_thread->td_pcb - 16);
137 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
139 * The last byte of the i/o map must be followed by an 0xff byte.
140 * We arbitrarily allocate 16 bytes here, to keep the starting
141 * address on a doubleword boundary.
143 offset = PAGE_SIZE - 16;
144 ext->ext_tss.tss_ioopt =
145 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
146 ext->ext_iomap = (caddr_t)ext + offset;
147 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
149 addr = (u_long *)ext->ext_vm86.vm86_intmap;
150 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
153 ssd.ssd_base = (unsigned)&ext->ext_tss;
154 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
155 ssdtosd(&ssd, &ext->ext_tssd);
157 /* switch to the new TSS after syscall completes */
164 i386_set_ioperm(struct proc *p, char *args)
167 struct i386_ioperm_args ua;
170 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
173 if ((error = suser_cred(p->p_ucred, 0)) != 0)
179 * While this is restricted to root, we should probably figure out
180 * whether any other driver is using this i/o address, as so not to
181 * cause confusion. This probably requires a global 'usage registry'.
184 if (p->p_thread->td_pcb->pcb_ext == 0)
185 if ((error = i386_extend_pcb(p)) != 0)
187 iomap = (char *)p->p_thread->td_pcb->pcb_ext->ext_iomap;
189 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
192 for (i = ua.start; i < ua.start + ua.length; i++) {
194 iomap[i >> 3] &= ~(1 << (i & 7));
196 iomap[i >> 3] |= (1 << (i & 7));
202 i386_get_ioperm(struct proc *p, char *args)
205 struct i386_ioperm_args ua;
208 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
210 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
213 if (p->p_thread->td_pcb->pcb_ext == 0) {
218 iomap = (char *)p->p_thread->td_pcb->pcb_ext->ext_iomap;
221 state = (iomap[i >> 3] >> (i & 7)) & 1;
225 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
226 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
232 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
237 * Update the GDT entry pointing to the LDT to point to the LDT of the
238 * current process. Do not staticize.
241 set_user_ldt(struct pcb *pcb)
243 struct pcb_ldt *pcb_ldt;
245 if (pcb != curthread->td_pcb)
248 pcb_ldt = pcb->pcb_ldt;
250 gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
252 gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
254 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
255 mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
259 user_ldt_alloc(struct pcb *pcb, int len)
261 struct pcb_ldt *pcb_ldt, *new_ldt;
263 MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
264 M_SUBPROC, M_WAITOK);
268 new_ldt->ldt_len = len = NEW_MAX_LD(len);
269 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
270 len * sizeof(union descriptor));
271 if (new_ldt->ldt_base == NULL) {
272 FREE(new_ldt, M_SUBPROC);
275 new_ldt->ldt_refcnt = 1;
276 new_ldt->ldt_active = 0;
278 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
279 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
280 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
282 if ((pcb_ldt = pcb->pcb_ldt)) {
283 if (len > pcb_ldt->ldt_len)
284 len = pcb_ldt->ldt_len;
285 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
286 len * sizeof(union descriptor));
288 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
294 user_ldt_free(struct pcb *pcb)
296 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
302 if (pcb == curthread->td_pcb) {
304 mdcpu->gd_currentldt = _default_ldt;
309 if (--pcb_ldt->ldt_refcnt == 0) {
310 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
311 pcb_ldt->ldt_len * sizeof(union descriptor));
312 FREE(pcb_ldt, M_SUBPROC);
317 i386_get_ldt(struct proc *p, char *args, int *res)
320 struct pcb *pcb = p->p_thread->td_pcb;
321 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
323 union descriptor *lp;
325 struct i386_ldt_args ua, *uap = &ua;
327 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
331 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
332 uap->start, uap->num, (void *)uap->descs);
335 /* verify range of LDTs exist */
336 if ((uap->start < 0) || (uap->num <= 0))
342 nldt = pcb_ldt->ldt_len;
343 num = min(uap->num, nldt);
344 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
346 nldt = sizeof(ldt)/sizeof(ldt[0]);
347 num = min(uap->num, nldt);
348 lp = &ldt[uap->start];
350 if (uap->start + num > nldt) {
355 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
363 i386_set_ldt(struct proc *p, char *args, int *res)
367 struct pcb *pcb = p->p_thread->td_pcb;
368 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
369 union descriptor *descs;
371 struct i386_ldt_args ua, *uap = &ua;
373 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
377 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
378 uap->start, uap->num, (void *)uap->descs);
381 /* verify range of descriptors to modify */
382 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
387 largest_ld = uap->start + uap->num - 1;
388 if (largest_ld >= MAX_LD)
391 /* allocate user ldt */
392 if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
393 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
397 pcb_ldt->ldt_sd = new_ldt->ldt_sd;
398 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
399 pcb_ldt->ldt_len * sizeof(union descriptor));
400 pcb_ldt->ldt_base = new_ldt->ldt_base;
401 pcb_ldt->ldt_len = new_ldt->ldt_len;
402 FREE(new_ldt, M_SUBPROC);
404 pcb->pcb_ldt = pcb_ldt = new_ldt;
406 /* signal other cpus to reload ldt */
407 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt, NULL, pcb);
413 descs_size = uap->num * sizeof(union descriptor);
414 descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
417 error = copyin(&uap->descs[0], descs, descs_size);
419 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
422 /* Check descriptors for access violations */
423 for (i = 0, n = uap->start; i < uap->num; i++, n++) {
424 union descriptor *dp;
427 switch (dp->sd.sd_type) {
428 case SDT_SYSNULL: /* system null */
431 case SDT_SYS286TSS: /* system 286 TSS available */
432 case SDT_SYSLDT: /* system local descriptor table */
433 case SDT_SYS286BSY: /* system 286 TSS busy */
434 case SDT_SYSTASKGT: /* system task gate */
435 case SDT_SYS286IGT: /* system 286 interrupt gate */
436 case SDT_SYS286TGT: /* system 286 trap gate */
437 case SDT_SYSNULL2: /* undefined by Intel */
438 case SDT_SYS386TSS: /* system 386 TSS available */
439 case SDT_SYSNULL3: /* undefined by Intel */
440 case SDT_SYS386BSY: /* system 386 TSS busy */
441 case SDT_SYSNULL4: /* undefined by Intel */
442 case SDT_SYS386IGT: /* system 386 interrupt gate */
443 case SDT_SYS386TGT: /* system 386 trap gate */
444 case SDT_SYS286CGT: /* system 286 call gate */
445 case SDT_SYS386CGT: /* system 386 call gate */
446 /* I can't think of any reason to allow a user proc
447 * to create a segment of these types. They are
450 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
453 /* memory segment types */
454 case SDT_MEMEC: /* memory execute only conforming */
455 case SDT_MEMEAC: /* memory execute only accessed conforming */
456 case SDT_MEMERC: /* memory execute read conforming */
457 case SDT_MEMERAC: /* memory execute read accessed conforming */
458 /* Must be "present" if executable and conforming. */
459 if (dp->sd.sd_p == 0) {
460 kmem_free(kernel_map, (vm_offset_t)descs,
465 case SDT_MEMRO: /* memory read only */
466 case SDT_MEMROA: /* memory read only accessed */
467 case SDT_MEMRW: /* memory read write */
468 case SDT_MEMRWA: /* memory read write accessed */
469 case SDT_MEMROD: /* memory read only expand dwn limit */
470 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
471 case SDT_MEMRWD: /* memory read write expand dwn limit */
472 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
473 case SDT_MEME: /* memory execute only */
474 case SDT_MEMEA: /* memory execute only accessed */
475 case SDT_MEMER: /* memory execute read */
476 case SDT_MEMERA: /* memory execute read accessed */
479 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
484 /* Only user (ring-3) descriptors may be present. */
485 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL)) {
486 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
495 &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
496 uap->num * sizeof(union descriptor));
500 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);