]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/sys_machdep.c
This commit was generated by cvs2svn to compensate for changes in r47148,
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / 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  *      $Id: sys_machdep.c,v 1.40 1999/04/27 11:14:33 phk Exp $
35  *
36  */
37
38 #include "opt_user_ldt.h"
39 #include "opt_vm86.h"
40 #include "opt_smp.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.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/pcb_ext.h>    /* pcb.h included by sys/user.h */
57 #include <machine/sysarch.h>
58 #include <machine/smp.h>
59
60 #include <vm/vm_kern.h>         /* for kernel_map */
61
62 #define MAX_LD 8192
63 #define LD_PER_PAGE 512
64 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
65 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
66
67
68
69 #ifdef USER_LDT
70 void set_user_ldt       __P((struct pcb *pcb));
71 static int i386_get_ldt __P((struct proc *, char *));
72 static int i386_set_ldt __P((struct proc *, char *));
73 #endif
74 #ifdef VM86
75 static int i386_get_ioperm      __P((struct proc *, char *));
76 static int i386_set_ioperm      __P((struct proc *, char *));
77 int i386_extend_pcb     __P((struct proc *));
78 #endif
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 #ifdef VM86
105         case I386_GET_IOPERM:
106                 error = i386_get_ioperm(p, uap->parms);
107                 break;
108         case I386_SET_IOPERM:
109                 error = i386_set_ioperm(p, uap->parms);
110                 break;
111         case I386_VM86:
112                 error = vm86_sysarch(p, uap->parms);
113                 break;
114 #endif
115         default:
116                 error = EINVAL;
117                 break;
118         }
119         return (error);
120 }
121
122 #ifdef VM86
123 int
124 i386_extend_pcb(struct proc *p)
125 {
126         int i, offset;
127         u_long *addr;
128         struct pcb_ext *ext;
129         struct soft_segment_descriptor ssd = {
130                 0,                      /* segment base address (overwritten) */
131                 ctob(IOPAGES + 1) - 1,  /* length */
132                 SDT_SYS386TSS,          /* segment type */
133                 0,                      /* priority level */
134                 1,                      /* descriptor present */
135                 0, 0,
136                 0,                      /* default 32 size */
137                 0                       /* granularity */
138         };
139
140         ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
141         if (ext == 0)
142                 return (ENOMEM);
143         p->p_addr->u_pcb.pcb_ext = ext;
144         bzero(ext, sizeof(struct pcb_ext)); 
145         ext->ext_tss.tss_esp0 = (unsigned)p->p_addr + ctob(UPAGES) - 16;
146         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
147         /*
148          * The last byte of the i/o map must be followed by an 0xff byte.
149          * We arbitrarily allocate 16 bytes here, to keep the starting
150          * address on a doubleword boundary.
151          */
152         offset = PAGE_SIZE - 16;
153         ext->ext_tss.tss_ioopt = 
154             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
155         ext->ext_iomap = (caddr_t)ext + offset;
156         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
157
158         addr = (u_long *)ext->ext_vm86.vm86_intmap;
159         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
160                 *addr++ = ~0;
161
162         ssd.ssd_base = (unsigned)&ext->ext_tss;
163         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
164         ssdtosd(&ssd, &ext->ext_tssd);
165         
166         /* switch to the new TSS after syscall completes */
167         need_resched();
168
169         return 0;
170 }
171
172 struct i386_ioperm_args {
173         u_int start;
174         u_int length;
175         int enable;
176 };
177
178 static int
179 i386_set_ioperm(p, args)
180         struct proc *p;
181         char *args;
182 {
183         int i, error;
184         struct i386_ioperm_args ua;
185         char *iomap;
186
187         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
188                 return (error);
189
190         if ((error = suser(p)) != 0)
191                 return (error);
192         if (securelevel > 0)
193                 return (EPERM);
194         /*
195          * XXX 
196          * While this is restricted to root, we should probably figure out
197          * whether any other driver is using this i/o address, as so not to
198          * cause confusion.  This probably requires a global 'usage registry'.
199          */
200
201         if (p->p_addr->u_pcb.pcb_ext == 0)
202                 if ((error = i386_extend_pcb(p)) != 0)
203                         return (error);
204         iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
205
206         if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
207                 return (EINVAL);
208
209         for (i = ua.start; i < ua.start + ua.length; i++) {
210                 if (ua.enable) 
211                         iomap[i >> 3] &= ~(1 << (i & 7));
212                 else
213                         iomap[i >> 3] |= (1 << (i & 7));
214         }
215         return (error);
216 }
217
218 static int
219 i386_get_ioperm(p, args)
220         struct proc *p;
221         char *args;
222 {
223         int i, state, error;
224         struct i386_ioperm_args ua;
225         char *iomap;
226
227         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
228                 return (error);
229         if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
230                 return (EINVAL);
231
232         if (p->p_addr->u_pcb.pcb_ext == 0) {
233                 ua.length = 0;
234                 goto done;
235         }
236
237         iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
238
239         i = ua.start;
240         state = (iomap[i >> 3] >> (i & 7)) & 1;
241         ua.enable = !state;
242         ua.length = 1;
243
244         for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
245                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
246                         break;
247                 ua.length++;
248         }
249                         
250 done:
251         error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
252         return (error);
253 }
254 #endif /* VM86 */
255
256 #ifdef USER_LDT
257 /*
258  * Update the GDT entry pointing to the LDT to point to the LDT of the
259  * current process.  Do not staticize.
260  */   
261 void
262 set_user_ldt(struct pcb *pcb)
263 {
264         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)pcb->pcb_ldt;
265         gdt_segs[GUSERLDT_SEL].ssd_limit = (pcb->pcb_ldt_len * sizeof(union descriptor)) - 1;
266 #ifdef SMP
267         ssdtosd(&gdt_segs[GUSERLDT_SEL], &gdt[cpuid * NGDT + GUSERLDT_SEL].sd);
268 #else
269         ssdtosd(&gdt_segs[GUSERLDT_SEL], &gdt[GUSERLDT_SEL].sd);
270 #endif
271         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
272         currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
273 }
274
275 struct i386_get_ldt_args {
276         int start;
277         union descriptor *desc;
278         int num;
279 };
280
281 static int
282 i386_get_ldt(p, args)
283         struct proc *p;
284         char *args;
285 {
286         int error = 0;
287         struct pcb *pcb = &p->p_addr->u_pcb;
288         int nldt, num;
289         union descriptor *lp;
290         int s;
291         struct i386_get_ldt_args ua;
292         struct i386_get_ldt_args *uap = &ua;
293
294         if ((error = copyin(args, uap, sizeof(struct i386_get_ldt_args))) < 0)
295                 return(error);
296
297 #ifdef  DEBUG
298         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
299             uap->start, uap->num, (void *)uap->desc);
300 #endif
301
302         /* verify range of LDTs exist */
303         if ((uap->start < 0) || (uap->num <= 0))
304                 return(EINVAL);
305
306         s = splhigh();
307
308         if (pcb->pcb_ldt) {
309                 nldt = pcb->pcb_ldt_len;
310                 num = min(uap->num, nldt);
311                 lp = &((union descriptor *)(pcb->pcb_ldt))[uap->start];
312         } else {
313                 nldt = sizeof(ldt)/sizeof(ldt[0]);
314                 num = min(uap->num, nldt);
315                 lp = &ldt[uap->start];
316         }
317         if (uap->start > nldt) {
318                 splx(s);
319                 return(EINVAL);
320         }
321
322         error = copyout(lp, uap->desc, num * sizeof(union descriptor));
323         if (!error)
324                 p->p_retval[0] = num;
325
326         splx(s);
327         return(error);
328 }
329
330 struct i386_set_ldt_args {
331         int start;
332         union descriptor *desc;
333         int num;
334 };
335
336 static int
337 i386_set_ldt(p, args)
338         struct proc *p;
339         char *args;
340 {
341         int error = 0, i, n;
342         int largest_ld;
343         struct pcb *pcb = &p->p_addr->u_pcb;
344         int s;
345         struct i386_set_ldt_args ua, *uap;
346
347         if ((error = copyin(args, &ua, sizeof(struct i386_set_ldt_args))) < 0)
348                 return(error);
349
350         uap = &ua;
351
352 #ifdef  DEBUG
353         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
354             uap->start, uap->num, (void *)uap->desc);
355 #endif
356
357         /* verify range of descriptors to modify */
358         if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
359                 (uap->num > MAX_LD))
360         {
361                 return(EINVAL);
362         }
363         largest_ld = uap->start + uap->num - 1;
364         if (largest_ld >= MAX_LD)
365                 return(EINVAL);
366   
367         /* allocate user ldt */
368         if (!pcb->pcb_ldt || (largest_ld >= pcb->pcb_ldt_len)) {
369                 union descriptor *new_ldt = (union descriptor *)kmem_alloc(
370                         kernel_map, SIZE_FROM_LARGEST_LD(largest_ld));
371                 if (new_ldt == NULL) {
372                         return ENOMEM;
373                 }
374                 if (pcb->pcb_ldt) {
375                         bcopy(pcb->pcb_ldt, new_ldt, pcb->pcb_ldt_len
376                                 * sizeof(union descriptor));
377                         kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
378                                 pcb->pcb_ldt_len * sizeof(union descriptor));
379                 } else {
380                         bcopy(ldt, new_ldt, sizeof(ldt));
381                 }
382                 pcb->pcb_ldt = (caddr_t)new_ldt;
383                 pcb->pcb_ldt_len = NEW_MAX_LD(largest_ld);
384                 if (pcb == curpcb)
385                     set_user_ldt(pcb);
386         }
387
388         /* Check descriptors for access violations */
389         for (i = 0, n = uap->start; i < uap->num; i++, n++) {
390                 union descriptor desc, *dp;
391                 dp = &uap->desc[i];
392                 error = copyin(dp, &desc, sizeof(union descriptor));
393                 if (error)
394                         return(error);
395
396                 switch (desc.sd.sd_type) {
397                 case SDT_SYSNULL:       /* system null */ 
398                         desc.sd.sd_p = 0;
399                         break;
400                 case SDT_SYS286TSS: /* system 286 TSS available */
401                 case SDT_SYSLDT:    /* system local descriptor table */
402                 case SDT_SYS286BSY: /* system 286 TSS busy */
403                 case SDT_SYSTASKGT: /* system task gate */
404                 case SDT_SYS286IGT: /* system 286 interrupt gate */
405                 case SDT_SYS286TGT: /* system 286 trap gate */
406                 case SDT_SYSNULL2:  /* undefined by Intel */ 
407                 case SDT_SYS386TSS: /* system 386 TSS available */
408                 case SDT_SYSNULL3:  /* undefined by Intel */
409                 case SDT_SYS386BSY: /* system 386 TSS busy */
410                 case SDT_SYSNULL4:  /* undefined by Intel */ 
411                 case SDT_SYS386IGT: /* system 386 interrupt gate */
412                 case SDT_SYS386TGT: /* system 386 trap gate */
413                 case SDT_SYS286CGT: /* system 286 call gate */ 
414                 case SDT_SYS386CGT: /* system 386 call gate */
415                         /* I can't think of any reason to allow a user proc
416                          * to create a segment of these types.  They are
417                          * for OS use only.
418                          */
419                         return EACCES;
420  
421                 /* memory segment types */
422                 case SDT_MEMEC:   /* memory execute only conforming */
423                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
424                 case SDT_MEMERC:  /* memory execute read conforming */
425                 case SDT_MEMERAC: /* memory execute read accessed conforming */
426                          /* Must be "present" if executable and conforming. */
427                          if (desc.sd.sd_p == 0)
428                                  return (EACCES);
429                         break;
430                 case SDT_MEMRO:   /* memory read only */
431                 case SDT_MEMROA:  /* memory read only accessed */
432                 case SDT_MEMRW:   /* memory read write */
433                 case SDT_MEMRWA:  /* memory read write accessed */
434                 case SDT_MEMROD:  /* memory read only expand dwn limit */
435                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
436                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
437                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
438                 case SDT_MEME:    /* memory execute only */ 
439                 case SDT_MEMEA:   /* memory execute only accessed */
440                 case SDT_MEMER:   /* memory execute read */
441                 case SDT_MEMERA:  /* memory execute read accessed */
442                         break;
443                 default:
444                         return(EINVAL);
445                         /*NOTREACHED*/
446                 }
447  
448                 /* Only user (ring-3) descriptors may be present. */
449                 if ((desc.sd.sd_p != 0) && (desc.sd.sd_dpl != SEL_UPL))
450                         return (EACCES);
451         }
452
453         s = splhigh();
454
455         /* Fill in range */
456         error = copyin(uap->desc, 
457                  &((union descriptor *)(pcb->pcb_ldt))[uap->start],
458                 uap->num * sizeof(union descriptor));
459         if (!error)
460                 p->p_retval[0] = uap->start;
461
462         splx(s);
463         return(error);
464 }
465 #endif  /* USER_LDT */