kernel: Make SMP support default (and non-optional).
[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         int off = GTLS_START + mycpu->gd_cpuid * NGDT;
259         for (i = 0; i < NGTLS; ++i)
260                 gdt[off + i].sd = td->td_tls.tls[i];
261 }
262
263 static
264 void
265 set_user_ldt_cpusync(void *arg)
266 {
267         set_user_ldt(arg);
268 }
269
270 /*
271  * Update the GDT entry pointing to the LDT to point to the LDT of the
272  * current process.  Used by assembly, do not staticize.
273  *
274  * Must be called from a critical section (else an interrupt thread preemption
275  * may cause %gs to fault).  Normally called from the low level swtch.s code.
276  */   
277 void
278 set_user_ldt(struct pcb *pcb)
279 {
280         struct pcb_ldt *pcb_ldt;
281
282         if (pcb != curthread->td_pcb)
283                 return;
284
285         pcb_ldt = pcb->pcb_ldt;
286         gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
287         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
288         mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
289 }
290
291 struct pcb_ldt *
292 user_ldt_alloc(struct pcb *pcb, int len)
293 {
294         struct pcb_ldt *pcb_ldt, *new_ldt;
295
296         new_ldt = kmalloc(sizeof(struct pcb_ldt), M_SUBPROC, M_WAITOK);
297
298         new_ldt->ldt_len = len = NEW_MAX_LD(len);
299         new_ldt->ldt_base = (caddr_t)kmem_alloc(&kernel_map,
300                                                 len * sizeof(union descriptor));
301         if (new_ldt->ldt_base == NULL) {
302                 kfree(new_ldt, M_SUBPROC);
303                 return NULL;
304         }
305         new_ldt->ldt_refcnt = 1;
306         new_ldt->ldt_active = 0;
307
308         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
309         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
310         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
311
312         if ((pcb_ldt = pcb->pcb_ldt)) {
313                 if (len > pcb_ldt->ldt_len)
314                         len = pcb_ldt->ldt_len;
315                 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
316                         len * sizeof(union descriptor));
317         } else {
318                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
319         }
320         return new_ldt;
321 }
322
323 void
324 user_ldt_free(struct pcb *pcb)
325 {
326         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
327
328         if (pcb_ldt == NULL)
329                 return;
330
331         crit_enter();
332         if (pcb == curthread->td_pcb) {
333                 lldt(_default_ldt);
334                 mdcpu->gd_currentldt = _default_ldt;
335         }
336         pcb->pcb_ldt = NULL;
337         crit_exit();
338
339         if (--pcb_ldt->ldt_refcnt == 0) {
340                 kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
341                           pcb_ldt->ldt_len * sizeof(union descriptor));
342                 kfree(pcb_ldt, M_SUBPROC);
343         }
344 }
345
346 static int
347 ki386_get_ldt(struct lwp *lwp, char *args, int *res)
348 {
349         int error = 0;
350         struct pcb *pcb = lwp->lwp_thread->td_pcb;
351         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
352         unsigned int nldt, num;
353         union descriptor *lp;
354         struct i386_ldt_args ua, *uap = &ua;
355
356         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
357                 return(error);
358
359 #ifdef  DEBUG
360         kprintf("ki386_get_ldt: start=%d num=%d descs=%p\n",
361             uap->start, uap->num, (void *)uap->descs);
362 #endif
363
364         crit_enter();
365
366         if (pcb_ldt) {
367                 nldt = (unsigned int)pcb_ldt->ldt_len;
368                 num = min(uap->num, nldt);
369                 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
370         } else {
371                 nldt = (unsigned int)(NELEM(ldt));
372                 num = min(uap->num, nldt);
373                 lp = &ldt[uap->start];
374         }
375
376         /*
377          * note: uap->(args), num, and nldt are unsigned.  nldt and num
378          * are limited in scope, but uap->start can be anything.
379          */
380         if (uap->start > nldt || uap->start + num > nldt) {
381                 crit_exit();
382                 return(EINVAL);
383         }
384
385         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
386         if (!error)
387                 *res = num;
388         crit_exit();
389         return(error);
390 }
391
392 static int
393 ki386_set_ldt(struct lwp *lp, char *args, int *res)
394 {
395         int error = 0;
396         int largest_ld;
397         struct pcb *pcb = lp->lwp_thread->td_pcb;
398         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
399         union descriptor *descs;
400         int descs_size;
401         struct i386_ldt_args ua, *uap = &ua;
402
403         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
404                 return(error);
405
406 #ifdef  DEBUG
407         kprintf("ki386_set_ldt: start=%d num=%d descs=%p\n",
408             uap->start, uap->num, (void *)uap->descs);
409 #endif
410
411         /* verify range of descriptors to modify */
412         if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
413                 (uap->num > MAX_LD))
414         {
415                 return(EINVAL);
416         }
417         largest_ld = uap->start + uap->num - 1;
418         if (largest_ld >= MAX_LD)
419                 return(EINVAL);
420
421         /* allocate user ldt */
422         if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
423                 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
424                 if (new_ldt == NULL)
425                         return ENOMEM;
426                 if (pcb_ldt) {
427                         pcb_ldt->ldt_sd = new_ldt->ldt_sd;
428                         kmem_free(&kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
429                                   pcb_ldt->ldt_len * sizeof(union descriptor));
430                         pcb_ldt->ldt_base = new_ldt->ldt_base;
431                         pcb_ldt->ldt_len = new_ldt->ldt_len;
432                         kfree(new_ldt, M_SUBPROC);
433                 } else {
434                         pcb->pcb_ldt = pcb_ldt = new_ldt;
435                 }
436                 /*
437                  * Since the LDT may be shared, we must signal other cpus to
438                  * reload it.  XXX we need to track which cpus might be
439                  * using the shared ldt and only signal those.
440                  */
441                 lwkt_cpusync_simple(-1, set_user_ldt_cpusync, pcb);
442         }
443
444         descs_size = uap->num * sizeof(union descriptor);
445         descs = (union descriptor *)kmem_alloc(&kernel_map, descs_size);
446         if (descs == NULL)
447                 return (ENOMEM);
448         error = copyin(&uap->descs[0], descs, descs_size);
449         if (error) {
450                 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
451                 return (error);
452         }
453         /* Check descriptors for access violations */
454         error = check_descs(descs, uap->num);
455         if (error) {
456                 kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
457                 return (error);
458         }
459
460         /*
461          * Fill in the actual ldt entries.  Since %fs or %gs might point to
462          * one of these entries a critical section is required to prevent an
463          * interrupt thread from preempting us, switch back, and faulting
464          * on the load of %fs due to a half-formed descriptor.
465          */
466         crit_enter();
467         bcopy(descs, 
468                  &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
469                 uap->num * sizeof(union descriptor));
470         *res = uap->start;
471
472         crit_exit();
473         kmem_free(&kernel_map, (vm_offset_t)descs, descs_size);
474         return (0);
475 }
476
477 static int
478 check_descs(union descriptor *descs, int num)
479 {
480         int i;
481
482         /* Check descriptors for access violations */
483         for (i = 0; i < num; i++) {
484                 union descriptor *dp;
485                 dp = &descs[i];
486
487                 switch (dp->sd.sd_type) {
488                 case SDT_SYSNULL:       /* system null */ 
489                         dp->sd.sd_p = 0;
490                         break;
491                 case SDT_SYS286TSS: /* system 286 TSS available */
492                 case SDT_SYSLDT:    /* system local descriptor table */
493                 case SDT_SYS286BSY: /* system 286 TSS busy */
494                 case SDT_SYSTASKGT: /* system task gate */
495                 case SDT_SYS286IGT: /* system 286 interrupt gate */
496                 case SDT_SYS286TGT: /* system 286 trap gate */
497                 case SDT_SYSNULL2:  /* undefined by Intel */ 
498                 case SDT_SYS386TSS: /* system 386 TSS available */
499                 case SDT_SYSNULL3:  /* undefined by Intel */
500                 case SDT_SYS386BSY: /* system 386 TSS busy */
501                 case SDT_SYSNULL4:  /* undefined by Intel */ 
502                 case SDT_SYS386IGT: /* system 386 interrupt gate */
503                 case SDT_SYS386TGT: /* system 386 trap gate */
504                 case SDT_SYS286CGT: /* system 286 call gate */ 
505                 case SDT_SYS386CGT: /* system 386 call gate */
506                         /* I can't think of any reason to allow a user proc
507                          * to create a segment of these types.  They are
508                          * for OS use only.
509                          */
510                         return EACCES;
511
512                 /* memory segment types */
513                 case SDT_MEMEC:   /* memory execute only conforming */
514                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
515                 case SDT_MEMERC:  /* memory execute read conforming */
516                 case SDT_MEMERAC: /* memory execute read accessed conforming */
517                         /* Must be "present" if executable and conforming. */
518                         if (dp->sd.sd_p == 0)
519                                 return (EACCES);
520                         break;
521                 case SDT_MEMRO:   /* memory read only */
522                 case SDT_MEMROA:  /* memory read only accessed */
523                 case SDT_MEMRW:   /* memory read write */
524                 case SDT_MEMRWA:  /* memory read write accessed */
525                 case SDT_MEMROD:  /* memory read only expand dwn limit */
526                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
527                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
528                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
529                 case SDT_MEME:    /* memory execute only */ 
530                 case SDT_MEMEA:   /* memory execute only accessed */
531                 case SDT_MEMER:   /* memory execute read */
532                 case SDT_MEMERA:  /* memory execute read accessed */
533                         break;
534                 default:
535                         return(EINVAL);
536                         /*NOTREACHED*/
537                 }
538
539                 /* Only user (ring-3) descriptors may be present. */
540                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
541                         return (EACCES);
542         }
543         return (0);
544 }
545
546 /*
547  * Called when /dev/io is opened
548  */
549 int
550 cpu_set_iopl(void)
551 {
552         curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
553         return(0);
554 }
555
556 /*
557  * Called when /dev/io is closed
558  */
559 int
560 cpu_clr_iopl(void)
561 {
562         curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
563         return(0);
564 }
565