Saving the FPU state IA32
Posted: Fri May 10, 2013 8:01 am
Hi,
I would like to save the FPU state onto a context pointer in order to switch the context between two threads. I have the following context:
The total context size is 148 bytes.
My switch context method is below. I want to save the context onto "*o" and restore the previous saved context from "n".
I am getting a GP fault on this code. Any clues of what I am doing wrong? I am considering that fnsave pushes 108 bytes, as states the manual.
Thanks in advance,
Giovani
I would like to save the FPU state onto a context pointer in order to switch the context between two threads. I have the following context:
Code: Select all
struct _fpstate _fpu_ctx; //has 108 bytes
Reg32 _edi; //GP registers + eflags + eip = 40 bytes
Reg32 _esi;
Reg32 _ebp;
Reg32 _esp;
Reg32 _ebx;
Reg32 _edx;
Reg32 _ecx;
Reg32 _eax;
Reg32 _eflags;
Reg32 _eip;
My switch context method is below. I want to save the context onto "*o" and restore the previous saved context from "n".
Code: Select all
void IA32::switch_context(Context * volatile * o, Context * volatile n)
{
ASM(" pushfl \n"
" pushal \n" //the first two instructions pushes 40 bytes
" sub %esp, 108 \n"
" fnsave -108(%esp) \n"
" movl 148(%esp), %eax # old \n"
" movl %esp, (%eax) \n"
" movl 152(%esp), %esp # new \n"
" frstor (%esp) \n"
" add %esp, 108 \n"
" popal \n"
" popfl \n");
}
Thanks in advance,
Giovani