problems enabling 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
assainator
Member
Member
Posts: 30
Joined: Sun Jan 24, 2010 1:12 am
Location: The Netherlands

problems enabling paging

Post by assainator »

heey all,

after setting up gdt, idt, irs's, irq's and the pit I wanted to turn to memory managment and have chosen to use paging (as is supposed to be the easiest one).
I create the pagingTable and directory. But as soon as i enable it, bochs just restarts.

I have the following code:

Paging.h

Code: Select all

//prototypes
void paging_install();
Paging.c

Code: Select all

#include <Paging.h>
#include <Printing.h>

unsigned int *page_directory;
unsigned int *page_table;

extern int __KERNEL_END;
extern void enablePaging();


void paging_install()
{
  //the paging directory is just behind the kernel
  page_directory = (unsigned int *) __KERNEL_END;
  
  //the first paging directory (this means for the first 4mb for memory) is right behind the paging directory
  page_table = (unsigned int *) __KERNEL_END + 0x01000;
  
  //now we fill the table
  int i;
  int adress = 0;
  for(i = 0; i < 1024; i++)
  {
    page_table[i] = adress | 3; //Attributes: supervisor, read/write, present
    adress += 0x01000;       //0x01000 = 4096 = 4kb
  }
  
  //at the table to the directory
  page_directory[0] = (int)page_table;
  page_directory[0] |= 3; //Attributes: supervisor, read/write, present
  
  //fill the rest of the tables in the directory with non-present flags
  for(i = 0; i < 1024; i++)
  {
    page_directory[0] = 0 | 2; //Attributes: supervisor, read/write, not present
  }
  
  //
  //finally enable and start paging
  //
  
  //load the page directory pointer
  __asm__ __volatile__("mov %0, %%cr3":: "b"(page_directory));
  
  //now start it!
  //enablePaging();
}

paging.asm

Code: Select all

[global _enablePaging]
_enablePaging:
  mov eax, cr0
  or eax, 0x80000000
  mov cr0, eax

link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;_end = .;__end = .;___KERNEL_END = .;
}
Every human has to do something idiot to prevent becoming a complete one
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: problems enabling paging

Post by neon »

Hello,

In paging_install you first set page_directory[0] to point to your page table after which you clear it in the following loop. This will invalidate your first page table and will crash.

If the error occurs again, post your bochs crash log (last few lines only please).
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: problems enabling paging

Post by lemonyii »

Code: Select all

page_directory = (unsigned int *) __KERNEL_END;
 
  //the first paging directory (this means for the first 4mb for memory) is right behind the paging directory
  page_table = (unsigned int *) __KERNEL_END + 0x01000;
is the __KERNEL_END aligned to 4k? if not ,try (__KERNEL_END+0xfff)&0xfffff000
Enjoy my life!------A fish with a tattooed retina
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: problems enabling paging

Post by lemonyii »

and here

Code: Select all

//fill the rest of the tables in the directory with non-present flags
  for(i = 0; i < 1024; i++)
  {
    page_directory[0] = 0 | 2; //Attributes: supervisor, read/write, not present
  }
why is "i" start at 0 and never used? you have cleaned the first entry.
Enjoy my life!------A fish with a tattooed retina
assainator
Member
Member
Posts: 30
Joined: Sun Jan 24, 2010 1:12 am
Location: The Netherlands

Re: problems enabling paging

Post by assainator »

With both changes
1. for(i = 1; i < 1024; i++)
2. (__KERNEL_END + 0X0fff) & 0x0fffff000

bochs still restarts as soon as my kernel is loaded.

Code: Select all


unsigned int *page_directory;
unsigned int *page_table;

extern int __KERNEL_END;
extern void enablePaging();


void paging_install()
{
  [b]int temp = (__KERNEL_END + 0X0fff) & 0x0fffff000;
  //the paging directory is just behind the kernel
  page_directory = (unsigned int *) temp;
  
  //the first paging directory (this means for the first 4mb for memory) is right behind the paging directory
  page_table = (unsigned int *) temp + 0x01000;[/b]
  
  //now we fill the table
  int i;
  int adress = 0;
  for(i = 0; i < 1024; i++)
  {
    page_table[i] = adress | 3; //Attributes: supervisor, read/write, present
    adress += 0x01000;       //0x01000 = 4096 = 4kb
  }
  
  //at the table to the directory
  page_directory[0] = (int)page_table;
  page_directory[0] |= 3; //Attributes: supervisor, read/write, present
  
  //fill the rest of the tables in the directory with non-present flags
  for([b]i = 1[/b]; i < 1024; i++)
  {
    page_directory[0] = 0 | 2; //Attributes: supervisor, read/write, not present
  }
  
  //
  //finally enable and start paging
  //
  
  //load the page directory pointer
  __asm__ __volatile__("mov %0, %%cr3":: "b"(page_directory));
  
  //now start it!
  enablePaging();
}
Every human has to do something idiot to prevent becoming a complete one
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: problems enabling paging

Post by qw »

assainator wrote:

Code: Select all

[global _enablePaging]
_enablePaging:
  mov eax, cr0
  or eax, 0x80000000
  mov cr0, eax
RET?
assainator
Member
Member
Posts: 30
Joined: Sun Jan 24, 2010 1:12 am
Location: The Netherlands

Re: problems enabling paging

Post by assainator »

Update _enablePaging but it still doesn't work......
Every human has to do something idiot to prevent becoming a complete one
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: problems enabling paging

Post by lemonyii »

Code: Select all

  //fill the rest of the tables in the directory with non-present flags
  for([b]i = 1[/b]; i < 1024; i++)
  {
    page_directory[0] = 0 | 2; //Attributes: supervisor, read/write, not present
  }
you changed the loop,but not "page_directory[0] " ,OK?
it should be "page_directory " ,right?

cheers!
and if you may,help me,see
http://forum.osdev.org/viewtopic.php?t=21811
thx
Enjoy my life!------A fish with a tattooed retina
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: problems enabling paging

Post by Combuster »

Even the compiler could have told you about this bug. If you want to save yourself some work, have the compiler enforce proper coding practices. (pass -Wall -Wextra -Werror -pedantic on the command line)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
assainator
Member
Member
Posts: 30
Joined: Sun Jan 24, 2010 1:12 am
Location: The Netherlands

Re: problems enabling paging

Post by assainator »

lemonyii wrote:

Code: Select all

  //fill the rest of the tables in the directory with non-present flags
  for([b]i = 1[/b]; i < 1024; i++)
  {
    page_directory[0] = 0 | 2; //Attributes: supervisor, read/write, not present
  }
you changed the loop,but not "page_directory[0] " ,OK?
it should be "page_directory " ,right?

cheers!
and if you may,help me,see
http://forum.osdev.org/viewtopic.php?t=21811
thx



Thanks, that fixed the problem.
And i have looked at your topic but i haven't touched the APIC yet.

assainator
Every human has to do something idiot to prevent becoming a complete one
Post Reply