ptrace_h.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ptrace cpu depend helper functions
  3. *
  4. * Copyright 2003, 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file COPYING in the main directory of
  8. * this archive for more details.
  9. */
  10. #include <linux/linkage.h>
  11. #include <linux/sched.h>
  12. #include <asm/ptrace.h>
  13. #define BREAKINST 0x5730 /* trapa #3 */
  14. /* disable singlestep */
  15. void user_disable_single_step(struct task_struct *child)
  16. {
  17. if ((long)child->thread.breakinfo.addr != -1L) {
  18. *(child->thread.breakinfo.addr) = child->thread.breakinfo.inst;
  19. child->thread.breakinfo.addr = (unsigned short *)-1L;
  20. }
  21. }
  22. /* calculate next pc */
  23. enum jump_type {none, /* normal instruction */
  24. jabs, /* absolute address jump */
  25. ind, /* indirect address jump */
  26. ret, /* return to subrutine */
  27. reg, /* register indexed jump */
  28. relb, /* pc relative jump (byte offset) */
  29. relw, /* pc relative jump (word offset) */
  30. };
  31. /* opcode decode table define
  32. ptn: opcode pattern
  33. msk: opcode bitmask
  34. len: instruction length (<0 next table index)
  35. jmp: jump operation mode */
  36. struct optable {
  37. unsigned char bitpattern;
  38. unsigned char bitmask;
  39. signed char length;
  40. signed char type;
  41. } __packed __aligned(1);
  42. #define OPTABLE(ptn, msk, len, jmp) \
  43. { \
  44. .bitpattern = ptn, \
  45. .bitmask = msk, \
  46. .length = len, \
  47. .type = jmp, \
  48. }
  49. static const struct optable optable_0[] = {
  50. OPTABLE(0x00, 0xff, 1, none), /* 0x00 */
  51. OPTABLE(0x01, 0xff, -1, none), /* 0x01 */
  52. OPTABLE(0x02, 0xfe, 1, none), /* 0x02-0x03 */
  53. OPTABLE(0x04, 0xee, 1, none), /* 0x04-0x05/0x14-0x15 */
  54. OPTABLE(0x06, 0xfe, 1, none), /* 0x06-0x07 */
  55. OPTABLE(0x08, 0xea, 1, none), /* 0x08-0x09/0x0c-0x0d/0x18-0x19/0x1c-0x1d */
  56. OPTABLE(0x0a, 0xee, 1, none), /* 0x0a-0x0b/0x1a-0x1b */
  57. OPTABLE(0x0e, 0xee, 1, none), /* 0x0e-0x0f/0x1e-0x1f */
  58. OPTABLE(0x10, 0xfc, 1, none), /* 0x10-0x13 */
  59. OPTABLE(0x16, 0xfe, 1, none), /* 0x16-0x17 */
  60. OPTABLE(0x20, 0xe0, 1, none), /* 0x20-0x3f */
  61. OPTABLE(0x40, 0xf0, 1, relb), /* 0x40-0x4f */
  62. OPTABLE(0x50, 0xfc, 1, none), /* 0x50-0x53 */
  63. OPTABLE(0x54, 0xfd, 1, ret), /* 0x54/0x56 */
  64. OPTABLE(0x55, 0xff, 1, relb), /* 0x55 */
  65. OPTABLE(0x57, 0xff, 1, none), /* 0x57 */
  66. OPTABLE(0x58, 0xfb, 2, relw), /* 0x58/0x5c */
  67. OPTABLE(0x59, 0xfb, 1, reg), /* 0x59/0x5b */
  68. OPTABLE(0x5a, 0xfb, 2, jabs), /* 0x5a/0x5e */
  69. OPTABLE(0x5b, 0xfb, 2, ind), /* 0x5b/0x5f */
  70. OPTABLE(0x60, 0xe8, 1, none), /* 0x60-0x67/0x70-0x77 */
  71. OPTABLE(0x68, 0xfa, 1, none), /* 0x68-0x69/0x6c-0x6d */
  72. OPTABLE(0x6a, 0xfe, -2, none), /* 0x6a-0x6b */
  73. OPTABLE(0x6e, 0xfe, 2, none), /* 0x6e-0x6f */
  74. OPTABLE(0x78, 0xff, 4, none), /* 0x78 */
  75. OPTABLE(0x79, 0xff, 2, none), /* 0x79 */
  76. OPTABLE(0x7a, 0xff, 3, none), /* 0x7a */
  77. OPTABLE(0x7b, 0xff, 2, none), /* 0x7b */
  78. OPTABLE(0x7c, 0xfc, 2, none), /* 0x7c-0x7f */
  79. OPTABLE(0x80, 0x80, 1, none), /* 0x80-0xff */
  80. };
  81. static const struct optable optable_1[] = {
  82. OPTABLE(0x00, 0xff, -3, none), /* 0x0100 */
  83. OPTABLE(0x40, 0xf0, -3, none), /* 0x0140-0x14f */
  84. OPTABLE(0x80, 0xf0, 1, none), /* 0x0180-0x018f */
  85. OPTABLE(0xc0, 0xc0, 2, none), /* 0x01c0-0x01ff */
  86. };
  87. static const struct optable optable_2[] = {
  88. OPTABLE(0x00, 0x20, 2, none), /* 0x6a0?/0x6a8?/0x6b0?/0x6b8? */
  89. OPTABLE(0x20, 0x20, 3, none), /* 0x6a2?/0x6aa?/0x6b2?/0x6ba? */
  90. };
  91. static const struct optable optable_3[] = {
  92. OPTABLE(0x69, 0xfb, 2, none), /* 0x010069/0x01006d/014069/0x01406d */
  93. OPTABLE(0x6b, 0xff, -4, none), /* 0x01006b/0x01406b */
  94. OPTABLE(0x6f, 0xff, 3, none), /* 0x01006f/0x01406f */
  95. OPTABLE(0x78, 0xff, 5, none), /* 0x010078/0x014078 */
  96. };
  97. static const struct optable optable_4[] = {
  98. /* 0x0100690?/0x01006d0?/0140690?/0x01406d0?/
  99. 0x0100698?/0x01006d8?/0140698?/0x01406d8? */
  100. OPTABLE(0x00, 0x78, 3, none),
  101. /* 0x0100692?/0x01006d2?/0140692?/0x01406d2?/
  102. 0x010069a?/0x01006da?/014069a?/0x01406da? */
  103. OPTABLE(0x20, 0x78, 4, none),
  104. };
  105. static const struct optables_list {
  106. const struct optable *ptr;
  107. int size;
  108. } optables[] = {
  109. #define OPTABLES(no) \
  110. { \
  111. .ptr = optable_##no, \
  112. .size = sizeof(optable_##no) / sizeof(struct optable), \
  113. }
  114. OPTABLES(0),
  115. OPTABLES(1),
  116. OPTABLES(2),
  117. OPTABLES(3),
  118. OPTABLES(4),
  119. };
  120. const unsigned char condmask[] = {
  121. 0x00, 0x40, 0x01, 0x04, 0x02, 0x08, 0x10, 0x20
  122. };
  123. static int isbranch(struct task_struct *task, int reson)
  124. {
  125. unsigned char cond = h8300_get_reg(task, PT_CCR);
  126. /* encode complex conditions */
  127. /* B4: N^V
  128. B5: Z|(N^V)
  129. B6: C|Z */
  130. __asm__("bld #3,%w0\n\t"
  131. "bxor #1,%w0\n\t"
  132. "bst #4,%w0\n\t"
  133. "bor #2,%w0\n\t"
  134. "bst #5,%w0\n\t"
  135. "bld #2,%w0\n\t"
  136. "bor #0,%w0\n\t"
  137. "bst #6,%w0\n\t"
  138. : "=&r"(cond) : "0"(cond) : "cc");
  139. cond &= condmask[reson >> 1];
  140. if (!(reson & 1))
  141. return cond == 0;
  142. else
  143. return cond != 0;
  144. }
  145. static unsigned short *decode(struct task_struct *child,
  146. const struct optable *op,
  147. char *fetch_p, unsigned short *pc,
  148. unsigned char inst)
  149. {
  150. unsigned long addr;
  151. unsigned long *sp;
  152. int regno;
  153. switch (op->type) {
  154. case none:
  155. return (unsigned short *)pc + op->length;
  156. case jabs:
  157. addr = *(unsigned long *)pc;
  158. return (unsigned short *)(addr & 0x00ffffff);
  159. case ind:
  160. addr = *pc & 0xff;
  161. return (unsigned short *)(*(unsigned long *)addr);
  162. case ret:
  163. sp = (unsigned long *)h8300_get_reg(child, PT_USP);
  164. /* user stack frames
  165. | er0 | temporary saved
  166. +--------+
  167. | exp | exception stack frames
  168. +--------+
  169. | ret pc | userspace return address
  170. */
  171. return (unsigned short *)(*(sp+2) & 0x00ffffff);
  172. case reg:
  173. regno = (*pc >> 4) & 0x07;
  174. if (regno == 0)
  175. addr = h8300_get_reg(child, PT_ER0);
  176. else
  177. addr = h8300_get_reg(child, regno-1 + PT_ER1);
  178. return (unsigned short *)addr;
  179. case relb:
  180. if (inst == 0x55 || isbranch(child, inst & 0x0f))
  181. pc = (unsigned short *)((unsigned long)pc +
  182. ((signed char)(*fetch_p)));
  183. return pc+1; /* skip myself */
  184. case relw:
  185. if (inst == 0x5c || isbranch(child, (*fetch_p & 0xf0) >> 4))
  186. pc = (unsigned short *)((unsigned long)pc +
  187. ((signed short)(*(pc+1))));
  188. return pc+2; /* skip myself */
  189. default:
  190. return NULL;
  191. }
  192. }
  193. static unsigned short *nextpc(struct task_struct *child, unsigned short *pc)
  194. {
  195. const struct optable *op;
  196. unsigned char *fetch_p;
  197. int op_len;
  198. unsigned char inst;
  199. op = optables[0].ptr;
  200. op_len = optables[0].size;
  201. fetch_p = (unsigned char *)pc;
  202. inst = *fetch_p++;
  203. do {
  204. if ((inst & op->bitmask) == op->bitpattern) {
  205. if (op->length < 0) {
  206. op = optables[-op->length].ptr;
  207. op_len = optables[-op->length].size + 1;
  208. inst = *fetch_p++;
  209. } else
  210. return decode(child, op, fetch_p, pc, inst);
  211. } else
  212. op++;
  213. } while (--op_len > 0);
  214. return NULL;
  215. }
  216. /* Set breakpoint(s) to simulate a single step from the current PC. */
  217. void user_enable_single_step(struct task_struct *child)
  218. {
  219. unsigned short *next;
  220. next = nextpc(child, (unsigned short *)h8300_get_reg(child, PT_PC));
  221. child->thread.breakinfo.addr = next;
  222. child->thread.breakinfo.inst = *next;
  223. *next = BREAKINST;
  224. }
  225. asmlinkage void trace_trap(unsigned long bp)
  226. {
  227. if ((unsigned long)current->thread.breakinfo.addr == bp) {
  228. user_disable_single_step(current);
  229. force_sig(SIGTRAP, current);
  230. } else
  231. force_sig(SIGILL, current);
  232. }