20 #include "VirtualAddressSpace.h" 21 #include "pedigree/kernel/Log.h" 22 #include "pedigree/kernel/panic.h" 23 #include "pedigree/kernel/processor/PhysicalMemoryManager.h" 24 #include "pedigree/kernel/processor/Processor.h" 25 #include "pedigree/kernel/processor/types.h" 26 #include "pedigree/kernel/utilities/utility.h" 28 static bool g_Kseg2Initialised =
false;
52 if (!g_Kseg2Initialised)
55 g_Kseg2Initialised =
true;
63 for (
int i = 0; i < 1024; i++)
82 uintptr_t page =
reinterpret_cast<uintptr_t
>(virtualAddress) >> 12;
87 uintptr_t chunkIdx = (page * 2) / 4096;
90 uintptr_t chunkOff = (page * 2) % 4096;
100 if (chunkAddr < 0x20000000)
103 *
reinterpret_cast<uintptr_t *
>(KSEG0(chunkAddr) + chunkOff);
108 if ((mapping & MIPS32_PTE_VALID) == 0)
116 panic(
"Physical addressing over 512MB not implemented yet!");
121 physical_uintptr_t
physicalAddress,
void *virtualAddress,
size_t flags)
124 uintptr_t nVirtualAddress =
reinterpret_cast<uintptr_t
>(virtualAddress);
128 if (!IS_KUSEG(nVirtualAddress) && !IS_KSEG2(nVirtualAddress))
131 "MIPS32VirtualAddressSpace::map(" 132 <<
Hex << physicalAddress <<
", " << nVirtualAddress <<
", " 133 << flags <<
"): invalid virtual address.");
139 if ((nVirtualAddress >= 0xC0000000) && (nVirtualAddress < 0xC0800000))
142 "MIPS32VirtualAddressSpace::map(" 143 <<
Hex << physicalAddress <<
", " << nVirtualAddress <<
", " 144 << flags <<
"): invalid virtual address.");
149 uintptr_t page = nVirtualAddress >> 12;
153 uintptr_t chunkIdx = page / 512;
157 uintptr_t chunkOff = (page % 512) * 8;
167 setPageTableChunk(chunkIdx, chunkAddr);
171 if (chunkAddr < 0x20000000)
174 reinterpret_cast<uintptr_t *
>(KSEG0(chunkAddr) + chunkOff);
180 "MIPS32VirtualAddressSpace::map(" 181 <<
Hex << physicalAddress <<
", " << nVirtualAddress <<
", " 182 << flags <<
"): Address already mapped.");
198 (flags &
CacheDisable) ? MIPS32_PTE_UNCACHED : MIPS32_PTE_CACHED;
199 uintptr_t d = (flags &
Write) ? MIPS32_PTE_DIRTY : 0;
200 uintptr_t v = MIPS32_PTE_VALID;
204 *mapping = pfn | c | d | v | g;
216 panic(
"Physical addressing over 512MB not implemented yet!");
221 void *virtualAddress, physical_uintptr_t &
physicalAddress,
size_t &flags)
224 uintptr_t page =
reinterpret_cast<uintptr_t
>(virtualAddress) >> 12;
229 uintptr_t chunkIdx = (page * 2) / 4096;
232 uintptr_t chunkOff = (page * 2) % 4096;
245 if (chunkAddr < 0x20000000)
248 *
reinterpret_cast<uintptr_t *
>(KSEG0(chunkAddr) + chunkOff);
250 *
reinterpret_cast<uintptr_t *
>(KSEG0(chunkAddr) + chunkOff + 4);
258 if ((mapping & MIPS32_PTE_VALID) == 0)
264 physicalAddress = (mapping << 6) & 0xFFFFF000;
271 panic(
"Physical addressing over 512MB not implemented yet!");
289 else if (chunkIdx >= 1536)
295 ERROR(
"Invalid page table chunk index: " <<
Dec << chunkIdx);
305 void MIPS32VirtualAddressSpace::freeStack(
void *pStack)
310 void MIPS32VirtualAddressSpace::setPageTableChunk(
311 uintptr_t chunkIdx, uintptr_t chunkAddr)
313 NOTICE(
"setPageTableChunk(" <<
Hex << chunkIdx <<
", " << chunkAddr <<
")");
318 else if (chunkIdx >= 1536)
324 ERROR(
"Invalid page table chunk index: " <<
Dec << chunkIdx);
333 if (frame < 0x20000000)
335 ByteSet(reinterpret_cast<void *>(KSEG0(frame)), 0,
PAGE_SIZE);
336 return static_cast<uintptr_t
>(frame);
341 panic(
"Physical addressing over 512MB not implemented yet!");
virtual bool map(physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags)
MIPS32VirtualAddressSpace()
static PhysicalMemoryManager & instance()
uintptr_t generateNullChunk()
static MIPS32VirtualAddressSpace m_KernelSpace
uintptr_t m_pKusegDirectory[1024]
virtual void getMapping(void *virtualAddress, physical_uintptr_t &physicalAddress, size_t &flags)
virtual physical_uintptr_t allocatePage(size_t pageConstraints=0)=0
virtual void setFlags(void *virtualAddress, size_t newFlags)
static EXPORTED_PUBLIC VirtualAddressSpace & getKernelAddressSpace()
virtual bool isAddressValid(void *virtualAddress)
virtual ~MIPS32VirtualAddressSpace()
static const size_t Write
static VirtualAddressSpace * create()
uintptr_t physicalAddress(physical_uintptr_t address) PURE
static uintptr_t m_pKseg2Directory[512]
virtual bool isMapped(void *virtualAddress)
virtual void unmap(void *virtualAddress)
virtual void freePage(physical_uintptr_t page)=0
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
virtual void * allocateStack()
static const size_t CacheDisable
uintptr_t getPageTableChunk(uintptr_t chunkIdx)