blob: 1489383870d294e25aa8ba5515bbbcaeb786ec6e [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000080/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Paul Bakkerbb0139c2012-10-31 09:53:08 +000081#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000082 defined(__GNUC__) && ( defined(__i386__) || ( \
83 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000084
85#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87unsigned long hardclock( void )
88{
Paul Bakkerca410102011-10-19 14:27:36 +000089 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010090 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000091 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
Paul Bakker9af723c2014-05-01 13:03:14 +020093#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
94 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakkerbb0139c2012-10-31 09:53:08 +000096#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020097 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000098
99#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101unsigned long hardclock( void )
102{
103 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100104 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200105 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106}
Paul Bakker9af723c2014-05-01 13:03:14 +0200107#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
108 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000110#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200111 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000112
113#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115unsigned long hardclock( void )
116{
117 unsigned long tbl, tbu0, tbu1;
118
119 do
120 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100121 asm volatile( "mftbu %0" : "=r" (tbu0) );
122 asm volatile( "mftb %0" : "=r" (tbl ) );
123 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 }
125 while( tbu0 != tbu1 );
126
127 return( tbl );
128}
Paul Bakker9af723c2014-05-01 13:03:14 +0200129#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
130 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000132#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
133 defined(__GNUC__) && defined(__sparc64__)
134
135#if defined(__OpenBSD__)
136#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000137#else
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000138#define POLARSSL_HAVE_HARDCLOCK
139
140unsigned long hardclock( void )
141{
142 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100143 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000144 return( tick );
145}
Paul Bakker9af723c2014-05-01 13:03:14 +0200146#endif /* __OpenBSD__ */
147#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
148 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149
150#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
151 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
152
153#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155unsigned long hardclock( void )
156{
157 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100158 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
159 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 return( tick );
161}
Paul Bakker9af723c2014-05-01 13:03:14 +0200162#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
163 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000165#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
166 defined(__GNUC__) && defined(__alpha__)
167
168#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170unsigned long hardclock( void )
171{
172 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100173 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 return( cc & 0xFFFFFFFF );
175}
Paul Bakker9af723c2014-05-01 13:03:14 +0200176#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
177 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000179#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
180 defined(__GNUC__) && defined(__ia64__)
181
182#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184unsigned long hardclock( void )
185{
186 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100187 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 return( itc );
189}
Paul Bakker9af723c2014-05-01 13:03:14 +0200190#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
191 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100193#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
194 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000195
196#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000197
198unsigned long hardclock( void )
199{
200 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100201
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100202 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000203
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000205}
Paul Bakker9af723c2014-05-01 13:03:14 +0200206#endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000207
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000208#if !defined(POLARSSL_HAVE_HARDCLOCK)
209
210#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212static int hardclock_init = 0;
213static struct timeval tv_init;
214
215unsigned long hardclock( void )
216{
217 struct timeval tv_cur;
218
219 if( hardclock_init == 0 )
220 {
221 gettimeofday( &tv_init, NULL );
222 hardclock_init = 1;
223 }
224
225 gettimeofday( &tv_cur, NULL );
226 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
227 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
228}
Paul Bakker9af723c2014-05-01 13:03:14 +0200229#endif /* !POLARSSL_HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakker2eee9022011-04-24 15:28:55 +0000231volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100233#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235unsigned long get_timer( struct hr_time *val, int reset )
236{
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 struct _hr_time *t = (struct _hr_time *) val;
238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 if( reset )
Gilles Peskine2484ffe2017-10-16 19:33:06 +0200240 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 QueryPerformanceCounter( &t->start );
Gilles Peskine2484ffe2017-10-16 19:33:06 +0200242 return( 0 );
243 }
244 else
245 {
246 unsigned long delta;
247 LARGE_INTEGER now, hfreq;
248 QueryPerformanceCounter( &now );
249 QueryPerformanceFrequency( &hfreq );
250 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
251 / hfreq.QuadPart );
252 return( delta );
253 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000254}
255
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000256/* It's OK to use a global because alarm() is supposed to be global anyway */
257static DWORD alarmMs;
258
Manuel Pégourié-Gonnard6d71e4e2015-02-11 12:54:35 +0000259static DWORD WINAPI TimerProc( LPVOID TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100260{
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000261 ((void) TimerContext);
262 Sleep( alarmMs );
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100263 alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 return( TRUE );
265}
266
267void set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100268{
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 DWORD ThreadId;
270
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100271 alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000272 alarmMs = seconds * 1000;
273 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274}
275
276void m_sleep( int milliseconds )
277{
278 Sleep( milliseconds );
279}
280
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100281#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283unsigned long get_timer( struct hr_time *val, int reset )
284{
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 struct _hr_time *t = (struct _hr_time *) val;
286
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 if( reset )
288 {
Gilles Peskine2484ffe2017-10-16 19:33:06 +0200289 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200290 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 }
Gilles Peskine2484ffe2017-10-16 19:33:06 +0200292 else
293 {
294 unsigned long delta;
295 struct timeval now;
296 gettimeofday( &now, NULL );
297 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
298 + ( now.tv_usec - t->start.tv_usec ) / 1000;
299 return( delta );
300 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000301}
302
Paul Bakker49d75672012-09-26 15:22:07 +0000303#if defined(INTEGRITY)
304void m_sleep( int milliseconds )
305{
306 usleep( milliseconds * 1000 );
307}
308
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100309#else /* INTEGRITY */
Paul Bakker49d75672012-09-26 15:22:07 +0000310
Paul Bakker5121ce52009-01-03 21:22:43 +0000311static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100312{
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 alarmed = 1;
314 signal( signum, sighandler );
315}
316
317void set_alarm( int seconds )
318{
319 alarmed = 0;
320 signal( SIGALRM, sighandler );
321 alarm( seconds );
Gilles Peskinede896eb2017-10-10 20:10:46 +0200322 if( seconds == 0 )
323 {
324 /* alarm(0) cancelled any previous pending alarm, but the
325 handler won't fire, so raise the flag straight away. */
326 alarmed = 1;
327 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000328}
329
330void m_sleep( int milliseconds )
331{
332 struct timeval tv;
333
334 tv.tv_sec = milliseconds / 1000;
Manuel Pégourié-Gonnarddfbf9c72014-02-20 22:16:43 +0100335 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 select( 0, NULL, NULL, NULL, &tv );
338}
Paul Bakker49d75672012-09-26 15:22:07 +0000339#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100341#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100343#if defined(POLARSSL_SELF_TEST)
344
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200345/* To test net_usleep against our functions */
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200346#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200347#include "polarssl/net.h"
348#endif
349
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100350/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200351 * Busy-waits for the given number of milliseconds.
352 * Used for testing hardclock.
353 */
354static void busy_msleep( unsigned long msec )
355{
356 struct hr_time hires;
357 unsigned long i = 0; /* for busy-waiting */
358 volatile unsigned long j; /* to prevent optimisation */
359
360 (void) get_timer( &hires, 1 );
361
362 while( get_timer( &hires, 0 ) < msec )
363 i++;
364
365 j = i;
366 (void) j;
367}
368
Gilles Peskinee4050692017-10-10 20:09:26 +0200369#define FAIL do \
370 { \
371 if( verbose != 0 ) \
372 { \
373 polarssl_printf( "failed at line %d\n", __LINE__ ); \
374 polarssl_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d\n", \
375 cycles, ratio, millisecs, secs, hardfail ); \
376 polarssl_printf( " elapsed(hires)=%lu\n", \
377 get_timer( &hires, 0 ) ); \
378 } \
379 return( 1 ); \
380 } while( 0 )
381
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200382/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100383 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200384 *
385 * Warning: this is work in progress, some tests may not be reliable enough
386 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100387 */
388int timing_self_test( int verbose )
389{
Gilles Peskinee4050692017-10-10 20:09:26 +0200390 unsigned long cycles = 0, ratio = 0;
391 unsigned long millisecs = 0, secs = 0;
392 int hardfail = 0;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100393 struct hr_time hires;
394
Paul Bakker66d5d072014-06-17 16:39:18 +0200395 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200396 polarssl_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100397
398 if( verbose != 0 )
399 polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
400
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100401 {
Gilles Peskine8833e862017-12-20 22:33:11 +0100402 secs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100403 (void) get_timer( &hires, 1 );
404
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200405 m_sleep( (int)( 500 * secs ) );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100406
407 millisecs = get_timer( &hires, 0 );
408
Manuel Pégourié-Gonnard25f44a62015-08-18 20:11:48 +0200409 if( millisecs < 400 * secs || millisecs > 600 * secs )
Gilles Peskinee4050692017-10-10 20:09:26 +0200410 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100411 }
412
413 if( verbose != 0 )
414 polarssl_printf( "passed\n" );
415
416 if( verbose != 0 )
417 polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
418
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100419 {
Gilles Peskine8833e862017-12-20 22:33:11 +0100420 secs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100421 (void) get_timer( &hires, 1 );
422
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200423 set_alarm( (int) secs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424 while( !alarmed )
425 ;
426
427 millisecs = get_timer( &hires, 0 );
428
Manuel Pégourié-Gonnard25f44a62015-08-18 20:11:48 +0200429 /* For some reason on Windows it looks like alarm has an extra delay
430 * (maybe related to creating a new thread). Allow some room here. */
431 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskinee4050692017-10-10 20:09:26 +0200432 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100433 }
434
435 if( verbose != 0 )
436 polarssl_printf( "passed\n" );
437
438 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200439 polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100440
441 /*
442 * Allow one failure for possible counter wrapping.
443 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
444 * since the whole test is about 10ms, it shouldn't happen twice in a row.
445 */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100446
447hard_test:
448 if( hardfail > 1 )
449 {
450 if( verbose != 0 )
Manuel Pégourié-Gonnard3ab7b962015-07-06 17:17:55 +0200451 polarssl_printf( "failed (ignored)\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100452
Manuel Pégourié-Gonnard3ab7b962015-07-06 17:17:55 +0200453 goto hard_test_done;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100454 }
455
456 /* Get a reference ratio cycles/ms */
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200457 millisecs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100458 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200459 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100460 cycles = hardclock() - cycles;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200461 ratio = cycles / millisecs;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100462
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200463 /* Check that the ratio is mostly constant */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100464 for( millisecs = 2; millisecs <= 4; millisecs++ )
465 {
466 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200467 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100468 cycles = hardclock() - cycles;
469
470 /* Allow variation up to 20% */
471 if( cycles / millisecs < ratio - ratio / 5 ||
472 cycles / millisecs > ratio + ratio / 5 )
473 {
474 hardfail++;
475 goto hard_test;
476 }
477 }
478
479 if( verbose != 0 )
480 polarssl_printf( "passed\n" );
481
Manuel Pégourié-Gonnard3ab7b962015-07-06 17:17:55 +0200482hard_test_done:
483
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200484#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200485 if( verbose != 0 )
486 polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
487
488 for( secs = 1; secs <= 3; secs++ )
489 {
490 (void) get_timer( &hires, 1 );
491
492 net_usleep( 500000 * secs );
493
494 millisecs = get_timer( &hires, 0 );
495
Manuel Pégourié-Gonnard3a5ee1c2015-08-19 14:48:34 +0200496 if( millisecs < 400 * secs || millisecs > 600 * secs )
Gilles Peskinee4050692017-10-10 20:09:26 +0200497 FAIL;
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200498 }
499
500 if( verbose != 0 )
501 polarssl_printf( "passed\n" );
Paul Bakker9af723c2014-05-01 13:03:14 +0200502#endif /* POLARSSL_NET_C */
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200503
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200504 if( verbose != 0 )
505 polarssl_printf( "\n" );
506
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100507 return( 0 );
508}
509
510#endif /* POLARSSL_SELF_TEST */
511
512#endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */