memory size probing

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
slacker

memory size probing

Post by slacker »

i was wondering how you are supposed to use probing to get the size of memory since you will get an error if u try to access a location that does not exist.
pskyboy

Re:memory size probing

Post by pskyboy »

When probing memory directly you will not get an error if you prob a location that doesn't exist. Instead it will just not store the value you placed there. The standard procedure for memory testing is to start at a certain memory location usually the top of 1Mb and then write a DWORD to the top of each megabyte from there onwards. You then read the DWORD you just wrote and compare it to what it is ment to be and if it is the same you have another mb of memory. Her is a copy of my memory checker function.

Code: Select all

void CPhysicalManager::CheckMemory()
{
    //Print a Message to let the Developer/User know we are checking the memory
    kprintf("Checking Memory", 1);
    
    UINT* pStartMem = (UINT*)0x1FFFFC;    //Start at top of 1mb (2mb - 4bytes) as we already know we must have 1mb
    const UINT uiMBOfMem = 0x100000;      //Amount of bytes to make up 1mb, define as const as this doesn't change
    
    const UINT uiTestDWORD = 0x506E6978;  //Test DWORD to write = Pnix, for Phoenix, define as const for speed up
    UINT uiTotalMemory = 0x100000;        //We know we have 1mb already
     
    //Write to the Highest DWORD in each megabyte to test for amount of memory available
    do
    {
        //Calculate next memory address    
        pStartMem = (UINT*)(uiMBOfMem + (UINT)pStartMem);
        
        //Write the test DWORD to top of this 1Mb of Memory
        *pStartMem = uiTestDWORD;
        
        //Calculate the Amount of Physical Memory now available 
        uiTotalMemory += uiMBOfMem;
        
    }while(*pStartMem == uiTestDWORD);
    
    //Store the result from the memory test in the Total Amount of Pages
    //Shift amount of mem left by 12 to give amount of pages
    uiTotalPages = uiTotalMemory >> 12;
    
    //kprintf(uiTotalPages, 13);
}
Tim

Re:memory size probing

Post by Tim »

Use the BIOS, or whatever memory map GRUB gives you. See the FAQ for details.
slacker

Re:memory size probing

Post by slacker »

i dont use grub.... will never use grub
Tim

Re:memory size probing

Post by Tim »

Why not?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:memory size probing

Post by distantvoices »

slacker... will never use this will never use that ... of course it is your decision, but do not close ways to ease life just due to stubborn self confidence or proudness.

grub is a good wheel. why reinventing it? Smart guys have invented it. I for my part won't try to be smarter. I lack the time and the will to do this. I "wrote" a boot sector, of course, to see how the principle runs, but then - ZACK - I took a professional solution.

stay safe.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:memory size probing

Post by df »

weve all written bootsectors and loaders too.. but we all mostly use grub...

reason being, there are MANY idosynchrisies in booting a PC, which grub pretty much handle. all the little quirks in bios and different pc manufacturers etc.

a lot of people seem to have a thing against using grub.... ohwell..

you can lead a horse to water but cant make him drink...
-- Stu --
pskyboy

Re:memory size probing

Post by pskyboy »

Yes, but if your arrogant enough like me you think you can write something better :). It also presents a challenge to be taken.
"Why climb everest? Because its there"
I think that somes up mine along with other people on this boards attitude.

Peter
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:memory size probing

Post by distantvoices »

peter, that is, why i have "written" my own boot sector.

but the point is: there is no need to waste good breath to climb a mountain just cuz it is there, when others have done this already for you *gg* Ok, this argument leads to one other point: why the heck do i write an os then? humhom, it's more fun then writing boot sectors, that's it. *ggg* and it is more interesting and you can do more hacky and prizzling things ;) once you are in pmode.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Tux

Re:memory size probing

Post by Tux »

?

Code: Select all

(Why do people confuse simple things even more?)
//C++ function

int getmemory()
{
  int where=0x200000;
  for(;;)
  {
    char * point=(char *) where;
    point[0]='L'; //L is for Linux :)
    if(point[0]=='L')
    {
      where*=2;
    }
    else
    {
      where/=2;
      return where;
    }
  }
}

//Also, here is a function to output the result
        void val2chr(int a,char *b)
        {
          int top=0;
          int bottom=0;
          int pos=10;
          char mov[10];
          
          if(a==0)
          {
            b[0] = '0';
            return;
          }
          
          //Loop it out
          while(a!=0)
          {
            top=a/10;
            top*=10;
            bottom=a-top;
            mov[pos]=(char)(((int) '0') + bottom);
            a/=10;
            pos--;
          }
          
          //Move it all
          for(int loop=0;loop<(10-pos);loop++)
          {
            b[loop]=mov[pos+loop+1];
          }
        }
//The above function does some unneccesary steps, 
//but it still does the job
Tux

Re:memory size probing

Post by Tux »

I forgot to turn off special.

Code: Select all

          if(a==0)
          {
            b[0] = '0';
            return;
          }
That's one of the messups

Code: Select all

    point[0]='L'; //L is for Linux :)
    if(point[0]=='L')
    {
      where*=2;
    }
That was the first one!
slacker

Re:memory size probing

Post by slacker »

it takes a simple asm file to use instead of grub and if you know exactly what is going on in the bootloader you can debug a problem easier.
Tim

Re:memory size probing

Post by Tim »

Right, this is why you shouldn't bother writing your own bootloader.

A boot loader is like the icon for a GUI application. You can start off by making one, and spend far too long on it; doing this makes it look like you're doing work on the app (OS), but you really haven't started. Once the app (OS) is running, the icon (boot loader) is discarded and never used again until the next time the user starts it. Usually, a pre-made icon (boot loader, e.g. GRUB) will be enough. Of course, before you release the full version of your app (OS), you need a nice customised icon (full boot loader appropriate to your kernel).

Icon design is not application development. Writing boot loaders is not OS development.

"But it helps me learn!", you might say. Well, yes, you acquire new skills. But these are virtually pointless skills. This is what you'll learn:
  • 16-bit assembly code. Even if you do write assembly code in the full OS, it'll be 32-bit, which is very different.
  • How to interface to the BIOS. You'll need to write your own drivers from scratch later, so you won't use the BIOS again.
  • How to write code to fit into 512 bytes. This encourages very bad programming, since you use all kinds of tricks to do as much as you can in those 512 bytes. You should never use these tricks outside of a boot loader.
I use GRUB now. I had my own boot loader (several, actually), but I came to realise that it was pointless to maintain my own.
_mark

Re:memory size probing

Post by _mark »

Personally I think it is cheating to not write your own bootloader. After all, if you do not have the skills to write and OS, you will find out in the bootloader stage.

_mark()
Tim

Re:memory size probing

Post by Tim »

Why? A boot loader requires an entirely different skill set to overall kernel development, as I summarised in my last post. It's possible to be very good at writing small assembly programs, such as a boot loader, and bad at a large software engineering project, such as an OS kernel; and vice versa.
Post Reply