Files
mkrtos-real/mkrtos_knl/knl/task.c

645 lines
17 KiB
C
Raw Normal View History

2023-09-29 01:03:19 +08:00
/**
* @file task.c
* @author ATShining (1358745329@qq.com)
* @brief
2023-09-29 01:03:19 +08:00
* @version 0.1
* @date 2023-09-29
*
2023-09-29 01:03:19 +08:00
* @copyright Copyright (c) 2023
*
2023-09-29 01:03:19 +08:00
*/
2023-08-20 20:52:23 +08:00
#include "task.h"
#include "access.h"
#include "assert.h"
2023-08-20 20:52:23 +08:00
#include "factory.h"
#include "init.h"
#include "knl_misc.h"
#include "kobject.h"
#include "map.h"
#include "printk.h"
2023-09-02 00:18:54 +08:00
#include "spinlock.h"
2023-11-19 23:01:35 +08:00
#include "string.h"
#include "thread.h"
#include "thread_arch.h"
2024-12-27 08:23:26 +08:00
#include "thread_task_arch.h"
2024-04-01 16:10:59 +00:00
#if IS_ENABLED(CONFIG_MMU)
2024-04-07 14:05:03 +00:00
#include "arch.h"
2024-04-01 16:10:59 +00:00
#endif
/**
* @brief
*
*/
2024-09-19 23:30:54 +08:00
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
2024-02-04 22:24:56 +08:00
TASK_COPY_DATA, //!< 拷贝数据到task的数据区域
TASK_SET_OBJ_NAME, //!< 设置对象的名字
TASK_COPY_DATA_TO, //!< 从当前task拷贝数据到目的task
TASK_SET_COM_POINT, //!< 通信点
2024-12-27 08:23:26 +08:00
TASK_COM_UNLOCK, //!< 任务通信解锁
TASK_COM_LOCK, //!< 任务通信加锁
2023-08-20 20:52:23 +08:00
};
2023-08-28 22:11:49 +08:00
static bool_t task_put(kobject_t *kobj);
static void task_release_stage1(kobject_t *kobj);
static void task_release_stage2(kobject_t *kobj);
2024-03-31 16:06:11 +00:00
#if IS_ENABLED(CONFIG_BUDDY_SLAB)
#include <slab.h>
static slab_t *task_slab;
#endif
2024-03-31 16:06:11 +00:00
/**
* @brief task的内存
*
*/
static void task_mem_init(void)
{
#if IS_ENABLED(CONFIG_BUDDY_SLAB)
task_slab = slab_create(sizeof(task_t), "task");
assert(task_slab);
#endif
}
INIT_KOBJ_MEM(task_mem_init);
#if !IS_ENABLED(CONFIG_MMU)
/**
* @brief task分配其可以使用的内存空间
*
* @param tk
* @param lim
* @param size
2024-12-29 21:50:20 +08:00
* @param mem_block 使
* @return int
*/
2024-12-29 21:50:20 +08:00
int task_alloc_base_ram(task_t *tk, ram_limit_t *lim, size_t size, int mem_block)
{
2024-09-19 23:30:54 +08:00
if (tk->mm_space.mm_block)
{
return -EACCES;
}
// 申请init的ram内存
2024-12-29 21:50:20 +08:00
void *ram = mpu_ram_alloc(&tk->mm_space, lim, size + THREAD_MSG_BUG_LEN, mem_block);
2024-09-19 23:30:54 +08:00
if (!ram)
{
printk("Failed to request process memory.\n");
return -ENOMEM;
}
2023-11-19 23:01:35 +08:00
memset(ram, 0, size + THREAD_MSG_BUG_LEN);
2023-09-03 15:55:06 +08:00
mm_space_set_ram_block(&tk->mm_space, ram, size + THREAD_MSG_BUG_LEN);
printk("task alloc [0x%x - 0x%x]\n", ram, (umword_t)ram + size + THREAD_MSG_BUG_LEN - 1);
return 0;
}
#endif
/**
* @brief 线task
*
* @param th
* @return task_t*
*/
2023-09-03 15:55:06 +08:00
task_t *thread_get_bind_task(thread_t *th)
{
return container_of(th->task, task_t, kobj);
}
/**
* @brief
*
* @param sp0
* @param sp1
* @param status0
* @param status1
*/
static void task_unlock_2(spinlock_t *sp0, spinlock_t *sp1, int status0, int status1)
{
2024-09-19 23:30:54 +08:00
if (sp0 < sp1)
{
spinlock_set(sp1, status1);
spinlock_set(sp0, status0);
2024-09-19 23:30:54 +08:00
}
else if (sp0 > sp1)
2024-09-19 23:30:54 +08:00
{
spinlock_set(sp0, status0);
spinlock_set(sp1, status1);
} else {
spinlock_set(sp0, status0);
}
}
/**
* @brief
*
* @param sp0
* @param sp1
* @param st0
* @param st1
* @return int
*/
static int task_lock_2(spinlock_t *sp0, spinlock_t *sp1, int *st0, int *st1)
{
int status0;
int status1;
2024-09-19 23:30:54 +08:00
if (sp0 < sp1)
{
status0 = spinlock_lock(sp0);
2024-09-19 23:30:54 +08:00
if (status0 < 0)
{
return FALSE;
}
status1 = spinlock_lock(sp1);
2024-09-19 23:30:54 +08:00
if (status1 < 0)
{
spinlock_set(sp0, status0);
return FALSE;
}
*st0 = status0;
*st1 = status1;
2024-09-19 23:30:54 +08:00
}
else if (sp0 > sp1)
2024-09-19 23:30:54 +08:00
{
status0 = spinlock_lock(sp1);
2024-09-19 23:30:54 +08:00
if (status0 < 0)
{
return FALSE;
}
status1 = spinlock_lock(sp0);
2024-09-19 23:30:54 +08:00
if (status1 < 0)
{
spinlock_set(sp1, status0);
return FALSE;
}
*st0 = status1;
*st1 = status0;
} else {
status0 = spinlock_lock(sp0);
if (status0 < 0)
{
return FALSE;
}
*st0 = status0;
*st1 = status0;
}
return TRUE;
}
/**
* @brief pidpid为0的进程才能够设置
*
* @param task
* @param pid pid
* @return int
*/
2023-12-01 22:18:10 +08:00
int task_set_pid(task_t *task, pid_t pid)
{
task_t *cur_task = thread_get_current_task();
2024-09-19 23:30:54 +08:00
if (cur_task->pid == 0)
{
2023-12-01 22:18:10 +08:00
task->pid = pid;
return 0;
2024-09-19 23:30:54 +08:00
}
else
{
2023-12-01 22:18:10 +08:00
return -EACCES;
}
}
/**
* @brief
*
* @param kobj
* @param sys_p
* @param in_tag
* @param f
*/
2023-09-03 15:55:06 +08:00
static void task_syscall_func(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_tag, entry_frame_t *f)
2023-08-20 20:52:23 +08:00
{
2023-08-28 22:11:49 +08:00
task_t *cur_task = thread_get_current_task();
task_t *tag_task = container_of(kobj, task_t, kobj);
2023-09-07 23:47:34 +08:00
msg_tag_t tag = msg_tag_init4(0, 0, 0, -EINVAL);
2023-08-20 20:52:23 +08:00
2024-09-19 23:30:54 +08:00
if (sys_p.prot != TASK_PROT)
{
2024-04-04 16:51:29 +00:00
f->regs[0] = msg_tag_init4(0, 0, 0, -EINVAL).raw;
2023-09-03 15:55:06 +08:00
return;
2023-08-20 20:52:23 +08:00
}
2024-09-19 23:30:54 +08:00
switch (sys_p.op)
{
case TASK_SET_OBJ_NAME:
{
mword_t status = spinlock_lock(&tag_task->kobj.lock);
2024-09-19 23:30:54 +08:00
if (status < 0)
{
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
kobject_t *source_kobj = obj_space_lookup_kobj(&cur_task->obj_space, f->regs[0]);
2024-09-19 23:30:54 +08:00
if (!source_kobj)
{
spinlock_set(&tag_task->kobj.lock, status);
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
kobject_set_name(source_kobj, (char *)(&f->regs[1]));
spinlock_set(&tag_task->kobj.lock, status);
tag = msg_tag_init4(0, 0, 0, 1);
2024-09-19 23:30:54 +08:00
}
break;
case TASK_OBJ_VALID: //!< 查看某个obj在task中是否存在
2023-09-07 23:47:34 +08:00
{
mword_t status = spinlock_lock(&tag_task->kobj.lock);
2024-09-19 23:30:54 +08:00
if (status < 0)
{
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
2024-04-04 16:51:29 +00:00
kobject_t *source_kobj = obj_space_lookup_kobj(&cur_task->obj_space, f->regs[1]);
2023-09-07 23:47:34 +08:00
2024-09-19 23:30:54 +08:00
if (!source_kobj)
{
spinlock_set(&tag_task->kobj.lock, status);
2023-12-10 00:26:10 +08:00
tag = msg_tag_init4(0, 0, 0, 0);
2023-09-07 23:47:34 +08:00
break;
}
2024-04-04 16:51:29 +00:00
f->regs[1] = source_kobj->kobj_type;
spinlock_set(&tag_task->kobj.lock, status);
2023-09-07 23:47:34 +08:00
tag = msg_tag_init4(0, 0, 0, 1);
2024-09-19 23:30:54 +08:00
}
break;
2023-12-10 10:20:05 +08:00
case TASK_OBJ_MAP: //!< 从一个task中映射一个对象到目标进程
{
2023-09-21 21:44:17 +08:00
kobj_del_list_t del;
int st0, st1;
2023-09-21 21:44:17 +08:00
kobj_del_list_init(&del);
int suc = task_lock_2(&tag_task->kobj.lock, &cur_task->kobj.lock, &st0, &st1);
2024-09-19 23:30:54 +08:00
if (!suc)
{
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
2023-09-09 14:39:48 +08:00
int ret = obj_map_src_dst(&tag_task->obj_space, &cur_task->obj_space,
2024-04-04 16:51:29 +00:00
f->regs[2], f->regs[1],
tag_task->lim, f->regs[3], &del);
2023-09-21 21:44:17 +08:00
kobj_del_list_to_do(&del);
task_unlock_2(&tag_task->kobj.lock, &cur_task->kobj.lock, st0, st1);
2023-09-07 23:47:34 +08:00
tag = msg_tag_init4(0, 0, 0, ret);
2024-09-19 23:30:54 +08:00
}
break;
2023-12-10 10:20:05 +08:00
case TASK_OBJ_UNMAP: //!< 解除task中一个进程的创建
2023-08-28 22:11:49 +08:00
{
kobject_t *del_kobj;
kobj_del_list_t kobj_list;
2023-09-02 00:18:54 +08:00
mword_t status = spinlock_lock(&tag_task->kobj.lock);
2024-09-19 23:30:54 +08:00
if (status < 0)
{
2023-09-07 23:47:34 +08:00
tag = msg_tag_init4(0, 0, 0, -EINVAL);
2023-09-02 00:18:54 +08:00
break;
}
2023-08-28 22:11:49 +08:00
kobj_del_list_init(&kobj_list);
2024-04-04 16:51:29 +00:00
obj_unmap(&tag_task->obj_space, vpage_create_raw(f->regs[1]), &kobj_list);
2023-09-02 00:18:54 +08:00
spinlock_set(&tag_task->kobj.lock, status);
status = cpulock_lock();
kobj_del_list_to_do(&kobj_list);
cpulock_set(status);
tag = msg_tag_init4(0, 0, 0, 0);
2024-09-19 23:30:54 +08:00
}
break;
#if !IS_ENABLED(CONFIG_MMU)
2023-12-10 10:20:05 +08:00
case TASK_ALLOC_RAM_BASE: //!< 分配task所拥有的内存空间
{
2023-09-02 00:18:54 +08:00
mword_t status = spinlock_lock(&tag_task->kobj.lock);
2024-09-19 23:30:54 +08:00
if (status < 0)
{
2023-09-07 23:47:34 +08:00
tag = msg_tag_init4(0, 0, 0, -EINVAL);
2023-09-02 00:18:54 +08:00
break;
}
2024-12-29 21:50:20 +08:00
int ret = task_alloc_base_ram(tag_task, tag_task->lim, f->regs[1], f->regs[2]);
2023-09-07 23:47:34 +08:00
tag = msg_tag_init4(0, 0, 0, ret);
tag_task->text_addr = (void *)(f->regs[3]);
tag_task->text_size = (size_t)(f->regs[4]);
2024-04-04 16:51:29 +00:00
f->regs[1] = (umword_t)(tag_task->mm_space.mm_block);
2023-09-02 00:18:54 +08:00
spinlock_set(&tag_task->kobj.lock, status);
2024-09-19 23:30:54 +08:00
}
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
2024-02-04 22:24:56 +08:00
case TASK_COPY_DATA: //!< 拷贝数据到task的内存区域
{
void *mem;
size_t size;
2024-04-04 16:51:29 +00:00
umword_t st_addr = f->regs[0];
size_t cp_size = f->regs[1];
2024-02-04 22:24:56 +08:00
2024-09-19 23:30:54 +08:00
if (cp_size > THREAD_MSG_BUG_LEN)
{
2024-02-04 22:24:56 +08:00
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
2024-09-19 23:30:54 +08:00
if (!is_rw_access(tag_task, (void *)st_addr, cp_size, FALSE))
{
2024-02-04 22:24:56 +08:00
tag = msg_tag_init4(0, 0, 0, -EPERM);
break;
}
// printk("addr:0x%x %d.\n", st_addr, cp_size);
void *msg = thread_get_msg_buf(thread_get_current());
memcpy((void *)st_addr, msg, cp_size);
tag = msg_tag_init4(0, 0, 0, 0);
break;
}
2023-12-10 10:20:05 +08:00
case TASK_SET_PID: //!< 设置pid
2023-11-29 23:25:32 +08:00
{
2024-04-04 16:51:29 +00:00
tag = msg_tag_init4(0, 0, 0, task_set_pid(tag_task, f->regs[0]));
2024-09-19 23:30:54 +08:00
}
break;
2023-12-10 10:20:05 +08:00
case TASK_GET_PID: //!< 获取pid
2023-11-29 23:25:32 +08:00
{
2024-04-04 16:51:29 +00:00
f->regs[1] = tag_task->pid;
2023-11-29 23:25:32 +08:00
tag = msg_tag_init4(0, 0, 0, 0);
2024-09-19 23:30:54 +08:00
}
break;
case TASK_COPY_DATA_TO: //!< 从当前task拷贝到目的task
{
int st0, st1;
int ret = 0;
obj_handler_t dst_task_hd;
addr_t src_addr;
addr_t dst_addr;
size_t copy_len;
int suc;
dst_task_hd = f->regs[0];
src_addr = f->regs[1];
dst_addr = f->regs[2];
copy_len = f->regs[3];
kobject_t *source_kobj = obj_space_lookup_kobj_cmp_type(&cur_task->obj_space, dst_task_hd, TASK_TYPE);
if (!source_kobj)
{
ret = -EINVAL;
goto copy_data_to_end2;
}
task_t *dst_task_obj = container_of(source_kobj, task_t, kobj);
suc = task_lock_2(&tag_task->kobj.lock, &dst_task_obj->kobj.lock, &st0, &st1);
if (!suc)
{
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;
goto copy_data_to_end;
}
paddr_t src_paddr = (paddr_t)task_get_currnt_paddr((vaddr_t)src_addr);
paddr_t dst_paddr = (paddr_t)task_get_currnt_paddr((vaddr_t)dst_addr);
memcpy((void *)dst_paddr, (void *)src_paddr, copy_len);
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;
2024-12-27 08:23:26 +08:00
case TASK_SET_COM_POINT: //!< 设置通信点
{
2024-12-27 08:23:26 +08:00
if (!is_rw_access(tag_task, (void *)(f->regs[1]), f->regs[2], FALSE))
{
tag = msg_tag_init4(0, 0, 0, -EPERM);
break;
}
if (f->regs[4] >= WORD_BITS)
{
tag = msg_tag_init4(0, 0, 0, -EINVAL);
break;
}
2024-12-29 21:50:20 +08:00
if (!is_rw_access(tag_task, (void *)(f->regs[3]),
ROUND_UP(f->regs[4], 8), FALSE))
2024-12-27 08:23:26 +08:00
{
tag = msg_tag_init4(0, 0, 0, -EPERM);
break;
}
2024-12-29 21:50:20 +08:00
if (!is_rw_access(tag_task, (void *)(f->regs[5]),
THREAD_MSG_BUG_LEN + CONFIG_THREAD_MAP_BUF_LEN * WORD_BYTES, FALSE))
2024-12-27 08:23:26 +08:00
{
tag = msg_tag_init4(0, 0, 0, -EPERM);
break;
}
int stack_size = f->regs[2];
tag_task->notify_point = (void *)(f->regs[0]);
2024-12-27 08:23:26 +08:00
tag_task->nofity_stack = (addr_t)(f->regs[1] + stack_size);
tag_task->nofity_bitmap = (void *)(f->regs[3]);
tag_task->nofity_bitmap_len = (f->regs[4]);
tag_task->nofity_msg_buf = (addr_t)f->regs[5];
2024-12-29 21:50:20 +08:00
tag_task->nofity_map_buf = (umword_t *)((addr_t)f->regs[5] + THREAD_MSG_BUG_LEN);
2025-02-16 23:11:18 +08:00
sema_init(&tag_task->notify_sema, tag_task->nofity_bitmap_len, tag_task->nofity_bitmap_len);
2024-12-27 08:23:26 +08:00
tag = msg_tag_init4(0, 0, 0, 0);
}
break;
case TASK_COM_UNLOCK:
{
task_t *cur_task = thread_get_current_task();
mutex_unlock(&cur_task->nofity_lock);
tag = msg_tag_init4(0, 0, 0, 0);
}
break;
case TASK_COM_LOCK:
{
task_t *cur_task = thread_get_current_task();
mutex_lock(&cur_task->nofity_lock, 0);
tag = msg_tag_init4(0, 0, 0, 0);
}
break;
2023-08-20 20:52:23 +08:00
default:
break;
}
2024-04-04 16:51:29 +00:00
f->regs[0] = tag.raw;
2023-08-20 20:52:23 +08:00
}
2023-08-27 21:25:09 +08:00
void task_init(task_t *task, ram_limit_t *ram, int is_knl)
2023-08-20 20:52:23 +08:00
{
assert(task);
assert(ram);
kobject_init(&task->kobj, TASK_TYPE);
2023-08-20 20:52:23 +08:00
obj_space_init(&task->obj_space, ram);
2023-08-27 21:25:09 +08:00
mm_space_init(&task->mm_space, is_knl);
2023-08-28 22:11:49 +08:00
ref_counter_init(&task->ref_cn);
ref_counter_inc(&task->ref_cn);
slist_init(&task->del_node);
2024-12-27 08:23:26 +08:00
slist_init(&task->nofity_theads_head);
mutex_init(&task->nofity_lock);
2023-12-02 09:49:15 +08:00
task->pid = -1;
2023-08-27 16:52:53 +08:00
task->lim = ram;
2023-08-28 22:11:49 +08:00
task->kobj.invoke_func = task_syscall_func;
task->kobj.put_func = task_put;
task->kobj.stage_1_func = task_release_stage1;
task->kobj.stage_2_func = task_release_stage2;
2024-04-01 16:10:59 +00:00
#if IS_ENABLED(CONFIG_MMU)
knl_pdir_init(&task->mm_space.mem_dir, task->mm_space.mem_dir.dir, 3 /*TODO:*/);
2024-04-01 16:10:59 +00:00
#else
assert(task_vma_alloc(&task->mm_space.mem_vma, vma_addr_create(VPAGE_PROT_RO, 0, 0),
align_power_of_2(CONFIG_SYS_TEXT_SIZE), CONFIG_SYS_TEXT_ADDR, NULL) >= 0);
2024-04-01 16:10:59 +00:00
#endif
2023-08-28 22:11:49 +08:00
}
static bool_t task_put(kobject_t *kobj)
{
task_t *tk = container_of(kobj, task_t, kobj);
return ref_counter_dec(&tk->ref_cn) == 1;
}
static void task_release_stage1(kobject_t *kobj)
{
2024-12-27 08:23:26 +08:00
int ret;
2023-08-28 22:11:49 +08:00
task_t *tk = container_of(kobj, task_t, kobj);
kobj_del_list_t kobj_list;
2023-09-02 00:18:54 +08:00
kobject_invalidate(kobj);
2023-08-28 22:11:49 +08:00
kobj_del_list_init(&kobj_list);
obj_unmap_all(&tk->obj_space, &kobj_list);
kobj_del_list_to_do(&kobj_list);
thread_fast_ipc_com_t *restore_th_com;
2024-12-27 08:23:26 +08:00
thread_fast_ipc_item_t ipc_item;
slist_foreach(restore_th_com, &tk->nofity_theads_head, fast_ipc_node)
2024-12-27 08:23:26 +08:00
{
thread_t *restore_th = restore_th_com->th;
ret = thread_fast_ipc_pop(restore_th, &ipc_item); /*TODO:这里有问题*/
2024-12-27 08:23:26 +08:00
if (ret >= 0)
{
2025-01-21 22:04:29 +08:00
// 还原栈和usp TODO: arch相关的
2024-12-27 08:23:26 +08:00
restore_th->ipc_status = THREAD_NONE;
if (restore_th->status == THREAD_SUSPEND)
{
thread_ready(restore_th, TRUE);
}
restore_th->task = ipc_item.task_bk;
thread_user_pf_restore(restore_th, ipc_item.usp_backup);
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;
2024-12-27 08:23:26 +08:00
}
}
task_vma_clean(&tk->mm_space.mem_vma);
2023-08-28 22:11:49 +08:00
}
static void task_release_stage2(kobject_t *kobj)
{
task_t *tk = container_of(kobj, task_t, kobj);
kobj_del_list_t kobj_list;
2023-10-05 23:10:18 +08:00
// task_t *cur_tk = thread_get_current_task();
2023-08-28 22:11:49 +08:00
obj_space_release(&tk->obj_space, tk->lim);
2024-04-04 16:51:29 +00:00
2024-09-17 19:31:16 +08:00
#if !IS_ENABLED(CONFIG_MMU)
2024-09-19 23:30:54 +08:00
if (tk->mm_space.mm_block)
{
2025-01-26 11:54:01 +08:00
#if CONFIG_MPU
2024-04-04 16:51:29 +00:00
mm_limit_free_align(tk->lim, tk->mm_space.mm_block, tk->mm_space.mm_block_size);
#else
2024-04-04 16:51:29 +00:00
mm_limit_free(tk->lim, tk->mm_space.mm_block);
#endif
2024-04-04 16:51:29 +00:00
}
2024-09-17 19:31:16 +08:00
#endif
2024-04-04 16:51:29 +00:00
#if IS_ENABLED(CONFIG_BUDDY_SLAB)
mm_limit_free_slab(task_slab, tk->lim, tk);
#else
2023-08-28 22:11:49 +08:00
mm_limit_free(tk->lim, tk);
2024-04-04 16:51:29 +00:00
#endif
// if (cur_tk == tk)
// {
2024-04-09 16:07:36 +00:00
thread_sched(TRUE);
2024-04-27 03:47:45 +00:00
// arch_to_sche();
// }
2024-12-27 08:23:26 +08:00
printk("release tk %x, name is:%s\n", tk, kobject_get_name(&tk->kobj));
2023-08-20 20:52:23 +08:00
}
void task_kill(task_t *tk)
{
kobj_del_list_t kobj_list;
kobj_del_list_init(&kobj_list);
obj_unmap(&tk->obj_space, vpage_create3(KOBJ_DELETE_RIGHT, 0, TASK_PROT), &kobj_list);
kobj_del_list_to_do(&kobj_list);
2024-04-09 16:07:36 +00:00
thread_sched(TRUE);
2024-04-27 03:47:45 +00:00
// arch_to_sche();
}
2023-08-27 21:25:09 +08:00
task_t *task_create(ram_limit_t *lim, int is_knl)
2023-08-20 20:52:23 +08:00
{
2024-03-31 16:06:11 +00:00
task_t *tk = NULL;
#if IS_ENABLED(CONFIG_BUDDY_SLAB)
2024-04-04 16:51:29 +00:00
tk = mm_limit_alloc_slab(task_slab, lim);
2024-03-31 16:06:11 +00:00
#else
tk = mm_limit_alloc(lim, sizeof(task_t));
#endif
2023-08-20 20:52:23 +08:00
2024-09-19 23:30:54 +08:00
if (!tk)
{
2023-08-20 20:52:23 +08:00
return NULL;
}
2023-08-27 21:25:09 +08:00
task_init(tk, lim, is_knl);
2025-02-16 23:11:18 +08:00
printk("create task is 0x%x\n", tk);
2023-08-20 20:52:23 +08:00
return tk;
}
/**
* @brief
*
* @param lim
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @return kobject_t*
*/
static kobject_t *task_create_func(ram_limit_t *lim, umword_t arg0, umword_t arg1,
umword_t arg2, umword_t arg3)
{
2023-08-27 21:25:09 +08:00
task_t *tk = task_create(lim, FALSE);
2023-08-20 20:52:23 +08:00
return &tk->kobj;
}
/**
2024-02-04 22:24:56 +08:00
* @brief
2023-08-20 20:52:23 +08:00
*
*/
2023-08-22 00:26:34 +08:00
void task_factory_register(void)
2023-08-20 20:52:23 +08:00
{
factory_register(task_create_func, TASK_PROT);
}
2023-08-22 00:26:34 +08:00
INIT_KOBJ(task_factory_register);