summaryrefslogtreecommitdiffstats
path: root/kmod/asm.h
blob: 35f803b40a36367d174df9a4d0dae260870ffb72 (plain) (blame)
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
#pragma once

#include <linux/kernel.h>

#define CPUID_AFFECTED_REGS "rax", "rbx", "rcx", "rdx"

__attribute__((always_inline))
static inline uint64_t cachepc_readpmc(uint64_t event);

__attribute__((always_inline))
static inline void cachepc_cpuid(void);

__attribute__((always_inline))
static inline void cachepc_lfence(void);

__attribute__((always_inline))
static inline void cachepc_sfence(void);

__attribute__((always_inline))
static inline void cachepc_mfence(void);

__attribute__((always_inline))
static inline void cachepc_readq(void *p);

uint64_t
cachepc_readpmc(uint64_t event)
{
	uint32_t lo, hi;

	event = 0xC0010201 + 2 * event;

	asm volatile (
		"rdmsr"
		: "=a" (lo), "=d" (hi)
		: "c"(event)
	);

	return ((uint64_t) hi << 32) | (uint64_t) lo;
}

void
cachepc_cpuid(void)
{
	asm volatile(
		"mov $0x80000005, %%eax\n\t"
		"cpuid\n\t"
		::: CPUID_AFFECTED_REGS
	);
}

void
cachepc_lfence(void)
{
	asm volatile(
		"lfence\n\t"
		::: "memory"
	);
}

void
cachepc_sfence(void)
{
	asm volatile(
		"sfence\n\t"
		::: "memory"
	);
}

void
cachepc_mfence(void)
{
	asm volatile(
		"mfence\n\t"
		::: "memory"
	);
}

void
cachepc_readq(void *p)
{
	asm volatile (
		"movq (%0), %%r10\n\t"
		: : "r" (p) : "r10"
	);
}