Page 1 of 1

Help me! Memory Managment

Posted: Fri Jul 31, 2009 2:04 pm
by TheGuy
Being fourteen and not even in high school it is quite hard for my kernel developing streak. Because of this I am having trouble with memory management.
If someone would please point me to a tutorial or help me out, by explaining in "English" terms (I understand the concept, just not how) . I have tried to
put this code together:

Code: Select all

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// end is defined in the linker script.
extern u32int end;
u32int placement_address = (u32int)&end;

// Page direcory 'Just need some free memory (4kb) that is aligned (can be divided by: 4096 without a remainder).' 0x9C000
unsigned long *page_directory = (unsigned long *) 0x9C000;

// Page Table the page table comes right after the page directory
unsigned long *page_table = (unsigned long *) 0x9D000;

// Address holds the physical address of where a page is
unsigned long address=0; 

// I is a basic counting variable for our loops
unsigned int i;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Maps the first table, then sets up other basic requirements
void MapFirstTable()
{

putshex(placement_address);

// Map the first 4MB of memory
for(i=0; i<1024; i++)
{
	page_table[i] = address | 3; // attribute set to: supervisor level, read/write, present(011 in binary)
	address = address + 4096; // 4096 = 4kb
};


// fill the first entry of the page directory
page_directory[0] = page_table; // attribute set to: supervisor level, read/write, present(011 in binary)
page_directory[0] = page_directory[0] | 3;

for(i=1; i<1024; i++)
{
	page_directory[i] = 0 | 2; // attribute set to: supervisor level, read/write, not present(010 in binary)
};

// write_cr3, read_cr3, write_cr0, and read_cr0 all come from the assembly functions
write_cr3(page_directory); // put that page directory address into CR3
write_cr0((unsigned long)read_cr0() | 0x80000000); // set the paging bit in CR0 to 1

// PAGING IS ENABLED
}
And it returns this as the placement address (If it is wrong it might be the putshex).
0x1068a0

Please help. (pl0x).

Re: Help me! Memory Managment

Posted: Fri Jul 31, 2009 6:14 pm
by frank
Your not telling us exactly what goes wrong. The placement address looks correct to me. It is at a location just above where the kernel is loaded (typically at 0x100000). Does the code crash? What are the expected results?

Re: Help me! Memory Managment

Posted: Sat Aug 01, 2009 9:07 am
by Combuster
unsigned long *page_directory = (unsigned long *) 0x9C000;
That's not necessarily usable memory. In fact, the bios may be writing over that without your knowing, even in protected mode.

Re: Help me! Memory Managment

Posted: Sat Aug 01, 2009 7:05 pm
by TheGuy
Combuster wrote:
unsigned long *page_directory = (unsigned long *) 0x9C000;
That's not necessarily usable memory. In fact, the bios may be writing over that without your knowing, even in protected mode.
Thank you, I just needed someone to look through my code. After looking through tutorials, I have to put put 1024 tables in the directory, but make some flags. Should I continue with this type of style or go more like James?

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 8:38 am
by gravaera
Tp be honest, before you started coding a kernel, you should have had a design idea in mind. What's so special about my OS? How can I implement these special functions? How do I intend to implement XYZ? So when reading tutorials, you should be looking for the concept of how to to XYZ, and not really the method the person uses in his or her tutorial.

So if I were to answer you: I'd say, do a solid draft design, and identify the strong and weak points of your idea, and YOU think about how to design YOUR operating system. I mean: even James doesn't use the code from his tutorial. I vaguely remember him saying in a previous post that his kernel's design is radically different from the code he put up in the tutorial.

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 9:15 am
by NickJohnson
Combuster wrote:
unsigned long *page_directory = (unsigned long *) 0x9C000;
That's not necessarily usable memory. In fact, the bios may be writing over that without your knowing, even in protected mode.
Really? I thought that the BIOS only needed the BDA/EBDA to function, so the area from 0x500 to (about) 0x9FC00 is free for the protected mode kernel to allocate. Should none of the "free" lower memory be touched?

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 10:02 am
by Combuster
NickJohnson wrote:I thought that the BIOS only needed the BDA/EBDA to function, so the area from 0x500 to (about) 0x9FC00 is free for the protected mode kernel to allocate. Should none of the "free" lower memory be touched?
Who said the EBDA is always 1K in size? The code in question "needs" only 8k to cause problems. And judging from linux bug reports, they had to update the EBDA reservation code several times. Brendan once posted that paranoia dictates not to use anything over the 512K mark.

A google quote for you:
interesting, EBDA should be in [0x90000, 0x100000]

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 10:22 am
by NickJohnson
But at least according to the wiki, the EBDA is almost never larger than 8KB, and the page directory is obviously 4KB, so the OP almost definitely won't have a collision (at least not in any VM), so I'd say the address of the page directory was actually not his issue. I'm not saying he shouldn't move it to avoid issues with esoteric BIOSes, but that action shouldn't fix his problem.

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 3:45 pm
by Firestryke31
Doesn't BIOS int 12h give the start of the EBDA (shifted down 12 bits)? And doesn't GRUB pass this as the first memory part of the multiboot structure? Why not calculate the address you need from this?

Or am I missing something where the BIOS manufacturers made it impossible to (consistently) tell?

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 3:52 pm
by Combuster
I'd start by using E820 for the memory map. The EBDA will not show up as available space.

Re: Help me! Memory Managment

Posted: Sun Aug 02, 2009 10:08 pm
by kop99
Hi, TheGuy.

Linux kernel is good reference. You could analyse the related code, and get some help.
And my recommended book is "Understanding the Linux Virtual Memory Manager".

Re: Help me! Memory Managment

Posted: Mon Aug 03, 2009 1:24 am
by TheGuy
kop99 wrote:Hi, TheGuy.

Linux kernel is good reference. You could analyse the related code, and get some help.
And my recommended book is "Understanding the Linux Virtual Memory Manager".
Thank you very much. I will look into that. I have made a function that lists the first 4mb (The paged memory) and I get:

(1023) (Address?) (Permission)
0x3ff 0x3ff003 0x2

So I belive my system is working, should I now make it more like James Molly, and make structures and information.
Like this:

Single
------------------------------------------
(1023) (Address?) (Permission) (Empty)
0x3ff 0x3ff003 0x2 False
------------------------------------------

Group
------------------------------------------
(Empty) //So if group is full skip over it, if not check for the address
False
------------------------------------------
(1) (Address?) (Permission) (Empty)
0x1 0x1003 0x2 False
...
(1024) (Address?) (Permission) (Empty)
0x3ff 0x3ff003 0x2 False
-------------------------------------------

Super Group
------------------------------------------
(Empty) //So if super group is full skip over it, if not check for the address (Whole memory?)
False
------------------------------------------
(1) (Empty)
0x1 False
...
(1024) (Empty)
0x3ff False
-------------------------------------------

So my idea is to make groups which tell me if a group is full and then I can look in one group at singles if it isn't (Maybe make one small group which contains 32?).

Any input?