26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C)
36 #define polarssl_printf printf
39 #if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
43 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
56 #include <sys/types.h>
68 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
69 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
71 #define POLARSSL_HAVE_HARDCLOCK
83 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
84 defined(__GNUC__) && defined(__i386__)
86 #define POLARSSL_HAVE_HARDCLOCK
91 asm volatile(
"rdtsc" :
"=a" (lo),
"=d" (hi) );
97 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
98 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
100 #define POLARSSL_HAVE_HARDCLOCK
104 unsigned long lo, hi;
105 asm volatile(
"rdtsc" :
"=a" (lo),
"=d" (hi) );
106 return( lo | ( hi << 32 ) );
111 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
112 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
114 #define POLARSSL_HAVE_HARDCLOCK
118 unsigned long tbl, tbu0, tbu1;
122 asm volatile(
"mftbu %0" :
"=r" (tbu0) );
123 asm volatile(
"mftb %0" :
"=r" (tbl ) );
124 asm volatile(
"mftbu %0" :
"=r" (tbu1) );
126 while( tbu0 != tbu1 );
133 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
134 defined(__GNUC__) && defined(__sparc64__)
136 #if defined(__OpenBSD__)
137 #warning OpenBSD does not allow access to tick register using software version instead
139 #define POLARSSL_HAVE_HARDCLOCK
144 asm volatile(
"rdpr %%tick, %0;" :
"=&r" (tick) );
151 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
152 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
154 #define POLARSSL_HAVE_HARDCLOCK
159 asm volatile(
".byte 0x83, 0x41, 0x00, 0x00" );
160 asm volatile(
"mov %%g1, %0" :
"=r" (tick) );
166 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
167 defined(__GNUC__) && defined(__alpha__)
169 #define POLARSSL_HAVE_HARDCLOCK
174 asm volatile(
"rpcc %0" :
"=r" (cc) );
175 return( cc & 0xFFFFFFFF );
180 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
181 defined(__GNUC__) && defined(__ia64__)
183 #define POLARSSL_HAVE_HARDCLOCK
188 asm volatile(
"mov %0 = ar.itc" :
"=r" (itc) );
194 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
195 !defined(EFIX64) && !defined(EFI32)
197 #define POLARSSL_HAVE_HARDCLOCK
201 LARGE_INTEGER offset;
203 QueryPerformanceCounter( &offset );
205 return( (
unsigned long)( offset.QuadPart ) );
209 #if !defined(POLARSSL_HAVE_HARDCLOCK)
211 #define POLARSSL_HAVE_HARDCLOCK
213 static int hardclock_init = 0;
214 static struct timeval tv_init;
218 struct timeval tv_cur;
220 if( hardclock_init == 0 )
222 gettimeofday( &tv_init, NULL );
226 gettimeofday( &tv_cur, NULL );
227 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
228 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
234 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
239 LARGE_INTEGER offset, hfreq;
240 struct _hr_time *t = (
struct _hr_time *) val;
242 QueryPerformanceCounter( &offset );
243 QueryPerformanceFrequency( &hfreq );
245 delta = (
unsigned long)( ( 1000 *
246 ( offset.QuadPart - t->start.QuadPart ) ) /
250 QueryPerformanceCounter( &t->start );
255 DWORD WINAPI TimerProc( LPVOID uElapse )
257 Sleep( (DWORD) uElapse );
267 CloseHandle( CreateThread( NULL, 0, TimerProc,
268 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
271 void m_sleep(
int milliseconds )
273 Sleep( milliseconds );
281 struct timeval offset;
282 struct _hr_time *t = (
struct _hr_time *) val;
284 gettimeofday( &offset, NULL );
288 t->start.tv_sec = offset.tv_sec;
289 t->start.tv_usec = offset.tv_usec;
293 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
294 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
299 #if defined(INTEGRITY)
300 void m_sleep(
int milliseconds )
302 usleep( milliseconds * 1000 );
307 static void sighandler(
int signum )
310 signal( signum, sighandler );
316 signal( SIGALRM, sighandler );
320 void m_sleep(
int milliseconds )
324 tv.tv_sec = milliseconds / 1000;
325 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
327 select( 0, NULL, NULL, NULL, &tv );
333 #if defined(POLARSSL_SELF_TEST)
336 #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
344 static void busy_msleep(
unsigned long msec )
348 volatile unsigned long j;
367 unsigned long cycles, ratio;
368 unsigned long millisecs, secs;
378 for( secs = 1; secs <= 3; secs++ )
382 m_sleep( (
int)( 500 * secs ) );
386 if( millisecs < 450 * secs || millisecs > 550 * secs )
401 for( secs = 1; secs <= 3; secs++ )
411 if( millisecs < 900 * secs || millisecs > 1100 * secs )
445 busy_msleep( millisecs );
447 ratio = cycles / millisecs;
450 for( millisecs = 2; millisecs <= 4; millisecs++ )
453 busy_msleep( millisecs );
457 if( cycles / millisecs < ratio - ratio / 5 ||
458 cycles / millisecs > ratio + ratio / 5 )
468 #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
472 for( secs = 1; secs <= 3; secs++ )
480 if( millisecs < 450 * secs || millisecs > 550 * secs )