1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011-2012 Synopsys (www.synopsys.com)
6 * -Adapted (from .26 to .35)
7 * -original contribution by Tim.yao@amlogic.com
10 #include <linux/types.h>
11 #include <linux/perf_event.h>
12 #include <linux/ptrace.h>
13 #include <linux/uaccess.h>
14 #include <asm/disasm.h>
16 #ifdef CONFIG_CPU_BIG_ENDIAN
18 #define FIRST_BYTE_16 "swap %1, %1\n swape %1, %1\n"
19 #define FIRST_BYTE_32 "swape %1, %1\n"
26 #define __get8_unaligned_check(val, addr, err) \
28 "1: ldb.ab %1, [%2, 1]\n" \
30 " .section .fixup,\"ax\"\n" \
35 " .section __ex_table,\"a\"\n" \
39 : "=r" (err), "=&r" (val), "=r" (addr) \
40 : "0" (err), "2" (addr))
42 #define get16_unaligned_check(val, addr) \
44 unsigned int err = 0, v, a = addr; \
45 __get8_unaligned_check(v, a, err); \
46 val = v << ((BE) ? 8 : 0); \
47 __get8_unaligned_check(v, a, err); \
48 val |= v << ((BE) ? 0 : 8); \
53 #define get32_unaligned_check(val, addr) \
55 unsigned int err = 0, v, a = addr; \
56 __get8_unaligned_check(v, a, err); \
57 val = v << ((BE) ? 24 : 0); \
58 __get8_unaligned_check(v, a, err); \
59 val |= v << ((BE) ? 16 : 8); \
60 __get8_unaligned_check(v, a, err); \
61 val |= v << ((BE) ? 8 : 16); \
62 __get8_unaligned_check(v, a, err); \
63 val |= v << ((BE) ? 0 : 24); \
68 #define put16_unaligned_check(val, addr) \
70 unsigned int err = 0, v = val, a = addr;\
74 "1: stb.ab %1, [%2, 1]\n" \
78 " .section .fixup,\"ax\"\n" \
83 " .section __ex_table,\"a\"\n" \
88 : "=r" (err), "=&r" (v), "=&r" (a) \
89 : "0" (err), "1" (v), "2" (a)); \
95 #define put32_unaligned_check(val, addr) \
97 unsigned int err = 0, v = val, a = addr;\
101 "1: stb.ab %1, [%2, 1]\n" \
103 "2: stb.ab %1, [%2, 1]\n" \
105 "3: stb.ab %1, [%2, 1]\n" \
107 "4: stb %1, [%2]\n" \
109 " .section .fixup,\"ax\"\n" \
114 " .section __ex_table,\"a\"\n" \
121 : "=r" (err), "=&r" (v), "=&r" (a) \
122 : "0" (err), "1" (v), "2" (a)); \
129 int unaligned_enabled __read_mostly
= 1; /* Enabled by default */
130 int no_unaligned_warning __read_mostly
= 1; /* Only 1 warning by default */
132 static void fixup_load(struct disasm_state
*state
, struct pt_regs
*regs
,
133 struct callee_regs
*cregs
)
137 /* register write back */
138 if ((state
->aa
== 1) || (state
->aa
== 2)) {
139 set_reg(state
->wb_reg
, state
->src1
+ state
->src2
, regs
, cregs
);
145 if (state
->zz
== 0) {
146 get32_unaligned_check(val
, state
->src1
+ state
->src2
);
148 get16_unaligned_check(val
, state
->src1
+ state
->src2
);
151 val
= (val
<< 16) >> 16;
154 if (state
->pref
== 0)
155 set_reg(state
->dest
, val
, regs
, cregs
);
159 fault
: state
->fault
= 1;
162 static void fixup_store(struct disasm_state
*state
, struct pt_regs
*regs
,
163 struct callee_regs
*cregs
)
165 /* register write back */
166 if ((state
->aa
== 1) || (state
->aa
== 2)) {
167 set_reg(state
->wb_reg
, state
->src2
+ state
->src3
, regs
, cregs
);
171 } else if (state
->aa
== 3) {
172 if (state
->zz
== 2) {
173 set_reg(state
->wb_reg
, state
->src2
+ (state
->src3
<< 1),
175 } else if (!state
->zz
) {
176 set_reg(state
->wb_reg
, state
->src2
+ (state
->src3
<< 2),
185 put32_unaligned_check(state
->src1
, state
->src2
+ state
->src3
);
187 put16_unaligned_check(state
->src1
, state
->src2
+ state
->src3
);
191 fault
: state
->fault
= 1;
195 * Handle an unaligned access
196 * Returns 0 if successfully handled, 1 if some error happened
198 int misaligned_fixup(unsigned long address
, struct pt_regs
*regs
,
199 struct callee_regs
*cregs
)
201 struct disasm_state state
;
202 char buf
[TASK_COMM_LEN
];
204 /* handle user mode only and only if enabled by sysadmin */
205 if (!user_mode(regs
) || !unaligned_enabled
)
208 if (no_unaligned_warning
) {
209 pr_warn_once("%s(%d) made unaligned access which was emulated"
210 " by kernel assist\n. This can degrade application"
211 " performance significantly\n. To enable further"
212 " logging of such instances, please \n"
213 " echo 0 > /proc/sys/kernel/ignore-unaligned-usertrap\n",
214 get_task_comm(buf
, current
), task_pid_nr(current
));
216 /* Add rate limiting if it gets down to it */
217 pr_warn("%s(%d): unaligned access to/from 0x%lx by PC: 0x%lx\n",
218 get_task_comm(buf
, current
), task_pid_nr(current
),
223 disasm_instr(regs
->ret
, &state
, 1, regs
, cregs
);
228 /* ldb/stb should not have unaligned exception */
229 if ((state
.zz
== 1) || (state
.di
))
233 fixup_load(&state
, regs
, cregs
);
235 fixup_store(&state
, regs
, cregs
);
240 /* clear any remanants of delay slot */
241 if (delay_mode(regs
)) {
242 regs
->ret
= regs
->bta
& ~1U;
243 regs
->status32
&= ~STATUS_DE_MASK
;
245 regs
->ret
+= state
.instr_len
;
247 /* handle zero-overhead-loop */
248 if ((regs
->ret
== regs
->lp_end
) && (regs
->lp_count
)) {
249 regs
->ret
= regs
->lp_start
;
254 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, address
);
258 pr_err("Alignment trap: fault in fix-up %08lx at [<%08lx>]\n",
259 state
.words
[0], address
);