Implement serial logging. Finish modularizing power management.
[monolithium.git] / kernel / src / process.c
index 4bd790136cdf4b5045d0132453bacfdc18667642..097e8a845c729b38033e4a3c1541f39a43c05264 100644 (file)
 #include <exec/aout.h>
 #include <cpu.h>
 
-dword_t load_elf(handle_t file, process_params_t *parameters, thread_state_t *initial_state);
+dword_t load_elf(handle_t file, thread_state_t *initial_state);
+dword_t load_aout(handle_t file, thread_state_t *initial_state);
 
-typedef dword_t (*loader_proc)(handle_t, process_params_t*, thread_state_t*);
+typedef dword_t (*loader_proc)(handle_t, thread_state_t*);
 
 static dword_t pid_alloc_bitmap[MAX_PROCESSES / 32];
 static lock_t pid_bitmap_lock = 0;
@@ -141,13 +142,13 @@ sysret_t syscall_open_process(dword_t pid, handle_t *handle)
     return ret;
 }
 
-dword_t load_executable(handle_t file, process_params_t *parameters, thread_state_t *initial_state)
+dword_t load_executable(handle_t file, thread_state_t *initial_state)
 {
     loader_proc *loader = loaders;
 
     while (*loader)
     {
-        dword_t ret = (*loader++)(file, parameters, initial_state);
+        dword_t ret = (*loader++)(file, initial_state);
         if (ret == ERR_INVALID) continue;
         return ret;
     }
@@ -157,52 +158,25 @@ dword_t load_executable(handle_t file, process_params_t *parameters, thread_stat
 
 void init_user_stack(uintptr_t *stack_pointer, process_params_t *parameters)
 {
-    static const byte_t program_end_code[] = {
-        /* push eax */
-        0x50,
-
-        /* push INVALID_HANDLE */
-        0x68,
-        INVALID_HANDLE & 0xFF,
-        (INVALID_HANDLE >> 8) & 0xFF,
-        (INVALID_HANDLE >> 16) & 0xFF,
-        (INVALID_HANDLE >> 24) & 0xFF,
-
-        /* 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 -= (strlen(parameters->command_line) + 1 + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
-    strcpy((char*)*stack_pointer, parameters->command_line);
-    parameters->command_line = (char*)*stack_pointer;
+    if (parameters->command_line)
+    {
+        *stack_pointer -= (strlen(parameters->command_line) + 1 + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
+        strcpy((char*)*stack_pointer, parameters->command_line);
+        parameters->command_line = (char*)*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;
+    parameters = (process_params_t*)*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);
+    init_thread_stack(stack_pointer, parameters);
 }
 
 sysret_t syscall_create_process(const char *path, dword_t flags, process_params_t *parameters, handle_t *process_handle, handle_t *thread_handle)
@@ -247,12 +221,18 @@ sysret_t syscall_create_process(const char *path, dword_t flags, process_params_
             }
 
             safe_params.command_line = cmdline;
+            parameters = &safe_params;
         }
     }
     else
     {
         safe_path = (char*)path;
-        if (parameters) safe_params = *parameters;
+
+        if (parameters)
+        {
+            safe_params = *parameters;
+            parameters = &safe_params;
+        }
     }
 
     if (safe_path)
@@ -341,7 +321,8 @@ sysret_t syscall_create_process(const char *path, dword_t flags, process_params_
     if (file != INVALID_HANDLE)
     {
         process_t *old_process = switch_process(proc);
-        ret = load_executable(file, parameters ? &safe_params : NULL, &initial_state);
+        ret = load_executable(file, &initial_state);
+        init_user_stack((uintptr_t*)&initial_state.regs.esp, parameters);
         switch_process(old_process);
         if (ret != ERR_SUCCESS) goto cleanup;
     }
@@ -407,9 +388,8 @@ cleanup:
             syscall(SYSCALL_TERMINATE, proc->pid, 1);
             if (safe_process_handle != INVALID_HANDLE) syscall_close_object(safe_process_handle);
             if (safe_thread_handle != INVALID_HANDLE) syscall_close_object(safe_thread_handle);
+            dereference(&proc->header);
         }
-
-        dereference(&proc->header);
     }
     else
     {
@@ -751,7 +731,7 @@ process_t *switch_process(process_t *new_process)
     return old_process;
 }
 
-void process_init(char *root_directory)
+void process_init(void)
 {
     memset(pid_alloc_bitmap, 0, sizeof(pid_alloc_bitmap));