-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterrupt_handler.s
293 lines (253 loc) · 7.35 KB
/
interrupt_handler.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
# Tim Henderson & Steve Johnson
# interrupt handler
# This is where the magic happens
.data
__save_gp: .word 0
__save_sp: .word 0
__save_fp: .word 0
__save_ra: .word 0
__save_t0: .word 0
__save_t1: .word 0
__save_t2: .word 0
__save_t3: .word 0
__save_t4: .word 0
__save_t5: .word 0
__save_t6: .word 0
__save_t7: .word 0
__save_t8: .word 0
__save_t9: .word 0
__save_s0: .word 0
__save_s1: .word 0
__save_s2: .word 0
__save_s3: .word 0
__save_s4: .word 0
__save_s5: .word 0
__save_s6: .word 0
__save_s7: .word 0
__save_HCB_ADDR: .word 0
__k_HCB_ADDR: .word 0
.text
save_state:
{
sw $gp __save_gp # save the pointer registers
sw $sp __save_sp
sw $fp __save_fp
sw $ra __save_ra
sw $t0 __save_t0 # save $t0 - $t3
sw $t1 __save_t1
sw $t2 __save_t2
sw $t3 __save_t3
sw $t4 __save_t4
sw $t5 __save_t5
sw $t6 __save_t6
sw $t7 __save_t7
sw $t8 __save_t8
sw $t9 __save_t9
sw $s0 __save_s0 # save $s0 - $s3
sw $s1 __save_s1
sw $s2 __save_s2
sw $s3 __save_s3
sw $s4 __save_s4 # save $s0 - $s3
sw $s5 __save_s5
sw $s6 __save_s6
sw $s7 __save_s7
j save_state_return
}
.text
restore_state:
{
lw $t0 __save_t0 # load $t0 - $t3
lw $t1 __save_t1
lw $t2 __save_t2
lw $t3 __save_t3
lw $t4 __save_t4
lw $t5 __save_t5
lw $t6 __save_t6
lw $t7 __save_t7
lw $t8 __save_t8
lw $t9 __save_t9
lw $s0 __save_s0 # load $s0 - $s3
lw $s1 __save_s1
lw $s2 __save_s2
lw $s3 __save_s3
lw $s4 __save_s4
lw $s5 __save_s5
lw $s6 __save_s6
lw $s7 __save_s7
lw $gp __save_gp # load the pointer registers
lw $sp __save_sp
lw $fp __save_fp
lw $ra __save_ra
j restore_state_return
}
.data
__int_msg: .asciiz "interrupt handler entered\n"
.text
interrupt_handler:
j save_state
save_state_return:
{
la $a0 current_pcb
lw $a0 0($a0)
li $a1 0
call save_proc
######### SAVE THE STACK #########
{
@hcb_addr = $s1
@pcb_id = $s2
@sp = $s3
@error = $s4
@stack_id = $s5
@stackheap = $s6
@curpcb_addr = $t0
khcb_getaddr @hcb_addr
la @curpcb_addr current_pcb
lw @pcb_id 0(@curpcb_addr)
geti 7 @pcb_id @hcb_addr @sp @error
addu $a0 @sp $zero
call save_stack
addu @stack_id $v0 $zero
puti 4 @pcb_id @hcb_addr @stack_id @error
}
######### SAVE THE STACK #########
######### PERFORM MAGIC #########
#Check kernel message sent to interrupt handler
la $a0 KMSG
lw $a0 0($a0)
beqz $a0 km_clock_interrupt
li $a1 1
beq $a0 $a1 km_wait
li $a1 2
beq $a0 $a1 km_exit
b reset_kmsg #default: do nothing
km_wait:
{
@khcb_addr = $s0
@h = $s1
@err = $s2
@pid = $s3
@pcb = $s4
@mem_id = $s5
khcb_getaddr @khcb_addr
#Load the head of the process linked list
geti 1 $zero @khcb_addr @h @err
bnez @err ch_err
#Load the currently active process's PID
la $a0 current_pid
lw @pid 0($a0)
#Linear search for the current node
#Note: this is stupid and inefficient. We should just be storing the node mem_id.
#However, we already gave the demo and wrote the report, and I'm busy.
addu $a0 @h $zero
addu $a1 @pid $zero #this is the old pid
call ll_find_pid
addu @mem_id $v0 $zero
#Get the next node in the list (i.e. next process)
addu $a0 @h $zero
addu $a1 @mem_id $zero #where mem_id is a list node
call ll_next
addu @mem_id $v0 $zero
#Load the new PID and PCB ID
geti 1 @mem_id @khcb_addr @pid @err
bnez @err ch_err
geti 2 @mem_id @khcb_addr @pcb @err
bnez @err ch_err
#Save the current PID and current PCB back to kernel data
la $a0 current_pid
sw @pid 0($a0)
la $a0 current_pcb
sw @pcb 0($a0)
b noerr
ch_err:
println errmsg
kill_jist
.data
errmsg: .asciiz "error in cmgr_wait"
.text
noerr:
}
b reset_kmsg
km_exit:
{
#This does pretty much the same thing as km_wait with a few notable differences
@khcb_addr = $s0
@h = $s1
@err = $s2
@pid = $s3
@pcb = $s4
@mem_id = $s5
khcb_getaddr @khcb_addr
geti 1 $zero @khcb_addr @h @err
bnez @err ex_err
la $a0 current_pid
lw @pid 0($a0)
addu $a0 @h $zero
addu $a1 @pid $zero #this is the old pid
call ll_find_pid
addu @mem_id $v0 $zero
#Remove the current process!
addu $a0 @h $zero
addu $a1 @mem_id $zero #where mem_id is the list node to remove
call ll_remove
addu @h $v0 $zero
#Write the (possibly) new address of the list head back to kernel data
khcb_getaddr @khcb_addr
puti 1 $zero @khcb_addr @h @err
#Go back to list head
addu @mem_id @h $zero
geti 1 @mem_id @khcb_addr @pid @err
bnez @err ex_err
geti 2 @mem_id @khcb_addr @pcb @err
bnez @err ex_err
la $a0 current_pid
sw @pid 0($a0)
la $a0 current_pcb
sw @pcb 0($a0)
b noerr
ex_err:
println errmsg
li $v0 10
syscall
.data
errmsg: .asciiz "error in cmgr_exit"
.text
noerr:
}
b reset_kmsg
km_clock_interrupt:
#do nothing
b reset_kmsg
reset_kmsg:
la $a0 KMSG
sw $zero 0($a0)
la $a0 current_pcb
lw $a0 0($a0)
call restore_proc
######### RESTORE THE STACK ##########
{
@hcb_addr = $s1
@pcb_id = $s2
@error = $s3
@curpcb_addr = $s4
@stack_id = $s5
@sp = $s6
khcb_getaddr @hcb_addr
la @curpcb_addr current_pcb
lw @pcb_id 0(@curpcb_addr)
geti 7 @pcb_id @hcb_addr @sp @error
geti 4 @pcb_id @hcb_addr @stack_id @error
addu $a0 @stack_id $0
addu $a1 @sp $0
call restore_stack
}
######### RESTORE THE STACK ##########
}
j restore_state
restore_state_return:
la $a0 interrupt_return
jr $a0
.data
rstr: .asciiz "Loading stack_id "
sstr: .asciiz "Saving stack_id "
memid_msg: .asciiz "mem_id: "
.text