commit 7223002cb9a28c1cc45943ea980d2367b840c544
parent c49d611c72bb8695818395f8fde47c1dbd5dbaa7
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 19 Mar 2024 10:02:54 +0900
try to implement context switch
Diffstat:
2 files changed, 34 insertions(+), 13 deletions(-)
diff --git a/ex3/main.c b/ex3/main.c
@@ -39,11 +39,11 @@ typedef struct proc_tab {
} proc_tab;
proc_tab ptab;
+proc p0, p1;
int
main(void) {
init();
- proc p0, p1;
memcpy((void *)0x20000000, &proc0_start, (long) &proc0_size);
memcpy((void *)0x20000100, &proc1_start, (long) &proc1_size);
@@ -53,18 +53,18 @@ main(void) {
p0.id = 0;
p0.r[13] = 0x20000100; // sp
- p0.r[14] = (unsigned int) main; // lr
+ p0.r[14] = (unsigned int) halt; // lr
p0.r[15] = 0x20000001; // pc
+ p0.r[16] = 0;
p0.next = &p1;
p1.id = 1;
p1.r[13] = 0x20000200; // sp
- p1.r[14] = (unsigned int) main; // lr
+ p1.r[14] = (unsigned int) halt; // lr
p1.r[15] = 0x20000101; // pc
+ p1.r[16] = 0;
p1.next = NULL;
- proc0();
-
return 0;
}
@@ -76,7 +76,7 @@ scheduler(void) {
// Switch_context saves registers r to the currently executing proc struct
// and then move it to the tail of task queue.
// It returns the next PC of the next task in the queue
-unsigned int
+unsigned int *
switch_context(unsigned int r[17]) {
proc *p;
@@ -91,9 +91,8 @@ switch_context(unsigned int r[17]) {
ptab.tail->next = p;
ptab.tail = p;
ptab.tail->next = NULL;
- restore_context(ptab.head->r);
- return ptab.head->r[15];
+ return ptab.head->r;
}
void
diff --git a/ex3/proc.s b/ex3/proc.s
@@ -108,15 +108,37 @@ isr_alarm:
str r0, [r1, r3]
mov r0, r1
- bl switch_context
- sub r2, r2, #4
- str r0, [r1, r2]
-
+ bl switch_context // r0 is the r[17] of the next task.
add sp, sp, #(17*4)
+ add r2, sp, #4 // for the popped lr
+
+ ldr r1, [r0, #(0 * 4)] // r0
+ str r1, [r2, #0]
+ ldr r1, [r0, #(1 * 4)] // r1
+ str r1, [r2, #0x4]
+ ldr r1, [r0, #(2 * 4)] // r2
+ str r1, [r2, #0x8]
+ ldr r1, [r0, #(3 * 4)] // r3
+ str r1, [r2, #0xc]
+ ldr r1, [r0, #(12 * 4)] // r12
+ str r1, [r2, #0x10]
+ ldr r1, [r0, #(14 * 4)] // lr
+ str r1, [r2, #0x14]
+ ldr r1, [r0, #(15 * 4)] // ReturnAddress
+ str r1, [r2, #0x18]
+ ldr r1, [r0, #(16 * 4)] // xpsr
+ str r1, [r2, #0x1c]
+ mov r4, r0
+
ldr r0, =(1000 * 1000)
bl set_alarm
- pop {pc}
+ pop {r5}
+
+ ldr r0, [r4, #(13 * 4)] // sp
+ mov sp, r0
+
+ bx r5
.align 2
alarm_msg: