Commit b31d98675726045c6d2bda1096424185e54bb020
- Diff rendering mode:
- inline
- side by side
kernel/kernel.cc
(6 / 3)
|   | |||
| 25 | 25 | #include "acpi.h" | |
| 26 | 26 | #include "paging.h" | |
| 27 | 27 | #include "fs/initrd.h" | |
| 28 | #include "fs/vfs.h" | ||
| 29 | 28 | #include "exec/elf32.h" | |
| 29 | #include "process/task.h" | ||
| 30 | 30 | ||
| 31 | 31 | // Symmetry Kernel | |
| 32 | 32 | ||
| … | … | ||
| 35 | 35 | // Show messages on startup? | |
| 36 | 36 | bool quiet_boot = false; | |
| 37 | 37 | ||
| 38 | u32int initial_esp; | ||
| 38 | uintptr initial_esp; | ||
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | 41 | // This is the main function of the kernel - after the bootloader | |
| … | … | ||
| 44 | 44 | // Although this currently just does a few tests and waits for interrupts | |
| 45 | 45 | // after initialising everything, this will eventually just initialise and | |
| 46 | 46 | // then start the init process. | |
| 47 | extern "C" void _main(struct multiboot * mboot_ptr, u32int initial_stack) | ||
| 47 | extern "C" void _main(struct multiboot * mboot_ptr, uintptr initial_stack) | ||
| 48 | 48 | { | |
| 49 | 49 | // Execute static constructors | |
| 50 | 50 | construct(); | |
| … | … | ||
| 102 | 102 | ||
| 103 | 103 | // Mount the initial ramdisk | |
| 104 | 104 | fs_root = initrd::initialise(initrd_address); | |
| 105 | |||
| 106 | // Start multitasking | ||
| 107 | multitasking::initialise(); | ||
| 105 | 108 | } | |
| 106 | 109 | ||
| 107 | 110 | void start_error() |
kernel/process/task.cc
(59 / 1)
|   | |||
| 18 | 18 | #include "iostream.h" | |
| 19 | 19 | #include "scheduler.h" | |
| 20 | 20 | #include "task.h" | |
| 21 | #include "paging.h" | ||
| 21 | 22 | ||
| 23 | extern uintptr initial_esp; | ||
| 24 | extern paging::page_directory_t * current_directory; | ||
| 25 | |||
| 22 | 26 | namespace multitasking | |
| 23 | 27 | { | |
| 28 | void move_stack(void * new_stack_start, size_t size); | ||
| 29 | |||
| 24 | 30 | void initialise() | |
| 25 | 31 | { | |
| 26 | 32 | debug::startup_message("Starting Multitasking"); | |
| 27 | 33 | ||
| 34 | // Relocate the stack to a known location | ||
| 35 | move_stack((void *)0xE0000000, 0x2000); | ||
| 28 | 36 | ||
| 29 | debug::startup_status(true); | ||
| 37 | debug::startup_status(false); | ||
| 38 | } | ||
| 39 | |||
| 40 | void move_stack(void * new_stack_start, size_t size) | ||
| 41 | { | ||
| 42 | // Allocate space for the new stack | ||
| 43 | for(uintptr i = (uintptr)new_stack_start; i >= ((uintptr)new_stack_start - size); i -= PAGE_SIZE) | ||
| 44 | { | ||
| 45 | // The stack should be user mode and writable | ||
| 46 | paging::allocate_frame(paging::get_page(i, true, current_directory), false, true); | ||
| 47 | } | ||
| 48 | |||
| 49 | // Flush the TLB by reading and writing the directory address | ||
| 50 | uintptr pd_addr; | ||
| 51 | asm volatile("mov %%cr3, %0" : "=r" (pd_addr)); | ||
| 52 | asm volatile("mov %0, %%cr3" : : "r" (pd_addr)); | ||
| 53 | |||
| 54 | uintptr old_stack_pointer; | ||
| 55 | uintptr old_base_pointer; | ||
| 56 | |||
| 57 | // Retrieve the old stack and base pointers | ||
| 58 | asm volatile("mov %%esp, %0" : "=r" (old_stack_pointer)); | ||
| 59 | asm volatile("mov %%ebp, %0" : "=r" (old_base_pointer)); | ||
| 60 | |||
| 61 | // Calculate offset and stack and base pointers to our new stack | ||
| 62 | uintptr offset = (uintptr)new_stack_start - initial_esp; | ||
| 63 | uintptr new_stack_pointer = old_stack_pointer + offset; | ||
| 64 | uintptr new_base_pointer = old_base_pointer + offset; | ||
| 65 | |||
| 66 | // Copy the old stack to our net stack location | ||
| 67 | memcpy((void *)new_stack_pointer, (void *)old_stack_pointer, initial_esp - old_stack_pointer); | ||
| 68 | |||
| 69 | // Recurse through all the values in the new stack | ||
| 70 | for(uintptr i = (uintptr)new_stack_start; i < (uintptr)new_stack_start - size; i -= 4) | ||
| 71 | { | ||
| 72 | uintptr temp = *(uintptr *)i; | ||
| 73 | |||
| 74 | // Assume that temp is a base pointer if it is in the range of the old stack, and | ||
| 75 | // translate it to our new stack location. | ||
| 76 | // This will also remap any other pointers that coincidentally fall in this range. | ||
| 77 | if((old_stack_pointer < temp) && (temp < initial_esp)) | ||
| 78 | { | ||
| 79 | temp = temp + offset; | ||
| 80 | uintptr * temp2 = (uintptr *)i; | ||
| 81 | *temp2 = temp; | ||
| 82 | } | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Switch to the new stack | ||
| 86 | asm volatile("mov %0, %%esp" : : "r" (new_stack_pointer)); | ||
| 87 | asm volatile("mov %0, %%ebp" : : "r" (new_base_pointer)); | ||
| 30 | 88 | } | |
| 31 | 89 | } |

