Paging makes me crazy *_*

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
User avatar
finarfin
Member
Member
Posts: 106
Joined: Fri Feb 23, 2007 1:41 am
Location: Italy & Ireland
Contact:

Paging makes me crazy *_*

Post 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:
User avatar
kmcguire
Member
Member
Posts: 120
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Re: Paging makes me crazy *_*

Post 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 */
User avatar
finarfin
Member
Member
Posts: 106
Joined: Fri Feb 23, 2007 1:41 am
Location: Italy & Ireland
Contact:

Re: Paging makes me crazy *_*

Post by finarfin »

Thanks a lot.
Elen síla lúmenn' omentielvo
- DreamOS64 - My latest attempt with osdev: https://github.com/dreamos82/Dreamos64
- Osdev Notes - My notes about osdeving! https://github.com/dreamos82/Osdev-Notes
- My old Os Project: https://github.com/dreamos82/DreamOs
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Paging makes me crazy *_*

Post by eax »

Taken out so as not to confuse other newbies
Last edited by eax on Sun Jun 29, 2008 11:41 am, edited 2 times in total.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Paging makes me crazy *_*

Post 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
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Paging makes me crazy *_*

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