OSDev.org
https://forum.osdev.org/

CR2 empty?
https://forum.osdev.org/viewtopic.php?f=1&t=10347
Page 1 of 2

Author:  Poseidon [ Wed Mar 23, 2005 12:11 pm ]
Post subject:  CR2 empty?

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.

Author:  distantvoices [ Wed Mar 23, 2005 12:50 pm ]
Post subject:  Re:CR2 empty?

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.

Author:  Poseidon [ Wed Mar 23, 2005 1:27 pm ]
Post subject:  Re:CR2 empty?

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. :)

Author:  distantvoices [ Wed Mar 23, 2005 1:51 pm ]
Post subject:  Re:CR2 empty?

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 :-)

Author:  Pype.Clicker [ Thu Mar 24, 2005 2:45 am ]
Post subject:  Re:CR2 empty?

int x = (int*)0xcafebabe; still remains my favourite :P

Author:  Poseidon [ Thu Mar 24, 2005 7:22 am ]
Post subject:  Re:CR2 empty?

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 :)

Author:  AR [ Thu Mar 24, 2005 7:55 am ]
Post subject:  Re:CR2 empty?

No, that will probably require a code snippet to figure out.

Author:  Poseidon [ Thu Mar 24, 2005 8:23 am ]
Post subject:  Re:CR2 empty?

:) 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 :)

Author:  wacco [ Thu Mar 24, 2005 11:33 am ]
Post subject:  Re:CR2 empty?

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.) :)

Author:  Poseidon [ Thu Mar 24, 2005 11:38 am ]
Post subject:  Re:CR2 empty?

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 :-\.

Author:  wacco [ Thu Mar 24, 2005 11:53 am ]
Post subject:  Re:CR2 empty?

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 :)

Author:  Poseidon [ Thu Mar 24, 2005 12:31 pm ]
Post subject:  Re:CR2 empty?

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? :)

Author:  AR [ Fri Mar 25, 2005 1:19 am ]
Post subject:  Re:CR2 empty?

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.

Author:  Poseidon [ Fri Mar 25, 2005 2:58 am ]
Post subject:  Re:CR2 empty?

how can i inspect the memory with bochs exactly?

Author:  AR [ Fri Mar 25, 2005 3:09 am ]
Post subject:  Re:CR2 empty?

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.

Page 1 of 2 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/