Newer
Older
ubixos / src / sys / vmm / memory.c
@reddawg reddawg on 11 May 2002 1 KB Woot Woot
/**************************************************************************************
 Copyright (c) 2002
      The UbixOS Project

 $Id$
**************************************************************************************/

#include <vmm/memory.h>
#include <ubixos/io.h>

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);
  return(memKb<<20);
  }