Changed mmap and related functions to not be methods
This commit is contained in:
@@ -1,71 +0,0 @@
|
|||||||
#include "addressspace.hpp"
|
|
||||||
#include "util.hpp"
|
|
||||||
|
|
||||||
kernel::AddressSpace::AddressSpace(MemoryAllocator& malloc)
|
|
||||||
: malloc(malloc)
|
|
||||||
{
|
|
||||||
this->pageTables = (PageTableEntry*) 0xFFC00000;
|
|
||||||
this->pageDirectory = (PageTableEntry*) 0xFFFFF000;
|
|
||||||
}
|
|
||||||
|
|
||||||
int kernel::AddressSpace::mmap(void* start, size_t length, int flags)
|
|
||||||
{
|
|
||||||
size_t tableIndex = (size_t) start / 4096;
|
|
||||||
for(int i = (int) length; i > 0; i -= 4096)
|
|
||||||
{
|
|
||||||
size_t directoryIndex = tableIndex / 1024;
|
|
||||||
if(!pageDirectory[directoryIndex].getPresent())
|
|
||||||
{
|
|
||||||
physaddr_t newPT = malloc.allocate(4096);
|
|
||||||
if(newPT == 0)
|
|
||||||
return -1;
|
|
||||||
pageDirectory[directoryIndex] = newPT;
|
|
||||||
pageDirectory[directoryIndex].setPresent(true);
|
|
||||||
pageDirectory[directoryIndex].setUsermode(false);
|
|
||||||
pageDirectory[directoryIndex].setRw(true);
|
|
||||||
}
|
|
||||||
if(!pageTables[tableIndex].getPresent())
|
|
||||||
{
|
|
||||||
physaddr_t page = malloc.allocate(4096);
|
|
||||||
pageTables[tableIndex] = page;
|
|
||||||
pageTables[tableIndex].setPresent(true);
|
|
||||||
pageTables[tableIndex].setUsermode(false);
|
|
||||||
if(flags & MMAP_RW)
|
|
||||||
pageTables[tableIndex].setRw(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
tableIndex++;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int kernel::AddressSpace::munmap(void* start, size_t length)
|
|
||||||
{
|
|
||||||
if((size_t) start % 4096 != 0)
|
|
||||||
return -1;
|
|
||||||
size_t tableIndex = (size_t) start / 4096;
|
|
||||||
for(int i = (int) length; i > 0; i -= 4096)
|
|
||||||
{
|
|
||||||
size_t directoryIndex = tableIndex / 1024;
|
|
||||||
if(pageDirectory[directoryIndex].getPresent())
|
|
||||||
{
|
|
||||||
pageDirectory[directoryIndex] = 0;
|
|
||||||
pageDirectory[directoryIndex].setPresent(false);
|
|
||||||
pageDirectory[directoryIndex].setUsermode(false);
|
|
||||||
pageDirectory[directoryIndex].setRw(false);
|
|
||||||
}
|
|
||||||
tableIndex++;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
physaddr_t kernel::AddressSpace::getPhysicalAddress(void* virtualAddress)
|
|
||||||
const
|
|
||||||
{
|
|
||||||
size_t index = (size_t) virtualAddress / 4096;
|
|
||||||
PageTableEntry pte = pageTables[index];
|
|
||||||
if(pte.getPresent())
|
|
||||||
return pte.getPhysicalAddress();
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
#ifndef SRC_ADDRESSSPACE_H_
|
|
||||||
#define SRC_ADDRESSSPACE_H_
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include "memoryallocator.hpp"
|
|
||||||
#include "x86/pagetableentry.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
#define MMAP_RW 0x01
|
|
||||||
#define MMAP_EXEC 0x02
|
|
||||||
#define MMAP_SHARED 0x04
|
|
||||||
|
|
||||||
namespace kernel {
|
|
||||||
|
|
||||||
class AddressSpace {
|
|
||||||
public:
|
|
||||||
|
|
||||||
AddressSpace(MemoryAllocator& malloc);
|
|
||||||
|
|
||||||
int mmap(void* start, size_t length, int flags);
|
|
||||||
|
|
||||||
int munmap(void* start, size_t length);
|
|
||||||
|
|
||||||
physaddr_t getPhysicalAddress(void* virtualAddress) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
MemoryAllocator& malloc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array of 1024 page tables, each containing 1024 entries.
|
|
||||||
* The last table represents the page directory.
|
|
||||||
*/
|
|
||||||
PageTableEntry* pageTables;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array of 1024 PDEs, located at the end of the pageTables array
|
|
||||||
*/
|
|
||||||
PageTableEntry* pageDirectory;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} /* namespace kernel */
|
|
||||||
|
|
||||||
#endif
|
|
||||||
27
src/mmgr/mmap.hpp
Normal file
27
src/mmgr/mmap.hpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef MMAP_H
|
||||||
|
#define MMAP_H
|
||||||
|
|
||||||
|
#include "memoryallocator.hpp"
|
||||||
|
|
||||||
|
#define MMAP_RW 0x01
|
||||||
|
#define MMAP_EXEC 0x02
|
||||||
|
#define MMAP_SHARED 0x04
|
||||||
|
|
||||||
|
namespace kernel
|
||||||
|
{
|
||||||
|
|
||||||
|
int mmap(MemoryAllocator& allocator, void* start, size_t length, int flags);
|
||||||
|
|
||||||
|
int munmap(MemoryAllocator& allocator, void* start, size_t length);
|
||||||
|
|
||||||
|
bool isMapped(void* addr);
|
||||||
|
|
||||||
|
physaddr_t getPhysicalAddress(void* addr);
|
||||||
|
|
||||||
|
int createAddressSpace(void* table);
|
||||||
|
|
||||||
|
int loadAddressSpace(physaddr_t table);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
7
src/mmgr/mmgr.hpp
Normal file
7
src/mmgr/mmgr.hpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef MMGR_H
|
||||||
|
#define MMGR_H
|
||||||
|
|
||||||
|
#include "mmap.hpp"
|
||||||
|
#include "buddyallocator.hpp"
|
||||||
|
|
||||||
|
#endif
|
||||||
98
src/mmgr/x86/mmap.cpp
Normal file
98
src/mmgr/x86/mmap.cpp
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#include "../mmap.hpp"
|
||||||
|
#include "pagetableentry.hpp"
|
||||||
|
|
||||||
|
int kernel::mmap(MemoryAllocator& allocator, void* start, size_t length, int flags)
|
||||||
|
{
|
||||||
|
if((size_t) start % 4096 != 0)
|
||||||
|
return -1;
|
||||||
|
PageTableEntry* pageTables = (PageTableEntry*) 0xFFC00000;
|
||||||
|
PageTableEntry* pageDirectory = (PageTableEntry*) 0xFFFFF000;
|
||||||
|
size_t tableIndex = (size_t) start / 4096;
|
||||||
|
for(int i = (int) length; i > 0; i -= 4096)
|
||||||
|
{
|
||||||
|
size_t directoryIndex = tableIndex / 1024;
|
||||||
|
if(!pageDirectory[directoryIndex].getPresent())
|
||||||
|
{
|
||||||
|
physaddr_t newPT = allocator.allocate(4096);
|
||||||
|
if(newPT == 0)
|
||||||
|
return -1;
|
||||||
|
pageDirectory[directoryIndex] = newPT;
|
||||||
|
pageDirectory[directoryIndex].setPresent(true);
|
||||||
|
pageDirectory[directoryIndex].setUsermode(false);
|
||||||
|
pageDirectory[directoryIndex].setRw(true);
|
||||||
|
}
|
||||||
|
if(!pageTables[tableIndex].getPresent())
|
||||||
|
{
|
||||||
|
physaddr_t page = allocator.allocate(4096);
|
||||||
|
pageTables[tableIndex] = page;
|
||||||
|
pageTables[tableIndex].setPresent(true);
|
||||||
|
pageTables[tableIndex].setUsermode(false);
|
||||||
|
if(flags & MMAP_RW)
|
||||||
|
pageTables[tableIndex].setRw(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
tableIndex++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int kernel::munmap(MemoryAllocator& allocator, void* start, size_t length)
|
||||||
|
{
|
||||||
|
if((size_t) start % 4096 != 0)
|
||||||
|
return -1;
|
||||||
|
PageTableEntry* pageTables = (PageTableEntry*) 0xFFC00000;
|
||||||
|
PageTableEntry* pageDirectory = (PageTableEntry*) 0xFFFFF000;
|
||||||
|
size_t tableIndex = (size_t) start / 4096;
|
||||||
|
for(int i = (int) length; i > 0; i -= 4096)
|
||||||
|
{
|
||||||
|
size_t directoryIndex = tableIndex / 1024;
|
||||||
|
if(pageDirectory[directoryIndex].getPresent())
|
||||||
|
{
|
||||||
|
pageDirectory[directoryIndex] = 0;
|
||||||
|
pageDirectory[directoryIndex].setPresent(false);
|
||||||
|
pageDirectory[directoryIndex].setUsermode(false);
|
||||||
|
pageDirectory[directoryIndex].setRw(false);
|
||||||
|
}
|
||||||
|
tableIndex++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool kernel::isMapped(void* addr)
|
||||||
|
{
|
||||||
|
PageTableEntry* pageDirectory = (PageTableEntry*) 0xFFFFF000;
|
||||||
|
size_t tableIndex = (size_t) addr / 4096;
|
||||||
|
size_t directoryIndex = tableIndex / 1024;
|
||||||
|
if(pageDirectory[directoryIndex].getPresent())
|
||||||
|
{
|
||||||
|
PageTableEntry* pageTables = (PageTableEntry*) 0xFFC00000;
|
||||||
|
return pageTables[tableIndex].getPresent();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
physaddr_t kernel::getPhysicalAddress(void* addr)
|
||||||
|
{
|
||||||
|
PageTableEntry* pageTables = (PageTableEntry*) 0xFFC00000;
|
||||||
|
size_t tableIndex = (size_t) addr / 4096;
|
||||||
|
return pageTables[tableIndex].getPhysicalAddress() + (size_t) addr & 0xFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
int kernel::createAddressSpace(void* table)
|
||||||
|
{
|
||||||
|
if((size_t) table & 0xFFF != 0)
|
||||||
|
return -1;
|
||||||
|
PageTableEntry* pageDirectory = (PageTableEntry*) 0xFFFFF000;
|
||||||
|
PageTableEntry* newDirectory = (PageTableEntry*) table;
|
||||||
|
newDirectory[1022] = pageDirectory[1022];
|
||||||
|
newDirectory[1023] = pageDirectory[1023];
|
||||||
|
}
|
||||||
|
|
||||||
|
int kernel::loadAddressSpace(physaddr_t table)
|
||||||
|
{
|
||||||
|
if(table & 0xFFF != 0)
|
||||||
|
return -1;
|
||||||
|
asm("mov %0, %cr3"
|
||||||
|
:
|
||||||
|
: "r" (table));
|
||||||
|
}
|
||||||
@@ -130,4 +130,12 @@ physaddr_t PageTableEntry::operator=(physaddr_t rhs)
|
|||||||
return setPhysicalAddress(rhs);
|
return setPhysicalAddress(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace qkernel */
|
PageTableEntry PageTableEntry::operator=(PageTableEntry rhs)
|
||||||
|
{
|
||||||
|
uint32_t* iThis = (uint32_t*) this;
|
||||||
|
uint32_t* iThat = (uint32_t*) &rhs;
|
||||||
|
*iThis = *iThat;
|
||||||
|
return rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ public:
|
|||||||
bool getWriteThrough() const;
|
bool getWriteThrough() const;
|
||||||
void setWriteThrough(bool writeThrough);
|
void setWriteThrough(bool writeThrough);
|
||||||
physaddr_t operator=(physaddr_t rhs);
|
physaddr_t operator=(physaddr_t rhs);
|
||||||
|
PageTableEntry operator=(PageTableEntry rhs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t present : 1;
|
uint32_t present : 1;
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
#include "systypes.hpp"
|
#include "systypes.hpp"
|
||||||
#include "systeminfo.hpp"
|
#include "systeminfo.hpp"
|
||||||
#include "mmgr/memorymap.hpp"
|
#include "mmgr/mmgr.hpp"
|
||||||
#include "mmgr/buddyallocator.hpp"
|
|
||||||
#include "mmgr/addressspace.hpp"
|
|
||||||
#include "pio.hpp"
|
#include "pio.hpp"
|
||||||
#include "tty.hpp"
|
#include "tty.hpp"
|
||||||
#include "util.hpp"
|
#include "util.hpp"
|
||||||
@@ -18,7 +16,7 @@ extern MemoryMap::Region memory_map;
|
|||||||
|
|
||||||
void main(char* cmdline)
|
void main(char* cmdline)
|
||||||
{
|
{
|
||||||
TTY tty((char*) 0xC00B8000);
|
TTY tty((char*) 0xFF8B8000);
|
||||||
tty << PACKAGE_STRING << "\n\n";
|
tty << PACKAGE_STRING << "\n\n";
|
||||||
tty << "Low memory: \t" << (int) system_info.getLowMemory() << " KiB\n";
|
tty << "Low memory: \t" << (int) system_info.getLowMemory() << " KiB\n";
|
||||||
tty << "High memory:\t" << (int) system_info.getHighMemory() << " KiB\n";
|
tty << "High memory:\t" << (int) system_info.getHighMemory() << " KiB\n";
|
||||||
@@ -30,9 +28,8 @@ void main(char* cmdline)
|
|||||||
tty << (void*) memmap[i].getLocation() << "\t\t";
|
tty << (void*) memmap[i].getLocation() << "\t\t";
|
||||||
tty << (int) memmap[i].getSize() << "\n";
|
tty << (int) memmap[i].getSize() << "\n";
|
||||||
}
|
}
|
||||||
BuddyAllocator alloc(memmap, (char*) 0xC0000000, system_info.getHighMemory() / 4 + 256, 6);
|
BuddyAllocator alloc(memmap, (char*) 0xFF800000, system_info.getHighMemory() / 4 + 256, 6);
|
||||||
AddressSpace vmem(alloc);
|
mmap(alloc, (void*) 0, 4096, MMAP_RW);
|
||||||
vmem.mmap((void*) 0x80000000, 0x10000, MMAP_RW);
|
|
||||||
outb(0xa1, 0xff);
|
outb(0xa1, 0xff);
|
||||||
outb(0x21, 0xff);
|
outb(0x21, 0xff);
|
||||||
tty << "Nothing left to do. Hanging.\n";
|
tty << "Nothing left to do. Hanging.\n";
|
||||||
|
|||||||
Reference in New Issue
Block a user