blob: fe1daa247f596d6b49cd6b101be6337166c201dc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010029#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#include <stdio.h>
33#define polarssl_printf printf
34#endif
35
Paul Bakkerf2561b32014-02-06 15:11:55 +010036#if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakkerfa6a6202013-10-28 18:48:30 +010040#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#include <windows.h>
43#include <winbase.h>
44
45struct _hr_time
46{
47 LARGE_INTEGER start;
48};
49
50#else
51
52#include <unistd.h>
53#include <sys/types.h>
54#include <sys/time.h>
55#include <signal.h>
56#include <time.h>
57
58struct _hr_time
59{
60 struct timeval start;
61};
62
Paul Bakker9af723c2014-05-01 13:03:14 +020063#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Paul Bakkerbb0139c2012-10-31 09:53:08 +000065#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020066 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakkerbb0139c2012-10-31 09:53:08 +000068#define POLARSSL_HAVE_HARDCLOCK
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070unsigned long hardclock( void )
71{
72 unsigned long tsc;
73 __asm rdtsc
74 __asm mov [tsc], eax
75 return( tsc );
76}
Paul Bakker9af723c2014-05-01 13:03:14 +020077#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
78 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Paul Bakkerbb0139c2012-10-31 09:53:08 +000080#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
81 defined(__GNUC__) && defined(__i386__)
82
83#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85unsigned long hardclock( void )
86{
Paul Bakkerca410102011-10-19 14:27:36 +000087 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010088 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000089 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
Paul Bakker9af723c2014-05-01 13:03:14 +020091#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
92 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakkerbb0139c2012-10-31 09:53:08 +000094#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020095 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000096
97#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99unsigned long hardclock( void )
100{
101 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100102 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200103 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
Paul Bakker9af723c2014-05-01 13:03:14 +0200105#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
106 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000108#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200109 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000110
111#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113unsigned long hardclock( void )
114{
115 unsigned long tbl, tbu0, tbu1;
116
117 do
118 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100119 asm volatile( "mftbu %0" : "=r" (tbu0) );
120 asm volatile( "mftb %0" : "=r" (tbl ) );
121 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 }
123 while( tbu0 != tbu1 );
124
125 return( tbl );
126}
Paul Bakker9af723c2014-05-01 13:03:14 +0200127#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
128 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000130#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
131 defined(__GNUC__) && defined(__sparc64__)
132
133#if defined(__OpenBSD__)
134#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000135#else
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000136#define POLARSSL_HAVE_HARDCLOCK
137
138unsigned long hardclock( void )
139{
140 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100141 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000142 return( tick );
143}
Paul Bakker9af723c2014-05-01 13:03:14 +0200144#endif /* __OpenBSD__ */
145#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
146 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000147
148#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
149 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
150
151#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153unsigned long hardclock( void )
154{
155 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100156 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
157 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 return( tick );
159}
Paul Bakker9af723c2014-05-01 13:03:14 +0200160#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
161 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000163#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
164 defined(__GNUC__) && defined(__alpha__)
165
166#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168unsigned long hardclock( void )
169{
170 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100171 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 return( cc & 0xFFFFFFFF );
173}
Paul Bakker9af723c2014-05-01 13:03:14 +0200174#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
175 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000177#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
178 defined(__GNUC__) && defined(__ia64__)
179
180#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182unsigned long hardclock( void )
183{
184 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100185 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 return( itc );
187}
Paul Bakker9af723c2014-05-01 13:03:14 +0200188#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
189 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100191#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
192 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000193
194#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000195
196unsigned long hardclock( void )
197{
198 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100199
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100200 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000201
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200202 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000203}
Paul Bakker9af723c2014-05-01 13:03:14 +0200204#endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000205
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000206#if !defined(POLARSSL_HAVE_HARDCLOCK)
207
208#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210static int hardclock_init = 0;
211static struct timeval tv_init;
212
213unsigned long hardclock( void )
214{
215 struct timeval tv_cur;
216
217 if( hardclock_init == 0 )
218 {
219 gettimeofday( &tv_init, NULL );
220 hardclock_init = 1;
221 }
222
223 gettimeofday( &tv_cur, NULL );
224 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
225 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
226}
Paul Bakker9af723c2014-05-01 13:03:14 +0200227#endif /* !POLARSSL_HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Paul Bakker2eee9022011-04-24 15:28:55 +0000229volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100231#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233unsigned long get_timer( struct hr_time *val, int reset )
234{
235 unsigned long delta;
236 LARGE_INTEGER offset, hfreq;
237 struct _hr_time *t = (struct _hr_time *) val;
238
239 QueryPerformanceCounter( &offset );
240 QueryPerformanceFrequency( &hfreq );
241
242 delta = (unsigned long)( ( 1000 *
243 ( offset.QuadPart - t->start.QuadPart ) ) /
244 hfreq.QuadPart );
245
246 if( reset )
247 QueryPerformanceCounter( &t->start );
248
249 return( delta );
250}
251
252DWORD WINAPI TimerProc( LPVOID uElapse )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100253{
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 Sleep( (DWORD) uElapse );
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100255 alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 return( TRUE );
257}
258
259void set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100260{
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 DWORD ThreadId;
262
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100263 alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 CloseHandle( CreateThread( NULL, 0, TimerProc,
265 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
266}
267
268void m_sleep( int milliseconds )
269{
270 Sleep( milliseconds );
271}
272
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100273#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275unsigned long get_timer( struct hr_time *val, int reset )
276{
277 unsigned long delta;
278 struct timeval offset;
279 struct _hr_time *t = (struct _hr_time *) val;
280
281 gettimeofday( &offset, NULL );
282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 if( reset )
284 {
285 t->start.tv_sec = offset.tv_sec;
286 t->start.tv_usec = offset.tv_usec;
Alfred Klompb308dd72014-07-14 22:32:21 +0200287 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 }
289
Alfred Klompb308dd72014-07-14 22:32:21 +0200290 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
291 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
292
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 return( delta );
294}
295
Paul Bakker49d75672012-09-26 15:22:07 +0000296#if defined(INTEGRITY)
297void m_sleep( int milliseconds )
298{
299 usleep( milliseconds * 1000 );
300}
301
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100302#else /* INTEGRITY */
Paul Bakker49d75672012-09-26 15:22:07 +0000303
Paul Bakker5121ce52009-01-03 21:22:43 +0000304static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100305{
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 alarmed = 1;
307 signal( signum, sighandler );
308}
309
310void set_alarm( int seconds )
311{
312 alarmed = 0;
313 signal( SIGALRM, sighandler );
314 alarm( seconds );
315}
316
317void m_sleep( int milliseconds )
318{
319 struct timeval tv;
320
321 tv.tv_sec = milliseconds / 1000;
Manuel Pégourié-Gonnarddfbf9c72014-02-20 22:16:43 +0100322 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324 select( 0, NULL, NULL, NULL, &tv );
325}
Paul Bakker49d75672012-09-26 15:22:07 +0000326#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100328#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100330#if defined(POLARSSL_SELF_TEST)
331
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200332/* To test net_usleep against our functions */
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200333#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200334#include "polarssl/net.h"
335#endif
336
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100337/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200338 * Busy-waits for the given number of milliseconds.
339 * Used for testing hardclock.
340 */
341static void busy_msleep( unsigned long msec )
342{
343 struct hr_time hires;
344 unsigned long i = 0; /* for busy-waiting */
345 volatile unsigned long j; /* to prevent optimisation */
346
347 (void) get_timer( &hires, 1 );
348
349 while( get_timer( &hires, 0 ) < msec )
350 i++;
351
352 j = i;
353 (void) j;
354}
355
356/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100357 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200358 *
359 * Warning: this is work in progress, some tests may not be reliable enough
360 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100361 */
362int timing_self_test( int verbose )
363{
364 unsigned long cycles, ratio;
365 unsigned long millisecs, secs;
366 int hardfail;
367 struct hr_time hires;
368
Paul Bakker66d5d072014-06-17 16:39:18 +0200369 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200370 polarssl_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100371
372 if( verbose != 0 )
373 polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
374
375 for( secs = 1; secs <= 3; secs++ )
376 {
377 (void) get_timer( &hires, 1 );
378
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200379 m_sleep( (int)( 500 * secs ) );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100380
381 millisecs = get_timer( &hires, 0 );
382
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200383 if( millisecs < 450 * secs || millisecs > 550 * secs )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100384 {
385 if( verbose != 0 )
386 polarssl_printf( "failed\n" );
387
388 return( 1 );
389 }
390 }
391
392 if( verbose != 0 )
393 polarssl_printf( "passed\n" );
394
395 if( verbose != 0 )
396 polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
397
398 for( secs = 1; secs <= 3; secs++ )
399 {
400 (void) get_timer( &hires, 1 );
401
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200402 set_alarm( (int) secs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100403 while( !alarmed )
404 ;
405
406 millisecs = get_timer( &hires, 0 );
407
408 if( millisecs < 900 * secs || millisecs > 1100 * secs )
409 {
410 if( verbose != 0 )
411 polarssl_printf( "failed\n" );
412
413 return( 1 );
414 }
415 }
416
417 if( verbose != 0 )
418 polarssl_printf( "passed\n" );
419
420 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200421 polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100422
423 /*
424 * Allow one failure for possible counter wrapping.
425 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
426 * since the whole test is about 10ms, it shouldn't happen twice in a row.
427 */
428 hardfail = 0;
429
430hard_test:
431 if( hardfail > 1 )
432 {
433 if( verbose != 0 )
434 polarssl_printf( "failed\n" );
435
436 return( 1 );
437 }
438
439 /* Get a reference ratio cycles/ms */
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200440 millisecs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100441 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200442 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100443 cycles = hardclock() - cycles;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200444 ratio = cycles / millisecs;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100445
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200446 /* Check that the ratio is mostly constant */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100447 for( millisecs = 2; millisecs <= 4; millisecs++ )
448 {
449 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200450 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100451 cycles = hardclock() - cycles;
452
453 /* Allow variation up to 20% */
454 if( cycles / millisecs < ratio - ratio / 5 ||
455 cycles / millisecs > ratio + ratio / 5 )
456 {
457 hardfail++;
458 goto hard_test;
459 }
460 }
461
462 if( verbose != 0 )
463 polarssl_printf( "passed\n" );
464
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200465#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200466 if( verbose != 0 )
467 polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
468
469 for( secs = 1; secs <= 3; secs++ )
470 {
471 (void) get_timer( &hires, 1 );
472
473 net_usleep( 500000 * secs );
474
475 millisecs = get_timer( &hires, 0 );
476
477 if( millisecs < 450 * secs || millisecs > 550 * secs )
478 {
479 if( verbose != 0 )
480 polarssl_printf( "failed\n" );
481
482 return( 1 );
483 }
484 }
485
486 if( verbose != 0 )
487 polarssl_printf( "passed\n" );
Paul Bakker9af723c2014-05-01 13:03:14 +0200488#endif /* POLARSSL_NET_C */
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200489
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200490 if( verbose != 0 )
491 polarssl_printf( "\n" );
492
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100493 return( 0 );
494}
495
496#endif /* POLARSSL_SELF_TEST */
497
498#endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */