Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 2 | #include <linux/init.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/proc_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 7 | #include <linux/types.h> |
| 8 | #include <asm/ptrace.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 10 | |
| 11 | #define SAMPLE_BUFFER_SIZE 8192 |
| 12 | |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 13 | static char *sample_buffer; |
| 14 | static char *sample_buffer_pos; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 15 | static int prof_running = 0; |
| 16 | |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 17 | void cris_profile_sample(struct pt_regs *regs) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 18 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 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 | |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 27 | *(unsigned int *)(sample_buffer_pos + 4) = instruction_pointer(regs); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 28 | sample_buffer_pos += 8; |
| 29 | |
| 30 | if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE) |
| 31 | sample_buffer_pos = sample_buffer; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static ssize_t |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 35 | read_cris_profile(struct file *file, char __user *buf, |
| 36 | size_t count, loff_t *ppos) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 37 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 38 | unsigned long p = *ppos; |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 39 | ssize_t ret; |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 40 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 41 | ret = simple_read_from_buffer(buf, count, ppos, sample_buffer, |
| 42 | SAMPLE_BUFFER_SIZE); |
| 43 | if (ret < 0) |
| 44 | return ret; |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 45 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 46 | memset(sample_buffer + p, 0, ret); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 47 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 48 | return ret; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static ssize_t |
| 52 | write_cris_profile(struct file *file, const char __user *buf, |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 53 | size_t count, loff_t *ppos) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 54 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 55 | sample_buffer_pos = sample_buffer; |
| 56 | memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE); |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 57 | return count < SAMPLE_BUFFER_SIZE ? count : SAMPLE_BUFFER_SIZE; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 60 | static const struct file_operations cris_proc_profile_operations = { |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 61 | .read = read_cris_profile, |
| 62 | .write = write_cris_profile, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 63 | .llseek = default_llseek, |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 66 | static int __init init_cris_profile(void) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 67 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 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 | |
Denis V. Lunev | c293819 | 2008-04-29 01:02:23 -0700 | [diff] [blame] | 77 | entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL, |
| 78 | &cris_proc_profile_operations); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 79 | if (entry) { |
Geert Uytterhoeven | 254844d | 2013-05-07 11:20:26 +0200 | [diff] [blame] | 80 | proc_set_size(entry, SAMPLE_BUFFER_SIZE); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 81 | } |
| 82 | prof_running = 1; |
| 83 | |
| 84 | return 0; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 85 | } |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 86 | __initcall(init_cris_profile); |
Jesper Nilsson | c1c8f55 | 2010-08-04 11:44:08 +0200 | [diff] [blame] | 87 | |