Memory management
Memory management
Hi,
I've followed this tutorial for learning basic kernel things: http://www.osdever.net/bkerndev/index.php?the_id=90
After that, I've tried to rewrite all that code (I don't like copy + pasted code), and change things the way I like them. Now I have the basics of a working kernel, with some more "drivers" than the tutorial had.
Writing drivers wasn't a problem, just read the device specs (simple onboard things) en write something.
Then I started to look at memory management and threads. I've read a lot about paging, and I think I understand how it works in theory. But I still have the idea I can't code it, does anybody know if there are good tutorials (with code examples) on this topic? I've already read a lot of theory, but I think it's easier to read it with code.
I've followed this tutorial for learning basic kernel things: http://www.osdever.net/bkerndev/index.php?the_id=90
After that, I've tried to rewrite all that code (I don't like copy + pasted code), and change things the way I like them. Now I have the basics of a working kernel, with some more "drivers" than the tutorial had.
Writing drivers wasn't a problem, just read the device specs (simple onboard things) en write something.
Then I started to look at memory management and threads. I've read a lot about paging, and I think I understand how it works in theory. But I still have the idea I can't code it, does anybody know if there are good tutorials (with code examples) on this topic? I've already read a lot of theory, but I think it's easier to read it with code.
Re:Memory management
do you have a physical memory maneger, yet? if you don't i can help.
p.s. i agree it is never a good idea to copy+paste(bkerndev has small syntax errors in the tutorial and it annoys me when everyone asks about the same error and you now they copy+paste )
p.s. i agree it is never a good idea to copy+paste(bkerndev has small syntax errors in the tutorial and it annoys me when everyone asks about the same error and you now they copy+paste )
Re:Memory management
Hi, I am rewriting my memory manager, a phyiscal page allocator. But I think got confuse. When I build my page_table, it included the flag bits at the end. I then wrote an allocator and return address. I return what is in the page_table in stead doing +=4096 in every loop. say 0x10003.
This address should be 0x10000 phyical address and 0x10003(virtual address) in the page_table right?
I just need some comfirmation on this.
Thanks in advance
This address should be 0x10000 phyical address and 0x10003(virtual address) in the page_table right?
I just need some comfirmation on this.
Thanks in advance
Re:Memory management
I haven't really understood if you are writing a physical page allocator OR a virtual memory manager now ???, but I can say 0x10003 is not the virtual address. It is the physical address (0x10000) with the bits 0 (present) and 1 (write) set, so you can access the page and write to it.chaisu chase wrote: This address should be 0x10000 phyical address and 0x10003(virtual address) in the page_table right?
I just need some comfirmation on this.
The virtual address is determined by the position within the page table to which you write this value. So, if you want to map 0x10000 (physical) to 0x10000 (virtual), you place the value 0x10003 into the entry No. 16 in the first page table.
Hope I could help at least a bit....
cheers joe
Re:Memory management
Thanks I am writing a phyiscal allocator I think..using a bitmap, mapping the first 4MB physical address.
I want my allocator to return the finds a free page from the bitmap, and return a page entry 0x10003 instead of += 4096 repeatly to get the physical address. if so, the address I return(the page entry(0x100003) should be removed the flag bits first to make it 0x10000 instead. And use to this address(0x10000).
Thats was the idea(donno if its correct). So I just wanted to make sure if the idea is right. I might have complicated things.
Thanks for you reply~!
P.S. One more question,
If I try to assign this address 0x105000 (end of kernel) to a stack like so:
Do I need to update the page_table manually? Coz I did this and check the pagetable, the address wasn't marked as dirty or Accessed? DOes the CPU update it for me?
I want my allocator to return the finds a free page from the bitmap, and return a page entry 0x10003 instead of += 4096 repeatly to get the physical address. if so, the address I return(the page entry(0x100003) should be removed the flag bits first to make it 0x10000 instead. And use to this address(0x10000).
Thats was the idea(donno if its correct). So I just wanted to make sure if the idea is right. I might have complicated things.
Thanks for you reply~!
P.S. One more question,
If I try to assign this address 0x105000 (end of kernel) to a stack like so:
Code: Select all
NODE *current
current = ((NODE *)kernel_end_addr;
current->address = something;
Re:Memory management
There is something I don't understand.My page directory is at 9C000.
My first page table is at 9D000. If I create a few more page table. they would pointed to:
My page directory[0] pointing to 9D000
My page directory[1] pointing to 9E000
...
My page_directory[1023] pointing 49C000 I suppose..Right?
At some point say, 105000 which is my kernel end. Does that mean, I will use it for page_stack. So if I keep adding new page(for instance), I might have restricted the size of my size if at some point, my page directory[say 100] pointed to 106000. What should I do if that happen?
I don't think I m quite understand how to implement memory manager(physical, logical and virtual).
Thanks in advance.
My first page table is at 9D000. If I create a few more page table. they would pointed to:
My page directory[0] pointing to 9D000
My page directory[1] pointing to 9E000
...
My page_directory[1023] pointing 49C000 I suppose..Right?
At some point say, 105000 which is my kernel end. Does that mean, I will use it for page_stack. So if I keep adding new page(for instance), I might have restricted the size of my size if at some point, my page directory[say 100] pointed to 106000. What should I do if that happen?
I don't think I m quite understand how to implement memory manager(physical, logical and virtual).
Thanks in advance.
Re:Memory management
The Page Directory is an array of pointers to physical memory, because all address are aligned to 4096bytes, the lowest 12bit are unused so are used as flags, anything in those bits should be ANDed off (ie. pagedirentry = page_directory[1] & 0xFFFFF000;). The page tables are also arrays, once again pointing to physical memory.
The positions in the virtual space are determined by the location of the pointer. ie.
pagedirectory[0 ]->pagetable[0 ] = Virtual 0x0
pagedirectory[0 ]->pagetable[1] = Virtual 0x1000
pagedirectory[1]->pagetable[0 ] = Virtual 0x400000
pagedirectory[1023]->pagetable[1023] = Virtual 0xFFFFF000
The pointers in the tables do not have to be contiguous.
The positions in the virtual space are determined by the location of the pointer. ie.
pagedirectory[0 ]->pagetable[0 ] = Virtual 0x0
pagedirectory[0 ]->pagetable[1] = Virtual 0x1000
pagedirectory[1]->pagetable[0 ] = Virtual 0x400000
pagedirectory[1023]->pagetable[1023] = Virtual 0xFFFFF000
The pointers in the tables do not have to be contiguous.
Re:Memory management
Right...Just to make sure, how did you get the virtual address? 0x400000 from page directory[1] at page table[0]? Do you bind the indexes together to get this value(0x400000)?
binded by page_dir[1]'s index(1) to the 31 to 22 bit, and page_table[0]'s index(0) to the 21 to 12. The and rest is the offset for CPL, present etc. right?
The page table is an array pointing to physical memory, so do you mean at page directory[1] = 9E000; This is the pointer pointing to physical address of a new page_table for (4MB to 8MB) which contains from 0x0 to 0x3FF00?
What is in the page_table[0] for directory[1]? a pointer to where? 0x0?
what is in the page_table[1] for directory[1]? a pointer to where? 0x1000?
which represent 0x400000 and 0x401000 respectively?
I am sorry for asking so many boring questions. A yes and no answer will be fine. Its really hard to picture the whole thing.
binded by page_dir[1]'s index(1) to the 31 to 22 bit, and page_table[0]'s index(0) to the 21 to 12. The and rest is the offset for CPL, present etc. right?
The page table is an array pointing to physical memory, so do you mean at page directory[1] = 9E000; This is the pointer pointing to physical address of a new page_table for (4MB to 8MB) which contains from 0x0 to 0x3FF00?
What is in the page_table[0] for directory[1]? a pointer to where? 0x0?
what is in the page_table[1] for directory[1]? a pointer to where? 0x1000?
which represent 0x400000 and 0x401000 respectively?
I am sorry for asking so many boring questions. A yes and no answer will be fine. Its really hard to picture the whole thing.
Re:Memory management
The way to work out the position is quite simple:
page_directory[1]->page_table[3]
Equation: (page_dir_index * 0x400000) + (page_table_index * 0x1000)
So for the above example: 1 * 0x400000 + 3 * 0x1000 = 0x403000 Virtual
page_directory[1]->page_table[3]
Equation: (page_dir_index * 0x400000) + (page_table_index * 0x1000)
So for the above example: 1 * 0x400000 + 3 * 0x1000 = 0x403000 Virtual
Re:Memory management
Thanks, thats the same as fiddling with bits by shifting.
And sorry for the 9E000, I thought there was a third table and forth and so on...
So the 0x403000 address, if I return this address to some functions, say if I want to struct something, does that mean this virtual address will be used?
Thanks in advance
And sorry for the 9E000, I thought there was a third table and forth and so on...
So the 0x403000 address, if I return this address to some functions, say if I want to struct something, does that mean this virtual address will be used?
Thanks in advance