misalignment.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /* 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. #if 0
  39. #define kdebug(FMT, ...) printk(KERN_DEBUG "MISALIGN: "FMT"\n", ##__VA_ARGS__)
  40. #else
  41. #define kdebug(FMT, ...) do {} while (0)
  42. #endif
  43. static int misalignment_addr(unsigned long *registers, unsigned params,
  44. unsigned opcode, unsigned disp,
  45. void **_address, unsigned long **_postinc);
  46. static int misalignment_reg(unsigned long *registers, unsigned params,
  47. unsigned opcode, unsigned disp,
  48. unsigned long **_register);
  49. static const unsigned Dreg_index[] = {
  50. REG_D0 >> 2, REG_D1 >> 2, REG_D2 >> 2, REG_D3 >> 2
  51. };
  52. static const unsigned Areg_index[] = {
  53. REG_A0 >> 2, REG_A1 >> 2, REG_A2 >> 2, REG_A3 >> 2
  54. };
  55. static const unsigned Rreg_index[] = {
  56. REG_E0 >> 2, REG_E1 >> 2, REG_E2 >> 2, REG_E3 >> 2,
  57. REG_E4 >> 2, REG_E5 >> 2, REG_E6 >> 2, REG_E7 >> 2,
  58. REG_A0 >> 2, REG_A1 >> 2, REG_A2 >> 2, REG_A3 >> 2,
  59. REG_D0 >> 2, REG_D1 >> 2, REG_D2 >> 2, REG_D3 >> 2
  60. };
  61. enum format_id {
  62. FMT_S0,
  63. FMT_S1,
  64. FMT_S2,
  65. FMT_S4,
  66. FMT_D0,
  67. FMT_D1,
  68. FMT_D2,
  69. FMT_D4,
  70. FMT_D6,
  71. FMT_D7,
  72. FMT_D8,
  73. FMT_D9,
  74. };
  75. static const struct {
  76. u_int8_t opsz, dispsz;
  77. } format_tbl[16] = {
  78. [FMT_S0] = { 8, 0 },
  79. [FMT_S1] = { 8, 8 },
  80. [FMT_S2] = { 8, 16 },
  81. [FMT_S4] = { 8, 32 },
  82. [FMT_D0] = { 16, 0 },
  83. [FMT_D1] = { 16, 8 },
  84. [FMT_D2] = { 16, 16 },
  85. [FMT_D4] = { 16, 32 },
  86. [FMT_D6] = { 24, 0 },
  87. [FMT_D7] = { 24, 8 },
  88. [FMT_D8] = { 24, 24 },
  89. [FMT_D9] = { 24, 32 },
  90. };
  91. enum value_id {
  92. DM0, /* data reg in opcode in bits 0-1 */
  93. DM1, /* data reg in opcode in bits 2-3 */
  94. DM2, /* data reg in opcode in bits 4-5 */
  95. AM0, /* addr reg in opcode in bits 0-1 */
  96. AM1, /* addr reg in opcode in bits 2-3 */
  97. AM2, /* addr reg in opcode in bits 4-5 */
  98. RM0, /* reg in opcode in bits 0-3 */
  99. RM1, /* reg in opcode in bits 2-5 */
  100. RM2, /* reg in opcode in bits 4-7 */
  101. RM4, /* reg in opcode in bits 8-11 */
  102. RM6, /* reg in opcode in bits 12-15 */
  103. RD0, /* reg in displacement in bits 0-3 */
  104. RD2, /* reg in displacement in bits 4-7 */
  105. SP, /* stack pointer */
  106. SD8, /* 8-bit signed displacement */
  107. SD16, /* 16-bit signed displacement */
  108. SD24, /* 24-bit signed displacement */
  109. SIMM4_2, /* 4-bit signed displacement in opcode bits 4-7 */
  110. SIMM8, /* 8-bit signed immediate */
  111. IMM24, /* 24-bit unsigned immediate */
  112. IMM32, /* 32-bit unsigned immediate */
  113. IMM32_HIGH8, /* 32-bit unsigned immediate, high 8-bits in opcode */
  114. DN0 = DM0,
  115. DN1 = DM1,
  116. DN2 = DM2,
  117. AN0 = AM0,
  118. AN1 = AM1,
  119. AN2 = AM2,
  120. RN0 = RM0,
  121. RN1 = RM1,
  122. RN2 = RM2,
  123. RN4 = RM4,
  124. RN6 = RM6,
  125. DI = DM1,
  126. RI = RM2,
  127. };
  128. struct mn10300_opcode {
  129. const char *name;
  130. u_int32_t opcode;
  131. u_int32_t opmask;
  132. unsigned exclusion;
  133. enum format_id format;
  134. unsigned cpu_mask;
  135. #define AM33 330
  136. unsigned params[2];
  137. #define MEM(ADDR) (0x80000000 | (ADDR))
  138. #define MEM2(ADDR1, ADDR2) (0x80000000 | (ADDR1) << 8 | (ADDR2))
  139. #define MEMINC(ADDR) (0x81000000 | (ADDR))
  140. #define MEMINC2(ADDR, INC) (0x81000000 | (ADDR) << 8 | (INC))
  141. };
  142. /* LIBOPCODES EXCERPT
  143. Assemble Matsushita MN10300 instructions.
  144. Copyright 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  145. This program is free software; you can redistribute it and/or modify
  146. it under the terms of the GNU General Public Licence as published by
  147. the Free Software Foundation; either version 2 of the Licence, or
  148. (at your option) any later version.
  149. This program is distributed in the hope that it will be useful,
  150. but WITHOUT ANY WARRANTY; without even the implied warranty of
  151. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  152. GNU General Public Licence for more details.
  153. You should have received a copy of the GNU General Public Licence
  154. along with this program; if not, write to the Free Software
  155. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  156. */
  157. static const struct mn10300_opcode mn10300_opcodes[] = {
  158. { "mov", 0x60, 0xf0, 0, FMT_S0, 0, {DM1, MEM(AN0)}},
  159. { "mov", 0x70, 0xf0, 0, FMT_S0, 0, {MEM(AM0), DN1}},
  160. { "mov", 0xf000, 0xfff0, 0, FMT_D0, 0, {MEM(AM0), AN1}},
  161. { "mov", 0xf010, 0xfff0, 0, FMT_D0, 0, {AM1, MEM(AN0)}},
  162. { "mov", 0xf300, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), DN2}},
  163. { "mov", 0xf340, 0xffc0, 0, FMT_D0, 0, {DM2, MEM2(DI, AN0)}},
  164. { "mov", 0xf380, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), AN2}},
  165. { "mov", 0xf3c0, 0xffc0, 0, FMT_D0, 0, {AM2, MEM2(DI, AN0)}},
  166. { "mov", 0xf80000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8, AM0), DN1}},
  167. { "mov", 0xf81000, 0xfff000, 0, FMT_D1, 0, {DM1, MEM2(SD8, AN0)}},
  168. { "mov", 0xf82000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8,AM0), AN1}},
  169. { "mov", 0xf83000, 0xfff000, 0, FMT_D1, 0, {AM1, MEM2(SD8, AN0)}},
  170. { "mov", 0xf8f000, 0xfffc00, 0, FMT_D1, AM33, {MEM2(SD8, AM0), SP}},
  171. { "mov", 0xf8f400, 0xfffc00, 0, FMT_D1, AM33, {SP, MEM2(SD8, AN0)}},
  172. { "mov", 0xf90a00, 0xffff00, 0, FMT_D6, AM33, {MEM(RM0), RN2}},
  173. { "mov", 0xf91a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEM(RN0)}},
  174. { "mov", 0xf96a00, 0xffff00, 0x12, FMT_D6, AM33, {MEMINC(RM0), RN2}},
  175. { "mov", 0xf97a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEMINC(RN0)}},
  176. { "mov", 0xfa000000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), DN1}},
  177. { "mov", 0xfa100000, 0xfff00000, 0, FMT_D2, 0, {DM1, MEM2(SD16, AN0)}},
  178. { "mov", 0xfa200000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), AN1}},
  179. { "mov", 0xfa300000, 0xfff00000, 0, FMT_D2, 0, {AM1, MEM2(SD16, AN0)}},
  180. { "mov", 0xfb0a0000, 0xffff0000, 0, FMT_D7, AM33, {MEM2(SD8, RM0), RN2}},
  181. { "mov", 0xfb1a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEM2(SD8, RN0)}},
  182. { "mov", 0xfb6a0000, 0xffff0000, 0x22, FMT_D7, AM33, {MEMINC2 (RM0, SIMM8), RN2}},
  183. { "mov", 0xfb7a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEMINC2 (RN0, SIMM8)}},
  184. { "mov", 0xfb8e0000, 0xffff000f, 0, FMT_D7, AM33, {MEM2(RI, RM0), RD2}},
  185. { "mov", 0xfb9e0000, 0xffff000f, 0, FMT_D7, AM33, {RD2, MEM2(RI, RN0)}},
  186. { "mov", 0xfc000000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), DN1}},
  187. { "mov", 0xfc100000, 0xfff00000, 0, FMT_D4, 0, {DM1, MEM2(IMM32,AN0)}},
  188. { "mov", 0xfc200000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), AN1}},
  189. { "mov", 0xfc300000, 0xfff00000, 0, FMT_D4, 0, {AM1, MEM2(IMM32,AN0)}},
  190. { "mov", 0xfd0a0000, 0xffff0000, 0, FMT_D8, AM33, {MEM2(SD24, RM0), RN2}},
  191. { "mov", 0xfd1a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEM2(SD24, RN0)}},
  192. { "mov", 0xfd6a0000, 0xffff0000, 0x22, FMT_D8, AM33, {MEMINC2 (RM0, IMM24), RN2}},
  193. { "mov", 0xfd7a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEMINC2 (RN0, IMM24)}},
  194. { "mov", 0xfe0a0000, 0xffff0000, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8,RM0), RN2}},
  195. { "mov", 0xfe1a0000, 0xffff0000, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, RN0)}},
  196. { "mov", 0xfe6a0000, 0xffff0000, 0x22, FMT_D9, AM33, {MEMINC2 (RM0, IMM32_HIGH8), RN2}},
  197. { "mov", 0xfe7a0000, 0xffff0000, 0, FMT_D9, AM33, {RN2, MEMINC2 (RM0, IMM32_HIGH8)}},
  198. { "movhu", 0xf060, 0xfff0, 0, FMT_D0, 0, {MEM(AM0), DN1}},
  199. { "movhu", 0xf070, 0xfff0, 0, FMT_D0, 0, {DM1, MEM(AN0)}},
  200. { "movhu", 0xf480, 0xffc0, 0, FMT_D0, 0, {MEM2(DI, AM0), DN2}},
  201. { "movhu", 0xf4c0, 0xffc0, 0, FMT_D0, 0, {DM2, MEM2(DI, AN0)}},
  202. { "movhu", 0xf86000, 0xfff000, 0, FMT_D1, 0, {MEM2(SD8, AM0), DN1}},
  203. { "movhu", 0xf87000, 0xfff000, 0, FMT_D1, 0, {DM1, MEM2(SD8, AN0)}},
  204. { "movhu", 0xf94a00, 0xffff00, 0, FMT_D6, AM33, {MEM(RM0), RN2}},
  205. { "movhu", 0xf95a00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEM(RN0)}},
  206. { "movhu", 0xf9ea00, 0xffff00, 0x12, FMT_D6, AM33, {MEMINC(RM0), RN2}},
  207. { "movhu", 0xf9fa00, 0xffff00, 0, FMT_D6, AM33, {RM2, MEMINC(RN0)}},
  208. { "movhu", 0xfa600000, 0xfff00000, 0, FMT_D2, 0, {MEM2(SD16, AM0), DN1}},
  209. { "movhu", 0xfa700000, 0xfff00000, 0, FMT_D2, 0, {DM1, MEM2(SD16, AN0)}},
  210. { "movhu", 0xfb4a0000, 0xffff0000, 0, FMT_D7, AM33, {MEM2(SD8, RM0), RN2}},
  211. { "movhu", 0xfb5a0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEM2(SD8, RN0)}},
  212. { "movhu", 0xfbce0000, 0xffff000f, 0, FMT_D7, AM33, {MEM2(RI, RM0), RD2}},
  213. { "movhu", 0xfbde0000, 0xffff000f, 0, FMT_D7, AM33, {RD2, MEM2(RI, RN0)}},
  214. { "movhu", 0xfbea0000, 0xffff0000, 0x22, FMT_D7, AM33, {MEMINC2 (RM0, SIMM8), RN2}},
  215. { "movhu", 0xfbfa0000, 0xffff0000, 0, FMT_D7, AM33, {RM2, MEMINC2 (RN0, SIMM8)}},
  216. { "movhu", 0xfc600000, 0xfff00000, 0, FMT_D4, 0, {MEM2(IMM32,AM0), DN1}},
  217. { "movhu", 0xfc700000, 0xfff00000, 0, FMT_D4, 0, {DM1, MEM2(IMM32,AN0)}},
  218. { "movhu", 0xfd4a0000, 0xffff0000, 0, FMT_D8, AM33, {MEM2(SD24, RM0), RN2}},
  219. { "movhu", 0xfd5a0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEM2(SD24, RN0)}},
  220. { "movhu", 0xfdea0000, 0xffff0000, 0x22, FMT_D8, AM33, {MEMINC2 (RM0, IMM24), RN2}},
  221. { "movhu", 0xfdfa0000, 0xffff0000, 0, FMT_D8, AM33, {RM2, MEMINC2 (RN0, IMM24)}},
  222. { "movhu", 0xfe4a0000, 0xffff0000, 0, FMT_D9, AM33, {MEM2(IMM32_HIGH8,RM0), RN2}},
  223. { "movhu", 0xfe5a0000, 0xffff0000, 0, FMT_D9, AM33, {RM2, MEM2(IMM32_HIGH8, RN0)}},
  224. { "movhu", 0xfeea0000, 0xffff0000, 0x22, FMT_D9, AM33, {MEMINC2 (RM0, IMM32_HIGH8), RN2}},
  225. { "movhu", 0xfefa0000, 0xffff0000, 0, FMT_D9, AM33, {RN2, MEMINC2 (RM0, IMM32_HIGH8)}},
  226. { 0, 0, 0, 0, 0, 0, {0}},
  227. };
  228. /*
  229. * fix up misalignment problems where possible
  230. */
  231. asmlinkage void misalignment(struct pt_regs *regs, enum exception_code code)
  232. {
  233. const struct exception_table_entry *fixup;
  234. const struct mn10300_opcode *pop;
  235. unsigned long *registers = (unsigned long *) regs;
  236. unsigned long data, *store, *postinc;
  237. mm_segment_t seg;
  238. siginfo_t info;
  239. uint32_t opcode, disp, noc, xo, xm;
  240. uint8_t *pc, byte;
  241. void *address;
  242. unsigned tmp, npop;
  243. kdebug("==>misalignment({pc=%lx})", regs->pc);
  244. if (in_interrupt())
  245. die("Misalignment trap in interrupt context", regs, code);
  246. if (regs->epsw & EPSW_IE)
  247. asm volatile("or %0,epsw" : : "i"(EPSW_IE));
  248. seg = get_fs();
  249. set_fs(KERNEL_DS);
  250. fixup = search_exception_tables(regs->pc);
  251. /* first thing to do is to match the opcode */
  252. pc = (u_int8_t *) regs->pc;
  253. if (__get_user(byte, pc) != 0)
  254. goto fetch_error;
  255. opcode = byte;
  256. noc = 8;
  257. for (pop = mn10300_opcodes; pop->name; pop++) {
  258. npop = ilog2(pop->opcode | pop->opmask);
  259. if (npop <= 0 || npop > 31)
  260. continue;
  261. npop = (npop + 8) & ~7;
  262. got_more_bits:
  263. if (npop == noc) {
  264. if ((opcode & pop->opmask) == pop->opcode)
  265. goto found_opcode;
  266. } else if (npop > noc) {
  267. xo = pop->opcode >> (npop - noc);
  268. xm = pop->opmask >> (npop - noc);
  269. if ((opcode & xm) != xo)
  270. continue;
  271. /* we've got a partial match (an exact match on the
  272. * first N bytes), so we need to get some more data */
  273. pc++;
  274. if (__get_user(byte, pc) != 0)
  275. goto fetch_error;
  276. opcode = opcode << 8 | byte;
  277. noc += 8;
  278. goto got_more_bits;
  279. } else {
  280. /* there's already been a partial match as long as the
  281. * complete match we're now considering, so this one
  282. * should't match */
  283. continue;
  284. }
  285. }
  286. /* didn't manage to find a fixup */
  287. if (!user_mode(regs))
  288. printk(KERN_CRIT "MISALIGN: %lx: unsupported instruction %x\n",
  289. regs->pc, opcode);
  290. failed:
  291. set_fs(seg);
  292. if (die_if_no_fixup("misalignment error", regs, code))
  293. return;
  294. info.si_signo = SIGBUS;
  295. info.si_errno = 0;
  296. info.si_code = BUS_ADRALN;
  297. info.si_addr = (void *) regs->pc;
  298. force_sig_info(SIGBUS, &info, current);
  299. return;
  300. /* error reading opcodes */
  301. fetch_error:
  302. if (!user_mode(regs))
  303. printk(KERN_CRIT
  304. "MISALIGN: %p: fault whilst reading instruction data\n",
  305. pc);
  306. goto failed;
  307. bad_addr_mode:
  308. if (!user_mode(regs))
  309. printk(KERN_CRIT
  310. "MISALIGN: %lx: unsupported addressing mode %x\n",
  311. regs->pc, opcode);
  312. goto failed;
  313. bad_reg_mode:
  314. if (!user_mode(regs))
  315. printk(KERN_CRIT
  316. "MISALIGN: %lx: unsupported register mode %x\n",
  317. regs->pc, opcode);
  318. goto failed;
  319. unsupported_instruction:
  320. if (!user_mode(regs))
  321. printk(KERN_CRIT
  322. "MISALIGN: %lx: unsupported instruction %x (%s)\n",
  323. regs->pc, opcode, pop->name);
  324. goto failed;
  325. transfer_failed:
  326. set_fs(seg);
  327. if (fixup) {
  328. regs->pc = fixup->fixup;
  329. return;
  330. }
  331. if (die_if_no_fixup("misalignment fixup", regs, code))
  332. return;
  333. info.si_signo = SIGSEGV;
  334. info.si_errno = 0;
  335. info.si_code = 0;
  336. info.si_addr = (void *) regs->pc;
  337. force_sig_info(SIGSEGV, &info, current);
  338. return;
  339. /* we matched the opcode */
  340. found_opcode:
  341. kdebug("%lx: %x==%x { %x, %x }",
  342. regs->pc, opcode, pop->opcode, pop->params[0], pop->params[1]);
  343. tmp = format_tbl[pop->format].opsz;
  344. if (tmp > noc)
  345. BUG(); /* match was less complete than it ought to have been */
  346. if (tmp < noc) {
  347. tmp = noc - tmp;
  348. opcode >>= tmp;
  349. pc -= tmp >> 3;
  350. }
  351. /* grab the extra displacement (note it's LSB first) */
  352. disp = 0;
  353. tmp = format_tbl[pop->format].dispsz >> 3;
  354. while (tmp > 0) {
  355. tmp--;
  356. disp <<= 8;
  357. pc++;
  358. if (__get_user(byte, pc) != 0)
  359. goto fetch_error;
  360. disp |= byte;
  361. }
  362. set_fs(KERNEL_XDS);
  363. if (fixup || regs->epsw & EPSW_nSL)
  364. set_fs(seg);
  365. tmp = (pop->params[0] ^ pop->params[1]) & 0x80000000;
  366. if (!tmp) {
  367. if (!user_mode(regs))
  368. printk(KERN_CRIT
  369. "MISALIGN: %lx:"
  370. " insn not move to/from memory %x\n",
  371. regs->pc, opcode);
  372. goto failed;
  373. }
  374. if (pop->params[0] & 0x80000000) {
  375. /* move memory to register */
  376. if (!misalignment_addr(registers, pop->params[0], opcode, disp,
  377. &address, &postinc))
  378. goto bad_addr_mode;
  379. if (!misalignment_reg(registers, pop->params[1], opcode, disp,
  380. &store))
  381. goto bad_reg_mode;
  382. if (strcmp(pop->name, "mov") == 0) {
  383. kdebug("mov (%p),DARn", address);
  384. if (copy_from_user(&data, (void *) address, 4) != 0)
  385. goto transfer_failed;
  386. if (pop->params[0] & 0x1000000)
  387. *postinc += 4;
  388. } else if (strcmp(pop->name, "movhu") == 0) {
  389. kdebug("movhu (%p),DARn", address);
  390. data = 0;
  391. if (copy_from_user(&data, (void *) address, 2) != 0)
  392. goto transfer_failed;
  393. if (pop->params[0] & 0x1000000)
  394. *postinc += 2;
  395. } else {
  396. goto unsupported_instruction;
  397. }
  398. *store = data;
  399. } else {
  400. /* move register to memory */
  401. if (!misalignment_reg(registers, pop->params[0], opcode, disp,
  402. &store))
  403. goto bad_reg_mode;
  404. if (!misalignment_addr(registers, pop->params[1], opcode, disp,
  405. &address, &postinc))
  406. goto bad_addr_mode;
  407. data = *store;
  408. if (strcmp(pop->name, "mov") == 0) {
  409. kdebug("mov %lx,(%p)", data, address);
  410. if (copy_to_user((void *) address, &data, 4) != 0)
  411. goto transfer_failed;
  412. if (pop->params[1] & 0x1000000)
  413. *postinc += 4;
  414. } else if (strcmp(pop->name, "movhu") == 0) {
  415. kdebug("movhu %hx,(%p)", (uint16_t) data, address);
  416. if (copy_to_user((void *) address, &data, 2) != 0)
  417. goto transfer_failed;
  418. if (pop->params[1] & 0x1000000)
  419. *postinc += 2;
  420. } else {
  421. goto unsupported_instruction;
  422. }
  423. }
  424. tmp = format_tbl[pop->format].opsz + format_tbl[pop->format].dispsz;
  425. regs->pc += tmp >> 3;
  426. set_fs(seg);
  427. return;
  428. }
  429. /*
  430. * determine the address that was being accessed
  431. */
  432. static int misalignment_addr(unsigned long *registers, unsigned params,
  433. unsigned opcode, unsigned disp,
  434. void **_address, unsigned long **_postinc)
  435. {
  436. unsigned long *postinc = NULL, address = 0, tmp;
  437. params &= 0x7fffffff;
  438. do {
  439. switch (params & 0xff) {
  440. case DM0:
  441. postinc = &registers[Dreg_index[opcode & 0x03]];
  442. address += *postinc;
  443. break;
  444. case DM1:
  445. postinc = &registers[Dreg_index[opcode >> 2 & 0x0c]];
  446. address += *postinc;
  447. break;
  448. case DM2:
  449. postinc = &registers[Dreg_index[opcode >> 4 & 0x30]];
  450. address += *postinc;
  451. break;
  452. case AM0:
  453. postinc = &registers[Areg_index[opcode & 0x03]];
  454. address += *postinc;
  455. break;
  456. case AM1:
  457. postinc = &registers[Areg_index[opcode >> 2 & 0x0c]];
  458. address += *postinc;
  459. break;
  460. case AM2:
  461. postinc = &registers[Areg_index[opcode >> 4 & 0x30]];
  462. address += *postinc;
  463. break;
  464. case RM0:
  465. postinc = &registers[Rreg_index[opcode & 0x0f]];
  466. address += *postinc;
  467. break;
  468. case RM1:
  469. postinc = &registers[Rreg_index[opcode >> 2 & 0x0f]];
  470. address += *postinc;
  471. break;
  472. case RM2:
  473. postinc = &registers[Rreg_index[opcode >> 4 & 0x0f]];
  474. address += *postinc;
  475. break;
  476. case RM4:
  477. postinc = &registers[Rreg_index[opcode >> 8 & 0x0f]];
  478. address += *postinc;
  479. break;
  480. case RM6:
  481. postinc = &registers[Rreg_index[opcode >> 12 & 0x0f]];
  482. address += *postinc;
  483. break;
  484. case RD0:
  485. postinc = &registers[Rreg_index[disp & 0x0f]];
  486. address += *postinc;
  487. break;
  488. case RD2:
  489. postinc = &registers[Rreg_index[disp >> 4 & 0x0f]];
  490. address += *postinc;
  491. break;
  492. case SD8:
  493. case SIMM8:
  494. address += (int32_t) (int8_t) (disp & 0xff);
  495. break;
  496. case SD16:
  497. address += (int32_t) (int16_t) (disp & 0xffff);
  498. break;
  499. case SD24:
  500. tmp = disp << 8;
  501. asm("asr 8,%0" : "=r"(tmp) : "0"(tmp));
  502. address += tmp;
  503. break;
  504. case SIMM4_2:
  505. tmp = opcode >> 4 & 0x0f;
  506. tmp <<= 28;
  507. asm("asr 28,%0" : "=r"(tmp) : "0"(tmp));
  508. address += tmp;
  509. break;
  510. case IMM24:
  511. address += disp & 0x00ffffff;
  512. break;
  513. case IMM32:
  514. case IMM32_HIGH8:
  515. address += disp;
  516. break;
  517. default:
  518. return 0;
  519. }
  520. } while ((params >>= 8));
  521. *_address = (void *) address;
  522. *_postinc = postinc;
  523. return 1;
  524. }
  525. /*
  526. * determine the register that is acting as source/dest
  527. */
  528. static int misalignment_reg(unsigned long *registers, unsigned params,
  529. unsigned opcode, unsigned disp,
  530. unsigned long **_register)
  531. {
  532. params &= 0x7fffffff;
  533. if (params & 0xffffff00)
  534. return 0;
  535. switch (params & 0xff) {
  536. case DM0:
  537. *_register = &registers[Dreg_index[opcode & 0x03]];
  538. break;
  539. case DM1:
  540. *_register = &registers[Dreg_index[opcode >> 2 & 0x03]];
  541. break;
  542. case DM2:
  543. *_register = &registers[Dreg_index[opcode >> 4 & 0x03]];
  544. break;
  545. case AM0:
  546. *_register = &registers[Areg_index[opcode & 0x03]];
  547. break;
  548. case AM1:
  549. *_register = &registers[Areg_index[opcode >> 2 & 0x03]];
  550. break;
  551. case AM2:
  552. *_register = &registers[Areg_index[opcode >> 4 & 0x03]];
  553. break;
  554. case RM0:
  555. *_register = &registers[Rreg_index[opcode & 0x0f]];
  556. break;
  557. case RM1:
  558. *_register = &registers[Rreg_index[opcode >> 2 & 0x0f]];
  559. break;
  560. case RM2:
  561. *_register = &registers[Rreg_index[opcode >> 4 & 0x0f]];
  562. break;
  563. case RM4:
  564. *_register = &registers[Rreg_index[opcode >> 8 & 0x0f]];
  565. break;
  566. case RM6:
  567. *_register = &registers[Rreg_index[opcode >> 12 & 0x0f]];
  568. break;
  569. case RD0:
  570. *_register = &registers[Rreg_index[disp & 0x0f]];
  571. break;
  572. case RD2:
  573. *_register = &registers[Rreg_index[disp >> 4 & 0x0f]];
  574. break;
  575. case SP:
  576. *_register = &registers[REG_SP >> 2];
  577. break;
  578. default:
  579. return 0;
  580. }
  581. return 1;
  582. }
  583. /*
  584. * misalignment handler tests
  585. */
  586. #ifdef CONFIG_TEST_MISALIGNMENT_HANDLER
  587. static u8 __initdata testbuf[512] __attribute__((aligned(16))) = {
  588. [257] = 0x11,
  589. [258] = 0x22,
  590. [259] = 0x33,
  591. [260] = 0x44,
  592. };
  593. #define ASSERTCMP(X, OP, Y) \
  594. do { \
  595. if (unlikely(!((X) OP (Y)))) { \
  596. printk(KERN_ERR "\n"); \
  597. printk(KERN_ERR "MISALIGN: Assertion failed at line %u\n", \
  598. __LINE__); \
  599. printk(KERN_ERR "0x%lx " #OP " 0x%lx is false\n", \
  600. (unsigned long)(X), (unsigned long)(Y)); \
  601. BUG(); \
  602. } \
  603. } while(0)
  604. static int __init test_misalignment(void)
  605. {
  606. register void *r asm("e0");
  607. register u32 y asm("e1");
  608. void *p = testbuf, *q;
  609. u32 tmp, tmp2, x;
  610. printk(KERN_NOTICE "==>test_misalignment() [testbuf=%p]\n", p);
  611. p++;
  612. printk(KERN_NOTICE "___ MOV (Am),Dn ___\n");
  613. q = p + 256;
  614. asm volatile("mov (%0),%1" : "+a"(q), "=d"(x));
  615. ASSERTCMP(q, ==, p + 256);
  616. ASSERTCMP(x, ==, 0x44332211);
  617. printk(KERN_NOTICE "___ MOV (256,Am),Dn ___\n");
  618. q = p;
  619. asm volatile("mov (256,%0),%1" : "+a"(q), "=d"(x));
  620. ASSERTCMP(q, ==, p);
  621. ASSERTCMP(x, ==, 0x44332211);
  622. printk(KERN_NOTICE "___ MOV (Di,Am),Dn ___\n");
  623. tmp = 256;
  624. q = p;
  625. asm volatile("mov (%2,%0),%1" : "+a"(q), "=d"(x), "+d"(tmp));
  626. ASSERTCMP(q, ==, p);
  627. ASSERTCMP(x, ==, 0x44332211);
  628. ASSERTCMP(tmp, ==, 256);
  629. printk(KERN_NOTICE "___ MOV (256,Rm),Rn ___\n");
  630. r = p;
  631. asm volatile("mov (256,%0),%1" : "+r"(r), "=r"(y));
  632. ASSERTCMP(r, ==, p);
  633. ASSERTCMP(y, ==, 0x44332211);
  634. printk(KERN_NOTICE "___ MOV (Rm+),Rn ___\n");
  635. r = p + 256;
  636. asm volatile("mov (%0+),%1" : "+r"(r), "=r"(y));
  637. ASSERTCMP(r, ==, p + 256 + 4);
  638. ASSERTCMP(y, ==, 0x44332211);
  639. printk(KERN_NOTICE "___ MOV (Rm+,8),Rn ___\n");
  640. r = p + 256;
  641. asm volatile("mov (%0+,8),%1" : "+r"(r), "=r"(y));
  642. ASSERTCMP(r, ==, p + 256 + 8);
  643. ASSERTCMP(y, ==, 0x44332211);
  644. printk(KERN_NOTICE "___ MOV (7,SP),Rn ___\n");
  645. asm volatile(
  646. "add -16,sp \n"
  647. "mov +0x11,%0 \n"
  648. "movbu %0,(7,sp) \n"
  649. "mov +0x22,%0 \n"
  650. "movbu %0,(8,sp) \n"
  651. "mov +0x33,%0 \n"
  652. "movbu %0,(9,sp) \n"
  653. "mov +0x44,%0 \n"
  654. "movbu %0,(10,sp) \n"
  655. "mov (7,sp),%1 \n"
  656. "add +16,sp \n"
  657. : "+a"(q), "=d"(x));
  658. ASSERTCMP(x, ==, 0x44332211);
  659. printk(KERN_NOTICE "___ MOV (259,SP),Rn ___\n");
  660. asm volatile(
  661. "add -264,sp \n"
  662. "mov +0x11,%0 \n"
  663. "movbu %0,(259,sp) \n"
  664. "mov +0x22,%0 \n"
  665. "movbu %0,(260,sp) \n"
  666. "mov +0x33,%0 \n"
  667. "movbu %0,(261,sp) \n"
  668. "mov +0x55,%0 \n"
  669. "movbu %0,(262,sp) \n"
  670. "mov (259,sp),%1 \n"
  671. "add +264,sp \n"
  672. : "+d"(tmp), "=d"(x));
  673. ASSERTCMP(x, ==, 0x55332211);
  674. printk(KERN_NOTICE "___ MOV (260,SP),Rn ___\n");
  675. asm volatile(
  676. "add -264,sp \n"
  677. "mov +0x11,%0 \n"
  678. "movbu %0,(260,sp) \n"
  679. "mov +0x22,%0 \n"
  680. "movbu %0,(261,sp) \n"
  681. "mov +0x33,%0 \n"
  682. "movbu %0,(262,sp) \n"
  683. "mov +0x55,%0 \n"
  684. "movbu %0,(263,sp) \n"
  685. "mov (260,sp),%1 \n"
  686. "add +264,sp \n"
  687. : "+d"(tmp), "=d"(x));
  688. ASSERTCMP(x, ==, 0x55332211);
  689. printk(KERN_NOTICE "___ MOV_LNE ___\n");
  690. tmp = 1;
  691. tmp2 = 2;
  692. q = p + 256;
  693. asm volatile(
  694. "setlb \n"
  695. "mov %2,%3 \n"
  696. "mov %1,%2 \n"
  697. "cmp +0,%1 \n"
  698. "mov_lne (%0+,4),%1"
  699. : "+r"(q), "+d"(tmp), "+d"(tmp2), "=d"(x)
  700. :
  701. : "cc");
  702. ASSERTCMP(q, ==, p + 256 + 12);
  703. ASSERTCMP(x, ==, 0x44332211);
  704. printk(KERN_NOTICE "___ MOV in SETLB ___\n");
  705. tmp = 1;
  706. tmp2 = 2;
  707. q = p + 256;
  708. asm volatile(
  709. "setlb \n"
  710. "mov %1,%3 \n"
  711. "mov (%0+),%1 \n"
  712. "cmp +0,%1 \n"
  713. "lne "
  714. : "+a"(q), "+d"(tmp), "+d"(tmp2), "=d"(x)
  715. :
  716. : "cc");
  717. ASSERTCMP(q, ==, p + 256 + 8);
  718. ASSERTCMP(x, ==, 0x44332211);
  719. printk(KERN_NOTICE "<==test_misalignment()\n");
  720. return 0;
  721. }
  722. arch_initcall(test_misalignment);
  723. #endif /* CONFIG_TEST_MISALIGNMENT_HANDLER */