支持unmap

This commit is contained in:
zhangzheng
2023-08-28 22:11:49 +08:00
parent f8ac961264
commit dadfda135c
39 changed files with 799 additions and 106 deletions

View File

@@ -84,7 +84,7 @@
#define LINE_MAX 4096
#define RE_DUP_MAX 255
#define NL_ARGMAX 6
#define NL_ARGMAX 5
#define NL_MSGMAX 32767
#define NL_SETMAX 255
#define NL_TEXTMAX 2048

View File

@@ -102,7 +102,7 @@ static const unsigned char states[]['z'-'A'+1] = {
union arg
{
uintmax_t i;
long double f;
double f;
void *p;
};
@@ -138,7 +138,7 @@ static void out(FILE *f, const char *s, size_t l)
static void pad(FILE *f, char c, int w, int l, int fl)
{
char pad[64];
char pad[32];
if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
l = w - l;
memset(pad, c, l>sizeof pad ? sizeof pad : l);
@@ -665,7 +665,7 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
va_list ap2;
int nl_type[NL_ARGMAX+1] = {0};
union arg nl_arg[NL_ARGMAX+1];
unsigned char internal_buf[80], *saved_buf = 0;
unsigned char internal_buf[32], *saved_buf = 0;
int olderr;
int ret;

View File

@@ -2,3 +2,14 @@
#define WORD_BYTES (sizeof(void *))
#define WORD_BITS (WORD_BYTES * 8)
#define RAM_BASE() \
({ \
umword_t _val; \
__asm__ __volatile__( \
"mov %0, r9\n" \
: "=r"(_val) \
: \
:); \
_val; \
})

View File

@@ -0,0 +1,5 @@
#pragma
#include "u_types.h"
msg_tag_t factory_create_thread(obj_handler_t obj, obj_handler_t tgt_obj_handler);

View File

@@ -31,3 +31,4 @@ typedef struct msg_tag
#define msg_tag_get_prot(tag) \
((mword_t)((tag).prot))

View File

@@ -0,0 +1,5 @@
#pragma once
#include "u_prot.h"
#include "u_types.h"
msg_tag_t task_unmap(obj_handler_t task_han, obj_handler_t obj);

View File

@@ -0,0 +1,7 @@
#pragma once
#include "u_types.h"
msg_tag_t thread_exec_regs(obj_handler_t obj, umword_t pc, umword_t sp, umword_t ram);
msg_tag_t thread_run(obj_handler_t obj);
msg_tag_t thread_bind_task(obj_handler_t obj, obj_handler_t tk_obj);

View File

@@ -0,0 +1,17 @@
#include "u_types.h"
#include "u_prot.h"
msg_tag_t factory_create_thread(obj_handler_t obj, obj_handler_t tgt_obj_handler)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(0, 2, FACTORY_PROT).raw,
THREAD_PROT,
tgt_obj_handler,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}

View File

@@ -27,7 +27,7 @@ void ulog_write_bytes(obj_handler_t obj_inx, const uint8_t *data, umword_t len)
write_word_buf[3],
write_word_buf[4]);
}
if (j == len)
if (j >= len)
{
return;
}

View File

@@ -0,0 +1,23 @@
#include "u_types.h"
#include "u_prot.h"
enum task_op_code
{
TASK_OBJ_MAP,
TASK_OBJ_UNMAP,
};
msg_tag_t task_unmap(obj_handler_t task_han, obj_handler_t obj)
{
register volatile umword_t r0 asm("r0");
syscall(task_han, msg_tag_init3(TASK_OBJ_UNMAP, 1, TASK_PROT).raw,
obj,
0,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}

View File

@@ -0,0 +1,50 @@
#include "u_prot.h"
#include "u_types.h"
enum thread_op
{
SET_EXEC_REGS,
RUN_THREAD,
BIND_TASK,
};
msg_tag_t thread_exec_regs(obj_handler_t obj, umword_t pc, umword_t sp, umword_t ram)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(SET_EXEC_REGS, 3, THREAD_PROT).raw,
obj,
pc,
sp,
ram,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}
msg_tag_t thread_run(obj_handler_t obj)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(RUN_THREAD, 1, THREAD_PROT).raw,
obj,
0,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}
msg_tag_t thread_bind_task(obj_handler_t obj, obj_handler_t tk_obj)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(BIND_TASK, 1, THREAD_PROT).raw,
obj,
tk_obj,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}

View File

@@ -10,3 +10,4 @@ set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
add_subdirectory(init)
add_subdirectory(shell)

View File

@@ -1,13 +1,13 @@
#define HEAP_SIZE 4
#define STACK_SIZE 1024 * 3
#define STACK_SIZE 1024 * 2
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))
#define STACK_ATTR SECTION("STACK") __attribute__((zero_init))
#elif defined(__GNUC__)
#define HEAP_ATTR
#define STACK_ATTR
#define HEAP_ATTR __attribute__((__section__(".bss.heap")))
#define STACK_ATTR __attribute__((__section__(".bss.stack")))
#elif defined(__IAR_SYSTEMS_ICC__)
#define HEAP_ATTR
#define STACK_ATTR

View File

@@ -2,6 +2,9 @@
#include "u_log.h"
#include "u_prot.h"
#include "u_mm.h"
#include "u_factory.h"
#include "u_thread.h"
#include "u_task.h"
#include <assert.h>
#include <stdio.h>
void ulog_test(void)
@@ -11,18 +14,49 @@ void ulog_test(void)
}
void mm_test(void)
{
void *mem = mm_alloc_page(MM_PROT, 1, REGION_RWX);
void *mem = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem);
memset(mem, 0, 512);
void *mem1 = mm_alloc_page(MM_PROT, 1, REGION_RWX);
memset((char *)mem, 0, 1024);
void *mem1 = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem1);
memset(mem1, 0, 512);
memset(mem1, 0, 1024);
// mm_free_page(MM_PROT, mem1, 1);
// mm_free_page(MM_PROT, mem, 1);
mm_free_page(MM_PROT, mem1, 2);
mm_free_page(MM_PROT, mem, 2);
// memset(mem, 0, 512);
// memset(mem1, 0, 512);
}
void thread_test_func(void)
{
printf("thread_test_func.\n");
task_unmap(TASK_PROT, 11);
printf("Error\n");
}
void thread_test_func2(void)
{
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, 10);
printf("Error\n");
}
static __attribute__((aligned(8))) uint8_t val[1024];
static __attribute__((aligned(8))) uint8_t val1[1024];
void factory_test(void)
{
// void *mem = mm_alloc_page(MM_PROT, 4, REGION_RWX);
// assert(mem);
// memset(mem, 0, 2048);
factory_create_thread(FACTORY_PROT, 11);
thread_exec_regs(11, (umword_t)thread_test_func, (umword_t)val + 1024, RAM_BASE());
thread_bind_task(11, TASK_PROT);
thread_run(11);
#if 1
factory_create_thread(FACTORY_PROT, 10);
thread_exec_regs(10, (umword_t)thread_test_func2, (umword_t)val1 + 1024, RAM_BASE());
thread_bind_task(10, TASK_PROT);
thread_run(10);
#endif
}
void mpu_test(void)
{
@@ -41,16 +75,18 @@ void printf_test(void)
float a = 1.1;
float b = 1.2;
float c = a + b;
c = c;
printf("%c %d %lf\n", 'a', 1234, 1.1);
printf("%c %d %f\n", 'a', 1234, 1.1);
// c = c;
// printf("%c %d %lf\n", 'a', 1234, 1.1); 浮点打印有问题
}
int main(int argc, char *args[])
{
factory_test();
mm_test();
mpu_test();
ulog_test();
printf_test();
while (1)
;
printf("exit init.\n");
task_unmap(TASK_PROT, TASK_PROT);
return 0;
}

View File

@@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 3.13)
file(GLOB_RECURSE deps *.c *.S)
add_executable(shell.elf
${deps}
)
target_link_libraries(shell.elf
PUBLIC
muslc
sys
${GCC_LIB_PATH}/libgcc.a
)
target_include_directories(
shell.elf
PUBLIC
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/arm/
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/generic
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/obj/src/internal
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/src/include
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/src/internal
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/obj/include
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/include
)
add_dependencies(shell.elf
muslc
)
set_target_properties(shell.elf PROPERTIES LINK_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie "
)
add_custom_target(
shell_dump ALL
COMMAND
${CMAKE_OBJDUMP} -s -S shell.elf > ${CMAKE_SOURCE_DIR}/build/output/shell.S
COMMAND
${CMAKE_READELF} -a shell.elf > ${CMAKE_SOURCE_DIR}/build/output/shell.txt
COMMAND
${CMAKE_OBJCOPY} -O binary -S shell.elf shell.bin
COMMAND
${CMAKE_SIZE} shell.elf
COMMAND
mkdir -p ${CMAKE_SOURCE_DIR}/build/output
COMMAND
cp shell.bin ${CMAKE_SOURCE_DIR}/build/output/shell
COMMAND
cp shell.elf ${CMAKE_SOURCE_DIR}/build/output/shell.elf
)
add_dependencies(shell_dump shell.elf)

View File

@@ -0,0 +1,119 @@
ENTRY(_start_)
_system_stack_size = 0x100;
SECTIONS
{
.text : {
. = ALIGN(4);
__text_start__ = .;
KEEP(*(.first))
*(.text)
*(.text.*)
KEEP(*(.init))
KEEP(*(.fini))
/* .ctors */
*crtbegin.o(.ctors)
*crtbegin?.o(.ctors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
*(SORT(.ctors.*))
*(.ctors)
/* .dtors */
*crtbegin.o(.dtors)
*crtbegin?.o(.dtors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
*(SORT(.dtors.*))
*(.dtors)
*(SORT(.rodata.*))
*(.rodata)
KEEP(*(.eh_frame*))
. = ALIGN(4);
__rel_start__ = .;
*(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
__rel_end__ = .;
}
.ARM.exidx : {
. = ALIGN(4);
__exdix_start = .;
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
/* This is used by the startup in order to initialize the .data secion */
__exdix_end = .;
}
.permissions_table : {
. = ALIGN(4);
__permissions_table_start__ = .;
KEEP(*(.permissions_table))
__permissions_table_end__ = .;
}
PROVIDE(__ram_size__ = __bss_end__ - __data_start__);
.data : {
. = ALIGN(4);
__data_start__ = .;
__got_start__ = .;
*(.got)
__got_end__ = .;
. = ALIGN(4);
*(.data)
*(.data.*)
*(vtable)
*(.data*)
. = ALIGN(4);
/* preinit data */
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP(*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
/* init data */
PROVIDE_HIDDEN (__init_array_start = .);
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
/* finit data */
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP(*(SORT(.fini_array.*)))
KEEP(*(.fini_array))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);
/* All data end */
__data_end__ = .;
}
PROVIDE(__heap_size__ = __heap_end__ - __heap_start__);
PROVIDE(__stack_size__ = __stack_end__ - __stack_start__);
.bss : {
. = ALIGN(4);
/* This is used by the startup in order to initialize the .bss secion */
__bss_start__ = .;
*(.bss)
*(COMMON)
. = ALIGN(4);
__heap_start__ = .;
KEEP(*(.bss.heap))
__heap_end__ = .;
. = ALIGN(4);
__stack_start__ = .;
KEEP(*(.bss.stack))
__stack_end__ = .;
*(.bss.*)
/* This is used by the startup in order to initialize the .bss secion */
. = ALIGN(4);
__bss_end__ = .;
}
_end = .;
}

View File

@@ -0,0 +1,17 @@
#define HEAP_SIZE 4
#define STACK_SIZE 1024 * 2
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))
#define STACK_ATTR SECTION("STACK") __attribute__((zero_init))
#elif defined(__GNUC__)
#define HEAP_ATTR __attribute__((__section__(".bss.heap")))
#define STACK_ATTR __attribute__((__section__(".bss.stack")))
#elif defined(__IAR_SYSTEMS_ICC__)
#define HEAP_ATTR
#define STACK_ATTR
#endif
__attribute__((used)) HEAP_ATTR static char heap[HEAP_SIZE];
__attribute__((used)) STACK_ATTR static char stack[STACK_SIZE];

View File

@@ -0,0 +1,15 @@
#include "u_log.h"
#include "u_prot.h"
#include "u_mm.h"
#include "u_factory.h"
#include "u_thread.h"
#include "u_task.h"
#include <assert.h>
#include <stdio.h>
int main(int argc, char *args[])
{
printf("shell>\n");
while(1);
return 0;
}