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