7cd9d6018f59dfb877975d7894243dd4503819e7
[akaros.git] / user / parlib / x86 / vcore.c
1 #include <ros/syscall.h>
2 #include <parlib/vcore.h>
3 #include <parlib/stdio.h>
4 #include <stdlib.h>
5
6 struct syscall vc_entry = {
7         .num = SYS_vc_entry,
8         .err = 0,
9         .retval = 0,
10         .flags = 0,
11         .ev_q = 0,
12         .u_data = 0,
13         .arg0 = 0,
14         .arg1 = 0,
15         .arg2 = 0,
16         .arg3 = 0,
17         .arg4 = 0,
18         .arg5 = 0,
19 };
20
21 void print_hw_tf(struct hw_trapframe *hw_tf)
22 {
23         printf("[user] HW TRAP frame 0x%016x\n", hw_tf);
24         printf("  rax  0x%016lx\n",           hw_tf->tf_rax);
25         printf("  rbx  0x%016lx\n",           hw_tf->tf_rbx);
26         printf("  rcx  0x%016lx\n",           hw_tf->tf_rcx);
27         printf("  rdx  0x%016lx\n",           hw_tf->tf_rdx);
28         printf("  rbp  0x%016lx\n",           hw_tf->tf_rbp);
29         printf("  rsi  0x%016lx\n",           hw_tf->tf_rsi);
30         printf("  rdi  0x%016lx\n",           hw_tf->tf_rdi);
31         printf("  r8   0x%016lx\n",           hw_tf->tf_r8);
32         printf("  r9   0x%016lx\n",           hw_tf->tf_r9);
33         printf("  r10  0x%016lx\n",           hw_tf->tf_r10);
34         printf("  r11  0x%016lx\n",           hw_tf->tf_r11);
35         printf("  r12  0x%016lx\n",           hw_tf->tf_r12);
36         printf("  r13  0x%016lx\n",           hw_tf->tf_r13);
37         printf("  r14  0x%016lx\n",           hw_tf->tf_r14);
38         printf("  r15  0x%016lx\n",           hw_tf->tf_r15);
39         printf("  trap 0x%08x\n",             hw_tf->tf_trapno);
40         printf("  gsbs 0x%016lx\n",           hw_tf->tf_gsbase);
41         printf("  fsbs 0x%016lx\n",           hw_tf->tf_fsbase);
42         printf("  err  0x--------%08x\n",     hw_tf->tf_err);
43         printf("  rip  0x%016lx\n",           hw_tf->tf_rip);
44         printf("  cs   0x------------%04x\n", hw_tf->tf_cs);
45         printf("  flag 0x%016lx\n",           hw_tf->tf_rflags);
46         printf("  rsp  0x%016lx\n",           hw_tf->tf_rsp);
47         printf("  ss   0x------------%04x\n", hw_tf->tf_ss);
48 }
49
50 void print_sw_tf(struct sw_trapframe *sw_tf)
51 {
52         printf("[user] SW TRAP frame 0x%016p\n", sw_tf);
53         printf("  rbx  0x%016lx\n",           sw_tf->tf_rbx);
54         printf("  rbp  0x%016lx\n",           sw_tf->tf_rbp);
55         printf("  r12  0x%016lx\n",           sw_tf->tf_r12);
56         printf("  r13  0x%016lx\n",           sw_tf->tf_r13);
57         printf("  r14  0x%016lx\n",           sw_tf->tf_r14);
58         printf("  r15  0x%016lx\n",           sw_tf->tf_r15);
59         printf("  gsbs 0x%016lx\n",           sw_tf->tf_gsbase);
60         printf("  fsbs 0x%016lx\n",           sw_tf->tf_fsbase);
61         printf("  rip  0x%016lx\n",           sw_tf->tf_rip);
62         printf("  rsp  0x%016lx\n",           sw_tf->tf_rsp);
63         printf(" mxcsr 0x%08x\n",             sw_tf->tf_mxcsr);
64         printf(" fpucw 0x%04x\n",             sw_tf->tf_fpucw);
65 }
66
67 void print_vm_tf(struct vm_trapframe *vm_tf)
68 {
69         printf("[user] VM Trapframe 0x%016x\n", vm_tf);
70         printf("  rax  0x%016lx\n",           vm_tf->tf_rax);
71         printf("  rbx  0x%016lx\n",           vm_tf->tf_rbx);
72         printf("  rcx  0x%016lx\n",           vm_tf->tf_rcx);
73         printf("  rdx  0x%016lx\n",           vm_tf->tf_rdx);
74         printf("  rbp  0x%016lx\n",           vm_tf->tf_rbp);
75         printf("  rsi  0x%016lx\n",           vm_tf->tf_rsi);
76         printf("  rdi  0x%016lx\n",           vm_tf->tf_rdi);
77         printf("  r8   0x%016lx\n",           vm_tf->tf_r8);
78         printf("  r9   0x%016lx\n",           vm_tf->tf_r9);
79         printf("  r10  0x%016lx\n",           vm_tf->tf_r10);
80         printf("  r11  0x%016lx\n",           vm_tf->tf_r11);
81         printf("  r12  0x%016lx\n",           vm_tf->tf_r12);
82         printf("  r13  0x%016lx\n",           vm_tf->tf_r13);
83         printf("  r14  0x%016lx\n",           vm_tf->tf_r14);
84         printf("  r15  0x%016lx\n",           vm_tf->tf_r15);
85         printf("  rip  0x%016lx\n",           vm_tf->tf_rip);
86         printf("  rflg 0x%016lx\n",           vm_tf->tf_rflags);
87         printf("  rsp  0x%016lx\n",           vm_tf->tf_rsp);
88         printf("  cr2  0x%016lx\n",           vm_tf->tf_cr2);
89         printf("  cr3  0x%016lx\n",           vm_tf->tf_cr3);
90         printf("Gpcore 0x%08x\n",             vm_tf->tf_guest_pcoreid);
91         printf("Flags  0x%08x\n",             vm_tf->tf_flags);
92         printf("Inject 0x%08x\n",             vm_tf->tf_trap_inject);
93         printf("ExitRs 0x%08x\n",             vm_tf->tf_exit_reason);
94         printf("ExitQl 0x%08x\n",             vm_tf->tf_exit_qual);
95         printf("Intr1  0x%016lx\n",           vm_tf->tf_intrinfo1);
96         printf("Intr2  0x%016lx\n",           vm_tf->tf_intrinfo2);
97         printf("GVA    0x%016lx\n",           vm_tf->tf_guest_va);
98         printf("GPA    0x%016lx\n",           vm_tf->tf_guest_pa);
99 }
100
101 void print_user_context(struct user_context *ctx)
102 {
103         switch (ctx->type) {
104         case ROS_HW_CTX:
105                 print_hw_tf(&ctx->tf.hw_tf);
106                 break;
107         case ROS_SW_CTX:
108                 print_sw_tf(&ctx->tf.sw_tf);
109                 break;
110         case ROS_VM_CTX:
111                 print_vm_tf(&ctx->tf.vm_tf);
112                 break;
113         default:
114                 printf("Unknown context type %d\n", ctx->type);
115         }
116 }
117
118 /* The second-lowest level function jumped to by the kernel on every vcore
119  * entry.  We get called from __kernel_vcore_entry.
120  *
121  * We should consider making it mandatory to set the tls_desc in the kernel. We
122  * wouldn't even need to pass the vcore id to user space at all if we did this.
123  * It would already be set in the preinstalled TLS as __vcore_id. */
124 void __attribute__((noreturn)) __kvc_entry_c(void)
125 {
126         /* The kernel sets the TLS desc for us, based on whatever is in VCPD.
127          *
128          * x86 32-bit TLS is pretty jacked up, so the kernel doesn't set the TLS
129          * desc for us.  it's a little more expensive to do it here, esp for
130          * amd64.  Can remove this when/if we overhaul 32 bit TLS. */
131         int id = __vcore_id_on_entry;
132
133         #ifndef __x86_64__
134         set_tls_desc(vcpd_of(id)->vcore_tls_desc);
135         #endif
136         /* Every time the vcore comes up, it must set that it is in vcore context.
137          * uthreads may share the same TLS as their vcore (when uthreads do not have
138          * their own TLS), and if a uthread was preempted, __vcore_context == FALSE,
139          * and that will continue to be true the next time the vcore pops up. */
140         __vcore_context = TRUE;
141         vcore_entry();
142         fprintf(stderr, "vcore_entry() should never return!\n");
143         abort();
144         __builtin_unreachable();
145 }