Help me! Memory Managment

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.
Post Reply
TheGuy
Posts: 13
Joined: Wed Jul 08, 2009 11:14 pm

Help me! Memory Managment

Post 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).
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Help me! Memory Managment

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Help me! Memory Managment

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
TheGuy
Posts: 13
Joined: Wed Jul 08, 2009 11:14 pm

Re: Help me! Memory Managment

Post 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?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Help me! Memory Managment

Post 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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Help me! Memory Managment

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Help me! Memory Managment

Post 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]
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Help me! Memory Managment

Post 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.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Help me! Memory Managment

Post 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?
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Help me! Memory Managment

Post by Combuster »

I'd start by using E820 for the memory map. The EBDA will not show up as available space.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Help me! Memory Managment

Post 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".
TheGuy
Posts: 13
Joined: Wed Jul 08, 2009 11:14 pm

Re: Help me! Memory Managment

Post 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?
Post Reply