Skip to contentSkip to conversation

target/arm: fix PAuth in user mode

PATCH Superseded Rémi Denis-Courmont 3 participants 16 messages Jan 25, 2019 – Feb 1, 2019

lore
Share & tools
lore
#0 lore raw

cover target/arm: fix PAuth in user mode

Hello,

As things stand, ARMv8.3-PAuth does not work in user mode. These tiny patches attempt to fix that.

Br,

----------------------------------------------------------------
Remi Denis-Courmont (3):
target/arm: fix AArch64 virtual address space size target/arm: actually enable PAuth in user mode target/arm: fix decoding of B{,L}RA{A,B}

 target/arm/cpu.h           | 2 +-
 target/arm/cpu64.c         | 4 ++--
 target/arm/translate-a64.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Signature · 2 lines
Rémi Denis-Courmont
http://www.remlab.net/
#1 lore raw

PATCH 1/3 target/arm: fix AArch64 virtual address space size

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is signed 48-bits. User mode can only handle the positive half of the address space, so that makes a limit of 47 bits.

(With LVA, it would be 52 and 51 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

---
 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index ff81db420d..2ccd04b8f7 100644
@@ -2503,7 +2503,7 @@ bool write_cpustate_to_list(ARMCPU *cpu);
 
 #if defined(TARGET_AARCH64)
 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
-#  define TARGET_VIRT_ADDR_SPACE_BITS 64
+#  define TARGET_VIRT_ADDR_SPACE_BITS 47
 #else
 #  define TARGET_PHYS_ADDR_SPACE_BITS 40
 #  define TARGET_VIRT_ADDR_SPACE_BITS 32
Signature · 1 line
2.20.1
#2 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] target/arm: fix AArch64 virtual address space size

Quoted 25 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is signed 48-bits. User mode can only handle the positive half of the address space, so that makes a limit of 47 bits.

(With LVA, it would be 52 and 51 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

---
 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +0 -0
index ff81db420d..2ccd04b8f7 100644

target/arm/cpu.h bool write_cpustate_to_list(ARMCPU *cpu); · line 2509

Quoted 8 lines
target/arm/cpu.h +1 -1
@@ -2503,7 +2503,7 @@ bool write_cpustate_to_list(ARMCPU *cpu);
 
 #if defined(TARGET_AARCH64)
 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
-#  define TARGET_VIRT_ADDR_SPACE_BITS 64
+#  define TARGET_VIRT_ADDR_SPACE_BITS 47
 #else
 #  define TARGET_PHYS_ADDR_SPACE_BITS 40
 #  define TARGET_VIRT_ADDR_SPACE_BITS 32

This doesn't really conflict, as this doesn't affect much besides the sizing of the PageDesc for page_find.

It's true adjusting this is worthwhile. The current setting requires 7 levels (6 * 10 bits + 1 * 4 bits), whereas a 48-bit address space only requires 5 levels (4 * 10 bits + 8 bits). Even for LVA it will be (4 * 10 + 1 * 12 bits).

This will both save memory and reduce the time required for TranlationBlock maintenance.

Your choice of 47 is wrong. This value is also used for system mode, and the kernel does use the negative half of the address space, so we need to have all 48 bits here.

r~

#3 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] target/arm: fix AArch64 virtual address space size

	Hi,
Quoted 33 lines · Le lauantaina 26. tammikuuta 2019, 1.29.27 EET Richard Henderson a écrit :
Quoted 32 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is signed 48-bits. User mode can only handle the positive half of the address space, so that makes a limit of 47 bits.

(With LVA, it would be 52 and 51 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

---

 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index ff81db420d..2ccd04b8f7 100644
@@ -2503,7 +2503,7 @@ bool write_cpustate_to_list(ARMCPU *cpu);
 
 #if defined(TARGET_AARCH64)
 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
 
-#  define TARGET_VIRT_ADDR_SPACE_BITS 64
+#  define TARGET_VIRT_ADDR_SPACE_BITS 47
 
Quoted 6 lines
Quoted 3 lines
target/arm/cpu.h +0 -0
 #else

# define TARGET_PHYS_ADDR_SPACE_BITS 40
# define TARGET_VIRT_ADDR_SPACE_BITS 32

This doesn't really conflict, as this doesn't affect much besides the sizing of the PageDesc for page_find.

Uh, that value controls the range of target virtual addresses user mode maps to host virtual addresses. Without this patch, AFAICT, there are no guarantees, only chance, that emulated system calls such as mmap() will not allocate architecturally invalid addresses.

Quoted 4 lines

It's true adjusting this is worthwhile. The current setting requires 7 levels (6 * 10 bits + 1 * 4 bits), whereas a 48-bit address space only requires 5 levels (4 * 10 bits + 8 bits). Even for LVA it will be (4 * 10
+ 1 * 12 bits).

LVA only works with 64 KiB page size and user mode forces 4 KiB page size at the moment. And I somewhat doubt that somebody would ever need LVA in QEMU user mode. So that's highly hypothetical.

Quoted 4 lines

This will both save memory and reduce the time required for TranlationBlock maintenance.

Your choice of 47 is wrong. This value is also used for system mode,

No, it is not. System mode uses the other value, TARGET_PHYS_ADDR_SPACE_BITS, which this patch leaves untouched.

Quoted 3 lines

and
the kernel does use the negative half of the address space, so we need to have all 48 bits here.

I believe that TARGET_PHYS_ADDR_SPACE_BITS could be reduced to 56 bits. Anything less would presumably break handling of the address "sign" bit 55.

However, that would merely constitute an optimization, as there are no risk of generating architecturally invalid addresses at run-time in system mode.

Signature · 2 lines
Rémi Denis-Courmont
http://www.remlab.net/
#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] target/arm: fix AArch64 virtual address space size

Quoted 2 lines · Le lauantaina 26. tammikuuta 2019, 8.36.00 EET Rémi Denis-Courmont a écrit :

I believe that TARGET_PHYS_ADDR_SPACE_BITS could be reduced to 56 bits. Anything less would presumably break handling of the address "sign" bit 55.

Err, sorry. That is already 48 bits and that is the correct value of course. There is no sign bit in physical address space.

Signature · 2 lines
レミ・デニ-クールモン
http://www.remlab.net/
#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] target/arm: fix AArch64 virtual address space size

In reply to Rémi Denis-Courmont
Quoted 43 lines · Rémi Denis-Courmont
	Hi,
Quoted 33 lines · Le lauantaina 26. tammikuuta 2019, 1.29.27 EET Richard Henderson a écrit :
Quoted 32 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is signed 48-bits. User mode can only handle the positive half of the address space, so that makes a limit of 47 bits.

(With LVA, it would be 52 and 51 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

---

 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index ff81db420d..2ccd04b8f7 100644
@@ -2503,7 +2503,7 @@ bool write_cpustate_to_list(ARMCPU *cpu);
 
 #if defined(TARGET_AARCH64)
 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
 
-#  define TARGET_VIRT_ADDR_SPACE_BITS 64
+#  define TARGET_VIRT_ADDR_SPACE_BITS 47
 
Quoted 6 lines
Quoted 3 lines
target/arm/cpu.h +0 -0
 #else

# define TARGET_PHYS_ADDR_SPACE_BITS 40
# define TARGET_VIRT_ADDR_SPACE_BITS 32

This doesn't really conflict, as this doesn't affect much besides the sizing of the PageDesc for page_find.

Quoted 4 lines

Uh, that value controls the range of target virtual addresses user mode maps to host virtual addresses. Without this patch, AFAICT, there are no guarantees, only chance, that emulated system calls such as mmap() will not allocate architecturally invalid addresses.

Well, that's the theory, yes.

On the plus side, the host platform that virtually everyone uses, x86_64, also uses a 48-bit virtual address space, so there's no chance of such an invalid address.

On the minus side, the code within target_mmap is not sufficient to ensure that mmap will not produce invalid addresses.

See 76393642ae6, where I extended target/alpha TARGET_VIRT_ADDR_SPACE_BITS to 63 to work around exactly this problem. To wit: target_mmap loops forever asking the kernel for an address below 2**40, which the kernel refused to provide.

So changing this value for target/arm runs the risk of causing aarch64-linux-user to fail on a ppc64/s390x/sparc64 host. For all of the tens of people around the world that care about such a configuration.

r~

#6 lore raw

PATCH 2/3 target/arm: actually enable PAuth in user mode

In reply to Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

This always enables IA, IB, DA and DB keys in user mode on the maximum CPU, in a manner that is consistent with the other CPUs. That is to say redefining the reset value of SCTLR_ELx registers.

Without this patch, the PAC* and AUT* instructions have no effects (except PACGA of course).

---
 target/arm/cpu64.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/cpu64.c +2 -2
index e9bc461c36..148c103ca4 100644
@@ -413,8 +413,8 @@ static void aarch64_max_initfn(Object *obj)
                                 (void *)&apdb_bit, &error_fatal);
 
             /* Enable all PAC keys by default.  */
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnIA | SCTLR_EnIB;
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnDA | SCTLR_EnDB;
+            cpu->reset_sctlr |= SCTLR_EnIA | SCTLR_EnIB;
+            cpu->reset_sctlr |= SCTLR_EnDA | SCTLR_EnDB;
         }
 #endif
 
Signature · 1 line
2.20.1
#7 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] target/arm: actually enable PAuth in user mode

Quoted 19 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

This always enables IA, IB, DA and DB keys in user mode on the maximum CPU, in a manner that is consistent with the other CPUs. That is to say redefining the reset value of SCTLR_ELx registers.

Without this patch, the PAC* and AUT* instructions have no effects (except PACGA of course).

---
 target/arm/cpu64.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/cpu64.c +0 -0
index e9bc461c36..148c103ca4 100644

target/arm/cpu64.c static void aarch64_max_initfn(Object *obj) · line 417

Quoted 7 lines
target/arm/cpu64.c +2 -2
@@ -413,8 +413,8 @@ static void aarch64_max_initfn(Object *obj)
                                 (void *)&apdb_bit, &error_fatal);
 
             /* Enable all PAC keys by default.  */
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnIA | SCTLR_EnIB;
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnDA | SCTLR_EnDB;
+            cpu->reset_sctlr |= SCTLR_EnIA | SCTLR_EnIB;
+            cpu->reset_sctlr |= SCTLR_EnDA | SCTLR_EnDB;

I just sent another patch for this:
http://lists.nongnu.org/archive/html/qemu-devel/2019-01/msg06737.html

This way is valid as well, but would also need to adjust the property callbacks to modify reset_sctlr as well.

Peter, do you have a preference?

r~

#8 lore raw

PATCH 3/3 target/arm: fix decoding of B{,L}RA{A,B}

In reply to Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

A flawed test lead to the instructions always being treated as unallocated encodings.

---
 target/arm/translate-a64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/translate-a64.c +1 -1
index 4d28a27c3b..c550a3f013 100644
@@ -2036,7 +2036,7 @@ static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
         if (!dc_isar_feature(aa64_pauth, s)) {
             goto do_unallocated;
         }
-        if (op3 != 2 || op3 != 3) {
+        if ((op3 & ~1) != 2) {
             goto do_unallocated;
         }
         if (s->pauth_active) {
Signature · 1 line
2.20.1
Reviewed-by on v1 3/3
#9 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 3/3] target/arm: fix decoding of B{, L}RA{A, B}

Quoted 9 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

A flawed test lead to the instructions always being treated as unallocated encodings.

---
 target/arm/translate-a64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

r~

#10 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 3/3] target/arm: fix decoding of B{, L}RA{A, B}

Quoted 6 lines · Richard Henderson
Quoted 5 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

A flawed test lead to the instructions always being treated as unallocated encodings.

Quoted 6 lines
Quoted 4 lines
---
 target/arm/translate-a64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

r~

#11 lore raw

PATCH v2 1/3 target/arm: fix AArch64 virtual address space size

In reply to Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is 48-bits plus a sign bit. User mode can only handle the positive half of the address space, so that makes a limit of 48 bits.

(With LVA, it would be 53 and 52 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

---
 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index ff81db420d..a3781600ba 100644
@@ -2503,7 +2503,7 @@ bool write_cpustate_to_list(ARMCPU *cpu);
 
 #if defined(TARGET_AARCH64)
 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
-#  define TARGET_VIRT_ADDR_SPACE_BITS 64
+#  define TARGET_VIRT_ADDR_SPACE_BITS 48
 #else
 #  define TARGET_PHYS_ADDR_SPACE_BITS 40
 #  define TARGET_VIRT_ADDR_SPACE_BITS 32
Signature · 1 line
2.20.1
Reviewed-by on v2 1/3
#12 lore raw

Re: [Qemu-devel] [PATCHv2 1/3] target/arm: fix AArch64 virtual address space size

Quoted 14 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

Since QEMU does not support the ARMv8.2-LVA, Large Virtual Address, extension (yet), the VA address space is 48-bits plus a sign bit. User mode can only handle the positive half of the address space, so that makes a limit of 48 bits.

(With LVA, it would be 53 and 52 bits respectively.)

The incorrectly large address space conflicts with PAuth instructions, which bits 48-54 and 56-63 for the pointer authentication code. This also conflicts with (as yet unsupported by QEMU) data tagging and with the ARMv8.5-MTE extension.

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

r~

#13 lore raw

PATCH v2 2/3 target/arm: actually enable PAuth in user mode

In reply to Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

This always enables IA, IB, DA and DB keys in user mode on the maximum CPU, in a manner that is consistent with the other CPUs. That is to say redefining the reset value of SCTLR_ELx registers.

Without this patch, the PAC* and AUT* instructions have no effects (except PACGA of course).

---
 target/arm/cpu64.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/arm/cpu64.c +5 -5
index e9bc461c36..c8ed943c65 100644
@@ -287,7 +287,7 @@ static void cpu_max_get_packey(Object *obj, Visitor *v, const char *name,
 {
     ARMCPU *cpu = ARM_CPU(obj);
     const uint64_t *bit = opaque;
-    bool enabled = (cpu->env.cp15.sctlr_el[1] & *bit) != 0;
+    bool enabled = (cpu->reset_sctlr & *bit) != 0;
 
     visit_type_bool(v, name, &enabled, errp);
 }
@@ -304,9 +304,9 @@ static void cpu_max_set_packey(Object *obj, Visitor *v, const char *name,
 
     if (!err) {
         if (enabled) {
-            cpu->env.cp15.sctlr_el[1] |= *bit;
+            cpu->reset_sctlr |= *bit;
         } else {
-            cpu->env.cp15.sctlr_el[1] &= ~*bit;
+            cpu->reset_sctlr &= ~*bit;
         }
     }
     error_propagate(errp, err);
@@ -413,8 +413,8 @@ static void aarch64_max_initfn(Object *obj)
                                 (void *)&apdb_bit, &error_fatal);
 
             /* Enable all PAC keys by default.  */
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnIA | SCTLR_EnIB;
-            cpu->env.cp15.sctlr_el[1] |= SCTLR_EnDA | SCTLR_EnDB;
+            cpu->reset_sctlr |= SCTLR_EnIA | SCTLR_EnIB;
+            cpu->reset_sctlr |= SCTLR_EnDA | SCTLR_EnDB;
         }
 #endif
 
Signature · 1 line
2.20.1
Reviewed-by on v2 2/3
#14 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCHv2 2/3] target/arm: actually enable PAuth in user mode

Quoted 6 lines · Rémi Denis-Courmont

From: Remi Denis-Courmont <[email protected]>

This always enables IA, IB, DA and DB keys in user mode on the maximum CPU, in a manner that is consistent with the other CPUs. That is to say redefining the reset value of SCTLR_ELx registers.

Quoted 7 lines

Without this patch, the PAC* and AUT* instructions have no effects (except PACGA of course).

---
 target/arm/cpu64.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

r~

#15 lore raw
In reply to Rémi Denis-Courmont
Quoted 6 lines · Rémi Denis-Courmont

Hello,

As things stand, ARMv8.3-PAuth does not work in user mode. These tiny patches attempt to fix that.

Quoted 7 lines

Br,

----------------------------------------------------------------
Remi Denis-Courmont (3):
target/arm: fix AArch64 virtual address space size target/arm: actually enable PAuth in user mode target/arm: fix decoding of B{,L}RA{A,B}

Thanks for this patchset. I've applied patches 1 (v2) and 3 to my target-arm.next tree. (For patch 2 I prefer the approach in Richard's patchset which deletes the cpu properties entirely.)

PS: for future patchsets, if you do a v2 of a patch it's better to then resend the whole patchset as a fresh email thread (not a followup. That makes our automated patch email handling tooling happier as it can keep the v1 and v2 versions distinct.

thanks
-- PMM