rewrite the device subsystem interfaces (#48)
[lunaix-os.git] / lunaix-os / arch / x86 / includes / asm / hart.h
1 #ifndef __LUNAIX_ARCH_HART_H
2 #define __LUNAIX_ARCH_HART_H
3
4 #include "x86_ivs.h"
5
6 #ifndef __ASM__
7 #include <lunaix/types.h>
8
9 #include "x86_cpu.h"
10
11 struct hart_state;
12
13 #ifdef CONFIG_ARCH_X86_64
14 struct regcontext
15 {
16     reg_t rax;
17     reg_t rbx;
18     reg_t rcx;
19     reg_t rdx;
20     reg_t rdi;
21     reg_t rbp;
22     reg_t rsi;
23     reg_t r8;
24     reg_t r9;
25     reg_t r10;
26     reg_t r11;
27     reg_t r12;
28     reg_t r13;
29     reg_t r14;
30     reg_t r15;
31 } compact;
32
33 struct exec_param
34 {
35     struct hart_state* parent_state;
36     reg_t vector;
37     reg_t err_code;
38     reg_t rip;
39     reg_t cs;
40     reg_t rflags;
41     reg_t rsp;
42     reg_t ss;
43 } compact;
44
45
46 #else
47 struct regcontext
48 {
49     reg_t eax;
50     reg_t ebx;
51     reg_t ecx;
52     reg_t edx;
53     reg_t edi;
54     reg_t ebp;
55     reg_t esi;
56     reg_t ds;
57     reg_t es;
58     reg_t fs;
59     reg_t gs;
60 } compact;
61
62 struct exec_param
63 {
64     struct hart_state* parent_state;
65     reg_t vector;
66     reg_t err_code;
67     reg_t eip;
68     reg_t cs;
69     reg_t eflags;
70     reg_t esp;
71     reg_t ss;
72 } compact;
73
74 #endif
75
76
77 struct hart_state
78 {
79     reg_t depth;
80     struct regcontext registers;
81     union
82     {
83         reg_t sp;
84         volatile struct exec_param* execp;
85     };
86 } compact;
87
88 static inline int
89 hart_vector_stamp(struct hart_state* hstate) {
90     return hstate->execp->vector;
91 }
92
93 static inline unsigned int
94 hart_ecause(struct hart_state* hstate) {
95     return hstate->execp->err_code;
96 }
97
98 static inline struct hart_state*
99 hart_parent_state(struct hart_state* hstate)
100 {
101     return hstate->execp->parent_state;
102 }
103
104 static inline void
105 hart_push_state(struct hart_state* p_hstate, struct hart_state* hstate)
106 {
107     hstate->execp->parent_state = p_hstate;
108 }
109
110
111 #ifdef CONFIG_ARCH_X86_64
112 static inline ptr_t
113 hart_pc(struct hart_state* hstate)
114 {
115     return hstate->execp->rip;
116 }
117
118 static inline ptr_t
119 hart_sp(struct hart_state* hstate)
120 {
121     return hstate->execp->rsp;
122 }
123
124 static inline bool
125 kernel_context(struct hart_state* hstate)
126 {
127     return !((hstate->execp->cs) & 0b11);
128 }
129
130 static inline ptr_t
131 hart_stack_frame(struct hart_state* hstate)
132 {
133     return hstate->registers.rbp;
134 }
135
136 #else
137 static inline ptr_t
138 hart_pc(struct hart_state* hstate)
139 {
140     return hstate->execp->eip;
141 }
142
143 static inline ptr_t
144 hart_sp(struct hart_state* hstate)
145 {
146     return hstate->execp->esp;
147 }
148
149 static inline bool
150 kernel_context(struct hart_state* hstate)
151 {
152     return !((hstate->execp->cs) & 0b11);
153 }
154
155 static inline ptr_t
156 hart_stack_frame(struct hart_state* hstate)
157 {
158     return hstate->registers.ebp;
159 }
160
161 #endif
162
163 #endif
164
165 #endif /* __LUNAIX_ARCH_HART_H */