summaryrefslogtreecommitdiff
path: root/com32/include/sys/cpu.h
blob: dcc4abfa55a36c66827cc04e65292086cf6effda (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef _CPU_H
#define _CPU_H

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

static inline uint64_t rdtsc(void)
{
  uint64_t v;
  asm volatile("rdtsc" : "=A" (v));
  return v;
}

static inline uint32_t rdtscl(void)
{
  uint32_t v;
  asm volatile("rdtsc" : "=a" (v) : : "edx");
  return v;
}

static inline void cpuid_count(uint32_t op, uint32_t cnt,
			       uint32_t *eax, uint32_t *ebx,
			       uint32_t *ecx, uint32_t *edx)
{
  asm("cpuid"
      : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
      : "a" (op), "c" (cnt));
}
static inline void cpuid(uint32_t op, uint32_t *eax, uint32_t *ebx,
			 uint32_t *ecx, uint32_t *edx)
{
  cpuid_count(op, 0, eax, ebx, ecx, edx);
}
static inline __constfunc uint32_t cpuid_eax(uint32_t level)
{
  uint32_t v;

  asm("cpuid" : "=a" (v) : "a" (level) : "ebx", "ecx", "edx");
  return v;
}
static inline __constfunc uint32_t cpuid_ebx(uint32_t level)
{
  uint32_t v;

  asm("cpuid" : "=b" (v),  "+a" (level) : : "ecx", "edx");
  return v;
}
static inline __constfunc uint32_t cpuid_ecx(uint32_t level)
{
  uint32_t v;

  asm("cpuid" : "=c" (v), "+a" (level) : : "ebx", "edx");
  return v;
}
static inline __constfunc uint32_t cpuid_edx(uint32_t level)
{
  uint32_t v;

  asm("cpuid" : "=d" (v), "+a" (level) : : "ebx", "ecx");
  return v;
}

/* Standard macro to see if a specific flag is changeable */
static inline __constfunc bool cpu_has_eflag(uint32_t flag)
{
  uint32_t f1, f2;
  
  asm("pushfl\n\t"
      "pushfl\n\t"
      "popl %0\n\t"
      "movl %0,%1\n\t"
      "xorl %2,%0\n\t"
      "pushl %0\n\t"
      "popfl\n\t"
      "pushfl\n\t"
      "popl %0\n\t"
      "popfl\n\t"
      : "=&r" (f1), "=&r" (f2)
      : "ir" (flag));
  
  return ((f1^f2) & flag) != 0;
}

static inline uint64_t rdmsr(uint32_t msr)
{
  uint64_t v;

  asm volatile("rdmsr" : "=A" (v) : "c" (msr));
  return v;
}
static inline void wrmsr(uint64_t v, uint32_t msr)
{
  asm volatile("wrmsr" : : "A" (v), "c" (msr));
}

static inline void cpu_relax(void)
{
  asm volatile("rep ; nop");
}

/* These are local cli/sti; not SMP-safe!!! */

static inline void cli(void)
{
  asm volatile("cli");
}

static inline void sti(void)
{
  asm volatile("sti");
}

#endif