1 /* 2 * Copyright 2020-2021, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * X512 <danger_mail@list.ru> 7 */ 8 #ifndef _RISCV64VMTRANSLATIONMAP_H_ 9 #define _RISCV64VMTRANSLATIONMAP_H_ 10 11 12 #include <vm/VMTranslationMap.h> 13 #include <arch_cpu_defs.h> 14 15 16 struct RISCV64VMTranslationMap: public VMTranslationMap { 17 RISCV64VMTranslationMap(bool kernel, 18 phys_addr_t pageTable = 0); 19 virtual ~RISCV64VMTranslationMap(); 20 21 virtual bool Lock(); 22 virtual void Unlock(); 23 24 virtual addr_t MappedSize() const; 25 virtual size_t MaxPagesNeededToMap(addr_t start, 26 addr_t end) const; 27 28 virtual status_t Map(addr_t virtualAddress, 29 phys_addr_t physicalAddress, 30 uint32 attributes, uint32 memoryType, 31 vm_page_reservation* reservation); 32 virtual status_t Unmap(addr_t start, addr_t end); 33 34 virtual status_t DebugMarkRangePresent(addr_t start, addr_t end, 35 bool markPresent); 36 37 virtual status_t UnmapPage(VMArea* area, addr_t address, 38 bool updatePageQueue); 39 virtual void UnmapPages(VMArea* area, addr_t base, 40 size_t size, bool updatePageQueue); 41 virtual void UnmapArea(VMArea* area, 42 bool deletingAddressSpace, 43 bool ignoreTopCachePageFlags); 44 45 virtual status_t Query(addr_t virtualAddress, 46 phys_addr_t* _physicalAddress, 47 uint32* _flags); 48 virtual status_t QueryInterrupt(addr_t virtualAddress, 49 phys_addr_t* _physicalAddress, 50 uint32* _flags); 51 52 virtual status_t Protect(addr_t base, addr_t top, 53 uint32 attributes, uint32 memoryType); 54 status_t ProtectPage(VMArea* area, addr_t address, 55 uint32 attributes); 56 status_t ProtectArea(VMArea* area, 57 uint32 attributes); 58 59 status_t SetFlags(addr_t virtualAddress, 60 uint32 flags); 61 62 virtual status_t ClearFlags(addr_t virtualAddress, 63 uint32 flags); 64 65 virtual bool ClearAccessedAndModified( 66 VMArea* area, addr_t address, 67 bool unmapIfUnaccessed, 68 bool& _modified); 69 70 virtual void Flush(); 71 72 virtual void DebugPrintMappingInfo(addr_t virtualAddress); 73 virtual bool DebugGetReverseMappingInfo( 74 phys_addr_t physicalAddress, 75 ReverseMappingInfoCallback& callback); 76 77 inline phys_addr_t PageTable(); 78 inline uint64 Satp(); 79 80 status_t MemcpyToMap(addr_t to, const char *from, 81 size_t size); 82 status_t MemcpyFromMap(char *to, addr_t from, 83 size_t size); 84 status_t MemsetToMap(addr_t to, char c, size_t count); 85 ssize_t StrlcpyFromMap(char *to, addr_t from, 86 size_t size); 87 ssize_t StrlcpyToMap(addr_t to, const char *from, 88 size_t size); 89 90 private: 91 Pte* LookupPte(addr_t virtAdr, bool alloc, 92 vm_page_reservation* reservation); 93 phys_addr_t LookupAddr(addr_t virtAdr); 94 95 bool fIsKernel; 96 phys_addr_t fPageTable; 97 uint64_t fPageTableSize; // in page units 98 }; 99 100 101 inline phys_addr_t RISCV64VMTranslationMap::PageTable() 102 { 103 return fPageTable; 104 } 105 106 inline uint64 RISCV64VMTranslationMap::Satp() 107 { 108 SatpReg satp; 109 satp.ppn = fPageTable / B_PAGE_SIZE; 110 satp.asid = 0; 111 satp.mode = satpModeSv39; 112 return satp.val; 113 } 114 115 116 struct RISCV64VMPhysicalPageMapper: public VMPhysicalPageMapper { 117 RISCV64VMPhysicalPageMapper(); 118 virtual ~RISCV64VMPhysicalPageMapper(); 119 120 virtual status_t GetPage(phys_addr_t physicalAddress, 121 addr_t* _virtualAddress, 122 void** _handle); 123 virtual status_t PutPage(addr_t virtualAddress, 124 void* handle); 125 126 virtual status_t GetPageCurrentCPU( 127 phys_addr_t physicalAddress, 128 addr_t* _virtualAddress, 129 void** _handle); 130 virtual status_t PutPageCurrentCPU(addr_t virtualAddress, 131 void* _handle); 132 133 virtual status_t GetPageDebug(phys_addr_t physicalAddress, 134 addr_t* _virtualAddress, 135 void** _handle); 136 virtual status_t PutPageDebug(addr_t virtualAddress, 137 void* handle); 138 139 virtual status_t MemsetPhysical(phys_addr_t address, int value, 140 phys_size_t length); 141 virtual status_t MemcpyFromPhysical(void* to, phys_addr_t from, 142 size_t length, bool user); 143 virtual status_t MemcpyToPhysical(phys_addr_t to, 144 const void* from, size_t length, 145 bool user); 146 virtual void MemcpyPhysicalPage(phys_addr_t to, 147 phys_addr_t from); 148 }; 149 150 151 #endif // _RISCV64VMTRANSLATIONMAP_H_ 152