1 /* 2 * Copyright 2004-2007, Axel Dörfler, axeld@pinc-software.de. 3 * Based on code written by Travis Geiselbrecht for NewOS. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 #include "mmu.h" 10 11 #include <boot/platform.h> 12 #include <boot/stdio.h> 13 #include <boot/kernel_args.h> 14 #include <boot/stage2.h> 15 #include <arch/cpu.h> 16 #include <arch_kernel.h> 17 #include <kernel.h> 18 19 #include <OS.h> 20 21 #include <string.h> 22 23 extern uint8 gStackEnd; 24 25 uint8* gMemBase = NULL; 26 size_t gTotalMem = 0; 27 uint8* gFreeMem = &gStackEnd; 28 29 30 // #pragma mark - 31 32 extern "C" status_t 33 platform_allocate_region(void** _address, size_t size, uint8 protection, 34 bool exactAddress) 35 { 36 if (exactAddress) 37 return B_ERROR; 38 39 gFreeMem = (uint8*)(((addr_t)gFreeMem 40 + (B_PAGE_SIZE - 1)) / B_PAGE_SIZE * B_PAGE_SIZE); 41 *_address = gFreeMem; 42 gFreeMem += size; 43 44 return B_OK; 45 } 46 47 48 extern "C" status_t 49 platform_free_region(void* address, size_t size) 50 { 51 return B_OK; 52 } 53 54 55 void 56 platform_release_heap(struct stage2_args* args, void* base) 57 { 58 } 59 60 61 status_t 62 platform_init_heap(struct stage2_args* args, void** _base, void** _top) 63 { 64 void* heap = (void*)gFreeMem; 65 gFreeMem += args->heap_size; 66 67 *_base = heap; 68 *_top = (void*)((int8*)heap + args->heap_size); 69 return B_OK; 70 } 71 72 73 status_t 74 platform_bootloader_address_to_kernel_address(void* address, addr_t* _result) 75 { 76 *_result = (addr_t)address; 77 return B_OK; 78 } 79 80 81 status_t 82 platform_kernel_address_to_bootloader_address(addr_t address, void** _result) 83 { 84 *_result = (void*)address; 85 return B_OK; 86 } 87 88 89 // #pragma mark - 90 91 void 92 mmu_init(void) 93 { 94 } 95 96 97 void 98 mmu_init_for_kernel(void) 99 { 100 // map in a kernel stack 101 void* stack_address = NULL; 102 if (platform_allocate_region(&stack_address, 103 KERNEL_STACK_SIZE + KERNEL_STACK_GUARD_PAGES * B_PAGE_SIZE, 0, false) 104 != B_OK) { 105 panic("Unabled to allocate a stack"); 106 } 107 gKernelArgs.cpu_kstack[0].start = (addr_t)stack_address; 108 gKernelArgs.cpu_kstack[0].size = KERNEL_STACK_SIZE 109 + KERNEL_STACK_GUARD_PAGES * B_PAGE_SIZE; 110 dprintf("Kernel stack at %#lx\n", gKernelArgs.cpu_kstack[0].start); 111 112 gKernelArgs.physical_memory_range[0].start = (addr_t)gMemBase; 113 gKernelArgs.physical_memory_range[0].size = gTotalMem; 114 gKernelArgs.num_physical_memory_ranges = 1; 115 116 gKernelArgs.physical_allocated_range[0].start = (addr_t)gMemBase; 117 gKernelArgs.physical_allocated_range[0].size = gFreeMem - gMemBase; 118 gKernelArgs.num_physical_allocated_ranges = 1; 119 120 gKernelArgs.virtual_allocated_range[0].start = (addr_t)gMemBase; 121 gKernelArgs.virtual_allocated_range[0].size = gFreeMem - gMemBase; 122 gKernelArgs.num_virtual_allocated_ranges = 1; 123 } 124