aboutsummaryrefslogtreecommitdiff
path: root/com32/include/sys/cpu.h
blob: 76c45da030ccc4b758ee833d10b666663818580c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef _CPU_H
#define _CPU_H

#include <stdbool.h>
#include <stdint.h>
#include <klibc/compiler.h>

#if __SIZEOF_POINTER__ == 4
#include <i386/cpu.h>
#elif __SIZEOF_POINTER__ == 8
#include <x86_64/cpu.h>
#else 
#error "unsupported architecture"
#endif

typedef unsigned long irq_state_t;

static inline irq_state_t irq_state(void)
{
    irq_state_t __st;

    asm volatile("pushf ; pop %0" : "=rm" (__st) : : "memory");
    return __st;
}

static inline irq_state_t irq_save(void)
{
    irq_state_t __st = irq_state();
    cli();
    return __st;
}

static inline void irq_restore(irq_state_t __st)
{
    asm volatile("push %0 ; popf" : : "rm" (__st) : "memory");
}

/* Standard macro to see if a specific flag is changeable */
static inline __constfunc bool cpu_has_eflag(unsigned long flag)
{
	unsigned long f0, f1;
	asm("pushf ; "
	    "pushf ; "
	    "pop %0 ; "
	    "mov %0,%1 ; "
	    "xor %2,%1 ; "
	    "push %1 ; "
	    "popf ; "
	    "pushf ; "
	    "pop %1 ; "
	    "popf"
	    : "=&r" (f0), "=&r" (f1)
	    : "ri" (flag));
	return !!((f0^f1) & flag);
}

#endif