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/perf_event.h>
16 #include <linux/ptrace.h>
17 #include <linux/uaccess.h>
18 #include <asm/disasm.h>
20 #ifdef CONFIG_CPU_BIG_ENDIAN
22 #define FIRST_BYTE_16 "swap %1, %1\n swape %1, %1\n"
23 #define FIRST_BYTE_32 "swape %1, %1\n"
30 #define __get8_unaligned_check(val, addr, err) \
32 "1: ldb.ab %1, [%2, 1]\n" \
34 " .section .fixup,\"ax\"\n" \
39 " .section __ex_table,\"a\"\n" \
43 : "=r" (err), "=&r" (val), "=r" (addr) \
44 : "0" (err), "2" (addr))
46 #define get16_unaligned_check(val, addr) \
48 unsigned int err = 0, v, a = addr; \
49 __get8_unaligned_check(v, a, err); \
50 val = v << ((BE) ? 8 : 0); \
51 __get8_unaligned_check(v, a, err); \
52 val |= v << ((BE) ? 0 : 8); \
57 #define get32_unaligned_check(val, addr) \
59 unsigned int err = 0, v, a = addr; \
60 __get8_unaligned_check(v, a, err); \
61 val = v << ((BE) ? 24 : 0); \
62 __get8_unaligned_check(v, a, err); \
63 val |= v << ((BE) ? 16 : 8); \
64 __get8_unaligned_check(v, a, err); \
65 val |= v << ((BE) ? 8 : 16); \
66 __get8_unaligned_check(v, a, err); \
67 val |= v << ((BE) ? 0 : 24); \
72 #define put16_unaligned_check(val, addr) \
74 unsigned int err = 0, v = val, a = addr;\
78 "1: stb.ab %1, [%2, 1]\n" \
82 " .section .fixup,\"ax\"\n" \
87 " .section __ex_table,\"a\"\n" \
92 : "=r" (err), "=&r" (v), "=&r" (a) \
93 : "0" (err), "1" (v), "2" (a)); \
99 #define put32_unaligned_check(val, addr) \
101 unsigned int err = 0, v = val, a = addr;\
105 "1: stb.ab %1, [%2, 1]\n" \
107 "2: stb.ab %1, [%2, 1]\n" \
109 "3: stb.ab %1, [%2, 1]\n" \
111 "4: stb %1, [%2]\n" \
113 " .section .fixup,\"ax\"\n" \
118 " .section __ex_table,\"a\"\n" \
125 : "=r" (err), "=&r" (v), "=&r" (a) \
126 : "0" (err), "1" (v), "2" (a)); \
133 int unaligned_enabled __read_mostly = 1; /* Enabled by default */
134 int no_unaligned_warning __read_mostly = 1; /* Only 1 warning by default */
136 static void fixup_load(struct disasm_state *state, struct pt_regs *regs,
137 struct callee_regs *cregs)
141 /* register write back */
142 if ((state->aa == 1) || (state->aa == 2)) {
143 set_reg(state->wb_reg, state->src1 + state->src2, regs, cregs);
149 if (state->zz == 0) {
150 get32_unaligned_check(val, state->src1 + state->src2);
152 get16_unaligned_check(val, state->src1 + state->src2);
155 val = (val << 16) >> 16;
158 if (state->pref == 0)
159 set_reg(state->dest, val, regs, cregs);
163 fault: state->fault = 1;
166 static void fixup_store(struct disasm_state *state, struct pt_regs *regs,
167 struct callee_regs *cregs)
169 /* register write back */
170 if ((state->aa == 1) || (state->aa == 2)) {
171 set_reg(state->wb_reg, state->src2 + state->src3, regs, cregs);
175 } else if (state->aa == 3) {
176 if (state->zz == 2) {
177 set_reg(state->wb_reg, state->src2 + (state->src3 << 1),
179 } else if (!state->zz) {
180 set_reg(state->wb_reg, state->src2 + (state->src3 << 2),
189 put32_unaligned_check(state->src1, state->src2 + state->src3);
191 put16_unaligned_check(state->src1, state->src2 + state->src3);
195 fault: state->fault = 1;
199 * Handle an unaligned access
200 * Returns 0 if successfully handled, 1 if some error happened
202 int misaligned_fixup(unsigned long address, struct pt_regs *regs,
203 struct callee_regs *cregs)
205 struct disasm_state state;
206 char buf[TASK_COMM_LEN];
208 /* handle user mode only and only if enabled by sysadmin */
209 if (!user_mode(regs) || !unaligned_enabled)
212 if (no_unaligned_warning) {
213 pr_warn_once("%s(%d) made unaligned access which was emulated"
214 " by kernel assist\n. This can degrade application"
215 " performance significantly\n. To enable further"
216 " logging of such instances, please \n"
217 " echo 0 > /proc/sys/kernel/ignore-unaligned-usertrap\n",
218 get_task_comm(buf, current), task_pid_nr(current));
220 /* Add rate limiting if it gets down to it */
221 pr_warn("%s(%d): unaligned access to/from 0x%lx by PC: 0x%lx\n",
222 get_task_comm(buf, current), task_pid_nr(current),
227 disasm_instr(regs->ret, &state, 1, regs, cregs);
232 /* ldb/stb should not have unaligned exception */
233 if ((state.zz == 1) || (state.di))
237 fixup_load(&state, regs, cregs);
239 fixup_store(&state, regs, cregs);
244 if (delay_mode(regs)) {
245 regs->ret = regs->bta;
246 regs->status32 &= ~STATUS_DE_MASK;
248 regs->ret += state.instr_len;
250 /* handle zero-overhead-loop */
251 if ((regs->ret == regs->lp_end) && (regs->lp_count)) {
252 regs->ret = regs->lp_start;
257 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, address);
261 pr_err("Alignment trap: fault in fix-up %08lx at [<%08lx>]\n",
262 state.words[0], address);