Error in Setting Up Paging?

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
User avatar
narke
Member
Member
Posts: 119
Joined: Wed Dec 26, 2007 3:37 am
Location: France

Error in Setting Up Paging?

Post by narke »

This tutorial is very cool but there is a line:

Code: Select all

//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: Select all

//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
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: Error in Setting Up Paging?

Post by AndrewBuckley »

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
??
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Error in Setting Up Paging?

Post by iansjack »

Merlin wrote: could be wrong.
Well, at least you got that bit right.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Error in Setting Up Paging?

Post by sortie »

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].
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Error in Setting Up Paging?

Post by bluemoon »

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.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: Error in Setting Up Paging?

Post by Griwes »

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
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Error in Setting Up Paging?

Post by dozniak »

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: Select all

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