blob: 5437320a300af6bbd7e92a1c1d9c428a85d5d898 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Paul Bakkerf2561b32014-02-06 15:11:55 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010032#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C)
33#include "polarssl/platform.h"
34#else
35#include <stdio.h>
36#define polarssl_printf printf
37#endif
38
Paul Bakkerf2561b32014-02-06 15:11:55 +010039#if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000044
45#include <windows.h>
46#include <winbase.h>
47
48struct _hr_time
49{
50 LARGE_INTEGER start;
51};
52
53#else
54
55#include <unistd.h>
56#include <sys/types.h>
57#include <sys/time.h>
58#include <signal.h>
59#include <time.h>
60
61struct _hr_time
62{
63 struct timeval start;
64};
65
Paul Bakker9af723c2014-05-01 13:03:14 +020066#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakkerbb0139c2012-10-31 09:53:08 +000068#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +010069 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkerbb0139c2012-10-31 09:53:08 +000071#define POLARSSL_HAVE_HARDCLOCK
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073unsigned long hardclock( void )
74{
75 unsigned long tsc;
76 __asm rdtsc
77 __asm mov [tsc], eax
78 return( tsc );
79}
Paul Bakker9af723c2014-05-01 13:03:14 +020080#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
81 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakkerbb0139c2012-10-31 09:53:08 +000083#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
84 defined(__GNUC__) && defined(__i386__)
85
86#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88unsigned long hardclock( void )
89{
Paul Bakkerca410102011-10-19 14:27:36 +000090 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010091 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000092 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000093}
Paul Bakker9af723c2014-05-01 13:03:14 +020094#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
95 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakkerbb0139c2012-10-31 09:53:08 +000097#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
98 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
99
100#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102unsigned long hardclock( void )
103{
104 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100105 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 return( lo | (hi << 32) );
107}
Paul Bakker9af723c2014-05-01 13:03:14 +0200108#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
109 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000111#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
112 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
113
114#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116unsigned long hardclock( void )
117{
118 unsigned long tbl, tbu0, tbu1;
119
120 do
121 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100122 asm volatile( "mftbu %0" : "=r" (tbu0) );
123 asm volatile( "mftb %0" : "=r" (tbl ) );
124 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 }
126 while( tbu0 != tbu1 );
127
128 return( tbl );
129}
Paul Bakker9af723c2014-05-01 13:03:14 +0200130#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
131 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000133#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
134 defined(__GNUC__) && defined(__sparc64__)
135
136#if defined(__OpenBSD__)
137#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000138#else
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000139#define POLARSSL_HAVE_HARDCLOCK
140
141unsigned long hardclock( void )
142{
143 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100144 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000145 return( tick );
146}
Paul Bakker9af723c2014-05-01 13:03:14 +0200147#endif /* __OpenBSD__ */
148#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
149 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000150
151#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
152 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
153
154#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156unsigned long hardclock( void )
157{
158 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100159 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
160 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 return( tick );
162}
Paul Bakker9af723c2014-05-01 13:03:14 +0200163#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
164 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000166#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
167 defined(__GNUC__) && defined(__alpha__)
168
169#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171unsigned long hardclock( void )
172{
173 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100174 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 return( cc & 0xFFFFFFFF );
176}
Paul Bakker9af723c2014-05-01 13:03:14 +0200177#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
178 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000180#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
181 defined(__GNUC__) && defined(__ia64__)
182
183#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185unsigned long hardclock( void )
186{
187 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100188 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 return( itc );
190}
Paul Bakker9af723c2014-05-01 13:03:14 +0200191#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
192 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100194#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
195 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000196
197#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000198
199unsigned long hardclock( void )
200{
201 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100202
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100203 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000204
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200205 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000206}
Paul Bakker9af723c2014-05-01 13:03:14 +0200207#endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000208
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000209#if !defined(POLARSSL_HAVE_HARDCLOCK)
210
211#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213static int hardclock_init = 0;
214static struct timeval tv_init;
215
216unsigned long hardclock( void )
217{
218 struct timeval tv_cur;
219
220 if( hardclock_init == 0 )
221 {
222 gettimeofday( &tv_init, NULL );
223 hardclock_init = 1;
224 }
225
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 ) );
229}
Paul Bakker9af723c2014-05-01 13:03:14 +0200230#endif /* !POLARSSL_HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakker2eee9022011-04-24 15:28:55 +0000232volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100234#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236unsigned long get_timer( struct hr_time *val, int reset )
237{
238 unsigned long delta;
239 LARGE_INTEGER offset, hfreq;
240 struct _hr_time *t = (struct _hr_time *) val;
241
242 QueryPerformanceCounter( &offset );
243 QueryPerformanceFrequency( &hfreq );
244
245 delta = (unsigned long)( ( 1000 *
246 ( offset.QuadPart - t->start.QuadPart ) ) /
247 hfreq.QuadPart );
248
249 if( reset )
250 QueryPerformanceCounter( &t->start );
251
252 return( delta );
253}
254
255DWORD WINAPI TimerProc( LPVOID uElapse )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100256{
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 Sleep( (DWORD) uElapse );
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100258 alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 return( TRUE );
260}
261
262void set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100263{
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 DWORD ThreadId;
265
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100266 alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 CloseHandle( CreateThread( NULL, 0, TimerProc,
268 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
269}
270
271void m_sleep( int milliseconds )
272{
273 Sleep( milliseconds );
274}
275
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100276#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278unsigned long get_timer( struct hr_time *val, int reset )
279{
280 unsigned long delta;
281 struct timeval offset;
282 struct _hr_time *t = (struct _hr_time *) val;
283
284 gettimeofday( &offset, NULL );
285
286 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
287 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
288
289 if( reset )
290 {
291 t->start.tv_sec = offset.tv_sec;
292 t->start.tv_usec = offset.tv_usec;
293 }
294
295 return( delta );
296}
297
Paul Bakker49d75672012-09-26 15:22:07 +0000298#if defined(INTEGRITY)
299void m_sleep( int milliseconds )
300{
301 usleep( milliseconds * 1000 );
302}
303
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100304#else /* INTEGRITY */
Paul Bakker49d75672012-09-26 15:22:07 +0000305
Paul Bakker5121ce52009-01-03 21:22:43 +0000306static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100307{
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 alarmed = 1;
309 signal( signum, sighandler );
310}
311
312void set_alarm( int seconds )
313{
314 alarmed = 0;
315 signal( SIGALRM, sighandler );
316 alarm( seconds );
317}
318
319void m_sleep( int milliseconds )
320{
321 struct timeval tv;
322
323 tv.tv_sec = milliseconds / 1000;
Manuel Pégourié-Gonnarddfbf9c72014-02-20 22:16:43 +0100324 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 select( 0, NULL, NULL, NULL, &tv );
327}
Paul Bakker49d75672012-09-26 15:22:07 +0000328#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100330#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100332#if defined(POLARSSL_SELF_TEST)
333
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200334/* To test net_usleep against our functions */
335#if defined(POLARSSL_NET_C)
336#include "polarssl/net.h"
337#endif
338
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100339/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200340 * Busy-waits for the given number of milliseconds.
341 * Used for testing hardclock.
342 */
343static void busy_msleep( unsigned long msec )
344{
345 struct hr_time hires;
346 unsigned long i = 0; /* for busy-waiting */
347 volatile unsigned long j; /* to prevent optimisation */
348
349 (void) get_timer( &hires, 1 );
350
351 while( get_timer( &hires, 0 ) < msec )
352 i++;
353
354 j = i;
355 (void) j;
356}
357
358/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100359 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200360 *
361 * Warning: this is work in progress, some tests may not be reliable enough
362 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100363 */
364int timing_self_test( int verbose )
365{
366 unsigned long cycles, ratio;
367 unsigned long millisecs, secs;
368 int hardfail;
369 struct hr_time hires;
370
371 if( verbose != 0)
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200372 polarssl_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100373
374 if( verbose != 0 )
375 polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
376
377 for( secs = 1; secs <= 3; secs++ )
378 {
379 (void) get_timer( &hires, 1 );
380
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200381 m_sleep( 500 * secs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100382
383 millisecs = get_timer( &hires, 0 );
384
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200385 if( millisecs < 450 * secs || millisecs > 550 * secs )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100386 {
387 if( verbose != 0 )
388 polarssl_printf( "failed\n" );
389
390 return( 1 );
391 }
392 }
393
394 if( verbose != 0 )
395 polarssl_printf( "passed\n" );
396
397 if( verbose != 0 )
398 polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
399
400 for( secs = 1; secs <= 3; secs++ )
401 {
402 (void) get_timer( &hires, 1 );
403
404 set_alarm( secs );
405 while( !alarmed )
406 ;
407
408 millisecs = get_timer( &hires, 0 );
409
410 if( millisecs < 900 * secs || millisecs > 1100 * secs )
411 {
412 if( verbose != 0 )
413 polarssl_printf( "failed\n" );
414
415 return( 1 );
416 }
417 }
418
419 if( verbose != 0 )
420 polarssl_printf( "passed\n" );
421
422 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200423 polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424
425 /*
426 * Allow one failure for possible counter wrapping.
427 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
428 * since the whole test is about 10ms, it shouldn't happen twice in a row.
429 */
430 hardfail = 0;
431
432hard_test:
433 if( hardfail > 1 )
434 {
435 if( verbose != 0 )
436 polarssl_printf( "failed\n" );
437
438 return( 1 );
439 }
440
441 /* Get a reference ratio cycles/ms */
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200442 millisecs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100443 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200444 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100445 cycles = hardclock() - cycles;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200446 ratio = cycles / millisecs;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100447
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200448 /* Check that the ratio is mostly constant */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100449 for( millisecs = 2; millisecs <= 4; millisecs++ )
450 {
451 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200452 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100453 cycles = hardclock() - cycles;
454
455 /* Allow variation up to 20% */
456 if( cycles / millisecs < ratio - ratio / 5 ||
457 cycles / millisecs > ratio + ratio / 5 )
458 {
459 hardfail++;
460 goto hard_test;
461 }
462 }
463
464 if( verbose != 0 )
465 polarssl_printf( "passed\n" );
466
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200467#if defined(POLARSSL_NET_C)
468 if( verbose != 0 )
469 polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
470
471 for( secs = 1; secs <= 3; secs++ )
472 {
473 (void) get_timer( &hires, 1 );
474
475 net_usleep( 500000 * secs );
476
477 millisecs = get_timer( &hires, 0 );
478
479 if( millisecs < 450 * secs || millisecs > 550 * secs )
480 {
481 if( verbose != 0 )
482 polarssl_printf( "failed\n" );
483
484 return( 1 );
485 }
486 }
487
488 if( verbose != 0 )
489 polarssl_printf( "passed\n" );
Paul Bakker9af723c2014-05-01 13:03:14 +0200490#endif /* POLARSSL_NET_C */
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200491
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200492 if( verbose != 0 )
493 polarssl_printf( "\n" );
494
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100495 return( 0 );
496}
497
498#endif /* POLARSSL_SELF_TEST */
499
500#endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */