Help with paging

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
Zity

Help with paging

Post by Zity »

Hello..
I tries to enable paging in my os, but i doesn't work. When i try to enable it, restart my computer.
I used this tutorial http://osdever.net/tutorials/paging.php?the_id=43
I get no error the pc just restart and I have no idea about whats wrong.

Hope Somebody can help ???
bluecode

Re:Help with paging

Post by bluecode »

Can you give us some Code?
Zity

Re:Help with paging

Post by Zity »

Here is my C function
-------------------------------

Code: Select all

void enable_paging()
{
unsigned long *page_directory = (unsigned long *) 0x9C000;
unsigned long *page_table = (unsigned long *) 0x9D000;
unsigned long address=0; // holds the physical address of where a page is
unsigned int i;

// map the first 4MB of memory
for(i=0; i<1024; i++)
{
   page_table[i] = address | 3; // attribute set to: supervisor level, read/write, present(011 in binary)
   address = address + 4096;
};

page_directory[0] = page_table; // HERE I GET THIS WARNING "warning: assignment makes integer from pointer without a cast"
page_directory[0] = page_directory[0] | 3;

for(i=1; i<1024; i++)
{
   page_directory[i] = 0 | 2; // attribute set to: supervisor level, read/write, not present(010 in binary)
};
write_cr3(page_directory); // put that page directory address into CR3
write_cr0(read_cr0() | 0x80000000); // set the paging bit in CR0 to 1
}
Here is my assembler code
---------------------------------

Code: Select all

[global _read_cr0]
_read_cr0:
   mov eax, cr0
   retn

[global _write_cr0]
_write_cr0:
   push ebp
   mov ebp, esp
   mov eax, [ebp+8]
   mov cr0,  eax
   pop ebp
   retn

[global _read_cr3]
_read_cr3:
   mov eax, cr3
   retn

[global _write_cr3]
_write_cr3:
   push ebp
   mov ebp, esp
   mov eax, [ebp+8]
   mov cr3, eax
   pop ebp
   retn
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging

Post by Pype.Clicker »

That tutorial assumes your startup code, stack, IDT, GDT and stuff are all located below 4MB (physically speaking) and that code, data and stack segment are zero-based.

Is this the case for you ?
Zity

Re:Help with paging

Post by Zity »

Yes i think so...
Zity

Re:Help with paging

Post by Zity »

Now i get a "Page Fault" from my interrupts..
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging

Post by Pype.Clicker »

That's yet much better... if you can, try to make your handler print the value for CR2 and the return address (cs:eip) where the fault occured.
Zity

Re:Help with paging

Post by Zity »

Can't figure out how to do that :-[
Zity

Re:Help with paging

Post by Zity »

Allright, fount out how to write the cr2 value but now is the interrupt handler i silent >:(
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging

Post by Pype.Clicker »

in case of such error that disappear when the content of the code change, i suggest you make sure you put your *whole* kernel in memory ... What bootloader do you use ?
Zity

Re:Help with paging

Post by Zity »

User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging

Post by Pype.Clicker »

according to the "read.me"
7) Reads the file (KERNEL.BIN) to RAM starting at physical 1Mb.
8) Builds two page tables and one page directory mapping:
a) The first 4Mb linear to the first 4Mb physical
b) Linear FF800000 (where all my protected mode images start) to physical
RAM starting at 1Mb
c) Self map the page directory to the end of linear memory
9) Switch to protected mode with paging turned on
10) JMP to KERNEL.BIN at linear FF800000
So the page table and directory *is already set up*. By reloading it (without mapping FF8xxxxx, you're making your kernel disappear from the virtual address space ...
Zity

Re:Help with paging

Post by Zity »

Hmm.. I'm just stupid sometimes.. ::)
Post Reply