Fixes to memory management that helps me sleep at night.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-10-10 17:22:03 +02:00
parent c16bdd2604
commit 1b7dc2d817
2 changed files with 7 additions and 34 deletions

View File

@ -35,15 +35,6 @@ namespace Sortix
{ {
namespace ELF namespace ELF
{ {
// This works around an optimizer bug I ran into, where the memcpy below
// somehow gets executed prior to the memory was mapped. Somehow, when I
// tried to debug it, it suddenly worked. So here's some deep magic that
// somehow fixes my code.
void PreventHazardousCodeReordering()
{
Log::Print("");
}
addr_t Construct32(Process* process, const void* file, size_t filelen) addr_t Construct32(Process* process, const void* file, size_t filelen)
{ {
if ( filelen < sizeof(Header32) ) { return 0; } if ( filelen < sizeof(Header32) ) { return 0; }
@ -70,6 +61,10 @@ namespace Sortix
// Reset the current address space. // Reset the current address space.
process->ResetAddressSpace(); process->ResetAddressSpace();
// Flush the TLB such that no stale information from the last
// address space is used when creating the new one.
Memory::Flush();
// Create all the segments in the final process. // Create all the segments in the final process.
// TODO: Handle errors on bad/malicious input or out-of-mem! // TODO: Handle errors on bad/malicious input or out-of-mem!
for ( uint16_t i = 0; i < numprogheaders; i++ ) for ( uint16_t i = 0; i < numprogheaders; i++ )
@ -94,7 +89,7 @@ namespace Sortix
return 0; return 0;
} }
if ( !Memory::MapRangeUser(mapto, mapbytes) ) if ( !Memory::MapRangeUser(mapto, mapbytes))
{ {
return 0; return 0;
} }
@ -105,8 +100,6 @@ namespace Sortix
segment->next = process->segments; segment->next = process->segments;
process->segments = segment; process->segments = segment;
PreventHazardousCodeReordering();
// Copy as much data as possible and memset the rest to 0. // Copy as much data as possible and memset the rest to 0.
byte* memdest = (byte*) virtualaddr; byte* memdest = (byte*) virtualaddr;
byte* memsource = (byte*) ( ((addr_t)file) + pht->offset); byte* memsource = (byte*) ( ((addr_t)file) + pht->offset);

View File

@ -66,7 +66,7 @@ namespace Sortix
// Loop over every detected memory region. // Loop over every detected memory region.
for ( for (
mmap_t mmap = (mmap_t) bootinfo->mmap_addr; mmap_t mmap = (mmap_t) bootinfo->mmap_addr;
(addr_t) mmap < bootinfo->mmap_addr + bootinfo->mmap_length; (addr_t) mmap < bootinfo->mmap_addr + bootinfo->mmap_length;
mmap = (mmap_t) ((addr_t) mmap + mmap->size + sizeof(mmap->size)) mmap = (mmap_t) ((addr_t) mmap + mmap->size + sizeof(mmap->size))
) )
@ -425,20 +425,10 @@ namespace Sortix
while ( positionstack[TOPPMLLEVEL] < ENTRIES ) while ( positionstack[TOPPMLLEVEL] < ENTRIES )
{ {
if ( level == 1 )
{
//Log::PrintF("[%zu > %zu]", positionstack[2], positionstack[1]);
}
else
{
//Log::PrintF("[%zu]", positionstack[2]);
}
const size_t pos = positionstack[level]; const size_t pos = positionstack[level];
if ( pos == ENTRIES ) if ( pos == ENTRIES )
{ {
//Log::PrintF(" done with level\n");
(positionstack[++level])++; (positionstack[++level])++;
pmloffset /= ENTRIES; pmloffset /= ENTRIES;
continue; continue;
@ -454,7 +444,6 @@ namespace Sortix
if ( unlikely(phys == 0) ) if ( unlikely(phys == 0) )
{ {
//Log::PrintF(" out of memory!\n");
// Oh no. Out of memory! We'll have to undo everything // Oh no. Out of memory! We'll have to undo everything
// we just did. Argh! // we just did. Argh!
failure = true; failure = true;
@ -472,8 +461,6 @@ namespace Sortix
if ( level == 1 ) if ( level == 1 )
{ {
//Log::PrintF(" copy\n");
// Determine the source page's address. // Determine the source page's address.
const void* src = (const void*) (pmloffset * 4096UL); const void* src = (const void*) (pmloffset * 4096UL);
@ -484,8 +471,6 @@ namespace Sortix
} }
else else
{ {
//Log::PrintF(" recurse\n");
// Fork the PML recursively! // Fork the PML recursively!
pmloffset = pmloffset * ENTRIES + pos; pmloffset = pmloffset * ENTRIES + pos;
positionstack[--level] = 0; positionstack[--level] = 0;
@ -496,15 +481,12 @@ namespace Sortix
// If this entry should be linked, link it. // If this entry should be linked, link it.
else else
{ {
//Log::PrintF(" link\n");
FORKPML[level].entry[pos] = entry; FORKPML[level].entry[pos] = entry;
} }
positionstack[level]++; positionstack[level]++;
} }
//Log::PrintF("Fork: Loop Terminated\n");
if ( !failure ) if ( !failure )
{ {
// Now, the new top pml needs to have its fractal memory fixed. // Now, the new top pml needs to have its fractal memory fixed.
@ -524,8 +506,6 @@ namespace Sortix
childaddr = (FORKPML + i)->entry[ENTRIES-2] & PML_ADDRESS; childaddr = (FORKPML + i)->entry[ENTRIES-2] & PML_ADDRESS;
} }
//Log::PrintF("Fork: Done\n");
return newtoppmladdr; return newtoppmladdr;
} }