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