ipc增加测试

This commit is contained in:
zhangzheng
2023-08-30 00:36:52 +08:00
parent 1733c9dd14
commit e4ea80232c
30 changed files with 731 additions and 307 deletions

View File

@@ -114,7 +114,10 @@
"u_task.h": "c",
"cpiofs.h": "c",
"u_factory.h": "c",
"u_thread.h": "c"
"u_thread.h": "c",
"u_app.h": "c",
"u_util.h": "c",
"u_mm.h": "c"
},
"cortex-debug.showRTOS": false,
}

View File

@@ -14,7 +14,8 @@
#include "arch.h"
#include "ref.h"
#define THREAD_BLOCK_SIZE 0x400 //!< 线程块大小,栈在块的顶部
struct thread;
typedef struct thread thread_t;
enum thread_state
{
THREAD_IDLE,
@@ -45,11 +46,13 @@ typedef struct sp_info
#define THREAD_MSG_BUG_LEN 128 //!< 默认的消息寄存器大小
#define MSG_BUF_HAS_DATA_FLAGS 0x01U //!< 已经有数据了
#define MSG_BUF_CALL_FLAGS 0x02U //!< 是CALL
typedef struct msg_buf
{
void *msg; //!< buf长度 @see THREAD_MSG_BUG_LEN
uhmword_t len; //!< 这里不是buf的大小而是存储接收或者发送的长度
uhmword_t flags; //!< 传输标志
void *msg; //!< buf长度 @see THREAD_MSG_BUG_LEN
uhmword_t len; //!< 这里不是buf的大小而是存储接收或者发送的长度
uhmword_t flags; //!< 传输标志
thread_t *call_send; //!< 当是call操作时存放call的发起方
} msg_buf_t;
#define THREAD_MAIGC 0xdeadead

View File

@@ -6,7 +6,6 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b)) //!< 最大值
#define MK_SET_BIT(a, b) ((a) |= 1 << (b)) //!< 设置BIT
#define MK_CLR_BIT(a, b) ((a) &= ~(1 << (b))) //!< 清除BIT
#define MK_GET_BIT(a, b) (((a) >> (b)) & 0x1) //!< 获取某位的bit
#define ABS(a) ((a) < 0 ? -(a) : (a)) //!< 取绝对值

View File

@@ -23,6 +23,7 @@ typedef struct ipc
//!< 等待回复的链表
//!< 发送消息后立刻进入挂起状态并标记flags在等待中这时rcv变量将不能改变直到接受者reply后清空。
thread_t *rcv; //!< 只有一个接受者
thread_t *call_send;
} ipc_t;
enum ipc_op
@@ -31,6 +32,218 @@ enum ipc_op
IPC_REVC, //!< 接受IPC消息
};
static bool_t is_call_send(ipc_t *ipc)
{
if (ipc->rcv->msg.call_send)
{
return TRUE;
}
return FALSE;
}
static void add_wait(ipc_t *ipc, thread_t *th)
{
umword_t status;
status = spinlock_lock(&ipc->lock);
slist_add_append(&ipc->wait_send, &th->wait);
thread_suspend(th);
spinlock_set(&ipc->lock, status);
}
// if (ipc->rcv->msg.call_send == th)
// {
// }
static int ipc_recv(ipc_t *ipc, entry_frame_t *f);
static int ipc_send(ipc_t *ipc, entry_frame_t *f)
{
umword_t status;
size_t send_len_c = -1;
umword_t send_flag = f->r[2];
thread_t *th = thread_get_current();
void *send_addr = th->msg.msg;
th->msg.len = f->r[1];
if (send_flag & MSG_BUF_CALL_FLAGS)
{
th->msg.flags |= MSG_BUF_CALL_FLAGS;
th->msg.call_send = NULL;
}
if (!ipc->rcv)
{
status = spinlock_lock(&ipc->lock);
if (!ipc->rcv)
{
slist_add_append(&ipc->wait_send, &th->wait);
thread_suspend(th);
}
spinlock_set(&ipc->lock, status);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
/*如果是call则需要吧发送者转变为接收者*/
/*TODO:call等待接收者回复消息*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
return ret;
}
return th->msg.len;
}
}
again_send:
assert(ipc->rcv);
if (ipc->rcv->status != THREAD_SUSPEND)
{
if (ipc->rcv->status == THREAD_DEAD)
{
return -EACCES;
}
add_wait(ipc, th);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
/*如果是call则需要吧发送者转变为接收者*/
/*TODO:call等待接收者回复消息*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
return ret;
}
return th->msg.len;
}
goto again_send;
}
else
{
status = spinlock_lock(&ipc->lock);
if (ipc->rcv->msg.call_send == th || ipc->rcv->msg.call_send == NULL)
{
if (ipc->rcv->status == THREAD_SUSPEND)
{
send_len_c = MIN(THREAD_MSG_BUG_LEN, th->msg.len);
// 接收线程正在等待中,直接复制数据到目标缓存中,然后唤醒目标线程
memcpy(ipc->rcv->msg.msg, th->msg.msg, send_len_c); //!< 拷贝数据
ipc->rcv->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
// ipc->rcv->msg.flags |= MSG_BUF_CALL_FLAGS;
ipc->rcv->msg.call_send = th; //! 当前线程是call发起方
}
else
{
ipc->rcv->msg.call_send = NULL;
}
ipc->rcv->msg.len = send_len_c;
thread_ready(ipc->rcv, TRUE); //!< 直接唤醒接受者
spinlock_set(&ipc->lock, status);
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
ipc->rcv = th;
/*如果是call则需要吧发送者转变为接收者*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
send_len_c = ret;
}
else
{
ipc->rcv->msg.call_send = NULL;
ipc->rcv = NULL; //! 如果不是call则清空接收者
}
}
}
else if (ipc->rcv->msg.call_send != th && ipc->rcv->msg.call_send != NULL)
{
spinlock_set(&ipc->lock, status);
add_wait(ipc, th);
goto again_send;
}
else
{
spinlock_set(&ipc->lock, status);
}
}
return send_len_c;
}
static int ipc_recv(ipc_t *ipc, entry_frame_t *f)
{
thread_t *th = thread_get_current();
if (!ipc->rcv)
{
umword_t status = spinlock_lock(&ipc->lock);
if (!ipc->rcv)
{
ipc->rcv = th;
}
else
{
spinlock_set(&ipc->lock, status);
return -EAGAIN;
}
spinlock_set(&ipc->lock, status);
}
void *recv_addr = th->msg.msg;
again_recv:
// 没有数据则睡眠
while (slist_is_empty(&ipc->wait_send))
{
thread_suspend(th);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
// 已经有数据了,直接返
return th->msg.len;
}
}
size_t recv_len_c = 0;
umword_t status = spinlock_lock(&ipc->lock);
if (slist_is_empty(&ipc->wait_send))
{
spinlock_set(&ipc->lock, status);
goto again_recv;
}
/*************************/
thread_t *send_th = NULL;
slist_foreach(send_th, &ipc->wait_send, wait)
{
if (send_th->msg.call_send == th)
{
break;
}
}
if (send_th == NULL)
{
slist_head_t *mslist = slist_first(&ipc->wait_send); // TODO从链表中找到与th相同的
slist_del(mslist);
send_th = container_of(mslist, thread_t, wait);
} else {
slist_del(&send_th->wait);
}
/*************************/
// slist_head_t *mslist = slist_first(&ipc->wait_send); //TODO从链表中找到与th相同的
// slist_del(mslist);
// thread_t *send_th = container_of(mslist, thread_t, wait);
recv_len_c = MIN(THREAD_MSG_BUG_LEN, send_th->msg.len);
memcpy(recv_addr, send_th->msg.msg, recv_len_c); //!< 拷贝数据
send_th->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
send_th->msg.len = recv_len_c;
if ((send_th->msg.flags & MSG_BUF_CALL_FLAGS))
{
th->msg.call_send = send_th; // 当前线程的callsend设置为发送方。
ipc->rcv = send_th; // 直接切换接收者,上下文切换之后发送者就变为接收者了
}
else
{
th->msg.call_send = NULL;
ipc->rcv = NULL; //! 如果不是call则清空接收者
}
thread_ready(send_th, TRUE); //!< 唤醒发送线程
spinlock_set(&ipc->lock, status);
return recv_len_c;
}
/**
* @brief ipc的系统调用
*
@@ -55,110 +268,12 @@ static msg_tag_t ipc_syscall(kobject_t *kobj, ram_limit_t *ram, entry_frame_t *f
{
case IPC_SEND:
{
umword_t status;
size_t send_len_c = -1;
void *send_addr = th->msg.msg;
th->msg.len = f->r[1];
if (!ipc->rcv)
{
status = spinlock_lock(&ipc->lock);
if (!ipc->rcv)
{
slist_add_append(&ipc->wait_send, &th->wait);
thread_suspend(th);
}
spinlock_set(&ipc->lock, status);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
return msg_tag_init3(0, 0, th->msg.len);
}
}
again_send:
if (ipc->rcv->status != THREAD_SUSPEND)
{
if (ipc->rcv->status == THREAD_DEAD)
{
return msg_tag_init3(0, 0, -EACCES);
}
status = spinlock_lock(&ipc->lock);
slist_add_append(&ipc->wait_send, &th->wait);
thread_suspend(th);
spinlock_set(&ipc->lock, status);
goto again_send;
}
else
{
status = spinlock_lock(&ipc->lock);
if (ipc->rcv->status == THREAD_SUSPEND)
{
send_len_c = MIN(THREAD_MSG_BUG_LEN, th->msg.len);
// 接收线程正在等待中
memcpy(ipc->rcv->msg.msg, th->msg.msg, send_len_c); //!< 拷贝数据
ipc->rcv->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
ipc->rcv->msg.len = send_len_c;
thread_ready(ipc->rcv, TRUE); //!< 直接唤醒接受者
ipc->rcv = NULL;
}
spinlock_set(&ipc->lock, status);
}
return msg_tag_init3(0, 0, send_len_c);
return msg_tag_init3(0, 0, ipc_send(ipc, f));
}
break;
case IPC_REVC:
{
if (!ipc->rcv)
{
umword_t status = spinlock_lock(&ipc->lock);
if (!ipc->rcv)
{
ipc->rcv = th;
}
else
{
spinlock_set(&ipc->lock, status);
return msg_tag_init3(0, 0, -EAGAIN);
}
spinlock_set(&ipc->lock, status);
}
void *recv_addr = th->msg.msg;
if (0)
{
return msg_tag_init3(0, 0, -EINVAL);
}
again_recv:
// 没有数据则睡眠
while (slist_is_empty(&ipc->wait_send))
{
thread_suspend(th);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
// 已经有数据了,直接返
return msg_tag_init3(0, 0, th->msg.len);
}
}
size_t recv_len_c = 0;
umword_t status = spinlock_lock(&ipc->lock);
if (slist_is_empty(&ipc->wait_send))
{
spinlock_set(&ipc->lock, status);
goto again_recv;
}
slist_head_t *mslist = slist_first(&ipc->wait_send);
slist_del(mslist);
thread_t *send_th = container_of(mslist, thread_t, wait);
recv_len_c = MIN(THREAD_MSG_BUG_LEN, send_th->msg.len);
memcpy(recv_addr, send_th->msg.msg, recv_len_c); //!< 拷贝数据
ipc->rcv = NULL;
send_th->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
send_th->msg.len = recv_len_c;
spinlock_set(&ipc->lock, status);
thread_ready(send_th, TRUE); //!< 唤醒发送线程
return msg_tag_init3(0, 0, recv_len_c);
return msg_tag_init3(0, 0, ipc_recv(ipc, f));
}
break;
}

View File

@@ -9,3 +9,4 @@ set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
add_subdirectory(sys)
add_subdirectory(libc_backend)
add_subdirectory(mlibc)
add_subdirectory(cpio)

View File

@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w \
-fPIC -n -pie -fpie -fpic -msingle-pic-base -mno-pic-data-is-text-relative \
-Wl,--gc-sections -D__dietlibc__ -D__arm__ -D__WORDSIZE=32 -D__ARM_ARCH_7M__ \
" )
set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
file(GLOB_RECURSE deps *.c *.S)
add_library(
cpio
STATIC
${deps}
)
target_include_directories(
cpio
PUBLIC
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/cpio/
${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
)
# target_link_libraries(
# cpio
# PUBLIC
# muslc
# )
# add_dependencies(cpio muslc)

View File

@@ -1,9 +1,8 @@
#include "u_types.h"
#include "cpiofs.h"
#include "u_util.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)
{

View File

@@ -0,0 +1,32 @@
#include "u_types.h"
typedef struct app_info
{
const char d[32];
const char magic[8];
union
{
struct exec_head_info
{
umword_t ram_size;
umword_t heap_offset;
umword_t stack_offset;
umword_t heap_size;
umword_t stack_size;
umword_t data_offset;
umword_t bss_offset;
umword_t got_start;
umword_t got_end;
umword_t rel_start;
umword_t rel_end;
umword_t text_start;
} i;
const char d1[128];
};
const char dot_text[];
} app_info_t;
static inline app_info_t *app_info_get(void *addr)
{
return (app_info_t *)addr;
}

View File

@@ -13,3 +13,4 @@
:); \
_val; \
})

View File

@@ -0,0 +1,8 @@
#pragma once
#include "u_types.h"
#define HANDLER_INVALID ((umword_t)(-1))
obj_handler_t handler_alloc(void);
void handler_free(obj_handler_t hd_inx);
void handler_free_umap(obj_handler_t hd_inx);

View File

@@ -1,5 +1,9 @@
#pragma once
#include "u_prot.h"
#define MSG_BUG_LEN 128
#define MSG_BUF_CALL_FLAGS 0x02U //!< 是CALL
msg_tag_t ipc_recv(obj_handler_t obj);
msg_tag_t ipc_send(obj_handler_t obj, umword_t len);
msg_tag_t ipc_send(obj_handler_t obj, umword_t len, umword_t flags);

View File

@@ -9,6 +9,8 @@
#define IPC_PROT 5
#define MM_PROT 6
#define TASK_THIS TASK_PROT
typedef struct msg_tag
{
union

View File

@@ -2,8 +2,8 @@
#include "u_types.h"
msg_tag_t thread_msg_bug_set(obj_handler_t obj, void *msg);
msg_tag_t thread_msg_bug_get(obj_handler_t obj, umword_t *msg, umword_t *len);
msg_tag_t thread_msg_buf_set(obj_handler_t obj, void *msg);
msg_tag_t thread_msg_buf_get(obj_handler_t obj, umword_t *msg, umword_t *len);
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,9 @@
#pragma once
#define MK_SET_BIT(a, b) ((a) |= 1 << (b)) //!< 设置BIT
#define MK_CLR_BIT(a, b) ((a) &= ~(1 << (b))) //!< 清除BIT
#define MK_GET_BIT(a, b) (((a) >> (b)) & 0x1) //!< 获取某位的bit
#define ABS(a) ((a) < 0 ? -(a) : (a)) //!< 取绝对值
#define MIN(a, b) ((a) < (b) ? (a) : (b)) //!< 最小值
#define ALIGN(mem, align) (((mem) + ((align)-1)) & (~((align)-1))) //!< 向上对齐
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) //!< 获取数组大小

View File

@@ -0,0 +1,51 @@
#include "u_types.h"
#include "u_arch.h"
#include "u_util.h"
#include "u_task.h"
#include "u_hd_man.h"
#define HANDLER_START_INX 10
#define HANDLER_MAX_NR 64
static umword_t bitmap_handler_alloc[HANDLER_MAX_NR / WORD_BYTES];
// TODO:加锁
obj_handler_t handler_alloc(void)
{
for (umword_t i = 0; i < ARRAY_SIZE(bitmap_handler_alloc); i++)
{
if (bitmap_handler_alloc[i] != (umword_t)(-1))
{
for (int j = 0; j < WORD_BITS; j++)
{
if (MK_GET_BIT(bitmap_handler_alloc[i], j) == 0)
{
// 找到空闲的
umword_t find_inx = i * WORD_BITS + j;
MK_SET_BIT(bitmap_handler_alloc[i], j);
return HANDLER_START_INX + find_inx;
}
}
}
}
return HANDLER_INVALID;
}
// TODO:加锁
void handler_free(obj_handler_t hd_inx)
{
hd_inx -= HANDLER_START_INX;
umword_t word_offset = hd_inx / WORD_BITS;
umword_t bits_offset = hd_inx % WORD_BITS;
MK_CLR_BIT(bitmap_handler_alloc[word_offset], bits_offset);
}
/**
* @brief 删除用户态的hd inx并释放内核中的内核对象
*
* @param hd_inx
*/
void handler_free_umap(obj_handler_t hd_inx)
{
task_unmap(TASK_THIS, hd_inx);
handler_free(hd_inx);
}

View File

@@ -9,7 +9,7 @@ msg_tag_t ipc_recv(obj_handler_t obj)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(IPC_REVC, 2, IPC_PROT).raw,
syscall(obj, msg_tag_init3(IPC_REVC, 0, IPC_PROT).raw,
0,
0,
0,
@@ -19,13 +19,13 @@ msg_tag_t ipc_recv(obj_handler_t obj)
return tag;
}
msg_tag_t ipc_send(obj_handler_t obj, umword_t len)
msg_tag_t ipc_send(obj_handler_t obj, umword_t len, umword_t flags)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(IPC_SEND, 2, IPC_PROT).raw,
len,
0,
flags,
0,
0,
0);

View File

@@ -9,7 +9,7 @@ enum thread_op
MSG_BUG_GET,
MSG_BUG_SET,
};
msg_tag_t thread_msg_bug_set(obj_handler_t obj, void *msg)
msg_tag_t thread_msg_buf_set(obj_handler_t obj, void *msg)
{
register volatile umword_t r0 asm("r0");
register volatile umword_t r1 asm("r1");
@@ -23,7 +23,7 @@ msg_tag_t thread_msg_bug_set(obj_handler_t obj, void *msg)
0);
return msg_tag_init(r0);
}
msg_tag_t thread_msg_bug_get(obj_handler_t obj, umword_t *msg, umword_t *len)
msg_tag_t thread_msg_buf_get(obj_handler_t obj, umword_t *msg, umword_t *len)
{
register volatile umword_t r0 asm("r0");
register volatile umword_t r1 asm("r1");

View File

@@ -9,12 +9,14 @@ target_link_libraries(init.elf
PUBLIC
muslc
sys
cpio
${GCC_LIB_PATH}/libgcc.a
)
target_include_directories(
init.elf
PUBLIC
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/cpio
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/arm/
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/generic

View File

@@ -7,205 +7,21 @@
#include "u_task.h"
#include <assert.h>
#include <stdio.h>
void ulog_test(void)
{
ulog_write_str(LOG_PROT, "Init task running..\n");
ulog_write_str(LOG_PROT, "todo..\n");
}
void mm_test(void)
{
void *mem = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem);
memset((char *)mem, 0, 1024);
void *mem1 = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem1);
memset(mem1, 0, 1024);
mm_free_page(MM_PROT, mem1, 2);
mm_free_page(MM_PROT, mem, 2);
// memset(mem, 0, 512);
// memset(mem1, 0, 512);
}
#include "u_ipc.h"
void thread_test_func(void)
{
char *buf;
umword_t len;
thread_msg_bug_get(11, (umword_t *)(&buf), NULL);
while (1)
{
msg_tag_t tag = ipc_recv(12);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
strcpy(buf, "reply");
ipc_send(12, strlen("reply"));
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, 11);
printf("Error\n");
}
void thread_test_func2(void)
{
char *buf;
umword_t len;
thread_msg_bug_get(10, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "1234");
ipc_send(12, strlen(buf));
msg_tag_t tag = ipc_recv(12);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, 10);
printf("Error\n");
}
void thread_test_func3(void)
{
char *buf;
umword_t len;
thread_msg_bug_get(13, (umword_t *)(&buf), NULL);
strcpy(buf, "____");
while (1)
{
ipc_send(12, strlen(buf));
}
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
{
const char d[32];
const char magic[8];
union
{
struct exec_head_info
{
umword_t ram_size;
umword_t heap_offset;
umword_t stack_offset;
umword_t heap_size;
umword_t stack_size;
umword_t data_offset;
umword_t bss_offset;
umword_t got_start;
umword_t got_end;
umword_t rel_start;
umword_t rel_end;
umword_t text_start;
} i;
const char d1[128];
};
const char dot_text[];
} app_info_t;
static char buf0[128];
static char buf1[128];
#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_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 1
thread_msg_bug_set(11, buf0);
thread_exec_regs(11, (umword_t)thread_test_func, (umword_t)val + 1024, RAM_BASE());
thread_bind_task(11, TASK_PROT);
thread_run(11);
factory_create_thread(FACTORY_PROT, 10);
thread_msg_bug_set(10, buf1);
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
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)
{
printf("factory_create_task no memory\n");
return;
}
tag = task_alloc_ram_base(10, app->i.ram_size, &ram_base);
if (msg_tag_get_prot(tag) < 0)
{
printf("task_alloc_ram_base no memory\n");
return;
}
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)addr, (umword_t)sp_addr_top, ram_base);
thread_bind_task(11, 10);
thread_run(11);
#endif
}
void mpu_test(void)
{
#if 0
// mpu保护测试
int *test = ((int *)0x8000000);
*test = 1;
printf("test is %d\n", *test);
#endif
}
void printf_test(void)
{
printf("print test0.\n");
printf("print test1.\n");
printf("print test2.\n");
float a = 1.1;
float b = 1.2;
float c = a + b;
// 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[])
{
ulog_write_str(LOG_PROT, "init..\n");
factory_test();
#if 0
mm_test();
mpu_test();
ulog_test();
#endif
factory_test();
thread_test();
app_test();
mpu_test();
printf_test();
#endif
ipc_test();
printf("exit init.\n");
while (1)
;

View File

@@ -0,0 +1,46 @@
#include "u_types.h"
#include "u_prot.h"
#include "u_app.h"
#include "u_factory.h"
#include "u_mm.h"
#include "u_task.h"
#include "u_hd_man.h"
#include "u_thread.h"
#include "cpiofs.h"
#include <assert.h>
/**
* @brief 加载一个app并启动
*
*/
void app_test(void)
{
msg_tag_t tag;
umword_t addr = cpio_find_file((umword_t)0x8020000, (umword_t)0x8040000, "shell");
assert(addr);
app_info_t *app = (app_info_t *)addr;
umword_t ram_base;
obj_handler_t hd_task = handler_alloc();
obj_handler_t hd_thread = handler_alloc();
assert(hd_task != HANDLER_INVALID);
assert(hd_thread != HANDLER_INVALID);
tag = factory_create_task(FACTORY_PROT, hd_task);
assert(msg_tag_get_prot(tag) >= 0);
tag = factory_create_thread(FACTORY_PROT, hd_thread);
assert(msg_tag_get_prot(tag) >= 0);
tag = task_alloc_ram_base(hd_task, app->i.ram_size, &ram_base);
assert(msg_tag_get_prot(tag) >= 0);
tag = task_map(hd_task, LOG_PROT, LOG_PROT);
assert(msg_tag_get_prot(tag) >= 0);
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;
tag = thread_exec_regs(hd_thread, (umword_t)addr, (umword_t)sp_addr_top, ram_base);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_bind_task(hd_thread, hd_task);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_run(hd_thread);
assert(msg_tag_get_prot(tag) >= 0);
}

View File

@@ -0,0 +1,14 @@
#include "u_types.h"
#include "u_prot.h"
#include "u_factory.h"
#include "u_task.h"
void factory_test(void)
{
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;
}
task_unmap(TASK_PROT, 12);
}

View File

@@ -0,0 +1,133 @@
#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 "u_ipc.h"
#include "u_hd_man.h"
#include <assert.h>
#include <stdio.h>
#define DEBUG_IPC_CALL 1
static umword_t th1_hd = 0;
static umword_t th2_hd = 0;
static umword_t ipc_hd = 0;
static char msg_buf0[MSG_BUG_LEN];
static char msg_buf1[MSG_BUG_LEN];
#define STACK_SIZE 1024
static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE];
static __attribute__((aligned(8))) uint8_t stack1[STACK_SIZE];
#if !DEBUG_IPC_CALL
static void thread_test_func(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(th1_hd, (umword_t *)(&buf), NULL);
while (1)
{
msg_tag_t tag = ipc_recv(ipc_hd);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
strcpy(buf, "reply");
ipc_send(ipc_hd, strlen("reply"));
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, th1_hd);
printf("Error\n");
}
static void thread_test_func2(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "1234");
ipc_send(ipc_hd, strlen(buf));
msg_tag_t tag = ipc_recv(ipc_hd);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, th2_hd);
printf("Error\n");
}
#else
static void thread_test_func(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(th1_hd, (umword_t *)(&buf), NULL);
while (1)
{
ipc_recv(ipc_hd);
printf(buf);
strcpy(buf, "hello thread2.\n");
ipc_send(ipc_hd, strlen(buf), 0);
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, th1_hd);
printf("Error\n");
}
static void thread_test_func2(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "hello thread1.\n");
ipc_send(ipc_hd, strlen(buf), MSG_BUF_CALL_FLAGS);
printf(buf);
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, th2_hd);
printf("Error\n");
}
#endif
/**
* @brief 启动两个线程并进行ipc测试
*
*/
void ipc_test(void)
{
th1_hd = handler_alloc();
assert(th1_hd != HANDLER_INVALID);
th2_hd = handler_alloc();
assert(th2_hd != HANDLER_INVALID);
ipc_hd = handler_alloc();
assert(ipc_hd != HANDLER_INVALID);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, ipc_hd);
assert(msg_tag_get_prot(tag) >= 0);
tag = factory_create_thread(FACTORY_PROT, th1_hd);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_msg_buf_set(th1_hd, msg_buf0);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_exec_regs(th1_hd, (umword_t)thread_test_func, (umword_t)stack0 + STACK_SIZE, RAM_BASE());
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_bind_task(th1_hd, TASK_THIS);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_run(th1_hd);
assert(msg_tag_get_prot(tag) >= 0);
tag = factory_create_thread(FACTORY_PROT, th2_hd);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_msg_buf_set(th2_hd, msg_buf1);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_exec_regs(th2_hd, (umword_t)thread_test_func2, (umword_t)stack1 + STACK_SIZE, RAM_BASE());
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_bind_task(th2_hd, TASK_THIS);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_run(th2_hd);
assert(msg_tag_get_prot(tag) >= 0);
}

View File

@@ -0,0 +1,8 @@
#include "u_types.h"
#include "u_prot.h"
void ulog_test(void)
{
ulog_write_str(LOG_PROT, "Init task running..\n");
ulog_write_str(LOG_PROT, "todo..\n");
}

View File

@@ -0,0 +1,18 @@
#include "u_types.h"
#include "u_prot.h"
#include "u_mm.h"
#include <assert.h>
void mm_test(void)
{
void *mem = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem);
memset((char *)mem, 0, 1024);
void *mem1 = mm_alloc_page(MM_PROT, 2, REGION_RWX);
assert(mem1);
memset(mem1, 0, 1024);
mm_free_page(MM_PROT, mem1, 2);
mm_free_page(MM_PROT, mem, 2);
// memset(mem, 0, 512);
// memset(mem1, 0, 512);
}

View File

@@ -0,0 +1,12 @@
#include "u_types.h"
void mpu_test(void)
{
#if 0
// mpu保护测试
int *test = ((int *)0x8000000);
*test = 1;
printf("test is %d\n", *test);
#endif
}

View File

@@ -0,0 +1,14 @@
#include "u_types.h"
#include <stdio.h>
void printf_test(void)
{
printf("print test0.\n");
printf("print test1.\n");
printf("print test2.\n");
float a = 1.1;
float b = 1.2;
float c = a + b;
// printf("%c %d %f\n", 'a', 1234, 1.1);
// c = c;
// printf("%c %d %lf\n", 'a', 1234, 1.1); 浮点打印有问题
}

View File

@@ -0,0 +1,11 @@
#pragma once
// TODO:修改为init call
void mm_test(void);
void ulog_test(void);
void factory_test(void);
void thread_test(void);
void app_test(void);
void mpu_test(void);
void printf_test(void);
void ipc_test(void);

View File

@@ -0,0 +1,85 @@
#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 "u_ipc.h"
#include <assert.h>
#include <stdio.h>
static char msg_buf0[MSG_BUG_LEN];
static char msg_buf1[MSG_BUG_LEN];
#define STACK_SIZE 1024
static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE];
static __attribute__((aligned(8))) uint8_t stack1[STACK_SIZE];
static void thread_test_func(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(11, (umword_t *)(&buf), NULL);
while (1)
{
msg_tag_t tag = ipc_recv(12);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
strcpy(buf, "reply");
ipc_send(12, strlen("reply"), 0);
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, 11);
printf("Error\n");
}
static void thread_test_func2(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(10, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "1234");
ipc_send(12, strlen(buf), 0);
msg_tag_t tag = ipc_recv(12);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, 10);
printf("Error\n");
}
/**
* @brief 启动两个线程并进行ipc测试
*
*/
void thread_test(void)
{
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;
}
thread_msg_buf_set(11, msg_buf0);
thread_exec_regs(11, (umword_t)thread_test_func, (umword_t)stack0 + STACK_SIZE, RAM_BASE());
thread_bind_task(11, TASK_PROT);
thread_run(11);
factory_create_thread(FACTORY_PROT, 10);
thread_msg_buf_set(10, msg_buf1);
thread_exec_regs(10, (umword_t)thread_test_func2, (umword_t)stack1 + STACK_SIZE, RAM_BASE());
thread_bind_task(10, TASK_PROT);
thread_run(10);
}

View File

@@ -3,7 +3,7 @@
export KNL=mkrtos
export KEN_OFFSET=0x2000
export INIT_OFFSET=0x10000
export BOOTFS_ADDR_OFFSET=0x1ff8c
export BOOTFS_ADDR_OFFSET=0x20000
export KNL_TEXT=0x8000000
export KNL_DATA=0x20000000
export KNL_DATA_SIZE=64K