Files
riscv-operating-system-mooc/code/os/03-contextswitch/sched.c
Wang Chen a94fb0cd58 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 <wangchen20@iscas.ac.cn>
2023-06-17 22:12:59 +08:00

53 lines
881 B
C

#include "os.h"
/* defined in entry.S */
extern void switch_to(struct context *next);
#define STACK_SIZE 1024
/*
* 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)
{
asm volatile("csrw mscratch, %0" : : "r" (x));
}
void user_task0(void);
void sched_init()
{
w_mscratch(0);
ctx_task.sp = (reg_t) &task_stack[STACK_SIZE];
ctx_task.ra = (reg_t) user_task0;
}
void schedule()
{
struct context *next = &ctx_task;
switch_to(next);
}
/*
* a very rough implementaion, just to consume the cpu
*/
void task_delay(volatile int count)
{
count *= 50000;
while (count--);
}
void user_task0(void)
{
uart_puts("Task 0: Created!\n");
while (1) {
uart_puts("Task 0: Running...\n");
task_delay(1000);
}
}