Support thread.

This commit is contained in:
TXuian
2024-05-19 11:48:43 +08:00
parent 736ba18769
commit 4803239498
17 changed files with 175 additions and 56 deletions

View File

@@ -41,7 +41,7 @@ Modification:
#define SYSCALL_POLL_SESSION 7 // server poll for it's server sessions
#define SYSCALL_CLOSE_SESSION 8 // client close it's client sessions
#define SYSCALL_EXEC 9 // run elf using current task
#define SYSCALL_THREAD 9 // generate a thread using old memspace
#define SYSCALL_SYS_STATE 10 // run system state
#define SYSCALL_REGISTER_IRQ 11 //
@@ -85,6 +85,7 @@ typedef int (*ipc_write_fn)(struct Session* session, int fd, char* src, int offs
int syscall(int sys_num, uintptr_t param1, uintptr_t param2, uintptr_t param3, uintptr_t param4);
int sys_spawn(char* img_start, char* name, char** argv);
int sys_thread(uintptr_t entry, char* name, char** argv);
int sys_exit(struct Thread* ptask);
int sys_yield(task_yield_reason reason);
int sys_kill(int id);

View File

@@ -1,5 +1,6 @@
SRC_FILES := syscall.c \
sys_spawn.c \
sys_thread.c \
sys_yield.c \
sys_register_as_server.c \
sys_connect_session.c \

View File

@@ -34,7 +34,7 @@ Modification:
#include "syscall.h"
#include "task.h"
extern int task_exec(struct Thread* task, char* img_start, char* name, char** argv);
extern int sys_new_thread(struct MemSpace* pmemspace, struct Thread* task, uintptr_t entry, char* name, char** argv);
int sys_spawn(char* img_start, char* name, char** argv)
{
// alloc a new memspace
@@ -60,34 +60,5 @@ int sys_spawn(char* img_start, char* name, char** argv)
}
assert(!IS_DOUBLE_LIST_EMPTY(&pmemspace->thread_list_guard));
// init params
struct ThreadStackPointer loaded_sp = load_user_stack(pmemspace, argv);
if (loaded_sp.stack_idx == -1) {
ERROR("Uable to load params to memspace.\n");
/* memspace is freed alone with free_pcb() */
xizi_task_manager.free_pcb(new_task_cb);
return -1;
}
// init trapframe
new_task_cb->thread_context.user_stack_idx = loaded_sp.stack_idx;
new_task_cb->thread_context.uspace_stack_addr = USER_MEM_TOP - ((loaded_sp.stack_idx + 1) * USER_STACK_SIZE);
new_task_cb->thread_context.ustack_kvaddr = loaded_sp.user_stack_vaddr;
arch_init_trapframe(new_task_cb->thread_context.trapframe, 0, 0);
arch_trapframe_set_sp_pc(new_task_cb->thread_context.trapframe, loaded_sp.user_sp, (uintptr_t)entry);
arch_set_main_params(new_task_cb->thread_context.trapframe, loaded_sp.argc, loaded_sp.user_sp);
// init thread name
char* last = NULL;
for (last = name; *name; name++) {
if (*name == '/') {
last = name + 1;
}
}
strncpy(new_task_cb->name, last, sizeof(new_task_cb->name));
// init pcb schedule attributes
xizi_task_manager.task_set_default_schedule_attr(new_task_cb);
return 0;
return sys_new_thread(pmemspace, new_task_cb, (uintptr_t)entry, name, argv);
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file sys_spawn.c
* @brief spawn a task
* @version 3.0
* @author AIIT XUOS Lab
* @date 2023.08.25
*/
/*************************************************
File name: sys_thread.c
Description: spawn a thread
Others:
History:
1. Date: 2023-08-28
Author: AIIT XUOS Lab
Modification:
1. first version
*************************************************/
#include "actracer.h"
#include "assert.h"
#include "memspace.h"
#include "multicores.h"
#include "share_page.h"
#include "syscall.h"
#include "task.h"
int sys_new_thread(struct MemSpace* pmemspace, struct Thread* task, uintptr_t entry, char* name, char** argv)
{
struct ThreadStackPointer loaded_sp = load_user_stack(pmemspace, argv);
if (loaded_sp.stack_idx == -1) {
ERROR("Uable to load params to memspace.\n");
/* memspace is freed alone with free_pcb() */
xizi_task_manager.free_pcb(task);
return -1;
}
// init trapframe
task->thread_context.user_stack_idx = loaded_sp.stack_idx;
task->thread_context.uspace_stack_addr = USER_MEM_TOP - ((loaded_sp.stack_idx + 1) * USER_STACK_SIZE);
task->thread_context.ustack_kvaddr = loaded_sp.user_stack_vaddr;
arch_init_trapframe(task->thread_context.trapframe, 0, 0);
arch_trapframe_set_sp_pc(task->thread_context.trapframe, loaded_sp.user_sp, (uintptr_t)entry);
arch_set_main_params(task->thread_context.trapframe, loaded_sp.argc, loaded_sp.user_sp);
// init thread name
char* last = NULL;
for (last = name; *name; name++) {
if (*name == '/') {
last = name + 1;
}
}
strncpy(task->name, last, sizeof(task->name));
// init pcb schedule attributes
xizi_task_manager.task_set_default_schedule_attr(task);
return task->tid;
}
int sys_thread(uintptr_t entry, char* name, char** argv)
{
struct Thread* cur_task = cur_cpu()->task;
// use current task's memspace
struct MemSpace* pmemspace = cur_task->memspace;
struct Thread* task = xizi_task_manager.new_task_cb(pmemspace);
if (UNLIKELY(!task)) {
ERROR("Unable to new task control block.\n");
return -1;
}
assert(!IS_DOUBLE_LIST_EMPTY(&pmemspace->thread_list_guard));
return sys_new_thread(pmemspace, task, entry, name, argv);
}

View File

@@ -44,6 +44,9 @@ int syscall(int sys_num, uintptr_t param1, uintptr_t param2, uintptr_t param3, u
case SYSCALL_SPAWN:
ret = sys_spawn((char*)param1, (char*)param2, (char**)param3);
break;
case SYSCALL_THREAD:
ret = sys_thread((uintptr_t)param1, (char*)param2, (char**)param3);
break;
case SYSCALL_EXIT:
ret = sys_exit(cur_cpu()->task);
break;
@@ -62,10 +65,6 @@ int syscall(int sys_num, uintptr_t param1, uintptr_t param2, uintptr_t param3, u
case SYSCALL_CLOSE_SESSION:
ret = sys_close_session(cur_cpu()->task, (struct Session*)param1);
break;
case SYSCALL_EXEC:
// ret = sys_exec((char*)param1, (char*)param2, (char**)param3);
ret = -1;
break;
case SYSCALL_SYS_STATE:
ret = sys_state(param1, (sys_state_info*)param2);
break;

View File

@@ -214,7 +214,7 @@ struct ThreadStackPointer load_user_stack(struct MemSpace* pmemspace, char** arg
/* start loading main params into user stack */
/// @warning supports only main style params
uintptr_t user_vspace_sp = USER_MEM_TOP;
uintptr_t user_vspace_sp = USER_MEM_TOP - (stack_idx * USER_STACK_SIZE);
static uintptr_t user_stack_init[MAX_SUPPORT_PARAMS];
memset(user_stack_init, 0, sizeof(user_stack_init));
uintptr_t argc = 0;

View File

@@ -204,28 +204,31 @@ static struct Thread* _new_task_cb(struct MemSpace* pmemspace)
if (!task) {
return NULL;
}
// init vm
if (pmemspace != NULL) {
task->memspace = pmemspace;
task->thread_context.user_stack_idx = -1;
doubleListNodeInit(&task->memspace_list_node);
doubleListAddOnBack(&task->memspace_list_node, &pmemspace->thread_list_guard);
} else {
task->memspace = NULL;
}
/* init basic task member */
doubleListNodeInit(&task->cli_sess_listhead);
doubleListNodeInit(&task->svr_sess_listhead);
/* when creating a new task, memspace will be freed outside during memory shortage */
task->memspace = NULL;
/* init main thread of task */
task->thread_context.task = task;
// alloc stack page for task
if ((void*)(task->thread_context.kern_stack_addr = (uintptr_t)kalloc(USER_STACK_SIZE)) == NULL) {
/* here inside, will no free memspace */
_dealloc_task_cb(task);
return NULL;
}
/* from now on, _new_task_cb() will not generate error */
/* init vm */
assert(pmemspace != NULL);
task->memspace = pmemspace;
task->thread_context.user_stack_idx = -1;
doubleListNodeInit(&task->memspace_list_node);
doubleListAddOnBack(&task->memspace_list_node, &pmemspace->thread_list_guard);
/* set context of main thread stack */
/// stack bottom
memset((void*)task->thread_context.kern_stack_addr, 0x00, USER_STACK_SIZE);

View File

@@ -61,10 +61,7 @@ void software_irq_dispatch(struct trapframe* tf)
// call syscall
int ret = arch_syscall(cur_task->thread_context.trapframe, &syscall_num);
if (syscall_num != SYSCALL_EXEC) {
arch_set_return(tf, ret);
}
arch_set_return(tf, ret);
}
if ((cur_cpu()->task == NULL && cur_task != NULL) || cur_task->state != RUNNING) {