paging error

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
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

paging error

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: paging error

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: paging error

Post by Troy Martin »

neon, my guess is that knl_end is a pointer to the location of the end of his kernel.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: paging error

Post 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?
OS-LUX V0.0
Working on...
Memory management: the Pool
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

Re: paging error

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: paging error

Post 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.
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: paging error

Post 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?
OS-LUX V0.0
Working on...
Memory management: the Pool
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: paging error

Post by cr2 »

Try mapping all of your memory space to the kernel's page directory.
OS-LUX V0.0
Working on...
Memory management: the Pool
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

Re: paging error

Post by ahmedhalawa »

cr2 i'll try
and i have marked it for r/w .
Post Reply