2 * Copyright (C) 2011-2012 Synopsys (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 * -Adapted (from .26 to .35)
10 * -original contribution by Tim.yao@amlogic.com
14 #include <linux/types.h>
15 #include <linux/ptrace.h>
16 #include <linux/uaccess.h>
17 #include <asm/disasm.h>
19 #ifdef CONFIG_CPU_BIG_ENDIAN
21 #define FIRST_BYTE_16 "swap %1, %1\n swape %1, %1\n"
22 #define FIRST_BYTE_32 "swape %1, %1\n"
29 #define __get8_unaligned_check(val, addr, err) \
31 "1: ldb.ab %1, [%2, 1]\n" \
33 " .section .fixup,\"ax\"\n" \
38 " .section __ex_table,\"a\"\n" \
42 : "=r" (err), "=&r" (val), "=r" (addr) \
43 : "0" (err), "2" (addr))
45 #define get16_unaligned_check(val, addr) \
47 unsigned int err = 0, v, a = addr; \
48 __get8_unaligned_check(v, a, err); \
49 val = v << ((BE) ? 8 : 0); \
50 __get8_unaligned_check(v, a, err); \
51 val |= v << ((BE) ? 0 : 8); \
56 #define get32_unaligned_check(val, addr) \
58 unsigned int err = 0, v, a = addr; \
59 __get8_unaligned_check(v, a, err); \
60 val = v << ((BE) ? 24 : 0); \
61 __get8_unaligned_check(v, a, err); \
62 val |= v << ((BE) ? 16 : 8); \
63 __get8_unaligned_check(v, a, err); \
64 val |= v << ((BE) ? 8 : 16); \
65 __get8_unaligned_check(v, a, err); \
66 val |= v << ((BE) ? 0 : 24); \
71 #define put16_unaligned_check(val, addr) \
73 unsigned int err = 0, v = val, a = addr;\
77 "1: stb.ab %1, [%2, 1]\n" \
81 " .section .fixup,\"ax\"\n" \
86 " .section __ex_table,\"a\"\n" \
91 : "=r" (err), "=&r" (v), "=&r" (a) \
92 : "0" (err), "1" (v), "2" (a)); \
98 #define put32_unaligned_check(val, addr) \
100 unsigned int err = 0, v = val, a = addr;\
104 "1: stb.ab %1, [%2, 1]\n" \
106 "2: stb.ab %1, [%2, 1]\n" \
108 "3: stb.ab %1, [%2, 1]\n" \
110 "4: stb %1, [%2]\n" \
112 " .section .fixup,\"ax\"\n" \
117 " .section __ex_table,\"a\"\n" \
124 : "=r" (err), "=&r" (v), "=&r" (a) \
125 : "0" (err), "1" (v), "2" (a)); \
132 int unaligned_enabled __read_mostly
= 1; /* Enabled by default */
133 int no_unaligned_warning __read_mostly
= 1; /* Only 1 warning by default */
135 static void fixup_load(struct disasm_state
*state
, struct pt_regs
*regs
,
136 struct callee_regs
*cregs
)
140 /* register write back */
141 if ((state
->aa
== 1) || (state
->aa
== 2)) {
142 set_reg(state
->wb_reg
, state
->src1
+ state
->src2
, regs
, cregs
);
148 if (state
->zz
== 0) {
149 get32_unaligned_check(val
, state
->src1
+ state
->src2
);
151 get16_unaligned_check(val
, state
->src1
+ state
->src2
);
154 val
= (val
<< 16) >> 16;
157 if (state
->pref
== 0)
158 set_reg(state
->dest
, val
, regs
, cregs
);
162 fault
: state
->fault
= 1;
165 static void fixup_store(struct disasm_state
*state
, struct pt_regs
*regs
,
166 struct callee_regs
*cregs
)
168 /* register write back */
169 if ((state
->aa
== 1) || (state
->aa
== 2)) {
170 set_reg(state
->wb_reg
, state
->src2
+ state
->src3
, regs
, cregs
);
174 } else if (state
->aa
== 3) {
175 if (state
->zz
== 2) {
176 set_reg(state
->wb_reg
, state
->src2
+ (state
->src3
<< 1),
178 } else if (!state
->zz
) {
179 set_reg(state
->wb_reg
, state
->src2
+ (state
->src3
<< 2),
188 put32_unaligned_check(state
->src1
, state
->src2
+ state
->src3
);
190 put16_unaligned_check(state
->src1
, state
->src2
+ state
->src3
);
194 fault
: state
->fault
= 1;
198 * Handle an unaligned access
199 * Returns 0 if successfully handled, 1 if some error happened
201 int misaligned_fixup(unsigned long address
, struct pt_regs
*regs
,
202 struct callee_regs
*cregs
)
204 struct disasm_state state
;
205 char buf
[TASK_COMM_LEN
];
207 /* handle user mode only and only if enabled by sysadmin */
208 if (!user_mode(regs
) || !unaligned_enabled
)
211 if (no_unaligned_warning
) {
212 pr_warn_once("%s(%d) made unaligned access which was emulated"
213 " by kernel assist\n. This can degrade application"
214 " performance significantly\n. To enable further"
215 " logging of such instances, please \n"
216 " echo 0 > /proc/sys/kernel/ignore-unaligned-usertrap\n",
217 get_task_comm(buf
, current
), task_pid_nr(current
));
219 /* Add rate limiting if it gets down to it */
220 pr_warn("%s(%d): unaligned access to/from 0x%lx by PC: 0x%lx\n",
221 get_task_comm(buf
, current
), task_pid_nr(current
),
226 disasm_instr(regs
->ret
, &state
, 1, regs
, cregs
);
231 /* ldb/stb should not have unaligned exception */
232 if ((state
.zz
== 1) || (state
.di
))
236 fixup_load(&state
, regs
, cregs
);
238 fixup_store(&state
, regs
, cregs
);
243 if (delay_mode(regs
)) {
244 regs
->ret
= regs
->bta
;
245 regs
->status32
&= ~STATUS_DE_MASK
;
247 regs
->ret
+= state
.instr_len
;
249 /* handle zero-overhead-loop */
250 if ((regs
->ret
== regs
->lp_end
) && (regs
->lp_count
)) {
251 regs
->ret
= regs
->lp_start
;
259 pr_err("Alignment trap: fault in fix-up %08lx at [<%08lx>]\n",
260 state
.words
[0], address
);