]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/cris/kernel/profile.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / arch / cris / kernel / profile.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
21783c97
MS
2#include <linux/init.h>
3#include <linux/errno.h>
4#include <linux/kernel.h>
5#include <linux/proc_fs.h>
5a0e3ad6 6#include <linux/slab.h>
21783c97
MS
7#include <linux/types.h>
8#include <asm/ptrace.h>
7c0f6ba6 9#include <linux/uaccess.h>
21783c97
MS
10
11#define SAMPLE_BUFFER_SIZE 8192
12
c1c8f558
JN
13static char *sample_buffer;
14static char *sample_buffer_pos;
21783c97
MS
15static int prof_running = 0;
16
c1c8f558 17void cris_profile_sample(struct pt_regs *regs)
21783c97 18{
542401d9
CG
19 if (!prof_running)
20 return;
21
22 if (user_mode(regs))
23 *(unsigned int*)sample_buffer_pos = current->pid;
24 else
25 *(unsigned int*)sample_buffer_pos = 0;
26
c1c8f558 27 *(unsigned int *)(sample_buffer_pos + 4) = instruction_pointer(regs);
542401d9
CG
28 sample_buffer_pos += 8;
29
30 if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE)
31 sample_buffer_pos = sample_buffer;
21783c97
MS
32}
33
34static ssize_t
542401d9
CG
35read_cris_profile(struct file *file, char __user *buf,
36 size_t count, loff_t *ppos)
21783c97 37{
542401d9 38 unsigned long p = *ppos;
ed62f77b 39 ssize_t ret;
542401d9 40
ed62f77b
AM
41 ret = simple_read_from_buffer(buf, count, ppos, sample_buffer,
42 SAMPLE_BUFFER_SIZE);
43 if (ret < 0)
44 return ret;
542401d9 45
ed62f77b 46 memset(sample_buffer + p, 0, ret);
542401d9 47
ed62f77b 48 return ret;
21783c97
MS
49}
50
51static ssize_t
52write_cris_profile(struct file *file, const char __user *buf,
542401d9 53 size_t count, loff_t *ppos)
21783c97 54{
542401d9
CG
55 sample_buffer_pos = sample_buffer;
56 memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE);
c1c8f558 57 return count < SAMPLE_BUFFER_SIZE ? count : SAMPLE_BUFFER_SIZE;
21783c97
MS
58}
59
5dfe4c96 60static const struct file_operations cris_proc_profile_operations = {
21783c97
MS
61 .read = read_cris_profile,
62 .write = write_cris_profile,
6038f373 63 .llseek = default_llseek,
21783c97
MS
64};
65
c1c8f558 66static int __init init_cris_profile(void)
21783c97 67{
542401d9
CG
68 struct proc_dir_entry *entry;
69
70 sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
71 if (!sample_buffer) {
72 return -ENOMEM;
73 }
74
75 sample_buffer_pos = sample_buffer;
76
c293819a
DL
77 entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL,
78 &cris_proc_profile_operations);
542401d9 79 if (entry) {
254844d3 80 proc_set_size(entry, SAMPLE_BUFFER_SIZE);
542401d9
CG
81 }
82 prof_running = 1;
83
84 return 0;
21783c97 85}
21783c97 86__initcall(init_cris_profile);
c1c8f558 87