Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / platform / pc32 / i386 / sys_machdep.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
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 *
36 */
37
38#include "opt_user_ldt.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/sysproto.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45
46#include <vm/vm.h>
47#include <sys/lock.h>
48#include <vm/pmap.h>
49#include <vm/vm_map.h>
50#include <vm/vm_extern.h>
51
52#include <sys/user.h>
53
54#include <machine/cpu.h>
55#include <machine/ipl.h>
56#include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
57#include <machine/sysarch.h>
58#ifdef SMP
59#include <machine/smp.h>
60#endif
61
62#include <vm/vm_kern.h> /* for kernel_map */
63
64#define MAX_LD 8192
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)
68
69
70
71#ifdef USER_LDT
72static int i386_get_ldt __P((struct proc *, char *));
73static int i386_set_ldt __P((struct proc *, char *));
74#endif
75static int i386_get_ioperm __P((struct proc *, char *));
76static int i386_set_ioperm __P((struct proc *, char *));
77int i386_extend_pcb __P((struct proc *));
78
79#ifndef _SYS_SYSPROTO_H_
80struct sysarch_args {
81 int op;
82 char *parms;
83};
84#endif
85
86int
87sysarch(p, uap)
88 struct proc *p;
89 register struct sysarch_args *uap;
90{
91 int error = 0;
92
93 switch(uap->op) {
94#ifdef USER_LDT
95 case I386_GET_LDT:
96 error = i386_get_ldt(p, uap->parms);
97 break;
98
99 case I386_SET_LDT:
100 error = i386_set_ldt(p, uap->parms);
101 break;
102#endif
103 case I386_GET_IOPERM:
104 error = i386_get_ioperm(p, uap->parms);
105 break;
106 case I386_SET_IOPERM:
107 error = i386_set_ioperm(p, uap->parms);
108 break;
109 case I386_VM86:
110 error = vm86_sysarch(p, uap->parms);
111 break;
112 default:
113 error = EOPNOTSUPP;
114 break;
115 }
116 return (error);
117}
118
119int
120i386_extend_pcb(struct proc *p)
121{
122 int i, offset;
123 u_long *addr;
124 struct pcb_ext *ext;
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 */
131 0, 0,
132 0, /* default 32 size */
133 0 /* granularity */
134 };
135
136 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
137 if (ext == 0)
138 return (ENOMEM);
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);
143 /*
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.
147 */
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;
153
154 addr = (u_long *)ext->ext_vm86.vm86_intmap;
155 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
156 *addr++ = ~0;
157
158 ssd.ssd_base = (unsigned)&ext->ext_tss;
159 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
160 ssdtosd(&ssd, &ext->ext_tssd);
161
162 /* switch to the new TSS after syscall completes */
163 need_resched();
164
165 return 0;
166}
167
168static int
169i386_set_ioperm(p, args)
170 struct proc *p;
171 char *args;
172{
173 int i, error;
174 struct i386_ioperm_args ua;
175 char *iomap;
176
177 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
178 return (error);
179
180 if ((error = suser(p)) != 0)
181 return (error);
182 if (securelevel > 0)
183 return (EPERM);
184 /*
185 * XXX
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'.
189 */
190
191 if (p->p_addr->u_pcb.pcb_ext == 0)
192 if ((error = i386_extend_pcb(p)) != 0)
193 return (error);
194 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
195
196 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
197 return (EINVAL);
198
199 for (i = ua.start; i < ua.start + ua.length; i++) {
200 if (ua.enable)
201 iomap[i >> 3] &= ~(1 << (i & 7));
202 else
203 iomap[i >> 3] |= (1 << (i & 7));
204 }
205 return (error);
206}
207
208static int
209i386_get_ioperm(p, args)
210 struct proc *p;
211 char *args;
212{
213 int i, state, error;
214 struct i386_ioperm_args ua;
215 char *iomap;
216
217 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
218 return (error);
219 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
220 return (EINVAL);
221
222 if (p->p_addr->u_pcb.pcb_ext == 0) {
223 ua.length = 0;
224 goto done;
225 }
226
227 iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
228
229 i = ua.start;
230 state = (iomap[i >> 3] >> (i & 7)) & 1;
231 ua.enable = !state;
232 ua.length = 1;
233
234 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
235 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
236 break;
237 ua.length++;
238 }
239
240done:
241 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
242 return (error);
243}
244
245#ifdef USER_LDT
246/*
247 * Update the GDT entry pointing to the LDT to point to the LDT of the
248 * current process. Do not staticize.
249 */
250void
251set_user_ldt(struct pcb *pcb)
252{
253 struct pcb_ldt *pcb_ldt;
254
255 if (pcb != curpcb)
256 return;
257
258 pcb_ldt = pcb->pcb_ldt;
259#ifdef SMP
260 gdt[cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
261#else
262 gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
263#endif
264 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
265 currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
266}
267
268struct pcb_ldt *
269user_ldt_alloc(struct pcb *pcb, int len)
270{
271 struct pcb_ldt *pcb_ldt, *new_ldt;
272
273 MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
274 M_SUBPROC, M_WAITOK);
275 if (new_ldt == NULL)
276 return NULL;
277
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);
283 return NULL;
284 }
285 new_ldt->ldt_refcnt = 1;
286 new_ldt->ldt_active = 0;
287
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);
291
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));
297 } else {
298 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
299 }
300 return new_ldt;
301}
302
303void
304user_ldt_free(struct pcb *pcb)
305{
306 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
307
308 if (pcb_ldt == NULL)
309 return;
310
311 if (pcb == curpcb) {
312 lldt(_default_ldt);
313 currentldt = _default_ldt;
314 }
315
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);
320 }
321 pcb->pcb_ldt = NULL;
322}
323
324static int
325i386_get_ldt(p, args)
326 struct proc *p;
327 char *args;
328{
329 int error = 0;
330 struct pcb *pcb = &p->p_addr->u_pcb;
331 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
332 int nldt, num;
333 union descriptor *lp;
334 int s;
335 struct i386_ldt_args ua, *uap = &ua;
336
337 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
338 return(error);
339
340#ifdef DEBUG
341 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
342 uap->start, uap->num, (void *)uap->descs);
343#endif
344
345 /* verify range of LDTs exist */
346 if ((uap->start < 0) || (uap->num <= 0))
347 return(EINVAL);
348
349 s = splhigh();
350
351 if (pcb_ldt) {
352 nldt = pcb_ldt->ldt_len;
353 num = min(uap->num, nldt);
354 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
355 } else {
356 nldt = sizeof(ldt)/sizeof(ldt[0]);
357 num = min(uap->num, nldt);
358 lp = &ldt[uap->start];
359 }
360 if (uap->start + num > nldt) {
361 splx(s);
362 return(EINVAL);
363 }
364
365 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
366 if (!error)
367 p->p_retval[0] = num;
368
369 splx(s);
370 return(error);
371}
372
373static int
374i386_set_ldt(p, args)
375 struct proc *p;
376 char *args;
377{
378 int error = 0, i, n;
379 int largest_ld;
380 struct pcb *pcb = &p->p_addr->u_pcb;
381 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
382 union descriptor *descs;
383 int descs_size, s;
384 struct i386_ldt_args ua, *uap = &ua;
385
386 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
387 return(error);
388
389#ifdef DEBUG
390 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
391 uap->start, uap->num, (void *)uap->descs);
392#endif
393
394 /* verify range of descriptors to modify */
395 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
396 (uap->num > MAX_LD))
397 {
398 return(EINVAL);
399 }
400 largest_ld = uap->start + uap->num - 1;
401 if (largest_ld >= MAX_LD)
402 return(EINVAL);
403
404 /* allocate user ldt */
405 if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
406 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
407 if (new_ldt == NULL)
408 return ENOMEM;
409 if (pcb_ldt) {
410 pcb_ldt->ldt_sd = new_ldt->ldt_sd;
411 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
412 pcb_ldt->ldt_len * sizeof(union descriptor));
413 pcb_ldt->ldt_base = new_ldt->ldt_base;
414 pcb_ldt->ldt_len = new_ldt->ldt_len;
415 FREE(new_ldt, M_SUBPROC);
416 } else
417 pcb->pcb_ldt = pcb_ldt = new_ldt;
418#ifdef SMP
419 /* signal other cpus to reload ldt */
420 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt, NULL, pcb);
421#else
422 set_user_ldt(pcb);
423#endif
424 }
425
426 descs_size = uap->num * sizeof(union descriptor);
427 descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
428 if (descs == NULL)
429 return (ENOMEM);
430 error = copyin(&uap->descs[0], descs, descs_size);
431 if (error) {
432 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
433 return (error);
434 }
435 /* Check descriptors for access violations */
436 for (i = 0, n = uap->start; i < uap->num; i++, n++) {
437 union descriptor *dp;
438 dp = &descs[i];
439
440 switch (dp->sd.sd_type) {
441 case SDT_SYSNULL: /* system null */
442 dp->sd.sd_p = 0;
443 break;
444 case SDT_SYS286TSS: /* system 286 TSS available */
445 case SDT_SYSLDT: /* system local descriptor table */
446 case SDT_SYS286BSY: /* system 286 TSS busy */
447 case SDT_SYSTASKGT: /* system task gate */
448 case SDT_SYS286IGT: /* system 286 interrupt gate */
449 case SDT_SYS286TGT: /* system 286 trap gate */
450 case SDT_SYSNULL2: /* undefined by Intel */
451 case SDT_SYS386TSS: /* system 386 TSS available */
452 case SDT_SYSNULL3: /* undefined by Intel */
453 case SDT_SYS386BSY: /* system 386 TSS busy */
454 case SDT_SYSNULL4: /* undefined by Intel */
455 case SDT_SYS386IGT: /* system 386 interrupt gate */
456 case SDT_SYS386TGT: /* system 386 trap gate */
457 case SDT_SYS286CGT: /* system 286 call gate */
458 case SDT_SYS386CGT: /* system 386 call gate */
459 /* I can't think of any reason to allow a user proc
460 * to create a segment of these types. They are
461 * for OS use only.
462 */
463 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
464 return EACCES;
465
466 /* memory segment types */
467 case SDT_MEMEC: /* memory execute only conforming */
468 case SDT_MEMEAC: /* memory execute only accessed conforming */
469 case SDT_MEMERC: /* memory execute read conforming */
470 case SDT_MEMERAC: /* memory execute read accessed conforming */
471 /* Must be "present" if executable and conforming. */
472 if (dp->sd.sd_p == 0) {
473 kmem_free(kernel_map, (vm_offset_t)descs,
474 descs_size);
475 return (EACCES);
476 }
477 break;
478 case SDT_MEMRO: /* memory read only */
479 case SDT_MEMROA: /* memory read only accessed */
480 case SDT_MEMRW: /* memory read write */
481 case SDT_MEMRWA: /* memory read write accessed */
482 case SDT_MEMROD: /* memory read only expand dwn limit */
483 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
484 case SDT_MEMRWD: /* memory read write expand dwn limit */
485 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
486 case SDT_MEME: /* memory execute only */
487 case SDT_MEMEA: /* memory execute only accessed */
488 case SDT_MEMER: /* memory execute read */
489 case SDT_MEMERA: /* memory execute read accessed */
490 break;
491 default:
492 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
493 return(EINVAL);
494 /*NOTREACHED*/
495 }
496
497 /* Only user (ring-3) descriptors may be present. */
498 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL)) {
499 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
500 return (EACCES);
501 }
502 }
503
504 s = splhigh();
505
506 /* Fill in range */
507 bcopy(descs,
508 &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
509 uap->num * sizeof(union descriptor));
510 p->p_retval[0] = uap->start;
511
512 splx(s);
513 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
514 return (0);
515}
516#endif /* USER_LDT */