s390x: Add misaligned instruction tests
cover s390x: Add misaligned instruction tests
Instructions on s390 must be halfword aligned.
Add two tests for that.
These currently fail when using TCG.
Nina Schoetterl-Glausch (2):
s390x/spec_ex: Add test introducing odd address into PSW s390x/spec_ex: Add test of EXECUTE with odd target address
s390x/spec_ex.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 4 deletions(-)
base-commit: 7e9737739f738c9a2e555947082a59edbc5b49b9
Signature · 1 line
2.36.1
PATCH 1/2 s390x/spec_ex: Add test introducing odd address into PSW
Instructions on s390 must be halfword aligned.
Introducing an odd instruction address into the PSW leads to a specification exception when attempting to execute the instruction at the odd address.
Add a test for this.
--- s390x/spec_ex.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-)
s390x/spec_ex.c +69 -4
@@ -44,9 +44,10 @@ static void fixup_invalid_psw(struct stack_frame_int *stack)/* * Load possibly invalid psw, but setup fixup_psw before, * so that fixup_invalid_psw() can bring us back onto the right track. +* The provided argument is loaded into register 1. * Also acts as compiler barrier, -> none required in expect/check_invalid_psw */ -static void load_psw (struct psw psw) +static void load_psw_with_arg (struct psw psw,uint64_t arg) {uint64_t scratch;
@@ -57,15 +58,22 @@ static void load_psw(struct psw psw) fixup_psw.mask =extract_psw_mask (); asmvolatile ("larl %[scratch],0f\n" " stg %[scratch],%[fixup_addr]\n" +" lgr %%r1,%[arg]\n" " lpswe %[psw]\n" "0: nop\n" : [scratch]"=&d" (scratch), [fixup_addr]"=&T" (fixup_psw.addr) - : [psw]"Q" (psw) - :"cc" ,"memory" + : [psw]"Q" (psw), + [arg]"d" (arg) + :"cc" ,"memory" ,"%r1" ); } +static void load_psw (struct psw psw) +{ +load_psw_with_arg (psw,0 ); +} +static void load_short_psw (struct short_psw psw) {uint64_t scratch;
@@ -88,12 +96,18 @@ static void expect_invalid_psw(struct psw psw) invalid_psw_expected = true; } +static void clear_invalid_psw (void ) +{ + expected_psw = (struct psw ){0 }; + invalid_psw_expected = false; +} +static int check_invalid_psw (void ) {/* Since the fixup sets this to false we check for false here. */ if (!invalid_psw_expected) {if (expected_psw.mask == invalid_psw.mask && - expected_psw.addr == invalid_psw.addr) + expected_psw.addr == invalid_psw.addr - lowcore.pgm_int_id)return 0 ;report_fail ("Wrong invalid PSW" ); }else {
@@ -115,6 +129,56 @@ static int psw_bit_12_is_1(void)return check_invalid_psw (); } +static int psw_odd_address (void ) +{ +struct psw odd = { + .mask =extract_psw_mask (), + }; +uint64_t regs[16 ]; +int r; + +/* +* This asm is reentered at an odd address, which should cause a specification +* exception before the first unaligned instruction is executed. +* In this case, the interrupt handler fixes the address and the test succeeds. +* If, however, unaligned instructions *are* executed, they are jumped to +* from somewhere, with unknown registers, so save and restore those before. +*/ + asmvolatile ("stmg %%r0,%%r15,%[regs]\n" +//can only offset by even number when using larl -> increment by one +" larl %[r],0f\n" +" aghi %[r],1\n" +" stg %[r],%[addr]\n" +" xr %[r],%[r]\n" +" brc 0xf,1f\n" +"0: . = . + 1\n" +" lmg %%r0,%%r15,0(%%r1)\n" +//address of the instruction itself, should be odd, store for assert +" larl %[r],0\n" +" stg %[r],%[addr]\n" +" larl %[r],0f\n" +" aghi %[r],1\n" +" bcr 0xf,%[r]\n" +"0: . = . + 1\n" +"1:\n" + : [addr]"=T" (odd.addr), + [regs]"=Q" (regs), + [r]"=d" (r) + : :"cc" ,"memory" + ); + +if (!r) { +expect_invalid_psw (odd); +load_psw_with_arg (odd, (uint64_t )®s); +return check_invalid_psw (); + }else { +assert (odd.addr &1 ); +clear_invalid_psw (); +report_fail ("executed unaligned instructions" ); +return 1 ; + } +} +/* A short PSW needs to have bit 12 set to be valid. */ static int short_psw_bit_12_is_0 (void ) {
@@ -176,6 +240,7 @@ struct spec_ex_trigger {static const struct spec_ex_trigger spec_ex_triggers[] = { {"psw_bit_12_is_1" , &psw_bit_12_is_1, false, &fixup_invalid_psw }, {"short_psw_bit_12_is_0" , &short_psw_bit_12_is_0, false, &fixup_invalid_psw }, + {"psw_odd_address" , &psw_odd_address, false, &fixup_invalid_psw }, {"bad_alignment" , &bad_alignment, true,NULL }, {"not_even" , ¬_even, true,NULL }, {NULL ,NULL , false,NULL },
Signature · 1 line
2.36.1
Re: [kvm-unit-tests PATCH v1 1/2] s390x/spec_ex: Add test introducing odd address into PSW
Quoted 53 lines · On Wed, 15 Feb 2023 18:18:51 +0100 Nina Schoetterl-Glausch <[email protected]>…
Instructions on s390 must be halfword aligned.
Introducing an odd instruction address into the PSW leads to a specification exception when attempting to execute the instruction at the odd address.
Add a test for this.Signed-off-by Nina Schoetterl-Glausch <[email protected]>--- s390x/spec_ex.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-)s390x/spec_ex.c +11
-3@@ -44,9 +44,10 @@ static void fixup_invalid_psw(struct stack_frame_int *stack)/* * Load possibly invalid psw, but setup fixup_psw before, * so that fixup_invalid_psw() can bring us back onto the right track. +* The provided argument is loaded into register 1. * Also acts as compiler barrier, -> none required in expect/check_invalid_psw */ -static void load_psw (struct psw psw) +static void load_psw_with_arg (struct psw psw,uint64_t arg) {uint64_t scratch;@@ -57,15 +58,22 @@ static void load_psw(struct psw psw) fixup_psw.mask =extract_psw_mask (); asmvolatile ("larl %[scratch],0f\n" " stg %[scratch],%[fixup_addr]\n" +" lgr %%r1,%[arg]\n" " lpswe %[psw]\n" "0: nop\n" : [scratch]"=&d" (scratch), [fixup_addr]"=&T" (fixup_psw.addr) - : [psw]"Q" (psw) - :"cc" ,"memory" + : [psw]"Q" (psw), + [arg]"d" (arg) + :"cc" ,"memory" ,"%r1" ); } +static void load_psw (struct psw psw) +{ +load_psw_with_arg (psw,0 ); +} +static void load_short_psw (struct short_psw psw) {uint64_t scratch;
s390x/spec_ex.c static void expect_invalid_psw(struct psw psw) · line 101
Quoted 6 lines
s390x/spec_ex.c +3
-0@@ -88,12 +96,18 @@ static void expect_invalid_psw(struct psw psw) invalid_psw_expected = true; } +static void clear_invalid_psw (void ) +{ + expected_psw = (struct psw ){0 };
as of today, you can use PSW(0, 0) :)
s390x/spec_ex.c static void expect_invalid_psw(struct psw psw) · line 110
Quoted 10 lines
s390x/spec_ex.c +4
-1+ invalid_psw_expected = false; +} +static int check_invalid_psw (void ) {/* Since the fixup sets this to false we check for false here. */ if (!invalid_psw_expected) {if (expected_psw.mask == invalid_psw.mask && - expected_psw.addr == invalid_psw.addr) + expected_psw.addr == invalid_psw.addr - lowcore.pgm_int_id)
can you explain this change?
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 134
Quoted 10 lines
s390x/spec_ex.c +3
-0return 0 ;report_fail ("Wrong invalid PSW" ); }else {@@ -115,6 +129,56 @@ static int psw_bit_12_is_1(void)return check_invalid_psw (); } +static int psw_odd_address (void ) +{ +struct psw odd = {
now you can use PSW_WITH_CUR_MASK(0) here
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 146
Quoted 12 lines
s390x/spec_ex.c +12
-0+ .mask = extract_psw_mask(), + }; +uint64_t regs[16 ]; +int r; + +/* +* This asm is reentered at an odd address, which should cause a specification +* exception before the first unaligned instruction is executed. +* In this case, the interrupt handler fixes the address and the test succeeds. +* If, however, unaligned instructions *are* executed, they are jumped to +* from somewhere, with unknown registers, so save and restore those before. +*/
I wonder if this could be simplified
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 151
Quoted 5 lines
s390x/spec_ex.c +5
-0+ asmvolatile ("stmg %%r0,%%r15,%[regs]\n" +//can only offset by even number when using larl -> increment by one +" larl %[r],0f\n" +" aghi %[r],1\n" + " stg %[r],%[addr]\n"
the above is ok (set up illegal PSW)
(maybe call expect_invalid_psw here, see comments below)
put the address of the exit label in a register
then do a lpswe here to jump to the invalid PSW
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 153
Quoted 2 lines
s390x/spec_ex.c +2
-0+" xr %[r],%[r]\n" +" brc 0xf,1f\n"
then do the above. that will only happen if the PSW was not loaded.
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 154
Quoted 1 line
s390x/spec_ex.c +1
-0+"0: . = . + 1\n"
if we are here, things went wrong.
write something in r, jump to the exit label (using the address in the register that we saved earlier)
Quoted 5 lines
s390x/spec_ex.c +5
-0+" lmg %%r0,%%r15,0(%%r1)\n" +//address of the instruction itself, should be odd, store for assert +" larl %[r],0\n" +" stg %[r],%[addr]\n" +" larl %[r],0f\n"
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 169
Quoted 10 lines
s390x/spec_ex.c +10
-0+" aghi %[r],1\n" +" bcr 0xf,%[r]\n" +"0: . = . + 1\n" +"1:\n" + : [addr] "=T" (odd.addr), + [regs] "=Q" (regs), + [r] "=d" (r) + : : "cc", "memory" + ); +
if we come out here and r is 0, then things went well, otherwise we fail.
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 171
Quoted 2 lines
s390x/spec_ex.c +2
-0+if (!r) { +expect_invalid_psw (odd);
that ^ should probably go before the asm (or _in_ the asm, maybe you can call the C function from asm)
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 172
Quoted 1 line
s390x/spec_ex.c +1
-0+load_psw_with_arg (odd, (uint64_t )®s);
this would not be needed anymore ^
this way you don't need to save registers or do crazy things where you jump back in the middle of the asm from C code. and then you don't even need load_psw_with_arg
Quoted 20 lines
s390x/spec_ex.c +10
-0+return check_invalid_psw (); + }else { +assert (odd.addr &1 ); +clear_invalid_psw (); +report_fail ("executed unaligned instructions" ); +return 1 ; + } +} +/* A short PSW needs to have bit 12 set to be valid. */ static int short_psw_bit_12_is_0 (void ) {@@ -176,6 +240,7 @@ struct spec_ex_trigger {static const struct spec_ex_trigger spec_ex_triggers[] = { {"psw_bit_12_is_1" , &psw_bit_12_is_1, false, &fixup_invalid_psw }, {"short_psw_bit_12_is_0" , &short_psw_bit_12_is_0, false, &fixup_invalid_psw }, + {"psw_odd_address" , &psw_odd_address, false, &fixup_invalid_psw }, {"bad_alignment" , &bad_alignment, true,NULL }, {"not_even" , ¬_even, true,NULL }, {NULL ,NULL , false,NULL },
Re: [kvm-unit-tests PATCH v1 1/2] s390x/spec_ex: Add test introducing odd address into PSW
Quoted 65 lines · Claudio Imbrenda
Quoted 53 lines · On Wed, 15 Feb 2023 18:18:51 +0100 Nina Schoetterl-Glausch <[email protected]>…
Instructions on s390 must be halfword aligned.
Introducing an odd instruction address into the PSW leads to a specification exception when attempting to execute the instruction at the odd address.
Add a test for this.Signed-off-by Nina Schoetterl-Glausch <[email protected]>--- s390x/spec_ex.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-)s390x/spec_ex.c +11
-3@@ -44,9 +44,10 @@ static void fixup_invalid_psw(struct stack_frame_int *stack)/* * Load possibly invalid psw, but setup fixup_psw before, * so that fixup_invalid_psw() can bring us back onto the right track. +* The provided argument is loaded into register 1. * Also acts as compiler barrier, -> none required in expect/check_invalid_psw */ -static void load_psw (struct psw psw) +static void load_psw_with_arg (struct psw psw,uint64_t arg) {uint64_t scratch;@@ -57,15 +58,22 @@ static void load_psw(struct psw psw) fixup_psw.mask =extract_psw_mask (); asmvolatile ("larl %[scratch],0f\n" " stg %[scratch],%[fixup_addr]\n" +" lgr %%r1,%[arg]\n" " lpswe %[psw]\n" "0: nop\n" : [scratch]"=&d" (scratch), [fixup_addr]"=&T" (fixup_psw.addr) - : [psw]"Q" (psw) - :"cc" ,"memory" + : [psw]"Q" (psw), + [arg]"d" (arg) + :"cc" ,"memory" ,"%r1" ); } +static void load_psw (struct psw psw) +{ +load_psw_with_arg (psw,0 ); +} +static void load_short_psw (struct short_psw psw) {uint64_t scratch;s390x/spec_ex.c static void expect_invalid_psw(struct psw psw) · line 101
Quoted 6 lines
s390x/spec_ex.c +3
-0@@ -88,12 +96,18 @@ static void expect_invalid_psw(struct psw psw) invalid_psw_expected = true; } +static void clear_invalid_psw (void ) +{ + expected_psw = (struct psw ){0 };as of today, you can use
PSW(0, 0):)
Quoted 12 lines
s390x/spec_ex.c static void expect_invalid_psw(struct psw psw) · line 110
Quoted 10 lines
s390x/spec_ex.c +4
-1+ invalid_psw_expected = false; +} +static int check_invalid_psw (void ) {/* Since the fixup sets this to false we check for false here. */ if (!invalid_psw_expected) {if (expected_psw.mask == invalid_psw.mask && - expected_psw.addr == invalid_psw.addr) + expected_psw.addr == invalid_psw.addr - lowcore.pgm_int_id)can you explain this change?
In the existing invalid PSW tests, the instruction length code is 0, so no change there. In case of an odd address being introduced into the PSW, the address is incremented by an unpredictable amount, the subtraction removes that.
Quoted 79 lines
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 134
Quoted 11 lines
s390x/spec_ex.c +3
-0return 0 ;report_fail ("Wrong invalid PSW" ); }else {@@ -115,6 +129,56 @@ static int psw_bit_12_is_1(void)return check_invalid_psw (); } +static int psw_odd_address (void ) +{ +struct psw odd = {now you can use
PSW_WITH_CUR_MASK(0)heres390x/spec_ex.c static int psw_bit_12_is_1(void) · line 146
Quoted 12 lines
s390x/spec_ex.c +12
-0+ .mask = extract_psw_mask(), + }; +uint64_t regs[16 ]; +int r; + +/* +* This asm is reentered at an odd address, which should cause a specification +* exception before the first unaligned instruction is executed. +* In this case, the interrupt handler fixes the address and the test succeeds. +* If, however, unaligned instructions *are* executed, they are jumped to +* from somewhere, with unknown registers, so save and restore those before. +*/ I wonder if this could be simplified
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 151
Quoted 5 lines
s390x/spec_ex.c +5
-0+ asmvolatile ("stmg %%r0,%%r15,%[regs]\n" +//can only offset by even number when using larl -> increment by one +" larl %[r],0f\n" +" aghi %[r],1\n" + " stg %[r],%[addr]\n"the above is ok (set up illegal PSW)
(maybe call
expect_invalid_pswhere, see comments below)put the address of the exit label in a register
then do a lpswe here to jump to the invalid PSW
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 153
Quoted 2 lines
s390x/spec_ex.c +2
-0+" xr %[r],%[r]\n" +" brc 0xf,1f\n" then do the above. that will only happen if the PSW was not loaded.
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 154
Quoted 1 line
s390x/spec_ex.c +1
-0+"0: . = . + 1\n" if we are here, things went wrong.
write something in r, jump to the exit label (using the address in the register that we saved earlier)Quoted 5 lines
s390x/spec_ex.c +5
-0+" lmg %%r0,%%r15,0(%%r1)\n" +//address of the instruction itself, should be odd, store for assert +" larl %[r],0\n" +" stg %[r],%[addr]\n" +" larl %[r],0f\n" s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 169
Quoted 10 lines
s390x/spec_ex.c +10
-0+" aghi %[r],1\n" +" bcr 0xf,%[r]\n" +"0: . = . + 1\n" +"1:\n" + : [addr] "=T" (odd.addr), + [regs] "=Q" (regs), + [r] "=d" (r) + : : "cc", "memory" + ); +if we come out here and r is 0, then things went well, otherwise we fail.
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 171
Quoted 2 lines
s390x/spec_ex.c +2
-0+if (!r) { +expect_invalid_psw (odd);that ^ should probably go before the asm (or _in_ the asm, maybe you can call the C function from asm)
Quoted 9 lines
s390x/spec_ex.c static int psw_bit_12_is_1(void) · line 172
Quoted 1 line
s390x/spec_ex.c +1
-0+load_psw_with_arg (odd, (uint64_t )®s);this would not be needed anymore ^
this way you don't need to save registers or do crazy things where you jump back in the middle of the asm from C code. and then you don't even need
load_psw_with_arg
I'll see what I can do.
Quoted 22 lines
Quoted 21 lines
s390x/spec_ex.c +10
-0+return check_invalid_psw (); + }else { +assert (odd.addr &1 ); +clear_invalid_psw (); +report_fail ("executed unaligned instructions" ); +return 1 ; + } +} +/* A short PSW needs to have bit 12 set to be valid. */ static int short_psw_bit_12_is_0 (void ) {@@ -176,6 +240,7 @@ struct spec_ex_trigger {static const struct spec_ex_trigger spec_ex_triggers[] = { {"psw_bit_12_is_1" , &psw_bit_12_is_1, false, &fixup_invalid_psw }, {"short_psw_bit_12_is_0" , &short_psw_bit_12_is_0, false, &fixup_invalid_psw }, + {"psw_odd_address" , &psw_odd_address, false, &fixup_invalid_psw }, {"bad_alignment" , &bad_alignment, true,NULL }, {"not_even" , ¬_even, true,NULL }, {NULL ,NULL , false,NULL },
PATCH 2/2 s390x/spec_ex: Add test of EXECUTE with odd target address
In reply to Nina Schoetterl-GlauschThe EXECUTE instruction executes the instruction at the given target address. This address must be halfword aligned, otherwise a specification exception occurs.
Add a test for this.
--- s390x/spec_ex.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
s390x/spec_ex.c +25 -0
@@ -200,6 +200,30 @@ static int short_psw_bit_12_is_0(void)return 0 ; } +static int odd_ex_target (void ) +{ +uint64_t target_addr_pre; +int to =0 , from =0x0dd ; + + asmvolatile (".pushsection .rodata\n" +"odd_ex_target_pre_insn:\n" +" .balign 2\n" +" . = . + 1\n" +" lr %[to],%[from]\n" +" .popsection\n" + +" larl %[target_addr_pre],odd_ex_target_pre_insn\n" +" ex 0,1(%[target_addr_pre])\n" + : [target_addr_pre]"=&a" (target_addr_pre), + [to]"+d" (to) + : [from]"d" (from) + ); + +assert ((target_addr_pre +1 ) &1 ); +report (to != from,"did not perform ex with odd target" ); +return 0 ; +} +static int bad_alignment (void ) { uint32_t words[5] __attribute__((aligned(16)));
@@ -241,6 +265,7 @@ static const struct spec_ex_trigger spec_ex_triggers[] = { { "psw_bit_12_is_1", &psw_bit_12_is_1, false, &fixup_invalid_psw }, { "short_psw_bit_12_is_0", &short_psw_bit_12_is_0, false, &fixup_invalid_psw }, { "psw_odd_address", &psw_odd_address, false, &fixup_invalid_psw }, + { "odd_ex_target", &odd_ex_target, true, NULL }, { "bad_alignment", &bad_alignment, true, NULL }, { "not_even", ¬_even, true, NULL }, { NULL, NULL, false, NULL },
Signature · 1 line
2.36.1
Re: [kvm-unit-tests PATCH v1 2/2] s390x/spec_ex: Add test of EXECUTE with odd target address
Quoted 6 lines · On Wed, 15 Feb 2023 18:18:52 +0100 Nina Schoetterl-Glausch <[email protected]>…
The EXECUTE instruction executes the instruction at the given target address. This address must be halfword aligned, otherwise a specification exception occurs.
Add a test for this.Signed-off-by Nina Schoetterl-Glausch <[email protected]>
Quoted 47 lines
--- s390x/spec_ex.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)s390x/spec_ex.c +25
-0@@ -200,6 +200,30 @@ static int short_psw_bit_12_is_0(void)return 0 ; } +static int odd_ex_target (void ) +{ +uint64_t target_addr_pre; +int to =0 , from =0x0dd ; + + asmvolatile (".pushsection .rodata\n" +"odd_ex_target_pre_insn:\n" +" .balign 2\n" +" . = . + 1\n" +" lr %[to],%[from]\n" +" .popsection\n" + +" larl %[target_addr_pre],odd_ex_target_pre_insn\n" +" ex 0,1(%[target_addr_pre])\n" + : [target_addr_pre]"=&a" (target_addr_pre), + [to]"+d" (to) + : [from]"d" (from) + ); + +assert ((target_addr_pre +1 ) &1 ); +report (to != from,"did not perform ex with odd target" ); +return 0 ; +} +static int bad_alignment (void ) { uint32_t words[5] __attribute__((aligned(16)));@@ -241,6 +265,7 @@ static const struct spec_ex_trigger spec_ex_triggers[] = { { "psw_bit_12_is_1", &psw_bit_12_is_1, false, &fixup_invalid_psw }, { "short_psw_bit_12_is_0", &short_psw_bit_12_is_0, false, &fixup_invalid_psw }, { "psw_odd_address", &psw_odd_address, false, &fixup_invalid_psw }, + { "odd_ex_target", &odd_ex_target, true, NULL }, { "bad_alignment", &bad_alignment, true, NULL }, { "not_even", ¬_even, true, NULL }, { NULL, NULL, false, NULL },