Professional Documents
Culture Documents
COMP200
Outline
Introduction to Multitasking
Pre-emption and Scheduling
Kernel structure
Tasks
WASM directives
The Handler
The Dispatcher
Setup Code
COMP200
Introduction to Multitasking
Multitasking Diagram
Task 1
COMP200
Task 3
Multitasking Kernel
COMP200
Cooperative Multitasking
Preemptive Multitasking
Tasks are given a limit on how long they can use the CPU for
at a time.
All tasks will get a go on the CPU.
COMP200
Preemptive Multitasking
COMP200
Scheduling
COMP200
Saving/Restoring Context
COMP200
Setup code.
Handler.
Dispatcher.
Tasks.
10
Setup code.
Handler.
Dispatcher.
Tasks.
COMP200
11
Kernel structure
main:
# Setup tasks.
# Setup interrupts.
# Go to the first task.
handler:
# Check which exception occurred.
# If it is the timer interrupt then we
# jump to handle_interrupt.
# Otherwise we jump to the system.
# exception handler.
handle_interrupt:
# Acknowledge the interrupt.
# Subtract 1 from the timeslice counter
# If timeslice counter is zero then goto dispatcher
# Otherwise we return from exception.
dispatcher:
# Save context for current task
# Select the next task to run
# Set the timeslice counter to an appropriate value
# Load context for next task
# Continue with next task
COMP200
12
COMP200
13
Setup code
Handler
Dispatcher
Tasks
COMP200
14
Tasks
Code
Data
Registers
Processor status
Program counter
Control registers
Stack
Stack pointer
COMP200
15
Task stacks
COMP200
16
.equ directives
Allow us to define constants
Much like #define in C.
If we want to change the value then we only have to
change it in one place.
e.g.
.equ
sw
left_ssd_addr, 0x73002
$4, left_ssd_addr($0)
COMP200
17
text segment
data segment
Instructions
Data with a specified initial value
bss segment
Uninitialised data
Any .word directives in the bss segment cannot have a
value specified.
COMP200
18
.space
Allows us to allocate a certain amount of space in
memory for a data structure.
This saves us having to use .word repeatedly.
Because no initial values are specified this can only
be used inside the bss segment.
e.g. reserve 18 words of space
task1_pcb:
.space
18
COMP200
19
COMP200
20
The Handler
Interrupt Handler
Use timer to generate regular (fast) interrupts
COMP200
21
Handler Code
handler:
# Get the exception status register
movsg
$13, $estat
# Check for any other exceptions than
# the one we want (IRQ2)
andi
$13, $13, 0xffb0
# If no other exceptions have occurred then
# we branch to our handler.
beqz
$13, handle_interrupt
# Otherwise we must jump to the old
# exception handler.
lw
$13, old_vector($0)
jr
$13
handle_interrupt:
# Handle our timer interrupt
COMP200
22
Register $13
23
COMP200
24
COMP200
25
The Dispatcher
COMP200
26
Saving Context
27
COMP200
28
task1_pcb:
.space
COMP200
17
29
Managing Tasks
COMP200
30
PCB version 2
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
$10
$11
$12
$13
$sp
$ra
$ear
$cctrl
.equ
.equ
.equ
.equ
.equ
.equ
.equ
.bss
task1_pcb:
.space
task2_pcb:
.space
task3_pcb:
.space
COMP200
pcb_link,
pcb_reg1,
pcb_reg2,
pcb_reg3,
0
1
2
3
pcb_ra, 15
pcb_ear, 16
pcb_cctrl, 17
18
18
18
31
Task 2 PCB
Task 3 PCB
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
$10
$11
$12
$13
$sp
$ra
$ear
$cctrl
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
$10
$11
$12
$13
$sp
$ra
$ear
$cctrl
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
$10
$11
$12
$13
$sp
$ra
$ear
$cctrl
COMP200
32
COMP200
33
31
$cctrl
KU
OKU
IE
OIE
IRQ2
0 0 0 0 0 1 0 0 1 1 0 1
0x0000004d
COMP200
34
COMP200
We do this
for each task
35
.bss
current_task:
.word
COMP200
36
# Save $ear
movsg $1, $ear
sw
$1, pcb_ear($13)
# Save $cctrl
movsg $1, $cctrl
sw
$1, pcb_cctrl($13)
COMP200
37
current_task
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
link
$1
$2
$3
$4
$5
$6
$7
$8
$9
COMP200
38
COMP200
39
Summary
40