misaligned.c 5.3 KB

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