Skip to contentSkip to conversation

Saving floating point registers inside ISR

Xenomai-help Henry Bausley 3 participants 3 messages Jun 4, 2008 – Jun 5, 2008

lore
Share & tools
lore
#0 lore raw

I am converting a 440EP power pc FSM Labs RT-Linux application to a Xenomai 2.4.2 posix application. Previously we saved the floating point registers in the ISR. I had the following code with RTLinux.

//----------------------------------------------------------------------------- int phase_intr(xnintr_t *cookie)
{
rthal_fpenv_t fpustate;

  rthal_save_fpu(&fpustate);

  phase();

rthal_restore_fpu(&fpustate);
return (XN_ISR_HANDLED);
}

with xenomai if we do the the same thing the system hangs.

However if we make the location where we save the registers global, or declare it static inside the ISR we don't have a problem. I don't want to do that since there are cases when the interrupt can be re-entered. Does anyone have any ideas?

rthal_fpenv_t fpustate;

//----------------------------------------------------------------------------- int phase_intr(xnintr_t *cookie)
{

  rthal_save_fpu(&fpustate);

  phase();

rthal_restore_fpu(&fpustate);
return (XN_ISR_HANDLED);
}

#1 lore raw
Quoted 8 lines · Henry Bausley

I am converting a 440EP power pc FSM Labs RT-Linux application to a Xenomai 2.4.2 posix application. Previously we saved the floating point registers in the ISR. I had the following code with RTLinux.

//----------------------------------------------------------------------------- int phase_intr(xnintr_t *cookie)
{
rthal_fpenv_t fpustate;

Do you know how large this structure is? And do you know how much stack space is still free when entering here, in the worst case? That may differ from RTLinux, probably running on some older kernel.

Quoted 9 lines
  rthal_save_fpu(&fpustate);

  phase();

rthal_restore_fpu(&fpustate);
return (XN_ISR_HANDLED);
}

Quoted 5 lines

with xenomai if we do the the same thing the system hangs.

However if we make the location where we save the registers global, or declare it static inside the ISR we don't have a problem. I don't want

That should ring the bell above...

Quoted 9 lines

to do that since there are cases when the interrupt can be re-entered. Does anyone have any ideas?

rthal_fpenv_t fpustate;

//----------------------------------------------------------------------------- int phase_intr(xnintr_t *cookie)
{

Quoted 8 lines
  rthal_save_fpu(&fpustate);

  phase();

rthal_restore_fpu(&fpustate);
return (XN_ISR_HANDLED);
}

If the stack size is not the problem here, compare to disassembly of the involved versions, local vs. global fpustate, and then the one from RTLinux.

Jan

#2 lore raw
In reply to Henry Bausley
Quoted 4 lines · Henry Bausley

I am converting a 440EP power pc FSM Labs RT-Linux application to a Xenomai 2.4.2 posix application. Previously we saved the floating point registers in the ISR. I had the following code with RTLinux.

Maybe what kills your box is an exception: when a user-space task has not used the FPU, or when a kernel-space task did not set the XNFPU bit, any use of floating point instructions triggers an exception. You should normally get a clear message in the kernel logs. So, you should try and call rthal_enable_fpu right before rthal_save_fpu. Note however that when doing so, you break Xenomai and Linux FPU "state machine". So, what you should really do is save the exception bit, enable it before calling rthal_save_fpu, then restore it after rthal_restore_fpu.

You can use for instance

#define rthal_save_fp_bit() ({                           \
    register unsigned long _msr;                        \
    __asm__ __volatile__ ( "mfmsr %0" : "=r"(_msr) );   \
    __asm__ __volatile__ ( "mtmsrd %0"                  \
                           : /* no output */            \
                           : "r"(_msr | MSR_FP)         \
                           : "memory" );                \
    _msr & MSR_FP;
})
#define rthal_restore_fp_bit(msr)  ({ \
    register unsigned long _msr;                        \
    __asm__ __volatile__ ( "mfmsr %0" : "=r"(_msr) );   \
    __asm__ __volatile__ ( "mtmsrd %0"                  \
                           : /* no output */            \
                           : "r"((_msr & ~MSR_FP) | (msr))         \
                           : "memory" );                \
})

Then you would do:

int phase_intr(xnintr_t *cookie)
{
unsigned long fp_bit;
rthal_fpenv_t fpustate;

fp_bit = rthal_save_fp_bit();
rthal_save_fpu(&fpustate);

phase();

rthal_restore_fpu(&fpustate);
rthal_restore_fp_bit(fp_bit);

return (XN_ISR_HANDLED);
}

Signature · 1 line
 Gilles