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 * $Id: sys_machdep.c,v 1.41 1999/04/28 01:03:25 luoqi Exp $
38 #include "opt_user_ldt.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_extern.h>
54 #include <machine/cpu.h>
55 #include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
56 #include <machine/sysarch.h>
57 #include <machine/smp.h>
59 #include <vm/vm_kern.h> /* for kernel_map */
62 #define LD_PER_PAGE 512
63 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
64 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
69 void set_user_ldt __P((struct pcb *pcb));
70 static int i386_get_ldt __P((struct proc *, char *));
71 static int i386_set_ldt __P((struct proc *, char *));
73 static int i386_get_ioperm __P((struct proc *, char *));
74 static int i386_set_ioperm __P((struct proc *, char *));
75 int i386_extend_pcb __P((struct proc *));
77 #ifndef _SYS_SYSPROTO_H_
87 register struct sysarch_args *uap;
94 error = i386_get_ldt(p, uap->parms);
98 error = i386_set_ldt(p, uap->parms);
101 case I386_GET_IOPERM:
102 error = i386_get_ioperm(p, uap->parms);
104 case I386_SET_IOPERM:
105 error = i386_set_ioperm(p, uap->parms);
108 error = vm86_sysarch(p, uap->parms);
118 i386_extend_pcb(struct proc *p)
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 p->p_addr->u_pcb.pcb_ext = ext;
138 bzero(ext, sizeof(struct pcb_ext));
139 ext->ext_tss.tss_esp0 = (unsigned)p->p_addr + ctob(UPAGES) - 16;
140 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
142 * The last byte of the i/o map must be followed by an 0xff byte.
143 * We arbitrarily allocate 16 bytes here, to keep the starting
144 * address on a doubleword boundary.
146 offset = PAGE_SIZE - 16;
147 ext->ext_tss.tss_ioopt =
148 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
149 ext->ext_iomap = (caddr_t)ext + offset;
150 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
152 addr = (u_long *)ext->ext_vm86.vm86_intmap;
153 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
156 ssd.ssd_base = (unsigned)&ext->ext_tss;
157 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
158 ssdtosd(&ssd, &ext->ext_tssd);
160 /* switch to the new TSS after syscall completes */
166 struct i386_ioperm_args {
173 i386_set_ioperm(p, args)
178 struct i386_ioperm_args ua;
181 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
184 if ((error = suser(p)) != 0)
190 * While this is restricted to root, we should probably figure out
191 * whether any other driver is using this i/o address, as so not to
192 * cause confusion. This probably requires a global 'usage registry'.
195 if (p->p_addr->u_pcb.pcb_ext == 0)
196 if ((error = i386_extend_pcb(p)) != 0)
198 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
200 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
203 for (i = ua.start; i < ua.start + ua.length; i++) {
205 iomap[i >> 3] &= ~(1 << (i & 7));
207 iomap[i >> 3] |= (1 << (i & 7));
213 i386_get_ioperm(p, args)
218 struct i386_ioperm_args ua;
221 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
223 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
226 if (p->p_addr->u_pcb.pcb_ext == 0) {
231 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
234 state = (iomap[i >> 3] >> (i & 7)) & 1;
238 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
239 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
245 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
251 * Update the GDT entry pointing to the LDT to point to the LDT of the
252 * current process. Do not staticize.
255 set_user_ldt(struct pcb *pcb)
257 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)pcb->pcb_ldt;
258 gdt_segs[GUSERLDT_SEL].ssd_limit = (pcb->pcb_ldt_len * sizeof(union descriptor)) - 1;
260 ssdtosd(&gdt_segs[GUSERLDT_SEL], &gdt[cpuid * NGDT + GUSERLDT_SEL].sd);
262 ssdtosd(&gdt_segs[GUSERLDT_SEL], &gdt[GUSERLDT_SEL].sd);
264 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
265 currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
268 struct i386_get_ldt_args {
270 union descriptor *desc;
275 i386_get_ldt(p, args)
280 struct pcb *pcb = &p->p_addr->u_pcb;
282 union descriptor *lp;
284 struct i386_get_ldt_args ua;
285 struct i386_get_ldt_args *uap = &ua;
287 if ((error = copyin(args, uap, sizeof(struct i386_get_ldt_args))) < 0)
291 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
292 uap->start, uap->num, (void *)uap->desc);
295 /* verify range of LDTs exist */
296 if ((uap->start < 0) || (uap->num <= 0))
302 nldt = pcb->pcb_ldt_len;
303 num = min(uap->num, nldt);
304 lp = &((union descriptor *)(pcb->pcb_ldt))[uap->start];
306 nldt = sizeof(ldt)/sizeof(ldt[0]);
307 num = min(uap->num, nldt);
308 lp = &ldt[uap->start];
310 if (uap->start > nldt) {
315 error = copyout(lp, uap->desc, num * sizeof(union descriptor));
317 p->p_retval[0] = num;
323 struct i386_set_ldt_args {
325 union descriptor *desc;
330 i386_set_ldt(p, args)
336 struct pcb *pcb = &p->p_addr->u_pcb;
338 struct i386_set_ldt_args ua, *uap;
340 if ((error = copyin(args, &ua, sizeof(struct i386_set_ldt_args))) < 0)
346 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
347 uap->start, uap->num, (void *)uap->desc);
350 /* verify range of descriptors to modify */
351 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
356 largest_ld = uap->start + uap->num - 1;
357 if (largest_ld >= MAX_LD)
360 /* allocate user ldt */
361 if (!pcb->pcb_ldt || (largest_ld >= pcb->pcb_ldt_len)) {
362 union descriptor *new_ldt = (union descriptor *)kmem_alloc(
363 kernel_map, SIZE_FROM_LARGEST_LD(largest_ld));
364 if (new_ldt == NULL) {
368 bcopy(pcb->pcb_ldt, new_ldt, pcb->pcb_ldt_len
369 * sizeof(union descriptor));
370 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
371 pcb->pcb_ldt_len * sizeof(union descriptor));
373 bcopy(ldt, new_ldt, sizeof(ldt));
375 pcb->pcb_ldt = (caddr_t)new_ldt;
376 pcb->pcb_ldt_len = NEW_MAX_LD(largest_ld);
381 /* Check descriptors for access violations */
382 for (i = 0, n = uap->start; i < uap->num; i++, n++) {
383 union descriptor desc, *dp;
385 error = copyin(dp, &desc, sizeof(union descriptor));
389 switch (desc.sd.sd_type) {
390 case SDT_SYSNULL: /* system null */
393 case SDT_SYS286TSS: /* system 286 TSS available */
394 case SDT_SYSLDT: /* system local descriptor table */
395 case SDT_SYS286BSY: /* system 286 TSS busy */
396 case SDT_SYSTASKGT: /* system task gate */
397 case SDT_SYS286IGT: /* system 286 interrupt gate */
398 case SDT_SYS286TGT: /* system 286 trap gate */
399 case SDT_SYSNULL2: /* undefined by Intel */
400 case SDT_SYS386TSS: /* system 386 TSS available */
401 case SDT_SYSNULL3: /* undefined by Intel */
402 case SDT_SYS386BSY: /* system 386 TSS busy */
403 case SDT_SYSNULL4: /* undefined by Intel */
404 case SDT_SYS386IGT: /* system 386 interrupt gate */
405 case SDT_SYS386TGT: /* system 386 trap gate */
406 case SDT_SYS286CGT: /* system 286 call gate */
407 case SDT_SYS386CGT: /* system 386 call gate */
408 /* I can't think of any reason to allow a user proc
409 * to create a segment of these types. They are
414 /* memory segment types */
415 case SDT_MEMEC: /* memory execute only conforming */
416 case SDT_MEMEAC: /* memory execute only accessed conforming */
417 case SDT_MEMERC: /* memory execute read conforming */
418 case SDT_MEMERAC: /* memory execute read accessed conforming */
419 /* Must be "present" if executable and conforming. */
420 if (desc.sd.sd_p == 0)
423 case SDT_MEMRO: /* memory read only */
424 case SDT_MEMROA: /* memory read only accessed */
425 case SDT_MEMRW: /* memory read write */
426 case SDT_MEMRWA: /* memory read write accessed */
427 case SDT_MEMROD: /* memory read only expand dwn limit */
428 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
429 case SDT_MEMRWD: /* memory read write expand dwn limit */
430 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
431 case SDT_MEME: /* memory execute only */
432 case SDT_MEMEA: /* memory execute only accessed */
433 case SDT_MEMER: /* memory execute read */
434 case SDT_MEMERA: /* memory execute read accessed */
441 /* Only user (ring-3) descriptors may be present. */
442 if ((desc.sd.sd_p != 0) && (desc.sd.sd_dpl != SEL_UPL))
449 error = copyin(uap->desc,
450 &((union descriptor *)(pcb->pcb_ldt))[uap->start],
451 uap->num * sizeof(union descriptor));
453 p->p_retval[0] = uap->start;
458 #endif /* USER_LDT */