2
votes

I have written my own setjmp/longjmp which fits my needs, as shown below. I tested it on 32 bit systems and it works good. I save and restore registers eax, ebx, ecx, edi, esi, esp, ebp and eip for that purpose.

However, I know, this would not be enough for a 64 bit system. First of all, I think I need to replace registers eX with rX. Secondly, I think I need to save the 8 extra registers found in x86-64 bit, which are r8, r9, r10, r11, r12, r13, r14, r15. Would that be enough, or do I need to do more?

#define MY_SETJMP(n) __asm__ __volatile__ ("movl %eax, regax"#n";" \
     "movl %ebx, regbx"#n";" \
     "movl %ecx, regcx"#n";" \
     "movl %edi, regdi"#n";" \
     "movl %esi, regsi"#n";" \
     "movl %esp, regsp"#n";" \
     "movl %ebp, regbp"#n";" \
     "call next"#n";" \
     "next"#n": pop regip"#n";" \
     "addl $6, regip"#n";" \
     )

#define MY_LONGJMP(n) __asm__ __volatile__ ("movl regax"#n", %eax;" \
     "movl regbx"#n", %ebx;" \
     "movl regcx"#n", %ecx;" \
     "movl regdi"#n", %edi;" \
     "movl regsi"#n", %esi;" \
     "movl regsp"#n", %esp;" \
     "movl regbp"#n", %ebp;" \
     "jmp *regip"#n";" \
     )
2
Why did you write your own instead of using the working system one?Donal Fellows
Like I said, I am using it for a special purpose, which couldn't be done using the existing setjmpt/longjmp.MetallicPriest

2 Answers

7
votes

Your sequence of instruction does not save the flags register, which it probably should even in IA32. The wikipedia page contains the instructions pushf and popf that you can use to do so.

You need to save all the vector registers, unless you know that the program doesn't use them. Beware: they can also be used for scalar floating-point, so you do not need to have vectorized code in your program for them to be in use. Oh, and if the program uses floating-point, you should save the historical floating-point stack in case that is used. Dan Kruchinin's answer shows how to save all these in one step.

4
votes

Probably you need to save x87 context as well using fxsave/fxrestore instructions: http://siyobik.info/main/reference/instruction/FXSAVE

Though, I'm not sure fxsave/fxresrtore can be safely used from user-space application (i.e. outside of supervisor mode), but you can do almost everything fxsave does by yourself.