L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
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 * This file is part of TUD:OS and distributed under the terms of the
17 * GNU Lesser General Public License 2.1.
18 * Please see the COPYING-LGPL-2.1 file for details.
19 */
20
21#ifndef __l4_rdtsc_h
22#define __l4_rdtsc_h
23
29#include <l4/sys/compiler.h>
30#include <l4/sys/l4int.h>
31#include <l4/sys/kip.h>
32
34
35/* interface */
41extern l4_uint32_t l4_scaler_tsc_to_ns;
42extern l4_uint32_t l4_scaler_tsc_to_us;
43extern l4_uint32_t l4_scaler_ns_to_tsc;
44extern l4_uint32_t l4_scaler_tsc_linux;
45
51l4_rdtsc (void);
52
60
68l4_rdpmc (int ecx);
69
77
84
91
97L4_INLINE void
99
107
113L4_INLINE void
115
121L4_INLINE void
123
132
147l4_tsc_init(l4_kernel_info_t const *kip);
148
154l4_get_hz (void);
155
159
160/* implementation */
161
164{
165 return l4_tsc_init(kip);
166}
167
169l4_rdtsc (void)
170{
172
173 __asm__ __volatile__
174 (" \n\t"
175 ".byte 0x0f, 0x31 \n\t"
176 /*"rdtsc\n\t"*/
177 :
178 "=A" (v)
179 : /* no inputs */
180 );
181
182 return v;
183}
184
185/* the same, but only 32 bit. Useful for smaller differences,
186 needs less cycles. */
189{
190 l4_uint32_t x;
191
192 __asm__ __volatile__ (
193 ".byte 0x0f, 0x31\n\t" // rdtsc
194 : "=a" (x)
195 :
196 : "edx");
197
198 return x;
199}
200
202l4_rdpmc (int ecx)
203{
205
206 __asm__ __volatile__ (
207 "rdpmc \n\t"
208 :
209 "=A" (v)
210 : "c" (ecx)
211 );
212
213 return v;
214}
215
216/* the same, but only 32 bit. Useful for smaller differences,
217 needs less cycles. */
220{
221 l4_uint32_t x;
222
223 __asm__ __volatile__ (
224 "rdpmc \n\t"
225 : "=a" (x)
226 : "c" (ecx)
227 : "edx");
228
229 return x;
230}
231
234{
235 l4_uint32_t dummy;
236 l4_uint64_t ns;
237 __asm__
238 (" \n\t"
239 "movl %%edx, %%ecx \n\t"
240 "mull %3 \n\t"
241 "movl %%ecx, %%eax \n\t"
242 "movl %%edx, %%ecx \n\t"
243 "mull %3 \n\t"
244 "addl %%ecx, %%eax \n\t"
245 "adcl $0, %%edx \n\t"
246 "shld $5, %%eax, %%edx \n\t"
247 "shll $5, %%eax \n\t"
248 :"=A" (ns),
249 "=&c" (dummy)
250 :"0" (tsc),
251 "g" (l4_scaler_tsc_to_ns)
252 );
253 return ns;
254}
255
258{
259 l4_uint32_t dummy;
260 l4_uint64_t us;
261 __asm__
262 (" \n\t"
263 "movl %%edx, %%ecx \n\t"
264 "mull %3 \n\t"
265 "movl %%ecx, %%eax \n\t"
266 "movl %%edx, %%ecx \n\t"
267 "mull %3 \n\t"
268 "addl %%ecx, %%eax \n\t"
269 "adcl $0, %%edx \n\t"
270 :"=A" (us),
271 "=&c" (dummy)
272 :"0" (tsc),
273 "g" (l4_scaler_tsc_to_us)
274 );
275 return us;
276}
277
278L4_INLINE void
280{
281 l4_uint32_t dummy;
282 __asm__
283 (" \n\t"
284 "movl %%edx, %%ecx \n\t"
285 "mull %4 \n\t"
286 "movl %%ecx, %%eax \n\t"
287 "movl %%edx, %%ecx \n\t"
288 "mull %4 \n\t"
289 "addl %%ecx, %%eax \n\t"
290 "adcl $0, %%edx \n\t"
291 "movl $1000000000, %%ecx \n\t"
292 "shld $5, %%eax, %%edx \n\t"
293 "shll $5, %%eax \n\t"
294 "divl %%ecx \n\t"
295 :"=a" (*s), "=d" (*ns), "=&c" (dummy)
296 : "A" (tsc), "g" (l4_scaler_tsc_to_ns)
297 );
298}
299
302{
303 l4_uint32_t dummy;
304 l4_cpu_time_t tsc;
305 __asm__
306 (" \n\t"
307 "movl %%edx, %%ecx \n\t"
308 "mull %3 \n\t"
309 "movl %%ecx, %%eax \n\t"
310 "movl %%edx, %%ecx \n\t"
311 "mull %3 \n\t"
312 "addl %%ecx, %%eax \n\t"
313 "adcl $0, %%edx \n\t"
314 "shld $5, %%eax, %%edx \n\t"
315 "shll $5, %%eax \n\t"
316 :"=A" (tsc),
317 "=&c" (dummy)
318 :"0" (ns),
319 "g" (l4_scaler_ns_to_tsc)
320 );
321 return tsc;
322}
323
324L4_INLINE void
326{
327 l4_cpu_time_t stop = l4_rdtsc();
328 stop += l4_ns_to_tsc(ns);
329
330 while (l4_rdtsc() < stop)
331 ;
332}
333
334L4_INLINE void
336{
337 l4_cpu_time_t stop = l4_rdtsc ();
338 stop += l4_ns_to_tsc(us*1000ULL);
339
340 while (l4_rdtsc() < stop)
341 ;
342}
343
344#endif /* __l4_rdtsc_h */
345
L4 compiler related defines.
l4_uint64_t l4_cpu_time_t
CPU clock type.
Definition l4int.h:58
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:40
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:42
#define L4_CV
Define calling convention.
Definition linkage.h:44
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:62
#define EXTERN_C_BEGIN
Start section with C types and functions.
Definition compiler.h:192
#define EXTERN_C_END
End section with C types and functions.
Definition compiler.h:193
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:235
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:207
l4_uint64_t l4_tsc_to_us(l4_cpu_time_t tsc)
Convert timestamp into micro seconds value.
Definition rdtsc.h:221
l4_uint32_t l4_calibrate_tsc(l4_kernel_info_t const *kip)
Determine scalers for timestamp calculations.
Definition rdtsc.h:161
l4_cpu_time_t l4_rdtsc(void)
Read current value of CPU-internal timestamp counter.
Definition rdtsc.h:167
l4_cpu_time_t l4_ns_to_tsc(l4_uint64_t ns)
Convert nano seconds into CPU ticks.
Definition rdtsc.h:250
void l4_busy_wait_us(l4_uint64_t us)
Wait busy for a small amount of time.
Definition rdtsc.h:274
l4_uint32_t l4_rdpmc_32(int ecx)
Return the least significant 32 bit of a performance counter.
Definition rdtsc.h:197
l4_uint64_t l4_rdpmc(int ecx)
Return current value of CPU-internal performance measurement counter.
Definition rdtsc.h:177
void l4_busy_wait_ns(l4_uint64_t ns)
Wait busy for a small amount of time.
Definition rdtsc.h:264
l4_uint32_t l4_rdtsc_32(void)
Read the lest significant 32 bit of the TSC.
Definition rdtsc.h:187
L4 Kernel Interface Page.
Definition __kip-32bit.h:39
Kernel Info Page access functions.