summaryrefslogtreecommitdiffstats
path: root/src/asm.h
blob: 3498f2c8f94ffdc52571d3e7309e69ae7d6aa0cd (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
86
87
88
89
90
91
92
93
#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);

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

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

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

	return ((uint64_t) hi << 32) | 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"
		::
	);
}

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

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

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

void
cachepc_victim(void *p)
{
	cachepc_mfence();
	cachepc_readq(p);
}