blob: aad0a9e5991a3e358685726cb5fed98afa4de8ed [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>
5#include <linux/types.h>
6#include <asm/ptrace.h>
7#include <asm/uaccess.h>
8
9#define SAMPLE_BUFFER_SIZE 8192
10
11static char* sample_buffer;
12static char* sample_buffer_pos;
13static int prof_running = 0;
14
15void
16cris_profile_sample(struct pt_regs* regs)
17{
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
26 *(unsigned int*)(sample_buffer_pos + 4) = instruction_pointer(regs);
27 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;
38
39 if (p > SAMPLE_BUFFER_SIZE)
40 return 0;
41
42 if (p + count > SAMPLE_BUFFER_SIZE)
43 count = SAMPLE_BUFFER_SIZE - p;
44 if (copy_to_user(buf, sample_buffer + p,count))
Mikael Starvik21783c92005-07-27 11:44:40 -070045 return -EFAULT;
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070046
47 memset(sample_buffer + p, 0, count);
48 *ppos += count;
49
50 return count;
Mikael Starvik21783c92005-07-27 11:44:40 -070051}
52
53static ssize_t
54write_cris_profile(struct file *file, const char __user *buf,
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070055 size_t count, loff_t *ppos)
Mikael Starvik21783c92005-07-27 11:44:40 -070056{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070057 sample_buffer_pos = sample_buffer;
58 memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE);
Mikael Starvik21783c92005-07-27 11:44:40 -070059}
60
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -080061static const struct file_operations cris_proc_profile_operations = {
Mikael Starvik21783c92005-07-27 11:44:40 -070062 .read = read_cris_profile,
63 .write = write_cris_profile,
64};
65
66static int
67__init init_cris_profile(void)
68{
Cyrill Gorcunov542401d2007-05-06 14:50:56 -070069 struct proc_dir_entry *entry;
70
71 sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
72 if (!sample_buffer) {
73 return -ENOMEM;
74 }
75
76 sample_buffer_pos = sample_buffer;
77
78 entry = create_proc_entry("system_profile", S_IWUSR | S_IRUGO, NULL);
79 if (entry) {
80 entry->proc_fops = &cris_proc_profile_operations;
81 entry->size = SAMPLE_BUFFER_SIZE;
82 }
83 prof_running = 1;
84
85 return 0;
Mikael Starvik21783c92005-07-27 11:44:40 -070086}
87
88__initcall(init_cris_profile);