Page 1 of 1

Paging questions

Posted: Sun Mar 03, 2013 1:20 pm
by Mikemk
I have some questions about paging. Can I say, use paging in a bootloader so my kernel only sees ram that's actually available, then use another level of paging for the memory manager, then another level, and another, etc., or can I only use one?

Re: Paging questions

Posted: Sun Mar 03, 2013 1:34 pm
by iansjack
What do you mean by "another level"? You want logical addresses that are translated to other logical addresses that are translated to pysical pages? I can't see the point of that, but I can't see what else you mean.

Re: Paging questions

Posted: Sun Mar 03, 2013 3:25 pm
by Mikemk
Yes, that way my kernel can basically "map out" all the non usable ram (memory mapped devices, etc.) from the view of my memory manager. Then the mm can find available ram to assign to running programs. It would really simplify algorithms if available - otherwise the mm would have to look both at what is non available and non usable when finding ram.

Re: Paging questions

Posted: Sun Mar 03, 2013 3:31 pm
by rdos
m12 wrote:Yes, that way my kernel can basically "map out" all the non usable ram (memory mapped devices, etc.) from the view of my memory manager. Then the mm can find available ram to assign to running programs. It would really simplify algorithms if available - otherwise the mm would have to look both at what is non available and non usable when finding ram.
The memory manager typically doesn't need to map unusable or free ram to anything. It is only the allocated memory that needs to be mapped in the paging structures. How you manage free and unavailable ram is your own choice.

Re: Paging questions

Posted: Sun Mar 03, 2013 4:01 pm
by iansjack
One way of handling physical memory is to keep a bitmap of unallocated and allocated pages. Pages that you never want to allocate are marked in this bitmap as used when you initialize the memory structures and variables at boot time. When the memory manager needs a free page it searchs through the bitmap for unallocated memory. The areas that you have marked as reserved will never be considered available for allocation. No need for complicated multi-level memory mapping (which the hardware doesn't support). I don't know if people have other methods of allocating pages, but that works for me.

KISS.

Re: Paging questions

Posted: Sun Mar 03, 2013 4:07 pm
by Mikemk
But then if, say, the ram is full and needs to be swapped out, the memory manager might swap out the bios or memory mapped devices, etc. My idea would be simpler to implement if there is hardware support for it, otherwise i'll just do it the normal way.

Re: Paging questions

Posted: Sun Mar 03, 2013 4:49 pm
by iansjack
No. When determining what memory to swap out you will be looking at page tables to see which pages are dirty, which have been accessed, which are read only, that sort of thing. And it's in the page tables that you record that the page has been swapped out (with the "Not Present" flag and record details of its location on the swap device). So you are only concerned with looking at pages that have been mapped in some page table. You certainly wouldn't just look at the bitmap of allocated pages. Pages that haven't been allocated to some page table simply aren't available for swapping.

If for some reason you want to do things your way, just make the bitmap slightly more complicated so that it records a number of values rather than just on/off. You could have free/allocated/don't_touch, for example. I'm afraid that multiple levels of logical mappings would not be simpler; it would just be an added level of complication, and an added level of lookup to potentially slow down memory access.

Re: Paging questions

Posted: Sun Mar 03, 2013 5:43 pm
by Mikemk
Ok, thank you. a couple other questions:
  1. Does paging apply to ring 0 processes (eg kernel)
  2. Do the GDT and IDT reference physical or virtual addresses? I'm guessing the GDT references physical, but what about IDT?

Re: Paging questions

Posted: Sun Mar 03, 2013 6:18 pm
by Griwes
I am calling RTFM on this one.

Re: Paging questions

Posted: Sun Mar 03, 2013 6:38 pm
by Mikemk
I've been reading through and rereading through the wiki for months. It's a lot of information

Re: Paging questions

Posted: Sun Mar 03, 2013 6:54 pm
by Brendan
Hi,
m12 wrote:
  1. Does paging apply to ring 0 processes (eg kernel)
  2. Do the GDT and IDT reference physical or virtual addresses? I'm guessing the GDT references physical, but what about IDT?
Paging applies to all privilege levels (e.g. including kernels).

There are 3 kinds of addresses - virtual addresses, linear addresses and physical addresses. The CPU converts a virtual address into a linear address by applying segmentation (e.g. adding "segment register base"). The CPU converts a linear address into a physical address by using paging. If segmentation is effectively disabled (e.g. all segment register bases set to zero and segment limits set to "max") then there's no difference between virtual addresses and linear addresses. If paging is disabled then there's no difference between linear addresses and physical addresses. If both paging and segmentation are disabled then virtual addresses are the same as physical addresses.

Most things use virtual addresses (e.g. for "lgdt [foo]", foo is a virtual address). Some things use linear addresses (e.g. for "lgdt [foo]", the structure at foo contains the base address and limit for the GDT, and the base address is a linear address). The only thing that uses physical addresses is the paging structures (e.g. CR3, page table entries, page directory entries, etc).


Cheers,

Brendan

Re: Paging questions

Posted: Sun Mar 03, 2013 7:08 pm
by Mikemk
Thank you.