From 70a2051749d2c52c3c36ea045f849ded9bafb612 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Wed, 1 Feb 2023 13:48:01 +0000 Subject: [PATCH] uthread ok --- user/uthread.c | 22 ++++++++++++++++++++++ user/uthread_switch.S | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/user/uthread.c b/user/uthread.c index 06349f5..770df0d 100644 --- a/user/uthread.c +++ b/user/uthread.c @@ -10,9 +10,28 @@ #define STACK_SIZE 8192 #define MAX_THREAD 4 +typedef struct ucontext { + uint64 ra; + uint64 sp; + + // callee-saved + uint64 s0; + uint64 s1; + uint64 s2; + uint64 s3; + uint64 s4; + uint64 s5; + uint64 s6; + uint64 s7; + uint64 s8; + uint64 s9; + uint64 s10; + uint64 s11; +} ucontext; struct thread { char stack[STACK_SIZE]; /* the thread's stack */ + ucontext ctx; int state; /* FREE, RUNNING, RUNNABLE */ }; struct thread all_thread[MAX_THREAD]; @@ -62,6 +81,7 @@ thread_schedule(void) * Invoke thread_switch to switch from t to next_thread: * thread_switch(??, ??); */ + thread_switch((uint64)&t->ctx, (uint64)&next_thread->ctx); } else next_thread = 0; } @@ -76,6 +96,8 @@ thread_create(void (*func)()) } t->state = RUNNABLE; // YOUR CODE HERE + t->ctx.ra = (uint64)func; + t->ctx.sp = (uint64)t->stack + STACK_SIZE; } void diff --git a/user/uthread_switch.S b/user/uthread_switch.S index 5defb12..102d3fb 100644 --- a/user/uthread_switch.S +++ b/user/uthread_switch.S @@ -8,4 +8,33 @@ .globl thread_switch thread_switch: /* YOUR CODE HERE */ + sd ra, 0(a0) + sd sp, 8(a0) + sd s0, 16(a0) + sd s1, 24(a0) + sd s2, 32(a0) + sd s3, 40(a0) + sd s4, 48(a0) + sd s5, 56(a0) + sd s6, 64(a0) + sd s7, 72(a0) + sd s8, 80(a0) + sd s9, 88(a0) + sd s10, 96(a0) + sd s11, 104(a0) + + ld ra, 0(a1) + ld sp, 8(a1) + ld s0, 16(a1) + ld s1, 24(a1) + ld s2, 32(a1) + ld s3, 40(a1) + ld s4, 48(a1) + ld s5, 56(a1) + ld s6, 64(a1) + ld s7, 72(a1) + ld s8, 80(a1) + ld s9, 88(a1) + ld s10, 96(a1) + ld s11, 104(a1) ret /* return to ra */