Skip to contentSkip to conversation

xen/arm: AArch32-V8R: Add MPU register definitions

PATCH Superseded Ayan Kumar Halder 5 participants 10 messages Apr 17, 2025 – Apr 29, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: AArch32-V8R: Add MPU register definitions

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>, E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

 xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
 xen/arch/arm/include/asm/mpu.h        |   4 +
 xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
 3 files changed, 198 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h
xen/arch/arm/include/asm/arm32/mpu.h new file +59 -0
new file mode 100644
index 0000000000..4aabd93479
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */
+typedef union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */
+typedef union {
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.
+         */
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */
+        unsigned int limit:26;  /* Limit Address */
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +4 -0
index 77d0566f97..67127149c0 100644
@@ -8,8 +8,12 @@
 
 #if defined(CONFIG_ARM_64)
 # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
 #endif
 
+#define PRENR_MASK  GENMASK(31, 0)
+
 #define MPU_REGION_SHIFT  6
 #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
 #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +135 -0

153 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

Hi Ayan,

Quoted 27 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>, E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

  xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
  xen/arch/arm/include/asm/mpu.h        |   4 +
  xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
  3 files changed, 198 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +0 -0
new file mode 100644
index 0000000000..4aabd93479

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 9 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +9 -0
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

Quoted 13 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */
+typedef union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */
+typedef union {
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

xen/arch/arm/include/asm/arm32/mpu.h line 33

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

xen/arch/arm/include/asm/arm32/mpu.h line 36

Quoted 3 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */
+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

Quoted 101 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +4 -0
index 77d0566f97..67127149c0 100644
@@ -8,8 +8,12 @@
 
  #if defined(CONFIG_ARM_64)
  # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
  #endif
 
+#define PRENR_MASK  GENMASK(31, 0)
+
  #define MPU_REGION_SHIFT  6
  #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
  #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +58 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
  /* CP15 CR0: MPU Type Register */
  #define HMPUIR          p15,4,c0,c0,4
 
+/* CP15 CR6: Protection Region Enable Register */
+#define HPRENR          p15,4,c6,c1,1
+
  /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */
  #define HPRSELR         p15,4,c6,c2,1
  #define HPRBAR          p15,4,c6,c3,0
  #define HPRLAR          p15,4,c6,c8,1
 
+/* CP15 CR6: MPU Protection Region Base/Limit Address Register */
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 10 lines
xen/arch/arm/include/asm/mpu/cpregs.h +10 -0
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
+#define HPRLAR28        p15,5,c6,c14,1
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
  /* Aliases of AArch64 names for use in common code */
  #ifdef CONFIG_ARM_32
  /* Alphabetically... */
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

xen/arch/arm/include/asm/mpu/cpregs.h line 158

Quoted 5 lines
xen/arch/arm/include/asm/mpu/cpregs.h +1 -0
  #define PRSELR_EL2      HPRSELR
+
  #endif /* CONFIG_ARM_32 */
 
  #endif /* __ARM_MPU_CPREGS_H */

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw

Hello all,
This is my first post to xen-devel. Since I'm new, let me give a brief intro about "Why?"

1. I am interested in easing the ongoing burden of repeatedly obtaining safety certification for Xen for eventual use in high assurance operational *systems*. Call this an evergreen certification effort.

2. When I read the ISO26262 and avionics cert standards, I note that there is a system architecture (it has its own "System V" lifecycle) that encompasses a set-of-n hardware items (e.g. fabricated SoC analysis and also FPGA soft IP development) and also a set-of-m software items (e.g., bootloader, hypervisor, guest OSes, apps, containers, etc.). For high assurance, we must evaluate the entire system and not just some of its items.

3. Also from the standards the govern the lifecycle and processes for developing high assurance systems, hardware, and software, maintaining (automated) bidirectional requirements identifier traceability from safety requirements "down" (i.e., through levels of requirements decomposition, into *-architecture level designs, into software unit designs, into software unit code, and into software verification tests at the unit, library, module / architecture, and system level. From my perspective, there's a lot riding on these requirements IDs. I would guess that many in this community would feel concerned about dumping many (hundreds?) of requirements IDs directly into the Xen code base, even if these were located within comments.

4. Because of 1, 2, and 3 (above), when I look to understand Xen requirements sources, my first pass is to remember that Xen is just one software item, and it will need to accept integration requirements flowing in from the system (i.e., via KConfig settings and Device Tree / SDT language) and also from the core types that it relies upon (i.e., scoped down to the operationally-deployed core types including Arm Cortex-R52 during boot, but extending to co-processors it uses and then to IO devices that are used, etc.) Finally, sometimes other software items flow requirements into Xen, e.g., when Linux paravirtualization or virtio implementations are scoped-in.

In summary, finding an agreeable way to embed requirements IDs into the Xen source code seems essential to me, so that we can achieve evergreen high assurance certifications for Xen.

Proposal 1: Rather than start by adding many requirements IDs into comments, we might instead start out by using a less-impactful approach.
I propose that we could hand-pick some C data types (CDTs) that were known to be critically important to Xen's high assurance, and simply (re)name these CDTs, for example, structs and unions.

This proposal is to adopt a naming convention for handpicked safety-relevant CDTs, starting with the first two identified below.

For example in this patch, e.g. in xen/arch/arm/include/asm/arm32/mpu.h, two CDT structs convey the bitfield requirements from the new MPU co-processors. Specifically, in the typedefs "prbar_t" and "prlar_t" no struct tag (name) is used for either of the bitfield struct. I propose adding struct tag names to both, and then leveraging these struct tag names as requirements IDs.

Suggested Naming Convention:
(a) E.g., For prbar_t, tag its bitfield struct with the name "PRBAR_BF"
(b) E.g., For prlar_t, tag its bitfied struct with the name "PRLAR_BF"
(c) Proposed bitfield struct naming convention: <register>_BF
(d) Going forward, for bitfield structs that flow-in safety-relevant requirements from the core's Technical Reference Manual (TRM)
and registers, to use the suffix "_BF" to identify these structs. And to faithfully reuse the TRM's name for the register. (application to structs)
(e) Going forward, for unions that wrap "_BF" structs, to name any union member name corresponding to the "_BF" as "bf" and any union member name corresponding to the whole as "whole". (application to unions that immediately wrap "_BF" structs)

The benefits of systematically following these naming conventions will be:
1) Even simple tools like grep -R can find all "_BF" requirements IDs that flow from hardware registers that have been handpicked due to their safety-relevance.
2) Any CDT-parsing tool such as Doxygen, etc., can automatically find and trace all usages of "_BF" structs at several levels
(a) Dependent structs such as "pr_t" can be automatically traced "up" to their "_BF" ancestor CDTs.
(b) C function argument CDTs can be automatically traced up to any
ancestor "_BF" CDTs, with the implication that the function may be safety-relevant because it accesses one or more "_BF" CDTs.
(c) Simple pointer-to-CDT usages should not obscure any facts that
either the ".bf.*" (accessing a safety-relevant bitfield by name) or the
".whole" (accessing the entire register contents by value) were accessed.
3) This convention will naturally allow the new "_BF" requirements IDs
to flow into all future use / access of these safety-relevant registers, including future verification tests.
4) Also, it will naturally allow the new "<register>_BF" requirements IDs
to be traced "up" into the TRM and automatically de-reference the semantics
and usage and safety-relevant notes about the bitfield, the register, and the larger context of use for that register (e.g., at which instruction will the PMU's enforcement of "HPRBAR10" / "p15,4,c6,c13,0" be activated?)
5) The xen-devel community may find that using this naming convention
provides an easier on-ramp to "start seeing safety and high assurance."
This is because engineers who read any future C source code,
or review any future patch series will see that specific lines in that C source code are indelibly tattooed when they refer to a safety-relevant register. Whether any line of code access the "_BF" register in part or in whole, that will become explicitly visible. The goal is to raise community awareness by flagging access / use.

Thank you!

Sincerely,
--mark

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: Thursday, April 17, 2025 11:55 PM
To: Ayan Kumar Halder <[email protected]>; [email protected]
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Michal Orzel <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v1] xen/arm: AArch32-V8R: Add MPU register definitions

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Hi Ayan,

Quoted 29 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>,
E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

  xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
  xen/arch/arm/include/asm/mpu.h        |   4 +
  xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
  3 files changed, 198 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +0 -0
new file mode 100644
index 0000000000..4aabd93479

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 9 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +9 -0
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

Quoted 13 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */ typedef
+union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */ typedef
+union {
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

xen/arch/arm/include/asm/arm32/mpu.h line 33

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

xen/arch/arm/include/asm/arm32/mpu.h line 36

Quoted 3 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */
+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

Quoted 102 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
(quoted diff) +0 -0

b/xen/arch/arm/include/asm/mpu.h index 77d0566f97..67127149c0 100644

xen/arch/arm/include/asm/mpu.h +4 -0
@@ -8,8 +8,12 @@
 
  #if defined(CONFIG_ARM_64)
  # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
  #endif
 
+#define PRENR_MASK  GENMASK(31, 0)
+
  #define MPU_REGION_SHIFT  6
  #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
  #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +58 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
  /* CP15 CR0: MPU Type Register */
  #define HMPUIR          p15,4,c0,c0,4
 
+/* CP15 CR6: Protection Region Enable Register */
+#define HPRENR          p15,4,c6,c1,1
+
  /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */
  #define HPRSELR         p15,4,c6,c2,1
  #define HPRBAR          p15,4,c6,c3,0
  #define HPRLAR          p15,4,c6,c8,1
 
+/* CP15 CR6: MPU Protection Region Base/Limit Address Register */
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 10 lines
xen/arch/arm/include/asm/mpu/cpregs.h +10 -0
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
+#define HPRLAR28        p15,5,c6,c14,1
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
  /* Aliases of AArch64 names for use in common code */
  #ifdef CONFIG_ARM_32
  /* Alphabetically... */
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

xen/arch/arm/include/asm/mpu/cpregs.h line 158

Quoted 5 lines
xen/arch/arm/include/asm/mpu/cpregs.h +1 -0
  #define PRSELR_EL2      HPRSELR
+
  #endif /* CONFIG_ARM_32 */
 
  #endif /* __ARM_MPU_CPREGS_H */

Cheers,

--
Julien Grall

________________________________
The information contained in this e-mail and any attachments from Parry Labs may contain confidential and/or proprietary information, and is intended only for the named recipient to whom it was originally addressed. If you are not the intended recipient, any disclosure, distribution, or copying of this e-mail or its attachments is strictly prohibited. If you have received this e-mail in error, please notify the sender immediately by return e-mail and permanently delete the e-mail and any attachments.

#4 lore raw
In reply to Julien Grall

Hi Julien,

cc-ed Luca for feedback on specific points.

Quoted 33 lines · Julien Grall

Hi Ayan,

Quoted 30 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>, E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

  xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++ xen/arch/arm/include/asm/mpu.h        |   4 + xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++

  3 files changed, 198 insertions(+)

  create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
new file mode 100644
index 0000000000..4aabd93479
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
Quoted 8 lines

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 6 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +6 -0
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

We can rename this as PRBAR_EL2_XN if this sounds better (cc @Luca) in Luca's patch

The description can be

Refer ARM DDI 0568A.c  ID110520 , E2.2.2

HPRBAR [0:1] Execute Never

Quoted 19 lines
Quoted 19 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +18 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */
+typedef union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */
+typedef union {
+    struct {
Quoted 8 lines
Quoted 6 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +5 -0
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a 

res0 bit for ns.

typo: Arm64.

Ack

Quoted 6 lines
Quoted 2 lines

+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

On Arm64, ns == 0 as it can only support secure mode.

So we can change this on Arm64 as well :-

unsigned int res0:2; /* ns == 0 as only secure mode is supported */

@Luca to clarify

Quoted 3 lines
Quoted 3 lines

+        unsigned int ns:1;     /* Reserved 0 by hardware */ +        unsigned int res0:1;   /* Reserved 0 by hardware */

Then, we can change this on Arm32 as well.

Quoted 3 lines
Quoted 1 line

+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

Ack.

Quoted 13 lines

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 11 lines

+    } reg;
+    uint32_t bits;

xen/arch/arm/include/asm/arm32/mpu.h new file +8 -0
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

No, in arm64 this is

typedef struct {
     prbar_t prbar;
     prlar_t prlar;
} pr_t;

The reason being Arm64 uses unused bits (ie 'pad') to store the type.

Quoted 102 lines
Quoted 102 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +3 -0
index 77d0566f97..67127149c0 100644
@@ -8,8 +8,12 @@
     #if defined(CONFIG_ARM_64)
   # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
   #endif
   +#define PRENR_MASK  GENMASK(31, 0)
+
   #define MPU_REGION_SHIFT  6
   #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
   #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +0 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
   /* CP15 CR0: MPU Type Register */
   #define HMPUIR          p15,4,c0,c0,4

  +/* CP15 CR6: Protection Region Enable Register */

xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+#define HPRENR          p15,4,c6,c1,1
+

  /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */ #define HPRSELR         p15,4,c6,c2,1
  #define HPRBAR          p15,4,c6,c3,0
  #define HPRLAR          p15,4,c6,c8,1
  +/* CP15 CR6: MPU Protection Region Base/Limit Address Register */

xen/arch/arm/include/asm/mpu/cpregs.h +58 -0
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
+#define HPRLAR28        p15,5,c6,c14,1
Quoted 8 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 6 lines
xen/arch/arm/include/asm/mpu/cpregs.h +6 -0
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

This looks tricky. So I will prefer to keep this as it is.

Quoted 13 lines
Quoted 8 lines

+
  /* Aliases of AArch64 names for use in common code */ #ifdef CONFIG_ARM_32
  /* Alphabetically... */
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

Yes , we can have

#define PR{B,L}AR_EL2_(n)          HPR{B,L}AR##n for arm32

#define PR{B,L}AR_EL2_(n)          PR{B,L}AR##n##_EL2

I will send a v2 with these changes. Please have a look at that.

- Ayan

Quoted 8 lines
Quoted 5 lines

  #define PRSELR_EL2      HPRSELR
+
  #endif /* CONFIG_ARM_32 */
    #endif /* __ARM_MPU_CPREGS_H */

Cheers,

#3 lore raw
In reply to Mark Brown

Hi Mark,

Quoted 2 lines · Mark Brown

Hello all,
This is my first post to xen-devel. Since I'm new, let me give a brief intro about "Why?"

Welcome :)

Quoted 21 lines

1. I am interested in easing the ongoing burden of repeatedly obtaining safety certification for Xen for eventual use in high assurance operational *systems*. Call this an evergreen certification effort.

2. When I read the ISO26262 and avionics cert standards, I note that there is a system architecture (it has its own "System V" lifecycle) that encompasses a set-of-n hardware items (e.g. fabricated SoC analysis and also FPGA soft IP development) and also a set-of-m software items (e.g., bootloader, hypervisor, guest OSes, apps, containers, etc.). For high assurance, we must evaluate the entire system and not just some of its items.

3. Also from the standards the govern the lifecycle and processes for developing high assurance systems, hardware, and software, maintaining (automated) bidirectional requirements identifier traceability from safety requirements "down" (i.e., through levels of requirements decomposition, into *-architecture level designs, into software unit designs, into software unit code, and into software verification tests at the unit, library, module / architecture, and system level. From my perspective, there's a lot riding on these requirements IDs. I would guess that many in this community would feel concerned about dumping many (hundreds?) of requirements IDs directly into the Xen code base, even if these were located within comments.

Quoted 8 lines

4. Because of 1, 2, and 3 (above), when I look to understand Xen requirements sources, my first pass is to remember that Xen is just one software item, and it will need to accept integration requirements flowing in from the system (i.e., via KConfig settings and Device Tree / SDT language) and also from the core types that it relies upon (i.e., scoped down to the operationally-deployed core types including Arm Cortex-R52 during boot, but extending to co-processors it uses and then to IO devices that are used, etc.) Finally, sometimes other software items flow requirements into Xen, e.g., when Linux paravirtualization or virtio implementations are scoped-in.

Thanks for the context. This is exciting as Xen finds new use cases in avionics.

However I think the right way to go on this is to send a RFC (request for comments) patch on the coding style.

Especially on ...

Quoted 20 lines

In summary, finding an agreeable way to embed requirements IDs into the Xen source code seems essential to me, so that we can achieve evergreen high assurance certifications for Xen.

Proposal 1: Rather than start by adding many requirements IDs into comments, we might instead start out by using a less-impactful approach.
I propose that we could hand-pick some C data types (CDTs) that were known to be critically important to Xen's high assurance, and simply (re)name these CDTs, for example, structs and unions.

This proposal is to adopt a naming convention for handpicked safety-relevant CDTs, starting with the first two identified below.

For example in this patch, e.g. in xen/arch/arm/include/asm/arm32/mpu.h, two CDT structs convey the bitfield requirements from the new MPU co-processors. Specifically, in the typedefs "prbar_t" and "prlar_t" no struct tag (name) is used for either of the bitfield struct. I propose adding struct tag names to both, and then leveraging these struct tag names as requirements IDs.

Quoted 4 lines

Suggested Naming Convention:
(a) E.g., For prbar_t, tag its bitfield struct with the name "PRBAR_BF"
(b) E.g., For prlar_t, tag its bitfied struct with the name "PRLAR_BF"
(c) Proposed bitfield struct naming convention: <register>_BF

You can send a patch to update https://gitlab.com/xen-project/xen/-/blob/staging/CODING_STYLE?ref_type=heads with the suggested naming convention.

However, we need a rationale for the above proposal. So..

Quoted 9 lines

(d) Going forward, for bitfield structs that flow-in safety-relevant requirements from the core's Technical Reference Manual (TRM)
and registers, to use the suffix "_BF" to identify these structs. And to faithfully reuse the TRM's name for the register. (application to structs)
(e) Going forward, for unions that wrap "_BF" structs, to name any union member name corresponding to the "_BF" as "bf" and any union member name corresponding to the whole as "whole". (application to unions that immediately wrap "_BF" structs)

Quoted 13 lines

The benefits of systematically following these naming conventions will be:
1) Even simple tools like grep -R can find all "_BF" requirements IDs that flow from hardware registers that have been handpicked due to their safety-relevance.
2) Any CDT-parsing tool such as Doxygen, etc., can automatically find and trace all usages of "_BF" structs at several levels
(a) Dependent structs such as "pr_t" can be automatically traced "up" to their "_BF" ancestor CDTs.
(b) C function argument CDTs can be automatically traced up to any ancestor "_BF" CDTs, with the implication that the function may be safety-relevant because it accesses one or more "_BF" CDTs.
(c) Simple pointer-to-CDT usages should not obscure any facts that either the ".bf.*" (accessing a safety-relevant bitfield by name) or the
".whole" (accessing the entire register contents by value) were accessed.

This should be a part of the commit message. I would avoid adding any mention of requirements or req-ids in the coding style. You can put this in the cover letter to explain what is the long term view on this.

In short I am suggesting that the changes suggested for the coding style should be concise, so the maintainers can give their opinion.

Also if you can prepare a proof of concept with a sample Doxygen report, that might be helpful.

What might help the maintainers is to understand :-

1. What is the minimum change you are suggesting ?

Proposed bitfield struct naming convention: <register>_BF ....

2. How is it going to be helpful to the community in general (eg better code readability, ease of review, ease of mantainance)

"<register>_BF" can be linked to the specific section of Architecture manual within the code itself. It helps to review, mantain and update the code.

Quoted 7 lines

3) This convention will naturally allow the new "_BF" requirements IDs to flow into all future use / access of these safety-relevant registers, including future verification tests.
4) Also, it will naturally allow the new "<register>_BF" requirements IDs to be traced "up" into the TRM and automatically de-reference the semantics and usage and safety-relevant notes about the bitfield, the register, and the larger context of use for that register (e.g., at which instruction will the PMU's

Quoted 8 lines

enforcement of "HPRBAR10" / "p15,4,c6,c13,0" be activated?)
5) The xen-devel community may find that using this naming convention
provides an easier on-ramp to "start seeing safety and high assurance."
This is because engineers who read any future C source code,
or review any future patch series will see that specific lines in that C source code are indelibly tattooed when they refer to a safety-relevant register. Whether any line of code access the "_BF" register in part or in whole, that will become explicitly visible. The goal is to raise community awareness by flagging access / use.

You can mention this as part of the commit message.

Also it will be helpful if you can pick up an existing file (eg anything arch/arm64/mmu ) to explain the concept. As the new code is in flux, so it make things easier to understand the proposed coding style changes in the existing code.

You can take a look at https://lists.xen.org/archives/html/xen-devel/2022-05/msg01995.html to understand how we propose new changes to the coding style.

Hope this helps.

- Ayan

Quoted 244 lines

Thank you!

Sincerely,
--mark

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: Thursday, April 17, 2025 11:55 PM
To: Ayan Kumar Halder <[email protected]>; [email protected]
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Michal Orzel <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v1] xen/arm: AArch32-V8R: Add MPU register definitions

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Hi Ayan,

Quoted 29 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>,
E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

   xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
   xen/arch/arm/include/asm/mpu.h        |   4 +
   xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
   3 files changed, 198 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +0 -0
new file mode 100644
index 0000000000..4aabd93479

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 9 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +9 -0
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

Quoted 13 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */ typedef
+union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */ typedef
+union {
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

xen/arch/arm/include/asm/arm32/mpu.h line 33

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

xen/arch/arm/include/asm/arm32/mpu.h line 36

Quoted 3 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */
+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

Quoted 102 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
(quoted diff) +0 -0

b/xen/arch/arm/include/asm/mpu.h index 77d0566f97..67127149c0 100644

xen/arch/arm/include/asm/mpu.h +4 -0
@@ -8,8 +8,12 @@
 
   #if defined(CONFIG_ARM_64)
   # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
   #endif
 
+#define PRENR_MASK  GENMASK(31, 0)
+
   #define MPU_REGION_SHIFT  6
   #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
   #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +58 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
   /* CP15 CR0: MPU Type Register */
   #define HMPUIR          p15,4,c0,c0,4
 
+/* CP15 CR6: Protection Region Enable Register */
+#define HPRENR          p15,4,c6,c1,1
+
   /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */
   #define HPRSELR         p15,4,c6,c2,1
   #define HPRBAR          p15,4,c6,c3,0
   #define HPRLAR          p15,4,c6,c8,1
 
+/* CP15 CR6: MPU Protection Region Base/Limit Address Register */
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 10 lines
xen/arch/arm/include/asm/mpu/cpregs.h +10 -0
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
+#define HPRLAR28        p15,5,c6,c14,1
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
   /* Aliases of AArch64 names for use in common code */
   #ifdef CONFIG_ARM_32
   /* Alphabetically... */
   #define MPUIR_EL2       HMPUIR
   #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

xen/arch/arm/include/asm/mpu/cpregs.h line 158

Quoted 5 lines
xen/arch/arm/include/asm/mpu/cpregs.h +1 -0
   #define PRSELR_EL2      HPRSELR
+
   #endif /* CONFIG_ARM_32 */
 
   #endif /* __ARM_MPU_CPREGS_H */

Cheers,

--
Julien Grall

________________________________
The information contained in this e-mail and any attachments from Parry Labs may contain confidential and/or proprietary information, and is intended only for the named recipient to whom it was originally addressed. If you are not the intended recipient, any disclosure, distribution, or copying of this e-mail or its attachments is strictly prohibited. If you have received this e-mail in error, please notify the sender immediately by return e-mail and permanently delete the e-mail and any attachments.

#5 lore raw
In reply to Ayan Kumar Halder

Hi Ayan

Quoted 21 lines · Ayan Kumar Halder
Quoted 2 lines
Quoted 2 lines

+        unsigned int ns:1;     /* Reserved 0 by hardware */ +        unsigned int res0:1;   /* Reserved 0 by hardware */

Then, we can change this on Arm32 as well.

Quoted 3 lines
Quoted 1 line

+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

Ack.

Quoted 13 lines

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 11 lines

+    } reg;
+    uint32_t bits;

xen/arch/arm/include/asm/arm32/mpu.h new file +8 -0
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

Quoted 8 lines

No, in arm64 this is

typedef struct {
     prbar_t prbar;
     prlar_t prlar;
} pr_t;

The reason being Arm64 uses unused bits (ie 'pad') to store the type.

Hmmm... They are bits from the system register (prbar), right? If so, you can't use them for storing the p2m_type as AFAICT they are RES0.

You could possibly mask the bits before writing them. But then, it will become risky if the fields are defined.

Also, the number of MPU regions is fairly small. So, I don't think it is worth it to store p2m_type.

Regardless that, I think it would be better to defer the introduction of p2m_type until guest support is added. So it will be easier to understand how this is going to be used and the size (for instance, I doubt 64-bit is necessary...).

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw
In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 49 lines · Ayan Kumar Halder

Hi Julien,

cc-ed Luca for feedback on specific points.

Quoted 33 lines · Julien Grall

Hi Ayan,

Quoted 30 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>, E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

  xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
  xen/arch/arm/include/asm/mpu.h        |   4 +
  xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
  3 files changed, 198 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
new file mode 100644
index 0000000000..4aabd93479
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
Quoted 8 lines

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 6 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +6 -0
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

Quoted 1 line

We can rename this as PRBAR_EL2_XN if this sounds better (cc @Luca) in Luca's patch

what about PRBAR_EL2_XN_ENABLED? I can change it in my serie

Quoted 41 lines

The description can be

Refer ARM DDI 0568A.c ID110520 , E2.2.2

HPRBAR [0:1] Execute Never

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +17 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */
+typedef union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */
+typedef union {
Quoted 8 lines

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 6 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +6 -0
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

Ack

Quoted 6 lines

xen/arch/arm/include/asm/arm32/mpu.h line 33

Quoted 2 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

Quoted 7 lines

On Arm64, ns == 0 as it can only support secure mode.

So we can change this on Arm64 as well :-

unsigned int res0:2; /* ns == 0 as only secure mode is supported */

@Luca to clarify

From the specifications: "Non-secure bit. Specifies whether the output address is in the Secure or Non-secure memory”, I’m not sure that we should remove it from Arm64, so I don’t think you should have something only for compatibility, maybe the code accessing .ns can be compiled out for Arm32 or we can have arch-specific implementation. I think you are referring to pr_of_xenaddr when you say “compatibility with Arm64 code"

Quoted 157 lines
Quoted 4 lines

xen/arch/arm/include/asm/arm32/mpu.h line 35

Quoted 4 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +2 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */

Then, we can change this on Arm32 as well.

Quoted 3 lines

xen/arch/arm/include/asm/arm32/mpu.h line 36

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

Ack.

Quoted 13 lines

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 11 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

No, in arm64 this is

typedef struct {
     prbar_t prbar;
     prlar_t prlar;
} pr_t;

The reason being Arm64 uses unused bits (ie 'pad') to store the type.

Quoted 102 lines
Quoted 102 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +3 -0
index 77d0566f97..67127149c0 100644
@@ -8,8 +8,12 @@
    #if defined(CONFIG_ARM_64)
  # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
  #endif
  +#define PRENR_MASK  GENMASK(31, 0)
+
  #define MPU_REGION_SHIFT  6
  #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
  #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +60 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
  /* CP15 CR0: MPU Type Register */
  #define HMPUIR          p15,4,c0,c0,4
  +/* CP15 CR6: Protection Region Enable Register */
+#define HPRENR          p15,4,c6,c1,1
+
  /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */
  #define HPRSELR         p15,4,c6,c2,1
  #define HPRBAR          p15,4,c6,c3,0
  #define HPRLAR          p15,4,c6,c8,1
  +/* CP15 CR6: MPU Protection Region Base/Limit Address Register */
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
+#define HPRLAR28        p15,5,c6,c14,1
Quoted 8 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 6 lines
xen/arch/arm/include/asm/mpu/cpregs.h +6 -0
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

This looks tricky. So I will prefer to keep this as it is.

Quoted 13 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 8 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
  /* Aliases of AArch64 names for use in common code */
  #ifdef CONFIG_ARM_32
  /* Alphabetically... */
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

Quoted 5 lines

Yes , we can have

#define PR{B,L}AR_EL2_(n) HPR{B,L}AR##n for arm32

#define PR{B,L}AR_EL2_(n) PR{B,L}AR##n##_EL2

we could have them in mm.c, I see in your v2 you’ve put them in cpregs.h, but since they are only used by the generator, I would put them there.

#6 lore raw
In reply to Julien Grall
Quoted 34 lines · Julien Grall

Hi Ayan

Quoted 20 lines · Ayan Kumar Halder
Quoted 2 lines

xen/arch/arm/include/asm/arm32/mpu.h line 35

Quoted 2 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +2 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */

Then, we can change this on Arm32 as well.

Quoted 3 lines
Quoted 1 line

+ unsigned int limit:26; /* Limit Address */

NIT: Can we align the comments?

Ack.

Quoted 13 lines

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 11 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

Quoted 6 lines

No, in arm64 this is

typedef struct {
     prbar_t prbar;
     prlar_t prlar;
} pr_t;

The reason being Arm64 uses unused bits (ie 'pad') to store the type.

Quoted 3 lines

Hmmm... They are bits from the system register (prbar), right? If so, you can't use them for storing the p2m_type as AFAICT they are RES0.

You could possibly mask the bits before writing them.

This was the approach taken in the Arm64, I think the reason was because in this way the overall maximum structure was less than a page and it was easier to manage something, I’ll see if I can find what, I think it was related to p2m...

Quoted 11 lines

But then, it will become risky if the fields are defined.

Also, the number of MPU regions is fairly small. So, I don't think it is worth it to store p2m_type.

Regardless that, I think it would be better to defer the introduction of p2m_type until guest support is added. So it will be easier to understand how this is going to be used and the size (for instance, I doubt 64-bit is necessary...).

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw
In reply to Luca Fancellu
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 48 lines
Quoted 48 lines · Ayan Kumar Halder

Hi Julien,

cc-ed Luca for feedback on specific points.

Quoted 40 lines · Julien Grall

Hi Ayan,

Quoted 27 lines · Ayan Kumar Halder

Add the definitions for HPRBAR<0..31>, HPRLAR<0..31> and HPRENR. The definitions are taken from ARM DDI 0568A.c ID110520, E2.2.3 HPRBAR<n>, E2.2.4 HPRENR and E2.2.6 HPRLAR<n>.

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch32-V8R architecture. This is the arm32 equivalent of
"arm/mpu: Introduce MPU memory region map structure".

---
This patch should be applied after
"[PATCH v3 0/7] First chunk for Arm R82 and MPU support" in order to enable compilation for AArch32.

   xen/arch/arm/include/asm/arm32/mpu.h  |  59 +++++++++++
   xen/arch/arm/include/asm/mpu.h        |   4 +
   xen/arch/arm/include/asm/mpu/cpregs.h | 135 ++++++++++++++++++++++++++
   3 files changed, 198 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h

xen/arch/arm/include/asm/arm32/mpu.h new file +0 -0
new file mode 100644
index 0000000000..4aabd93479

xen/arch/arm/include/asm/arm32/mpu.h line 9

Quoted 9 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +9 -0
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM32_MPU_H
+#define __ARM_ARM32_MPU_H
+
+#define XN_EL2_ENABLED  0x1

I understand the define is introduced in Luca's patch, but this a bit non-descriptive... Can we find a better name? Maybe by adding the name of the register and some documentation?

Quoted 2 lines
Quoted 1 line

We can rename this as PRBAR_EL2_XN if this sounds better (cc @Luca) in Luca's patch

what about PRBAR_EL2_XN_ENABLED? I can change it in my serie

I am ok with the name.

Quoted 40 lines
Quoted 40 lines

The description can be

Refer ARM DDI 0568A.c ID110520 , E2.2.2

HPRBAR [0:1] Execute Never

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +16 -0
+
+#ifndef __ASSEMBLY__
+
+/* Hypervisor Protection Region Base Address Register */
+typedef union {
+    struct {
+        unsigned int xn:1;       /* Execute-Never */
+        unsigned int ap:2;       /* Acess Permission */
+        unsigned int sh:2;       /* Sharebility */
+        unsigned int res0:1;     /* Reserved as 0 */
+        unsigned int base:26;    /* Base Address */
+    } reg;
+    uint32_t bits;
+} prbar_t;
+
+/* Hypervisor Protection Region Limit Address Register */
Quoted 8 lines

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 7 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +7 -0
+typedef union {
+    struct {
+        unsigned int en:1;     /* Region enable */
+        unsigned int ai:3;     /* Memory Attribute Index */
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

Ack

Quoted 4 lines

xen/arch/arm/include/asm/arm32/mpu.h line 33

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+         */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

On Arm64, ns == 0 as it can only support secure mode.

So we can change this on Arm64 as well :-

Quoted 7 lines
Quoted 3 lines

unsigned int res0:2; /* ns == 0 as only secure mode is supported */

@Luca to clarify

From the specifications: "Non-secure bit. Specifies whether the output address is in the Secure or Non-secure memory”, I’m not sure
that we should remove it from Arm64, so I don’t think you should have something only for compatibility, maybe the code accessing .ns can be compiled out for Arm32 or we can have arch-specific implementation. I think you are referring to pr_of_xenaddr when you say “compatibility with Arm64 code"

Yes, that is correct. So are you saying that we should have an "ifdef" in the function.

+++ b/xen/arch/arm/mpu/mm.c
@@ -221,7 +221,9 @@ pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) /* Build up value for PRLAR_EL2. */
     prlar = (prlar_t) {
         .reg = {
+#ifdef CONFIG_ARM_64
             .ns = 0,        /* Hyp mode is in secure world */ +#endif
             .ai = attr,
             .en = 1,        /* Region enabled */
         }};

I am ok with this. I just want to know if you and Julien are aligned as well.

Quoted 147 lines
Quoted 147 lines
Quoted 3 lines

xen/arch/arm/include/asm/arm32/mpu.h line 35

Quoted 3 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +2 -0
+        unsigned int ns:1;     /* Reserved 0 by hardware */
+        unsigned int res0:1;   /* Reserved 0 by hardware */

Then, we can change this on Arm32 as well.

Quoted 2 lines

xen/arch/arm/include/asm/arm32/mpu.h line 36

Quoted 1 line
xen/arch/arm/include/asm/arm32/mpu.h new file +1 -0
+        unsigned int limit:26;  /* Limit Address */

NIT: Can we align the comments?

Ack.

Quoted 11 lines

xen/arch/arm/include/asm/arm32/mpu.h line 46

Quoted 10 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +10 -0
+    } reg;
+    uint32_t bits;
+} prlar_t;
+
+/* Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+    uint64_t p2m_type;          /* Used to store p2m types. */
+} pr_t;

This looks to be common with arm64. If so, I would prefer if the structure is in a common header.

No, in arm64 this is

typedef struct {
      prbar_t prbar;
      prlar_t prlar;
} pr_t;

The reason being Arm64 uses unused bits (ie 'pad') to store the type.

Quoted 100 lines
Quoted 100 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +13 -0
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +3 -0
index 77d0566f97..67127149c0 100644
@@ -8,8 +8,12 @@
     #if defined(CONFIG_ARM_64)
   # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
   #endif
   +#define PRENR_MASK  GENMASK(31, 0)
+
   #define MPU_REGION_SHIFT  6
   #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
   #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
xen/arch/arm/include/asm/mpu/cpregs.h +59 -0
index d5cd0e04d5..7cf52aa09a 100644
@@ -6,18 +6,153 @@
   /* CP15 CR0: MPU Type Register */
   #define HMPUIR          p15,4,c0,c0,4
   +/* CP15 CR6: Protection Region Enable Register */
+#define HPRENR          p15,4,c6,c1,1
+
   /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */
   #define HPRSELR         p15,4,c6,c2,1
   #define HPRBAR          p15,4,c6,c3,0
   #define HPRLAR          p15,4,c6,c8,1
   +/* CP15 CR6: MPU Protection Region Base/Limit Address Register */
+#define HPRBAR0         p15,4,c6,c8,0
+#define HPRLAR0         p15,4,c6,c8,1
+#define HPRBAR1         p15,4,c6,c8,4
+#define HPRLAR1         p15,4,c6,c8,5
+#define HPRBAR2         p15,4,c6,c9,0
+#define HPRLAR2         p15,4,c6,c9,1
+#define HPRBAR3         p15,4,c6,c9,4
+#define HPRLAR3         p15,4,c6,c9,5
+#define HPRBAR4         p15,4,c6,c10,0
+#define HPRLAR4         p15,4,c6,c10,1
+#define HPRBAR5         p15,4,c6,c10,4
+#define HPRLAR5         p15,4,c6,c10,5
+#define HPRBAR6         p15,4,c6,c11,0
+#define HPRLAR6         p15,4,c6,c11,1
+#define HPRBAR7         p15,4,c6,c11,4
+#define HPRLAR7         p15,4,c6,c11,5
+#define HPRBAR8         p15,4,c6,c12,0
+#define HPRLAR8         p15,4,c6,c12,1
+#define HPRBAR9         p15,4,c6,c12,4
+#define HPRLAR9         p15,4,c6,c12,5
+#define HPRBAR10        p15,4,c6,c13,0
+#define HPRLAR10        p15,4,c6,c13,1
+#define HPRBAR11        p15,4,c6,c13,4
+#define HPRLAR11        p15,4,c6,c13,5
+#define HPRBAR12        p15,4,c6,c14,0
+#define HPRLAR12        p15,4,c6,c14,1
+#define HPRBAR13        p15,4,c6,c14,4
+#define HPRLAR13        p15,4,c6,c14,5
+#define HPRBAR14        p15,4,c6,c15,0
+#define HPRLAR14        p15,4,c6,c15,1
+#define HPRBAR15        p15,4,c6,c15,4
+#define HPRLAR15        p15,4,c6,c15,5
+#define HPRBAR16        p15,5,c6,c8,0
+#define HPRLAR16        p15,5,c6,c8,1
+#define HPRBAR17        p15,5,c6,c8,4
+#define HPRLAR17        p15,5,c6,c8,5
+#define HPRBAR18        p15,5,c6,c9,0
+#define HPRLAR18        p15,5,c6,c9,1
+#define HPRBAR19        p15,5,c6,c9,4
+#define HPRLAR19        p15,5,c6,c9,5
+#define HPRBAR20        p15,5,c6,c10,0
+#define HPRLAR20        p15,5,c6,c10,1
+#define HPRBAR21        p15,5,c6,c10,4
+#define HPRLAR21        p15,5,c6,c10,5
+#define HPRBAR22        p15,5,c6,c11,0
+#define HPRLAR22        p15,5,c6,c11,1
+#define HPRBAR23        p15,5,c6,c11,4
+#define HPRLAR23        p15,5,c6,c11,5
+#define HPRBAR24        p15,5,c6,c12,0
+#define HPRLAR24        p15,5,c6,c12,1
+#define HPRBAR25        p15,5,c6,c12,4
+#define HPRLAR25        p15,5,c6,c12,5
+#define HPRBAR26        p15,5,c6,c13,0
+#define HPRLAR26        p15,5,c6,c13,1
+#define HPRBAR27        p15,5,c6,c13,4
+#define HPRLAR27        p15,5,c6,c13,5
+#define HPRBAR28        p15,5,c6,c14,0
Quoted 8 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 81

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +7 -0
+#define HPRLAR28        p15,5,c6,c14,1
+#define HPRBAR29        p15,5,c6,c14,4
+#define HPRLAR29        p15,5,c6,c14,5
+#define HPRBAR30        p15,5,c6,c15,0
+#define HPRLAR30        p15,5,c6,c15,1
+#define HPRBAR31        p15,5,c6,c15,4
+#define HPRLAR31        p15,5,c6,c15,5

NIT: Is there any way we could generate the values using macros?

This looks tricky. So I will prefer to keep this as it is.

Quoted 11 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
   /* Aliases of AArch64 names for use in common code */
   #ifdef CONFIG_ARM_32
   /* Alphabetically... */
   #define MPUIR_EL2       HMPUIR
   #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

Quoted 7 lines
Quoted 5 lines

Yes , we can have

#define PR{B,L}AR_EL2_(n) HPR{B,L}AR##n for arm32

#define PR{B,L}AR_EL2_(n) PR{B,L}AR##n##_EL2

we could have them in mm.c, I see in your v2 you’ve put them in cpregs.h, but since they are only used by the generator, I would put them there.

You mean the above two macros should be moved to mm.c. I am ok with that.

Just 2 more things to be aligned :-

1. Are we ok to use PRBAR_EL2_(num) and PRLAR_EL2_(num) in the common code (ie mpu/mm.c) ?

2. Are you ok to introduce ifdef in prepare_selector() ?

Please have a look at my v2 for reference.

- Ayan

Quoted 3 lines
#9 lore raw

Hi Ayan,

Quoted 35 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

xen/arch/arm/include/asm/arm32/mpu.h line 32

Quoted 3 lines
xen/arch/arm/include/asm/arm32/mpu.h new file +3 -0
+        /*
+         * There is no actual ns bit in hardware. It is used here for
+         * compatibility with Armr64 code. Thus, we are reusing a res0 bit for ns.

typo: Arm64.

Ack

Quoted 4 lines
Quoted 1 line

+ */

Hmmmm, this would mean someone may mistakenly set the bit to 0 by mistake. If the field is always meant to be 0 on arm64, then I would consider to name is res0 on arm64 with an explanation.

This would make clear the bit is not supposed to have a value other than 0.

On Arm64, ns == 0 as it can only support secure mode.

So we can change this on Arm64 as well :-

Quoted 7 lines
Quoted 3 lines

unsigned int res0:2; /* ns == 0 as only secure mode is supported */

@Luca to clarify

From the specifications: "Non-secure bit. Specifies whether the output address is in the Secure or Non-secure memory”, I’m not sure that we should remove it from Arm64, so I don’t think you should have something only for compatibility, maybe the code accessing .ns can be compiled out for Arm32 or we can have arch-specific implementation. I think you are referring to pr_of_xenaddr when you say “compatibility with Arm64 code"

Yes, that is correct. So are you saying that we should have an "ifdef" in the function.

+++ b/xen/arch/arm/mpu/mm.c

(quoted diff) +2 -0
@@ -221,7 +221,9 @@ pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr)
     /* Build up value for PRLAR_EL2. */
     prlar = (prlar_t) {
         .reg = {
+#ifdef CONFIG_ARM_64
             .ns = 0,        /* Hyp mode is in secure world */
+#endif
             .ai = attr,
             .en = 1,        /* Region enabled */
         }};
Quoted 1 line

I am ok with this. I just want to know if you and Julien are aligned as well.

this is my proposal, yes

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 2 lines
Quoted 1 line

NIT: Is there any way we could generate the values using macros?

This looks tricky. So I will prefer to keep this as it is.

Quoted 11 lines

xen/arch/arm/include/asm/mpu/cpregs.h line 88

Quoted 7 lines
xen/arch/arm/include/asm/mpu/cpregs.h +2 -0
+
  /* Aliases of AArch64 names for use in common code */
  #ifdef CONFIG_ARM_32
  /* Alphabetically... */
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
+#define PRBAR0_EL2      HPRBAR0

AFAIU, the alias will be mainly used in the macro generate the switch. Rather than open-coding all the PR*AR_EL2, can we provide two macros PR{B, L}AR_N that will be implemented as HPR{B,L}AR##n for arm32 and PR{B,L}AR##n for arm64?

Quoted 7 lines
Quoted 5 lines

Yes , we can have

#define PR{B,L}AR_EL2_(n) HPR{B,L}AR##n for arm32

#define PR{B,L}AR_EL2_(n) PR{B,L}AR##n##_EL2

we could have them in mm.c, I see in your v2 you’ve put them in cpregs.h, but since they are only used by the generator, I would put them there.

You mean the above two macros should be moved to mm.c. I am ok with that.

Quoted 7 lines

Just 2 more things to be aligned :-

1. Are we ok to use PRBAR_EL2_(num) and PRLAR_EL2_(num) in the common code (ie mpu/mm.c) ?

2. Are you ok to introduce ifdef in prepare_selector() ?

Please have a look at my v2 for reference.

I will change my implementation to introduce on Arm64 PR{B,L}AR_EL2_(num) -> PR{B,L}AR##num##_EL2, I will then protect everything with CONFIG_ARM_64, this will ensure proper compilation on both architecture.

When you will introduce your changes, you will have only to revert the #ifdef CONFIG_ARM_64 that protects the common code and implement the changes to build on Arm32.

I think this is the best way to ensure we are not blocked by each other while keeping the churn as low as possible.

Please @julien and/or other maintainers let me know your opinion on this.

Cheers,
Luca