1 | /* |
---|
2 | * hal_kentry.h - General values used in the different kernel entries |
---|
3 | * |
---|
4 | * Copyright (c) 2017 Maxime Villard |
---|
5 | * |
---|
6 | * This file is part of ALMOS-MKH. |
---|
7 | * |
---|
8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
9 | * under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; version 2.0 of the License. |
---|
11 | * |
---|
12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | * General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | */ |
---|
21 | |
---|
22 | #define T_PRIVINFLT 0 /* privileged instruction */ |
---|
23 | #define T_BPTFLT 1 /* breakpoint trap */ |
---|
24 | #define T_ARITHTRAP 2 /* arithmetic trap */ |
---|
25 | #define T_ASTFLT 3 /* asynchronous system trap */ |
---|
26 | #define T_PROTFLT 4 /* protection fault */ |
---|
27 | #define T_TRCTRAP 5 /* trace trap */ |
---|
28 | #define T_PAGEFLT 6 /* page fault */ |
---|
29 | #define T_ALIGNFLT 7 /* alignment fault */ |
---|
30 | #define T_DIVIDE 8 /* integer divide fault */ |
---|
31 | #define T_NMI 9 /* non-maskable interrupt */ |
---|
32 | #define T_OFLOW 10 /* overflow trap */ |
---|
33 | #define T_BOUND 11 /* bounds check fault */ |
---|
34 | #define T_DNA 12 /* device not available fault */ |
---|
35 | #define T_DOUBLEFLT 13 /* double fault */ |
---|
36 | #define T_FPOPFLT 14 /* fp coprocessor operand fetch fault */ |
---|
37 | #define T_TSSFLT 15 /* invalid tss fault */ |
---|
38 | #define T_SEGNPFLT 16 /* segment not present fault */ |
---|
39 | #define T_STKFLT 17 /* stack fault */ |
---|
40 | #define T_MCA 18 /* machine check */ |
---|
41 | #define T_XMM 19 /* SSE FP exception */ |
---|
42 | #define T_RESERVED 20 /* reserved fault base */ |
---|
43 | |
---|
44 | /* Trap's coming from user mode */ |
---|
45 | #define T_USER 0x100 |
---|
46 | |
---|
47 | #define TLSVAR(off) %gs:TLS_ ## off |
---|
48 | |
---|
49 | /* |
---|
50 | * Processor Status Longword. |
---|
51 | */ |
---|
52 | #define PSL_C 0x00000001 /* carry flag */ |
---|
53 | #define PSL_PF 0x00000004 /* parity flag */ |
---|
54 | #define PSL_AF 0x00000010 /* auxiliary carry flag */ |
---|
55 | #define PSL_Z 0x00000040 /* zero flag */ |
---|
56 | #define PSL_N 0x00000080 /* sign flag */ |
---|
57 | #define PSL_T 0x00000100 /* trap flag */ |
---|
58 | #define PSL_I 0x00000200 /* interrupt enable flag */ |
---|
59 | #define PSL_D 0x00000400 /* direction flag */ |
---|
60 | #define PSL_V 0x00000800 /* overflow flag */ |
---|
61 | #define PSL_IOPL 0x00003000 /* i/o privilege level */ |
---|
62 | #define PSL_NT 0x00004000 /* nested task */ |
---|
63 | #define PSL_RF 0x00010000 /* resume flag */ |
---|
64 | #define PSL_VM 0x00020000 /* virtual 8086 mode */ |
---|
65 | #define PSL_AC 0x00040000 /* alignment check flag */ |
---|
66 | #define PSL_VIF 0x00080000 /* virtual interrupt enable flag */ |
---|
67 | #define PSL_VIP 0x00100000 /* virtual interrupt pending flag */ |
---|
68 | #define PSL_ID 0x00200000 /* identification flag */ |
---|
69 | |
---|
70 | #define PSL_MBO 0x00000002 /* must be one bits */ |
---|
71 | #define PSL_MBZ 0xffc08028 /* must be zero bits */ |
---|
72 | |
---|
73 | #define PSL_USERSET (PSL_MBO | PSL_I) |
---|
74 | |
---|
75 | /* |
---|
76 | * Trap frame |
---|
77 | */ |
---|
78 | |
---|
79 | #define TF_REGSIZE (19 * 8) |
---|
80 | |
---|
81 | #define INTR_SAVE_REGS \ |
---|
82 | subq $TF_REGSIZE,%rsp ; \ |
---|
83 | movq %rax,TF_RAX(%rsp) ; \ |
---|
84 | movq %rbx,TF_RBX(%rsp) ; \ |
---|
85 | movq %rcx,TF_RCX(%rsp) ; \ |
---|
86 | movq %rdx,TF_RDX(%rsp) ; \ |
---|
87 | movq %rbp,TF_RBP(%rsp) ; \ |
---|
88 | movq %rdi,TF_RDI(%rsp) ; \ |
---|
89 | movq %rsi,TF_RSI(%rsp) ; \ |
---|
90 | movq %r8,TF_R8(%rsp) ; \ |
---|
91 | movq %r9,TF_R9(%rsp) ; \ |
---|
92 | movq %r10,TF_R10(%rsp) ; \ |
---|
93 | movq %r11,TF_R11(%rsp) ; \ |
---|
94 | movq %r12,TF_R12(%rsp) ; \ |
---|
95 | movq %r13,TF_R13(%rsp) ; \ |
---|
96 | movq %r14,TF_R14(%rsp) ; \ |
---|
97 | movq %r15,TF_R15(%rsp) ; \ |
---|
98 | /* movw %gs,TF_GS(%rsp) */ ; \ |
---|
99 | movw %fs,TF_FS(%rsp) ; \ |
---|
100 | movw %es,TF_ES(%rsp) ; \ |
---|
101 | movw %ds,TF_DS(%rsp) ; \ |
---|
102 | cld |
---|
103 | |
---|
104 | #define INTR_RESTORE_REGS \ |
---|
105 | movq TF_RAX(%rsp),%rax ; \ |
---|
106 | movq TF_RBX(%rsp),%rbx ; \ |
---|
107 | movq TF_RCX(%rsp),%rcx ; \ |
---|
108 | movq TF_RDX(%rsp),%rdx ; \ |
---|
109 | movq TF_RBP(%rsp),%rbp ; \ |
---|
110 | movq TF_RDI(%rsp),%rdi ; \ |
---|
111 | movq TF_RSI(%rsp),%rsi ; \ |
---|
112 | movq TF_R8(%rsp),%r8 ; \ |
---|
113 | movq TF_R9(%rsp),%r9 ; \ |
---|
114 | movq TF_R10(%rsp),%r10 ; \ |
---|
115 | movq TF_R11(%rsp),%r11 ; \ |
---|
116 | movq TF_R12(%rsp),%r12 ; \ |
---|
117 | movq TF_R13(%rsp),%r13 ; \ |
---|
118 | movq TF_R14(%rsp),%r14 ; \ |
---|
119 | movq TF_R15(%rsp),%r15 ; \ |
---|
120 | /* movw TF_GS(%rsp),%gs */ ; \ |
---|
121 | movw TF_FS(%rsp),%fs ; \ |
---|
122 | movw TF_ES(%rsp),%es ; \ |
---|
123 | movw TF_DS(%rsp),%ds ; \ |
---|
124 | addq $TF_REGSIZE,%rsp ; \ |
---|
125 | cld |
---|
126 | |
---|
127 | #ifndef x86_ASM |
---|
128 | |
---|
129 | /* |
---|
130 | * The x86_64 CPU trap frame. !!WARNING!! The size of this structure must be |
---|
131 | * exactly TF_SIZE. |
---|
132 | */ |
---|
133 | typedef struct hal_trapframe_s { |
---|
134 | /* Pushed by INTR_SAVE_REGS */ |
---|
135 | uint64_t tf_rax; |
---|
136 | uint64_t tf_rbx; |
---|
137 | uint64_t tf_rcx; |
---|
138 | uint64_t tf_rdx; |
---|
139 | uint64_t tf_rdi; |
---|
140 | uint64_t tf_rsi; |
---|
141 | uint64_t tf_rbp; |
---|
142 | uint64_t tf_r8; |
---|
143 | uint64_t tf_r9; |
---|
144 | uint64_t tf_r10; |
---|
145 | uint64_t tf_r11; |
---|
146 | uint64_t tf_r12; |
---|
147 | uint64_t tf_r13; |
---|
148 | uint64_t tf_r14; |
---|
149 | uint64_t tf_r15; |
---|
150 | uint64_t tf_gs; |
---|
151 | uint64_t tf_fs; |
---|
152 | uint64_t tf_es; |
---|
153 | uint64_t tf_ds; |
---|
154 | |
---|
155 | /* Pushed by the ISR */ |
---|
156 | uint64_t tf_trapno; |
---|
157 | |
---|
158 | /* Pushed by the hardware if exception */ |
---|
159 | uint64_t tf_err; /* in fact, this one may not... */ |
---|
160 | uint64_t tf_rip; |
---|
161 | uint64_t tf_cs; |
---|
162 | uint64_t tf_rflags; |
---|
163 | |
---|
164 | /* Always pushed by the hardware */ |
---|
165 | uint64_t tf_rsp; |
---|
166 | uint64_t tf_ss; |
---|
167 | } hal_trapframe_t; |
---|
168 | |
---|
169 | typedef struct hal_cpu_context_s { |
---|
170 | uint64_t ctx_rsp0; |
---|
171 | uint64_t ctx_tf; |
---|
172 | uint64_t ctx_intr; |
---|
173 | hal_trapframe_t ctx_hidden_tf; |
---|
174 | } hal_cpu_context_t; |
---|
175 | |
---|
176 | #else |
---|
177 | |
---|
178 | /* Offsets in the trapframe structure */ |
---|
179 | #define TF_RAX 0 |
---|
180 | #define TF_RBX 8 |
---|
181 | #define TF_RCX 16 |
---|
182 | #define TF_RDX 24 |
---|
183 | #define TF_RDI 32 |
---|
184 | #define TF_RSI 40 |
---|
185 | #define TF_RBP 48 |
---|
186 | #define TF_R8 56 |
---|
187 | #define TF_R9 64 |
---|
188 | #define TF_R10 72 |
---|
189 | #define TF_R11 80 |
---|
190 | #define TF_R12 88 |
---|
191 | #define TF_R13 96 |
---|
192 | #define TF_R14 104 |
---|
193 | #define TF_R15 112 |
---|
194 | #define TF_GS 120 |
---|
195 | #define TF_FS 128 |
---|
196 | #define TF_ES 136 |
---|
197 | #define TF_DS 144 |
---|
198 | |
---|
199 | /* Size of the trapframe structure */ |
---|
200 | #define TF_SIZE 208 |
---|
201 | |
---|
202 | /* Offsets in the context structure */ |
---|
203 | #define CTX_RSP0 0 |
---|
204 | #define CTX_TF 8 |
---|
205 | #define CTX_INTR 16 |
---|
206 | |
---|
207 | #endif |
---|
208 | |
---|