Page 1 of 1

paging error

Posted: Sat Sep 06, 2008 7:43 am
by ahmedhalawa
I have written code for paging but that cann't be run! , pleas help?
pag.cpp

Code: Select all

#include "inc/page.h"
#include "inc/phmman.h"
#include "inc/video.h"
#include "inc/system.h"
/**********************/
dir_t *kdir=0,*cdir=0;
/**********************/
void load(uint *dir)
{   uint cr0;
    asm volatile("mov %0, %%cr3":: "r"(dir));
    asm volatile("mov %%cr0, %0": "=r"(cr0));
    cr0 |= 0x80000000;
    asm volatile("mov %0, %%cr0":: "r"(cr0));
}
void vmem::alloc(uint add,int pre,int rw,int us)
{
    video v;
    phmman phm;  // phiscal memory manger
    int page = add / 0x1000;
    int table= page/ 1024;
    if(kdir->tables[table].pre==1)
    {
        page_t *otable = (page_t *)kdir->tables[table].add;
        if(otable->pages[page].pre == 1)
        {return;
        }else{
            otable->pages[page].pre = pre;
            otable->pages[page].r_w = rw;
            otable->pages[page].u_s = us;
            otable->pages[page].add = add;
        }
    }else{
       page_t *ntable = (page_t *)phm.allocpg();
       kdir->tables[table].pre = pre;
       kdir->tables[table].r_w = rw;
       kdir->tables[table].u_s = us;
       kdir->tables[table].add = (uint)ntable;
       ntable->pages[page].pre = pre;
       ntable->pages[page].r_w = rw;
       ntable->pages[page].u_s = us;
       ntable->pages[page].add = add;
    }
}
 void vmem::install()
{
    video v;
    phmman phm;extern uint knl_end;
    kdir = (dir_t *)phm.allocpg();
    kdir = (dir_t *)phm.allocpg();

    for(int i=0;i<= knl_end;i += 4096)
    {
        alloc(i,1,1,0);
    }
    //v.puthex("",(int)kdir);
    load((uint*)kdir);
}

page.h

Code: Select all

#ifndef _H_virtual_memory_
#define _H_virtual_memory_
#include "phmem.h"
#include "system.h"

typedef struct page_entery
{
    uint pre:1;
    uint r_w:1;
    uint u_s:1;
    uint acc:1;
    uint dir:1;
    uint rev:7;
    uint add:20;
}page_e;
typedef struct page_table
{
    page_e pages[1024];
}page_t;
typedef struct dir_entry
{
    uint pre:1;
    uint r_w:1;
    uint u_s:1;
    uint acc:1;
    uint dir:1;
    uint rev:7;
    uint add:20;
}dir_e;
typedef struct dir_table
{
    dir_e tables[1024];
}dir_t;

class vmem
{
public:
      void install();
      void alloc(uint add,int pre,int rw,int us);
};
#endif

bochs error msg is

rip = 101dbd
cr0 = 0xe0000011 cr1=0x0 cr2=0xc0000022
cr3= 0x1efe000 cr4=0000000
>> add byte ptr ds:[eax+eax],cl:000c00
exception():3rd (13)exception with no resolution shutdown status is 00,reseting

Re: paging error

Posted: Sat Sep 06, 2008 9:17 am
by neon
What does knl_end contain? I just see it being used without being initialized :/

I would recommend stepping through your code and insuring the directory table is properly set up. Assuming you have an IDT set up, usually it would triple fault when the directory table entry is invalid.

All you need to do at the initil stage is identity map a single table for your kernel. This may help make things a little easier then focusing on everything at once.

Re: paging error

Posted: Sat Sep 06, 2008 10:08 am
by Troy Martin
neon, my guess is that knl_end is a pointer to the location of the end of his kernel.

Re: paging error

Posted: Sat Sep 06, 2008 10:49 am
by cr2
a few quick questions...

A. Why are you allocating pdir twice?
B. Where does phmman::allocpg() come from?
C. Why are you allocating up to knl_end and not 4K?
D. How did you map your page table to your page directory?
E. Why didn't you wipe the page table and your page directory before writing to it?

Re: paging error

Posted: Fri Sep 12, 2008 6:47 am
by ahmedhalawa
answer 1:
I allocating page dir twice beacuse my phiscal memory manger allocating pages from
end of memory so i think if allocat the last page which in end of memory the page
size willn't be 4096 byte .
answer 2:
phmman::allocpg() its belong to mey phiscal memory manger
answer 3:
beacuse most of tutorail say the every page entry have an address which point to
phsical mem page so i want to make my kernel run and every thing i'm going to run it
answer 4:
at frist look to my code
by putting the address of the table in one of page dir entry then make bit present =1
in this entry
answer 5:
:!: :!: :?: i can't stand this question pleas choose easy word

Re: paging error

Posted: Fri Sep 12, 2008 7:17 am
by neon
What dont you understand about the last question? If the page tables nor page directory is cleared, its PRESENT bit would be 0 indicating to the processor that these are not present so any access to them will #PF rather then #GPF. If they are NOT cleared, then who knows what they contain.
neon, my guess is that knl_end is a pointer to the location of the end of his kernel.
Of course. It still doesnt tell me exactally what it contains, however. The end of the kernel can be any value. I do not know if the OP defines the symbol in his linker map or finds it out another way.
answer 3:
beacuse most of tutorail say the every page entry have an address which point to
phsical mem page so i want to make my kernel run and every thing i'm going to run it
Just a nitpick: Not every page needs to be mapped, only pages marked PRESENT.

Re: paging error

Posted: Fri Sep 12, 2008 9:19 am
by cr2
ahmedhalawa wrote: answer 4:
at frist look to my code
by putting the address of the table in one of page dir entry then make bit present =1
in this entry
ever considered marking it READ/WRITE?

Re: paging error

Posted: Fri Sep 12, 2008 9:25 am
by cr2
Try mapping all of your memory space to the kernel's page directory.

Re: paging error

Posted: Sat Sep 13, 2008 9:20 am
by ahmedhalawa
cr2 i'll try
and i have marked it for r/w .