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 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
13 * economic rights: Technische Universität Dresden (Germany)
14 * License: see LICENSE.spdx (in this directory or the directories above)
15 */
16
17#ifndef __l4_rdtsc_h
18#define __l4_rdtsc_h
19
25#include <l4/sys/compiler.h>
26#include <l4/sys/l4int.h>
27#include <l4/sys/kip.h>
28
30
31/* interface */
37extern l4_uint32_t l4_scaler_tsc_to_ns;
38extern l4_uint32_t l4_scaler_tsc_to_us;
39extern l4_uint32_t l4_scaler_ns_to_tsc;
40extern l4_uint32_t l4_scaler_tsc_linux;
41
47l4_rdtsc (void);
48
56
64l4_rdpmc (int ecx);
65
73
80
87
93L4_INLINE void
95
103
109L4_INLINE void
111
117L4_INLINE void
119
128
144
151
155
156/* implementation */
157
160{
161 return l4_tsc_init(kip);
162}
163
166{
167 l4_umword_t lo, hi;
168
169 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
170
171 return ((l4_cpu_time_t)hi << 32) | lo;
172}
173
175l4_rdpmc (int ecx)
176{
177 l4_umword_t lo, hi;
178
179 __asm__ __volatile__ ("rdpmc" : "=a"(lo), "=d"(hi) : "c"(ecx));
180
181 return ((l4_cpu_time_t)hi << 32) | lo;
182}
183
186{
187 l4_umword_t lo, hi;
188
189 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
190
191 return lo;
192}
193
196{
197 l4_umword_t lo, hi;
198
199 __asm__ __volatile__ ("rdpmc" : "=a"(lo), "=d"(hi) : "c"(ecx));
200
201 return lo;
202}
203
206{
207 l4_uint64_t ns, dummy;
208 __asm__
209 (" \n\t"
210 "mulq %3 \n\t"
211 "shrd $27, %%rdx, %%rax \n\t"
212 :"=a" (ns), "=d"(dummy)
213 :"a" (tsc), "r" ((l4_uint64_t)l4_scaler_tsc_to_ns)
214 );
215 return ns;
216}
217
220{
221 l4_uint64_t ns, dummy;
222 __asm__
223 (" \n\t"
224 "mulq %3 \n\t"
225 "shrd $32, %%rdx, %%rax \n\t"
226 :"=a" (ns), "=d" (dummy)
227 :"a" (tsc), "r" ((l4_uint64_t)l4_scaler_tsc_to_us)
228 );
229 return ns;
230}
231
232L4_INLINE void
234{
235 __asm__
236 (" \n\t"
237 "mulq %3 \n\t"
238 "shrd $27, %%rdx, %%rax \n\t"
239 "xorq %%rdx, %%rdx \n\t"
240 "divq %4 \n\t"
241 :"=a" (*s), "=&d" (*ns)
242 : "a" (tsc), "r" ((l4_uint64_t)l4_scaler_tsc_to_ns),
243 "rm"(1000000000ULL)
244 );
245}
246
249{
250 l4_uint64_t tsc, dummy;
251 __asm__
252 (" \n\t"
253 "mulq %3 \n\t"
254 "shrd $27, %%rdx, %%rax \n\t"
255 :"=a" (tsc), "=d" (dummy)
256 :"a" (ns), "r" ((l4_uint64_t)l4_scaler_ns_to_tsc)
257 );
258 return tsc;
259}
260
261L4_INLINE void
263{
264 l4_cpu_time_t stop = l4_rdtsc();
265 stop += l4_ns_to_tsc(ns);
266
267 while (l4_rdtsc() < stop)
268 ;
269}
270
271L4_INLINE void
273{
274 l4_cpu_time_t stop = l4_rdtsc ();
275 stop += l4_ns_to_tsc(us*1000ULL);
276
277 while (l4_rdtsc() < stop)
278 ;
279}
280
281#endif /* __l4_rdtsc_h */
282
L4 compiler related defines.
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
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.