]> pd.if.org Git - zos/blob - idt.s
add a readme with a public domain note
[zos] / idt.s
1 struc idt_entry
2 .handler_low:   resw 1
3 .selector:      resw 1
4 .ist:           resb 1
5 .flags:         resb 1
6 .handler_high:  resw 1
7 .handler_highest:       resd 1
8 .reserved:      resd 1
9 endstruc
10
11 align 16
12
13 global set_idt_entry:function
14
15 ;void set_idt_entry(
16 ;       struct idt_entry *entry, rdi
17 ;       uintptr_t handler,       rsi
18 ;       uint16_t selector,       rdx
19 ;       uint8_t flags,           rcx
20 ;       uint8_t ist              r8
21
22 set_idt_entry:
23         mov [rdi + idt_entry.flags], cl
24         mov [rdi + idt_entry.ist], r8b
25         mov [rdi + idt_entry.selector], dx
26         mov [rdi + idt_entry.handler_low], si
27         shr rsi, 16
28         mov [rdi + idt_entry.handler_high], si
29         shr rsi, 16
30         mov [rdi + idt_entry.handler_highest], esi
31         mov [rdi + idt_entry.reserved], dword 0x0
32
33 global load_idt:function
34
35 load_idt:
36 ;       size_t limit = sizeof(struct idt_entry) * length - 1;
37         shl rsi, 4
38         sub rsi, 1
39
40         sub rsp, 10
41         mov [rsp], si
42         mov [rsp + 2], rdi
43         lidt [rsp]
44         add rsp, 10
45         ret
46
47 section .text
48
49 extern interrupt_handler
50 extern global_errno
51 extern printk
52
53 %macro makeisr 2
54 global isr%1:function
55 isr%1:
56         push %2 ; err_code
57         push %1 ; int_no
58         jmp interrupt_handler_prepare
59 %endmacro
60
61 %macro makeisr 1
62 global isr%1:function
63 isr%1:
64         ; error code pushed by processor
65         push %1 ; int_no
66         jmp interrupt_handler_prepare
67 %endmacro
68
69 %macro makeirq 2
70 global irq%1:function
71 irq%1:
72         push 0 ; err_code
73         push %2 ; int_no
74         jmp interrupt_handler_prepare
75 %endmacro
76
77 ; create isr 0-7, which need an error code pushed
78 %assign i 0
79 %rep 8
80
81 makeisr i,0 
82
83 %assign i i+1
84 %endrep
85
86 makeisr 8
87 makeisr 9,0
88 makeisr 10
89 makeisr 11
90 makeisr 12
91 makeisr 13
92 makeisr 14
93
94 ; make isr 15-31
95 %assign i 15
96 %rep 17
97 makeisr i,0 
98 %assign i i+1
99 %endrep
100
101 ; make irqs
102 %assign i 32
103 %assign j 0
104 %rep 16
105 makeirq j,i
106 %assign i i+1
107 %assign j j+1
108 %endrep
109
110 ; make remaining interrupts
111 %assign i 48
112 %rep 208
113 makeisr i,0 
114 %assign i i+1
115 %endrep
116
117 interrupt_handler_prepare:
118
119         push r15
120         push r14
121         push r13
122         push r12
123         push r11
124         push r10
125         push r9
126         push r8
127         push rax
128         push rcx
129         push rdx
130         push rbx
131         push rsp
132         push rbp
133         push rsi
134         push rdi
135
136         ; CR2 for page faults, always push for consistent frame
137         mov rbp, cr2
138         push rbp
139
140         mov eax, [qword global_errno]
141         push rax
142
143         mov rdi, rsp ; context stack frame pointer
144         mov rbx, rsp ; rbx is preserved by the abi, so save it
145         and rsp, 0xFFFFFFF0 ; should be sign extended
146         call interrupt_handler
147         mov rsp, rbx ; restore the stack
148
149 load_interrupted_registers:
150         ; Restore the previous kernel errno.
151         pop rax
152         mov [qword global_errno], eax
153
154         add rsp, 8 ; remove CR2
155
156         pop rdi
157         pop rsi
158         pop rbp
159         add rsp, 8 ; skip rsp
160         pop rbx
161         pop rdx
162         pop rcx
163         pop rax
164         pop r8
165         pop r9
166         pop r10
167         pop r11
168         pop r12
169         pop r13
170         pop r14
171         pop r15
172
173         ; Remove int_no and err_code
174         add rsp, 16
175
176         iretq
177 ;.size interrupt_handler_prepare, . - interrupt_handler_prepare
178
179 global interrupt_handler_null:function
180
181 align 16
182 interrupt_handler_null:
183         iretq
184 ;.size interrupt_handler_null, . - interrupt_handler_null
185
186 rfprintf:  db 'rflags : %llx', 10, 0
187 csprintf:  db 'cs     : %hx', 10, 0
188 ripprintf: db 'rip    : %llx', 10, 0
189 rspprintf: db 'rsp    : %llx', 10, 0
190 ssprintf:  db 'ss     : %hx', 10, 0
191 csvprintf: db 'gdt    : %016llx', 10, 0
192
193 global interrupts_enabled:function
194 interrupts_enabled:
195         pushf
196         pop rax
197         shr rax, 9
198         and rax, 0x1
199         ret
200
201 global enable_interrupts:function
202 enable_interrupts:
203         sti
204         ret
205
206 global disable_interrupts:function
207 disable_interrupts:
208         cli
209         ret
210