BXE: min->MIN, plus an spatch
[akaros.git] / kern / arch / x86 / trap64.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 void print_trapframe(struct hw_trapframe *hw_tf)
27 {
28         static spinlock_t ptf_lock = SPINLOCK_INITIALIZER_IRQSAVE;
29
30         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
31         /* This is only called in debug scenarios, and often when the kernel trapped
32          * and needs to tell us about it.  Disable the lock checker so it doesn't go
33          * nuts when we print/panic */
34         pcpui->__lock_checking_enabled--;
35         spin_lock_irqsave(&ptf_lock);
36         printk("HW TRAP frame at %p on core %d\n", hw_tf, core_id());
37         printk("  rax  0x%016lx\n",           hw_tf->tf_rax);
38         printk("  rbx  0x%016lx\n",           hw_tf->tf_rbx);
39         printk("  rcx  0x%016lx\n",           hw_tf->tf_rcx);
40         printk("  rdx  0x%016lx\n",           hw_tf->tf_rdx);
41         printk("  rbp  0x%016lx\n",           hw_tf->tf_rbp);
42         printk("  rsi  0x%016lx\n",           hw_tf->tf_rsi);
43         printk("  rdi  0x%016lx\n",           hw_tf->tf_rdi);
44         printk("  r8   0x%016lx\n",           hw_tf->tf_r8);
45         printk("  r9   0x%016lx\n",           hw_tf->tf_r9);
46         printk("  r10  0x%016lx\n",           hw_tf->tf_r10);
47         printk("  r11  0x%016lx\n",           hw_tf->tf_r11);
48         printk("  r12  0x%016lx\n",           hw_tf->tf_r12);
49         printk("  r13  0x%016lx\n",           hw_tf->tf_r13);
50         printk("  r14  0x%016lx\n",           hw_tf->tf_r14);
51         printk("  r15  0x%016lx\n",           hw_tf->tf_r15);
52         printk("  trap 0x%08x %s\n",          hw_tf->tf_trapno,
53                                               x86_trapname(hw_tf->tf_trapno));
54         /* FYI: these aren't physically adjacent to trap and err */
55         if (hw_tf->tf_cs == GD_KT)
56                 printk("  gsbs 0x%016lx\n",       read_msr(MSR_GS_BASE));
57         else
58                 printk("  gsbs 0x%016lx\n",       hw_tf->tf_gsbase);
59         printk("  fsbs 0x%016lx\n",           hw_tf->tf_fsbase);
60         printk("  err  0x--------%08x\n",     hw_tf->tf_err);
61         printk("  rip  0x%016lx\n",           hw_tf->tf_rip);
62         printk("  cs   0x------------%04x\n", hw_tf->tf_cs);
63         printk("  flag 0x%016lx\n",           hw_tf->tf_rflags);
64         printk("  rsp  0x%016lx\n",           hw_tf->tf_rsp);
65         printk("  ss   0x------------%04x\n", hw_tf->tf_ss);
66         spin_unlock_irqsave(&ptf_lock);
67         pcpui->__lock_checking_enabled++;
68
69         /* Used in trapentry64.S */
70         static_assert(offsetof(struct hw_trapframe, tf_cs) - 
71                       offsetof(struct hw_trapframe, tf_rax) == 0x90);
72         /* Used in trap64.h */
73         static_assert(offsetof(struct per_cpu_info, stacktop) == 0);
74 }
75
76 void print_swtrapframe(struct sw_trapframe *sw_tf)
77 {
78         static spinlock_t ptf_lock = SPINLOCK_INITIALIZER_IRQSAVE;
79         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
80         pcpui->__lock_checking_enabled--;
81         spin_lock_irqsave(&ptf_lock);
82         printk("SW TRAP frame at %p on core %d\n", sw_tf, core_id());
83         printk("  rbx  0x%016lx\n",           sw_tf->tf_rbx);
84         printk("  rbp  0x%016lx\n",           sw_tf->tf_rbp);
85         printk("  r12  0x%016lx\n",           sw_tf->tf_r12);
86         printk("  r13  0x%016lx\n",           sw_tf->tf_r13);
87         printk("  r14  0x%016lx\n",           sw_tf->tf_r14);
88         printk("  r15  0x%016lx\n",           sw_tf->tf_r15);
89         printk("  gsbs 0x%016lx\n",           sw_tf->tf_gsbase);
90         printk("  fsbs 0x%016lx\n",           sw_tf->tf_fsbase);
91         printk("  rip  0x%016lx\n",           sw_tf->tf_rip);
92         printk("  rsp  0x%016lx\n",           sw_tf->tf_rsp);
93         printk(" mxcsr 0x%08x\n",             sw_tf->tf_mxcsr);
94         printk(" fpucw 0x%04x\n",             sw_tf->tf_fpucw);
95         spin_unlock_irqsave(&ptf_lock);
96         pcpui->__lock_checking_enabled++;
97 }
98
99 void __arch_reflect_trap_hwtf(struct hw_trapframe *hw_tf, unsigned int trap_nr,
100                               unsigned int err, unsigned long aux)
101 {
102         hw_tf->tf_trapno = trap_nr;
103         /* this can be necessary, since hw_tf is the pcpui one, and the err that
104          * came in probably came from the kernel stack's hw_tf. */
105         hw_tf->tf_err = err;
106         hw_tf->tf_padding4 = (uint32_t)(aux);
107         hw_tf->tf_padding5 = (uint32_t)(aux >> 32);
108         hw_tf->tf_padding3 = ROS_ARCH_REFL_ID;
109 }