kprobes-arm.c 51 KB

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