This commit is contained in:
lr
2024-05-29 15:37:20 +08:00
139 changed files with 4951 additions and 486 deletions
@@ -9,6 +9,12 @@ user_ldflags = --start-group,-lgcc,-lc,--end-group
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
#cflags = -Wall -g -std=c11
endif
ifeq ($(BOARD), ok1028a-c)
toolchain ?= aarch64-none-elf-
user_ldflags = -N -Ttext 0
cflags = -Wall -g -std=c11 -mtune=cortex-a72 -nostdlib -nodefaultlibs -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
endif
cc = ${toolchain}gcc
ld = ${toolchain}g++
objdump = ${toolchain}objdump
+22 -15
View File
@@ -79,15 +79,26 @@ struct IpcMsg* new_ipc_msg(struct Session* session, const int argc, const int* a
bool ipc_msg_set_nth_arg(struct IpcMsg* msg, const int arg_num, const void* const data, const int len)
{
if (arg_num >= msg->header.nr_args) {
printf("[%s] IPC: arg_num out of msg range, arg_num: %d, nr_args: %d\n", __func__, arg_num, msg->header.nr_args);
printf("[%s] IPC: arg_num out of msg range, arg_num: %d, nr_args: %u\n", __func__, arg_num, msg->header.nr_args);
return false;
}
struct IpcArgInfo* nth_arg_info = IPCMSG_ARG_INFO(msg, arg_num);
if (len > nth_arg_info->len) {
printf("[%s] IPC: size of arg out of buffer range, given len: %d, len %d\n", __func__, len, nth_arg_info->len);
if (len < 0 || (uint32_t)len > (uint32_t)nth_arg_info->len) {
printf("[%s] IPC: size of arg out of buffer range, given len: %d, len %u\n", __func__, len, nth_arg_info->len);
return false;
}
void* buf = ipc_msg_get_nth_arg_buf(msg, arg_num);
// handle attributes of different params
if (data == NULL) {
nth_arg_info->null_ptr = 1;
memset(buf, 0x0, len);
return true;
} else {
nth_arg_info->null_ptr = 0;
}
memmove(buf, data, len);
return true;
}
@@ -109,6 +120,12 @@ bool ipc_msg_get_nth_arg(struct IpcMsg* msg, const int arg_num, void* data, cons
printf("[%s] IPC: size of arg out of buffer range", __func__);
return false;
}
// handle null ptr: do nothing
if (nth_arg_info->null_ptr == 1) {
return true;
}
void* buf = ipc_msg_get_nth_arg_buf(msg, arg_num);
memmove(data, buf, len);
return true;
@@ -170,12 +187,6 @@ bool is_cur_handler_been_delayed()
return ipc_server_loop_cur_msg->header.delayed == 1;
}
bool server_set_cycle_handler(struct IpcNode* ipc_node, void (*handler)())
{
ipc_node->cycle_handler = handler;
return true;
}
void ipc_server_loop(struct IpcNode* ipc_node)
{
struct Session session_list[NR_MAX_SESSION];
@@ -204,7 +215,6 @@ void ipc_server_loop(struct IpcNode* ipc_node)
interfaces[opcode] should explicitly call delay_session() and return to delay this session
*/
while (ipc_server_loop_cur_msg->header.magic == IPC_MSG_MAGIC && ipc_server_loop_cur_msg->header.valid == 1 && ipc_server_loop_cur_msg->header.done == 0) {
// printf("session %d [%d, %d]\n", session_list[i].id, session_list[i].head, session_list[i].tail);
if (session_used_size(&session_list[i]) == 0 && session_forward_tail(&session_list[i], ipc_server_loop_cur_msg->header.len) < 0) {
break;
}
@@ -213,13 +223,13 @@ void ipc_server_loop(struct IpcNode* ipc_node)
if (ipc_node->interfaces[ipc_server_loop_cur_msg->header.opcode]) {
ipc_node->interfaces[ipc_server_loop_cur_msg->header.opcode](ipc_server_loop_cur_msg);
// check if this session is delayed by op handler, all messages after the delayed message in current session is blocked.
if (is_cur_session_delayed()) {
if (ipc_server_loop_cur_msg->header.done == 0) {
ipc_server_loop_cur_msg->header.delayed = 1;
has_delayed = true;
break;
}
} else {
printf("Unsupport opcode(%d) for server: %s\n", ipc_server_loop_cur_msg->header.opcode, ipc_node->name);
printf("Unsupport opcode(%u) for server: %s\n", ipc_server_loop_cur_msg->header.opcode, ipc_node->name);
}
// current msg is a message that needs to ignore
// finish this message in server's perspective
@@ -233,8 +243,5 @@ void ipc_server_loop(struct IpcNode* ipc_node)
ipc_server_loop_cur_msg = NULL;
}
}
if (ipc_node->cycle_handler) {
ipc_node->cycle_handler();
}
}
}
+14 -2
View File
@@ -60,6 +60,13 @@ typedef struct {
struct IpcArgInfo {
uint16_t offset;
uint16_t len;
union {
uint16_t attr;
struct {
uint16_t null_ptr : 1;
uint16_t reserved : 15;
};
};
} __attribute__((packed));
/* [header, ipc_arg_buffer_len[], ipc_arg_buffer[]] */
@@ -76,7 +83,6 @@ typedef int (*IpcInterface)(struct IpcMsg* msg);
struct IpcNode {
char* name;
IpcInterface interfaces[UINT8_MAX];
void (*cycle_handler)();
} __attribute__((packed));
#define IPC_SERVER_LOOP(ipc_node_name) rpc_server_loop_##rpc_node_name
@@ -105,6 +111,10 @@ struct IpcNode {
/// @return
__attribute__((__always_inline__)) static inline void* ipc_msg_get_nth_arg_buf(struct IpcMsg* msg, int arg_num)
{
if (IPCMSG_ARG_INFO(msg, arg_num)->null_ptr == 1) {
return NULL;
}
return (void*)((char*)msg + IPCMSG_ARG_INFO(msg, arg_num)->offset);
}
@@ -243,6 +253,8 @@ bool is_cur_session_delayed(void);
}
int cur_session_id(void);
bool server_set_cycle_handler(struct IpcNode* ipc_node, void (*handler)());
/// @brief delay the session(message, or a inter-process-call)
/// the delayed call will be handled again later from begining, not from the position where delay_session() is called.
/// @param
void delay_session(void);
bool is_cur_handler_been_delayed();
@@ -46,10 +46,10 @@ int free_session(struct Session* session)
void* session_alloc_buf(struct Session* session, int len)
{
if (len > session_remain_capacity(session)) {
if (len < 0 || len > session_remain_capacity(session)) {
return NULL;
}
void* buf = session->buf + session->tail;
void* buf = (void*)((uintptr_t)session->buf + session->tail);
// we mapped double size of page, so it's ok to write buffer directly
memset(buf, 0, len);
session_forward_tail(session, len);
@@ -58,7 +58,7 @@ void* session_alloc_buf(struct Session* session, int len)
bool session_free_buf(struct Session* session, int len)
{
if (len > session_used_size(session)) {
if (len < 0 || len > session_used_size(session)) {
return false;
}
assert(session_forward_head(session, len) != -1);
@@ -1,15 +1,23 @@
ifeq ($(BOARD), imx6q-sabrelite)
toolchain ?= arm-none-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), zynq7000-zc702)
toolchain ?= arm-xilinx-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), ok1028a-c)
toolchain ?= aarch64-none-elf-
user_ldflags = -N -Ttext 0
cflags = -Wall -g -std=c11 -mtune=cortex-a72 -nostdlib -nodefaultlibs -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
endif
cc = ${toolchain}gcc
ld = ${toolchain}g++
objdump = ${toolchain}objdump
user_ldflags = -N -Ttext 0
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
c_useropts = -O2
INC_DIR = -I$(KERNEL_ROOT)/services/app \
@@ -41,7 +41,7 @@
#define PUT_NOTAG(p, val) (*(unsigned int*)(p) = (val))
// Store predecessor or successor pointer for free blocks
#define SET_PTR(p, ptr) (*(unsigned int*)(p) = (unsigned int)(ptr))
#define SET_PTR(p, ptr) (*(uintptr_t*)(p) = (uintptr_t)(ptr))
// Read the size and allocation bit from address p
#define GET_SIZE(p) (GET(p) & ~0x7)
@@ -1,15 +1,24 @@
ifeq ($(BOARD), imx6q-sabrelite)
toolchain ?= arm-none-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), zynq7000-zc702)
toolchain ?= arm-xilinx-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), ok1028a-c)
toolchain ?= aarch64-none-elf-
user_ldflags = -N -Ttext 0
cflags = -Wall -g -std=c11 -mtune=cortex-a72 -nostdlib -nodefaultlibs -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
endif
cc = ${toolchain}gcc
ld = ${toolchain}g++
objdump = ${toolchain}objdump
user_ldflags = -N -Ttext 0
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
c_useropts = -O2
INC_DIR = -I$(KERNEL_ROOT)/services/app \
@@ -1,15 +1,22 @@
ifeq ($(BOARD), imx6q-sabrelite)
toolchain ?= arm-none-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), zynq7000-zc702)
toolchain ?= arm-xilinx-eabi-
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
user_ldflags = -N -Ttext 0
endif
ifeq ($(BOARD), ok1028a-c)
toolchain ?= aarch64-none-elf-
user_ldflags = -N -Ttext 0
cflags = -Wall -g -std=c11 -mtune=cortex-a72 -nostdlib -nodefaultlibs -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
endif
cc = ${toolchain}gcc
ld = ${toolchain}g++
objdump = ${toolchain}objdump
user_ldflags = -N -Ttext 0
cflags = -std=c11 -march=armv7-a -mtune=cortex-a9 -nostdlib -nodefaultlibs -mfloat-abi=soft -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -ggdb -Wno-unused -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie
c_useropts = -O2
INC_DIR = -I$(KERNEL_ROOT)/services/app \
@@ -14,14 +14,19 @@
int spawn(struct Session* session, int fd, ipc_read_fn ipc_read, ipc_fsize_fn ipc_fsize, char* name, char** argv)
{
/* read elf image */
int file_size = ipc_fsize(session, fd);
void* img = malloc(file_size);
int read_len = 0, cur_read_len = 0;
int read_len = 0;
while (read_len < file_size) {
cur_read_len = file_size - read_len < 4096 ? file_size - read_len : 4096;
read_len += ipc_read(session, fd, img + read_len, read_len, cur_read_len);
int cur_read_len = file_size - read_len < 4096 ? file_size - read_len : 4096;
if (cur_read_len < 0) {
return -1;
}
read_len += ipc_read(session, fd, (char*)((uintptr_t)img + read_len), read_len, cur_read_len);
}
int ret = syscall(SYSCALL_SPAWN, (intptr_t)img, (intptr_t)name, (intptr_t)argv, 0);
/* sys call */
int ret = syscall(SYSCALL_SPAWN, (uintptr_t)img, (uintptr_t)name, (uintptr_t)argv, 0);
free(img);
return ret;
}
@@ -104,4 +109,24 @@ int mmap(uintptr_t vaddr, uintptr_t paddr, int len, bool is_dev)
int register_irq(int irq, int opcode)
{
return syscall(SYSCALL_REGISTER_IRQ, (intptr_t)irq, (intptr_t)opcode, 0, 0);
}
int semaphore_new(int val)
{
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_NEW, (intptr_t)val, 0, 0);
}
bool semaphore_free(int sem_id)
{
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_FREE, (intptr_t)sem_id, 0, 0);
}
bool semaphore_wait(int sem_id)
{
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_WAIT, (intptr_t)sem_id, 0, 0);
}
bool semaphore_signal(int sem_id)
{
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_SIGNAL, (intptr_t)sem_id, 0, 0);
}
@@ -32,6 +32,8 @@
#define SYSCALL_REGISTER_IRQ 11 //
#define SYSCALL_KILL 12 // kill the task by id
#define SYSCALL_SEMAPHORE 13 // semaphore related operations
// clang-format on
typedef enum {
@@ -58,6 +60,13 @@ typedef union {
int priority;
} sys_state_info;
typedef enum {
SYS_SEM_NEW = 0,
SYS_SEM_FREE,
SYS_SEM_SIGNAL,
SYS_SEM_WAIT,
} sys_sem_option;
typedef int (*ipc_read_fn)(struct Session* session, int fd, char* dst, int offset, int len);
typedef int (*ipc_fsize_fn)(struct Session* session, int fd);
typedef int (*ipc_write_fn)(struct Session* session, int fd, char* src, int offset, int len);
@@ -69,15 +78,23 @@ int thread(void* entry, const char* name, char** argv);
void exit(int status);
int yield(task_yield_reason reason);
int kill(int pid);
int register_server(char* name);
int session(char* path, int capacity, struct Session* user_session);
int poll_session(struct Session* userland_session_arr, int arr_capacity);
int close_session(struct Session* session);
int register_irq(int irq, int opcode);
int mmap(uintptr_t vaddr, uintptr_t paddr, int len, bool is_dev);
int task_heap_base();
int get_memblock_info(sys_state_info* info);
int set_priority(sys_state_info* info);
int show_task();
int show_mem();
int show_cpu();
int mmap(uintptr_t vaddr, uintptr_t paddr, int len, bool is_dev);
int register_irq(int irq, int opcode);
int semaphore_new(int val);
bool semaphore_free(int sem_id);
bool semaphore_wait(int sem_id);
bool semaphore_signal(int sem_id);