problems with getting eip in an exception

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
Poseidon

problems with getting eip in an exception

Post by Poseidon »

I'm currently working on an exception handler which gives some more info than only the name of the exception ;D. My only problem is that I can't get the eip value from the stack. This is what I have currently (I've tried loads of other ways before):

Code: Select all

   movl %esp, %ebp
   addl $0x8, %ebp
   movl (%ebp), %eax
   movl %eax, (eip) // eip is here no register, but a double word from my c code.
but this crashes my whole system. Anyone ideas?

Thanks.
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:problems with getting eip in an exception

Post by Pype.Clicker »

does this appear right in your ASM stub ?

maybe you'd like to ensure DS register has a valid value before you blindly mov things to memory :P
Poseidon

Re:problems with getting eip in an exception

Post by Poseidon »

it works now... i forgot to pop ebp from the stack :-[
i didn't really understand you pype. what value should ds have then?
AR

Re:problems with getting eip in an exception

Post by AR »

He's probably referring to making sure DS has a valid Kernel space segment so you aren't using the user space segment in the kernel (if that's even allowed).
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:problems with getting eip in an exception

Post by Pype.Clicker »

Poseidon wrote: it works now... i forgot to pop ebp from the stack :-[
hehe ... stack garbage ... the usual assembly error.

that being said, keep in mind that some exceptions (notably GPF) push an error code on the stack, and that some other don't.
i didn't really understand you pype. what value should ds have then?
AR got me right. If the exception comes from loading an invalid DS segment, or if it comes from userland, you'll be in trouble when trying to use DS. so it'd be wise to have something like

Code: Select all

exception__x:
    push ebp
    mov ebp, esp
    pushad  ;; just to make sure
    push ds
    push es
    mov ax, KERNEL_SEGMENT
    mov ds,ax
    mov es,ax
    __handle your exception here__
    pop es
    pop ds
    popad
    pop ebp
    __add esp,4 if some error code was pushed__
    iret

    
Post Reply