From 3a985252d941315d0eca228c6833311281bc38c1 Mon Sep 17 00:00:00 2001 From: TXuian <1163589503@qq.com> Date: Tue, 30 Apr 2024 18:17:31 +0800 Subject: [PATCH] Support blocking server. --- Ubiquitous/XiZi_AIoT/services/app/Makefile | 6 +- Ubiquitous/XiZi_AIoT/services/app/test_irq.h | 20 ++++++ .../XiZi_AIoT/services/app/test_irq_block.c | 35 +++++++++ .../XiZi_AIoT/services/app/test_irq_handler.c | 38 ++++++---- .../services/fs/fs_server/include/fs.h | 2 +- .../XiZi_AIoT/services/lib/ipc/libipc.c | 72 ++++++++++--------- .../XiZi_AIoT/services/lib/ipc/libipc.h | 9 ++- .../XiZi_AIoT/services/tools/mkfs/mkfs.h | 2 +- Ubiquitous/XiZi_AIoT/softkernel/include/ipc.h | 2 +- .../softkernel/syscall/sys_poll_session.c | 28 ++++++-- .../softkernel/syscall/sys_register_irq.c | 4 ++ .../XiZi_AIoT/softkernel/syscall/sys_yield.c | 14 +++- Ubiquitous/XiZi_AIoT/softkernel/task/task.c | 1 + 13 files changed, 174 insertions(+), 59 deletions(-) create mode 100644 Ubiquitous/XiZi_AIoT/services/app/test_irq.h create mode 100644 Ubiquitous/XiZi_AIoT/services/app/test_irq_block.c diff --git a/Ubiquitous/XiZi_AIoT/services/app/Makefile b/Ubiquitous/XiZi_AIoT/services/app/Makefile index a0df04ce6..3c5ab1a89 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/Makefile +++ b/Ubiquitous/XiZi_AIoT/services/app/Makefile @@ -24,7 +24,7 @@ INC_DIR = -I$(KERNEL_ROOT)/services/shell/letter-shell \ -I$(KERNEL_ROOT)/services/app ifeq ($(BOARD), imx6q-sabrelite) -all: init test_fs simple_client simple_server shell fs_server test_irq_hdlr test_irq_send readme.txt | bin +all: init test_fs simple_client simple_server shell fs_server test_irq_hdlr test_irq_block test_irq_send readme.txt | bin else all: init test_fs simple_client simple_server shell fs_server test_irq_hdlr readme.txt | bin endif @@ -42,6 +42,10 @@ test_irq_send: test_irq_sender.o usyscall.o libserial.o @${objdump} -S $@ > $@.asm endif +test_irq_block: test_irq_block.o libserial.o libipc.o session.o usyscall.o libmem.o + @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} + @${objdump} -S $@ > $@.asm + test_irq_hdlr: test_irq_handler.o libserial.o libipc.o session.o usyscall.o libmem.o @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} @${objdump} -S $@ > $@.asm diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_irq.h b/Ubiquitous/XiZi_AIoT/services/app/test_irq.h new file mode 100644 index 000000000..f060ef947 --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/services/app/test_irq.h @@ -0,0 +1,20 @@ +/* + * 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. + */ +#include "libipc.h" +#include "libserial.h" +#include "usyscall.h" + +IPC_SERVICES(IpcSwIntrHandler, Ipc_intr_3, Ipc_wait_intr_3); + +enum { + SW_INTERRUPT_3 = 3, +}; diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_irq_block.c b/Ubiquitous/XiZi_AIoT/services/app/test_irq_block.c new file mode 100644 index 000000000..32d5103e7 --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/services/app/test_irq_block.c @@ -0,0 +1,35 @@ +/* + * 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. + */ + +#include "test_irq.h" + +IPC_INTERFACE(Ipc_wait_intr_3, 1, ignore, 0); +int wait_intr(struct Session* session, void* ignore_param) +{ + return IPC_CALL(Ipc_wait_intr_3)(session, NULL); +} + +static char prog_name[] = "TEST_IRQ_BLOCK"; +int main(int argc, char* argv[]) +{ + struct Session session; + if (connect_session(&session, "TestIRQ", 4096) < 0) { + printf("connect session failed\n"); + exit(); + } + + printf("%s start waiting for IRQ.\n", prog_name); + wait_intr(&session, NULL); + printf("%s return from waiting for IRQ.\n", prog_name); + + exit(); +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_irq_handler.c b/Ubiquitous/XiZi_AIoT/services/app/test_irq_handler.c index 28e8fdd04..c1f49fb0c 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/test_irq_handler.c +++ b/Ubiquitous/XiZi_AIoT/services/app/test_irq_handler.c @@ -10,35 +10,45 @@ * See the Mulan PSL v2 for more details. */ -#include "libipc.h" -#include "libserial.h" -#include "usyscall.h" +#include "test_irq.h" -IPC_SERVICES(IpcSwIntrHandler, Ipc_intr_3); - -enum { - SW_INTERRUPT_3 = 3, -}; - -void sgi_test_handler(void) +static bool has_one_interrupt = false; +int IPC_DO_SERVE_FUNC(Ipc_intr_3)(void* ignore) { printf("TEST_SW_HDLR: In %s()\n", __func__); + has_one_interrupt = true; + return 0; } -int IPC_DO_SERVE_FUNC(Ipc_intr_3)(void* useless) +int IPC_DO_SERVE_FUNC(Ipc_wait_intr_3)(void* ignore) { - sgi_test_handler(); + // delay the this handle + if (!has_one_interrupt) { + delay_session(); + return -1; + } + + // serve can be done by now + has_one_interrupt = false; return 0; } IPC_SERVER_INTERFACE(Ipc_intr_3, 1); -IPC_SERVER_REGISTER_INTERFACES(IpcSwIntrHandler, 1, Ipc_intr_3); +IPC_SERVER_INTERFACE(Ipc_wait_intr_3, 1); +IPC_SERVER_REGISTER_INTERFACES(IpcSwIntrHandler, 2, Ipc_intr_3, Ipc_wait_intr_3); int main() { - if (register_irq(SW_INTERRUPT_3, Ipc_intr_3) == -1) { + if (register_irq(SW_INTERRUPT_3, Ipc_intr_3) < 0) { printf("TEST_SW_HDLR: bind failed"); exit(); } + + static char prog_name[] = "TestIRQ"; + if (register_server("TestIRQ") < 0) { + printf("register server name: %s failed.\n", prog_name); + exit(); + } + ipc_server_loop(&IpcSwIntrHandler); exit(); diff --git a/Ubiquitous/XiZi_AIoT/services/fs/fs_server/include/fs.h b/Ubiquitous/XiZi_AIoT/services/fs/fs_server/include/fs.h index 42a3d7ffd..72911f2d2 100644 --- a/Ubiquitous/XiZi_AIoT/services/fs/fs_server/include/fs.h +++ b/Ubiquitous/XiZi_AIoT/services/fs/fs_server/include/fs.h @@ -88,7 +88,7 @@ struct Inode { }; // directory entry -#define DIR_NAME_SIZE 14 +#define DIR_NAME_SIZE 30 struct DirectEntry { uint16_t inum; char name[DIR_NAME_SIZE]; diff --git a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c index e00df4e2d..ca047a429 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c +++ b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c @@ -156,6 +156,11 @@ void delay_session(void) session_delayed = true; } +bool is_cur_session_delayed(void) +{ + return session_delayed; +} + void ipc_server_loop(struct IpcNode* ipc_node) { struct Session session_list[NR_MAX_SESSION]; @@ -168,43 +173,46 @@ void ipc_server_loop(struct IpcNode* ipc_node) */ poll_session(session_list, NR_MAX_SESSION); /* handle each session */ - for (int i = 0; i < NR_MAX_SESSION; i++) { - if (session_list[i].buf == NULL) { - yield(SYS_TASK_YIELD_NO_REASON); - break; - } - cur_sess_id = session_list[i].id; - struct IpcMsg* msg = IPCSESSION_MSG(&session_list[i]); - /* handle every message in current session - a session could be delay in case one of its message(current message) needs to wait for an interrupt message's arrival - interfaces[opcode] should explicitly call delay_session() and return to delay this session - */ - while (msg->header.magic == IPC_MSG_MAGIC && msg->header.valid == 1 && msg->header.done == 0) { - // printf("session %d [%d, %d]\n", session_list[i].id, session_list[i].head, session_list[i].tail); - if (session_used_size(&session_list[i]) == 0 && session_forward_tail(&session_list[i], msg->header.len) < 0) { - break; + for (int repeat = 0; repeat <= 1; repeat++) { + for (int i = 0; i < NR_MAX_SESSION; i++) { + if (session_list[i].buf == NULL) { + yield(SYS_TASK_YIELD_NO_REASON); + continue; } - - // this is a message needs to handle - if (ipc_node->interfaces[msg->header.opcode]) { - ipc_node->interfaces[msg->header.opcode](msg); - // check if this session is delayed by op handler, all messages after the delayed message in current session is blocked. - if (session_delayed) { - session_delayed = false; + cur_sess_id = session_list[i].id; + struct IpcMsg* msg = IPCSESSION_MSG(&session_list[i]); + /* handle every message in current session + a session could be delay in case one of its message(current message) needs to wait for an interrupt message's arrival + interfaces[opcode] should explicitly call delay_session() and return to delay this session + */ + while (msg->header.magic == IPC_MSG_MAGIC && msg->header.valid == 1 && msg->header.done == 0) { + // printf("session %d [%d, %d]\n", session_list[i].id, session_list[i].head, session_list[i].tail); + if (session_used_size(&session_list[i]) == 0 && session_forward_tail(&session_list[i], msg->header.len) < 0) { break; } - } else { - printf("Unsupport opcode(%d) for server: %s\n", msg->header.opcode, ipc_node->name); + + // this is a message needs to handle + if (ipc_node->interfaces[msg->header.opcode]) { + ipc_node->interfaces[msg->header.opcode](msg); + // check if this session is delayed by op handler, all messages after the delayed message in current session is blocked. + if (is_cur_session_delayed()) { + msg->header.delayed = 1; + session_delayed = false; + break; + } + } else { + printf("Unsupport opcode(%d) for server: %s\n", msg->header.opcode, ipc_node->name); + } + // current msg is a message that needs to ignore + // finish this message in server's perspective + if (session_forward_head(&session_list[i], msg->header.len) < 0) { + break; + } + msg = IPCSESSION_MSG(&session_list[i]); } - // current msg is a message that needs to ignore - // finish this message in server's perspective - if (session_forward_head(&session_list[i], msg->header.len) < 0) { - break; - } - msg = IPCSESSION_MSG(&session_list[i]); + // stop handle this session + cur_sess_id = -1; } - // stop handle this session - cur_sess_id = -1; } } } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h index c7e399f05..cee3c0343 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h +++ b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h @@ -47,7 +47,7 @@ typedef struct { uint64_t valid : 1; // for server to peek new msg uint64_t done : 1; // for client to check request done uint64_t init : 1; // for client to check request done - uint64_t reserved : 1; + uint64_t delayed : 1; uint64_t nr_args : 4; uint64_t opcode : 8; uint64_t len : 16; @@ -225,6 +225,7 @@ void ipc_server_loop(struct IpcNode* ipc_node); return res; \ } +bool is_cur_session_delayed(void); #define IPC_SERVER_INTERFACE(ipc_name, argc) \ static int IPC_SERVE(ipc_name)(struct IpcMsg * msg) \ { \ @@ -233,8 +234,10 @@ void ipc_server_loop(struct IpcNode* ipc_node); argv[i] = ipc_msg_get_nth_arg_buf(msg, i); \ } \ int32_t _ret = IPC_DO_SERVE##argc(ipc_name); \ - ipc_msg_set_return(msg, &_ret); \ - msg->header.done = 1; \ + if (!is_cur_session_delayed()) { \ + ipc_msg_set_return(msg, &_ret); \ + msg->header.done = 1; \ + } \ return 0; \ } diff --git a/Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs.h b/Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs.h index 328f7f086..a61af5517 100755 --- a/Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs.h +++ b/Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs.h @@ -80,7 +80,7 @@ struct Inode { }; // Directory is a file containing a sequence of DirEntry structures. -#define DIR_NAME_SIZE 14 +#define DIR_NAME_SIZE 30 struct DirEntry { ushort inum; char name[DIR_NAME_SIZE]; diff --git a/Ubiquitous/XiZi_AIoT/softkernel/include/ipc.h b/Ubiquitous/XiZi_AIoT/softkernel/include/ipc.h index 924ded3d5..775ee2bcb 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/include/ipc.h +++ b/Ubiquitous/XiZi_AIoT/softkernel/include/ipc.h @@ -41,7 +41,7 @@ typedef struct { uint64_t valid : 1; // for server to peek new msg uint64_t done : 1; // for client to check request done uint64_t init : 1; // for client to check request done - uint64_t reserved : 1; + uint64_t delayed : 1; uint64_t nr_args : 4; uint64_t opcode : 8; uint64_t len : 16; diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c index 4a9eb2999..5771324ea 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_poll_session.c @@ -28,11 +28,20 @@ Modification: 1. first version *************************************************/ #include "assert.h" +#include "ipc.h" #include "multicores.h" #include "share_page.h" #include "syscall.h" #include "task.h" +#define IPCSESSION_MSG(session) ((struct IpcMsg*)((char*)((session)->buf) + (session)->head)) + +static inline bool is_msg_needed(struct IpcMsg* msg) +{ + assert(msg != NULL); + return msg->header.magic == IPC_MSG_MAGIC && msg->header.valid == 1 && msg->header.done == 0 && msg->header.delayed == 0; +} + int sys_poll_session(struct Session* userland_session_arr, int arr_capacity) { struct TaskMicroDescriptor* cur_task = cur_cpu()->task; @@ -71,10 +80,11 @@ int sys_poll_session(struct Session* userland_session_arr, int arr_capacity) } /* poll with new sessions */ - int i = 0; + int nr_sessions_need_to_handle = 0; + int session_idx = 0; DOUBLE_LIST_FOR_EACH_ENTRY(server_session, &cur_task->svr_sess_listhead, node) { - if (i >= arr_capacity) { + if (session_idx >= arr_capacity) { break; } @@ -90,16 +100,24 @@ int sys_poll_session(struct Session* userland_session_arr, int arr_capacity) break; } - userland_session_arr[i++] = (struct Session) { + userland_session_arr[session_idx] = (struct Session) { .buf = (void*)server_session->buf_addr, .capacity = server_session->capacity, .head = server_session->head, .tail = server_session->tail, .id = SERVER_SESSION_BACKEND(server_session)->session_id, }; + + struct IpcMsg* msg = IPCSESSION_MSG(&userland_session_arr[session_idx]); + if (is_msg_needed(msg)) { + nr_sessions_need_to_handle++; + } + + session_idx++; } - if (LIKELY(i < arr_capacity)) { - userland_session_arr[i].buf = 0; + if (session_idx < arr_capacity && nr_sessions_need_to_handle == 0) { + userland_session_arr[session_idx].buf = 0; + sys_yield(SYS_TASK_YIELD_BLOCK_IPC); } return 0; diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_register_irq.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_register_irq.c index 63ad8e0de..d0b63502c 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_register_irq.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_register_irq.c @@ -76,6 +76,10 @@ static void send_irq_to_user(int irq_num) buf->header.magic = IPC_MSG_MAGIC; buf->header.valid = 1; + if (irq_forward_table[irq_num].handle_task->state == BLOCKED) { + xizi_task_manager.task_unblock(irq_forward_table[irq_num].handle_task); + } + /* add session head */ session->head = (session->head + len) % session->capacity; } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c index 7db762765..758c486d3 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/syscall/sys_yield.c @@ -31,7 +31,7 @@ Modification: #include "syscall.h" #include "task.h" -#include "log.h" +#include "assert.h" int sys_yield(task_yield_reason reason) { @@ -46,5 +46,17 @@ int sys_yield(task_yield_reason reason) xizi_task_manager.task_block(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); + } + } + return 0; } \ 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 78047d040..34cbfc969 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/task/task.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/task/task.c @@ -291,6 +291,7 @@ static void _task_yield_noschedule(struct TaskMicroDescriptor* task, bool blocki task->state = READY; } task->remain_tick = TASK_CLOCK_TICK; + cur_cpu()->task = NULL; task_node_add_to_ready_list_back(task); }