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