3 * \brief CPU related functions
5 * \author Frank Mehnert <fm3@os.inf.tu-dresden.de> */
8 * (c) 2004-2009 Author(s)
9 * economic rights: Technische Universität Dresden (Germany)
10 * This file is part of TUD:OS and distributed under the terms of the
11 * GNU Lesser General Public License 2.1.
12 * Please see the COPYING-LGPL-2.1 file for details.
15 #ifndef __L4_UTIL_CPU_H
16 #define __L4_UTIL_CPU_H
18 #include <l4/sys/compiler.h>
23 * \defgroup l4util_cpu CPU related functions
29 * Check whether the CPU supports the "cpuid" instruction.
31 * \return 1 if it has, 0 if it has not
33 L4_INLINE int l4util_cpu_has_cpuid(void);
36 * Returns the CPU capabilities if the "cpuid" instruction is available.
38 * \return CPU capabilities if the "cpuid" instruction is available,
39 * 0 if the "cpuid" instruction is not supported.
41 L4_INLINE unsigned int l4util_cpu_capabilities(void);
44 * Returns the CPU capabilities.
46 * \return CPU capabilities.
48 L4_INLINE unsigned int l4util_cpu_capabilities_nocheck(void);
51 * Generic CPUID access function.
54 l4util_cpu_cpuid(unsigned long mode,
55 unsigned long *eax, unsigned long *ebx,
56 unsigned long *ecx, unsigned long *edx);
60 l4util_cpu_pause(void)
62 __asm__ __volatile__ ("rep; nop");
66 l4util_cpu_has_cpuid(void)
70 asm volatile("pushl %%ebx \t\n"
72 "popl %%eax \t\n" /* get eflags */
73 "movl %%eax, %%ebx \t\n" /* save it */
74 "xorl $0x200000, %%eax \t\n" /* toggle ID bit */
76 "popfl \t\n" /* set again */
78 "popl %%eax \t\n" /* get it again */
79 "xorl %%eax, %%ebx \t\n"
81 "popfl \t\n" /* restore saved flags */
87 return eax & 0x200000;
91 l4util_cpu_cpuid(unsigned long mode,
92 unsigned long *eax, unsigned long *ebx,
93 unsigned long *ecx, unsigned long *edx)
95 asm volatile("pushl %%ebx \t\n"
97 "mov %%ebx, %[b] \t\n"
107 L4_INLINE unsigned int
108 l4util_cpu_capabilities_nocheck(void)
110 unsigned long dummy, capability;
112 /* get CPU capabilities */
113 l4util_cpu_cpuid(1, &dummy, &dummy, &dummy, &capability);
118 L4_INLINE unsigned int
119 l4util_cpu_capabilities(void)
121 if (!l4util_cpu_has_cpuid())
122 return 0; /* CPU has not cpuid instruction */
124 return l4util_cpu_capabilities_nocheck();