Monitor command showmappings updates
[akaros.git] / kern / arch / x86 / trap32.c
1 /* Copyright (c) 2009-13 The Regents of the University of California
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * x86 trap.c bit-specific functions. */
6
7 #include <arch/mmu.h>
8 #include <arch/x86.h>
9 #include <arch/arch.h>
10 #include <arch/console.h>
11 #include <arch/apic.h>
12 #include <ros/common.h>
13 #include <smp.h>
14 #include <assert.h>
15 #include <pmap.h>
16 #include <trap.h>
17 #include <monitor.h>
18 #include <process.h>
19 #include <mm.h>
20 #include <stdio.h>
21 #include <slab.h>
22 #include <syscall.h>
23 #include <kdebug.h>
24 #include <kmalloc.h>
25
26 /* Starts running the current TF, just using ret. */
27 void pop_kernel_ctx(struct kernel_ctx *ctx)
28 {
29         asm volatile ("movl %1,%%esp;           " /* move to future stack */
30                       "pushl %2;                " /* push cs */
31                       "movl %0,%%esp;           " /* move to TF */
32                       "addl $0x20,%%esp;        " /* move to tf_gs slot */
33                       "movl %1,(%%esp);         " /* write future esp */
34                       "subl $0x20,%%esp;        " /* move back to tf start */
35                       "popal;                   " /* restore regs */
36                       "popl %%esp;              " /* set stack ptr */
37                       "subl $0x4,%%esp;         " /* jump down past CS */
38                       "ret                      " /* return to the EIP */
39                       :
40                       : "g"(&ctx->hw_tf), "r"(ctx->hw_tf.tf_esp),
41                         "r"(ctx->hw_tf.tf_eip)
42                       : "memory");
43         panic("ret failed");                            /* mostly to placate your mom */
44 }
45
46 static void print_regs(push_regs_t *regs)
47 {
48         printk("  edi  0x%08x\n", regs->reg_edi);
49         printk("  esi  0x%08x\n", regs->reg_esi);
50         printk("  ebp  0x%08x\n", regs->reg_ebp);
51         printk("  oesp 0x%08x\n", regs->reg_oesp);
52         printk("  ebx  0x%08x\n", regs->reg_ebx);
53         printk("  edx  0x%08x\n", regs->reg_edx);
54         printk("  ecx  0x%08x\n", regs->reg_ecx);
55         printk("  eax  0x%08x\n", regs->reg_eax);
56 }
57
58 void print_trapframe(struct hw_trapframe *hw_tf)
59 {
60         static spinlock_t ptf_lock = SPINLOCK_INITIALIZER_IRQSAVE;
61
62         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
63         /* This is only called in debug scenarios, and often when the kernel trapped
64          * and needs to tell us about it.  Disable the lock checker so it doesn't go
65          * nuts when we print/panic */
66         pcpui->__lock_checking_enabled--;
67         spin_lock_irqsave(&ptf_lock);
68         printk("TRAP frame at %p on core %d\n", hw_tf, core_id());
69         print_regs(&hw_tf->tf_regs);
70         printk("  gs   0x----%04x\n", hw_tf->tf_gs);
71         printk("  fs   0x----%04x\n", hw_tf->tf_fs);
72         printk("  es   0x----%04x\n", hw_tf->tf_es);
73         printk("  ds   0x----%04x\n", hw_tf->tf_ds);
74         printk("  trap 0x%08x %s\n",  hw_tf->tf_trapno,
75                                       x86_trapname(hw_tf->tf_trapno));
76         printk("  err  0x%08x\n",     hw_tf->tf_err);
77         printk("  eip  0x%08x\n",     hw_tf->tf_eip);
78         printk("  cs   0x----%04x\n", hw_tf->tf_cs);
79         printk("  flag 0x%08x\n",     hw_tf->tf_eflags);
80         /* Prevents us from thinking these mean something for nested interrupts. */
81         if (hw_tf->tf_cs != GD_KT) {
82                 printk("  esp  0x%08x\n",     hw_tf->tf_esp);
83                 printk("  ss   0x----%04x\n", hw_tf->tf_ss);
84         }
85         spin_unlock_irqsave(&ptf_lock);
86         pcpui->__lock_checking_enabled++;
87 }
88
89 void __arch_reflect_trap_hwtf(struct hw_trapframe *hw_tf, unsigned int trap_nr,
90                               unsigned int err, unsigned long aux)
91 {
92         hw_tf->tf_trapno = trap_nr;
93         /* this can be necessary, since hw_tf is the pcpui one, and the err that
94          * came in probably came from the kernel stack's hw_tf. */
95         hw_tf->tf_err = err;
96         hw_tf->tf_regs.reg_oesp = aux;
97         hw_tf->tf_padding3 = ROS_ARCH_REFL_ID;
98 }