Decoupling Architectural-specific Code (#35)
[lunaix-os.git] / lunaix-os / arch / i386 / includes / sys / hart.h
1 #ifndef __LUNAIX_ARCH_HART_H
2 #define __LUNAIX_ARCH_HART_H
3
4 #include "vectors.h"
5
6 #ifndef __ASM__
7 #include <lunaix/compiler.h>
8 #include <sys/cpu.h>
9
10 struct exec_param;
11
12 struct regcontext
13 {
14     reg_t eax;
15     reg_t ebx;
16     reg_t ecx;
17     reg_t edx;
18     reg_t edi;
19     reg_t ebp;
20     reg_t esi;
21     reg_t ds;
22     reg_t es;
23     reg_t fs;
24     reg_t gs;
25 } compact;
26
27 struct hart_state
28 {
29     unsigned int depth;
30     struct regcontext registers;
31     union
32     {
33         reg_t esp;
34         volatile struct exec_param* execp;
35     };
36 } compact;
37
38 struct exec_param
39 {
40     struct hart_state* parent_state;
41     reg_t vector;
42     reg_t err_code;
43     reg_t eip;
44     reg_t cs;
45     reg_t eflags;
46     reg_t esp;
47     reg_t ss;
48 } compact;
49
50
51 static inline void
52 hart_flow_redirect(struct hart_state* hstate, ptr_t pc, ptr_t sp)
53 {
54     hstate->execp->eip = pc;
55     hstate->execp->esp = sp;
56 }
57
58 static inline ptr_t
59 hart_pc(struct hart_state* hstate)
60 {
61     return hstate->execp->eip;
62 }
63
64 static inline ptr_t
65 hart_sp(struct hart_state* hstate)
66 {
67     return hstate->execp->esp;
68 }
69
70 static inline bool
71 kernel_context(struct hart_state* hstate)
72 {
73     return !((hstate->execp->cs) & 0b11);
74 }
75
76 static inline ptr_t
77 hart_stack_frame(struct hart_state* hstate)
78 {
79     return hstate->registers.ebp;
80 }
81
82 static inline int
83 hart_vector_stamp(struct hart_state* hstate) {
84     return hstate->execp->vector;
85 }
86
87 static inline unsigned int
88 hart_ecause(struct hart_state* hstate) {
89     return hstate->execp->err_code;
90 }
91
92 static inline struct hart_state*
93 hart_parent_state(struct hart_state* hstate)
94 {
95     return hstate->execp->parent_state;
96 }
97
98 static inline void
99 hart_push_state(struct hart_state* p_hstate, struct hart_state* hstate)
100 {
101     hstate->execp->parent_state = p_hstate;
102 }
103
104 #endif
105
106 #endif /* __LUNAIX_ARCH_HART_H */