Applied a few constants

This commit is contained in:
PatrickvL 2016-05-20 11:17:51 +02:00
parent a283c21d40
commit 8e87d4f465
10 changed files with 48 additions and 41 deletions

View file

@ -13,7 +13,7 @@ Box::Box() {
hm = new HandleManager();
pm = new PageManager();
pm->add_region(0, 64 * 1024 * 1024);
pm->add_region(0, 64 * ONE_MB);
tm = new ThreadManager();
io = new IOManager();

26
Cpu.cpp
View file

@ -29,10 +29,10 @@ Cpu::Cpu(uint8_t *ram, uint8_t *kram) {
hv->map_phys(mem, 0x00000000, RAM_SIZE);
hv->map_phys(kmem, 0xc0000000, KRAM_SIZE);
auto directory = 64*1024*1024; // Page directory base
auto directory = 64*ONE_MB; // Page directory base
auto dir = (uint32_t *) (mem + directory);
for(auto i = 0; i < 1024; ++i) {
dir[i] = (directory + 4096 + 4096 * i) | 0x7;
dir[i] = (directory + PAGE_SIZE + PAGE_SIZE * i) | 0x7;
auto table = (uint32_t *) (mem + (dir[i] & ~0xFFF));
for(auto j = 0; j < 1024; ++j) {
table[j] = 0x0;
@ -41,43 +41,43 @@ Cpu::Cpu(uint8_t *ram, uint8_t *kram) {
hv->reg(CR3, directory);
hv->reg(CR0, 0x80000000 | 0x20 | 0x01); // Paging | NE | PE
auto gdt = ram + 96*1024*1024;
auto gdt = ram + 96*ONE_MB;
memset(gdt, 0, 0x10000);
gdt_encode(gdt, 0, 0, 0, 0); // Null entry
gdt_encode(gdt, 1, 0, 0xffffffff, 0x9A); // Code
gdt_encode(gdt, 2, 0, 0xffffffff, 0x92); // Data
hv->reg(GDT_LIMIT, 0xFFFF);
hv->reg(GDT_BASE, 96*1024*1024);
map_pages(96 * 1024 * 1024, 96 * 1024 * 1024, 16);
hv->reg(GDT_BASE, 96*ONE_MB);
map_pages(96 * ONE_MB, 96 * ONE_MB, 16);
hv->reg(CR4, 0x2000);
}
void Cpu::map_pages(uint32_t virt, uint32_t phys, uint32_t count, bool present) {
auto dir = (uint32_t *) (mem + 64*1024*1024);
auto dir = (uint32_t *) (mem + 64*ONE_MB);
for(auto i = 0; i < count; ++i) {
auto table = (uint32_t *) (mem + (dir[virt >> 22] & ~0xFFF));
table[(virt >> 12) & 0x3ff] = phys | 0x6 | (present ? 1 : 0);
virt += 4096;
phys += 4096;
virt += PAGE_SIZE;
phys += PAGE_SIZE;
}
hv->invalidate_tlb(); // Do we really need to do this all the time?
}
void Cpu::map_io(uint32_t base, uint32_t pages, MMIOReceiver *recv) {
auto memblock = new uint8_t[pages * 4096];
hv->map_phys(memblock, base, pages * 4096);
auto memblock = new uint8_t[pages * PAGE_SIZE];
hv->map_phys(memblock, base, pages * PAGE_SIZE);
for(auto i = 0; i < pages; ++i) {
mmio[base] = recv;
recv->buffers[base] = memblock;
map_pages(base, base, 1, false); // Pages are not marked present
base += 4096;
memblock += 4096;
base += PAGE_SIZE;
memblock += PAGE_SIZE;
}
}
void Cpu::flip_page(uint32_t base, bool val) {
auto dir = (uint32_t *) (mem + 64*1024*1024);
auto dir = (uint32_t *) (mem + 64*ONE_MB);
auto table = (uint32_t *) (mem + (dir[base >> 22] & ~0xFFF));
table[(base >> 12) & 0x3ff] = (table[(base >> 12) & 0x3ff] & ~1) | (val ? 1 : 0);
hv->invalidate_tlb();

View file

@ -1,7 +1,7 @@
#include "Zookeeper.hpp"
Gpu::Gpu() {
box->cpu->map_io(0xFD000000, 4096, (MMIOReceiver *) this); // 16MB address space
box->cpu->map_io(0xFD000000, PAGE_SIZE, (MMIOReceiver *) this); // 16MB address space
}
uint32_t Gpu::read(uint32_t addr) {

View file

@ -25,9 +25,9 @@ uint32_t Hypercall::map_contiguous(uint32_t virt_base, uint32_t phys_low, uint32
}
uint32_t Hypercall::query_map_size(uint32_t base) {
auto count = 0;
while(box->cpu->is_mapped(base + count * 4096))
while(box->cpu->is_mapped(base + count * PAGE_SIZE))
count++;
return count * 4096;
return count * PAGE_SIZE;
}
void Hypercall::unmap(uint32_t virt_base, uint32_t count) {
box->pm->unmap(virt_base, count);

View file

@ -182,13 +182,13 @@ void DirHandle::ioctl(uint32_t code, void *ibuf, uint32_t isize, void *obuf, uin
switch(code) {
case IOCTL_DISK_GET_DRIVE_GEOMETRY: {
auto geom = (DISK_GEOMETRY *) obuf;
geom->BytesPerSector = 1024*1024;
geom->BytesPerSector = ONE_MB;
break;
}
case IOCTL_DISK_GET_PARTITION_INFO: {
auto part = (PARTITION_INFORMATION *) obuf;
part->StartingOffset = 0;
part->PartitionLength = 1 * 1024 * 1024 * 1024; // 1GB
part->PartitionLength = ONE_GB;
part->HiddenSectors = 0;
part->PartitionNumber = 5; // Who cares?
part->PartitionType = 8;

View file

@ -51,7 +51,7 @@ NTSTATUS NTAPI kernel_PsCreateSystemThreadEx(
p->StartContext1 = StartContext1;
p->StartContext2 = StartContext2;
*ThreadHandle = create_thread(threadex_proxy, ((uint8_t *) malloc(1024*1024)) + 1024 * 1024, (uint32_t) p);
*ThreadHandle = create_thread(threadex_proxy, ((uint8_t *) malloc(ONE_MB)) + ONE_MB, (uint32_t) p);
if(ThreadId != NULL)
*ThreadId = *ThreadHandle;

View file

@ -4,15 +4,15 @@ PageManager::PageManager() {
virtgroup_t group;
group.start = 0x10000000;
group.end = 0xc0000000;
group.count = (group.end - group.start) / 4096;
group.count = (group.end - group.start) / PAGE_SIZE;
virtGroups.push_back(group);
}
void PageManager::add_region(uint32_t base, uint32_t size) {
if(size & 0xFFF)
size = (size & ~0xFFF) + 4096;
size = (size & ~0xFFF) + PAGE_SIZE;
for(uint32_t addr = 0; addr < base + size; addr += 4096)
for(uint32_t addr = 0; addr < base + size; addr += PAGE_SIZE)
freePhysPages.push_back(addr);
}
@ -21,7 +21,7 @@ uint32_t PageManager::map(uint32_t base, uint32_t count) {
base = box->pm->alloc_virt(count);
for(auto i = 0; i < count; ++i) {
auto virt = base + i * 4096;
auto virt = base + i * PAGE_SIZE;
if(!box->cpu->is_mapped(virt))
box->cpu->map_pages(virt, box->pm->alloc_phys(), 1);
}
@ -43,7 +43,7 @@ void PageManager::unmap(uint32_t base, uint32_t count) {
auto addr = base;
for(auto i = 0; i < count; ++i) {
box->pm->free_phys(box->cpu->virt2phys(addr));
addr += 4096;
addr += PAGE_SIZE;
}
box->pm->free_virt(base, count);
}
@ -53,7 +53,7 @@ uint32_t PageManager::alloc_phys(uint32_t count, uint32_t phys_low, uint32_t phy
if(count == 1) {
for(auto iter = freePhysPages.begin(); iter != freePhysPages.end(); ++iter) {
if(phys_low < *iter && phys_high >= (*iter + 4096)) {
if(phys_low < *iter && phys_high >= (*iter + PAGE_SIZE)) {
auto page = *iter;
freePhysPages.erase(iter);
return page;
@ -62,19 +62,19 @@ uint32_t PageManager::alloc_phys(uint32_t count, uint32_t phys_low, uint32_t phy
bailout(true); // Could not find page in range
} else {
for(auto base : freePhysPages) {
if(!(phys_low < base && (base + count * 4096) <= phys_high))
if(!(phys_low < base && (base + count * PAGE_SIZE) <= phys_high))
continue;
// This is the least efficient search ever.
auto found = true;
for(auto i = 1; i < count; ++i) {
if(find(freePhysPages.begin(), freePhysPages.end(), base + i * 4096) == freePhysPages.end()) {
if(find(freePhysPages.begin(), freePhysPages.end(), base + i * PAGE_SIZE) == freePhysPages.end()) {
found = false;
break;
}
}
if(found) {
for(auto i = 0; i < count; ++i) {
auto iter = find(freePhysPages.begin(), freePhysPages.end(), base + i * 4096);
auto iter = find(freePhysPages.begin(), freePhysPages.end(), base + i * PAGE_SIZE);
freePhysPages.erase(iter);
}
return base;
@ -97,16 +97,16 @@ uint32_t PageManager::alloc_virt(uint32_t count, uint32_t low) {
virtGroups.erase(iter);
else {
iter->count -= count;
iter->start += count * 4096;
iter->start += count * PAGE_SIZE;
}
return start;
} else {
auto off = low - iter->start;
auto coff = off / 4096;
auto coff = off / PAGE_SIZE;
auto start = low;
if(iter->count == coff + count) {
iter->count -= count;
iter->end -= count * 4096;
iter->end -= count * PAGE_SIZE;
} else {
auto oend = iter->end;
auto ocount = iter->count;
@ -114,7 +114,7 @@ uint32_t PageManager::alloc_virt(uint32_t count, uint32_t low) {
iter->end = low;
virtgroup_t group;
group.count = ocount - count - coff;
group.start = low + count * 4096;
group.start = low + count * PAGE_SIZE;
group.end = oend;
virtGroups.insert(++iter, group);
}
@ -127,7 +127,7 @@ uint32_t PageManager::alloc_virt(uint32_t count, uint32_t low) {
}
void PageManager::free_virt(uint32_t start, uint32_t count) {
auto end = start + count * 4096;
auto end = start + count * PAGE_SIZE;
if(virtGroups.size() == 0) {
virtgroup_t group;
group.start = start;

View file

@ -28,7 +28,7 @@ uint32_t Xbe::LoadImage() {
//cout << "Loading image at " << hex << header->base << " to " << hex << header->base + file_size << endl;
//cout << "OEP is " << hex << header->oep << endl;
box->xbebase = header->base;
box->pm->map(header->base, pagepad(file_size) / 4096);
box->pm->map(header->base, pagepad(file_size) / PAGE_SIZE);
box->cpu->write_memory(header->base, file_size, file_data);
auto end = file_size;
@ -38,8 +38,8 @@ uint32_t Xbe::LoadImage() {
auto psize = pagepad(sect->vsize);
auto base = sect->vaddr & ~0xFFF;
if(sect->vaddr & 0xFFF)
psize += 4096;
box->pm->map(base, psize / 4096);
psize += PAGE_SIZE;
box->pm->map(base, psize / PAGE_SIZE);
//cout << "Loading section of 0x" << hex << sect->vsize << " bytes (padded to 0x" << psize << ") to 0x" << base << endl;
box->cpu->write_memory(sect->vaddr, sect->rsize, &file_data[sect->raddr]);
auto nend = (sect->vaddr - header->base) + sect->rsize;

View file

@ -17,12 +17,19 @@ using namespace boost::algorithm;
#define bailout(expr) do { if(expr) { cout << "Bailout: " << #expr << " @ " << __FILE__ << " (" << dec << __LINE__ << ")" << endl; exit(1); } } while(0)
#define pagepad(expr) (((expr) & 0xFFF) ? ((expr) & ~0xFFF) + 4096 : (expr))
#define ONE_KB 1024
#define ONE_MB (1024 * ONE_KB)
#define ONE_GB (1024 * ONE_MB)
#define PAGE_SIZE (4 * ONE_KB)
#define PAGE_MASK (PAGE_SIZE - 1)
#define pagepad(expr) (((expr) & PAGE_MASK) ? ((expr) & ~PAGE_MASK) + PAGE_SIZE : (expr))
#define IN(a, b) (((b).find(a)) != (b).end())
#define RAM_SIZE 128*1024*1024
#define KRAM_SIZE 128*1024*1024
#define RAM_SIZE (128 * ONE_MB)
#define KRAM_SIZE (128 * ONE_MB)
#include "HV.hpp"
#ifdef MAC

View file

@ -12,7 +12,7 @@ uint32_t load_multiboot(Cpu *cpu, uint32_t *header) {
memcpy(cpu->kmem + (header[4] - 0xC0000000), rel + (header[4] - header[3]), memsize);
memset(cpu->kmem + (header[5] - 0xC0000000), 0, header[6] - header[5]);
cpu->map_pages(0xC0000000, 0xC0000000, memsize / 4096);
cpu->map_pages(0xC0000000, 0xC0000000, memsize / PAGE_SIZE);
return header[7];
}