#include <mm_virt.h>
#include <mm_phys.h>
#include <proc.h>
+#include <hal_proc.h>
#define TAB 22
-#define KERNEL_STACKS 0xF0000000
-#define KERNEL_STACK_SIZE 0x00008000
-#define KERNEL_STACKS_END 0xFC000000
#define WORKER_STACKS 0x00100000 // Thread0 Only!
-#define WORKER_STACK_SIZE KERNEL_STACK_SIZE
+#define WORKER_STACK_SIZE MM_KERNEL_STACK_SIZE
#define WORKER_STACKS_END 0xB0000000
#define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
void MM_PreinitVirtual(void);
void MM_InstallVirtual(void);
void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
-void MM_DumpTables(tVAddr Start, tVAddr End);
-tVAddr MM_ClearUser(void);
+//void MM_DumpTables(tVAddr Start, tVAddr End);
+//void MM_ClearUser(void);
tPAddr MM_DuplicatePage(tVAddr VAddr);
// === GLOBALS ===
{
if( gaPageDir[ i ] ) continue;
// Skip stack tables, they are process unique
- if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
+ if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
gaPageDir[ i ] = 0;
continue;
}
}
/**
- * \fn tVAddr MM_ClearUser()
* \brief Clear user's address space
*/
-tVAddr MM_ClearUser(void)
+void MM_ClearUser(void)
{
Uint i, j;
INVLPG( &gaPageTable[i*1024] );
}
INVLPG( gaPageDir );
-
- return *gpPageCR3;
}
/**
Uint i, j;
tVAddr ret;
Uint page = 0;
- tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
+ tVAddr kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
void *tmp;
Mutex_Acquire( &glTempFractal );
}
// Allocate kernel stack
- for(i = KERNEL_STACKS >> 22;
- i < KERNEL_STACKS_END >> 22;
- i ++ )
+ for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
{
// Check if directory is allocated
if( (gaPageDir[i] & 1) == 0 ) {
}
// We don't care about other kernel stacks
- if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
+ if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) {
gaTmpTable[i*1024+j] = 0;
continue;
}
{
tVAddr base;
Uint i;
- for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
+ for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
{
// Check if space is free
if(MM_GetPhysAddr(base) != 0) continue;
// Allocate
- //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
- for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
+ //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
+ for(i = 0; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
{
if( MM_Allocate(base+i) == 0 )
{
// On error, print a warning and return error
Warning("MM_NewKStack - Out of memory");
// - Clean up
- //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
+ //for( i += 0x1000 ; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
// MM_Deallocate(base+i);
return 0;
}
}
// Success
- Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
- return base+KERNEL_STACK_SIZE;
+// Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
+ return base+MM_KERNEL_STACK_SIZE;
}
// No stacks left
- Warning("MM_NewKStack - No address space left");
+ Log_Warning("MMVirt", "MM_NewKStack - No address space left");
return 0;
}
return ret;
}
+/**
+ * \brief Check if the provided buffer is valid
+ * \return Boolean valid
+ */
+int MM_IsValidBuffer(tVAddr Addr, size_t Size)
+{
+ int bIsUser;
+ int dir, tab;
+
+ Size += Addr & (PAGE_SIZE-1);
+ Addr &= ~(PAGE_SIZE-1);
+
+ dir = Addr >> 22;
+ tab = Addr >> 12;
+
+// Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
+
+ if( !(gaPageDir[dir] & 1) ) return 0;
+ if( !(gaPageTable[tab] & 1) ) return 0;
+
+ bIsUser = !!(gaPageTable[tab] & PF_USER);
+
+ while( Size >= PAGE_SIZE )
+ {
+ if( (tab & 1023) == 0 )
+ {
+ dir ++;
+ if( !(gaPageDir[dir] & 1) ) return 0;
+ }
+
+ if( !(gaPageTable[tab] & 1) ) return 0;
+ if( bIsUser && !(gaPageTable[tab] & PF_USER) ) return 0;
+
+ tab ++;
+ Size -= PAGE_SIZE;
+ }
+ return 1;
+}
+
/**
* \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
* \brief Duplicates a virtual page to a physical one