Skip to contentSkip to conversation

target/i386: fix missing PF_INSTR in SIGSEGV context

PATCH Applied Simon Scherer 3 participants 4 messages Apr 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386: fix missing PF_INSTR in SIGSEGV context

When running linux-user emulation, the SIGSEGV handler does not correctly set the 4th bit (PF_INSTR) in the error_code variable of the context argument (context->uc_mcontext.gregs[REG_ERR]).

Because this bit is never set, guest applications cannot distinguish if a fault was due to missing executable permissions. This patch ensures that when a page fault occurs during an instruction fetch, the PF_INSTR flag is properly populated in the signal context.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3384

---
 VERSION                            | 2 +-
 target/i386/tcg/user/excp_helper.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
VERSION +1 -1
index de760e732f..340ebebb29 100644
@@ -1 +1 @@
-10.2.92
+10.2.93
target/i386/tcg/user/excp_helper.c +4 -3
index 98fab4cbc3..6c5df5e0e8 100644
@@ -36,9 +36,10 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,
      * signal and set exception_index to EXCP_INTERRUPT.
      */
     env->cr[2] = addr;
-    env->error_code = ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)
-                    | (maperr ? 0 : PG_ERROR_P_MASK)
-                    | PG_ERROR_U_MASK;
+    env->error_code = (maperr ? 0 : PG_ERROR_P_MASK)
+                    | ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)
+                    | PG_ERROR_U_MASK
+                    | ((access_type == MMU_INST_FETCH) ? PG_ERROR_I_D_MASK : 0);
     cs->exception_index = EXCP0E_PAGE;
 
     /* Disable do_interrupt_user. */
Signature · 1 line
2.53.0
#1 lore raw
Quoted 22 lines · Simon Scherer

When running linux-user emulation, the SIGSEGV handler does not correctly set the 4th bit (PF_INSTR) in the error_code variable of the context argument (context->uc_mcontext.gregs[REG_ERR]).

Because this bit is never set, guest applications cannot distinguish if a fault was due to missing executable permissions. This patch ensures that when a page fault occurs during an instruction fetch, the PF_INSTR flag is properly populated in the signal context.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3384

---
 VERSION                            | 2 +-
 target/i386/tcg/user/excp_helper.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
VERSION +0 -0
index de760e732f..340ebebb29 100644

VERSION line 1

Quoted 2 lines
VERSION +1 -1
@@ -1 +1 @@
-10.2.92
+10.2.93

Your patch seems to have a stray other change in it.

You don't need to respin it just to fix this, and if it gets reviewed without needing any other changes then the maintainer will probably fix that up, but if you need to do a v2 of the patch then you can sort it out for that.

target/i386/tcg/user/excp_helper.c void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr, · line 40

Quoted 13 lines
target/i386/tcg/user/excp_helper.c +2 -3
index 98fab4cbc3..6c5df5e0e8 100644
@@ -36,9 +36,10 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,
      * signal and set exception_index to EXCP_INTERRUPT.
      */
     env->cr[2] = addr;
-    env->error_code = ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)
-                    | (maperr ? 0 : PG_ERROR_P_MASK)
-                    | PG_ERROR_U_MASK;
+    env->error_code = (maperr ? 0 : PG_ERROR_P_MASK)
+                    | ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)

Why did you reorder these two lines ? Is there a code change here I'm missing?

target/i386/tcg/user/excp_helper.c void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr, · line 45

Quoted 5 lines
target/i386/tcg/user/excp_helper.c +2 -0
+                    | PG_ERROR_U_MASK
+                    | ((access_type == MMU_INST_FETCH) ? PG_ERROR_I_D_MASK : 0);
     cs->exception_index = EXCP0E_PAGE;
 
     /* Disable do_interrupt_user. */

thanks
-- PMM

#2 lore raw
Quoted 10 lines · Peter Maydell

VERSION line 1

Quoted 7 lines
VERSION +1 -1
index de760e732f..340ebebb29 100644
@@ -1 +1 @@
-10.2.92
+10.2.93

Your patch seems to have a stray other change in it.

Quoted 4 lines

You don't need to respin it just to fix this, and if it gets reviewed without needing any other changes then the maintainer will probably fix that up, but if you need to do a v2 of the patch then you can sort it out for that.

Will do.

Quoted 7 lines
Quoted 7 lines
target/i386/tcg/user/excp_helper.c +0 -0
index 98fab4cbc3..6c5df5e0e8 100644
@@ -36,9 +36,10 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,
       * signal and set exception_index to EXCP_INTERRUPT.
       */
Quoted 8 lines

target/i386/tcg/user/excp_helper.c void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr, · line 40

Quoted 6 lines
target/i386/tcg/user/excp_helper.c +2 -3
      env->cr[2] = addr;
-    env->error_code = ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)
-                    | (maperr ? 0 : PG_ERROR_P_MASK)
-                    | PG_ERROR_U_MASK;
+    env->error_code = (maperr ? 0 : PG_ERROR_P_MASK)
+                    | ((access_type == MMU_DATA_STORE) << PG_ERROR_W_BIT)

Why did you reorder these two lines ? Is there a code change here I'm missing?

It sorts bits in the order they appear in the word, so it makes sense.

Paolo

Quoted 10 lines

target/i386/tcg/user/excp_helper.c void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr, · line 45

Quoted 5 lines
target/i386/tcg/user/excp_helper.c +2 -0
+                    | PG_ERROR_U_MASK
+                    | ((access_type == MMU_INST_FETCH) ? PG_ERROR_I_D_MASK : 0);
      cs->exception_index = EXCP0E_PAGE;
 
      /* Disable do_interrupt_user. */

thanks
-- PMM