kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[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  *
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/malloc.h>
42 #include <sys/thread.h>
43 #include <sys/proc.h>
44 #include <sys/priv.h>
45 #include <sys/thread.h>
46 #include <sys/memrange.h>
47
48 #include <vm/vm.h>
49 #include <sys/lock.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
53
54 #include <sys/user.h>
55
56 #include <machine/cpu.h>
57 #include <machine/pcb_ext.h>    /* pcb.h included by sys/user.h */
58 #include <machine/sysarch.h>
59 #include <machine/smp.h>
60 #include <machine/globaldata.h> /* mdcpu */
61
62 #include <vm/vm_kern.h>         /* for kernel_map */
63
64 #include <sys/thread2.h>
65 #include <sys/mplock2.h>
66
67 #define MAX_LD 8192
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)
71
72
73
74 static int ki386_get_ldt(struct lwp *, char *, int *);
75 static int ki386_set_ldt(struct lwp *, char *, int *);
76 static int ki386_get_ioperm(struct lwp *, char *);
77 static int ki386_set_ioperm(struct lwp *, char *);
78 static int check_descs(union descriptor *, int);
79 int i386_extend_pcb(struct lwp *);
80
81 /*
82  * sysarch_args(int op, char *params)
83  *
84  * MPALMOSTSAFE
85  */
86 int
87 sys_sysarch(struct sysarch_args *uap)
88 {
89         struct lwp *lp = curthread->td_lwp;
90         int error = 0;
91
92         get_mplock();
93
94         switch(uap->op) {
95         case I386_GET_LDT:
96                 error = ki386_get_ldt(lp, uap->parms, &uap->sysmsg_result);
97                 break;
98         case I386_SET_LDT:
99                 error = ki386_set_ldt(lp, uap->parms, &uap->sysmsg_result);
100                 break;
101         case I386_GET_IOPERM:
102                 error = ki386_get_ioperm(lp, uap->parms);
103                 break;
104         case I386_SET_IOPERM:
105                 error = ki386_set_ioperm(lp, uap->parms);
106                 break;
107         case I386_VM86:
108                 error = vm86_sysarch(lp, uap->parms);
109                 break;
110         default:
111                 error = EOPNOTSUPP;
112                 break;
113         }
114         rel_mplock();
115         return (error);
116 }
117
118 int
119 i386_extend_pcb(struct lwp *lp)
120 {
121         int i, offset;
122         u_long *addr;
123         struct pcb_ext *ext;
124         struct soft_segment_descriptor ssd = {
125                 0,                      /* segment base address (overwritten) */
126                 ctob(IOPAGES + 1) - 1,  /* length */
127                 SDT_SYS386TSS,          /* segment type */
128                 0,                      /* priority level */
129                 1,                      /* descriptor present */
130                 0, 0,
131                 0,                      /* default 32 size */
132                 0                       /* granularity */
133         };
134
135         ext = (struct pcb_ext *)kmem_alloc(&kernel_map, ctob(IOPAGES+1));
136         if (ext == NULL)
137                 return (ENOMEM);
138         bzero(ext, sizeof(struct pcb_ext)); 
139         ext->ext_tss.tss_esp0 = (unsigned)((char *)lp->lwp_thread->td_pcb - 16);
140         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
141         /*
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.
145          */
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;
151
152         addr = (u_long *)ext->ext_vm86.vm86_intmap;
153         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
154                 *addr++ = ~0;
155
156         ssd.ssd_base = (unsigned)&ext->ext_tss;
157         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
158         ssdtosd(&ssd, &ext->ext_tssd);
159
160         /* 
161          * Put the new TSS where the switch code can find it.  Do
162          * a forced switch to ourself to activate it.
163          */
164         crit_enter();
165         lp->lwp_thread->td_pcb->pcb_ext = ext;
166         lp->lwp_thread->td_switch(lp->lwp_thread);
167         crit_exit();
168         
169         return 0;
170 }
171
172 static int
173 ki386_set_ioperm(struct lwp *lp, char *args)
174 {
175         int i, error;
176         struct i386_ioperm_args ua;
177         char *iomap;
178
179         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
180                 return (error);
181
182         if ((error = priv_check_cred(lp->lwp_thread->td_ucred, PRIV_ROOT, 0)) != 0)
183                 return (error);
184         if (securelevel > 0)
185                 return (EPERM);
186         /*
187          * XXX 
188          * While this is restricted to root, we should probably figure out
189          * whether any other driver is using this i/o address, as so not to
190          * cause confusion.  This probably requires a global 'usage registry'.
191          */
192
193         if (lp->lwp_thread->td_pcb->pcb_ext == 0)
194                 if ((error = i386_extend_pcb(lp)) != 0)
195                         return (error);
196         iomap = (char *)lp->lwp_thread->td_pcb->pcb_ext->ext_iomap;
197
198         if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
199                 return (EINVAL);
200
201         for (i = ua.start; i < ua.start + ua.length; i++) {
202                 if (ua.enable) 
203                         iomap[i >> 3] &= ~(1 << (i & 7));
204                 else
205                         iomap[i >> 3] |= (1 << (i & 7));
206         }
207         return (error);
208 }
209
210 static int
211 ki386_get_ioperm(struct lwp *lp, 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 (lp->lwp_thread->td_pcb->pcb_ext == 0) {
223                 ua.length = 0;
224                 goto done;
225         }
226
227         iomap = (char *)lp->lwp_thread->td_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                         
240 done:
241         error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
242         return (error);
243 }
244
245 /*
246  * Update the TLS entries for the process.  Used by assembly, do not staticize.
247  *
248  * Must be called from a critical section (else an interrupt thread preemption
249  * may cause %gs to fault).  Normally called from the low level swtch.s code.
250  *
251  * MPSAFE
252  */
253 void
254 set_user_TLS(void)
255 {
256         struct thread *td = curthread;
257         int i;
258 #ifdef SMP
259         int off = GTLS_START + mycpu->gd_cpuid * NGDT;
260 #else
261         const int off = GTLS_START;
262 #endif
263         for (i = 0; i < NGTLS; ++i)
264                 gdt[off + i].sd = td->td_tls.tls[i];
265 }
266
267 #ifdef SMP
268 static
269 void
270 set_user_ldt_cpusync(void *arg)
271 {
272         set_user_ldt(arg);
273 }
274 #endif
275
276 /*
277  * Update the GDT entry pointing to the LDT to point to the LDT of the
278  * current process.  Used by assembly, do not staticize.
279  *
280  * Must be called from a critical section (else an interrupt thread preemption
281  * may cause %gs to fault).  Normally called from the low level swtch.s code.
282  */   
283 void
284 set_user_ldt(struct pcb *pcb)
285 {
286         struct pcb_ldt *pcb_ldt;
287
288         if (pcb != curthread->td_pcb)
289                 return;
290
291         pcb_ldt = pcb->pcb_ldt;
292 #ifdef SMP
293         gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
294 #else
295         gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
296 #endif
297         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
298         mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
299 }
300
301 struct pcb_ldt *
302 user_ldt_alloc(struct pcb *pcb, int len)
303 {
304         struct pcb_ldt *pcb_ldt, *new_ldt;
305
306         MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
307                 M_SUBPROC, M_WAITOK);
308
309         new_ldt->ldt_len = len = NEW_MAX_LD(len);
310         new_ldt->ldt_base = (caddr_t)kmem_alloc(&kernel_map,
311                                                 len * sizeof(union descriptor));
312         if (new_ldt->ldt_base == NULL) {
313                 FREE(new_ldt, M_SUBPROC);
314                 return NULL;
315         }
316         new_ldt->ldt_refcnt = 1;
317         new_ldt->ldt_active = 0;
318
319         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
320         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
321         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
322
323         if ((pcb_ldt = pcb->pcb_ldt)) {
324                 if (len > pcb_ldt->ldt_len)
325                         len = pcb_ldt->ldt_len;
326                 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
327                         len * sizeof(union descriptor));
328         } else {
329                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
330         }
331         return new_ldt;
332 }
333
334 void
335 user_ldt_free(struct pcb *pcb)
336 {
337         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
338
339         if (pcb_ldt == NULL)
340                 return;
341
342         crit_enter();
343         if (pcb == curthread->td_pcb) {
344                 lldt(_default_ldt);
345                 mdcpu->gd_currentldt = _default_ldt;
346         }
347         pcb->pcb_ldt = NULL;
348         crit_exit();
349
350         if (--pcb_ldt->ldt_refcnt == 0) {
351                 kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
352                           pcb_ldt->ldt_len * sizeof(union descriptor));
353                 FREE(pcb_ldt, M_SUBPROC);
354         }
355 }
356
357 static int
358 ki386_get_ldt(struct lwp *lwp, char *args, int *res)
359 {
360         int error = 0;
361         struct pcb *pcb = lwp->lwp_thread->td_pcb;
362         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
363         unsigned int nldt, num;
364         union descriptor *lp;
365         struct i386_ldt_args ua, *uap = &ua;
366
367         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
368                 return(error);
369
370 #ifdef  DEBUG
371         kprintf("ki386_get_ldt: start=%d num=%d descs=%p\n",
372             uap->start, uap->num, (void *)uap->descs);
373 #endif
374
375         crit_enter();
376
377         if (pcb_ldt) {
378                 nldt = (unsigned int)pcb_ldt->ldt_len;
379                 num = min(uap->num, nldt);
380                 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
381         } else {
382                 nldt = (unsigned int)(NELEM(ldt));
383                 num = min(uap->num, nldt);
384                 lp = &ldt[uap->start];
385         }
386
387         /*
388          * note: uap->(args), num, and nldt are unsigned.  nldt and num
389          * are limited in scope, but uap->start can be anything.
390          */
391         if (uap->start > nldt || uap->start + num > nldt) {
392                 crit_exit();
393                 return(EINVAL);
394         }
395
396         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
397         if (!error)
398                 *res = num;
399         crit_exit();
400         return(error);
401 }
402
403 static int
404 ki386_set_ldt(struct lwp *lp, char *args, int *res)
405 {
406         int error = 0;
407         int largest_ld;
408         struct pcb *pcb = lp->lwp_thread->td_pcb;
409         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
410         union descriptor *descs;
411         int descs_size;
412         struct i386_ldt_args ua, *uap = &ua;
413
414         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
415                 return(error);
416
417 #ifdef  DEBUG
418         kprintf("ki386_set_ldt: start=%d num=%d descs=%p\n",
419             uap->start, uap->num, (void *)uap->descs);
420 #endif
421
422         /* verify range of descriptors to modify */
423         if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
424                 (uap->num > MAX_LD))
425         {
426                 return(EINVAL);
427         }
428         largest_ld = uap->start + uap->num - 1;
429         if (largest_ld >= MAX_LD)
430                 return(EINVAL);
431
432         /* allocate user ldt */
433         if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
434                 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
435                 if (new_ldt == NULL)
436                         return ENOMEM;
437                 if (pcb_ldt) {
438                         pcb_ldt->ldt_sd = new_ldt->ldt_sd;
439                         kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
440                                   pcb_ldt->ldt_len * sizeof(union descriptor));
441                         pcb_ldt->ldt_base = new_ldt->ldt_base;
442                         pcb_ldt->ldt_len = new_ldt->ldt_len;
443                         FREE(new_ldt, M_SUBPROC);
444                 } else {
445                         pcb->pcb_ldt = pcb_ldt = new_ldt;
446                 }
447                 /*
448                  * Since the LDT may be shared, we must signal other cpus to
449                  * reload it.  XXX we need to track which cpus might be
450                  * using the shared ldt and only signal those.
451                  */
452 #ifdef SMP
453                 lwkt_cpusync_simple(-1, set_user_ldt_cpusync, pcb);
454 #else
455                 set_user_ldt(pcb);
456 #endif
457         }
458
459         descs_size = uap->num * sizeof(union descriptor);
460         descs = (union descriptor *)kmem_alloc(&kernel_map, descs_size);
461         if (descs == NULL)
462                 return (ENOMEM);
463         error = copyin(&uap->descs[0], descs, descs_size);
464         if (error) {
465                 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
466                 return (error);
467         }
468         /* Check descriptors for access violations */
469         error = check_descs(descs, uap->num);
470         if (error) {
471                 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
472                 return (error);
473         }
474
475         /*
476          * Fill in the actual ldt entries.  Since %fs or %gs might point to
477          * one of these entries a critical section is required to prevent an
478          * interrupt thread from preempting us, switch back, and faulting
479          * on the load of %fs due to a half-formed descriptor.
480          */
481         crit_enter();
482         bcopy(descs, 
483                  &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
484                 uap->num * sizeof(union descriptor));
485         *res = uap->start;
486
487         crit_exit();
488         kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
489         return (0);
490 }
491
492 static int
493 check_descs(union descriptor *descs, int num)
494 {
495         int i;
496
497         /* Check descriptors for access violations */
498         for (i = 0; i < num; i++) {
499                 union descriptor *dp;
500                 dp = &descs[i];
501
502                 switch (dp->sd.sd_type) {
503                 case SDT_SYSNULL:       /* system null */ 
504                         dp->sd.sd_p = 0;
505                         break;
506                 case SDT_SYS286TSS: /* system 286 TSS available */
507                 case SDT_SYSLDT:    /* system local descriptor table */
508                 case SDT_SYS286BSY: /* system 286 TSS busy */
509                 case SDT_SYSTASKGT: /* system task gate */
510                 case SDT_SYS286IGT: /* system 286 interrupt gate */
511                 case SDT_SYS286TGT: /* system 286 trap gate */
512                 case SDT_SYSNULL2:  /* undefined by Intel */ 
513                 case SDT_SYS386TSS: /* system 386 TSS available */
514                 case SDT_SYSNULL3:  /* undefined by Intel */
515                 case SDT_SYS386BSY: /* system 386 TSS busy */
516                 case SDT_SYSNULL4:  /* undefined by Intel */ 
517                 case SDT_SYS386IGT: /* system 386 interrupt gate */
518                 case SDT_SYS386TGT: /* system 386 trap gate */
519                 case SDT_SYS286CGT: /* system 286 call gate */ 
520                 case SDT_SYS386CGT: /* system 386 call gate */
521                         /* I can't think of any reason to allow a user proc
522                          * to create a segment of these types.  They are
523                          * for OS use only.
524                          */
525                         return EACCES;
526
527                 /* memory segment types */
528                 case SDT_MEMEC:   /* memory execute only conforming */
529                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
530                 case SDT_MEMERC:  /* memory execute read conforming */
531                 case SDT_MEMERAC: /* memory execute read accessed conforming */
532                         /* Must be "present" if executable and conforming. */
533                         if (dp->sd.sd_p == 0)
534                                 return (EACCES);
535                         break;
536                 case SDT_MEMRO:   /* memory read only */
537                 case SDT_MEMROA:  /* memory read only accessed */
538                 case SDT_MEMRW:   /* memory read write */
539                 case SDT_MEMRWA:  /* memory read write accessed */
540                 case SDT_MEMROD:  /* memory read only expand dwn limit */
541                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
542                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
543                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
544                 case SDT_MEME:    /* memory execute only */ 
545                 case SDT_MEMEA:   /* memory execute only accessed */
546                 case SDT_MEMER:   /* memory execute read */
547                 case SDT_MEMERA:  /* memory execute read accessed */
548                         break;
549                 default:
550                         return(EINVAL);
551                         /*NOTREACHED*/
552                 }
553
554                 /* Only user (ring-3) descriptors may be present. */
555                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
556                         return (EACCES);
557         }
558         return (0);
559 }
560
561 /*
562  * Called when /dev/io is opened
563  */
564 int
565 cpu_set_iopl(void)
566 {
567         curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
568         return(0);
569 }
570
571 /*
572  * Called when /dev/io is closed
573  */
574 int
575 cpu_clr_iopl(void)
576 {
577         curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
578         return(0);
579 }
580