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