aa64: finalise context switch, page fault handler and syscall
[lunaix-os.git] / lunaix-os / arch / aarch64 / exception / context.S
1 #define __ASM__
2 #include <asm/aa64_msrs.h>
3 #include <asm/bits.h>
4 #include "hart_fields.inc"
5
6 .section .data
7     .align 4
8     .skip 256
9     _aa64_csw_temp_stack:
10
11 .section .text
12     
13     .globl _aa64_evec_prehandle
14     .globl _aa64_switch_task
15
16     _aa64_evec_prehandle:
17         // reservation for struct exec_param
18         sub   sp,  sp,  #8         // push  xzr
19         sub   sp,  sp,  #16        // push {sp_el0, link}
20         sub   sp,  sp,  #16        // push {spsr,   parent_hart}
21
22         stp   lr,  fp, [sp, #-16]! //push {x31-x1}
23         stp  x28, x27, [sp, #-16]!
24         stp  x26, x25, [sp, #-16]!
25         stp  x24, x23, [sp, #-16]!
26         stp  x22, x21, [sp, #-16]!
27         stp  x20, x19, [sp, #-16]!
28         stp  x18, x17, [sp, #-16]!
29         stp  x16, x15, [sp, #-16]!
30         stp  x14, x13, [sp, #-16]!
31         stp  x12, x11, [sp, #-16]!
32         stp  x10,  x9, [sp, #-16]!
33         stp   x8,  x7, [sp, #-16]!
34         stp   x6,  x5, [sp, #-16]!
35         stp   x4,  x3, [sp, #-16]!
36         stp   x2,  x1, [sp, #-16]!
37         stp   x0, xzr, [sp, #-16]!  // push {x0, xzr}
38
39         add   x1, sp,  #hart_execp
40
41         mrs   x0, SP_EL0
42         str   x0, [x1, #execp_spel0_saved]
43
44         mrs   x0, ELR_E1
45         str   x0, [x1, #execp_link]
46
47         mrs   x0, SPSR_EL1
48         str   x0, [x1, #execp_spsr]
49         
50         mov   x0, sp
51         bl    handle_exception
52         
53     do_eret:
54         add   sp, x0,   xzr
55
56         add   x1,   x0, #hart_execp
57
58         ldr   x0,  [x1, #execp_spsr]
59         msr   SPSR_EL1, x0
60
61         ldr   x0,  [x1, #execp_link]
62         msr   ELR_E1,   x0
63
64         ldr   x0,  [x1, #execp_spel0_saved]
65         msr   SP_EL0,   x0
66
67         add   sp,  sp, #hart_regs
68
69         ldr   x0,      [sp, #8 ]!
70         ldp   x1,  x2, [sp, #16]!
71         ldp   x3,  x4, [sp, #16]!
72         ldp   x5,  x6, [sp, #16]!
73         ldp   x7,  x8, [sp, #16]!
74         ldp   x9, x10, [sp, #16]!
75         ldp  x11, x12, [sp, #16]!
76         ldp  x13, x14, [sp, #16]!
77         ldp  x15, x16, [sp, #16]!
78         ldp  x17, x18, [sp, #16]!
79         ldp  x19, x20, [sp, #16]!
80         ldp  x21, x22, [sp, #16]!
81         ldp  x23, x24, [sp, #16]!
82         ldp  x25, x26, [sp, #16]!
83         ldp  x27, x28, [sp, #16]!
84         ldp   fp,  lr, [sp, #16]!
85         
86         // sp now point to the start of exec_param
87
88         sub   sp,  sp, #execp_end
89
90         eret
91
92     _aa64_switch_task:
93         // TODO
94         adr  sp, _aa64_csw_temp_stack
95         
96         bl   aa64_switch_ttbr
97         
98         bl   switch_signposting
99         cbnz x0, _aa64_handle_signal
100
101         b     do_eret
102
103     _aa64_handle_signal:
104         mov  x1, #SPSR_EL0_preset
105         msr  SPSR_EL1, x1
106
107         ldr  x1, [x0, #sigact]
108         msr  ELR_E1, x1
109
110         msr  SP_EL0, x0
111
112         eret