1 /* 2 * Copyright 2007-2010, François Revol, revol@free.fr. 3 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights 5 * reserved. 6 * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk. 7 * Distributed under the terms of the MIT License. 8 * 9 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 10 * Distributed under the terms of the NewOS License. 11 */ 12 13 14 #include <arch_cpu_defs.h> 15 #include <boot/kernel_args.h> 16 #include <KernelExport.h> 17 #include <kernel.h> 18 #include <vm/vm.h> 19 #include <vm/vm_priv.h> 20 #include <vm/VMAddressSpace.h> 21 #include <Clint.h> 22 #include <Htif.h> 23 #include <Plic.h> 24 25 #include "RISCV64VMTranslationMap.h" 26 27 28 #define TRACE_VM_TMAP 29 #ifdef TRACE_VM_TMAP 30 # define TRACE(x...) dprintf(x) 31 #else 32 # define TRACE(x...) ; 33 #endif 34 35 36 ssize_t gVirtFromPhysOffset = 0; 37 38 phys_addr_t sPageTable = 0; 39 char sPhysicalPageMapperData[sizeof(RISCV64VMPhysicalPageMapper)]; 40 41 42 // TODO: Consolidate function with RISCV64VMTranslationMap 43 44 static Pte* 45 LookupPte(addr_t virtAdr, bool alloc, kernel_args* args, 46 phys_addr_t (*get_free_page)(kernel_args *)) 47 { 48 Pte *pte = (Pte*)VirtFromPhys(sPageTable); 49 for (int level = 2; level > 0; level --) { 50 pte += VirtAdrPte(virtAdr, level); 51 if (!((1 << pteValid) & pte->flags)) { 52 if (!alloc) 53 return NULL; 54 pte->ppn = get_free_page(args); 55 if (pte->ppn == 0) 56 return NULL; 57 memset((Pte*)VirtFromPhys(B_PAGE_SIZE * pte->ppn), 0, B_PAGE_SIZE); 58 pte->flags |= (1 << pteValid); 59 } 60 pte = (Pte*)VirtFromPhys(B_PAGE_SIZE * pte->ppn); 61 } 62 pte += VirtAdrPte(virtAdr, 0); 63 return pte; 64 } 65 66 67 static void 68 Map(addr_t virtAdr, phys_addr_t physAdr, uint64 flags, kernel_args* args, 69 phys_addr_t (*get_free_page)(kernel_args *)) 70 { 71 // dprintf("Map(0x%" B_PRIxADDR ", 0x%" B_PRIxADDR ")\n", virtAdr, physAdr); 72 Pte* pte = LookupPte(virtAdr, true, args, get_free_page); 73 if (pte == NULL) panic("can't allocate page table"); 74 75 pte->ppn = physAdr / B_PAGE_SIZE; 76 pte->flags = (1 << pteValid) | (1 << pteAccessed) | (1 << pteDirty) | flags; 77 78 FlushTlbPage(virtAdr); 79 } 80 81 82 //#pragma mark - 83 84 status_t 85 arch_vm_translation_map_init(kernel_args *args, 86 VMPhysicalPageMapper** _physicalPageMapper) 87 { 88 TRACE("vm_translation_map_init: entry\n"); 89 90 #ifdef TRACE_VM_TMAP 91 TRACE("physical memory ranges:\n"); 92 for (uint32 i = 0; i < args->num_physical_memory_ranges; i++) { 93 phys_addr_t start = args->physical_memory_range[i].start; 94 phys_addr_t end = start + args->physical_memory_range[i].size; 95 TRACE(" %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end); 96 } 97 98 TRACE("allocated physical ranges:\n"); 99 for (uint32 i = 0; i < args->num_physical_allocated_ranges; i++) { 100 phys_addr_t start = args->physical_allocated_range[i].start; 101 phys_addr_t end = start + args->physical_allocated_range[i].size; 102 TRACE(" %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end); 103 } 104 105 TRACE("allocated virtual ranges:\n"); 106 for (uint32 i = 0; i < args->num_virtual_allocated_ranges; i++) { 107 addr_t start = args->virtual_allocated_range[i].start; 108 addr_t end = start + args->virtual_allocated_range[i].size; 109 TRACE(" %" B_PRIxADDR " - %" B_PRIxADDR "\n", start, end); 110 } 111 112 TRACE("kernel args ranges:\n"); 113 for (uint32 i = 0; i < args->num_kernel_args_ranges; i++) { 114 phys_addr_t start = args->kernel_args_range[i].start; 115 phys_addr_t end = start + args->kernel_args_range[i].size; 116 TRACE(" %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end); 117 } 118 #endif 119 120 sPageTable = SatpReg{.val = Satp()}.ppn * B_PAGE_SIZE; 121 122 dprintf("physMapBase: %#" B_PRIxADDR "\n", args->arch_args.physMap.start); 123 dprintf("physMemBase: %#" B_PRIxADDR "\n", args->physical_memory_range[0].start); 124 gVirtFromPhysOffset = args->arch_args.physMap.start - args->physical_memory_range[0].start; 125 126 clear_ac(); 127 128 *_physicalPageMapper = new(&sPhysicalPageMapperData) 129 RISCV64VMPhysicalPageMapper(); 130 131 return B_OK; 132 } 133 134 135 status_t 136 arch_vm_translation_map_init_post_sem(kernel_args *args) 137 { 138 return B_OK; 139 } 140 141 142 status_t 143 arch_vm_translation_map_init_post_area(kernel_args *args) 144 { 145 TRACE("vm_translation_map_init_post_area: entry\n"); 146 return B_OK; 147 } 148 149 150 status_t 151 arch_vm_translation_map_early_map(kernel_args *args, 152 addr_t virtAdr, phys_addr_t physAdr, uint8 attributes, 153 phys_addr_t (*get_free_page)(kernel_args *)) 154 { 155 //dprintf("early_map(%#" B_PRIxADDR ", %#" B_PRIxADDR ")\n", virtAdr, physAdr); 156 uint64 flags = 0; 157 if ((attributes & B_KERNEL_READ_AREA) != 0) 158 flags |= (1 << pteRead); 159 if ((attributes & B_KERNEL_WRITE_AREA) != 0) 160 flags |= (1 << pteWrite); 161 if ((attributes & B_KERNEL_EXECUTE_AREA) != 0) 162 flags |= (1 << pteExec); 163 Map(virtAdr, physAdr, flags, args, get_free_page); 164 return B_OK; 165 } 166 167 168 status_t 169 arch_vm_translation_map_create_map(bool kernel, VMTranslationMap** _map) 170 { 171 *_map = new(std::nothrow) RISCV64VMTranslationMap(kernel, 172 (kernel) ? sPageTable : 0); 173 174 if (*_map == NULL) 175 return B_NO_MEMORY; 176 177 return B_OK; 178 } 179 180 181 bool 182 arch_vm_translation_map_is_kernel_page_accessible(addr_t virtualAddress, 183 uint32 protection) 184 { 185 return virtualAddress != 0; 186 } 187