unaligned.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Unaligned memory access handler
  3. *
  4. * Copyright (C) 2001 Randolph Chung <tausq@debian.org>
  5. * Significantly tweaked by LaMont Jones <lamont@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/jiffies.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/sched.h>
  26. #include <linux/signal.h>
  27. #include <linux/ratelimit.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/hardirq.h>
  30. /* #define DEBUG_UNALIGNED 1 */
  31. #ifdef DEBUG_UNALIGNED
  32. #define DPRINTF(fmt, args...) do { printk(KERN_DEBUG "%s:%d:%s ", __FILE__, __LINE__, __func__ ); printk(KERN_DEBUG fmt, ##args ); } while (0)
  33. #else
  34. #define DPRINTF(fmt, args...)
  35. #endif
  36. #ifdef CONFIG_64BIT
  37. #define RFMT "%016lx"
  38. #else
  39. #define RFMT "%08lx"
  40. #endif
  41. #define FIXUP_BRANCH(lbl) \
  42. "\tldil L%%" #lbl ", %%r1\n" \
  43. "\tldo R%%" #lbl "(%%r1), %%r1\n" \
  44. "\tbv,n %%r0(%%r1)\n"
  45. /* If you use FIXUP_BRANCH, then you must list this clobber */
  46. #define FIXUP_BRANCH_CLOBBER "r1"
  47. /* 1111 1100 0000 0000 0001 0011 1100 0000 */
  48. #define OPCODE1(a,b,c) ((a)<<26|(b)<<12|(c)<<6)
  49. #define OPCODE2(a,b) ((a)<<26|(b)<<1)
  50. #define OPCODE3(a,b) ((a)<<26|(b)<<2)
  51. #define OPCODE4(a) ((a)<<26)
  52. #define OPCODE1_MASK OPCODE1(0x3f,1,0xf)
  53. #define OPCODE2_MASK OPCODE2(0x3f,1)
  54. #define OPCODE3_MASK OPCODE3(0x3f,1)
  55. #define OPCODE4_MASK OPCODE4(0x3f)
  56. /* skip LDB - never unaligned (index) */
  57. #define OPCODE_LDH_I OPCODE1(0x03,0,0x1)
  58. #define OPCODE_LDW_I OPCODE1(0x03,0,0x2)
  59. #define OPCODE_LDD_I OPCODE1(0x03,0,0x3)
  60. #define OPCODE_LDDA_I OPCODE1(0x03,0,0x4)
  61. #define OPCODE_LDCD_I OPCODE1(0x03,0,0x5)
  62. #define OPCODE_LDWA_I OPCODE1(0x03,0,0x6)
  63. #define OPCODE_LDCW_I OPCODE1(0x03,0,0x7)
  64. /* skip LDB - never unaligned (short) */
  65. #define OPCODE_LDH_S OPCODE1(0x03,1,0x1)
  66. #define OPCODE_LDW_S OPCODE1(0x03,1,0x2)
  67. #define OPCODE_LDD_S OPCODE1(0x03,1,0x3)
  68. #define OPCODE_LDDA_S OPCODE1(0x03,1,0x4)
  69. #define OPCODE_LDCD_S OPCODE1(0x03,1,0x5)
  70. #define OPCODE_LDWA_S OPCODE1(0x03,1,0x6)
  71. #define OPCODE_LDCW_S OPCODE1(0x03,1,0x7)
  72. /* skip STB - never unaligned */
  73. #define OPCODE_STH OPCODE1(0x03,1,0x9)
  74. #define OPCODE_STW OPCODE1(0x03,1,0xa)
  75. #define OPCODE_STD OPCODE1(0x03,1,0xb)
  76. /* skip STBY - never unaligned */
  77. /* skip STDBY - never unaligned */
  78. #define OPCODE_STWA OPCODE1(0x03,1,0xe)
  79. #define OPCODE_STDA OPCODE1(0x03,1,0xf)
  80. #define OPCODE_FLDWX OPCODE1(0x09,0,0x0)
  81. #define OPCODE_FLDWXR OPCODE1(0x09,0,0x1)
  82. #define OPCODE_FSTWX OPCODE1(0x09,0,0x8)
  83. #define OPCODE_FSTWXR OPCODE1(0x09,0,0x9)
  84. #define OPCODE_FLDWS OPCODE1(0x09,1,0x0)
  85. #define OPCODE_FLDWSR OPCODE1(0x09,1,0x1)
  86. #define OPCODE_FSTWS OPCODE1(0x09,1,0x8)
  87. #define OPCODE_FSTWSR OPCODE1(0x09,1,0x9)
  88. #define OPCODE_FLDDX OPCODE1(0x0b,0,0x0)
  89. #define OPCODE_FSTDX OPCODE1(0x0b,0,0x8)
  90. #define OPCODE_FLDDS OPCODE1(0x0b,1,0x0)
  91. #define OPCODE_FSTDS OPCODE1(0x0b,1,0x8)
  92. #define OPCODE_LDD_L OPCODE2(0x14,0)
  93. #define OPCODE_FLDD_L OPCODE2(0x14,1)
  94. #define OPCODE_STD_L OPCODE2(0x1c,0)
  95. #define OPCODE_FSTD_L OPCODE2(0x1c,1)
  96. #define OPCODE_LDW_M OPCODE3(0x17,1)
  97. #define OPCODE_FLDW_L OPCODE3(0x17,0)
  98. #define OPCODE_FSTW_L OPCODE3(0x1f,0)
  99. #define OPCODE_STW_M OPCODE3(0x1f,1)
  100. #define OPCODE_LDH_L OPCODE4(0x11)
  101. #define OPCODE_LDW_L OPCODE4(0x12)
  102. #define OPCODE_LDWM OPCODE4(0x13)
  103. #define OPCODE_STH_L OPCODE4(0x19)
  104. #define OPCODE_STW_L OPCODE4(0x1A)
  105. #define OPCODE_STWM OPCODE4(0x1B)
  106. #define MAJOR_OP(i) (((i)>>26)&0x3f)
  107. #define R1(i) (((i)>>21)&0x1f)
  108. #define R2(i) (((i)>>16)&0x1f)
  109. #define R3(i) ((i)&0x1f)
  110. #define FR3(i) ((((i)<<1)&0x1f)|(((i)>>6)&1))
  111. #define IM(i,n) (((i)>>1&((1<<(n-1))-1))|((i)&1?((0-1L)<<(n-1)):0))
  112. #define IM5_2(i) IM((i)>>16,5)
  113. #define IM5_3(i) IM((i),5)
  114. #define IM14(i) IM((i),14)
  115. #define ERR_NOTHANDLED -1
  116. #define ERR_PAGEFAULT -2
  117. int unaligned_enabled __read_mostly = 1;
  118. void die_if_kernel (char *str, struct pt_regs *regs, long err);
  119. static int emulate_ldh(struct pt_regs *regs, int toreg)
  120. {
  121. unsigned long saddr = regs->ior;
  122. unsigned long val = 0;
  123. int ret;
  124. DPRINTF("load " RFMT ":" RFMT " to r%d for 2 bytes\n",
  125. regs->isr, regs->ior, toreg);
  126. __asm__ __volatile__ (
  127. " mtsp %4, %%sr1\n"
  128. "1: ldbs 0(%%sr1,%3), %%r20\n"
  129. "2: ldbs 1(%%sr1,%3), %0\n"
  130. " depw %%r20, 23, 24, %0\n"
  131. " copy %%r0, %1\n"
  132. "3: \n"
  133. " .section .fixup,\"ax\"\n"
  134. "4: ldi -2, %1\n"
  135. FIXUP_BRANCH(3b)
  136. " .previous\n"
  137. ASM_EXCEPTIONTABLE_ENTRY(1b, 4b)
  138. ASM_EXCEPTIONTABLE_ENTRY(2b, 4b)
  139. : "=r" (val), "=r" (ret)
  140. : "0" (val), "r" (saddr), "r" (regs->isr)
  141. : "r20", FIXUP_BRANCH_CLOBBER );
  142. DPRINTF("val = 0x" RFMT "\n", val);
  143. if (toreg)
  144. regs->gr[toreg] = val;
  145. return ret;
  146. }
  147. static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
  148. {
  149. unsigned long saddr = regs->ior;
  150. unsigned long val = 0;
  151. int ret;
  152. DPRINTF("load " RFMT ":" RFMT " to r%d for 4 bytes\n",
  153. regs->isr, regs->ior, toreg);
  154. __asm__ __volatile__ (
  155. " zdep %3,28,2,%%r19\n" /* r19=(ofs&3)*8 */
  156. " mtsp %4, %%sr1\n"
  157. " depw %%r0,31,2,%3\n"
  158. "1: ldw 0(%%sr1,%3),%0\n"
  159. "2: ldw 4(%%sr1,%3),%%r20\n"
  160. " subi 32,%%r19,%%r19\n"
  161. " mtctl %%r19,11\n"
  162. " vshd %0,%%r20,%0\n"
  163. " copy %%r0, %1\n"
  164. "3: \n"
  165. " .section .fixup,\"ax\"\n"
  166. "4: ldi -2, %1\n"
  167. FIXUP_BRANCH(3b)
  168. " .previous\n"
  169. ASM_EXCEPTIONTABLE_ENTRY(1b, 4b)
  170. ASM_EXCEPTIONTABLE_ENTRY(2b, 4b)
  171. : "=r" (val), "=r" (ret)
  172. : "0" (val), "r" (saddr), "r" (regs->isr)
  173. : "r19", "r20", FIXUP_BRANCH_CLOBBER );
  174. DPRINTF("val = 0x" RFMT "\n", val);
  175. if (flop)
  176. ((__u32*)(regs->fr))[toreg] = val;
  177. else if (toreg)
  178. regs->gr[toreg] = val;
  179. return ret;
  180. }
  181. static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
  182. {
  183. unsigned long saddr = regs->ior;
  184. __u64 val = 0;
  185. int ret;
  186. DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
  187. regs->isr, regs->ior, toreg);
  188. #ifdef CONFIG_PA20
  189. #ifndef CONFIG_64BIT
  190. if (!flop)
  191. return -1;
  192. #endif
  193. __asm__ __volatile__ (
  194. " depd,z %3,60,3,%%r19\n" /* r19=(ofs&7)*8 */
  195. " mtsp %4, %%sr1\n"
  196. " depd %%r0,63,3,%3\n"
  197. "1: ldd 0(%%sr1,%3),%0\n"
  198. "2: ldd 8(%%sr1,%3),%%r20\n"
  199. " subi 64,%%r19,%%r19\n"
  200. " mtsar %%r19\n"
  201. " shrpd %0,%%r20,%%sar,%0\n"
  202. " copy %%r0, %1\n"
  203. "3: \n"
  204. " .section .fixup,\"ax\"\n"
  205. "4: ldi -2, %1\n"
  206. FIXUP_BRANCH(3b)
  207. " .previous\n"
  208. ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
  209. ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
  210. : "=r" (val), "=r" (ret)
  211. : "0" (val), "r" (saddr), "r" (regs->isr)
  212. : "r19", "r20", FIXUP_BRANCH_CLOBBER );
  213. #else
  214. {
  215. unsigned long valh=0,vall=0;
  216. __asm__ __volatile__ (
  217. " zdep %5,29,2,%%r19\n" /* r19=(ofs&3)*8 */
  218. " mtsp %6, %%sr1\n"
  219. " dep %%r0,31,2,%5\n"
  220. "1: ldw 0(%%sr1,%5),%0\n"
  221. "2: ldw 4(%%sr1,%5),%1\n"
  222. "3: ldw 8(%%sr1,%5),%%r20\n"
  223. " subi 32,%%r19,%%r19\n"
  224. " mtsar %%r19\n"
  225. " vshd %0,%1,%0\n"
  226. " vshd %1,%%r20,%1\n"
  227. " copy %%r0, %2\n"
  228. "4: \n"
  229. " .section .fixup,\"ax\"\n"
  230. "5: ldi -2, %2\n"
  231. FIXUP_BRANCH(4b)
  232. " .previous\n"
  233. ASM_EXCEPTIONTABLE_ENTRY(1b,5b)
  234. ASM_EXCEPTIONTABLE_ENTRY(2b,5b)
  235. ASM_EXCEPTIONTABLE_ENTRY(3b,5b)
  236. : "=r" (valh), "=r" (vall), "=r" (ret)
  237. : "0" (valh), "1" (vall), "r" (saddr), "r" (regs->isr)
  238. : "r19", "r20", FIXUP_BRANCH_CLOBBER );
  239. val=((__u64)valh<<32)|(__u64)vall;
  240. }
  241. #endif
  242. DPRINTF("val = 0x%llx\n", val);
  243. if (flop)
  244. regs->fr[toreg] = val;
  245. else if (toreg)
  246. regs->gr[toreg] = val;
  247. return ret;
  248. }
  249. static int emulate_sth(struct pt_regs *regs, int frreg)
  250. {
  251. unsigned long val = regs->gr[frreg];
  252. int ret;
  253. if (!frreg)
  254. val = 0;
  255. DPRINTF("store r%d (0x" RFMT ") to " RFMT ":" RFMT " for 2 bytes\n", frreg,
  256. val, regs->isr, regs->ior);
  257. __asm__ __volatile__ (
  258. " mtsp %3, %%sr1\n"
  259. " extrw,u %1, 23, 8, %%r19\n"
  260. "1: stb %1, 1(%%sr1, %2)\n"
  261. "2: stb %%r19, 0(%%sr1, %2)\n"
  262. " copy %%r0, %0\n"
  263. "3: \n"
  264. " .section .fixup,\"ax\"\n"
  265. "4: ldi -2, %0\n"
  266. FIXUP_BRANCH(3b)
  267. " .previous\n"
  268. ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
  269. ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
  270. : "=r" (ret)
  271. : "r" (val), "r" (regs->ior), "r" (regs->isr)
  272. : "r19", FIXUP_BRANCH_CLOBBER );
  273. return ret;
  274. }
  275. static int emulate_stw(struct pt_regs *regs, int frreg, int flop)
  276. {
  277. unsigned long val;
  278. int ret;
  279. if (flop)
  280. val = ((__u32*)(regs->fr))[frreg];
  281. else if (frreg)
  282. val = regs->gr[frreg];
  283. else
  284. val = 0;
  285. DPRINTF("store r%d (0x" RFMT ") to " RFMT ":" RFMT " for 4 bytes\n", frreg,
  286. val, regs->isr, regs->ior);
  287. __asm__ __volatile__ (
  288. " mtsp %3, %%sr1\n"
  289. " zdep %2, 28, 2, %%r19\n"
  290. " dep %%r0, 31, 2, %2\n"
  291. " mtsar %%r19\n"
  292. " depwi,z -2, %%sar, 32, %%r19\n"
  293. "1: ldw 0(%%sr1,%2),%%r20\n"
  294. "2: ldw 4(%%sr1,%2),%%r21\n"
  295. " vshd %%r0, %1, %%r22\n"
  296. " vshd %1, %%r0, %%r1\n"
  297. " and %%r20, %%r19, %%r20\n"
  298. " andcm %%r21, %%r19, %%r21\n"
  299. " or %%r22, %%r20, %%r20\n"
  300. " or %%r1, %%r21, %%r21\n"
  301. " stw %%r20,0(%%sr1,%2)\n"
  302. " stw %%r21,4(%%sr1,%2)\n"
  303. " copy %%r0, %0\n"
  304. "3: \n"
  305. " .section .fixup,\"ax\"\n"
  306. "4: ldi -2, %0\n"
  307. FIXUP_BRANCH(3b)
  308. " .previous\n"
  309. ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
  310. ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
  311. : "=r" (ret)
  312. : "r" (val), "r" (regs->ior), "r" (regs->isr)
  313. : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER );
  314. return 0;
  315. }
  316. static int emulate_std(struct pt_regs *regs, int frreg, int flop)
  317. {
  318. __u64 val;
  319. int ret;
  320. if (flop)
  321. val = regs->fr[frreg];
  322. else if (frreg)
  323. val = regs->gr[frreg];
  324. else
  325. val = 0;
  326. DPRINTF("store r%d (0x%016llx) to " RFMT ":" RFMT " for 8 bytes\n", frreg,
  327. val, regs->isr, regs->ior);
  328. #ifdef CONFIG_PA20
  329. #ifndef CONFIG_64BIT
  330. if (!flop)
  331. return -1;
  332. #endif
  333. __asm__ __volatile__ (
  334. " mtsp %3, %%sr1\n"
  335. " depd,z %2, 60, 3, %%r19\n"
  336. " depd %%r0, 63, 3, %2\n"
  337. " mtsar %%r19\n"
  338. " depdi,z -2, %%sar, 64, %%r19\n"
  339. "1: ldd 0(%%sr1,%2),%%r20\n"
  340. "2: ldd 8(%%sr1,%2),%%r21\n"
  341. " shrpd %%r0, %1, %%sar, %%r22\n"
  342. " shrpd %1, %%r0, %%sar, %%r1\n"
  343. " and %%r20, %%r19, %%r20\n"
  344. " andcm %%r21, %%r19, %%r21\n"
  345. " or %%r22, %%r20, %%r20\n"
  346. " or %%r1, %%r21, %%r21\n"
  347. "3: std %%r20,0(%%sr1,%2)\n"
  348. "4: std %%r21,8(%%sr1,%2)\n"
  349. " copy %%r0, %0\n"
  350. "5: \n"
  351. " .section .fixup,\"ax\"\n"
  352. "6: ldi -2, %0\n"
  353. FIXUP_BRANCH(5b)
  354. " .previous\n"
  355. ASM_EXCEPTIONTABLE_ENTRY(1b,6b)
  356. ASM_EXCEPTIONTABLE_ENTRY(2b,6b)
  357. ASM_EXCEPTIONTABLE_ENTRY(3b,6b)
  358. ASM_EXCEPTIONTABLE_ENTRY(4b,6b)
  359. : "=r" (ret)
  360. : "r" (val), "r" (regs->ior), "r" (regs->isr)
  361. : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER );
  362. #else
  363. {
  364. unsigned long valh=(val>>32),vall=(val&0xffffffffl);
  365. __asm__ __volatile__ (
  366. " mtsp %4, %%sr1\n"
  367. " zdep %2, 29, 2, %%r19\n"
  368. " dep %%r0, 31, 2, %2\n"
  369. " mtsar %%r19\n"
  370. " zvdepi -2, 32, %%r19\n"
  371. "1: ldw 0(%%sr1,%3),%%r20\n"
  372. "2: ldw 8(%%sr1,%3),%%r21\n"
  373. " vshd %1, %2, %%r1\n"
  374. " vshd %%r0, %1, %1\n"
  375. " vshd %2, %%r0, %2\n"
  376. " and %%r20, %%r19, %%r20\n"
  377. " andcm %%r21, %%r19, %%r21\n"
  378. " or %1, %%r20, %1\n"
  379. " or %2, %%r21, %2\n"
  380. "3: stw %1,0(%%sr1,%1)\n"
  381. "4: stw %%r1,4(%%sr1,%3)\n"
  382. "5: stw %2,8(%%sr1,%3)\n"
  383. " copy %%r0, %0\n"
  384. "6: \n"
  385. " .section .fixup,\"ax\"\n"
  386. "7: ldi -2, %0\n"
  387. FIXUP_BRANCH(6b)
  388. " .previous\n"
  389. ASM_EXCEPTIONTABLE_ENTRY(1b,7b)
  390. ASM_EXCEPTIONTABLE_ENTRY(2b,7b)
  391. ASM_EXCEPTIONTABLE_ENTRY(3b,7b)
  392. ASM_EXCEPTIONTABLE_ENTRY(4b,7b)
  393. ASM_EXCEPTIONTABLE_ENTRY(5b,7b)
  394. : "=r" (ret)
  395. : "r" (valh), "r" (vall), "r" (regs->ior), "r" (regs->isr)
  396. : "r19", "r20", "r21", "r1", FIXUP_BRANCH_CLOBBER );
  397. }
  398. #endif
  399. return ret;
  400. }
  401. void handle_unaligned(struct pt_regs *regs)
  402. {
  403. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
  404. unsigned long newbase = R1(regs->iir)?regs->gr[R1(regs->iir)]:0;
  405. int modify = 0;
  406. int ret = ERR_NOTHANDLED;
  407. struct siginfo si;
  408. register int flop=0; /* true if this is a flop */
  409. __inc_irq_stat(irq_unaligned_count);
  410. /* log a message with pacing */
  411. if (user_mode(regs)) {
  412. if (current->thread.flags & PARISC_UAC_SIGBUS) {
  413. goto force_sigbus;
  414. }
  415. if (!(current->thread.flags & PARISC_UAC_NOPRINT) &&
  416. __ratelimit(&ratelimit)) {
  417. char buf[256];
  418. sprintf(buf, "%s(%d): unaligned access to 0x" RFMT " at ip=0x" RFMT "\n",
  419. current->comm, task_pid_nr(current), regs->ior, regs->iaoq[0]);
  420. printk(KERN_WARNING "%s", buf);
  421. #ifdef DEBUG_UNALIGNED
  422. show_regs(regs);
  423. #endif
  424. }
  425. if (!unaligned_enabled)
  426. goto force_sigbus;
  427. }
  428. /* handle modification - OK, it's ugly, see the instruction manual */
  429. switch (MAJOR_OP(regs->iir))
  430. {
  431. case 0x03:
  432. case 0x09:
  433. case 0x0b:
  434. if (regs->iir&0x20)
  435. {
  436. modify = 1;
  437. if (regs->iir&0x1000) /* short loads */
  438. if (regs->iir&0x200)
  439. newbase += IM5_3(regs->iir);
  440. else
  441. newbase += IM5_2(regs->iir);
  442. else if (regs->iir&0x2000) /* scaled indexed */
  443. {
  444. int shift=0;
  445. switch (regs->iir & OPCODE1_MASK)
  446. {
  447. case OPCODE_LDH_I:
  448. shift= 1; break;
  449. case OPCODE_LDW_I:
  450. shift= 2; break;
  451. case OPCODE_LDD_I:
  452. case OPCODE_LDDA_I:
  453. shift= 3; break;
  454. }
  455. newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0)<<shift;
  456. } else /* simple indexed */
  457. newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0);
  458. }
  459. break;
  460. case 0x13:
  461. case 0x1b:
  462. modify = 1;
  463. newbase += IM14(regs->iir);
  464. break;
  465. case 0x14:
  466. case 0x1c:
  467. if (regs->iir&8)
  468. {
  469. modify = 1;
  470. newbase += IM14(regs->iir&~0xe);
  471. }
  472. break;
  473. case 0x16:
  474. case 0x1e:
  475. modify = 1;
  476. newbase += IM14(regs->iir&6);
  477. break;
  478. case 0x17:
  479. case 0x1f:
  480. if (regs->iir&4)
  481. {
  482. modify = 1;
  483. newbase += IM14(regs->iir&~4);
  484. }
  485. break;
  486. }
  487. /* TODO: make this cleaner... */
  488. switch (regs->iir & OPCODE1_MASK)
  489. {
  490. case OPCODE_LDH_I:
  491. case OPCODE_LDH_S:
  492. ret = emulate_ldh(regs, R3(regs->iir));
  493. break;
  494. case OPCODE_LDW_I:
  495. case OPCODE_LDWA_I:
  496. case OPCODE_LDW_S:
  497. case OPCODE_LDWA_S:
  498. ret = emulate_ldw(regs, R3(regs->iir),0);
  499. break;
  500. case OPCODE_STH:
  501. ret = emulate_sth(regs, R2(regs->iir));
  502. break;
  503. case OPCODE_STW:
  504. case OPCODE_STWA:
  505. ret = emulate_stw(regs, R2(regs->iir),0);
  506. break;
  507. #ifdef CONFIG_PA20
  508. case OPCODE_LDD_I:
  509. case OPCODE_LDDA_I:
  510. case OPCODE_LDD_S:
  511. case OPCODE_LDDA_S:
  512. ret = emulate_ldd(regs, R3(regs->iir),0);
  513. break;
  514. case OPCODE_STD:
  515. case OPCODE_STDA:
  516. ret = emulate_std(regs, R2(regs->iir),0);
  517. break;
  518. #endif
  519. case OPCODE_FLDWX:
  520. case OPCODE_FLDWS:
  521. case OPCODE_FLDWXR:
  522. case OPCODE_FLDWSR:
  523. flop=1;
  524. ret = emulate_ldw(regs,FR3(regs->iir),1);
  525. break;
  526. case OPCODE_FLDDX:
  527. case OPCODE_FLDDS:
  528. flop=1;
  529. ret = emulate_ldd(regs,R3(regs->iir),1);
  530. break;
  531. case OPCODE_FSTWX:
  532. case OPCODE_FSTWS:
  533. case OPCODE_FSTWXR:
  534. case OPCODE_FSTWSR:
  535. flop=1;
  536. ret = emulate_stw(regs,FR3(regs->iir),1);
  537. break;
  538. case OPCODE_FSTDX:
  539. case OPCODE_FSTDS:
  540. flop=1;
  541. ret = emulate_std(regs,R3(regs->iir),1);
  542. break;
  543. case OPCODE_LDCD_I:
  544. case OPCODE_LDCW_I:
  545. case OPCODE_LDCD_S:
  546. case OPCODE_LDCW_S:
  547. ret = ERR_NOTHANDLED; /* "undefined", but lets kill them. */
  548. break;
  549. }
  550. #ifdef CONFIG_PA20
  551. switch (regs->iir & OPCODE2_MASK)
  552. {
  553. case OPCODE_FLDD_L:
  554. flop=1;
  555. ret = emulate_ldd(regs,R2(regs->iir),1);
  556. break;
  557. case OPCODE_FSTD_L:
  558. flop=1;
  559. ret = emulate_std(regs, R2(regs->iir),1);
  560. break;
  561. case OPCODE_LDD_L:
  562. ret = emulate_ldd(regs, R2(regs->iir),0);
  563. break;
  564. case OPCODE_STD_L:
  565. ret = emulate_std(regs, R2(regs->iir),0);
  566. break;
  567. }
  568. #endif
  569. switch (regs->iir & OPCODE3_MASK)
  570. {
  571. case OPCODE_FLDW_L:
  572. flop=1;
  573. ret = emulate_ldw(regs, R2(regs->iir),0);
  574. break;
  575. case OPCODE_LDW_M:
  576. ret = emulate_ldw(regs, R2(regs->iir),1);
  577. break;
  578. case OPCODE_FSTW_L:
  579. flop=1;
  580. ret = emulate_stw(regs, R2(regs->iir),1);
  581. break;
  582. case OPCODE_STW_M:
  583. ret = emulate_stw(regs, R2(regs->iir),0);
  584. break;
  585. }
  586. switch (regs->iir & OPCODE4_MASK)
  587. {
  588. case OPCODE_LDH_L:
  589. ret = emulate_ldh(regs, R2(regs->iir));
  590. break;
  591. case OPCODE_LDW_L:
  592. case OPCODE_LDWM:
  593. ret = emulate_ldw(regs, R2(regs->iir),0);
  594. break;
  595. case OPCODE_STH_L:
  596. ret = emulate_sth(regs, R2(regs->iir));
  597. break;
  598. case OPCODE_STW_L:
  599. case OPCODE_STWM:
  600. ret = emulate_stw(regs, R2(regs->iir),0);
  601. break;
  602. }
  603. if (ret == 0 && modify && R1(regs->iir))
  604. regs->gr[R1(regs->iir)] = newbase;
  605. if (ret == ERR_NOTHANDLED)
  606. printk(KERN_CRIT "Not-handled unaligned insn 0x%08lx\n", regs->iir);
  607. DPRINTF("ret = %d\n", ret);
  608. if (ret)
  609. {
  610. /*
  611. * The unaligned handler failed.
  612. * If we were called by __get_user() or __put_user() jump
  613. * to it's exception fixup handler instead of crashing.
  614. */
  615. if (!user_mode(regs) && fixup_exception(regs))
  616. return;
  617. printk(KERN_CRIT "Unaligned handler failed, ret = %d\n", ret);
  618. die_if_kernel("Unaligned data reference", regs, 28);
  619. if (ret == ERR_PAGEFAULT)
  620. {
  621. si.si_signo = SIGSEGV;
  622. si.si_errno = 0;
  623. si.si_code = SEGV_MAPERR;
  624. si.si_addr = (void __user *)regs->ior;
  625. force_sig_info(SIGSEGV, &si, current);
  626. }
  627. else
  628. {
  629. force_sigbus:
  630. /* couldn't handle it ... */
  631. si.si_signo = SIGBUS;
  632. si.si_errno = 0;
  633. si.si_code = BUS_ADRALN;
  634. si.si_addr = (void __user *)regs->ior;
  635. force_sig_info(SIGBUS, &si, current);
  636. }
  637. return;
  638. }
  639. /* else we handled it, let life go on. */
  640. regs->gr[0]|=PSW_N;
  641. }
  642. /*
  643. * NB: check_unaligned() is only used for PCXS processors right
  644. * now, so we only check for PA1.1 encodings at this point.
  645. */
  646. int
  647. check_unaligned(struct pt_regs *regs)
  648. {
  649. unsigned long align_mask;
  650. /* Get alignment mask */
  651. align_mask = 0UL;
  652. switch (regs->iir & OPCODE1_MASK) {
  653. case OPCODE_LDH_I:
  654. case OPCODE_LDH_S:
  655. case OPCODE_STH:
  656. align_mask = 1UL;
  657. break;
  658. case OPCODE_LDW_I:
  659. case OPCODE_LDWA_I:
  660. case OPCODE_LDW_S:
  661. case OPCODE_LDWA_S:
  662. case OPCODE_STW:
  663. case OPCODE_STWA:
  664. align_mask = 3UL;
  665. break;
  666. default:
  667. switch (regs->iir & OPCODE4_MASK) {
  668. case OPCODE_LDH_L:
  669. case OPCODE_STH_L:
  670. align_mask = 1UL;
  671. break;
  672. case OPCODE_LDW_L:
  673. case OPCODE_LDWM:
  674. case OPCODE_STW_L:
  675. case OPCODE_STWM:
  676. align_mask = 3UL;
  677. break;
  678. }
  679. break;
  680. }
  681. return (int)(regs->ior & align_mask);
  682. }