-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore_proc.c
130 lines (110 loc) · 3.16 KB
/
core_proc.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (C) 2015-2016 Yizhou Shan <shanyizhou@ict.ac.cn>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define pr_fmt(fmt) "CORE PROC: " fmt
#include "core_pmu.h"
#include <asm/uaccess.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/mutex.h>
#include <linux/mutex.h>
#include <linux/percpu.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
const char pmu_proc_format[] = "CPU %2d, NMI times = %lld\n";
static int core_pmu_proc_show(struct seq_file *m, void *v)
{
int cpu;
seq_printf(m, "Counter init value: %lld 0x%llx\n",
(s64)pre_event_init_value, pre_event_init_value);
for_each_online_cpu(cpu) {
seq_printf(m, pmu_proc_format, cpu,
per_cpu(PERCPU_NMI_TIMES, cpu));
}
return 0;
}
static int core_pmu_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, core_pmu_proc_show, NULL);
}
static DEFINE_MUTEX(core_pmu_proc_mutex);
/*
* Control core pmu behaviour in an ugly way. This is the most important
* interface between user and kernel space, we rely on this.
*/
static ssize_t core_pmu_proc_write(struct file *file, const char __user *buf,
size_t count, loff_t *offs)
{
char ctl[2];
if (count !=2 || *offs)
return -EINVAL;
if (copy_from_user(ctl, buf, count))
return -EFAULT;
mutex_lock(&core_pmu_proc_mutex);
switch (ctl[0]) {
case '0': /* 0 = no overflow = disable */
pre_event_init_value = 0;
core_pmu_clear_counter();
core_pmu_clear_msrs();
break;
case '1': /* -32 */
pre_event_init_value = -32;
core_pmu_clear_counter();
core_pmu_start_sampling();
break;
case '2': /* -64 */
pre_event_init_value = -64;
core_pmu_clear_counter();
core_pmu_start_sampling();
break;
case '3': /* -128 */
pre_event_init_value = -128;
core_pmu_clear_counter();
core_pmu_start_sampling();
break;
case '4': /* -256 */
pre_event_init_value = -256;
core_pmu_clear_counter();
core_pmu_start_sampling();
break;
default:
count = -EINVAL;
}
mutex_unlock(&core_pmu_proc_mutex);
return count;
}
const struct file_operations core_pmu_proc_fops = {
.open = core_pmu_proc_open,
.read = seq_read,
.write = core_pmu_proc_write,
.llseek = seq_lseek,
.release = single_release
};
static bool is_proc_registed = false;
int __must_check core_pmu_proc_create(void)
{
if (proc_create("core_pmu", 0644, NULL, &core_pmu_proc_fops)) {
is_proc_registed = true;
return 0;
}
return -ENOENT;
}
void core_pmu_proc_remove(void)
{
if (is_proc_registed)
remove_proc_entry("core_pmu", NULL);
}