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