debuggers.hg

view freebsd-5.3-xen-sparse/i386-xen/i386-xen/machdep.c @ 4628:35357e323f14

bitkeeper revision 1.1338 (4266317ezHysqYzH_WRvfueqwU4i4Q)

Grant tables for FreeBSD.
Signed-off-by: Kip Macy <kmacy@fsmware.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 20 10:39:58 2005 +0000 (2005-04-20)
parents 5fd95a1db011
children f9f8f250228f
line source
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 */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/sys/i386/i386/machdep.c,v 1.584 2003/12/03 21:12:09 jhb Exp $");
43 #include "opt_apic.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_isa.h"
51 #include "opt_kstack_pages.h"
52 #include "opt_maxmem.h"
53 #include "opt_msgbuf.h"
54 #include "opt_npx.h"
55 #include "opt_perfmon.h"
56 #include "opt_xen.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/sysproto.h>
61 #include <sys/signalvar.h>
62 #include <sys/imgact.h>
63 #include <sys/kdb.h>
64 #include <sys/kernel.h>
65 #include <sys/ktr.h>
66 #include <sys/linker.h>
67 #include <sys/lock.h>
68 #include <sys/malloc.h>
69 #include <sys/memrange.h>
70 #include <sys/mutex.h>
71 #include <sys/pcpu.h>
72 #include <sys/proc.h>
73 #include <sys/bio.h>
74 #include <sys/buf.h>
75 #include <sys/reboot.h>
76 #include <sys/callout.h>
77 #include <sys/msgbuf.h>
78 #include <sys/sched.h>
79 #include <sys/sysent.h>
80 #include <sys/sysctl.h>
81 #include <sys/ucontext.h>
82 #include <sys/vmmeter.h>
83 #include <sys/bus.h>
84 #include <sys/eventhandler.h>
86 #include <vm/vm.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_pager.h>
93 #include <vm/vm_extern.h>
95 #include <sys/user.h>
96 #include <sys/exec.h>
97 #include <sys/cons.h>
99 #ifdef DDB
100 #ifndef KDB
101 #error KDB must be enabled in order for DDB to work!
102 #endif
103 #include <ddb/ddb.h>
104 #include <ddb/db_sym.h>
105 #endif
107 #include <net/netisr.h>
109 #include <machine/cpu.h>
110 #include <machine/cputypes.h>
111 #include <machine/reg.h>
112 #include <machine/clock.h>
113 #include <machine/specialreg.h>
114 #include <machine/bootinfo.h>
115 #include <machine/intr_machdep.h>
116 #include <machine/md_var.h>
117 #include <machine/pc/bios.h>
118 #include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
119 #include <machine/proc.h>
120 #ifdef PERFMON
121 #include <machine/perfmon.h>
122 #endif
123 #ifdef SMP
124 #include <machine/privatespace.h>
125 #include <machine/smp.h>
126 #endif
128 #ifdef DEV_ISA
129 #include <i386/isa/icu.h>
130 #endif
132 #include <isa/rtc.h>
133 #include <sys/ptrace.h>
134 #include <machine/sigframe.h>
137 /* XEN includes */
138 #include <machine/hypervisor-ifs.h>
139 #include <machine/xen-os.h>
140 #include <machine/hypervisor.h>
141 #include <machine/xenfunc.h>
142 #include <machine/xenvar.h>
143 #include <machine/xen_intr.h>
145 void Xhypervisor_callback(void);
146 void failsafe_callback(void);
148 /***************/
151 /* Sanity check for __curthread() */
152 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
154 extern void init386(void);
155 extern void dblfault_handler(void);
157 extern void printcpuinfo(void); /* XXX header file */
158 extern void finishidentcpu(void);
159 extern void panicifcpuunsupported(void);
160 extern void initializecpu(void);
161 void initvalues(start_info_t *startinfo);
163 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
164 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
166 #if !defined(CPU_ENABLE_SSE) && defined(I686_CPU)
167 #define CPU_ENABLE_SSE
168 #endif
169 #if defined(CPU_DISABLE_SSE)
170 #undef CPU_ENABLE_SSE
171 #endif
173 static void cpu_startup(void *);
174 static void fpstate_drop(struct thread *td);
175 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
176 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
177 #ifdef CPU_ENABLE_SSE
178 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
179 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
180 #endif /* CPU_ENABLE_SSE */
181 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
183 #ifdef DDB
184 extern vm_offset_t ksym_start, ksym_end;
185 #endif
187 int _udatasel, _ucodesel;
188 u_int basemem;
190 start_info_t *xen_start_info;
191 unsigned long *xen_phys_machine;
192 int xendebug_flags;
193 int init_first = 0;
194 int cold = 1;
196 #ifdef COMPAT_43
197 static void osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code);
198 #endif
199 #ifdef COMPAT_FREEBSD4
200 static void freebsd4_sendsig(sig_t catcher, int sig, sigset_t *mask,
201 u_long code);
202 #endif
204 long Maxmem = 0;
206 vm_paddr_t phys_avail[10];
208 /* must be 2 less so 0 0 can signal end of chunks */
209 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
211 struct kva_md_info kmi;
213 static struct trapframe proc0_tf;
214 #ifndef SMP
215 static struct pcpu __pcpu;
216 #endif
217 struct mtx icu_lock;
219 struct mem_range_softc mem_range_softc;
221 static void
222 cpu_startup(void *dummy)
223 {
224 /*
225 * Good {morning,afternoon,evening,night}.
226 */
227 startrtclock();
229 printcpuinfo();
230 panicifcpuunsupported();
231 #ifdef PERFMON
232 perfmon_init();
233 #endif
234 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem),
235 ptoa((uintmax_t)Maxmem) / 1048576);
236 /*
237 * Display any holes after the first chunk of extended memory.
238 */
239 if (bootverbose) {
240 int indx;
242 printf("Physical memory chunk(s):\n");
243 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
244 vm_paddr_t size;
246 size = phys_avail[indx + 1] - phys_avail[indx];
247 printf(
248 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
249 (uintmax_t)phys_avail[indx],
250 (uintmax_t)phys_avail[indx + 1] - 1,
251 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
252 }
253 }
255 vm_ksubmap_init(&kmi);
257 printf("avail memory = %ju (%ju MB)\n",
258 ptoa((uintmax_t)cnt.v_free_count),
259 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
261 /*
262 * Set up buffers, so they can be used to read disk labels.
263 */
264 bufinit();
265 vm_pager_bufferinit();
267 cpu_setregs();
269 }
271 /*
272 * Send an interrupt to process.
273 *
274 * Stack is set up to allow sigcode stored
275 * at top to call routine, followed by kcall
276 * to sigreturn routine below. After sigreturn
277 * resets the signal mask, the stack, and the
278 * frame pointer, it returns to the user
279 * specified pc, psl.
280 */
281 #ifdef COMPAT_43
282 static void
283 osendsig(catcher, sig, mask, code)
284 sig_t catcher;
285 int sig;
286 sigset_t *mask;
287 u_long code;
288 {
289 struct osigframe sf, *fp;
290 struct proc *p;
291 struct thread *td;
292 struct sigacts *psp;
293 struct trapframe *regs;
294 int oonstack;
296 td = curthread;
297 p = td->td_proc;
298 PROC_LOCK_ASSERT(p, MA_OWNED);
299 psp = p->p_sigacts;
300 mtx_assert(&psp->ps_mtx, MA_OWNED);
301 regs = td->td_frame;
302 oonstack = sigonstack(regs->tf_esp);
304 /* Allocate space for the signal handler context. */
305 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
306 SIGISMEMBER(psp->ps_sigonstack, sig)) {
307 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
308 td->td_sigstk.ss_size - sizeof(struct osigframe));
309 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
310 td->td_sigstk.ss_flags |= SS_ONSTACK;
311 #endif
312 } else
313 fp = (struct osigframe *)regs->tf_esp - 1;
315 /* Translate the signal if appropriate. */
316 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
317 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
319 /* Build the argument list for the signal handler. */
320 sf.sf_signum = sig;
321 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
322 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
323 /* Signal handler installed with SA_SIGINFO. */
324 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
325 sf.sf_siginfo.si_signo = sig;
326 sf.sf_siginfo.si_code = code;
327 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
328 } else {
329 /* Old FreeBSD-style arguments. */
330 sf.sf_arg2 = code;
331 sf.sf_addr = regs->tf_err;
332 sf.sf_ahu.sf_handler = catcher;
333 }
334 mtx_unlock(&psp->ps_mtx);
335 PROC_UNLOCK(p);
337 /* Save most if not all of trap frame. */
338 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
339 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
340 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
341 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
342 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
343 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
344 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
345 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
346 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
347 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
348 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
349 sf.sf_siginfo.si_sc.sc_gs = rgs();
350 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
352 /* Build the signal context to be used by osigreturn(). */
353 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
354 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
355 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
356 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
357 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
358 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
359 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
360 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
362 /*
363 * Copy the sigframe out to the user's stack.
364 */
365 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
366 #ifdef DEBUG
367 printf("process %ld has trashed its stack\n", (long)p->p_pid);
368 #endif
369 PROC_LOCK(p);
370 sigexit(td, SIGILL);
371 }
373 regs->tf_esp = (int)fp;
374 regs->tf_eip = PS_STRINGS - szosigcode;
375 regs->tf_eflags &= ~PSL_T;
376 regs->tf_cs = _ucodesel;
377 regs->tf_ds = _udatasel;
378 regs->tf_es = _udatasel;
379 regs->tf_fs = _udatasel;
380 load_gs(_udatasel);
381 regs->tf_ss = _udatasel;
382 PROC_LOCK(p);
383 mtx_lock(&psp->ps_mtx);
384 }
385 #endif /* COMPAT_43 */
387 #ifdef COMPAT_FREEBSD4
388 static void
389 freebsd4_sendsig(catcher, sig, mask, code)
390 sig_t catcher;
391 int sig;
392 sigset_t *mask;
393 u_long code;
394 {
395 struct sigframe4 sf, *sfp;
396 struct proc *p;
397 struct thread *td;
398 struct sigacts *psp;
399 struct trapframe *regs;
400 int oonstack;
402 td = curthread;
403 p = td->td_proc;
404 PROC_LOCK_ASSERT(p, MA_OWNED);
405 psp = p->p_sigacts;
406 mtx_assert(&psp->ps_mtx, MA_OWNED);
407 regs = td->td_frame;
408 oonstack = sigonstack(regs->tf_esp);
410 /* Save user context. */
411 bzero(&sf, sizeof(sf));
412 sf.sf_uc.uc_sigmask = *mask;
413 sf.sf_uc.uc_stack = td->td_sigstk;
414 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
415 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
416 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
417 sf.sf_uc.uc_mcontext.mc_gs = rgs();
418 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
420 /* Allocate space for the signal handler context. */
421 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
422 SIGISMEMBER(psp->ps_sigonstack, sig)) {
423 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
424 td->td_sigstk.ss_size - sizeof(struct sigframe4));
425 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
426 td->td_sigstk.ss_flags |= SS_ONSTACK;
427 #endif
428 } else
429 sfp = (struct sigframe4 *)regs->tf_esp - 1;
431 /* Translate the signal if appropriate. */
432 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
433 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
435 /* Build the argument list for the signal handler. */
436 sf.sf_signum = sig;
437 sf.sf_ucontext = (register_t)&sfp->sf_uc;
438 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
439 /* Signal handler installed with SA_SIGINFO. */
440 sf.sf_siginfo = (register_t)&sfp->sf_si;
441 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
443 /* Fill in POSIX parts */
444 sf.sf_si.si_signo = sig;
445 sf.sf_si.si_code = code;
446 sf.sf_si.si_addr = (void *)regs->tf_err;
447 } else {
448 /* Old FreeBSD-style arguments. */
449 sf.sf_siginfo = code;
450 sf.sf_addr = regs->tf_err;
451 sf.sf_ahu.sf_handler = catcher;
452 }
453 mtx_unlock(&psp->ps_mtx);
454 PROC_UNLOCK(p);
456 /*
457 * Copy the sigframe out to the user's stack.
458 */
459 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
460 #ifdef DEBUG
461 printf("process %ld has trashed its stack\n", (long)p->p_pid);
462 #endif
463 PROC_LOCK(p);
464 sigexit(td, SIGILL);
465 }
467 regs->tf_esp = (int)sfp;
468 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
469 regs->tf_eflags &= ~PSL_T;
470 regs->tf_cs = _ucodesel;
471 regs->tf_ds = _udatasel;
472 regs->tf_es = _udatasel;
473 regs->tf_fs = _udatasel;
474 regs->tf_ss = _udatasel;
475 PROC_LOCK(p);
476 mtx_lock(&psp->ps_mtx);
477 }
478 #endif /* COMPAT_FREEBSD4 */
480 void
481 sendsig(catcher, sig, mask, code)
482 sig_t catcher;
483 int sig;
484 sigset_t *mask;
485 u_long code;
486 {
487 struct sigframe sf, *sfp;
488 struct proc *p;
489 struct thread *td;
490 struct sigacts *psp;
491 char *sp;
492 struct trapframe *regs;
493 int oonstack;
495 td = curthread;
496 p = td->td_proc;
497 PROC_LOCK_ASSERT(p, MA_OWNED);
498 psp = p->p_sigacts;
499 mtx_assert(&psp->ps_mtx, MA_OWNED);
500 #ifdef COMPAT_FREEBSD4
501 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
502 freebsd4_sendsig(catcher, sig, mask, code);
503 return;
504 }
505 #endif
506 #ifdef COMPAT_43
507 if (SIGISMEMBER(psp->ps_osigset, sig)) {
508 osendsig(catcher, sig, mask, code);
509 return;
510 }
511 #endif
512 regs = td->td_frame;
513 oonstack = sigonstack(regs->tf_esp);
515 /* Save user context. */
516 bzero(&sf, sizeof(sf));
517 sf.sf_uc.uc_sigmask = *mask;
518 sf.sf_uc.uc_stack = td->td_sigstk;
519 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
520 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
521 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
522 sf.sf_uc.uc_mcontext.mc_gs = rgs();
523 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
524 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
525 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
526 fpstate_drop(td);
528 /* Allocate space for the signal handler context. */
529 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
530 SIGISMEMBER(psp->ps_sigonstack, sig)) {
531 sp = td->td_sigstk.ss_sp +
532 td->td_sigstk.ss_size - sizeof(struct sigframe);
533 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
534 td->td_sigstk.ss_flags |= SS_ONSTACK;
535 #endif
536 } else
537 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
538 /* Align to 16 bytes. */
539 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
541 /* Translate the signal if appropriate. */
542 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
543 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
545 /* Build the argument list for the signal handler. */
546 sf.sf_signum = sig;
547 sf.sf_ucontext = (register_t)&sfp->sf_uc;
548 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
549 /* Signal handler installed with SA_SIGINFO. */
550 sf.sf_siginfo = (register_t)&sfp->sf_si;
551 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
553 /* Fill in POSIX parts */
554 sf.sf_si.si_signo = sig;
555 sf.sf_si.si_code = code;
556 sf.sf_si.si_addr = (void *)regs->tf_err;
557 } else {
558 /* Old FreeBSD-style arguments. */
559 sf.sf_siginfo = code;
560 sf.sf_addr = regs->tf_err;
561 sf.sf_ahu.sf_handler = catcher;
562 }
563 mtx_unlock(&psp->ps_mtx);
564 PROC_UNLOCK(p);
565 /*
566 * Copy the sigframe out to the user's stack.
567 */
568 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
569 #ifdef DEBUG
570 printf("process %ld has trashed its stack\n", (long)p->p_pid);
571 #endif
572 PROC_LOCK(p);
573 sigexit(td, SIGILL);
574 }
576 regs->tf_esp = (int)sfp;
577 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
578 regs->tf_eflags &= ~PSL_T;
579 regs->tf_cs = _ucodesel;
580 regs->tf_ds = _udatasel;
581 regs->tf_es = _udatasel;
582 regs->tf_fs = _udatasel;
583 regs->tf_ss = _udatasel;
584 PROC_LOCK(p);
585 mtx_lock(&psp->ps_mtx);
586 }
588 /*
589 * Build siginfo_t for SA thread
590 */
591 void
592 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
593 {
594 struct proc *p;
595 struct thread *td;
597 td = curthread;
598 p = td->td_proc;
599 PROC_LOCK_ASSERT(p, MA_OWNED);
601 bzero(si, sizeof(*si));
602 si->si_signo = sig;
603 si->si_code = code;
604 si->si_addr = (void *)td->td_frame->tf_err;
605 /* XXXKSE fill other fields */
606 }
608 /*
609 * System call to cleanup state after a signal
610 * has been taken. Reset signal mask and
611 * stack state from context left by sendsig (above).
612 * Return to previous pc and psl as specified by
613 * context left by sendsig. Check carefully to
614 * make sure that the user has not modified the
615 * state to gain improper privileges.
616 *
617 * MPSAFE
618 */
619 #ifdef COMPAT_43
620 int
621 osigreturn(td, uap)
622 struct thread *td;
623 struct osigreturn_args /* {
624 struct osigcontext *sigcntxp;
625 } */ *uap;
626 {
627 struct osigcontext sc;
628 struct trapframe *regs;
629 struct osigcontext *scp;
630 struct proc *p = td->td_proc;
631 int eflags, error;
633 regs = td->td_frame;
634 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
635 if (error != 0)
636 return (error);
637 scp = &sc;
638 eflags = scp->sc_ps;
639 /*
640 * Don't allow users to change privileged or reserved flags.
641 */
642 /*
643 * XXX do allow users to change the privileged flag PSL_RF.
644 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
645 * should sometimes set it there too. tf_eflags is kept in
646 * the signal context during signal handling and there is no
647 * other place to remember it, so the PSL_RF bit may be
648 * corrupted by the signal handler without us knowing.
649 * Corruption of the PSL_RF bit at worst causes one more or
650 * one less debugger trap, so allowing it is fairly harmless.
651 */
652 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
653 return (EINVAL);
654 }
656 /*
657 * Don't allow users to load a valid privileged %cs. Let the
658 * hardware check for invalid selectors, excess privilege in
659 * other selectors, invalid %eip's and invalid %esp's.
660 */
661 if (!CS_SECURE(scp->sc_cs)) {
662 trapsignal(td, SIGBUS, T_PROTFLT);
663 return (EINVAL);
664 }
665 regs->tf_ds = scp->sc_ds;
666 regs->tf_es = scp->sc_es;
667 regs->tf_fs = scp->sc_fs;
669 /* Restore remaining registers. */
670 regs->tf_eax = scp->sc_eax;
671 regs->tf_ebx = scp->sc_ebx;
672 regs->tf_ecx = scp->sc_ecx;
673 regs->tf_edx = scp->sc_edx;
674 regs->tf_esi = scp->sc_esi;
675 regs->tf_edi = scp->sc_edi;
676 regs->tf_cs = scp->sc_cs;
677 regs->tf_ss = scp->sc_ss;
678 regs->tf_isp = scp->sc_isp;
679 regs->tf_ebp = scp->sc_fp;
680 regs->tf_esp = scp->sc_sp;
681 regs->tf_eip = scp->sc_pc;
682 regs->tf_eflags = eflags;
684 PROC_LOCK(p);
685 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
686 if (scp->sc_onstack & 1)
687 td->td_sigstk.ss_flags |= SS_ONSTACK;
688 else
689 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
690 #endif
691 SIGSETOLD(td->td_sigmask, scp->sc_mask);
692 SIG_CANTMASK(td->td_sigmask);
693 signotify(td);
694 PROC_UNLOCK(p);
695 return (EJUSTRETURN);
696 }
697 #endif /* COMPAT_43 */
699 #ifdef COMPAT_FREEBSD4
700 /*
701 * MPSAFE
702 */
703 int
704 freebsd4_sigreturn(td, uap)
705 struct thread *td;
706 struct freebsd4_sigreturn_args /* {
707 const ucontext4 *sigcntxp;
708 } */ *uap;
709 {
710 struct ucontext4 uc;
711 struct proc *p = td->td_proc;
712 struct trapframe *regs;
713 const struct ucontext4 *ucp;
714 int cs, eflags, error;
716 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
717 if (error != 0)
718 return (error);
719 ucp = &uc;
720 regs = td->td_frame;
721 eflags = ucp->uc_mcontext.mc_eflags;
722 /*
723 * Don't allow users to change privileged or reserved flags.
724 */
725 /*
726 * XXX do allow users to change the privileged flag PSL_RF.
727 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
728 * should sometimes set it there too. tf_eflags is kept in
729 * the signal context during signal handling and there is no
730 * other place to remember it, so the PSL_RF bit may be
731 * corrupted by the signal handler without us knowing.
732 * Corruption of the PSL_RF bit at worst causes one more or
733 * one less debugger trap, so allowing it is fairly harmless.
734 */
735 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
736 printf("freebsd4_sigreturn: eflags = 0x%x\n", eflags);
737 return (EINVAL);
738 }
740 /*
741 * Don't allow users to load a valid privileged %cs. Let the
742 * hardware check for invalid selectors, excess privilege in
743 * other selectors, invalid %eip's and invalid %esp's.
744 */
745 cs = ucp->uc_mcontext.mc_cs;
746 if (!CS_SECURE(cs)) {
747 printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
748 trapsignal(td, SIGBUS, T_PROTFLT);
749 return (EINVAL);
750 }
752 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
754 PROC_LOCK(p);
755 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
756 if (ucp->uc_mcontext.mc_onstack & 1)
757 td->td_sigstk.ss_flags |= SS_ONSTACK;
758 else
759 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
760 #endif
762 td->td_sigmask = ucp->uc_sigmask;
763 SIG_CANTMASK(td->td_sigmask);
764 signotify(td);
765 PROC_UNLOCK(p);
766 return (EJUSTRETURN);
767 }
768 #endif /* COMPAT_FREEBSD4 */
770 /*
771 * MPSAFE
772 */
773 int
774 sigreturn(td, uap)
775 struct thread *td;
776 struct sigreturn_args /* {
777 const __ucontext *sigcntxp;
778 } */ *uap;
779 {
780 ucontext_t uc;
781 struct proc *p = td->td_proc;
782 struct trapframe *regs;
783 const ucontext_t *ucp;
784 int cs, eflags, error, ret;
786 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
787 if (error != 0)
788 return (error);
789 ucp = &uc;
790 regs = td->td_frame;
791 eflags = ucp->uc_mcontext.mc_eflags;
792 /*
793 * Don't allow users to change privileged or reserved flags.
794 */
795 /*
796 * XXX do allow users to change the privileged flag PSL_RF.
797 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
798 * should sometimes set it there too. tf_eflags is kept in
799 * the signal context during signal handling and there is no
800 * other place to remember it, so the PSL_RF bit may be
801 * corrupted by the signal handler without us knowing.
802 * Corruption of the PSL_RF bit at worst causes one more or
803 * one less debugger trap, so allowing it is fairly harmless.
804 */
805 #if 0
806 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
807 __asm__("int $0x3");
808 printf("sigreturn: eflags = 0x%x\n", eflags);
809 return (EINVAL);
810 }
811 #endif
812 /*
813 * Don't allow users to load a valid privileged %cs. Let the
814 * hardware check for invalid selectors, excess privilege in
815 * other selectors, invalid %eip's and invalid %esp's.
816 */
817 cs = ucp->uc_mcontext.mc_cs;
818 if (!CS_SECURE(cs)) {
819 __asm__("int $0x3");
820 printf("sigreturn: cs = 0x%x\n", cs);
821 trapsignal(td, SIGBUS, T_PROTFLT);
822 return (EINVAL);
823 }
825 ret = set_fpcontext(td, &ucp->uc_mcontext);
826 if (ret != 0)
827 return (ret);
828 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
829 PROC_LOCK(p);
830 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
831 if (ucp->uc_mcontext.mc_onstack & 1)
832 td->td_sigstk.ss_flags |= SS_ONSTACK;
833 else
834 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
835 #endif
837 td->td_sigmask = ucp->uc_sigmask;
838 SIG_CANTMASK(td->td_sigmask);
839 signotify(td);
840 PROC_UNLOCK(p);
841 return (EJUSTRETURN);
842 }
844 /*
845 * Machine dependent boot() routine
846 *
847 * I haven't seen anything to put here yet
848 * Possibly some stuff might be grafted back here from boot()
849 */
850 void
851 cpu_boot(int howto)
852 {
853 }
855 /*
856 * Shutdown the CPU as much as possible
857 */
858 void
859 cpu_halt(void)
860 {
861 HYPERVISOR_shutdown();
862 }
864 /*
865 * Hook to idle the CPU when possible. In the SMP case we default to
866 * off because a halted cpu will not currently pick up a new thread in the
867 * run queue until the next timer tick. If turned on this will result in
868 * approximately a 4.2% loss in real time performance in buildworld tests
869 * (but improves user and sys times oddly enough), and saves approximately
870 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
871 *
872 * XXX we need to have a cpu mask of idle cpus and generate an IPI or
873 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
874 * Then we can have our cake and eat it too.
875 *
876 * XXX I'm turning it on for SMP as well by default for now. It seems to
877 * help lock contention somewhat, and this is critical for HTT. -Peter
878 */
879 static int cpu_idle_hlt = 1;
880 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
881 &cpu_idle_hlt, 0, "Idle loop HLT enable");
883 static void
884 cpu_idle_default(void)
885 {
886 #if 0
887 /*
888 * we must absolutely guarentee that hlt is the
889 * absolute next instruction after sti or we
890 * introduce a timing window.
891 */
892 __asm __volatile("sti; hlt");
893 #endif
894 idle_block();
895 enable_intr();
896 }
898 /*
899 * Note that we have to be careful here to avoid a race between checking
900 * sched_runnable() and actually halting. If we don't do this, we may waste
901 * the time between calling hlt and the next interrupt even though there
902 * is a runnable process.
903 */
904 void
905 cpu_idle(void)
906 {
908 #ifdef SMP
909 if (mp_grab_cpu_hlt())
910 return;
911 #endif
913 if (cpu_idle_hlt) {
914 disable_intr();
915 if (sched_runnable())
916 enable_intr();
917 else
918 (*cpu_idle_hook)();
919 }
920 }
922 /* Other subsystems (e.g., ACPI) can hook this later. */
923 void (*cpu_idle_hook)(void) = cpu_idle_default;
925 /*
926 * Clear registers on exec
927 */
928 void
929 exec_setregs(td, entry, stack, ps_strings)
930 struct thread *td;
931 u_long entry;
932 u_long stack;
933 u_long ps_strings;
934 {
935 struct trapframe *regs = td->td_frame;
936 struct pcb *pcb = td->td_pcb;
938 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
939 pcb->pcb_gs = _udatasel;
940 load_gs(_udatasel);
942 if (td->td_proc->p_md.md_ldt)
943 user_ldt_free(td);
945 bzero((char *)regs, sizeof(struct trapframe));
946 regs->tf_eip = entry;
947 regs->tf_esp = stack;
948 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
949 regs->tf_ss = _udatasel;
950 regs->tf_ds = _udatasel;
951 regs->tf_es = _udatasel;
952 regs->tf_fs = _udatasel;
953 regs->tf_cs = _ucodesel;
955 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
956 regs->tf_ebx = ps_strings;
958 /*
959 * Reset the hardware debug registers if they were in use.
960 * They won't have any meaning for the newly exec'd process.
961 */
962 if (pcb->pcb_flags & PCB_DBREGS) {
963 pcb->pcb_dr0 = 0;
964 pcb->pcb_dr1 = 0;
965 pcb->pcb_dr2 = 0;
966 pcb->pcb_dr3 = 0;
967 pcb->pcb_dr6 = 0;
968 pcb->pcb_dr7 = 0;
969 if (pcb == PCPU_GET(curpcb)) {
970 /*
971 * Clear the debug registers on the running
972 * CPU, otherwise they will end up affecting
973 * the next process we switch to.
974 */
975 reset_dbregs();
976 }
977 pcb->pcb_flags &= ~PCB_DBREGS;
978 }
980 /*
981 * Initialize the math emulator (if any) for the current process.
982 * Actually, just clear the bit that says that the emulator has
983 * been initialized. Initialization is delayed until the process
984 * traps to the emulator (if it is done at all) mainly because
985 * emulators don't provide an entry point for initialization.
986 */
987 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
989 /* Initialize the npx (if any) for the current process. */
990 /*
991 * XXX the above load_cr0() also initializes it and is a layering
992 * violation if NPX is configured. It drops the npx partially
993 * and this would be fatal if we were interrupted now, and decided
994 * to force the state to the pcb, and checked the invariant
995 * (CR0_TS clear) if and only if PCPU_GET(fpcurthread) != NULL).
996 * ALL of this can happen except the check. The check used to
997 * happen and be fatal later when we didn't complete the drop
998 * before returning to user mode. This should be fixed properly
999 * soon.
1000 */
1001 fpstate_drop(td);
1003 /*
1004 * XXX - Linux emulator
1005 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1006 * on it.
1007 */
1008 td->td_retval[1] = 0;
1011 void
1012 cpu_setregs(void)
1014 /* nothing for Xen to do */
1017 static int
1018 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
1020 int error;
1021 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1022 req);
1023 if (!error && req->newptr)
1024 resettodr();
1025 return (error);
1028 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1029 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1031 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1032 CTLFLAG_RW, &disable_rtc_set, 0, "");
1034 SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
1035 CTLFLAG_RD, &bootinfo, bootinfo, "");
1037 u_long bootdev; /* not a dev_t - encoding is different */
1038 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1039 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1041 /*
1042 * Initialize 386 and configure to run kernel
1043 */
1045 /*
1046 * Initialize segments & interrupt table
1047 */
1049 int _default_ldt;
1050 union descriptor *gdt; /* global descriptor table */
1051 static struct gate_descriptor idt0[NIDT];
1052 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1053 union descriptor *ldt; /* local descriptor table */
1054 struct region_descriptor r_idt; /* table descriptors */
1056 int private_tss; /* flag indicating private tss */
1058 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1059 extern int has_f00f_bug;
1060 #endif
1062 static struct i386tss dblfault_tss;
1063 static char dblfault_stack[PAGE_SIZE];
1065 extern struct user *proc0uarea;
1066 extern vm_offset_t proc0kstack;
1069 /* software prototypes -- in more palatable form */
1070 struct soft_segment_descriptor gdt_segs[] = {
1071 /* GNULL_SEL 0 Null Descriptor */
1072 { 0x0, /* segment base address */
1073 0x0, /* length */
1074 0, /* segment type */
1075 SEL_KPL, /* segment descriptor priority level */
1076 0, /* segment descriptor present */
1077 0, 0,
1078 0, /* default 32 vs 16 bit size */
1079 0 /* limit granularity (byte/page units)*/ },
1080 /* GCODE_SEL 1 Code Descriptor for kernel */
1081 { 0x0, /* segment base address */
1082 0x0, /* length - all address space */
1083 0, /* segment type */
1084 0, /* segment descriptor priority level */
1085 0, /* segment descriptor present */
1086 0, 0,
1087 0, /* default 32 vs 16 bit size */
1088 0 /* limit granularity (byte/page units)*/ },
1090 /* GDATA_SEL 2 Data Descriptor for kernel */
1091 { 0x0, /* segment base address */
1092 0x0, /* length - all address space */
1093 0, /* segment type */
1094 0, /* segment descriptor priority level */
1095 0, /* segment descriptor present */
1096 0, 0,
1097 0, /* default 32 vs 16 bit size */
1098 0 /* limit granularity (byte/page units)*/ },
1100 /* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
1101 { 0x0, /* segment base address */
1102 0xfffff, /* length - all address space */
1103 SDT_MEMRWA, /* segment type */
1104 SEL_KPL, /* segment descriptor priority level */
1105 1, /* segment descriptor present */
1106 0, 0,
1107 1, /* default 32 vs 16 bit size */
1108 1 /* limit granularity (byte/page units)*/ },
1109 #if 0
1110 /* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1112 0x0, /* segment base address */
1113 sizeof(struct i386tss)-1,/* length */
1114 SDT_SYS386TSS, /* segment type */
1115 0, /* segment descriptor priority level */
1116 1, /* segment descriptor present */
1117 0, 0,
1118 0, /* unused - default 32 vs 16 bit size */
1119 0 /* limit granularity (byte/page units)*/ },
1120 /* GLDT_SEL 5 LDT Descriptor */
1121 { (int) ldt, /* segment base address */
1122 sizeof(ldt)-1, /* length - all address space */
1123 SDT_SYSLDT, /* segment type */
1124 SEL_UPL, /* segment descriptor priority level */
1125 1, /* segment descriptor present */
1126 0, 0,
1127 0, /* unused - default 32 vs 16 bit size */
1128 0 /* limit granularity (byte/page units)*/ },
1129 /* GUSERLDT_SEL 6 User LDT Descriptor per process */
1130 { (int) ldt, /* segment base address */
1131 (512 * sizeof(union descriptor)-1), /* length */
1132 SDT_SYSLDT, /* segment type */
1133 0, /* segment descriptor priority level */
1134 1, /* segment descriptor present */
1135 0, 0,
1136 0, /* unused - default 32 vs 16 bit size */
1137 0 /* limit granularity (byte/page units)*/ },
1138 /* GTGATE_SEL 7 Null Descriptor - Placeholder */
1139 { 0x0, /* segment base address */
1140 0x0, /* length - all address space */
1141 0, /* segment type */
1142 0, /* segment descriptor priority level */
1143 0, /* segment descriptor present */
1144 0, 0,
1145 0, /* default 32 vs 16 bit size */
1146 0 /* limit granularity (byte/page units)*/ },
1147 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1148 { 0x400, /* segment base address */
1149 0xfffff, /* length */
1150 SDT_MEMRWA, /* segment type */
1151 0, /* segment descriptor priority level */
1152 1, /* segment descriptor present */
1153 0, 0,
1154 1, /* default 32 vs 16 bit size */
1155 1 /* limit granularity (byte/page units)*/ },
1156 /* GPANIC_SEL 9 Panic Tss Descriptor */
1157 { (int) &dblfault_tss, /* segment base address */
1158 sizeof(struct i386tss)-1,/* length - all address space */
1159 SDT_SYS386TSS, /* segment type */
1160 0, /* segment descriptor priority level */
1161 1, /* segment descriptor present */
1162 0, 0,
1163 0, /* unused - default 32 vs 16 bit size */
1164 0 /* limit granularity (byte/page units)*/ },
1165 /* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1166 { 0, /* segment base address (overwritten) */
1167 0xfffff, /* length */
1168 SDT_MEMERA, /* segment type */
1169 0, /* segment descriptor priority level */
1170 1, /* segment descriptor present */
1171 0, 0,
1172 0, /* default 32 vs 16 bit size */
1173 1 /* limit granularity (byte/page units)*/ },
1174 /* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1175 { 0, /* segment base address (overwritten) */
1176 0xfffff, /* length */
1177 SDT_MEMERA, /* segment type */
1178 0, /* segment descriptor priority level */
1179 1, /* segment descriptor present */
1180 0, 0,
1181 0, /* default 32 vs 16 bit size */
1182 1 /* limit granularity (byte/page units)*/ },
1183 /* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1184 { 0, /* segment base address (overwritten) */
1185 0xfffff, /* length */
1186 SDT_MEMRWA, /* segment type */
1187 0, /* segment descriptor priority level */
1188 1, /* segment descriptor present */
1189 0, 0,
1190 1, /* default 32 vs 16 bit size */
1191 1 /* limit granularity (byte/page units)*/ },
1192 /* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1193 { 0, /* segment base address (overwritten) */
1194 0xfffff, /* length */
1195 SDT_MEMRWA, /* segment type */
1196 0, /* segment descriptor priority level */
1197 1, /* segment descriptor present */
1198 0, 0,
1199 0, /* default 32 vs 16 bit size */
1200 1 /* limit granularity (byte/page units)*/ },
1201 /* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1202 { 0, /* segment base address (overwritten) */
1203 0xfffff, /* length */
1204 SDT_MEMRWA, /* segment type */
1205 0, /* segment descriptor priority level */
1206 1, /* segment descriptor present */
1207 0, 0,
1208 0, /* default 32 vs 16 bit size */
1209 1 /* limit granularity (byte/page units)*/ },
1210 #endif
1211 };
1213 static struct soft_segment_descriptor ldt_segs[] = {
1214 /* Null Descriptor - overwritten by call gate */
1215 { 0x0, /* segment base address */
1216 0x0, /* length - all address space */
1217 0, /* segment type */
1218 0, /* segment descriptor priority level */
1219 0, /* segment descriptor present */
1220 0, 0,
1221 0, /* default 32 vs 16 bit size */
1222 0 /* limit granularity (byte/page units)*/ },
1223 /* Null Descriptor - overwritten by call gate */
1224 { 0x0, /* segment base address */
1225 0x0, /* length - all address space */
1226 0, /* segment type */
1227 0, /* segment descriptor priority level */
1228 0, /* segment descriptor present */
1229 0, 0,
1230 0, /* default 32 vs 16 bit size */
1231 0 /* limit granularity (byte/page units)*/ },
1232 /* Null Descriptor - overwritten by call gate */
1233 { 0x0, /* segment base address */
1234 0x0, /* length - all address space */
1235 0, /* segment type */
1236 0, /* segment descriptor priority level */
1237 0, /* segment descriptor present */
1238 0, 0,
1239 0, /* default 32 vs 16 bit size */
1240 0 /* limit granularity (byte/page units)*/ },
1241 /* Code Descriptor for user */
1242 { 0x0, /* segment base address */
1243 0xfffff, /* length - all address space */
1244 SDT_MEMERA, /* segment type */
1245 SEL_UPL, /* segment descriptor priority level */
1246 1, /* segment descriptor present */
1247 0, 0,
1248 1, /* default 32 vs 16 bit size */
1249 1 /* limit granularity (byte/page units)*/ },
1250 /* Null Descriptor - overwritten by call gate */
1251 { 0x0, /* segment base address */
1252 0x0, /* length - all address space */
1253 0, /* segment type */
1254 0, /* segment descriptor priority level */
1255 0, /* segment descriptor present */
1256 0, 0,
1257 0, /* default 32 vs 16 bit size */
1258 0 /* limit granularity (byte/page units)*/ },
1259 /* Data Descriptor for user */
1260 { 0x0, /* segment base address */
1261 0xfffff, /* length - all address space */
1262 SDT_MEMRWA, /* segment type */
1263 SEL_UPL, /* segment descriptor priority level */
1264 1, /* segment descriptor present */
1265 0, 0,
1266 1, /* default 32 vs 16 bit size */
1267 1 /* limit granularity (byte/page units)*/ },
1268 };
1270 struct proc_ldt default_proc_ldt;
1272 void
1273 setidt(idx, func, typ, dpl, selec)
1274 int idx;
1275 inthand_t *func;
1276 int typ;
1277 int dpl;
1278 int selec;
1280 struct gate_descriptor *ip;
1282 ip = idt + idx;
1283 ip->gd_looffset = (int)func;
1284 ip->gd_selector = selec;
1285 ip->gd_stkcpy = 0;
1286 ip->gd_xx = 0;
1287 ip->gd_type = typ;
1288 ip->gd_dpl = dpl;
1289 ip->gd_p = 1;
1290 ip->gd_hioffset = ((int)func)>>16 ;
1293 #define IDTVEC(name) __CONCAT(X,name)
1295 extern inthand_t
1296 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1297 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1298 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1299 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1300 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1302 #ifdef DDB
1303 /*
1304 * Display the index and function name of any IDT entries that don't use
1305 * the default 'rsvd' entry point.
1306 */
1307 DB_SHOW_COMMAND(idt, db_show_idt)
1309 struct gate_descriptor *ip;
1310 int idx, quit;
1311 uintptr_t func;
1313 ip = idt;
1314 db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE);
1315 for (idx = 0, quit = 0; idx < NIDT; idx++) {
1316 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1317 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1318 db_printf("%3d\t", idx);
1319 db_printsym(func, DB_STGY_PROC);
1320 db_printf("\n");
1322 ip++;
1325 #endif
1327 void
1328 sdtossd(sd, ssd)
1329 struct segment_descriptor *sd;
1330 struct soft_segment_descriptor *ssd;
1332 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1333 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1334 ssd->ssd_type = sd->sd_type;
1335 ssd->ssd_dpl = sd->sd_dpl;
1336 ssd->ssd_p = sd->sd_p;
1337 ssd->ssd_def32 = sd->sd_def32;
1338 ssd->ssd_gran = sd->sd_gran;
1341 #define PHYSMAP_SIZE (2 * 8)
1343 /*
1344 * Populate the (physmap) array with base/bound pairs describing the
1345 * available physical memory in the system, then test this memory and
1346 * build the phys_avail array describing the actually-available memory.
1348 * If we cannot accurately determine the physical memory map, then use
1349 * value from the 0xE801 call, and failing that, the RTC.
1351 * Total memory size may be set by the kernel environment variable
1352 * hw.physmem or the compile-time define MAXMEM.
1354 * XXX first should be vm_paddr_t.
1355 */
1356 static void
1357 getmemsize(void)
1359 int i;
1360 printf("start_info %p\n", xen_start_info);
1361 printf("start_info->nr_pages %ld\n", xen_start_info->nr_pages);
1362 Maxmem = xen_start_info->nr_pages - init_first;
1363 /* call pmap initialization to make new kernel address space */
1364 pmap_bootstrap((init_first)<< PAGE_SHIFT, 0);
1365 for (i = 0; i < 10; i++)
1366 phys_avail[i] = 0;
1367 physmem = Maxmem;
1368 avail_end = ptoa(Maxmem) - round_page(MSGBUF_SIZE);
1369 phys_avail[0] = init_first << PAGE_SHIFT;
1370 phys_avail[1] = avail_end;
1373 extern unsigned long cpu0prvpage;
1374 extern unsigned long *SMPpt;
1375 pteinfo_t *pteinfo_list;
1376 unsigned long *xen_machine_phys = ((unsigned long *)VADDR(1008, 0));
1377 pt_entry_t *KPTphysv;
1378 int preemptable;
1379 int gdt_set;
1381 /* Linux infection */
1382 #define PAGE_OFFSET KERNBASE
1383 #define __pa(x) ((unsigned long)(x)-PAGE_OFFSET)
1384 #define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
1385 void
1386 initvalues(start_info_t *startinfo)
1388 int i;
1389 #ifdef WRITABLE_PAGETABLES
1390 XENPRINTF("using writable pagetables\n");
1391 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
1392 #endif
1394 xen_start_info = startinfo;
1395 xen_phys_machine = (unsigned long *)startinfo->mfn_list;
1396 unsigned long tmpindex = ((__pa(xen_start_info->pt_base) >> PAGE_SHIFT) + xen_start_info->nr_pt_frames) + 3 /* number of pages allocated after the pts + 1*/;
1397 xendebug_flags = 0xffffffff;
1398 /* pre-zero unused mapped pages */
1399 bzero((char *)(KERNBASE + (tmpindex << PAGE_SHIFT)), (1024 - tmpindex)*PAGE_SIZE);
1400 IdlePTD = (pd_entry_t *)xpmap_ptom(__pa(startinfo->pt_base));
1401 KPTphysv = (pt_entry_t *)(startinfo->pt_base + PAGE_SIZE);
1402 XENPRINTF("IdlePTD %p\n", IdlePTD);
1403 XENPRINTF("nr_pages: %ld shared_info: 0x%lx flags: 0x%lx pt_base: 0x%lx "
1404 "mod_start: 0x%lx mod_len: 0x%lx\n",
1405 xen_start_info->nr_pages, xen_start_info->shared_info,
1406 xen_start_info->flags, xen_start_info->pt_base,
1407 xen_start_info->mod_start, xen_start_info->mod_len);
1409 /* setup self-referential mapping first so vtomach will work */
1410 xen_queue_pt_update(IdlePTD + PTDPTDI , (unsigned long)IdlePTD |
1411 PG_V | PG_A);
1412 xen_flush_queue();
1413 /* Map proc0's UPAGES */
1414 proc0uarea = (struct user *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1415 tmpindex += UAREA_PAGES;
1417 /* Map proc0's KSTACK */
1418 proc0kstack = KERNBASE + (tmpindex << PAGE_SHIFT);
1419 tmpindex += KSTACK_PAGES;
1421 /* allocate page for gdt */
1422 gdt = (union descriptor *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1423 tmpindex++;
1425 /* allocate page for ldt */
1426 ldt = (union descriptor *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1427 tmpindex++;
1428 #ifdef SMP
1429 /* allocate cpu0 private page */
1430 cpu0prvpage = (KERNBASE + (tmpindex << PAGE_SHIFT));
1431 tmpindex++;
1433 /* allocate SMP page table */
1434 SMPpt = (unsigned long *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1436 /* Map the private page into the SMP page table */
1437 SMPpt[0] = vtomach(cpu0prvpage) | PG_RW | PG_M | PG_V | PG_A;
1439 /* map SMP page table RO */
1440 PT_SET_MA(SMPpt, vtomach(SMPpt) & ~PG_RW);
1442 /* put the page table into the pde */
1443 xen_queue_pt_update(IdlePTD + MPPTDI, xpmap_ptom((tmpindex << PAGE_SHIFT))| PG_M | PG_RW | PG_V | PG_A);
1445 tmpindex++;
1446 #endif
1448 #ifdef PMAP_DEBUG
1449 pteinfo_list = (pteinfo_t *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1450 tmpindex += ((xen_start_info->nr_pages >> 10) + 1)*(1 + XPQ_CALL_DEPTH*XPQ_CALL_COUNT);
1452 if (tmpindex > 980)
1453 __asm__("int3");
1454 #endif
1455 /* unmap remaining pages from initial 4MB chunk */
1456 for (i = tmpindex; i%1024 != 0; i++)
1457 PT_CLEAR_VA(KPTphysv + i, TRUE);
1459 /* allocate remainder of NKPT pages */
1460 for (i = 0; i < NKPT-1; i++, tmpindex++)
1461 PD_SET_VA(((unsigned long *)startinfo->pt_base) + KPTDI + i + 1, (tmpindex << PAGE_SHIFT)| PG_M | PG_RW | PG_V | PG_A, TRUE);
1462 tmpindex += NKPT-1;
1463 PT_UPDATES_FLUSH();
1465 HYPERVISOR_shared_info = (shared_info_t *)(KERNBASE + (tmpindex << PAGE_SHIFT));
1466 PT_SET_MA(HYPERVISOR_shared_info, xen_start_info->shared_info | PG_A | PG_V | PG_RW | PG_M);
1467 tmpindex++;
1469 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list = (unsigned long)xen_phys_machine;
1471 init_first = tmpindex;
1475 void
1476 init386(void)
1478 int gsel_tss, metadata_missing, off, x, error;
1479 struct pcpu *pc;
1480 unsigned long gdtmachpfn;
1481 trap_info_t trap_table[] = {
1482 { 0, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(div)},
1483 { 1, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dbg)},
1484 { 3, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bpt)},
1485 { 4, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ofl)},
1486 /* This is UPL on Linux and KPL on BSD */
1487 { 5, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bnd)},
1488 { 6, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ill)},
1489 { 7, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dna)},
1490 /*
1491 * { 8, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(XXX)},
1492 * no handler for double fault
1493 */
1494 { 9, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpusegm)},
1495 {10, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(tss)},
1496 {11, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(missing)},
1497 {12, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(stk)},
1498 {13, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(prot)},
1499 {14, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(page)},
1500 {15, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(rsvd)},
1501 {16, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpu)},
1502 {17, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(align)},
1503 {18, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(mchk)},
1504 {19, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(xmm)},
1505 {0x80, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(int0x80_syscall)},
1506 { 0, 0, 0, 0 }
1507 };
1508 proc0.p_uarea = proc0uarea;
1509 thread0.td_kstack = proc0kstack;
1510 thread0.td_pcb = (struct pcb *)
1511 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1513 /*
1514 * This may be done better later if it gets more high level
1515 * components in it. If so just link td->td_proc here.
1516 */
1517 proc_linkup(&proc0, &ksegrp0, &thread0);
1519 metadata_missing = 0;
1520 if (xen_start_info->mod_start)
1521 preload_metadata = (caddr_t)xen_start_info->mod_start;
1522 else
1523 metadata_missing = 1;
1525 /* XXX - temporary hack */
1526 preload_metadata = (caddr_t)0;
1527 /* XXX */
1529 if (envmode == 1)
1530 kern_envp = static_env;
1531 else if ((caddr_t)xen_start_info->cmd_line)
1532 kern_envp = xen_setbootenv((caddr_t)xen_start_info->cmd_line);
1534 boothowto |= xen_boothowto(kern_envp);
1536 if (boothowto & RB_GDB_PAUSE)
1537 __asm__("int $0x3;");
1539 /* Init basic tunables, hz etc */
1540 init_param1();
1541 /*
1542 * make gdt memory segments, the code segment goes up to end of the
1543 * page with etext in it, the data segment goes to the end of
1544 * the address space
1545 */
1546 #if 0
1547 /*
1548 * XEN occupies the upper 64MB of virtual address space
1549 * At its base it manages an array mapping machine page frames
1550 * to physical page frames - hence we need to be able to
1551 * access 4GB - (64MB - 4MB + 64k)
1552 */
1553 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - ((1 << 26) - (1 << 22) + (1 << 16)));
1554 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - ((1 << 26) - (1 << 22) + (1 << 16)));
1555 #endif
1556 #ifdef SMP
1557 /* this correspond to the cpu private page as mapped into the SMP page
1558 * table in initvalues
1559 */
1560 pc = &SMP_prvspace[0].pcpu;
1561 gdt_segs[GPRIV_SEL].ssd_limit =
1562 atop(sizeof(struct privatespace) - 1);
1563 #else
1564 pc = &__pcpu;
1565 gdt_segs[GPRIV_SEL].ssd_limit =
1566 atop(sizeof(struct pcpu) - 1);
1567 #endif
1568 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
1569 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
1570 for (x = 0; x < NGDT; x++)
1571 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1573 PT_SET_MA(gdt, *vtopte((unsigned long)gdt) & ~PG_RW);
1574 gdtmachpfn = vtomach(gdt) >> PAGE_SHIFT;
1575 if (HYPERVISOR_set_gdt(&gdtmachpfn, LAST_RESERVED_GDT_ENTRY + 1)) {
1576 XENPRINTF("set_gdt failed\n");
1579 lgdt_finish();
1580 gdt_set = 1;
1582 if ((error = HYPERVISOR_set_trap_table(trap_table)) != 0) {
1583 panic("set_trap_table failed - error %d\n", error);
1585 if ((error = HYPERVISOR_set_fast_trap(0x80)) != 0) {
1586 panic("set_fast_trap failed - error %d\n", error);
1588 HYPERVISOR_set_callbacks(GSEL(GCODE_SEL, SEL_KPL), (unsigned long)Xhypervisor_callback,
1589 GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback);
1593 pcpu_init(pc, 0, sizeof(struct pcpu));
1594 PCPU_SET(prvspace, pc);
1595 PCPU_SET(curthread, &thread0);
1596 PCPU_SET(curpcb, thread0.td_pcb);
1597 PCPU_SET(pdir, (unsigned long)IdlePTD);
1598 /*
1599 * Initialize mutexes.
1601 */
1602 mutex_init();
1604 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN);
1605 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS);
1609 /* make ldt memory segments */
1610 /*
1611 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it
1612 * should be spelled ...MAX_USER...
1613 */
1614 ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
1615 ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
1616 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1617 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1618 default_proc_ldt.ldt_base = (caddr_t)ldt;
1619 default_proc_ldt.ldt_len = 6;
1620 _default_ldt = (int)&default_proc_ldt;
1621 PCPU_SET(currentldt, _default_ldt)
1622 PT_SET_MA(ldt, *vtopte((unsigned long)ldt) & ~PG_RW);
1623 xen_set_ldt((unsigned long) ldt, (sizeof ldt_segs / sizeof ldt_segs[0]));
1626 /*
1627 * Initialize the console before we print anything out.
1628 */
1629 cninit();
1630 if (metadata_missing)
1631 printf("WARNING: loader(8) metadata is missing!\n");
1633 #ifdef DDB
1634 ksym_start = bootinfo.bi_symtab;
1635 ksym_end = bootinfo.bi_esymtab;
1636 #endif
1637 kdb_init();
1638 #ifdef KDB
1639 if (boothowto & RB_KDB)
1640 kdb_enter("Boot flags requested debugger");
1641 #endif
1643 finishidentcpu(); /* Final stage of CPU initialization */
1644 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
1645 GSEL(GCODE_SEL, SEL_KPL));
1646 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
1647 GSEL(GCODE_SEL, SEL_KPL));
1648 initializecpu(); /* Initialize CPU registers */
1650 /* make an initial tss so cpu can get interrupt stack on syscall! */
1651 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
1652 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
1653 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
1654 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
1655 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1656 #if 0
1657 private_tss = 0;
1658 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
1659 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
1660 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
1661 #endif
1662 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL), PCPU_GET(common_tss.tss_esp0));
1665 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1666 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
1667 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1668 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1670 dblfault_tss.tss_cr3 = (int)IdlePTD;
1671 dblfault_tss.tss_eip = (int)dblfault_handler;
1672 dblfault_tss.tss_eflags = PSL_KERNEL;
1673 dblfault_tss.tss_ds = dblfault_tss.tss_es =
1674 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1675 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1676 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1677 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1679 getmemsize();
1680 init_param2(physmem);
1681 /* now running on new page tables, configured,and u/iom is accessible */
1682 /* Map the message buffer. */
1683 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1684 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1685 PT_UPDATES_FLUSH();
1687 /* safe to enable xen page queue locking */
1689 msgbufinit(msgbufp, MSGBUF_SIZE);
1690 /* XXX KMM I don't think we need call gates */
1691 #if 0
1692 printf("modify ldt\n");
1693 /* make a call gate to reenter kernel with */
1694 gdp = &ldt[LSYS5CALLS_SEL].gd;
1696 x = (int) &IDTVEC(lcall_syscall);
1697 gdp->gd_looffset = x;
1698 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1699 gdp->gd_stkcpy = 1;
1700 gdp->gd_type = SDT_SYS386CGT;
1701 gdp->gd_dpl = SEL_UPL;
1702 gdp->gd_p = 1;
1703 gdp->gd_hioffset = x >> 16;
1705 /* XXX does this work? */
1706 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1707 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
1708 #endif
1709 /* transfer to user mode */
1711 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1712 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1714 /* setup proc 0's pcb */
1715 thread0.td_pcb->pcb_flags = 0; /* XXXKSE */
1716 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
1717 thread0.td_pcb->pcb_ext = 0;
1718 thread0.td_frame = &proc0_tf;
1721 void
1722 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
1725 pcpu->pc_acpi_id = 0xffffffff;
1728 /*
1729 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1730 * we want to start a backtrace from the function that caused us to enter
1731 * the debugger. We have the context in the trapframe, but base the trace
1732 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1733 * enough for a backtrace.
1734 */
1735 void
1736 makectx(struct trapframe *tf, struct pcb *pcb)
1739 pcb->pcb_edi = tf->tf_edi;
1740 pcb->pcb_esi = tf->tf_esi;
1741 pcb->pcb_ebp = tf->tf_ebp;
1742 pcb->pcb_ebx = tf->tf_ebx;
1743 pcb->pcb_eip = tf->tf_eip;
1744 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
1747 int
1748 ptrace_set_pc(struct thread *td, u_long addr)
1751 td->td_frame->tf_eip = addr;
1752 return (0);
1755 int
1756 ptrace_single_step(struct thread *td)
1758 td->td_frame->tf_eflags |= PSL_T;
1759 return (0);
1762 int
1763 ptrace_clear_single_step(struct thread *td)
1765 td->td_frame->tf_eflags &= ~PSL_T;
1766 return (0);
1769 int
1770 fill_regs(struct thread *td, struct reg *regs)
1772 struct pcb *pcb;
1773 struct trapframe *tp;
1775 tp = td->td_frame;
1776 regs->r_fs = tp->tf_fs;
1777 regs->r_es = tp->tf_es;
1778 regs->r_ds = tp->tf_ds;
1779 regs->r_edi = tp->tf_edi;
1780 regs->r_esi = tp->tf_esi;
1781 regs->r_ebp = tp->tf_ebp;
1782 regs->r_ebx = tp->tf_ebx;
1783 regs->r_edx = tp->tf_edx;
1784 regs->r_ecx = tp->tf_ecx;
1785 regs->r_eax = tp->tf_eax;
1786 regs->r_eip = tp->tf_eip;
1787 regs->r_cs = tp->tf_cs;
1788 regs->r_eflags = tp->tf_eflags;
1789 regs->r_esp = tp->tf_esp;
1790 regs->r_ss = tp->tf_ss;
1791 pcb = td->td_pcb;
1792 regs->r_gs = pcb->pcb_gs;
1793 return (0);
1796 int
1797 set_regs(struct thread *td, struct reg *regs)
1799 struct pcb *pcb;
1800 struct trapframe *tp;
1802 tp = td->td_frame;
1803 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
1804 !CS_SECURE(regs->r_cs))
1805 return (EINVAL);
1806 tp->tf_fs = regs->r_fs;
1807 tp->tf_es = regs->r_es;
1808 tp->tf_ds = regs->r_ds;
1809 tp->tf_edi = regs->r_edi;
1810 tp->tf_esi = regs->r_esi;
1811 tp->tf_ebp = regs->r_ebp;
1812 tp->tf_ebx = regs->r_ebx;
1813 tp->tf_edx = regs->r_edx;
1814 tp->tf_ecx = regs->r_ecx;
1815 tp->tf_eax = regs->r_eax;
1816 tp->tf_eip = regs->r_eip;
1817 tp->tf_cs = regs->r_cs;
1818 tp->tf_eflags = regs->r_eflags;
1819 tp->tf_esp = regs->r_esp;
1820 tp->tf_ss = regs->r_ss;
1821 pcb = td->td_pcb;
1822 pcb->pcb_gs = regs->r_gs;
1823 return (0);
1826 #ifdef CPU_ENABLE_SSE
1827 static void
1828 fill_fpregs_xmm(sv_xmm, sv_87)
1829 struct savexmm *sv_xmm;
1830 struct save87 *sv_87;
1832 register struct env87 *penv_87 = &sv_87->sv_env;
1833 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
1834 int i;
1836 bzero(sv_87, sizeof(*sv_87));
1838 /* FPU control/status */
1839 penv_87->en_cw = penv_xmm->en_cw;
1840 penv_87->en_sw = penv_xmm->en_sw;
1841 penv_87->en_tw = penv_xmm->en_tw;
1842 penv_87->en_fip = penv_xmm->en_fip;
1843 penv_87->en_fcs = penv_xmm->en_fcs;
1844 penv_87->en_opcode = penv_xmm->en_opcode;
1845 penv_87->en_foo = penv_xmm->en_foo;
1846 penv_87->en_fos = penv_xmm->en_fos;
1848 /* FPU registers */
1849 for (i = 0; i < 8; ++i)
1850 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
1853 static void
1854 set_fpregs_xmm(sv_87, sv_xmm)
1855 struct save87 *sv_87;
1856 struct savexmm *sv_xmm;
1858 register struct env87 *penv_87 = &sv_87->sv_env;
1859 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
1860 int i;
1862 /* FPU control/status */
1863 penv_xmm->en_cw = penv_87->en_cw;
1864 penv_xmm->en_sw = penv_87->en_sw;
1865 penv_xmm->en_tw = penv_87->en_tw;
1866 penv_xmm->en_fip = penv_87->en_fip;
1867 penv_xmm->en_fcs = penv_87->en_fcs;
1868 penv_xmm->en_opcode = penv_87->en_opcode;
1869 penv_xmm->en_foo = penv_87->en_foo;
1870 penv_xmm->en_fos = penv_87->en_fos;
1872 /* FPU registers */
1873 for (i = 0; i < 8; ++i)
1874 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
1876 #endif /* CPU_ENABLE_SSE */
1878 int
1879 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1881 #ifdef CPU_ENABLE_SSE
1882 if (cpu_fxsr) {
1883 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
1884 (struct save87 *)fpregs);
1885 return (0);
1887 #endif /* CPU_ENABLE_SSE */
1888 bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
1889 return (0);
1892 int
1893 set_fpregs(struct thread *td, struct fpreg *fpregs)
1895 #ifdef CPU_ENABLE_SSE
1896 if (cpu_fxsr) {
1897 set_fpregs_xmm((struct save87 *)fpregs,
1898 &td->td_pcb->pcb_save.sv_xmm);
1899 return (0);
1901 #endif /* CPU_ENABLE_SSE */
1902 bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
1903 return (0);
1906 /*
1907 * Get machine context.
1908 */
1909 int
1910 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
1912 struct trapframe *tp;
1914 tp = td->td_frame;
1916 PROC_LOCK(curthread->td_proc);
1917 mcp->mc_onstack = sigonstack(tp->tf_esp);
1918 PROC_UNLOCK(curthread->td_proc);
1919 mcp->mc_gs = td->td_pcb->pcb_gs;
1920 mcp->mc_fs = tp->tf_fs;
1921 mcp->mc_es = tp->tf_es;
1922 mcp->mc_ds = tp->tf_ds;
1923 mcp->mc_edi = tp->tf_edi;
1924 mcp->mc_esi = tp->tf_esi;
1925 mcp->mc_ebp = tp->tf_ebp;
1926 mcp->mc_isp = tp->tf_isp;
1927 if (flags & GET_MC_CLEAR_RET) {
1928 mcp->mc_eax = 0;
1929 mcp->mc_edx = 0;
1930 } else {
1931 mcp->mc_eax = tp->tf_eax;
1932 mcp->mc_edx = tp->tf_edx;
1934 mcp->mc_ebx = tp->tf_ebx;
1935 mcp->mc_ecx = tp->tf_ecx;
1936 mcp->mc_eip = tp->tf_eip;
1937 mcp->mc_cs = tp->tf_cs;
1938 mcp->mc_eflags = tp->tf_eflags;
1939 mcp->mc_esp = tp->tf_esp;
1940 mcp->mc_ss = tp->tf_ss;
1941 mcp->mc_len = sizeof(*mcp);
1942 get_fpcontext(td, mcp);
1943 return (0);
1946 /*
1947 * Set machine context.
1949 * However, we don't set any but the user modifiable flags, and we won't
1950 * touch the cs selector.
1951 */
1952 int
1953 set_mcontext(struct thread *td, const mcontext_t *mcp)
1955 struct trapframe *tp;
1956 int eflags, ret;
1958 tp = td->td_frame;
1959 if (mcp->mc_len != sizeof(*mcp))
1960 return (EINVAL);
1961 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
1962 (tp->tf_eflags & ~PSL_USERCHANGE);
1963 if ((ret = set_fpcontext(td, mcp)) == 0) {
1964 tp->tf_fs = mcp->mc_fs;
1965 tp->tf_es = mcp->mc_es;
1966 tp->tf_ds = mcp->mc_ds;
1967 tp->tf_edi = mcp->mc_edi;
1968 tp->tf_esi = mcp->mc_esi;
1969 tp->tf_ebp = mcp->mc_ebp;
1970 tp->tf_ebx = mcp->mc_ebx;
1971 tp->tf_edx = mcp->mc_edx;
1972 tp->tf_ecx = mcp->mc_ecx;
1973 tp->tf_eax = mcp->mc_eax;
1974 tp->tf_eip = mcp->mc_eip;
1975 tp->tf_eflags = eflags;
1976 tp->tf_esp = mcp->mc_esp;
1977 tp->tf_ss = mcp->mc_ss;
1978 td->td_pcb->pcb_gs = mcp->mc_gs;
1979 ret = 0;
1981 return (ret);
1984 static void
1985 get_fpcontext(struct thread *td, mcontext_t *mcp)
1987 #ifndef DEV_NPX
1988 mcp->mc_fpformat = _MC_FPFMT_NODEV;
1989 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
1990 #else
1991 union savefpu *addr;
1993 /*
1994 * XXX mc_fpstate might be misaligned, since its declaration is not
1995 * unportabilized using __attribute__((aligned(16))) like the
1996 * declaration of struct savemm, and anyway, alignment doesn't work
1997 * for auto variables since we don't use gcc's pessimal stack
1998 * alignment. Work around this by abusing the spare fields after
1999 * mcp->mc_fpstate.
2001 * XXX unpessimize most cases by only aligning when fxsave might be
2002 * called, although this requires knowing too much about
2003 * npxgetregs()'s internals.
2004 */
2005 addr = (union savefpu *)&mcp->mc_fpstate;
2006 if (td == PCPU_GET(fpcurthread) &&
2007 #ifdef CPU_ENABLE_SSE
2008 cpu_fxsr &&
2009 #endif
2010 ((uintptr_t)(void *)addr & 0xF)) {
2011 do
2012 addr = (void *)((char *)addr + 4);
2013 while ((uintptr_t)(void *)addr & 0xF);
2015 mcp->mc_ownedfp = npxgetregs(td, addr);
2016 if (addr != (union savefpu *)&mcp->mc_fpstate) {
2017 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2018 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
2020 mcp->mc_fpformat = npxformat();
2021 #endif
2024 static int
2025 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2027 union savefpu *addr;
2029 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2030 return (0);
2031 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2032 mcp->mc_fpformat != _MC_FPFMT_XMM)
2033 return (EINVAL);
2034 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2035 /* We don't care what state is left in the FPU or PCB. */
2036 fpstate_drop(td);
2037 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2038 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2039 /* XXX align as above. */
2040 addr = (union savefpu *)&mcp->mc_fpstate;
2041 if (td == PCPU_GET(fpcurthread) &&
2042 #ifdef CPU_ENABLE_SSE
2043 cpu_fxsr &&
2044 #endif
2045 ((uintptr_t)(void *)addr & 0xF)) {
2046 do
2047 addr = (void *)((char *)addr + 4);
2048 while ((uintptr_t)(void *)addr & 0xF);
2049 bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
2051 #ifdef DEV_NPX
2052 /*
2053 * XXX we violate the dubious requirement that npxsetregs()
2054 * be called with interrupts disabled.
2055 */
2056 npxsetregs(td, addr);
2057 #endif
2058 /*
2059 * Don't bother putting things back where they were in the
2060 * misaligned case, since we know that the caller won't use
2061 * them again.
2062 */
2063 } else
2064 return (EINVAL);
2065 return (0);
2068 static void
2069 fpstate_drop(struct thread *td)
2071 register_t s;
2073 s = intr_disable();
2074 #ifdef DEV_NPX
2075 if (PCPU_GET(fpcurthread) == td)
2076 npxdrop();
2077 #endif
2078 /*
2079 * XXX force a full drop of the npx. The above only drops it if we
2080 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2082 * XXX I don't much like npxgetregs()'s semantics of doing a full
2083 * drop. Dropping only to the pcb matches fnsave's behaviour.
2084 * We only need to drop to !PCB_INITDONE in sendsig(). But
2085 * sendsig() is the only caller of npxgetregs()... perhaps we just
2086 * have too many layers.
2087 */
2088 curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
2089 intr_restore(s);
2092 int
2093 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2095 struct pcb *pcb;
2097 if (td == NULL) {
2098 dbregs->dr[0] = rdr0();
2099 dbregs->dr[1] = rdr1();
2100 dbregs->dr[2] = rdr2();
2101 dbregs->dr[3] = rdr3();
2102 dbregs->dr[4] = rdr4();
2103 dbregs->dr[5] = rdr5();
2104 dbregs->dr[6] = rdr6();
2105 dbregs->dr[7] = rdr7();
2106 } else {
2107 pcb = td->td_pcb;
2108 dbregs->dr[0] = pcb->pcb_dr0;
2109 dbregs->dr[1] = pcb->pcb_dr1;
2110 dbregs->dr[2] = pcb->pcb_dr2;
2111 dbregs->dr[3] = pcb->pcb_dr3;
2112 dbregs->dr[4] = 0;
2113 dbregs->dr[5] = 0;
2114 dbregs->dr[6] = pcb->pcb_dr6;
2115 dbregs->dr[7] = pcb->pcb_dr7;
2117 return (0);
2120 int
2121 set_dbregs(struct thread *td, struct dbreg *dbregs)
2123 struct pcb *pcb;
2124 int i;
2125 u_int32_t mask1, mask2;
2127 if (td == NULL) {
2128 load_dr0(dbregs->dr[0]);
2129 load_dr1(dbregs->dr[1]);
2130 load_dr2(dbregs->dr[2]);
2131 load_dr3(dbregs->dr[3]);
2132 load_dr4(dbregs->dr[4]);
2133 load_dr5(dbregs->dr[5]);
2134 load_dr6(dbregs->dr[6]);
2135 load_dr7(dbregs->dr[7]);
2136 } else {
2137 /*
2138 * Don't let an illegal value for dr7 get set. Specifically,
2139 * check for undefined settings. Setting these bit patterns
2140 * result in undefined behaviour and can lead to an unexpected
2141 * TRCTRAP.
2142 */
2143 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
2144 i++, mask1 <<= 2, mask2 <<= 2)
2145 if ((dbregs->dr[7] & mask1) == mask2)
2146 return (EINVAL);
2148 pcb = td->td_pcb;
2150 /*
2151 * Don't let a process set a breakpoint that is not within the
2152 * process's address space. If a process could do this, it
2153 * could halt the system by setting a breakpoint in the kernel
2154 * (if ddb was enabled). Thus, we need to check to make sure
2155 * that no breakpoints are being enabled for addresses outside
2156 * process's address space, unless, perhaps, we were called by
2157 * uid 0.
2159 * XXX - what about when the watched area of the user's
2160 * address space is written into from within the kernel
2161 * ... wouldn't that still cause a breakpoint to be generated
2162 * from within kernel mode?
2163 */
2165 if (suser(td) != 0) {
2166 if (dbregs->dr[7] & 0x3) {
2167 /* dr0 is enabled */
2168 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2169 return (EINVAL);
2172 if (dbregs->dr[7] & (0x3<<2)) {
2173 /* dr1 is enabled */
2174 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2175 return (EINVAL);
2178 if (dbregs->dr[7] & (0x3<<4)) {
2179 /* dr2 is enabled */
2180 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2181 return (EINVAL);
2184 if (dbregs->dr[7] & (0x3<<6)) {
2185 /* dr3 is enabled */
2186 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2187 return (EINVAL);
2191 pcb->pcb_dr0 = dbregs->dr[0];
2192 pcb->pcb_dr1 = dbregs->dr[1];
2193 pcb->pcb_dr2 = dbregs->dr[2];
2194 pcb->pcb_dr3 = dbregs->dr[3];
2195 pcb->pcb_dr6 = dbregs->dr[6];
2196 pcb->pcb_dr7 = dbregs->dr[7];
2198 pcb->pcb_flags |= PCB_DBREGS;
2201 return (0);
2204 /*
2205 * Return > 0 if a hardware breakpoint has been hit, and the
2206 * breakpoint was in user space. Return 0, otherwise.
2207 */
2208 int
2209 user_dbreg_trap(void)
2211 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2212 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2213 int nbp; /* number of breakpoints that triggered */
2214 caddr_t addr[4]; /* breakpoint addresses */
2215 int i;
2217 dr7 = rdr7();
2218 if ((dr7 & 0x000000ff) == 0) {
2219 /*
2220 * all GE and LE bits in the dr7 register are zero,
2221 * thus the trap couldn't have been caused by the
2222 * hardware debug registers
2223 */
2224 return 0;
2227 nbp = 0;
2228 dr6 = rdr6();
2229 bp = dr6 & 0x0000000f;
2231 if (!bp) {
2232 /*
2233 * None of the breakpoint bits are set meaning this
2234 * trap was not caused by any of the debug registers
2235 */
2236 return 0;
2239 /*
2240 * at least one of the breakpoints were hit, check to see
2241 * which ones and if any of them are user space addresses
2242 */
2244 if (bp & 0x01) {
2245 addr[nbp++] = (caddr_t)rdr0();
2247 if (bp & 0x02) {
2248 addr[nbp++] = (caddr_t)rdr1();
2250 if (bp & 0x04) {
2251 addr[nbp++] = (caddr_t)rdr2();
2253 if (bp & 0x08) {
2254 addr[nbp++] = (caddr_t)rdr3();
2257 for (i=0; i<nbp; i++) {
2258 if (addr[i] <
2259 (caddr_t)VM_MAXUSER_ADDRESS) {
2260 /*
2261 * addr[i] is in user space
2262 */
2263 return nbp;
2267 /*
2268 * None of the breakpoints are in user space.
2269 */
2270 return 0;
2273 #ifndef DEV_APIC
2274 #include <machine/apicvar.h>
2276 /*
2277 * Provide stub functions so that the MADT APIC enumerator in the acpi
2278 * kernel module will link against a kernel without 'device apic'.
2280 * XXX - This is a gross hack.
2281 */
2282 void
2283 apic_register_enumerator(struct apic_enumerator *enumerator)
2287 void *
2288 ioapic_create(uintptr_t addr, int32_t id, int intbase)
2290 return (NULL);
2293 int
2294 ioapic_disable_pin(void *cookie, u_int pin)
2296 return (ENXIO);
2299 int
2300 ioapic_get_vector(void *cookie, u_int pin)
2302 return (-1);
2305 void
2306 ioapic_register(void *cookie)
2310 int
2311 ioapic_remap_vector(void *cookie, u_int pin, int vector)
2313 return (ENXIO);
2316 int
2317 ioapic_set_extint(void *cookie, u_int pin)
2319 return (ENXIO);
2322 int
2323 ioapic_set_nmi(void *cookie, u_int pin)
2325 return (ENXIO);
2328 int
2329 ioapic_set_polarity(void *cookie, u_int pin,enum intr_polarity pol )
2331 return (ENXIO);
2334 int
2335 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger )
2337 return (ENXIO);
2340 void
2341 lapic_create(u_int apic_id, int boot_cpu)
2345 void
2346 lapic_init(uintptr_t addr)
2350 int
2351 lapic_set_lvt_mode(u_int apic_id, u_int lvt, u_int32_t mode)
2353 return (ENXIO);
2356 int
2357 lapic_set_lvt_polarity(u_int apic_id, u_int lvt, enum intr_polarity pol)
2359 return (ENXIO);
2362 int
2363 lapic_set_lvt_triggermode(u_int apic_id, u_int lvt, enum intr_trigger trigger)
2365 return (ENXIO);
2367 #endif
2369 #ifdef KDB
2371 /*
2372 * Provide inb() and outb() as functions. They are normally only
2373 * available as macros calling inlined functions, thus cannot be
2374 * called from the debugger.
2376 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2377 */
2379 #undef inb
2380 #undef outb
2382 /* silence compiler warnings */
2383 u_char inb(u_int);
2384 void outb(u_int, u_char);
2386 u_char
2387 inb(u_int port)
2389 u_char data;
2390 /*
2391 * We use %%dx and not %1 here because i/o is done at %dx and not at
2392 * %edx, while gcc generates inferior code (movw instead of movl)
2393 * if we tell it to load (u_short) port.
2394 */
2395 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2396 return (data);
2399 void
2400 outb(u_int port, u_char data)
2402 u_char al;
2403 /*
2404 * Use an unnecessary assignment to help gcc's register allocator.
2405 * This make a large difference for gcc-1.40 and a tiny difference
2406 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2407 * best results. gcc-2.6.0 can't handle this.
2408 */
2409 al = data;
2410 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2413 #endif /* KDB */