xref: /openbmc/linux/arch/sh/mm/alignment.c (revision a09d2831)
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 
30 void inc_unaligned_byte_access(void)
31 {
32 	se_half++;
33 }
34 
35 void inc_unaligned_word_access(void)
36 {
37 	se_word++;
38 }
39 
40 void inc_unaligned_dword_access(void)
41 {
42 	se_dword++;
43 }
44 
45 void inc_unaligned_multi_access(void)
46 {
47 	se_multi++;
48 }
49 
50 void inc_unaligned_user_access(void)
51 {
52 	se_user++;
53 }
54 
55 void inc_unaligned_kernel_access(void)
56 {
57 	se_sys++;
58 }
59 
60 unsigned int unaligned_user_action(void)
61 {
62 	return se_usermode;
63 }
64 
65 void unaligned_fixups_notify(struct task_struct *tsk, insn_size_t insn,
66 			     struct pt_regs *regs)
67 {
68 	if (user_mode(regs) && (se_usermode & UM_WARN) && printk_ratelimit())
69 		pr_notice("Fixing up unaligned userspace access "
70 			  "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
71 			  tsk->comm, task_pid_nr(tsk),
72 			  (void *)regs->pc, insn);
73 	else if (se_kernmode_warn && printk_ratelimit())
74 		pr_notice("Fixing up unaligned kernel access "
75 			  "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
76 			  tsk->comm, task_pid_nr(tsk),
77 			  (void *)regs->pc, insn);
78 }
79 
80 static const char *se_usermode_action[] = {
81 	"ignored",
82 	"warn",
83 	"fixup",
84 	"fixup+warn",
85 	"signal",
86 	"signal+warn"
87 };
88 
89 static int alignment_proc_show(struct seq_file *m, void *v)
90 {
91 	seq_printf(m, "User:\t\t%lu\n", se_user);
92 	seq_printf(m, "System:\t\t%lu\n", se_sys);
93 	seq_printf(m, "Half:\t\t%lu\n", se_half);
94 	seq_printf(m, "Word:\t\t%lu\n", se_word);
95 	seq_printf(m, "DWord:\t\t%lu\n", se_dword);
96 	seq_printf(m, "Multi:\t\t%lu\n", se_multi);
97 	seq_printf(m, "User faults:\t%i (%s)\n", se_usermode,
98 			se_usermode_action[se_usermode]);
99 	seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
100 			se_kernmode_warn ? "+warn" : "");
101 	return 0;
102 }
103 
104 static int alignment_proc_open(struct inode *inode, struct file *file)
105 {
106 	return single_open(file, alignment_proc_show, NULL);
107 }
108 
109 static ssize_t alignment_proc_write(struct file *file,
110 		const char __user *buffer, size_t count, loff_t *pos)
111 {
112 	int *data = PDE(file->f_path.dentry->d_inode)->data;
113 	char mode;
114 
115 	if (count > 0) {
116 		if (get_user(mode, buffer))
117 			return -EFAULT;
118 		if (mode >= '0' && mode <= '5')
119 			*data = mode - '0';
120 	}
121 	return count;
122 }
123 
124 static const struct file_operations alignment_proc_fops = {
125 	.owner		= THIS_MODULE,
126 	.open		= alignment_proc_open,
127 	.read		= seq_read,
128 	.llseek		= seq_lseek,
129 	.release	= single_release,
130 	.write		= alignment_proc_write,
131 };
132 
133 /*
134  * This needs to be done after sysctl_init, otherwise sys/ will be
135  * overwritten.  Actually, this shouldn't be in sys/ at all since
136  * it isn't a sysctl, and it doesn't contain sysctl information.
137  * We now locate it in /proc/cpu/alignment instead.
138  */
139 static int __init alignment_init(void)
140 {
141 	struct proc_dir_entry *dir, *res;
142 
143 	dir = proc_mkdir("cpu", NULL);
144 	if (!dir)
145 		return -ENOMEM;
146 
147 	res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
148 			       &alignment_proc_fops, &se_usermode);
149 	if (!res)
150 		return -ENOMEM;
151 
152         res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
153 			       &alignment_proc_fops, &se_kernmode_warn);
154         if (!res)
155                 return -ENOMEM;
156 
157 	return 0;
158 }
159 fs_initcall(alignment_init);
160