Page 1 of 1

problems enabling paging

Posted: Wed Apr 07, 2010 1:06 pm
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 = .;
}

Re: problems enabling paging

Posted: Wed Apr 07, 2010 1:20 pm
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).

Re: problems enabling paging

Posted: Thu Apr 08, 2010 12:30 am
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

Re: problems enabling paging

Posted: Thu Apr 08, 2010 12:37 am
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.

Re: problems enabling paging

Posted: Thu Apr 08, 2010 2:25 am
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();
}

Re: problems enabling paging

Posted: Thu Apr 08, 2010 3:32 am
by qw
assainator wrote:

Code: Select all

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

Re: problems enabling paging

Posted: Thu Apr 08, 2010 4:27 am
by assainator
Update _enablePaging but it still doesn't work......

Re: problems enabling paging

Posted: Thu Apr 08, 2010 5:05 am
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

Re: problems enabling paging

Posted: Thu Apr 08, 2010 7:09 am
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)

Re: problems enabling paging

Posted: Thu Apr 08, 2010 8:42 am
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