Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Portable interface to the CPU cycle counter |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 29 | #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 Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 36 | #if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 38 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 40 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | |
| 42 | #include <windows.h> |
| 43 | #include <winbase.h> |
| 44 | |
| 45 | struct _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 | |
| 58 | struct _hr_time |
| 59 | { |
| 60 | struct timeval start; |
| 61 | }; |
| 62 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 63 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 65 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 66 | ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 68 | #define POLARSSL_HAVE_HARDCLOCK |
| 69 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | unsigned long hardclock( void ) |
| 71 | { |
| 72 | unsigned long tsc; |
| 73 | __asm rdtsc |
| 74 | __asm mov [tsc], eax |
| 75 | return( tsc ); |
| 76 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 77 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 78 | ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 3843353 | 2015-02-11 11:35:58 +0000 | [diff] [blame] | 80 | /* some versions of mingw-64 have 32-bit longs even on x84_64 */ |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 81 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
Manuel Pégourié-Gonnard | 3843353 | 2015-02-11 11:35:58 +0000 | [diff] [blame] | 82 | defined(__GNUC__) && ( defined(__i386__) || ( \ |
| 83 | ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) ) |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 84 | |
| 85 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | |
| 87 | unsigned long hardclock( void ) |
| 88 | { |
Paul Bakker | ca41010 | 2011-10-19 14:27:36 +0000 | [diff] [blame] | 89 | unsigned long lo, hi; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 90 | asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
Paul Bakker | ca41010 | 2011-10-19 14:27:36 +0000 | [diff] [blame] | 91 | return( lo ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 93 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 94 | __GNUC__ && __i386__ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 96 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 97 | defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) ) |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 98 | |
| 99 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | |
| 101 | unsigned long hardclock( void ) |
| 102 | { |
| 103 | unsigned long lo, hi; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 104 | asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 105 | return( lo | ( hi << 32 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 107 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 108 | __GNUC__ && ( __amd64__ || __x86_64__ ) */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 110 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 111 | defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) ) |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 112 | |
| 113 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | |
| 115 | unsigned long hardclock( void ) |
| 116 | { |
| 117 | unsigned long tbl, tbu0, tbu1; |
| 118 | |
| 119 | do |
| 120 | { |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 121 | asm volatile( "mftbu %0" : "=r" (tbu0) ); |
| 122 | asm volatile( "mftb %0" : "=r" (tbl ) ); |
| 123 | asm volatile( "mftbu %0" : "=r" (tbu1) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | } |
| 125 | while( tbu0 != tbu1 ); |
| 126 | |
| 127 | return( tbl ); |
| 128 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 129 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 130 | __GNUC__ && ( __powerpc__ || __ppc__ ) */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 132 | #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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | #else |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 138 | #define POLARSSL_HAVE_HARDCLOCK |
| 139 | |
| 140 | unsigned long hardclock( void ) |
| 141 | { |
| 142 | unsigned long tick; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 143 | asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) ); |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 144 | return( tick ); |
| 145 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 146 | #endif /* __OpenBSD__ */ |
| 147 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 148 | __GNUC__ && __sparc64__ */ |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 149 | |
| 150 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 151 | defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__) |
| 152 | |
| 153 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
| 155 | unsigned long hardclock( void ) |
| 156 | { |
| 157 | unsigned long tick; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 158 | asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" ); |
| 159 | asm volatile( "mov %%g1, %0" : "=r" (tick) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | return( tick ); |
| 161 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 162 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 163 | __GNUC__ && __sparc__ && !__sparc64__ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 165 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 166 | defined(__GNUC__) && defined(__alpha__) |
| 167 | |
| 168 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | |
| 170 | unsigned long hardclock( void ) |
| 171 | { |
| 172 | unsigned long cc; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 173 | asm volatile( "rpcc %0" : "=r" (cc) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | return( cc & 0xFFFFFFFF ); |
| 175 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 176 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 177 | __GNUC__ && __alpha__ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 179 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ |
| 180 | defined(__GNUC__) && defined(__ia64__) |
| 181 | |
| 182 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | |
| 184 | unsigned long hardclock( void ) |
| 185 | { |
| 186 | unsigned long itc; |
Manuel Pégourié-Gonnard | d6aebe1 | 2014-03-27 21:15:40 +0100 | [diff] [blame] | 187 | asm volatile( "mov %0 = ar.itc" : "=r" (itc) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | return( itc ); |
| 189 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 190 | #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && |
| 191 | __GNUC__ && __ia64__ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 193 | #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \ |
| 194 | !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 195 | |
| 196 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 197 | |
| 198 | unsigned long hardclock( void ) |
| 199 | { |
| 200 | LARGE_INTEGER offset; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 202 | QueryPerformanceCounter( &offset ); |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 203 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 204 | return( (unsigned long)( offset.QuadPart ) ); |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 205 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 206 | #endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */ |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 207 | |
Paul Bakker | bb0139c | 2012-10-31 09:53:08 +0000 | [diff] [blame] | 208 | #if !defined(POLARSSL_HAVE_HARDCLOCK) |
| 209 | |
| 210 | #define POLARSSL_HAVE_HARDCLOCK |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | |
| 212 | static int hardclock_init = 0; |
| 213 | static struct timeval tv_init; |
| 214 | |
| 215 | unsigned 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 Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 229 | #endif /* !POLARSSL_HAVE_HARDCLOCK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | |
Paul Bakker | 2eee902 | 2011-04-24 15:28:55 +0000 | [diff] [blame] | 231 | volatile int alarmed = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 233 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | |
| 235 | unsigned long get_timer( struct hr_time *val, int reset ) |
| 236 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 237 | struct _hr_time *t = (struct _hr_time *) val; |
| 238 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | if( reset ) |
Gilles Peskine | 2484ffe | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 240 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 241 | QueryPerformanceCounter( &t->start ); |
Gilles Peskine | 2484ffe | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 242 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 256 | /* It's OK to use a global because alarm() is supposed to be global anyway */ |
| 257 | static DWORD alarmMs; |
| 258 | |
Manuel Pégourié-Gonnard | 6d71e4e | 2015-02-11 12:54:35 +0000 | [diff] [blame] | 259 | static DWORD WINAPI TimerProc( LPVOID TimerContext ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 260 | { |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 261 | ((void) TimerContext); |
| 262 | Sleep( alarmMs ); |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 263 | alarmed = 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | return( TRUE ); |
| 265 | } |
| 266 | |
| 267 | void set_alarm( int seconds ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 268 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 269 | DWORD ThreadId; |
| 270 | |
Manuel Pégourié-Gonnard | f39f732 | 2018-01-29 10:16:30 +0100 | [diff] [blame] | 271 | if( seconds == 0 ) |
| 272 | { |
| 273 | /* No need to create a thread for this simple case. |
| 274 | * Also, this shorcut is more reliable at least on MinGW32 */ |
| 275 | alarmed = 1; |
| 276 | return; |
| 277 | } |
| 278 | |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 279 | alarmed = 0; |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 280 | alarmMs = seconds * 1000; |
| 281 | CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void m_sleep( int milliseconds ) |
| 285 | { |
| 286 | Sleep( milliseconds ); |
| 287 | } |
| 288 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 289 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | |
| 291 | unsigned long get_timer( struct hr_time *val, int reset ) |
| 292 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | struct _hr_time *t = (struct _hr_time *) val; |
| 294 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 295 | if( reset ) |
| 296 | { |
Gilles Peskine | 2484ffe | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 297 | gettimeofday( &t->start, NULL ); |
Alfred Klomp | b308dd7 | 2014-07-14 22:32:21 +0200 | [diff] [blame] | 298 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | } |
Gilles Peskine | 2484ffe | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 300 | else |
| 301 | { |
| 302 | unsigned long delta; |
| 303 | struct timeval now; |
| 304 | gettimeofday( &now, NULL ); |
| 305 | delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul |
| 306 | + ( now.tv_usec - t->start.tv_usec ) / 1000; |
| 307 | return( delta ); |
| 308 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 311 | #if defined(INTEGRITY) |
| 312 | void m_sleep( int milliseconds ) |
| 313 | { |
| 314 | usleep( milliseconds * 1000 ); |
| 315 | } |
| 316 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 317 | #else /* INTEGRITY */ |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 318 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | static void sighandler( int signum ) |
Manuel Pégourié-Gonnard | 487588d | 2014-03-27 19:02:07 +0100 | [diff] [blame] | 320 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | alarmed = 1; |
| 322 | signal( signum, sighandler ); |
| 323 | } |
| 324 | |
| 325 | void set_alarm( int seconds ) |
| 326 | { |
| 327 | alarmed = 0; |
| 328 | signal( SIGALRM, sighandler ); |
| 329 | alarm( seconds ); |
Gilles Peskine | de896eb | 2017-10-10 20:10:46 +0200 | [diff] [blame] | 330 | if( seconds == 0 ) |
| 331 | { |
| 332 | /* alarm(0) cancelled any previous pending alarm, but the |
| 333 | handler won't fire, so raise the flag straight away. */ |
| 334 | alarmed = 1; |
| 335 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void m_sleep( int milliseconds ) |
| 339 | { |
| 340 | struct timeval tv; |
| 341 | |
| 342 | tv.tv_sec = milliseconds / 1000; |
Manuel Pégourié-Gonnard | dfbf9c7 | 2014-02-20 22:16:43 +0100 | [diff] [blame] | 343 | tv.tv_usec = ( milliseconds % 1000 ) * 1000; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | |
| 345 | select( 0, NULL, NULL, NULL, &tv ); |
| 346 | } |
Paul Bakker | 49d7567 | 2012-09-26 15:22:07 +0000 | [diff] [blame] | 347 | #endif /* INTEGRITY */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 349 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 351 | #if defined(POLARSSL_SELF_TEST) |
| 352 | |
Manuel Pégourié-Gonnard | 79e5842 | 2014-04-02 18:42:01 +0200 | [diff] [blame] | 353 | /* To test net_usleep against our functions */ |
Manuel Pégourié-Gonnard | 462906f | 2014-07-21 17:37:01 +0200 | [diff] [blame] | 354 | #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME) |
Manuel Pégourié-Gonnard | 79e5842 | 2014-04-02 18:42:01 +0200 | [diff] [blame] | 355 | #include "polarssl/net.h" |
| 356 | #endif |
| 357 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 358 | /* |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 359 | * Busy-waits for the given number of milliseconds. |
| 360 | * Used for testing hardclock. |
| 361 | */ |
| 362 | static void busy_msleep( unsigned long msec ) |
| 363 | { |
| 364 | struct hr_time hires; |
| 365 | unsigned long i = 0; /* for busy-waiting */ |
| 366 | volatile unsigned long j; /* to prevent optimisation */ |
| 367 | |
| 368 | (void) get_timer( &hires, 1 ); |
| 369 | |
| 370 | while( get_timer( &hires, 0 ) < msec ) |
| 371 | i++; |
| 372 | |
| 373 | j = i; |
| 374 | (void) j; |
| 375 | } |
| 376 | |
Gilles Peskine | e405069 | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 377 | #define FAIL do \ |
| 378 | { \ |
| 379 | if( verbose != 0 ) \ |
| 380 | { \ |
| 381 | polarssl_printf( "failed at line %d\n", __LINE__ ); \ |
| 382 | polarssl_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d\n", \ |
| 383 | cycles, ratio, millisecs, secs, hardfail ); \ |
| 384 | polarssl_printf( " elapsed(hires)=%lu\n", \ |
| 385 | get_timer( &hires, 0 ) ); \ |
| 386 | } \ |
| 387 | return( 1 ); \ |
| 388 | } while( 0 ) |
| 389 | |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 390 | /* |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 391 | * Checkup routine |
Manuel Pégourié-Gonnard | 0f79bab | 2014-04-09 09:56:16 +0200 | [diff] [blame] | 392 | * |
| 393 | * Warning: this is work in progress, some tests may not be reliable enough |
| 394 | * yet! False positives may happen. |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 395 | */ |
| 396 | int timing_self_test( int verbose ) |
| 397 | { |
Gilles Peskine | e405069 | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 398 | unsigned long cycles = 0, ratio = 0; |
| 399 | unsigned long millisecs = 0, secs = 0; |
| 400 | int hardfail = 0; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 401 | struct hr_time hires; |
| 402 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 403 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 404 | polarssl_printf( " TIMING tests note: will take some time!\n" ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 405 | |
| 406 | if( verbose != 0 ) |
| 407 | polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " ); |
| 408 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 409 | { |
Gilles Peskine | 8833e86 | 2017-12-20 22:33:11 +0100 | [diff] [blame] | 410 | secs = 1; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 411 | (void) get_timer( &hires, 1 ); |
| 412 | |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 413 | m_sleep( (int)( 500 * secs ) ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 414 | |
| 415 | millisecs = get_timer( &hires, 0 ); |
| 416 | |
Manuel Pégourié-Gonnard | 25f44a6 | 2015-08-18 20:11:48 +0200 | [diff] [blame] | 417 | if( millisecs < 400 * secs || millisecs > 600 * secs ) |
Gilles Peskine | e405069 | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 418 | FAIL; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if( verbose != 0 ) |
| 422 | polarssl_printf( "passed\n" ); |
| 423 | |
| 424 | if( verbose != 0 ) |
| 425 | polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " ); |
| 426 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 427 | { |
Gilles Peskine | 8833e86 | 2017-12-20 22:33:11 +0100 | [diff] [blame] | 428 | secs = 1; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 429 | (void) get_timer( &hires, 1 ); |
| 430 | |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 431 | set_alarm( (int) secs ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 432 | while( !alarmed ) |
| 433 | ; |
| 434 | |
| 435 | millisecs = get_timer( &hires, 0 ); |
| 436 | |
Manuel Pégourié-Gonnard | 25f44a6 | 2015-08-18 20:11:48 +0200 | [diff] [blame] | 437 | /* For some reason on Windows it looks like alarm has an extra delay |
| 438 | * (maybe related to creating a new thread). Allow some room here. */ |
| 439 | if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 ) |
Gilles Peskine | e405069 | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 440 | FAIL; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if( verbose != 0 ) |
| 444 | polarssl_printf( "passed\n" ); |
| 445 | |
| 446 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 447 | polarssl_printf( " TIMING test #3 (hardclock / get_timer): " ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Allow one failure for possible counter wrapping. |
| 451 | * On a 4Ghz 32-bit machine the cycle counter wraps about once per second; |
| 452 | * since the whole test is about 10ms, it shouldn't happen twice in a row. |
| 453 | */ |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 454 | |
| 455 | hard_test: |
| 456 | if( hardfail > 1 ) |
| 457 | { |
| 458 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 3ab7b96 | 2015-07-06 17:17:55 +0200 | [diff] [blame] | 459 | polarssl_printf( "failed (ignored)\n" ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | 3ab7b96 | 2015-07-06 17:17:55 +0200 | [diff] [blame] | 461 | goto hard_test_done; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Get a reference ratio cycles/ms */ |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 465 | millisecs = 1; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 466 | cycles = hardclock(); |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 467 | busy_msleep( millisecs ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 468 | cycles = hardclock() - cycles; |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 469 | ratio = cycles / millisecs; |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 470 | |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 471 | /* Check that the ratio is mostly constant */ |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 472 | for( millisecs = 2; millisecs <= 4; millisecs++ ) |
| 473 | { |
| 474 | cycles = hardclock(); |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 475 | busy_msleep( millisecs ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 476 | cycles = hardclock() - cycles; |
| 477 | |
| 478 | /* Allow variation up to 20% */ |
| 479 | if( cycles / millisecs < ratio - ratio / 5 || |
| 480 | cycles / millisecs > ratio + ratio / 5 ) |
| 481 | { |
| 482 | hardfail++; |
| 483 | goto hard_test; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if( verbose != 0 ) |
| 488 | polarssl_printf( "passed\n" ); |
| 489 | |
Manuel Pégourié-Gonnard | 3ab7b96 | 2015-07-06 17:17:55 +0200 | [diff] [blame] | 490 | hard_test_done: |
| 491 | |
Manuel Pégourié-Gonnard | 462906f | 2014-07-21 17:37:01 +0200 | [diff] [blame] | 492 | #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME) |
Manuel Pégourié-Gonnard | 79e5842 | 2014-04-02 18:42:01 +0200 | [diff] [blame] | 493 | if( verbose != 0 ) |
| 494 | polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " ); |
| 495 | |
| 496 | for( secs = 1; secs <= 3; secs++ ) |
| 497 | { |
| 498 | (void) get_timer( &hires, 1 ); |
| 499 | |
| 500 | net_usleep( 500000 * secs ); |
| 501 | |
| 502 | millisecs = get_timer( &hires, 0 ); |
| 503 | |
Manuel Pégourié-Gonnard | 3a5ee1c | 2015-08-19 14:48:34 +0200 | [diff] [blame] | 504 | if( millisecs < 400 * secs || millisecs > 600 * secs ) |
Gilles Peskine | e405069 | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 505 | FAIL; |
Manuel Pégourié-Gonnard | 79e5842 | 2014-04-02 18:42:01 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if( verbose != 0 ) |
| 509 | polarssl_printf( "passed\n" ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 510 | #endif /* POLARSSL_NET_C */ |
Manuel Pégourié-Gonnard | 79e5842 | 2014-04-02 18:42:01 +0200 | [diff] [blame] | 511 | |
Manuel Pégourié-Gonnard | e1ac0f8 | 2014-05-28 11:44:20 +0200 | [diff] [blame] | 512 | if( verbose != 0 ) |
| 513 | polarssl_printf( "\n" ); |
| 514 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 515 | return( 0 ); |
| 516 | } |
| 517 | |
| 518 | #endif /* POLARSSL_SELF_TEST */ |
| 519 | |
| 520 | #endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */ |