OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 28, 2024 9:56 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: CR2 empty?
PostPosted: Wed Mar 23, 2005 12:11 pm 
I'm currently working on a pagefault handler. To get the virtual address called I read cr2 liike this:

in the pagefault main function:
Code:
uint v_addr = read_cr2();


read_cr2():
Code:
read_cr2:
   movl %cr2, %eax
   ret


The problem is that v_addr contains the value 0. Can't see what's wrong with it :(. Hope anyone finds the mistake.. :)

Thanks.


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Wed Mar 23, 2005 12:50 pm 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
have you triggered a pagefault prior to calling this function? if not, there is likely to be nothing or garbage in cr2.

I read cr2 directly after a page fault to save off the address causing the exception. One never knows what ideas can spring into a processor's mind. ];->

btw, if my knowledge of at&t syntax isn't too weak, I daresay your function is correct. Mine looks aequivalent, just that it's written in intel syntax.

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:CR2 empty?
PostPosted: Wed Mar 23, 2005 1:27 pm 
weird... i did another test and cr2 had a value... gotta look through the other test (it was from my malloc function), maybe it does something at address 0 (have no idea what) and otherwise it's just really weird. Thanks for the help. :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Wed Mar 23, 2005 1:51 pm 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
you can test this rather simple:

try to access some not mapped in address, say 0xdeadbeef. - send your cpu to hell on purpose, so to say.

Have the kernel trap into the page fault handler and there, read the cr2 value. If it shows the address you have tried to access - voila, your function works perfectly :-)

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 2:45 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
int x = (int*)0xcafebabe; still remains my favourite :P

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 7:22 am 
when i create via the normal routine (so not using a page fault, doesn't work yet) a pagetable, bind free space to it, create an integer with the virtual address of the page and fill it with a number it's always -1 ???. does anyone have a damn idea why this is happening, otherwise i'll post some code.

Thanks :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 7:55 am 
No, that will probably require a code snippet to figure out.


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 8:23 am 
:) ok, here's some code:

Code:
#define MEM_PAGEDIR 0xFFFFF000
#define MEM_PAGETABLE 0xFFC00000
#define cr3_reset() write_cr3(read_cr3())

uint mm_create_page_table(uint v_addr) {
   int *pagedir = (int *) MEM_PAGEDIR;
   int entry = v_addr >> 22;
   int i;
   
   int *pagetable = (int *) MEM_PAGETABLE;
   pagetable += entry * 1024;

   unsigned int page = mm_alloc_page();
   pagedir[entry] = page | 3;
   cr3_reset();
      
   for (i = 0; i < 1024; i++)
      pagetable[i] = 0 | 2;
         
   return page;
}

uint mm_page_bind(uint v_addr, char cpl, char wr) {
   uint pde = v_addr >> 22;
   uint pte = (v_addr >> 12) & 1023;
   uint page = mm_alloc_page();
   
   uint *pagedir = (uint *) MEM_PAGEDIR;
   uint *pagetable = (uint *) MEM_PAGETABLE;
   pagetable += pde * 1024;
   
   pagetable[pte] = page | (1 + (cpl << 2) + (wr << 1));
   return page;
}

// from init.c:
   mm_create_page_table(0xA0000000);
   mm_page_bind(0xA0000000, 1, 1);
   
   char *test = (char *) 0xA0001004;
   *test = 0xFF;

// when I print test it is -1, when i check it with an ' if ' it's also -1, so my printf function is ok :)


I didn't include mm_alloc_page(), this one contains no errors. If anyone could find the error, I would be really grateful :)

thanks :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 11:33 am 
My first guess: are you printing or if()'ing test as a number? It's a char, and your printf function says -1. Which is correct, since 0xFF is -1 in 2-complement notation.

(right..? I'm sorta feeling a bit lost, but iirc it was.) :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 11:38 am 
I'm testing it as a number :P. Char was first an int, but I wanted to see there was any difference (there wasn't).

I still don't have the error :-\.


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 11:53 am 
Now I'm somewhat confused to what error you're referring. You mean the -1? In that case, there is no error as far as I can see (afaics? :P ) but to be sure, write something else as 0xFF to 0xA0001004 and see what printf says.

About the CR2 which is sometimes 0, I'd think it's a dead pointer somewhere in your malloc(), and that your malloc runs in ring-0, causing eip to actually change to NULL, and crash since there is no code there, which in turn causes the pagefault.

Or something like that. HtH though :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Thu Mar 24, 2005 12:31 pm 
I've just dumped test2 as hex, and that says 0xFFFFFFFF. I checked it again with an 'if'. How is it possible a char contains the value 0xFFFFFFFF??? It doesn't matter I give test2 the value 0x10, 0x35 or 0xFF, it also doesn't matter I define it as int, short or as char. When I make test2 unsigned, the hex value is 0xFFFF. I'm really confused now.

Anyone? :)


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Fri Mar 25, 2005 1:19 am 
On the x86, char and short are scaled up to int anyway, it doesn't make any difference what type you use since GCC 32bit aligns it to speed up access.

If printf always writes -1 no matter what you change test to then you may have a problem. Try 0x7F, that should print 127 (The highest possible value of an signed byte). Also try send 0x7F to it directly and see what it prints in both cases, or you could just breakpoint the instruction and inspect the memory with Bochs.


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Fri Mar 25, 2005 2:58 am 
how can i inspect the memory with bochs exactly?


Top
  
 
 Post subject: Re:CR2 empty?
PostPosted: Fri Mar 25, 2005 3:09 am 
In the Bochs debugger, type "x 0xA0001004", you can place a breakpoint using "pb linearaddress"(IIRC) or you can just place __asm__ ("hlt"); in your code then Ctrl+C in the console to show the debugger prompt.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group