misaligned.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/arch/nios2/kernel/misaligned.c
  3. *
  4. * basic emulation for mis-aligned accesses on the NIOS II cpu
  5. * modelled after the version for arm in arm/alignment.c
  6. *
  7. * Brad Parker <brad@heeltoe.com>
  8. * Copyright (C) 2010 Ambient Corporation
  9. * Copyright (c) 2010 Altera Corporation, San Jose, California, USA.
  10. * Copyright (c) 2010 Arrow Electronics, Inc.
  11. *
  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.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/seq_file.h>
  23. #include <asm/traps.h>
  24. #include <asm/unaligned.h>
  25. /* instructions we emulate */
  26. #define INST_LDHU 0x0b
  27. #define INST_STH 0x0d
  28. #define INST_LDH 0x0f
  29. #define INST_STW 0x15
  30. #define INST_LDW 0x17
  31. static unsigned long ma_user, ma_kern, ma_skipped, ma_half, ma_word;
  32. static unsigned int ma_usermode;
  33. #define UM_WARN 0x01
  34. #define UM_FIXUP 0x02
  35. #define UM_SIGNAL 0x04
  36. #define KM_WARN 0x08
  37. /* see arch/nios2/include/asm/ptrace.h */
  38. static u8 sys_stack_frame_reg_offset[] = {
  39. /* struct pt_regs */
  40. 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 0,
  41. /* struct switch_stack */
  42. 16, 17, 18, 19, 20, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0, 0
  43. };
  44. static int reg_offsets[32];
  45. static inline u32 get_reg_val(struct pt_regs *fp, int reg)
  46. {
  47. u8 *p = ((u8 *)fp) + reg_offsets[reg];
  48. return *(u32 *)p;
  49. }
  50. static inline void put_reg_val(struct pt_regs *fp, int reg, u32 val)
  51. {
  52. u8 *p = ((u8 *)fp) + reg_offsets[reg];
  53. *(u32 *)p = val;
  54. }
  55. /*
  56. * (mis)alignment handler
  57. */
  58. asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
  59. {
  60. u32 isn, addr, val;
  61. int in_kernel;
  62. u8 a, b, d0, d1, d2, d3;
  63. u16 imm16;
  64. unsigned int fault;
  65. /* back up one instruction */
  66. fp->ea -= 4;
  67. if (fixup_exception(fp)) {
  68. ma_skipped++;
  69. return;
  70. }
  71. in_kernel = !user_mode(fp);
  72. isn = *(unsigned long *)(fp->ea);
  73. fault = 0;
  74. /* do fixup if in kernel or mode turned on */
  75. if (in_kernel || (ma_usermode & UM_FIXUP)) {
  76. /* decompose instruction */
  77. a = (isn >> 27) & 0x1f;
  78. b = (isn >> 22) & 0x1f;
  79. imm16 = (isn >> 6) & 0xffff;
  80. addr = get_reg_val(fp, a) + imm16;
  81. /* do fixup to saved registers */
  82. switch (isn & 0x3f) {
  83. case INST_LDHU:
  84. fault |= __get_user(d0, (u8 *)(addr+0));
  85. fault |= __get_user(d1, (u8 *)(addr+1));
  86. val = (d1 << 8) | d0;
  87. put_reg_val(fp, b, val);
  88. ma_half++;
  89. break;
  90. case INST_STH:
  91. val = get_reg_val(fp, b);
  92. d1 = val >> 8;
  93. d0 = val >> 0;
  94. pr_debug("sth: ra=%d (%08x) rb=%d (%08x), imm16 %04x addr %08x val %08x\n",
  95. a, get_reg_val(fp, a),
  96. b, get_reg_val(fp, b),
  97. imm16, addr, val);
  98. if (in_kernel) {
  99. *(u8 *)(addr+0) = d0;
  100. *(u8 *)(addr+1) = d1;
  101. } else {
  102. fault |= __put_user(d0, (u8 *)(addr+0));
  103. fault |= __put_user(d1, (u8 *)(addr+1));
  104. }
  105. ma_half++;
  106. break;
  107. case INST_LDH:
  108. fault |= __get_user(d0, (u8 *)(addr+0));
  109. fault |= __get_user(d1, (u8 *)(addr+1));
  110. val = (short)((d1 << 8) | d0);
  111. put_reg_val(fp, b, val);
  112. ma_half++;
  113. break;
  114. case INST_STW:
  115. val = get_reg_val(fp, b);
  116. d3 = val >> 24;
  117. d2 = val >> 16;
  118. d1 = val >> 8;
  119. d0 = val >> 0;
  120. if (in_kernel) {
  121. *(u8 *)(addr+0) = d0;
  122. *(u8 *)(addr+1) = d1;
  123. *(u8 *)(addr+2) = d2;
  124. *(u8 *)(addr+3) = d3;
  125. } else {
  126. fault |= __put_user(d0, (u8 *)(addr+0));
  127. fault |= __put_user(d1, (u8 *)(addr+1));
  128. fault |= __put_user(d2, (u8 *)(addr+2));
  129. fault |= __put_user(d3, (u8 *)(addr+3));
  130. }
  131. ma_word++;
  132. break;
  133. case INST_LDW:
  134. fault |= __get_user(d0, (u8 *)(addr+0));
  135. fault |= __get_user(d1, (u8 *)(addr+1));
  136. fault |= __get_user(d2, (u8 *)(addr+2));
  137. fault |= __get_user(d3, (u8 *)(addr+3));
  138. val = (d3 << 24) | (d2 << 16) | (d1 << 8) | d0;
  139. put_reg_val(fp, b, val);
  140. ma_word++;
  141. break;
  142. }
  143. }
  144. addr = RDCTL(CTL_BADADDR);
  145. cause >>= 2;
  146. if (fault) {
  147. if (in_kernel) {
  148. pr_err("fault during kernel misaligned fixup @ %#lx; addr 0x%08x; isn=0x%08x\n",
  149. fp->ea, (unsigned int)addr,
  150. (unsigned int)isn);
  151. } else {
  152. pr_err("fault during user misaligned fixup @ %#lx; isn=%08x addr=0x%08x sp=0x%08lx pid=%d\n",
  153. fp->ea,
  154. (unsigned int)isn, addr, fp->sp,
  155. current->pid);
  156. _exception(SIGSEGV, fp, SEGV_MAPERR, fp->ea);
  157. return;
  158. }
  159. }
  160. /*
  161. * kernel mode -
  162. * note exception and skip bad instruction (return)
  163. */
  164. if (in_kernel) {
  165. ma_kern++;
  166. fp->ea += 4;
  167. if (ma_usermode & KM_WARN) {
  168. pr_err("kernel unaligned access @ %#lx; BADADDR 0x%08x; cause=%d, isn=0x%08x\n",
  169. fp->ea,
  170. (unsigned int)addr, cause,
  171. (unsigned int)isn);
  172. /* show_regs(fp); */
  173. }
  174. return;
  175. }
  176. ma_user++;
  177. /*
  178. * user mode -
  179. * possibly warn,
  180. * possibly send SIGBUS signal to process
  181. */
  182. if (ma_usermode & UM_WARN) {
  183. pr_err("user unaligned access @ %#lx; isn=0x%08lx ea=0x%08lx ra=0x%08lx sp=0x%08lx\n",
  184. (unsigned long)addr, (unsigned long)isn,
  185. fp->ea, fp->ra, fp->sp);
  186. }
  187. if (ma_usermode & UM_SIGNAL)
  188. _exception(SIGBUS, fp, BUS_ADRALN, fp->ea);
  189. else
  190. fp->ea += 4; /* else advance */
  191. }
  192. static void __init misaligned_calc_reg_offsets(void)
  193. {
  194. int i, r, offset;
  195. /* pre-calc offsets of registers on sys call stack frame */
  196. offset = 0;
  197. /* struct pt_regs */
  198. for (i = 0; i < 16; i++) {
  199. r = sys_stack_frame_reg_offset[i];
  200. reg_offsets[r] = offset;
  201. offset += 4;
  202. }
  203. /* struct switch_stack */
  204. offset = -sizeof(struct switch_stack);
  205. for (i = 16; i < 32; i++) {
  206. r = sys_stack_frame_reg_offset[i];
  207. reg_offsets[r] = offset;
  208. offset += 4;
  209. }
  210. }
  211. static int __init misaligned_init(void)
  212. {
  213. /* default mode - silent fix */
  214. ma_usermode = UM_FIXUP | KM_WARN;
  215. misaligned_calc_reg_offsets();
  216. return 0;
  217. }
  218. fs_initcall(misaligned_init);