Skip to contentSkip to conversation

target/arm: Allow user-mode code to write CPSR.E via MSR

PATCH In review Peter Maydell 4 participants 4 messages May 15, 2020 – May 18, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm: Allow user-mode code to write CPSR.E via MSR

Using the MSR instruction to write to CPSR.E is deprecated, but it is required to work from any mode including unprivileged code. We were incorrectly forbidding usermode code from writing it because CPSR_USER did not include the CPSR_E bit.

We use CPSR_USER in only three places:
* as the mask of what to allow userspace MSR to write to CPSR
* when deciding what bits a linux-user signal-return should be able to write from the sigcontext structure
* in target_user_copy_regs() when we set up the initial registers for the linux-user process

In the first two cases not being able to update CPSR.E is a bug, and in the third case it doesn't matter because CPSR.E is always 0 there. So we can fix both bugs by adding CPSR_E to CPSR_EXEC.

(The recommended way to change CPSR.E is to use the 'SETEND' instruction, which we do correctly allow from usermode code.)

---
Bug reported on IRC. Quick-and-dirty test case at:
https://people.linaro.org/~peter.maydell/msr-setend.c

 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index 5d995368d4f..677584e5da0 100644
@@ -1230,7 +1230,7 @@ void pmu_init(ARMCPU *cpu);
 #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
     | CPSR_NZCV)
 /* Bits writable in user mode.  */
-#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
+#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE | CPSR_E)
 /* Execution state bits.  MRS read as zero, MSR writes ignored.  */
 #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Peter Maydell

Using the MSR instruction to write to CPSR.E is deprecated, but it is required to work from any mode including unprivileged code. We were incorrectly forbidding usermode code from writing it because CPSR_USER did not include the CPSR_E bit.

We use CPSR_USER in only three places:
* as the mask of what to allow userspace MSR to write to CPSR
* when deciding what bits a linux-user signal-return should be able to write from the sigcontext structure
* in target_user_copy_regs() when we set up the initial registers for the linux-user process

Quoted 4 lines

In the first two cases not being able to update CPSR.E is a bug, and in the third case it doesn't matter because CPSR.E is always 0 there. So we can fix both bugs by adding CPSR_E to CPSR_EXEC.

Wrong variable in description here.

Otherwise,

r~

Quoted 26 lines

(The recommended way to change CPSR.E is to use the 'SETEND' instruction, which we do correctly allow from usermode code.)

---
Bug reported on IRC. Quick-and-dirty test case at:
https://people.linaro.org/~peter.maydell/msr-setend.c

 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index 5d995368d4f..677584e5da0 100644
@@ -1230,7 +1230,7 @@ void pmu_init(ARMCPU *cpu);
 #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
     | CPSR_NZCV)
 /* Bits writable in user mode.  */
-#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
+#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE | CPSR_E)
 /* Execution state bits.  MRS read as zero, MSR writes ignored.  */
 #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
 
#2 lore raw
Quoted 13 lines · Richard Henderson
Quoted 12 lines · Peter Maydell

Using the MSR instruction to write to CPSR.E is deprecated, but it is required to work from any mode including unprivileged code. We were incorrectly forbidding usermode code from writing it because CPSR_USER did not include the CPSR_E bit.

We use CPSR_USER in only three places:
* as the mask of what to allow userspace MSR to write to CPSR
* when deciding what bits a linux-user signal-return should be able to write from the sigcontext structure
* in target_user_copy_regs() when we set up the initial registers for the linux-user process

Quoted 6 lines
Quoted 4 lines

In the first two cases not being able to update CPSR.E is a bug, and in the third case it doesn't matter because CPSR.E is always 0 there. So we can fix both bugs by adding CPSR_E to CPSR_EXEC.

Wrong variable in description here.

Indeed CPSR_EXEC -> CPSR_USER typo.

Quoted 14 lines

Otherwise,

r~

Quoted 7 lines

(The recommended way to change CPSR.E is to use the 'SETEND' instruction, which we do correctly allow from usermode code.)

---
Bug reported on IRC.

Similar to commit a1ecb4381829d7:

Quoted 6 lines

Quick-and-dirty test case at:

Quoted 5 lines

https://people.linaro.org/~peter.maydell/msr-setend.c

  target/arm/cpu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

target/arm/cpu.h void pmu_init(ARMCPU *cpu); · line 1233

Quoted 10 lines
target/arm/cpu.h +1 -1
index 5d995368d4f..677584e5da0 100644
@@ -1230,7 +1230,7 @@ void pmu_init(ARMCPU *cpu);
  #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
      | CPSR_NZCV)
  /* Bits writable in user mode.  */
-#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
+#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE | CPSR_E)
Quoted 6 lines
Quoted 4 lines
target/arm/cpu.h +0 -0
  /* Execution state bits.  MRS read as zero, MSR writes ignored.  */
  #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
 
#3 lore raw

Thank you Philippe and the QEMU team!

--Randy

Quoted 68 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Richard Henderson
Quoted 12 lines · Peter Maydell

Using the MSR instruction to write to CPSR.E is deprecated, but it is required to work from any mode including unprivileged code. We were incorrectly forbidding usermode code from writing it because CPSR_USER did not include the CPSR_E bit.

We use CPSR_USER in only three places:
* as the mask of what to allow userspace MSR to write to CPSR
* when deciding what bits a linux-user signal-return should be able to write from the sigcontext structure
* in target_user_copy_regs() when we set up the initial registers for the linux-user process

Quoted 6 lines
Quoted 4 lines

In the first two cases not being able to update CPSR.E is a bug, and in the third case it doesn't matter because CPSR.E is always 0 there. So we can fix both bugs by adding CPSR_E to CPSR_EXEC.

Wrong variable in description here.

Indeed CPSR_EXEC -> CPSR_USER typo.

Quoted 14 lines

Otherwise,

r~

Quoted 7 lines

(The recommended way to change CPSR.E is to use the 'SETEND' instruction, which we do correctly allow from usermode code.)

---
Bug reported on IRC.

Similar to commit a1ecb4381829d7:

Quoted 6 lines

Quick-and-dirty test case at:

Quoted 5 lines

https://people.linaro.org/~peter.maydell/msr-setend.c

  target/arm/cpu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

target/arm/cpu.h void pmu_init(ARMCPU *cpu); · line 1233

Quoted 10 lines
target/arm/cpu.h +1 -1
index 5d995368d4f..677584e5da0 100644
@@ -1230,7 +1230,7 @@ void pmu_init(ARMCPU *cpu);
  #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
      | CPSR_NZCV)
  /* Bits writable in user mode.  */
-#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
+#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE | CPSR_E)
Quoted 6 lines
Quoted 4 lines
target/arm/cpu.h +0 -0
  /* Execution state bits.  MRS read as zero, MSR writes ignored.  */
  #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
 
Signature · 3 lines
Randy Yates, DSP/Embedded Firmware Developer
Digital Signal Labs
http://www.digitalsignallabs.com