From e71a426f7cc3cd98e43ca8f529ee8e1e13395885 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Mon, 31 May 2010 10:39:18 +0800 Subject: [PATCH] Fixed bug with physical memory allocation, update MM_Allocate to check mapping before allocating. --- Kernel/arch/x86_64/mm_phys.c | 11 ++++++----- Kernel/arch/x86_64/mm_virt.c | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Kernel/arch/x86_64/mm_phys.c b/Kernel/arch/x86_64/mm_phys.c index 1e7c4c2a..77ca92e7 100644 --- a/Kernel/arch/x86_64/mm_phys.c +++ b/Kernel/arch/x86_64/mm_phys.c @@ -3,7 +3,7 @@ * * Physical Memory Manager */ -#define DEBUG 0 +#define DEBUG 1 #include #include #include @@ -314,7 +314,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) */ tPAddr MM_AllocPhysRange(int Num, int Bits) { - tPAddr addr; + tPAddr addr, ret; int rangeID; int nFree = 0, i; @@ -420,12 +420,13 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) // Mark pages as allocated addr -= Num; - for( i = 0; i < Num; i++ ) + for( i = 0; i < Num; i++, addr++ ) { gaMainBitmap[addr >> 6] |= 1 << (addr & 63); rangeID = MM_int_GetRangeID(addr); giPhysRangeFree[ rangeID ] --; } + ret = addr; // Save the return address // Update super bitmap Num += addr & (64-1); @@ -438,8 +439,8 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) } RELEASE(&glPhysicalPages); - LEAVE('x', addr << 12); - return addr << 12; + LEAVE('x', ret << 12); + return ret << 12; } /** diff --git a/Kernel/arch/x86_64/mm_virt.c b/Kernel/arch/x86_64/mm_virt.c index 8e5ef9c8..b4b26ec1 100644 --- a/Kernel/arch/x86_64/mm_virt.c +++ b/Kernel/arch/x86_64/mm_virt.c @@ -281,19 +281,31 @@ tPAddr MM_Allocate(tVAddr VAddr) { tPAddr ret; - Log("MM_Allocate: (VAddr=%x)", VAddr); - Log("MM_Allocate: MM_AllocPhys()"); + ENTER("xVAddr", VAddr); + + if( !MM_Map(VAddr, 0) ) // Make sure things are allocated + { + Warning("MM_Allocate: Unable to map, tables did not initialise"); + LEAVE('i', 0); + return 0; + } + ret = MM_AllocPhys(); - Log("MM_Allocate: ret = %x", ret); - if(!ret) return 0; + LOG("ret = %x", ret); + if(!ret) { + LEAVE('i', 0); + return 0; + } if( !MM_Map(VAddr, ret) ) { - Warning("MM_Allocate: Unable to map", ret); + Warning("MM_Allocate: Unable to map. Strange, we should have errored earlier"); MM_DerefPhys(ret); + LEAVE('i'); return 0; } + LEAVE('x', ret); return ret; } -- 2.20.1