/**************************************************************************************
Copyright (c) 2002 The UbixOS Project
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are prohibited.
$Id$
**************************************************************************************/
#include <vmm/memory.h>
#include <vmm/paging.h>
#include <ubixos/io.h>
mMap *memoryMap = (mMap *) 0x100000;
int numPages;
int countMemory() {
register unsigned long *mem;
unsigned long memCount=-1, tempMemory;
unsigned short memKb=0;
unsigned char irq1State, irq2State;
unsigned long cr0;
/* Save States Of IRQ 1 & 2 */
irq1State=inportByte(0x21);
irq2State=inportByte(0xA1);
/* Shut Off Both IRQS */
outportByte(0x21, 0xFF);
outportByte(0xA1, 0xFF);
/* Save CR0 */
asm(
"movl %%cr0, %%ebx\n"
: "=a"(cr0)
:
: "ebx"
);
asm("wbinvd");
asm(
"movl %%ebx, %%cr0\n"
:
: "a" (cr0 | 0x00000001 | 0x40000000 | 0x20000000)
: "ebx"
);
while(memKb<4096 && memCount!=0) {
memKb++;
if (memCount == -1) memCount = 0;
memCount+=1024*1024;
mem=(unsigned long *)memCount;
tempMemory=*mem;
*mem=0x55AA55AA;
asm("":::"memory");
if (*mem!=0x55AA55AA) {
memCount=0;
}
else {
*mem=0xAA55AA55;
asm("":::"memory");
if (*mem!=0xAA55AA55) {
memCount=0;
}
}
asm("":::"memory");
*mem=tempMemory;
}
asm(
"movl %%ebx, %%cr0\n"
:
: "a" (cr0)
: "ebx"
);
/* Return IRQ 1 & 2's States */
outportByte(0x21, irq1State);
outportByte(0xA1, irq2State);
numPages = (memKb*1024*1024)/4096;
return(memKb<<20);
}
void initMmap() {
int i=0;
memoryMap = (mMap *) 0x100000;
for (i=0;i<numPages;i++) {
memoryMap[i].status = memNotavail;
memoryMap[i].pid = -1;
memoryMap[i].pageAddr = i*4096;
}
for (i=(memoryStart/4096);i<numPages;i++) {
memoryMap[i].status = memAvail;
}
}
unsigned long findFreepage(int pid) {
int i=0;
for (i=0;i<=numPages;i++) {
if (memoryMap[i].status == memAvail) {
memoryMap[i].status = memNotavail;
memoryMap[i].pid = pid;
return(memoryMap[i].pageAddr);
}
}
return(-1);
}
void freePage(unsigned long pageAddr) {
int i=0;
i = (pageAddr/4096);
memoryMap[i].status = memAvail;
}
void freeProcesspages(int pid) {
int i=0;
for (i=0;i<numPages;i++) {
if (memoryMap[i].pid == pid) {
memoryMap[i].status = memAvail;
}
}
}