2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
44 #include "opt_atalk.h"
45 #include "opt_compat.h"
50 #include "opt_maxmem.h"
51 #include "opt_msgbuf.h"
52 #include "opt_perfmon.h"
54 #include "opt_sysvipc.h"
55 #include "opt_user_ldt.h"
56 #include "opt_userconfig.h"
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>
66 #include <sys/reboot.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
70 #include <sys/msgbuf.h>
71 #include <sys/sysent.h>
72 #include <sys/sysctl.h>
73 #include <sys/vmmeter.h>
89 #include <vm/vm_param.h>
90 #include <vm/vm_prot.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vm_extern.h>
100 #include <sys/exec.h>
101 #include <sys/cons.h>
105 #include <net/netisr.h>
107 #include <machine/cpu.h>
108 #include <machine/reg.h>
109 #include <machine/clock.h>
110 #include <machine/specialreg.h>
111 #include <machine/bootinfo.h>
112 #include <machine/ipl.h>
113 #include <machine/md_var.h>
114 #include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
116 #include <machine/smp.h>
117 #include <machine/globaldata.h>
120 #include <machine/perfmon.h>
124 #include <i386/isa/isa_device.h>
126 #include <i386/isa/intr_machdep.h>
128 #include <machine/vm86.h>
129 #include <machine/random.h>
130 #include <sys/ptrace.h>
131 #include <machine/sigframe.h>
133 extern void init386 __P((int first));
134 extern void dblfault_handler __P((void));
136 extern void printcpuinfo(void); /* XXX header file */
137 extern void earlysetcpuclass(void); /* same header file */
138 extern void finishidentcpu(void);
139 extern void panicifcpuunsupported(void);
140 extern void initializecpu(void);
142 static void cpu_startup __P((void *));
143 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
145 static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
147 int _udatasel, _ucodesel;
150 #if defined(SWTCH_OPTIM_STATS)
151 extern int swtch_optim_stats;
152 SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
153 CTLFLAG_RD, &swtch_optim_stats, 0, "");
154 SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
155 CTLFLAG_RD, &tlb_flush_count, 0, "");
159 static int ispc98 = 1;
161 static int ispc98 = 0;
163 SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
169 sysctl_hw_physmem SYSCTL_HANDLER_ARGS
171 int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
175 SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
176 0, 0, sysctl_hw_physmem, "I", "");
179 sysctl_hw_usermem SYSCTL_HANDLER_ARGS
181 int error = sysctl_handle_int(oidp, 0,
182 ctob(physmem - cnt.v_wire_count), req);
186 SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
187 0, 0, sysctl_hw_usermem, "I", "");
190 sysctl_hw_availpages SYSCTL_HANDLER_ARGS
192 int error = sysctl_handle_int(oidp, 0,
193 i386_btop(avail_end - avail_start), req);
197 SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD,
198 0, 0, sysctl_hw_availpages, "I", "");
201 sysctl_machdep_msgbuf SYSCTL_HANDLER_ARGS
205 /* Unwind the buffer, so that it's linear (possibly starting with
206 * some initial nulls).
208 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr+msgbufp->msg_bufr,
209 msgbufp->msg_size-msgbufp->msg_bufr,req);
210 if(error) return(error);
211 if(msgbufp->msg_bufr>0) {
212 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr,
213 msgbufp->msg_bufr,req);
218 SYSCTL_PROC(_machdep, OID_AUTO, msgbuf, CTLTYPE_STRING|CTLFLAG_RD,
219 0, 0, sysctl_machdep_msgbuf, "A","Contents of kernel message buffer");
221 static int msgbuf_clear;
224 sysctl_machdep_msgbuf_clear SYSCTL_HANDLER_ARGS
227 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
229 if (!error && req->newptr) {
230 /* Clear the buffer and reset write pointer */
231 bzero(msgbufp->msg_ptr,msgbufp->msg_size);
232 msgbufp->msg_bufr=msgbufp->msg_bufx=0;
238 SYSCTL_PROC(_machdep, OID_AUTO, msgbuf_clear, CTLTYPE_INT|CTLFLAG_RW,
239 &msgbuf_clear, 0, sysctl_machdep_msgbuf_clear, "I",
240 "Clear kernel message buffer");
242 int bootverbose = 0, Maxmem = 0;
245 vm_offset_t phys_avail[10];
247 /* must be 2 less so 0 0 can signal end of chunks */
248 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
250 static vm_offset_t buffer_sva, buffer_eva;
251 vm_offset_t clean_sva, clean_eva;
252 static vm_offset_t pager_sva, pager_eva;
254 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
267 if (boothowto & RB_VERBOSE)
271 * Good {morning,afternoon,evening,night}.
277 panicifcpuunsupported();
281 printf("real memory = %u (%uK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024);
283 * Display any holes after the first chunk of extended memory.
288 printf("Physical memory chunk(s):\n");
289 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
290 int size1 = phys_avail[indx + 1] - phys_avail[indx];
292 printf("0x%08x - 0x%08x, %u bytes (%u pages)\n",
293 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
299 * Calculate callout wheel size
301 for (callwheelsize = 1, callwheelbits = 0;
302 callwheelsize < ncallout;
303 callwheelsize <<= 1, ++callwheelbits)
305 callwheelmask = callwheelsize - 1;
308 * Allocate space for system data structures.
309 * The first available kernel virtual address is in "v".
310 * As pages of kernel virtual memory are allocated, "v" is incremented.
311 * As pages of memory are allocated and cleared,
312 * "firstaddr" is incremented.
313 * An index into the kernel page table corresponding to the
314 * virtual memory address maintained in "v" is kept in "mapaddr".
318 * Make two passes. The first pass calculates how much memory is
319 * needed and allocates it. The second pass assigns virtual
320 * addresses to the various data structures.
324 v = (caddr_t)firstaddr;
326 #define valloc(name, type, num) \
327 (name) = (type *)v; v = (caddr_t)((name)+(num))
328 #define valloclim(name, type, num, lim) \
329 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
331 valloc(callout, struct callout, ncallout);
332 valloc(callwheel, struct callout_tailq, callwheelsize);
334 valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
337 valloc(sema, struct semid_ds, seminfo.semmni);
338 valloc(sem, struct sem, seminfo.semmns);
339 /* This is pretty disgusting! */
340 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
343 valloc(msgpool, char, msginfo.msgmax);
344 valloc(msgmaps, struct msgmap, msginfo.msgseg);
345 valloc(msghdrs, struct msg, msginfo.msgtql);
346 valloc(msqids, struct msqid_ds, msginfo.msgmni);
352 nbuf += min((physmem - 1024) / 8, 2048);
354 nbuf += (physmem - 16384) / 20;
356 nswbuf = max(min(nbuf/4, 256), 16);
358 valloc(swbuf, struct buf, nswbuf);
359 valloc(buf, struct buf, nbuf);
363 * End of first pass, size has been calculated so allocate memory
365 if (firstaddr == 0) {
366 size = (vm_size_t)(v - firstaddr);
367 firstaddr = (int)kmem_alloc(kernel_map, round_page(size));
369 panic("startup: no room for tables");
374 * End of second pass, addresses have been assigned
376 if ((vm_size_t)(v - firstaddr) != size)
377 panic("startup: table size inconsistency");
379 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
380 (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
381 buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
383 pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
384 (nswbuf*MAXPHYS) + pager_map_size);
385 pager_map->system_map = 1;
386 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
387 (16*(ARG_MAX+(PAGE_SIZE*3))));
390 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size
391 * we use the more space efficient malloc in place of kmem_alloc.
394 vm_offset_t mb_map_size;
396 mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES;
397 mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE));
398 mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT);
399 bzero(mclrefcnt, mb_map_size / MCLBYTES);
400 mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
402 mb_map->system_map = 1;
406 * Initialize callouts
408 SLIST_INIT(&callfree);
409 for (i = 0; i < ncallout; i++) {
410 callout_init(&callout[i]);
411 callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
412 SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
415 for (i = 0; i < callwheelsize; i++) {
416 TAILQ_INIT(&callwheel[i]);
419 #if defined(USERCONFIG)
421 cninit(); /* the preferred console may have changed */
424 printf("avail memory = %u (%uK bytes)\n", ptoa(cnt.v_free_count),
425 ptoa(cnt.v_free_count) / 1024);
428 * Set up buffers, so they can be used to read disk labels.
431 vm_pager_bufferinit();
435 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
437 mp_start(); /* fire up the APs and APICs */
443 register_netisr(num, handler)
448 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
449 printf("register_netisr: bad isr number: %d\n", num);
452 netisrs[num] = handler;
460 const struct netisrtab *nit;
462 nit = (const struct netisrtab *)data;
463 register_netisr(nit->nit_num, nit->nit_isr);
467 * Send an interrupt to process.
469 * Stack is set up to allow sigcode stored
470 * at top to call routine, followed by kcall
471 * to sigreturn routine below. After sigreturn
472 * resets the signal mask, the stack, and the
473 * frame pointer, it returns to the user
477 osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
479 register struct proc *p = curproc;
480 register struct trapframe *regs;
481 register struct osigframe *fp;
483 struct sigacts *psp = p->p_sigacts;
486 regs = p->p_md.md_regs;
487 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
489 /* Allocate and validate space for the signal handler context. */
490 if ((p->p_flag & P_ALTSTACK) && !oonstack &&
491 SIGISMEMBER(psp->ps_sigonstack, sig)) {
492 fp = (struct osigframe *)(p->p_sigstk.ss_sp +
493 p->p_sigstk.ss_size - sizeof(struct osigframe));
494 p->p_sigstk.ss_flags |= SS_ONSTACK;
497 fp = (struct osigframe *)regs->tf_esp - 1;
500 * grow() will return FALSE if the fp will not fit inside the stack
501 * and the stack can not be grown. useracc will return FALSE
502 * if access is denied.
504 if (grow_stack(p, (int)fp) == FALSE ||
505 useracc((caddr_t)fp, sizeof(struct osigframe), B_WRITE) == FALSE) {
507 * Process has trashed its stack; give it an illegal
508 * instruction to halt it in its tracks.
510 SIGACTION(p, SIGILL) = SIG_DFL;
511 SIGDELSET(p->p_sigignore, SIGILL);
512 SIGDELSET(p->p_sigcatch, SIGILL);
513 SIGDELSET(p->p_sigmask, SIGILL);
518 /* Translate the signal if appropriate */
519 if (p->p_sysent->sv_sigtbl) {
520 if (sig <= p->p_sysent->sv_sigsize)
521 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
524 /* Build the argument list for the signal handler. */
526 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
527 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
528 /* Signal handler installed with SA_SIGINFO. */
529 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
530 sf.sf_siginfo.si_signo = sig;
531 sf.sf_siginfo.si_code = code;
532 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
535 /* Old FreeBSD-style arguments. */
537 sf.sf_addr = (char *)regs->tf_err;
538 sf.sf_ahu.sf_handler = catcher;
541 /* save scratch registers */
542 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
543 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
544 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
545 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
546 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
547 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
548 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
549 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
550 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
551 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
552 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
553 sf.sf_siginfo.si_sc.sc_gs = rgs();
554 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
556 /* Build the signal context to be used by sigreturn. */
557 sf.sf_siginfo.si_sc.sc_onstack = oonstack;
558 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
559 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
560 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
561 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
562 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
563 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
564 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
567 * If we're a vm86 process, we want to save the segment registers.
568 * We also change eflags to be our emulated eflags, not the actual
571 if (regs->tf_eflags & PSL_VM) {
572 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
573 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
575 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
576 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
577 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
578 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
580 if (vm86->vm86_has_vme == 0)
581 sf.sf_siginfo.si_sc.sc_ps =
582 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP))
583 | (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
584 /* see sendsig for comment */
585 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
588 /* Copy the sigframe out to the user's stack. */
589 if (copyout(&sf, fp, sizeof(struct osigframe)) != 0) {
591 * Something is wrong with the stack pointer.
592 * ...Kill the process.
597 regs->tf_esp = (int)fp;
598 regs->tf_eip = PS_STRINGS - oszsigcode;
599 regs->tf_cs = _ucodesel;
600 regs->tf_ds = _udatasel;
601 regs->tf_es = _udatasel;
602 regs->tf_fs = _udatasel;
604 regs->tf_ss = _udatasel;
608 sendsig(catcher, sig, mask, code)
614 struct proc *p = curproc;
615 struct trapframe *regs;
616 struct sigacts *psp = p->p_sigacts;
617 struct sigframe sf, *sfp;
620 if (SIGISMEMBER(psp->ps_osigset, sig)) {
621 osendsig(catcher, sig, mask, code);
625 regs = p->p_md.md_regs;
626 oonstack = (p->p_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
628 /* save user context */
629 bzero(&sf, sizeof(struct sigframe));
630 sf.sf_uc.uc_sigmask = *mask;
631 sf.sf_uc.uc_stack = p->p_sigstk;
632 sf.sf_uc.uc_mcontext.mc_onstack = oonstack;
633 sf.sf_uc.uc_mcontext.mc_gs = rgs();
634 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(struct trapframe));
636 /* Allocate and validate space for the signal handler context. */
637 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
638 SIGISMEMBER(psp->ps_sigonstack, sig)) {
639 sfp = (struct sigframe *)(p->p_sigstk.ss_sp +
640 p->p_sigstk.ss_size - sizeof(struct sigframe));
641 p->p_sigstk.ss_flags |= SS_ONSTACK;
644 sfp = (struct sigframe *)regs->tf_esp - 1;
647 * grow() will return FALSE if the sfp will not fit inside the stack
648 * and the stack can not be grown. useracc will return FALSE if
651 if (grow_stack(p, (int)sfp) == FALSE ||
652 useracc((caddr_t)sfp, sizeof(struct sigframe), B_WRITE) == FALSE) {
654 * Process has trashed its stack; give it an illegal
655 * instruction to halt it in its tracks.
658 printf("process %d has trashed its stack\n", p->p_pid);
660 SIGACTION(p, SIGILL) = SIG_DFL;
661 SIGDELSET(p->p_sigignore, SIGILL);
662 SIGDELSET(p->p_sigcatch, SIGILL);
663 SIGDELSET(p->p_sigmask, SIGILL);
668 /* Translate the signal is appropriate */
669 if (p->p_sysent->sv_sigtbl) {
670 if (sig <= p->p_sysent->sv_sigsize)
671 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
674 /* Build the argument list for the signal handler. */
676 sf.sf_ucontext = (register_t)&sfp->sf_uc;
677 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
678 /* Signal handler installed with SA_SIGINFO. */
679 sf.sf_siginfo = (register_t)&sfp->sf_si;
680 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
682 /* fill siginfo structure */
683 sf.sf_si.si_signo = sig;
684 sf.sf_si.si_code = code;
685 sf.sf_si.si_addr = (void*)regs->tf_err;
688 /* Old FreeBSD-style arguments. */
689 sf.sf_siginfo = code;
690 sf.sf_addr = (char *)regs->tf_err;
691 sf.sf_ahu.sf_handler = catcher;
695 * If we're a vm86 process, we want to save the segment registers.
696 * We also change eflags to be our emulated eflags, not the actual
699 if (regs->tf_eflags & PSL_VM) {
700 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
701 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
703 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
704 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
705 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
706 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
708 if (vm86->vm86_has_vme == 0)
709 sf.sf_uc.uc_mcontext.mc_eflags =
710 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
711 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
714 * We should never have PSL_T set when returning from vm86
715 * mode. It may be set here if we deliver a signal before
716 * getting to vm86 mode, so turn it off.
718 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
719 * syscalls made by the signal handler. This just avoids
720 * wasting time for our lazy fixup of such faults. PSL_NT
721 * does nothing in vm86 mode, but vm86 programs can set it
722 * almost legitimately in probes for old cpu types.
724 tf->tf_eflags &= ~(PSL_VM|PSL_NT|PSL_T|PSL_VIF|PSL_VIP);
728 * Copy the sigframe out to the user's stack.
730 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
732 * Something is wrong with the stack pointer.
733 * ...Kill the process.
738 regs->tf_esp = (int)sfp;
739 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
740 regs->tf_cs = _ucodesel;
741 regs->tf_ds = _udatasel;
742 regs->tf_es = _udatasel;
743 regs->tf_fs = _udatasel;
745 regs->tf_ss = _udatasel;
749 * System call to cleanup state after a signal
750 * has been taken. Reset signal mask and
751 * stack state from context left by sendsig (above).
752 * Return to previous pc and psl as specified by
753 * context left by sendsig. Check carefully to
754 * make sure that the user has not modified the
755 * state to gain improper privileges.
757 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
758 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
763 struct osigreturn_args /* {
764 struct osigcontext *sigcntxp;
767 register struct osigcontext *scp;
768 register struct trapframe *regs = p->p_md.md_regs;
773 if (useracc((caddr_t)scp, sizeof (struct osigcontext), B_WRITE) == 0)
777 if (eflags & PSL_VM) {
778 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
779 struct vm86_kernel *vm86;
782 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
783 * set up the vm86 area, and we can't enter vm86 mode.
785 if (p->p_addr->u_pcb.pcb_ext == 0)
787 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
788 if (vm86->vm86_inited == 0)
791 /* go back to user mode if both flags are set */
792 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
793 trapsignal(p, SIGBUS, 0);
795 if (vm86->vm86_has_vme) {
796 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
797 (eflags & VME_USERCHANGE) | PSL_VM;
799 vm86->vm86_eflags = eflags; /* save VIF, VIP */
800 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
802 tf->tf_vm86_ds = scp->sc_ds;
803 tf->tf_vm86_es = scp->sc_es;
804 tf->tf_vm86_fs = scp->sc_fs;
805 tf->tf_vm86_gs = scp->sc_gs;
806 tf->tf_ds = _udatasel;
807 tf->tf_es = _udatasel;
808 tf->tf_fs = _udatasel;
811 * Don't allow users to change privileged or reserved flags.
814 * XXX do allow users to change the privileged flag PSL_RF.
815 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
816 * should sometimes set it there too. tf_eflags is kept in
817 * the signal context during signal handling and there is no
818 * other place to remember it, so the PSL_RF bit may be
819 * corrupted by the signal handler without us knowing.
820 * Corruption of the PSL_RF bit at worst causes one more or
821 * one less debugger trap, so allowing it is fairly harmless.
823 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
828 * Don't allow users to load a valid privileged %cs. Let the
829 * hardware check for invalid selectors, excess privilege in
830 * other selectors, invalid %eip's and invalid %esp's.
832 if (!CS_SECURE(scp->sc_cs)) {
833 trapsignal(p, SIGBUS, T_PROTFLT);
836 regs->tf_ds = scp->sc_ds;
837 regs->tf_es = scp->sc_es;
838 regs->tf_fs = scp->sc_fs;
841 /* restore scratch registers */
842 regs->tf_eax = scp->sc_eax;
843 regs->tf_ebx = scp->sc_ebx;
844 regs->tf_ecx = scp->sc_ecx;
845 regs->tf_edx = scp->sc_edx;
846 regs->tf_esi = scp->sc_esi;
847 regs->tf_edi = scp->sc_edi;
848 regs->tf_cs = scp->sc_cs;
849 regs->tf_ss = scp->sc_ss;
850 regs->tf_isp = scp->sc_isp;
852 if (scp->sc_onstack & 01)
853 p->p_sigstk.ss_flags |= SS_ONSTACK;
855 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
857 SIGSETOLD(p->p_sigmask, scp->sc_mask);
858 SIG_CANTMASK(p->p_sigmask);
859 regs->tf_ebp = scp->sc_fp;
860 regs->tf_esp = scp->sc_sp;
861 regs->tf_eip = scp->sc_pc;
862 regs->tf_eflags = eflags;
869 struct sigreturn_args /* {
870 ucontext_t *sigcntxp;
873 struct trapframe *regs;
877 if (((struct osigcontext *)uap->sigcntxp)->sc_trapno == 0x01d516)
878 return osigreturn(p, (struct osigreturn_args *)uap);
880 regs = p->p_md.md_regs;
882 eflags = ucp->uc_mcontext.mc_eflags;
884 if (useracc((caddr_t)ucp, sizeof(ucontext_t), B_WRITE) == 0)
887 if (eflags & PSL_VM) {
888 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
889 struct vm86_kernel *vm86;
892 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
893 * set up the vm86 area, and we can't enter vm86 mode.
895 if (p->p_addr->u_pcb.pcb_ext == 0)
897 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86;
898 if (vm86->vm86_inited == 0)
901 /* go back to user mode if both flags are set */
902 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
903 trapsignal(p, SIGBUS, 0);
905 if (vm86->vm86_has_vme) {
906 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
907 (eflags & VME_USERCHANGE) | PSL_VM;
909 vm86->vm86_eflags = eflags; /* save VIF, VIP */
910 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
912 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
913 tf->tf_eflags = eflags;
914 tf->tf_vm86_ds = tf->tf_ds;
915 tf->tf_vm86_es = tf->tf_es;
916 tf->tf_vm86_fs = tf->tf_fs;
917 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
918 tf->tf_ds = _udatasel;
919 tf->tf_es = _udatasel;
920 tf->tf_fs = _udatasel;
923 * Don't allow users to change privileged or reserved flags.
926 * XXX do allow users to change the privileged flag PSL_RF.
927 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
928 * should sometimes set it there too. tf_eflags is kept in
929 * the signal context during signal handling and there is no
930 * other place to remember it, so the PSL_RF bit may be
931 * corrupted by the signal handler without us knowing.
932 * Corruption of the PSL_RF bit at worst causes one more or
933 * one less debugger trap, so allowing it is fairly harmless.
935 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
936 printf("sigreturn: eflags = 0x%x\n", eflags);
941 * Don't allow users to load a valid privileged %cs. Let the
942 * hardware check for invalid selectors, excess privilege in
943 * other selectors, invalid %eip's and invalid %esp's.
945 cs = ucp->uc_mcontext.mc_cs;
946 if (!CS_SECURE(cs)) {
947 printf("sigreturn: cs = 0x%x\n", cs);
948 trapsignal(p, SIGBUS, T_PROTFLT);
951 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(struct trapframe));
954 if (ucp->uc_mcontext.mc_onstack & 1)
955 p->p_sigstk.ss_flags |= SS_ONSTACK;
957 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
959 p->p_sigmask = ucp->uc_sigmask;
960 SIG_CANTMASK(p->p_sigmask);
965 * Machine dependent boot() routine
967 * I haven't seen anything to put here yet
968 * Possibly some stuff might be grafted back here from boot()
976 * Shutdown the CPU as much as possible
986 * Clear registers on exec
989 setregs(p, entry, stack, ps_strings)
995 struct trapframe *regs = p->p_md.md_regs;
996 struct pcb *pcb = &p->p_addr->u_pcb;
999 /* was i386_user_cleanup() in NetBSD */
1001 if (pcb == curpcb) {
1003 currentldt = _default_ldt;
1005 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
1006 pcb->pcb_ldt_len * sizeof(union descriptor));
1007 pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
1011 bzero((char *)regs, sizeof(struct trapframe));
1012 regs->tf_eip = entry;
1013 regs->tf_esp = stack;
1014 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1015 regs->tf_ss = _udatasel;
1016 regs->tf_ds = _udatasel;
1017 regs->tf_es = _udatasel;
1018 regs->tf_fs = _udatasel;
1019 regs->tf_cs = _ucodesel;
1021 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1022 regs->tf_ebx = ps_strings;
1024 /* reset %gs as well */
1028 pcb->pcb_gs = _udatasel;
1031 * Initialize the math emulator (if any) for the current process.
1032 * Actually, just clear the bit that says that the emulator has
1033 * been initialized. Initialization is delayed until the process
1034 * traps to the emulator (if it is done at all) mainly because
1035 * emulators don't provide an entry point for initialization.
1037 p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP;
1040 * Arrange to trap the next npx or `fwait' instruction (see npx.c
1041 * for why fwait must be trapped at least if there is an npx or an
1042 * emulator). This is mainly to handle the case where npx0 is not
1043 * configured, since the npx routines normally set up the trap
1044 * otherwise. It should be done only at boot time, but doing it
1045 * here allows modifying `npx_exists' for testing the emulator on
1046 * systems with an npx.
1048 load_cr0(rcr0() | CR0_MP | CR0_TS);
1051 /* Initialize the npx (if any) for the current process. */
1052 npxinit(__INITIAL_NPXCW__);
1056 * XXX - Linux emulator
1057 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1064 sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
1067 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1069 if (!error && req->newptr)
1074 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1075 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1077 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1078 CTLFLAG_RW, &disable_rtc_set, 0, "");
1080 SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
1081 CTLFLAG_RD, &bootinfo, bootinfo, "");
1083 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1084 CTLFLAG_RW, &wall_cmos_clock, 0, "");
1087 * Initialize 386 and configure to run kernel
1091 * Initialize segments & interrupt table
1096 union descriptor gdt[NGDT * NCPU]; /* global descriptor table */
1098 union descriptor gdt[NGDT]; /* global descriptor table */
1100 static struct gate_descriptor idt0[NIDT];
1101 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1102 union descriptor ldt[NLDT]; /* local descriptor table */
1104 /* table descriptors - used to load tables by microp */
1105 struct region_descriptor r_gdt, r_idt;
1109 extern struct segment_descriptor common_tssd, *tss_gdt;
1111 int private_tss; /* flag indicating private tss */
1113 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1114 extern int has_f00f_bug;
1117 static struct i386tss dblfault_tss;
1118 static char dblfault_stack[PAGE_SIZE];
1120 extern struct user *proc0paddr;
1123 /* software prototypes -- in more palatable form */
1124 struct soft_segment_descriptor gdt_segs[] = {
1125 /* GNULL_SEL 0 Null Descriptor */
1126 { 0x0, /* segment base address */
1128 0, /* segment type */
1129 0, /* segment descriptor priority level */
1130 0, /* segment descriptor present */
1132 0, /* default 32 vs 16 bit size */
1133 0 /* limit granularity (byte/page units)*/ },
1134 /* GCODE_SEL 1 Code Descriptor for kernel */
1135 { 0x0, /* segment base address */
1136 0xfffff, /* length - all address space */
1137 SDT_MEMERA, /* segment type */
1138 0, /* segment descriptor priority level */
1139 1, /* segment descriptor present */
1141 1, /* default 32 vs 16 bit size */
1142 1 /* limit granularity (byte/page units)*/ },
1143 /* GDATA_SEL 2 Data Descriptor for kernel */
1144 { 0x0, /* segment base address */
1145 0xfffff, /* length - all address space */
1146 SDT_MEMRWA, /* segment type */
1147 0, /* segment descriptor priority level */
1148 1, /* segment descriptor present */
1150 1, /* default 32 vs 16 bit size */
1151 1 /* limit granularity (byte/page units)*/ },
1152 /* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
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 */
1159 1, /* default 32 vs 16 bit size */
1160 1 /* limit granularity (byte/page units)*/ },
1161 /* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1163 0x0, /* segment base address */
1164 sizeof(struct i386tss)-1,/* length - all address space */
1165 SDT_SYS386TSS, /* segment type */
1166 0, /* segment descriptor priority level */
1167 1, /* segment descriptor present */
1169 0, /* unused - default 32 vs 16 bit size */
1170 0 /* limit granularity (byte/page units)*/ },
1171 /* GLDT_SEL 5 LDT Descriptor */
1172 { (int) ldt, /* segment base address */
1173 sizeof(ldt)-1, /* length - all address space */
1174 SDT_SYSLDT, /* segment type */
1175 SEL_UPL, /* segment descriptor priority level */
1176 1, /* segment descriptor present */
1178 0, /* unused - default 32 vs 16 bit size */
1179 0 /* limit granularity (byte/page units)*/ },
1180 /* GUSERLDT_SEL 6 User LDT Descriptor per process */
1181 { (int) ldt, /* segment base address */
1182 (512 * sizeof(union descriptor)-1), /* length */
1183 SDT_SYSLDT, /* segment type */
1184 0, /* segment descriptor priority level */
1185 1, /* segment descriptor present */
1187 0, /* unused - default 32 vs 16 bit size */
1188 0 /* limit granularity (byte/page units)*/ },
1189 /* GTGATE_SEL 7 Null Descriptor - Placeholder */
1190 { 0x0, /* segment base address */
1191 0x0, /* length - all address space */
1192 0, /* segment type */
1193 0, /* segment descriptor priority level */
1194 0, /* segment descriptor present */
1196 0, /* default 32 vs 16 bit size */
1197 0 /* limit granularity (byte/page units)*/ },
1198 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1199 { 0x400, /* segment base address */
1200 0xfffff, /* length */
1201 SDT_MEMRWA, /* segment type */
1202 0, /* segment descriptor priority level */
1203 1, /* segment descriptor present */
1205 1, /* default 32 vs 16 bit size */
1206 1 /* limit granularity (byte/page units)*/ },
1207 /* GPANIC_SEL 9 Panic Tss Descriptor */
1208 { (int) &dblfault_tss, /* segment base address */
1209 sizeof(struct i386tss)-1,/* length - all address space */
1210 SDT_SYS386TSS, /* segment type */
1211 0, /* segment descriptor priority level */
1212 1, /* segment descriptor present */
1214 0, /* unused - default 32 vs 16 bit size */
1215 0 /* limit granularity (byte/page units)*/ },
1216 /* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1217 { 0, /* segment base address (overwritten) */
1218 0xfffff, /* length */
1219 SDT_MEMERA, /* segment type */
1220 0, /* segment descriptor priority level */
1221 1, /* segment descriptor present */
1223 0, /* default 32 vs 16 bit size */
1224 1 /* limit granularity (byte/page units)*/ },
1225 /* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit 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 */
1232 0, /* default 32 vs 16 bit size */
1233 1 /* limit granularity (byte/page units)*/ },
1234 /* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1235 { 0, /* segment base address (overwritten) */
1236 0xfffff, /* length */
1237 SDT_MEMRWA, /* segment type */
1238 0, /* segment descriptor priority level */
1239 1, /* segment descriptor present */
1241 1, /* default 32 vs 16 bit size */
1242 1 /* limit granularity (byte/page units)*/ },
1243 /* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
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 */
1250 0, /* default 32 vs 16 bit size */
1251 1 /* limit granularity (byte/page units)*/ },
1252 /* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
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 */
1259 0, /* default 32 vs 16 bit size */
1260 1 /* limit granularity (byte/page units)*/ },
1263 static struct soft_segment_descriptor ldt_segs[] = {
1264 /* Null Descriptor - overwritten by call gate */
1265 { 0x0, /* segment base address */
1266 0x0, /* length - all address space */
1267 0, /* segment type */
1268 0, /* segment descriptor priority level */
1269 0, /* segment descriptor present */
1271 0, /* default 32 vs 16 bit size */
1272 0 /* limit granularity (byte/page units)*/ },
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 */
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 */
1289 0, /* default 32 vs 16 bit size */
1290 0 /* limit granularity (byte/page units)*/ },
1291 /* Code Descriptor for user */
1292 { 0x0, /* segment base address */
1293 0xfffff, /* length - all address space */
1294 SDT_MEMERA, /* segment type */
1295 SEL_UPL, /* segment descriptor priority level */
1296 1, /* segment descriptor present */
1298 1, /* default 32 vs 16 bit size */
1299 1 /* limit granularity (byte/page units)*/ },
1300 /* Null Descriptor - overwritten by call gate */
1301 { 0x0, /* segment base address */
1302 0x0, /* length - all address space */
1303 0, /* segment type */
1304 0, /* segment descriptor priority level */
1305 0, /* segment descriptor present */
1307 0, /* default 32 vs 16 bit size */
1308 0 /* limit granularity (byte/page units)*/ },
1309 /* Data Descriptor for user */
1310 { 0x0, /* segment base address */
1311 0xfffff, /* length - all address space */
1312 SDT_MEMRWA, /* segment type */
1313 SEL_UPL, /* segment descriptor priority level */
1314 1, /* segment descriptor present */
1316 1, /* default 32 vs 16 bit size */
1317 1 /* limit granularity (byte/page units)*/ },
1321 setidt(idx, func, typ, dpl, selec)
1328 struct gate_descriptor *ip;
1331 ip->gd_looffset = (int)func;
1332 ip->gd_selector = selec;
1338 ip->gd_hioffset = ((int)func)>>16 ;
1341 #define IDTVEC(name) __CONCAT(X,name)
1344 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1345 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1346 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1347 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1348 IDTVEC(syscall), IDTVEC(int0x80_syscall);
1352 struct segment_descriptor *sd;
1353 struct soft_segment_descriptor *ssd;
1355 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1356 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1357 ssd->ssd_type = sd->sd_type;
1358 ssd->ssd_dpl = sd->sd_dpl;
1359 ssd->ssd_p = sd->sd_p;
1360 ssd->ssd_def32 = sd->sd_def32;
1361 ssd->ssd_gran = sd->sd_gran;
1364 #define PHYSMAP_SIZE (2 * 8)
1367 * Populate the (physmap) array with base/bound pairs describing the
1368 * available physical memory in the system, then test this memory and
1369 * build the phys_avail array describing the actually-available memory.
1371 * If we cannot accurately determine the physical memory map, then use
1372 * value from the 0xE801 call, and failing that, the RTC.
1374 * Total memory size may be set by the kernel environment variable
1375 * hw.physmem or the compile-time define MAXMEM.
1378 getmemsize(int first)
1380 int i, physmap_idx, pa_indx;
1381 u_int basemem, extmem;
1382 struct vm86frame vmf;
1383 struct vm86context vmc;
1384 vm_offset_t pa, physmap[PHYSMAP_SIZE];
1393 bzero(&vmf, sizeof(struct vm86frame));
1394 bzero(physmap, sizeof(physmap));
1397 * Perform "base memory" related probes & setup
1399 vm86_intcall(0x12, &vmf);
1400 basemem = vmf.vmf_ax;
1401 if (basemem > 640) {
1402 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1408 * XXX if biosbasemem is now < 640, there is a `hole'
1409 * between the end of base memory and the start of
1410 * ISA memory. The hole may be empty or it may
1411 * contain BIOS code or data. Map it read/write so
1412 * that the BIOS can write to it. (Memory from 0 to
1413 * the physical end of the kernel is mapped read-only
1414 * to begin with and then parts of it are remapped.
1415 * The parts that aren't remapped form holes that
1416 * remain read-only and are unused by the kernel.
1417 * The base memory area is below the physical end of
1418 * the kernel and right now forms a read-only hole.
1419 * The part of it from PAGE_SIZE to
1420 * (trunc_page(biosbasemem * 1024) - 1) will be
1421 * remapped and used by the kernel later.)
1423 * This code is similar to the code used in
1424 * pmap_mapdev, but since no memory needs to be
1425 * allocated we simply change the mapping.
1427 for (pa = trunc_page(basemem * 1024);
1428 pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1429 pte = (pt_entry_t)vtopte(pa + KERNBASE);
1430 *pte = pa | PG_RW | PG_V;
1434 * if basemem != 640, map pages r/w into vm86 page table so
1435 * that the bios can scribble on it.
1437 pte = (pt_entry_t)vm86paddr;
1438 for (i = basemem / 4; i < 160; i++)
1439 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1442 * map page 1 R/W into the kernel page table so we can use it
1443 * as a buffer. The kernel will unmap this page later.
1445 pte = (pt_entry_t)vtopte(KERNBASE + (1 << PAGE_SHIFT));
1446 *pte = (1 << PAGE_SHIFT) | PG_RW | PG_V;
1449 * get memory map with INT 15:E820
1451 #define SMAPSIZ sizeof(*smap)
1452 #define SMAP_SIG 0x534D4150 /* 'SMAP' */
1455 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1456 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1461 vmf.vmf_eax = 0xE820;
1462 vmf.vmf_edx = SMAP_SIG;
1463 vmf.vmf_ecx = SMAPSIZ;
1464 i = vm86_datacall(0x15, &vmf, &vmc);
1465 if (i || vmf.vmf_eax != SMAP_SIG)
1467 if (boothowto & RB_VERBOSE)
1468 printf("SMAP type=%02x base=%08x %08x len=%08x %08x\n",
1470 *(u_int32_t *)((char *)&smap->base + 4),
1471 (u_int32_t)smap->base,
1472 *(u_int32_t *)((char *)&smap->length + 4),
1473 (u_int32_t)smap->length);
1475 if (smap->type != 0x01)
1478 if (smap->length == 0)
1481 if (smap->base >= 0xffffffff) {
1482 printf("%uK of memory above 4GB ignored\n",
1483 (u_int)(smap->length / 1024));
1487 for (i = 0; i <= physmap_idx; i += 2) {
1488 if (smap->base < physmap[i + 1]) {
1489 if (boothowto & RB_VERBOSE)
1491 "Overlapping or non-montonic memory region, ignoring second region\n");
1496 if (smap->base == physmap[physmap_idx + 1]) {
1497 physmap[physmap_idx + 1] += smap->length;
1502 if (physmap_idx == PHYSMAP_SIZE) {
1504 "Too many segments in the physical address map, giving up\n");
1507 physmap[physmap_idx] = smap->base;
1508 physmap[physmap_idx + 1] = smap->base + smap->length;
1510 } while (vmf.vmf_ebx != 0);
1512 if (physmap[1] != 0)
1516 * If we failed above, try memory map with INT 15:E801
1518 vmf.vmf_ax = 0xE801;
1519 if (vm86_intcall(0x15, &vmf) == 0) {
1520 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1524 vm86_intcall(0x15, &vmf);
1525 extmem = vmf.vmf_ax;
1528 * Prefer the RTC value for extended memory.
1530 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1535 * Special hack for chipsets that still remap the 384k hole when
1536 * there's 16MB of memory - this really confuses people that
1537 * are trying to use bus mastering ISA controllers with the
1538 * "16MB limit"; they only have 16MB, but the remapping puts
1539 * them beyond the limit.
1541 * If extended memory is between 15-16MB (16-17MB phys address range),
1544 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1548 physmap[1] = basemem * 1024;
1550 physmap[physmap_idx] = 0x100000;
1551 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1555 * Now, physmap contains a map of physical memory.
1559 /* make hole for AP bootstrap code */
1560 physmap[1] = mp_bootaddress(physmap[1] / 1024);
1562 /* look for the MP hardware - needed for apic addresses */
1567 * Maxmem isn't the "maximum memory", it's one larger than the
1568 * highest page of the physical address space. It should be
1569 * called something like "Maxphyspage". We may adjust this
1570 * based on ``hw.physmem'' and the results of the memory test.
1572 Maxmem = atop(physmap[physmap_idx + 1]);
1575 Maxmem = MAXMEM / 4;
1579 * hw.maxmem is a size in bytes; we also allow k, m, and g suffixes
1580 * for the appropriate modifiers. This overrides MAXMEM.
1582 if ((cp = getenv("hw.physmem")) != NULL) {
1583 u_int64_t AllowMem, sanity;
1586 sanity = AllowMem = strtouq(cp, &ep, 0);
1587 if ((ep != cp) && (*ep != 0)) {
1600 AllowMem = sanity = 0;
1602 if (AllowMem < sanity)
1606 printf("Ignoring invalid memory size of '%s'\n", cp);
1608 Maxmem = atop(AllowMem);
1611 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1612 (boothowto & RB_VERBOSE))
1613 printf("Physical memory use set to %uK\n", Maxmem * 4);
1616 * If Maxmem has been increased beyond what the system has detected,
1617 * extend the last memory segment to the new limit.
1619 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1620 physmap[physmap_idx + 1] = ptoa(Maxmem);
1622 /* call pmap initialization to make new kernel address space */
1623 pmap_bootstrap(first, 0);
1626 * Size up each available chunk of physical memory.
1628 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1630 phys_avail[pa_indx++] = physmap[0];
1631 phys_avail[pa_indx] = physmap[0];
1633 pte = (pt_entry_t)vtopte(KERNBASE);
1635 pte = (pt_entry_t)CMAP1;
1639 * physmap is in bytes, so when converting to page boundaries,
1640 * round up the start address and round down the end address.
1642 for (i = 0; i <= physmap_idx; i += 2) {
1646 if (physmap[i + 1] < end)
1647 end = trunc_page(physmap[i + 1]);
1648 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1653 int *ptr = (int *)CADDR1;
1657 * block out kernel memory as not available.
1659 if (pa >= 0x100000 && pa < first)
1665 * map page into kernel: valid, read/write,non-cacheable
1667 *pte = pa | PG_V | PG_RW | PG_N;
1672 * Test for alternating 1's and 0's
1674 *(volatile int *)ptr = 0xaaaaaaaa;
1675 if (*(volatile int *)ptr != 0xaaaaaaaa) {
1679 * Test for alternating 0's and 1's
1681 *(volatile int *)ptr = 0x55555555;
1682 if (*(volatile int *)ptr != 0x55555555) {
1688 *(volatile int *)ptr = 0xffffffff;
1689 if (*(volatile int *)ptr != 0xffffffff) {
1695 *(volatile int *)ptr = 0x0;
1696 if (*(volatile int *)ptr != 0x0) {
1700 * Restore original value.
1705 * Adjust array of valid/good pages.
1707 if (page_bad == TRUE) {
1711 * If this good page is a continuation of the
1712 * previous set of good pages, then just increase
1713 * the end pointer. Otherwise start a new chunk.
1714 * Note that "end" points one higher than end,
1715 * making the range >= start and < end.
1716 * If we're also doing a speculative memory
1717 * test and we at or past the end, bump up Maxmem
1718 * so that we keep going. The first bad page
1719 * will terminate the loop.
1721 if (phys_avail[pa_indx] == pa) {
1722 phys_avail[pa_indx] += PAGE_SIZE;
1725 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1726 printf("Too many holes in the physical address space, giving up\n");
1730 phys_avail[pa_indx++] = pa; /* start */
1731 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1741 * The last chunk must contain at least one page plus the message
1742 * buffer to avoid complicating other code (message buffer address
1743 * calculation, etc.).
1745 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1746 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1747 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1748 phys_avail[pa_indx--] = 0;
1749 phys_avail[pa_indx--] = 0;
1752 Maxmem = atop(phys_avail[pa_indx]);
1754 /* Trim off space for the message buffer. */
1755 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1757 avail_end = phys_avail[pa_indx];
1765 struct gate_descriptor *gdp;
1768 /* table descriptors - used to load tables by microp */
1769 struct region_descriptor r_gdt, r_idt;
1774 * Prevent lowering of the ipl if we call tsleep() early.
1778 proc0.p_addr = proc0paddr;
1780 atdevbase = ISA_HOLE_START + KERNBASE;
1782 if (bootinfo.bi_modulep) {
1783 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1784 preload_bootstrap_relocate(KERNBASE);
1786 if (bootinfo.bi_envp)
1787 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1790 * make gdt memory segments, the code segment goes up to end of the
1791 * page with etext in it, the data segment goes to the end of
1795 * XXX text protection is temporarily (?) disabled. The limit was
1796 * i386_btop(round_page(etext)) - 1.
1798 gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
1799 gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
1801 gdt_segs[GPRIV_SEL].ssd_limit =
1802 i386_btop(sizeof(struct privatespace)) - 1;
1803 gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[0];
1804 gdt_segs[GPROC0_SEL].ssd_base =
1805 (int) &SMP_prvspace[0].globaldata.gd_common_tss;
1806 SMP_prvspace[0].globaldata.gd_prvspace = &SMP_prvspace[0];
1808 gdt_segs[GPRIV_SEL].ssd_limit = i386_btop(0) - 1;
1809 gdt_segs[GPROC0_SEL].ssd_base = (int) &common_tss;
1812 for (x = 0; x < NGDT; x++) {
1814 /* avoid overwriting db entries with APM ones */
1815 if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL)
1818 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1821 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1822 r_gdt.rd_base = (int) gdt;
1825 /* make ldt memory segments */
1827 * The data segment limit must not cover the user area because we
1828 * don't want the user area to be writable in copyout() etc. (page
1829 * level protection is lost in kernel mode on 386's). Also, we
1830 * don't want the user area to be writable directly (page level
1831 * protection of the user area is not available on 486's with
1832 * CR0_WP set, because there is no user-read/kernel-write mode).
1834 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it
1835 * should be spelled ...MAX_USER...
1837 #define VM_END_USER_RW_ADDRESS VM_MAXUSER_ADDRESS
1839 * The code segment limit has to cover the user area until we move
1840 * the signal trampoline out of the user area. This is safe because
1841 * the code segment cannot be written to directly.
1843 #define VM_END_USER_R_ADDRESS (VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1844 ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1845 ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1846 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1847 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1849 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1852 currentldt = _default_ldt;
1856 for (x = 0; x < NIDT; x++)
1857 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1858 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1859 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1860 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1861 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1862 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1863 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1864 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1865 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1866 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1867 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1868 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1869 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1870 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1871 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1872 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1873 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1874 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1875 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1876 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1877 setidt(0x80, &IDTVEC(int0x80_syscall),
1878 SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1880 r_idt.rd_limit = sizeof(idt0) - 1;
1881 r_idt.rd_base = (int) idt;
1885 * Initialize the console before we print anything out.
1897 if (boothowto & RB_KDB)
1898 Debugger("Boot flags requested debugger");
1901 finishidentcpu(); /* Final stage of CPU initialization */
1902 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1903 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1904 initializecpu(); /* Initialize CPU registers */
1906 /* make an initial tss so cpu can get interrupt stack on syscall! */
1907 common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE - 16;
1908 common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1909 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1911 tss_gdt = &gdt[GPROC0_SEL].sd;
1912 common_tssd = *tss_gdt;
1913 common_tss.tss_ioopt = (sizeof common_tss) << 16;
1916 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1917 dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1918 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1919 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1920 dblfault_tss.tss_cr3 = (int)IdlePTD;
1921 dblfault_tss.tss_eip = (int) dblfault_handler;
1922 dblfault_tss.tss_eflags = PSL_KERNEL;
1923 dblfault_tss.tss_ds = dblfault_tss.tss_es =
1924 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1925 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1926 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1927 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1932 /* now running on new page tables, configured,and u/iom is accessible */
1934 /* Map the message buffer. */
1935 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1936 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1938 msgbufinit(msgbufp, MSGBUF_SIZE);
1940 /* make a call gate to reenter kernel with */
1941 gdp = &ldt[LSYS5CALLS_SEL].gd;
1943 x = (int) &IDTVEC(syscall);
1944 gdp->gd_looffset = x++;
1945 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1947 gdp->gd_type = SDT_SYS386CGT;
1948 gdp->gd_dpl = SEL_UPL;
1950 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1952 /* XXX does this work? */
1953 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1954 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
1956 /* transfer to user mode */
1958 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1959 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1961 /* setup proc 0's pcb */
1962 proc0.p_addr->u_pcb.pcb_flags = 0;
1963 proc0.p_addr->u_pcb.pcb_cr3 = (int)IdlePTD;
1965 proc0.p_addr->u_pcb.pcb_mpnest = 1;
1967 proc0.p_addr->u_pcb.pcb_ext = 0;
1970 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1971 static void f00f_hack(void *unused);
1972 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
1975 f00f_hack(void *unused) {
1976 struct gate_descriptor *new_idt;
1978 struct region_descriptor r_idt;
1985 printf("Intel Pentium detected, installing workaround for F00F bug\n");
1987 r_idt.rd_limit = sizeof(idt0) - 1;
1989 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
1991 panic("kmem_alloc returned 0");
1992 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
1993 panic("kmem_alloc returned non-page-aligned memory");
1994 /* Put the first seven entries in the lower page */
1995 new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
1996 bcopy(idt, new_idt, sizeof(idt0));
1997 r_idt.rd_base = (int)new_idt;
2000 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2001 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2002 panic("vm_map_protect failed");
2005 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2008 ptrace_set_pc(p, addr)
2012 p->p_md.md_regs->tf_eip = addr;
2017 ptrace_single_step(p)
2020 p->p_md.md_regs->tf_eflags |= PSL_T;
2024 int ptrace_read_u_check(p, addr, len)
2031 if ((vm_offset_t) (addr + len) < addr)
2033 if ((vm_offset_t) (addr + len) <= sizeof(struct user))
2036 gap = (char *) p->p_md.md_regs - (char *) p->p_addr;
2038 if ((vm_offset_t) addr < gap)
2040 if ((vm_offset_t) (addr + len) <=
2041 (vm_offset_t) (gap + sizeof(struct trapframe)))
2046 int ptrace_write_u(p, off, data)
2051 struct trapframe frame_copy;
2053 struct trapframe *tp;
2056 * Privileged kernel state is scattered all over the user area.
2057 * Only allow write access to parts of regs and to fpregs.
2059 min = (char *)p->p_md.md_regs - (char *)p->p_addr;
2060 if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
2061 tp = p->p_md.md_regs;
2063 *(int *)((char *)&frame_copy + (off - min)) = data;
2064 if (!EFL_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
2065 !CS_SECURE(frame_copy.tf_cs))
2067 *(int*)((char *)p->p_addr + off) = data;
2070 min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
2071 if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
2072 *(int*)((char *)p->p_addr + off) = data;
2084 struct trapframe *tp;
2086 tp = p->p_md.md_regs;
2087 regs->r_fs = tp->tf_fs;
2088 regs->r_es = tp->tf_es;
2089 regs->r_ds = tp->tf_ds;
2090 regs->r_edi = tp->tf_edi;
2091 regs->r_esi = tp->tf_esi;
2092 regs->r_ebp = tp->tf_ebp;
2093 regs->r_ebx = tp->tf_ebx;
2094 regs->r_edx = tp->tf_edx;
2095 regs->r_ecx = tp->tf_ecx;
2096 regs->r_eax = tp->tf_eax;
2097 regs->r_eip = tp->tf_eip;
2098 regs->r_cs = tp->tf_cs;
2099 regs->r_eflags = tp->tf_eflags;
2100 regs->r_esp = tp->tf_esp;
2101 regs->r_ss = tp->tf_ss;
2102 pcb = &p->p_addr->u_pcb;
2103 regs->r_gs = pcb->pcb_gs;
2113 struct trapframe *tp;
2115 tp = p->p_md.md_regs;
2116 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2117 !CS_SECURE(regs->r_cs))
2119 tp->tf_fs = regs->r_fs;
2120 tp->tf_es = regs->r_es;
2121 tp->tf_ds = regs->r_ds;
2122 tp->tf_edi = regs->r_edi;
2123 tp->tf_esi = regs->r_esi;
2124 tp->tf_ebp = regs->r_ebp;
2125 tp->tf_ebx = regs->r_ebx;
2126 tp->tf_edx = regs->r_edx;
2127 tp->tf_ecx = regs->r_ecx;
2128 tp->tf_eax = regs->r_eax;
2129 tp->tf_eip = regs->r_eip;
2130 tp->tf_cs = regs->r_cs;
2131 tp->tf_eflags = regs->r_eflags;
2132 tp->tf_esp = regs->r_esp;
2133 tp->tf_ss = regs->r_ss;
2134 pcb = &p->p_addr->u_pcb;
2135 pcb->pcb_gs = regs->r_gs;
2140 fill_fpregs(p, fpregs)
2142 struct fpreg *fpregs;
2144 bcopy(&p->p_addr->u_pcb.pcb_savefpu, fpregs, sizeof *fpregs);
2149 set_fpregs(p, fpregs)
2151 struct fpreg *fpregs;
2153 bcopy(fpregs, &p->p_addr->u_pcb.pcb_savefpu, sizeof *fpregs);
2158 fill_dbregs(p, dbregs)
2160 struct dbreg *dbregs;
2164 pcb = &p->p_addr->u_pcb;
2165 dbregs->dr0 = pcb->pcb_dr0;
2166 dbregs->dr1 = pcb->pcb_dr1;
2167 dbregs->dr2 = pcb->pcb_dr2;
2168 dbregs->dr3 = pcb->pcb_dr3;
2171 dbregs->dr6 = pcb->pcb_dr6;
2172 dbregs->dr7 = pcb->pcb_dr7;
2177 set_dbregs(p, dbregs)
2179 struct dbreg *dbregs;
2183 pcb = &p->p_addr->u_pcb;
2186 * Don't let a process set a breakpoint that is not within the
2187 * process's address space. If a process could do this, it
2188 * could halt the system by setting a breakpoint in the kernel
2189 * (if ddb was enabled). Thus, we need to check to make sure
2190 * that no breakpoints are being enabled for addresses outside
2191 * process's address space, unless, perhaps, we were called by
2194 * XXX - what about when the watched area of the user's
2195 * address space is written into from within the kernel
2196 * ... wouldn't that still cause a breakpoint to be generated
2197 * from within kernel mode?
2200 if (p->p_cred->pc_ucred->cr_uid != 0) {
2201 if (dbregs->dr7 & 0x3) {
2202 /* dr0 is enabled */
2203 if (dbregs->dr0 >= VM_MAXUSER_ADDRESS)
2207 if (dbregs->dr7 & (0x3<<2)) {
2208 /* dr1 is enabled */
2209 if (dbregs->dr1 >= VM_MAXUSER_ADDRESS)
2213 if (dbregs->dr7 & (0x3<<4)) {
2214 /* dr2 is enabled */
2215 if (dbregs->dr2 >= VM_MAXUSER_ADDRESS)
2219 if (dbregs->dr7 & (0x3<<6)) {
2220 /* dr3 is enabled */
2221 if (dbregs->dr3 >= VM_MAXUSER_ADDRESS)
2226 pcb->pcb_dr0 = dbregs->dr0;
2227 pcb->pcb_dr1 = dbregs->dr1;
2228 pcb->pcb_dr2 = dbregs->dr2;
2229 pcb->pcb_dr3 = dbregs->dr3;
2230 pcb->pcb_dr6 = dbregs->dr6;
2231 pcb->pcb_dr7 = dbregs->dr7;
2233 pcb->pcb_flags |= PCB_DBREGS;
2240 Debugger(const char *msg)
2242 printf("Debugger(\"%s\") called.\n", msg);
2246 #include <sys/disklabel.h>
2249 * Determine the size of the transfer, and make sure it is
2250 * within the boundaries of the partition. Adjust transfer
2251 * if needed, and signal errors or early completion.
2254 bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
2256 struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
2257 int labelsect = lp->d_partitions[0].p_offset;
2258 int maxsz = p->p_size,
2259 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
2261 /* overwriting disk label ? */
2262 /* XXX should also protect bootstrap in first 8K */
2263 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
2264 #if LABELSECTOR != 0
2265 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
2267 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2268 bp->b_error = EROFS;
2272 #if defined(DOSBBSECTOR) && defined(notyet)
2273 /* overwriting master boot record? */
2274 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
2275 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
2276 bp->b_error = EROFS;
2281 /* beyond partition? */
2282 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
2283 /* if exactly at end of disk, return an EOF */
2284 if (bp->b_blkno == maxsz) {
2285 bp->b_resid = bp->b_bcount;
2288 /* or truncate if part of it fits */
2289 sz = maxsz - bp->b_blkno;
2291 bp->b_error = EINVAL;
2294 bp->b_bcount = sz << DEV_BSHIFT;
2297 bp->b_pblkno = bp->b_blkno + p->p_offset;
2301 bp->b_flags |= B_ERROR;
2308 * Provide inb() and outb() as functions. They are normally only
2309 * available as macros calling inlined functions, thus cannot be
2310 * called inside DDB.
2312 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2318 /* silence compiler warnings */
2320 void outb(u_int, u_char);
2327 * We use %%dx and not %1 here because i/o is done at %dx and not at
2328 * %edx, while gcc generates inferior code (movw instead of movl)
2329 * if we tell it to load (u_short) port.
2331 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2336 outb(u_int port, u_char data)
2340 * Use an unnecessary assignment to help gcc's register allocator.
2341 * This make a large difference for gcc-1.40 and a tiny difference
2342 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2343 * best results. gcc-2.6.0 can't handle this.
2346 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));