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