lkml.org 
[lkml]   [2008]   [May]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectBug in arch/i386/lib/delay.c file, delay_loop function
Hi all,

when trying to understand how Bogomips are implemented I have found
bug in arch/i386/lib/delay.c file, delay_loop function

/* simple loop based delay: */
static void delay_loop(unsigned long loops)
{
int d0;

__asm__ __volatile__(
"\tjmp 1f\n"
".align 16\n"
"1:\tjmp 2f\n"
".align 16\n"
"2:\tdecl %0\n\tjns 2b"
:"=&a" (d0)
:"0" (loops));
}


The function fails for loops > 2^31+1. It because SF is set when dec
returns numbers > 2^31

The fix is to use jnz instruction instead of jns (and add one decl
instruction to the end to have exactly the same number of loops as in
original version):

__asm__ __volatile__(
"\tjmp 1f\n"
".align 16\n"
"1:\tjmp 2f\n"
".align 16\n"
"2:\tdecl %0\n\tjnz 2b\n"
"decl %0"
:"=&a" (d0)
:"0" (loops));

IMHO, d0 is not needed at all so that we can further simplify the code:
static void delay_loop(unsigned long loops)
__asm__ __volatile__(
"\tjmp 1f\n"
".align 16\n"
"1:\tjmp 2f\n"
".align 16\n"
"2:\tdecl %0\n\tjnz 2b\n"
"decl %0"
:/*we don't need output */
:"a" (loops));
}

I will attach three small C-program to test it
delay-orig.c - original loop from kernel source code
delay-fixed.c - fixed loop
delay-fixed1.c - fixed loop without d0 variable

Outputs:
============== delay-orig.c ==================
time delay-orig 2147483649
loops 2147483649
loops 2147483649
do -2147483648

real 0m0.002s
user 0m0.000s
sys 0m0.000s

================== delay-fixed.c =============
time delay-fixed 2147483649
loops 2147483649
loops 2147483649
do -1

real 0m1.025s
user 0m1.024s
sys 0m0.000s

========== delay-fixed1.c =====================
time delay-fixed1 2147483649
loops 2147483649
loops 2147483649

real 0m1.073s
user 0m1.060s
sys 0m0.004s


and update kernel source file arch/i386/lib/delay.c. Please let me
know if these modifications make sense.

Thanks a lot
Jiri
[unhandled content-type:application/x-bzip2]
\
 
 \ /
  Last update: 2008-05-30 17:19    [W:0.274 / U:0.024 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site