2 * linux/arch/nios2/kernel/misaligned.c
4 * basic emulation for mis-aligned accesses on the NIOS II cpu
5 * modelled after the version for arm in arm/alignment.c
8 * Copyright (C) 2010 Ambient Corporation
9 * Copyright (c) 2010 Altera Corporation, San Jose, California, USA.
10 * Copyright (c) 2010 Arrow Electronics, Inc.
12 * This file is subject to the terms and conditions of the GNU General
13 * Public License. See the file COPYING in the main directory of
14 * this archive for more details.
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/proc_fs.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/uaccess.h>
23 #include <linux/seq_file.h>
25 #include <asm/traps.h>
26 #include <asm/unaligned.h>
28 /* instructions we emulate */
29 #define INST_LDHU 0x0b
35 static unsigned int ma_usermode;
38 #define UM_SIGNAL 0x04
41 /* see arch/nios2/include/asm/ptrace.h */
42 static u8 sys_stack_frame_reg_offset[] = {
44 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 0,
45 /* struct switch_stack */
46 16, 17, 18, 19, 20, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0, 0
49 static int reg_offsets[32];
51 static inline u32 get_reg_val(struct pt_regs *fp, int reg)
53 u8 *p = ((u8 *)fp) + reg_offsets[reg];
57 static inline void put_reg_val(struct pt_regs *fp, int reg, u32 val)
59 u8 *p = ((u8 *)fp) + reg_offsets[reg];
64 * (mis)alignment handler
66 asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
70 u8 a, b, d0, d1, d2, d3;
74 /* back up one instruction */
77 if (fixup_exception(fp)) {
81 in_kernel = !user_mode(fp);
83 isn = *(unsigned long *)(fp->ea);
87 /* do fixup if in kernel or mode turned on */
88 if (in_kernel || (ma_usermode & UM_FIXUP)) {
89 /* decompose instruction */
90 a = (isn >> 27) & 0x1f;
91 b = (isn >> 22) & 0x1f;
92 imm16 = (isn >> 6) & 0xffff;
93 addr = get_reg_val(fp, a) + imm16;
95 /* do fixup to saved registers */
98 fault |= __get_user(d0, (u8 *)(addr+0));
99 fault |= __get_user(d1, (u8 *)(addr+1));
100 val = (d1 << 8) | d0;
101 put_reg_val(fp, b, val);
104 val = get_reg_val(fp, b);
108 *(u8 *)(addr+0) = d0;
109 *(u8 *)(addr+1) = d1;
111 fault |= __put_user(d0, (u8 *)(addr+0));
112 fault |= __put_user(d1, (u8 *)(addr+1));
116 fault |= __get_user(d0, (u8 *)(addr+0));
117 fault |= __get_user(d1, (u8 *)(addr+1));
118 val = (short)((d1 << 8) | d0);
119 put_reg_val(fp, b, val);
122 val = get_reg_val(fp, b);
128 *(u8 *)(addr+0) = d0;
129 *(u8 *)(addr+1) = d1;
130 *(u8 *)(addr+2) = d2;
131 *(u8 *)(addr+3) = d3;
133 fault |= __put_user(d0, (u8 *)(addr+0));
134 fault |= __put_user(d1, (u8 *)(addr+1));
135 fault |= __put_user(d2, (u8 *)(addr+2));
136 fault |= __put_user(d3, (u8 *)(addr+3));
140 fault |= __get_user(d0, (u8 *)(addr+0));
141 fault |= __get_user(d1, (u8 *)(addr+1));
142 fault |= __get_user(d2, (u8 *)(addr+2));
143 fault |= __get_user(d3, (u8 *)(addr+3));
144 val = (d3 << 24) | (d2 << 16) | (d1 << 8) | d0;
145 put_reg_val(fp, b, val);
150 addr = RDCTL(CTL_BADADDR);
155 pr_err("fault during kernel misaligned fixup @ %#lx; addr 0x%08x; isn=0x%08x\n",
156 fp->ea, (unsigned int)addr,
159 pr_err("fault during user misaligned fixup @ %#lx; isn=%08x addr=0x%08x sp=0x%08lx pid=%d\n",
161 (unsigned int)isn, addr, fp->sp,
164 _exception(SIGSEGV, fp, SEGV_MAPERR, fp->ea);
171 * note exception and skip bad instruction (return)
176 if (ma_usermode & KM_WARN) {
177 pr_err("kernel unaligned access @ %#lx; BADADDR 0x%08x; cause=%d, isn=0x%08x\n",
179 (unsigned int)addr, cause,
190 * possibly send SIGBUS signal to process
192 if (ma_usermode & UM_WARN) {
193 pr_err("user unaligned access @ %#lx; isn=0x%08lx ea=0x%08lx ra=0x%08lx sp=0x%08lx\n",
194 (unsigned long)addr, (unsigned long)isn,
195 fp->ea, fp->ra, fp->sp);
198 if (ma_usermode & UM_SIGNAL)
199 _exception(SIGBUS, fp, BUS_ADRALN, fp->ea);
201 fp->ea += 4; /* else advance */
204 static void __init misaligned_calc_reg_offsets(void)
208 /* pre-calc offsets of registers on sys call stack frame */
212 for (i = 0; i < 16; i++) {
213 r = sys_stack_frame_reg_offset[i];
214 reg_offsets[r] = offset;
218 /* struct switch_stack */
219 offset = -sizeof(struct switch_stack);
220 for (i = 16; i < 32; i++) {
221 r = sys_stack_frame_reg_offset[i];
222 reg_offsets[r] = offset;
228 static int __init misaligned_init(void)
230 /* default mode - silent fix */
231 ma_usermode = UM_FIXUP | KM_WARN;
233 misaligned_calc_reg_offsets();
238 fs_initcall(misaligned_init);