x86_64: save/pop kernel contexts
[akaros.git] / kern / arch / x86 / trap32.h
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.h bit-specific functions.  This is included by trap.h, do not
6  * include it directly.  Any function beginning with x86_ is internal to x86,
7  * and not to be called by the main kernel.  Other functions are part of the
8  * kernel-arch interface. */
9
10 #ifndef ROS_KERN_ARCH_TRAP32_H
11 #define ROS_KERN_ARCH_TRAP32_H
12
13 #ifndef ROS_KERN_ARCH_TRAP_H
14 #error "Do not include arch/trap32.h directly."
15 #endif
16
17 /* For kernel contexts, when we save/restore/move them around. */
18 struct kernel_ctx {
19         struct hw_trapframe             hw_tf;
20 };
21
22 static inline bool in_kernel(struct hw_trapframe *hw_tf)
23 {
24         return (hw_tf->tf_cs & ~3) == GD_KT;
25 }
26
27 static inline void save_kernel_ctx(struct kernel_ctx *ctx)
28 {
29         /* Save the regs and the future esp. */
30         asm volatile("movl %%esp,(%0);       " /* save esp in it's slot*/
31                      "pushl %%eax;           " /* temp save eax */
32                      "leal 1f,%%eax;         " /* get future eip */
33                      "movl %%eax,(%1);       " /* store future eip */
34                      "popl %%eax;            " /* restore eax */
35                      "movl %2,%%esp;         " /* move to the beginning of the tf */
36                      "addl $0x20,%%esp;      " /* move to after the push_regs */
37                      "pushal;                " /* save regs */
38                      "addl $0x44,%%esp;      " /* move to esp slot */
39                      "popl %%esp;            " /* restore esp */
40                      "1:                     " /* where this tf will restart */
41                      : 
42                      : "r"(&ctx->hw_tf.tf_esp), "r"(&ctx->hw_tf.tf_eip),
43                        "g"(&ctx->hw_tf)
44                      : "eax", "memory", "cc");
45 }
46
47 static inline uintptr_t x86_get_ip_hw(struct hw_trapframe *hw_tf)
48 {
49         return hw_tf->tf_eip;
50 }
51
52 static inline void x86_advance_ip(struct hw_trapframe *hw_tf, size_t bytes)
53 {
54         hw_tf->tf_eip += bytes;
55 }
56
57 static inline void x86_fake_rdtscp(struct hw_trapframe *hw_tf)
58 {
59         uint64_t tsc_time = read_tsc();
60         hw_tf->tf_eip += 3;
61         hw_tf->tf_regs.reg_eax = tsc_time & 0xffffffff;
62         hw_tf->tf_regs.reg_edx = tsc_time >> 32;
63         hw_tf->tf_regs.reg_ecx = core_id();
64 }
65
66 static inline void x86_sysenter_init(uintptr_t stacktop)
67 {
68         write_msr(MSR_IA32_SYSENTER_CS, GD_KT);
69         write_msr(MSR_IA32_SYSENTER_ESP, stacktop);
70         write_msr(MSR_IA32_SYSENTER_EIP, (uintptr_t) &sysenter_handler);
71 }
72
73 static inline void x86_set_sysenter_stacktop(uintptr_t stacktop)
74 {
75         write_msr(MSR_IA32_SYSENTER_ESP, stacktop);
76 }
77
78 static inline long x86_get_sysenter_arg0(struct hw_trapframe *hw_tf)
79 {
80         return hw_tf->tf_regs.reg_eax;
81 }
82
83 static inline long x86_get_sysenter_arg1(struct hw_trapframe *hw_tf)
84 {
85         return hw_tf->tf_regs.reg_esi;
86 }
87
88 static inline uintptr_t x86_get_stacktop_tss(struct taskstate *tss)
89 {
90         return tss->ts_esp0;
91 }
92
93 static inline void x86_set_stacktop_tss(struct taskstate *tss, uintptr_t top)
94 {
95         tss->ts_esp0 = top;
96         tss->ts_ss0 = GD_KD;
97 }
98
99 #endif /* ROS_KERN_ARCH_TRAP32_H */