1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011-2012 Synopsys (www.synopsys.com)
6 * -Adapted (from .26 to .35)
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>
15 #include "unaligned.h"
17 #ifdef CONFIG_CPU_BIG_ENDIAN
19 #define FIRST_BYTE_16 "swap %1, %1\n swape %1, %1\n"
20 #define FIRST_BYTE_32 "swape %1, %1\n"
27 #define __get8_unaligned_check(val, addr, err) \
29 "1: ldb.ab %1, [%2, 1]\n" \
31 " .section .fixup,\"ax\"\n" \
36 " .section __ex_table,\"a\"\n" \
40 : "=r" (err), "=&r" (val), "=r" (addr) \
41 : "0" (err), "2" (addr))
43 #define get16_unaligned_check(val, addr) \
45 unsigned int err = 0, v, a = addr; \
46 __get8_unaligned_check(v, a, err); \
47 val = v << ((BE) ? 8 : 0); \
48 __get8_unaligned_check(v, a, err); \
49 val |= v << ((BE) ? 0 : 8); \
54 #define get32_unaligned_check(val, addr) \
56 unsigned int err = 0, v, a = addr; \
57 __get8_unaligned_check(v, a, err); \
58 val = v << ((BE) ? 24 : 0); \
59 __get8_unaligned_check(v, a, err); \
60 val |= v << ((BE) ? 16 : 8); \
61 __get8_unaligned_check(v, a, err); \
62 val |= v << ((BE) ? 8 : 16); \
63 __get8_unaligned_check(v, a, err); \
64 val |= v << ((BE) ? 0 : 24); \
69 #define put16_unaligned_check(val, addr) \
71 unsigned int err = 0, v = val, a = addr;\
75 "1: stb.ab %1, [%2, 1]\n" \
79 " .section .fixup,\"ax\"\n" \
84 " .section __ex_table,\"a\"\n" \
89 : "=r" (err), "=&r" (v), "=&r" (a) \
90 : "0" (err), "1" (v), "2" (a)); \
96 #define put32_unaligned_check(val, addr) \
98 unsigned int err = 0, v = val, a = addr;\
102 "1: stb.ab %1, [%2, 1]\n" \
104 "2: stb.ab %1, [%2, 1]\n" \
106 "3: stb.ab %1, [%2, 1]\n" \
108 "4: stb %1, [%2]\n" \
110 " .section .fixup,\"ax\"\n" \
115 " .section __ex_table,\"a\"\n" \
122 : "=r" (err), "=&r" (v), "=&r" (a) \
123 : "0" (err), "1" (v), "2" (a)); \
130 int unaligned_enabled __read_mostly = 1; /* Enabled by default */
131 int no_unaligned_warning __read_mostly = 1; /* Only 1 warning by default */
133 static void fixup_load(struct disasm_state *state, struct pt_regs *regs,
134 struct callee_regs *cregs)
138 /* register write back */
139 if ((state->aa == 1) || (state->aa == 2)) {
140 set_reg(state->wb_reg, state->src1 + state->src2, regs, cregs);
146 if (state->zz == 0) {
147 get32_unaligned_check(val, state->src1 + state->src2);
149 get16_unaligned_check(val, state->src1 + state->src2);
152 val = (val << 16) >> 16;
155 if (state->pref == 0)
156 set_reg(state->dest, val, regs, cregs);
160 fault: state->fault = 1;
163 static void fixup_store(struct disasm_state *state, struct pt_regs *regs,
164 struct callee_regs *cregs)
166 /* register write back */
167 if ((state->aa == 1) || (state->aa == 2)) {
168 set_reg(state->wb_reg, state->src2 + state->src3, regs, cregs);
172 } else if (state->aa == 3) {
173 if (state->zz == 2) {
174 set_reg(state->wb_reg, state->src2 + (state->src3 << 1),
176 } else if (!state->zz) {
177 set_reg(state->wb_reg, state->src2 + (state->src3 << 2),
186 put32_unaligned_check(state->src1, state->src2 + state->src3);
188 put16_unaligned_check(state->src1, state->src2 + state->src3);
192 fault: state->fault = 1;
196 * Handle an unaligned access
197 * Returns 0 if successfully handled, 1 if some error happened
199 int misaligned_fixup(unsigned long address, struct pt_regs *regs,
200 struct callee_regs *cregs)
202 struct disasm_state state;
203 char buf[TASK_COMM_LEN];
205 /* handle user mode only and only if enabled by sysadmin */
206 if (!user_mode(regs) || !unaligned_enabled)
209 if (no_unaligned_warning) {
210 pr_warn_once("%s(%d) made unaligned access which was emulated"
211 " by kernel assist\n. This can degrade application"
212 " performance significantly\n. To enable further"
213 " logging of such instances, please \n"
214 " echo 0 > /proc/sys/kernel/ignore-unaligned-usertrap\n",
215 get_task_comm(buf, current), task_pid_nr(current));
217 /* Add rate limiting if it gets down to it */
218 pr_warn("%s(%d): unaligned access to/from 0x%lx by PC: 0x%lx\n",
219 get_task_comm(buf, current), task_pid_nr(current),
224 disasm_instr(regs->ret, &state, 1, regs, cregs);
229 /* ldb/stb should not have unaligned exception */
230 if ((state.zz == 1) || (state.di))
234 fixup_load(&state, regs, cregs);
236 fixup_store(&state, regs, cregs);
241 /* clear any remnants of delay slot */
242 if (delay_mode(regs)) {
243 regs->ret = regs->bta & ~1U;
244 regs->status32 &= ~STATUS_DE_MASK;
246 regs->ret += state.instr_len;
248 /* handle zero-overhead-loop */
249 if ((regs->ret == regs->lp_end) && (regs->lp_count)) {
250 regs->ret = regs->lp_start;
255 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, address);
259 pr_err("Alignment trap: fault in fix-up %08lx at [<%08lx>]\n",
260 state.words[0], address);