Paul Mundt | a99eae5 | 2010-01-12 16:12:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Alignment access counters and corresponding user-space interfaces. |
| 3 | * |
| 4 | * Copyright (C) 2009 ST Microelectronics |
| 5 | * Copyright (C) 2009 - 2010 Paul Mundt |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/seq_file.h> |
| 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <asm/alignment.h> |
| 17 | |
| 18 | static unsigned long se_user; |
| 19 | static unsigned long se_sys; |
| 20 | static unsigned long se_half; |
| 21 | static unsigned long se_word; |
| 22 | static unsigned long se_dword; |
| 23 | static unsigned long se_multi; |
| 24 | /* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not |
| 25 | valid! */ |
| 26 | static int se_usermode = UM_WARN | UM_FIXUP; |
| 27 | /* 0: no warning 1: print a warning message, disabled by default */ |
| 28 | static int se_kernmode_warn; |
| 29 | |
Paul Mundt | 7c1b2c6 | 2010-02-23 11:48:50 +0900 | [diff] [blame^] | 30 | core_param(alignment, se_usermode, int, 0600); |
| 31 | |
Paul Mundt | a99eae5 | 2010-01-12 16:12:25 +0900 | [diff] [blame] | 32 | void inc_unaligned_byte_access(void) |
| 33 | { |
| 34 | se_half++; |
| 35 | } |
| 36 | |
| 37 | void inc_unaligned_word_access(void) |
| 38 | { |
| 39 | se_word++; |
| 40 | } |
| 41 | |
| 42 | void inc_unaligned_dword_access(void) |
| 43 | { |
| 44 | se_dword++; |
| 45 | } |
| 46 | |
| 47 | void inc_unaligned_multi_access(void) |
| 48 | { |
| 49 | se_multi++; |
| 50 | } |
| 51 | |
| 52 | void inc_unaligned_user_access(void) |
| 53 | { |
| 54 | se_user++; |
| 55 | } |
| 56 | |
| 57 | void inc_unaligned_kernel_access(void) |
| 58 | { |
| 59 | se_sys++; |
| 60 | } |
| 61 | |
| 62 | unsigned int unaligned_user_action(void) |
| 63 | { |
| 64 | return se_usermode; |
| 65 | } |
| 66 | |
| 67 | void unaligned_fixups_notify(struct task_struct *tsk, insn_size_t insn, |
| 68 | struct pt_regs *regs) |
| 69 | { |
| 70 | if (user_mode(regs) && (se_usermode & UM_WARN) && printk_ratelimit()) |
| 71 | pr_notice("Fixing up unaligned userspace access " |
| 72 | "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n", |
| 73 | tsk->comm, task_pid_nr(tsk), |
Paul Mundt | 88ea1a4 | 2010-01-19 15:41:50 +0900 | [diff] [blame] | 74 | (void *)instruction_pointer(regs), insn); |
Paul Mundt | a99eae5 | 2010-01-12 16:12:25 +0900 | [diff] [blame] | 75 | else if (se_kernmode_warn && printk_ratelimit()) |
| 76 | pr_notice("Fixing up unaligned kernel access " |
| 77 | "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n", |
| 78 | tsk->comm, task_pid_nr(tsk), |
Paul Mundt | 88ea1a4 | 2010-01-19 15:41:50 +0900 | [diff] [blame] | 79 | (void *)instruction_pointer(regs), insn); |
Paul Mundt | a99eae5 | 2010-01-12 16:12:25 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static const char *se_usermode_action[] = { |
| 83 | "ignored", |
| 84 | "warn", |
| 85 | "fixup", |
| 86 | "fixup+warn", |
| 87 | "signal", |
| 88 | "signal+warn" |
| 89 | }; |
| 90 | |
| 91 | static int alignment_proc_show(struct seq_file *m, void *v) |
| 92 | { |
| 93 | seq_printf(m, "User:\t\t%lu\n", se_user); |
| 94 | seq_printf(m, "System:\t\t%lu\n", se_sys); |
| 95 | seq_printf(m, "Half:\t\t%lu\n", se_half); |
| 96 | seq_printf(m, "Word:\t\t%lu\n", se_word); |
| 97 | seq_printf(m, "DWord:\t\t%lu\n", se_dword); |
| 98 | seq_printf(m, "Multi:\t\t%lu\n", se_multi); |
| 99 | seq_printf(m, "User faults:\t%i (%s)\n", se_usermode, |
| 100 | se_usermode_action[se_usermode]); |
| 101 | seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn, |
| 102 | se_kernmode_warn ? "+warn" : ""); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int alignment_proc_open(struct inode *inode, struct file *file) |
| 107 | { |
| 108 | return single_open(file, alignment_proc_show, NULL); |
| 109 | } |
| 110 | |
| 111 | static ssize_t alignment_proc_write(struct file *file, |
| 112 | const char __user *buffer, size_t count, loff_t *pos) |
| 113 | { |
| 114 | int *data = PDE(file->f_path.dentry->d_inode)->data; |
| 115 | char mode; |
| 116 | |
| 117 | if (count > 0) { |
| 118 | if (get_user(mode, buffer)) |
| 119 | return -EFAULT; |
| 120 | if (mode >= '0' && mode <= '5') |
| 121 | *data = mode - '0'; |
| 122 | } |
| 123 | return count; |
| 124 | } |
| 125 | |
| 126 | static const struct file_operations alignment_proc_fops = { |
| 127 | .owner = THIS_MODULE, |
| 128 | .open = alignment_proc_open, |
| 129 | .read = seq_read, |
| 130 | .llseek = seq_lseek, |
| 131 | .release = single_release, |
| 132 | .write = alignment_proc_write, |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * This needs to be done after sysctl_init, otherwise sys/ will be |
| 137 | * overwritten. Actually, this shouldn't be in sys/ at all since |
| 138 | * it isn't a sysctl, and it doesn't contain sysctl information. |
| 139 | * We now locate it in /proc/cpu/alignment instead. |
| 140 | */ |
| 141 | static int __init alignment_init(void) |
| 142 | { |
| 143 | struct proc_dir_entry *dir, *res; |
| 144 | |
| 145 | dir = proc_mkdir("cpu", NULL); |
| 146 | if (!dir) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir, |
| 150 | &alignment_proc_fops, &se_usermode); |
| 151 | if (!res) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir, |
| 155 | &alignment_proc_fops, &se_kernmode_warn); |
| 156 | if (!res) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | fs_initcall(alignment_init); |