# OSDev.org

The Place to Start for Operating System Developers
 It is currently Fri Oct 23, 2020 7:18 pm

 All times are UTC - 6 hours

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Question about 32-bits pagingPosted: Fri Sep 20, 2019 10:54 am

Joined: Fri Sep 20, 2019 10:37 am
Posts: 2
Hi all!

I'm getting back into osdev and there is one thing I can't figure out about paging.
My code for getting a page table entry from a virtual address looks basically like this
Code:
uint32_t dir_index = DIRECTORY_INDEX(virt);
uint32_t table_index = TABLE_INDEX(virt);

directory_entry_t* dir = (directory_entry_t*) 0xFFFFF000;
page_t* table = ((uint32_t*) 0xFFC00000) + (0x400 * dir_index);

if (dir[dir_index] & PAGE_PRESENT) {
return &table[table_index];
}

It's about the same code given in the wiki page about paging (https://wiki.osdev.org/Paging#Manipulation) and it works like a charm.

My question: why is table not calculated like in the following code? Why does my current code work?
Code:
page_t* table = ((uint32_t*) 0xFFC00000) + (dir_index << 12);

Why do we multiply the index by 0x400 (i.e left-shift by 10) instead of multiplying by 0x1000? Multiplying by 0x400 is like left-shifting by 10 bits, so the index in the page directory should be mangled by 2 bits... I don't get it. Isn't that address translated the same way every other linear address is?

That's the last thing standing in my way for understanding paging

Top

 Post subject: Re: Question about 32-bits pagingPosted: Fri Sep 20, 2019 11:12 am
 Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1046
NotSchur wrote:
Code:
page_t* table = ((uint32_t*) 0xFFC00000) + (0x400 * dir_index);

My question: why is table not calculated like in the following code? Why does my current code work?
Code:
page_t* table = ((uint32_t*) 0xFFC00000) + (dir_index << 12);

Why do we multiply the index by 0x400 (i.e left-shift by 10) instead of multiplying by 0x1000? Multiplying by 0x400 is like left-shifting by 10 bits, so the index in the page directory should be mangled by 2 bits... I don't get it.

It's C. You're adding to a pointer to uint32_t. Adding 1 advances the pointer to the next uint32_t (4 bytes away).

Top

 Post subject: Re: Question about 32-bits pagingPosted: Fri Sep 20, 2019 3:32 pm
 Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 394
NotSchur wrote:
My question: why is table not calculated like in the following code? Why does my current code work?
Code:
page_t* table = ((uint32_t*) 0xFFC00000) + (dir_index << 12);

To expand on alexfru's response, if this code snippet were instead:

Code:
page_t* table = (uint32_t*)((0xFFC00000) + (dir_index << 12));

So that you cast to a pointer *after* adding dir_index, it should work identically to your code.

Top

 Post subject: Re: Question about 32-bits pagingPosted: Fri Sep 20, 2019 4:33 pm

Joined: Fri Sep 20, 2019 10:37 am
Posts: 2
That makes perfect sense now! Kicking myself for not having spotted this
Thanks a lot to both of you!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 4 posts ]

 All times are UTC - 6 hours

#### Who is online

Users browsing this forum: Google [Bot] and 12 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Operating System Development    OS Development    OS Design & Theory    Announcements, Test Requests, & Job openings Everything Else    General Programming    General Ramblings    Auto-Delete Forum OSDev.org    OSDev Wiki    About this site