Modify context and trapframe init

This commit is contained in:
songyanguang 2025-01-13 19:58:37 +08:00
parent ee49e0d71c
commit 1631e60baa
4 changed files with 34 additions and 8 deletions

View File

@ -72,10 +72,11 @@ Modification:
#include <string.h>
#include "cortex.h"
#include "asm/csr.h"
#define NR_CPU 1 // maximum number of CPUs
#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
__attribute__((always_inline)) static inline uint64_t EL0_mode() // Set ARM mode to EL0
{
@ -113,10 +114,14 @@ struct context {
/// @brief init task context, set return address to trap return
/// @param ctx
extern void task_prepare_enter(void);
__attribute__((__always_inline__)) static inline void arch_init_context(struct context* ctx, unsigned long sp)
__attribute__((__always_inline__)) static inline void arch_init_context(struct context* ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->ra = (uintptr_t)(task_prepare_enter);
}
__attribute__((__always_inline__)) static inline void arch_context_set_sp(struct context* ctx, unsigned long sp)
{
ctx->sp = sp;
}
@ -171,7 +176,7 @@ __attribute__((__always_inline__)) static inline void arch_init_trapframe(struct
memset(tf, 0, sizeof(*tf));
tf->sp = sp;
tf->epc = pc;
tf->status &= ~SSTATUS_SPP; // clear SPP to 0 for user mode
tf->status = SR_PIE;
}
/// @brief set pc and sp to trapframe
@ -218,6 +223,16 @@ __attribute__((__always_inline__)) static inline void arch_set_return(struct tra
tf->a0 = (uint64_t)ret;
}
// TODO: refer to jh7110 Linux
struct thread_info {
unsigned long flags; /* low level flags */
int preempt_count; /* 0=>preemptible, <0=>BUG */
long kernel_sp; /* Kernel stack pointer */
long user_sp; /* User stack pointer */
int cpu;
};
void cpu_start_secondary(uint8_t cpu_id);
void start_smp_cache_broadcast(int cpu_id);
#endif

View File

@ -44,6 +44,7 @@ clear_bss:
blt a3, a4, clear_bss
clear_bss_done:
li a0, 1
la a2, boot_cpu_hartid
sd a0, (a2)
@ -58,10 +59,14 @@ clear_bss_done:
call _debug_uart_init
/* Restore C environment */
la tp, init_thread_info
sw zero, TASK_TI_CPU(tp)
la sp, init_thread_union + THREAD_SIZE
/* Start the kernel */
tail bootmain
relocate_enable_mmu:
/* Relocate return address */
la a1, kernel_map

View File

@ -53,6 +53,10 @@ int sys_new_thread(struct MemSpace* pmemspace, struct Thread* task, uintptr_t en
arch_trapframe_set_sp_pc(task->thread_context.trapframe, loaded_sp.user_sp, (uintptr_t)entry);
arch_set_main_params(task->thread_context.trapframe, loaded_sp.argc, loaded_sp.user_sp);
#ifdef __riscv
arch_context_set_sp(task->thread_context.context, (uintptr_t)task->thread_context.trapframe);
#endif
// init thread name
char* last = NULL;
for (last = name; *name; name++) {

View File

@ -207,6 +207,7 @@ static void _dealloc_task_cb(struct Thread* task)
slab_free(&xizi_task_manager.task_allocator, (void*)task);
}
#ifndef __riscv
/* alloc a new task with init */
extern void trap_return(void);
__attribute__((optimize("O0"))) void task_prepare_enter()
@ -215,6 +216,7 @@ __attribute__((optimize("O0"))) void task_prepare_enter()
xizi_leave_kernel();
trap_return();
}
#endif
static struct Thread* _new_task_cb(struct MemSpace* pmemspace)
{
@ -251,7 +253,11 @@ static struct Thread* _new_task_cb(struct MemSpace* pmemspace)
/* set context of main thread stack */
/// stack bottom
memset((void*)task->thread_context.kern_stack_addr, 0x00, USER_STACK_SIZE);
#ifndef __riscv
char* sp = (char*)task->thread_context.kern_stack_addr + USER_STACK_SIZE - 4;
#else
char* sp = (char*)task->thread_context.kern_stack_addr + USER_STACK_SIZE;
#endif
/// 1. trap frame into stack, for process to nomally return by trap_return
sp -= sizeof(*task->thread_context.trapframe);
@ -260,11 +266,7 @@ static struct Thread* _new_task_cb(struct MemSpace* pmemspace)
/// 2. context into stack
sp -= sizeof(*task->thread_context.context);
task->thread_context.context = (struct context*)sp;
#ifndef __riscv
arch_init_context(task->thread_context.context);
#else
arch_init_context(task->thread_context.context, task->thread_context.kern_stack_addr);
#endif
return task;
}