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