1 /* 2 * Copyright 2010, Ithamar R. Adema, ithamar.adema@team-embedded.nl 3 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 5 * Distributed under the terms of the MIT License. 6 * 7 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 8 * Distributed under the terms of the NewOS License. 9 */ 10 11 12 #include "paging/32bit/ARMPagingMethod32Bit.h" 13 14 #include <stdlib.h> 15 #include <string.h> 16 17 #include <AutoDeleter.h> 18 19 #include <arch/smp.h> 20 #include <arch_system_info.h> 21 #include <boot/kernel_args.h> 22 #include <int.h> 23 #include <thread.h> 24 #include <vm/vm.h> 25 #include <vm/VMAddressSpace.h> 26 27 #include "paging/32bit/ARMPagingStructures32Bit.h" 28 #include "paging/32bit/ARMVMTranslationMap32Bit.h" 29 #include "paging/arm_physical_page_mapper.h" 30 #include "paging/arm_physical_page_mapper_large_memory.h" 31 32 33 //#define TRACE_ARM_PAGING_METHOD_32_BIT 34 #ifdef TRACE_ARM_PAGING_METHOD_32_BIT 35 # define TRACE(x...) dprintf(x) 36 #else 37 # define TRACE(x...) ; 38 #endif 39 40 41 #define MAX_INITIAL_POOLS \ 42 (ROUNDUP(SMP_MAX_CPUS * TOTAL_SLOTS_PER_CPU + EXTRA_SLOTS, 1024) / 1024) 43 44 45 using ARMLargePhysicalPageMapper::PhysicalPageSlot; 46 47 48 // #pragma mark - X86PagingMethod32Bit::PhysicalPageSlotPool 49 50 struct ARMPagingMethod32Bit::PhysicalPageSlotPool 51 : ARMLargePhysicalPageMapper::PhysicalPageSlotPool { 52 public: 53 virtual ~PhysicalPageSlotPool(); 54 55 status_t InitInitial(kernel_args* args); 56 status_t InitInitialPostArea(kernel_args* args); 57 58 void Init(area_id dataArea, void* data, 59 area_id virtualArea, addr_t virtualBase); 60 61 virtual status_t AllocatePool( 62 ARMLargePhysicalPageMapper 63 ::PhysicalPageSlotPool*& _pool); 64 virtual void Map(phys_addr_t physicalAddress, 65 addr_t virtualAddress); 66 67 public: 68 static PhysicalPageSlotPool sInitialPhysicalPagePool[MAX_INITIAL_POOLS]; 69 70 private: 71 area_id fDataArea; 72 area_id fVirtualArea; 73 addr_t fVirtualBase; 74 page_table_entry* fPageTable; 75 }; 76 77 78 ARMPagingMethod32Bit::PhysicalPageSlotPool 79 ARMPagingMethod32Bit::PhysicalPageSlotPool::sInitialPhysicalPagePool[ 80 MAX_INITIAL_POOLS]; 81 82 83 ARMPagingMethod32Bit::PhysicalPageSlotPool::~PhysicalPageSlotPool() 84 { 85 } 86 87 88 status_t 89 ARMPagingMethod32Bit::PhysicalPageSlotPool::InitInitial(kernel_args* args) 90 { 91 // allocate a virtual address range for the pages to be mapped into 92 addr_t virtualBase = vm_allocate_early(args, 1024 * B_PAGE_SIZE, 0, 0, 93 kPageTableAlignment); 94 if (virtualBase == 0) { 95 panic("LargeMemoryPhysicalPageMapper::Init(): Failed to reserve " 96 "physical page pool space in virtual address space!"); 97 return B_ERROR; 98 } 99 100 // allocate memory for the page table and data 101 size_t areaSize = B_PAGE_SIZE + sizeof(PhysicalPageSlot[1024]); 102 page_table_entry* pageTable = (page_table_entry*)vm_allocate_early(args, 103 areaSize, ~0L, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, 0); 104 if (pageTable == 0) { 105 panic("ARMPagingMethod32Bit::PhysicalPageSlotPool::InitInitial(): " 106 "Failed to allocate memory for page table!"); 107 return B_ERROR; 108 } 109 110 // prepare the page table 111 _EarlyPreparePageTables(pageTable, virtualBase, 1024 * B_PAGE_SIZE); 112 113 // init the pool structure and add the initial pool 114 Init(-1, pageTable, -1, (addr_t)virtualBase); 115 116 return B_OK; 117 } 118 119 120 status_t 121 ARMPagingMethod32Bit::PhysicalPageSlotPool::InitInitialPostArea( 122 kernel_args* args) 123 { 124 // create an area for the (already allocated) data 125 size_t areaSize = B_PAGE_SIZE + sizeof(PhysicalPageSlot[1024]); 126 void* temp = fPageTable; 127 area_id area = create_area("physical page pool", &temp, 128 B_EXACT_ADDRESS, areaSize, B_ALREADY_WIRED, 129 B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 130 if (area < 0) { 131 panic("LargeMemoryPhysicalPageMapper::InitPostArea(): Failed to " 132 "create area for physical page pool."); 133 return area; 134 } 135 fDataArea = area; 136 137 // create an area for the virtual address space 138 temp = (void*)fVirtualBase; 139 area = vm_create_null_area(VMAddressSpace::KernelID(), 140 "physical page pool space", &temp, B_EXACT_ADDRESS, 141 1024 * B_PAGE_SIZE, 0); 142 if (area < B_OK) { 143 panic("LargeMemoryPhysicalPageMapper::InitPostArea(): Failed to " 144 "create area for physical page pool space."); 145 return area; 146 } 147 fVirtualArea = area; 148 149 return B_OK; 150 } 151 152 153 void 154 ARMPagingMethod32Bit::PhysicalPageSlotPool::Init(area_id dataArea, void* data, 155 area_id virtualArea, addr_t virtualBase) 156 { 157 fDataArea = dataArea; 158 fVirtualArea = virtualArea; 159 fVirtualBase = virtualBase; 160 fPageTable = (page_table_entry*)data; 161 162 // init slot list 163 fSlots = (PhysicalPageSlot*)(fPageTable + 1024); 164 addr_t slotAddress = virtualBase; 165 for (int32 i = 0; i < 1024; i++, slotAddress += B_PAGE_SIZE) { 166 PhysicalPageSlot* slot = &fSlots[i]; 167 slot->next = slot + 1; 168 slot->pool = this; 169 slot->address = slotAddress; 170 } 171 172 fSlots[1023].next = NULL; 173 // terminate list 174 } 175 176 177 void 178 ARMPagingMethod32Bit::PhysicalPageSlotPool::Map(phys_addr_t physicalAddress, 179 addr_t virtualAddress) 180 { 181 page_table_entry& pte = fPageTable[ 182 (virtualAddress - fVirtualBase) / B_PAGE_SIZE]; 183 pte = (physicalAddress & ARM_PTE_ADDRESS_MASK) 184 | ARM_MMU_L2_TYPE_SMALLEXT; 185 186 arch_cpu_invalidate_TLB_range(virtualAddress, virtualAddress + B_PAGE_SIZE); 187 // invalidate_TLB(virtualAddress); 188 } 189 190 191 status_t 192 ARMPagingMethod32Bit::PhysicalPageSlotPool::AllocatePool( 193 ARMLargePhysicalPageMapper::PhysicalPageSlotPool*& _pool) 194 { 195 // create the pool structure 196 PhysicalPageSlotPool* pool = new(std::nothrow) PhysicalPageSlotPool; 197 if (pool == NULL) 198 return B_NO_MEMORY; 199 ObjectDeleter<PhysicalPageSlotPool> poolDeleter(pool); 200 201 // create an area that can contain the page table and the slot 202 // structures 203 size_t areaSize = B_PAGE_SIZE + sizeof(PhysicalPageSlot[1024]); 204 void* data; 205 virtual_address_restrictions virtualRestrictions = {}; 206 virtualRestrictions.address_specification = B_ANY_KERNEL_ADDRESS; 207 physical_address_restrictions physicalRestrictions = {}; 208 area_id dataArea = create_area_etc(B_SYSTEM_TEAM, "physical page pool", 209 PAGE_ALIGN(areaSize), B_FULL_LOCK, 210 B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, CREATE_AREA_DONT_WAIT, 0, 211 &virtualRestrictions, &physicalRestrictions, &data); 212 if (dataArea < 0) 213 return dataArea; 214 215 // create the null area for the virtual address space 216 void* virtualBase; 217 area_id virtualArea = vm_create_null_area( 218 VMAddressSpace::KernelID(), "physical page pool space", 219 &virtualBase, B_ANY_KERNEL_BLOCK_ADDRESS, 1024 * B_PAGE_SIZE, 220 CREATE_AREA_PRIORITY_VIP); 221 if (virtualArea < 0) { 222 delete_area(dataArea); 223 return virtualArea; 224 } 225 226 // prepare the page table 227 memset(data, 0, B_PAGE_SIZE); 228 229 // get the page table's physical address 230 phys_addr_t physicalTable; 231 ARMVMTranslationMap32Bit* map = static_cast<ARMVMTranslationMap32Bit*>( 232 VMAddressSpace::Kernel()->TranslationMap()); 233 uint32 dummyFlags; 234 cpu_status state = disable_interrupts(); 235 map->QueryInterrupt((addr_t)data, &physicalTable, &dummyFlags); 236 restore_interrupts(state); 237 238 // put the page table into the page directory 239 int32 index = VADDR_TO_PDENT((addr_t)virtualBase); 240 page_directory_entry* entry 241 = &map->PagingStructures32Bit()->pgdir_virt[index]; 242 PutPageTableInPageDir(entry, physicalTable, 243 B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 244 ARMPagingStructures32Bit::UpdateAllPageDirs(index, *entry); 245 246 // init the pool structure 247 pool->Init(dataArea, data, virtualArea, (addr_t)virtualBase); 248 poolDeleter.Detach(); 249 _pool = pool; 250 return B_OK; 251 } 252 253 254 // #pragma mark - ARMPagingMethod32Bit 255 256 257 ARMPagingMethod32Bit::ARMPagingMethod32Bit() 258 : 259 fKernelPhysicalPageDirectory(0), 260 fKernelVirtualPageDirectory(NULL), 261 fPhysicalPageMapper(NULL), 262 fKernelPhysicalPageMapper(NULL) 263 { 264 } 265 266 267 ARMPagingMethod32Bit::~ARMPagingMethod32Bit() 268 { 269 } 270 271 272 status_t 273 ARMPagingMethod32Bit::Init(kernel_args* args, 274 VMPhysicalPageMapper** _physicalPageMapper) 275 { 276 TRACE("X86PagingMethod32Bit::Init(): entry\n"); 277 278 fKernelPhysicalPageDirectory = args->arch_args.phys_pgdir; 279 fKernelVirtualPageDirectory = (page_directory_entry*) 280 args->arch_args.vir_pgdir; 281 282 #ifdef TRACE_X86_PAGING_METHOD_32_BIT 283 TRACE("page dir: %p (physical: %#" B_PRIx32 ")\n", 284 fKernelVirtualPageDirectory, fKernelPhysicalPageDirectory); 285 #endif 286 287 ARMPagingStructures32Bit::StaticInit(); 288 289 // create the initial pools for the physical page mapper 290 int32 poolCount = _GetInitialPoolCount(); 291 PhysicalPageSlotPool* pool = PhysicalPageSlotPool::sInitialPhysicalPagePool; 292 293 for (int32 i = 0; i < poolCount; i++) { 294 new(&pool[i]) PhysicalPageSlotPool; 295 status_t error = pool[i].InitInitial(args); 296 if (error != B_OK) { 297 panic("ARMPagingMethod32Bit::Init(): Failed to create initial pool " 298 "for physical page mapper!"); 299 return error; 300 } 301 } 302 303 // create physical page mapper 304 large_memory_physical_page_ops_init(args, pool, poolCount, sizeof(*pool), 305 fPhysicalPageMapper, fKernelPhysicalPageMapper); 306 // TODO: Select the best page mapper! 307 308 // enable global page feature if available 309 #if 0 //IRA: check for ARMv6!! 310 if (x86_check_feature(IA32_FEATURE_PGE, FEATURE_COMMON)) { 311 // this prevents kernel pages from being flushed from TLB on 312 // context-switch 313 x86_write_cr4(x86_read_cr4() | IA32_CR4_GLOBAL_PAGES); 314 } 315 #endif 316 TRACE("ARMPagingMethod32Bit::Init(): done\n"); 317 318 *_physicalPageMapper = fPhysicalPageMapper; 319 return B_OK; 320 } 321 322 323 status_t 324 ARMPagingMethod32Bit::InitPostArea(kernel_args* args) 325 { 326 void *temp; 327 area_id area; 328 329 temp = (void*)fKernelVirtualPageDirectory; 330 area = create_area("kernel_pgdir", &temp, B_EXACT_ADDRESS, args->arch_args.next_pagetable, 331 B_ALREADY_WIRED, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 332 ASSERT_PRINT(area >= 0, "Failed mapping the kernel page directory: 0x%08lx!", area); 333 334 int32 poolCount = _GetInitialPoolCount(); 335 for (int32 i = 0; i < poolCount; i++) { 336 status_t error = PhysicalPageSlotPool::sInitialPhysicalPagePool[i] 337 .InitInitialPostArea(args); 338 if (error != B_OK) 339 return error; 340 } 341 342 return B_OK; 343 } 344 345 346 status_t 347 ARMPagingMethod32Bit::CreateTranslationMap(bool kernel, VMTranslationMap** _map) 348 { 349 ARMVMTranslationMap32Bit* map = new(std::nothrow) ARMVMTranslationMap32Bit; 350 if (map == NULL) 351 return B_NO_MEMORY; 352 353 status_t error = map->Init(kernel); 354 if (error != B_OK) { 355 delete map; 356 return error; 357 } 358 359 *_map = map; 360 return B_OK; 361 } 362 363 364 static phys_addr_t 365 get_free_pgtable(kernel_args* args) 366 { 367 phys_addr_t phys = args->arch_args.phys_pgdir + args->arch_args.next_pagetable; 368 //addr_t virt = args->arch_args.vir_pgdir + args->arch_args.next_pagetable; 369 args->arch_args.next_pagetable += ARM_MMU_L2_COARSE_TABLE_SIZE; 370 return phys; 371 } 372 373 status_t 374 ARMPagingMethod32Bit::MapEarly(kernel_args* args, addr_t virtualAddress, 375 phys_addr_t physicalAddress, uint8 attributes, 376 page_num_t (*get_free_page)(kernel_args*)) 377 { 378 // check to see if a page table exists for this range 379 int index = VADDR_TO_PDENT(virtualAddress); 380 if ((fKernelVirtualPageDirectory[index] & ARM_PDE_TYPE_MASK) == 0) { 381 phys_addr_t pgtable; 382 page_directory_entry *e; 383 // we need to allocate a pgtable 384 pgtable = get_free_pgtable(args); 385 386 TRACE("ARMPagingMethod32Bit::MapEarly(): asked for free page for " 387 "pgtable. %#" B_PRIxPHYSADDR "\n", pgtable); 388 389 // put it in the pgdir 390 e = &fKernelVirtualPageDirectory[index]; 391 PutPageTableInPageDir(e, pgtable, attributes); 392 393 // zero it out in it's new mapping 394 memset((void*)pgtable, 0, B_PAGE_SIZE); 395 } 396 397 page_table_entry* ptEntry = (page_table_entry*) 398 (fKernelVirtualPageDirectory[index] & ARM_PDE_ADDRESS_MASK); 399 ptEntry += VADDR_TO_PTENT(virtualAddress); 400 401 ASSERT_PRINT( 402 (*ptEntry & ARM_PTE_TYPE_MASK) == 0, 403 "virtual address: %#" B_PRIxADDR ", pde: %#" B_PRIx32 404 ", existing pte: %#" B_PRIx32, virtualAddress, fKernelVirtualPageDirectory[index], 405 *ptEntry); 406 407 // now, fill in the pentry 408 PutPageTableEntryInTable(ptEntry, 409 physicalAddress, attributes, 0, IS_KERNEL_ADDRESS(virtualAddress)); 410 411 return B_OK; 412 } 413 414 415 bool 416 ARMPagingMethod32Bit::IsKernelPageAccessible(addr_t virtualAddress, 417 uint32 protection) 418 { 419 #if 0 420 // We only trust the kernel team's page directory. So switch to it first. 421 // Always set it to make sure the TLBs don't contain obsolete data. 422 uint32 physicalPageDirectory = x86_read_cr3(); 423 x86_write_cr3(fKernelPhysicalPageDirectory); 424 425 // get the page directory entry for the address 426 page_directory_entry pageDirectoryEntry; 427 uint32 index = VADDR_TO_PDENT(virtualAddress); 428 429 if (physicalPageDirectory == fKernelPhysicalPageDirectory) { 430 pageDirectoryEntry = fKernelVirtualPageDirectory[index]; 431 } else if (fPhysicalPageMapper != NULL) { 432 // map the original page directory and get the entry 433 void* handle; 434 addr_t virtualPageDirectory; 435 status_t error = fPhysicalPageMapper->GetPageDebug( 436 physicalPageDirectory, &virtualPageDirectory, &handle); 437 if (error == B_OK) { 438 pageDirectoryEntry 439 = ((page_directory_entry*)virtualPageDirectory)[index]; 440 fPhysicalPageMapper->PutPageDebug(virtualPageDirectory, handle); 441 } else 442 pageDirectoryEntry = 0; 443 } else 444 pageDirectoryEntry = 0; 445 446 // map the page table and get the entry 447 page_table_entry pageTableEntry; 448 index = VADDR_TO_PTENT(virtualAddress); 449 450 if ((pageDirectoryEntry & X86_PDE_PRESENT) != 0 451 && fPhysicalPageMapper != NULL) { 452 void* handle; 453 addr_t virtualPageTable; 454 status_t error = fPhysicalPageMapper->GetPageDebug( 455 pageDirectoryEntry & X86_PDE_ADDRESS_MASK, &virtualPageTable, 456 &handle); 457 if (error == B_OK) { 458 pageTableEntry = ((page_table_entry*)virtualPageTable)[index]; 459 fPhysicalPageMapper->PutPageDebug(virtualPageTable, handle); 460 } else 461 pageTableEntry = 0; 462 } else 463 pageTableEntry = 0; 464 465 // switch back to the original page directory 466 if (physicalPageDirectory != fKernelPhysicalPageDirectory) 467 x86_write_cr3(physicalPageDirectory); 468 469 if ((pageTableEntry & X86_PTE_PRESENT) == 0) 470 return false; 471 472 // present means kernel-readable, so check for writable 473 return (protection & B_KERNEL_WRITE_AREA) == 0 474 || (pageTableEntry & X86_PTE_WRITABLE) != 0; 475 #endif 476 //IRA: fix the above! 477 return true; 478 } 479 480 481 /*static*/ void 482 ARMPagingMethod32Bit::PutPageTableInPageDir(page_directory_entry* entry, 483 phys_addr_t pgtablePhysical, uint32 attributes) 484 { 485 *entry = (pgtablePhysical & ARM_PDE_ADDRESS_MASK) | ARM_MMU_L1_TYPE_COARSE; 486 // TODO: we ignore the attributes of the page table - for compatibility 487 // with BeOS we allow having user accessible areas in the kernel address 488 // space. This is currently being used by some drivers, mainly for the 489 // frame buffer. Our current real time data implementation makes use of 490 // this fact, too. 491 // We might want to get rid of this possibility one day, especially if 492 // we intend to port it to a platform that does not support this. 493 } 494 495 496 /*static*/ void 497 ARMPagingMethod32Bit::PutPageTableEntryInTable(page_table_entry* entry, 498 phys_addr_t physicalAddress, uint32 attributes, uint32 memoryType, 499 bool globalPage) 500 { 501 page_table_entry page = (physicalAddress & ARM_PTE_ADDRESS_MASK) 502 | ARM_MMU_L2_TYPE_SMALLEXT; 503 #if 0 //IRA 504 | X86_PTE_PRESENT | (globalPage ? X86_PTE_GLOBAL : 0) 505 | MemoryTypeToPageTableEntryFlags(memoryType); 506 507 // if the page is user accessible, it's automatically 508 // accessible in kernel space, too (but with the same 509 // protection) 510 if ((attributes & B_USER_PROTECTION) != 0) { 511 page |= X86_PTE_USER; 512 if ((attributes & B_WRITE_AREA) != 0) 513 page |= X86_PTE_WRITABLE; 514 } else if ((attributes & B_KERNEL_WRITE_AREA) != 0) 515 page |= X86_PTE_WRITABLE; 516 #endif 517 // put it in the page table 518 *(volatile page_table_entry*)entry = page; 519 } 520 521 522 inline int32 523 ARMPagingMethod32Bit::_GetInitialPoolCount() 524 { 525 int32 requiredSlots = smp_get_num_cpus() * TOTAL_SLOTS_PER_CPU 526 + EXTRA_SLOTS; 527 return (requiredSlots + 1023) / 1024; 528 } 529 530 531 /*static*/ void 532 ARMPagingMethod32Bit::_EarlyPreparePageTables(page_table_entry* pageTables, 533 addr_t address, size_t size) 534 { 535 ARMPagingMethod32Bit* method = ARMPagingMethod32Bit::Method(); 536 memset(pageTables, 0, 256 * (size / (B_PAGE_SIZE * 256))); 537 538 // put the array of pgtables directly into the kernel pagedir 539 // these will be wired and kept mapped into virtual space to be easy to get 540 // to 541 { 542 addr_t virtualTable = (addr_t)pageTables; 543 544 for (size_t i = 0; i < (size / (B_PAGE_SIZE * 256)); 545 i++, virtualTable += 256*sizeof(page_directory_entry)) { 546 phys_addr_t physicalTable = 0; 547 _EarlyQuery(virtualTable, &physicalTable); 548 page_directory_entry* entry = method->KernelVirtualPageDirectory() 549 + VADDR_TO_PDENT(address) + i; 550 PutPageTableInPageDir(entry, physicalTable, 551 B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 552 } 553 } 554 } 555 556 557 //! TODO: currently assumes this translation map is active 558 /*static*/ status_t 559 ARMPagingMethod32Bit::_EarlyQuery(addr_t virtualAddress, 560 phys_addr_t *_physicalAddress) 561 { 562 ARMPagingMethod32Bit* method = ARMPagingMethod32Bit::Method(); 563 int index = VADDR_TO_PDENT(virtualAddress); 564 if ((method->KernelVirtualPageDirectory()[index] & ARM_PDE_TYPE_MASK) == 0) { 565 // no pagetable here 566 return B_ERROR; 567 } 568 569 page_table_entry* entry = (page_table_entry*) 570 (method->KernelVirtualPageDirectory()[index] & ARM_PDE_ADDRESS_MASK); 571 entry += VADDR_TO_PTENT(virtualAddress); 572 573 if ((*entry & ARM_PTE_TYPE_MASK) == 0) { 574 // page mapping not valid 575 return B_ERROR; 576 } 577 578 *_physicalAddress = (*entry & ARM_PTE_ADDRESS_MASK) 579 | VADDR_TO_PGOFF(virtualAddress); 580 581 return B_OK; 582 } 583