A good way for storing&accessing paging structure

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
lexected
Posts: 23
Joined: Wed Aug 14, 2013 11:48 am

A good way for storing&accessing paging structure

Post by lexected »

Hello

During the summer summer I jumped from writing C programs to lower level and learnt many things about assembly and operating systems. Now I want to get back to it and I'm decided to write a microkernel.

I have used GRUB2 for loading my ELF file, which is planned to be a higher half kernel. Its entry point maps the whole kernel to the highest gigabyte of memory and tries to create more address spaces for other processes. Physical memory management code uses memory map provided by GRUB to fill a bitmap, which covers whole possible 4GBs of memory. My kernel uses page directory (set up by entry assembly) located after kernels code, data and uninitialized data, together with a single page table which has been enough so far.

Whenever I want to create a new page table or page directory, I need to allocate it somewhere. I can get address of first free physical page from the bitmap I have, but I need to access it somehow, as it's not paged. So, I can use a special address within my kernel´s page table to write PDEies pointing on page tables that are global (kernel) and then use it for different PDE, or I can identity-page its physical location. If I choose to do the first, I need to map every single page directory or page table I'm going to work with to my special page and execute invlpg each time I manipulate with it. The second option requires creating lots of page tables for only holding another page tables and directories for different address spaces to be accessible for the kernel every time its necessary, but it can save me some time. I would choose to create a special physical location that would be identity-paged in kernel´s space, but the problem is that I don't know how many page tables and directories am I going to have. And if I wanted to manage somthing like "blocks of paging entities", I would need to hold bitmap for each of them + have some array or list in kernel.

How can I hold page tables and page directories to be quickly accessible when system needs it, without limiting quantity of address spaces?
How do hobby operating systems usually solve this problem?

Thanks,
Filip.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: A good way for storing&accessing paging structure

Post by BMW »

Thankfully, there is a very elegant solution to this problem.

I'll give you a clue: what would happen if you made the last page directory entry point to the physical address of the page directory?

See if you can work it out. :) If not, I'll explain a bit more.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: A good way for storing&accessing paging structure

Post by bluemoon »

BMW wrote:Thankfully, there is a very elegant solution to this problem.
I'll give you a clue: what would happen if you made the last page directory entry point to the physical address of the page directory?
See if you can work it out. :) If not, I'll explain a bit more.
Note that it is not a must to use the last entry for recursive paging, you can indeed use any entry in the first level of page structure.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: A good way for storing&accessing paging structure

Post by BMW »

bluemoon wrote:Note that it is not a must to use the last entry for recursive paging, you can indeed use any entry in the first level of page structure.
Indeed, of course. I just said the last entry as it is what I use, and because he has a higher half kernel.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
lexected
Posts: 23
Joined: Wed Aug 14, 2013 11:48 am

Re: A good way for storing&accessing paging structure

Post by lexected »

That's interesting, thank you all!

I have one more question:
As I want to create a microkernel, I want to place everything that has something to do with filesystems or storage devices into user processes, and I wonder how can I load for example ATA driver process code, when I don't have anything that could load the process itself. Do I need to include this "basic" processes to kernel file and place them to physical memory by editing linker script? Is there any more elegant way for "loading" these processes?
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: A good way for storing&accessing paging structure

Post by BMW »

filipadamer wrote:That's interesting, thank you all!

I have one more question:
As I want to create a microkernel, I want to place everything that has something to do with filesystems or storage devices into user processes, and I wonder how can I load for example ATA driver process code, when I don't have anything that could load the process itself. Do I need to include this "basic" processes to kernel file and place them to physical memory by editing linker script? Is there any more elegant way for "loading" these processes?
Would it be possible to get your bootloader to load it?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
lexected
Posts: 23
Joined: Wed Aug 14, 2013 11:48 am

Re: A good way for storing&accessing paging structure

Post by lexected »

I thought that I could use "initrd" from GRUB, but it's only possible after using "linux" command (I use multiboot). I don't know.
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: A good way for storing&accessing paging structure

Post by Combuster »

"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 ]
lexected
Posts: 23
Joined: Wed Aug 14, 2013 11:48 am

Re: A good way for storing&accessing paging structure

Post by lexected »

Thanks for the link on modules.
In GRUB2 manual I found only insmod command which loads relocatable object for GRUB´s kernel. Can I load my user space programs on arbitrary addresses (by customizing linker output), or it's necessary to write my own module?
User avatar
mnovotny
Member
Member
Posts: 27
Joined: Mon Aug 10, 2009 2:54 am
Location: Slovakia

Re: A good way for storing&accessing paging structure

Post by mnovotny »

GRUB2 has "module" command too, but it's part of the multiboot/multiboot2 (GRUB) module. This is how I boot my OS in grub.cfg (this is in menuentry):

multiboot /boot/bootem
module /boot/kernelem
boot

(Yes, I load kernel as a module.)

You could load all the critical drivers as modules and GRUB will tell you where they are loaded via the multiboot structure. However the modules are not parsed by GRUB, they are just copied raw, so if you're using elf or some other format you will have to parse it yourself.
Keep the world with all its sin
It's not fit for livin' in
lexected
Posts: 23
Joined: Wed Aug 14, 2013 11:48 am

Re: A good way for storing&accessing paging structure

Post by lexected »

Thank you, I found it in GRUB sourcecode. (BTW SK :D )
Post Reply