Rename functions to avoid conflicts with libc.
[dragonfly.git] / sys / i386 / i386 / sys_machdep.c
CommitLineData
984263bc
MD
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: src/sys/i386/i386/sys_machdep.c,v 1.47.2.3 2002/10/07 17:20:00 jhb Exp $
73f913ca 35 * $DragonFly: src/sys/i386/i386/Attic/sys_machdep.c,v 1.25 2006/09/03 17:55:34 dillon Exp $
984263bc
MD
36 *
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/sysproto.h>
42#include <sys/malloc.h>
85100692 43#include <sys/thread.h>
984263bc 44#include <sys/proc.h>
cf4e130b 45#include <sys/thread.h>
984263bc
MD
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/ipl.h>
57#include <machine/pcb_ext.h> /* pcb.h included by sys/user.h */
58#include <machine/sysarch.h>
984263bc 59#include <machine/smp.h>
85100692 60#include <machine/globaldata.h> /* mdcpu */
984263bc
MD
61
62#include <vm/vm_kern.h> /* for kernel_map */
cf4e130b 63#include <sys/thread2.h>
984263bc
MD
64
65#define MAX_LD 8192
66#define LD_PER_PAGE 512
67#define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
68#define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
69
70
71
73f913ca
MD
72static int ki386_get_ldt(struct lwp *, char *, int *);
73static int ki386_set_ldt(struct lwp *, char *, int *);
74static int ki386_get_ioperm(struct lwp *, char *);
75static int ki386_set_ioperm(struct lwp *, char *);
806bf111 76static int check_descs(union descriptor *, int);
065b709a 77int i386_extend_pcb(struct lwp *);
984263bc 78
41c20dac
MD
79/*
80 * sysarch_args(int op, char *params)
81 */
984263bc
MD
82
83int
753fd850 84sys_sysarch(struct sysarch_args *uap)
984263bc 85{
065b709a 86 struct lwp *lp = curthread->td_lwp;
984263bc
MD
87 int error = 0;
88
89 switch(uap->op) {
984263bc 90 case I386_GET_LDT:
73f913ca 91 error = ki386_get_ldt(lp, uap->parms, &uap->sysmsg_result);
984263bc 92 break;
984263bc 93 case I386_SET_LDT:
73f913ca 94 error = ki386_set_ldt(lp, uap->parms, &uap->sysmsg_result);
984263bc 95 break;
984263bc 96 case I386_GET_IOPERM:
73f913ca 97 error = ki386_get_ioperm(lp, uap->parms);
984263bc
MD
98 break;
99 case I386_SET_IOPERM:
73f913ca 100 error = ki386_set_ioperm(lp, uap->parms);
984263bc
MD
101 break;
102 case I386_VM86:
065b709a 103 error = vm86_sysarch(lp, uap->parms);
984263bc
MD
104 break;
105 default:
106 error = EOPNOTSUPP;
107 break;
108 }
109 return (error);
110}
111
112int
065b709a 113i386_extend_pcb(struct lwp *lp)
984263bc
MD
114{
115 int i, offset;
116 u_long *addr;
117 struct pcb_ext *ext;
118 struct soft_segment_descriptor ssd = {
119 0, /* segment base address (overwritten) */
120 ctob(IOPAGES + 1) - 1, /* length */
121 SDT_SYS386TSS, /* segment type */
122 0, /* priority level */
123 1, /* descriptor present */
124 0, 0,
125 0, /* default 32 size */
126 0 /* granularity */
127 };
128
129 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
130 if (ext == 0)
131 return (ENOMEM);
984263bc 132 bzero(ext, sizeof(struct pcb_ext));
065b709a 133 ext->ext_tss.tss_esp0 = (unsigned)((char *)lp->lwp_thread->td_pcb - 16);
984263bc
MD
134 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
135 /*
136 * The last byte of the i/o map must be followed by an 0xff byte.
137 * We arbitrarily allocate 16 bytes here, to keep the starting
138 * address on a doubleword boundary.
139 */
140 offset = PAGE_SIZE - 16;
141 ext->ext_tss.tss_ioopt =
142 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
143 ext->ext_iomap = (caddr_t)ext + offset;
144 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
145
146 addr = (u_long *)ext->ext_vm86.vm86_intmap;
147 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
148 *addr++ = ~0;
149
150 ssd.ssd_base = (unsigned)&ext->ext_tss;
151 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
152 ssdtosd(&ssd, &ext->ext_tssd);
984263bc 153
df011816
MD
154 /*
155 * Put the new TSS where the switch code can find it. Do
156 * a forced switch to ourself to activate it.
157 */
158 crit_enter();
159 lp->lwp_thread->td_pcb->pcb_ext = ext;
160 lp->lwp_thread->td_switch(lp->lwp_thread);
161 crit_exit();
162
984263bc
MD
163 return 0;
164}
165
166static int
73f913ca 167ki386_set_ioperm(struct lwp *lp, char *args)
984263bc
MD
168{
169 int i, error;
170 struct i386_ioperm_args ua;
171 char *iomap;
172
173 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
174 return (error);
175
065b709a 176 if ((error = suser_cred(lp->lwp_proc->p_ucred, 0)) != 0)
984263bc
MD
177 return (error);
178 if (securelevel > 0)
179 return (EPERM);
180 /*
181 * XXX
182 * While this is restricted to root, we should probably figure out
183 * whether any other driver is using this i/o address, as so not to
184 * cause confusion. This probably requires a global 'usage registry'.
185 */
186
065b709a
SS
187 if (lp->lwp_thread->td_pcb->pcb_ext == 0)
188 if ((error = i386_extend_pcb(lp)) != 0)
984263bc 189 return (error);
065b709a 190 iomap = (char *)lp->lwp_thread->td_pcb->pcb_ext->ext_iomap;
984263bc
MD
191
192 if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
193 return (EINVAL);
194
195 for (i = ua.start; i < ua.start + ua.length; i++) {
196 if (ua.enable)
197 iomap[i >> 3] &= ~(1 << (i & 7));
198 else
199 iomap[i >> 3] |= (1 << (i & 7));
200 }
201 return (error);
202}
203
204static int
73f913ca 205ki386_get_ioperm(struct lwp *lp, char *args)
984263bc
MD
206{
207 int i, state, error;
208 struct i386_ioperm_args ua;
209 char *iomap;
210
211 if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
212 return (error);
213 if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
214 return (EINVAL);
215
065b709a 216 if (lp->lwp_thread->td_pcb->pcb_ext == 0) {
984263bc
MD
217 ua.length = 0;
218 goto done;
219 }
220
065b709a 221 iomap = (char *)lp->lwp_thread->td_pcb->pcb_ext->ext_iomap;
984263bc
MD
222
223 i = ua.start;
224 state = (iomap[i >> 3] >> (i & 7)) & 1;
225 ua.enable = !state;
226 ua.length = 1;
227
228 for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
229 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
230 break;
231 ua.length++;
232 }
233
234done:
235 error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
236 return (error);
237}
238
806bf111
MD
239/*
240 * Update the TLS entries for the process. Used by assembly, do not staticize.
241 *
242 * Must be called from a critical section (else an interrupt thread preemption
243 * may cause %gs to fault). Normally called from the low level swtch.s code.
244 */
245void
246set_user_TLS(void)
247{
248 struct thread *td = curthread;
249 int i;
250#ifdef SMP
251 int off = GTLS_START + mycpu->gd_cpuid * NGDT;
252#else
253 const int off = GTLS_START;
254#endif
255 for (i = 0; i < NGTLS; ++i)
256 gdt[off + i].sd = td->td_tls[i];
257}
258
4e2eba5b 259#ifdef SMP
6819df07
MD
260static
261void
262set_user_ldt_cpusync(struct lwkt_cpusync *cmd)
263{
264 set_user_ldt(cmd->cs_data);
265}
4e2eba5b 266#endif
6819df07 267
984263bc
MD
268/*
269 * Update the GDT entry pointing to the LDT to point to the LDT of the
806bf111
MD
270 * current process. Used by assembly, do not staticize.
271 *
272 * Must be called from a critical section (else an interrupt thread preemption
273 * may cause %gs to fault). Normally called from the low level swtch.s code.
984263bc
MD
274 */
275void
276set_user_ldt(struct pcb *pcb)
277{
278 struct pcb_ldt *pcb_ldt;
279
b7c628e4 280 if (pcb != curthread->td_pcb)
984263bc
MD
281 return;
282
283 pcb_ldt = pcb->pcb_ldt;
284#ifdef SMP
7b95be2a 285 gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
984263bc
MD
286#else
287 gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
288#endif
289 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
85100692 290 mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
984263bc
MD
291}
292
293struct pcb_ldt *
294user_ldt_alloc(struct pcb *pcb, int len)
295{
296 struct pcb_ldt *pcb_ldt, *new_ldt;
297
298 MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
299 M_SUBPROC, M_WAITOK);
300 if (new_ldt == NULL)
301 return NULL;
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 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
314 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
315 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
316
317 if ((pcb_ldt = pcb->pcb_ldt)) {
318 if (len > pcb_ldt->ldt_len)
319 len = pcb_ldt->ldt_len;
320 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
321 len * sizeof(union descriptor));
322 } else {
323 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
324 }
325 return new_ldt;
326}
327
328void
329user_ldt_free(struct pcb *pcb)
330{
331 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
332
333 if (pcb_ldt == NULL)
334 return;
335
cf4e130b 336 crit_enter();
b7c628e4 337 if (pcb == curthread->td_pcb) {
984263bc 338 lldt(_default_ldt);
85100692 339 mdcpu->gd_currentldt = _default_ldt;
984263bc 340 }
cf4e130b
MD
341 pcb->pcb_ldt = NULL;
342 crit_exit();
984263bc
MD
343
344 if (--pcb_ldt->ldt_refcnt == 0) {
345 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
346 pcb_ldt->ldt_len * sizeof(union descriptor));
347 FREE(pcb_ldt, M_SUBPROC);
348 }
984263bc
MD
349}
350
351static int
73f913ca 352ki386_get_ldt(struct lwp *lwp, char *args, int *res)
984263bc
MD
353{
354 int error = 0;
065b709a 355 struct pcb *pcb = lwp->lwp_thread->td_pcb;
984263bc 356 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
6d2edbe1 357 unsigned int nldt, num;
984263bc 358 union descriptor *lp;
984263bc
MD
359 struct i386_ldt_args ua, *uap = &ua;
360
361 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
362 return(error);
363
364#ifdef DEBUG
73f913ca 365 printf("ki386_get_ldt: start=%d num=%d descs=%p\n",
984263bc
MD
366 uap->start, uap->num, (void *)uap->descs);
367#endif
368
9acd5bbb 369 crit_enter();
984263bc
MD
370
371 if (pcb_ldt) {
6d2edbe1 372 nldt = (unsigned int)pcb_ldt->ldt_len;
984263bc
MD
373 num = min(uap->num, nldt);
374 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
375 } else {
6d2edbe1 376 nldt = (unsigned int)(sizeof(ldt) / sizeof(ldt[0]));
984263bc
MD
377 num = min(uap->num, nldt);
378 lp = &ldt[uap->start];
379 }
6d2edbe1
MD
380
381 /*
382 * note: uap->(args), num, and nldt are unsigned. nldt and num
383 * are limited in scope, but uap->start can be anything.
384 */
385 if (uap->start > nldt || uap->start + num > nldt) {
9acd5bbb 386 crit_exit();
984263bc
MD
387 return(EINVAL);
388 }
389
390 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
391 if (!error)
90b9818c 392 *res = num;
9acd5bbb 393 crit_exit();
984263bc
MD
394 return(error);
395}
396
397static int
73f913ca 398ki386_set_ldt(struct lwp *lp, char *args, int *res)
984263bc 399{
806bf111 400 int error = 0;
984263bc 401 int largest_ld;
065b709a 402 struct pcb *pcb = lp->lwp_thread->td_pcb;
984263bc
MD
403 struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
404 union descriptor *descs;
806bf111 405 int descs_size;
984263bc
MD
406 struct i386_ldt_args ua, *uap = &ua;
407
408 if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
409 return(error);
410
411#ifdef DEBUG
73f913ca 412 printf("ki386_set_ldt: start=%d num=%d descs=%p\n",
984263bc
MD
413 uap->start, uap->num, (void *)uap->descs);
414#endif
415
416 /* verify range of descriptors to modify */
417 if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
418 (uap->num > MAX_LD))
419 {
420 return(EINVAL);
421 }
422 largest_ld = uap->start + uap->num - 1;
423 if (largest_ld >= MAX_LD)
424 return(EINVAL);
425
426 /* allocate user ldt */
427 if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
428 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
429 if (new_ldt == NULL)
430 return ENOMEM;
431 if (pcb_ldt) {
432 pcb_ldt->ldt_sd = new_ldt->ldt_sd;
433 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
434 pcb_ldt->ldt_len * sizeof(union descriptor));
435 pcb_ldt->ldt_base = new_ldt->ldt_base;
436 pcb_ldt->ldt_len = new_ldt->ldt_len;
437 FREE(new_ldt, M_SUBPROC);
806bf111 438 } else {
984263bc 439 pcb->pcb_ldt = pcb_ldt = new_ldt;
806bf111
MD
440 }
441 /*
442 * Since the LDT may be shared, we must signal other cpus to
443 * reload it. XXX we need to track which cpus might be
444 * using the shared ldt and only signal those.
445 */
984263bc 446#ifdef SMP
6819df07 447 lwkt_cpusync_simple(-1, set_user_ldt_cpusync, pcb);
984263bc
MD
448#else
449 set_user_ldt(pcb);
450#endif
451 }
452
453 descs_size = uap->num * sizeof(union descriptor);
454 descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
455 if (descs == NULL)
456 return (ENOMEM);
457 error = copyin(&uap->descs[0], descs, descs_size);
458 if (error) {
459 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
460 return (error);
461 }
462 /* Check descriptors for access violations */
806bf111
MD
463 error = check_descs(descs, uap->num);
464 if (error) {
465 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
466 return (error);
467 }
468
469 /*
470 * Fill in the actual ldt entries. Since %fs might point to one of
471 * these entries a critical section is required to prevent an
472 * interrupt thread from preempting us, switch back, and faulting
473 * on the load of %fs due to a half-formed descriptor.
474 */
475 crit_enter();
476 bcopy(descs,
477 &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
478 uap->num * sizeof(union descriptor));
479 *res = uap->start;
480
481 crit_exit();
482 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
483 return (0);
484}
485
486static int
487check_descs(union descriptor *descs, int num)
488{
489 int i;
490
491 /* Check descriptors for access violations */
492 for (i = 0; i < num; i++) {
984263bc
MD
493 union descriptor *dp;
494 dp = &descs[i];
495
496 switch (dp->sd.sd_type) {
497 case SDT_SYSNULL: /* system null */
498 dp->sd.sd_p = 0;
499 break;
500 case SDT_SYS286TSS: /* system 286 TSS available */
501 case SDT_SYSLDT: /* system local descriptor table */
502 case SDT_SYS286BSY: /* system 286 TSS busy */
503 case SDT_SYSTASKGT: /* system task gate */
504 case SDT_SYS286IGT: /* system 286 interrupt gate */
505 case SDT_SYS286TGT: /* system 286 trap gate */
506 case SDT_SYSNULL2: /* undefined by Intel */
507 case SDT_SYS386TSS: /* system 386 TSS available */
508 case SDT_SYSNULL3: /* undefined by Intel */
509 case SDT_SYS386BSY: /* system 386 TSS busy */
510 case SDT_SYSNULL4: /* undefined by Intel */
511 case SDT_SYS386IGT: /* system 386 interrupt gate */
512 case SDT_SYS386TGT: /* system 386 trap gate */
513 case SDT_SYS286CGT: /* system 286 call gate */
514 case SDT_SYS386CGT: /* system 386 call gate */
515 /* I can't think of any reason to allow a user proc
516 * to create a segment of these types. They are
517 * for OS use only.
518 */
984263bc
MD
519 return EACCES;
520
521 /* memory segment types */
522 case SDT_MEMEC: /* memory execute only conforming */
523 case SDT_MEMEAC: /* memory execute only accessed conforming */
524 case SDT_MEMERC: /* memory execute read conforming */
525 case SDT_MEMERAC: /* memory execute read accessed conforming */
526 /* Must be "present" if executable and conforming. */
806bf111 527 if (dp->sd.sd_p == 0)
984263bc 528 return (EACCES);
984263bc
MD
529 break;
530 case SDT_MEMRO: /* memory read only */
531 case SDT_MEMROA: /* memory read only accessed */
532 case SDT_MEMRW: /* memory read write */
533 case SDT_MEMRWA: /* memory read write accessed */
534 case SDT_MEMROD: /* memory read only expand dwn limit */
535 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
536 case SDT_MEMRWD: /* memory read write expand dwn limit */
537 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
538 case SDT_MEME: /* memory execute only */
539 case SDT_MEMEA: /* memory execute only accessed */
540 case SDT_MEMER: /* memory execute read */
541 case SDT_MEMERA: /* memory execute read accessed */
542 break;
543 default:
984263bc
MD
544 return(EINVAL);
545 /*NOTREACHED*/
546 }
547
548 /* Only user (ring-3) descriptors may be present. */
806bf111 549 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
984263bc 550 return (EACCES);
984263bc 551 }
984263bc
MD
552 return (0);
553}