OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:12 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Error in Setting Up Paging?
PostPosted: Thu Aug 15, 2013 9:05 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 26, 2007 3:37 am
Posts: 117
Location: France
This tutorial is very cool but there is a line:
Code:
//our first page table comes right after the page directory
unsigned int *first_page_table = page_directory + 0x1000;


The thing is that page_directory and first_page_table are declared as pointers to integers, for example on 32-bit Intel an int is 4 bytes long, so if you advance a pointer to an integer by 1 in fact you will advance by 4 bytes in the memory region.
So, as 0x1000 = 4096, in fact it will advance in memory by 0x1000 * 4 = 4096 * 4 = 16384.

But we need to advance only by 4096 bytes (0x400 in hex) becauses the next page table is in 4096 bytes.

So the correct line should be:
Code:
//our first page table comes right after the page directory (4096 bytes offset),
unsigned int *first_page_table = page_directory + 1024;


Isn't it?

_________________
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Thu Aug 15, 2013 1:29 pm 
Offline
Member
Member

Joined: Thu Jan 29, 2009 9:13 am
Posts: 95
I think in C there might be a difference between nth elements in an array and adding to an address. could be wrong.
u32int array
array[1] != ptr_to_array +1
??


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Thu Aug 15, 2013 1:36 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Merlin wrote:
could be wrong.

Well, at least you got that bit right.


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Thu Aug 15, 2013 2:36 pm 
Offline
Member
Member
User avatar

Joined: Wed Mar 21, 2012 3:01 pm
Posts: 930
In the C programming language, the expression ptr[index] is actually the same as typing *(ptr + index). Indeed, the [] operator is just syntactic sugar. Curiously, *(ptr + index) is the same as *(index + ptr) which through the first rule is the same as index[ptr].


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Fri Aug 16, 2013 10:04 pm 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
sortie wrote:
Curiously, *(ptr + index) is the same as *(index + ptr) which through the first rule is the same as index[ptr].


No, you need to consider data type in question too (which is crucial to produce final memory address), *(ptr + index) uses ptr as current data type, while *(index + ptr) uses index.


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Sat Aug 17, 2013 4:30 am 
Offline
Member
Member
User avatar

Joined: Sat Jul 30, 2011 10:07 am
Posts: 374
Location: Wrocław/Racibórz, Poland
No, it doesn't, it uses pointer's type, regardless of its position in the expression.

And for the original problem, there are about three possible solutions: change the 0x1000 to 1024 AND change the pointer's type to uint32_t (so it's always 4096 bytes), add a sane datatypes of size of 4096 that represent paging structures (and use + 1 and cast to proper type) or use an integer, not a pointer, to calculate those addresses and cast that integer to pointers to either uint32_t or proper types representing paging structures.

_________________
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations


Top
 Profile  
 
 Post subject: Re: Error in Setting Up Paging?
PostPosted: Sat Aug 17, 2013 4:33 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
bluemoon wrote:
sortie wrote:
Curiously, *(ptr + index) is the same as *(index + ptr) which through the first rule is the same as index[ptr].


No, you need to consider data type in question too (which is crucial to produce final memory address), *(ptr + index) uses ptr as current data type, while *(index + ptr) uses index.


No, since index is size_t or ssize_t compatible type, the dereference would not work if it was used as the base type in such expression.

It's easy to illustrate with the simple code snippet:
Code:
#include <stdio.h>
#include <stdint.h>

int main()
{
    uint64_t arr[]={1,2,3};
    uint32_t agg[]={5,6,7};

    printf("%llu\n", *(arr+1)); // 2
    printf("%llu\n", *(1+arr)); // 2, not 0x0200 0000 0000 0000
    printf("%u\n", *(agg+2)); // 7
    printf("%u\n", *(2+agg)); // 7, not 0x0006 0000
}

_________________
Learn to read.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group