bsp,abstract-machien: context creation and switch

This commit is contained in:
xinyangli 2024-07-26 12:18:54 +08:00
parent 6aebc44707
commit 19222732a8
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
4 changed files with 55 additions and 14 deletions

View file

@ -1,5 +1,7 @@
typedef long suseconds_t; typedef long suseconds_t;
typedef unsigned long useconds_t; typedef unsigned long useconds_t;
typedef long unsigned int clock_t;
typedef long long int time_t;
#include <sys/types.h> #include <sys/types.h>
#ifdef __ISA_NATIVE__ #ifdef __ISA_NATIVE__

View file

@ -38,7 +38,7 @@ if PLATFORM == 'gcc':
DEVICE = ' -mcmodel=medany -march=rv64imac -mabi=lp64 ' 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' 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__ ' 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 = '' CPATH = ''
LPATH = '' LPATH = ''

View file

@ -1,9 +1,25 @@
#include "rtdef.h"
#include <am.h> #include <am.h>
#include <klib.h> #include <klib.h>
#include <rtthread.h> #include <rtthread.h>
static Context* ev_handler(Event e, Context *c) { static Context* ev_handler(Event e, Context *c) {
switch (e.event) { 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); default: printf("Unhandled event ID = %d\n", e.event); assert(0);
} }
return c; return c;
@ -14,18 +30,37 @@ void __am_cte_init() {
} }
void rt_hw_context_switch_to(rt_ubase_t to) { 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) { 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) { void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread) {
assert(0); 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); 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;
} }

View file

@ -1,7 +1,7 @@
#include <am.h> #include <am.h>
#include <rtthread.h>
#include <klib.h>
#include <klib-macros.h> #include <klib-macros.h>
#include <klib.h>
#include <rtthread.h>
#define AM_APPS_HEAP_SIZE 0x2000000 #define AM_APPS_HEAP_SIZE 0x2000000
#define RT_HW_HEAP_BEGIN heap.start #define RT_HW_HEAP_BEGIN heap.start
@ -21,15 +21,18 @@ void rt_hw_board_init() {
uint32_t size = AM_APPS_HEAP_SIZE; uint32_t size = AM_APPS_HEAP_SIZE;
void *p = NULL; 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); am_apps_heap = RANGE(p, p + size);
extern char __am_apps_data_start, __am_apps_data_end; extern char __am_apps_data_start, __am_apps_data_end;
extern char __am_apps_bss_start, __am_apps_bss_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_data = RANGE(&__am_apps_data_start, &__am_apps_data_end);
am_apps_bss = RANGE(&__am_apps_bss_start, &__am_apps_bss_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", // 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_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; uint32_t data_size = am_apps_data.end - am_apps_data.start;
if (data_size != 0) { if (data_size != 0) {
@ -48,7 +51,8 @@ void rt_hw_board_init() {
#endif #endif
#ifdef RT_USING_HEAP #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 #endif
} }