init paging problem

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
marino

init paging problem

Post by marino »

I read ImplementBasicPaging tutorial and write this code. But weird things happen, Im going to be mad. I map 1-1 virtual-physical memory.

Code: Select all

void InitPaging(void)
{
    unsigned long *PageDirectory = (unsigned long *) 0x9C000;
    unsigned long *PageTable = (unsigned long *) 0x9D000;

    unsigned long Address=0;
    unsigned int i,t;

    for(t=0; t<1024; t++) {
        for(i=0; i<1024; i++) {
            PageTable[i] = Address | PTE_SUPERVISOR | PTE_READWRITE | PTE_PRESENT;
            Address += 4096;           // 4096 = 4kb
        }

        (unsigned long*)PageDirectory[t] = (unsigned long*)PageTable;
        PageDirectory[t] = PageDirectory[t] | PDE_SUPERVISOR | PDE_READWRITE | PDE_PRESENT;
        PageTable += 4096;
    }

    WriteCR3((unsigned int)PageDirectory);
    WriteCR0(ReadCR0() | 0x80000000);
}
When I check last value of t it is 3 not 1024... And last value of i is also 1010.

I think Im misused some pointers but I couldnt find.
What is wrong with this code, please help..
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:init paging problem

Post by Brendan »

Hi,

You're trying to fully fill the page directory, or do a 1:1 mapping of the entire 4 GB. To do this you'd need 1024 page tables, which works out to 4 MB of RAM (everything from 0x0009D000 to 0x0049D000 would be overwritten).

If the last value of 't' is 3 and the last value of 'i' is 1010, then you would've done 4082 page table entries, which means it would've overwritten from 0x0009D000 to 0x000A0FC8 before it stopped. Did the video display go funny?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
marino

Re:init paging problem

Post by marino »

Brendan wrote: Hi,

You're trying to fully fill the page directory, or do a 1:1 mapping of the entire 4 GB. To do this you'd need 1024 page tables, which works out to 4 MB of RAM (everything from 0x0009D000 to 0x0049D000 would be overwritten).

If the last value of 't' is 3 and the last value of 'i' is 1010, then you would've done 4082 page table entries, which means it would've overwritten from 0x0009D000 to 0x000A0FC8 before it stopped. Did the video display go funny?


Cheers,

Brendan
Yeah, as you said creating page table entries not completed. But whenever I put a counter in inside loop, it counts up to 3058 (but last value of t is 3, i is 1010). It means the inside loop doesnt count up to 1024 as well. And last pagetable entries addr is not around 0x0049d000. It is around 17Mb. And my screen is not screwed up, it works well.

But..

When I try to allocate memory with malloc function it works good until 30Mb. And if I want to alloc more memory it throws fage fault (What makes me suspect my page initialization function and found this weird things..)

....going to be mad.. please help...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:init paging problem

Post by Brendan »

Hi,
marino wrote:Yeah, as you said creating page table entries not completed. But whenever I put a counter in inside loop, it counts up to 3058 (but last value of t is 3, i is 1010). It means the inside loop doesnt count up to 1024 as well. And last pagetable entries addr is not around 0x0049d000. It is around 17Mb. And my screen is not screwed up, it works well.

But..

When I try to allocate memory with malloc function it works good until 30Mb. And if I want to alloc more memory it throws fage fault (What makes me suspect my page initialization function and found this weird things..)
Out of curiousity, is it possible that your stack is somewhere between 0x0009E000 and 0x000A0000?

In this case you'd be trashing your stack (and the local variables your code is using) before it gets a chance to trash video memory, and before you try to fill the ROM with page table entries...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply