Skip to contentSkip to conversation

Prefixed instruction tests to cover negative cases

PATCH Applied Balamuruhan S 2 participants 6 messages Jun 26, 2020 – Jul 24, 2020 PowerPC

lore
Share & tools
lore
#0 lore raw

v2 cover Prefixed instruction tests to cover negative cases

This patchset adds support to test negative scenarios and adds testcase for paddi with few fixes. It is based on powerpc/next and on top of Jordan's tests for prefixed instructions patchsets,

https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-May/211394.html
https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-June/211768.html

Changes in v2:
-------------
Fix review comments from Sandipan and Jordan,
* use helper function to print word/prefix instructions
* reuse bits of flags to represent negative test scenario
* always set NIP instead of only setting for relative prefixed instructions

Balamuruhan S (4):
powerpc test_emulate_step: enhancement to test negative scenarios powerpc test_emulate_step: add negative tests for prefixed addi powerpc sstep: introduce macros to retrieve Prefix instruction
operands
powerpc test_emulate_step: move extern declaration to sstep.h

 arch/powerpc/include/asm/sstep.h     |  6 ++++
 arch/powerpc/lib/sstep.c             | 12 ++++----
 arch/powerpc/lib/test_emulate_step.c | 42 ++++++++++++++++++++--------
 3 files changed, 43 insertions(+), 17 deletions(-)

base-commit: 64677779e8962c20b580b471790fe42367750599
prerequisite-patch-id: 3fff52f42000e816e2e8b4f75a2bca651dec5efe
prerequisite-patch-id: 5d7904bf38248ec39ed0f6223500286b9eaf82a9
prerequisite-patch-id: 7236d3caa4dc6de6079ae893678223876a3bb364
prerequisite-patch-id: 733e7f9b5c6ade64b8a1c7458b5aefe6b7d6fcff
prerequisite-patch-id: 4793e7716f3f56577a49976539d06db37ba31a80
prerequisite-patch-id: ffb024c2590e7249190b0137acf267e821a816a7
prerequisite-patch-id: 86e64f47de2dc6e9a6e1404de12d7c91775c22c8
prerequisite-patch-id: 9fbe5c3af9590696c230944cdee3c45e01f44d6d
prerequisite-patch-id: 3f9c6238023c867e27d87de26487e7e665a9dc12

Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 1/4 powerpc test_emulate_step: enhancement to test negative scenarios

add provision to declare test is a negative scenario, verify whether emulation fails and avoid executing it.

---
 arch/powerpc/lib/test_emulate_step.c | 30 +++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)
arch/powerpc/lib/test_emulate_step.c +21 -9
index 0ca2b7cc8d8c..7c30a69c174f 100644
@@ -118,6 +118,7 @@
 #define IGNORE_GPR(n)	(0x1UL << (n))
 #define IGNORE_XER	(0x1UL << 32)
 #define IGNORE_CCR	(0x1UL << 33)
+#define NEGATIVE_TEST	(0x1UL << 63)
 
 static void __init init_pt_regs(struct pt_regs *regs)
 {
@@ -1202,8 +1203,10 @@ static struct compute_test compute_tests[] = {
 };
 
 static int __init emulate_compute_instr(struct pt_regs *regs,
-					struct ppc_inst instr)
+					struct ppc_inst instr,
+					bool negative)
 {
+	int analysed;
 	extern s32 patch__exec_instr;
 	struct instruction_op op;
 
@@ -1212,13 +1215,17 @@ static int __init emulate_compute_instr(struct pt_regs *regs,
 
 	regs->nip = patch_site_addr(&patch__exec_instr);
 
-	if (analyse_instr(&op, regs, instr) != 1 ||
-	    GETTYPE(op.type) != COMPUTE) {
-		pr_info("execution failed, instruction = %s\n", ppc_inst_as_str(instr));
+	analysed = analyse_instr(&op, regs, instr);
+	if (analysed != 1 || GETTYPE(op.type) != COMPUTE) {
+		if (negative)
+			return -EFAULT;
+		pr_info("emulation failed, instruction = %s\n", ppc_inst_as_str(instr));
 		return -EFAULT;
 	}
-
-	emulate_update_regs(regs, &op);
+	if (analysed == 1 && negative)
+		pr_info("negative test failed, instruction = %s\n", ppc_inst_as_str(instr));
+	if (!negative)
+		emulate_update_regs(regs, &op);
 	return 0;
 }
 
@@ -1256,7 +1263,7 @@ static void __init run_tests_compute(void)
 	struct pt_regs *regs, exp, got;
 	unsigned int i, j, k;
 	struct ppc_inst instr;
-	bool ignore_gpr, ignore_xer, ignore_ccr, passed;
+	bool ignore_gpr, ignore_xer, ignore_ccr, passed, rc, negative;
 
 	for (i = 0; i < ARRAY_SIZE(compute_tests); i++) {
 		test = &compute_tests[i];
@@ -1270,6 +1277,7 @@ static void __init run_tests_compute(void)
 			instr = test->subtests[j].instr;
 			flags = test->subtests[j].flags;
 			regs = &test->subtests[j].regs;
+			negative = flags & NEGATIVE_TEST;
 			ignore_xer = flags & IGNORE_XER;
 			ignore_ccr = flags & IGNORE_CCR;
 			passed = true;
@@ -1284,8 +1292,12 @@ static void __init run_tests_compute(void)
 			exp.msr = MSR_KERNEL;
 			got.msr = MSR_KERNEL;
 
-			if (emulate_compute_instr(&got, instr) ||
-			    execute_compute_instr(&exp, instr)) {
+			rc = emulate_compute_instr(&got, instr, negative) != 0;
+			if (negative) {
+				/* skip executing instruction */
+				passed = rc;
+				goto print;
+			} else if (rc || execute_compute_instr(&exp, instr)) {
 				passed = false;
 				goto print;
 			}
Signature · 1 line
2.24.1
#2 lore raw

PATCH v2 2/4 powerpc test_emulate_step: add negative tests for prefixed addi

In reply to Balamuruhan S

testcases for paddi instruction to cover the negative case, if R is equal to 1 and RA is not equal to 0, the instruction form is invalid.

---
 arch/powerpc/lib/test_emulate_step.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/powerpc/lib/test_emulate_step.c +10 -0
index 7c30a69c174f..0ee59301ef99 100644
@@ -1197,6 +1197,16 @@ static struct compute_test compute_tests[] = {
 				.regs = {
 					.gpr[21] = 0,
 				}
+			},
+			/* Invalid instruction form with R = 1 and RA != 0 */
+			{
+				.descr = "RA = R22(0), SI = 0, R = 1",
+				.instr = TEST_PADDI(21, 22, 0, 1),
+				.flags = NEGATIVE_TEST,
+				.regs = {
+					.gpr[21] = 0,
+					.gpr[22] = 0,
+				}
 			}
 		}
 	}
Signature · 1 line
2.24.1
#3 lore raw

PATCH v2 3/4 powerpc sstep: introduce macros to retrieve Prefix instruction operands

In reply to Balamuruhan S

retrieve prefix instruction operands RA and pc relative bit R values using macros and adopt it in sstep.c and test_emulate_step.c.

---
 arch/powerpc/include/asm/sstep.h |  4 ++++
 arch/powerpc/lib/sstep.c         | 12 ++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/sstep.h +4 -0
index 3b01c69a44aa..325975b4ef30 100644
@@ -104,6 +104,10 @@ enum instruction_type {
 
 #define MKOP(t, f, s)	((t) | (f) | SIZE(s))
 
+/* Prefix instruction operands */
+#define GET_PREFIX_RA(i)	(((i) >> 16) & 0x1f)
+#define GET_PREFIX_R(i)		((i) & (1ul << 20))
+
 struct instruction_op {
 	int type;
 	int reg;
arch/powerpc/lib/sstep.c +6 -6
index 5abe98216dc2..fb4c5767663d 100644
@@ -200,8 +200,8 @@ static nokprobe_inline unsigned long mlsd_8lsd_ea(unsigned int instr,
 	unsigned int  dd;
 	unsigned long ea, d0, d1, d;
 
-	prefix_r = instr & (1ul << 20);
-	ra = (suffix >> 16) & 0x1f;
+	prefix_r = GET_PREFIX_R(instr);
+	ra = GET_PREFIX_RA(suffix);
 
 	d0 = instr & 0x3ffff;
 	d1 = suffix & 0xffff;
@@ -1339,8 +1339,8 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 	switch (opcode) {
 #ifdef __powerpc64__
 	case 1:
-		prefix_r = word & (1ul << 20);
-		ra = (suffix >> 16) & 0x1f;
+		prefix_r = GET_PREFIX_R(word);
+		ra = GET_PREFIX_RA(suffix);
 		rd = (suffix >> 21) & 0x1f;
 		op->reg = rd;
 		op->val = regs->gpr[rd];
@@ -2715,8 +2715,8 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 		}
 		break;
 	case 1: /* Prefixed instructions */
-		prefix_r = word & (1ul << 20);
-		ra = (suffix >> 16) & 0x1f;
+		prefix_r = GET_PREFIX_R(word);
+		ra = GET_PREFIX_RA(suffix);
 		op->update_reg = ra;
 		rd = (suffix >> 21) & 0x1f;
 		op->reg = rd;
Signature · 1 line
2.24.1
#4 lore raw

PATCH v2 4/4 powerpc test_emulate_step: move extern declaration to sstep.h

In reply to Balamuruhan S

fix checkpatch.pl warnings by moving extern declaration from source file to headerfile.

---
 arch/powerpc/include/asm/sstep.h     | 2 ++
 arch/powerpc/lib/test_emulate_step.c | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/sstep.h +2 -0
index 325975b4ef30..c8e37ef060c1 100644
@@ -108,6 +108,8 @@ enum instruction_type {
 #define GET_PREFIX_RA(i)	(((i) >> 16) & 0x1f)
 #define GET_PREFIX_R(i)		((i) & (1ul << 20))
 
+extern s32 patch__exec_instr;
+
 struct instruction_op {
 	int type;
 	int reg;
arch/powerpc/lib/test_emulate_step.c +0 -2
index 0ee59301ef99..c46bf6fc199b 100644
@@ -1217,7 +1217,6 @@ static int __init emulate_compute_instr(struct pt_regs *regs,
 					bool negative)
 {
 	int analysed;
-	extern s32 patch__exec_instr;
 	struct instruction_op op;
 
 	if (!regs || !ppc_inst_val(instr))
@@ -1243,7 +1242,6 @@ static int __init execute_compute_instr(struct pt_regs *regs,
 					struct ppc_inst instr)
 {
 	extern int exec_instr(struct pt_regs *regs);
-	extern s32 patch__exec_instr;
 
 	if (!regs || !ppc_inst_val(instr))
 		return -EINVAL;
Signature · 1 line
2.24.1
#5 lore raw
In reply to Balamuruhan S
Quoted 8 lines · Balamuruhan S

This patchset adds support to test negative scenarios and adds testcase for paddi with few fixes. It is based on powerpc/next and on top of Jordan's tests for prefixed instructions patchsets,

https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-May/211394.html
https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-June/211768.html

[...]

Applied to powerpc/next.

[1/4] powerpc/test_emulate_step: Enhancement to test negative scenarios

      https://git.kernel.org/powerpc/c/93c3a0ba2a0863a5c82a518d64044434f82a57f5
[2/4] powerpc/test_emulate_step: Add negative tests for prefixed addi
      https://git.kernel.org/powerpc/c/7e67c73b939b25d4ad18a536e52282aa35d8ee56
[3/4] powerpc/sstep: Introduce macros to retrieve Prefix instruction operands
      https://git.kernel.org/powerpc/c/68a180a44c29d7e918ae7d3c18a01b0751d1c22f
[4/4] powerpc/test_emulate_step: Move extern declaration to sstep.h
      https://git.kernel.org/powerpc/c/e93ad65e3611b06288efdf0cfd76c012df3feec1

cheers