严格代码检查,修复大量错误,以及diskio.c ioctl参数大小导致的卡死问题

This commit is contained in:
zhangzheng
2025-03-23 17:35:56 +08:00
parent 749e24ddb7
commit 54b6bc9c24
139 changed files with 1317 additions and 1077 deletions

View File

@@ -470,7 +470,7 @@ static void print_call_stack(uint32_t sp)
if (cur_depth)
{
cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
cmb_println(print_info[PRINT_CALL_STACK_INFO], kobject_get_name(&thread_get_current_task()->kobj), CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
call_stack_info);
}
else

View File

@@ -117,7 +117,7 @@ void arch_init(void)
SCB->SHCSR |= SCB_SHCSR_BUSFAULTENA_Msk;
((uint8_t *)(0xE000E008))[0] |= 0x6;
cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
cm_backtrace_init("mkrtos", HARDWARE_VERSION, SOFTWARE_VERSION);
// RCC_ClocksTypeDef RCC_ClocksStatus;
// RCC_GetClocksFreq(&RCC_ClocksStatus);

View File

@@ -25,6 +25,7 @@ void putc(int c);
* @return int
*/
int getc(void);
void put_bytes(const uint8_t *data, size_t len);
/**
* @brief 打印内核信息
*

View File

@@ -1,4 +1,4 @@
#pragma
#pragma once
#include "thread.h"
typedef struct thread_wait_entry

View File

@@ -11,7 +11,7 @@ extern "C"
#endif
#include "stdarg.h"
#define XF_USE_OUTPUT 1 /* 1: Enable output functions */
#define XF_CRLF 1 /* 1: Convert \n ==> \r\n in the output char */
#define XF_CRLF 0 /* 1: Convert \n ==> \r\n in the output char */
#define XF_USE_DUMP 0 /* 1: Enable put_dump function */
#define XF_USE_LLI 0 /* 1: Enable long long integer in size prefix ll */
#define XF_USE_FP 0 /* 1: Enable support for floating point in type e and f */

View File

@@ -15,7 +15,7 @@
#include <util.h>
/**
* @brief 检测内存是否可访问
*
* TODO: 需要检查动态申请的内存
* @param tg_task 目标进程
* @param addr 开始地址
* @param size 大小

View File

@@ -518,7 +518,7 @@ static int futex_lock_pi(thread_t *cur_th, futex_t *fst, uint32_t *uaddr, uint32
spinlock_set(&fst->kobj.lock, status);
return -EACCES;
}
if (*paddr = 0)
if ((*paddr) == 0)
{
*paddr = tid;
}

View File

@@ -56,19 +56,10 @@ static void log_reg(void)
INIT_KOBJ(log_reg);
static msg_tag_t log_write_data(log_t *log, const char *data, int len)
{
for (int i = 0; i < len && data[i]; i++)
{
#if 1
if (data[i] == '\n')
{
putc('\r');
}
#endif
putc(data[i]);
}
put_bytes((const uint8_t *)data, len);
return msg_tag_init(0);
}
static int log_read_data(log_t *log, uint8_t *data, int len)
static int log_read_data(log_t *log, uint8_t *data, int len, int flags)
{
for (int i = 0; i < len; i++)
{
@@ -77,7 +68,7 @@ static int log_read_data(log_t *log, uint8_t *data, int len)
if (c < 0 && i == 0)
{
int ret = irq_sender_wait(&log->kobj, thread_get_current(), 0);
int ret = irq_sender_wait(&log->kobj, thread_get_current(), flags);
if (ret < 0)
{
return ret;
@@ -108,7 +99,7 @@ log_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_tag, entry_frame
break;
case READ_DATA:
{
int ret = log_read_data((log_t *)kobj, (uint8_t *)(&f->regs[1]), MIN(f->regs[1], WORD_BYTES * 3));
int ret = log_read_data((log_t *)kobj, (uint8_t *)(&f->regs[1]), MIN(f->regs[1], WORD_BYTES * 3), f->regs[2]);
tag = msg_tag_init4(0, 0, 0, ret);
}
break;

View File

@@ -66,6 +66,7 @@ int obj_map_src_dst(obj_space_t *dst_space, obj_space_t *src_space,
if (obj_space_lookup_kobj(dst_space, dst_inx))
{ //!< 已经存在则解除注释
obj_unmap(dst_space, vpage_create3(0, 0, dst_inx), del_list);
printk("unmap:%d \n", dst_inx);
}
return obj_map(dst_space, dst_inx, obj_map_entry_kobj_get(entry_obj),

View File

@@ -191,7 +191,6 @@ void mem_free_align(mem_t *_this, void *f_mem)
{
struct mem_heap *mem;
umword_t *real_mem;
int find = 0;
umword_t status = spinlock_lock(&_this->lock);
for (mem = _this->heap_start; mem != _this->heap_end; mem = mem->next)
@@ -199,7 +198,6 @@ void mem_free_align(mem_t *_this, void *f_mem)
assert(mem->magic == MAGIC_NUM);
if ((ptr_t)mem == (ptr_t)f_mem - MEM_HEAP_STRUCT_SIZE)
{
find = 1;
break;
}
}

View File

@@ -294,6 +294,7 @@ int task_vma_alloc(task_vma_t *task_vma, vma_addr_t vaddr, size_t size,
int ret = 0;
size_t mem_align_size;
bool_t is_alloc_mem = FALSE;
mword_t status = -1;
pt_task = container_of(
container_of(task_vma, mm_space_t, mem_vma),
@@ -321,7 +322,7 @@ int task_vma_alloc(task_vma_t *task_vma, vma_addr_t vaddr, size_t size,
ret = -EINVAL;
goto err_end;
}
mword_t status = task_vma_lock(task_vma);
status = task_vma_lock(task_vma);
if (paddr == 0)
{
@@ -410,7 +411,10 @@ err_end:
mm_limit_free_align(pt_task->lim, (void *)vma_addr, mem_align_size);
}
end:
task_vma_unlock(task_vma, status);
if (status >= 0)
{
task_vma_unlock(task_vma, status);
}
return ret;
}
/**
@@ -456,9 +460,6 @@ static int vma_idl_tree_eq_cmp_handler(const void *key, const void *data)
return 1;
}
}
static inline int task_vma_addr_check(vaddr_t addr, size_t size)
{
}
static int task_vma_free_inner(task_vma_t *task_vma, vaddr_t vaddr, size_t size, bool_t is_free_mem)
{
addr_t vma_addr;
@@ -535,7 +536,7 @@ end:
mpu_switch_to_task(pt_task);
}
task_vma_unlock(task_vma, status);
return 0;
return ret;
}
/**
* @brief 释放申请的虚拟内存,并释放已经申请的物理内存
@@ -700,7 +701,7 @@ int task_vma_clean(task_vma_t *task_vma)
.tree = &task_vma->alloc_tree,
}; // 删除所有的节点
ret = mln_rbtree_iterate(&task_vma->alloc_tree, rbtree_iterate_handler_delete, &params);
return 0;
return ret;
}
#if IS_ENABLED(CONFIG_MPU)

View File

@@ -39,6 +39,18 @@ void putc(int c)
{
uart_putc(uart_get_global(), c);
}
void put_bytes(const uint8_t *data, size_t len)
{
umword_t state = 0;
state = spinlock_lock(&lock);
for (int i = 0; i < len; i++)
{
uart_putc(uart_get_global(), data[i]);
}
spinlock_set(&lock, state);
}
int getc(void)
{
return uart_getc(uart_get_global());

View File

@@ -48,7 +48,6 @@ void sema_up(sema_t *obj)
thread_t *th = thread_get_current();
ref_counter_inc(&obj->ref);
again:
status = spinlock_lock(&obj->lock);
if (slist_is_empty(&obj->suspend_head))
{

View File

@@ -453,7 +453,7 @@ static void share_mem_unmap_op(task_t *tk, share_mem_t *sm)
}
static void share_mem_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_tag, entry_frame_t *f)
{
ssize_t ret;
ssize_t ret = 0;
msg_tag_t tag = msg_tag_init4(0, 0, 0, -EINVAL);
task_t *task = thread_get_current_task();
share_mem_t *sm = container_of(kobj, share_mem_t, kobj);

View File

@@ -35,6 +35,7 @@ enum task_op_code
TASK_OBJ_MAP, //!< 进行映射操作
TASK_OBJ_UNMAP, //!< 进行解除映射操作
TASK_ALLOC_RAM_BASE, //!< 分配task的基础内存
TASK_GET_RAM_INFO, //!< 获取task的ram信息
TASK_OBJ_VALID, //!< 判断一个对象是否有效
TASK_SET_PID, //!< 设置task的pid
TASK_GET_PID, //!< 获取task的pid
@@ -120,10 +121,12 @@ static void task_unlock_2(spinlock_t *sp0, spinlock_t *sp1, int status0, int sta
spinlock_set(sp1, status1);
spinlock_set(sp0, status0);
}
else
else if (sp0 > sp1)
{
spinlock_set(sp0, status0);
spinlock_set(sp1, status1);
} else {
spinlock_set(sp0, status0);
}
}
/**
@@ -155,7 +158,7 @@ static int task_lock_2(spinlock_t *sp0, spinlock_t *sp1, int *st0, int *st1)
*st0 = status0;
*st1 = status1;
}
else
else if (sp0 > sp1)
{
status0 = spinlock_lock(sp1);
if (status0 < 0)
@@ -170,6 +173,14 @@ static int task_lock_2(spinlock_t *sp0, spinlock_t *sp1, int *st0, int *st1)
}
*st0 = status1;
*st1 = status0;
} else {
status0 = spinlock_lock(sp0);
if (status0 < 0)
{
return FALSE;
}
*st0 = status0;
*st1 = status0;
}
return TRUE;
}
@@ -315,6 +326,19 @@ static void task_syscall_func(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t i
spinlock_set(&tag_task->kobj.lock, status);
}
break;
case TASK_GET_RAM_INFO:
{
mword_t status = spinlock_lock(&tag_task->kobj.lock);
if (status < 0)
{
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
mm_space_get_ram_block(&tag_task->mm_space, (void**)(&f->regs[1]), (size_t *)(&f->regs[2]));
spinlock_set(&tag_task->kobj.lock, status);
tag = msg_tag_init4(0, 0, 0, 0);
}
break;
#endif
case TASK_COPY_DATA: //!< 拷贝数据到task的内存区域
{
@@ -371,7 +395,7 @@ static void task_syscall_func(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t i
if (!source_kobj)
{
ret = -EINVAL;
goto copy_data_to_end;
goto copy_data_to_end2;
}
task_t *dst_task_obj = container_of(source_kobj, task_t, kobj);
@@ -381,11 +405,13 @@ static void task_syscall_func(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t i
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
#if 0/*TODO: 需要检查动态申请的内存*/
if (!is_rw_access(tag_task, (void *)src_addr, copy_len, FALSE))
{
ret = -EPERM;
goto copy_data_to_end;
}
#endif
if (!is_rw_access(dst_task_obj, (void *)dst_addr, copy_len, FALSE))
{
ret = -EPERM;
@@ -399,6 +425,7 @@ static void task_syscall_func(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t i
copy_data_to_end:
task_unlock_2(&tag_task->kobj.lock, &dst_task_obj->kobj.lock, st0, st1);
copy_data_to_end2:
tag = msg_tag_init4(0, 0, 0, ret);
}
break;
@@ -524,6 +551,8 @@ static void task_release_stage1(kobject_t *kobj)
pf_t *cur_pf = ((pf_t *)((char *)restore_th + CONFIG_THREAD_BLOCK_SIZE + 8)) - 1;
cur_pf->regs[5] = (umword_t)(thread_get_bind_task(restore_th)->mm_space.mm_block);
ref_counter_dec_and_release(&tk->ref_cn, &tk->kobj);
} else {
break;
}
}

View File

@@ -138,6 +138,7 @@ void thread_init(thread_t *th, ram_limit_t *lim, umword_t flags)
ref_counter_inc(&th->ref);
thread_arch_init(th, flags);
kobject_set_name(&th->kobj, kobject_get_name(&th->kobj));
slist_init(&th->com->fast_ipc_node);
stack_init(&th->com->fast_ipc_stack, &th->com->fast_ipc_stack_data,
ARRARY_LEN(th->com->fast_ipc_stack_data),
@@ -470,176 +471,6 @@ thread_t *thread_create(ram_limit_t *ram, umword_t flags)
printk("create thread 0x%x\n", th);
return th;
}
#if 0
/**
* @brief 线程超时检查
*
* @param tick
*/
void thread_timeout_check(ssize_t tick)
{
thread_wait_entry_t *pos;
slist_head_t *head =
(slist_head_t *)pre_cpu_get_current_cpu_var(&wait_send_queue);
slist_foreach_not_next(pos, head, node_timeout)
{
if (pos->th->status != THREAD_SUSPEND)
{
printk("%s:%d th_name:%s.\n", __func__, __LINE__, pos->th->kobj.dbg.name);
}
assert(pos->th->status == THREAD_SUSPEND);
thread_wait_entry_t *next = slist_next_entry(
pos, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_send_queue),
node_timeout);
if (pos->times > 0)
{
pos->times -= tick;
if (pos->times <= 0)
{
pos->th->ipc_status = THREAD_TIMEOUT;
slist_del(&pos->node_timeout);
if (slist_in_list(&pos->node))
{
slist_del(&pos->node);
}
// printk("send timeout:0x%lx\n", pos->th);
thread_ready(pos->th, TRUE);
}
}
pos = next;
}
thread_wait_entry_t *pos2;
slist_foreach_not_next(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_recv_queue),
node)
{
assert(pos2->th->status == THREAD_SUSPEND);
thread_wait_entry_t *next = slist_next_entry(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_recv_queue),
node);
if (pos2->times > 0)
{
pos2->times -= tick;
if (pos2->times <= 0)
{
pos2->th->ipc_status = THREAD_TIMEOUT;
assert(slist_in_list(&pos2->node));
slist_del(&pos2->node);
// printk("recv timeout:0x%lx\n", pos2->th);
thread_ready(pos2->th, TRUE);
}
}
pos2 = next;
}
}
static void thread_timeout_del_form_send_queue(thread_t *th)
{
thread_wait_entry_t *pos2;
assert(th->cpu == arch_get_current_cpu_id());
slist_foreach_not_next(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_send_queue),
node_timeout)
{
thread_wait_entry_t *next = slist_next_entry(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_send_queue),
node_timeout);
if (pos2->th == th)
{
slist_del(&pos2->node_timeout);
if (slist_in_list(&pos2->node))
{
slist_del(&pos2->node);
}
break;
}
pos2 = next;
}
}
#if IS_ENABLED(CONFIG_SMP)
static int thread_timeout_del_form_send_queue_handler(ipi_msg_t *msg, bool_t *is_sched)
{
thread_timeout_del_form_send_queue((void *)(msg->msg));
thread_ready((void *)(msg->msg), TRUE); //!< 直接唤醒接受者
return 0;
}
#endif
void thread_timeout_del_from_send_queue_remote(thread_t *th)
{
#if IS_ENABLED(CONFIG_SMP)
if (th->cpu == arch_get_current_cpu_id())
{
thread_timeout_del_form_send_queue(th);
thread_ready(th, TRUE); //!< 直接唤醒接受者
}
else
{
thread_t *cur_th = thread_get_current();
cur_th->ipi_msg_node.msg = (umword_t)th;
cur_th->ipi_msg_node.cb = thread_timeout_del_form_send_queue_handler;
cpu_ipi_to_msg(1 << th->cpu, &cur_th->ipi_msg_node, IPI_CALL);
}
#else
thread_timeout_del_form_send_queue(th);
thread_ready(th, TRUE); //!< 直接唤醒接受者
#endif
}
static void thread_timeout_del_recv(thread_t *th)
{
thread_wait_entry_t *pos2;
assert(th->cpu == arch_get_current_cpu_id());
slist_foreach_not_next(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_recv_queue),
node)
{
thread_wait_entry_t *next = slist_next_entry(
pos2, (slist_head_t *)pre_cpu_get_current_cpu_var(&wait_recv_queue),
node);
if (pos2->th == th)
{
slist_del(&pos2->node);
break;
}
pos2 = next;
}
}
#if IS_ENABLED(CONFIG_SMP)
static int thread_timeout_del_recv_remote_handler(ipi_msg_t *msg, bool_t *is_sched)
{
thread_t *th = (void *)(msg->msg);
thread_timeout_del_recv(th);
thread_ready(th, TRUE); //!< 直接唤醒接受者
return 0;
}
#endif
void thread_timeout_del_recv_remote(thread_t *th, bool_t is_sche)
{
#if IS_ENABLED(CONFIG_SMP)
if (th->cpu == arch_get_current_cpu_id())
{
thread_timeout_del_recv(th);
thread_ready(th, is_sche); //!< 直接唤醒接受者
}
else
{
thread_t *cur_th = thread_get_current();
cur_th->ipi_msg_node.msg = (umword_t)th;
cur_th->ipi_msg_node.cb = thread_timeout_del_recv_remote_handler;
cpu_ipi_to_msg(1 << th->cpu, &cur_th->ipi_msg_node, IPI_CALL);
}
#else
thread_timeout_del_recv(th);
thread_ready(th, is_sche); //!< 直接唤醒接受者
#endif
}
#endif
static int ipc_dat_copy_raw(obj_space_t *dst_obj, obj_space_t *src_obj, ram_limit_t *lim,
ipc_msg_t *dst_ipc, ipc_msg_t *src_ipc, msg_tag_t tag, int is_reply)
@@ -1362,58 +1193,6 @@ msg_tag_t thread_do_ipc(kobject_t *kobj, entry_frame_t *f, umword_t user_id)
return thread_fast_ipc_call(to_tk, f, user_id);
}
break;
#if 0
case IPC_CALL:
{
msg_tag_t in_tag = msg_tag_init(f->regs[0]);
msg_tag_t recv_tag;
th_hd = f->regs[2];
ipc_timeout_t ipc_tm_out = ipc_timeout_create(f->regs[3]);
to_th->user_id = user_id;
ret = thread_ipc_call(to_th, in_tag, &recv_tag, ipc_tm_out, &f->regs[1],
TRUE);
if (ret < 0)
{
return msg_tag_init4(0, 0, 0, ret);
}
return recv_tag;
}
case IPC_REPLY:
{
msg_tag_t in_tag = msg_tag_init(f->regs[0]);
ret = thread_ipc_reply(in_tag);
return msg_tag_init4(0, 0, 0, ret);
}
case IPC_RECV:
case IPC_WAIT:
{
msg_tag_t ret_msg;
ipc_timeout_t ipc_tm_out = ipc_timeout_create(f->regs[3]);
kobject_t *ipc_kobj = obj_space_lookup_kobj_cmp_type(
&cur_task->obj_space, f->regs[4], IPC_TYPE);
int ret = thread_ipc_recv(&ret_msg, ipc_tm_out, &f->regs[1],
(ipc_t *)ipc_kobj, NULL);
if (ret < 0)
{
return msg_tag_init4(0, 0, 0, ret);
}
return ret_msg;
}
case IPC_SEND:
{
msg_tag_t in_tag = msg_tag_init(f->regs[0]);
msg_tag_t recv_tag;
// th_hd = f->regs[2];
ipc_timeout_t ipc_tm_out = ipc_timeout_create(f->regs[3]);
to_th->user_id = user_id;
ret = thread_ipc_call(to_th, in_tag, NULL, ipc_tm_out, NULL, FALSE);
return msg_tag_init4(0, 0, 0, ret);
}
#endif
default:
ret = -ENOSYS;
break;
@@ -1611,6 +1390,7 @@ static void thread_syscall(kobject_t *kobj, syscall_prot_t sys_p,
(tge_prio >= PRIO_MAX ? PRIO_MAX - 1 : tge_prio);
thread_ready(tag_th, TRUE);
}
goto run_thread_end;
#endif
run_thread_end:
cpulock_set(status);

View File

@@ -48,8 +48,8 @@ static task_t knl_task;
static thread_t *init_thread;
static task_t *init_task;
static thread_t *knl_thread[CONFIG_CPU];
static slist_head_t del_task_head; //!<链表中是需要被删除的进程
static slist_head_t del_thread_head;//!<链表中是需要被释放内存的线程
static slist_head_t del_task_head; //!< 链表中是需要被删除的进程
static slist_head_t del_thread_head; //!< 链表中是需要被释放内存的线程
static umword_t cpu_usage[CONFIG_CPU];
static spinlock_t del_lock;
static umword_t cpu_usage_last_tick_val[CONFIG_CPU];
@@ -63,19 +63,15 @@ static void knl_release_thread(void)
if (slist_is_empty(&del_thread_head))
{
spinlock_set(&del_lock, status2);
return ;
return;
}
slist_foreach_not_next(pos, &del_thread_head, release_node)
{
thread_t *next = slist_next_entry(pos, &del_thread_head, release_node);
int ret;
// printk("+++++release th:0x%x\n", pos);
ret = ref_counter_dec_and_release(&pos->ref, &pos->kobj);
// if (ret == 1)
// {
// printk("------release th:0x%x\n", pos);
// }
ref_counter_dec_and_release(&pos->ref, &pos->kobj);
// printk("------release th:0x%x\n", pos);
slist_del(&pos->release_node);
pos = next;
}
@@ -90,7 +86,7 @@ static void knl_del_task(void)
if (slist_is_empty(&del_task_head))
{
spinlock_set(&del_lock, status2);
return ;
return;
}
// 在这里删除进程
slist_foreach_not_next(pos, &del_task_head, del_node)
@@ -305,7 +301,8 @@ bool_t task_knl_kill(thread_t *kill_thread, bool_t is_knl)
task_t *task = container_of(kill_thread->task, task_t, kobj);
if (!is_knl)
{
printk("kill %s th:0x%x task:0x%x, pid:%d\n", kobject_get_name(&task->kobj), kill_thread, task, task->pid);
printk("kill task:0x%x-->%s th:0x%x-->%s, pid:%d\n", task, kobject_get_name(&task->kobj), kill_thread,
kobject_get_name(&kill_thread->kobj), task->pid);
umword_t status2;
status2 = spinlock_lock(&del_lock);