blob: 0273d1af84b0dedb8e5af9c83435ecd72e63e060 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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 Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_TIMING_C)
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 Bakkerff60ee62010-03-16 21:09:09 +000032#if defined(_WIN32)
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 Bakkerbb0139c2012-10-31 09:53:08 +0000175#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER)
176
177#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000178
179unsigned long hardclock( void )
180{
181 LARGE_INTEGER offset;
182
183 QueryPerformanceCounter( &offset );
184
185 return (unsigned long)( offset.QuadPart );
186}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000187#endif
Paul Bakker2eee9022011-04-24 15:28:55 +0000188
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000189#if !defined(POLARSSL_HAVE_HARDCLOCK)
190
191#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193static int hardclock_init = 0;
194static struct timeval tv_init;
195
196unsigned long hardclock( void )
197{
198 struct timeval tv_cur;
199
200 if( hardclock_init == 0 )
201 {
202 gettimeofday( &tv_init, NULL );
203 hardclock_init = 1;
204 }
205
206 gettimeofday( &tv_cur, NULL );
207 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
208 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
209}
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Paul Bakker2eee9022011-04-24 15:28:55 +0000212volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakkerff60ee62010-03-16 21:09:09 +0000214#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216unsigned long get_timer( struct hr_time *val, int reset )
217{
218 unsigned long delta;
219 LARGE_INTEGER offset, hfreq;
220 struct _hr_time *t = (struct _hr_time *) val;
221
222 QueryPerformanceCounter( &offset );
223 QueryPerformanceFrequency( &hfreq );
224
225 delta = (unsigned long)( ( 1000 *
226 ( offset.QuadPart - t->start.QuadPart ) ) /
227 hfreq.QuadPart );
228
229 if( reset )
230 QueryPerformanceCounter( &t->start );
231
232 return( delta );
233}
234
235DWORD WINAPI TimerProc( LPVOID uElapse )
236{
237 Sleep( (DWORD) uElapse );
238 alarmed = 1;
239 return( TRUE );
240}
241
242void set_alarm( int seconds )
243{
244 DWORD ThreadId;
245
246 alarmed = 0;
247 CloseHandle( CreateThread( NULL, 0, TimerProc,
248 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
249}
250
251void m_sleep( int milliseconds )
252{
253 Sleep( milliseconds );
254}
255
256#else
257
258unsigned long get_timer( struct hr_time *val, int reset )
259{
260 unsigned long delta;
261 struct timeval offset;
262 struct _hr_time *t = (struct _hr_time *) val;
263
264 gettimeofday( &offset, NULL );
265
266 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
267 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
268
269 if( reset )
270 {
271 t->start.tv_sec = offset.tv_sec;
272 t->start.tv_usec = offset.tv_usec;
273 }
274
275 return( delta );
276}
277
Paul Bakker49d75672012-09-26 15:22:07 +0000278#if defined(INTEGRITY)
279void m_sleep( int milliseconds )
280{
281 usleep( milliseconds * 1000 );
282}
283
284#else
285
Paul Bakker5121ce52009-01-03 21:22:43 +0000286static void sighandler( int signum )
287{
288 alarmed = 1;
289 signal( signum, sighandler );
290}
291
292void set_alarm( int seconds )
293{
294 alarmed = 0;
295 signal( SIGALRM, sighandler );
296 alarm( seconds );
297}
298
299void m_sleep( int milliseconds )
300{
301 struct timeval tv;
302
303 tv.tv_sec = milliseconds / 1000;
304 tv.tv_usec = milliseconds * 1000;
305
306 select( 0, NULL, NULL, NULL, &tv );
307}
Paul Bakker49d75672012-09-26 15:22:07 +0000308#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310#endif
311
312#endif