• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _CPU_H
2 #define _CPU_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <klibc/compiler.h>
7 
8 #if __SIZEOF_POINTER__ == 4
9 #include <i386/cpu.h>
10 #elif __SIZEOF_POINTER__ == 8
11 #include <x86_64/cpu.h>
12 #else
13 #error "unsupported architecture"
14 #endif
15 
16 typedef unsigned long irq_state_t;
17 
irq_state(void)18 static inline irq_state_t irq_state(void)
19 {
20     irq_state_t __st;
21 
22     asm volatile("pushf ; pop %0" : "=rm" (__st) : : "memory");
23     return __st;
24 }
25 
irq_save(void)26 static inline irq_state_t irq_save(void)
27 {
28     irq_state_t __st = irq_state();
29     cli();
30     return __st;
31 }
32 
irq_restore(irq_state_t __st)33 static inline void irq_restore(irq_state_t __st)
34 {
35     asm volatile("push %0 ; popf" : : "rm" (__st) : "memory");
36 }
37 
38 /* Standard macro to see if a specific flag is changeable */
cpu_has_eflag(unsigned long flag)39 static inline __constfunc bool cpu_has_eflag(unsigned long flag)
40 {
41 	unsigned long f0, f1;
42 	asm("pushf ; "
43 	    "pushf ; "
44 	    "pop %0 ; "
45 	    "mov %0,%1 ; "
46 	    "xor %2,%1 ; "
47 	    "push %1 ; "
48 	    "popf ; "
49 	    "pushf ; "
50 	    "pop %1 ; "
51 	    "popf"
52 	    : "=&r" (f0), "=&r" (f1)
53 	    : "ri" (flag));
54 	return !!((f0^f1) & flag);
55 }
56 
57 #endif
58