From 4716a4e018a0d7f7c519478d613aae15a58cf31a Mon Sep 17 00:00:00 2001 From: tuyuyang <1163589503@qq.com> Date: Sun, 28 Jul 2024 14:02:14 +0800 Subject: [PATCH] Support multi page task mapping --- .../XiZi_AIoT/services/app/shell_port.c | 2 +- .../XiZi_AIoT/services/app/simple_client.c | 2 +- .../services/lib/usyscall/usyscall.c | 3 ++- .../XiZi_AIoT/softkernel/include/buddy.h | 2 +- .../XiZi_AIoT/softkernel/memory/pagetable.c | 23 ++++++++----------- .../softkernel/memory/pagetable_level2.c | 2 -- .../softkernel/memory/pagetable_level3.c | 2 -- .../XiZi_AIoT/softkernel/memory/share_page.c | 2 +- .../XiZi_AIoT/softkernel/syscall/sys_mmap.c | 23 ++++++++----------- .../softkernel/syscall/sys_poll_session.c | 4 +++- .../XiZi_AIoT/softkernel/syscall/sys_yield.c | 19 +++++++-------- .../XiZi_AIoT/softkernel/task/memspace.c | 3 ++- Ubiquitous/XiZi_AIoT/softkernel/task/task.c | 2 +- 13 files changed, 41 insertions(+), 48 deletions(-) diff --git a/Ubiquitous/XiZi_AIoT/services/app/shell_port.c b/Ubiquitous/XiZi_AIoT/services/app/shell_port.c index 785ad8b6e..7f9c286c4 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/shell_port.c +++ b/Ubiquitous/XiZi_AIoT/services/app/shell_port.c @@ -50,7 +50,7 @@ int main(void) shellInit(&shell, shellBuffer, 512); - while (connect_session(&session_fs, "MemFS", 8092) < 0) + while (connect_session(&session_fs, "MemFS", 0x10000) < 0) ; if (!session_fs.buf) { printf("session connect faield\n"); diff --git a/Ubiquitous/XiZi_AIoT/services/app/simple_client.c b/Ubiquitous/XiZi_AIoT/services/app/simple_client.c index 2a23acb6a..184df5071 100755 --- a/Ubiquitous/XiZi_AIoT/services/app/simple_client.c +++ b/Ubiquitous/XiZi_AIoT/services/app/simple_client.c @@ -108,7 +108,7 @@ int main(int argc, char** argv) struct Session fs_session; static char id_buf[33] = { 0 }; if (id > 1) { - if (connect_session(&fs_session, "MemFS", 8192) < 0) { + if (connect_session(&fs_session, "MemFS", 0x2000) < 0) { printf("connect fs_session failed\n"); } else { int fd; diff --git a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c index debd8af62..8bdf03709 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c +++ b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c @@ -15,11 +15,12 @@ int spawn(struct Session* session, int fd, ipc_read_fn ipc_read, ipc_fsize_fn ipc_fsize, char* name, char** argv) { /* read elf image */ + int max_communicate_size = session->capacity - 0x1000; int file_size = ipc_fsize(session, fd); void* img = malloc(file_size); int read_len = 0; while (read_len < file_size) { - int cur_read_len = file_size - read_len < 4096 ? file_size - read_len : 4096; + int cur_read_len = file_size - read_len < max_communicate_size ? file_size - read_len : max_communicate_size; if (cur_read_len < 0) { return -1; } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/include/buddy.h b/Ubiquitous/XiZi_AIoT/softkernel/include/buddy.h index e6f3cf77b..0493ccdbf 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/include/buddy.h +++ b/Ubiquitous/XiZi_AIoT/softkernel/include/buddy.h @@ -36,7 +36,7 @@ Modification: #include #include -#define MAX_BUDDY_ORDER (12) +#define MAX_BUDDY_ORDER (20) #define FREE_LIST_INDEX(order) \ (1 << order) diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable.c index 65ce48aca..53404786f 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable.c @@ -158,21 +158,18 @@ static uintptr_t _resize_user_pgdir(struct MemSpace* pmemspace, uintptr_t old_si } uintptr_t cur_size = ALIGNUP(old_size, PAGE_SIZE); + uintptr_t size_needed = ALIGNUP(new_size, PAGE_SIZE) - cur_size; - while (cur_size < new_size) { - char* new_page = kalloc(PAGE_SIZE); - if (new_page == NULL) { - ERROR("No memory\n"); - return cur_size; - } - memset(new_page, 0, PAGE_SIZE); - - if (!xizi_pager.map_pages(pmemspace, cur_size, V2P(new_page), PAGE_SIZE, false)) { - return cur_size; - } - CreateResourceTag(NULL, &pmemspace->tag, NULL, TRACER_MEM_FROM_BUDDY_AC_RESOURCE, V2P(new_page)); - cur_size += PAGE_SIZE; + char* new_page = kalloc(size_needed); + if (new_page == NULL) { + ERROR("No memory\n"); + return cur_size; } + memset(new_page, 0, size_needed); + if (!xizi_pager.map_pages(pmemspace, cur_size, V2P(new_page), size_needed, false)) { + return cur_size; + } + CreateResourceTag(NULL, &pmemspace->tag, NULL, TRACER_MEM_FROM_BUDDY_AC_RESOURCE, V2P_WO(new_page)); return new_size; } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level2.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level2.c index bbc32ea0b..2ab5375aa 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level2.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level2.c @@ -65,8 +65,6 @@ uintptr_t* _page_walk(uintptr_t* pgdir, uintptr_t vaddr, bool alloc) void _free_user_pgdir(struct TopLevelPageDirectory* pgdir) { - uintptr_t low_bound = kern_virtmem_buddy.mem_start, high_bound = kern_virtmem_buddy.mem_end; - uintptr_t user_low_bound = user_phy_freemem_buddy.mem_start, user_high_bound = user_phy_freemem_buddy.mem_end; uintptr_t end_idx = USER_MEM_TOP >> LEVEL3_PDE_SHIFT; for (uintptr_t level4_entry_idx = 0; level4_entry_idx < end_idx; level4_entry_idx++) { diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level3.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level3.c index c9eb9a412..9e1779fc6 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level3.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/pagetable_level3.c @@ -83,8 +83,6 @@ void _free_user_pgdir(struct TopLevelPageDirectory* pgdir) return; } - uintptr_t low_bound = kern_virtmem_buddy.mem_start, high_bound = kern_virtmem_buddy.mem_end; - uintptr_t user_low_bound = user_phy_freemem_buddy.mem_start, user_high_bound = user_phy_freemem_buddy.mem_end; uintptr_t end_idx = (USER_MEM_TOP >> LEVEL2_PDE_SHIFT) & (NUM_LEVEL2_PDE - 1); for (uintptr_t l2_entry_idx = 0; l2_entry_idx < end_idx; l2_entry_idx++) { diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c index 1d5e7a43a..a728cfb3f 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c @@ -111,7 +111,7 @@ static uintptr_t map_task_share_page(struct Thread* task, const uintptr_t paddr, vaddr = alloc_share_page_addr(task, nr_pages * 2); // time to use buddy - if (vaddr >= USER_IPC_USE_ALLOCATOR_WATERMARK) { + if (vaddr + (2 * nr_pages * PAGE_SIZE) >= USER_IPC_USE_ALLOCATOR_WATERMARK) { task->memspace->massive_ipc_allocator = (struct KBuddy*)slab_alloc(&xizi_task_manager.task_buddy_allocator); if (!task->memspace->massive_ipc_allocator) { ERROR("Alloc task buddy failed.\n"); diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_mmap.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_mmap.c index 56e019faf..becf0b9d7 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_mmap.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_mmap.c @@ -53,22 +53,17 @@ int sys_mmap(uintptr_t* vaddr, uintptr_t* paddr, int len, int is_dev) return -1; } } else { - int load_len = 0; uintptr_t load_vaddr = *vaddr; - while (load_len < true_len) { - char* new_paddr = raw_alloc(PAGE_SIZE); - CreateResourceTag(NULL, &cur_task->memspace->tag, NULL, TRACER_MEM_FROM_BUDDY_AC_RESOURCE, new_paddr); - if (new_paddr == NULL) { - return -1; - } - if (xizi_share_page_manager.task_map_pages(cur_task, load_vaddr, (uintptr_t)new_paddr, 1, false) == (uintptr_t)NULL) { - raw_free(new_paddr); - return -1; - } - load_vaddr += PAGE_SIZE; - load_len += PAGE_SIZE; - *paddr = (uintptr_t)new_paddr; + char* new_paddr = raw_alloc(true_len); + if (new_paddr == NULL) { + return -1; } + if (xizi_share_page_manager.task_map_pages(cur_task, load_vaddr, (uintptr_t)new_paddr, true_len / PAGE_SIZE, false) == (uintptr_t)NULL) { + raw_free(new_paddr); + return -1; + } + CreateResourceTag(NULL, &cur_task->memspace->tag, NULL, TRACER_MEM_FROM_BUDDY_AC_RESOURCE, new_paddr); + *paddr = (uintptr_t)new_paddr; } cur_task->memspace->mem_size += true_len; diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c index 95fb8f743..55451238e 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c @@ -52,6 +52,7 @@ int sys_poll_session(struct Session* userland_session_arr, int arr_capacity) struct double_list_node* cur_node = NULL; struct server_session* server_session = NULL; + /* update old sessions */ for (int i = 0; i < arr_capacity; i++) { if (UNLIKELY(userland_session_arr[i].buf == NULL)) { @@ -111,12 +112,13 @@ int sys_poll_session(struct Session* userland_session_arr, int arr_capacity) }; struct IpcMsg* msg = IPCSESSION_MSG(&userland_session_arr[session_idx]); - if (is_msg_needed(msg)) { + if (msg != NULL && is_msg_needed(msg)) { nr_sessions_need_to_handle++; } session_idx++; } + if (session_idx < arr_capacity) { userland_session_arr[session_idx].buf = NULL; if (!has_middle_delete && nr_sessions_need_to_handle == 0) { diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c index 0e0f644e2..45c3f468f 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c @@ -42,19 +42,20 @@ int sys_yield(task_yield_reason reason) if ((reason & SYS_TASK_YIELD_BLOCK_IPC) != 0) { if (cur_task->advance_unblock) { cur_task->advance_unblock = false; + return 0; } else { xizi_task_manager.task_block(&xizi_task_manager.task_blocked_list_head, cur_task); } - } - // wake up all possible server - struct client_session* client_session = NULL; - DOUBLE_LIST_FOR_EACH_ENTRY(client_session, &cur_task->cli_sess_listhead, node) - { - assert(client_session != NULL); - struct session_backend* session_backend = CLIENT_SESSION_BACKEND(client_session); - if (session_backend->server->state == BLOCKED) { - xizi_task_manager.task_unblock(session_backend->server); + // wake up all possible server + struct client_session* client_session = NULL; + DOUBLE_LIST_FOR_EACH_ENTRY(client_session, &cur_task->cli_sess_listhead, node) + { + assert(client_session != NULL); + struct session_backend* session_backend = CLIENT_SESSION_BACKEND(client_session); + if (session_backend->server->state == BLOCKED) { + xizi_task_manager.task_unblock(session_backend->server); + } } } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/task/memspace.c b/Ubiquitous/XiZi_AIoT/softkernel/task/memspace.c index 4f515d78f..eb1f15c6c 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/task/memspace.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/task/memspace.c @@ -146,6 +146,7 @@ uintptr_t* load_memspace(struct MemSpace* pmemspace, char* img_start) if (ph.vaddr % PAGE_SIZE != 0) { LOG("Unsupported elf file, try use flag -N to compile.\n"); } + for (int addr_offset = 0; addr_offset < ph.filesz; addr_offset += PAGE_SIZE) { uintptr_t page_paddr = xizi_pager.address_translate(&pmemspace->pgdir, ph.vaddr + addr_offset); if (page_paddr == 0) { @@ -257,8 +258,8 @@ struct ThreadStackPointer load_user_stack(struct MemSpace* pmemspace, char** arg pmemspace->mem_size += USER_STACK_SIZE; loaded_sp.argc = argc; - loaded_sp.stack_idx = stack_idx; loaded_sp.user_sp = user_vspace_sp; loaded_sp.user_stack_vaddr = (uintptr_t)stack_bottom; + loaded_sp.stack_idx = stack_idx; return loaded_sp; } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/softkernel/task/task.c b/Ubiquitous/XiZi_AIoT/softkernel/task/task.c index ba54cb0a1..27df0e16e 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/task/task.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/task/task.c @@ -350,7 +350,7 @@ static void _task_unblock(struct Thread* task) assert(task->state == BLOCKED); task_node_leave_list(task); task->state = READY; - task_node_add_to_ready_list_head(task); + task_node_add_to_ready_list_back(task); } /// @brief @warning not tested function