Decoupling Architectural-specific Code (#35)
[lunaix-os.git] / lunaix-os / arch / i386 / includes / sys / abi.h
1 #ifndef __LUNAIX_I386ABI_H
2 #define __LUNAIX_I386ABI_H
3
4 #include "sys/x86_isa.h"
5
6 #define stack_alignment 0xfffffff0
7
8 #ifndef __ASM__
9 #define align_stack(ptr) ((ptr) & stack_alignment)
10 #define store_retval(retval) current_thread->hstate->registers.eax = (retval)
11
12 #define store_retval_to(th, retval) (th)->hstate->registers.eax = (retval)
13
14 static inline void must_inline 
15 j_usr(ptr_t sp, ptr_t pc) 
16 {
17     asm volatile("movw %0, %%ax\n"
18                  "movw %%ax, %%es\n"
19                  "movw %%ax, %%ds\n"
20                  "movw %%ax, %%fs\n"
21                  "movw %%ax, %%gs\n"
22                  "pushl %0\n"
23                  "pushl %1\n"
24                  "pushl %2\n"
25                  "pushl %3\n"
26                  "retf" ::"i"(UDATA_SEG),
27                  "r"(sp),
28                  "i"(UCODE_SEG),
29                  "r"(pc)
30                  : "eax", "memory");
31 }
32
33
34 static inline void must_inline noret
35 switch_context() {
36     asm volatile("jmp do_switch\n");
37     unreachable;
38 }
39
40 #define push_arg1(stack_ptr, arg) *((typeof((arg))*)(stack_ptr)--) = arg
41 #define push_arg2(stack_ptr, arg1, arg2)                                       \
42     {                                                                          \
43         *((typeof((arg1))*)(stack_ptr)--) = arg1;                              \
44         *((typeof((arg2))*)(stack_ptr)--) = arg2;                              \
45     }
46 #define push_arg3(stack_ptr, arg1, arg2, arg3)                                 \
47     {                                                                          \
48         *((typeof((arg1))*)(stack_ptr)--) = arg1;                              \
49         *((typeof((arg2))*)(stack_ptr)--) = arg2;                              \
50         *((typeof((arg3))*)(stack_ptr)--) = arg3;                              \
51     }
52 #define push_arg4(stack_ptr, arg1, arg2, arg3, arg4)                           \
53     {                                                                          \
54         *((typeof((arg1))*)(stack_ptr)--) = arg1;                              \
55         *((typeof((arg2))*)(stack_ptr)--) = arg2;                              \
56         *((typeof((arg3))*)(stack_ptr)--) = arg3;                              \
57         *((typeof((arg4))*)(stack_ptr)--) = arg4;                              \
58     }
59
60
61 static inline ptr_t must_inline
62 abi_get_callframe()
63 {
64     ptr_t val;
65     asm("movl %%ebp, %0" : "=r"(val)::);
66     return val;
67 }
68
69 static inline ptr_t
70 abi_get_retaddr()
71 {
72     return *((ptr_t*)abi_get_callframe() + 1);
73 }
74
75 static inline ptr_t
76 abi_get_retaddrat(ptr_t fp)
77 {
78     return *((ptr_t*)fp + 1);
79 }
80 #endif
81 #endif /* __LUNAIX_ABI_H */