blob: 05a3c2f8d2df0a08ab2229febb30d6caa914cd77 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* MN10300 Misalignment fixup handler
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/timer.h>
18#include <linux/mm.h>
19#include <linux/smp.h>
20#include <linux/smp_lock.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/spinlock.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <asm/processor.h>
27#include <asm/system.h>
28#include <asm/uaccess.h>
29#include <asm/io.h>
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/pgalloc.h>
33#include <asm/cpu-regs.h>
34#include <asm/busctl-regs.h>
35#include <asm/fpu.h>
36#include <asm/gdb-stub.h>
37#include <asm/asm-offsets.h>
38
39#if 0
David Howells31ea24b2008-11-12 15:34:59 +000040#define kdebug(FMT, ...) printk(KERN_DEBUG "MISALIGN: "FMT"\n", ##__VA_ARGS__)
David Howellsb920de12008-02-08 04:19:31 -080041#else
42#define kdebug(FMT, ...) do {} while (0)
43#endif
44
45static int misalignment_addr(unsigned long *registers, unsigned params,
David Howellsb308bf32008-11-12 15:35:14 +000046 unsigned opcode, unsigned long disp,
David Howellsb920de12008-02-08 04:19:31 -080047 void **_address, unsigned long **_postinc);
48
49static int misalignment_reg(unsigned long *registers, unsigned params,
David Howellsb308bf32008-11-12 15:35:14 +000050 unsigned opcode, unsigned long disp,
David Howellsb920de12008-02-08 04:19:31 -080051 unsigned long **_register);
52
David Howellsb920de12008-02-08 04:19:31 -080053static const unsigned Dreg_index[] = {
54 REG_D0 >> 2, REG_D1 >> 2, REG_D2 >> 2, REG_D3 >> 2
55};
56
57static const unsigned Areg_index[] = {
58 REG_A0 >> 2, REG_A1 >> 2, REG_A2 >> 2, REG_A3 >> 2
59};
60
61static const unsigned Rreg_index[] = {
62 REG_E0 >> 2, REG_E1 >> 2, REG_E2 >> 2, REG_E3 >> 2,
63 REG_E4 >> 2, REG_E5 >> 2, REG_E6 >> 2, REG_E7 >> 2,
64 REG_A0 >> 2, REG_A1 >> 2, REG_A2 >> 2, REG_A3 >> 2,
65 REG_D0 >> 2, REG_D1 >> 2, REG_D2 >> 2, REG_D3 >> 2
66};
67
68enum format_id {
69 FMT_S0,
70 FMT_S1,
71 FMT_S2,
72 FMT_S4,
73 FMT_D0,
74 FMT_D1,
75 FMT_D2,
76 FMT_D4,
77 FMT_D6,
78 FMT_D7,
79 FMT_D8,
80 FMT_D9,
81};
82
David Howells31ea24b2008-11-12 15:34:59 +000083static const struct {
David Howellsb920de12008-02-08 04:19:31 -080084 u_int8_t opsz, dispsz;
85} format_tbl[16] = {
86 [FMT_S0] = { 8, 0 },
87 [FMT_S1] = { 8, 8 },
88 [FMT_S2] = { 8, 16 },
89 [FMT_S4] = { 8, 32 },
90 [FMT_D0] = { 16, 0 },
91 [FMT_D1] = { 16, 8 },
92 [FMT_D2] = { 16, 16 },
93 [FMT_D4] = { 16, 32 },
94 [FMT_D6] = { 24, 0 },
95 [FMT_D7] = { 24, 8 },
96 [FMT_D8] = { 24, 24 },
97 [FMT_D9] = { 24, 32 },
98};
99
100enum value_id {
101 DM0, /* data reg in opcode in bits 0-1 */
102 DM1, /* data reg in opcode in bits 2-3 */
103 DM2, /* data reg in opcode in bits 4-5 */
104 AM0, /* addr reg in opcode in bits 0-1 */
105 AM1, /* addr reg in opcode in bits 2-3 */
106 AM2, /* addr reg in opcode in bits 4-5 */
107 RM0, /* reg in opcode in bits 0-3 */
108 RM1, /* reg in opcode in bits 2-5 */
109 RM2, /* reg in opcode in bits 4-7 */
110 RM4, /* reg in opcode in bits 8-11 */
111 RM6, /* reg in opcode in bits 12-15 */
112
113 RD0, /* reg in displacement in bits 0-3 */
114 RD2, /* reg in displacement in bits 4-7 */
115
116 SP, /* stack pointer */
117
118 SD8, /* 8-bit signed displacement */
119 SD16, /* 16-bit signed displacement */
120 SD24, /* 24-bit signed displacement */
121 SIMM4_2, /* 4-bit signed displacement in opcode bits 4-7 */
122 SIMM8, /* 8-bit signed immediate */
David Howellsee6e7402008-11-12 15:35:09 +0000123 IMM8, /* 8-bit unsigned immediate */
124 IMM16, /* 16-bit unsigned immediate */
David Howellsb920de12008-02-08 04:19:31 -0800125 IMM24, /* 24-bit unsigned immediate */
126 IMM32, /* 32-bit unsigned immediate */
David Howellsee6e7402008-11-12 15:35:09 +0000127 IMM32_HIGH8, /* 32-bit unsigned immediate, LSB in opcode */
128
129 IMM32_MEM, /* 32-bit unsigned displacement */
130 IMM32_HIGH8_MEM, /* 32-bit unsigned displacement, LSB in opcode */
David Howellsb920de12008-02-08 04:19:31 -0800131
132 DN0 = DM0,
133 DN1 = DM1,
134 DN2 = DM2,
135 AN0 = AM0,
136 AN1 = AM1,
137 AN2 = AM2,
138 RN0 = RM0,
139 RN1 = RM1,
140 RN2 = RM2,
141 RN4 = RM4,
142 RN6 = RM6,
143 DI = DM1,
144 RI = RM2,
145
146};
147
148struct mn10300_opcode {
149 const char *name;
150 u_int32_t opcode;
151 u_int32_t opmask;
152 unsigned exclusion;
153
154 enum format_id format;
155
156 unsigned cpu_mask;
157#define AM33 330
158
159 unsigned params[2];
160#define MEM(ADDR) (0x80000000 | (ADDR))
161#define MEM2(ADDR1, ADDR2) (0x80000000 | (ADDR1) << 8 | (ADDR2))
162#define MEMINC(ADDR) (0x81000000 | (ADDR))
163#define MEMINC2(ADDR, INC) (0x81000000 | (ADDR) << 8 | (INC))
164};
165
166/* LIBOPCODES EXCERPT
167 Assemble Matsushita MN10300 instructions.
168 Copyright 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
169
170 This program is free software; you can redistribute it and/or modify
171 it under the terms of the GNU General Public Licence as published by
172 the Free Software Foundation; either version 2 of the Licence, or
173 (at your option) any later version.
174
175 This program is distributed in the hope that it will be useful,
176 but WITHOUT ANY WARRANTY; without even the implied warranty of
177 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
178 GNU General Public Licence for more details.
179
180 You should have received a copy of the GNU General Public Licence
181 along with this program; if not, write to the Free Software
182 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
183*/
184static const struct mn10300_opcode mn10300_opcodes[] = {
David Howellsee6e7402008-11-12 15:35:09 +0000185{ "mov", 0x4200, 0xf300, 0, FMT_S1, 0, {DM1, MEM2(IMM8, SP)}},
186{ "mov", 0x4300, 0xf300, 0, FMT_S1, 0, {AM1, MEM2(IMM8, SP)}},
187{ "mov", 0x5800, 0xfc00, 0, FMT_S1, 0, {MEM2(IMM8, SP), DN0}},
188{ "mov", 0x5c00, 0xfc00, 0, FMT_S1, 0, {MEM2(IMM8, SP), AN0}},
David Howellsb920de12008-02-08 04:19:31 -0800189{ "mov", 0x60, 0xf0, 0, FMT_S0, 0, {DM1, MEM(AN0)}},
190{ "mov", 0x70, 0xf0, 0, FMT_S0, 0, {MEM(AM0), DN1}},
191{ "mov", 0xf000, 0xfff0, 0, FMT_D0, 0, {MEM(AM0), AN1}},
192{ "mov", 0xf010, 0xfff0, 0, FMT_D0, 0, {AM1, MEM(AN0)}},
193{ "mov", 0xf300, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), DN2}},
194{ "mov", 0xf340, 0xffc0, 0, FMT_D0, 0, {DM2, MEM2(DI, AN0)}},
195{ "mov", 0xf380, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), AN2}},
196{ "mov", 0xf3c0, 0xffc0, 0, FMT_D0, 0, {AM2, MEM2(DI, AN0)}},
197{ "mov", 0xf80000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8, AM0), DN1}},
198{ "mov", 0xf81000, 0xfff000, 0, FMT_D1, 0, {DM1, MEM2(SD8, AN0)}},
199{ "mov", 0xf82000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8,AM0), AN1}},
200{ "mov", 0xf83000, 0xfff000, 0, FMT_D1, 0, {AM1, MEM2(SD8, AN0)}},
201{ "mov", 0xf8f000, 0xfffc00, 0, FMT_D1, AM33, {MEM2(SD8, AM0), SP}},
202{ "mov", 0xf8f400, 0xfffc00, 0, FMT_D1, AM33, {SP, MEM2(SD8, AN0)}},
203{ "mov", 0xf90a00, 0xffff00, 0, FMT_D6, AM33, {MEM(RM0), RN2}},
204{ "mov", 0xf91a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEM(RN0)}},
205{ "mov", 0xf96a00, 0xffff00, 0x12, FMT_D6, AM33, {MEMINC(RM0), RN2}},
206{ "mov", 0xf97a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEMINC(RN0)}},
207{ "mov", 0xfa000000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), DN1}},
208{ "mov", 0xfa100000, 0xfff00000, 0, FMT_D2, 0, {DM1, MEM2(SD16, AN0)}},
209{ "mov", 0xfa200000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), AN1}},
210{ "mov", 0xfa300000, 0xfff00000, 0, FMT_D2, 0, {AM1, MEM2(SD16, AN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000211{ "mov", 0xfa900000, 0xfff30000, 0, FMT_D2, 0, {AM1, MEM2(IMM16, SP)}},
212{ "mov", 0xfa910000, 0xfff30000, 0, FMT_D2, 0, {DM1, MEM2(IMM16, SP)}},
213{ "mov", 0xfab00000, 0xfffc0000, 0, FMT_D2, 0, {MEM2(IMM16, SP), AN0}},
214{ "mov", 0xfab40000, 0xfffc0000, 0, FMT_D2, 0, {MEM2(IMM16, SP), DN0}},
David Howellsb920de12008-02-08 04:19:31 -0800215{ "mov", 0xfb0a0000, 0xffff0000, 0, FMT_D7, AM33, {MEM2(SD8, RM0), RN2}},
216{ "mov", 0xfb1a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEM2(SD8, RN0)}},
217{ "mov", 0xfb6a0000, 0xffff0000, 0x22, FMT_D7, AM33, {MEMINC2 (RM0, SIMM8), RN2}},
218{ "mov", 0xfb7a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEMINC2 (RN0, SIMM8)}},
David Howellsee6e7402008-11-12 15:35:09 +0000219{ "mov", 0xfb8a0000, 0xffff0f00, 0, FMT_D7, AM33, {MEM2(IMM8, SP), RN2}},
David Howellsb920de12008-02-08 04:19:31 -0800220{ "mov", 0xfb8e0000, 0xffff000f, 0, FMT_D7, AM33, {MEM2(RI, RM0), RD2}},
David Howellsee6e7402008-11-12 15:35:09 +0000221{ "mov", 0xfb9a0000, 0xffff0f00, 0, FMT_D7, AM33, {RM2, MEM2(IMM8, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800222{ "mov", 0xfb9e0000, 0xffff000f, 0, FMT_D7, AM33, {RD2, MEM2(RI, RN0)}},
223{ "mov", 0xfc000000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), DN1}},
224{ "mov", 0xfc100000, 0xfff00000, 0, FMT_D4, 0, {DM1, MEM2(IMM32,AN0)}},
225{ "mov", 0xfc200000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), AN1}},
226{ "mov", 0xfc300000, 0xfff00000, 0, FMT_D4, 0, {AM1, MEM2(IMM32,AN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000227{ "mov", 0xfc800000, 0xfff30000, 0, FMT_D4, 0, {AM1, MEM(IMM32_MEM)}},
228{ "mov", 0xfc810000, 0xfff30000, 0, FMT_D4, 0, {DM1, MEM(IMM32_MEM)}},
229{ "mov", 0xfc900000, 0xfff30000, 0, FMT_D4, 0, {AM1, MEM2(IMM32, SP)}},
230{ "mov", 0xfc910000, 0xfff30000, 0, FMT_D4, 0, {DM1, MEM2(IMM32, SP)}},
231{ "mov", 0xfca00000, 0xfffc0000, 0, FMT_D4, 0, {MEM(IMM32_MEM), AN0}},
232{ "mov", 0xfca40000, 0xfffc0000, 0, FMT_D4, 0, {MEM(IMM32_MEM), DN0}},
233{ "mov", 0xfcb00000, 0xfffc0000, 0, FMT_D4, 0, {MEM2(IMM32, SP), AN0}},
234{ "mov", 0xfcb40000, 0xfffc0000, 0, FMT_D4, 0, {MEM2(IMM32, SP), DN0}},
David Howellsb920de12008-02-08 04:19:31 -0800235{ "mov", 0xfd0a0000, 0xffff0000, 0, FMT_D8, AM33, {MEM2(SD24, RM0), RN2}},
236{ "mov", 0xfd1a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEM2(SD24, RN0)}},
237{ "mov", 0xfd6a0000, 0xffff0000, 0x22, FMT_D8, AM33, {MEMINC2 (RM0, IMM24), RN2}},
238{ "mov", 0xfd7a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEMINC2 (RN0, IMM24)}},
David Howellsee6e7402008-11-12 15:35:09 +0000239{ "mov", 0xfd8a0000, 0xffff0f00, 0, FMT_D8, AM33, {MEM2(IMM24, SP), RN2}},
240{ "mov", 0xfd9a0000, 0xffff0f00, 0, FMT_D8, AM33, {RM2, MEM2(IMM24, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800241{ "mov", 0xfe0a0000, 0xffff0000, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8,RM0), RN2}},
David Howellsee6e7402008-11-12 15:35:09 +0000242{ "mov", 0xfe0a0000, 0xffff0000, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8,RM0), RN2}},
243{ "mov", 0xfe0e0000, 0xffff0f00, 0, FMT_D9, AM33, {MEM(IMM32_HIGH8_MEM), RN2}},
David Howellsb920de12008-02-08 04:19:31 -0800244{ "mov", 0xfe1a0000, 0xffff0000, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, RN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000245{ "mov", 0xfe1a0000, 0xffff0000, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, RN0)}},
246{ "mov", 0xfe1e0000, 0xffff0f00, 0, FMT_D9, AM33, {RM2, MEM(IMM32_HIGH8_MEM)}},
David Howellsb920de12008-02-08 04:19:31 -0800247{ "mov", 0xfe6a0000, 0xffff0000, 0x22, FMT_D9, AM33, {MEMINC2 (RM0, IMM32_HIGH8), RN2}},
248{ "mov", 0xfe7a0000, 0xffff0000, 0, FMT_D9, AM33, {RN2, MEMINC2 (RM0, IMM32_HIGH8)}},
David Howellsee6e7402008-11-12 15:35:09 +0000249{ "mov", 0xfe8a0000, 0xffff0f00, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8, SP), RN2}},
250{ "mov", 0xfe9a0000, 0xffff0f00, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800251
252{ "movhu", 0xf060, 0xfff0, 0, FMT_D0, 0, {MEM(AM0), DN1}},
253{ "movhu", 0xf070, 0xfff0, 0, FMT_D0, 0, {DM1, MEM(AN0)}},
254{ "movhu", 0xf480, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), DN2}},
255{ "movhu", 0xf4c0, 0xffc0, 0, FMT_D0, 0, {DM2, MEM2(DI, AN0)}},
256{ "movhu", 0xf86000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8, AM0), DN1}},
257{ "movhu", 0xf87000, 0xfff000, 0, FMT_D1, 0, {DM1, MEM2(SD8, AN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000258{ "movhu", 0xf89300, 0xfff300, 0, FMT_D1, 0, {DM1, MEM2(IMM8, SP)}},
259{ "movhu", 0xf8bc00, 0xfffc00, 0, FMT_D1, 0, {MEM2(IMM8, SP), DN0}},
David Howellsb920de12008-02-08 04:19:31 -0800260{ "movhu", 0xf94a00, 0xffff00, 0, FMT_D6, AM33, {MEM(RM0), RN2}},
261{ "movhu", 0xf95a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEM(RN0)}},
262{ "movhu", 0xf9ea00, 0xffff00, 0x12, FMT_D6, AM33, {MEMINC(RM0), RN2}},
263{ "movhu", 0xf9fa00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEMINC(RN0)}},
264{ "movhu", 0xfa600000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), DN1}},
265{ "movhu", 0xfa700000, 0xfff00000, 0, FMT_D2, 0, {DM1, MEM2(SD16, AN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000266{ "movhu", 0xfa930000, 0xfff30000, 0, FMT_D2, 0, {DM1, MEM2(IMM16, SP)}},
267{ "movhu", 0xfabc0000, 0xfffc0000, 0, FMT_D2, 0, {MEM2(IMM16, SP), DN0}},
David Howellsb920de12008-02-08 04:19:31 -0800268{ "movhu", 0xfb4a0000, 0xffff0000, 0, FMT_D7, AM33, {MEM2(SD8, RM0), RN2}},
269{ "movhu", 0xfb5a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEM2(SD8, RN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000270{ "movhu", 0xfbca0000, 0xffff0f00, 0, FMT_D7, AM33, {MEM2(IMM8, SP), RN2}},
David Howellsb920de12008-02-08 04:19:31 -0800271{ "movhu", 0xfbce0000, 0xffff000f, 0, FMT_D7, AM33, {MEM2(RI, RM0), RD2}},
David Howellsee6e7402008-11-12 15:35:09 +0000272{ "movhu", 0xfbda0000, 0xffff0f00, 0, FMT_D7, AM33, {RM2, MEM2(IMM8, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800273{ "movhu", 0xfbde0000, 0xffff000f, 0, FMT_D7, AM33, {RD2, MEM2(RI, RN0)}},
274{ "movhu", 0xfbea0000, 0xffff0000, 0x22, FMT_D7, AM33, {MEMINC2 (RM0, SIMM8), RN2}},
275{ "movhu", 0xfbfa0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEMINC2 (RN0, SIMM8)}},
276{ "movhu", 0xfc600000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), DN1}},
277{ "movhu", 0xfc700000, 0xfff00000, 0, FMT_D4, 0, {DM1, MEM2(IMM32,AN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000278{ "movhu", 0xfc830000, 0xfff30000, 0, FMT_D4, 0, {DM1, MEM(IMM32_MEM)}},
279{ "movhu", 0xfc930000, 0xfff30000, 0, FMT_D4, 0, {DM1, MEM2(IMM32, SP)}},
280{ "movhu", 0xfcac0000, 0xfffc0000, 0, FMT_D4, 0, {MEM(IMM32_MEM), DN0}},
281{ "movhu", 0xfcbc0000, 0xfffc0000, 0, FMT_D4, 0, {MEM2(IMM32, SP), DN0}},
David Howellsb920de12008-02-08 04:19:31 -0800282{ "movhu", 0xfd4a0000, 0xffff0000, 0, FMT_D8, AM33, {MEM2(SD24, RM0), RN2}},
283{ "movhu", 0xfd5a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEM2(SD24, RN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000284{ "movhu", 0xfdca0000, 0xffff0f00, 0, FMT_D8, AM33, {MEM2(IMM24, SP), RN2}},
285{ "movhu", 0xfdda0000, 0xffff0f00, 0, FMT_D8, AM33, {RM2, MEM2(IMM24, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800286{ "movhu", 0xfdea0000, 0xffff0000, 0x22, FMT_D8, AM33, {MEMINC2 (RM0, IMM24), RN2}},
287{ "movhu", 0xfdfa0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEMINC2 (RN0, IMM24)}},
288{ "movhu", 0xfe4a0000, 0xffff0000, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8,RM0), RN2}},
David Howellsee6e7402008-11-12 15:35:09 +0000289{ "movhu", 0xfe4e0000, 0xffff0f00, 0, FMT_D9, AM33, {MEM(IMM32_HIGH8_MEM), RN2}},
David Howellsb920de12008-02-08 04:19:31 -0800290{ "movhu", 0xfe5a0000, 0xffff0000, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, RN0)}},
David Howellsee6e7402008-11-12 15:35:09 +0000291{ "movhu", 0xfe5e0000, 0xffff0f00, 0, FMT_D9, AM33, {RM2, MEM(IMM32_HIGH8_MEM)}},
292{ "movhu", 0xfeca0000, 0xffff0f00, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8, SP), RN2}},
293{ "movhu", 0xfeda0000, 0xffff0f00, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, SP)}},
David Howellsb920de12008-02-08 04:19:31 -0800294{ "movhu", 0xfeea0000, 0xffff0000, 0x22, FMT_D9, AM33, {MEMINC2 (RM0, IMM32_HIGH8), RN2}},
295{ "movhu", 0xfefa0000, 0xffff0000, 0, FMT_D9, AM33, {RN2, MEMINC2 (RM0, IMM32_HIGH8)}},
296{ 0, 0, 0, 0, 0, 0, {0}},
297};
298
299/*
300 * fix up misalignment problems where possible
301 */
302asmlinkage void misalignment(struct pt_regs *regs, enum exception_code code)
303{
304 const struct exception_table_entry *fixup;
305 const struct mn10300_opcode *pop;
306 unsigned long *registers = (unsigned long *) regs;
David Howellsb308bf32008-11-12 15:35:14 +0000307 unsigned long data, *store, *postinc, disp;
David Howellsb920de12008-02-08 04:19:31 -0800308 mm_segment_t seg;
309 siginfo_t info;
David Howellsb308bf32008-11-12 15:35:14 +0000310 uint32_t opcode, noc, xo, xm;
David Howellsb920de12008-02-08 04:19:31 -0800311 uint8_t *pc, byte;
312 void *address;
David Howellsb308bf32008-11-12 15:35:14 +0000313 unsigned tmp, npop, dispsz, loop;
David Howellsb920de12008-02-08 04:19:31 -0800314
David Howells31ea24b2008-11-12 15:34:59 +0000315 kdebug("==>misalignment({pc=%lx})", regs->pc);
David Howellsb920de12008-02-08 04:19:31 -0800316
David Howellsb920de12008-02-08 04:19:31 -0800317 if (regs->epsw & EPSW_IE)
318 asm volatile("or %0,epsw" : : "i"(EPSW_IE));
319
320 seg = get_fs();
321 set_fs(KERNEL_DS);
322
323 fixup = search_exception_tables(regs->pc);
324
325 /* first thing to do is to match the opcode */
326 pc = (u_int8_t *) regs->pc;
327
328 if (__get_user(byte, pc) != 0)
329 goto fetch_error;
330 opcode = byte;
331 noc = 8;
332
333 for (pop = mn10300_opcodes; pop->name; pop++) {
David Howells31ea24b2008-11-12 15:34:59 +0000334 npop = ilog2(pop->opcode | pop->opmask);
David Howellsb920de12008-02-08 04:19:31 -0800335 if (npop <= 0 || npop > 31)
336 continue;
337 npop = (npop + 8) & ~7;
338
339 got_more_bits:
340 if (npop == noc) {
341 if ((opcode & pop->opmask) == pop->opcode)
342 goto found_opcode;
343 } else if (npop > noc) {
344 xo = pop->opcode >> (npop - noc);
345 xm = pop->opmask >> (npop - noc);
346
347 if ((opcode & xm) != xo)
348 continue;
349
350 /* we've got a partial match (an exact match on the
351 * first N bytes), so we need to get some more data */
352 pc++;
353 if (__get_user(byte, pc) != 0)
354 goto fetch_error;
355 opcode = opcode << 8 | byte;
356 noc += 8;
357 goto got_more_bits;
358 } else {
359 /* there's already been a partial match as long as the
360 * complete match we're now considering, so this one
361 * should't match */
362 continue;
363 }
364 }
365
366 /* didn't manage to find a fixup */
367 if (!user_mode(regs))
368 printk(KERN_CRIT "MISALIGN: %lx: unsupported instruction %x\n",
369 regs->pc, opcode);
370
371failed:
372 set_fs(seg);
373 if (die_if_no_fixup("misalignment error", regs, code))
374 return;
375
376 info.si_signo = SIGBUS;
377 info.si_errno = 0;
378 info.si_code = BUS_ADRALN;
379 info.si_addr = (void *) regs->pc;
380 force_sig_info(SIGBUS, &info, current);
381 return;
382
383 /* error reading opcodes */
384fetch_error:
385 if (!user_mode(regs))
386 printk(KERN_CRIT
387 "MISALIGN: %p: fault whilst reading instruction data\n",
388 pc);
389 goto failed;
390
391bad_addr_mode:
392 if (!user_mode(regs))
393 printk(KERN_CRIT
394 "MISALIGN: %lx: unsupported addressing mode %x\n",
395 regs->pc, opcode);
396 goto failed;
397
398bad_reg_mode:
399 if (!user_mode(regs))
400 printk(KERN_CRIT
401 "MISALIGN: %lx: unsupported register mode %x\n",
402 regs->pc, opcode);
403 goto failed;
404
405unsupported_instruction:
406 if (!user_mode(regs))
407 printk(KERN_CRIT
408 "MISALIGN: %lx: unsupported instruction %x (%s)\n",
409 regs->pc, opcode, pop->name);
410 goto failed;
411
412transfer_failed:
413 set_fs(seg);
414 if (fixup) {
415 regs->pc = fixup->fixup;
416 return;
417 }
418 if (die_if_no_fixup("misalignment fixup", regs, code))
419 return;
420
421 info.si_signo = SIGSEGV;
422 info.si_errno = 0;
423 info.si_code = 0;
424 info.si_addr = (void *) regs->pc;
425 force_sig_info(SIGSEGV, &info, current);
426 return;
427
428 /* we matched the opcode */
429found_opcode:
David Howells31ea24b2008-11-12 15:34:59 +0000430 kdebug("%lx: %x==%x { %x, %x }",
David Howellsb920de12008-02-08 04:19:31 -0800431 regs->pc, opcode, pop->opcode, pop->params[0], pop->params[1]);
432
433 tmp = format_tbl[pop->format].opsz;
434 if (tmp > noc)
435 BUG(); /* match was less complete than it ought to have been */
436
437 if (tmp < noc) {
438 tmp = noc - tmp;
439 opcode >>= tmp;
440 pc -= tmp >> 3;
441 }
442
443 /* grab the extra displacement (note it's LSB first) */
444 disp = 0;
David Howellsb308bf32008-11-12 15:35:14 +0000445 dispsz = format_tbl[pop->format].dispsz;
446 for (loop = 0; loop < dispsz; loop += 8) {
David Howellsb920de12008-02-08 04:19:31 -0800447 pc++;
448 if (__get_user(byte, pc) != 0)
449 goto fetch_error;
David Howellsb308bf32008-11-12 15:35:14 +0000450 disp |= byte << loop;
451 kdebug("{%p} disp[%02x]=%02x", pc, loop, byte);
David Howellsb920de12008-02-08 04:19:31 -0800452 }
453
David Howellsb308bf32008-11-12 15:35:14 +0000454 kdebug("disp=%lx", disp);
455
David Howellsb920de12008-02-08 04:19:31 -0800456 set_fs(KERNEL_XDS);
457 if (fixup || regs->epsw & EPSW_nSL)
458 set_fs(seg);
459
460 tmp = (pop->params[0] ^ pop->params[1]) & 0x80000000;
461 if (!tmp) {
462 if (!user_mode(regs))
463 printk(KERN_CRIT
464 "MISALIGN: %lx:"
465 " insn not move to/from memory %x\n",
466 regs->pc, opcode);
467 goto failed;
468 }
469
470 if (pop->params[0] & 0x80000000) {
471 /* move memory to register */
472 if (!misalignment_addr(registers, pop->params[0], opcode, disp,
473 &address, &postinc))
474 goto bad_addr_mode;
475
476 if (!misalignment_reg(registers, pop->params[1], opcode, disp,
477 &store))
478 goto bad_reg_mode;
479
480 if (strcmp(pop->name, "mov") == 0) {
David Howells31ea24b2008-11-12 15:34:59 +0000481 kdebug("mov (%p),DARn", address);
David Howellsb920de12008-02-08 04:19:31 -0800482 if (copy_from_user(&data, (void *) address, 4) != 0)
483 goto transfer_failed;
484 if (pop->params[0] & 0x1000000)
485 *postinc += 4;
486 } else if (strcmp(pop->name, "movhu") == 0) {
David Howells31ea24b2008-11-12 15:34:59 +0000487 kdebug("movhu (%p),DARn", address);
David Howellsb920de12008-02-08 04:19:31 -0800488 data = 0;
489 if (copy_from_user(&data, (void *) address, 2) != 0)
490 goto transfer_failed;
491 if (pop->params[0] & 0x1000000)
492 *postinc += 2;
493 } else {
494 goto unsupported_instruction;
495 }
496
497 *store = data;
498 } else {
499 /* move register to memory */
500 if (!misalignment_reg(registers, pop->params[0], opcode, disp,
501 &store))
502 goto bad_reg_mode;
503
504 if (!misalignment_addr(registers, pop->params[1], opcode, disp,
505 &address, &postinc))
506 goto bad_addr_mode;
507
508 data = *store;
509
510 if (strcmp(pop->name, "mov") == 0) {
David Howells31ea24b2008-11-12 15:34:59 +0000511 kdebug("mov %lx,(%p)", data, address);
David Howellsb920de12008-02-08 04:19:31 -0800512 if (copy_to_user((void *) address, &data, 4) != 0)
513 goto transfer_failed;
514 if (pop->params[1] & 0x1000000)
515 *postinc += 4;
516 } else if (strcmp(pop->name, "movhu") == 0) {
David Howells31ea24b2008-11-12 15:34:59 +0000517 kdebug("movhu %hx,(%p)", (uint16_t) data, address);
David Howellsb920de12008-02-08 04:19:31 -0800518 if (copy_to_user((void *) address, &data, 2) != 0)
519 goto transfer_failed;
520 if (pop->params[1] & 0x1000000)
521 *postinc += 2;
522 } else {
523 goto unsupported_instruction;
524 }
525 }
526
527 tmp = format_tbl[pop->format].opsz + format_tbl[pop->format].dispsz;
528 regs->pc += tmp >> 3;
529
530 set_fs(seg);
531 return;
532}
533
534/*
535 * determine the address that was being accessed
536 */
537static int misalignment_addr(unsigned long *registers, unsigned params,
David Howellsb308bf32008-11-12 15:35:14 +0000538 unsigned opcode, unsigned long disp,
David Howellsb920de12008-02-08 04:19:31 -0800539 void **_address, unsigned long **_postinc)
540{
541 unsigned long *postinc = NULL, address = 0, tmp;
542
David Howellsf911c682008-11-12 15:35:20 +0000543 params &= 0x00ffffff;
David Howellsb920de12008-02-08 04:19:31 -0800544
545 do {
546 switch (params & 0xff) {
547 case DM0:
548 postinc = &registers[Dreg_index[opcode & 0x03]];
549 address += *postinc;
550 break;
551 case DM1:
552 postinc = &registers[Dreg_index[opcode >> 2 & 0x0c]];
553 address += *postinc;
554 break;
555 case DM2:
556 postinc = &registers[Dreg_index[opcode >> 4 & 0x30]];
557 address += *postinc;
558 break;
559 case AM0:
560 postinc = &registers[Areg_index[opcode & 0x03]];
561 address += *postinc;
562 break;
563 case AM1:
564 postinc = &registers[Areg_index[opcode >> 2 & 0x0c]];
565 address += *postinc;
566 break;
567 case AM2:
568 postinc = &registers[Areg_index[opcode >> 4 & 0x30]];
569 address += *postinc;
570 break;
571 case RM0:
572 postinc = &registers[Rreg_index[opcode & 0x0f]];
573 address += *postinc;
574 break;
575 case RM1:
576 postinc = &registers[Rreg_index[opcode >> 2 & 0x0f]];
577 address += *postinc;
578 break;
579 case RM2:
580 postinc = &registers[Rreg_index[opcode >> 4 & 0x0f]];
581 address += *postinc;
582 break;
583 case RM4:
584 postinc = &registers[Rreg_index[opcode >> 8 & 0x0f]];
585 address += *postinc;
586 break;
587 case RM6:
588 postinc = &registers[Rreg_index[opcode >> 12 & 0x0f]];
589 address += *postinc;
590 break;
591 case RD0:
592 postinc = &registers[Rreg_index[disp & 0x0f]];
593 address += *postinc;
594 break;
595 case RD2:
596 postinc = &registers[Rreg_index[disp >> 4 & 0x0f]];
597 address += *postinc;
598 break;
David Howellsee6e7402008-11-12 15:35:09 +0000599 case SP:
600 address += registers[REG_SP >> 2];
601 break;
David Howellsb920de12008-02-08 04:19:31 -0800602
603 case SD8:
604 case SIMM8:
605 address += (int32_t) (int8_t) (disp & 0xff);
606 break;
607 case SD16:
608 address += (int32_t) (int16_t) (disp & 0xffff);
609 break;
610 case SD24:
611 tmp = disp << 8;
612 asm("asr 8,%0" : "=r"(tmp) : "0"(tmp));
613 address += tmp;
614 break;
615 case SIMM4_2:
616 tmp = opcode >> 4 & 0x0f;
617 tmp <<= 28;
618 asm("asr 28,%0" : "=r"(tmp) : "0"(tmp));
619 address += tmp;
620 break;
621 case IMM24:
622 address += disp & 0x00ffffff;
623 break;
624 case IMM32:
David Howellsee6e7402008-11-12 15:35:09 +0000625 case IMM32_MEM:
David Howellsb920de12008-02-08 04:19:31 -0800626 case IMM32_HIGH8:
David Howellsee6e7402008-11-12 15:35:09 +0000627 case IMM32_HIGH8_MEM:
David Howellsb920de12008-02-08 04:19:31 -0800628 address += disp;
629 break;
630 default:
David Howellsf911c682008-11-12 15:35:20 +0000631 BUG();
David Howellsb920de12008-02-08 04:19:31 -0800632 return 0;
633 }
634 } while ((params >>= 8));
635
636 *_address = (void *) address;
637 *_postinc = postinc;
638 return 1;
639}
640
641/*
642 * determine the register that is acting as source/dest
643 */
644static int misalignment_reg(unsigned long *registers, unsigned params,
David Howellsb308bf32008-11-12 15:35:14 +0000645 unsigned opcode, unsigned long disp,
David Howellsb920de12008-02-08 04:19:31 -0800646 unsigned long **_register)
647{
648 params &= 0x7fffffff;
649
650 if (params & 0xffffff00)
651 return 0;
652
653 switch (params & 0xff) {
654 case DM0:
655 *_register = &registers[Dreg_index[opcode & 0x03]];
656 break;
657 case DM1:
658 *_register = &registers[Dreg_index[opcode >> 2 & 0x03]];
659 break;
660 case DM2:
661 *_register = &registers[Dreg_index[opcode >> 4 & 0x03]];
662 break;
663 case AM0:
664 *_register = &registers[Areg_index[opcode & 0x03]];
665 break;
666 case AM1:
667 *_register = &registers[Areg_index[opcode >> 2 & 0x03]];
668 break;
669 case AM2:
670 *_register = &registers[Areg_index[opcode >> 4 & 0x03]];
671 break;
672 case RM0:
673 *_register = &registers[Rreg_index[opcode & 0x0f]];
674 break;
675 case RM1:
676 *_register = &registers[Rreg_index[opcode >> 2 & 0x0f]];
677 break;
678 case RM2:
679 *_register = &registers[Rreg_index[opcode >> 4 & 0x0f]];
680 break;
681 case RM4:
682 *_register = &registers[Rreg_index[opcode >> 8 & 0x0f]];
683 break;
684 case RM6:
685 *_register = &registers[Rreg_index[opcode >> 12 & 0x0f]];
686 break;
687 case RD0:
688 *_register = &registers[Rreg_index[disp & 0x0f]];
689 break;
690 case RD2:
691 *_register = &registers[Rreg_index[disp >> 4 & 0x0f]];
692 break;
693 case SP:
694 *_register = &registers[REG_SP >> 2];
695 break;
696
697 default:
David Howellsf911c682008-11-12 15:35:20 +0000698 BUG();
David Howellsb920de12008-02-08 04:19:31 -0800699 return 0;
700 }
701
702 return 1;
703}
David Howells9f555882008-11-12 15:35:04 +0000704
705/*
706 * misalignment handler tests
707 */
708#ifdef CONFIG_TEST_MISALIGNMENT_HANDLER
709static u8 __initdata testbuf[512] __attribute__((aligned(16))) = {
710 [257] = 0x11,
711 [258] = 0x22,
712 [259] = 0x33,
713 [260] = 0x44,
714};
715
716#define ASSERTCMP(X, OP, Y) \
717do { \
718 if (unlikely(!((X) OP (Y)))) { \
719 printk(KERN_ERR "\n"); \
720 printk(KERN_ERR "MISALIGN: Assertion failed at line %u\n", \
721 __LINE__); \
722 printk(KERN_ERR "0x%lx " #OP " 0x%lx is false\n", \
723 (unsigned long)(X), (unsigned long)(Y)); \
724 BUG(); \
725 } \
726} while(0)
727
728static int __init test_misalignment(void)
729{
730 register void *r asm("e0");
731 register u32 y asm("e1");
732 void *p = testbuf, *q;
733 u32 tmp, tmp2, x;
734
735 printk(KERN_NOTICE "==>test_misalignment() [testbuf=%p]\n", p);
736 p++;
737
738 printk(KERN_NOTICE "___ MOV (Am),Dn ___\n");
739 q = p + 256;
740 asm volatile("mov (%0),%1" : "+a"(q), "=d"(x));
741 ASSERTCMP(q, ==, p + 256);
742 ASSERTCMP(x, ==, 0x44332211);
743
744 printk(KERN_NOTICE "___ MOV (256,Am),Dn ___\n");
745 q = p;
746 asm volatile("mov (256,%0),%1" : "+a"(q), "=d"(x));
747 ASSERTCMP(q, ==, p);
748 ASSERTCMP(x, ==, 0x44332211);
749
750 printk(KERN_NOTICE "___ MOV (Di,Am),Dn ___\n");
751 tmp = 256;
752 q = p;
753 asm volatile("mov (%2,%0),%1" : "+a"(q), "=d"(x), "+d"(tmp));
754 ASSERTCMP(q, ==, p);
755 ASSERTCMP(x, ==, 0x44332211);
756 ASSERTCMP(tmp, ==, 256);
757
758 printk(KERN_NOTICE "___ MOV (256,Rm),Rn ___\n");
759 r = p;
760 asm volatile("mov (256,%0),%1" : "+r"(r), "=r"(y));
761 ASSERTCMP(r, ==, p);
762 ASSERTCMP(y, ==, 0x44332211);
763
764 printk(KERN_NOTICE "___ MOV (Rm+),Rn ___\n");
765 r = p + 256;
766 asm volatile("mov (%0+),%1" : "+r"(r), "=r"(y));
767 ASSERTCMP(r, ==, p + 256 + 4);
768 ASSERTCMP(y, ==, 0x44332211);
769
770 printk(KERN_NOTICE "___ MOV (Rm+,8),Rn ___\n");
771 r = p + 256;
772 asm volatile("mov (%0+,8),%1" : "+r"(r), "=r"(y));
773 ASSERTCMP(r, ==, p + 256 + 8);
774 ASSERTCMP(y, ==, 0x44332211);
775
776 printk(KERN_NOTICE "___ MOV (7,SP),Rn ___\n");
777 asm volatile(
778 "add -16,sp \n"
779 "mov +0x11,%0 \n"
780 "movbu %0,(7,sp) \n"
781 "mov +0x22,%0 \n"
782 "movbu %0,(8,sp) \n"
783 "mov +0x33,%0 \n"
784 "movbu %0,(9,sp) \n"
785 "mov +0x44,%0 \n"
786 "movbu %0,(10,sp) \n"
787 "mov (7,sp),%1 \n"
788 "add +16,sp \n"
789 : "+a"(q), "=d"(x));
790 ASSERTCMP(x, ==, 0x44332211);
791
792 printk(KERN_NOTICE "___ MOV (259,SP),Rn ___\n");
793 asm volatile(
794 "add -264,sp \n"
795 "mov +0x11,%0 \n"
796 "movbu %0,(259,sp) \n"
797 "mov +0x22,%0 \n"
798 "movbu %0,(260,sp) \n"
799 "mov +0x33,%0 \n"
800 "movbu %0,(261,sp) \n"
801 "mov +0x55,%0 \n"
802 "movbu %0,(262,sp) \n"
803 "mov (259,sp),%1 \n"
804 "add +264,sp \n"
805 : "+d"(tmp), "=d"(x));
806 ASSERTCMP(x, ==, 0x55332211);
807
808 printk(KERN_NOTICE "___ MOV (260,SP),Rn ___\n");
809 asm volatile(
810 "add -264,sp \n"
811 "mov +0x11,%0 \n"
812 "movbu %0,(260,sp) \n"
813 "mov +0x22,%0 \n"
814 "movbu %0,(261,sp) \n"
815 "mov +0x33,%0 \n"
816 "movbu %0,(262,sp) \n"
817 "mov +0x55,%0 \n"
818 "movbu %0,(263,sp) \n"
819 "mov (260,sp),%1 \n"
820 "add +264,sp \n"
821 : "+d"(tmp), "=d"(x));
822 ASSERTCMP(x, ==, 0x55332211);
823
824
825 printk(KERN_NOTICE "___ MOV_LNE ___\n");
826 tmp = 1;
827 tmp2 = 2;
828 q = p + 256;
829 asm volatile(
830 "setlb \n"
831 "mov %2,%3 \n"
832 "mov %1,%2 \n"
833 "cmp +0,%1 \n"
834 "mov_lne (%0+,4),%1"
835 : "+r"(q), "+d"(tmp), "+d"(tmp2), "=d"(x)
836 :
837 : "cc");
838 ASSERTCMP(q, ==, p + 256 + 12);
839 ASSERTCMP(x, ==, 0x44332211);
840
841 printk(KERN_NOTICE "___ MOV in SETLB ___\n");
842 tmp = 1;
843 tmp2 = 2;
844 q = p + 256;
845 asm volatile(
846 "setlb \n"
847 "mov %1,%3 \n"
848 "mov (%0+),%1 \n"
849 "cmp +0,%1 \n"
850 "lne "
851 : "+a"(q), "+d"(tmp), "+d"(tmp2), "=d"(x)
852 :
853 : "cc");
854
855 ASSERTCMP(q, ==, p + 256 + 8);
856 ASSERTCMP(x, ==, 0x44332211);
857
858 printk(KERN_NOTICE "<==test_misalignment()\n");
859 return 0;
860}
861
862arch_initcall(test_misalignment);
863
864#endif /* CONFIG_TEST_MISALIGNMENT_HANDLER */