Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Portable interface to the CPU cycle counter |
| 3 | * |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * 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 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 28 | #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C) |
| 29 | #include "polarssl/platform.h" |
| 30 | #else |
| 31 | #include <stdio.h> |
| 32 | #define polarssl_printf printf |
| 33 | #endif |
| 34 | |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 35 | #if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 37 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 39 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | |
| 41 | #include <windows.h> |
| 42 | #include <winbase.h> |
| 43 | |
| 44 | struct _hr_time |
| 45 | { |
| 46 | LARGE_INTEGER start; |
| 47 | }; |
| 48 | |
| 49 | #else |
| 50 | |
| 51 | #include <unistd.h> |
| 52 | #include <sys/types.h> |
| 53 | #include <sys/time.h> |
| 54 | #include <signal.h> |
| 55 | #include <time.h> |
| 56 | |
| 57 | struct _hr_time |
| 58 | { |
| 59 | struct timeval start; |
| 60 | }; |
| 61 | |
| 62 | #endif |
| 63 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 64 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 65 | (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 67 | #define POLARSSL_HAVE_HARDCLOCK |
| 68 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | unsigned long hardclock( void ) |
| 70 | { |
| 71 | unsigned long tsc; |
| 72 | __asm rdtsc |
| 73 | __asm mov [tsc], eax |
| 74 | return( tsc ); |
| 75 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 76 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 78 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 79 | defined(__GNUC__) && defined(__i386__) |
| 80 | |
| 81 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | |
| 83 | unsigned long hardclock( void ) |
| 84 | { |
Paul Bakker | ca41010 | 2011-10-19 14:27:36 +0000 | [diff] [blame] | 85 | unsigned long lo, hi; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 86 | asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
Paul Bakker | ca41010 | 2011-10-19 14:27:36 +0000 | [diff] [blame] | 87 | return( lo ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 89 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 91 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 92 | defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__)) |
| 93 | |
| 94 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | |
| 96 | unsigned long hardclock( void ) |
| 97 | { |
| 98 | unsigned long lo, hi; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 99 | asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | return( lo | (hi << 32) ); |
| 101 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 102 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 104 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 105 | defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__)) |
| 106 | |
| 107 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 109 | unsigned long hardclock( void ) |
| 110 | { |
| 111 | unsigned long tbl, tbu0, tbu1; |
| 112 | |
| 113 | do |
| 114 | { |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 115 | asm volatile( "mftbu %0" : "=r" (tbu0) ); |
| 116 | asm volatile( "mftb %0" : "=r" (tbl ) ); |
| 117 | asm volatile( "mftbu %0" : "=r" (tbu1) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | } |
| 119 | while( tbu0 != tbu1 ); |
| 120 | |
| 121 | return( tbl ); |
| 122 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 123 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 125 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 126 | defined(__GNUC__) && defined(__sparc64__) |
| 127 | |
| 128 | #if defined(__OpenBSD__) |
| 129 | #warning OpenBSD does not allow access to tick register using software version instead |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | #else |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 131 | #define POLARSSL_HAVE_HARDCLOCK |
| 132 | |
| 133 | unsigned long hardclock( void ) |
| 134 | { |
| 135 | unsigned long tick; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 136 | asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) ); |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 137 | return( tick ); |
| 138 | } |
| 139 | #endif |
| 140 | #endif |
| 141 | |
| 142 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 143 | defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__) |
| 144 | |
| 145 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | |
| 147 | unsigned long hardclock( void ) |
| 148 | { |
| 149 | unsigned long tick; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 150 | asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" ); |
| 151 | asm volatile( "mov %%g1, %0" : "=r" (tick) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | return( tick ); |
| 153 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 154 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 156 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 157 | defined(__GNUC__) && defined(__alpha__) |
| 158 | |
| 159 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | |
| 161 | unsigned long hardclock( void ) |
| 162 | { |
| 163 | unsigned long cc; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 164 | asm volatile( "rpcc %0" : "=r" (cc) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | return( cc & 0xFFFFFFFF ); |
| 166 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 167 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 169 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 170 | defined(__GNUC__) && defined(__ia64__) |
| 171 | |
| 172 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | |
| 174 | unsigned long hardclock( void ) |
| 175 | { |
| 176 | unsigned long itc; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame^] | 177 | asm volatile( "mov %0 = ar.itc" : "=r" (itc) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | return( itc ); |
| 179 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 180 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 182 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \ |
| 183 | !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 184 | |
| 185 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 186 | |
| 187 | unsigned long hardclock( void ) |
| 188 | { |
| 189 | LARGE_INTEGER offset; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 190 | |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 191 | QueryPerformanceCounter( &offset ); |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 192 | |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 193 | return (unsigned long)( offset.QuadPart ); |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 194 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 195 | #endif |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 196 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 197 | #if !defined(POLARSSL_HAVE_HARDCLOCK) |
| 198 | |
| 199 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | |
| 201 | static int hardclock_init = 0; |
| 202 | static struct timeval tv_init; |
| 203 | |
| 204 | unsigned long hardclock( void ) |
| 205 | { |
| 206 | struct timeval tv_cur; |
| 207 | |
| 208 | if( hardclock_init == 0 ) |
| 209 | { |
| 210 | gettimeofday( &tv_init, NULL ); |
| 211 | hardclock_init = 1; |
| 212 | } |
| 213 | |
| 214 | gettimeofday( &tv_cur, NULL ); |
| 215 | return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000 |
| 216 | + ( tv_cur.tv_usec - tv_init.tv_usec ) ); |
| 217 | } |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 218 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 220 | volatile int alarmed = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 222 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | |
| 224 | unsigned long get_timer( struct hr_time *val, int reset ) |
| 225 | { |
| 226 | unsigned long delta; |
| 227 | LARGE_INTEGER offset, hfreq; |
| 228 | struct _hr_time *t = (struct _hr_time *) val; |
| 229 | |
| 230 | QueryPerformanceCounter( &offset ); |
| 231 | QueryPerformanceFrequency( &hfreq ); |
| 232 | |
| 233 | delta = (unsigned long)( ( 1000 * |
| 234 | ( offset.QuadPart - t->start.QuadPart ) ) / |
| 235 | hfreq.QuadPart ); |
| 236 | |
| 237 | if( reset ) |
| 238 | QueryPerformanceCounter( &t->start ); |
| 239 | |
| 240 | return( delta ); |
| 241 | } |
| 242 | |
| 243 | DWORD WINAPI TimerProc( LPVOID uElapse ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 244 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | Sleep( (DWORD) uElapse ); |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 246 | alarmed = 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | return( TRUE ); |
| 248 | } |
| 249 | |
| 250 | void set_alarm( int seconds ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 251 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | DWORD ThreadId; |
| 253 | |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 254 | alarmed = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | CloseHandle( CreateThread( NULL, 0, TimerProc, |
| 256 | (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) ); |
| 257 | } |
| 258 | |
| 259 | void m_sleep( int milliseconds ) |
| 260 | { |
| 261 | Sleep( milliseconds ); |
| 262 | } |
| 263 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 264 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | |
| 266 | unsigned long get_timer( struct hr_time *val, int reset ) |
| 267 | { |
| 268 | unsigned long delta; |
| 269 | struct timeval offset; |
| 270 | struct _hr_time *t = (struct _hr_time *) val; |
| 271 | |
| 272 | gettimeofday( &offset, NULL ); |
| 273 | |
| 274 | delta = ( offset.tv_sec - t->start.tv_sec ) * 1000 |
| 275 | + ( offset.tv_usec - t->start.tv_usec ) / 1000; |
| 276 | |
| 277 | if( reset ) |
| 278 | { |
| 279 | t->start.tv_sec = offset.tv_sec; |
| 280 | t->start.tv_usec = offset.tv_usec; |
| 281 | } |
| 282 | |
| 283 | return( delta ); |
| 284 | } |
| 285 | |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 286 | #if defined(INTEGRITY) |
| 287 | void m_sleep( int milliseconds ) |
| 288 | { |
| 289 | usleep( milliseconds * 1000 ); |
| 290 | } |
| 291 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 292 | #else /* INTEGRITY */ |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 293 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 294 | static void sighandler( int signum ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 295 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 296 | alarmed = 1; |
| 297 | signal( signum, sighandler ); |
| 298 | } |
| 299 | |
| 300 | void set_alarm( int seconds ) |
| 301 | { |
| 302 | alarmed = 0; |
| 303 | signal( SIGALRM, sighandler ); |
| 304 | alarm( seconds ); |
| 305 | } |
| 306 | |
| 307 | void m_sleep( int milliseconds ) |
| 308 | { |
| 309 | struct timeval tv; |
| 310 | |
| 311 | tv.tv_sec = milliseconds / 1000; |
Manuel Pégourié-Gonnard | dfbf9c7 | 2014-02-20 22:16:43 +0100 | [diff] [blame] | 312 | tv.tv_usec = ( milliseconds % 1000 ) * 1000; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 313 | |
| 314 | select( 0, NULL, NULL, NULL, &tv ); |
| 315 | } |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 316 | #endif /* INTEGRITY */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 318 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 320 | #if defined(POLARSSL_SELF_TEST) |
| 321 | |
| 322 | /* |
| 323 | * Checkup routine |
| 324 | */ |
| 325 | int timing_self_test( int verbose ) |
| 326 | { |
| 327 | unsigned long cycles, ratio; |
| 328 | unsigned long millisecs, secs; |
| 329 | int hardfail; |
| 330 | struct hr_time hires; |
| 331 | |
| 332 | if( verbose != 0) |
| 333 | polarssl_printf( " TIMING tests warning: will take some time!\n" ); |
| 334 | |
| 335 | if( verbose != 0 ) |
| 336 | polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " ); |
| 337 | |
| 338 | for( secs = 1; secs <= 3; secs++ ) |
| 339 | { |
| 340 | (void) get_timer( &hires, 1 ); |
| 341 | |
| 342 | m_sleep( 1000 * secs ); |
| 343 | |
| 344 | millisecs = get_timer( &hires, 0 ); |
| 345 | |
| 346 | if( millisecs < 900 * secs || millisecs > 1100 * secs ) |
| 347 | { |
| 348 | if( verbose != 0 ) |
| 349 | polarssl_printf( "failed\n" ); |
| 350 | |
| 351 | return( 1 ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if( verbose != 0 ) |
| 356 | polarssl_printf( "passed\n" ); |
| 357 | |
| 358 | if( verbose != 0 ) |
| 359 | polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " ); |
| 360 | |
| 361 | for( secs = 1; secs <= 3; secs++ ) |
| 362 | { |
| 363 | (void) get_timer( &hires, 1 ); |
| 364 | |
| 365 | set_alarm( secs ); |
| 366 | while( !alarmed ) |
| 367 | ; |
| 368 | |
| 369 | millisecs = get_timer( &hires, 0 ); |
| 370 | |
| 371 | if( millisecs < 900 * secs || millisecs > 1100 * secs ) |
| 372 | { |
| 373 | if( verbose != 0 ) |
| 374 | polarssl_printf( "failed\n" ); |
| 375 | |
| 376 | return( 1 ); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if( verbose != 0 ) |
| 381 | polarssl_printf( "passed\n" ); |
| 382 | |
| 383 | if( verbose != 0 ) |
| 384 | polarssl_printf( " TIMING test #3 (hardclock / m_sleep ): " ); |
| 385 | |
| 386 | /* |
| 387 | * Allow one failure for possible counter wrapping. |
| 388 | * On a 4Ghz 32-bit machine the cycle counter wraps about once per second; |
| 389 | * since the whole test is about 10ms, it shouldn't happen twice in a row. |
| 390 | */ |
| 391 | hardfail = 0; |
| 392 | |
| 393 | hard_test: |
| 394 | if( hardfail > 1 ) |
| 395 | { |
| 396 | if( verbose != 0 ) |
| 397 | polarssl_printf( "failed\n" ); |
| 398 | |
| 399 | return( 1 ); |
| 400 | } |
| 401 | |
| 402 | /* Get a reference ratio cycles/ms */ |
| 403 | cycles = hardclock(); |
| 404 | m_sleep( 1 ); |
| 405 | cycles = hardclock() - cycles; |
| 406 | ratio = cycles / 1; |
| 407 | |
| 408 | for( millisecs = 2; millisecs <= 4; millisecs++ ) |
| 409 | { |
| 410 | cycles = hardclock(); |
| 411 | m_sleep( millisecs ); |
| 412 | cycles = hardclock() - cycles; |
| 413 | |
| 414 | /* Allow variation up to 20% */ |
| 415 | if( cycles / millisecs < ratio - ratio / 5 || |
| 416 | cycles / millisecs > ratio + ratio / 5 ) |
| 417 | { |
| 418 | hardfail++; |
| 419 | goto hard_test; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if( verbose != 0 ) |
| 424 | polarssl_printf( "passed\n" ); |
| 425 | |
| 426 | if( verbose != 0 ) |
| 427 | polarssl_printf( "\n" ); |
| 428 | |
| 429 | return( 0 ); |
| 430 | } |
| 431 | |
| 432 | #endif /* POLARSSL_SELF_TEST */ |
| 433 | |
| 434 | #endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */ |