blob: cd9f15b92f8f152cacb69c68abf59aea81833afe [file] [log] [blame]
Mikael Starvik21783c92005-07-27 11:44:40 -07001#include <linux/init.h>
2#include <linux/errno.h>
3#include <linux/kernel.h>
4#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Mikael Starvik21783c92005-07-27 11:44:40 -07006#include <linux/types.h>
7#include <asm/ptrace.h>
8#include <asm/uaccess.h>
9
10#define SAMPLE_BUFFER_SIZE 8192
11
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020012static char *sample_buffer;
13static char *sample_buffer_pos;
Mikael Starvik21783c92005-07-27 11:44:40 -070014static int prof_running = 0;
15
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020016void cris_profile_sample(struct pt_regs *regs)
Mikael Starvik21783c92005-07-27 11:44:40 -070017{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070018 if (!prof_running)
19 return;
20
21 if (user_mode(regs))
22 *(unsigned int*)sample_buffer_pos = current->pid;
23 else
24 *(unsigned int*)sample_buffer_pos = 0;
25
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020026 *(unsigned int *)(sample_buffer_pos + 4) = instruction_pointer(regs);
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070027 sample_buffer_pos += 8;
28
29 if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE)
30 sample_buffer_pos = sample_buffer;
Mikael Starvik21783c92005-07-27 11:44:40 -070031}
32
33static ssize_t
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070034read_cris_profile(struct file *file, char __user *buf,
35 size_t count, loff_t *ppos)
Mikael Starvik21783c92005-07-27 11:44:40 -070036{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070037 unsigned long p = *ppos;
Akinobu Mitaed62f772008-07-23 21:28:46 -070038 ssize_t ret;
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070039
Akinobu Mitaed62f772008-07-23 21:28:46 -070040 ret = simple_read_from_buffer(buf, count, ppos, sample_buffer,
41 SAMPLE_BUFFER_SIZE);
42 if (ret < 0)
43 return ret;
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070044
Akinobu Mitaed62f772008-07-23 21:28:46 -070045 memset(sample_buffer + p, 0, ret);
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070046
Akinobu Mitaed62f772008-07-23 21:28:46 -070047 return ret;
Mikael Starvik21783c92005-07-27 11:44:40 -070048}
49
50static ssize_t
51write_cris_profile(struct file *file, const char __user *buf,
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070052 size_t count, loff_t *ppos)
Mikael Starvik21783c92005-07-27 11:44:40 -070053{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070054 sample_buffer_pos = sample_buffer;
55 memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE);
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020056 return count < SAMPLE_BUFFER_SIZE ? count : SAMPLE_BUFFER_SIZE;
Mikael Starvik21783c92005-07-27 11:44:40 -070057}
58
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -080059static const struct file_operations cris_proc_profile_operations = {
Mikael Starvik21783c92005-07-27 11:44:40 -070060 .read = read_cris_profile,
61 .write = write_cris_profile,
Arnd Bergmann6038f372010-08-15 18:52:59 +020062 .llseek = default_llseek,
Mikael Starvik21783c92005-07-27 11:44:40 -070063};
64
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020065static int __init init_cris_profile(void)
Mikael Starvik21783c92005-07-27 11:44:40 -070066{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070067 struct proc_dir_entry *entry;
68
69 sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
70 if (!sample_buffer) {
71 return -ENOMEM;
72 }
73
74 sample_buffer_pos = sample_buffer;
75
Denis V. Lunevc2938192008-04-29 01:02:23 -070076 entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL,
77 &cris_proc_profile_operations);
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070078 if (entry) {
Geert Uytterhoeven254844d2013-05-07 11:20:26 +020079 proc_set_size(entry, SAMPLE_BUFFER_SIZE);
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070080 }
81 prof_running = 1;
82
83 return 0;
Mikael Starvik21783c92005-07-27 11:44:40 -070084}
Mikael Starvik21783c92005-07-27 11:44:40 -070085__initcall(init_cris_profile);
Jesper Nilssonc1c8f552010-08-04 11:44:08 +020086