Deleted Added
full compact
machdep.c (53624) machdep.c (53648)
1/*-
2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
1/*-
2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
38 * $FreeBSD: head/sys/i386/i386/machdep.c 53624 1999-11-23 04:09:13Z green $
38 * $FreeBSD: head/sys/i386/i386/machdep.c 53648 1999-11-24 01:03:08Z archie $
39 */
40
41#include "apm.h"
42#include "ether.h"
43#include "npx.h"
44#include "opt_atalk.h"
45#include "opt_compat.h"
46#include "opt_cpu.h"
47#include "opt_ddb.h"
48#include "opt_inet.h"
49#include "opt_ipx.h"
50#include "opt_maxmem.h"
51#include "opt_msgbuf.h"
52#include "opt_perfmon.h"
53#include "opt_smp.h"
54#include "opt_sysvipc.h"
55#include "opt_user_ldt.h"
56#include "opt_userconfig.h"
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/sysproto.h>
61#include <sys/signalvar.h>
62#include <sys/kernel.h>
63#include <sys/linker.h>
64#include <sys/proc.h>
65#include <sys/buf.h>
66#include <sys/reboot.h>
67#include <sys/callout.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <sys/msgbuf.h>
71#include <sys/sysent.h>
72#include <sys/sysctl.h>
73#include <sys/vmmeter.h>
74#include <sys/bus.h>
75
76#ifdef SYSVSHM
77#include <sys/shm.h>
78#endif
79
80#ifdef SYSVMSG
81#include <sys/msg.h>
82#endif
83
84#ifdef SYSVSEM
85#include <sys/sem.h>
86#endif
87
88#include <vm/vm.h>
89#include <vm/vm_param.h>
90#include <sys/lock.h>
91#include <vm/vm_kern.h>
92#include <vm/vm_object.h>
93#include <vm/vm_page.h>
94#include <vm/vm_map.h>
95#include <vm/vm_pager.h>
96#include <vm/vm_extern.h>
97
98#include <sys/user.h>
99#include <sys/exec.h>
100#include <sys/cons.h>
101
102#include <ddb/ddb.h>
103
104#include <net/netisr.h>
105
106#include <machine/cpu.h>
107#include <machine/reg.h>
108#include <machine/clock.h>
109#include <machine/specialreg.h>
110#include <machine/bootinfo.h>
111#include <machine/ipl.h>
112#include <machine/md_var.h>
113#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
114#ifdef SMP
115#include <machine/smp.h>
116#include <machine/globaldata.h>
117#endif
118#ifdef PERFMON
119#include <machine/perfmon.h>
120#endif
121
122#ifdef OLD_BUS_ARCH
123#include <i386/isa/isa_device.h>
124#endif
125#include <i386/isa/intr_machdep.h>
126#include <isa/rtc.h>
127#include <machine/vm86.h>
128#include <machine/random.h>
129#include <sys/ptrace.h>
130#include <machine/sigframe.h>
131
132extern void init386 __P((int first));
133extern void dblfault_handler __P((void));
134
135extern void printcpuinfo(void); /* XXX header file */
136extern void earlysetcpuclass(void); /* same header file */
137extern void finishidentcpu(void);
138extern void panicifcpuunsupported(void);
139extern void initializecpu(void);
140
141static void cpu_startup __P((void *));
142SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
143
144static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
145
146int _udatasel, _ucodesel;
147u_int atdevbase;
148
149#if defined(SWTCH_OPTIM_STATS)
150extern int swtch_optim_stats;
151SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
152 CTLFLAG_RD, &swtch_optim_stats, 0, "");
153SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
154 CTLFLAG_RD, &tlb_flush_count, 0, "");
155#endif
156
157#ifdef PC98
158static int ispc98 = 1;
159#else
160static int ispc98 = 0;
161#endif
162SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
163
164int physmem = 0;
165int cold = 1;
166
167static int
168sysctl_hw_physmem SYSCTL_HANDLER_ARGS
169{
170 int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
171 return (error);
172}
173
174SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
175 0, 0, sysctl_hw_physmem, "I", "");
176
177static int
178sysctl_hw_usermem SYSCTL_HANDLER_ARGS
179{
180 int error = sysctl_handle_int(oidp, 0,
181 ctob(physmem - cnt.v_wire_count), req);
182 return (error);
183}
184
185SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
186 0, 0, sysctl_hw_usermem, "I", "");
187
188static int
189sysctl_hw_availpages SYSCTL_HANDLER_ARGS
190{
191 int error = sysctl_handle_int(oidp, 0,
192 i386_btop(avail_end - avail_start), req);
193 return (error);
194}
195
196SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD,
197 0, 0, sysctl_hw_availpages, "I", "");
198
199static int
200sysctl_machdep_msgbuf SYSCTL_HANDLER_ARGS
201{
202 int error;
203
204 /* Unwind the buffer, so that it's linear (possibly starting with
205 * some initial nulls).
206 */
207 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr+msgbufp->msg_bufr,
208 msgbufp->msg_size-msgbufp->msg_bufr,req);
209 if(error) return(error);
210 if(msgbufp->msg_bufr>0) {
211 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr,
212 msgbufp->msg_bufr,req);
213 }
214 return(error);
215}
216
217SYSCTL_PROC(_machdep, OID_AUTO, msgbuf, CTLTYPE_STRING|CTLFLAG_RD,
218 0, 0, sysctl_machdep_msgbuf, "A","Contents of kernel message buffer");
219
220static int msgbuf_clear;
221
222static int
223sysctl_machdep_msgbuf_clear SYSCTL_HANDLER_ARGS
224{
225 int error;
226 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
227 req);
228 if (!error && req->newptr) {
229 /* Clear the buffer and reset write pointer */
230 bzero(msgbufp->msg_ptr,msgbufp->msg_size);
231 msgbufp->msg_bufr=msgbufp->msg_bufx=0;
232 msgbuf_clear=0;
233 }
234 return (error);
235}
236
237SYSCTL_PROC(_machdep, OID_AUTO, msgbuf_clear, CTLTYPE_INT|CTLFLAG_RW,
238 &msgbuf_clear, 0, sysctl_machdep_msgbuf_clear, "I",
239 "Clear kernel message buffer");
240
241int bootverbose = 0, Maxmem = 0;
242long dumplo;
243
244vm_offset_t phys_avail[10];
245
246/* must be 2 less so 0 0 can signal end of chunks */
247#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
248
249static vm_offset_t buffer_sva, buffer_eva;
250vm_offset_t clean_sva, clean_eva;
251static vm_offset_t pager_sva, pager_eva;
252
253#define offsetof(type, member) ((size_t)(&((type *)0)->member))
254
255static void
256cpu_startup(dummy)
257 void *dummy;
258{
259 register unsigned i;
260 register caddr_t v;
261 vm_offset_t maxaddr;
262 vm_size_t size = 0;
263 int firstaddr;
264 vm_offset_t minaddr;
265
266 if (boothowto & RB_VERBOSE)
267 bootverbose++;
268
269 /*
270 * Good {morning,afternoon,evening,night}.
271 */
272 printf(version);
273 earlysetcpuclass();
274 startrtclock();
275 printcpuinfo();
276 panicifcpuunsupported();
277#ifdef PERFMON
278 perfmon_init();
279#endif
280 printf("real memory = %u (%uK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024);
281 /*
282 * Display any holes after the first chunk of extended memory.
283 */
284 if (bootverbose) {
285 int indx;
286
287 printf("Physical memory chunk(s):\n");
288 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
289 int size1 = phys_avail[indx + 1] - phys_avail[indx];
290
291 printf("0x%08x - 0x%08x, %u bytes (%u pages)\n",
292 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
293 size1 / PAGE_SIZE);
294 }
295 }
296
297 /*
298 * Calculate callout wheel size
299 */
300 for (callwheelsize = 1, callwheelbits = 0;
301 callwheelsize < ncallout;
302 callwheelsize <<= 1, ++callwheelbits)
303 ;
304 callwheelmask = callwheelsize - 1;
305
306 /*
307 * Allocate space for system data structures.
308 * The first available kernel virtual address is in "v".
309 * As pages of kernel virtual memory are allocated, "v" is incremented.
310 * As pages of memory are allocated and cleared,
311 * "firstaddr" is incremented.
312 * An index into the kernel page table corresponding to the
313 * virtual memory address maintained in "v" is kept in "mapaddr".
314 */
315
316 /*
317 * Make two passes. The first pass calculates how much memory is
318 * needed and allocates it. The second pass assigns virtual
319 * addresses to the various data structures.
320 */
321 firstaddr = 0;
322again:
323 v = (caddr_t)firstaddr;
324
325#define valloc(name, type, num) \
326 (name) = (type *)v; v = (caddr_t)((name)+(num))
327#define valloclim(name, type, num, lim) \
328 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
329
330 valloc(callout, struct callout, ncallout);
331 valloc(callwheel, struct callout_tailq, callwheelsize);
332#ifdef SYSVSHM
333 valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
334#endif
335#ifdef SYSVSEM
336 valloc(sema, struct semid_ds, seminfo.semmni);
337 valloc(sem, struct sem, seminfo.semmns);
338 /* This is pretty disgusting! */
339 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
340#endif
341#ifdef SYSVMSG
342 valloc(msgpool, char, msginfo.msgmax);
343 valloc(msgmaps, struct msgmap, msginfo.msgseg);
344 valloc(msghdrs, struct msg, msginfo.msgtql);
345 valloc(msqids, struct msqid_ds, msginfo.msgmni);
346#endif
347
348 if (nbuf == 0) {
349 nbuf = 50;
350 if (physmem > 1024)
351 nbuf += min((physmem - 1024) / 8, 2048);
352 if (physmem > 16384)
353 nbuf += (physmem - 16384) / 20;
354 }
355 nswbuf = max(min(nbuf/4, 256), 16);
356
357 valloc(swbuf, struct buf, nswbuf);
358 valloc(buf, struct buf, nbuf);
359 v = bufhashinit(v);
360
361 /*
362 * End of first pass, size has been calculated so allocate memory
363 */
364 if (firstaddr == 0) {
365 size = (vm_size_t)(v - firstaddr);
366 firstaddr = (int)kmem_alloc(kernel_map, round_page(size));
367 if (firstaddr == 0)
368 panic("startup: no room for tables");
369 goto again;
370 }
371
372 /*
373 * End of second pass, addresses have been assigned
374 */
375 if ((vm_size_t)(v - firstaddr) != size)
376 panic("startup: table size inconsistency");
377
378 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
379 (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
380 buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
381 (nbuf*BKVASIZE));
382 pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
383 (nswbuf*MAXPHYS) + pager_map_size);
384 pager_map->system_map = 1;
385 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
386 (16*(ARG_MAX+(PAGE_SIZE*3))));
387
388 /*
389 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size
390 * we use the more space efficient malloc in place of kmem_alloc.
391 */
392 {
393 vm_offset_t mb_map_size;
394
395 mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES;
396 mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE));
397 mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT);
398 bzero(mclrefcnt, mb_map_size / MCLBYTES);
399 mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
400 mb_map_size);
401 mb_map->system_map = 1;
402 }
403
404 /*
405 * Initialize callouts
406 */
407 SLIST_INIT(&callfree);
408 for (i = 0; i < ncallout; i++) {
409 callout_init(&callout[i]);
410 callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
411 SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
412 }
413
414 for (i = 0; i < callwheelsize; i++) {
415 TAILQ_INIT(&callwheel[i]);
416 }
417
418#if defined(USERCONFIG)
419 userconfig();
420 cninit(); /* the preferred console may have changed */
421#endif
422
423 printf("avail memory = %u (%uK bytes)\n", ptoa(cnt.v_free_count),
424 ptoa(cnt.v_free_count) / 1024);
425
426 /*
427 * Set up buffers, so they can be used to read disk labels.
428 */
429 bufinit();
430 vm_pager_bufferinit();
431
432#ifdef SMP
433 /*
434 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
435 */
436 mp_start(); /* fire up the APs and APICs */
437 mp_announce();
438#endif /* SMP */
439}
440
441int
442register_netisr(num, handler)
443 int num;
444 netisr_t *handler;
445{
446
447 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
448 printf("register_netisr: bad isr number: %d\n", num);
449 return (EINVAL);
450 }
451 netisrs[num] = handler;
452 return (0);
453}
454
455void
456netisr_sysinit(data)
457 void *data;
458{
459 const struct netisrtab *nit;
460
461 nit = (const struct netisrtab *)data;
462 register_netisr(nit->nit_num, nit->nit_isr);
463}
464
465/*
466 * Send an interrupt to process.
467 *
468 * Stack is set up to allow sigcode stored
469 * at top to call routine, followed by kcall
470 * to sigreturn routine below. After sigreturn
471 * resets the signal mask, the stack, and the
472 * frame pointer, it returns to the user
473 * specified pc, psl.
474 */
475static void
476osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
477{
478 register struct proc *p = curproc;
479 register struct trapframe *regs;
480 register struct osigframe *fp;
481 struct osigframe sf;
482 struct sigacts *psp = p->p_sigacts;
483 int oonstack;
484
485 regs = p->p_md.md_regs;
486 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
487
488 /* Allocate and validate space for the signal handler context. */
489 if ((p->p_flag & P_ALTSTACK) && !oonstack &&
490 SIGISMEMBER(psp->ps_sigonstack, sig)) {
491 fp = (struct osigframe *)(p->p_sigstk.ss_sp +
492 p->p_sigstk.ss_size - sizeof(struct osigframe));
493 p->p_sigstk.ss_flags |= SS_ONSTACK;
494 }
495 else
496 fp = (struct osigframe *)regs->tf_esp - 1;
497
498 /*
499 * grow() will return FALSE if the fp will not fit inside the stack
500 * and the stack can not be grown. useracc will return FALSE
501 * if access is denied.
502 */
503 if (grow_stack(p, (int)fp) == FALSE ||
504 !useracc((caddr_t)fp, sizeof(struct osigframe), VM_PROT_WRITE)) {
505 /*
506 * Process has trashed its stack; give it an illegal
507 * instruction to halt it in its tracks.
508 */
509 SIGACTION(p, SIGILL) = SIG_DFL;
510 SIGDELSET(p->p_sigignore, SIGILL);
511 SIGDELSET(p->p_sigcatch, SIGILL);
512 SIGDELSET(p->p_sigmask, SIGILL);
513 psignal(p, SIGILL);
514 return;
515 }
516
517 /* Translate the signal if appropriate */
518 if (p->p_sysent->sv_sigtbl) {
519 if (sig <= p->p_sysent->sv_sigsize)
520 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
521 }
522
523 /* Build the argument list for the signal handler. */
524 sf.sf_signum = sig;
525 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
526 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
527 /* Signal handler installed with SA_SIGINFO. */
528 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
529 sf.sf_siginfo.si_signo = sig;
530 sf.sf_siginfo.si_code = code;
531 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
532 }
533 else {
534 /* Old FreeBSD-style arguments. */
535 sf.sf_arg2 = code;
536 sf.sf_addr = (register_t *)regs->tf_err;
537 sf.sf_ahu.sf_handler = catcher;
538 }
539
540 /* save scratch registers */
541 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
542 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
543 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
544 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
545 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
546 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
547 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
548 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
549 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
550 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
551 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
552 sf.sf_siginfo.si_sc.sc_gs = rgs();
553 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
554
555 /* Build the signal context to be used by sigreturn. */
556 sf.sf_siginfo.si_sc.sc_onstack = oonstack;
557 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
558 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
559 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
560 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
561 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
562 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
563 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
564
565 /*
566 * If we're a vm86 process, we want to save the segment registers.
567 * We also change eflags to be our emulated eflags, not the actual
568 * eflags.
569 */
570 if (regs->tf_eflags & PSL_VM) {
571 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
572 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
573
574 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
575 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
576 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
577 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
578
579 if (vm86->vm86_has_vme == 0)
580 sf.sf_siginfo.si_sc.sc_ps =
581 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP))
582 | (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
583 /* see sendsig for comment */
584 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
585 }
586
587 /* Copy the sigframe out to the user's stack. */
588 if (copyout(&sf, fp, sizeof(struct osigframe)) != 0) {
589 /*
590 * Something is wrong with the stack pointer.
591 * ...Kill the process.
592 */
593 sigexit(p, SIGILL);
594 }
595
596 regs->tf_esp = (int)fp;
597 regs->tf_eip = PS_STRINGS - oszsigcode;
598 regs->tf_cs = _ucodesel;
599 regs->tf_ds = _udatasel;
600 regs->tf_es = _udatasel;
601 regs->tf_fs = _udatasel;
602 load_gs(_udatasel);
603 regs->tf_ss = _udatasel;
604}
605
606void
607sendsig(catcher, sig, mask, code)
608 sig_t catcher;
609 int sig;
610 sigset_t *mask;
611 u_long code;
612{
613 struct proc *p = curproc;
614 struct trapframe *regs;
615 struct sigacts *psp = p->p_sigacts;
616 struct sigframe sf, *sfp;
617 int oonstack;
618
619 if (SIGISMEMBER(psp->ps_osigset, sig)) {
620 osendsig(catcher, sig, mask, code);
621 return;
622 }
623
624 regs = p->p_md.md_regs;
625 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
626
627 /* save user context */
628 bzero(&sf, sizeof(struct sigframe));
629 sf.sf_uc.uc_sigmask = *mask;
630 sf.sf_uc.uc_stack = p->p_sigstk;
631 sf.sf_uc.uc_mcontext.mc_onstack = oonstack;
632 sf.sf_uc.uc_mcontext.mc_gs = rgs();
633 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(struct trapframe));
634
635 /* Allocate and validate space for the signal handler context. */
636 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
637 SIGISMEMBER(psp->ps_sigonstack, sig)) {
638 sfp = (struct sigframe *)(p->p_sigstk.ss_sp +
639 p->p_sigstk.ss_size - sizeof(struct sigframe));
640 p->p_sigstk.ss_flags |= SS_ONSTACK;
641 }
642 else
643 sfp = (struct sigframe *)regs->tf_esp - 1;
644
645 /*
646 * grow() will return FALSE if the sfp will not fit inside the stack
647 * and the stack can not be grown. useracc will return FALSE if
648 * access is denied.
649 */
650 if (grow_stack(p, (int)sfp) == FALSE ||
651 !useracc((caddr_t)sfp, sizeof(struct sigframe), VM_PROT_WRITE)) {
652 /*
653 * Process has trashed its stack; give it an illegal
654 * instruction to halt it in its tracks.
655 */
656#ifdef DEBUG
657 printf("process %d has trashed its stack\n", p->p_pid);
658#endif
659 SIGACTION(p, SIGILL) = SIG_DFL;
660 SIGDELSET(p->p_sigignore, SIGILL);
661 SIGDELSET(p->p_sigcatch, SIGILL);
662 SIGDELSET(p->p_sigmask, SIGILL);
663 psignal(p, SIGILL);
664 return;
665 }
666
667 /* Translate the signal is appropriate */
668 if (p->p_sysent->sv_sigtbl) {
669 if (sig <= p->p_sysent->sv_sigsize)
670 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
671 }
672
673 /* Build the argument list for the signal handler. */
674 sf.sf_signum = sig;
675 sf.sf_ucontext = (register_t)&sfp->sf_uc;
676 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
677 /* Signal handler installed with SA_SIGINFO. */
678 sf.sf_siginfo = (register_t)&sfp->sf_si;
679 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
680
681 /* fill siginfo structure */
682 sf.sf_si.si_signo = sig;
683 sf.sf_si.si_code = code;
684 sf.sf_si.si_addr = (void*)regs->tf_err;
685 }
686 else {
687 /* Old FreeBSD-style arguments. */
688 sf.sf_siginfo = code;
689 sf.sf_addr = (register_t *)regs->tf_err;
690 sf.sf_ahu.sf_handler = catcher;
691 }
692
693 /*
694 * If we're a vm86 process, we want to save the segment registers.
695 * We also change eflags to be our emulated eflags, not the actual
696 * eflags.
697 */
698 if (regs->tf_eflags & PSL_VM) {
699 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
700 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
701
702 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
703 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
704 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
705 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
706
707 if (vm86->vm86_has_vme == 0)
708 sf.sf_uc.uc_mcontext.mc_eflags =
709 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
710 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
711
712 /*
713 * We should never have PSL_T set when returning from vm86
714 * mode. It may be set here if we deliver a signal before
715 * getting to vm86 mode, so turn it off.
716 *
717 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
718 * syscalls made by the signal handler. This just avoids
719 * wasting time for our lazy fixup of such faults. PSL_NT
720 * does nothing in vm86 mode, but vm86 programs can set it
721 * almost legitimately in probes for old cpu types.
722 */
723 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
724 }
725
726 /*
727 * Copy the sigframe out to the user's stack.
728 */
729 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
730 /*
731 * Something is wrong with the stack pointer.
732 * ...Kill the process.
733 */
734 sigexit(p, SIGILL);
735 }
736
737 regs->tf_esp = (int)sfp;
738 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
739 regs->tf_cs = _ucodesel;
740 regs->tf_ds = _udatasel;
741 regs->tf_es = _udatasel;
742 regs->tf_fs = _udatasel;
743 load_gs(_udatasel);
744 regs->tf_ss = _udatasel;
745}
746
747/*
748 * System call to cleanup state after a signal
749 * has been taken. Reset signal mask and
750 * stack state from context left by sendsig (above).
751 * Return to previous pc and psl as specified by
752 * context left by sendsig. Check carefully to
753 * make sure that the user has not modified the
754 * state to gain improper privileges.
755 */
756#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
757#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
758
759int
760osigreturn(p, uap)
761 struct proc *p;
762 struct osigreturn_args /* {
763 struct osigcontext *sigcntxp;
764 } */ *uap;
765{
766 register struct osigcontext *scp;
767 register struct trapframe *regs = p->p_md.md_regs;
768 int eflags;
769
770 scp = uap->sigcntxp;
771
772 if (!useracc((caddr_t)scp, sizeof (struct osigcontext), VM_PROT_READ))
773 return(EFAULT);
774
775 eflags = scp->sc_ps;
776 if (eflags & PSL_VM) {
777 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
778 struct vm86_kernel *vm86;
779
780 /*
781 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
782 * set up the vm86 area, and we can't enter vm86 mode.
783 */
784 if (p->p_addr->u_pcb.pcb_ext == 0)
785 return (EINVAL);
786 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
787 if (vm86->vm86_inited == 0)
788 return (EINVAL);
789
790 /* go back to user mode if both flags are set */
791 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
792 trapsignal(p, SIGBUS, 0);
793
794 if (vm86->vm86_has_vme) {
795 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
796 (eflags & VME_USERCHANGE) | PSL_VM;
797 } else {
798 vm86->vm86_eflags = eflags; /* save VIF, VIP */
799 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
800 }
801 tf->tf_vm86_ds = scp->sc_ds;
802 tf->tf_vm86_es = scp->sc_es;
803 tf->tf_vm86_fs = scp->sc_fs;
804 tf->tf_vm86_gs = scp->sc_gs;
805 tf->tf_ds = _udatasel;
806 tf->tf_es = _udatasel;
807 tf->tf_fs = _udatasel;
808 } else {
809 /*
810 * Don't allow users to change privileged or reserved flags.
811 */
812 /*
813 * XXX do allow users to change the privileged flag PSL_RF.
814 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
815 * should sometimes set it there too. tf_eflags is kept in
816 * the signal context during signal handling and there is no
817 * other place to remember it, so the PSL_RF bit may be
818 * corrupted by the signal handler without us knowing.
819 * Corruption of the PSL_RF bit at worst causes one more or
820 * one less debugger trap, so allowing it is fairly harmless.
821 */
822 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
823 return(EINVAL);
824 }
825
826 /*
827 * Don't allow users to load a valid privileged %cs. Let the
828 * hardware check for invalid selectors, excess privilege in
829 * other selectors, invalid %eip's and invalid %esp's.
830 */
831 if (!CS_SECURE(scp->sc_cs)) {
832 trapsignal(p, SIGBUS, T_PROTFLT);
833 return(EINVAL);
834 }
835 regs->tf_ds = scp->sc_ds;
836 regs->tf_es = scp->sc_es;
837 regs->tf_fs = scp->sc_fs;
838 }
839
840 /* restore scratch registers */
841 regs->tf_eax = scp->sc_eax;
842 regs->tf_ebx = scp->sc_ebx;
843 regs->tf_ecx = scp->sc_ecx;
844 regs->tf_edx = scp->sc_edx;
845 regs->tf_esi = scp->sc_esi;
846 regs->tf_edi = scp->sc_edi;
847 regs->tf_cs = scp->sc_cs;
848 regs->tf_ss = scp->sc_ss;
849 regs->tf_isp = scp->sc_isp;
850
851 if (scp->sc_onstack & 01)
852 p->p_sigstk.ss_flags |= SS_ONSTACK;
853 else
854 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
855
856 SIGSETOLD(p->p_sigmask, scp->sc_mask);
857 SIG_CANTMASK(p->p_sigmask);
858 regs->tf_ebp = scp->sc_fp;
859 regs->tf_esp = scp->sc_sp;
860 regs->tf_eip = scp->sc_pc;
861 regs->tf_eflags = eflags;
862 return(EJUSTRETURN);
863}
864
865int
866sigreturn(p, uap)
867 struct proc *p;
868 struct sigreturn_args /* {
869 ucontext_t *sigcntxp;
870 } */ *uap;
871{
872 struct trapframe *regs;
873 ucontext_t *ucp;
874 int cs, eflags;
875
876 ucp = uap->sigcntxp;
877
878 if (!useracc((caddr_t)ucp, sizeof(struct osigcontext), VM_PROT_READ))
879 return (EFAULT);
880 if (((struct osigcontext *)ucp)->sc_trapno == 0x01d516)
881 return (osigreturn(p, (struct osigreturn_args *)uap));
882
883 /*
884 * Since ucp is not an osigcontext but a ucontext_t, we have to
885 * check again if all of it is accessible. A ucontext_t is
886 * much larger, so instead of just checking for the pointer
887 * being valid for the size of an osigcontext, now check for
888 * it being valid for a whole, new-style ucontext_t.
889 */
890 if (!useracc((caddr_t)ucp, sizeof(ucontext_t), VM_PROT_READ))
891 return (EFAULT);
892
893 regs = p->p_md.md_regs;
894 eflags = ucp->uc_mcontext.mc_eflags;
895
896 if (eflags & PSL_VM) {
897 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
898 struct vm86_kernel *vm86;
899
900 /*
901 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
902 * set up the vm86 area, and we can't enter vm86 mode.
903 */
904 if (p->p_addr->u_pcb.pcb_ext == 0)
905 return (EINVAL);
906 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
907 if (vm86->vm86_inited == 0)
908 return (EINVAL);
909
910 /* go back to user mode if both flags are set */
911 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
912 trapsignal(p, SIGBUS, 0);
913
914 if (vm86->vm86_has_vme) {
915 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
916 (eflags & VME_USERCHANGE) | PSL_VM;
917 } else {
918 vm86->vm86_eflags = eflags; /* save VIF, VIP */
919 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
920 }
921 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
922 tf->tf_eflags = eflags;
923 tf->tf_vm86_ds = tf->tf_ds;
924 tf->tf_vm86_es = tf->tf_es;
925 tf->tf_vm86_fs = tf->tf_fs;
926 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
927 tf->tf_ds = _udatasel;
928 tf->tf_es = _udatasel;
929 tf->tf_fs = _udatasel;
930 } else {
931 /*
932 * Don't allow users to change privileged or reserved flags.
933 */
934 /*
935 * XXX do allow users to change the privileged flag PSL_RF.
936 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
937 * should sometimes set it there too. tf_eflags is kept in
938 * the signal context during signal handling and there is no
939 * other place to remember it, so the PSL_RF bit may be
940 * corrupted by the signal handler without us knowing.
941 * Corruption of the PSL_RF bit at worst causes one more or
942 * one less debugger trap, so allowing it is fairly harmless.
943 */
944 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
945 printf("sigreturn: eflags = 0x%x\n", eflags);
946 return(EINVAL);
947 }
948
949 /*
950 * Don't allow users to load a valid privileged %cs. Let the
951 * hardware check for invalid selectors, excess privilege in
952 * other selectors, invalid %eip's and invalid %esp's.
953 */
954 cs = ucp->uc_mcontext.mc_cs;
955 if (!CS_SECURE(cs)) {
956 printf("sigreturn: cs = 0x%x\n", cs);
957 trapsignal(p, SIGBUS, T_PROTFLT);
958 return(EINVAL);
959 }
960 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(struct trapframe));
961 }
962
963 if (ucp->uc_mcontext.mc_onstack & 1)
964 p->p_sigstk.ss_flags |= SS_ONSTACK;
965 else
966 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
967
968 p->p_sigmask = ucp->uc_sigmask;
969 SIG_CANTMASK(p->p_sigmask);
970 return(EJUSTRETURN);
971}
972
973/*
974 * Machine dependent boot() routine
975 *
976 * I haven't seen anything to put here yet
977 * Possibly some stuff might be grafted back here from boot()
978 */
979void
980cpu_boot(int howto)
981{
982}
983
984/*
985 * Shutdown the CPU as much as possible
986 */
987void
988cpu_halt(void)
989{
990 for (;;)
991 __asm__ ("hlt");
992}
993
994/*
995 * Clear registers on exec
996 */
997void
998setregs(p, entry, stack, ps_strings)
999 struct proc *p;
1000 u_long entry;
1001 u_long stack;
1002 u_long ps_strings;
1003{
1004 struct trapframe *regs = p->p_md.md_regs;
1005 struct pcb *pcb = &p->p_addr->u_pcb;
1006
1007#ifdef USER_LDT
1008 /* was i386_user_cleanup() in NetBSD */
1009 if (pcb->pcb_ldt) {
1010 if (pcb == curpcb) {
1011 lldt(_default_ldt);
1012 currentldt = _default_ldt;
1013 }
1014 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
1015 pcb->pcb_ldt_len * sizeof(union descriptor));
1016 pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
1017 }
1018#endif
1019
1020 bzero((char *)regs, sizeof(struct trapframe));
1021 regs->tf_eip = entry;
1022 regs->tf_esp = stack;
1023 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1024 regs->tf_ss = _udatasel;
1025 regs->tf_ds = _udatasel;
1026 regs->tf_es = _udatasel;
1027 regs->tf_fs = _udatasel;
1028 regs->tf_cs = _ucodesel;
1029
1030 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1031 regs->tf_ebx = ps_strings;
1032
1033 /* reset %gs as well */
1034 if (pcb == curpcb)
1035 load_gs(_udatasel);
1036 else
1037 pcb->pcb_gs = _udatasel;
1038
1039 /*
1040 * Initialize the math emulator (if any) for the current process.
1041 * Actually, just clear the bit that says that the emulator has
1042 * been initialized. Initialization is delayed until the process
1043 * traps to the emulator (if it is done at all) mainly because
1044 * emulators don't provide an entry point for initialization.
1045 */
1046 p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP;
1047
1048 /*
1049 * Arrange to trap the next npx or `fwait' instruction (see npx.c
1050 * for why fwait must be trapped at least if there is an npx or an
1051 * emulator). This is mainly to handle the case where npx0 is not
1052 * configured, since the npx routines normally set up the trap
1053 * otherwise. It should be done only at boot time, but doing it
1054 * here allows modifying `npx_exists' for testing the emulator on
1055 * systems with an npx.
1056 */
1057 load_cr0(rcr0() | CR0_MP | CR0_TS);
1058
1059#if NNPX > 0
1060 /* Initialize the npx (if any) for the current process. */
1061 npxinit(__INITIAL_NPXCW__);
1062#endif
1063
1064 /*
1065 * XXX - Linux emulator
1066 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1067 * on it.
1068 */
1069 p->p_retval[1] = 0;
1070}
1071
1072static int
1073sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
1074{
1075 int error;
1076 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1077 req);
1078 if (!error && req->newptr)
1079 resettodr();
1080 return (error);
1081}
1082
1083SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1084 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1085
1086SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1087 CTLFLAG_RW, &disable_rtc_set, 0, "");
1088
1089SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
1090 CTLFLAG_RD, &bootinfo, bootinfo, "");
1091
1092SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1093 CTLFLAG_RW, &wall_cmos_clock, 0, "");
1094
1095/*
1096 * Initialize 386 and configure to run kernel
1097 */
1098
1099/*
1100 * Initialize segments & interrupt table
1101 */
1102
1103int _default_ldt;
1104#ifdef SMP
1105union descriptor gdt[NGDT * NCPU]; /* global descriptor table */
1106#else
1107union descriptor gdt[NGDT]; /* global descriptor table */
1108#endif
1109static struct gate_descriptor idt0[NIDT];
1110struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1111union descriptor ldt[NLDT]; /* local descriptor table */
1112#ifdef SMP
1113/* table descriptors - used to load tables by microp */
1114struct region_descriptor r_gdt, r_idt;
1115#endif
1116
1117#ifndef SMP
1118extern struct segment_descriptor common_tssd, *tss_gdt;
1119#endif
1120int private_tss; /* flag indicating private tss */
1121
1122#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1123extern int has_f00f_bug;
1124#endif
1125
1126static struct i386tss dblfault_tss;
1127static char dblfault_stack[PAGE_SIZE];
1128
1129extern struct user *proc0paddr;
1130
1131
1132/* software prototypes -- in more palatable form */
1133struct soft_segment_descriptor gdt_segs[] = {
1134/* GNULL_SEL 0 Null Descriptor */
1135{ 0x0, /* segment base address */
1136 0x0, /* length */
1137 0, /* segment type */
1138 0, /* segment descriptor priority level */
1139 0, /* segment descriptor present */
1140 0, 0,
1141 0, /* default 32 vs 16 bit size */
1142 0 /* limit granularity (byte/page units)*/ },
1143/* GCODE_SEL 1 Code Descriptor for kernel */
1144{ 0x0, /* segment base address */
1145 0xfffff, /* length - all address space */
1146 SDT_MEMERA, /* segment type */
1147 0, /* segment descriptor priority level */
1148 1, /* segment descriptor present */
1149 0, 0,
1150 1, /* default 32 vs 16 bit size */
1151 1 /* limit granularity (byte/page units)*/ },
1152/* GDATA_SEL 2 Data Descriptor for kernel */
1153{ 0x0, /* segment base address */
1154 0xfffff, /* length - all address space */
1155 SDT_MEMRWA, /* segment type */
1156 0, /* segment descriptor priority level */
1157 1, /* segment descriptor present */
1158 0, 0,
1159 1, /* default 32 vs 16 bit size */
1160 1 /* limit granularity (byte/page units)*/ },
1161/* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
1162{ 0x0, /* segment base address */
1163 0xfffff, /* length - all address space */
1164 SDT_MEMRWA, /* segment type */
1165 0, /* segment descriptor priority level */
1166 1, /* segment descriptor present */
1167 0, 0,
1168 1, /* default 32 vs 16 bit size */
1169 1 /* limit granularity (byte/page units)*/ },
1170/* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1171{
1172 0x0, /* segment base address */
1173 sizeof(struct i386tss)-1,/* length - all address space */
1174 SDT_SYS386TSS, /* segment type */
1175 0, /* segment descriptor priority level */
1176 1, /* segment descriptor present */
1177 0, 0,
1178 0, /* unused - default 32 vs 16 bit size */
1179 0 /* limit granularity (byte/page units)*/ },
1180/* GLDT_SEL 5 LDT Descriptor */
1181{ (int) ldt, /* segment base address */
1182 sizeof(ldt)-1, /* length - all address space */
1183 SDT_SYSLDT, /* segment type */
1184 SEL_UPL, /* segment descriptor priority level */
1185 1, /* segment descriptor present */
1186 0, 0,
1187 0, /* unused - default 32 vs 16 bit size */
1188 0 /* limit granularity (byte/page units)*/ },
1189/* GUSERLDT_SEL 6 User LDT Descriptor per process */
1190{ (int) ldt, /* segment base address */
1191 (512 * sizeof(union descriptor)-1), /* length */
1192 SDT_SYSLDT, /* segment type */
1193 0, /* segment descriptor priority level */
1194 1, /* segment descriptor present */
1195 0, 0,
1196 0, /* unused - default 32 vs 16 bit size */
1197 0 /* limit granularity (byte/page units)*/ },
1198/* GTGATE_SEL 7 Null Descriptor - Placeholder */
1199{ 0x0, /* segment base address */
1200 0x0, /* length - all address space */
1201 0, /* segment type */
1202 0, /* segment descriptor priority level */
1203 0, /* segment descriptor present */
1204 0, 0,
1205 0, /* default 32 vs 16 bit size */
1206 0 /* limit granularity (byte/page units)*/ },
1207/* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1208{ 0x400, /* segment base address */
1209 0xfffff, /* length */
1210 SDT_MEMRWA, /* segment type */
1211 0, /* segment descriptor priority level */
1212 1, /* segment descriptor present */
1213 0, 0,
1214 1, /* default 32 vs 16 bit size */
1215 1 /* limit granularity (byte/page units)*/ },
1216/* GPANIC_SEL 9 Panic Tss Descriptor */
1217{ (int) &dblfault_tss, /* segment base address */
1218 sizeof(struct i386tss)-1,/* length - all address space */
1219 SDT_SYS386TSS, /* segment type */
1220 0, /* segment descriptor priority level */
1221 1, /* segment descriptor present */
1222 0, 0,
1223 0, /* unused - default 32 vs 16 bit size */
1224 0 /* limit granularity (byte/page units)*/ },
1225/* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1226{ 0, /* segment base address (overwritten) */
1227 0xfffff, /* length */
1228 SDT_MEMERA, /* segment type */
1229 0, /* segment descriptor priority level */
1230 1, /* segment descriptor present */
1231 0, 0,
1232 0, /* default 32 vs 16 bit size */
1233 1 /* limit granularity (byte/page units)*/ },
1234/* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1235{ 0, /* segment base address (overwritten) */
1236 0xfffff, /* length */
1237 SDT_MEMERA, /* segment type */
1238 0, /* segment descriptor priority level */
1239 1, /* segment descriptor present */
1240 0, 0,
1241 0, /* default 32 vs 16 bit size */
1242 1 /* limit granularity (byte/page units)*/ },
1243/* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1244{ 0, /* segment base address (overwritten) */
1245 0xfffff, /* length */
1246 SDT_MEMRWA, /* segment type */
1247 0, /* segment descriptor priority level */
1248 1, /* segment descriptor present */
1249 0, 0,
1250 1, /* default 32 vs 16 bit size */
1251 1 /* limit granularity (byte/page units)*/ },
1252/* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1253{ 0, /* segment base address (overwritten) */
1254 0xfffff, /* length */
1255 SDT_MEMRWA, /* segment type */
1256 0, /* segment descriptor priority level */
1257 1, /* segment descriptor present */
1258 0, 0,
1259 0, /* default 32 vs 16 bit size */
1260 1 /* limit granularity (byte/page units)*/ },
1261/* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1262{ 0, /* segment base address (overwritten) */
1263 0xfffff, /* length */
1264 SDT_MEMRWA, /* segment type */
1265 0, /* segment descriptor priority level */
1266 1, /* segment descriptor present */
1267 0, 0,
1268 0, /* default 32 vs 16 bit size */
1269 1 /* limit granularity (byte/page units)*/ },
1270};
1271
1272static struct soft_segment_descriptor ldt_segs[] = {
1273 /* Null Descriptor - overwritten by call gate */
1274{ 0x0, /* segment base address */
1275 0x0, /* length - all address space */
1276 0, /* segment type */
1277 0, /* segment descriptor priority level */
1278 0, /* segment descriptor present */
1279 0, 0,
1280 0, /* default 32 vs 16 bit size */
1281 0 /* limit granularity (byte/page units)*/ },
1282 /* Null Descriptor - overwritten by call gate */
1283{ 0x0, /* segment base address */
1284 0x0, /* length - all address space */
1285 0, /* segment type */
1286 0, /* segment descriptor priority level */
1287 0, /* segment descriptor present */
1288 0, 0,
1289 0, /* default 32 vs 16 bit size */
1290 0 /* limit granularity (byte/page units)*/ },
1291 /* Null Descriptor - overwritten by call gate */
1292{ 0x0, /* segment base address */
1293 0x0, /* length - all address space */
1294 0, /* segment type */
1295 0, /* segment descriptor priority level */
1296 0, /* segment descriptor present */
1297 0, 0,
1298 0, /* default 32 vs 16 bit size */
1299 0 /* limit granularity (byte/page units)*/ },
1300 /* Code Descriptor for user */
1301{ 0x0, /* segment base address */
1302 0xfffff, /* length - all address space */
1303 SDT_MEMERA, /* segment type */
1304 SEL_UPL, /* segment descriptor priority level */
1305 1, /* segment descriptor present */
1306 0, 0,
1307 1, /* default 32 vs 16 bit size */
1308 1 /* limit granularity (byte/page units)*/ },
1309 /* Null Descriptor - overwritten by call gate */
1310{ 0x0, /* segment base address */
1311 0x0, /* length - all address space */
1312 0, /* segment type */
1313 0, /* segment descriptor priority level */
1314 0, /* segment descriptor present */
1315 0, 0,
1316 0, /* default 32 vs 16 bit size */
1317 0 /* limit granularity (byte/page units)*/ },
1318 /* Data Descriptor for user */
1319{ 0x0, /* segment base address */
1320 0xfffff, /* length - all address space */
1321 SDT_MEMRWA, /* segment type */
1322 SEL_UPL, /* segment descriptor priority level */
1323 1, /* segment descriptor present */
1324 0, 0,
1325 1, /* default 32 vs 16 bit size */
1326 1 /* limit granularity (byte/page units)*/ },
1327};
1328
1329void
1330setidt(idx, func, typ, dpl, selec)
1331 int idx;
1332 inthand_t *func;
1333 int typ;
1334 int dpl;
1335 int selec;
1336{
1337 struct gate_descriptor *ip;
1338
1339 ip = idt + idx;
1340 ip->gd_looffset = (int)func;
1341 ip->gd_selector = selec;
1342 ip->gd_stkcpy = 0;
1343 ip->gd_xx = 0;
1344 ip->gd_type = typ;
1345 ip->gd_dpl = dpl;
1346 ip->gd_p = 1;
1347 ip->gd_hioffset = ((int)func)>>16 ;
1348}
1349
1350#define IDTVEC(name) __CONCAT(X,name)
1351
1352extern inthand_t
1353 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1354 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1355 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1356 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1357 IDTVEC(syscall), IDTVEC(int0x80_syscall);
1358
1359void
1360sdtossd(sd, ssd)
1361 struct segment_descriptor *sd;
1362 struct soft_segment_descriptor *ssd;
1363{
1364 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1365 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1366 ssd->ssd_type = sd->sd_type;
1367 ssd->ssd_dpl = sd->sd_dpl;
1368 ssd->ssd_p = sd->sd_p;
1369 ssd->ssd_def32 = sd->sd_def32;
1370 ssd->ssd_gran = sd->sd_gran;
1371}
1372
1373#define PHYSMAP_SIZE (2 * 8)
1374
1375/*
1376 * Populate the (physmap) array with base/bound pairs describing the
1377 * available physical memory in the system, then test this memory and
1378 * build the phys_avail array describing the actually-available memory.
1379 *
1380 * If we cannot accurately determine the physical memory map, then use
1381 * value from the 0xE801 call, and failing that, the RTC.
1382 *
1383 * Total memory size may be set by the kernel environment variable
1384 * hw.physmem or the compile-time define MAXMEM.
1385 */
1386static void
1387getmemsize(int first)
1388{
1389 int i, physmap_idx, pa_indx;
1390 u_int basemem, extmem;
1391 struct vm86frame vmf;
1392 struct vm86context vmc;
1393 vm_offset_t pa, physmap[PHYSMAP_SIZE];
1394 pt_entry_t pte;
1395 const char *cp;
1396 struct {
1397 u_int64_t base;
1398 u_int64_t length;
1399 u_int32_t type;
1400 } *smap;
1401
1402 bzero(&vmf, sizeof(struct vm86frame));
1403 bzero(physmap, sizeof(physmap));
1404
1405 /*
1406 * Perform "base memory" related probes & setup
1407 */
1408 vm86_intcall(0x12, &vmf);
1409 basemem = vmf.vmf_ax;
1410 if (basemem > 640) {
1411 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1412 basemem);
1413 basemem = 640;
1414 }
1415
1416 /*
1417 * XXX if biosbasemem is now < 640, there is a `hole'
1418 * between the end of base memory and the start of
1419 * ISA memory. The hole may be empty or it may
1420 * contain BIOS code or data. Map it read/write so
1421 * that the BIOS can write to it. (Memory from 0 to
1422 * the physical end of the kernel is mapped read-only
1423 * to begin with and then parts of it are remapped.
1424 * The parts that aren't remapped form holes that
1425 * remain read-only and are unused by the kernel.
1426 * The base memory area is below the physical end of
1427 * the kernel and right now forms a read-only hole.
1428 * The part of it from PAGE_SIZE to
1429 * (trunc_page(biosbasemem * 1024) - 1) will be
1430 * remapped and used by the kernel later.)
1431 *
1432 * This code is similar to the code used in
1433 * pmap_mapdev, but since no memory needs to be
1434 * allocated we simply change the mapping.
1435 */
1436 for (pa = trunc_page(basemem * 1024);
1437 pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1438 pte = (pt_entry_t)vtopte(pa + KERNBASE);
1439 *pte = pa | PG_RW | PG_V;
1440 }
1441
1442 /*
1443 * if basemem != 640, map pages r/w into vm86 page table so
1444 * that the bios can scribble on it.
1445 */
1446 pte = (pt_entry_t)vm86paddr;
1447 for (i = basemem / 4; i < 160; i++)
1448 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1449
1450 /*
1451 * map page 1 R/W into the kernel page table so we can use it
1452 * as a buffer. The kernel will unmap this page later.
1453 */
1454 pte = (pt_entry_t)vtopte(KERNBASE + (1 << PAGE_SHIFT));
1455 *pte = (1 << PAGE_SHIFT) | PG_RW | PG_V;
1456
1457 /*
1458 * get memory map with INT 15:E820
1459 */
1460#define SMAPSIZ sizeof(*smap)
1461#define SMAP_SIG 0x534D4150 /* 'SMAP' */
1462
1463 vmc.npages = 0;
1464 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1465 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1466
1467 physmap_idx = 0;
1468 vmf.vmf_ebx = 0;
1469 do {
1470 vmf.vmf_eax = 0xE820;
1471 vmf.vmf_edx = SMAP_SIG;
1472 vmf.vmf_ecx = SMAPSIZ;
1473 i = vm86_datacall(0x15, &vmf, &vmc);
1474 if (i || vmf.vmf_eax != SMAP_SIG)
1475 break;
1476 if (boothowto & RB_VERBOSE)
1477 printf("SMAP type=%02x base=%08x %08x len=%08x %08x\n",
1478 smap->type,
1479 *(u_int32_t *)((char *)&smap->base + 4),
1480 (u_int32_t)smap->base,
1481 *(u_int32_t *)((char *)&smap->length + 4),
1482 (u_int32_t)smap->length);
1483
1484 if (smap->type != 0x01)
1485 goto next_run;
1486
1487 if (smap->length == 0)
1488 goto next_run;
1489
1490 if (smap->base >= 0xffffffff) {
1491 printf("%uK of memory above 4GB ignored\n",
1492 (u_int)(smap->length / 1024));
1493 goto next_run;
1494 }
1495
1496 for (i = 0; i <= physmap_idx; i += 2) {
1497 if (smap->base < physmap[i + 1]) {
1498 if (boothowto & RB_VERBOSE)
1499 printf(
1500 "Overlapping or non-montonic memory region, ignoring second region\n");
1501 goto next_run;
1502 }
1503 }
1504
1505 if (smap->base == physmap[physmap_idx + 1]) {
1506 physmap[physmap_idx + 1] += smap->length;
1507 goto next_run;
1508 }
1509
1510 physmap_idx += 2;
1511 if (physmap_idx == PHYSMAP_SIZE) {
1512 printf(
1513 "Too many segments in the physical address map, giving up\n");
1514 break;
1515 }
1516 physmap[physmap_idx] = smap->base;
1517 physmap[physmap_idx + 1] = smap->base + smap->length;
1518next_run:
1519 } while (vmf.vmf_ebx != 0);
1520
1521 if (physmap[1] != 0)
1522 goto physmap_done;
1523
1524 /*
1525 * If we failed above, try memory map with INT 15:E801
1526 */
1527 vmf.vmf_ax = 0xE801;
1528 if (vm86_intcall(0x15, &vmf) == 0) {
1529 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1530 } else {
1531#if 0
1532 vmf.vmf_ah = 0x88;
1533 vm86_intcall(0x15, &vmf);
1534 extmem = vmf.vmf_ax;
1535#else
1536 /*
1537 * Prefer the RTC value for extended memory.
1538 */
1539 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1540#endif
1541 }
1542
1543 /*
1544 * Special hack for chipsets that still remap the 384k hole when
1545 * there's 16MB of memory - this really confuses people that
1546 * are trying to use bus mastering ISA controllers with the
1547 * "16MB limit"; they only have 16MB, but the remapping puts
1548 * them beyond the limit.
1549 *
1550 * If extended memory is between 15-16MB (16-17MB phys address range),
1551 * chop it to 15MB.
1552 */
1553 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1554 extmem = 15 * 1024;
1555
1556 physmap[0] = 0;
1557 physmap[1] = basemem * 1024;
1558 physmap_idx = 2;
1559 physmap[physmap_idx] = 0x100000;
1560 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1561
1562physmap_done:
1563 /*
1564 * Now, physmap contains a map of physical memory.
1565 */
1566
1567#ifdef SMP
1568 /* make hole for AP bootstrap code */
1569 physmap[1] = mp_bootaddress(physmap[1] / 1024);
1570
1571 /* look for the MP hardware - needed for apic addresses */
1572 mp_probe();
1573#endif
1574
1575 /*
1576 * Maxmem isn't the "maximum memory", it's one larger than the
1577 * highest page of the physical address space. It should be
1578 * called something like "Maxphyspage". We may adjust this
1579 * based on ``hw.physmem'' and the results of the memory test.
1580 */
1581 Maxmem = atop(physmap[physmap_idx + 1]);
1582
1583#ifdef MAXMEM
1584 Maxmem = MAXMEM / 4;
1585#endif
1586
1587 /*
1588 * hw.maxmem is a size in bytes; we also allow k, m, and g suffixes
1589 * for the appropriate modifiers. This overrides MAXMEM.
1590 */
1591 if ((cp = getenv("hw.physmem")) != NULL) {
1592 u_int64_t AllowMem, sanity;
39 */
40
41#include "apm.h"
42#include "ether.h"
43#include "npx.h"
44#include "opt_atalk.h"
45#include "opt_compat.h"
46#include "opt_cpu.h"
47#include "opt_ddb.h"
48#include "opt_inet.h"
49#include "opt_ipx.h"
50#include "opt_maxmem.h"
51#include "opt_msgbuf.h"
52#include "opt_perfmon.h"
53#include "opt_smp.h"
54#include "opt_sysvipc.h"
55#include "opt_user_ldt.h"
56#include "opt_userconfig.h"
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/sysproto.h>
61#include <sys/signalvar.h>
62#include <sys/kernel.h>
63#include <sys/linker.h>
64#include <sys/proc.h>
65#include <sys/buf.h>
66#include <sys/reboot.h>
67#include <sys/callout.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <sys/msgbuf.h>
71#include <sys/sysent.h>
72#include <sys/sysctl.h>
73#include <sys/vmmeter.h>
74#include <sys/bus.h>
75
76#ifdef SYSVSHM
77#include <sys/shm.h>
78#endif
79
80#ifdef SYSVMSG
81#include <sys/msg.h>
82#endif
83
84#ifdef SYSVSEM
85#include <sys/sem.h>
86#endif
87
88#include <vm/vm.h>
89#include <vm/vm_param.h>
90#include <sys/lock.h>
91#include <vm/vm_kern.h>
92#include <vm/vm_object.h>
93#include <vm/vm_page.h>
94#include <vm/vm_map.h>
95#include <vm/vm_pager.h>
96#include <vm/vm_extern.h>
97
98#include <sys/user.h>
99#include <sys/exec.h>
100#include <sys/cons.h>
101
102#include <ddb/ddb.h>
103
104#include <net/netisr.h>
105
106#include <machine/cpu.h>
107#include <machine/reg.h>
108#include <machine/clock.h>
109#include <machine/specialreg.h>
110#include <machine/bootinfo.h>
111#include <machine/ipl.h>
112#include <machine/md_var.h>
113#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
114#ifdef SMP
115#include <machine/smp.h>
116#include <machine/globaldata.h>
117#endif
118#ifdef PERFMON
119#include <machine/perfmon.h>
120#endif
121
122#ifdef OLD_BUS_ARCH
123#include <i386/isa/isa_device.h>
124#endif
125#include <i386/isa/intr_machdep.h>
126#include <isa/rtc.h>
127#include <machine/vm86.h>
128#include <machine/random.h>
129#include <sys/ptrace.h>
130#include <machine/sigframe.h>
131
132extern void init386 __P((int first));
133extern void dblfault_handler __P((void));
134
135extern void printcpuinfo(void); /* XXX header file */
136extern void earlysetcpuclass(void); /* same header file */
137extern void finishidentcpu(void);
138extern void panicifcpuunsupported(void);
139extern void initializecpu(void);
140
141static void cpu_startup __P((void *));
142SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
143
144static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
145
146int _udatasel, _ucodesel;
147u_int atdevbase;
148
149#if defined(SWTCH_OPTIM_STATS)
150extern int swtch_optim_stats;
151SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
152 CTLFLAG_RD, &swtch_optim_stats, 0, "");
153SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
154 CTLFLAG_RD, &tlb_flush_count, 0, "");
155#endif
156
157#ifdef PC98
158static int ispc98 = 1;
159#else
160static int ispc98 = 0;
161#endif
162SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
163
164int physmem = 0;
165int cold = 1;
166
167static int
168sysctl_hw_physmem SYSCTL_HANDLER_ARGS
169{
170 int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
171 return (error);
172}
173
174SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
175 0, 0, sysctl_hw_physmem, "I", "");
176
177static int
178sysctl_hw_usermem SYSCTL_HANDLER_ARGS
179{
180 int error = sysctl_handle_int(oidp, 0,
181 ctob(physmem - cnt.v_wire_count), req);
182 return (error);
183}
184
185SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
186 0, 0, sysctl_hw_usermem, "I", "");
187
188static int
189sysctl_hw_availpages SYSCTL_HANDLER_ARGS
190{
191 int error = sysctl_handle_int(oidp, 0,
192 i386_btop(avail_end - avail_start), req);
193 return (error);
194}
195
196SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD,
197 0, 0, sysctl_hw_availpages, "I", "");
198
199static int
200sysctl_machdep_msgbuf SYSCTL_HANDLER_ARGS
201{
202 int error;
203
204 /* Unwind the buffer, so that it's linear (possibly starting with
205 * some initial nulls).
206 */
207 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr+msgbufp->msg_bufr,
208 msgbufp->msg_size-msgbufp->msg_bufr,req);
209 if(error) return(error);
210 if(msgbufp->msg_bufr>0) {
211 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr,
212 msgbufp->msg_bufr,req);
213 }
214 return(error);
215}
216
217SYSCTL_PROC(_machdep, OID_AUTO, msgbuf, CTLTYPE_STRING|CTLFLAG_RD,
218 0, 0, sysctl_machdep_msgbuf, "A","Contents of kernel message buffer");
219
220static int msgbuf_clear;
221
222static int
223sysctl_machdep_msgbuf_clear SYSCTL_HANDLER_ARGS
224{
225 int error;
226 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
227 req);
228 if (!error && req->newptr) {
229 /* Clear the buffer and reset write pointer */
230 bzero(msgbufp->msg_ptr,msgbufp->msg_size);
231 msgbufp->msg_bufr=msgbufp->msg_bufx=0;
232 msgbuf_clear=0;
233 }
234 return (error);
235}
236
237SYSCTL_PROC(_machdep, OID_AUTO, msgbuf_clear, CTLTYPE_INT|CTLFLAG_RW,
238 &msgbuf_clear, 0, sysctl_machdep_msgbuf_clear, "I",
239 "Clear kernel message buffer");
240
241int bootverbose = 0, Maxmem = 0;
242long dumplo;
243
244vm_offset_t phys_avail[10];
245
246/* must be 2 less so 0 0 can signal end of chunks */
247#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
248
249static vm_offset_t buffer_sva, buffer_eva;
250vm_offset_t clean_sva, clean_eva;
251static vm_offset_t pager_sva, pager_eva;
252
253#define offsetof(type, member) ((size_t)(&((type *)0)->member))
254
255static void
256cpu_startup(dummy)
257 void *dummy;
258{
259 register unsigned i;
260 register caddr_t v;
261 vm_offset_t maxaddr;
262 vm_size_t size = 0;
263 int firstaddr;
264 vm_offset_t minaddr;
265
266 if (boothowto & RB_VERBOSE)
267 bootverbose++;
268
269 /*
270 * Good {morning,afternoon,evening,night}.
271 */
272 printf(version);
273 earlysetcpuclass();
274 startrtclock();
275 printcpuinfo();
276 panicifcpuunsupported();
277#ifdef PERFMON
278 perfmon_init();
279#endif
280 printf("real memory = %u (%uK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024);
281 /*
282 * Display any holes after the first chunk of extended memory.
283 */
284 if (bootverbose) {
285 int indx;
286
287 printf("Physical memory chunk(s):\n");
288 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
289 int size1 = phys_avail[indx + 1] - phys_avail[indx];
290
291 printf("0x%08x - 0x%08x, %u bytes (%u pages)\n",
292 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
293 size1 / PAGE_SIZE);
294 }
295 }
296
297 /*
298 * Calculate callout wheel size
299 */
300 for (callwheelsize = 1, callwheelbits = 0;
301 callwheelsize < ncallout;
302 callwheelsize <<= 1, ++callwheelbits)
303 ;
304 callwheelmask = callwheelsize - 1;
305
306 /*
307 * Allocate space for system data structures.
308 * The first available kernel virtual address is in "v".
309 * As pages of kernel virtual memory are allocated, "v" is incremented.
310 * As pages of memory are allocated and cleared,
311 * "firstaddr" is incremented.
312 * An index into the kernel page table corresponding to the
313 * virtual memory address maintained in "v" is kept in "mapaddr".
314 */
315
316 /*
317 * Make two passes. The first pass calculates how much memory is
318 * needed and allocates it. The second pass assigns virtual
319 * addresses to the various data structures.
320 */
321 firstaddr = 0;
322again:
323 v = (caddr_t)firstaddr;
324
325#define valloc(name, type, num) \
326 (name) = (type *)v; v = (caddr_t)((name)+(num))
327#define valloclim(name, type, num, lim) \
328 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
329
330 valloc(callout, struct callout, ncallout);
331 valloc(callwheel, struct callout_tailq, callwheelsize);
332#ifdef SYSVSHM
333 valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
334#endif
335#ifdef SYSVSEM
336 valloc(sema, struct semid_ds, seminfo.semmni);
337 valloc(sem, struct sem, seminfo.semmns);
338 /* This is pretty disgusting! */
339 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
340#endif
341#ifdef SYSVMSG
342 valloc(msgpool, char, msginfo.msgmax);
343 valloc(msgmaps, struct msgmap, msginfo.msgseg);
344 valloc(msghdrs, struct msg, msginfo.msgtql);
345 valloc(msqids, struct msqid_ds, msginfo.msgmni);
346#endif
347
348 if (nbuf == 0) {
349 nbuf = 50;
350 if (physmem > 1024)
351 nbuf += min((physmem - 1024) / 8, 2048);
352 if (physmem > 16384)
353 nbuf += (physmem - 16384) / 20;
354 }
355 nswbuf = max(min(nbuf/4, 256), 16);
356
357 valloc(swbuf, struct buf, nswbuf);
358 valloc(buf, struct buf, nbuf);
359 v = bufhashinit(v);
360
361 /*
362 * End of first pass, size has been calculated so allocate memory
363 */
364 if (firstaddr == 0) {
365 size = (vm_size_t)(v - firstaddr);
366 firstaddr = (int)kmem_alloc(kernel_map, round_page(size));
367 if (firstaddr == 0)
368 panic("startup: no room for tables");
369 goto again;
370 }
371
372 /*
373 * End of second pass, addresses have been assigned
374 */
375 if ((vm_size_t)(v - firstaddr) != size)
376 panic("startup: table size inconsistency");
377
378 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
379 (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
380 buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
381 (nbuf*BKVASIZE));
382 pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
383 (nswbuf*MAXPHYS) + pager_map_size);
384 pager_map->system_map = 1;
385 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
386 (16*(ARG_MAX+(PAGE_SIZE*3))));
387
388 /*
389 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size
390 * we use the more space efficient malloc in place of kmem_alloc.
391 */
392 {
393 vm_offset_t mb_map_size;
394
395 mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES;
396 mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE));
397 mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT);
398 bzero(mclrefcnt, mb_map_size / MCLBYTES);
399 mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
400 mb_map_size);
401 mb_map->system_map = 1;
402 }
403
404 /*
405 * Initialize callouts
406 */
407 SLIST_INIT(&callfree);
408 for (i = 0; i < ncallout; i++) {
409 callout_init(&callout[i]);
410 callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
411 SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
412 }
413
414 for (i = 0; i < callwheelsize; i++) {
415 TAILQ_INIT(&callwheel[i]);
416 }
417
418#if defined(USERCONFIG)
419 userconfig();
420 cninit(); /* the preferred console may have changed */
421#endif
422
423 printf("avail memory = %u (%uK bytes)\n", ptoa(cnt.v_free_count),
424 ptoa(cnt.v_free_count) / 1024);
425
426 /*
427 * Set up buffers, so they can be used to read disk labels.
428 */
429 bufinit();
430 vm_pager_bufferinit();
431
432#ifdef SMP
433 /*
434 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
435 */
436 mp_start(); /* fire up the APs and APICs */
437 mp_announce();
438#endif /* SMP */
439}
440
441int
442register_netisr(num, handler)
443 int num;
444 netisr_t *handler;
445{
446
447 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
448 printf("register_netisr: bad isr number: %d\n", num);
449 return (EINVAL);
450 }
451 netisrs[num] = handler;
452 return (0);
453}
454
455void
456netisr_sysinit(data)
457 void *data;
458{
459 const struct netisrtab *nit;
460
461 nit = (const struct netisrtab *)data;
462 register_netisr(nit->nit_num, nit->nit_isr);
463}
464
465/*
466 * Send an interrupt to process.
467 *
468 * Stack is set up to allow sigcode stored
469 * at top to call routine, followed by kcall
470 * to sigreturn routine below. After sigreturn
471 * resets the signal mask, the stack, and the
472 * frame pointer, it returns to the user
473 * specified pc, psl.
474 */
475static void
476osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
477{
478 register struct proc *p = curproc;
479 register struct trapframe *regs;
480 register struct osigframe *fp;
481 struct osigframe sf;
482 struct sigacts *psp = p->p_sigacts;
483 int oonstack;
484
485 regs = p->p_md.md_regs;
486 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
487
488 /* Allocate and validate space for the signal handler context. */
489 if ((p->p_flag & P_ALTSTACK) && !oonstack &&
490 SIGISMEMBER(psp->ps_sigonstack, sig)) {
491 fp = (struct osigframe *)(p->p_sigstk.ss_sp +
492 p->p_sigstk.ss_size - sizeof(struct osigframe));
493 p->p_sigstk.ss_flags |= SS_ONSTACK;
494 }
495 else
496 fp = (struct osigframe *)regs->tf_esp - 1;
497
498 /*
499 * grow() will return FALSE if the fp will not fit inside the stack
500 * and the stack can not be grown. useracc will return FALSE
501 * if access is denied.
502 */
503 if (grow_stack(p, (int)fp) == FALSE ||
504 !useracc((caddr_t)fp, sizeof(struct osigframe), VM_PROT_WRITE)) {
505 /*
506 * Process has trashed its stack; give it an illegal
507 * instruction to halt it in its tracks.
508 */
509 SIGACTION(p, SIGILL) = SIG_DFL;
510 SIGDELSET(p->p_sigignore, SIGILL);
511 SIGDELSET(p->p_sigcatch, SIGILL);
512 SIGDELSET(p->p_sigmask, SIGILL);
513 psignal(p, SIGILL);
514 return;
515 }
516
517 /* Translate the signal if appropriate */
518 if (p->p_sysent->sv_sigtbl) {
519 if (sig <= p->p_sysent->sv_sigsize)
520 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
521 }
522
523 /* Build the argument list for the signal handler. */
524 sf.sf_signum = sig;
525 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
526 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
527 /* Signal handler installed with SA_SIGINFO. */
528 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
529 sf.sf_siginfo.si_signo = sig;
530 sf.sf_siginfo.si_code = code;
531 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
532 }
533 else {
534 /* Old FreeBSD-style arguments. */
535 sf.sf_arg2 = code;
536 sf.sf_addr = (register_t *)regs->tf_err;
537 sf.sf_ahu.sf_handler = catcher;
538 }
539
540 /* save scratch registers */
541 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
542 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
543 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
544 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
545 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
546 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
547 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
548 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
549 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
550 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
551 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
552 sf.sf_siginfo.si_sc.sc_gs = rgs();
553 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
554
555 /* Build the signal context to be used by sigreturn. */
556 sf.sf_siginfo.si_sc.sc_onstack = oonstack;
557 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
558 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
559 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
560 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
561 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
562 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
563 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
564
565 /*
566 * If we're a vm86 process, we want to save the segment registers.
567 * We also change eflags to be our emulated eflags, not the actual
568 * eflags.
569 */
570 if (regs->tf_eflags & PSL_VM) {
571 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
572 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
573
574 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
575 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
576 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
577 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
578
579 if (vm86->vm86_has_vme == 0)
580 sf.sf_siginfo.si_sc.sc_ps =
581 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP))
582 | (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
583 /* see sendsig for comment */
584 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
585 }
586
587 /* Copy the sigframe out to the user's stack. */
588 if (copyout(&sf, fp, sizeof(struct osigframe)) != 0) {
589 /*
590 * Something is wrong with the stack pointer.
591 * ...Kill the process.
592 */
593 sigexit(p, SIGILL);
594 }
595
596 regs->tf_esp = (int)fp;
597 regs->tf_eip = PS_STRINGS - oszsigcode;
598 regs->tf_cs = _ucodesel;
599 regs->tf_ds = _udatasel;
600 regs->tf_es = _udatasel;
601 regs->tf_fs = _udatasel;
602 load_gs(_udatasel);
603 regs->tf_ss = _udatasel;
604}
605
606void
607sendsig(catcher, sig, mask, code)
608 sig_t catcher;
609 int sig;
610 sigset_t *mask;
611 u_long code;
612{
613 struct proc *p = curproc;
614 struct trapframe *regs;
615 struct sigacts *psp = p->p_sigacts;
616 struct sigframe sf, *sfp;
617 int oonstack;
618
619 if (SIGISMEMBER(psp->ps_osigset, sig)) {
620 osendsig(catcher, sig, mask, code);
621 return;
622 }
623
624 regs = p->p_md.md_regs;
625 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
626
627 /* save user context */
628 bzero(&sf, sizeof(struct sigframe));
629 sf.sf_uc.uc_sigmask = *mask;
630 sf.sf_uc.uc_stack = p->p_sigstk;
631 sf.sf_uc.uc_mcontext.mc_onstack = oonstack;
632 sf.sf_uc.uc_mcontext.mc_gs = rgs();
633 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(struct trapframe));
634
635 /* Allocate and validate space for the signal handler context. */
636 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
637 SIGISMEMBER(psp->ps_sigonstack, sig)) {
638 sfp = (struct sigframe *)(p->p_sigstk.ss_sp +
639 p->p_sigstk.ss_size - sizeof(struct sigframe));
640 p->p_sigstk.ss_flags |= SS_ONSTACK;
641 }
642 else
643 sfp = (struct sigframe *)regs->tf_esp - 1;
644
645 /*
646 * grow() will return FALSE if the sfp will not fit inside the stack
647 * and the stack can not be grown. useracc will return FALSE if
648 * access is denied.
649 */
650 if (grow_stack(p, (int)sfp) == FALSE ||
651 !useracc((caddr_t)sfp, sizeof(struct sigframe), VM_PROT_WRITE)) {
652 /*
653 * Process has trashed its stack; give it an illegal
654 * instruction to halt it in its tracks.
655 */
656#ifdef DEBUG
657 printf("process %d has trashed its stack\n", p->p_pid);
658#endif
659 SIGACTION(p, SIGILL) = SIG_DFL;
660 SIGDELSET(p->p_sigignore, SIGILL);
661 SIGDELSET(p->p_sigcatch, SIGILL);
662 SIGDELSET(p->p_sigmask, SIGILL);
663 psignal(p, SIGILL);
664 return;
665 }
666
667 /* Translate the signal is appropriate */
668 if (p->p_sysent->sv_sigtbl) {
669 if (sig <= p->p_sysent->sv_sigsize)
670 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
671 }
672
673 /* Build the argument list for the signal handler. */
674 sf.sf_signum = sig;
675 sf.sf_ucontext = (register_t)&sfp->sf_uc;
676 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
677 /* Signal handler installed with SA_SIGINFO. */
678 sf.sf_siginfo = (register_t)&sfp->sf_si;
679 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
680
681 /* fill siginfo structure */
682 sf.sf_si.si_signo = sig;
683 sf.sf_si.si_code = code;
684 sf.sf_si.si_addr = (void*)regs->tf_err;
685 }
686 else {
687 /* Old FreeBSD-style arguments. */
688 sf.sf_siginfo = code;
689 sf.sf_addr = (register_t *)regs->tf_err;
690 sf.sf_ahu.sf_handler = catcher;
691 }
692
693 /*
694 * If we're a vm86 process, we want to save the segment registers.
695 * We also change eflags to be our emulated eflags, not the actual
696 * eflags.
697 */
698 if (regs->tf_eflags & PSL_VM) {
699 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
700 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
701
702 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
703 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
704 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
705 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
706
707 if (vm86->vm86_has_vme == 0)
708 sf.sf_uc.uc_mcontext.mc_eflags =
709 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
710 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
711
712 /*
713 * We should never have PSL_T set when returning from vm86
714 * mode. It may be set here if we deliver a signal before
715 * getting to vm86 mode, so turn it off.
716 *
717 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
718 * syscalls made by the signal handler. This just avoids
719 * wasting time for our lazy fixup of such faults. PSL_NT
720 * does nothing in vm86 mode, but vm86 programs can set it
721 * almost legitimately in probes for old cpu types.
722 */
723 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
724 }
725
726 /*
727 * Copy the sigframe out to the user's stack.
728 */
729 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
730 /*
731 * Something is wrong with the stack pointer.
732 * ...Kill the process.
733 */
734 sigexit(p, SIGILL);
735 }
736
737 regs->tf_esp = (int)sfp;
738 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
739 regs->tf_cs = _ucodesel;
740 regs->tf_ds = _udatasel;
741 regs->tf_es = _udatasel;
742 regs->tf_fs = _udatasel;
743 load_gs(_udatasel);
744 regs->tf_ss = _udatasel;
745}
746
747/*
748 * System call to cleanup state after a signal
749 * has been taken. Reset signal mask and
750 * stack state from context left by sendsig (above).
751 * Return to previous pc and psl as specified by
752 * context left by sendsig. Check carefully to
753 * make sure that the user has not modified the
754 * state to gain improper privileges.
755 */
756#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
757#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
758
759int
760osigreturn(p, uap)
761 struct proc *p;
762 struct osigreturn_args /* {
763 struct osigcontext *sigcntxp;
764 } */ *uap;
765{
766 register struct osigcontext *scp;
767 register struct trapframe *regs = p->p_md.md_regs;
768 int eflags;
769
770 scp = uap->sigcntxp;
771
772 if (!useracc((caddr_t)scp, sizeof (struct osigcontext), VM_PROT_READ))
773 return(EFAULT);
774
775 eflags = scp->sc_ps;
776 if (eflags & PSL_VM) {
777 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
778 struct vm86_kernel *vm86;
779
780 /*
781 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
782 * set up the vm86 area, and we can't enter vm86 mode.
783 */
784 if (p->p_addr->u_pcb.pcb_ext == 0)
785 return (EINVAL);
786 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
787 if (vm86->vm86_inited == 0)
788 return (EINVAL);
789
790 /* go back to user mode if both flags are set */
791 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
792 trapsignal(p, SIGBUS, 0);
793
794 if (vm86->vm86_has_vme) {
795 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
796 (eflags & VME_USERCHANGE) | PSL_VM;
797 } else {
798 vm86->vm86_eflags = eflags; /* save VIF, VIP */
799 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
800 }
801 tf->tf_vm86_ds = scp->sc_ds;
802 tf->tf_vm86_es = scp->sc_es;
803 tf->tf_vm86_fs = scp->sc_fs;
804 tf->tf_vm86_gs = scp->sc_gs;
805 tf->tf_ds = _udatasel;
806 tf->tf_es = _udatasel;
807 tf->tf_fs = _udatasel;
808 } else {
809 /*
810 * Don't allow users to change privileged or reserved flags.
811 */
812 /*
813 * XXX do allow users to change the privileged flag PSL_RF.
814 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
815 * should sometimes set it there too. tf_eflags is kept in
816 * the signal context during signal handling and there is no
817 * other place to remember it, so the PSL_RF bit may be
818 * corrupted by the signal handler without us knowing.
819 * Corruption of the PSL_RF bit at worst causes one more or
820 * one less debugger trap, so allowing it is fairly harmless.
821 */
822 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
823 return(EINVAL);
824 }
825
826 /*
827 * Don't allow users to load a valid privileged %cs. Let the
828 * hardware check for invalid selectors, excess privilege in
829 * other selectors, invalid %eip's and invalid %esp's.
830 */
831 if (!CS_SECURE(scp->sc_cs)) {
832 trapsignal(p, SIGBUS, T_PROTFLT);
833 return(EINVAL);
834 }
835 regs->tf_ds = scp->sc_ds;
836 regs->tf_es = scp->sc_es;
837 regs->tf_fs = scp->sc_fs;
838 }
839
840 /* restore scratch registers */
841 regs->tf_eax = scp->sc_eax;
842 regs->tf_ebx = scp->sc_ebx;
843 regs->tf_ecx = scp->sc_ecx;
844 regs->tf_edx = scp->sc_edx;
845 regs->tf_esi = scp->sc_esi;
846 regs->tf_edi = scp->sc_edi;
847 regs->tf_cs = scp->sc_cs;
848 regs->tf_ss = scp->sc_ss;
849 regs->tf_isp = scp->sc_isp;
850
851 if (scp->sc_onstack & 01)
852 p->p_sigstk.ss_flags |= SS_ONSTACK;
853 else
854 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
855
856 SIGSETOLD(p->p_sigmask, scp->sc_mask);
857 SIG_CANTMASK(p->p_sigmask);
858 regs->tf_ebp = scp->sc_fp;
859 regs->tf_esp = scp->sc_sp;
860 regs->tf_eip = scp->sc_pc;
861 regs->tf_eflags = eflags;
862 return(EJUSTRETURN);
863}
864
865int
866sigreturn(p, uap)
867 struct proc *p;
868 struct sigreturn_args /* {
869 ucontext_t *sigcntxp;
870 } */ *uap;
871{
872 struct trapframe *regs;
873 ucontext_t *ucp;
874 int cs, eflags;
875
876 ucp = uap->sigcntxp;
877
878 if (!useracc((caddr_t)ucp, sizeof(struct osigcontext), VM_PROT_READ))
879 return (EFAULT);
880 if (((struct osigcontext *)ucp)->sc_trapno == 0x01d516)
881 return (osigreturn(p, (struct osigreturn_args *)uap));
882
883 /*
884 * Since ucp is not an osigcontext but a ucontext_t, we have to
885 * check again if all of it is accessible. A ucontext_t is
886 * much larger, so instead of just checking for the pointer
887 * being valid for the size of an osigcontext, now check for
888 * it being valid for a whole, new-style ucontext_t.
889 */
890 if (!useracc((caddr_t)ucp, sizeof(ucontext_t), VM_PROT_READ))
891 return (EFAULT);
892
893 regs = p->p_md.md_regs;
894 eflags = ucp->uc_mcontext.mc_eflags;
895
896 if (eflags & PSL_VM) {
897 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
898 struct vm86_kernel *vm86;
899
900 /*
901 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
902 * set up the vm86 area, and we can't enter vm86 mode.
903 */
904 if (p->p_addr->u_pcb.pcb_ext == 0)
905 return (EINVAL);
906 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
907 if (vm86->vm86_inited == 0)
908 return (EINVAL);
909
910 /* go back to user mode if both flags are set */
911 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
912 trapsignal(p, SIGBUS, 0);
913
914 if (vm86->vm86_has_vme) {
915 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
916 (eflags & VME_USERCHANGE) | PSL_VM;
917 } else {
918 vm86->vm86_eflags = eflags; /* save VIF, VIP */
919 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
920 }
921 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
922 tf->tf_eflags = eflags;
923 tf->tf_vm86_ds = tf->tf_ds;
924 tf->tf_vm86_es = tf->tf_es;
925 tf->tf_vm86_fs = tf->tf_fs;
926 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
927 tf->tf_ds = _udatasel;
928 tf->tf_es = _udatasel;
929 tf->tf_fs = _udatasel;
930 } else {
931 /*
932 * Don't allow users to change privileged or reserved flags.
933 */
934 /*
935 * XXX do allow users to change the privileged flag PSL_RF.
936 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
937 * should sometimes set it there too. tf_eflags is kept in
938 * the signal context during signal handling and there is no
939 * other place to remember it, so the PSL_RF bit may be
940 * corrupted by the signal handler without us knowing.
941 * Corruption of the PSL_RF bit at worst causes one more or
942 * one less debugger trap, so allowing it is fairly harmless.
943 */
944 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
945 printf("sigreturn: eflags = 0x%x\n", eflags);
946 return(EINVAL);
947 }
948
949 /*
950 * Don't allow users to load a valid privileged %cs. Let the
951 * hardware check for invalid selectors, excess privilege in
952 * other selectors, invalid %eip's and invalid %esp's.
953 */
954 cs = ucp->uc_mcontext.mc_cs;
955 if (!CS_SECURE(cs)) {
956 printf("sigreturn: cs = 0x%x\n", cs);
957 trapsignal(p, SIGBUS, T_PROTFLT);
958 return(EINVAL);
959 }
960 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(struct trapframe));
961 }
962
963 if (ucp->uc_mcontext.mc_onstack & 1)
964 p->p_sigstk.ss_flags |= SS_ONSTACK;
965 else
966 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
967
968 p->p_sigmask = ucp->uc_sigmask;
969 SIG_CANTMASK(p->p_sigmask);
970 return(EJUSTRETURN);
971}
972
973/*
974 * Machine dependent boot() routine
975 *
976 * I haven't seen anything to put here yet
977 * Possibly some stuff might be grafted back here from boot()
978 */
979void
980cpu_boot(int howto)
981{
982}
983
984/*
985 * Shutdown the CPU as much as possible
986 */
987void
988cpu_halt(void)
989{
990 for (;;)
991 __asm__ ("hlt");
992}
993
994/*
995 * Clear registers on exec
996 */
997void
998setregs(p, entry, stack, ps_strings)
999 struct proc *p;
1000 u_long entry;
1001 u_long stack;
1002 u_long ps_strings;
1003{
1004 struct trapframe *regs = p->p_md.md_regs;
1005 struct pcb *pcb = &p->p_addr->u_pcb;
1006
1007#ifdef USER_LDT
1008 /* was i386_user_cleanup() in NetBSD */
1009 if (pcb->pcb_ldt) {
1010 if (pcb == curpcb) {
1011 lldt(_default_ldt);
1012 currentldt = _default_ldt;
1013 }
1014 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
1015 pcb->pcb_ldt_len * sizeof(union descriptor));
1016 pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
1017 }
1018#endif
1019
1020 bzero((char *)regs, sizeof(struct trapframe));
1021 regs->tf_eip = entry;
1022 regs->tf_esp = stack;
1023 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1024 regs->tf_ss = _udatasel;
1025 regs->tf_ds = _udatasel;
1026 regs->tf_es = _udatasel;
1027 regs->tf_fs = _udatasel;
1028 regs->tf_cs = _ucodesel;
1029
1030 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1031 regs->tf_ebx = ps_strings;
1032
1033 /* reset %gs as well */
1034 if (pcb == curpcb)
1035 load_gs(_udatasel);
1036 else
1037 pcb->pcb_gs = _udatasel;
1038
1039 /*
1040 * Initialize the math emulator (if any) for the current process.
1041 * Actually, just clear the bit that says that the emulator has
1042 * been initialized. Initialization is delayed until the process
1043 * traps to the emulator (if it is done at all) mainly because
1044 * emulators don't provide an entry point for initialization.
1045 */
1046 p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP;
1047
1048 /*
1049 * Arrange to trap the next npx or `fwait' instruction (see npx.c
1050 * for why fwait must be trapped at least if there is an npx or an
1051 * emulator). This is mainly to handle the case where npx0 is not
1052 * configured, since the npx routines normally set up the trap
1053 * otherwise. It should be done only at boot time, but doing it
1054 * here allows modifying `npx_exists' for testing the emulator on
1055 * systems with an npx.
1056 */
1057 load_cr0(rcr0() | CR0_MP | CR0_TS);
1058
1059#if NNPX > 0
1060 /* Initialize the npx (if any) for the current process. */
1061 npxinit(__INITIAL_NPXCW__);
1062#endif
1063
1064 /*
1065 * XXX - Linux emulator
1066 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1067 * on it.
1068 */
1069 p->p_retval[1] = 0;
1070}
1071
1072static int
1073sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
1074{
1075 int error;
1076 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1077 req);
1078 if (!error && req->newptr)
1079 resettodr();
1080 return (error);
1081}
1082
1083SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1084 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1085
1086SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1087 CTLFLAG_RW, &disable_rtc_set, 0, "");
1088
1089SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
1090 CTLFLAG_RD, &bootinfo, bootinfo, "");
1091
1092SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1093 CTLFLAG_RW, &wall_cmos_clock, 0, "");
1094
1095/*
1096 * Initialize 386 and configure to run kernel
1097 */
1098
1099/*
1100 * Initialize segments & interrupt table
1101 */
1102
1103int _default_ldt;
1104#ifdef SMP
1105union descriptor gdt[NGDT * NCPU]; /* global descriptor table */
1106#else
1107union descriptor gdt[NGDT]; /* global descriptor table */
1108#endif
1109static struct gate_descriptor idt0[NIDT];
1110struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1111union descriptor ldt[NLDT]; /* local descriptor table */
1112#ifdef SMP
1113/* table descriptors - used to load tables by microp */
1114struct region_descriptor r_gdt, r_idt;
1115#endif
1116
1117#ifndef SMP
1118extern struct segment_descriptor common_tssd, *tss_gdt;
1119#endif
1120int private_tss; /* flag indicating private tss */
1121
1122#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1123extern int has_f00f_bug;
1124#endif
1125
1126static struct i386tss dblfault_tss;
1127static char dblfault_stack[PAGE_SIZE];
1128
1129extern struct user *proc0paddr;
1130
1131
1132/* software prototypes -- in more palatable form */
1133struct soft_segment_descriptor gdt_segs[] = {
1134/* GNULL_SEL 0 Null Descriptor */
1135{ 0x0, /* segment base address */
1136 0x0, /* length */
1137 0, /* segment type */
1138 0, /* segment descriptor priority level */
1139 0, /* segment descriptor present */
1140 0, 0,
1141 0, /* default 32 vs 16 bit size */
1142 0 /* limit granularity (byte/page units)*/ },
1143/* GCODE_SEL 1 Code Descriptor for kernel */
1144{ 0x0, /* segment base address */
1145 0xfffff, /* length - all address space */
1146 SDT_MEMERA, /* segment type */
1147 0, /* segment descriptor priority level */
1148 1, /* segment descriptor present */
1149 0, 0,
1150 1, /* default 32 vs 16 bit size */
1151 1 /* limit granularity (byte/page units)*/ },
1152/* GDATA_SEL 2 Data Descriptor for kernel */
1153{ 0x0, /* segment base address */
1154 0xfffff, /* length - all address space */
1155 SDT_MEMRWA, /* segment type */
1156 0, /* segment descriptor priority level */
1157 1, /* segment descriptor present */
1158 0, 0,
1159 1, /* default 32 vs 16 bit size */
1160 1 /* limit granularity (byte/page units)*/ },
1161/* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
1162{ 0x0, /* segment base address */
1163 0xfffff, /* length - all address space */
1164 SDT_MEMRWA, /* segment type */
1165 0, /* segment descriptor priority level */
1166 1, /* segment descriptor present */
1167 0, 0,
1168 1, /* default 32 vs 16 bit size */
1169 1 /* limit granularity (byte/page units)*/ },
1170/* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1171{
1172 0x0, /* segment base address */
1173 sizeof(struct i386tss)-1,/* length - all address space */
1174 SDT_SYS386TSS, /* segment type */
1175 0, /* segment descriptor priority level */
1176 1, /* segment descriptor present */
1177 0, 0,
1178 0, /* unused - default 32 vs 16 bit size */
1179 0 /* limit granularity (byte/page units)*/ },
1180/* GLDT_SEL 5 LDT Descriptor */
1181{ (int) ldt, /* segment base address */
1182 sizeof(ldt)-1, /* length - all address space */
1183 SDT_SYSLDT, /* segment type */
1184 SEL_UPL, /* segment descriptor priority level */
1185 1, /* segment descriptor present */
1186 0, 0,
1187 0, /* unused - default 32 vs 16 bit size */
1188 0 /* limit granularity (byte/page units)*/ },
1189/* GUSERLDT_SEL 6 User LDT Descriptor per process */
1190{ (int) ldt, /* segment base address */
1191 (512 * sizeof(union descriptor)-1), /* length */
1192 SDT_SYSLDT, /* segment type */
1193 0, /* segment descriptor priority level */
1194 1, /* segment descriptor present */
1195 0, 0,
1196 0, /* unused - default 32 vs 16 bit size */
1197 0 /* limit granularity (byte/page units)*/ },
1198/* GTGATE_SEL 7 Null Descriptor - Placeholder */
1199{ 0x0, /* segment base address */
1200 0x0, /* length - all address space */
1201 0, /* segment type */
1202 0, /* segment descriptor priority level */
1203 0, /* segment descriptor present */
1204 0, 0,
1205 0, /* default 32 vs 16 bit size */
1206 0 /* limit granularity (byte/page units)*/ },
1207/* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1208{ 0x400, /* segment base address */
1209 0xfffff, /* length */
1210 SDT_MEMRWA, /* segment type */
1211 0, /* segment descriptor priority level */
1212 1, /* segment descriptor present */
1213 0, 0,
1214 1, /* default 32 vs 16 bit size */
1215 1 /* limit granularity (byte/page units)*/ },
1216/* GPANIC_SEL 9 Panic Tss Descriptor */
1217{ (int) &dblfault_tss, /* segment base address */
1218 sizeof(struct i386tss)-1,/* length - all address space */
1219 SDT_SYS386TSS, /* segment type */
1220 0, /* segment descriptor priority level */
1221 1, /* segment descriptor present */
1222 0, 0,
1223 0, /* unused - default 32 vs 16 bit size */
1224 0 /* limit granularity (byte/page units)*/ },
1225/* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1226{ 0, /* segment base address (overwritten) */
1227 0xfffff, /* length */
1228 SDT_MEMERA, /* segment type */
1229 0, /* segment descriptor priority level */
1230 1, /* segment descriptor present */
1231 0, 0,
1232 0, /* default 32 vs 16 bit size */
1233 1 /* limit granularity (byte/page units)*/ },
1234/* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1235{ 0, /* segment base address (overwritten) */
1236 0xfffff, /* length */
1237 SDT_MEMERA, /* segment type */
1238 0, /* segment descriptor priority level */
1239 1, /* segment descriptor present */
1240 0, 0,
1241 0, /* default 32 vs 16 bit size */
1242 1 /* limit granularity (byte/page units)*/ },
1243/* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1244{ 0, /* segment base address (overwritten) */
1245 0xfffff, /* length */
1246 SDT_MEMRWA, /* segment type */
1247 0, /* segment descriptor priority level */
1248 1, /* segment descriptor present */
1249 0, 0,
1250 1, /* default 32 vs 16 bit size */
1251 1 /* limit granularity (byte/page units)*/ },
1252/* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1253{ 0, /* segment base address (overwritten) */
1254 0xfffff, /* length */
1255 SDT_MEMRWA, /* segment type */
1256 0, /* segment descriptor priority level */
1257 1, /* segment descriptor present */
1258 0, 0,
1259 0, /* default 32 vs 16 bit size */
1260 1 /* limit granularity (byte/page units)*/ },
1261/* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1262{ 0, /* segment base address (overwritten) */
1263 0xfffff, /* length */
1264 SDT_MEMRWA, /* segment type */
1265 0, /* segment descriptor priority level */
1266 1, /* segment descriptor present */
1267 0, 0,
1268 0, /* default 32 vs 16 bit size */
1269 1 /* limit granularity (byte/page units)*/ },
1270};
1271
1272static struct soft_segment_descriptor ldt_segs[] = {
1273 /* Null Descriptor - overwritten by call gate */
1274{ 0x0, /* segment base address */
1275 0x0, /* length - all address space */
1276 0, /* segment type */
1277 0, /* segment descriptor priority level */
1278 0, /* segment descriptor present */
1279 0, 0,
1280 0, /* default 32 vs 16 bit size */
1281 0 /* limit granularity (byte/page units)*/ },
1282 /* Null Descriptor - overwritten by call gate */
1283{ 0x0, /* segment base address */
1284 0x0, /* length - all address space */
1285 0, /* segment type */
1286 0, /* segment descriptor priority level */
1287 0, /* segment descriptor present */
1288 0, 0,
1289 0, /* default 32 vs 16 bit size */
1290 0 /* limit granularity (byte/page units)*/ },
1291 /* Null Descriptor - overwritten by call gate */
1292{ 0x0, /* segment base address */
1293 0x0, /* length - all address space */
1294 0, /* segment type */
1295 0, /* segment descriptor priority level */
1296 0, /* segment descriptor present */
1297 0, 0,
1298 0, /* default 32 vs 16 bit size */
1299 0 /* limit granularity (byte/page units)*/ },
1300 /* Code Descriptor for user */
1301{ 0x0, /* segment base address */
1302 0xfffff, /* length - all address space */
1303 SDT_MEMERA, /* segment type */
1304 SEL_UPL, /* segment descriptor priority level */
1305 1, /* segment descriptor present */
1306 0, 0,
1307 1, /* default 32 vs 16 bit size */
1308 1 /* limit granularity (byte/page units)*/ },
1309 /* Null Descriptor - overwritten by call gate */
1310{ 0x0, /* segment base address */
1311 0x0, /* length - all address space */
1312 0, /* segment type */
1313 0, /* segment descriptor priority level */
1314 0, /* segment descriptor present */
1315 0, 0,
1316 0, /* default 32 vs 16 bit size */
1317 0 /* limit granularity (byte/page units)*/ },
1318 /* Data Descriptor for user */
1319{ 0x0, /* segment base address */
1320 0xfffff, /* length - all address space */
1321 SDT_MEMRWA, /* segment type */
1322 SEL_UPL, /* segment descriptor priority level */
1323 1, /* segment descriptor present */
1324 0, 0,
1325 1, /* default 32 vs 16 bit size */
1326 1 /* limit granularity (byte/page units)*/ },
1327};
1328
1329void
1330setidt(idx, func, typ, dpl, selec)
1331 int idx;
1332 inthand_t *func;
1333 int typ;
1334 int dpl;
1335 int selec;
1336{
1337 struct gate_descriptor *ip;
1338
1339 ip = idt + idx;
1340 ip->gd_looffset = (int)func;
1341 ip->gd_selector = selec;
1342 ip->gd_stkcpy = 0;
1343 ip->gd_xx = 0;
1344 ip->gd_type = typ;
1345 ip->gd_dpl = dpl;
1346 ip->gd_p = 1;
1347 ip->gd_hioffset = ((int)func)>>16 ;
1348}
1349
1350#define IDTVEC(name) __CONCAT(X,name)
1351
1352extern inthand_t
1353 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1354 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1355 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1356 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1357 IDTVEC(syscall), IDTVEC(int0x80_syscall);
1358
1359void
1360sdtossd(sd, ssd)
1361 struct segment_descriptor *sd;
1362 struct soft_segment_descriptor *ssd;
1363{
1364 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1365 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1366 ssd->ssd_type = sd->sd_type;
1367 ssd->ssd_dpl = sd->sd_dpl;
1368 ssd->ssd_p = sd->sd_p;
1369 ssd->ssd_def32 = sd->sd_def32;
1370 ssd->ssd_gran = sd->sd_gran;
1371}
1372
1373#define PHYSMAP_SIZE (2 * 8)
1374
1375/*
1376 * Populate the (physmap) array with base/bound pairs describing the
1377 * available physical memory in the system, then test this memory and
1378 * build the phys_avail array describing the actually-available memory.
1379 *
1380 * If we cannot accurately determine the physical memory map, then use
1381 * value from the 0xE801 call, and failing that, the RTC.
1382 *
1383 * Total memory size may be set by the kernel environment variable
1384 * hw.physmem or the compile-time define MAXMEM.
1385 */
1386static void
1387getmemsize(int first)
1388{
1389 int i, physmap_idx, pa_indx;
1390 u_int basemem, extmem;
1391 struct vm86frame vmf;
1392 struct vm86context vmc;
1393 vm_offset_t pa, physmap[PHYSMAP_SIZE];
1394 pt_entry_t pte;
1395 const char *cp;
1396 struct {
1397 u_int64_t base;
1398 u_int64_t length;
1399 u_int32_t type;
1400 } *smap;
1401
1402 bzero(&vmf, sizeof(struct vm86frame));
1403 bzero(physmap, sizeof(physmap));
1404
1405 /*
1406 * Perform "base memory" related probes & setup
1407 */
1408 vm86_intcall(0x12, &vmf);
1409 basemem = vmf.vmf_ax;
1410 if (basemem > 640) {
1411 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1412 basemem);
1413 basemem = 640;
1414 }
1415
1416 /*
1417 * XXX if biosbasemem is now < 640, there is a `hole'
1418 * between the end of base memory and the start of
1419 * ISA memory. The hole may be empty or it may
1420 * contain BIOS code or data. Map it read/write so
1421 * that the BIOS can write to it. (Memory from 0 to
1422 * the physical end of the kernel is mapped read-only
1423 * to begin with and then parts of it are remapped.
1424 * The parts that aren't remapped form holes that
1425 * remain read-only and are unused by the kernel.
1426 * The base memory area is below the physical end of
1427 * the kernel and right now forms a read-only hole.
1428 * The part of it from PAGE_SIZE to
1429 * (trunc_page(biosbasemem * 1024) - 1) will be
1430 * remapped and used by the kernel later.)
1431 *
1432 * This code is similar to the code used in
1433 * pmap_mapdev, but since no memory needs to be
1434 * allocated we simply change the mapping.
1435 */
1436 for (pa = trunc_page(basemem * 1024);
1437 pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1438 pte = (pt_entry_t)vtopte(pa + KERNBASE);
1439 *pte = pa | PG_RW | PG_V;
1440 }
1441
1442 /*
1443 * if basemem != 640, map pages r/w into vm86 page table so
1444 * that the bios can scribble on it.
1445 */
1446 pte = (pt_entry_t)vm86paddr;
1447 for (i = basemem / 4; i < 160; i++)
1448 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1449
1450 /*
1451 * map page 1 R/W into the kernel page table so we can use it
1452 * as a buffer. The kernel will unmap this page later.
1453 */
1454 pte = (pt_entry_t)vtopte(KERNBASE + (1 << PAGE_SHIFT));
1455 *pte = (1 << PAGE_SHIFT) | PG_RW | PG_V;
1456
1457 /*
1458 * get memory map with INT 15:E820
1459 */
1460#define SMAPSIZ sizeof(*smap)
1461#define SMAP_SIG 0x534D4150 /* 'SMAP' */
1462
1463 vmc.npages = 0;
1464 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1465 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1466
1467 physmap_idx = 0;
1468 vmf.vmf_ebx = 0;
1469 do {
1470 vmf.vmf_eax = 0xE820;
1471 vmf.vmf_edx = SMAP_SIG;
1472 vmf.vmf_ecx = SMAPSIZ;
1473 i = vm86_datacall(0x15, &vmf, &vmc);
1474 if (i || vmf.vmf_eax != SMAP_SIG)
1475 break;
1476 if (boothowto & RB_VERBOSE)
1477 printf("SMAP type=%02x base=%08x %08x len=%08x %08x\n",
1478 smap->type,
1479 *(u_int32_t *)((char *)&smap->base + 4),
1480 (u_int32_t)smap->base,
1481 *(u_int32_t *)((char *)&smap->length + 4),
1482 (u_int32_t)smap->length);
1483
1484 if (smap->type != 0x01)
1485 goto next_run;
1486
1487 if (smap->length == 0)
1488 goto next_run;
1489
1490 if (smap->base >= 0xffffffff) {
1491 printf("%uK of memory above 4GB ignored\n",
1492 (u_int)(smap->length / 1024));
1493 goto next_run;
1494 }
1495
1496 for (i = 0; i <= physmap_idx; i += 2) {
1497 if (smap->base < physmap[i + 1]) {
1498 if (boothowto & RB_VERBOSE)
1499 printf(
1500 "Overlapping or non-montonic memory region, ignoring second region\n");
1501 goto next_run;
1502 }
1503 }
1504
1505 if (smap->base == physmap[physmap_idx + 1]) {
1506 physmap[physmap_idx + 1] += smap->length;
1507 goto next_run;
1508 }
1509
1510 physmap_idx += 2;
1511 if (physmap_idx == PHYSMAP_SIZE) {
1512 printf(
1513 "Too many segments in the physical address map, giving up\n");
1514 break;
1515 }
1516 physmap[physmap_idx] = smap->base;
1517 physmap[physmap_idx + 1] = smap->base + smap->length;
1518next_run:
1519 } while (vmf.vmf_ebx != 0);
1520
1521 if (physmap[1] != 0)
1522 goto physmap_done;
1523
1524 /*
1525 * If we failed above, try memory map with INT 15:E801
1526 */
1527 vmf.vmf_ax = 0xE801;
1528 if (vm86_intcall(0x15, &vmf) == 0) {
1529 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1530 } else {
1531#if 0
1532 vmf.vmf_ah = 0x88;
1533 vm86_intcall(0x15, &vmf);
1534 extmem = vmf.vmf_ax;
1535#else
1536 /*
1537 * Prefer the RTC value for extended memory.
1538 */
1539 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1540#endif
1541 }
1542
1543 /*
1544 * Special hack for chipsets that still remap the 384k hole when
1545 * there's 16MB of memory - this really confuses people that
1546 * are trying to use bus mastering ISA controllers with the
1547 * "16MB limit"; they only have 16MB, but the remapping puts
1548 * them beyond the limit.
1549 *
1550 * If extended memory is between 15-16MB (16-17MB phys address range),
1551 * chop it to 15MB.
1552 */
1553 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1554 extmem = 15 * 1024;
1555
1556 physmap[0] = 0;
1557 physmap[1] = basemem * 1024;
1558 physmap_idx = 2;
1559 physmap[physmap_idx] = 0x100000;
1560 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1561
1562physmap_done:
1563 /*
1564 * Now, physmap contains a map of physical memory.
1565 */
1566
1567#ifdef SMP
1568 /* make hole for AP bootstrap code */
1569 physmap[1] = mp_bootaddress(physmap[1] / 1024);
1570
1571 /* look for the MP hardware - needed for apic addresses */
1572 mp_probe();
1573#endif
1574
1575 /*
1576 * Maxmem isn't the "maximum memory", it's one larger than the
1577 * highest page of the physical address space. It should be
1578 * called something like "Maxphyspage". We may adjust this
1579 * based on ``hw.physmem'' and the results of the memory test.
1580 */
1581 Maxmem = atop(physmap[physmap_idx + 1]);
1582
1583#ifdef MAXMEM
1584 Maxmem = MAXMEM / 4;
1585#endif
1586
1587 /*
1588 * hw.maxmem is a size in bytes; we also allow k, m, and g suffixes
1589 * for the appropriate modifiers. This overrides MAXMEM.
1590 */
1591 if ((cp = getenv("hw.physmem")) != NULL) {
1592 u_int64_t AllowMem, sanity;
1593 const char *ep;
1593 char *ep;
1594
1595 sanity = AllowMem = strtouq(cp, &ep, 0);
1596 if ((ep != cp) && (*ep != 0)) {
1597 switch(*ep) {
1598 case 'g':
1599 case 'G':
1600 AllowMem <<= 10;
1601 case 'm':
1602 case 'M':
1603 AllowMem <<= 10;
1604 case 'k':
1605 case 'K':
1606 AllowMem <<= 10;
1607 break;
1608 default:
1609 AllowMem = sanity = 0;
1610 }
1611 if (AllowMem < sanity)
1612 AllowMem = 0;
1613 }
1614 if (AllowMem == 0)
1615 printf("Ignoring invalid memory size of '%s'\n", cp);
1616 else
1617 Maxmem = atop(AllowMem);
1618 }
1619
1620 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1621 (boothowto & RB_VERBOSE))
1622 printf("Physical memory use set to %uK\n", Maxmem * 4);
1623
1624 /*
1625 * If Maxmem has been increased beyond what the system has detected,
1626 * extend the last memory segment to the new limit.
1627 */
1628 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1629 physmap[physmap_idx + 1] = ptoa(Maxmem);
1630
1631 /* call pmap initialization to make new kernel address space */
1632 pmap_bootstrap(first, 0);
1633
1634 /*
1635 * Size up each available chunk of physical memory.
1636 */
1637 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1638 pa_indx = 0;
1639 phys_avail[pa_indx++] = physmap[0];
1640 phys_avail[pa_indx] = physmap[0];
1641#if 0
1642 pte = (pt_entry_t)vtopte(KERNBASE);
1643#else
1644 pte = (pt_entry_t)CMAP1;
1645#endif
1646
1647 /*
1648 * physmap is in bytes, so when converting to page boundaries,
1649 * round up the start address and round down the end address.
1650 */
1651 for (i = 0; i <= physmap_idx; i += 2) {
1652 vm_offset_t end;
1653
1654 end = ptoa(Maxmem);
1655 if (physmap[i + 1] < end)
1656 end = trunc_page(physmap[i + 1]);
1657 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1658 int tmp, page_bad;
1659#if 0
1660 int *ptr = 0;
1661#else
1662 int *ptr = (int *)CADDR1;
1663#endif
1664
1665 /*
1666 * block out kernel memory as not available.
1667 */
1668 if (pa >= 0x100000 && pa < first)
1669 continue;
1670
1671 page_bad = FALSE;
1672
1673 /*
1674 * map page into kernel: valid, read/write,non-cacheable
1675 */
1676 *pte = pa | PG_V | PG_RW | PG_N;
1677 invltlb();
1678
1679 tmp = *(int *)ptr;
1680 /*
1681 * Test for alternating 1's and 0's
1682 */
1683 *(volatile int *)ptr = 0xaaaaaaaa;
1684 if (*(volatile int *)ptr != 0xaaaaaaaa) {
1685 page_bad = TRUE;
1686 }
1687 /*
1688 * Test for alternating 0's and 1's
1689 */
1690 *(volatile int *)ptr = 0x55555555;
1691 if (*(volatile int *)ptr != 0x55555555) {
1692 page_bad = TRUE;
1693 }
1694 /*
1695 * Test for all 1's
1696 */
1697 *(volatile int *)ptr = 0xffffffff;
1698 if (*(volatile int *)ptr != 0xffffffff) {
1699 page_bad = TRUE;
1700 }
1701 /*
1702 * Test for all 0's
1703 */
1704 *(volatile int *)ptr = 0x0;
1705 if (*(volatile int *)ptr != 0x0) {
1706 page_bad = TRUE;
1707 }
1708 /*
1709 * Restore original value.
1710 */
1711 *(int *)ptr = tmp;
1712
1713 /*
1714 * Adjust array of valid/good pages.
1715 */
1716 if (page_bad == TRUE) {
1717 continue;
1718 }
1719 /*
1720 * If this good page is a continuation of the
1721 * previous set of good pages, then just increase
1722 * the end pointer. Otherwise start a new chunk.
1723 * Note that "end" points one higher than end,
1724 * making the range >= start and < end.
1725 * If we're also doing a speculative memory
1726 * test and we at or past the end, bump up Maxmem
1727 * so that we keep going. The first bad page
1728 * will terminate the loop.
1729 */
1730 if (phys_avail[pa_indx] == pa) {
1731 phys_avail[pa_indx] += PAGE_SIZE;
1732 } else {
1733 pa_indx++;
1734 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1735 printf("Too many holes in the physical address space, giving up\n");
1736 pa_indx--;
1737 break;
1738 }
1739 phys_avail[pa_indx++] = pa; /* start */
1740 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1741 }
1742 physmem++;
1743 }
1744 }
1745 *pte = 0;
1746 invltlb();
1747
1748 /*
1749 * XXX
1750 * The last chunk must contain at least one page plus the message
1751 * buffer to avoid complicating other code (message buffer address
1752 * calculation, etc.).
1753 */
1754 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1755 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1756 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1757 phys_avail[pa_indx--] = 0;
1758 phys_avail[pa_indx--] = 0;
1759 }
1760
1761 Maxmem = atop(phys_avail[pa_indx]);
1762
1763 /* Trim off space for the message buffer. */
1764 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1765
1766 avail_end = phys_avail[pa_indx];
1767}
1768
1769void
1770init386(first)
1771 int first;
1772{
1773 int x;
1774 struct gate_descriptor *gdp;
1775 int gsel_tss;
1776#ifndef SMP
1777 /* table descriptors - used to load tables by microp */
1778 struct region_descriptor r_gdt, r_idt;
1779#endif
1780 int off;
1781
1782 /*
1783 * Prevent lowering of the ipl if we call tsleep() early.
1784 */
1785 safepri = cpl;
1786
1787 proc0.p_addr = proc0paddr;
1788
1789 atdevbase = ISA_HOLE_START + KERNBASE;
1790
1791 if (bootinfo.bi_modulep) {
1792 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1793 preload_bootstrap_relocate(KERNBASE);
1794 }
1795 if (bootinfo.bi_envp)
1796 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1797
1798 /*
1799 * make gdt memory segments, the code segment goes up to end of the
1800 * page with etext in it, the data segment goes to the end of
1801 * the address space
1802 */
1803 /*
1804 * XXX text protection is temporarily (?) disabled. The limit was
1805 * i386_btop(round_page(etext)) - 1.
1806 */
1807 gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
1808 gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
1809#ifdef SMP
1810 gdt_segs[GPRIV_SEL].ssd_limit =
1811 i386_btop(sizeof(struct privatespace)) - 1;
1812 gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[0];
1813 gdt_segs[GPROC0_SEL].ssd_base =
1814 (int) &SMP_prvspace[0].globaldata.gd_common_tss;
1815 SMP_prvspace[0].globaldata.gd_prvspace = &SMP_prvspace[0];
1816#else
1817 gdt_segs[GPRIV_SEL].ssd_limit = i386_btop(0) - 1;
1818 gdt_segs[GPROC0_SEL].ssd_base = (int) &common_tss;
1819#endif
1820
1821 for (x = 0; x < NGDT; x++) {
1822#ifdef BDE_DEBUGGER
1823 /* avoid overwriting db entries with APM ones */
1824 if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL)
1825 continue;
1826#endif
1827 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1828 }
1829
1830 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1831 r_gdt.rd_base = (int) gdt;
1832 lgdt(&r_gdt);
1833
1834 /* make ldt memory segments */
1835 /*
1836 * The data segment limit must not cover the user area because we
1837 * don't want the user area to be writable in copyout() etc. (page
1838 * level protection is lost in kernel mode on 386's). Also, we
1839 * don't want the user area to be writable directly (page level
1840 * protection of the user area is not available on 486's with
1841 * CR0_WP set, because there is no user-read/kernel-write mode).
1842 *
1843 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it
1844 * should be spelled ...MAX_USER...
1845 */
1846#define VM_END_USER_RW_ADDRESS VM_MAXUSER_ADDRESS
1847 /*
1848 * The code segment limit has to cover the user area until we move
1849 * the signal trampoline out of the user area. This is safe because
1850 * the code segment cannot be written to directly.
1851 */
1852#define VM_END_USER_R_ADDRESS (VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1853 ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1854 ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1855 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1856 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1857
1858 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1859 lldt(_default_ldt);
1860#ifdef USER_LDT
1861 currentldt = _default_ldt;
1862#endif
1863
1864 /* exceptions */
1865 for (x = 0; x < NIDT; x++)
1866 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1867 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1868 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1869 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1870 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1871 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1872 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1873 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1874 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1875 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1876 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1877 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1878 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1879 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1880 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1881 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1882 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1883 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1884 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1885 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1886 setidt(0x80, &IDTVEC(int0x80_syscall),
1887 SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1888
1889 r_idt.rd_limit = sizeof(idt0) - 1;
1890 r_idt.rd_base = (int) idt;
1891 lidt(&r_idt);
1892
1893 /*
1894 * Initialize the console before we print anything out.
1895 */
1896 cninit();
1897
1898#include "isa.h"
1899#if NISA >0
1900 isa_defaultirq();
1901#endif
1902 rand_initialize();
1903
1904#ifdef DDB
1905 kdb_init();
1906 if (boothowto & RB_KDB)
1907 Debugger("Boot flags requested debugger");
1908#endif
1909
1910 finishidentcpu(); /* Final stage of CPU initialization */
1911 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1912 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1913 initializecpu(); /* Initialize CPU registers */
1914
1915 /* make an initial tss so cpu can get interrupt stack on syscall! */
1916 common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE - 16;
1917 common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1918 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1919 private_tss = 0;
1920 tss_gdt = &gdt[GPROC0_SEL].sd;
1921 common_tssd = *tss_gdt;
1922 common_tss.tss_ioopt = (sizeof common_tss) << 16;
1923 ltr(gsel_tss);
1924
1925 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1926 dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1927 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1928 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1929 dblfault_tss.tss_cr3 = (int)IdlePTD;
1930 dblfault_tss.tss_eip = (int) dblfault_handler;
1931 dblfault_tss.tss_eflags = PSL_KERNEL;
1932 dblfault_tss.tss_ds = dblfault_tss.tss_es =
1933 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1934 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1935 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1936 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1937
1938 vm86_initialize();
1939 getmemsize(first);
1940
1941 /* now running on new page tables, configured,and u/iom is accessible */
1942
1943 /* Map the message buffer. */
1944 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1945 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1946
1947 msgbufinit(msgbufp, MSGBUF_SIZE);
1948
1949 /* make a call gate to reenter kernel with */
1950 gdp = &ldt[LSYS5CALLS_SEL].gd;
1951
1952 x = (int) &IDTVEC(syscall);
1953 gdp->gd_looffset = x++;
1954 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1955 gdp->gd_stkcpy = 1;
1956 gdp->gd_type = SDT_SYS386CGT;
1957 gdp->gd_dpl = SEL_UPL;
1958 gdp->gd_p = 1;
1959 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1960
1961 /* XXX does this work? */
1962 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1963 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
1964
1965 /* transfer to user mode */
1966
1967 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1968 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1969
1970 /* setup proc 0's pcb */
1971 proc0.p_addr->u_pcb.pcb_flags = 0;
1972 proc0.p_addr->u_pcb.pcb_cr3 = (int)IdlePTD;
1973#ifdef SMP
1974 proc0.p_addr->u_pcb.pcb_mpnest = 1;
1975#endif
1976 proc0.p_addr->u_pcb.pcb_ext = 0;
1977}
1978
1979#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1980static void f00f_hack(void *unused);
1981SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
1982
1983static void
1984f00f_hack(void *unused) {
1985 struct gate_descriptor *new_idt;
1986#ifndef SMP
1987 struct region_descriptor r_idt;
1988#endif
1989 vm_offset_t tmp;
1990
1991 if (!has_f00f_bug)
1992 return;
1993
1994 printf("Intel Pentium detected, installing workaround for F00F bug\n");
1995
1996 r_idt.rd_limit = sizeof(idt0) - 1;
1997
1998 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
1999 if (tmp == 0)
2000 panic("kmem_alloc returned 0");
2001 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
2002 panic("kmem_alloc returned non-page-aligned memory");
2003 /* Put the first seven entries in the lower page */
2004 new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
2005 bcopy(idt, new_idt, sizeof(idt0));
2006 r_idt.rd_base = (int)new_idt;
2007 lidt(&r_idt);
2008 idt = new_idt;
2009 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2010 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2011 panic("vm_map_protect failed");
2012 return;
2013}
2014#endif /* defined(I586_CPU) && !NO_F00F_HACK */
2015
2016int
2017ptrace_set_pc(p, addr)
2018 struct proc *p;
2019 unsigned long addr;
2020{
2021 p->p_md.md_regs->tf_eip = addr;
2022 return (0);
2023}
2024
2025int
2026ptrace_single_step(p)
2027 struct proc *p;
2028{
2029 p->p_md.md_regs->tf_eflags |= PSL_T;
2030 return (0);
2031}
2032
2033int ptrace_read_u_check(p, addr, len)
2034 struct proc *p;
2035 vm_offset_t addr;
2036 size_t len;
2037{
2038 vm_offset_t gap;
2039
2040 if ((vm_offset_t) (addr + len) < addr)
2041 return EPERM;
2042 if ((vm_offset_t) (addr + len) <= sizeof(struct user))
2043 return 0;
2044
2045 gap = (char *) p->p_md.md_regs - (char *) p->p_addr;
2046
2047 if ((vm_offset_t) addr < gap)
2048 return EPERM;
2049 if ((vm_offset_t) (addr + len) <=
2050 (vm_offset_t) (gap + sizeof(struct trapframe)))
2051 return 0;
2052 return EPERM;
2053}
2054
2055int ptrace_write_u(p, off, data)
2056 struct proc *p;
2057 vm_offset_t off;
2058 long data;
2059{
2060 struct trapframe frame_copy;
2061 vm_offset_t min;
2062 struct trapframe *tp;
2063
2064 /*
2065 * Privileged kernel state is scattered all over the user area.
2066 * Only allow write access to parts of regs and to fpregs.
2067 */
2068 min = (char *)p->p_md.md_regs - (char *)p->p_addr;
2069 if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
2070 tp = p->p_md.md_regs;
2071 frame_copy = *tp;
2072 *(int *)((char *)&frame_copy + (off - min)) = data;
2073 if (!EFL_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
2074 !CS_SECURE(frame_copy.tf_cs))
2075 return (EINVAL);
2076 *(int*)((char *)p->p_addr + off) = data;
2077 return (0);
2078 }
2079 min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
2080 if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
2081 *(int*)((char *)p->p_addr + off) = data;
2082 return (0);
2083 }
2084 return (EFAULT);
2085}
2086
2087int
2088fill_regs(p, regs)
2089 struct proc *p;
2090 struct reg *regs;
2091{
2092 struct pcb *pcb;
2093 struct trapframe *tp;
2094
2095 tp = p->p_md.md_regs;
2096 regs->r_fs = tp->tf_fs;
2097 regs->r_es = tp->tf_es;
2098 regs->r_ds = tp->tf_ds;
2099 regs->r_edi = tp->tf_edi;
2100 regs->r_esi = tp->tf_esi;
2101 regs->r_ebp = tp->tf_ebp;
2102 regs->r_ebx = tp->tf_ebx;
2103 regs->r_edx = tp->tf_edx;
2104 regs->r_ecx = tp->tf_ecx;
2105 regs->r_eax = tp->tf_eax;
2106 regs->r_eip = tp->tf_eip;
2107 regs->r_cs = tp->tf_cs;
2108 regs->r_eflags = tp->tf_eflags;
2109 regs->r_esp = tp->tf_esp;
2110 regs->r_ss = tp->tf_ss;
2111 pcb = &p->p_addr->u_pcb;
2112 regs->r_gs = pcb->pcb_gs;
2113 return (0);
2114}
2115
2116int
2117set_regs(p, regs)
2118 struct proc *p;
2119 struct reg *regs;
2120{
2121 struct pcb *pcb;
2122 struct trapframe *tp;
2123
2124 tp = p->p_md.md_regs;
2125 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2126 !CS_SECURE(regs->r_cs))
2127 return (EINVAL);
2128 tp->tf_fs = regs->r_fs;
2129 tp->tf_es = regs->r_es;
2130 tp->tf_ds = regs->r_ds;
2131 tp->tf_edi = regs->r_edi;
2132 tp->tf_esi = regs->r_esi;
2133 tp->tf_ebp = regs->r_ebp;
2134 tp->tf_ebx = regs->r_ebx;
2135 tp->tf_edx = regs->r_edx;
2136 tp->tf_ecx = regs->r_ecx;
2137 tp->tf_eax = regs->r_eax;
2138 tp->tf_eip = regs->r_eip;
2139 tp->tf_cs = regs->r_cs;
2140 tp->tf_eflags = regs->r_eflags;
2141 tp->tf_esp = regs->r_esp;
2142 tp->tf_ss = regs->r_ss;
2143 pcb = &p->p_addr->u_pcb;
2144 pcb->pcb_gs = regs->r_gs;
2145 return (0);
2146}
2147
2148int
2149fill_fpregs(p, fpregs)
2150 struct proc *p;
2151 struct fpreg *fpregs;
2152{
2153 bcopy(&p->p_addr->u_pcb.pcb_savefpu, fpregs, sizeof *fpregs);
2154 return (0);
2155}
2156
2157int
2158set_fpregs(p, fpregs)
2159 struct proc *p;
2160 struct fpreg *fpregs;
2161{
2162 bcopy(fpregs, &p->p_addr->u_pcb.pcb_savefpu, sizeof *fpregs);
2163 return (0);
2164}
2165
2166int
2167fill_dbregs(p, dbregs)
2168 struct proc *p;
2169 struct dbreg *dbregs;
2170{
2171 struct pcb *pcb;
2172
2173 pcb = &p->p_addr->u_pcb;
2174 dbregs->dr0 = pcb->pcb_dr0;
2175 dbregs->dr1 = pcb->pcb_dr1;
2176 dbregs->dr2 = pcb->pcb_dr2;
2177 dbregs->dr3 = pcb->pcb_dr3;
2178 dbregs->dr4 = 0;
2179 dbregs->dr5 = 0;
2180 dbregs->dr6 = pcb->pcb_dr6;
2181 dbregs->dr7 = pcb->pcb_dr7;
2182 return (0);
2183}
2184
2185int
2186set_dbregs(p, dbregs)
2187 struct proc *p;
2188 struct dbreg *dbregs;
2189{
2190 struct pcb *pcb;
2191
2192 pcb = &p->p_addr->u_pcb;
2193
2194 /*
2195 * Don't let a process set a breakpoint that is not within the
2196 * process's address space. If a process could do this, it
2197 * could halt the system by setting a breakpoint in the kernel
2198 * (if ddb was enabled). Thus, we need to check to make sure
2199 * that no breakpoints are being enabled for addresses outside
2200 * process's address space, unless, perhaps, we were called by
2201 * uid 0.
2202 *
2203 * XXX - what about when the watched area of the user's
2204 * address space is written into from within the kernel
2205 * ... wouldn't that still cause a breakpoint to be generated
2206 * from within kernel mode?
2207 */
2208
2209 if (p->p_ucred->cr_uid != 0) {
2210 if (dbregs->dr7 & 0x3) {
2211 /* dr0 is enabled */
2212 if (dbregs->dr0 >= VM_MAXUSER_ADDRESS)
2213 return (EINVAL);
2214 }
2215
2216 if (dbregs->dr7 & (0x3<<2)) {
2217 /* dr1 is enabled */
2218 if (dbregs->dr1 >= VM_MAXUSER_ADDRESS)
2219 return (EINVAL);
2220 }
2221
2222 if (dbregs->dr7 & (0x3<<4)) {
2223 /* dr2 is enabled */
2224 if (dbregs->dr2 >= VM_MAXUSER_ADDRESS)
2225 return (EINVAL);
2226 }
2227
2228 if (dbregs->dr7 & (0x3<<6)) {
2229 /* dr3 is enabled */
2230 if (dbregs->dr3 >= VM_MAXUSER_ADDRESS)
2231 return (EINVAL);
2232 }
2233 }
2234
2235 pcb->pcb_dr0 = dbregs->dr0;
2236 pcb->pcb_dr1 = dbregs->dr1;
2237 pcb->pcb_dr2 = dbregs->dr2;
2238 pcb->pcb_dr3 = dbregs->dr3;
2239 pcb->pcb_dr6 = dbregs->dr6;
2240 pcb->pcb_dr7 = dbregs->dr7;
2241
2242 pcb->pcb_flags |= PCB_DBREGS;
2243
2244 return (0);
2245}
2246
2247#ifndef DDB
2248void
2249Debugger(const char *msg)
2250{
2251 printf("Debugger(\"%s\") called.\n", msg);
2252}
2253#endif /* no DDB */
2254
2255#include <sys/disklabel.h>
2256
2257/*
2258 * Determine the size of the transfer, and make sure it is
2259 * within the boundaries of the partition. Adjust transfer
2260 * if needed, and signal errors or early completion.
2261 */
2262int
2263bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
2264{
2265 struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
2266 int labelsect = lp->d_partitions[0].p_offset;
2267 int maxsz = p->p_size,
2268 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
2269
2270 /* overwriting disk label ? */
2271 /* XXX should also protect bootstrap in first 8K */
2272 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
2273#if LABELSECTOR != 0
2274 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
2275#endif
2276 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2277 bp->b_error = EROFS;
2278 goto bad;
2279 }
2280
2281#if defined(DOSBBSECTOR) && defined(notyet)
2282 /* overwriting master boot record? */
2283 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
2284 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2285 bp->b_error = EROFS;
2286 goto bad;
2287 }
2288#endif
2289
2290 /* beyond partition? */
2291 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
2292 /* if exactly at end of disk, return an EOF */
2293 if (bp->b_blkno == maxsz) {
2294 bp->b_resid = bp->b_bcount;
2295 return(0);
2296 }
2297 /* or truncate if part of it fits */
2298 sz = maxsz - bp->b_blkno;
2299 if (sz <= 0) {
2300 bp->b_error = EINVAL;
2301 goto bad;
2302 }
2303 bp->b_bcount = sz << DEV_BSHIFT;
2304 }
2305
2306 bp->b_pblkno = bp->b_blkno + p->p_offset;
2307 return(1);
2308
2309bad:
2310 bp->b_flags |= B_ERROR;
2311 return(-1);
2312}
2313
2314#ifdef DDB
2315
2316/*
2317 * Provide inb() and outb() as functions. They are normally only
2318 * available as macros calling inlined functions, thus cannot be
2319 * called inside DDB.
2320 *
2321 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2322 */
2323
2324#undef inb
2325#undef outb
2326
2327/* silence compiler warnings */
2328u_char inb(u_int);
2329void outb(u_int, u_char);
2330
2331u_char
2332inb(u_int port)
2333{
2334 u_char data;
2335 /*
2336 * We use %%dx and not %1 here because i/o is done at %dx and not at
2337 * %edx, while gcc generates inferior code (movw instead of movl)
2338 * if we tell it to load (u_short) port.
2339 */
2340 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2341 return (data);
2342}
2343
2344void
2345outb(u_int port, u_char data)
2346{
2347 u_char al;
2348 /*
2349 * Use an unnecessary assignment to help gcc's register allocator.
2350 * This make a large difference for gcc-1.40 and a tiny difference
2351 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2352 * best results. gcc-2.6.0 can't handle this.
2353 */
2354 al = data;
2355 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2356}
2357
2358#endif /* DDB */
1594
1595 sanity = AllowMem = strtouq(cp, &ep, 0);
1596 if ((ep != cp) && (*ep != 0)) {
1597 switch(*ep) {
1598 case 'g':
1599 case 'G':
1600 AllowMem <<= 10;
1601 case 'm':
1602 case 'M':
1603 AllowMem <<= 10;
1604 case 'k':
1605 case 'K':
1606 AllowMem <<= 10;
1607 break;
1608 default:
1609 AllowMem = sanity = 0;
1610 }
1611 if (AllowMem < sanity)
1612 AllowMem = 0;
1613 }
1614 if (AllowMem == 0)
1615 printf("Ignoring invalid memory size of '%s'\n", cp);
1616 else
1617 Maxmem = atop(AllowMem);
1618 }
1619
1620 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1621 (boothowto & RB_VERBOSE))
1622 printf("Physical memory use set to %uK\n", Maxmem * 4);
1623
1624 /*
1625 * If Maxmem has been increased beyond what the system has detected,
1626 * extend the last memory segment to the new limit.
1627 */
1628 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1629 physmap[physmap_idx + 1] = ptoa(Maxmem);
1630
1631 /* call pmap initialization to make new kernel address space */
1632 pmap_bootstrap(first, 0);
1633
1634 /*
1635 * Size up each available chunk of physical memory.
1636 */
1637 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1638 pa_indx = 0;
1639 phys_avail[pa_indx++] = physmap[0];
1640 phys_avail[pa_indx] = physmap[0];
1641#if 0
1642 pte = (pt_entry_t)vtopte(KERNBASE);
1643#else
1644 pte = (pt_entry_t)CMAP1;
1645#endif
1646
1647 /*
1648 * physmap is in bytes, so when converting to page boundaries,
1649 * round up the start address and round down the end address.
1650 */
1651 for (i = 0; i <= physmap_idx; i += 2) {
1652 vm_offset_t end;
1653
1654 end = ptoa(Maxmem);
1655 if (physmap[i + 1] < end)
1656 end = trunc_page(physmap[i + 1]);
1657 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1658 int tmp, page_bad;
1659#if 0
1660 int *ptr = 0;
1661#else
1662 int *ptr = (int *)CADDR1;
1663#endif
1664
1665 /*
1666 * block out kernel memory as not available.
1667 */
1668 if (pa >= 0x100000 && pa < first)
1669 continue;
1670
1671 page_bad = FALSE;
1672
1673 /*
1674 * map page into kernel: valid, read/write,non-cacheable
1675 */
1676 *pte = pa | PG_V | PG_RW | PG_N;
1677 invltlb();
1678
1679 tmp = *(int *)ptr;
1680 /*
1681 * Test for alternating 1's and 0's
1682 */
1683 *(volatile int *)ptr = 0xaaaaaaaa;
1684 if (*(volatile int *)ptr != 0xaaaaaaaa) {
1685 page_bad = TRUE;
1686 }
1687 /*
1688 * Test for alternating 0's and 1's
1689 */
1690 *(volatile int *)ptr = 0x55555555;
1691 if (*(volatile int *)ptr != 0x55555555) {
1692 page_bad = TRUE;
1693 }
1694 /*
1695 * Test for all 1's
1696 */
1697 *(volatile int *)ptr = 0xffffffff;
1698 if (*(volatile int *)ptr != 0xffffffff) {
1699 page_bad = TRUE;
1700 }
1701 /*
1702 * Test for all 0's
1703 */
1704 *(volatile int *)ptr = 0x0;
1705 if (*(volatile int *)ptr != 0x0) {
1706 page_bad = TRUE;
1707 }
1708 /*
1709 * Restore original value.
1710 */
1711 *(int *)ptr = tmp;
1712
1713 /*
1714 * Adjust array of valid/good pages.
1715 */
1716 if (page_bad == TRUE) {
1717 continue;
1718 }
1719 /*
1720 * If this good page is a continuation of the
1721 * previous set of good pages, then just increase
1722 * the end pointer. Otherwise start a new chunk.
1723 * Note that "end" points one higher than end,
1724 * making the range >= start and < end.
1725 * If we're also doing a speculative memory
1726 * test and we at or past the end, bump up Maxmem
1727 * so that we keep going. The first bad page
1728 * will terminate the loop.
1729 */
1730 if (phys_avail[pa_indx] == pa) {
1731 phys_avail[pa_indx] += PAGE_SIZE;
1732 } else {
1733 pa_indx++;
1734 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1735 printf("Too many holes in the physical address space, giving up\n");
1736 pa_indx--;
1737 break;
1738 }
1739 phys_avail[pa_indx++] = pa; /* start */
1740 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1741 }
1742 physmem++;
1743 }
1744 }
1745 *pte = 0;
1746 invltlb();
1747
1748 /*
1749 * XXX
1750 * The last chunk must contain at least one page plus the message
1751 * buffer to avoid complicating other code (message buffer address
1752 * calculation, etc.).
1753 */
1754 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1755 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1756 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1757 phys_avail[pa_indx--] = 0;
1758 phys_avail[pa_indx--] = 0;
1759 }
1760
1761 Maxmem = atop(phys_avail[pa_indx]);
1762
1763 /* Trim off space for the message buffer. */
1764 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1765
1766 avail_end = phys_avail[pa_indx];
1767}
1768
1769void
1770init386(first)
1771 int first;
1772{
1773 int x;
1774 struct gate_descriptor *gdp;
1775 int gsel_tss;
1776#ifndef SMP
1777 /* table descriptors - used to load tables by microp */
1778 struct region_descriptor r_gdt, r_idt;
1779#endif
1780 int off;
1781
1782 /*
1783 * Prevent lowering of the ipl if we call tsleep() early.
1784 */
1785 safepri = cpl;
1786
1787 proc0.p_addr = proc0paddr;
1788
1789 atdevbase = ISA_HOLE_START + KERNBASE;
1790
1791 if (bootinfo.bi_modulep) {
1792 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1793 preload_bootstrap_relocate(KERNBASE);
1794 }
1795 if (bootinfo.bi_envp)
1796 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1797
1798 /*
1799 * make gdt memory segments, the code segment goes up to end of the
1800 * page with etext in it, the data segment goes to the end of
1801 * the address space
1802 */
1803 /*
1804 * XXX text protection is temporarily (?) disabled. The limit was
1805 * i386_btop(round_page(etext)) - 1.
1806 */
1807 gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
1808 gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
1809#ifdef SMP
1810 gdt_segs[GPRIV_SEL].ssd_limit =
1811 i386_btop(sizeof(struct privatespace)) - 1;
1812 gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[0];
1813 gdt_segs[GPROC0_SEL].ssd_base =
1814 (int) &SMP_prvspace[0].globaldata.gd_common_tss;
1815 SMP_prvspace[0].globaldata.gd_prvspace = &SMP_prvspace[0];
1816#else
1817 gdt_segs[GPRIV_SEL].ssd_limit = i386_btop(0) - 1;
1818 gdt_segs[GPROC0_SEL].ssd_base = (int) &common_tss;
1819#endif
1820
1821 for (x = 0; x < NGDT; x++) {
1822#ifdef BDE_DEBUGGER
1823 /* avoid overwriting db entries with APM ones */
1824 if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL)
1825 continue;
1826#endif
1827 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1828 }
1829
1830 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1831 r_gdt.rd_base = (int) gdt;
1832 lgdt(&r_gdt);
1833
1834 /* make ldt memory segments */
1835 /*
1836 * The data segment limit must not cover the user area because we
1837 * don't want the user area to be writable in copyout() etc. (page
1838 * level protection is lost in kernel mode on 386's). Also, we
1839 * don't want the user area to be writable directly (page level
1840 * protection of the user area is not available on 486's with
1841 * CR0_WP set, because there is no user-read/kernel-write mode).
1842 *
1843 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it
1844 * should be spelled ...MAX_USER...
1845 */
1846#define VM_END_USER_RW_ADDRESS VM_MAXUSER_ADDRESS
1847 /*
1848 * The code segment limit has to cover the user area until we move
1849 * the signal trampoline out of the user area. This is safe because
1850 * the code segment cannot be written to directly.
1851 */
1852#define VM_END_USER_R_ADDRESS (VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1853 ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1854 ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1855 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1856 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1857
1858 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1859 lldt(_default_ldt);
1860#ifdef USER_LDT
1861 currentldt = _default_ldt;
1862#endif
1863
1864 /* exceptions */
1865 for (x = 0; x < NIDT; x++)
1866 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1867 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1868 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1869 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1870 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1871 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1872 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1873 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1874 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1875 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1876 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1877 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1878 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1879 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1880 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1881 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1882 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1883 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1884 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1885 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1886 setidt(0x80, &IDTVEC(int0x80_syscall),
1887 SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1888
1889 r_idt.rd_limit = sizeof(idt0) - 1;
1890 r_idt.rd_base = (int) idt;
1891 lidt(&r_idt);
1892
1893 /*
1894 * Initialize the console before we print anything out.
1895 */
1896 cninit();
1897
1898#include "isa.h"
1899#if NISA >0
1900 isa_defaultirq();
1901#endif
1902 rand_initialize();
1903
1904#ifdef DDB
1905 kdb_init();
1906 if (boothowto & RB_KDB)
1907 Debugger("Boot flags requested debugger");
1908#endif
1909
1910 finishidentcpu(); /* Final stage of CPU initialization */
1911 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1912 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1913 initializecpu(); /* Initialize CPU registers */
1914
1915 /* make an initial tss so cpu can get interrupt stack on syscall! */
1916 common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE - 16;
1917 common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1918 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1919 private_tss = 0;
1920 tss_gdt = &gdt[GPROC0_SEL].sd;
1921 common_tssd = *tss_gdt;
1922 common_tss.tss_ioopt = (sizeof common_tss) << 16;
1923 ltr(gsel_tss);
1924
1925 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1926 dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1927 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1928 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1929 dblfault_tss.tss_cr3 = (int)IdlePTD;
1930 dblfault_tss.tss_eip = (int) dblfault_handler;
1931 dblfault_tss.tss_eflags = PSL_KERNEL;
1932 dblfault_tss.tss_ds = dblfault_tss.tss_es =
1933 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1934 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1935 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1936 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1937
1938 vm86_initialize();
1939 getmemsize(first);
1940
1941 /* now running on new page tables, configured,and u/iom is accessible */
1942
1943 /* Map the message buffer. */
1944 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1945 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1946
1947 msgbufinit(msgbufp, MSGBUF_SIZE);
1948
1949 /* make a call gate to reenter kernel with */
1950 gdp = &ldt[LSYS5CALLS_SEL].gd;
1951
1952 x = (int) &IDTVEC(syscall);
1953 gdp->gd_looffset = x++;
1954 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1955 gdp->gd_stkcpy = 1;
1956 gdp->gd_type = SDT_SYS386CGT;
1957 gdp->gd_dpl = SEL_UPL;
1958 gdp->gd_p = 1;
1959 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1960
1961 /* XXX does this work? */
1962 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1963 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
1964
1965 /* transfer to user mode */
1966
1967 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1968 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1969
1970 /* setup proc 0's pcb */
1971 proc0.p_addr->u_pcb.pcb_flags = 0;
1972 proc0.p_addr->u_pcb.pcb_cr3 = (int)IdlePTD;
1973#ifdef SMP
1974 proc0.p_addr->u_pcb.pcb_mpnest = 1;
1975#endif
1976 proc0.p_addr->u_pcb.pcb_ext = 0;
1977}
1978
1979#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1980static void f00f_hack(void *unused);
1981SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
1982
1983static void
1984f00f_hack(void *unused) {
1985 struct gate_descriptor *new_idt;
1986#ifndef SMP
1987 struct region_descriptor r_idt;
1988#endif
1989 vm_offset_t tmp;
1990
1991 if (!has_f00f_bug)
1992 return;
1993
1994 printf("Intel Pentium detected, installing workaround for F00F bug\n");
1995
1996 r_idt.rd_limit = sizeof(idt0) - 1;
1997
1998 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
1999 if (tmp == 0)
2000 panic("kmem_alloc returned 0");
2001 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
2002 panic("kmem_alloc returned non-page-aligned memory");
2003 /* Put the first seven entries in the lower page */
2004 new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
2005 bcopy(idt, new_idt, sizeof(idt0));
2006 r_idt.rd_base = (int)new_idt;
2007 lidt(&r_idt);
2008 idt = new_idt;
2009 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2010 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2011 panic("vm_map_protect failed");
2012 return;
2013}
2014#endif /* defined(I586_CPU) && !NO_F00F_HACK */
2015
2016int
2017ptrace_set_pc(p, addr)
2018 struct proc *p;
2019 unsigned long addr;
2020{
2021 p->p_md.md_regs->tf_eip = addr;
2022 return (0);
2023}
2024
2025int
2026ptrace_single_step(p)
2027 struct proc *p;
2028{
2029 p->p_md.md_regs->tf_eflags |= PSL_T;
2030 return (0);
2031}
2032
2033int ptrace_read_u_check(p, addr, len)
2034 struct proc *p;
2035 vm_offset_t addr;
2036 size_t len;
2037{
2038 vm_offset_t gap;
2039
2040 if ((vm_offset_t) (addr + len) < addr)
2041 return EPERM;
2042 if ((vm_offset_t) (addr + len) <= sizeof(struct user))
2043 return 0;
2044
2045 gap = (char *) p->p_md.md_regs - (char *) p->p_addr;
2046
2047 if ((vm_offset_t) addr < gap)
2048 return EPERM;
2049 if ((vm_offset_t) (addr + len) <=
2050 (vm_offset_t) (gap + sizeof(struct trapframe)))
2051 return 0;
2052 return EPERM;
2053}
2054
2055int ptrace_write_u(p, off, data)
2056 struct proc *p;
2057 vm_offset_t off;
2058 long data;
2059{
2060 struct trapframe frame_copy;
2061 vm_offset_t min;
2062 struct trapframe *tp;
2063
2064 /*
2065 * Privileged kernel state is scattered all over the user area.
2066 * Only allow write access to parts of regs and to fpregs.
2067 */
2068 min = (char *)p->p_md.md_regs - (char *)p->p_addr;
2069 if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
2070 tp = p->p_md.md_regs;
2071 frame_copy = *tp;
2072 *(int *)((char *)&frame_copy + (off - min)) = data;
2073 if (!EFL_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
2074 !CS_SECURE(frame_copy.tf_cs))
2075 return (EINVAL);
2076 *(int*)((char *)p->p_addr + off) = data;
2077 return (0);
2078 }
2079 min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
2080 if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
2081 *(int*)((char *)p->p_addr + off) = data;
2082 return (0);
2083 }
2084 return (EFAULT);
2085}
2086
2087int
2088fill_regs(p, regs)
2089 struct proc *p;
2090 struct reg *regs;
2091{
2092 struct pcb *pcb;
2093 struct trapframe *tp;
2094
2095 tp = p->p_md.md_regs;
2096 regs->r_fs = tp->tf_fs;
2097 regs->r_es = tp->tf_es;
2098 regs->r_ds = tp->tf_ds;
2099 regs->r_edi = tp->tf_edi;
2100 regs->r_esi = tp->tf_esi;
2101 regs->r_ebp = tp->tf_ebp;
2102 regs->r_ebx = tp->tf_ebx;
2103 regs->r_edx = tp->tf_edx;
2104 regs->r_ecx = tp->tf_ecx;
2105 regs->r_eax = tp->tf_eax;
2106 regs->r_eip = tp->tf_eip;
2107 regs->r_cs = tp->tf_cs;
2108 regs->r_eflags = tp->tf_eflags;
2109 regs->r_esp = tp->tf_esp;
2110 regs->r_ss = tp->tf_ss;
2111 pcb = &p->p_addr->u_pcb;
2112 regs->r_gs = pcb->pcb_gs;
2113 return (0);
2114}
2115
2116int
2117set_regs(p, regs)
2118 struct proc *p;
2119 struct reg *regs;
2120{
2121 struct pcb *pcb;
2122 struct trapframe *tp;
2123
2124 tp = p->p_md.md_regs;
2125 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2126 !CS_SECURE(regs->r_cs))
2127 return (EINVAL);
2128 tp->tf_fs = regs->r_fs;
2129 tp->tf_es = regs->r_es;
2130 tp->tf_ds = regs->r_ds;
2131 tp->tf_edi = regs->r_edi;
2132 tp->tf_esi = regs->r_esi;
2133 tp->tf_ebp = regs->r_ebp;
2134 tp->tf_ebx = regs->r_ebx;
2135 tp->tf_edx = regs->r_edx;
2136 tp->tf_ecx = regs->r_ecx;
2137 tp->tf_eax = regs->r_eax;
2138 tp->tf_eip = regs->r_eip;
2139 tp->tf_cs = regs->r_cs;
2140 tp->tf_eflags = regs->r_eflags;
2141 tp->tf_esp = regs->r_esp;
2142 tp->tf_ss = regs->r_ss;
2143 pcb = &p->p_addr->u_pcb;
2144 pcb->pcb_gs = regs->r_gs;
2145 return (0);
2146}
2147
2148int
2149fill_fpregs(p, fpregs)
2150 struct proc *p;
2151 struct fpreg *fpregs;
2152{
2153 bcopy(&p->p_addr->u_pcb.pcb_savefpu, fpregs, sizeof *fpregs);
2154 return (0);
2155}
2156
2157int
2158set_fpregs(p, fpregs)
2159 struct proc *p;
2160 struct fpreg *fpregs;
2161{
2162 bcopy(fpregs, &p->p_addr->u_pcb.pcb_savefpu, sizeof *fpregs);
2163 return (0);
2164}
2165
2166int
2167fill_dbregs(p, dbregs)
2168 struct proc *p;
2169 struct dbreg *dbregs;
2170{
2171 struct pcb *pcb;
2172
2173 pcb = &p->p_addr->u_pcb;
2174 dbregs->dr0 = pcb->pcb_dr0;
2175 dbregs->dr1 = pcb->pcb_dr1;
2176 dbregs->dr2 = pcb->pcb_dr2;
2177 dbregs->dr3 = pcb->pcb_dr3;
2178 dbregs->dr4 = 0;
2179 dbregs->dr5 = 0;
2180 dbregs->dr6 = pcb->pcb_dr6;
2181 dbregs->dr7 = pcb->pcb_dr7;
2182 return (0);
2183}
2184
2185int
2186set_dbregs(p, dbregs)
2187 struct proc *p;
2188 struct dbreg *dbregs;
2189{
2190 struct pcb *pcb;
2191
2192 pcb = &p->p_addr->u_pcb;
2193
2194 /*
2195 * Don't let a process set a breakpoint that is not within the
2196 * process's address space. If a process could do this, it
2197 * could halt the system by setting a breakpoint in the kernel
2198 * (if ddb was enabled). Thus, we need to check to make sure
2199 * that no breakpoints are being enabled for addresses outside
2200 * process's address space, unless, perhaps, we were called by
2201 * uid 0.
2202 *
2203 * XXX - what about when the watched area of the user's
2204 * address space is written into from within the kernel
2205 * ... wouldn't that still cause a breakpoint to be generated
2206 * from within kernel mode?
2207 */
2208
2209 if (p->p_ucred->cr_uid != 0) {
2210 if (dbregs->dr7 & 0x3) {
2211 /* dr0 is enabled */
2212 if (dbregs->dr0 >= VM_MAXUSER_ADDRESS)
2213 return (EINVAL);
2214 }
2215
2216 if (dbregs->dr7 & (0x3<<2)) {
2217 /* dr1 is enabled */
2218 if (dbregs->dr1 >= VM_MAXUSER_ADDRESS)
2219 return (EINVAL);
2220 }
2221
2222 if (dbregs->dr7 & (0x3<<4)) {
2223 /* dr2 is enabled */
2224 if (dbregs->dr2 >= VM_MAXUSER_ADDRESS)
2225 return (EINVAL);
2226 }
2227
2228 if (dbregs->dr7 & (0x3<<6)) {
2229 /* dr3 is enabled */
2230 if (dbregs->dr3 >= VM_MAXUSER_ADDRESS)
2231 return (EINVAL);
2232 }
2233 }
2234
2235 pcb->pcb_dr0 = dbregs->dr0;
2236 pcb->pcb_dr1 = dbregs->dr1;
2237 pcb->pcb_dr2 = dbregs->dr2;
2238 pcb->pcb_dr3 = dbregs->dr3;
2239 pcb->pcb_dr6 = dbregs->dr6;
2240 pcb->pcb_dr7 = dbregs->dr7;
2241
2242 pcb->pcb_flags |= PCB_DBREGS;
2243
2244 return (0);
2245}
2246
2247#ifndef DDB
2248void
2249Debugger(const char *msg)
2250{
2251 printf("Debugger(\"%s\") called.\n", msg);
2252}
2253#endif /* no DDB */
2254
2255#include <sys/disklabel.h>
2256
2257/*
2258 * Determine the size of the transfer, and make sure it is
2259 * within the boundaries of the partition. Adjust transfer
2260 * if needed, and signal errors or early completion.
2261 */
2262int
2263bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
2264{
2265 struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
2266 int labelsect = lp->d_partitions[0].p_offset;
2267 int maxsz = p->p_size,
2268 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
2269
2270 /* overwriting disk label ? */
2271 /* XXX should also protect bootstrap in first 8K */
2272 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
2273#if LABELSECTOR != 0
2274 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
2275#endif
2276 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2277 bp->b_error = EROFS;
2278 goto bad;
2279 }
2280
2281#if defined(DOSBBSECTOR) && defined(notyet)
2282 /* overwriting master boot record? */
2283 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
2284 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2285 bp->b_error = EROFS;
2286 goto bad;
2287 }
2288#endif
2289
2290 /* beyond partition? */
2291 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
2292 /* if exactly at end of disk, return an EOF */
2293 if (bp->b_blkno == maxsz) {
2294 bp->b_resid = bp->b_bcount;
2295 return(0);
2296 }
2297 /* or truncate if part of it fits */
2298 sz = maxsz - bp->b_blkno;
2299 if (sz <= 0) {
2300 bp->b_error = EINVAL;
2301 goto bad;
2302 }
2303 bp->b_bcount = sz << DEV_BSHIFT;
2304 }
2305
2306 bp->b_pblkno = bp->b_blkno + p->p_offset;
2307 return(1);
2308
2309bad:
2310 bp->b_flags |= B_ERROR;
2311 return(-1);
2312}
2313
2314#ifdef DDB
2315
2316/*
2317 * Provide inb() and outb() as functions. They are normally only
2318 * available as macros calling inlined functions, thus cannot be
2319 * called inside DDB.
2320 *
2321 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2322 */
2323
2324#undef inb
2325#undef outb
2326
2327/* silence compiler warnings */
2328u_char inb(u_int);
2329void outb(u_int, u_char);
2330
2331u_char
2332inb(u_int port)
2333{
2334 u_char data;
2335 /*
2336 * We use %%dx and not %1 here because i/o is done at %dx and not at
2337 * %edx, while gcc generates inferior code (movw instead of movl)
2338 * if we tell it to load (u_short) port.
2339 */
2340 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2341 return (data);
2342}
2343
2344void
2345outb(u_int port, u_char data)
2346{
2347 u_char al;
2348 /*
2349 * Use an unnecessary assignment to help gcc's register allocator.
2350 * This make a large difference for gcc-1.40 and a tiny difference
2351 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2352 * best results. gcc-2.6.0 can't handle this.
2353 */
2354 al = data;
2355 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2356}
2357
2358#endif /* DDB */