support ipc.

This commit is contained in:
zhangzheng
2023-08-29 16:16:25 +08:00
parent 24ed049af7
commit 3b1171fca8
36 changed files with 830 additions and 118 deletions

View File

@@ -27,11 +27,11 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/obj/include
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/include
)
target_link_libraries(
sys
PUBLIC
muslc
)
add_dependencies(sys muslc)
# target_link_libraries(
# sys
# PUBLIC
# muslc
# )
# add_dependencies(sys muslc)

View File

@@ -2,5 +2,6 @@
#include "u_types.h"
msg_tag_t factory_create_ipc(obj_handler_t obj, obj_handler_t tgt_obj_handler);
msg_tag_t factory_create_thread(obj_handler_t obj, obj_handler_t tgt_obj_handler);
msg_tag_t factory_create_task(obj_handler_t obj, obj_handler_t tgt_obj_handler);

View File

@@ -0,0 +1,5 @@
#pragma once
#include "u_prot.h"
msg_tag_t ipc_recv(obj_handler_t obj, void *buf, int len);
msg_tag_t ipc_send(obj_handler_t obj, void *buf, int len);

View File

@@ -1,6 +1,6 @@
#include "u_types.h"
#include "u_prot.h"
#include "u_factory.h"
msg_tag_t factory_create_thread(obj_handler_t obj, obj_handler_t tgt_obj_handler)
{
register volatile umword_t r0 asm("r0");
@@ -28,4 +28,18 @@ msg_tag_t factory_create_task(obj_handler_t obj, obj_handler_t tgt_obj_handler)
msg_tag_t tag = msg_tag_init(r0);
return tag;
}
}
msg_tag_t factory_create_ipc(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,
IPC_PROT,
tgt_obj_handler,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}

View File

@@ -0,0 +1,35 @@
#include "u_types.h"
#include "u_prot.h"
enum ipc_op
{
IPC_SEND, //!< 发送IPC消息
IPC_REVC, //!< 接受IPC消息
};
msg_tag_t ipc_recv(obj_handler_t obj, void *buf, int len)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(IPC_REVC, 2, IPC_PROT).raw,
buf,
len,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}
msg_tag_t ipc_send(obj_handler_t obj, void *buf, int len)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(IPC_SEND, 2, IPC_PROT).raw,
buf,
len,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
}

View File

@@ -36,5 +36,8 @@ void ulog_write_bytes(obj_handler_t obj_inx, const uint8_t *data, umword_t len)
void ulog_write_str(obj_handler_t obj_inx, const char *str)
{
ulog_write_bytes(obj_inx, (uint8_t *)str, strlen(str));
size_t i;
for (i = 0; str[i]; i++)
;
ulog_write_bytes(obj_inx, (uint8_t *)str, i);
}

View File

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

View File

@@ -0,0 +1,83 @@
#include "u_types.h"
#include "cpiofs.h"
#include <string.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b)) //!< 最小值
#define ALIGN(mem, align) (((mem) + ((align)-1)) & (~((align)-1))) //!< 向上对齐
int htoi(char *str, int len)
{
int n = 0;
int i = 0;
if (str == NULL || len <= 0)
return -1;
for (i = 0; i < len; i++)
{
if (str[i] != '0')
{
break;
}
}
str += i;
while (1)
{
if (*str >= '0' && *str <= '9')
{
n = 16 * n + (*str - '0');
}
else if (*str >= 'A' && *str <= 'F') // 十六进制还要判断字符是不是在A-F或者a-f之间
{
n = 16 * n + (*str - 'A' + 10);
}
else if (*str >= 'a' && *str <= 'f')
{
n = 16 * n + (*str - 'a' + 10);
}
else
{
break;
}
str++;
i++;
if (i >= len)
{
break;
}
}
return n;
}
umword_t cpio_find_file(umword_t st, umword_t en, const char *name)
{
uint8_t rByte;
int32_t bk_inx;
cpio_fs_t *file_info;
for (umword_t i = st; st < en;)
{
file_info = (cpio_fs_t *)i;
if (check_magic(file_info) < 0)
{
return 0;
}
int name_size = htoi(file_info->c_namesize, 8);
const char *f_name = (char *)(i + sizeof(cpio_fs_t));
if (strcmp(f_name, name) == 0)
{
return (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
}
if (strcmp("TRAILER!!", f_name) == 0)
{
return 0;
}
int file_size = htoi(file_info->c_filesize, 8);
i = ALIGN(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4) +
file_size,
4);
}
return 0;
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include "u_types.h"
//! 26bytes
#pragma pack(1)
typedef struct cpio_fs
{
char c_magic[6];
char c_ino[8];
char c_mode[8];
char c_uid[8];
char c_gid[8];
char c_nlink[8];
char c_mtime[8];
char c_filesize[8];
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8];
char c_check[8];
} cpio_fs_t;
static inline int64_t check_magic(char magic[6])
{
if (magic[0] == '0' && magic[1] == '7' && magic[2] == '0' &&
magic[3] == '7' && magic[4] == '0' && magic[5] == '1')
{
return 0;
}
return -1;
}
int htoi(char *str, int len);
umword_t cpio_find_file(umword_t st, umword_t en, const char *name);

View File

@@ -26,20 +26,55 @@ void mm_test(void)
// memset(mem, 0, 512);
// memset(mem1, 0, 512);
}
#include "u_ipc.h"
void thread_test_func(void)
{
char data[8];
while (1)
{
msg_tag_t tag = ipc_recv(12, data, sizeof(data));
if (msg_tag_get_prot(tag) > 0)
{
printf("recv data is %s\n", data);
}
strcpy(data, "reply");
ipc_send(12, data, sizeof(data));
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, 11);
printf("Error\n");
}
void thread_test_func2(void)
{
char data[8] = "1234";
while (1)
{
strcpy(data, "1234");
ipc_send(12, data, sizeof(data));
msg_tag_t tag = ipc_recv(12, data, sizeof(data));
if (msg_tag_get_prot(tag) > 0)
{
printf("recv data is %s\n", data);
}
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, 10);
printf("Error\n");
}
void thread_test_func3(void)
{
char data[8] = "____";
while (1)
{
ipc_send(12, data, sizeof(data));
}
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];
static __attribute__((aligned(8))) uint8_t val2[1024];
typedef struct app_info
{
@@ -67,19 +102,27 @@ typedef struct app_info
const char dot_text[];
} app_info_t;
#include "cpiofs.h"
void factory_test(void)
{
// void *mem = mm_alloc_page(MM_PROT, 4, REGION_RWX);
// assert(mem);
// memset(mem, 0, 2048);
msg_tag_t tag = factory_create_thread(FACTORY_PROT, 11);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, 12);
if (msg_tag_get_prot(tag) < 0)
{
printf("factory_create_ipc no memory\n");
return;
}
tag = factory_create_thread(FACTORY_PROT, 11);
if (msg_tag_get_prot(tag) < 0)
{
printf("factory_create_thread no memory\n");
return;
}
#if 0
#if 1
thread_exec_regs(11, (umword_t)thread_test_func, (umword_t)val + 1024, RAM_BASE());
thread_bind_task(11, TASK_PROT);
thread_run(11);
@@ -88,8 +131,16 @@ void factory_test(void)
thread_exec_regs(10, (umword_t)thread_test_func2, (umword_t)val1 + 1024, RAM_BASE());
thread_bind_task(10, TASK_PROT);
thread_run(10);
// factory_create_thread(FACTORY_PROT, 13);
// thread_exec_regs(13, (umword_t)thread_test_func3, (umword_t)val2 + 1024, RAM_BASE());
// thread_bind_task(13, TASK_PROT);
// thread_run(13);
#else
app_info_t *app = (app_info_t *)0x8014000;
umword_t addr = cpio_find_file((umword_t)0x801ff8c, (umword_t)0x8040000, "shell");
assert(addr);
app_info_t *app = (app_info_t *)addr;
umword_t ram_base;
tag = factory_create_task(FACTORY_PROT, 10);
if (msg_tag_get_prot(tag) < 0)
@@ -106,7 +157,7 @@ void factory_test(void)
task_map(10, LOG_PROT, LOG_PROT);
void *sp_addr = (char *)ram_base + app->i.stack_offset - app->i.data_offset;
void *sp_addr_top = (char *)sp_addr + app->i.stack_size;
thread_exec_regs(11, (umword_t)0x8014000, (umword_t)sp_addr_top, ram_base);
thread_exec_regs(11, (umword_t)addr, (umword_t)sp_addr_top, ram_base);
thread_bind_task(11, 10);
thread_run(11);
#endif

View File

@@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 3.13)
file(GLOB_RECURSE deps *.c *.S)
add_executable(path_manager.elf
${deps}
)
target_link_libraries(path_manager.elf
PUBLIC
# muslc
sys
${GCC_LIB_PATH}/libgcc.a
)
target_include_directories(
path_manager.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(path_manager.elf
muslc
)
set_target_properties(path_manager.elf PROPERTIES LINK_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie "
)
add_custom_target(
path_manager_dump ALL
COMMAND
${CMAKE_OBJDUMP} -s -S path_manager.elf > ${CMAKE_SOURCE_DIR}/build/output/path_manager.S
COMMAND
${CMAKE_READELF} -a path_manager.elf > ${CMAKE_SOURCE_DIR}/build/output/path_manager.txt
COMMAND
${CMAKE_OBJCOPY} -O binary -S path_manager.elf path_manager.bin
COMMAND
${CMAKE_SIZE} path_manager.elf
COMMAND
mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio
COMMAND
cp path_manager.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/path_manager
COMMAND
cp path_manager.elf ${CMAKE_SOURCE_DIR}/build/output/path_manager.elf
)
add_dependencies(path_manager_dump path_manager.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,23 @@
#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");
ulog_write_str(LOG_PROT, "MKRTOS:\n");
while (1)
;
return 0;
}
void _start_c_init(void)
{
main(0, 0);
while(1);
}

View File

@@ -0,0 +1,74 @@
extern int __text_start__;
extern int __got_start__;
extern int __got_end__;
extern int __rel_start__;
extern int __rel_end__;
// extern int __data_rel_ro_start__;
// extern int __data_rel_ro_end__;
extern unsigned int data_offset;
extern unsigned int rel_end;
void _reloc(unsigned int *gbase, unsigned int tbase)
{
int i;
unsigned int s;
unsigned int offset;
unsigned int gs, ge, ts;
unsigned int *rs, *re;
unsigned int *pointer;
unsigned int *drrs;
unsigned int *drre;
gs = (unsigned int)&__got_start__;
ge = (unsigned int)&__got_end__;
ts = (unsigned int)&__text_start__;
for (i = 0, s = gs; s < ge; s += 4, i++)
{
if (gbase[i] >= gs)
{
offset = gbase[i] - gs;
gbase[i] = offset + (unsigned int)gbase;
}
else
{
offset = gbase[i] - ts;
gbase[i] = offset + tbase;
}
}
// drrs = (unsigned int *)&__data_rel_ro_start__;
// drre = (unsigned int *)&__data_rel_ro_end__;
// int data_of = data_offset;
// data_of = data_of;
// for (i = 0, s = drrs; s < drre; s += 4, i++)
// {
// if (drrs[i]) {
// drrs[i] = (unsigned int )gbase + (drrs[i] - ((unsigned int )&__rel_end__ - tbase));
// }
// }
rs = (unsigned int *)&__rel_start__;
re = (unsigned int *)&__rel_end__;
// rel
for (i = 0, s = (unsigned int)rs; s < (unsigned int)re; s += 8, i += 2)
{
if (rs[i + 1] == 0x00000017)
{
offset = rs[i] - gs;
pointer = (unsigned int *)((unsigned int)gbase + offset);
if (*pointer >= gs)
{
offset = *pointer - gs;
*pointer = offset + (unsigned int)gbase;
}
else
{
offset = *pointer - ts;
*pointer = offset + (unsigned int)tbase;
}
}
}
return;
}

View File

@@ -0,0 +1,99 @@
.syntax unified
.section .first, #execinstr
.globl reloc
.globl data_offset
.align 2
.globl _start_
.type _start_, %function
_start_:
/*save r0 */
mov r12, r0
b __start
.SPACE 32 - (. - _start_)
_head_start:
.ascii "MKRTOS. "
.word __ram_size__
heap_offset: .word __heap_start__ - _start_
stack_offset: .word __stack_start__ - _start_
heap_size: .word __heap_size__
stack_size: .word __stack_size__
data_offset: .word __data_start__ - _start_
.word __bss_start__ - _start_
got_start: .word __got_start__
got_end: .word __got_end__
rel_start: .word __rel_start__
rel_end: .word __rel_end__
text_start: .word __text_start__
.SPACE 128 - (. - _start_)
.align
__start:
movs r1, #0
adr r2, _start_
bic r2, r2, #1
ldr r0, = __data_start__
add r0, r0, r2
ldr r3, = __data_end__
add r3, r3, r2
b LoopCopyDataInit
CopyDataInit:
ldr r2, [r0, r1]
str r2, [r9, r1]
adds r1, r1, #4
LoopCopyDataInit:
adds r2, r0, r1
cmp r2, r3
bcc CopyDataInit
//movs r0, #0
//ldr r1, = __data_start__
//ldr r2, = __bss_start__
//sub r2, r2, r1
//add r2, r2, r9
//ldr r3, = __bss_end__
//sub r3, r3, r1
//add r3, r3, r9
//b LoopFillZerobss
/* Zero fill the bss segment. */
//FillZerobss:
// str r0, [r2], #4
//LoopFillZerobss:
// cmp r2, r3
// bcc FillZerobss
/* initial stack */
ldr r0, =#0x23232323
ldr r1, = __data_start__
ldr r2, = __stack_start__
sub r2, r2, r1
add r2, r2, r9
ldr r3, = __stack_end__
sub r3, r3, r1
add r3, r3, r9
b LoopInitStack
/* fill the stack segment with '#'. */
FillStack:
str r0, [r2], #4
LoopInitStack:
cmp r2, r3
bcc FillStack
mov r0, r12
push {r0, r4}
//ldr r0, = __stack_end__
//mov sp, r0
mov r0, r9
adr r1, _start_
bic r1, r1, #1
bl _reloc
pop {r0, r12}
adr r1, _start_
bl _start_c_init
b .
.end

View File

@@ -41,9 +41,9 @@ add_custom_target(
COMMAND
${CMAKE_SIZE} shell.elf
COMMAND
mkdir -p ${CMAKE_SOURCE_DIR}/build/output
mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio
COMMAND
cp shell.bin ${CMAKE_SOURCE_DIR}/build/output/shell
cp shell.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/shell
COMMAND
cp shell.elf ${CMAKE_SOURCE_DIR}/build/output/shell.elf
)

View File

@@ -9,7 +9,8 @@
#include <stdio.h>
int main(int argc, char *args[])
{
printf("shell>\n");
// printf("shell>\n");
ulog_write_str(LOG_PROT, "MKRTOS:\n");
while(1);
return 0;
}