From a94fb0cd58092e938fb1f3d2510d17831031284c Mon Sep 17 00:00:00 2001 From: Wang Chen Date: Sat, 17 Jun 2023 21:46:13 +0800 Subject: [PATCH] stack pointer aligment Following the standard RISC-V calling convention, make sure the stack pointer sp is always 16-byte aligned. Fixed an issue, make sure the sp of task point to the bottom of the stack, while originally we waste one byte (forgive my stupid ~~~). Signed-off-by: Wang Chen --- code/os/00-bootstrap/start.S | 3 +++ code/os/01-helloRVOS/start.S | 3 +++ code/os/02-memanagement/start.S | 3 +++ code/os/03-contextswitch/sched.c | 8 ++++++-- code/os/03-contextswitch/start.S | 3 +++ code/os/04-multitask/sched.c | 8 ++++++-- code/os/04-multitask/start.S | 3 +++ code/os/05-traps/sched.c | 8 ++++++-- code/os/05-traps/start.S | 3 +++ code/os/06-interrupts/sched.c | 8 ++++++-- code/os/06-interrupts/start.S | 3 +++ code/os/07-hwtimer/sched.c | 8 ++++++-- code/os/07-hwtimer/start.S | 3 +++ code/os/08-preemptive/sched.c | 8 ++++++-- code/os/08-preemptive/start.S | 3 +++ code/os/09-lock/sched.c | 8 ++++++-- code/os/09-lock/start.S | 3 +++ code/os/10-swtimer/sched.c | 8 ++++++-- code/os/10-swtimer/start.S | 3 +++ code/os/11-syscall/sched.c | 8 ++++++-- code/os/11-syscall/start.S | 3 +++ 21 files changed, 90 insertions(+), 18 deletions(-) diff --git a/code/os/00-bootstrap/start.S b/code/os/00-bootstrap/start.S index 5c4e011..ae25bb9 100644 --- a/code/os/00-bootstrap/start.S +++ b/code/os/00-bootstrap/start.S @@ -26,6 +26,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/01-helloRVOS/start.S b/code/os/01-helloRVOS/start.S index 5c4e011..ae25bb9 100644 --- a/code/os/01-helloRVOS/start.S +++ b/code/os/01-helloRVOS/start.S @@ -26,6 +26,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/02-memanagement/start.S b/code/os/02-memanagement/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/02-memanagement/start.S +++ b/code/os/02-memanagement/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/03-contextswitch/sched.c b/code/os/03-contextswitch/sched.c index b2933d0..e1ef938 100644 --- a/code/os/03-contextswitch/sched.c +++ b/code/os/03-contextswitch/sched.c @@ -4,7 +4,11 @@ extern void switch_to(struct context *next); #define STACK_SIZE 1024 -uint8_t task_stack[STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[STACK_SIZE]; struct context ctx_task; static void w_mscratch(reg_t x) @@ -17,7 +21,7 @@ void sched_init() { w_mscratch(0); - ctx_task.sp = (reg_t) &task_stack[STACK_SIZE - 1]; + ctx_task.sp = (reg_t) &task_stack[STACK_SIZE]; ctx_task.ra = (reg_t) user_task0; } diff --git a/code/os/03-contextswitch/start.S b/code/os/03-contextswitch/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/03-contextswitch/start.S +++ b/code/os/03-contextswitch/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/04-multitask/sched.c b/code/os/04-multitask/sched.c index 1bb3bfd..bac0e02 100644 --- a/code/os/04-multitask/sched.c +++ b/code/os/04-multitask/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -51,7 +55,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].ra = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/04-multitask/start.S b/code/os/04-multitask/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/04-multitask/start.S +++ b/code/os/04-multitask/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/05-traps/sched.c b/code/os/05-traps/sched.c index 7e53dd4..fa8359b 100644 --- a/code/os/05-traps/sched.c +++ b/code/os/05-traps/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -46,7 +50,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].ra = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/05-traps/start.S b/code/os/05-traps/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/05-traps/start.S +++ b/code/os/05-traps/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/06-interrupts/sched.c b/code/os/06-interrupts/sched.c index 7e53dd4..fa8359b 100644 --- a/code/os/06-interrupts/sched.c +++ b/code/os/06-interrupts/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -46,7 +50,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].ra = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/06-interrupts/start.S b/code/os/06-interrupts/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/06-interrupts/start.S +++ b/code/os/06-interrupts/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/07-hwtimer/sched.c b/code/os/07-hwtimer/sched.c index 7e53dd4..fa8359b 100644 --- a/code/os/07-hwtimer/sched.c +++ b/code/os/07-hwtimer/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -46,7 +50,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].ra = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/07-hwtimer/start.S b/code/os/07-hwtimer/start.S index c7ad5e4..5bdeff1 100644 --- a/code/os/07-hwtimer/start.S +++ b/code/os/07-hwtimer/start.S @@ -36,6 +36,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/08-preemptive/sched.c b/code/os/08-preemptive/sched.c index 26ac265..7567eda 100644 --- a/code/os/08-preemptive/sched.c +++ b/code/os/08-preemptive/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -49,7 +53,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].pc = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/08-preemptive/start.S b/code/os/08-preemptive/start.S index 0b730a9..13cc266 100644 --- a/code/os/08-preemptive/start.S +++ b/code/os/08-preemptive/start.S @@ -46,6 +46,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/09-lock/sched.c b/code/os/09-lock/sched.c index 26ac265..7567eda 100644 --- a/code/os/09-lock/sched.c +++ b/code/os/09-lock/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -49,7 +53,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].pc = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/09-lock/start.S b/code/os/09-lock/start.S index 0b730a9..13cc266 100644 --- a/code/os/09-lock/start.S +++ b/code/os/09-lock/start.S @@ -46,6 +46,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/10-swtimer/sched.c b/code/os/10-swtimer/sched.c index 26ac265..7567eda 100644 --- a/code/os/10-swtimer/sched.c +++ b/code/os/10-swtimer/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -49,7 +53,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].pc = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/10-swtimer/start.S b/code/os/10-swtimer/start.S index 0b730a9..13cc266 100644 --- a/code/os/10-swtimer/start.S +++ b/code/os/10-swtimer/start.S @@ -46,6 +46,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks diff --git a/code/os/11-syscall/sched.c b/code/os/11-syscall/sched.c index 26ac265..7567eda 100644 --- a/code/os/11-syscall/sched.c +++ b/code/os/11-syscall/sched.c @@ -5,7 +5,11 @@ extern void switch_to(struct context *next); #define MAX_TASKS 10 #define STACK_SIZE 1024 -uint8_t task_stack[MAX_TASKS][STACK_SIZE]; +/* + * In the standard RISC-V calling convention, the stack pointer sp + * is always 16-byte aligned. + */ +uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE]; struct context ctx_tasks[MAX_TASKS]; /* @@ -49,7 +53,7 @@ void schedule() int task_create(void (*start_routin)(void)) { if (_top < MAX_TASKS) { - ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE - 1]; + ctx_tasks[_top].sp = (reg_t) &task_stack[_top][STACK_SIZE]; ctx_tasks[_top].pc = (reg_t) start_routin; _top++; return 0; diff --git a/code/os/11-syscall/start.S b/code/os/11-syscall/start.S index ba34f12..75d7808 100644 --- a/code/os/11-syscall/start.S +++ b/code/os/11-syscall/start.S @@ -70,6 +70,9 @@ park: wfi j park + # In the standard RISC-V calling convention, the stack pointer sp + # is always 16-byte aligned. +.align 16 stacks: .skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks