Move the stack initialization code to process.c
authorcoderain <coderain@sdf.org>
Tue, 4 Apr 2017 22:16:28 +0000 (00:16 +0200)
committercoderain <coderain@sdf.org>
Tue, 4 Apr 2017 22:16:28 +0000 (00:16 +0200)
kernel/include/process.h
kernel/src/exec/aout.c
kernel/src/process.c

index 24cb69ba28927fd3a1a3cd581064a48258c857b3..ce26214855f5c601576995a5dd6353a8565bdeb3 100644 (file)
@@ -93,6 +93,7 @@ extern process_t *kernel_process;
 
 process_t *get_current_process();
 dword_t open_process(dword_t pid, handle_t *handle);
+void init_user_stack(uintptr_t *stack_pointer, process_params_t *parameters);
 dword_t create_process(const char *path, dword_t flags, process_params_t *parameters, handle_t *process_handle, handle_t *thread_handle);
 void process_cleanup(object_t *proc);
 void destroy_process(process_t *process);
index b865aad31280e7511c312da77b2a4f9cc047ac7d..c8a572a6ef80da364332b78e81b2dd9137f26182 100644 (file)
@@ -49,10 +49,9 @@ dword_t load_aout(handle_t file, process_params_t *parameters, thread_state_t *i
                   0x200000,
                   MEMORY_BLOCK_ACCESSIBLE | MEMORY_BLOCK_WRITABLE | MEMORY_BLOCK_USERMODE | MEMORY_BLOCK_EVICTABLE);
     if (ret != ERR_SUCCESS) return ret;
-    initial_state->regs.esp = (uintptr_t)address + 0x200000 - (MAX_PARAMETERS * sizeof(uintptr_t)) - sizeof(process_params_t);
 
-    *(process_params_t*)initial_state->regs.esp = *parameters;
-    push_to_stack(&initial_state->regs.esp, initial_state->regs.esp);
+    initial_state->regs.esp = (uintptr_t)address + 0x200000;
+    init_user_stack((uintptr_t*)&initial_state->regs.esp, parameters);
 
     address = (void*)0x100000;
 
index 3bcd5f33b6875836642d15e14a028b582b6e50fc..c50379ba8df39973366a0f49a3fe2dba0d229fe4 100644 (file)
@@ -151,6 +151,45 @@ dword_t load_executable(handle_t file, process_params_t *parameters, thread_stat
     return ERR_INVALID;
 }
 
+void init_user_stack(uintptr_t *stack_pointer, process_params_t *parameters)
+{
+    static const byte_t program_end_code[] = {
+        /* push eax */
+        0x50,
+
+        /* mov eax, SYSCALL_TERMINATE */
+        0xB8,
+        SYSCALL_TERMINATE & 0xFF,
+        (SYSCALL_TERMINATE >> 8) & 0xFF,
+        (SYSCALL_TERMINATE >> 16) & 0xFF,
+        SYSCALL_TERMINATE >> 24,
+
+        /* mov edx, esp */
+        0x8B, 0xD4,
+
+        /* int SYSCALL_INTERRUPT */
+        0xCD, SYSCALL_INTERRUPT
+    };
+
+    uintptr_t stack_top = *stack_pointer;
+
+    *stack_pointer -= (sizeof(process_params_t) + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
+    *(process_params_t*)*stack_pointer = *parameters;
+    uintptr_t parameters_addr = *stack_pointer;
+
+    *stack_pointer -= (sizeof(program_end_code) + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
+    memcpy((void*)*stack_pointer, program_end_code, sizeof(program_end_code));
+    uintptr_t end_code_addr = *stack_pointer;
+
+    if ((stack_top - *stack_pointer) < MAX_PARAMETERS * sizeof(uintptr_t))
+    {
+        *stack_pointer = stack_top - MAX_PARAMETERS * sizeof(uintptr_t);
+    }
+
+    push_to_stack(stack_pointer, parameters_addr);
+    push_to_stack(stack_pointer, end_code_addr);
+}
+
 dword_t create_process(const char *path, dword_t flags, process_params_t *parameters, handle_t *process_handle, handle_t *thread_handle)
 {
     dword_t ret;