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