[U-Boot] [PATCH] powerpc/lib: fix unsafe register handling in wait_ticks

Joakim Tjernlund joakim.tjernlund at transmode.se
Mon Jan 28 14:27:41 CET 2013


> 
> On 01/27/2013 06:03 PM, Mats Kärrman wrote:
> > If watchdog is enabled, the arch/powerpc/lib/ticks.S::wait_ticks() 
function
> > calls the function specified by the WATCHDOG_RESET macro.
> > The wait_ticks function depends on the registers r0, r6 and r7 being
> > preserved however that is not guaranteed, e.g. if the reset function 
is a
> > C function this will probably overwrite r0 and cause an endless loop.
> > 
> > The following patch changes to using r14+r15 instead of r6+r7 (to 
resemble
> > what would have been generated by a C compiler) and saves all 
necessary
> > registers on the stack.
> > 
> > The patch has been tested on a custom MPC5125 based machine using the 
512x
> > powerpc architecture.
> > 
> > Signed-off-by: Mats Karrman <mats.karrman at tritech.se>
> > Cc: Wolfgang Denk <wd at denx.de>
> > Acked-by: Joakim Tjernlund <joakim.tjernlund at transmode.se>
> 
> Thanks for this fix. I just reproduced this problem on another MPC5200
> based board (a4m2k) while implementing watchdog support. And this patch
> fixes the problem (hangup in wait_ticks).
> 
> So:
> 
> Tested-by: Stefan Roese <sr at denx.de>

If someone wants to play with a C only impl. I THINK
it this is a good starting point:

#define WATCHDOG_RESET

/*static inline */unsigned long long get_ticks(void)
{
        union xx {
                unsigned long long r3_r4;
                struct yx {
                        unsigned long r3;
                        unsigned long r4;
                } xy;
        } res;
        register unsigned long tmp;

        do {
                asm ("mftbu %0\n\t"
                     "mftb %1\n\t"
                     "mftbu %2"
                     : "=r"(res.xy.r3), "=r"(res.xy.r4), "=r"(tmp));
        } while (res.xy.r3 != tmp);
        return res.r3_r4;
}

wait_ticks(unsigned long wt)
{
        unsigned long long ticks_end;

        ticks_end = get_ticks() + wt;
        WATCHDOG_RESET;
        do {

        } while((signed long long)ticks_end - (signed long 
long)get_ticks() >= 0);
}


More information about the U-Boot mailing list