blob: 00fb9e3f057c972cb09a07920b468a59ac505a39 [file] [log] [blame]
Paul Mundta99eae52010-01-12 16:12:25 +09001/*
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
18static unsigned long se_user;
19static unsigned long se_sys;
20static unsigned long se_half;
21static unsigned long se_word;
22static unsigned long se_dword;
23static unsigned long se_multi;
24/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
25 valid! */
26static int se_usermode = UM_WARN | UM_FIXUP;
27/* 0: no warning 1: print a warning message, disabled by default */
28static int se_kernmode_warn;
29
Paul Mundt7c1b2c62010-02-23 11:48:50 +090030core_param(alignment, se_usermode, int, 0600);
31
Paul Mundta99eae52010-01-12 16:12:25 +090032void inc_unaligned_byte_access(void)
33{
34 se_half++;
35}
36
37void inc_unaligned_word_access(void)
38{
39 se_word++;
40}
41
42void inc_unaligned_dword_access(void)
43{
44 se_dword++;
45}
46
47void inc_unaligned_multi_access(void)
48{
49 se_multi++;
50}
51
52void inc_unaligned_user_access(void)
53{
54 se_user++;
55}
56
57void inc_unaligned_kernel_access(void)
58{
59 se_sys++;
60}
61
62unsigned int unaligned_user_action(void)
63{
64 return se_usermode;
65}
66
67void 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 Mundt88ea1a42010-01-19 15:41:50 +090074 (void *)instruction_pointer(regs), insn);
Paul Mundta99eae52010-01-12 16:12:25 +090075 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 Mundt88ea1a42010-01-19 15:41:50 +090079 (void *)instruction_pointer(regs), insn);
Paul Mundta99eae52010-01-12 16:12:25 +090080}
81
82static const char *se_usermode_action[] = {
83 "ignored",
84 "warn",
85 "fixup",
86 "fixup+warn",
87 "signal",
88 "signal+warn"
89};
90
91static 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
106static int alignment_proc_open(struct inode *inode, struct file *file)
107{
108 return single_open(file, alignment_proc_show, NULL);
109}
110
111static 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
126static 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 */
141static 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}
161fs_initcall(alignment_init);