Skip to contentSkip to conversation

target/riscv: Bugfix reserved bits in PTE for RV64

PATCH Superseded [email protected] 4 participants 10 messages Sep 25, 2019 RISC-V

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 target/riscv: Bugfix reserved bits in PTE for RV64

From: Guo Ren <[email protected]>

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

---
 target/riscv/cpu_bits.h   | 3 +++
 target/riscv/cpu_helper.c | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)

---
Changelog V3:
- Use UUL define for PTE_RESERVED.
- Keep ppn >> PTE_PPN_SHIFT

Changelog V2:
- Bugfix pte destroyed cause boot fail
- Change to AND with a mask instead of shifting both directions

target/riscv/cpu_bits.h +3 -0
index e998348..cdc62a8 100644
@@ -470,6 +470,9 @@
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
 
+/* Reserved highest 10 bits in PTE */
+#define PTE_RESERVED        0xFFC0000000000000ULL
+
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
 
target/riscv/cpu_helper.c +3 -1
index 87dd6a6..7e04ff5 100644
@@ -258,10 +258,12 @@ restart:
         }
 #if defined(TARGET_RISCV32)
         target_ulong pte = ldl_phys(cs->as, pte_addr);
+        hwaddr ppn = pte;
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
+        hwaddr ppn = pte & ~PTE_RESERVED;
 #endif
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        ppn = ppn >> PTE_PPN_SHIFT;
 
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
Signature · 1 line
2.7.4
#1 lore raw
Quoted 9 lines · On Tue, Sep 24, 2019 at 9:48 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Hey,

As I mentioned on patch 2 I don't think this is right. It isn't up to HW to clear these bits, software should keep them clear.

Alistair

Quoted 51 lines
---
 target/riscv/cpu_bits.h   | 3 +++
 target/riscv/cpu_helper.c | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)

---
Changelog V3:
- Use UUL define for PTE_RESERVED.
- Keep ppn >> PTE_PPN_SHIFT

Changelog V2:
- Bugfix pte destroyed cause boot fail
- Change to AND with a mask instead of shifting both directions

target/riscv/cpu_bits.h +3 -0
index e998348..cdc62a8 100644
@@ -470,6 +470,9 @@
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
 
+/* Reserved highest 10 bits in PTE */
+#define PTE_RESERVED        0xFFC0000000000000ULL
+
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
 
target/riscv/cpu_helper.c +3 -2
index 87dd6a6..7e04ff5 100644
@@ -258,10 +258,12 @@ restart:
         }
 #if defined(TARGET_RISCV32)
         target_ulong pte = ldl_phys(cs->as, pte_addr);
+        hwaddr ppn = pte;
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
+        hwaddr ppn = pte & ~PTE_RESERVED;
 #endif
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        ppn = ppn >> PTE_PPN_SHIFT;
 
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
--

2.7.4

#8 lore raw
In reply to [email protected]
Quoted 3 lines · On Wed, Sep 25, 2019 at 12:49 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

nits: the title is probably better to be rephrased to: Ignore reserved bits when calculating PPN for RV64

Quoted 2 lines

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

nits: cannot

Quoted 5 lines

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Quoted 8 lines
---
 target/riscv/cpu_bits.h   | 3 +++
 target/riscv/cpu_helper.c | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)

---
Changelog V3:

nits: normally we put changelog before the changed file summary above, and there is no need to put another ---

Quoted 12 lines

- Use UUL define for PTE_RESERVED.
- Keep ppn >> PTE_PPN_SHIFT

Changelog V2:
- Bugfix pte destroyed cause boot fail
- Change to AND with a mask instead of shifting both directions

target/riscv/cpu_bits.h +0 -0
index e998348..cdc62a8 100644

target/riscv/cpu_bits.h line 474

Quoted 5 lines
target/riscv/cpu_bits.h +2 -0
@@ -470,6 +470,9 @@
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
 
+/* Reserved highest 10 bits in PTE */
+#define PTE_RESERVED        0xFFC0000000000000ULL

Can we define the macro for RV32 too, so that (see below)

Quoted 9 lines
target/riscv/cpu_bits.h +1 -0
+
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
 
target/riscv/cpu_helper.c +0 -0
index 87dd6a6..7e04ff5 100644

target/riscv/cpu_helper.c restart: · line 266

Quoted 10 lines
target/riscv/cpu_helper.c +3 -1
@@ -258,10 +258,12 @@ restart:
         }
 #if defined(TARGET_RISCV32)
         target_ulong pte = ldl_phys(cs->as, pte_addr);
+        hwaddr ppn = pte;
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
+        hwaddr ppn = pte & ~PTE_RESERVED;
 #endif
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        ppn = ppn >> PTE_PPN_SHIFT;

we can just do this in this single line?

target/riscv/cpu_helper.c restart:

Quoted 4 lines
target/riscv/cpu_helper.c +0 -1
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
--

Regards,
Bin

author
#9 lore raw
Quoted 8 lines · Bin Meng
Quoted 3 lines · On Wed, Sep 25, 2019 at 12:49 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

nits: the title is probably better to be rephrased to: Ignore reserved bits when calculating PPN for RV64

Yes, I forgot change the title.

Quoted 5 lines
Quoted 3 lines

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

nits: cannot

Thx

Quoted 15 lines
Quoted 6 lines

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Quoted 8 lines
---
 target/riscv/cpu_bits.h   | 3 +++
 target/riscv/cpu_helper.c | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)

---
Changelog V3:

Quoted 2 lines

nits: normally we put changelog before the changed file summary above, and there is no need to put another ---

OK, just remove ---

Quoted 16 lines
Quoted 16 lines

- Use UUL define for PTE_RESERVED.
- Keep ppn >> PTE_PPN_SHIFT

Changelog V2:
- Bugfix pte destroyed cause boot fail
- Change to AND with a mask instead of shifting both directions

target/riscv/cpu_bits.h +0 -0
index e998348..cdc62a8 100644
@@ -470,6 +470,9 @@
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
 
Quoted 4 lines

target/riscv/cpu_bits.h line 474

Quoted 2 lines
target/riscv/cpu_bits.h +2 -0
+/* Reserved highest 10 bits in PTE */
+#define PTE_RESERVED        0xFFC0000000000000ULL

Can we define the macro for RV32 too, so that (see below)

OK

Quoted 14 lines
Quoted 14 lines
target/riscv/cpu_bits.h +1 -0
+
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
 
target/riscv/cpu_helper.c +1 -0
index 87dd6a6..7e04ff5 100644
@@ -258,10 +258,12 @@ restart:
         }
 #if defined(TARGET_RISCV32)
         target_ulong pte = ldl_phys(cs->as, pte_addr);
+        hwaddr ppn = pte;
Quoted 8 lines

target/riscv/cpu_helper.c restart: · line 266

Quoted 6 lines
target/riscv/cpu_helper.c +2 -1
 #elif defined(TARGET_RISCV64)
         target_ulong pte = ldq_phys(cs->as, pte_addr);
+        hwaddr ppn = pte & ~PTE_RESERVED;
 #endif
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        ppn = ppn >> PTE_PPN_SHIFT;

we can just do this in this single line?

Yes

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
author
#2 lore raw
In reply to Alistair Francis
Quoted 8 lines · Alistair Francis
Quoted 6 lines · On Tue, Sep 24, 2019 at 9:48 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

Quoted 8 lines
Quoted 3 lines

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Hey,

As I mentioned on patch 2 I don't think this is right. It isn't up to HW to clear these bits, software should keep them clear.

These bits are not part of ppn in spec, so we still need to ignore them for ppn.

The patch is reasonable.

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#3 lore raw

Any code whose behavior is changed by this patch is wrong, so it doesn't seem like it matters much whether this is merged or not. Personally I'd lean towards making sure that attempts to use PTEs with the reserved bit set would always fault, rather than wrapping around to low memory and perhaps silently succeeding.

Jonathan

Quoted 33 lines · Guo Ren
Quoted 6 lines · Alistair Francis
Quoted 4 lines · On Tue, Sep 24, 2019 at 9:48 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so

we

Quoted 3 lines
Quoted 3 lines

need to ignore them. They can not be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged

Architecture

Quoted 7 lines
Quoted 2 lines

4.4 Sv39: Page-Based 39-bit Virtual-Memory System 4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Hey,

As I mentioned on patch 2 I don't think this is right. It isn't up to HW to clear these bits, software should keep them clear.

These bits are not part of ppn in spec, so we still need to ignore them for ppn.

The patch is reasonable.

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

author
#4 lore raw

I think your thoughts are wrong.
The specification is very clear: these bits are not part of ppn, not part of the translation target address. The current code is against the riscv-privilege specification.

Quoted 35 lines · Jonathan Behrens

Any code whose behavior is changed by this patch is wrong, so it doesn't seem like it matters much whether this is merged or not. Personally I'd lean towards making sure that attempts to use PTEs with the reserved bit set would always fault, rather than wrapping around to low memory and perhaps silently succeeding.

Jonathan

Quoted 29 lines · Guo Ren
Quoted 8 lines · Alistair Francis
Quoted 6 lines · On Tue, Sep 24, 2019 at 9:48 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

Highest 10 bits of PTE are reserved in riscv-privileged, ref: [1], so we need to ignore them. They can not be a part of ppn.

Quoted 8 lines
Quoted 3 lines

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

Hey,

As I mentioned on patch 2 I don't think this is right. It isn't up to HW to clear these bits, software should keep them clear.

These bits are not part of ppn in spec, so we still need to ignore them for ppn.

The patch is reasonable.

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#5 lore raw
Quoted 3 lines

The specification is very clear: these bits are not part of ppn, not part of the translation target address. The current code is against the riscv-privilege specification.

If all of the reserved bits are zero then the patch changes nothing. Further the only normative mention of the reserved bits in the spec says they must be: "Bits 63–54 are reserved for future use and must be zeroed by software for forward compatibility." Provided that software follows the spec current QEMU will behave properly. For software that ignores that directive an sets some of those bits, the spec says nothing about what hardware should do, so both the old an the new behavior are fine.

Jonathan

author
#6 lore raw

"Bits 63–54 are reserved for future use and must be zeroed by software for forward compatibility."

That doesn't mean 63-54 are belong to ppn, it's reserved for future and nobody know 63-54 will be part of ppn.
Current riscv qemu ppn implementation is obviously wrong. It shouldn't care the software's behavior, please follow the spec.

Quoted 15 lines · Jonathan Behrens
Quoted 4 lines

The specification is very clear: these bits are not part of ppn, not part of the translation target address. The current code is against the riscv-privilege specification.

If all of the reserved bits are zero then the patch changes nothing. Further the only normative mention of the reserved bits in the spec says they must be: "Bits 63–54 are reserved for future use and must be zeroed by software for forward compatibility." Provided that software follows the spec current QEMU will behave properly. For software that ignores that directive an sets some of those bits, the spec says nothing about what hardware should do, so both the old an the new behavior are fine.

Jonathan

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#7 lore raw
Quoted 8 lines · Guo Ren

"Bits 63–54 are reserved for future use and must be zeroed by software for forward compatibility."

That doesn't mean 63-54 are belong to ppn, it's reserved for future and nobody know 63-54 will be part of ppn.
Current riscv qemu ppn implementation is obviously wrong. It shouldn't care the software's behavior, please follow the spec.

You have convinced me, I think this is an acceptable change.

Alistair

Quoted 25 lines · Jonathan Behrens
Quoted 14 lines
Quoted 4 lines

The specification is very clear: these bits are not part of ppn, not part of the translation target address. The current code is against the riscv-privilege specification.

If all of the reserved bits are zero then the patch changes nothing. Further the only normative mention of the reserved bits in the spec says they must be: "Bits 63–54 are reserved for future use and must be zeroed by software for forward compatibility." Provided that software follows the spec current QEMU will behave properly. For software that ignores that directive an sets some of those bits, the spec says nothing about what hardware should do, so both the old an the new behavior are fine.

Quoted 1 line

Jonathan

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/