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: releng/5.0/sys/i386/i386/sys_machdep.c 104474 2002-10-04 20:19:36Z jhb $
35 *
36 */
37
38 #include "opt_kstack_pages.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/smp.h>
47 #include <sys/sysproto.h>
48 #include <sys/user.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_extern.h>
54
55 #include <machine/cpu.h>
56 #include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
57 #include <machine/proc.h>
58 #include <machine/sysarch.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 static int i386_get_ldt(struct thread *, char *);
70 static int i386_set_ldt(struct thread *, char *);
71 static int i386_get_ioperm(struct thread *, char *);
72 static int i386_set_ioperm(struct thread *, char *);
73 #ifdef SMP
74 static void set_user_ldt_rv(struct thread *);
75 #endif
76
77 #ifndef _SYS_SYSPROTO_H_
78 struct sysarch_args {
79 int op;
80 char *parms;
81 };
82 #endif
83
84 int
85 sysarch(td, uap)
86 struct thread *td;
87 register struct sysarch_args *uap;
88 {
89 int error = 0;
90
91 switch(uap->op) {
92 case I386_GET_LDT:
93 error = i386_get_ldt(td, uap->parms);
94 break;
95
96 case I386_SET_LDT:
97 error = i386_set_ldt(td, uap->parms);
98 break;
99 case I386_GET_IOPERM:
100 error = i386_get_ioperm(td, uap->parms);
101 break;
102 case I386_SET_IOPERM:
103 error = i386_set_ioperm(td, uap->parms);
104 break;
105 case I386_VM86:
106 error = vm86_sysarch(td, uap->parms);
107 break;
108 default:
109 error = EOPNOTSUPP;
110 break;
111 }
112 return (error);
113 }
114
115 int
116 i386_extend_pcb(struct thread *td)
117 {
118 int i, offset;
119 u_long *addr;
120 struct pcb_ext *ext;
121 struct soft_segment_descriptor ssd = {
122 0, /* segment base address (overwritten) */
123 ctob(IOPAGES + 1) - 1, /* length */
124 SDT_SYS386TSS, /* segment type */
125 0, /* priority level */
126 1, /* descriptor present */
127 0, 0,
128 0, /* default 32 size */
129 0 /* granularity */
130 };
131
132 if (td->td_proc->p_flag & P_KSES)
133 return (EINVAL); /* XXXKSE */
134 /* XXXKSE All the code below only works in 1:1 needs changing */
135 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
136 if (ext == 0)
137 return (ENOMEM);
138 bzero(ext, sizeof(struct pcb_ext));
139 /* -16 is so we can convert a trapframe into vm86trapframe inplace */
140 ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
141 sizeof(struct pcb) - 16;
142 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
143 /*
144 * The last byte of the i/o map must be followed by an 0xff byte.
145 * We arbitrarily allocate 16 bytes here, to keep the starting
146 * address on a doubleword boundary.
147 */
148 offset = PAGE_SIZE - 16;
149 ext->ext_tss.tss_ioopt =
150 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
151 ext->ext_iomap = (caddr_t)ext + offset;
152 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
153
154 addr = (u_long *)ext->ext_vm86.vm86_intmap;
155 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
156 *addr++ = ~0;
157
158 ssd.ssd_base = (unsigned)&ext->ext_tss;
159 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
160 ssdtosd(&ssd, &ext->ext_tssd);
161
162 KASSERT(td->td_proc == curthread->td_proc, ("giving TSS to !curproc"));
163 KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
164 mtx_lock_spin(&sched_lock);
165 td->td_pcb->pcb_ext = ext;
166
167 /* switch to the new TSS after syscall completes */
168 td->td_kse->ke_flags |= KEF_NEEDRESCHED;
169 mtx_unlock_spin(&sched_lock);
170
171 return 0;
172 }
173
174 static int
175 i386_set_ioperm(td, args)
176 struct thread *td;
177 char *args;
178 {
179 int i, error;
180 struct i386_ioperm_args ua;
181 char *iomap;
182
183 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
184 return (error);
185
186 if ((error = suser(td)) != 0)
187 return (error);
188 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
189 return (error);
190 /*
191 * XXX
192 * While this is restricted to root, we should probably figure out
193 * whether any other driver is using this i/o address, as so not to
194 * cause confusion. This probably requires a global 'usage registry'.
195 */
196
197 if (td->td_pcb->pcb_ext == 0)
198 if ((error = i386_extend_pcb(td)) != 0)
199 return (error);
200 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
201
202 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
203 return (EINVAL);
204
205 for (i = ua.start; i < ua.start + ua.length; i++) {
206 if (ua.enable)
207 iomap[i >> 3] &= ~(1 << (i & 7));
208 else
209 iomap[i >> 3] |= (1 << (i & 7));
210 }
211 return (error);
212 }
213
214 static int
215 i386_get_ioperm(td, args)
216 struct thread *td;
217 char *args;
218 {
219 int i, state, error;
220 struct i386_ioperm_args ua;
221 char *iomap;
222
223 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
224 return (error);
225 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
226 return (EINVAL);
227
228 if (td->td_pcb->pcb_ext == 0) {
229 ua.length = 0;
230 goto done;
231 }
232
233 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
234
235 i = ua.start;
236 state = (iomap[i >> 3] >> (i & 7)) & 1;
237 ua.enable = !state;
238 ua.length = 1;
239
240 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
241 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
242 break;
243 ua.length++;
244 }
245
246 done:
247 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
248 return (error);
249 }
250
251 /*
252 * Update the GDT entry pointing to the LDT to point to the LDT of the
253 * current process.
254 *
255 * This must be called with sched_lock held. Unfortunately, we can't use a
256 * mtx_assert() here because cpu_switch() calls this function after changing
257 * curproc but before sched_lock's owner is updated in mi_switch().
258 */
259 void
260 set_user_ldt(struct mdproc *mdp)
261 {
262 struct proc_ldt *pldt;
263
264 pldt = mdp->md_ldt;
265 #ifdef SMP
266 gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
267 #else
268 gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
269 #endif
270 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
271 PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
272 }
273
274 #ifdef SMP
275 static void
276 set_user_ldt_rv(struct thread *td)
277 {
278
279 if (td != PCPU_GET(curthread))
280 return;
281
282 mtx_lock_spin(&sched_lock);
283 set_user_ldt(&td->td_proc->p_md);
284 mtx_unlock_spin(&sched_lock);
285 }
286 #endif
287
288 /*
289 * Must be called with either sched_lock free or held but not recursed.
290 * If it does not return NULL, it will return with it owned.
291 */
292 struct proc_ldt *
293 user_ldt_alloc(struct mdproc *mdp, int len)
294 {
295 struct proc_ldt *pldt, *new_ldt;
296
297 if (mtx_owned(&sched_lock))
298 mtx_unlock_spin(&sched_lock);
299 mtx_assert(&sched_lock, MA_NOTOWNED);
300 MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
301 M_SUBPROC, M_WAITOK);
302
303 new_ldt->ldt_len = len = NEW_MAX_LD(len);
304 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
305 len * sizeof(union descriptor));
306 if (new_ldt->ldt_base == NULL) {
307 FREE(new_ldt, M_SUBPROC);
308 return NULL;
309 }
310 new_ldt->ldt_refcnt = 1;
311 new_ldt->ldt_active = 0;
312
313 mtx_lock_spin(&sched_lock);
314 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
315 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
316 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
317
318 if ((pldt = mdp->md_ldt)) {
319 if (len > pldt->ldt_len)
320 len = pldt->ldt_len;
321 bcopy(pldt->ldt_base, new_ldt->ldt_base,
322 len * sizeof(union descriptor));
323 } else {
324 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
325 }
326 return new_ldt;
327 }
328
329 /*
330 * Must be called either with sched_lock free or held but not recursed.
331 * If md_ldt is not NULL, it will return with sched_lock released.
332 */
333 void
334 user_ldt_free(struct thread *td)
335 {
336 struct mdproc *mdp = &td->td_proc->p_md;
337 struct proc_ldt *pldt = mdp->md_ldt;
338
339 if (pldt == NULL)
340 return;
341
342 if (!mtx_owned(&sched_lock))
343 mtx_lock_spin(&sched_lock);
344 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
345 if (td == PCPU_GET(curthread)) {
346 lldt(_default_ldt);
347 PCPU_SET(currentldt, _default_ldt);
348 }
349
350 mdp->md_ldt = NULL;
351 if (--pldt->ldt_refcnt == 0) {
352 mtx_unlock_spin(&sched_lock);
353 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
354 pldt->ldt_len * sizeof(union descriptor));
355 FREE(pldt, M_SUBPROC);
356 } else
357 mtx_unlock_spin(&sched_lock);
358 }
359
360 static int
361 i386_get_ldt(td, args)
362 struct thread *td;
363 char *args;
364 {
365 int error = 0;
366 struct proc_ldt *pldt = td->td_proc->p_md.md_ldt;
367 int nldt, num;
368 union descriptor *lp;
369 struct i386_ldt_args ua, *uap = &ua;
370
371 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
372 return(error);
373
374 #ifdef DEBUG
375 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
376 uap->start, uap->num, (void *)uap->descs);
377 #endif
378
379 /* verify range of LDTs exist */
380 if ((uap->start < 0) || (uap->num <= 0))
381 return(EINVAL);
382
383 if (pldt) {
384 nldt = pldt->ldt_len;
385 num = min(uap->num, nldt);
386 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
387 } else {
388 nldt = sizeof(ldt)/sizeof(ldt[0]);
389 num = min(uap->num, nldt);
390 lp = &ldt[uap->start];
391 }
392 if (uap->start + num > nldt)
393 return(EINVAL);
394
395 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
396 if (!error)
397 td->td_retval[0] = num;
398
399 return(error);
400 }
401
402 static int
403 i386_set_ldt(td, args)
404 struct thread *td;
405 char *args;
406 {
407 int error = 0, i, n;
408 int largest_ld;
409 struct mdproc *mdp = &td->td_proc->p_md;
410 struct proc_ldt *pldt = mdp->md_ldt;
411 struct i386_ldt_args ua, *uap = &ua;
412 union descriptor *descs;
413 caddr_t old_ldt_base;
414 int descs_size, old_ldt_len;
415 register_t savecrit;
416
417 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
418 return(error);
419
420 #ifdef DEBUG
421 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
422 uap->start, uap->num, (void *)uap->descs);
423 #endif
424
425 /* verify range of descriptors to modify */
426 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
427 (uap->num > MAX_LD))
428 {
429 return(EINVAL);
430 }
431 largest_ld = uap->start + uap->num - 1;
432 if (largest_ld >= MAX_LD)
433 return(EINVAL);
434
435 /* allocate user ldt */
436 if (!pldt || largest_ld >= pldt->ldt_len) {
437 struct proc_ldt *new_ldt = user_ldt_alloc(mdp, largest_ld);
438 if (new_ldt == NULL)
439 return ENOMEM;
440 if (pldt) {
441 old_ldt_base = pldt->ldt_base;
442 old_ldt_len = pldt->ldt_len;
443 pldt->ldt_sd = new_ldt->ldt_sd;
444 pldt->ldt_base = new_ldt->ldt_base;
445 pldt->ldt_len = new_ldt->ldt_len;
446 mtx_unlock_spin(&sched_lock);
447 kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
448 old_ldt_len * sizeof(union descriptor));
449 FREE(new_ldt, M_SUBPROC);
450 #ifndef SMP
451 mtx_lock_spin(&sched_lock);
452 #endif
453 } else {
454 mdp->md_ldt = pldt = new_ldt;
455 #ifdef SMP
456 mtx_unlock_spin(&sched_lock);
457 #endif
458 }
459 #ifdef SMP
460 /* signal other cpus to reload ldt */
461 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
462 NULL, td);
463 #else
464 set_user_ldt(mdp);
465 mtx_unlock_spin(&sched_lock);
466 #endif
467 }
468
469 descs_size = uap->num * sizeof(union descriptor);
470 descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
471 if (descs == NULL)
472 return (ENOMEM);
473 error = copyin(&uap->descs[0], descs, descs_size);
474 if (error) {
475 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
476 return (error);
477 }
478 /* Check descriptors for access violations */
479 for (i = 0, n = uap->start; i < uap->num; i++, n++) {
480 union descriptor *dp;
481 dp = &descs[i];
482
483 switch (dp->sd.sd_type) {
484 case SDT_SYSNULL: /* system null */
485 dp->sd.sd_p = 0;
486 break;
487 case SDT_SYS286TSS: /* system 286 TSS available */
488 case SDT_SYSLDT: /* system local descriptor table */
489 case SDT_SYS286BSY: /* system 286 TSS busy */
490 case SDT_SYSTASKGT: /* system task gate */
491 case SDT_SYS286IGT: /* system 286 interrupt gate */
492 case SDT_SYS286TGT: /* system 286 trap gate */
493 case SDT_SYSNULL2: /* undefined by Intel */
494 case SDT_SYS386TSS: /* system 386 TSS available */
495 case SDT_SYSNULL3: /* undefined by Intel */
496 case SDT_SYS386BSY: /* system 386 TSS busy */
497 case SDT_SYSNULL4: /* undefined by Intel */
498 case SDT_SYS386IGT: /* system 386 interrupt gate */
499 case SDT_SYS386TGT: /* system 386 trap gate */
500 case SDT_SYS286CGT: /* system 286 call gate */
501 case SDT_SYS386CGT: /* system 386 call gate */
502 /* I can't think of any reason to allow a user proc
503 * to create a segment of these types. They are
504 * for OS use only.
505 */
506 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
507 return EACCES;
508 /*NOTREACHED*/
509
510 /* memory segment types */
511 case SDT_MEMEC: /* memory execute only conforming */
512 case SDT_MEMEAC: /* memory execute only accessed conforming */
513 case SDT_MEMERC: /* memory execute read conforming */
514 case SDT_MEMERAC: /* memory execute read accessed conforming */
515 /* Must be "present" if executable and conforming. */
516 if (dp->sd.sd_p == 0) {
517 kmem_free(kernel_map, (vm_offset_t)descs,
518 descs_size);
519 return (EACCES);
520 }
521 break;
522 case SDT_MEMRO: /* memory read only */
523 case SDT_MEMROA: /* memory read only accessed */
524 case SDT_MEMRW: /* memory read write */
525 case SDT_MEMRWA: /* memory read write accessed */
526 case SDT_MEMROD: /* memory read only expand dwn limit */
527 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
528 case SDT_MEMRWD: /* memory read write expand dwn limit */
529 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
530 case SDT_MEME: /* memory execute only */
531 case SDT_MEMEA: /* memory execute only accessed */
532 case SDT_MEMER: /* memory execute read */
533 case SDT_MEMERA: /* memory execute read accessed */
534 break;
535 default:
536 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
537 return(EINVAL);
538 /*NOTREACHED*/
539 }
540
541 /* Only user (ring-3) descriptors may be present. */
542 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL)) {
543 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
544 return (EACCES);
545 }
546 }
547
548 /* Fill in range */
549 savecrit = intr_disable();
550 bcopy(descs,
551 &((union descriptor *)(pldt->ldt_base))[uap->start],
552 uap->num * sizeof(union descriptor));
553 td->td_retval[0] = uap->start;
554 intr_restore(savecrit);
555 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
556 return (0);
557 }
Cache object: e0c3c9a10a39d068e06471245b65053b
|