CR2 empty?

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.
Poseidon

CR2 empty?

Post by Poseidon »

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: Select all

uint v_addr = read_cr2();
read_cr2():

Code: Select all

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.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:CR2 empty?

Post by distantvoices »

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
Poseidon

Re:CR2 empty?

Post by Poseidon »

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. :)
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:CR2 empty?

Post by distantvoices »

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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:CR2 empty?

Post by Pype.Clicker »

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

Re:CR2 empty?

Post by Poseidon »

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

Re:CR2 empty?

Post by AR »

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

Re:CR2 empty?

Post by Poseidon »

:) ok, here's some code:

Code: Select all

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

Re:CR2 empty?

Post by wacco »

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

Re:CR2 empty?

Post by Poseidon »

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

Re:CR2 empty?

Post by wacco »

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

Re:CR2 empty?

Post by Poseidon »

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

Re:CR2 empty?

Post by AR »

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.
Poseidon

Re:CR2 empty?

Post by Poseidon »

how can i inspect the memory with bochs exactly?
AR

Re:CR2 empty?

Post by AR »

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.
Post Reply