Rename struct Thread; Completely split task memspace and shceduling
This commit is contained in:
67
Ubiquitous/XiZi_AIoT/softkernel/include/bitmap64.h
Normal file
67
Ubiquitous/XiZi_AIoT/softkernel/include/bitmap64.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 bitmap64.h
|
||||
* @brief 64 bit bitmap support
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023.08.25
|
||||
*/
|
||||
|
||||
/*************************************************
|
||||
File name: bitmap64.h
|
||||
Description: 64 bit bitmap support
|
||||
Others:
|
||||
History:
|
||||
1. Date: 2024-05-18
|
||||
Author: AIIT XUOS Lab
|
||||
Modification:
|
||||
1. first version
|
||||
*************************************************/
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "assert.h"
|
||||
|
||||
struct bitmap64 {
|
||||
uint64_t map;
|
||||
};
|
||||
|
||||
static inline void bitmap64_init(struct bitmap64* bitmap)
|
||||
{
|
||||
bitmap->map = 0;
|
||||
}
|
||||
|
||||
static inline int bitmap64_alloc(struct bitmap64* bitmap)
|
||||
{
|
||||
int free_bit = -1;
|
||||
// free bit is the first 0 bit, from [1, 64]
|
||||
free_bit = __builtin_ffsll(~(bitmap->map));
|
||||
// handle if bitmap is full (no using 64th bit here)
|
||||
if (free_bit == 64) {
|
||||
return -1;
|
||||
}
|
||||
assert(free_bit < 64 && free_bit >= 1);
|
||||
// alloc and return
|
||||
bitmap->map |= (1 << (free_bit - 1));
|
||||
return free_bit - 1;
|
||||
}
|
||||
|
||||
static inline void bitmap64_free(struct bitmap64* bitmap, int idx)
|
||||
{
|
||||
// usages of bitmap64 must be correct
|
||||
assert((bitmap->map & (1 << idx)) != 0);
|
||||
// free bit
|
||||
bitmap->map &= ~(uint64_t)(1 << idx);
|
||||
}
|
||||
43
Ubiquitous/XiZi_AIoT/softkernel/include/memspace.h
Normal file
43
Ubiquitous/XiZi_AIoT/softkernel/include/memspace.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 memspace.h
|
||||
* @brief memspace loader
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023.08.25
|
||||
*/
|
||||
|
||||
/*************************************************
|
||||
File name: memspace.h
|
||||
Description: memspace loader
|
||||
Others:
|
||||
History:
|
||||
1. Date: 2023-08-28
|
||||
Author: AIIT XUOS Lab
|
||||
Modification:
|
||||
1. first version
|
||||
*************************************************/
|
||||
|
||||
#include "task.h"
|
||||
|
||||
struct ThreadStackPointer {
|
||||
int argc;
|
||||
int stack_idx;
|
||||
uintptr_t user_sp;
|
||||
uintptr_t user_stack_vaddr;
|
||||
};
|
||||
|
||||
struct MemSpace* alloc_memspace();
|
||||
void free_memspace(struct MemSpace* pmemspace);
|
||||
uintptr_t* load_memspace(struct MemSpace* pmemspace, char* img_start);
|
||||
struct ThreadStackPointer load_user_stack(struct MemSpace* pmemspace, char** argv);
|
||||
@@ -36,7 +36,7 @@ Modification:
|
||||
struct CPU {
|
||||
int cpuid;
|
||||
|
||||
struct TaskMicroDescriptor* task;
|
||||
struct Thread* task;
|
||||
struct context* scheduler;
|
||||
};
|
||||
|
||||
|
||||
@@ -59,8 +59,8 @@ struct PagerRightGroup {
|
||||
struct XiziPageManager {
|
||||
bool (*new_pgdir)(struct TopLevelPageDirectory* pgdir);
|
||||
void (*free_user_pgdir)(struct TopLevelPageDirectory* pgdir);
|
||||
bool (*map_pages)(uintptr_t* pgdir, uintptr_t vaddr, uintptr_t paddr, uintptr_t len, bool is_dev);
|
||||
bool (*unmap_pages)(uintptr_t* pgdir, uintptr_t vaddr, uintptr_t len);
|
||||
bool (*map_pages)(uintptr_t* pd_addr, uintptr_t vaddr, uintptr_t paddr, uintptr_t len, bool is_dev);
|
||||
bool (*unmap_pages)(uintptr_t* pd_addr, uintptr_t vaddr, uintptr_t len);
|
||||
|
||||
uintptr_t (*resize_user_pgdir)(struct TopLevelPageDirectory* pgdir, uintptr_t old_size, uintptr_t new_size);
|
||||
uintptr_t (*address_translate)(struct TopLevelPageDirectory* pgdir, uintptr_t vaddr);
|
||||
|
||||
@@ -31,6 +31,6 @@ Modification:
|
||||
|
||||
#include "task.h"
|
||||
|
||||
struct TaskMicroDescriptor* max_priority_runnable_task(void);
|
||||
struct TaskMicroDescriptor* round_robin_runnable_task(uint32_t priority);
|
||||
struct Thread* max_priority_runnable_task(void);
|
||||
struct Thread* round_robin_runnable_task(uint32_t priority);
|
||||
void recover_priority(void);
|
||||
@@ -69,8 +69,8 @@ struct session_backend {
|
||||
struct client_session client_side;
|
||||
int session_id; // id of this session
|
||||
int nr_pages; // pages used by this pipe
|
||||
struct TaskMicroDescriptor* client; // client of this pipe
|
||||
struct TaskMicroDescriptor* server; // server of this pipe
|
||||
struct Thread* client; // client of this pipe
|
||||
struct Thread* server; // server of this pipe
|
||||
|
||||
uintptr_t buf_kernel_addr;
|
||||
};
|
||||
@@ -81,11 +81,11 @@ struct SharePageRightGroup {
|
||||
};
|
||||
|
||||
struct XiziSharePageManager {
|
||||
struct session_backend* (*create_share_pages)(struct TaskMicroDescriptor* client, struct TaskMicroDescriptor* server, const int capacity);
|
||||
void (*unmap_task_share_pages)(struct TaskMicroDescriptor* task, const uintptr_t task_vaddr, const int nr_pages);
|
||||
struct session_backend* (*create_share_pages)(struct Thread* client, struct Thread* server, const int capacity);
|
||||
void (*unmap_task_share_pages)(struct Thread* task, const uintptr_t task_vaddr, const int nr_pages);
|
||||
int (*delete_share_pages)(struct session_backend* session_backend);
|
||||
|
||||
uintptr_t (*task_map_pages)(struct TaskMicroDescriptor* task, const uintptr_t vaddr, const uintptr_t paddr, const int nr_pages, const int is_dev);
|
||||
uintptr_t (*task_map_pages)(struct Thread* task, const uintptr_t vaddr, const uintptr_t paddr, const int nr_pages, const int is_dev);
|
||||
};
|
||||
extern struct XiziSharePageManager xizi_share_page_manager;
|
||||
|
||||
|
||||
@@ -85,20 +85,20 @@ 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_exit(struct TaskMicroDescriptor* ptask);
|
||||
int sys_exit(struct Thread* ptask);
|
||||
int sys_yield(task_yield_reason reason);
|
||||
int sys_kill(int id);
|
||||
|
||||
int sys_register_as_server(char* name);
|
||||
int sys_connect_session(char* path, int capacity, struct Session* user_session);
|
||||
int sys_poll_session(struct Session* userland_session_arr, int arr_capacity);
|
||||
int sys_close_session(struct TaskMicroDescriptor* task, struct Session* session);
|
||||
int sys_close_session(struct Thread* task, struct Session* session);
|
||||
|
||||
int sys_exec(char* img_start, char* name, char** argv);
|
||||
int sys_state(sys_state_option option, sys_state_info* info);
|
||||
int sys_mmap(uintptr_t vaddr, uintptr_t paddr, int len, int is_dev);
|
||||
|
||||
int sys_register_irq(int irq_num, int irq_opcode);
|
||||
int sys_unbind_irq_all(struct TaskMicroDescriptor* task);
|
||||
int sys_unbind_irq(struct TaskMicroDescriptor* task, int irq_num);
|
||||
int sys_unbind_irq_all(struct Thread* task);
|
||||
int sys_unbind_irq(struct Thread* task, int irq_num);
|
||||
#endif
|
||||
|
||||
@@ -31,6 +31,7 @@ Modification:
|
||||
|
||||
#include "core.h"
|
||||
|
||||
#include "bitmap64.h"
|
||||
#include "buddy.h"
|
||||
#include "list.h"
|
||||
#include "object_allocator.h"
|
||||
@@ -52,41 +53,58 @@ enum ProcState {
|
||||
NEVER_RUN,
|
||||
};
|
||||
|
||||
struct MemSpace {
|
||||
/* task memory resources */
|
||||
struct TopLevelPageDirectory pgdir; // [phy] vm pgtbl base address
|
||||
uintptr_t heap_base; // mem size of proc used(allocated by kernel)
|
||||
uintptr_t mem_size;
|
||||
/* task communication mem resources */
|
||||
struct KBuddy* massive_ipc_allocator;
|
||||
|
||||
/* thread using this memspace */
|
||||
struct bitmap64 thread_stack_idx_bitmap;
|
||||
struct double_list_node thread_list_guard;
|
||||
};
|
||||
|
||||
/* Thread Control Block */
|
||||
struct Thread {
|
||||
struct TaskMicroDescriptor* task; // process of current thread
|
||||
uintptr_t stack_addr; // [virt] stack base address
|
||||
struct ThreadContext {
|
||||
struct Thread* task; // process of current thread
|
||||
|
||||
/* kernel stack of thread */
|
||||
uintptr_t kern_stack_addr; // [virt] stack base address
|
||||
|
||||
/* user stack */
|
||||
int user_stack_idx; // [virt] stack idx in user memspace
|
||||
uintptr_t uspace_stack_addr; // [virt] user stack base address in memspace
|
||||
uintptr_t ustack_kvaddr; // [virt] user stack memeory's kernel vaddr
|
||||
|
||||
/* kernel context of thread */
|
||||
struct context* context;
|
||||
/* user context of thread */
|
||||
struct trapframe* trapframe;
|
||||
};
|
||||
|
||||
/* Process Control Block */
|
||||
struct TaskMicroDescriptor {
|
||||
/* task debug resources */
|
||||
int pid;
|
||||
bool bind_irq;
|
||||
bool dead;
|
||||
struct Thread {
|
||||
/* task name */
|
||||
char name[TASK_NAME_MAX_LEN];
|
||||
|
||||
/// @todo support return value
|
||||
int ret; // state val that be returned to parent
|
||||
/// @todo support parent
|
||||
struct TaskMicroDescriptor* parent;
|
||||
/* task debug resources */
|
||||
int tid;
|
||||
bool bind_irq;
|
||||
bool dead;
|
||||
|
||||
/* task context resources */
|
||||
struct Thread main_thread; // will only access by task itself
|
||||
struct ThreadContext thread_context; // will only access by task itself
|
||||
|
||||
/* task memory resources */
|
||||
struct TopLevelPageDirectory pgdir; // [phy] vm pgtbl base address
|
||||
uintptr_t heap_base; // mem size of proc used(allocated by kernel)
|
||||
/// @todo support heap_base
|
||||
uintptr_t mem_size;
|
||||
/* thread mem space */
|
||||
struct MemSpace* memspace;
|
||||
struct double_list_node memspace_list_node;
|
||||
|
||||
/* task communication resources */
|
||||
struct double_list_node cli_sess_listhead;
|
||||
struct double_list_node svr_sess_listhead;
|
||||
bool current_ipc_handled;
|
||||
struct KBuddy* massive_ipc_allocator;
|
||||
struct TraceTag server_identifier;
|
||||
|
||||
/* task schedule attributes */
|
||||
@@ -106,36 +124,39 @@ struct XiziTaskManager {
|
||||
struct double_list_node task_list_head[TASK_MAX_PRIORITY]; /* list of task control blocks that are allocated */
|
||||
struct double_list_node task_running_list_head;
|
||||
struct double_list_node task_blocked_list_head;
|
||||
struct slab_allocator task_allocator;
|
||||
struct slab_allocator task_buddy_allocator;
|
||||
|
||||
/* mem allocator */
|
||||
struct slab_allocator memspace_allocator;
|
||||
struct slab_allocator task_allocator; // allocate struct Tread
|
||||
struct slab_allocator task_buddy_allocator; // allocate buddy for memspace
|
||||
uint32_t next_pid;
|
||||
|
||||
/* init task manager */
|
||||
void (*init)();
|
||||
/* new a task control block, checkout #sys_spawn for usage */
|
||||
struct TaskMicroDescriptor* (*new_task_cb)();
|
||||
struct Thread* (*new_task_cb)(struct MemSpace* pmemspace);
|
||||
/* free a task control block, this calls #free_user_pgdir to free all vitual spaces */
|
||||
void (*free_pcb)(struct TaskMicroDescriptor*);
|
||||
void (*free_pcb)(struct Thread*);
|
||||
/* init a task control block, set name, remain_tick, state, cwd, priority, etc. */
|
||||
void (*task_set_default_schedule_attr)(struct TaskMicroDescriptor*);
|
||||
void (*task_set_default_schedule_attr)(struct Thread*);
|
||||
|
||||
/* use by task_scheduler, find next READY task, should be in locked */
|
||||
struct TaskMicroDescriptor* (*next_runnable_task)(void);
|
||||
struct Thread* (*next_runnable_task)(void);
|
||||
/* function that's runing by kernel thread context, schedule use tasks */
|
||||
void (*task_scheduler)(struct SchedulerRightGroup);
|
||||
|
||||
/* handle task state */
|
||||
/* call to yield current use task */
|
||||
void (*task_yield_noschedule)(struct TaskMicroDescriptor* task, bool is_blocking);
|
||||
void (*task_yield_noschedule)(struct Thread* task, bool is_blocking);
|
||||
/* block and unblock task */
|
||||
void (*task_block)(struct TaskMicroDescriptor* task);
|
||||
void (*task_unblock)(struct TaskMicroDescriptor* task);
|
||||
void (*task_block)(struct Thread* task);
|
||||
void (*task_unblock)(struct Thread* task);
|
||||
/* set task priority */
|
||||
void (*set_cur_task_priority)(int priority);
|
||||
};
|
||||
|
||||
extern uint32_t ready_task_priority;
|
||||
extern struct TaskMicroDescriptor* next_task_emergency;
|
||||
extern struct Thread* next_task_emergency;
|
||||
extern struct XiziTaskManager xizi_task_manager;
|
||||
|
||||
int spawn_embedded_task(char* img_start, char* name, char** argv);
|
||||
|
||||
Reference in New Issue
Block a user