Page 1 of 1

Paging makes me crazy *_*

Posted: Sun Jun 15, 2008 7:04 am
by finarfin
Hi there,
I have some doubts about memory paging.
My idea is to have a pagedir for each process, currently I've loaded a pagedir with the kernel mapped inside, and it works well, but it's not clear to me how to create new pagedirs.
My questions are:
What exactly are the steps to follow to allocate a new pagedir?
I have to request a new 4k page physically or logically?
If i request a physical page how I can initialize it? I map it temporary on the current pagedir?
I read different tutorials without finding a clear explanation of this point.

Please help me ^^
:shock:

Re: Paging makes me crazy *_*

Posted: Sat Jun 21, 2008 10:42 am
by kmcguire
finarfin wrote:I have to request a new 4k page physically or logically?
You have to request a new 4K page physically. The value in the CR3 register is a physical address. All addresses in the page directory and page tables are physical addresses.
finarfin wrote:If i request a physical page how I can initialize it? I map it temporary on the current pagedir?
If you are currently operating inside a virtual address space then you would be _required_ to have the physical page mapped into your address space using some means in order to initialize it.
finarfin wrote:What exactly are the steps to follow to allocate a new pagedir?
  1. Allocate a new physical memory page. (Or, find one and set it as used)
  2. Map the new physical map so you can access it.
  3. Initialize the new physical memory page.
A simple setup that I have seen most people pick is to reserved a certain region of virtual memory as kernel space. I would pick this region to contain your current kernel. Then you create two sets of functions or a manner of choosing between allocating memory for kernel or user space.

The idea is kernel space is a range of tables in the page directory of every process that remain the same. You essentially use the same tables in this range for all directories. This range is the kernel space. This should allow you to allocate new page directory from physical memory and map them into kernel space so that all page directory (for all processes) are accessible in kernel space from ring zero(supervisor).

Code: Select all

0x0                                0x5000000                                               0xffffffff
[              kernel space          ][                        user space                     ]
I acquired the value 0x5000000 by choosing the 21st page directory entry, or also known as the 21st page table. So I choose to reserve the 1st to the 21st page table for kernel space. The 22nd and 1024th table are user space. Also to realize is that the 21st entry has an index of 20.

Code: Select all

directory[0] = 1st entry
directory[1] = 2nd entry
....
directory[20] = 21st entry
And, if you remember parts of a virtual address in binary (which are):
AAAAAAAAAABBBBBBBBBBCCCCCCCCCCCC
Where A is the page directory index, B is the page table index, and C is the byte index in the page. We can find the directory and table index from the value 0x5000000 above by doing:

Code: Select all

directory_index = 0x5000000 >> (10 + 12);
table_index = (0x5000000 << 10) >> 12; /* shift to the left to clear the directory index, and back to the right to clear the byte index */

Re: Paging makes me crazy *_*

Posted: Wed Jun 25, 2008 2:38 am
by finarfin
Thanks a lot.

Re: Paging makes me crazy *_*

Posted: Sun Jun 29, 2008 8:46 am
by eax
Taken out so as not to confuse other newbies

Re: Paging makes me crazy *_*

Posted: Sun Jun 29, 2008 9:46 am
by JamesM
Hi,

I don't have time for a full explanation, but 0x5000000 is equal to 80 MB, as shown by google.

the 0x5000000 is in bytes not bits, and is a 32-bit address that just so happens to have the top 5 bits clear. It would be better written as "0x05000000".

Cheers,

James

Re: Paging makes me crazy *_*

Posted: Sun Jun 29, 2008 11:08 am
by eax
Thanks a bunch for the reply James! I ended up with 10MB again until I went over exactly what you said and worked it out in the following way which I ended up getting the same answer so I hope Ive got it correct now :)

83886080bytes / 1024 = 81920KB
81920KB / 1024 = 80MB

I have been reading quite a diverse amount of stuff just trying to get my head around the theory's as well as learning alot from the code I'm looking at now but yeah I realize I am going to have to keep googling a hell of alot :mrgreen: Although Ive a long way to go well this is the closest ever Ive came to remotely understanding it all and I'm hoping "The indispensable PC Hardware Book" helps when it arrives.

Cheers for the reply :)

edit : Im also in the process of working this out

Code: Select all

A  A   A   A   A   A   A   A   A   A
0   1   2   3    4   5    6   7    8   9 

continue
 B   B    B    B    B     B     B     B     B    B
10  11  12    13  14    15    16    17    18   19

continue
C    C    C    C    C    C    C    C    C    C    C    C
20   21  22   23   24  25   26   27   28  29   30   31
^---------------------------------Is this the 21st entry then?
(Just noticed that adds to out 32 bits made up of:

Directory index [0-20] 0being first element makes 20 be 21st entry(start of byte index?)
PageTable index[10-19]
ByteIndex[20-31]


struggling with the following bit but will do my best to find something of relevance on google now

directory_index = 0x5000000 >> (10 + 12); (code above)

Now I try and break it down but Im struggling to see how the 14h has relevance
10h+12h = 22h
0x05000000 >> 22h = 14h

actually hang on, 14h = 20dec , 20 dec is the 19th entry ? am I on the right track here?

edit: Im just about ready to finish up brans guide but I will soon be having to create my own pages so Im spending a bit of time on theory today. This seems to be a good site regarding paging, it starts of explaining it very abstract:

http://www.linux-tutorial.info/modules. ... pageid=261

Im hoping once Im done with that I can answer my own question in my code block in this post :), either way maybe it will help someone and Ive learned stuff already and the first page at least makes much sense and I still havent read it all. Anyway hope it helps