From 19222732a885024eeab4c4bf4f1fccb581546eaa Mon Sep 17 00:00:00 2001 From: xinyangli Date: Fri, 26 Jul 2024 12:18:54 +0800 Subject: [PATCH] bsp,abstract-machien: context creation and switch --- bsp/abstract-machine/include/extra.h | 2 ++ bsp/abstract-machine/rtconfig.py | 2 +- bsp/abstract-machine/src/context.c | 43 +++++++++++++++++++++++++--- bsp/abstract-machine/src/init.c | 22 ++++++++------ 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/bsp/abstract-machine/include/extra.h b/bsp/abstract-machine/include/extra.h index 27c72f4..81a4ec1 100644 --- a/bsp/abstract-machine/include/extra.h +++ b/bsp/abstract-machine/include/extra.h @@ -1,5 +1,7 @@ typedef long suseconds_t; typedef unsigned long useconds_t; +typedef long unsigned int clock_t; +typedef long long int time_t; #include #ifdef __ISA_NATIVE__ diff --git a/bsp/abstract-machine/rtconfig.py b/bsp/abstract-machine/rtconfig.py index db68dba..d13b962 100644 --- a/bsp/abstract-machine/rtconfig.py +++ b/bsp/abstract-machine/rtconfig.py @@ -38,7 +38,7 @@ if PLATFORM == 'gcc': DEVICE = ' -mcmodel=medany -march=rv64imac -mabi=lp64 ' CFLAGS = DEVICE + '-ffreestanding -flax-vector-conversions -Wno-cpp -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -fdiagnostics-color=always' AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__ ' - LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,_start -T link.lds' + ' -lgcc -static' + LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,_start -T extra.ld' + ' -lgcc -static' CPATH = '' LPATH = '' diff --git a/bsp/abstract-machine/src/context.c b/bsp/abstract-machine/src/context.c index ee38829..ebed521 100644 --- a/bsp/abstract-machine/src/context.c +++ b/bsp/abstract-machine/src/context.c @@ -1,9 +1,25 @@ +#include "rtdef.h" #include #include #include static Context* ev_handler(Event e, Context *c) { switch (e.event) { + case EVENT_YIELD: { + // Context Switch + if (c->gpr[11] == 0) { + Context **to = (void *)c->gpr[10]; + *c = **to; + } else { + Context **from = (void *)c->gpr[10]; + Context **to = (void *)c->gpr[11]; + *from = c; + *(Context **)(c->gpr[10]) = c; + *c = **to; + } + break; + } + default: printf("Unhandled event ID = %d\n", e.event); assert(0); } return c; @@ -14,18 +30,37 @@ void __am_cte_init() { } void rt_hw_context_switch_to(rt_ubase_t to) { - assert(0); + asm volatile("li a1, 0"); + yield(); } void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to) { - assert(0); + yield(); } void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread) { assert(0); } -rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit) { +typedef struct { + void (*tentry)(void *); + void *parameter; + void (*texit)(void); +} tenter_parameters; + +__attribute__((noreturn)) void tenter_wrapper(void *p) { + tenter_parameters *pp = p; + pp->tentry(pp->parameter); + pp->texit(); assert(0); - return NULL; +} + +rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit) { + stack_addr = stack_addr - (uintptr_t)stack_addr % 4; + tenter_parameters *p_params = (tenter_parameters*)(stack_addr - sizeof(tenter_parameters)); + p_params->tentry = tentry; + p_params->parameter = parameter; + p_params->texit = texit; + void *sp = kcontext((Area) {(void *)(stack_addr - sizeof(tenter_parameters) - sizeof(Context) - 4), (stack_addr - sizeof(tenter_parameters))}, (void *)tenter_wrapper, (void *)p_params); + return sp; } diff --git a/bsp/abstract-machine/src/init.c b/bsp/abstract-machine/src/init.c index 93e2711..7499c57 100644 --- a/bsp/abstract-machine/src/init.c +++ b/bsp/abstract-machine/src/init.c @@ -1,14 +1,14 @@ #include -#include -#include #include +#include +#include -#define AM_APPS_HEAP_SIZE 0x2000000 +#define AM_APPS_HEAP_SIZE 0x2000000 #define RT_HW_HEAP_BEGIN heap.start #define RT_HW_HEAP_END heap.end Area am_apps_heap = {}, am_apps_data = {}, am_apps_bss = {}; -uint8_t * am_apps_data_content = NULL; +uint8_t *am_apps_data_content = NULL; void rt_hw_board_init() { int rt_hw_uart_init(void); @@ -21,15 +21,18 @@ void rt_hw_board_init() { uint32_t size = AM_APPS_HEAP_SIZE; void *p = NULL; - for (; p == NULL && size != 0; size /= 2) { p = rt_malloc(size); } + for (; p == NULL && size != 0; size /= 2) { + p = rt_malloc(size); + } am_apps_heap = RANGE(p, p + size); extern char __am_apps_data_start, __am_apps_data_end; extern char __am_apps_bss_start, __am_apps_bss_end; am_apps_data = RANGE(&__am_apps_data_start, &__am_apps_data_end); - am_apps_bss = RANGE(&__am_apps_bss_start, &__am_apps_bss_end); - printf("am-apps.data.size = %ld, am-apps.bss.size = %ld\n", - am_apps_data.end - am_apps_data.start, am_apps_bss.end - am_apps_bss.start); + am_apps_bss = RANGE(&__am_apps_bss_start, &__am_apps_bss_end); + // printf("am-apps.data.size = %ld, am-apps.bss.size = %ld\n", + // am_apps_data.end - am_apps_data.start, am_apps_bss.end - + // am_apps_bss.start); uint32_t data_size = am_apps_data.end - am_apps_data.start; if (data_size != 0) { @@ -48,7 +51,8 @@ void rt_hw_board_init() { #endif #ifdef RT_USING_HEAP - rt_kprintf("heap: [0x%08x - 0x%08x]\n", (rt_ubase_t) RT_HW_HEAP_BEGIN, (rt_ubase_t) RT_HW_HEAP_END); + rt_kprintf("heap: [0x%08x - 0x%08x]\n", (rt_ubase_t)RT_HW_HEAP_BEGIN, + (rt_ubase_t)RT_HW_HEAP_END); #endif }