x86_64: GS base work
[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 /* Starts running the current TF, just using ret. */
27 void pop_kernel_ctx(struct kernel_ctx *ctx)
28 {
29         #if 0
30         asm volatile ("movl %1,%%esp;           " /* move to future stack */
31                       "pushl %2;                " /* push cs */
32                       "movl %0,%%esp;           " /* move to TF */
33                       "addl $0x20,%%esp;        " /* move to tf_gs slot */
34                       "movl %1,(%%esp);         " /* write future esp */
35                       "subl $0x20,%%esp;        " /* move back to tf start */
36                       "popal;                   " /* restore regs */
37                       "popl %%esp;              " /* set stack ptr */
38                       "subl $0x4,%%esp;         " /* jump down past CS */
39                       "ret                      " /* return to the EIP */
40                       :
41                       : "g"(&ctx->hw_tf), "r"(ctx->hw_tf.tf_esp),
42                         "r"(ctx->hw_tf.tf_eip)
43                       : "memory");
44         #endif
45         panic("ret failed");
46 }
47
48 void print_trapframe(struct hw_trapframe *hw_tf)
49 {
50         static spinlock_t ptf_lock = SPINLOCK_INITIALIZER_IRQSAVE;
51
52         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
53         /* This is only called in debug scenarios, and often when the kernel trapped
54          * and needs to tell us about it.  Disable the lock checker so it doesn't go
55          * nuts when we print/panic */
56         pcpui->__lock_depth_disabled++;
57         spin_lock_irqsave(&ptf_lock);
58         printk("TRAP frame at %p on core %d\n", hw_tf, core_id());
59         printk("  rax  0x%016lx\n",           hw_tf->tf_rax);
60         printk("  rbx  0x%016lx\n",           hw_tf->tf_rbx);
61         printk("  rcx  0x%016lx\n",           hw_tf->tf_rcx);
62         printk("  rdx  0x%016lx\n",           hw_tf->tf_rdx);
63         printk("  rbp  0x%016lx\n",           hw_tf->tf_rbp);
64         printk("  rsi  0x%016lx\n",           hw_tf->tf_rsi);
65         printk("  rdi  0x%016lx\n",           hw_tf->tf_rdi);
66         printk("  r8   0x%016lx\n",           hw_tf->tf_r8);
67         printk("  r9   0x%016lx\n",           hw_tf->tf_r9);
68         printk("  r10  0x%016lx\n",           hw_tf->tf_r10);
69         printk("  r11  0x%016lx\n",           hw_tf->tf_r11);
70         printk("  r12  0x%016lx\n",           hw_tf->tf_r12);
71         printk("  r13  0x%016lx\n",           hw_tf->tf_r13);
72         printk("  r14  0x%016lx\n",           hw_tf->tf_r14);
73         printk("  r15  0x%016lx\n",           hw_tf->tf_r15);
74         printk("  trap 0x%08x %s\n",          hw_tf->tf_trapno,
75                                               x86_trapname(hw_tf->tf_trapno));
76         /* FYI: these aren't physically adjacent to trap and err */
77         printk("  gsbs 0x%016lx\n",           hw_tf->tf_gsbase);
78         printk("  fsbs 0x%016lx\n",           hw_tf->tf_fsbase);
79         printk("  err  0x--------%08x\n",     hw_tf->tf_err);
80         printk("  rip  0x%016lx\n",           hw_tf->tf_rip);
81         printk("  cs   0x------------%04x\n", hw_tf->tf_cs);
82         printk("  flag 0x%016lx\n",           hw_tf->tf_rflags);
83         printk("  rsp  0x%016lx\n",           hw_tf->tf_rsp);
84         printk("  ss   0x------------%04x\n", hw_tf->tf_ss);
85         spin_unlock_irqsave(&ptf_lock);
86         pcpui->__lock_depth_disabled--;
87
88         /* Used in trapentry64.S */
89         static_assert(offsetof(struct hw_trapframe, tf_cs) - 
90                       offsetof(struct hw_trapframe, tf_rax) == 0x90);
91 }
92
93 void page_fault_handler(struct hw_trapframe *hw_tf)
94 {
95         uintptr_t fault_va = rcr2();
96         int prot = hw_tf->tf_err & PF_ERROR_WRITE ? PROT_WRITE : PROT_READ;
97         int err;
98
99         /* TODO - handle kernel page faults */
100         if ((hw_tf->tf_cs & 3) == 0) {
101                 print_trapframe(hw_tf);
102                 panic("Page Fault in the Kernel at 0x%08x!", fault_va);
103                 /* if we want to do something like kill a process or other code, be
104                  * aware we are in a sort of irq-like context, meaning the main kernel
105                  * code we 'interrupted' could be holding locks - even irqsave locks. */
106         }
107         /* safe to reenable after rcr2 */
108         enable_irq();
109         if ((err = handle_page_fault(current, fault_va, prot))) {
110                 /* Destroy the faulting process */
111                 printk("[%08x] user %s fault va %08x ip %08x on core %d with err %d\n",
112                        current->pid, prot & PROT_READ ? "READ" : "WRITE", fault_va,
113                        hw_tf->tf_rip, core_id(), err);
114                 print_trapframe(hw_tf);
115                 /* Turn this on to help debug bad function pointers */
116                 printd("rsp %p\n\t 0(rsp): %p\n\t 8(rsp): %p\n\t 16(rsp): %p\n"
117                        "\t24(rsp): %p\n", hw_tf->tf_rsp,
118                        *(uintptr_t*)(hw_tf->tf_rsp +  0),
119                        *(uintptr_t*)(hw_tf->tf_rsp +  8),
120                        *(uintptr_t*)(hw_tf->tf_rsp + 16),
121                        *(uintptr_t*)(hw_tf->tf_rsp + 24));
122                 proc_destroy(current);
123         }
124 }
125
126 void sysenter_init(void)
127 {
128         //write_msr(MSR_IA32_SYSENTER_CS, GD_KT);
129         //write_msr(MSR_IA32_SYSENTER_ESP, ts.ts_esp0);
130         //write_msr(MSR_IA32_SYSENTER_EIP, (uint32_t) &sysenter_handler);
131 }