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
38 #include "opt_user_ldt.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.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>
59 #include <machine/smp.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)
72 static int i386_get_ldt __P((struct proc *, char *));
73 static int i386_set_ldt __P((struct proc *, char *));
75 static int i386_get_ioperm __P((struct proc *, char *));
76 static int i386_set_ioperm __P((struct proc *, char *));
77 int i386_extend_pcb __P((struct proc *));
79 #ifndef _SYS_SYSPROTO_H_
89 register struct sysarch_args *uap;
96 error = i386_get_ldt(p, uap->parms);
100 error = i386_set_ldt(p, uap->parms);
103 case I386_GET_IOPERM:
104 error = i386_get_ioperm(p, uap->parms);
106 case I386_SET_IOPERM:
107 error = i386_set_ioperm(p, uap->parms);
110 error = vm86_sysarch(p, uap->parms);
120 i386_extend_pcb(struct proc *p)
125 struct soft_segment_descriptor ssd = {
126 0, /* segment base address (overwritten) */
127 ctob(IOPAGES + 1) - 1, /* length */
128 SDT_SYS386TSS, /* segment type */
129 0, /* priority level */
130 1, /* descriptor present */
132 0, /* default 32 size */
136 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
139 p->p_addr->u_pcb.pcb_ext = ext;
140 bzero(ext, sizeof(struct pcb_ext));
141 ext->ext_tss.tss_esp0 = (unsigned)p->p_addr + ctob(UPAGES) - 16;
142 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
144 * The last byte of the i/o map must be followed by an 0xff byte.
145 * We arbitrarily allocate 16 bytes here, to keep the starting
146 * address on a doubleword boundary.
148 offset = PAGE_SIZE - 16;
149 ext->ext_tss.tss_ioopt =
150 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
151 ext->ext_iomap = (caddr_t)ext + offset;
152 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
154 addr = (u_long *)ext->ext_vm86.vm86_intmap;
155 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
158 ssd.ssd_base = (unsigned)&ext->ext_tss;
159 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
160 ssdtosd(&ssd, &ext->ext_tssd);
162 /* switch to the new TSS after syscall completes */
169 i386_set_ioperm(p, args)
174 struct i386_ioperm_args ua;
177 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
180 if ((error = suser(p)) != 0)
186 * While this is restricted to root, we should probably figure out
187 * whether any other driver is using this i/o address, as so not to
188 * cause confusion. This probably requires a global 'usage registry'.
191 if (p->p_addr->u_pcb.pcb_ext == 0)
192 if ((error = i386_extend_pcb(p)) != 0)
194 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
196 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
199 for (i = ua.start; i < ua.start + ua.length; i++) {
201 iomap[i >> 3] &= ~(1 << (i & 7));
203 iomap[i >> 3] |= (1 << (i & 7));
209 i386_get_ioperm(p, args)
214 struct i386_ioperm_args ua;
217 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
219 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
222 if (p->p_addr->u_pcb.pcb_ext == 0) {
227 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
230 state = (iomap[i >> 3] >> (i & 7)) & 1;
234 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
235 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
241 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
247 * Update the GDT entry pointing to the LDT to point to the LDT of the
248 * current process. Do not staticize.
251 set_user_ldt(struct pcb *pcb)
253 struct pcb_ldt *pcb_ldt;
258 pcb_ldt = pcb->pcb_ldt;
260 gdt[cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
262 gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
264 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
265 currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
269 user_ldt_alloc(struct pcb *pcb, int len)
271 struct pcb_ldt *pcb_ldt, *new_ldt;
273 MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
274 M_SUBPROC, M_WAITOK);
278 new_ldt->ldt_len = len = NEW_MAX_LD(len);
279 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
280 len * sizeof(union descriptor));
281 if (new_ldt->ldt_base == NULL) {
282 FREE(new_ldt, M_SUBPROC);
285 new_ldt->ldt_refcnt = 1;
286 new_ldt->ldt_active = 0;
288 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
289 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
290 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
292 if ((pcb_ldt = pcb->pcb_ldt)) {
293 if (len > pcb_ldt->ldt_len)
294 len = pcb_ldt->ldt_len;
295 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
296 len * sizeof(union descriptor));
298 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
304 user_ldt_free(struct pcb *pcb)
306 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
313 currentldt = _default_ldt;
316 if (--pcb_ldt->ldt_refcnt == 0) {
317 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
318 pcb_ldt->ldt_len * sizeof(union descriptor));
319 FREE(pcb_ldt, M_SUBPROC);
325 i386_get_ldt(p, args)
330 struct pcb *pcb = &p->p_addr->u_pcb;
331 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
333 union descriptor *lp;
335 struct i386_ldt_args ua, *uap = &ua;
337 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
341 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
342 uap->start, uap->num, (void *)uap->descs);
345 /* verify range of LDTs exist */
346 if ((uap->start < 0) || (uap->num <= 0))
352 nldt = pcb_ldt->ldt_len;
353 num = min(uap->num, nldt);
354 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
356 nldt = sizeof(ldt)/sizeof(ldt[0]);
357 num = min(uap->num, nldt);
358 lp = &ldt[uap->start];
360 if (uap->start > nldt) {
365 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
367 p->p_retval[0] = num;
374 i386_set_ldt(p, args)
380 struct pcb *pcb = &p->p_addr->u_pcb;
381 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
383 struct i386_ldt_args ua, *uap = &ua;
385 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
389 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
390 uap->start, uap->num, (void *)uap->descs);
393 /* verify range of descriptors to modify */
394 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
399 largest_ld = uap->start + uap->num - 1;
400 if (largest_ld >= MAX_LD)
403 /* allocate user ldt */
404 if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
405 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
409 pcb_ldt->ldt_sd = new_ldt->ldt_sd;
410 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
411 pcb_ldt->ldt_len * sizeof(union descriptor));
412 pcb_ldt->ldt_base = new_ldt->ldt_base;
413 pcb_ldt->ldt_len = new_ldt->ldt_len;
414 FREE(new_ldt, M_SUBPROC);
416 pcb->pcb_ldt = pcb_ldt = new_ldt;
418 /* signal other cpus to reload ldt */
419 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt, NULL, pcb);
425 /* Check descriptors for access violations */
426 for (i = 0, n = uap->start; i < uap->num; i++, n++) {
427 union descriptor desc, *dp;
429 error = copyin(dp, &desc, sizeof(union descriptor));
433 switch (desc.sd.sd_type) {
434 case SDT_SYSNULL: /* system null */
437 case SDT_SYS286TSS: /* system 286 TSS available */
438 case SDT_SYSLDT: /* system local descriptor table */
439 case SDT_SYS286BSY: /* system 286 TSS busy */
440 case SDT_SYSTASKGT: /* system task gate */
441 case SDT_SYS286IGT: /* system 286 interrupt gate */
442 case SDT_SYS286TGT: /* system 286 trap gate */
443 case SDT_SYSNULL2: /* undefined by Intel */
444 case SDT_SYS386TSS: /* system 386 TSS available */
445 case SDT_SYSNULL3: /* undefined by Intel */
446 case SDT_SYS386BSY: /* system 386 TSS busy */
447 case SDT_SYSNULL4: /* undefined by Intel */
448 case SDT_SYS386IGT: /* system 386 interrupt gate */
449 case SDT_SYS386TGT: /* system 386 trap gate */
450 case SDT_SYS286CGT: /* system 286 call gate */
451 case SDT_SYS386CGT: /* system 386 call gate */
452 /* I can't think of any reason to allow a user proc
453 * to create a segment of these types. They are
458 /* memory segment types */
459 case SDT_MEMEC: /* memory execute only conforming */
460 case SDT_MEMEAC: /* memory execute only accessed conforming */
461 case SDT_MEMERC: /* memory execute read conforming */
462 case SDT_MEMERAC: /* memory execute read accessed conforming */
463 /* Must be "present" if executable and conforming. */
464 if (desc.sd.sd_p == 0)
467 case SDT_MEMRO: /* memory read only */
468 case SDT_MEMROA: /* memory read only accessed */
469 case SDT_MEMRW: /* memory read write */
470 case SDT_MEMRWA: /* memory read write accessed */
471 case SDT_MEMROD: /* memory read only expand dwn limit */
472 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
473 case SDT_MEMRWD: /* memory read write expand dwn limit */
474 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
475 case SDT_MEME: /* memory execute only */
476 case SDT_MEMEA: /* memory execute only accessed */
477 case SDT_MEMER: /* memory execute read */
478 case SDT_MEMERA: /* memory execute read accessed */
485 /* Only user (ring-3) descriptors may be present. */
486 if ((desc.sd.sd_p != 0) && (desc.sd.sd_dpl != SEL_UPL))
493 error = copyin(uap->descs,
494 &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
495 uap->num * sizeof(union descriptor));
497 p->p_retval[0] = uap->start;
502 #endif /* USER_LDT */