powerpc: sstep: Emulation test infrastructure
cover powerpc: sstep: Emulation test infrastructure
This aims to add a test infrastructure for the in-kernel instruction emulation code. This is currently limited to testing only the basic integer operations and supports verification of the GPRs, LR, XER and CR.
There can be multiple test cases for each instruction. Each test case has to be provided with the initial register state (in the form of a struct pt_regs) and the 32-bit instruction to test.
Apart from verifying the end result, problems with the behaviour of certain instructions for things like setting certain bits in CR or XER (which can also be processor dependent) can be identified. For example, the newly introduced CA32 bit in XER, exclusive to P9 CPUs as of now, was not being set when expected for some of the arithmetic and shift instructions. With this infrastructure, it will be easier to identify such problems and rectify them. The test cases for the addc[.] instruction demonstrate this for different scenarios where the CA and CA32 bits of XER should be set.
Sandipan Das (5):
powerpc: Add bitmasks for D-form instruction fields
powerpc: Add bitmask for Rc instruction field
powerpc: sstep: Add instruction emulation selftests
powerpc: sstep: Add selftests for add[.] instruction
powerpc: sstep: Add selftests for addc[.] instruction
arch/powerpc/Kconfig.debug | 5 + arch/powerpc/include/asm/ppc-opcode.h | 5 + arch/powerpc/lib/Makefile | 1 + arch/powerpc/lib/exec_test_instr.S | 150 +++++++ arch/powerpc/lib/sstep_tests.c | 564 ++++++++++++++++++++++++++ 5 files changed, 725 insertions(+) create mode 100644 arch/powerpc/lib/exec_test_instr.S create mode 100644 arch/powerpc/lib/sstep_tests.c
Signature · 1 line
2.19.2
RFC PATCH 1/5 powerpc: Add bitmasks for D-form instruction fields
This adds the bitmask definitions of D, SI and UI fields found in D-form instructions.
--- arch/powerpc/include/asm/ppc-opcode.h | 3 +++ 1 file changed, 3 insertions(+)
arch/powerpc/include/asm/ppc-opcode.h +3 -0
@@ -403,6 +403,9 @@#define __PPC_CT (t) (((t) & 0x0f) << 21)#define __PPC_SPR (r) ((((r) & 0x1f) << 16) | ((((r) >> 5) & 0x1f) << 11))#define __PPC_RC21 (0x1 << 10) +#define __PPC_D (d) ((d) & 0xffff) +#define __PPC_SI (i) __PPC_D(i) +#define __PPC_UI (i) __PPC_D(i)/* * Only use the larx hint bit on 64bit CPUs. e500v1/v2 based CPUs will treat a
Signature · 1 line
2.19.2
RFC PATCH 2/5 powerpc: Add bitmask for Rc instruction field
In reply to Sandipan DasThis adds the bitmask definition for the Record bit that is available at the end (bit 31) of some instructions.
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -403,6 +403,7 @@#define __PPC_CT (t) (((t) & 0x0f) << 21)#define __PPC_SPR (r) ((((r) & 0x1f) << 16) | ((((r) >> 5) & 0x1f) << 11))#define __PPC_RC21 (0x1 << 10) +#define __PPC_RC31 (0x1)#define __PPC_D (d) ((d) & 0xffff)#define __PPC_SI (i) __PPC_D(i)#define __PPC_UI (i) __PPC_D(i)
Signature · 1 line
2.19.2
RFC PATCH 3/5 powerpc: sstep: Add instruction emulation selftests
In reply to Sandipan DasThis adds a selftest framework for the in-kernel instruction emulation infrastructure. This currently does not support the load/store and branch instructions and is limited to integer ALU instructions. Support for SPRs is also limited to LR, CR and XER for now.
Tests run at boot time if CONFIG_DEBUG_KERNEL, CONFIG_PPC64 and CONFIG_EMULATE_STEP_SELFTEST were set before the kernel build.
When writing the tests, one must not use any instructions that might overwrite the Stack Pointer (GPR1) or the Thread Pointer (GPR13).
--- arch/powerpc/Kconfig.debug | 5 + arch/powerpc/lib/Makefile | 1 + arch/powerpc/lib/exec_test_instr.S | 150 +++++++++++++++++++++++++++ arch/powerpc/lib/sstep_tests.c | 158 +++++++++++++++++++++++++++++ 4 files changed, 314 insertions(+) create mode 100644 arch/powerpc/lib/exec_test_instr.S create mode 100644 arch/powerpc/lib/sstep_tests.c
arch/powerpc/Kconfig.debug +5 -0
@@ -56,6 +56,11 @@ config CODE_PATCHING_SELFTEST bool "Run self-tests of the code-patching code" depends on DEBUG_KERNEL +config EMULATE_STEP_SELFTEST + bool "Run self-tests of the instruction emulation code" + depends on PPC64 && DEBUG_KERNEL + default n + config JUMP_LABEL_FEATURE_CHECKS bool "Enable use of jump label for cpu/mmu_has_feature()" depends on JUMP_LABEL
arch/powerpc/lib/Makefile +1 -0
@@ -31,6 +31,7 @@ obj64-y += copypage_64.o copyuser_64.o mem_64.o hweight_64.o \ obj64-$(CONFIG_SMP) += locks.o obj64-$(CONFIG_ALTIVEC) += vmx-helper.o obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o +obj64-$(CONFIG_EMULATE_STEP_SELFTEST) += exec_test_instr.o sstep_tests.o obj-y += checksum_$(BITS).o checksum_wrappers.o \ string_$(BITS).o memcmp_$(BITS).o
arch/powerpc/lib/exec_test_instr.S new file +150 -0
150 lines not on this page · raw message
arch/powerpc/lib/sstep_tests.c new file +158 -0
158 lines not on this page · raw message
Signature · 1 line
2.19.2
Re: [RFC PATCH 3/5] powerpc: sstep: Add instruction emulation selftests
Hi Sandipan,
I'm not really confident to review the asm, but I did have a couple of questions about the C:
Quoted 1 line
+#define
MAX_INSNS32
This doesn't seem to be used...
arch/powerpc/lib/sstep_tests.c line 73
Quoted 4 lines
arch/powerpc/lib/sstep_tests.c new file +4
-0+int execute_instr (struct pt_regs *regs,unsigned int instr) +{ +extern unsigned int exec_instr_execute[]; +extern int exec_instr (struct pt_regs *regs);
These externs sit inside the function scope. This feels less than ideal to me - is there a reason not to have these at global scope?
Quoted 5 lines
arch/powerpc/lib/sstep_tests.c new file +5
-0+ +if (!regs || !instr) +return -EINVAL ; + +/* Patch the NOP with the actual instruction */
arch/powerpc/lib/sstep_tests.c
Quoted 10 lines
arch/powerpc/lib/sstep_tests.c new file +9
-0+patch_instruction (&exec_instr_execute[0 ], instr); +if (exec_instr (regs)) { +pr_info ("execution failed, opcode = 0x%08x\n" , instr); +return -EFAULT ; + } + +return 0 ; +} +late_initcall (run_sstep_tests);
A design question: is there a reason to run these as an initcall rather than as a module that could either be built in or loaded separately? I'm not saying you have to do this, but I was wondering if you had considered it?
Lastly, snowpatch reports some checkpatch issues for this and your remaining patches: https://patchwork.ozlabs.org/patch/1035683/ (You are allowed to violate checkpatch rules with justification, FWIW)
Regards,
Daniel
Quoted 2 lines
Signature · 1 line
2.19.2
Re: [RFC PATCH 3/5] powerpc: sstep: Add instruction emulation selftests
Quoted 8 lines · Daniel Axtens
Hi Sandipan,
I'm not really confident to review the asm, but I did have a couple of questions about the C:
Quoted 1 line
+#define
MAX_INSNS32This doesn't seem to be used...
True. Thanks for pointing this out.
Quoted 8 lines
arch/powerpc/lib/sstep_tests.c line 73
Quoted 4 lines
arch/powerpc/lib/sstep_tests.c new file +4
-0+int execute_instr (struct pt_regs *regs,unsigned int instr) +{ +extern unsigned int exec_instr_execute[]; +extern int exec_instr (struct pt_regs *regs);These externs sit inside the function scope. This feels less than ideal to me - is there a reason not to have these at global scope?
Currently, execute_instr() is the only consumer. So, I thought I'd keep them local for now.
Quoted 14 lines
Quoted 13 lines
arch/powerpc/lib/sstep_tests.c new file +13
-0+ +if (!regs || !instr) +return -EINVAL ; + +/* Patch the NOP with the actual instruction */ +patch_instruction (&exec_instr_execute[0 ], instr); +if (exec_instr (regs)) { +pr_info ("execution failed, opcode = 0x%08x\n" , instr); +return -EFAULT ; + } + +return 0 ; +}
Quoted 6 lines
Quoted 1 line
+late_initcall(run_sstep_tests);
A design question: is there a reason to run these as an initcall rather than as a module that could either be built in or loaded separately? I'm not saying you have to do this, but I was wondering if you had considered it?
I did. As of now, there are some existing tests in test_emulate_step.c which use the same approach. So, I thought I'd stick with that approach to start off. This is anyway controlled by a Kconfig option.
Quoted 4 lines
Lastly, snowpatch reports some checkpatch issues for this and your remaining patches: https://patchwork.ozlabs.org/patch/1035683/ (You are allowed to violate checkpatch rules with justification, FWIW)
Will look into them.
Quoted 5 lines
Regards,
DanielQuoted 2 lines
Signature · 1 line
2.19.2
RFC PATCH 4/5 powerpc: sstep: Add selftests for add[.] instruction
In reply to Sandipan DasThis adds test cases for the add[.] instruction.
--- arch/powerpc/lib/sstep_tests.c | 194 +++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+)
arch/powerpc/lib/sstep_tests.c +194 -0
@@ -49,6 +49,200 @@ static struct sstep_test tests[] = { } } }, + { + .mnemonic = "add", + .subtests = + { + { + .descr = "RA = LONG_MIN, RB = LONG_MIN", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = LONG_MIN, + .gpr[22] = LONG_MIN, + } + }, + { + .descr = "RA = LONG_MIN, RB = LONG_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = LONG_MIN, + .gpr[22] = LONG_MAX, + } + }, + { + .descr = "RA = LONG_MAX, RB = LONG_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = LONG_MAX, + .gpr[22] = LONG_MAX, + } + }, + { + .descr = "RA = ULONG_MAX, RB = ULONG_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = ULONG_MAX, + .gpr[22] = ULONG_MAX, + } + }, + { + .descr = "RA = ULONG_MAX, RB = 0x1", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = ULONG_MAX, + .gpr[22] = 0x1, + } + }, + { + .descr = "RA = INT_MIN, RB = INT_MIN", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = INT_MIN, + .gpr[22] = INT_MIN, + } + }, + { + .descr = "RA = INT_MIN, RB = INT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = INT_MIN, + .gpr[22] = INT_MAX, + } + }, + { + .descr = "RA = INT_MAX, RB = INT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = INT_MAX, + .gpr[22] = INT_MAX, + } + }, + { + .descr = "RA = UINT_MAX, RB = UINT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = UINT_MAX, + .gpr[22] = UINT_MAX, + } + }, + { + .descr = "RA = UINT_MAX, RB = 0x1", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = UINT_MAX, + .gpr[22] = 0x1, + } + } + } + }, + { + .mnemonic = "add.", + .subtests = + { + { + .descr = "RA = LONG_MIN, RB = LONG_MIN", + .flags = IGNORE_CCR, + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = LONG_MIN, + .gpr[22] = LONG_MIN, + } + }, + { + .descr = "RA = LONG_MIN, RB = LONG_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = LONG_MIN, + .gpr[22] = LONG_MAX, + } + }, + { + .descr = "RA = LONG_MAX, RB = LONG_MAX", + .flags = IGNORE_CCR, + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = LONG_MAX, + .gpr[22] = LONG_MAX, + } + }, + { + .descr = "RA = ULONG_MAX, RB = ULONG_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = ULONG_MAX, + .gpr[22] = ULONG_MAX, + } + }, + { + .descr = "RA = ULONG_MAX, RB = 0x1", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = ULONG_MAX, + .gpr[22] = 0x1, + } + }, + { + .descr = "RA = INT_MIN, RB = INT_MIN", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = INT_MIN, + .gpr[22] = INT_MIN, + } + }, + { + .descr = "RA = INT_MIN, RB = INT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = INT_MIN, + .gpr[22] = INT_MAX, + } + }, + { + .descr = "RA = INT_MAX, RB = INT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = INT_MAX, + .gpr[22] = INT_MAX, + } + }, + { + .descr = "RA = UINT_MAX, RB = UINT_MAX", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = UINT_MAX, + .gpr[22] = UINT_MAX, + } + }, + { + .descr = "RA = UINT_MAX, RB = 0x1", + .instr = PPC_INST_ADD | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22) | __PPC_RC31, + .regs = + { + .gpr[21] = UINT_MAX, + .gpr[22] = 0x1, + } + } + } + }, };int emulate_instr (struct pt_regs *regs,unsigned int instr)
Signature · 1 line
2.19.2
RFC PATCH 5/5 powerpc: sstep: Add selftests for addc[.] instruction
In reply to Sandipan DasThis adds test cases for the addc[.] instruction.
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/lib/sstep_tests.c | 212 ++++++++++++++++++++++++++ 2 files changed, 213 insertions(+)
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -326,6 +326,7 @@#define PPC_INST_ADDI 0x38000000#define PPC_INST_ADDIS 0x3c000000#define PPC_INST_ADD 0x7c000214 +#define PPC_INST_ADDC 0x7c000014#define PPC_INST_SUB 0x7c000050#define PPC_INST_BLR 0x4e800020#define PPC_INST_BLRL 0x4e800021
arch/powerpc/lib/sstep_tests.c +212 -0
218 lines not on this page · raw message
Signature · 1 line
2.19.2
Re: [RFC PATCH 5/5] powerpc: sstep: Add selftests for addc[.] instruction
Hi Sandipan,
arch/powerpc/lib/sstep_tests.c static struct sstep_test tests[] = { · line 348
Quoted 9 lines
arch/powerpc/lib/sstep_tests.c +9
-0+ { + .descr = "RA = LONG_MIN | INT_MIN, RB = LONG_MIN | INT_MIN", + .instr = PPC_INST_ADDC | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = LONG_MIN | (uint) INT_MIN, + .gpr[22] = LONG_MIN | (uint) INT_MIN, + } + }
I don't know what this bit pattern is supposed to represent - is it supposed to be the smallest 32bit integer and the smallest 64bit integer 8000000080000000 - so you test 32 and 64 bit overflow at the same time?
For the series:
I notice the output is quite verbose, and doesn't include a line when it starts:
[ 0.826181] Running code patching self-tests ... [ 0.826607] Running feature fixup self-tests ... [ 0.826615] nop : R0 = LONG_MAX [PASS] [ 0.826617] add : RA = LONG_MIN, RB = LONG_MIN [PASS]
Maybe it would be good to include a line saying "Running single-step emulation self-tests" and perhaps by default on printing when there is a failure.
Finally, I think you might be able to squash patches 1 and 2 and patches 4 and 5, but that's just my personal preference.
Regards,
Daniel
Re: [RFC PATCH 5/5] powerpc: sstep: Add selftests for addc[.] instruction
Quoted 16 lines · Daniel Axtens
Hi Sandipan,
arch/powerpc/lib/sstep_tests.c static struct sstep_test tests[] = { · line 348
Quoted 9 lines
arch/powerpc/lib/sstep_tests.c +9
-0+ { + .descr = "RA = LONG_MIN | INT_MIN, RB = LONG_MIN | INT_MIN", + .instr = PPC_INST_ADDC | ___PPC_RT(20) | ___PPC_RA(21) | ___PPC_RB(22), + .regs = + { + .gpr[21] = LONG_MIN | (uint) INT_MIN, + .gpr[22] = LONG_MIN | (uint) INT_MIN, + } + }I don't know what this bit pattern is supposed to represent - is it supposed to be the smallest 32bit integer and the smallest 64bit integer 8000000080000000 - so you test 32 and 64 bit overflow at the same time?
Yes, exactly.
Quoted 7 lines
For the series:
I notice the output is quite verbose, and doesn't include a line when it starts:
Quoted 9 lines
[ 0.826181] Running code patching self-tests ... [ 0.826607] Running feature fixup self-tests ... [ 0.826615] nop : R0 = LONG_MAX [PASS] [ 0.826617] add : RA = LONG_MIN, RB = LONG_MIN [PASS]Maybe it would be good to include a line saying "Running single-step emulation self-tests" and perhaps by default on printing when there is a failure.
That makes sense. Will include it in the next revision.
Quoted 6 lines
Finally, I think you might be able to squash patches 1 and 2 and patches 4 and 5, but that's just my personal preference.
Regards,
Daniel
Hi Sandipan,
Quoted 9 lines
This aims to add a test infrastructure for the in-kernel instruction emulation code. This is currently limited to testing only the basic integer operations and supports verification of the GPRs, LR, XER and CR.
There can be multiple test cases for each instruction. Each test case has to be provided with the initial register state (in the form of a struct
pt_regs) and the 32-bit instruction to test.
Quoted 10 lines
Apart from verifying the end result, problems with the behaviour of certain instructions for things like setting certain bits in CR or XER (which can also be processor dependent) can be identified. For example, the newly introduced CA32 bit in XER, exclusive to P9 CPUs as of now, was not being set when expected for some of the arithmetic and shift instructions. With this infrastructure, it will be easier to identify such problems and rectify them. The test cases for the addc[.] instruction demonstrate this for different scenarios where the CA and CA32 bits of XER should be set.
I forgot to mention this in my other emails - I think this is a really good idea, thank you for doing it.
Regards,
Daniel
Quoted 18 lines
Sandipan Das (5):
powerpc: Add bitmasks for D-form instruction fields
powerpc: Add bitmask for Rc instruction field
powerpc: sstep: Add instruction emulation selftests
powerpc: sstep: Add selftests for add[.] instruction
powerpc: sstep: Add selftests for addc[.] instructionarch/powerpc/Kconfig.debug | 5 + arch/powerpc/include/asm/ppc-opcode.h | 5 + arch/powerpc/lib/Makefile | 1 + arch/powerpc/lib/exec_test_instr.S | 150 +++++++ arch/powerpc/lib/sstep_tests.c | 564 ++++++++++++++++++++++++++ 5 files changed, 725 insertions(+) create mode 100644 arch/powerpc/lib/exec_test_instr.S create mode 100644 arch/powerpc/lib/sstep_tests.cSignature · 1 line
2.19.2