L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rdtsc.h
Go to the documentation of this file.
1
9/*
10 * (c) 2003-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
11 * Alexander Warg <warg@os.inf.tu-dresden.de>,
12 * Frank Mehnert <fm3@os.inf.tu-dresden.de>,
13 * Jork Löser <jork@os.inf.tu-dresden.de>,
14 * Martin Pohlack <mp26@os.inf.tu-dresden.de>
15 * economic rights: Technische Universität Dresden (Germany)
16 * License: see LICENSE.spdx (in this directory or the directories above)
17 */
18
19#ifndef __l4_rdtsc_h
20#define __l4_rdtsc_h
21
27#include <l4/sys/compiler.h>
28#include <l4/sys/l4int.h>
29#include <l4/sys/kip.h>
30
32
33/* interface */
39extern l4_uint32_t l4_scaler_tsc_to_ns;
40extern l4_uint32_t l4_scaler_tsc_to_us;
41extern l4_uint32_t l4_scaler_ns_to_tsc;
42extern l4_uint32_t l4_scaler_tsc_linux;
43
49l4_rdtsc (void);
50
58
66l4_rdpmc (int ecx);
67
75
82
89
95L4_INLINE void
97
105
111L4_INLINE void
113
119L4_INLINE void
121
130
145l4_tsc_init(l4_kernel_info_t const *kip);
146
152l4_get_hz (void);
153
157
158/* implementation */
159
162{
163 return l4_tsc_init(kip);
164}
165
167l4_rdtsc (void)
168{
170
171 __asm__ __volatile__
172 (" \n\t"
173 ".byte 0x0f, 0x31 \n\t"
174 /*"rdtsc\n\t"*/
175 :
176 "=A" (v)
177 : /* no inputs */
178 );
179
180 return v;
181}
182
183/* the same, but only 32 bit. Useful for smaller differences,
184 needs less cycles. */
187{
188 l4_uint32_t x;
189
190 __asm__ __volatile__ (
191 ".byte 0x0f, 0x31\n\t" // rdtsc
192 : "=a" (x)
193 :
194 : "edx");
195
196 return x;
197}
198
200l4_rdpmc (int ecx)
201{
203
204 __asm__ __volatile__ (
205 "rdpmc \n\t"
206 :
207 "=A" (v)
208 : "c" (ecx)
209 );
210
211 return v;
212}
213
214/* the same, but only 32 bit. Useful for smaller differences,
215 needs less cycles. */
218{
219 l4_uint32_t x;
220
221 __asm__ __volatile__ (
222 "rdpmc \n\t"
223 : "=a" (x)
224 : "c" (ecx)
225 : "edx");
226
227 return x;
228}
229
232{
233 l4_uint32_t dummy;
234 l4_uint64_t ns;
235 __asm__
236 (" \n\t"
237 "movl %%edx, %%ecx \n\t"
238 "mull %3 \n\t"
239 "movl %%ecx, %%eax \n\t"
240 "movl %%edx, %%ecx \n\t"
241 "mull %3 \n\t"
242 "addl %%ecx, %%eax \n\t"
243 "adcl $0, %%edx \n\t"
244 "shld $5, %%eax, %%edx \n\t"
245 "shll $5, %%eax \n\t"
246 :"=A" (ns),
247 "=&c" (dummy)
248 :"0" (tsc),
249 "g" (l4_scaler_tsc_to_ns)
250 );
251 return ns;
252}
253
256{
257 l4_uint32_t dummy;
258 l4_uint64_t us;
259 __asm__
260 (" \n\t"
261 "movl %%edx, %%ecx \n\t"
262 "mull %3 \n\t"
263 "movl %%ecx, %%eax \n\t"
264 "movl %%edx, %%ecx \n\t"
265 "mull %3 \n\t"
266 "addl %%ecx, %%eax \n\t"
267 "adcl $0, %%edx \n\t"
268 :"=A" (us),
269 "=&c" (dummy)
270 :"0" (tsc),
271 "g" (l4_scaler_tsc_to_us)
272 );
273 return us;
274}
275
276L4_INLINE void
278{
279 l4_uint32_t dummy;
280 __asm__
281 (" \n\t"
282 "movl %%edx, %%ecx \n\t"
283 "mull %4 \n\t"
284 "movl %%ecx, %%eax \n\t"
285 "movl %%edx, %%ecx \n\t"
286 "mull %4 \n\t"
287 "addl %%ecx, %%eax \n\t"
288 "adcl $0, %%edx \n\t"
289 "movl $1000000000, %%ecx \n\t"
290 "shld $5, %%eax, %%edx \n\t"
291 "shll $5, %%eax \n\t"
292 "divl %%ecx \n\t"
293 :"=a" (*s), "=d" (*ns), "=&c" (dummy)
294 : "A" (tsc), "g" (l4_scaler_tsc_to_ns)
295 );
296}
297
300{
301 l4_uint32_t dummy;
302 l4_cpu_time_t tsc;
303 __asm__
304 (" \n\t"
305 "movl %%edx, %%ecx \n\t"
306 "mull %3 \n\t"
307 "movl %%ecx, %%eax \n\t"
308 "movl %%edx, %%ecx \n\t"
309 "mull %3 \n\t"
310 "addl %%ecx, %%eax \n\t"
311 "adcl $0, %%edx \n\t"
312 "shld $5, %%eax, %%edx \n\t"
313 "shll $5, %%eax \n\t"
314 :"=A" (tsc),
315 "=&c" (dummy)
316 :"0" (ns),
317 "g" (l4_scaler_ns_to_tsc)
318 );
319 return tsc;
320}
321
322L4_INLINE void
324{
325 l4_cpu_time_t stop = l4_rdtsc();
326 stop += l4_ns_to_tsc(ns);
327
328 while (l4_rdtsc() < stop)
329 ;
330}
331
332L4_INLINE void
334{
335 l4_cpu_time_t stop = l4_rdtsc ();
336 stop += l4_ns_to_tsc(us*1000ULL);
337
338 while (l4_rdtsc() < stop)
339 ;
340}
341
342#endif /* __l4_rdtsc_h */
343
L4 compiler related defines.
l4_uint64_t l4_cpu_time_t
CPU clock type.
Definition l4int.h:47
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
#define __END_DECLS
End section with C types and functions.
Definition compiler.h:167
#define L4_CV
Define calling convention.
Definition linkage.h:33
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51
#define __BEGIN_DECLS
Start section with C types and functions.
Definition compiler.h:164
void l4_tsc_to_s_and_ns(l4_cpu_time_t tsc, l4_uint32_t *s, l4_uint32_t *ns)
Convert timestamp to s.ns value.
Definition rdtsc.h:233
l4_uint32_t l4_get_hz(void)
Get CPU frequency in Hz.
l4_uint32_t l4_tsc_init(l4_kernel_info_t const *kip)
Initialize scaler for TSC calibrations from the kernel.
l4_uint64_t l4_tsc_to_ns(l4_cpu_time_t tsc)
Convert timestamp to ns value.
Definition rdtsc.h:205
l4_uint64_t l4_tsc_to_us(l4_cpu_time_t tsc)
Convert timestamp into micro seconds value.
Definition rdtsc.h:219
l4_uint32_t l4_calibrate_tsc(l4_kernel_info_t const *kip)
Determine scalers for timestamp calculations.
Definition rdtsc.h:159
l4_cpu_time_t l4_rdtsc(void)
Read current value of CPU-internal timestamp counter.
Definition rdtsc.h:165
l4_cpu_time_t l4_ns_to_tsc(l4_uint64_t ns)
Convert nano seconds into CPU ticks.
Definition rdtsc.h:248
void l4_busy_wait_us(l4_uint64_t us)
Wait busy for a small amount of time.
Definition rdtsc.h:272
l4_uint32_t l4_rdpmc_32(int ecx)
Return the least significant 32 bit of a performance counter.
Definition rdtsc.h:195
l4_uint64_t l4_rdpmc(int ecx)
Return current value of CPU-internal performance measurement counter.
Definition rdtsc.h:175
void l4_busy_wait_ns(l4_uint64_t ns)
Wait busy for a small amount of time.
Definition rdtsc.h:262
l4_uint32_t l4_rdtsc_32(void)
Read the lest significant 32 bit of the TSC.
Definition rdtsc.h:185
L4 Kernel Interface Page.
Definition __kip-32bit.h:28
Kernel Info Page access functions.