summaryrefslogtreecommitdiff
path: root/user/uthread.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2024-02-16 11:29:36 +0800
committerMole Shang <[email protected]>2024-02-16 11:31:23 +0800
commitedd523ffcb39c1c57944796fabfc71c70a10ce2e (patch)
treea37eaf56bbee9509f32a775793a61738b3d4bbac /user/uthread.c
parenta98c56a811142e5ede3332a7a444cca45f628769 (diff)
downloadxv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.gz
xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.bz2
xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.zip
lab thread: finish
Diffstat (limited to 'user/uthread.c')
-rw-r--r--user/uthread.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/user/uthread.c b/user/uthread.c
index 18b773d..7641d77 100644
--- a/user/uthread.c
+++ b/user/uthread.c
@@ -11,9 +11,30 @@
#define MAX_THREAD 4
+// Saved registers for kernel context switches.
+struct context {
+ 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;
+};
+
struct thread {
char stack[STACK_SIZE]; /* the thread's stack */
int state; /* FREE, RUNNING, RUNNABLE */
+ struct context context; /* saved registers to switch thru threads */
};
struct thread all_thread[MAX_THREAD];
struct thread *current_thread;
@@ -56,10 +77,8 @@ thread_schedule(void)
next_thread->state = RUNNING;
t = current_thread;
current_thread = next_thread;
- /* YOUR CODE HERE
- * Invoke thread_switch to switch from t to next_thread:
- * thread_switch(??, ??);
- */
+ // Invoke thread_switch to switch from t to next_thread:
+ thread_switch((uint64)&t->context, (uint64)&next_thread->context);
} else
next_thread = 0;
}
@@ -73,7 +92,11 @@ thread_create(void (*func)())
if (t->state == FREE) break;
}
t->state = RUNNABLE;
- // YOUR CODE HERE
+ // Set up new context to start executing at func
+ memset(&t->context, 0, sizeof(struct context));
+ t->context.ra = (uint64)func;
+ // stack grows downward, set sp at the highest stack address in out thread
+ t->context.sp = (uint64)t->stack + STACK_SIZE;
}
void