test_emulate_step.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Simple sanity tests for instruction emulation infrastructure.
  4. *
  5. * Copyright IBM Corp. 2016
  6. */
  7. #define pr_fmt(fmt) "emulate_step_test: " fmt
  8. #include <linux/ptrace.h>
  9. #include <asm/cpu_has_feature.h>
  10. #include <asm/sstep.h>
  11. #include <asm/ppc-opcode.h>
  12. #include <asm/code-patching.h>
  13. #include <asm/inst.h>
  14. #define MAX_SUBTESTS 16
  15. #define IGNORE_GPR(n) (0x1UL << (n))
  16. #define IGNORE_XER (0x1UL << 32)
  17. #define IGNORE_CCR (0x1UL << 33)
  18. #define NEGATIVE_TEST (0x1UL << 63)
  19. #define TEST_PLD(r, base, i, pr) \
  20. ppc_inst_prefix(PPC_PREFIX_8LS | __PPC_PRFX_R(pr) | IMM_H(i), \
  21. PPC_INST_PLD | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  22. #define TEST_PLWZ(r, base, i, pr) \
  23. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  24. PPC_RAW_LWZ(r, base, i))
  25. #define TEST_PSTD(r, base, i, pr) \
  26. ppc_inst_prefix(PPC_PREFIX_8LS | __PPC_PRFX_R(pr) | IMM_H(i), \
  27. PPC_INST_PSTD | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  28. #define TEST_PLFS(r, base, i, pr) \
  29. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  30. PPC_INST_LFS | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  31. #define TEST_PSTFS(r, base, i, pr) \
  32. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  33. PPC_INST_STFS | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  34. #define TEST_PLFD(r, base, i, pr) \
  35. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  36. PPC_INST_LFD | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  37. #define TEST_PSTFD(r, base, i, pr) \
  38. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  39. PPC_INST_STFD | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
  40. #define TEST_PADDI(t, a, i, pr) \
  41. ppc_inst_prefix(PPC_PREFIX_MLS | __PPC_PRFX_R(pr) | IMM_H(i), \
  42. PPC_RAW_ADDI(t, a, i))
  43. static void __init init_pt_regs(struct pt_regs *regs)
  44. {
  45. static unsigned long msr;
  46. static bool msr_cached;
  47. memset(regs, 0, sizeof(struct pt_regs));
  48. if (likely(msr_cached)) {
  49. regs->msr = msr;
  50. return;
  51. }
  52. asm volatile("mfmsr %0" : "=r"(regs->msr));
  53. regs->msr |= MSR_FP;
  54. regs->msr |= MSR_VEC;
  55. regs->msr |= MSR_VSX;
  56. msr = regs->msr;
  57. msr_cached = true;
  58. }
  59. static void __init show_result(char *mnemonic, char *result)
  60. {
  61. pr_info("%-14s : %s\n", mnemonic, result);
  62. }
  63. static void __init show_result_with_descr(char *mnemonic, char *descr,
  64. char *result)
  65. {
  66. pr_info("%-14s : %-50s %s\n", mnemonic, descr, result);
  67. }
  68. static void __init test_ld(void)
  69. {
  70. struct pt_regs regs;
  71. unsigned long a = 0x23;
  72. int stepped = -1;
  73. init_pt_regs(&regs);
  74. regs.gpr[3] = (unsigned long) &a;
  75. /* ld r5, 0(r3) */
  76. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LD(5, 3, 0)));
  77. if (stepped == 1 && regs.gpr[5] == a)
  78. show_result("ld", "PASS");
  79. else
  80. show_result("ld", "FAIL");
  81. }
  82. static void __init test_pld(void)
  83. {
  84. struct pt_regs regs;
  85. unsigned long a = 0x23;
  86. int stepped = -1;
  87. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  88. show_result("pld", "SKIP (!CPU_FTR_ARCH_31)");
  89. return;
  90. }
  91. init_pt_regs(&regs);
  92. regs.gpr[3] = (unsigned long)&a;
  93. /* pld r5, 0(r3), 0 */
  94. stepped = emulate_step(&regs, TEST_PLD(5, 3, 0, 0));
  95. if (stepped == 1 && regs.gpr[5] == a)
  96. show_result("pld", "PASS");
  97. else
  98. show_result("pld", "FAIL");
  99. }
  100. static void __init test_lwz(void)
  101. {
  102. struct pt_regs regs;
  103. unsigned int a = 0x4545;
  104. int stepped = -1;
  105. init_pt_regs(&regs);
  106. regs.gpr[3] = (unsigned long) &a;
  107. /* lwz r5, 0(r3) */
  108. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LWZ(5, 3, 0)));
  109. if (stepped == 1 && regs.gpr[5] == a)
  110. show_result("lwz", "PASS");
  111. else
  112. show_result("lwz", "FAIL");
  113. }
  114. static void __init test_plwz(void)
  115. {
  116. struct pt_regs regs;
  117. unsigned int a = 0x4545;
  118. int stepped = -1;
  119. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  120. show_result("plwz", "SKIP (!CPU_FTR_ARCH_31)");
  121. return;
  122. }
  123. init_pt_regs(&regs);
  124. regs.gpr[3] = (unsigned long)&a;
  125. /* plwz r5, 0(r3), 0 */
  126. stepped = emulate_step(&regs, TEST_PLWZ(5, 3, 0, 0));
  127. if (stepped == 1 && regs.gpr[5] == a)
  128. show_result("plwz", "PASS");
  129. else
  130. show_result("plwz", "FAIL");
  131. }
  132. static void __init test_lwzx(void)
  133. {
  134. struct pt_regs regs;
  135. unsigned int a[3] = {0x0, 0x0, 0x1234};
  136. int stepped = -1;
  137. init_pt_regs(&regs);
  138. regs.gpr[3] = (unsigned long) a;
  139. regs.gpr[4] = 8;
  140. regs.gpr[5] = 0x8765;
  141. /* lwzx r5, r3, r4 */
  142. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LWZX(5, 3, 4)));
  143. if (stepped == 1 && regs.gpr[5] == a[2])
  144. show_result("lwzx", "PASS");
  145. else
  146. show_result("lwzx", "FAIL");
  147. }
  148. static void __init test_std(void)
  149. {
  150. struct pt_regs regs;
  151. unsigned long a = 0x1234;
  152. int stepped = -1;
  153. init_pt_regs(&regs);
  154. regs.gpr[3] = (unsigned long) &a;
  155. regs.gpr[5] = 0x5678;
  156. /* std r5, 0(r3) */
  157. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STD(5, 3, 0)));
  158. if (stepped == 1 && regs.gpr[5] == a)
  159. show_result("std", "PASS");
  160. else
  161. show_result("std", "FAIL");
  162. }
  163. static void __init test_pstd(void)
  164. {
  165. struct pt_regs regs;
  166. unsigned long a = 0x1234;
  167. int stepped = -1;
  168. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  169. show_result("pstd", "SKIP (!CPU_FTR_ARCH_31)");
  170. return;
  171. }
  172. init_pt_regs(&regs);
  173. regs.gpr[3] = (unsigned long)&a;
  174. regs.gpr[5] = 0x5678;
  175. /* pstd r5, 0(r3), 0 */
  176. stepped = emulate_step(&regs, TEST_PSTD(5, 3, 0, 0));
  177. if (stepped == 1 || regs.gpr[5] == a)
  178. show_result("pstd", "PASS");
  179. else
  180. show_result("pstd", "FAIL");
  181. }
  182. static void __init test_ldarx_stdcx(void)
  183. {
  184. struct pt_regs regs;
  185. unsigned long a = 0x1234;
  186. int stepped = -1;
  187. unsigned long cr0_eq = 0x1 << 29; /* eq bit of CR0 */
  188. init_pt_regs(&regs);
  189. asm volatile("mfcr %0" : "=r"(regs.ccr));
  190. /*** ldarx ***/
  191. regs.gpr[3] = (unsigned long) &a;
  192. regs.gpr[4] = 0;
  193. regs.gpr[5] = 0x5678;
  194. /* ldarx r5, r3, r4, 0 */
  195. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LDARX(5, 3, 4, 0)));
  196. /*
  197. * Don't touch 'a' here. Touching 'a' can do Load/store
  198. * of 'a' which result in failure of subsequent stdcx.
  199. * Instead, use hardcoded value for comparison.
  200. */
  201. if (stepped <= 0 || regs.gpr[5] != 0x1234) {
  202. show_result("ldarx / stdcx.", "FAIL (ldarx)");
  203. return;
  204. }
  205. /*** stdcx. ***/
  206. regs.gpr[5] = 0x9ABC;
  207. /* stdcx. r5, r3, r4 */
  208. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STDCX(5, 3, 4)));
  209. /*
  210. * Two possible scenarios that indicates successful emulation
  211. * of stdcx. :
  212. * 1. Reservation is active and store is performed. In this
  213. * case cr0.eq bit will be set to 1.
  214. * 2. Reservation is not active and store is not performed.
  215. * In this case cr0.eq bit will be set to 0.
  216. */
  217. if (stepped == 1 && ((regs.gpr[5] == a && (regs.ccr & cr0_eq))
  218. || (regs.gpr[5] != a && !(regs.ccr & cr0_eq))))
  219. show_result("ldarx / stdcx.", "PASS");
  220. else
  221. show_result("ldarx / stdcx.", "FAIL (stdcx.)");
  222. }
  223. #ifdef CONFIG_PPC_FPU
  224. static void __init test_lfsx_stfsx(void)
  225. {
  226. struct pt_regs regs;
  227. union {
  228. float a;
  229. int b;
  230. } c;
  231. int cached_b;
  232. int stepped = -1;
  233. init_pt_regs(&regs);
  234. /*** lfsx ***/
  235. c.a = 123.45;
  236. cached_b = c.b;
  237. regs.gpr[3] = (unsigned long) &c.a;
  238. regs.gpr[4] = 0;
  239. /* lfsx frt10, r3, r4 */
  240. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LFSX(10, 3, 4)));
  241. if (stepped == 1)
  242. show_result("lfsx", "PASS");
  243. else
  244. show_result("lfsx", "FAIL");
  245. /*** stfsx ***/
  246. c.a = 678.91;
  247. /* stfsx frs10, r3, r4 */
  248. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STFSX(10, 3, 4)));
  249. if (stepped == 1 && c.b == cached_b)
  250. show_result("stfsx", "PASS");
  251. else
  252. show_result("stfsx", "FAIL");
  253. }
  254. static void __init test_plfs_pstfs(void)
  255. {
  256. struct pt_regs regs;
  257. union {
  258. float a;
  259. int b;
  260. } c;
  261. int cached_b;
  262. int stepped = -1;
  263. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  264. show_result("pld", "SKIP (!CPU_FTR_ARCH_31)");
  265. return;
  266. }
  267. init_pt_regs(&regs);
  268. /*** plfs ***/
  269. c.a = 123.45;
  270. cached_b = c.b;
  271. regs.gpr[3] = (unsigned long)&c.a;
  272. /* plfs frt10, 0(r3), 0 */
  273. stepped = emulate_step(&regs, TEST_PLFS(10, 3, 0, 0));
  274. if (stepped == 1)
  275. show_result("plfs", "PASS");
  276. else
  277. show_result("plfs", "FAIL");
  278. /*** pstfs ***/
  279. c.a = 678.91;
  280. /* pstfs frs10, 0(r3), 0 */
  281. stepped = emulate_step(&regs, TEST_PSTFS(10, 3, 0, 0));
  282. if (stepped == 1 && c.b == cached_b)
  283. show_result("pstfs", "PASS");
  284. else
  285. show_result("pstfs", "FAIL");
  286. }
  287. static void __init test_lfdx_stfdx(void)
  288. {
  289. struct pt_regs regs;
  290. union {
  291. double a;
  292. long b;
  293. } c;
  294. long cached_b;
  295. int stepped = -1;
  296. init_pt_regs(&regs);
  297. /*** lfdx ***/
  298. c.a = 123456.78;
  299. cached_b = c.b;
  300. regs.gpr[3] = (unsigned long) &c.a;
  301. regs.gpr[4] = 0;
  302. /* lfdx frt10, r3, r4 */
  303. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LFDX(10, 3, 4)));
  304. if (stepped == 1)
  305. show_result("lfdx", "PASS");
  306. else
  307. show_result("lfdx", "FAIL");
  308. /*** stfdx ***/
  309. c.a = 987654.32;
  310. /* stfdx frs10, r3, r4 */
  311. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STFDX(10, 3, 4)));
  312. if (stepped == 1 && c.b == cached_b)
  313. show_result("stfdx", "PASS");
  314. else
  315. show_result("stfdx", "FAIL");
  316. }
  317. static void __init test_plfd_pstfd(void)
  318. {
  319. struct pt_regs regs;
  320. union {
  321. double a;
  322. long b;
  323. } c;
  324. long cached_b;
  325. int stepped = -1;
  326. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  327. show_result("pld", "SKIP (!CPU_FTR_ARCH_31)");
  328. return;
  329. }
  330. init_pt_regs(&regs);
  331. /*** plfd ***/
  332. c.a = 123456.78;
  333. cached_b = c.b;
  334. regs.gpr[3] = (unsigned long)&c.a;
  335. /* plfd frt10, 0(r3), 0 */
  336. stepped = emulate_step(&regs, TEST_PLFD(10, 3, 0, 0));
  337. if (stepped == 1)
  338. show_result("plfd", "PASS");
  339. else
  340. show_result("plfd", "FAIL");
  341. /*** pstfd ***/
  342. c.a = 987654.32;
  343. /* pstfd frs10, 0(r3), 0 */
  344. stepped = emulate_step(&regs, TEST_PSTFD(10, 3, 0, 0));
  345. if (stepped == 1 && c.b == cached_b)
  346. show_result("pstfd", "PASS");
  347. else
  348. show_result("pstfd", "FAIL");
  349. }
  350. #else
  351. static void __init test_lfsx_stfsx(void)
  352. {
  353. show_result("lfsx", "SKIP (CONFIG_PPC_FPU is not set)");
  354. show_result("stfsx", "SKIP (CONFIG_PPC_FPU is not set)");
  355. }
  356. static void __init test_plfs_pstfs(void)
  357. {
  358. show_result("plfs", "SKIP (CONFIG_PPC_FPU is not set)");
  359. show_result("pstfs", "SKIP (CONFIG_PPC_FPU is not set)");
  360. }
  361. static void __init test_lfdx_stfdx(void)
  362. {
  363. show_result("lfdx", "SKIP (CONFIG_PPC_FPU is not set)");
  364. show_result("stfdx", "SKIP (CONFIG_PPC_FPU is not set)");
  365. }
  366. static void __init test_plfd_pstfd(void)
  367. {
  368. show_result("plfd", "SKIP (CONFIG_PPC_FPU is not set)");
  369. show_result("pstfd", "SKIP (CONFIG_PPC_FPU is not set)");
  370. }
  371. #endif /* CONFIG_PPC_FPU */
  372. #ifdef CONFIG_ALTIVEC
  373. static void __init test_lvx_stvx(void)
  374. {
  375. struct pt_regs regs;
  376. union {
  377. vector128 a;
  378. u32 b[4];
  379. } c;
  380. u32 cached_b[4];
  381. int stepped = -1;
  382. init_pt_regs(&regs);
  383. /*** lvx ***/
  384. cached_b[0] = c.b[0] = 923745;
  385. cached_b[1] = c.b[1] = 2139478;
  386. cached_b[2] = c.b[2] = 9012;
  387. cached_b[3] = c.b[3] = 982134;
  388. regs.gpr[3] = (unsigned long) &c.a;
  389. regs.gpr[4] = 0;
  390. /* lvx vrt10, r3, r4 */
  391. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LVX(10, 3, 4)));
  392. if (stepped == 1)
  393. show_result("lvx", "PASS");
  394. else
  395. show_result("lvx", "FAIL");
  396. /*** stvx ***/
  397. c.b[0] = 4987513;
  398. c.b[1] = 84313948;
  399. c.b[2] = 71;
  400. c.b[3] = 498532;
  401. /* stvx vrs10, r3, r4 */
  402. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STVX(10, 3, 4)));
  403. if (stepped == 1 && cached_b[0] == c.b[0] && cached_b[1] == c.b[1] &&
  404. cached_b[2] == c.b[2] && cached_b[3] == c.b[3])
  405. show_result("stvx", "PASS");
  406. else
  407. show_result("stvx", "FAIL");
  408. }
  409. #else
  410. static void __init test_lvx_stvx(void)
  411. {
  412. show_result("lvx", "SKIP (CONFIG_ALTIVEC is not set)");
  413. show_result("stvx", "SKIP (CONFIG_ALTIVEC is not set)");
  414. }
  415. #endif /* CONFIG_ALTIVEC */
  416. #ifdef CONFIG_VSX
  417. static void __init test_lxvd2x_stxvd2x(void)
  418. {
  419. struct pt_regs regs;
  420. union {
  421. vector128 a;
  422. u32 b[4];
  423. } c;
  424. u32 cached_b[4];
  425. int stepped = -1;
  426. init_pt_regs(&regs);
  427. /*** lxvd2x ***/
  428. cached_b[0] = c.b[0] = 18233;
  429. cached_b[1] = c.b[1] = 34863571;
  430. cached_b[2] = c.b[2] = 834;
  431. cached_b[3] = c.b[3] = 6138911;
  432. regs.gpr[3] = (unsigned long) &c.a;
  433. regs.gpr[4] = 0;
  434. /* lxvd2x vsr39, r3, r4 */
  435. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LXVD2X(39, R3, R4)));
  436. if (stepped == 1 && cpu_has_feature(CPU_FTR_VSX)) {
  437. show_result("lxvd2x", "PASS");
  438. } else {
  439. if (!cpu_has_feature(CPU_FTR_VSX))
  440. show_result("lxvd2x", "PASS (!CPU_FTR_VSX)");
  441. else
  442. show_result("lxvd2x", "FAIL");
  443. }
  444. /*** stxvd2x ***/
  445. c.b[0] = 21379463;
  446. c.b[1] = 87;
  447. c.b[2] = 374234;
  448. c.b[3] = 4;
  449. /* stxvd2x vsr39, r3, r4 */
  450. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STXVD2X(39, R3, R4)));
  451. if (stepped == 1 && cached_b[0] == c.b[0] && cached_b[1] == c.b[1] &&
  452. cached_b[2] == c.b[2] && cached_b[3] == c.b[3] &&
  453. cpu_has_feature(CPU_FTR_VSX)) {
  454. show_result("stxvd2x", "PASS");
  455. } else {
  456. if (!cpu_has_feature(CPU_FTR_VSX))
  457. show_result("stxvd2x", "PASS (!CPU_FTR_VSX)");
  458. else
  459. show_result("stxvd2x", "FAIL");
  460. }
  461. }
  462. #else
  463. static void __init test_lxvd2x_stxvd2x(void)
  464. {
  465. show_result("lxvd2x", "SKIP (CONFIG_VSX is not set)");
  466. show_result("stxvd2x", "SKIP (CONFIG_VSX is not set)");
  467. }
  468. #endif /* CONFIG_VSX */
  469. #ifdef CONFIG_VSX
  470. static void __init test_lxvp_stxvp(void)
  471. {
  472. struct pt_regs regs;
  473. union {
  474. vector128 a;
  475. u32 b[4];
  476. } c[2];
  477. u32 cached_b[8];
  478. int stepped = -1;
  479. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  480. show_result("lxvp", "SKIP (!CPU_FTR_ARCH_31)");
  481. show_result("stxvp", "SKIP (!CPU_FTR_ARCH_31)");
  482. return;
  483. }
  484. init_pt_regs(&regs);
  485. /*** lxvp ***/
  486. cached_b[0] = c[0].b[0] = 18233;
  487. cached_b[1] = c[0].b[1] = 34863571;
  488. cached_b[2] = c[0].b[2] = 834;
  489. cached_b[3] = c[0].b[3] = 6138911;
  490. cached_b[4] = c[1].b[0] = 1234;
  491. cached_b[5] = c[1].b[1] = 5678;
  492. cached_b[6] = c[1].b[2] = 91011;
  493. cached_b[7] = c[1].b[3] = 121314;
  494. regs.gpr[4] = (unsigned long)&c[0].a;
  495. /*
  496. * lxvp XTp,DQ(RA)
  497. * XTp = 32xTX + 2xTp
  498. * let TX=1 Tp=1 RA=4 DQ=0
  499. */
  500. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LXVP(34, 4, 0)));
  501. if (stepped == 1 && cpu_has_feature(CPU_FTR_VSX)) {
  502. show_result("lxvp", "PASS");
  503. } else {
  504. if (!cpu_has_feature(CPU_FTR_VSX))
  505. show_result("lxvp", "PASS (!CPU_FTR_VSX)");
  506. else
  507. show_result("lxvp", "FAIL");
  508. }
  509. /*** stxvp ***/
  510. c[0].b[0] = 21379463;
  511. c[0].b[1] = 87;
  512. c[0].b[2] = 374234;
  513. c[0].b[3] = 4;
  514. c[1].b[0] = 90;
  515. c[1].b[1] = 122;
  516. c[1].b[2] = 555;
  517. c[1].b[3] = 32144;
  518. /*
  519. * stxvp XSp,DQ(RA)
  520. * XSp = 32xSX + 2xSp
  521. * let SX=1 Sp=1 RA=4 DQ=0
  522. */
  523. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STXVP(34, 4, 0)));
  524. if (stepped == 1 && cached_b[0] == c[0].b[0] && cached_b[1] == c[0].b[1] &&
  525. cached_b[2] == c[0].b[2] && cached_b[3] == c[0].b[3] &&
  526. cached_b[4] == c[1].b[0] && cached_b[5] == c[1].b[1] &&
  527. cached_b[6] == c[1].b[2] && cached_b[7] == c[1].b[3] &&
  528. cpu_has_feature(CPU_FTR_VSX)) {
  529. show_result("stxvp", "PASS");
  530. } else {
  531. if (!cpu_has_feature(CPU_FTR_VSX))
  532. show_result("stxvp", "PASS (!CPU_FTR_VSX)");
  533. else
  534. show_result("stxvp", "FAIL");
  535. }
  536. }
  537. #else
  538. static void __init test_lxvp_stxvp(void)
  539. {
  540. show_result("lxvp", "SKIP (CONFIG_VSX is not set)");
  541. show_result("stxvp", "SKIP (CONFIG_VSX is not set)");
  542. }
  543. #endif /* CONFIG_VSX */
  544. #ifdef CONFIG_VSX
  545. static void __init test_lxvpx_stxvpx(void)
  546. {
  547. struct pt_regs regs;
  548. union {
  549. vector128 a;
  550. u32 b[4];
  551. } c[2];
  552. u32 cached_b[8];
  553. int stepped = -1;
  554. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  555. show_result("lxvpx", "SKIP (!CPU_FTR_ARCH_31)");
  556. show_result("stxvpx", "SKIP (!CPU_FTR_ARCH_31)");
  557. return;
  558. }
  559. init_pt_regs(&regs);
  560. /*** lxvpx ***/
  561. cached_b[0] = c[0].b[0] = 18233;
  562. cached_b[1] = c[0].b[1] = 34863571;
  563. cached_b[2] = c[0].b[2] = 834;
  564. cached_b[3] = c[0].b[3] = 6138911;
  565. cached_b[4] = c[1].b[0] = 1234;
  566. cached_b[5] = c[1].b[1] = 5678;
  567. cached_b[6] = c[1].b[2] = 91011;
  568. cached_b[7] = c[1].b[3] = 121314;
  569. regs.gpr[3] = (unsigned long)&c[0].a;
  570. regs.gpr[4] = 0;
  571. /*
  572. * lxvpx XTp,RA,RB
  573. * XTp = 32xTX + 2xTp
  574. * let TX=1 Tp=1 RA=3 RB=4
  575. */
  576. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_LXVPX(34, 3, 4)));
  577. if (stepped == 1 && cpu_has_feature(CPU_FTR_VSX)) {
  578. show_result("lxvpx", "PASS");
  579. } else {
  580. if (!cpu_has_feature(CPU_FTR_VSX))
  581. show_result("lxvpx", "PASS (!CPU_FTR_VSX)");
  582. else
  583. show_result("lxvpx", "FAIL");
  584. }
  585. /*** stxvpx ***/
  586. c[0].b[0] = 21379463;
  587. c[0].b[1] = 87;
  588. c[0].b[2] = 374234;
  589. c[0].b[3] = 4;
  590. c[1].b[0] = 90;
  591. c[1].b[1] = 122;
  592. c[1].b[2] = 555;
  593. c[1].b[3] = 32144;
  594. /*
  595. * stxvpx XSp,RA,RB
  596. * XSp = 32xSX + 2xSp
  597. * let SX=1 Sp=1 RA=3 RB=4
  598. */
  599. stepped = emulate_step(&regs, ppc_inst(PPC_RAW_STXVPX(34, 3, 4)));
  600. if (stepped == 1 && cached_b[0] == c[0].b[0] && cached_b[1] == c[0].b[1] &&
  601. cached_b[2] == c[0].b[2] && cached_b[3] == c[0].b[3] &&
  602. cached_b[4] == c[1].b[0] && cached_b[5] == c[1].b[1] &&
  603. cached_b[6] == c[1].b[2] && cached_b[7] == c[1].b[3] &&
  604. cpu_has_feature(CPU_FTR_VSX)) {
  605. show_result("stxvpx", "PASS");
  606. } else {
  607. if (!cpu_has_feature(CPU_FTR_VSX))
  608. show_result("stxvpx", "PASS (!CPU_FTR_VSX)");
  609. else
  610. show_result("stxvpx", "FAIL");
  611. }
  612. }
  613. #else
  614. static void __init test_lxvpx_stxvpx(void)
  615. {
  616. show_result("lxvpx", "SKIP (CONFIG_VSX is not set)");
  617. show_result("stxvpx", "SKIP (CONFIG_VSX is not set)");
  618. }
  619. #endif /* CONFIG_VSX */
  620. #ifdef CONFIG_VSX
  621. static void __init test_plxvp_pstxvp(void)
  622. {
  623. ppc_inst_t instr;
  624. struct pt_regs regs;
  625. union {
  626. vector128 a;
  627. u32 b[4];
  628. } c[2];
  629. u32 cached_b[8];
  630. int stepped = -1;
  631. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  632. show_result("plxvp", "SKIP (!CPU_FTR_ARCH_31)");
  633. show_result("pstxvp", "SKIP (!CPU_FTR_ARCH_31)");
  634. return;
  635. }
  636. /*** plxvp ***/
  637. cached_b[0] = c[0].b[0] = 18233;
  638. cached_b[1] = c[0].b[1] = 34863571;
  639. cached_b[2] = c[0].b[2] = 834;
  640. cached_b[3] = c[0].b[3] = 6138911;
  641. cached_b[4] = c[1].b[0] = 1234;
  642. cached_b[5] = c[1].b[1] = 5678;
  643. cached_b[6] = c[1].b[2] = 91011;
  644. cached_b[7] = c[1].b[3] = 121314;
  645. init_pt_regs(&regs);
  646. regs.gpr[3] = (unsigned long)&c[0].a;
  647. /*
  648. * plxvp XTp,D(RA),R
  649. * XTp = 32xTX + 2xTp
  650. * let RA=3 R=0 D=d0||d1=0 R=0 Tp=1 TX=1
  651. */
  652. instr = ppc_inst_prefix(PPC_RAW_PLXVP_P(34, 0, 3, 0), PPC_RAW_PLXVP_S(34, 0, 3, 0));
  653. stepped = emulate_step(&regs, instr);
  654. if (stepped == 1 && cpu_has_feature(CPU_FTR_VSX)) {
  655. show_result("plxvp", "PASS");
  656. } else {
  657. if (!cpu_has_feature(CPU_FTR_VSX))
  658. show_result("plxvp", "PASS (!CPU_FTR_VSX)");
  659. else
  660. show_result("plxvp", "FAIL");
  661. }
  662. /*** pstxvp ***/
  663. c[0].b[0] = 21379463;
  664. c[0].b[1] = 87;
  665. c[0].b[2] = 374234;
  666. c[0].b[3] = 4;
  667. c[1].b[0] = 90;
  668. c[1].b[1] = 122;
  669. c[1].b[2] = 555;
  670. c[1].b[3] = 32144;
  671. /*
  672. * pstxvp XSp,D(RA),R
  673. * XSp = 32xSX + 2xSp
  674. * let RA=3 D=d0||d1=0 R=0 Sp=1 SX=1
  675. */
  676. instr = ppc_inst_prefix(PPC_RAW_PSTXVP_P(34, 0, 3, 0), PPC_RAW_PSTXVP_S(34, 0, 3, 0));
  677. stepped = emulate_step(&regs, instr);
  678. if (stepped == 1 && cached_b[0] == c[0].b[0] && cached_b[1] == c[0].b[1] &&
  679. cached_b[2] == c[0].b[2] && cached_b[3] == c[0].b[3] &&
  680. cached_b[4] == c[1].b[0] && cached_b[5] == c[1].b[1] &&
  681. cached_b[6] == c[1].b[2] && cached_b[7] == c[1].b[3] &&
  682. cpu_has_feature(CPU_FTR_VSX)) {
  683. show_result("pstxvp", "PASS");
  684. } else {
  685. if (!cpu_has_feature(CPU_FTR_VSX))
  686. show_result("pstxvp", "PASS (!CPU_FTR_VSX)");
  687. else
  688. show_result("pstxvp", "FAIL");
  689. }
  690. }
  691. #else
  692. static void __init test_plxvp_pstxvp(void)
  693. {
  694. show_result("plxvp", "SKIP (CONFIG_VSX is not set)");
  695. show_result("pstxvp", "SKIP (CONFIG_VSX is not set)");
  696. }
  697. #endif /* CONFIG_VSX */
  698. static void __init run_tests_load_store(void)
  699. {
  700. test_ld();
  701. test_pld();
  702. test_lwz();
  703. test_plwz();
  704. test_lwzx();
  705. test_std();
  706. test_pstd();
  707. test_ldarx_stdcx();
  708. test_lfsx_stfsx();
  709. test_plfs_pstfs();
  710. test_lfdx_stfdx();
  711. test_plfd_pstfd();
  712. test_lvx_stvx();
  713. test_lxvd2x_stxvd2x();
  714. test_lxvp_stxvp();
  715. test_lxvpx_stxvpx();
  716. test_plxvp_pstxvp();
  717. }
  718. struct compute_test {
  719. char *mnemonic;
  720. unsigned long cpu_feature;
  721. struct {
  722. char *descr;
  723. unsigned long flags;
  724. ppc_inst_t instr;
  725. struct pt_regs regs;
  726. } subtests[MAX_SUBTESTS + 1];
  727. };
  728. /* Extreme values for si0||si1 (the MLS:D-form 34 bit immediate field) */
  729. #define SI_MIN BIT(33)
  730. #define SI_MAX (BIT(33) - 1)
  731. #define SI_UMAX (BIT(34) - 1)
  732. static struct compute_test compute_tests[] = {
  733. {
  734. .mnemonic = "nop",
  735. .subtests = {
  736. {
  737. .descr = "R0 = LONG_MAX",
  738. .instr = ppc_inst(PPC_RAW_NOP()),
  739. .regs = {
  740. .gpr[0] = LONG_MAX,
  741. }
  742. }
  743. }
  744. },
  745. {
  746. .mnemonic = "setb",
  747. .cpu_feature = CPU_FTR_ARCH_300,
  748. .subtests = {
  749. {
  750. .descr = "BFA = 1, CR = GT",
  751. .instr = ppc_inst(PPC_RAW_SETB(20, 1)),
  752. .regs = {
  753. .ccr = 0x4000000,
  754. }
  755. },
  756. {
  757. .descr = "BFA = 4, CR = LT",
  758. .instr = ppc_inst(PPC_RAW_SETB(20, 4)),
  759. .regs = {
  760. .ccr = 0x8000,
  761. }
  762. },
  763. {
  764. .descr = "BFA = 5, CR = EQ",
  765. .instr = ppc_inst(PPC_RAW_SETB(20, 5)),
  766. .regs = {
  767. .ccr = 0x200,
  768. }
  769. }
  770. }
  771. },
  772. {
  773. .mnemonic = "add",
  774. .subtests = {
  775. {
  776. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  777. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  778. .regs = {
  779. .gpr[21] = LONG_MIN,
  780. .gpr[22] = LONG_MIN,
  781. }
  782. },
  783. {
  784. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  785. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  786. .regs = {
  787. .gpr[21] = LONG_MIN,
  788. .gpr[22] = LONG_MAX,
  789. }
  790. },
  791. {
  792. .descr = "RA = LONG_MAX, RB = LONG_MAX",
  793. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  794. .regs = {
  795. .gpr[21] = LONG_MAX,
  796. .gpr[22] = LONG_MAX,
  797. }
  798. },
  799. {
  800. .descr = "RA = ULONG_MAX, RB = ULONG_MAX",
  801. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  802. .regs = {
  803. .gpr[21] = ULONG_MAX,
  804. .gpr[22] = ULONG_MAX,
  805. }
  806. },
  807. {
  808. .descr = "RA = ULONG_MAX, RB = 0x1",
  809. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  810. .regs = {
  811. .gpr[21] = ULONG_MAX,
  812. .gpr[22] = 0x1,
  813. }
  814. },
  815. {
  816. .descr = "RA = INT_MIN, RB = INT_MIN",
  817. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  818. .regs = {
  819. .gpr[21] = INT_MIN,
  820. .gpr[22] = INT_MIN,
  821. }
  822. },
  823. {
  824. .descr = "RA = INT_MIN, RB = INT_MAX",
  825. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  826. .regs = {
  827. .gpr[21] = INT_MIN,
  828. .gpr[22] = INT_MAX,
  829. }
  830. },
  831. {
  832. .descr = "RA = INT_MAX, RB = INT_MAX",
  833. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  834. .regs = {
  835. .gpr[21] = INT_MAX,
  836. .gpr[22] = INT_MAX,
  837. }
  838. },
  839. {
  840. .descr = "RA = UINT_MAX, RB = UINT_MAX",
  841. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  842. .regs = {
  843. .gpr[21] = UINT_MAX,
  844. .gpr[22] = UINT_MAX,
  845. }
  846. },
  847. {
  848. .descr = "RA = UINT_MAX, RB = 0x1",
  849. .instr = ppc_inst(PPC_RAW_ADD(20, 21, 22)),
  850. .regs = {
  851. .gpr[21] = UINT_MAX,
  852. .gpr[22] = 0x1,
  853. }
  854. }
  855. }
  856. },
  857. {
  858. .mnemonic = "add.",
  859. .subtests = {
  860. {
  861. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  862. .flags = IGNORE_CCR,
  863. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  864. .regs = {
  865. .gpr[21] = LONG_MIN,
  866. .gpr[22] = LONG_MIN,
  867. }
  868. },
  869. {
  870. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  871. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  872. .regs = {
  873. .gpr[21] = LONG_MIN,
  874. .gpr[22] = LONG_MAX,
  875. }
  876. },
  877. {
  878. .descr = "RA = LONG_MAX, RB = LONG_MAX",
  879. .flags = IGNORE_CCR,
  880. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  881. .regs = {
  882. .gpr[21] = LONG_MAX,
  883. .gpr[22] = LONG_MAX,
  884. }
  885. },
  886. {
  887. .descr = "RA = ULONG_MAX, RB = ULONG_MAX",
  888. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  889. .regs = {
  890. .gpr[21] = ULONG_MAX,
  891. .gpr[22] = ULONG_MAX,
  892. }
  893. },
  894. {
  895. .descr = "RA = ULONG_MAX, RB = 0x1",
  896. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  897. .regs = {
  898. .gpr[21] = ULONG_MAX,
  899. .gpr[22] = 0x1,
  900. }
  901. },
  902. {
  903. .descr = "RA = INT_MIN, RB = INT_MIN",
  904. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  905. .regs = {
  906. .gpr[21] = INT_MIN,
  907. .gpr[22] = INT_MIN,
  908. }
  909. },
  910. {
  911. .descr = "RA = INT_MIN, RB = INT_MAX",
  912. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  913. .regs = {
  914. .gpr[21] = INT_MIN,
  915. .gpr[22] = INT_MAX,
  916. }
  917. },
  918. {
  919. .descr = "RA = INT_MAX, RB = INT_MAX",
  920. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  921. .regs = {
  922. .gpr[21] = INT_MAX,
  923. .gpr[22] = INT_MAX,
  924. }
  925. },
  926. {
  927. .descr = "RA = UINT_MAX, RB = UINT_MAX",
  928. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  929. .regs = {
  930. .gpr[21] = UINT_MAX,
  931. .gpr[22] = UINT_MAX,
  932. }
  933. },
  934. {
  935. .descr = "RA = UINT_MAX, RB = 0x1",
  936. .instr = ppc_inst(PPC_RAW_ADD_DOT(20, 21, 22)),
  937. .regs = {
  938. .gpr[21] = UINT_MAX,
  939. .gpr[22] = 0x1,
  940. }
  941. }
  942. }
  943. },
  944. {
  945. .mnemonic = "addc",
  946. .subtests = {
  947. {
  948. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  949. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  950. .regs = {
  951. .gpr[21] = LONG_MIN,
  952. .gpr[22] = LONG_MIN,
  953. }
  954. },
  955. {
  956. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  957. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  958. .regs = {
  959. .gpr[21] = LONG_MIN,
  960. .gpr[22] = LONG_MAX,
  961. }
  962. },
  963. {
  964. .descr = "RA = LONG_MAX, RB = LONG_MAX",
  965. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  966. .regs = {
  967. .gpr[21] = LONG_MAX,
  968. .gpr[22] = LONG_MAX,
  969. }
  970. },
  971. {
  972. .descr = "RA = ULONG_MAX, RB = ULONG_MAX",
  973. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  974. .regs = {
  975. .gpr[21] = ULONG_MAX,
  976. .gpr[22] = ULONG_MAX,
  977. }
  978. },
  979. {
  980. .descr = "RA = ULONG_MAX, RB = 0x1",
  981. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  982. .regs = {
  983. .gpr[21] = ULONG_MAX,
  984. .gpr[22] = 0x1,
  985. }
  986. },
  987. {
  988. .descr = "RA = INT_MIN, RB = INT_MIN",
  989. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  990. .regs = {
  991. .gpr[21] = INT_MIN,
  992. .gpr[22] = INT_MIN,
  993. }
  994. },
  995. {
  996. .descr = "RA = INT_MIN, RB = INT_MAX",
  997. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  998. .regs = {
  999. .gpr[21] = INT_MIN,
  1000. .gpr[22] = INT_MAX,
  1001. }
  1002. },
  1003. {
  1004. .descr = "RA = INT_MAX, RB = INT_MAX",
  1005. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  1006. .regs = {
  1007. .gpr[21] = INT_MAX,
  1008. .gpr[22] = INT_MAX,
  1009. }
  1010. },
  1011. {
  1012. .descr = "RA = UINT_MAX, RB = UINT_MAX",
  1013. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  1014. .regs = {
  1015. .gpr[21] = UINT_MAX,
  1016. .gpr[22] = UINT_MAX,
  1017. }
  1018. },
  1019. {
  1020. .descr = "RA = UINT_MAX, RB = 0x1",
  1021. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  1022. .regs = {
  1023. .gpr[21] = UINT_MAX,
  1024. .gpr[22] = 0x1,
  1025. }
  1026. },
  1027. {
  1028. .descr = "RA = LONG_MIN | INT_MIN, RB = LONG_MIN | INT_MIN",
  1029. .instr = ppc_inst(PPC_RAW_ADDC(20, 21, 22)),
  1030. .regs = {
  1031. .gpr[21] = LONG_MIN | (uint)INT_MIN,
  1032. .gpr[22] = LONG_MIN | (uint)INT_MIN,
  1033. }
  1034. }
  1035. }
  1036. },
  1037. {
  1038. .mnemonic = "addc.",
  1039. .subtests = {
  1040. {
  1041. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  1042. .flags = IGNORE_CCR,
  1043. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1044. .regs = {
  1045. .gpr[21] = LONG_MIN,
  1046. .gpr[22] = LONG_MIN,
  1047. }
  1048. },
  1049. {
  1050. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  1051. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1052. .regs = {
  1053. .gpr[21] = LONG_MIN,
  1054. .gpr[22] = LONG_MAX,
  1055. }
  1056. },
  1057. {
  1058. .descr = "RA = LONG_MAX, RB = LONG_MAX",
  1059. .flags = IGNORE_CCR,
  1060. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1061. .regs = {
  1062. .gpr[21] = LONG_MAX,
  1063. .gpr[22] = LONG_MAX,
  1064. }
  1065. },
  1066. {
  1067. .descr = "RA = ULONG_MAX, RB = ULONG_MAX",
  1068. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1069. .regs = {
  1070. .gpr[21] = ULONG_MAX,
  1071. .gpr[22] = ULONG_MAX,
  1072. }
  1073. },
  1074. {
  1075. .descr = "RA = ULONG_MAX, RB = 0x1",
  1076. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1077. .regs = {
  1078. .gpr[21] = ULONG_MAX,
  1079. .gpr[22] = 0x1,
  1080. }
  1081. },
  1082. {
  1083. .descr = "RA = INT_MIN, RB = INT_MIN",
  1084. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1085. .regs = {
  1086. .gpr[21] = INT_MIN,
  1087. .gpr[22] = INT_MIN,
  1088. }
  1089. },
  1090. {
  1091. .descr = "RA = INT_MIN, RB = INT_MAX",
  1092. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1093. .regs = {
  1094. .gpr[21] = INT_MIN,
  1095. .gpr[22] = INT_MAX,
  1096. }
  1097. },
  1098. {
  1099. .descr = "RA = INT_MAX, RB = INT_MAX",
  1100. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1101. .regs = {
  1102. .gpr[21] = INT_MAX,
  1103. .gpr[22] = INT_MAX,
  1104. }
  1105. },
  1106. {
  1107. .descr = "RA = UINT_MAX, RB = UINT_MAX",
  1108. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1109. .regs = {
  1110. .gpr[21] = UINT_MAX,
  1111. .gpr[22] = UINT_MAX,
  1112. }
  1113. },
  1114. {
  1115. .descr = "RA = UINT_MAX, RB = 0x1",
  1116. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1117. .regs = {
  1118. .gpr[21] = UINT_MAX,
  1119. .gpr[22] = 0x1,
  1120. }
  1121. },
  1122. {
  1123. .descr = "RA = LONG_MIN | INT_MIN, RB = LONG_MIN | INT_MIN",
  1124. .instr = ppc_inst(PPC_RAW_ADDC_DOT(20, 21, 22)),
  1125. .regs = {
  1126. .gpr[21] = LONG_MIN | (uint)INT_MIN,
  1127. .gpr[22] = LONG_MIN | (uint)INT_MIN,
  1128. }
  1129. }
  1130. }
  1131. },
  1132. {
  1133. .mnemonic = "divde",
  1134. .subtests = {
  1135. {
  1136. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  1137. .instr = ppc_inst(PPC_RAW_DIVDE(20, 21, 22)),
  1138. .regs = {
  1139. .gpr[21] = LONG_MIN,
  1140. .gpr[22] = LONG_MIN,
  1141. }
  1142. },
  1143. {
  1144. .descr = "RA = 1L, RB = 0",
  1145. .instr = ppc_inst(PPC_RAW_DIVDE(20, 21, 22)),
  1146. .flags = IGNORE_GPR(20),
  1147. .regs = {
  1148. .gpr[21] = 1L,
  1149. .gpr[22] = 0,
  1150. }
  1151. },
  1152. {
  1153. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  1154. .instr = ppc_inst(PPC_RAW_DIVDE(20, 21, 22)),
  1155. .regs = {
  1156. .gpr[21] = LONG_MIN,
  1157. .gpr[22] = LONG_MAX,
  1158. }
  1159. }
  1160. }
  1161. },
  1162. {
  1163. .mnemonic = "divde.",
  1164. .subtests = {
  1165. {
  1166. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  1167. .instr = ppc_inst(PPC_RAW_DIVDE_DOT(20, 21, 22)),
  1168. .regs = {
  1169. .gpr[21] = LONG_MIN,
  1170. .gpr[22] = LONG_MIN,
  1171. }
  1172. },
  1173. {
  1174. .descr = "RA = 1L, RB = 0",
  1175. .instr = ppc_inst(PPC_RAW_DIVDE_DOT(20, 21, 22)),
  1176. .flags = IGNORE_GPR(20),
  1177. .regs = {
  1178. .gpr[21] = 1L,
  1179. .gpr[22] = 0,
  1180. }
  1181. },
  1182. {
  1183. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  1184. .instr = ppc_inst(PPC_RAW_DIVDE_DOT(20, 21, 22)),
  1185. .regs = {
  1186. .gpr[21] = LONG_MIN,
  1187. .gpr[22] = LONG_MAX,
  1188. }
  1189. }
  1190. }
  1191. },
  1192. {
  1193. .mnemonic = "divdeu",
  1194. .subtests = {
  1195. {
  1196. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  1197. .instr = ppc_inst(PPC_RAW_DIVDEU(20, 21, 22)),
  1198. .flags = IGNORE_GPR(20),
  1199. .regs = {
  1200. .gpr[21] = LONG_MIN,
  1201. .gpr[22] = LONG_MIN,
  1202. }
  1203. },
  1204. {
  1205. .descr = "RA = 1L, RB = 0",
  1206. .instr = ppc_inst(PPC_RAW_DIVDEU(20, 21, 22)),
  1207. .flags = IGNORE_GPR(20),
  1208. .regs = {
  1209. .gpr[21] = 1L,
  1210. .gpr[22] = 0,
  1211. }
  1212. },
  1213. {
  1214. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  1215. .instr = ppc_inst(PPC_RAW_DIVDEU(20, 21, 22)),
  1216. .regs = {
  1217. .gpr[21] = LONG_MIN,
  1218. .gpr[22] = LONG_MAX,
  1219. }
  1220. },
  1221. {
  1222. .descr = "RA = LONG_MAX - 1, RB = LONG_MAX",
  1223. .instr = ppc_inst(PPC_RAW_DIVDEU(20, 21, 22)),
  1224. .regs = {
  1225. .gpr[21] = LONG_MAX - 1,
  1226. .gpr[22] = LONG_MAX,
  1227. }
  1228. },
  1229. {
  1230. .descr = "RA = LONG_MIN + 1, RB = LONG_MIN",
  1231. .instr = ppc_inst(PPC_RAW_DIVDEU(20, 21, 22)),
  1232. .flags = IGNORE_GPR(20),
  1233. .regs = {
  1234. .gpr[21] = LONG_MIN + 1,
  1235. .gpr[22] = LONG_MIN,
  1236. }
  1237. }
  1238. }
  1239. },
  1240. {
  1241. .mnemonic = "divdeu.",
  1242. .subtests = {
  1243. {
  1244. .descr = "RA = LONG_MIN, RB = LONG_MIN",
  1245. .instr = ppc_inst(PPC_RAW_DIVDEU_DOT(20, 21, 22)),
  1246. .flags = IGNORE_GPR(20),
  1247. .regs = {
  1248. .gpr[21] = LONG_MIN,
  1249. .gpr[22] = LONG_MIN,
  1250. }
  1251. },
  1252. {
  1253. .descr = "RA = 1L, RB = 0",
  1254. .instr = ppc_inst(PPC_RAW_DIVDEU_DOT(20, 21, 22)),
  1255. .flags = IGNORE_GPR(20),
  1256. .regs = {
  1257. .gpr[21] = 1L,
  1258. .gpr[22] = 0,
  1259. }
  1260. },
  1261. {
  1262. .descr = "RA = LONG_MIN, RB = LONG_MAX",
  1263. .instr = ppc_inst(PPC_RAW_DIVDEU_DOT(20, 21, 22)),
  1264. .regs = {
  1265. .gpr[21] = LONG_MIN,
  1266. .gpr[22] = LONG_MAX,
  1267. }
  1268. },
  1269. {
  1270. .descr = "RA = LONG_MAX - 1, RB = LONG_MAX",
  1271. .instr = ppc_inst(PPC_RAW_DIVDEU_DOT(20, 21, 22)),
  1272. .regs = {
  1273. .gpr[21] = LONG_MAX - 1,
  1274. .gpr[22] = LONG_MAX,
  1275. }
  1276. },
  1277. {
  1278. .descr = "RA = LONG_MIN + 1, RB = LONG_MIN",
  1279. .instr = ppc_inst(PPC_RAW_DIVDEU_DOT(20, 21, 22)),
  1280. .flags = IGNORE_GPR(20),
  1281. .regs = {
  1282. .gpr[21] = LONG_MIN + 1,
  1283. .gpr[22] = LONG_MIN,
  1284. }
  1285. }
  1286. }
  1287. },
  1288. {
  1289. .mnemonic = "paddi",
  1290. .cpu_feature = CPU_FTR_ARCH_31,
  1291. .subtests = {
  1292. {
  1293. .descr = "RA = LONG_MIN, SI = SI_MIN, R = 0",
  1294. .instr = TEST_PADDI(21, 22, SI_MIN, 0),
  1295. .regs = {
  1296. .gpr[21] = 0,
  1297. .gpr[22] = LONG_MIN,
  1298. }
  1299. },
  1300. {
  1301. .descr = "RA = LONG_MIN, SI = SI_MAX, R = 0",
  1302. .instr = TEST_PADDI(21, 22, SI_MAX, 0),
  1303. .regs = {
  1304. .gpr[21] = 0,
  1305. .gpr[22] = LONG_MIN,
  1306. }
  1307. },
  1308. {
  1309. .descr = "RA = LONG_MAX, SI = SI_MAX, R = 0",
  1310. .instr = TEST_PADDI(21, 22, SI_MAX, 0),
  1311. .regs = {
  1312. .gpr[21] = 0,
  1313. .gpr[22] = LONG_MAX,
  1314. }
  1315. },
  1316. {
  1317. .descr = "RA = ULONG_MAX, SI = SI_UMAX, R = 0",
  1318. .instr = TEST_PADDI(21, 22, SI_UMAX, 0),
  1319. .regs = {
  1320. .gpr[21] = 0,
  1321. .gpr[22] = ULONG_MAX,
  1322. }
  1323. },
  1324. {
  1325. .descr = "RA = ULONG_MAX, SI = 0x1, R = 0",
  1326. .instr = TEST_PADDI(21, 22, 0x1, 0),
  1327. .regs = {
  1328. .gpr[21] = 0,
  1329. .gpr[22] = ULONG_MAX,
  1330. }
  1331. },
  1332. {
  1333. .descr = "RA = INT_MIN, SI = SI_MIN, R = 0",
  1334. .instr = TEST_PADDI(21, 22, SI_MIN, 0),
  1335. .regs = {
  1336. .gpr[21] = 0,
  1337. .gpr[22] = INT_MIN,
  1338. }
  1339. },
  1340. {
  1341. .descr = "RA = INT_MIN, SI = SI_MAX, R = 0",
  1342. .instr = TEST_PADDI(21, 22, SI_MAX, 0),
  1343. .regs = {
  1344. .gpr[21] = 0,
  1345. .gpr[22] = INT_MIN,
  1346. }
  1347. },
  1348. {
  1349. .descr = "RA = INT_MAX, SI = SI_MAX, R = 0",
  1350. .instr = TEST_PADDI(21, 22, SI_MAX, 0),
  1351. .regs = {
  1352. .gpr[21] = 0,
  1353. .gpr[22] = INT_MAX,
  1354. }
  1355. },
  1356. {
  1357. .descr = "RA = UINT_MAX, SI = 0x1, R = 0",
  1358. .instr = TEST_PADDI(21, 22, 0x1, 0),
  1359. .regs = {
  1360. .gpr[21] = 0,
  1361. .gpr[22] = UINT_MAX,
  1362. }
  1363. },
  1364. {
  1365. .descr = "RA = UINT_MAX, SI = SI_MAX, R = 0",
  1366. .instr = TEST_PADDI(21, 22, SI_MAX, 0),
  1367. .regs = {
  1368. .gpr[21] = 0,
  1369. .gpr[22] = UINT_MAX,
  1370. }
  1371. },
  1372. {
  1373. .descr = "RA is r0, SI = SI_MIN, R = 0",
  1374. .instr = TEST_PADDI(21, 0, SI_MIN, 0),
  1375. .regs = {
  1376. .gpr[21] = 0x0,
  1377. }
  1378. },
  1379. {
  1380. .descr = "RA = 0, SI = SI_MIN, R = 0",
  1381. .instr = TEST_PADDI(21, 22, SI_MIN, 0),
  1382. .regs = {
  1383. .gpr[21] = 0x0,
  1384. .gpr[22] = 0x0,
  1385. }
  1386. },
  1387. {
  1388. .descr = "RA is r0, SI = 0, R = 1",
  1389. .instr = TEST_PADDI(21, 0, 0, 1),
  1390. .regs = {
  1391. .gpr[21] = 0,
  1392. }
  1393. },
  1394. {
  1395. .descr = "RA is r0, SI = SI_MIN, R = 1",
  1396. .instr = TEST_PADDI(21, 0, SI_MIN, 1),
  1397. .regs = {
  1398. .gpr[21] = 0,
  1399. }
  1400. },
  1401. /* Invalid instruction form with R = 1 and RA != 0 */
  1402. {
  1403. .descr = "RA = R22(0), SI = 0, R = 1",
  1404. .instr = TEST_PADDI(21, 22, 0, 1),
  1405. .flags = NEGATIVE_TEST,
  1406. .regs = {
  1407. .gpr[21] = 0,
  1408. .gpr[22] = 0,
  1409. }
  1410. }
  1411. }
  1412. }
  1413. };
  1414. static int __init emulate_compute_instr(struct pt_regs *regs,
  1415. ppc_inst_t instr,
  1416. bool negative)
  1417. {
  1418. int analysed;
  1419. struct instruction_op op;
  1420. if (!regs || !ppc_inst_val(instr))
  1421. return -EINVAL;
  1422. /* This is not a return frame regs */
  1423. regs->nip = patch_site_addr(&patch__exec_instr);
  1424. analysed = analyse_instr(&op, regs, instr);
  1425. if (analysed != 1 || GETTYPE(op.type) != COMPUTE) {
  1426. if (negative)
  1427. return -EFAULT;
  1428. pr_info("emulation failed, instruction = %08lx\n", ppc_inst_as_ulong(instr));
  1429. return -EFAULT;
  1430. }
  1431. if (analysed == 1 && negative)
  1432. pr_info("negative test failed, instruction = %08lx\n", ppc_inst_as_ulong(instr));
  1433. if (!negative)
  1434. emulate_update_regs(regs, &op);
  1435. return 0;
  1436. }
  1437. static int __init execute_compute_instr(struct pt_regs *regs,
  1438. ppc_inst_t instr)
  1439. {
  1440. extern int exec_instr(struct pt_regs *regs);
  1441. if (!regs || !ppc_inst_val(instr))
  1442. return -EINVAL;
  1443. /* Patch the NOP with the actual instruction */
  1444. patch_instruction_site(&patch__exec_instr, instr);
  1445. if (exec_instr(regs)) {
  1446. pr_info("execution failed, instruction = %08lx\n", ppc_inst_as_ulong(instr));
  1447. return -EFAULT;
  1448. }
  1449. return 0;
  1450. }
  1451. #define gpr_mismatch(gprn, exp, got) \
  1452. pr_info("GPR%u mismatch, exp = 0x%016lx, got = 0x%016lx\n", \
  1453. gprn, exp, got)
  1454. #define reg_mismatch(name, exp, got) \
  1455. pr_info("%s mismatch, exp = 0x%016lx, got = 0x%016lx\n", \
  1456. name, exp, got)
  1457. static void __init run_tests_compute(void)
  1458. {
  1459. unsigned long flags;
  1460. struct compute_test *test;
  1461. struct pt_regs *regs, exp, got;
  1462. unsigned int i, j, k;
  1463. ppc_inst_t instr;
  1464. bool ignore_gpr, ignore_xer, ignore_ccr, passed, rc, negative;
  1465. for (i = 0; i < ARRAY_SIZE(compute_tests); i++) {
  1466. test = &compute_tests[i];
  1467. if (test->cpu_feature && !early_cpu_has_feature(test->cpu_feature)) {
  1468. show_result(test->mnemonic, "SKIP (!CPU_FTR)");
  1469. continue;
  1470. }
  1471. for (j = 0; j < MAX_SUBTESTS && test->subtests[j].descr; j++) {
  1472. instr = test->subtests[j].instr;
  1473. flags = test->subtests[j].flags;
  1474. regs = &test->subtests[j].regs;
  1475. negative = flags & NEGATIVE_TEST;
  1476. ignore_xer = flags & IGNORE_XER;
  1477. ignore_ccr = flags & IGNORE_CCR;
  1478. passed = true;
  1479. memcpy(&exp, regs, sizeof(struct pt_regs));
  1480. memcpy(&got, regs, sizeof(struct pt_regs));
  1481. /*
  1482. * Set a compatible MSR value explicitly to ensure
  1483. * that XER and CR bits are updated appropriately
  1484. */
  1485. exp.msr = MSR_KERNEL;
  1486. got.msr = MSR_KERNEL;
  1487. rc = emulate_compute_instr(&got, instr, negative) != 0;
  1488. if (negative) {
  1489. /* skip executing instruction */
  1490. passed = rc;
  1491. goto print;
  1492. } else if (rc || execute_compute_instr(&exp, instr)) {
  1493. passed = false;
  1494. goto print;
  1495. }
  1496. /* Verify GPR values */
  1497. for (k = 0; k < 32; k++) {
  1498. ignore_gpr = flags & IGNORE_GPR(k);
  1499. if (!ignore_gpr && exp.gpr[k] != got.gpr[k]) {
  1500. passed = false;
  1501. gpr_mismatch(k, exp.gpr[k], got.gpr[k]);
  1502. }
  1503. }
  1504. /* Verify LR value */
  1505. if (exp.link != got.link) {
  1506. passed = false;
  1507. reg_mismatch("LR", exp.link, got.link);
  1508. }
  1509. /* Verify XER value */
  1510. if (!ignore_xer && exp.xer != got.xer) {
  1511. passed = false;
  1512. reg_mismatch("XER", exp.xer, got.xer);
  1513. }
  1514. /* Verify CR value */
  1515. if (!ignore_ccr && exp.ccr != got.ccr) {
  1516. passed = false;
  1517. reg_mismatch("CR", exp.ccr, got.ccr);
  1518. }
  1519. print:
  1520. show_result_with_descr(test->mnemonic,
  1521. test->subtests[j].descr,
  1522. passed ? "PASS" : "FAIL");
  1523. }
  1524. }
  1525. }
  1526. static int __init test_emulate_step(void)
  1527. {
  1528. printk(KERN_INFO "Running instruction emulation self-tests ...\n");
  1529. run_tests_load_store();
  1530. run_tests_compute();
  1531. return 0;
  1532. }
  1533. late_initcall(test_emulate_step);