blob: f1d8a1fed8c91747bd4c7725e17557b475ce6dd1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Paul Bakkerf2561b32014-02-06 15:11:55 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakkerf2561b32014-02-06 15:11:55 +010028#if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerfa6a6202013-10-28 18:48:30 +010032#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#include <windows.h>
35#include <winbase.h>
36
37struct _hr_time
38{
39 LARGE_INTEGER start;
40};
41
42#else
43
44#include <unistd.h>
45#include <sys/types.h>
46#include <sys/time.h>
47#include <signal.h>
48#include <time.h>
49
50struct _hr_time
51{
52 struct timeval start;
53};
54
55#endif
56
Paul Bakkerbb0139c2012-10-31 09:53:08 +000057#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker34a90562009-04-19 21:17:09 +000058 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Paul Bakkerbb0139c2012-10-31 09:53:08 +000060#define POLARSSL_HAVE_HARDCLOCK
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062unsigned long hardclock( void )
63{
64 unsigned long tsc;
65 __asm rdtsc
66 __asm mov [tsc], eax
67 return( tsc );
68}
Paul Bakkerbb0139c2012-10-31 09:53:08 +000069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkerbb0139c2012-10-31 09:53:08 +000071#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
72 defined(__GNUC__) && defined(__i386__)
73
74#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76unsigned long hardclock( void )
77{
Paul Bakkerca410102011-10-19 14:27:36 +000078 unsigned long lo, hi;
79 asm( "rdtsc" : "=a" (lo), "=d" (hi) );
80 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000081}
Paul Bakkerbb0139c2012-10-31 09:53:08 +000082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Paul Bakkerbb0139c2012-10-31 09:53:08 +000084#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
85 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
86
87#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89unsigned long hardclock( void )
90{
91 unsigned long lo, hi;
92 asm( "rdtsc" : "=a" (lo), "=d" (hi) );
93 return( lo | (hi << 32) );
94}
Paul Bakkerbb0139c2012-10-31 09:53:08 +000095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakkerbb0139c2012-10-31 09:53:08 +000097#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
98 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
99
100#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102unsigned long hardclock( void )
103{
104 unsigned long tbl, tbu0, tbu1;
105
106 do
107 {
108 asm( "mftbu %0" : "=r" (tbu0) );
109 asm( "mftb %0" : "=r" (tbl ) );
110 asm( "mftbu %0" : "=r" (tbu1) );
111 }
112 while( tbu0 != tbu1 );
113
114 return( tbl );
115}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000118#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
119 defined(__GNUC__) && defined(__sparc64__)
120
121#if defined(__OpenBSD__)
122#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000123#else
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000124#define POLARSSL_HAVE_HARDCLOCK
125
126unsigned long hardclock( void )
127{
128 unsigned long tick;
129 asm( "rdpr %%tick, %0;" : "=&r" (tick) );
130 return( tick );
131}
132#endif
133#endif
134
135#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
136 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
137
138#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140unsigned long hardclock( void )
141{
142 unsigned long tick;
143 asm( ".byte 0x83, 0x41, 0x00, 0x00" );
144 asm( "mov %%g1, %0" : "=r" (tick) );
145 return( tick );
146}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
150 defined(__GNUC__) && defined(__alpha__)
151
152#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154unsigned long hardclock( void )
155{
156 unsigned long cc;
157 asm( "rpcc %0" : "=r" (cc) );
158 return( cc & 0xFFFFFFFF );
159}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000162#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
163 defined(__GNUC__) && defined(__ia64__)
164
165#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167unsigned long hardclock( void )
168{
169 unsigned long itc;
170 asm( "mov %0 = ar.itc" : "=r" (itc) );
171 return( itc );
172}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100175#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
176 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000177
178#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000179
180unsigned long hardclock( void )
181{
182 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100183
Paul Bakker2eee9022011-04-24 15:28:55 +0000184 QueryPerformanceCounter( &offset );
185
186 return (unsigned long)( offset.QuadPart );
187}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000188#endif
Paul Bakker2eee9022011-04-24 15:28:55 +0000189
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000190#if !defined(POLARSSL_HAVE_HARDCLOCK)
191
192#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194static int hardclock_init = 0;
195static struct timeval tv_init;
196
197unsigned long hardclock( void )
198{
199 struct timeval tv_cur;
200
201 if( hardclock_init == 0 )
202 {
203 gettimeofday( &tv_init, NULL );
204 hardclock_init = 1;
205 }
206
207 gettimeofday( &tv_cur, NULL );
208 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
209 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
210}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
Paul Bakker2eee9022011-04-24 15:28:55 +0000213volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100215#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217unsigned long get_timer( struct hr_time *val, int reset )
218{
219 unsigned long delta;
220 LARGE_INTEGER offset, hfreq;
221 struct _hr_time *t = (struct _hr_time *) val;
222
223 QueryPerformanceCounter( &offset );
224 QueryPerformanceFrequency( &hfreq );
225
226 delta = (unsigned long)( ( 1000 *
227 ( offset.QuadPart - t->start.QuadPart ) ) /
228 hfreq.QuadPart );
229
230 if( reset )
231 QueryPerformanceCounter( &t->start );
232
233 return( delta );
234}
235
236DWORD WINAPI TimerProc( LPVOID uElapse )
237{
238 Sleep( (DWORD) uElapse );
239 alarmed = 1;
240 return( TRUE );
241}
242
243void set_alarm( int seconds )
244{
245 DWORD ThreadId;
246
247 alarmed = 0;
248 CloseHandle( CreateThread( NULL, 0, TimerProc,
249 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
250}
251
252void m_sleep( int milliseconds )
253{
254 Sleep( milliseconds );
255}
256
257#else
258
259unsigned long get_timer( struct hr_time *val, int reset )
260{
261 unsigned long delta;
262 struct timeval offset;
263 struct _hr_time *t = (struct _hr_time *) val;
264
265 gettimeofday( &offset, NULL );
266
267 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
268 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
269
270 if( reset )
271 {
272 t->start.tv_sec = offset.tv_sec;
273 t->start.tv_usec = offset.tv_usec;
274 }
275
276 return( delta );
277}
278
Paul Bakker49d75672012-09-26 15:22:07 +0000279#if defined(INTEGRITY)
280void m_sleep( int milliseconds )
281{
282 usleep( milliseconds * 1000 );
283}
284
285#else
286
Paul Bakker5121ce52009-01-03 21:22:43 +0000287static void sighandler( int signum )
288{
289 alarmed = 1;
290 signal( signum, sighandler );
291}
292
293void set_alarm( int seconds )
294{
295 alarmed = 0;
296 signal( SIGALRM, sighandler );
297 alarm( seconds );
298}
299
300void m_sleep( int milliseconds )
301{
302 struct timeval tv;
303
304 tv.tv_sec = milliseconds / 1000;
Manuel Pégourié-Gonnarddfbf9c72014-02-20 22:16:43 +0100305 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 select( 0, NULL, NULL, NULL, &tv );
308}
Paul Bakker49d75672012-09-26 15:22:07 +0000309#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311#endif
312
313#endif