From eca27408be7efe770e1f74f42ed7e7f672112bd2 Mon Sep 17 00:00:00 2001 From: "zongmin.gu" Date: Tue, 12 May 2020 21:45:44 +0800 Subject: [PATCH] Support user manage stack Go/Java/JIT code manage their own stack So we need to help them to handle exception --- src/libos/include/task.h | 2 -- src/libos/src/process/task/task.c | 32 +++++--------------- src/libos/src/syscall/syscall_entry_x86-64.S | 15 ++------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/libos/include/task.h b/src/libos/include/task.h index b11f2f0c..4f8c3709 100644 --- a/src/libos/include/task.h +++ b/src/libos/include/task.h @@ -38,8 +38,6 @@ void do_exit_task(void); #else /* __ASSEMBLY__ */ /* See //common/inc/internal/thread_data.h */ -#define TD_STACK_BASE (8 * 2) -#define TD_STACK_LIMIT (8 * 3) #define TD_STACKGUARD_OFFSET (8 * 5) /* Override the field for stack guard */ #define TD_TASK_OFFSET TD_STACKGUARD_OFFSET diff --git a/src/libos/src/process/task/task.c b/src/libos/src/process/task/task.c index 77e6cabe..41a32058 100644 --- a/src/libos/src/process/task/task.c +++ b/src/libos/src/process/task/task.c @@ -1,3 +1,4 @@ +#include #include "task.h" /* See //common/inc/internal/thread_data.h */ @@ -22,6 +23,8 @@ extern void __set_stack_guard(uint64_t new_val); int sgx_enable_user_stack(size_t stack_base, size_t stack_limit); void sgx_disable_user_stack(void); +#define OCCLUM_PAGE_SIZE 4096 + static uint64_t get_syscall_stack(struct Task* this_task) { #define LARGE_ENOUGH_GAP (8192) char libos_stack_var = 0; @@ -37,28 +40,6 @@ static uint64_t get_syscall_stack(struct Task* this_task) { #define RESET_CURRENT_TASK() \ __set_stack_guard(stack_guard); -void switch_td_to_kernel(const struct Task* task) { - thread_data_t* td = get_thread_data(); - - // TODO: do do not support stack expansion, need a new design on SGX2 platform. - // Set the stack_commit_addr to 0, as the result no stack expansion happens at any situations - __atomic_store_n(&td->stack_commit_addr, 0, __ATOMIC_RELAXED); - td->stack_base_addr = task->kernel_stack_base; - td->stack_limit_addr = task->kernel_stack_limit; - td->stack_commit_addr = task->kernel_stack_limit; -} - -void switch_td_to_user(const struct Task* task) { - thread_data_t* td = get_thread_data(); - - // TODO: do do not support stack expansion, need a new design on SGX2 platform. - // Set the stack_commit_addr to 0, as the result no stack expansion happens at any situations - __atomic_store_n(&td->stack_commit_addr, 0, __ATOMIC_RELAXED); - td->stack_base_addr = task->user_stack_base; - td->stack_limit_addr = task->user_stack_limit; - td->stack_commit_addr = task->user_stack_limit; -} - int do_exec_task(struct Task* task) { jmp_buf libos_state = {0}; thread_data_t* td = get_thread_data(); @@ -67,7 +48,10 @@ int do_exec_task(struct Task* task) { task->kernel_stack_base = td->stack_base_addr; task->kernel_stack_limit = td->stack_limit_addr; - switch_td_to_user(task); + //Reserve two pages stack for exception handler + //The SGX SDK exception handler depends on the two pages as stack to handle exceptions in user's code + //TODO:Add a check in the sysreturn logic to confirm the stack is not corrupted + assert(task->kernel_stack_limit+OCCLUM_PAGE_SIZE*2 <= task->kernel_rsp); SET_CURRENT_TASK(task); @@ -84,7 +68,5 @@ int do_exec_task(struct Task* task) { void do_exit_task(void) { struct Task* task = __get_current_task(); jmp_buf* jb = task->saved_state; - - switch_td_to_kernel(task); longjmp(*jb, 1); } \ No newline at end of file diff --git a/src/libos/src/syscall/syscall_entry_x86-64.S b/src/libos/src/syscall/syscall_entry_x86-64.S index 110e8670..2327778b 100644 --- a/src/libos/src/syscall/syscall_entry_x86-64.S +++ b/src/libos/src/syscall/syscall_entry_x86-64.S @@ -65,13 +65,8 @@ __occlum_syscall_linux_abi: #else // SGX_MODE_HW movq TASK_KERNEL_FS(%r12), %r11 wrfsbase %r11 -#endif - // Switch to kernel stack base and limit - movq TASK_KERNEL_STACK_BASE(%r12), %r11 - movq %r11, %gs:TD_STACK_BASE - movq TASK_KERNEL_STACK_LIMIT(%r12), %r11 - movq %r11, %gs:TD_STACK_LIMIT - +#endif + call occlum_syscall // This should never happen! @@ -116,12 +111,6 @@ __occlum_sysret: movq TASK_USER_FS(%r12), %r11 wrfsbase %r11 #endif - // Switch to user stack base and limit - movq TASK_USER_STACK_BASE(%r12), %r11 - movq %r11, %gs:TD_STACK_BASE - movq TASK_USER_STACK_LIMIT(%r12), %r11 - movq %r11, %gs:TD_STACK_LIMIT - // Restore flags first leaq (17*8)(%rdi), %rsp popfq