-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint-arch.S
420 lines (286 loc) · 6.49 KB
/
int-arch.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SYS86 project
// Architecture specific interrupt handling
#define _ASSEMBLY
#include "task.h"
#include "int.h"
.code16
.text
//------------------------------------------------------------------------------
// void vect_set (byte_t vect, void * hand);
.global vect_set
vect_set:
mov %sp,%bx
mov 4(%bx),%cx
mov 2(%bx),%bx
xor %bh,%bh
shl $2,%bx
mov %ds,%dx
xor %ax,%ax
mov %ax,%ds
cli
mov %cx,0(%bx)
mov %cs,2(%bx)
sti
mov %dx,%ds
ret
//------------------------------------------------------------------------------
// Timer interrupt
.global int_timer0
int_timer0:
#ifdef CONFIG_TRACE
pushw $0x10
call trace_far
add $2,%sp
#endif // CONFIG_TRACE
pushw $VECT_TIMER0
jmp int_hand
// Serial interrupt
.global int_serial
int_serial:
#ifdef CONFIG_TRACE
pushw $0x20
call trace_far
add $2,%sp
#endif // CONFIG_TRACE
pushw $VECT_SERIAL
jmp int_hand
//------------------------------------------------------------------------------
// Asynchronous interrupt (HW IRQ or CPU trap)
int_hand:
// Keep interrupt disabled
// until context fully saved
// and stack switch completed
// IP CS FL already pushed
// Push remaining registers
push %es
push %ds
push %ax
push %cx
push %dx
push %bx
push %bp
push %si
push %di
// Get interrupt number
mov %sp,%bp
mov 9*2(%bp),%cx
// Switch to kernel data segment
mov $KERNEL_DS,%dx
mov %dx,%ds
// Get current task
mov task_now,%si
// Check interrupt level
// =0: single interrupt
// >0: nested interrupt
mov int_level,%ax
or %ax,%ax
jnz 1f
// Save interrupted stack top
// Switch to interrupt stack
mov %sp,task_tsp(%si)
mov %ss,task_tss(%si)
mov int_stack_top,%sp
mov %dx,%ss
// Increment interrupt level
1: inc %ax
mov %ax,int_level
sti
// Call interrupt procedure
// with interrupt number
push %cx
call int_proc
add $2,%sp
// Keep interrupt disabled
// until all stack switches completed
// and context fully restored
cli
// Decrement & check interrupt level
// =0: back to normal
// >0: still interrupted
mov int_level,%ax
dec %ax
mov %ax,int_level
jnz int_ret
// Switch back to interrupted stack
//mov task_now,%si // still the same task as on entry
mov task_tsp(%si),%sp
mov task_tss(%si),%ss
mov $1,%di // interrupted
// Now the interrupt stack is empty
// and context on top of interrupted stack
// so task switching can occur
stack_switch:
// May save previous stack top
mov task_prev,%bx
or %bx,%bx
jz 1f
mov %di,task_int(%bx)
mov %sp,task_tsp(%bx)
mov %ss,task_tss(%bx)
xor %bx,%bx
mov %bx,task_prev
// May switch to next stack top
1: mov task_next,%bx
or %bx,%bx
jz 1f
mov task_tsp(%bx),%sp
mov task_tss(%bx),%ss
mov task_int(%bx),%di
mov %bx,%si
mov %si,task_now
xor %bx,%bx
mov %bx,task_next
// Check task interrupt status
1: or %di,%di
jz switch_ret
movw $0,task_int(%si)
int_ret:
// Restore registers from interrupted stack
pop %di
pop %si
pop %bp
pop %bx
pop %dx
pop %cx
pop %ax
pop %ds
pop %es
add $2,%sp // skip int_num
// Interrupt return for remaining IP CS FL
#ifdef CONFIG_TRACE
pushw $0x30
call trace_far
add $2,%sp
#endif // CONFIG_TRACE
iret
//------------------------------------------------------------------------------
.global task_switch
// Switch task
task_switch:
// Save task interrupt flag
// and registers required by GCC-IA16
pushf
push %es
push %bp
push %si
push %di
xor %di,%di // not interrupted
jmp stack_switch
switch_ret:
pop %di
pop %si
pop %bp
pop %es
popf
#ifdef CONFIG_TRACE
pushw $0x40
call trace_far
add $2,%sp
#endif // CONFIG_TRACE
ret
//------------------------------------------------------------------------------
// Synchronous interrupt (system call)
.global int_system
int_system:
push %ds
push %si
push %di
// Switch to kernel data segment
mov $KERNEL_DS,%di
mov %di,%ds
// Get current task
mov task_now,%si
// Save user stack top
// Switch to kernel stack
mov %sp,task_usp(%si)
mov %ss,task_uss(%si)
mov task_stack(%si),%sp
add task_ssize(%si),%sp
mov %di,%ss
sti
// Call system procedure
// TODO: push caller context
// call sys_proc
// add $xxx,%sp
// Switch back to user stack
cli
mov task_usp(%si),%sp
mov task_uss(%si),%ss
pop %di
pop %si
pop %ds
iret
//------------------------------------------------------------------------------
.global stack_init_far
// Setup far stack with task initial context
// Tiny or small memory model SS=DS=ES
// +4: arg1: task *
// +6: arg2: task IP (user)
// +8: arg3: task CS (user)
// +10: arg4: task SP (user)
// +12: arg5: task SS (user)
stack_init_far:
push %bp
mov %sp,%bp
mov 4(%bp),%bx // arg1: task *
push %di
mov 10(%bp),%di // arg4
mov 12(%bp),%dx // arg5
sub $REGS_SIZE,%di
push %si
mov %di,%si
push %es
mov %dx,%es
cld
xor %ax,%ax
mov $7,%cx // 7 general registers
rep
stosw // DI SI BP BX DX CX AX
mov %dx,%ax
stosw // DS
stosw // ES
xor %ax,%ax
stosw // int num
mov 6(%bp),%ax // arg2
stosw // IP
mov 8(%bp),%ax // arg3
stosw // CS
mov $0x200,%ax // STI
stosw // FL
pop %es
// Save stack top in task
mov %si,task_tsp(%bx)
mov %dx,task_tss(%bx)
pop %si
pop %di
pop %bp
ret
//------------------------------------------------------------------------------
.global stack_init_near
// Setup near stack with task initial context
// Tiny or small memory model SS=DS=ES
// +4: arg1: task *
// +6: arg2: task IP (kernel)
// +8: arg3: task SP (kernel)
stack_init_near:
push %bp
mov %sp,%bp
mov 8(%bp),%bx // arg3
sub $6*2,%bx
xor %ax,%ax
mov %ax,0(%bx) // DI
mov %ax,2(%bx) // SI
mov %ax,4(%bx) // BP
mov %ds,6(%bx) // ES
movw $0x200,8(%bx) // FL STI
mov 6(%bp),%ax // arg2
mov %ax,10(%bx) // IP
// Save stack top in task
mov %bx,%ax
mov 4(%bp),%bx // arg1
mov %ax,task_tsp(%bx)
mov %ds,%ax
mov %ax,task_tss(%bx)
pop %bp
ret
//------------------------------------------------------------------------------