summaryrefslogtreecommitdiff
path: root/stage3/thread.c
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-22 12:29:45 +0100
committerLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-22 12:30:23 +0100
commit619657614489d5953583749617e6fd5a0804ce23 (patch)
treeb5eb413e7dbd196f705e752bcd474daa8e772936 /stage3/thread.c
parent2834bff2cf6131202a788b59a5fd81994e1ed5aa (diff)
downloadcuddles-619657614489d5953583749617e6fd5a0804ce23.tar.xz
SysV ABI compliant stack alignment
and fix outright memory corruption by the scheduler stack
Diffstat (limited to 'stage3/thread.c')
-rw-r--r--stage3/thread.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/stage3/thread.c b/stage3/thread.c
index f78b80a..aeec3bd 100644
--- a/stage3/thread.c
+++ b/stage3/thread.c
@@ -18,14 +18,23 @@ void thread_resume(void *ret, thread *t)
resume(t->stack, ret);
}
-#define STACK_SIZE 0xffff
+#define STACK_SIZE 0x10000
+
+// aligned on 16-byte boundary
+static void *alloc_stack(void **alloc)
+{
+ usize stack = (usize) malloc(STACK_SIZE+16);
+ if (alloc != nil)
+ *alloc = (void *) stack;
+ stack += 16 - stack % 16;
+ return (void *) stack;
+}
thread *thread_create(str name, void *init)
{
thread *t = malloc(sizeof *t);
t->name = name;
- t->stack_bottom = malloc(STACK_SIZE);
- t->stack = t->stack_bottom + STACK_SIZE - 8;
+ t->stack = alloc_stack(&t->stack_bottom) + STACK_SIZE - 16;
*(void **) t->stack = init;
t->stack -= 8*8;
return t;
@@ -75,7 +84,7 @@ void thread_sched(yield_arg *arg, void *stack)
void thread_init()
{
- thread_sched_stack = malloc(STACK_SIZE);
+ thread_sched_stack = alloc_stack(nil)+STACK_SIZE-8;
queue_read = (event_queue) { 0, 1024, malloc(1024 * sizeof(event)) };
queue_write = (event_queue) { 0, 1024, malloc(1024 * sizeof(event)) };