kprobes-arm.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * arch/arm/kernel/kprobes-decode.c
  3. *
  4. * Copyright (C) 2006, 2007 Motorola Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. /*
  16. * We do not have hardware single-stepping on ARM, This
  17. * effort is further complicated by the ARM not having a
  18. * "next PC" register. Instructions that change the PC
  19. * can't be safely single-stepped in a MP environment, so
  20. * we have a lot of work to do:
  21. *
  22. * In the prepare phase:
  23. * *) If it is an instruction that does anything
  24. * with the CPU mode, we reject it for a kprobe.
  25. * (This is out of laziness rather than need. The
  26. * instructions could be simulated.)
  27. *
  28. * *) Otherwise, decode the instruction rewriting its
  29. * registers to take fixed, ordered registers and
  30. * setting a handler for it to run the instruction.
  31. *
  32. * In the execution phase by an instruction's handler:
  33. *
  34. * *) If the PC is written to by the instruction, the
  35. * instruction must be fully simulated in software.
  36. *
  37. * *) Otherwise, a modified form of the instruction is
  38. * directly executed. Its handler calls the
  39. * instruction in insn[0]. In insn[1] is a
  40. * "mov pc, lr" to return.
  41. *
  42. * Before calling, load up the reordered registers
  43. * from the original instruction's registers. If one
  44. * of the original input registers is the PC, compute
  45. * and adjust the appropriate input register.
  46. *
  47. * After call completes, copy the output registers to
  48. * the original instruction's original registers.
  49. *
  50. * We don't use a real breakpoint instruction since that
  51. * would have us in the kernel go from SVC mode to SVC
  52. * mode losing the link register. Instead we use an
  53. * undefined instruction. To simplify processing, the
  54. * undefined instruction used for kprobes must be reserved
  55. * exclusively for kprobes use.
  56. *
  57. * TODO: ifdef out some instruction decoding based on architecture.
  58. */
  59. #include <linux/kernel.h>
  60. #include <linux/kprobes.h>
  61. #include "kprobes.h"
  62. #define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
  63. #define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25)
  64. #define is_r15(insn, bitpos) (((insn) & (0xf << bitpos)) == (0xf << bitpos))
  65. #define PSR_fs (PSR_f|PSR_s)
  66. #define KPROBE_RETURN_INSTRUCTION 0xe1a0f00e /* mov pc, lr */
  67. typedef long (insn_0arg_fn_t)(void);
  68. typedef long (insn_1arg_fn_t)(long);
  69. typedef long (insn_2arg_fn_t)(long, long);
  70. typedef long (insn_3arg_fn_t)(long, long, long);
  71. typedef long (insn_4arg_fn_t)(long, long, long, long);
  72. typedef long long (insn_llret_0arg_fn_t)(void);
  73. typedef long long (insn_llret_3arg_fn_t)(long, long, long);
  74. typedef long long (insn_llret_4arg_fn_t)(long, long, long, long);
  75. union reg_pair {
  76. long long dr;
  77. #ifdef __LITTLE_ENDIAN
  78. struct { long r0, r1; };
  79. #else
  80. struct { long r1, r0; };
  81. #endif
  82. };
  83. /*
  84. * The insnslot_?arg_r[w]flags() functions below are to keep the
  85. * msr -> *fn -> mrs instruction sequences indivisible so that
  86. * the state of the CPSR flags aren't inadvertently modified
  87. * just before or just after the call.
  88. */
  89. static inline long __kprobes
  90. insnslot_0arg_rflags(long cpsr, insn_0arg_fn_t *fn)
  91. {
  92. register long ret asm("r0");
  93. __asm__ __volatile__ (
  94. "msr cpsr_fs, %[cpsr] \n\t"
  95. "mov lr, pc \n\t"
  96. "mov pc, %[fn] \n\t"
  97. : "=r" (ret)
  98. : [cpsr] "r" (cpsr), [fn] "r" (fn)
  99. : "lr", "cc"
  100. );
  101. return ret;
  102. }
  103. static inline long long __kprobes
  104. insnslot_llret_0arg_rflags(long cpsr, insn_llret_0arg_fn_t *fn)
  105. {
  106. register long ret0 asm("r0");
  107. register long ret1 asm("r1");
  108. union reg_pair fnr;
  109. __asm__ __volatile__ (
  110. "msr cpsr_fs, %[cpsr] \n\t"
  111. "mov lr, pc \n\t"
  112. "mov pc, %[fn] \n\t"
  113. : "=r" (ret0), "=r" (ret1)
  114. : [cpsr] "r" (cpsr), [fn] "r" (fn)
  115. : "lr", "cc"
  116. );
  117. fnr.r0 = ret0;
  118. fnr.r1 = ret1;
  119. return fnr.dr;
  120. }
  121. static inline long __kprobes
  122. insnslot_1arg_rflags(long r0, long cpsr, insn_1arg_fn_t *fn)
  123. {
  124. register long rr0 asm("r0") = r0;
  125. register long ret asm("r0");
  126. __asm__ __volatile__ (
  127. "msr cpsr_fs, %[cpsr] \n\t"
  128. "mov lr, pc \n\t"
  129. "mov pc, %[fn] \n\t"
  130. : "=r" (ret)
  131. : "0" (rr0), [cpsr] "r" (cpsr), [fn] "r" (fn)
  132. : "lr", "cc"
  133. );
  134. return ret;
  135. }
  136. static inline long __kprobes
  137. insnslot_2arg_rflags(long r0, long r1, long cpsr, insn_2arg_fn_t *fn)
  138. {
  139. register long rr0 asm("r0") = r0;
  140. register long rr1 asm("r1") = r1;
  141. register long ret asm("r0");
  142. __asm__ __volatile__ (
  143. "msr cpsr_fs, %[cpsr] \n\t"
  144. "mov lr, pc \n\t"
  145. "mov pc, %[fn] \n\t"
  146. : "=r" (ret)
  147. : "0" (rr0), "r" (rr1),
  148. [cpsr] "r" (cpsr), [fn] "r" (fn)
  149. : "lr", "cc"
  150. );
  151. return ret;
  152. }
  153. static inline long __kprobes
  154. insnslot_3arg_rflags(long r0, long r1, long r2, long cpsr, insn_3arg_fn_t *fn)
  155. {
  156. register long rr0 asm("r0") = r0;
  157. register long rr1 asm("r1") = r1;
  158. register long rr2 asm("r2") = r2;
  159. register long ret asm("r0");
  160. __asm__ __volatile__ (
  161. "msr cpsr_fs, %[cpsr] \n\t"
  162. "mov lr, pc \n\t"
  163. "mov pc, %[fn] \n\t"
  164. : "=r" (ret)
  165. : "0" (rr0), "r" (rr1), "r" (rr2),
  166. [cpsr] "r" (cpsr), [fn] "r" (fn)
  167. : "lr", "cc"
  168. );
  169. return ret;
  170. }
  171. static inline long long __kprobes
  172. insnslot_llret_3arg_rflags(long r0, long r1, long r2, long cpsr,
  173. insn_llret_3arg_fn_t *fn)
  174. {
  175. register long rr0 asm("r0") = r0;
  176. register long rr1 asm("r1") = r1;
  177. register long rr2 asm("r2") = r2;
  178. register long ret0 asm("r0");
  179. register long ret1 asm("r1");
  180. union reg_pair fnr;
  181. __asm__ __volatile__ (
  182. "msr cpsr_fs, %[cpsr] \n\t"
  183. "mov lr, pc \n\t"
  184. "mov pc, %[fn] \n\t"
  185. : "=r" (ret0), "=r" (ret1)
  186. : "0" (rr0), "r" (rr1), "r" (rr2),
  187. [cpsr] "r" (cpsr), [fn] "r" (fn)
  188. : "lr", "cc"
  189. );
  190. fnr.r0 = ret0;
  191. fnr.r1 = ret1;
  192. return fnr.dr;
  193. }
  194. static inline long __kprobes
  195. insnslot_4arg_rflags(long r0, long r1, long r2, long r3, long cpsr,
  196. insn_4arg_fn_t *fn)
  197. {
  198. register long rr0 asm("r0") = r0;
  199. register long rr1 asm("r1") = r1;
  200. register long rr2 asm("r2") = r2;
  201. register long rr3 asm("r3") = r3;
  202. register long ret asm("r0");
  203. __asm__ __volatile__ (
  204. "msr cpsr_fs, %[cpsr] \n\t"
  205. "mov lr, pc \n\t"
  206. "mov pc, %[fn] \n\t"
  207. : "=r" (ret)
  208. : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3),
  209. [cpsr] "r" (cpsr), [fn] "r" (fn)
  210. : "lr", "cc"
  211. );
  212. return ret;
  213. }
  214. static inline long __kprobes
  215. insnslot_1arg_rwflags(long r0, long *cpsr, insn_1arg_fn_t *fn)
  216. {
  217. register long rr0 asm("r0") = r0;
  218. register long ret asm("r0");
  219. long oldcpsr = *cpsr;
  220. long newcpsr;
  221. __asm__ __volatile__ (
  222. "msr cpsr_fs, %[oldcpsr] \n\t"
  223. "mov lr, pc \n\t"
  224. "mov pc, %[fn] \n\t"
  225. "mrs %[newcpsr], cpsr \n\t"
  226. : "=r" (ret), [newcpsr] "=r" (newcpsr)
  227. : "0" (rr0), [oldcpsr] "r" (oldcpsr), [fn] "r" (fn)
  228. : "lr", "cc"
  229. );
  230. *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs);
  231. return ret;
  232. }
  233. static inline long __kprobes
  234. insnslot_2arg_rwflags(long r0, long r1, long *cpsr, insn_2arg_fn_t *fn)
  235. {
  236. register long rr0 asm("r0") = r0;
  237. register long rr1 asm("r1") = r1;
  238. register long ret asm("r0");
  239. long oldcpsr = *cpsr;
  240. long newcpsr;
  241. __asm__ __volatile__ (
  242. "msr cpsr_fs, %[oldcpsr] \n\t"
  243. "mov lr, pc \n\t"
  244. "mov pc, %[fn] \n\t"
  245. "mrs %[newcpsr], cpsr \n\t"
  246. : "=r" (ret), [newcpsr] "=r" (newcpsr)
  247. : "0" (rr0), "r" (rr1), [oldcpsr] "r" (oldcpsr), [fn] "r" (fn)
  248. : "lr", "cc"
  249. );
  250. *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs);
  251. return ret;
  252. }
  253. static inline long __kprobes
  254. insnslot_3arg_rwflags(long r0, long r1, long r2, long *cpsr,
  255. insn_3arg_fn_t *fn)
  256. {
  257. register long rr0 asm("r0") = r0;
  258. register long rr1 asm("r1") = r1;
  259. register long rr2 asm("r2") = r2;
  260. register long ret asm("r0");
  261. long oldcpsr = *cpsr;
  262. long newcpsr;
  263. __asm__ __volatile__ (
  264. "msr cpsr_fs, %[oldcpsr] \n\t"
  265. "mov lr, pc \n\t"
  266. "mov pc, %[fn] \n\t"
  267. "mrs %[newcpsr], cpsr \n\t"
  268. : "=r" (ret), [newcpsr] "=r" (newcpsr)
  269. : "0" (rr0), "r" (rr1), "r" (rr2),
  270. [oldcpsr] "r" (oldcpsr), [fn] "r" (fn)
  271. : "lr", "cc"
  272. );
  273. *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs);
  274. return ret;
  275. }
  276. static inline long __kprobes
  277. insnslot_4arg_rwflags(long r0, long r1, long r2, long r3, long *cpsr,
  278. insn_4arg_fn_t *fn)
  279. {
  280. register long rr0 asm("r0") = r0;
  281. register long rr1 asm("r1") = r1;
  282. register long rr2 asm("r2") = r2;
  283. register long rr3 asm("r3") = r3;
  284. register long ret asm("r0");
  285. long oldcpsr = *cpsr;
  286. long newcpsr;
  287. __asm__ __volatile__ (
  288. "msr cpsr_fs, %[oldcpsr] \n\t"
  289. "mov lr, pc \n\t"
  290. "mov pc, %[fn] \n\t"
  291. "mrs %[newcpsr], cpsr \n\t"
  292. : "=r" (ret), [newcpsr] "=r" (newcpsr)
  293. : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3),
  294. [oldcpsr] "r" (oldcpsr), [fn] "r" (fn)
  295. : "lr", "cc"
  296. );
  297. *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs);
  298. return ret;
  299. }
  300. static inline long long __kprobes
  301. insnslot_llret_4arg_rwflags(long r0, long r1, long r2, long r3, long *cpsr,
  302. insn_llret_4arg_fn_t *fn)
  303. {
  304. register long rr0 asm("r0") = r0;
  305. register long rr1 asm("r1") = r1;
  306. register long rr2 asm("r2") = r2;
  307. register long rr3 asm("r3") = r3;
  308. register long ret0 asm("r0");
  309. register long ret1 asm("r1");
  310. long oldcpsr = *cpsr;
  311. long newcpsr;
  312. union reg_pair fnr;
  313. __asm__ __volatile__ (
  314. "msr cpsr_fs, %[oldcpsr] \n\t"
  315. "mov lr, pc \n\t"
  316. "mov pc, %[fn] \n\t"
  317. "mrs %[newcpsr], cpsr \n\t"
  318. : "=r" (ret0), "=r" (ret1), [newcpsr] "=r" (newcpsr)
  319. : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3),
  320. [oldcpsr] "r" (oldcpsr), [fn] "r" (fn)
  321. : "lr", "cc"
  322. );
  323. *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs);
  324. fnr.r0 = ret0;
  325. fnr.r1 = ret1;
  326. return fnr.dr;
  327. }
  328. /*
  329. * To avoid the complications of mimicing single-stepping on a
  330. * processor without a Next-PC or a single-step mode, and to
  331. * avoid having to deal with the side-effects of boosting, we
  332. * simulate or emulate (almost) all ARM instructions.
  333. *
  334. * "Simulation" is where the instruction's behavior is duplicated in
  335. * C code. "Emulation" is where the original instruction is rewritten
  336. * and executed, often by altering its registers.
  337. *
  338. * By having all behavior of the kprobe'd instruction completed before
  339. * returning from the kprobe_handler(), all locks (scheduler and
  340. * interrupt) can safely be released. There is no need for secondary
  341. * breakpoints, no race with MP or preemptable kernels, nor having to
  342. * clean up resources counts at a later time impacting overall system
  343. * performance. By rewriting the instruction, only the minimum registers
  344. * need to be loaded and saved back optimizing performance.
  345. *
  346. * Calling the insnslot_*_rwflags version of a function doesn't hurt
  347. * anything even when the CPSR flags aren't updated by the
  348. * instruction. It's just a little slower in return for saving
  349. * a little space by not having a duplicate function that doesn't
  350. * update the flags. (The same optimization can be said for
  351. * instructions that do or don't perform register writeback)
  352. * Also, instructions can either read the flags, only write the
  353. * flags, or read and write the flags. To save combinations
  354. * rather than for sheer performance, flag functions just assume
  355. * read and write of flags.
  356. */
  357. static void __kprobes simulate_bbl(struct kprobe *p, struct pt_regs *regs)
  358. {
  359. kprobe_opcode_t insn = p->opcode;
  360. long iaddr = (long)p->addr;
  361. int disp = branch_displacement(insn);
  362. if (insn & (1 << 24))
  363. regs->ARM_lr = iaddr + 4;
  364. regs->ARM_pc = iaddr + 8 + disp;
  365. }
  366. static void __kprobes simulate_blx1(struct kprobe *p, struct pt_regs *regs)
  367. {
  368. kprobe_opcode_t insn = p->opcode;
  369. long iaddr = (long)p->addr;
  370. int disp = branch_displacement(insn);
  371. regs->ARM_lr = iaddr + 4;
  372. regs->ARM_pc = iaddr + 8 + disp + ((insn >> 23) & 0x2);
  373. regs->ARM_cpsr |= PSR_T_BIT;
  374. }
  375. static void __kprobes simulate_blx2bx(struct kprobe *p, struct pt_regs *regs)
  376. {
  377. kprobe_opcode_t insn = p->opcode;
  378. int rm = insn & 0xf;
  379. long rmv = regs->uregs[rm];
  380. if (insn & (1 << 5))
  381. regs->ARM_lr = (long)p->addr + 4;
  382. regs->ARM_pc = rmv & ~0x1;
  383. regs->ARM_cpsr &= ~PSR_T_BIT;
  384. if (rmv & 0x1)
  385. regs->ARM_cpsr |= PSR_T_BIT;
  386. }
  387. static void __kprobes simulate_mrs(struct kprobe *p, struct pt_regs *regs)
  388. {
  389. kprobe_opcode_t insn = p->opcode;
  390. int rd = (insn >> 12) & 0xf;
  391. unsigned long mask = 0xf8ff03df; /* Mask out execution state */
  392. regs->uregs[rd] = regs->ARM_cpsr & mask;
  393. }
  394. static void __kprobes simulate_mov_ipsp(struct kprobe *p, struct pt_regs *regs)
  395. {
  396. regs->uregs[12] = regs->uregs[13];
  397. }
  398. static void __kprobes emulate_ldrd(struct kprobe *p, struct pt_regs *regs)
  399. {
  400. insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0];
  401. kprobe_opcode_t insn = p->opcode;
  402. long ppc = (long)p->addr + 8;
  403. int rd = (insn >> 12) & 0xf;
  404. int rn = (insn >> 16) & 0xf;
  405. int rm = insn & 0xf; /* rm may be invalid, don't care. */
  406. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  407. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  408. /* Not following the C calling convention here, so need asm(). */
  409. __asm__ __volatile__ (
  410. "ldr r0, %[rn] \n\t"
  411. "ldr r1, %[rm] \n\t"
  412. "msr cpsr_fs, %[cpsr]\n\t"
  413. "mov lr, pc \n\t"
  414. "mov pc, %[i_fn] \n\t"
  415. "str r0, %[rn] \n\t" /* in case of writeback */
  416. "str r2, %[rd0] \n\t"
  417. "str r3, %[rd1] \n\t"
  418. : [rn] "+m" (rnv),
  419. [rd0] "=m" (regs->uregs[rd]),
  420. [rd1] "=m" (regs->uregs[rd+1])
  421. : [rm] "m" (rmv),
  422. [cpsr] "r" (regs->ARM_cpsr),
  423. [i_fn] "r" (i_fn)
  424. : "r0", "r1", "r2", "r3", "lr", "cc"
  425. );
  426. if (is_writeback(insn))
  427. regs->uregs[rn] = rnv;
  428. }
  429. static void __kprobes emulate_strd(struct kprobe *p, struct pt_regs *regs)
  430. {
  431. insn_4arg_fn_t *i_fn = (insn_4arg_fn_t *)&p->ainsn.insn[0];
  432. kprobe_opcode_t insn = p->opcode;
  433. long ppc = (long)p->addr + 8;
  434. int rd = (insn >> 12) & 0xf;
  435. int rn = (insn >> 16) & 0xf;
  436. int rm = insn & 0xf;
  437. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  438. /* rm/rmv may be invalid, don't care. */
  439. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  440. long rnv_wb;
  441. rnv_wb = insnslot_4arg_rflags(rnv, rmv, regs->uregs[rd],
  442. regs->uregs[rd+1],
  443. regs->ARM_cpsr, i_fn);
  444. if (is_writeback(insn))
  445. regs->uregs[rn] = rnv_wb;
  446. }
  447. static void __kprobes emulate_ldr(struct kprobe *p, struct pt_regs *regs)
  448. {
  449. insn_llret_3arg_fn_t *i_fn = (insn_llret_3arg_fn_t *)&p->ainsn.insn[0];
  450. kprobe_opcode_t insn = p->opcode;
  451. long ppc = (long)p->addr + 8;
  452. union reg_pair fnr;
  453. int rd = (insn >> 12) & 0xf;
  454. int rn = (insn >> 16) & 0xf;
  455. int rm = insn & 0xf;
  456. long rdv;
  457. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  458. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  459. long cpsr = regs->ARM_cpsr;
  460. fnr.dr = insnslot_llret_3arg_rflags(rnv, 0, rmv, cpsr, i_fn);
  461. if (rn != 15)
  462. regs->uregs[rn] = fnr.r0; /* Save Rn in case of writeback. */
  463. rdv = fnr.r1;
  464. if (rd == 15) {
  465. #if __LINUX_ARM_ARCH__ >= 5
  466. cpsr &= ~PSR_T_BIT;
  467. if (rdv & 0x1)
  468. cpsr |= PSR_T_BIT;
  469. regs->ARM_cpsr = cpsr;
  470. rdv &= ~0x1;
  471. #else
  472. rdv &= ~0x2;
  473. #endif
  474. }
  475. regs->uregs[rd] = rdv;
  476. }
  477. static void __kprobes emulate_str(struct kprobe *p, struct pt_regs *regs)
  478. {
  479. insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0];
  480. kprobe_opcode_t insn = p->opcode;
  481. long iaddr = (long)p->addr;
  482. int rd = (insn >> 12) & 0xf;
  483. int rn = (insn >> 16) & 0xf;
  484. int rm = insn & 0xf;
  485. long rdv = (rd == 15) ? iaddr + str_pc_offset : regs->uregs[rd];
  486. long rnv = (rn == 15) ? iaddr + 8 : regs->uregs[rn];
  487. long rmv = regs->uregs[rm]; /* rm/rmv may be invalid, don't care. */
  488. long rnv_wb;
  489. rnv_wb = insnslot_3arg_rflags(rnv, rdv, rmv, regs->ARM_cpsr, i_fn);
  490. if (rn != 15)
  491. regs->uregs[rn] = rnv_wb; /* Save Rn in case of writeback. */
  492. }
  493. static void __kprobes emulate_sat(struct kprobe *p, struct pt_regs *regs)
  494. {
  495. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  496. kprobe_opcode_t insn = p->opcode;
  497. int rd = (insn >> 12) & 0xf;
  498. int rm = insn & 0xf;
  499. long rmv = regs->uregs[rm];
  500. /* Writes Q flag */
  501. regs->uregs[rd] = insnslot_1arg_rwflags(rmv, &regs->ARM_cpsr, i_fn);
  502. }
  503. static void __kprobes emulate_sel(struct kprobe *p, struct pt_regs *regs)
  504. {
  505. insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0];
  506. kprobe_opcode_t insn = p->opcode;
  507. int rd = (insn >> 12) & 0xf;
  508. int rn = (insn >> 16) & 0xf;
  509. int rm = insn & 0xf;
  510. long rnv = regs->uregs[rn];
  511. long rmv = regs->uregs[rm];
  512. /* Reads GE bits */
  513. regs->uregs[rd] = insnslot_2arg_rflags(rnv, rmv, regs->ARM_cpsr, i_fn);
  514. }
  515. static void __kprobes emulate_none(struct kprobe *p, struct pt_regs *regs)
  516. {
  517. insn_0arg_fn_t *i_fn = (insn_0arg_fn_t *)&p->ainsn.insn[0];
  518. insnslot_0arg_rflags(regs->ARM_cpsr, i_fn);
  519. }
  520. static void __kprobes emulate_nop(struct kprobe *p, struct pt_regs *regs)
  521. {
  522. }
  523. static void __kprobes
  524. emulate_rd12_modify(struct kprobe *p, struct pt_regs *regs)
  525. {
  526. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  527. kprobe_opcode_t insn = p->opcode;
  528. int rd = (insn >> 12) & 0xf;
  529. long rdv = regs->uregs[rd];
  530. regs->uregs[rd] = insnslot_1arg_rflags(rdv, regs->ARM_cpsr, i_fn);
  531. }
  532. static void __kprobes
  533. emulate_rd12rn0_modify(struct kprobe *p, struct pt_regs *regs)
  534. {
  535. insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0];
  536. kprobe_opcode_t insn = p->opcode;
  537. int rd = (insn >> 12) & 0xf;
  538. int rn = insn & 0xf;
  539. long rdv = regs->uregs[rd];
  540. long rnv = regs->uregs[rn];
  541. regs->uregs[rd] = insnslot_2arg_rflags(rdv, rnv, regs->ARM_cpsr, i_fn);
  542. }
  543. static void __kprobes emulate_rd12rm0(struct kprobe *p, struct pt_regs *regs)
  544. {
  545. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  546. kprobe_opcode_t insn = p->opcode;
  547. int rd = (insn >> 12) & 0xf;
  548. int rm = insn & 0xf;
  549. long rmv = regs->uregs[rm];
  550. regs->uregs[rd] = insnslot_1arg_rflags(rmv, regs->ARM_cpsr, i_fn);
  551. }
  552. static void __kprobes
  553. emulate_rd12rn16rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
  554. {
  555. insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0];
  556. kprobe_opcode_t insn = p->opcode;
  557. int rd = (insn >> 12) & 0xf;
  558. int rn = (insn >> 16) & 0xf;
  559. int rm = insn & 0xf;
  560. long rnv = regs->uregs[rn];
  561. long rmv = regs->uregs[rm];
  562. regs->uregs[rd] =
  563. insnslot_2arg_rwflags(rnv, rmv, &regs->ARM_cpsr, i_fn);
  564. }
  565. static void __kprobes
  566. emulate_rd16rn12rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
  567. {
  568. insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0];
  569. kprobe_opcode_t insn = p->opcode;
  570. int rd = (insn >> 16) & 0xf;
  571. int rn = (insn >> 12) & 0xf;
  572. int rs = (insn >> 8) & 0xf;
  573. int rm = insn & 0xf;
  574. long rnv = regs->uregs[rn];
  575. long rsv = regs->uregs[rs];
  576. long rmv = regs->uregs[rm];
  577. regs->uregs[rd] =
  578. insnslot_3arg_rwflags(rnv, rsv, rmv, &regs->ARM_cpsr, i_fn);
  579. }
  580. static void __kprobes
  581. emulate_rd16rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
  582. {
  583. insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0];
  584. kprobe_opcode_t insn = p->opcode;
  585. int rd = (insn >> 16) & 0xf;
  586. int rs = (insn >> 8) & 0xf;
  587. int rm = insn & 0xf;
  588. long rsv = regs->uregs[rs];
  589. long rmv = regs->uregs[rm];
  590. regs->uregs[rd] =
  591. insnslot_2arg_rwflags(rsv, rmv, &regs->ARM_cpsr, i_fn);
  592. }
  593. static void __kprobes
  594. emulate_rdhi16rdlo12rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
  595. {
  596. insn_llret_4arg_fn_t *i_fn = (insn_llret_4arg_fn_t *)&p->ainsn.insn[0];
  597. kprobe_opcode_t insn = p->opcode;
  598. union reg_pair fnr;
  599. int rdhi = (insn >> 16) & 0xf;
  600. int rdlo = (insn >> 12) & 0xf;
  601. int rs = (insn >> 8) & 0xf;
  602. int rm = insn & 0xf;
  603. long rsv = regs->uregs[rs];
  604. long rmv = regs->uregs[rm];
  605. fnr.dr = insnslot_llret_4arg_rwflags(regs->uregs[rdhi],
  606. regs->uregs[rdlo], rsv, rmv,
  607. &regs->ARM_cpsr, i_fn);
  608. regs->uregs[rdhi] = fnr.r0;
  609. regs->uregs[rdlo] = fnr.r1;
  610. }
  611. static void __kprobes
  612. emulate_alu_imm_rflags(struct kprobe *p, struct pt_regs *regs)
  613. {
  614. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  615. kprobe_opcode_t insn = p->opcode;
  616. int rd = (insn >> 12) & 0xf;
  617. int rn = (insn >> 16) & 0xf;
  618. long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn];
  619. regs->uregs[rd] = insnslot_1arg_rflags(rnv, regs->ARM_cpsr, i_fn);
  620. }
  621. static void __kprobes
  622. emulate_alu_imm_rwflags(struct kprobe *p, struct pt_regs *regs)
  623. {
  624. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  625. kprobe_opcode_t insn = p->opcode;
  626. int rd = (insn >> 12) & 0xf;
  627. int rn = (insn >> 16) & 0xf;
  628. long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn];
  629. regs->uregs[rd] = insnslot_1arg_rwflags(rnv, &regs->ARM_cpsr, i_fn);
  630. }
  631. static void __kprobes
  632. emulate_alu_tests_imm(struct kprobe *p, struct pt_regs *regs)
  633. {
  634. insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0];
  635. kprobe_opcode_t insn = p->opcode;
  636. int rn = (insn >> 16) & 0xf;
  637. long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn];
  638. insnslot_1arg_rwflags(rnv, &regs->ARM_cpsr, i_fn);
  639. }
  640. static void __kprobes
  641. emulate_alu_rflags(struct kprobe *p, struct pt_regs *regs)
  642. {
  643. insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0];
  644. kprobe_opcode_t insn = p->opcode;
  645. long ppc = (long)p->addr + 8;
  646. int rd = (insn >> 12) & 0xf;
  647. int rn = (insn >> 16) & 0xf; /* rn/rnv/rs/rsv may be */
  648. int rs = (insn >> 8) & 0xf; /* invalid, don't care. */
  649. int rm = insn & 0xf;
  650. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  651. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  652. long rsv = regs->uregs[rs];
  653. regs->uregs[rd] =
  654. insnslot_3arg_rflags(rnv, rmv, rsv, regs->ARM_cpsr, i_fn);
  655. }
  656. static void __kprobes
  657. emulate_alu_rwflags(struct kprobe *p, struct pt_regs *regs)
  658. {
  659. insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0];
  660. kprobe_opcode_t insn = p->opcode;
  661. long ppc = (long)p->addr + 8;
  662. int rd = (insn >> 12) & 0xf;
  663. int rn = (insn >> 16) & 0xf; /* rn/rnv/rs/rsv may be */
  664. int rs = (insn >> 8) & 0xf; /* invalid, don't care. */
  665. int rm = insn & 0xf;
  666. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  667. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  668. long rsv = regs->uregs[rs];
  669. regs->uregs[rd] =
  670. insnslot_3arg_rwflags(rnv, rmv, rsv, &regs->ARM_cpsr, i_fn);
  671. }
  672. static void __kprobes
  673. emulate_alu_tests(struct kprobe *p, struct pt_regs *regs)
  674. {
  675. insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0];
  676. kprobe_opcode_t insn = p->opcode;
  677. long ppc = (long)p->addr + 8;
  678. int rn = (insn >> 16) & 0xf;
  679. int rs = (insn >> 8) & 0xf; /* rs/rsv may be invalid, don't care. */
  680. int rm = insn & 0xf;
  681. long rnv = (rn == 15) ? ppc : regs->uregs[rn];
  682. long rmv = (rm == 15) ? ppc : regs->uregs[rm];
  683. long rsv = regs->uregs[rs];
  684. insnslot_3arg_rwflags(rnv, rmv, rsv, &regs->ARM_cpsr, i_fn);
  685. }
  686. static enum kprobe_insn __kprobes
  687. prep_emulate_ldr_str(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  688. {
  689. int not_imm = (insn & (1 << 26)) ? (insn & (1 << 25))
  690. : (~insn & (1 << 22));
  691. if (is_writeback(insn) && is_r15(insn, 16))
  692. return INSN_REJECTED; /* Writeback to PC */
  693. insn &= 0xfff00fff;
  694. insn |= 0x00001000; /* Rn = r0, Rd = r1 */
  695. if (not_imm) {
  696. insn &= ~0xf;
  697. insn |= 2; /* Rm = r2 */
  698. }
  699. asi->insn[0] = insn;
  700. asi->insn_handler = (insn & (1 << 20)) ? emulate_ldr : emulate_str;
  701. return INSN_GOOD;
  702. }
  703. static enum kprobe_insn __kprobes
  704. prep_emulate_rd12_modify(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  705. {
  706. if (is_r15(insn, 12))
  707. return INSN_REJECTED; /* Rd is PC */
  708. insn &= 0xffff0fff; /* Rd = r0 */
  709. asi->insn[0] = insn;
  710. asi->insn_handler = emulate_rd12_modify;
  711. return INSN_GOOD;
  712. }
  713. static enum kprobe_insn __kprobes
  714. prep_emulate_rd12rn0_modify(kprobe_opcode_t insn,
  715. struct arch_specific_insn *asi)
  716. {
  717. if (is_r15(insn, 12))
  718. return INSN_REJECTED; /* Rd is PC */
  719. insn &= 0xffff0ff0; /* Rd = r0 */
  720. insn |= 0x00000001; /* Rn = r1 */
  721. asi->insn[0] = insn;
  722. asi->insn_handler = emulate_rd12rn0_modify;
  723. return INSN_GOOD;
  724. }
  725. static enum kprobe_insn __kprobes
  726. prep_emulate_rd12rm0(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  727. {
  728. if (is_r15(insn, 12))
  729. return INSN_REJECTED; /* Rd is PC */
  730. insn &= 0xffff0ff0; /* Rd = r0, Rm = r0 */
  731. asi->insn[0] = insn;
  732. asi->insn_handler = emulate_rd12rm0;
  733. return INSN_GOOD;
  734. }
  735. static enum kprobe_insn __kprobes
  736. prep_emulate_rd12rn16rm0_wflags(kprobe_opcode_t insn,
  737. struct arch_specific_insn *asi)
  738. {
  739. if (is_r15(insn, 12))
  740. return INSN_REJECTED; /* Rd is PC */
  741. insn &= 0xfff00ff0; /* Rd = r0, Rn = r0 */
  742. insn |= 0x00000001; /* Rm = r1 */
  743. asi->insn[0] = insn;
  744. asi->insn_handler = emulate_rd12rn16rm0_rwflags;
  745. return INSN_GOOD;
  746. }
  747. static enum kprobe_insn __kprobes
  748. prep_emulate_rd16rs8rm0_wflags(kprobe_opcode_t insn,
  749. struct arch_specific_insn *asi)
  750. {
  751. if (is_r15(insn, 16))
  752. return INSN_REJECTED; /* Rd is PC */
  753. insn &= 0xfff0f0f0; /* Rd = r0, Rs = r0 */
  754. insn |= 0x00000001; /* Rm = r1 */
  755. asi->insn[0] = insn;
  756. asi->insn_handler = emulate_rd16rs8rm0_rwflags;
  757. return INSN_GOOD;
  758. }
  759. static enum kprobe_insn __kprobes
  760. prep_emulate_rd16rn12rs8rm0_wflags(kprobe_opcode_t insn,
  761. struct arch_specific_insn *asi)
  762. {
  763. if (is_r15(insn, 16))
  764. return INSN_REJECTED; /* Rd is PC */
  765. insn &= 0xfff000f0; /* Rd = r0, Rn = r0 */
  766. insn |= 0x00000102; /* Rs = r1, Rm = r2 */
  767. asi->insn[0] = insn;
  768. asi->insn_handler = emulate_rd16rn12rs8rm0_rwflags;
  769. return INSN_GOOD;
  770. }
  771. static enum kprobe_insn __kprobes
  772. prep_emulate_rdhi16rdlo12rs8rm0_wflags(kprobe_opcode_t insn,
  773. struct arch_specific_insn *asi)
  774. {
  775. if (is_r15(insn, 16) || is_r15(insn, 12))
  776. return INSN_REJECTED; /* RdHi or RdLo is PC */
  777. insn &= 0xfff000f0; /* RdHi = r0, RdLo = r1 */
  778. insn |= 0x00001203; /* Rs = r2, Rm = r3 */
  779. asi->insn[0] = insn;
  780. asi->insn_handler = emulate_rdhi16rdlo12rs8rm0_rwflags;
  781. return INSN_GOOD;
  782. }
  783. /*
  784. * For the instruction masking and comparisons in all the "space_*"
  785. * functions below, Do _not_ rearrange the order of tests unless
  786. * you're very, very sure of what you are doing. For the sake of
  787. * efficiency, the masks for some tests sometimes assume other test
  788. * have been done prior to them so the number of patterns to test
  789. * for an instruction set can be as broad as possible to reduce the
  790. * number of tests needed.
  791. */
  792. static const union decode_item arm_1111_table[] = {
  793. /* Unconditional instructions */
  794. /* memory hint 1111 0100 x001 xxxx xxxx xxxx xxxx xxxx */
  795. /* PLDI (immediate) 1111 0100 x101 xxxx xxxx xxxx xxxx xxxx */
  796. /* PLDW (immediate) 1111 0101 x001 xxxx xxxx xxxx xxxx xxxx */
  797. /* PLD (immediate) 1111 0101 x101 xxxx xxxx xxxx xxxx xxxx */
  798. DECODE_SIMULATE (0xfe300000, 0xf4100000, kprobe_simulate_nop),
  799. /* BLX (immediate) 1111 101x xxxx xxxx xxxx xxxx xxxx xxxx */
  800. DECODE_SIMULATE (0xfe000000, 0xfa000000, simulate_blx1),
  801. /* CPS 1111 0001 0000 xxx0 xxxx xxxx xx0x xxxx */
  802. /* SETEND 1111 0001 0000 0001 xxxx xxxx 0000 xxxx */
  803. /* SRS 1111 100x x1x0 xxxx xxxx xxxx xxxx xxxx */
  804. /* RFE 1111 100x x0x1 xxxx xxxx xxxx xxxx xxxx */
  805. /* Coprocessor instructions... */
  806. /* MCRR2 1111 1100 0100 xxxx xxxx xxxx xxxx xxxx */
  807. /* MRRC2 1111 1100 0101 xxxx xxxx xxxx xxxx xxxx */
  808. /* LDC2 1111 110x xxx1 xxxx xxxx xxxx xxxx xxxx */
  809. /* STC2 1111 110x xxx0 xxxx xxxx xxxx xxxx xxxx */
  810. /* CDP2 1111 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */
  811. /* MCR2 1111 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */
  812. /* MRC2 1111 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */
  813. /* Other unallocated instructions... */
  814. DECODE_END
  815. };
  816. static enum kprobe_insn __kprobes
  817. space_cccc_000x(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  818. {
  819. /* cccc 0001 0xx0 xxxx xxxx xxxx xxxx xxx0 xxxx */
  820. if ((insn & 0x0f900010) == 0x01000000) {
  821. /* MRS cpsr : cccc 0001 0000 xxxx xxxx xxxx 0000 xxxx */
  822. if ((insn & 0x0ff000f0) == 0x01000000) {
  823. if (is_r15(insn, 12))
  824. return INSN_REJECTED; /* Rd is PC */
  825. asi->insn_handler = simulate_mrs;
  826. return INSN_GOOD_NO_SLOT;
  827. }
  828. /* SMLALxy : cccc 0001 0100 xxxx xxxx xxxx 1xx0 xxxx */
  829. if ((insn & 0x0ff00090) == 0x01400080)
  830. return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn,
  831. asi);
  832. /* SMULWy : cccc 0001 0010 xxxx xxxx xxxx 1x10 xxxx */
  833. /* SMULxy : cccc 0001 0110 xxxx xxxx xxxx 1xx0 xxxx */
  834. if ((insn & 0x0ff000b0) == 0x012000a0 ||
  835. (insn & 0x0ff00090) == 0x01600080)
  836. return prep_emulate_rd16rs8rm0_wflags(insn, asi);
  837. /* SMLAxy : cccc 0001 0000 xxxx xxxx xxxx 1xx0 xxxx : Q */
  838. /* SMLAWy : cccc 0001 0010 xxxx xxxx xxxx 1x00 xxxx : Q */
  839. if ((insn & 0x0ff00090) == 0x01000080 ||
  840. (insn & 0x0ff000b0) == 0x01200080)
  841. return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi);
  842. /* BXJ : cccc 0001 0010 xxxx xxxx xxxx 0010 xxxx */
  843. /* MSR : cccc 0001 0x10 xxxx xxxx xxxx 0000 xxxx */
  844. /* MRS spsr : cccc 0001 0100 xxxx xxxx xxxx 0000 xxxx */
  845. /* Other instruction encodings aren't yet defined */
  846. return INSN_REJECTED;
  847. }
  848. /* cccc 0001 0xx0 xxxx xxxx xxxx xxxx 0xx1 xxxx */
  849. else if ((insn & 0x0f900090) == 0x01000010) {
  850. /* BLX(2) : cccc 0001 0010 xxxx xxxx xxxx 0011 xxxx */
  851. /* BX : cccc 0001 0010 xxxx xxxx xxxx 0001 xxxx */
  852. if ((insn & 0x0ff000d0) == 0x01200010) {
  853. if ((insn & 0x0ff000ff) == 0x0120003f)
  854. return INSN_REJECTED; /* BLX pc */
  855. asi->insn_handler = simulate_blx2bx;
  856. return INSN_GOOD_NO_SLOT;
  857. }
  858. /* CLZ : cccc 0001 0110 xxxx xxxx xxxx 0001 xxxx */
  859. if ((insn & 0x0ff000f0) == 0x01600010)
  860. return prep_emulate_rd12rm0(insn, asi);
  861. /* QADD : cccc 0001 0000 xxxx xxxx xxxx 0101 xxxx :Q */
  862. /* QSUB : cccc 0001 0010 xxxx xxxx xxxx 0101 xxxx :Q */
  863. /* QDADD : cccc 0001 0100 xxxx xxxx xxxx 0101 xxxx :Q */
  864. /* QDSUB : cccc 0001 0110 xxxx xxxx xxxx 0101 xxxx :Q */
  865. if ((insn & 0x0f9000f0) == 0x01000050)
  866. return prep_emulate_rd12rn16rm0_wflags(insn, asi);
  867. /* BKPT : 1110 0001 0010 xxxx xxxx xxxx 0111 xxxx */
  868. /* SMC : cccc 0001 0110 xxxx xxxx xxxx 0111 xxxx */
  869. /* Other instruction encodings aren't yet defined */
  870. return INSN_REJECTED;
  871. }
  872. /* cccc 0000 xxxx xxxx xxxx xxxx xxxx 1001 xxxx */
  873. else if ((insn & 0x0f0000f0) == 0x00000090) {
  874. /* MUL : cccc 0000 0000 xxxx xxxx xxxx 1001 xxxx : */
  875. /* MULS : cccc 0000 0001 xxxx xxxx xxxx 1001 xxxx :cc */
  876. /* MLA : cccc 0000 0010 xxxx xxxx xxxx 1001 xxxx : */
  877. /* MLAS : cccc 0000 0011 xxxx xxxx xxxx 1001 xxxx :cc */
  878. /* UMAAL : cccc 0000 0100 xxxx xxxx xxxx 1001 xxxx : */
  879. /* undef : cccc 0000 0101 xxxx xxxx xxxx 1001 xxxx : */
  880. /* MLS : cccc 0000 0110 xxxx xxxx xxxx 1001 xxxx : */
  881. /* undef : cccc 0000 0111 xxxx xxxx xxxx 1001 xxxx : */
  882. /* UMULL : cccc 0000 1000 xxxx xxxx xxxx 1001 xxxx : */
  883. /* UMULLS : cccc 0000 1001 xxxx xxxx xxxx 1001 xxxx :cc */
  884. /* UMLAL : cccc 0000 1010 xxxx xxxx xxxx 1001 xxxx : */
  885. /* UMLALS : cccc 0000 1011 xxxx xxxx xxxx 1001 xxxx :cc */
  886. /* SMULL : cccc 0000 1100 xxxx xxxx xxxx 1001 xxxx : */
  887. /* SMULLS : cccc 0000 1101 xxxx xxxx xxxx 1001 xxxx :cc */
  888. /* SMLAL : cccc 0000 1110 xxxx xxxx xxxx 1001 xxxx : */
  889. /* SMLALS : cccc 0000 1111 xxxx xxxx xxxx 1001 xxxx :cc */
  890. if ((insn & 0x00d00000) == 0x00500000)
  891. return INSN_REJECTED;
  892. else if ((insn & 0x00e00000) == 0x00000000)
  893. return prep_emulate_rd16rs8rm0_wflags(insn, asi);
  894. else if ((insn & 0x00a00000) == 0x00200000)
  895. return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi);
  896. else
  897. return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn,
  898. asi);
  899. }
  900. /* cccc 000x xxxx xxxx xxxx xxxx xxxx 1xx1 xxxx */
  901. else if ((insn & 0x0e000090) == 0x00000090) {
  902. /* SWP : cccc 0001 0000 xxxx xxxx xxxx 1001 xxxx */
  903. /* SWPB : cccc 0001 0100 xxxx xxxx xxxx 1001 xxxx */
  904. /* ??? : cccc 0001 0x01 xxxx xxxx xxxx 1001 xxxx */
  905. /* ??? : cccc 0001 0x10 xxxx xxxx xxxx 1001 xxxx */
  906. /* ??? : cccc 0001 0x11 xxxx xxxx xxxx 1001 xxxx */
  907. /* STREX : cccc 0001 1000 xxxx xxxx xxxx 1001 xxxx */
  908. /* LDREX : cccc 0001 1001 xxxx xxxx xxxx 1001 xxxx */
  909. /* STREXD: cccc 0001 1010 xxxx xxxx xxxx 1001 xxxx */
  910. /* LDREXD: cccc 0001 1011 xxxx xxxx xxxx 1001 xxxx */
  911. /* STREXB: cccc 0001 1100 xxxx xxxx xxxx 1001 xxxx */
  912. /* LDREXB: cccc 0001 1101 xxxx xxxx xxxx 1001 xxxx */
  913. /* STREXH: cccc 0001 1110 xxxx xxxx xxxx 1001 xxxx */
  914. /* LDREXH: cccc 0001 1111 xxxx xxxx xxxx 1001 xxxx */
  915. /* LDRD : cccc 000x xxx0 xxxx xxxx xxxx 1101 xxxx */
  916. /* STRD : cccc 000x xxx0 xxxx xxxx xxxx 1111 xxxx */
  917. /* LDRH : cccc 000x xxx1 xxxx xxxx xxxx 1011 xxxx */
  918. /* STRH : cccc 000x xxx0 xxxx xxxx xxxx 1011 xxxx */
  919. /* LDRSB : cccc 000x xxx1 xxxx xxxx xxxx 1101 xxxx */
  920. /* LDRSH : cccc 000x xxx1 xxxx xxxx xxxx 1111 xxxx */
  921. if ((insn & 0x0f0000f0) == 0x01000090) {
  922. if ((insn & 0x0fb000f0) == 0x01000090) {
  923. /* SWP/SWPB */
  924. return prep_emulate_rd12rn16rm0_wflags(insn,
  925. asi);
  926. } else {
  927. /* STREX/LDREX variants and unallocaed space */
  928. return INSN_REJECTED;
  929. }
  930. } else if ((insn & 0x0e1000d0) == 0x00000d0) {
  931. /* STRD/LDRD */
  932. if ((insn & 0x0000e000) == 0x0000e000)
  933. return INSN_REJECTED; /* Rd is LR or PC */
  934. if (is_writeback(insn) && is_r15(insn, 16))
  935. return INSN_REJECTED; /* Writeback to PC */
  936. insn &= 0xfff00fff;
  937. insn |= 0x00002000; /* Rn = r0, Rd = r2 */
  938. if (!(insn & (1 << 22))) {
  939. /* Register index */
  940. insn &= ~0xf;
  941. insn |= 1; /* Rm = r1 */
  942. }
  943. asi->insn[0] = insn;
  944. asi->insn_handler =
  945. (insn & (1 << 5)) ? emulate_strd : emulate_ldrd;
  946. return INSN_GOOD;
  947. }
  948. /* LDRH/STRH/LDRSB/LDRSH */
  949. if (is_r15(insn, 12))
  950. return INSN_REJECTED; /* Rd is PC */
  951. return prep_emulate_ldr_str(insn, asi);
  952. }
  953. /* cccc 000x xxxx xxxx xxxx xxxx xxxx xxxx xxxx */
  954. /*
  955. * ALU op with S bit and Rd == 15 :
  956. * cccc 000x xxx1 xxxx 1111 xxxx xxxx xxxx
  957. */
  958. if ((insn & 0x0e10f000) == 0x0010f000)
  959. return INSN_REJECTED;
  960. /*
  961. * "mov ip, sp" is the most common kprobe'd instruction by far.
  962. * Check and optimize for it explicitly.
  963. */
  964. if (insn == 0xe1a0c00d) {
  965. asi->insn_handler = simulate_mov_ipsp;
  966. return INSN_GOOD_NO_SLOT;
  967. }
  968. /*
  969. * Data processing: Immediate-shift / Register-shift
  970. * ALU op : cccc 000x xxxx xxxx xxxx xxxx xxxx xxxx
  971. * CPY : cccc 0001 1010 xxxx xxxx 0000 0000 xxxx
  972. * MOV : cccc 0001 101x xxxx xxxx xxxx xxxx xxxx
  973. * *S (bit 20) updates condition codes
  974. * ADC/SBC/RSC reads the C flag
  975. */
  976. insn &= 0xfff00ff0; /* Rn = r0, Rd = r0 */
  977. insn |= 0x00000001; /* Rm = r1 */
  978. if (insn & 0x010) {
  979. insn &= 0xfffff0ff; /* register shift */
  980. insn |= 0x00000200; /* Rs = r2 */
  981. }
  982. asi->insn[0] = insn;
  983. if ((insn & 0x0f900000) == 0x01100000) {
  984. /*
  985. * TST : cccc 0001 0001 xxxx xxxx xxxx xxxx xxxx
  986. * TEQ : cccc 0001 0011 xxxx xxxx xxxx xxxx xxxx
  987. * CMP : cccc 0001 0101 xxxx xxxx xxxx xxxx xxxx
  988. * CMN : cccc 0001 0111 xxxx xxxx xxxx xxxx xxxx
  989. */
  990. asi->insn_handler = emulate_alu_tests;
  991. } else {
  992. /* ALU ops which write to Rd */
  993. asi->insn_handler = (insn & (1 << 20)) ? /* S-bit */
  994. emulate_alu_rwflags : emulate_alu_rflags;
  995. }
  996. return INSN_GOOD;
  997. }
  998. static enum kprobe_insn __kprobes
  999. space_cccc_001x(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1000. {
  1001. /* MOVW : cccc 0011 0000 xxxx xxxx xxxx xxxx xxxx */
  1002. /* MOVT : cccc 0011 0100 xxxx xxxx xxxx xxxx xxxx */
  1003. if ((insn & 0x0fb00000) == 0x03000000)
  1004. return prep_emulate_rd12_modify(insn, asi);
  1005. /* hints : cccc 0011 0010 0000 xxxx xxxx xxxx xxxx */
  1006. if ((insn & 0x0fff0000) == 0x03200000) {
  1007. unsigned op2 = insn & 0x000000ff;
  1008. if (op2 == 0x01 || op2 == 0x04) {
  1009. /* YIELD : cccc 0011 0010 0000 xxxx xxxx 0000 0001 */
  1010. /* SEV : cccc 0011 0010 0000 xxxx xxxx 0000 0100 */
  1011. asi->insn[0] = insn;
  1012. asi->insn_handler = emulate_none;
  1013. return INSN_GOOD;
  1014. } else if (op2 <= 0x03) {
  1015. /* NOP : cccc 0011 0010 0000 xxxx xxxx 0000 0000 */
  1016. /* WFE : cccc 0011 0010 0000 xxxx xxxx 0000 0010 */
  1017. /* WFI : cccc 0011 0010 0000 xxxx xxxx 0000 0011 */
  1018. /*
  1019. * We make WFE and WFI true NOPs to avoid stalls due
  1020. * to missing events whilst processing the probe.
  1021. */
  1022. asi->insn_handler = emulate_nop;
  1023. return INSN_GOOD_NO_SLOT;
  1024. }
  1025. /* For DBG and unallocated hints it's safest to reject them */
  1026. return INSN_REJECTED;
  1027. }
  1028. /*
  1029. * MSR : cccc 0011 0x10 xxxx xxxx xxxx xxxx xxxx
  1030. * ALU op with S bit and Rd == 15 :
  1031. * cccc 001x xxx1 xxxx 1111 xxxx xxxx xxxx
  1032. */
  1033. if ((insn & 0x0fb00000) == 0x03200000 || /* MSR */
  1034. (insn & 0x0e10f000) == 0x0210f000) /* ALU s-bit, R15 */
  1035. return INSN_REJECTED;
  1036. /*
  1037. * Data processing: 32-bit Immediate
  1038. * ALU op : cccc 001x xxxx xxxx xxxx xxxx xxxx xxxx
  1039. * MOV : cccc 0011 101x xxxx xxxx xxxx xxxx xxxx
  1040. * *S (bit 20) updates condition codes
  1041. * ADC/SBC/RSC reads the C flag
  1042. */
  1043. insn &= 0xfff00fff; /* Rn = r0 and Rd = r0 */
  1044. asi->insn[0] = insn;
  1045. if ((insn & 0x0f900000) == 0x03100000) {
  1046. /*
  1047. * TST : cccc 0011 0001 xxxx xxxx xxxx xxxx xxxx
  1048. * TEQ : cccc 0011 0011 xxxx xxxx xxxx xxxx xxxx
  1049. * CMP : cccc 0011 0101 xxxx xxxx xxxx xxxx xxxx
  1050. * CMN : cccc 0011 0111 xxxx xxxx xxxx xxxx xxxx
  1051. */
  1052. asi->insn_handler = emulate_alu_tests_imm;
  1053. } else {
  1054. /* ALU ops which write to Rd */
  1055. asi->insn_handler = (insn & (1 << 20)) ? /* S-bit */
  1056. emulate_alu_imm_rwflags : emulate_alu_imm_rflags;
  1057. }
  1058. return INSN_GOOD;
  1059. }
  1060. static enum kprobe_insn __kprobes
  1061. space_cccc_0110__1(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1062. {
  1063. /* SEL : cccc 0110 1000 xxxx xxxx xxxx 1011 xxxx GE: !!! */
  1064. if ((insn & 0x0ff000f0) == 0x068000b0) {
  1065. if (is_r15(insn, 12))
  1066. return INSN_REJECTED; /* Rd is PC */
  1067. insn &= 0xfff00ff0; /* Rd = r0, Rn = r0 */
  1068. insn |= 0x00000001; /* Rm = r1 */
  1069. asi->insn[0] = insn;
  1070. asi->insn_handler = emulate_sel;
  1071. return INSN_GOOD;
  1072. }
  1073. /* SSAT : cccc 0110 101x xxxx xxxx xxxx xx01 xxxx :Q */
  1074. /* USAT : cccc 0110 111x xxxx xxxx xxxx xx01 xxxx :Q */
  1075. /* SSAT16 : cccc 0110 1010 xxxx xxxx xxxx 0011 xxxx :Q */
  1076. /* USAT16 : cccc 0110 1110 xxxx xxxx xxxx 0011 xxxx :Q */
  1077. if ((insn & 0x0fa00030) == 0x06a00010 ||
  1078. (insn & 0x0fb000f0) == 0x06a00030) {
  1079. if (is_r15(insn, 12))
  1080. return INSN_REJECTED; /* Rd is PC */
  1081. insn &= 0xffff0ff0; /* Rd = r0, Rm = r0 */
  1082. asi->insn[0] = insn;
  1083. asi->insn_handler = emulate_sat;
  1084. return INSN_GOOD;
  1085. }
  1086. /* REV : cccc 0110 1011 xxxx xxxx xxxx 0011 xxxx */
  1087. /* REV16 : cccc 0110 1011 xxxx xxxx xxxx 1011 xxxx */
  1088. /* RBIT : cccc 0110 1111 xxxx xxxx xxxx 0011 xxxx */
  1089. /* REVSH : cccc 0110 1111 xxxx xxxx xxxx 1011 xxxx */
  1090. if ((insn & 0x0ff00070) == 0x06b00030 ||
  1091. (insn & 0x0ff00070) == 0x06f00030)
  1092. return prep_emulate_rd12rm0(insn, asi);
  1093. /* ??? : cccc 0110 0000 xxxx xxxx xxxx xxx1 xxxx : */
  1094. /* SADD16 : cccc 0110 0001 xxxx xxxx xxxx 0001 xxxx :GE */
  1095. /* SADDSUBX : cccc 0110 0001 xxxx xxxx xxxx 0011 xxxx :GE */
  1096. /* SSUBADDX : cccc 0110 0001 xxxx xxxx xxxx 0101 xxxx :GE */
  1097. /* SSUB16 : cccc 0110 0001 xxxx xxxx xxxx 0111 xxxx :GE */
  1098. /* SADD8 : cccc 0110 0001 xxxx xxxx xxxx 1001 xxxx :GE */
  1099. /* ??? : cccc 0110 0001 xxxx xxxx xxxx 1011 xxxx : */
  1100. /* ??? : cccc 0110 0001 xxxx xxxx xxxx 1101 xxxx : */
  1101. /* SSUB8 : cccc 0110 0001 xxxx xxxx xxxx 1111 xxxx :GE */
  1102. /* QADD16 : cccc 0110 0010 xxxx xxxx xxxx 0001 xxxx : */
  1103. /* QADDSUBX : cccc 0110 0010 xxxx xxxx xxxx 0011 xxxx : */
  1104. /* QSUBADDX : cccc 0110 0010 xxxx xxxx xxxx 0101 xxxx : */
  1105. /* QSUB16 : cccc 0110 0010 xxxx xxxx xxxx 0111 xxxx : */
  1106. /* QADD8 : cccc 0110 0010 xxxx xxxx xxxx 1001 xxxx : */
  1107. /* ??? : cccc 0110 0010 xxxx xxxx xxxx 1011 xxxx : */
  1108. /* ??? : cccc 0110 0010 xxxx xxxx xxxx 1101 xxxx : */
  1109. /* QSUB8 : cccc 0110 0010 xxxx xxxx xxxx 1111 xxxx : */
  1110. /* SHADD16 : cccc 0110 0011 xxxx xxxx xxxx 0001 xxxx : */
  1111. /* SHADDSUBX : cccc 0110 0011 xxxx xxxx xxxx 0011 xxxx : */
  1112. /* SHSUBADDX : cccc 0110 0011 xxxx xxxx xxxx 0101 xxxx : */
  1113. /* SHSUB16 : cccc 0110 0011 xxxx xxxx xxxx 0111 xxxx : */
  1114. /* SHADD8 : cccc 0110 0011 xxxx xxxx xxxx 1001 xxxx : */
  1115. /* ??? : cccc 0110 0011 xxxx xxxx xxxx 1011 xxxx : */
  1116. /* ??? : cccc 0110 0011 xxxx xxxx xxxx 1101 xxxx : */
  1117. /* SHSUB8 : cccc 0110 0011 xxxx xxxx xxxx 1111 xxxx : */
  1118. /* ??? : cccc 0110 0100 xxxx xxxx xxxx xxx1 xxxx : */
  1119. /* UADD16 : cccc 0110 0101 xxxx xxxx xxxx 0001 xxxx :GE */
  1120. /* UADDSUBX : cccc 0110 0101 xxxx xxxx xxxx 0011 xxxx :GE */
  1121. /* USUBADDX : cccc 0110 0101 xxxx xxxx xxxx 0101 xxxx :GE */
  1122. /* USUB16 : cccc 0110 0101 xxxx xxxx xxxx 0111 xxxx :GE */
  1123. /* UADD8 : cccc 0110 0101 xxxx xxxx xxxx 1001 xxxx :GE */
  1124. /* ??? : cccc 0110 0101 xxxx xxxx xxxx 1011 xxxx : */
  1125. /* ??? : cccc 0110 0101 xxxx xxxx xxxx 1101 xxxx : */
  1126. /* USUB8 : cccc 0110 0101 xxxx xxxx xxxx 1111 xxxx :GE */
  1127. /* UQADD16 : cccc 0110 0110 xxxx xxxx xxxx 0001 xxxx : */
  1128. /* UQADDSUBX : cccc 0110 0110 xxxx xxxx xxxx 0011 xxxx : */
  1129. /* UQSUBADDX : cccc 0110 0110 xxxx xxxx xxxx 0101 xxxx : */
  1130. /* UQSUB16 : cccc 0110 0110 xxxx xxxx xxxx 0111 xxxx : */
  1131. /* UQADD8 : cccc 0110 0110 xxxx xxxx xxxx 1001 xxxx : */
  1132. /* ??? : cccc 0110 0110 xxxx xxxx xxxx 1011 xxxx : */
  1133. /* ??? : cccc 0110 0110 xxxx xxxx xxxx 1101 xxxx : */
  1134. /* UQSUB8 : cccc 0110 0110 xxxx xxxx xxxx 1111 xxxx : */
  1135. /* UHADD16 : cccc 0110 0111 xxxx xxxx xxxx 0001 xxxx : */
  1136. /* UHADDSUBX : cccc 0110 0111 xxxx xxxx xxxx 0011 xxxx : */
  1137. /* UHSUBADDX : cccc 0110 0111 xxxx xxxx xxxx 0101 xxxx : */
  1138. /* UHSUB16 : cccc 0110 0111 xxxx xxxx xxxx 0111 xxxx : */
  1139. /* UHADD8 : cccc 0110 0111 xxxx xxxx xxxx 1001 xxxx : */
  1140. /* ??? : cccc 0110 0111 xxxx xxxx xxxx 1011 xxxx : */
  1141. /* ??? : cccc 0110 0111 xxxx xxxx xxxx 1101 xxxx : */
  1142. /* UHSUB8 : cccc 0110 0111 xxxx xxxx xxxx 1111 xxxx : */
  1143. if ((insn & 0x0f800010) == 0x06000010) {
  1144. if ((insn & 0x00300000) == 0x00000000 ||
  1145. (insn & 0x000000e0) == 0x000000a0 ||
  1146. (insn & 0x000000e0) == 0x000000c0)
  1147. return INSN_REJECTED; /* Unallocated space */
  1148. return prep_emulate_rd12rn16rm0_wflags(insn, asi);
  1149. }
  1150. /* PKHBT : cccc 0110 1000 xxxx xxxx xxxx x001 xxxx : */
  1151. /* PKHTB : cccc 0110 1000 xxxx xxxx xxxx x101 xxxx : */
  1152. if ((insn & 0x0ff00030) == 0x06800010)
  1153. return prep_emulate_rd12rn16rm0_wflags(insn, asi);
  1154. /* SXTAB16 : cccc 0110 1000 xxxx xxxx xxxx 0111 xxxx : */
  1155. /* SXTB16 : cccc 0110 1000 1111 xxxx xxxx 0111 xxxx : */
  1156. /* ??? : cccc 0110 1001 xxxx xxxx xxxx 0111 xxxx : */
  1157. /* SXTAB : cccc 0110 1010 xxxx xxxx xxxx 0111 xxxx : */
  1158. /* SXTB : cccc 0110 1010 1111 xxxx xxxx 0111 xxxx : */
  1159. /* SXTAH : cccc 0110 1011 xxxx xxxx xxxx 0111 xxxx : */
  1160. /* SXTH : cccc 0110 1011 1111 xxxx xxxx 0111 xxxx : */
  1161. /* UXTAB16 : cccc 0110 1100 xxxx xxxx xxxx 0111 xxxx : */
  1162. /* UXTB16 : cccc 0110 1100 1111 xxxx xxxx 0111 xxxx : */
  1163. /* ??? : cccc 0110 1101 xxxx xxxx xxxx 0111 xxxx : */
  1164. /* UXTAB : cccc 0110 1110 xxxx xxxx xxxx 0111 xxxx : */
  1165. /* UXTB : cccc 0110 1110 1111 xxxx xxxx 0111 xxxx : */
  1166. /* UXTAH : cccc 0110 1111 xxxx xxxx xxxx 0111 xxxx : */
  1167. /* UXTH : cccc 0110 1111 1111 xxxx xxxx 0111 xxxx : */
  1168. if ((insn & 0x0f8000f0) == 0x06800070) {
  1169. if ((insn & 0x00300000) == 0x00100000)
  1170. return INSN_REJECTED; /* Unallocated space */
  1171. if ((insn & 0x000f0000) == 0x000f0000)
  1172. return prep_emulate_rd12rm0(insn, asi);
  1173. else
  1174. return prep_emulate_rd12rn16rm0_wflags(insn, asi);
  1175. }
  1176. /* Other instruction encodings aren't yet defined */
  1177. return INSN_REJECTED;
  1178. }
  1179. static enum kprobe_insn __kprobes
  1180. space_cccc_0111__1(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1181. {
  1182. /* Undef : cccc 0111 1111 xxxx xxxx xxxx 1111 xxxx */
  1183. if ((insn & 0x0ff000f0) == 0x03f000f0)
  1184. return INSN_REJECTED;
  1185. /* SMLALD : cccc 0111 0100 xxxx xxxx xxxx 00x1 xxxx */
  1186. /* SMLSLD : cccc 0111 0100 xxxx xxxx xxxx 01x1 xxxx */
  1187. if ((insn & 0x0ff00090) == 0x07400010)
  1188. return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn, asi);
  1189. /* SMLAD : cccc 0111 0000 xxxx xxxx xxxx 00x1 xxxx :Q */
  1190. /* SMUAD : cccc 0111 0000 xxxx 1111 xxxx 00x1 xxxx :Q */
  1191. /* SMLSD : cccc 0111 0000 xxxx xxxx xxxx 01x1 xxxx :Q */
  1192. /* SMUSD : cccc 0111 0000 xxxx 1111 xxxx 01x1 xxxx : */
  1193. /* SMMLA : cccc 0111 0101 xxxx xxxx xxxx 00x1 xxxx : */
  1194. /* SMMUL : cccc 0111 0101 xxxx 1111 xxxx 00x1 xxxx : */
  1195. /* USADA8 : cccc 0111 1000 xxxx xxxx xxxx 0001 xxxx : */
  1196. /* USAD8 : cccc 0111 1000 xxxx 1111 xxxx 0001 xxxx : */
  1197. if ((insn & 0x0ff00090) == 0x07000010 ||
  1198. (insn & 0x0ff000d0) == 0x07500010 ||
  1199. (insn & 0x0ff000f0) == 0x07800010) {
  1200. if ((insn & 0x0000f000) == 0x0000f000)
  1201. return prep_emulate_rd16rs8rm0_wflags(insn, asi);
  1202. else
  1203. return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi);
  1204. }
  1205. /* SMMLS : cccc 0111 0101 xxxx xxxx xxxx 11x1 xxxx : */
  1206. if ((insn & 0x0ff000d0) == 0x075000d0)
  1207. return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi);
  1208. /* SBFX : cccc 0111 101x xxxx xxxx xxxx x101 xxxx : */
  1209. /* UBFX : cccc 0111 111x xxxx xxxx xxxx x101 xxxx : */
  1210. if ((insn & 0x0fa00070) == 0x07a00050)
  1211. return prep_emulate_rd12rm0(insn, asi);
  1212. /* BFI : cccc 0111 110x xxxx xxxx xxxx x001 xxxx : */
  1213. /* BFC : cccc 0111 110x xxxx xxxx xxxx x001 1111 : */
  1214. if ((insn & 0x0fe00070) == 0x07c00010) {
  1215. if ((insn & 0x0000000f) == 0x0000000f)
  1216. return prep_emulate_rd12_modify(insn, asi);
  1217. else
  1218. return prep_emulate_rd12rn0_modify(insn, asi);
  1219. }
  1220. return INSN_REJECTED;
  1221. }
  1222. static enum kprobe_insn __kprobes
  1223. space_cccc_01xx(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1224. {
  1225. /* LDR : cccc 01xx x0x1 xxxx xxxx xxxx xxxx xxxx */
  1226. /* LDRB : cccc 01xx x1x1 xxxx xxxx xxxx xxxx xxxx */
  1227. /* LDRBT : cccc 01x0 x111 xxxx xxxx xxxx xxxx xxxx */
  1228. /* LDRT : cccc 01x0 x011 xxxx xxxx xxxx xxxx xxxx */
  1229. /* STR : cccc 01xx x0x0 xxxx xxxx xxxx xxxx xxxx */
  1230. /* STRB : cccc 01xx x1x0 xxxx xxxx xxxx xxxx xxxx */
  1231. /* STRBT : cccc 01x0 x110 xxxx xxxx xxxx xxxx xxxx */
  1232. /* STRT : cccc 01x0 x010 xxxx xxxx xxxx xxxx xxxx */
  1233. if ((insn & 0x00500000) == 0x00500000 && is_r15(insn, 12))
  1234. return INSN_REJECTED; /* LDRB into PC */
  1235. return prep_emulate_ldr_str(insn, asi);
  1236. }
  1237. static enum kprobe_insn __kprobes
  1238. space_cccc_100x(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1239. {
  1240. /* LDM(2) : cccc 100x x101 xxxx 0xxx xxxx xxxx xxxx */
  1241. /* LDM(3) : cccc 100x x1x1 xxxx 1xxx xxxx xxxx xxxx */
  1242. if ((insn & 0x0e708000) == 0x85000000 ||
  1243. (insn & 0x0e508000) == 0x85010000)
  1244. return INSN_REJECTED;
  1245. /* LDM(1) : cccc 100x x0x1 xxxx xxxx xxxx xxxx xxxx */
  1246. /* STM(1) : cccc 100x x0x0 xxxx xxxx xxxx xxxx xxxx */
  1247. /*
  1248. * Make the instruction unconditional because the new emulation
  1249. * functions don't bother to setup the PSR context.
  1250. */
  1251. insn = (insn | 0xe0000000) & ~0x10000000;
  1252. return kprobe_decode_ldmstm(insn, asi);
  1253. }
  1254. static enum kprobe_insn __kprobes
  1255. space_cccc_101x(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1256. {
  1257. /* B : cccc 1010 xxxx xxxx xxxx xxxx xxxx xxxx */
  1258. /* BL : cccc 1011 xxxx xxxx xxxx xxxx xxxx xxxx */
  1259. asi->insn_handler = simulate_bbl;
  1260. return INSN_GOOD_NO_SLOT;
  1261. }
  1262. static enum kprobe_insn __kprobes
  1263. space_cccc_11xx(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1264. {
  1265. /* Coprocessor instructions... */
  1266. /* MCRR : cccc 1100 0100 xxxx xxxx xxxx xxxx xxxx : (Rd!=Rn) */
  1267. /* MRRC : cccc 1100 0101 xxxx xxxx xxxx xxxx xxxx : (Rd!=Rn) */
  1268. /* LDC : cccc 110x xxx1 xxxx xxxx xxxx xxxx xxxx */
  1269. /* STC : cccc 110x xxx0 xxxx xxxx xxxx xxxx xxxx */
  1270. /* CDP : cccc 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */
  1271. /* MCR : cccc 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */
  1272. /* MRC : cccc 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */
  1273. /* SVC : cccc 1111 xxxx xxxx xxxx xxxx xxxx xxxx */
  1274. return INSN_REJECTED;
  1275. }
  1276. static void __kprobes arm_singlestep(struct kprobe *p, struct pt_regs *regs)
  1277. {
  1278. regs->ARM_pc += 4;
  1279. p->ainsn.insn_handler(p, regs);
  1280. }
  1281. /* Return:
  1282. * INSN_REJECTED If instruction is one not allowed to kprobe,
  1283. * INSN_GOOD If instruction is supported and uses instruction slot,
  1284. * INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
  1285. *
  1286. * For instructions we don't want to kprobe (INSN_REJECTED return result):
  1287. * These are generally ones that modify the processor state making
  1288. * them "hard" to simulate such as switches processor modes or
  1289. * make accesses in alternate modes. Any of these could be simulated
  1290. * if the work was put into it, but low return considering they
  1291. * should also be very rare.
  1292. */
  1293. enum kprobe_insn __kprobes
  1294. arm_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  1295. {
  1296. asi->insn_singlestep = arm_singlestep;
  1297. asi->insn_check_cc = kprobe_condition_checks[insn>>28];
  1298. asi->insn[1] = KPROBE_RETURN_INSTRUCTION;
  1299. if ((insn & 0xf0000000) == 0xf0000000)
  1300. return kprobe_decode_insn(insn, asi, arm_1111_table, false);
  1301. else if ((insn & 0x0e000000) == 0x00000000)
  1302. return space_cccc_000x(insn, asi);
  1303. else if ((insn & 0x0e000000) == 0x02000000)
  1304. return space_cccc_001x(insn, asi);
  1305. else if ((insn & 0x0f000010) == 0x06000010)
  1306. return space_cccc_0110__1(insn, asi);
  1307. else if ((insn & 0x0f000010) == 0x07000010)
  1308. return space_cccc_0111__1(insn, asi);
  1309. else if ((insn & 0x0c000000) == 0x04000000)
  1310. return space_cccc_01xx(insn, asi);
  1311. else if ((insn & 0x0e000000) == 0x08000000)
  1312. return space_cccc_100x(insn, asi);
  1313. else if ((insn & 0x0e000000) == 0x0a000000)
  1314. return space_cccc_101x(insn, asi);
  1315. return space_cccc_11xx(insn, asi);
  1316. }