Skip to contentSkip to conversation

RISCV: enable DOMAIN_BUILD_HELPERS

PATCH Superseded Oleksii Kurochko 3 participants 39 messages Apr 28 – May 7 RISC-V

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v4 cover RISCV: enable DOMAIN_BUILD_HELPERS

Introduce necessary things to enable DOMAIN_BUILD_HELPERS config for RISC-V.

This patch series is rebased on top of [PATCH v3 0/3] dom0less: various updates as it could be some merge conflicts depends on which patch series will go first.

CI: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2477740734

---
Changes in v4:
- Move patch up, closer to other common/Arm related patches:
- xen/dom0less: rename kernel_zimage_probe() to kernel_image_probe()
- xen: move declaration of fw_unreserved_regions() to common header
- xen: move domain_use_host_layout() to common header
- xen: rename p2m_ipa_bits to p2m_gpa_bits

- Merged to upstream/staging:
xen/riscv: implement get_page_from_gfn()

- Rebase this patch series on top of [PATCH v3 0/3] dom0less: various updates.
- Address the comments from ML.
---
Changes in v3:
- Address the comments from ML.
---
Changes in v2:
- Address the comments from ML.
- Introduce some new patches to make dom0less solution more architecture indepenent from terminology point of view.
- Minor fixes.
---

Oleksii Kurochko (11):
xen: arm: fix len type for guest copy functions xen/dom0less: rename kernel_zimage_probe() to kernel_image_probe()
xen: move declaration of fw_unreserved_regions() to common header
xen: introduce domain-layout.h with common domain_use_host_layout()
xen: rename p2m_ipa_bits to p2m_gpa_bits
xen/riscv: implement copy_to_guest_phys()
xen/riscv: add Linux kernel loading support
xen/riscv: rework G-stage mode handling
xen/riscv: introduce p2m_gpa_bits
xen/riscv: add definition of guest RAM banks
xen/riscv: enable DOMAIN_BUILD_HELPERS

 xen/arch/arm/domain_build.c               |   1 +
 xen/arch/arm/guestcopy.c                  |   6 +-
 xen/arch/arm/include/asm/domain.h         |  14 --
 xen/arch/arm/include/asm/guest_access.h   |   2 +-
 xen/arch/arm/include/asm/p2m.h            |   2 +
 xen/arch/arm/include/asm/setup.h          |   3 -
 xen/arch/arm/kernel.c                     |  48 ++---
 xen/arch/arm/vgic-v3.c                    |   1 +
 xen/arch/riscv/Kconfig                    |   1 +
 xen/arch/riscv/Makefile                   |   3 +
 xen/arch/riscv/dom0less-build.c           |  70 +++++++
 xen/arch/riscv/guestcopy.c                | 116 +++++++++++
 xen/arch/riscv/include/asm/config.h       |  13 ++
 xen/arch/riscv/include/asm/domain.h       |   1 +
 xen/arch/riscv/include/asm/guest-layout.h |  23 ++
 xen/arch/riscv/include/asm/guest_access.h |   7 +
 xen/arch/riscv/include/asm/p2m.h          |  21 +-
 xen/arch/riscv/kernel.c                   | 242 ++++++++++++++++++++++
 xen/arch/riscv/p2m.c                      | 133 ++++++++----
 xen/arch/riscv/vmid.c                     |   2 +-
 xen/common/device-tree/domain-build.c     |   3 +-
 xen/common/device-tree/kernel.c           |   2 +-
 xen/include/public/arch-riscv.h           |   5 +
 xen/include/xen/bootinfo.h                |   4 +
 xen/include/xen/domain-layout.h           |  27 +++
 xen/include/xen/fdt-domain-build.h        |   2 +-
 xen/include/xen/fdt-kernel.h              |  15 +-
 27 files changed, 670 insertions(+), 97 deletions(-)
 create mode 100644 xen/arch/riscv/dom0less-build.c
 create mode 100644 xen/arch/riscv/guestcopy.c
 create mode 100644 xen/arch/riscv/include/asm/guest-layout.h
 create mode 100644 xen/arch/riscv/kernel.c
 create mode 100644 xen/include/xen/domain-layout.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/11 xen: arm: fix len type for guest copy functions

Widen the len argument of copy_to_guest_phys_flush_dcache() and the copy_to_guest_phys_cb typedef from unsigned int to unsigned long, as the function can be used to copy large blobs such as the initrd which may exceed 4GB. Update the internal copy_guest() len argument to unsigned long accordingly.

Change the type for local variable size in copy_guest() to avoid compilation error because of type mismatch.

raw_* wrappers above copy_guest() keep returning unsigned long to avoid type narrowing; it is not an issue for raw_*'s len argument to remain 'unsigned int' since the assignment to copy_guest()'s wider unsigned long parameter is safe and there is no raw_* users who are using a value bigger than what can fit into 'unsigned int'.

Fixes 2986481b3d9e6 ("xen/arm: guest_copy: Extend the prototype to pass the vCPU")
Fixes 5302bd490bea7 ("xen/arm: Introduce copy_to_guest_phys_flush_dcache")
Fixes d07b7369aa65b ("xen/common: dom0less: introduce common domain-build.c")

---
Changes in v4:
- Add Fixes: tags.
- Add "arm:" prefix to commit subject.
- Add Reviewed-by: Jan Beulich <[email protected]>.
- Avoid using of min_t.
---
Changes in v3:
- Correct the declaration of copy_to_guest_phys_cb() instead of return 'unsigned int' to be in sync with len argument, the type of len argument is changed on 'unsigned long' as initrd could be pretty big and also its size is stroed in 'paddr_t' which is 'unsigned long'.
- Update copy_guest() prototype to avoid trancation bug for len argument.
- Revert prototype changes for raw_* wrappers above copy_guest() as they should keep returning unsigned long to avoid type narrowing; it is not an issue for raw_*'s len argument to remain 'unsigned int' since the assignment to copy_guest()'s wider unsigned long parameter is safe.
- Change the type for local variable size in copy_guest() to avoid compilation error because of type mismatch.
- Add Reported-by: Jan Beulich <[email protected]>
---
Changes in v2:
- New patch.
---

---
 xen/arch/arm/guestcopy.c                | 6 +++---
 xen/arch/arm/include/asm/guest_access.h | 2 +-
 xen/include/xen/fdt-domain-build.h      | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
xen/arch/arm/guestcopy.c +3 -3
index fdb06422b8e9..86f1c9d0e318 100644
@@ -53,7 +53,7 @@ static struct page_info *translate_get_page(copy_info_t info, uint64_t addr,
     return page;
 }
 
-static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
+static unsigned long copy_guest(void *buf, uint64_t addr, unsigned long len,
                                 copy_info_t info, unsigned int flags)
 {
     /* XXX needs to handle faults */
@@ -65,7 +65,7 @@ static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
     while ( len )
     {
         void *p;
-        unsigned int size = min(len, (unsigned int)PAGE_SIZE - offset);
+        unsigned long size = min(len, PAGE_SIZE + 0UL - offset);
         struct page_info *page;
 
         page = translate_get_page(info, addr, flags & COPY_linear,
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                               paddr_t gpa,
                                               void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {
     return copy_guest(buf, gpa, len, GPA_INFO(d),
                       COPY_to_guest | COPY_ipa | COPY_flush_dcache);
xen/arch/arm/include/asm/guest_access.h +1 -1
index 18c88b70d7ec..c13cbec55b65 100644
@@ -14,7 +14,7 @@ unsigned long raw_clear_guest(void *to, unsigned int len);
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                               paddr_t gpa,
                                               void *buf,
-                                              unsigned int len);
+                                              unsigned long len);
 
 int access_guest_memory_by_gpa(struct domain *d, paddr_t gpa, void *buf,
                                uint32_t size, bool is_write);
xen/include/xen/fdt-domain-build.h +1 -1
index bc7744270c8f..6ad9e8fd1642 100644
@@ -48,7 +48,7 @@ static inline int get_allocation_size(paddr_t size)
 typedef unsigned long (*copy_to_guest_phys_cb)(struct domain *d,
                                                paddr_t gpa,
                                                void *buf,
-                                               unsigned int len);
+                                               unsigned long len);
 
 void initrd_load(struct kernel_info *kinfo,
                  copy_to_guest_phys_cb cb);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Hi Oleksii,

Quoted 28 lines
xen/arch/arm/guestcopy.c                | 6 +++---
xen/arch/arm/include/asm/guest_access.h | 2 +-
xen/include/xen/fdt-domain-build.h      | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
xen/arch/arm/guestcopy.c +2 -2
index fdb06422b8e9..86f1c9d0e318 100644
@@ -53,7 +53,7 @@ static struct page_info *translate_get_page(copy_info_t info, uint64_t addr,
    return page;
 }
 
-static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
+static unsigned long copy_guest(void *buf, uint64_t addr, unsigned long len,
                                copy_info_t info, unsigned int flags)
 {
    /* XXX needs to handle faults */
@@ -65,7 +65,7 @@ static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
    while ( len )
    {
        void *p;
-        unsigned int size = min(len, (unsigned int)PAGE_SIZE - offset);
+        unsigned long size = min(len, PAGE_SIZE + 0UL - offset);
        struct page_info *page;
 
        page = translate_get_page(info, addr, flags & COPY_linear,

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 6 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                              paddr_t gpa,
                                              void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/arm/kernel.c;h=7544fd50a20f08b5ba31cad7b94925112fdee956;hb=refs/heads/staging#l131

https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/common/device-tree/domain-build.c;h=c51520ebadf9950311f6c071e7e07042c7076a27;hb=refs/heads/staging#l442

Could you check and let me know if I’m correct or not?

Cheers,
Luca

#3 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Quoted 10 lines · Luca Fancellu

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                              paddr_t gpa,
                                              void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

Jan

Quoted 11 lines

https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/arm/kernel.c;h=7544fd50a20f08b5ba31cad7b94925112fdee956;hb=refs/heads/staging#l131

https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/common/device-tree/domain-build.c;h=c51520ebadf9950311f6c071e7e07042c7076a27;hb=refs/heads/staging#l442

Could you check and let me know if I’m correct or not?

Cheers,
Luca

#4 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Hi Jan,

Quoted 14 lines · Jan Beulich
Quoted 10 lines · Luca Fancellu

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                             paddr_t gpa,
                                             void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Quoted 3 lines

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

yes that’s true, truncation was already there in different places, do you want to deal with it separately so that we have a Fixes tag for it?

Cheers,
Luca

#5 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Quoted 13 lines · Luca Fancellu
Quoted 13 lines · Jan Beulich
Quoted 10 lines · Luca Fancellu

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                             paddr_t gpa,
                                             void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Quoted 6 lines
Quoted 3 lines

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

yes that’s true, truncation was already there in different places, do you want to deal with it separately so that we have a Fixes tag for it?

I already said I'd like that to be dealt with separately, didn't I?

Jan

#6 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Quoted 18 lines · Jan Beulich
Quoted 13 lines · Luca Fancellu
Quoted 13 lines · Jan Beulich
Quoted 10 lines · Luca Fancellu

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                            paddr_t gpa,
                                            void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

yes that’s true, truncation was already there in different places, do you want to deal with it separately so that we have a Fixes tag for it?

I already said I'd like that to be dealt with separately, didn't I?

I understood the “separately” part.

What I was asking is whether the reason is that this should be its own fix, with its own Fixes tag, since the truncation predates this patch.

I’m asking to understand your reasoning and calibrate my reviews accordingly. When the reasoning is not stated, it is hard to tell whether this is a general review rule I should apply in similar cases or just a preference for this patch.

Cheers,
Luca

#7 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Quoted 22 lines · Luca Fancellu
Quoted 22 lines · Jan Beulich
Quoted 17 lines · Luca Fancellu
Quoted 16 lines · Jan Beulich
Quoted 10 lines · Luca Fancellu

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
xen/arch/arm/guestcopy.c +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                            paddr_t gpa,
                                            void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

yes that’s true, truncation was already there in different places, do you want to deal with it separately so that we have a Fixes tag for it?

I already said I'd like that to be dealt with separately, didn't I?

I understood the “separately” part.

What I was asking is whether the reason is that this should be its own fix, with its own Fixes tag, since the truncation predates this patch.

The reason isn't so much the Fixes: tag, but independent issues generally wanting independent fixes. This also helps with backporting (in general; maybe not so much here).

Jan

Reviewed-by
#8 lore raw

Re: [PATCH v4 01/11] xen: arm: fix len type for guest copy functions

Quoted 32 lines · Jan Beulich
Quoted 27 lines · Luca Fancellu
Quoted 26 lines · Jan Beulich
Quoted 13 lines · Luca Fancellu
Quoted 13 lines · Jan Beulich
Quoted 10 lines · Luca Fancellu

Review unsigned long raw_copy_from_guest(void *to, const void __user *from, · line 140

Quoted 7 lines
(quoted diff) +1 -1
@@ -136,7 +136,7 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from,
 unsigned long copy_to_guest_phys_flush_dcache(struct domain *d,
                                           paddr_t gpa,
                                           void *buf,
-                                              unsigned int len)
+                                              unsigned long len)
 {

Now that we do this, potentially we could have truncation in the places where we store its return value inside an int:

Quoted 6 lines
Quoted 3 lines

Those would suffer from truncation before and after this change, wouldn't they? Just that where the truncation occurs does move. I.e. if necessary they would want dealing with separately.

yes that’s true, truncation was already there in different places, do you want to deal with it separately so that we have a Fixes tag for it?

I already said I'd like that to be dealt with separately, didn't I?

Quoted 7 lines
Quoted 3 lines

I understood the “separately” part.

What I was asking is whether the reason is that this should be its own fix, with its own Fixes tag, since the truncation predates this patch.

The reason isn't so much the Fixes: tag, but independent issues generally wanting independent fixes. This also helps with backporting (in general; maybe not so much here).

Thanks.

Cheers,
Luca

#9 lore raw

PATCH v4 2/11 xen/dom0less: rename kernel_zimage_probe() to kernel_image_probe()

In reply to Oleksii Kurochko

The helper kernel_zimage_probe() is referenced from common code (xen/common/device-tree/kernel.c), but its name is tied to the zImage format which is specific to Arm (from architectures supported by Xen).

Other architectures supported by Xen, such as RISC-V, do not use the zImage format and instead rely on other kernel image types (e.g. Image or compressed Image variants: Image.gz, etc). Using "zimage" in the name is therefore misleading in architecture-independent code.

Rename kernel_zimage_probe() to kernel_image_probe() and update the associated structure field from "zimage" to "image" to reflect that the code handles generic kernel images rather than the zImage format specifically.

No functional change intended.

---
Changes in v3-4:
- Nothing changed. Only rebase.
---
Changes in v2:
- new patch.
---

---
 xen/arch/arm/kernel.c           | 48 ++++++++++++++++-----------------
 xen/common/device-tree/kernel.c |  2 +-
 xen/include/xen/fdt-kernel.h    |  4 +--
 3 files changed, 27 insertions(+), 27 deletions(-)
xen/arch/arm/kernel.c +24 -24
index 9395b5af8745..a5554714cd7b 100644
@@ -101,8 +101,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
     paddr_t load_addr;
 
 #ifdef CONFIG_HAS_DOMAIN_TYPE
-    if ( (info->type == DOMAIN_64BIT) && (info->zimage.start == 0) )
-        return mem->bank[0].start + info->zimage.text_offset;
+    if ( (info->type == DOMAIN_64BIT) && (info->image.start == 0) )
+        return mem->bank[0].start + info->image.text_offset;
 #endif
 
     /*
@@ -111,19 +111,19 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
      * and above 32MiB. Load it as high as possible within these
      * constraints, while also avoiding the DTB.
      */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
     {
         paddr_t load_end;
 
         load_end = mem->bank[0].start + mem->bank[0].size;
         load_end = MIN(mem->bank[0].start + MB(128), load_end);
 
-        load_addr = load_end - info->zimage.len;
+        load_addr = load_end - info->image.len;
         /* Align to 2MB */
         load_addr &= ~((2 << 20) - 1);
     }
     else
-        load_addr = info->zimage.start;
+        load_addr = info->image.start;
 
     return load_addr;
 }
@@ -131,8 +131,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
 static void __init kernel_zimage_load(struct kernel_info *info)
 {
     paddr_t load_addr = kernel_zimage_place(info);
-    paddr_t paddr = info->zimage.kernel_addr;
-    paddr_t len = info->zimage.len;
+    paddr_t paddr = info->image.kernel_addr;
+    paddr_t len = info->image.len;
     void *kernel;
     int rc;
 
@@ -215,7 +215,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
         return -EOPNOTSUPP;
     }
 
-    info->zimage.start = be32_to_cpu(uimage.load);
+    info->image.start = be32_to_cpu(uimage.load);
     info->entry = be32_to_cpu(uimage.ep);
 
     /*
@@ -224,20 +224,20 @@ int __init kernel_uimage_probe(struct kernel_info *info,
      * independent image. That means Xen is free to load such an image at
      * any valid address.
      */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
         printk(XENLOG_INFO
                "No load address provided. Xen will decide where to load it.\n");
     else
         printk(XENLOG_INFO
                "Provided load address: %"PRIpaddr" and entry address: %"PRIpaddr"\n",
-               info->zimage.start, info->entry);
+               info->image.start, info->entry);
 
     /*
      * If the image supports position independent execution, then user cannot
      * provide an entry point as Xen will load such an image at any appropriate
      * memory address. Thus, we need to return error.
      */
-    if ( (info->zimage.start == 0) && (info->entry != 0) )
+    if ( (info->image.start == 0) && (info->entry != 0) )
     {
         printk(XENLOG_ERR
                "Entry point cannot be non zero for PIE image.\n");
@@ -257,13 +257,13 @@ int __init kernel_uimage_probe(struct kernel_info *info,
         if ( rc )
             return rc;
 
-        info->zimage.kernel_addr = mod->start;
-        info->zimage.len = mod->size;
+        info->image.kernel_addr = mod->start;
+        info->image.len = mod->size;
     }
     else
     {
-        info->zimage.kernel_addr = addr + sizeof(uimage);
-        info->zimage.len = len;
+        info->image.kernel_addr = addr + sizeof(uimage);
+        info->image.len = len;
     }
 
     info->load = kernel_zimage_load;
@@ -289,7 +289,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
      * Thus, Xen uses uimage.load attribute to determine the load address and
      * zimage.text_offset is ignored.
      */
-    info->zimage.text_offset = 0;
+    info->image.text_offset = 0;
 #endif
 
     return 0;
@@ -338,10 +338,10 @@ static int __init kernel_zimage64_probe(struct kernel_info *info,
     if ( (end - start) > size )
         return -EINVAL;
 
-    info->zimage.kernel_addr = addr;
-    info->zimage.len = end - start;
-    info->zimage.text_offset = zimage.text_offset;
-    info->zimage.start = 0;
+    info->image.kernel_addr = addr;
+    info->image.len = end - start;
+    info->image.text_offset = zimage.text_offset;
+    info->image.start = 0;
 
     info->load = kernel_zimage_load;
 
@@ -389,10 +389,10 @@ static int __init kernel_zimage32_probe(struct kernel_info *info,
         }
     }
 
-    info->zimage.kernel_addr = addr;
+    info->image.kernel_addr = addr;
 
-    info->zimage.start = start;
-    info->zimage.len = end - start;
+    info->image.start = start;
+    info->image.len = end - start;
 
     info->load = kernel_zimage_load;
 
@@ -403,7 +403,7 @@ static int __init kernel_zimage32_probe(struct kernel_info *info,
     return 0;
 }
 
-int __init kernel_zimage_probe(struct kernel_info *info, paddr_t addr,
+int __init kernel_image_probe(struct kernel_info *info, paddr_t addr,
                                paddr_t size)
 {
     int rc;
xen/common/device-tree/kernel.c +1 -1
index 28096121a52d..cfa27464f0fc 100644
@@ -235,7 +235,7 @@ int __init kernel_probe(struct kernel_info *info,
     if ( rc && rc != -EINVAL )
         return rc;
 
-    rc = kernel_zimage_probe(info, mod->start, mod->size);
+    rc = kernel_image_probe(info, mod->start, mod->size);
 
     return rc;
 }
xen/include/xen/fdt-kernel.h +2 -2
index 86a37a13048b..54be77aa191e 100644
@@ -63,7 +63,7 @@ struct kernel_info {
             paddr_t text_offset; /* 64-bit Image only */
 #endif
             paddr_t start; /* Must be 0 for 64-bit Image */
-        } zimage;
+        } image;
     };
 
 #ifdef CONFIG_HAS_DOMAIN_TYPE
@@ -133,7 +133,7 @@ void kernel_load(struct kernel_info *info);
 
 int kernel_decompress(struct boot_module *mod, uint32_t offset);
 
-int kernel_zimage_probe(struct kernel_info *info, paddr_t addr, paddr_t size);
+int kernel_image_probe(struct kernel_info *info, paddr_t addr, paddr_t size);
 
 /*
  * uImage isn't really used nowadays thereby leave kernel_uimage_probe()
Signature · 1 line
2.53.0
#10 lore raw

Re: [PATCH v4 02/11] xen/dom0less: rename kernel_zimage_probe() to kernel_image_probe()

Hi Oleksii,

Quoted 102 lines
xen/arch/arm/kernel.c +15 -15
index 9395b5af8745..a5554714cd7b 100644
@@ -101,8 +101,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
    paddr_t load_addr;
 
 #ifdef CONFIG_HAS_DOMAIN_TYPE
-    if ( (info->type == DOMAIN_64BIT) && (info->zimage.start == 0) )
-        return mem->bank[0].start + info->zimage.text_offset;
+    if ( (info->type == DOMAIN_64BIT) && (info->image.start == 0) )
+        return mem->bank[0].start + info->image.text_offset;
 #endif
 
    /*
@@ -111,19 +111,19 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
     * and above 32MiB. Load it as high as possible within these
     * constraints, while also avoiding the DTB.
     */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
    {
        paddr_t load_end;
 
        load_end = mem->bank[0].start + mem->bank[0].size;
        load_end = MIN(mem->bank[0].start + MB(128), load_end);
 
-        load_addr = load_end - info->zimage.len;
+        load_addr = load_end - info->image.len;
        /* Align to 2MB */
        load_addr &= ~((2 << 20) - 1);
    }
    else
-        load_addr = info->zimage.start;
+        load_addr = info->image.start;
 
    return load_addr;
 }
@@ -131,8 +131,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
 static void __init kernel_zimage_load(struct kernel_info *info)
 {
    paddr_t load_addr = kernel_zimage_place(info);
-    paddr_t paddr = info->zimage.kernel_addr;
-    paddr_t len = info->zimage.len;
+    paddr_t paddr = info->image.kernel_addr;
+    paddr_t len = info->image.len;
    void *kernel;
    int rc;
 
@@ -215,7 +215,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
        return -EOPNOTSUPP;
    }
 
-    info->zimage.start = be32_to_cpu(uimage.load);
+    info->image.start = be32_to_cpu(uimage.load);
    info->entry = be32_to_cpu(uimage.ep);
 
    /*
@@ -224,20 +224,20 @@ int __init kernel_uimage_probe(struct kernel_info *info,
     * independent image. That means Xen is free to load such an image at
     * any valid address.
     */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
        printk(XENLOG_INFO
               "No load address provided. Xen will decide where to load it.\n");
    else
        printk(XENLOG_INFO
               "Provided load address: %"PRIpaddr" and entry address: %"PRIpaddr"\n",
-               info->zimage.start, info->entry);
+               info->image.start, info->entry);
 
    /*
     * If the image supports position independent execution, then user cannot
     * provide an entry point as Xen will load such an image at any appropriate
     * memory address. Thus, we need to return error.
     */
-    if ( (info->zimage.start == 0) && (info->entry != 0) )
+    if ( (info->image.start == 0) && (info->entry != 0) )
    {
        printk(XENLOG_ERR
               "Entry point cannot be non zero for PIE image.\n");
@@ -257,13 +257,13 @@ int __init kernel_uimage_probe(struct kernel_info *info,
        if ( rc )
            return rc;
 
-        info->zimage.kernel_addr = mod->start;
-        info->zimage.len = mod->size;
+        info->image.kernel_addr = mod->start;
+        info->image.len = mod->size;
    }
    else
    {
-        info->zimage.kernel_addr = addr + sizeof(uimage);
-        info->zimage.len = len;
+        info->image.kernel_addr = addr + sizeof(uimage);
+        info->image.len = len;
    }
 
    info->load = kernel_zimage_load;

xen/arch/arm/kernel.c int __init kernel_uimage_probe(struct kernel_info *info, · line 290

Quoted 2 lines
xen/arch/arm/kernel.c +0 -0
@@ -289,7 +289,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
     * Thus, Xen uses uimage.load attribute to determine the load address and
     * zimage.text_offset is ignored.

Should we update the comment as well?

Also in here:
https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/include/xen/fdt-kernel.h;h=4d0467bb396a9cf317954fd511469e7f56d67844;hb=refs/heads/staging#l111

Cheers,
Luca

#11 lore raw

Re: [PATCH v4 02/11] xen/dom0less: rename kernel_zimage_probe() to kernel_image_probe()

Quoted 109 lines · Luca Fancellu

Hi Oleksii,

Quoted 102 lines
xen/arch/arm/kernel.c +15 -15
index 9395b5af8745..a5554714cd7b 100644
@@ -101,8 +101,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
     paddr_t load_addr;
 
 #ifdef CONFIG_HAS_DOMAIN_TYPE
-    if ( (info->type == DOMAIN_64BIT) && (info->zimage.start == 0) )
-        return mem->bank[0].start + info->zimage.text_offset;
+    if ( (info->type == DOMAIN_64BIT) && (info->image.start == 0) )
+        return mem->bank[0].start + info->image.text_offset;
 #endif
 
     /*
@@ -111,19 +111,19 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
      * and above 32MiB. Load it as high as possible within these
      * constraints, while also avoiding the DTB.
      */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
     {
         paddr_t load_end;
 
         load_end = mem->bank[0].start + mem->bank[0].size;
         load_end = MIN(mem->bank[0].start + MB(128), load_end);
 
-        load_addr = load_end - info->zimage.len;
+        load_addr = load_end - info->image.len;
         /* Align to 2MB */
         load_addr &= ~((2 << 20) - 1);
     }
     else
-        load_addr = info->zimage.start;
+        load_addr = info->image.start;
 
     return load_addr;
 }
@@ -131,8 +131,8 @@ static paddr_t __init kernel_zimage_place(struct kernel_info *info)
 static void __init kernel_zimage_load(struct kernel_info *info)
 {
     paddr_t load_addr = kernel_zimage_place(info);
-    paddr_t paddr = info->zimage.kernel_addr;
-    paddr_t len = info->zimage.len;
+    paddr_t paddr = info->image.kernel_addr;
+    paddr_t len = info->image.len;
     void *kernel;
     int rc;
 
@@ -215,7 +215,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
         return -EOPNOTSUPP;
     }
 
-    info->zimage.start = be32_to_cpu(uimage.load);
+    info->image.start = be32_to_cpu(uimage.load);
     info->entry = be32_to_cpu(uimage.ep);
 
     /*
@@ -224,20 +224,20 @@ int __init kernel_uimage_probe(struct kernel_info *info,
      * independent image. That means Xen is free to load such an image at
      * any valid address.
      */
-    if ( info->zimage.start == 0 )
+    if ( info->image.start == 0 )
         printk(XENLOG_INFO
                "No load address provided. Xen will decide where to load it.\n");
     else
         printk(XENLOG_INFO
                "Provided load address: %"PRIpaddr" and entry address: %"PRIpaddr"\n",
-               info->zimage.start, info->entry);
+               info->image.start, info->entry);
 
     /*
      * If the image supports position independent execution, then user cannot
      * provide an entry point as Xen will load such an image at any appropriate
      * memory address. Thus, we need to return error.
      */
-    if ( (info->zimage.start == 0) && (info->entry != 0) )
+    if ( (info->image.start == 0) && (info->entry != 0) )
     {
         printk(XENLOG_ERR
                "Entry point cannot be non zero for PIE image.\n");
@@ -257,13 +257,13 @@ int __init kernel_uimage_probe(struct kernel_info *info,
         if ( rc )
             return rc;
 
-        info->zimage.kernel_addr = mod->start;
-        info->zimage.len = mod->size;
+        info->image.kernel_addr = mod->start;
+        info->image.len = mod->size;
     }
     else
     {
-        info->zimage.kernel_addr = addr + sizeof(uimage);
-        info->zimage.len = len;
+        info->image.kernel_addr = addr + sizeof(uimage);
+        info->image.len = len;
     }
 
     info->load = kernel_zimage_load;

xen/arch/arm/kernel.c int __init kernel_uimage_probe(struct kernel_info *info, · line 290

Quoted 2 lines
xen/arch/arm/kernel.c +0 -0
@@ -289,7 +289,7 @@ int __init kernel_uimage_probe(struct kernel_info *info,
      * Thus, Xen uses uimage.load attribute to determine the load address and
      * zimage.text_offset is ignored.

Should we update the comment as well?

Quoted 2 lines

Also in here:
https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/include/xen/fdt-kernel.h;h=4d0467bb396a9cf317954fd511469e7f56d67844;hb=refs/heads/staging#l111

Agree, it should be updated. I'll do that in the next version.

Thanks.

~ Oleksii

#12 lore raw

PATCH v4 3/11 xen: move declaration of fw_unreserved_regions() to common header

In reply to Oleksii Kurochko

Since the implementation of fw_unreserved_regions() is in common code, move its declaration to xen/bootinfo.h.

---
Changes in v2-v4:
- Nothing changed. Only rebase.
---

---
 xen/arch/arm/include/asm/setup.h | 3 ---
 xen/include/xen/bootinfo.h       | 4 ++++
 2 files changed, 4 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -3
index 899e33925ca4..0d29b46ea52b 100644
@@ -43,9 +43,6 @@ int acpi_make_efi_nodes(void *fdt, struct membank tbl_add[]);
 void create_dom0(void);
 
 void discard_initial_modules(void);
-void fw_unreserved_regions(paddr_t s, paddr_t e,
-                           void (*cb)(paddr_t ps, paddr_t pe),
-                           unsigned int first);
 
 void init_pdx(void);
 void setup_mm(void);
xen/include/xen/bootinfo.h +4 -0
index f834f1957155..dbf492c2e36e 100644
@@ -210,4 +210,8 @@ static inline struct membanks *membanks_xzalloc(unsigned int nr,
     return banks;
 }
 
+void fw_unreserved_regions(paddr_t s, paddr_t e,
+                           void (*cb)(paddr_t ps, paddr_t pe),
+                           unsigned int first);
+
 #endif /* XEN_BOOTINFO_H */
Signature · 1 line
2.53.0
Reviewed-by on v4 3/11
#13 lore raw

Re: [PATCH v4 03/11] xen: move declaration of fw_unreserved_regions() to common header

Hi Oleksii,

Quoted 11 lines · Oleksii Kurochko

Since the implementation of fw_unreserved_regions() is in common code, move its declaration to xen/bootinfo.h.

---
Changes in v2-v4:
- Nothing changed. Only rebase.
---
---

looks ok to me.

Cheers,
Luca

#14 lore raw

PATCH v4 4/11 xen: introduce domain-layout.h with common domain_use_host_layout()

In reply to Oleksii Kurochko

domain_use_host_layout() is not architecture-specific and may be needed on x86 [1]. Replace the ARM-specific macro in asm/domain.h with a common static inline in a new dedicated header, xen/domain-layout.h.

xen/domain.h would be the natural home, but placing it there would require including xen/paging.h (for paging_mode_translate()) and xen/sched.h (for is_hardware_domain()), which would introduce circular dependencies. A separate header that callers opt into avoids this.

Adjust the implementation to take paging_mode_translate() into account so it works correctly for all architectures, including x86. Some extra details about implementation [2] and [3].

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602161038120.359097@ubuntu-linux-20-04-desktop/
[2] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271742400.3148344@ubuntu-linux-20-04-desktop/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271750190.3148344@ubuntu-linux-20-04-desktop/

---
Changes in v4:
- Update the comment above domain_use_host_layout().
---
Changes in v3:
- Make argument of domain_use_host_layout() const.
- Create a separate header to avoid circular heder dependecy and making domain_use_host_layour() as static inline.
- Rework domain_use_host_layout() to be protected by paging_mode_translate().
- Update the commit message.
---
Changes in v2:
- Drop ifdef around defintion of domain_use_host_layout() as it was suggested generic version. It could be returned back when the real use case for it will appear.
- Add Suggested-by: and update the commit message.
- Make domain_use_host_layout() function instead of macros to avoid ciclular header dependecies. Look at more details in
the commit message.
---

---
 xen/arch/arm/domain_build.c           |  1 +
 xen/arch/arm/include/asm/domain.h     | 14 --------------
 xen/arch/arm/vgic-v3.c                |  1 +
 xen/common/device-tree/domain-build.c |  1 +
 xen/include/xen/domain-layout.h       | 27 +++++++++++++++++++++++++++
 5 files changed, 30 insertions(+), 14 deletions(-)
 create mode 100644 xen/include/xen/domain-layout.h
xen/arch/arm/domain_build.c +1 -0
index ad665cd3c045..1efddc60ef0a 100644
@@ -2,6 +2,7 @@
 #include <xen/init.h>
 #include <xen/bootinfo.h>
 #include <xen/compile.h>
+#include <xen/domain-layout.h>
 #include <xen/dom0less-build.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/fdt-kernel.h>
xen/arch/arm/include/asm/domain.h +0 -14
index b24f02d269be..46a5cdc0c800 100644
@@ -18,20 +18,6 @@ struct hvm_domain
     uint64_t              params[HVM_NR_PARAMS];
 };
 
-/*
- * Is the domain using the host memory layout?
- *
- * Direct-mapped domain will always have the RAM mapped with GFN == MFN.
- * To avoid any trouble finding space, it is easier to force using the
- * host memory layout.
- *
- * The hardware domain will use the host layout regardless of
- * direct-mapped because some OS may rely on a specific address ranges
- * for the devices.
- */
-#define domain_use_host_layout(d) (is_domain_direct_mapped(d) || \
-                                   is_hardware_domain(d))
-
 struct vtimer {
     struct vcpu *v;
     int irq;
xen/arch/arm/vgic-v3.c +1 -0
index 77aab5c3c293..77517c303061 100644
@@ -10,6 +10,7 @@
  */
 
 #include <xen/bitops.h>
+#include <xen/domain-layout.h>
 #include <xen/init.h>
 #include <xen/irq.h>
 #include <xen/lib.h>
xen/common/device-tree/domain-build.c +1 -0
index c51520ebadf9..6949203dacdc 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
 #include <xen/bootinfo.h>
+#include <xen/domain-layout.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/init.h>
 #include <xen/lib.h>
xen/include/xen/domain-layout.h new file +27 -0
new file mode 100644
index 000000000000..0532a27b44ce
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__
+
+#include <xen/domain.h>
+#include <xen/paging.h>
+#include <xen/sched.h>
+
+/*
+ * Is a domain using the host memory layout?
+ *
+ * domain_use_host_layout() is always False for PV domains (including Dom0).
+ *
+ * Direct-mapped domains (autotranslated domains with memory allocated
+ * contiguously and mapped 1:1 so that GFN == MFN) must use the host
+ * memory layout since GFN == MFN by definition.
+ *
+ * The hardware domain will use the host layout (regardless of
+ * direct-mapped) because some OS may rely on specific address ranges
+ * for the devices.
+ */
+static inline bool domain_use_host_layout(const struct domain *d)
+{
+    return paging_mode_translate(d) &&
+           (is_domain_direct_mapped(d) || is_hardware_domain(d));
+}
+
+#endif /* __XEN_DOMAIN_LAYOUT_H__ */
Signature · 1 line
2.53.0
#15 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

Hi Oleksii,

xen/include/xen/domain-layout.h

Quoted 116 lines · Oleksii Kurochko

domain_use_host_layout() is not architecture-specific and may be needed on x86 [1]. Replace the ARM-specific macro in asm/domain.h with a common static inline in a new dedicated header, xen/domain-layout.h.

xen/domain.h would be the natural home, but placing it there would require including xen/paging.h (for paging_mode_translate()) and xen/sched.h (for is_hardware_domain()), which would introduce circular dependencies. A separate header that callers opt into avoids this.

Adjust the implementation to take paging_mode_translate() into account so it works correctly for all architectures, including x86. Some extra details about implementation [2] and [3].

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602161038120.359097@ubuntu-linux-20-04-desktop/
[2] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271742400.3148344@ubuntu-linux-20-04-desktop/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271750190.3148344@ubuntu-linux-20-04-desktop/

---
Changes in v4:
- Update the comment above domain_use_host_layout().
---
Changes in v3:
- Make argument of domain_use_host_layout() const.
- Create a separate header to avoid circular heder dependecy and making domain_use_host_layour() as static inline.
- Rework domain_use_host_layout() to be protected by paging_mode_translate().
- Update the commit message.
---
Changes in v2:
- Drop ifdef around defintion of domain_use_host_layout() as it was suggested generic version. It could be returned back when the real use case for it will appear.
- Add Suggested-by: and update the commit message.
- Make domain_use_host_layout() function instead of macros to avoid ciclular header dependecies. Look at more details in
the commit message.
---
---

xen/arch/arm/domain_build.c           |  1 +
xen/arch/arm/include/asm/domain.h     | 14 --------------
xen/arch/arm/vgic-v3.c                |  1 +
xen/common/device-tree/domain-build.c |  1 +
xen/include/xen/domain-layout.h       | 27 +++++++++++++++++++++++++++
5 files changed, 30 insertions(+), 14 deletions(-)

create mode 100644 xen/include/xen/domain-layout.h

xen/arch/arm/domain_build.c +1 -0
index ad665cd3c045..1efddc60ef0a 100644
@@ -2,6 +2,7 @@
 #include <xen/init.h>
 #include <xen/bootinfo.h>
 #include <xen/compile.h>
+#include <xen/domain-layout.h>
 #include <xen/dom0less-build.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/fdt-kernel.h>
xen/arch/arm/include/asm/domain.h +0 -14
index b24f02d269be..46a5cdc0c800 100644
@@ -18,20 +18,6 @@ struct hvm_domain
    uint64_t              params[HVM_NR_PARAMS];
 };
 
-/*
- * Is the domain using the host memory layout?
- *
- * Direct-mapped domain will always have the RAM mapped with GFN == MFN.
- * To avoid any trouble finding space, it is easier to force using the
- * host memory layout.
- *
- * The hardware domain will use the host layout regardless of
- * direct-mapped because some OS may rely on a specific address ranges
- * for the devices.
- */
-#define domain_use_host_layout(d) (is_domain_direct_mapped(d) || \
-                                   is_hardware_domain(d))
-
 struct vtimer {
    struct vcpu *v;
    int irq;
xen/arch/arm/vgic-v3.c +1 -0
index 77aab5c3c293..77517c303061 100644
@@ -10,6 +10,7 @@
 */
 
 #include <xen/bitops.h>
+#include <xen/domain-layout.h>
 #include <xen/init.h>
 #include <xen/irq.h>
 #include <xen/lib.h>
xen/common/device-tree/domain-build.c +1 -0
index c51520ebadf9..6949203dacdc 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
 #include <xen/bootinfo.h>
+#include <xen/domain-layout.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/init.h>
 #include <xen/lib.h>
xen/include/xen/domain-layout.h new file +0 -0
new file mode 100644
index 000000000000..0532a27b44ce

New files should have SPDX tag

xen/include/xen/domain-layout.h line 2

Quoted 3 lines
xen/include/xen/domain-layout.h new file +2 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__

I think this include guard doesn’t satisfy the coding style:
https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=CODING_STYLE;h=aae5a47ac20345978b3c465b9d85f1d5f6774731;hb=refs/heads/staging#l167

Apart from this, the rest looks ok to me, after fixing the above I will leave my R-by

Cheers,
Luca

#16 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

Hello Luca,

Quoted 7 lines · Luca Fancellu
Quoted 7 lines
xen/common/device-tree/domain-build.c +0 -0
index c51520ebadf9..6949203dacdc 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
Quoted 12 lines

xen/include/xen/domain-layout.h

Quoted 10 lines
xen/common/device-tree/domain-build.c +1 -0
 #include <xen/bootinfo.h>
+#include <xen/domain-layout.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/init.h>
 #include <xen/lib.h>
xen/include/xen/domain-layout.h new file +0 -0
new file mode 100644
index 000000000000..0532a27b44ce

New files should have SPDX tag

I will add:
/* SPDX-License-Identifier: GPL-2.0-only */

Quoted 7 lines

xen/include/xen/domain-layout.h line 2

Quoted 4 lines
xen/include/xen/domain-layout.h new file +2 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__

I think this include guard doesn’t satisfy the coding style:
https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=CODING_STYLE;h=aae5a47ac20345978b3c465b9d85f1d5f6774731;hb=refs/heads/staging#l167

Will rename to XEN_DOMAIN_LAYOUT_H.

Quoted 2 lines

Apart from this, the rest looks ok to me, after fixing the above I will leave my R-by

Thanks for review.

~ Oleksii

Acked-by on v4 4/11
#17 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

In reply to Oleksii Kurochko
Quoted 13 lines · Oleksii Kurochko

domain_use_host_layout() is not architecture-specific and may be needed on x86 [1]. Replace the ARM-specific macro in asm/domain.h with a common static inline in a new dedicated header, xen/domain-layout.h.

xen/domain.h would be the natural home, but placing it there would require including xen/paging.h (for paging_mode_translate()) and xen/sched.h (for is_hardware_domain()), which would introduce circular dependencies. A separate header that callers opt into avoids this.

Adjust the implementation to take paging_mode_translate() into account so it works correctly for all architectures, including x86. Some extra details about implementation [2] and [3].

Quoted 6 lines

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602161038120.359097@ubuntu-linux-20-04-desktop/
[2] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271742400.3148344@ubuntu-linux-20-04-desktop/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271750190.3148344@ubuntu-linux-20-04-desktop/

With the SPDX tag added as indicated by Luca:

One further minor remark:

xen/include/xen/domain-layout.h line 4

Quoted 7 lines
xen/include/xen/domain-layout.h new file +4 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__
+
+#include <xen/domain.h>

This isn't really needed. It is ...

xen/include/xen/domain-layout.h line 6

Quoted 2 lines
xen/include/xen/domain-layout.h new file +2 -0
+#include <xen/paging.h>
+#include <xen/sched.h>

... included by this one anyway (pretty much unavoidably right now, I guess).

Jan

#18 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

Quoted 18 lines · Jan Beulich
Quoted 17 lines · Oleksii Kurochko

domain_use_host_layout() is not architecture-specific and may be needed on x86 [1]. Replace the ARM-specific macro in asm/domain.h with a common static inline in a new dedicated header, xen/domain-layout.h.

xen/domain.h would be the natural home, but placing it there would require including xen/paging.h (for paging_mode_translate()) and xen/sched.h (for is_hardware_domain()), which would introduce circular dependencies. A separate header that callers opt into avoids this.

Adjust the implementation to take paging_mode_translate() into account so it works correctly for all architectures, including x86. Some extra details about implementation [2] and [3].

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602161038120.359097@ubuntu-linux-20-04-desktop/
[2] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271742400.3148344@ubuntu-linux-20-04-desktop/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2602271750190.3148344@ubuntu-linux-20-04-desktop/

Quoted 5 lines
Quoted 2 lines

With the SPDX tag added as indicated by Luca:

Thanks.

Quoted 13 lines

One further minor remark:

xen/include/xen/domain-layout.h line 4

Quoted 7 lines
xen/include/xen/domain-layout.h new file +4 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__
+
+#include <xen/domain.h>

This isn't really needed. It is ...

Quoted 5 lines

xen/include/xen/domain-layout.h line 6

Quoted 2 lines
xen/include/xen/domain-layout.h new file +2 -0
+#include <xen/paging.h>
+#include <xen/sched.h>

... included by this one anyway (pretty much unavoidably right now, I guess).

Then it will be needed to re-order them.

Do you want to put the comment above xen/sched.h:

/*
* Ensure xen/sched.h is included before xen/paging.h, since paging.h
depends
* on xen/domain.h, which is pulled in via sched.h.
*/
#include <xen/sched.h>
#include <xen/paging.h>

or just add this to commit message instead?

~ Oleksii

Quoted 2 lines

Jan

#19 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

Quoted 30 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich

xen/include/xen/domain-layout.h line 4

Quoted 7 lines · Oleksii Kurochko
xen/include/xen/domain-layout.h new file +4 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__
+
+#include <xen/domain.h>

This isn't really needed. It is ...

Quoted 5 lines

xen/include/xen/domain-layout.h line 6

Quoted 2 lines
xen/include/xen/domain-layout.h new file +2 -0
+#include <xen/paging.h>
+#include <xen/sched.h>

... included by this one anyway (pretty much unavoidably right now, I guess).

Then it will be needed to re-order them.

Do you want to put the comment above xen/sched.h:

/*
* Ensure xen/sched.h is included before xen/paging.h, since paging.h
depends
* on xen/domain.h, which is pulled in via sched.h.
*/
#include <xen/sched.h>
#include <xen/paging.h>

Quoted 1 line

or just add this to commit message instead?

No. xen/paging.h only includes two asm/*.h, so doesn't itself require anything. If there's anything missing for that header to be included first, I would assume it's then RISC-V's asm/paging.h or asm/p2m.h which lack a necessary #include? Yet without you indicating what exactly the missing piece is, this is somewhat guesswork on my part.

Jan

#20 lore raw

Re: [PATCH v4 04/11] xen: introduce domain-layout.h with common domain_use_host_layout()

Quoted 31 lines · Jan Beulich
Quoted 30 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich

xen/include/xen/domain-layout.h line 4

Quoted 7 lines · Oleksii Kurochko
xen/include/xen/domain-layout.h new file +4 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_DOMAIN_LAYOUT_H__
+#define __XEN_DOMAIN_LAYOUT_H__
+
+#include <xen/domain.h>

This isn't really needed. It is ...

Quoted 5 lines

xen/include/xen/domain-layout.h line 6

Quoted 2 lines
xen/include/xen/domain-layout.h new file +2 -0
+#include <xen/paging.h>
+#include <xen/sched.h>

... included by this one anyway (pretty much unavoidably right now, I guess).

Then it will be needed to re-order them.

Do you want to put the comment above xen/sched.h:

/*
* Ensure xen/sched.h is included before xen/paging.h, since paging.h
depends
* on xen/domain.h, which is pulled in via sched.h.
*/
#include <xen/sched.h>
#include <xen/paging.h>

Quoted 7 lines
Quoted 1 line

or just add this to commit message instead?

No. xen/paging.h only includes two asm/*.h, so doesn't itself require anything. If there's anything missing for that header to be included first, I would assume it's then RISC-V's asm/paging.h or asm/p2m.h which lack a necessary #include? Yet without you indicating what exactly the missing piece is, this is somewhat guesswork on my part.

The first time I read compiler error inattentively.

The following compilation error occurs after xen/domain.h is dropped from xen/domain-layout.h:

In file included from ./include/xen/paging.h:4,
from ./include/xen/domain-layout.h:6,
from common/device-tree/domain-build.c:4:
./arch/riscv/include/asm/paging.h:17:48: error: 'struct page_info' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
17 | void paging_free_page(struct domain *d, struct page_info *pg);

So the correct fix is to add forward declaration of struct page_info to RISC-V's asm/paging.h.

I will add the following to commit message:
"
To avoid the following compilation issue:

In file included from ./include/xen/paging.h:4,
from ./include/xen/domain-layout.h:6,
from common/device-tree/domain-build.c:4:
./arch/riscv/include/asm/paging.h:17:48: error: 'struct page_info' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
17 | void paging_free_page(struct domain *d, struct page_info *pg);

add the forward declaration of struct page_info to RISC-V's asm/paging.h. "

~ Oleksii

#21 lore raw

PATCH v4 5/11 xen: rename p2m_ipa_bits to p2m_gpa_bits

In reply to Oleksii Kurochko

The IPA terminology is Arm-specific, so rename p2m_ipa_bits to p2m_gpa_bits to use architecture-neutral naming in xen/common/device-tree/ code.

No functional changes.

---
Changes in v4:
- Nothing changed only rebase.
---
Changes in v3:
- Introduce #define p2m_gpa_bits p2m_ipa_bits for Arm instead of renaming of p2m_ipa_bits to p2m_gpa_bits to keep Arm part of changes clearer and keep using Arm-specific terminolgy inside Arm code.
---
Changes in v2:
- New patch.
---

---
 xen/arch/arm/include/asm/p2m.h        | 2 ++
 xen/common/device-tree/domain-build.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/p2m.h +2 -0
index 010ce8c9ebbd..7957dbd96e57 100644
@@ -14,6 +14,8 @@
 /* Holds the bit size of IPAs in p2m tables.  */
 extern unsigned int p2m_ipa_bits;
 
+#define p2m_gpa_bits p2m_ipa_bits
+
 #define MAX_VMID_8_BIT  (1UL << 8)
 #define MAX_VMID_16_BIT (1UL << 16)
 
xen/common/device-tree/domain-build.c +1 -1
index 6949203dacdc..2a760b007b57 100644
@@ -221,7 +221,7 @@ int __init find_unallocated_memory(const struct kernel_info *kinfo,
     }
 
     start = 0;
-    end = (1ULL << p2m_ipa_bits) - 1;
+    end = (1ULL << p2m_gpa_bits) - 1;
     res = rangeset_report_ranges(unalloc_mem, PFN_DOWN(start), PFN_DOWN(end),
                                  cb, free_regions);
     if ( res )
Signature · 1 line
2.53.0
Reviewed-by on v4 5/11
#22 lore raw

Re: [PATCH v4 05/11] xen: rename p2m_ipa_bits to p2m_gpa_bits

Hi Oleksii,

I would rephrase “xen: rename p2m_ipa_bits to p2m_gpa_bits” into something like “xen/device-tree: use p2m_gpa_bits in common code”, because we are not really renaming p2m_ipa_bits, we are only defining an alias.

Quoted 10 lines · Oleksii Kurochko

The IPA terminology is Arm-specific, so rename p2m_ipa_bits to p2m_gpa_bits to use architecture-neutral naming in xen/common/device-tree/ code.

No functional changes.

Quoted 14 lines

---
Changes in v4:
- Nothing changed only rebase.
---
Changes in v3:
- Introduce #define p2m_gpa_bits p2m_ipa_bits for Arm instead of renaming of p2m_ipa_bits to p2m_gpa_bits to keep Arm part of changes clearer and keep using Arm-specific terminolgy inside Arm code.
---
Changes in v2:
- New patch.
---
---

If the maintainer agrees and the title is fixed:

Cheers,
Luca

#23 lore raw

Re: [PATCH v4 05/11] xen: rename p2m_ipa_bits to p2m_gpa_bits

Hello Luca,

Quoted 5 lines · Luca Fancellu

Hi Oleksii,

I would rephrase “xen: rename p2m_ipa_bits to p2m_gpa_bits” into something like “xen/device-tree: use p2m_gpa_bits in common code”, because we are not really renaming p2m_ipa_bits, we are only defining an alias.

Make sense. I will rephrase commit message according to your suggestion.

Quoted 26 lines
Quoted 11 lines · Oleksii Kurochko

The IPA terminology is Arm-specific, so rename p2m_ipa_bits to p2m_gpa_bits to use architecture-neutral naming in xen/common/device-tree/ code.

No functional changes.

Quoted 14 lines

---
Changes in v4:
- Nothing changed only rebase.
---
Changes in v3:
- Introduce #define p2m_gpa_bits p2m_ipa_bits for Arm instead of renaming of p2m_ipa_bits to p2m_gpa_bits to keep Arm part of changes clearer and keep using Arm-specific terminolgy inside Arm code.
---
Changes in v2:
- New patch.
---
---

Quoted 3 lines

If the maintainer agrees and the title is fixed:

Thanks.

~ Oleksii

#24 lore raw

PATCH v4 6/11 xen/riscv: implement copy_to_guest_phys()

In reply to Oleksii Kurochko

Introduce copy_to_guest_phys() for RISC-V, based on the Arm implementation.

Add a generic copy_guest() helper for copying to and from guest physical (and potentially virtual addresses in the future), and implement translate_get_page() to translate a guest physical address into a struct page_info via the domain p2m.

Compared to the Arm code:
- Drop COPY_flush_dcache(), as no such use cases exist on RISC-V.
- Do not implement the linear mapping case, which is currently unused.
- Use PAGE_OFFSET() to initialize the local offset variable in copy_guest().

---
Changes in v4:
- Avoid using of min_t. (sync with Arm)
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in v3:
- s/if (page == NULL)/if ( !page ).
- Drop " - offset" for unmap_domain_page() inside copy_guest() function. unmap_domain_page() is expected to mask (or something similar) the page offset bits.
- Change some types for functions as copy_to_guest_phys_cb function pointer prototype was changed too in the prev. commit.
---
Changes in v2:
- Use BIT() instead of open-coding.
- Rename COPY_ipa to COPY_gpa.
- Rename COPY_linear to COPY_gva.
- Use BUG_ON(linear) instead if (lineer) + BUG_ON.
- Rename arg liner to gva for translate_get_page().
- Update translate_get_page() to properly handling write argument.
- Return unsigned int for copy_guest() and copy_to_guest_phys() as len function parameter is only 'unsigned int'.
- Reformat function arguments for alignment
---

---
 xen/arch/riscv/Makefile                   |   1 +
 xen/arch/riscv/guestcopy.c                | 116 ++++++++++++++++++++++
 xen/arch/riscv/include/asm/guest_access.h |   7 ++
 3 files changed, 124 insertions(+)
 create mode 100644 xen/arch/riscv/guestcopy.c
xen/arch/riscv/Makefile +1 -0
index 04f02ad89cba..cfc3fdf7d208 100644
@@ -4,6 +4,7 @@ obj-y += domain.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-y += entry.o
 obj-y += extable.o
+obj-y += guestcopy.o
 obj-y += imsic.o
 obj-y += intc.o
 obj-y += irq.o
xen/arch/riscv/guestcopy.c new file +116 -0

116 lines not on this page · raw message

xen/arch/riscv/include/asm/guest_access.h +7 -0
index 7cd51fbbdead..8d679319ded0 100644
@@ -2,6 +2,10 @@
 #ifndef ASM__RISCV__GUEST_ACCESS_H
 #define ASM__RISCV__GUEST_ACCESS_H
 
+#include <xen/types.h>
+
+struct domain;
+
 unsigned long raw_copy_to_guest(void *to, const void *from, unsigned len);
 unsigned long raw_copy_from_guest(void *to, const void *from, unsigned len);
 unsigned long raw_clear_guest(void *to, unsigned int len);
@@ -18,6 +22,9 @@ unsigned long raw_clear_guest(void *to, unsigned int len);
 #define guest_handle_okay(hnd, nr) (1)
 #define guest_handle_subrange_okay(hnd, first, last) (1)
 
+unsigned long copy_to_guest_phys(struct domain *d, paddr_t gpa, void *buf,
+                                 unsigned long len);
+
 #endif /* ASM__RISCV__GUEST_ACCESS_H */
 /*
  * Local variables:
Signature · 1 line
2.53.0
#25 lore raw

PATCH v4 7/11 xen/riscv: add Linux kernel loading support

In reply to Oleksii Kurochko

Introduce support for loading a Linux kernel Image which is got by uncompressing of Image.gz on RISC-V.

kernel_image_load() and place_modules() currently call panic() on failure rather than returning an error. This is because the common kernel_load() in common/device-tree/kernel.c does not expect a return code. Handling errors gracefully would require a separate refactor.

The implementation is based on the Xen Arm kernel loading code.

text_offset is available for both 32-bit and 64-bit Image, so fix that.

---
Changes in v4:
- Update the patch subject: add "... Linux kernel ...".
- Make bi variable unsigned. And use while() instead of for().
- Update the condition which calculates overlapping of kernel with modules.
- Reject a malformed image before the loop to avoid wrapping load_addr + image_size in the per-bankcheck below.
- Use mathematical representation to print ranges.
- Use #error instead of just returning unsupported.
- Update the comment above stuct image in kernel_image64_probe().
---

---
 xen/arch/riscv/Makefile             |   1 +
 xen/arch/riscv/include/asm/config.h |  13 ++
 xen/arch/riscv/kernel.c             | 242 ++++++++++++++++++++++++++++
 xen/include/xen/fdt-kernel.h        |  11 +-
 4 files changed, 265 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/riscv/kernel.c
xen/arch/riscv/Makefile +1 -0
index cfc3fdf7d208..eecdcbc76867 100644
@@ -8,6 +8,7 @@ obj-y += guestcopy.o
 obj-y += imsic.o
 obj-y += intc.o
 obj-y += irq.o
+obj-y += kernel.init.o
 obj-y += mm.o
 obj-y += p2m.o
 obj-y += paging.o
xen/arch/riscv/include/asm/config.h +13 -0
index 0613de008b13..fd69057826e1 100644
@@ -151,6 +151,19 @@
 extern unsigned long phys_offset; /* = load_start - XEN_VIRT_START */
 #endif
 
+/*
+ * KERNEL_LOAD_ADDR_ALIGNMENT is defined based on paragraph of
+ * "Kernel location" of boot.rst:
+ * https://docs.kernel.org/arch/riscv/boot.html#kernel-location
+ */
+#if defined(CONFIG_RISCV_32)
+#define KERNEL_LOAD_ADDR_ALIGNMENT MB(4)
+#elif defined(CONFIG_RISCV_64)
+#define KERNEL_LOAD_ADDR_ALIGNMENT MB(2)
+#else
+#error "Define KERNEL_LOAD_ADDR_ALIGNMENT"
+#endif
+
 #endif /* ASM__RISCV__CONFIG_H */
 /*
  * Local variables:
xen/arch/riscv/kernel.c new file +242 -0

242 lines not on this page · raw message

xen/include/xen/fdt-kernel.h +9 -2
index 54be77aa191e..c69da642eba8 100644
@@ -59,8 +59,15 @@ struct kernel_info {
         struct {
             paddr_t kernel_addr;
             paddr_t len;
-#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV_64)
-            paddr_t text_offset; /* 64-bit Image only */
+#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV)
+            /*
+             * ARM: 64-bit Image only.
+             * RISC-V: both 32-bit and 64-bit Images.
+             */
+            paddr_t text_offset;
+#endif
+#if defined(CONFIG_RISCV)
+            uint64_t image_size; /* Effective size of Image */
 #endif
             paddr_t start; /* Must be 0 for 64-bit Image */
         } image;
Signature · 1 line
2.53.0
#26 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 19 lines · Oleksii Kurochko
xen/arch/riscv/kernel.c new file +16 -0
@@ -0,0 +1,242 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bug.h>
+#include <xen/compiler.h>
+#include <xen/errno.h>
+#include <xen/fdt-kernel.h>
+#include <xen/guest_access.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+#include <xen/types.h>
+#include <xen/vmap.h>
+
+#include <asm/setup.h>
+
+#define IMAGE64_MAGIC_V2 0x05435352 /* Magic number 2, le, "RSC\x05" */

xen/arch/riscv/kernel.c line 26

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+
+static void __init place_modules(struct kernel_info *info, paddr_t kernbase,
+                                 paddr_t kernend)
+{
+    const struct boot_module *mod = info->bd.initrd;
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0,
+                                       KERNEL_LOAD_ADDR_ALIGNMENT);
+    const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt),
+                                    KERNEL_LOAD_ADDR_ALIGNMENT);

Why would modules need to be this strongly aligned?

xen/arch/riscv/kernel.c line 30

Quoted 4 lines
xen/arch/riscv/kernel.c new file +4 -0
+    const paddr_t modsize = initrd_len + dtb_len;
+    unsigned int bi = banks->nr_banks;
+
+    BUG_ON(modsize < initrd_len);

Where's the earlier check that allows this to be BUG_ON()?

Quoted 4 lines
xen/arch/riscv/kernel.c new file +4 -0
+    /*
+     * Place modules as high in RAM as possible, scanning banks from
+     * last to first so that the end of the last bank is preferred.
+     */

xen/arch/riscv/kernel.c line 45

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    while ( bi-- > 0 )
+    {
+        const struct membank *bank = &banks->bank[bi];
+        const paddr_t bank_end = bank->start + bank->size;
+        paddr_t modbase;
+
+        if ( modsize > bank->size )
+            continue;
+
+        modbase = ROUNDDOWN(bank_end - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

Same question here.

xen/arch/riscv/kernel.c line 52

Quoted 6 lines
xen/arch/riscv/kernel.c new file +6 -0
+        if ( modbase < bank->start )
+            continue;
+
+        /*
+         * If modules would overlap the kernel, try placing them below it.
+         */

With how kernel_image_place() works, and with the heavy alignment applied above, is this even possible to succeed? Oh, wait, yes - for the not- position-independent case.

xen/arch/riscv/kernel.c line 56

Quoted 4 lines
xen/arch/riscv/kernel.c new file +4 -0
+        if ( (modbase < ROUNDUP(kernend, KERNEL_LOAD_ADDR_ALIGNMENT)) &&
+             (modbase + modsize > kernbase) )
+        {
+            modbase = ROUNDDOWN(kernbase - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

What prevents this subtraction from underflowing?

xen/arch/riscv/kernel.c line 78

Quoted 9 lines
xen/arch/riscv/kernel.c new file +9 -0
+static paddr_t __init kernel_image_place(struct kernel_info *info)
+{
+    paddr_t load_addr = INVALID_PADDR;
+    uint64_t image_size = info->image.image_size ?: info->image.len;
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    unsigned int nr_banks = banks->nr_banks;
+    unsigned int bi;
+
+    dprintk(XENLOG_DEBUG, "nr_banks(%u)\n", nr_banks);

Did you mean to drop this before submitting?

Quoted 25 lines
xen/arch/riscv/kernel.c new file +25 -0
+    /*
+     * At the moment, RISC-V's Linux kernel should be always position
+     * independent based on "Per-MMU execution" of boot.rst:
+     *   https://docs.kernel.org/arch/riscv/boot.html#pre-mmu-execution
+     *
+     * But just for the case when RISC-V's Linux kernel isn't position
+     * independent it is needed to take load address from
+     * info->image.start.
+     *
+     * If `start` is zero, the Image is position independent.
+     */
+    if ( likely(!info->image.start) )
+    {
+        for ( bi = 0; bi != nr_banks; bi++ )
+        {
+            const struct membank *bank = &banks->bank[bi];
+            paddr_t bank_start = bank->start;
+            /*
+             * According to boot.rst kernel load address should be properly
+             * aligned:
+             *   https://docs.kernel.org/arch/riscv/boot.html#kernel-location
+             *
+             * As Image in this case is PIC we can ignore
+             * info->image.text_offset.
+             */

xen/arch/riscv/kernel.c line 114

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+            paddr_t aligned_start = ROUNDUP(bank_start, KERNEL_LOAD_ADDR_ALIGNMENT);
+            paddr_t bank_end = bank_start + bank->size;
+            paddr_t bank_size;
+
+            if ( aligned_start > bank_end )
+                continue;
+
+            bank_size = bank_end - aligned_start;
+
+            dprintk(XENLOG_DEBUG, "bank[%u].start=%"PRIpaddr"\n", bi, bank->start);

And this one? (I also find it puzzling that ->start would be of (primary) interest here, when ...

xen/arch/riscv/kernel.c line 116

Quoted 1 line
xen/arch/riscv/kernel.c new file +1 -0
+            if ( image_size <= bank_size )

... bank_size is what's relevant.

Quoted 23 lines
xen/arch/riscv/kernel.c new file +23 -0
+/* Check if the image is a 64-bit Image */
+static int __init kernel_image64_probe(struct kernel_info *info,
+                                       paddr_t addr, paddr_t size)
+{
+    /* https://www.kernel.org/doc/Documentation/riscv/boot-image-header.rst */
+    struct {
+        uint32_t code0;         /* Executable code */
+        uint32_t code1;         /* Executable code */
+        uint64_t text_offset;   /* Image load offset, little endian */
+        uint64_t image_size;    /* Effective Image size, little endian */
+        uint64_t flags;         /* kernel flags, little endian */
+        uint32_t version;       /* Version of this header */
+        uint32_t res1;          /* Reserved */
+        uint64_t res2;          /* Reserved */
+        uint64_t magic;         /* Deprecated: Magic number, little endian, "RISCV" */
+        uint32_t magic2;        /* Magic number 2, little endian, "RSC\x05" */
+        uint32_t res3;          /* Reserved for PE COFF offset */
+    } image;
+    uint64_t effective_size;
+
+    if ( size < sizeof(image) )
+        return -EINVAL;
+

xen/arch/riscv/kernel.c line 219

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    copy_from_paddr(&image, addr, sizeof(image));
+
+    /* Magic v1 is deprecated and may be removed.  Only use v2 */
+    if ( le32_to_cpu(image.magic2) != IMAGE64_MAGIC_V2 )
+        return -EINVAL;
+
+    effective_size = le64_to_cpu(image.image_size);
+
+    if ( effective_size && size > effective_size )
+        return -EINVAL;

Is the rhs of the && the wrong way round? If effective_size > size, aren't you in trouble? Question of course is what "effective" really means. Yet in any event it seems dubious to me that effective_size < size would really be a problem. IOW this will want commenting upon if the check is to stay.

Actually ...

xen/arch/riscv/kernel.c line 225

Quoted 5 lines
xen/arch/riscv/kernel.c new file +5 -0
+    info->image.kernel_addr = addr;
+    /* Actual size in the binary file */
+    info->image.len = size;
+    /* Total memory the kernel occupies at runtime */
+    info->image.image_size = effective_size;

... this looks to suggest something .bss-like.

Quoted 7 lines
xen/include/xen/fdt-kernel.h +0 -1
@@ -59,8 +59,15 @@ struct kernel_info {
         struct {
             paddr_t kernel_addr;
             paddr_t len;
-#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV_64)

xen/include/xen/fdt-kernel.h struct kernel_info { · line 70

Quoted 10 lines
xen/include/xen/fdt-kernel.h +9 -1
-            paddr_t text_offset; /* 64-bit Image only */
+#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV)
+            /*
+             * ARM: 64-bit Image only.
+             * RISC-V: both 32-bit and 64-bit Images.
+             */
+            paddr_t text_offset;
+#endif
+#if defined(CONFIG_RISCV)
+            uint64_t image_size; /* Effective size of Image */

As this (apparently) is for both RV64 and RV32 - can the latter really have wider than 32-bit image sizes? If not - use size_t or unsigned long here?

Jan

#27 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
xen/arch/riscv/kernel.c new file +21 -0
@@ -0,0 +1,242 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bug.h>
+#include <xen/compiler.h>
+#include <xen/errno.h>
+#include <xen/fdt-kernel.h>
+#include <xen/guest_access.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+#include <xen/types.h>
+#include <xen/vmap.h>
+
+#include <asm/setup.h>
+
+#define IMAGE64_MAGIC_V2 0x05435352 /* Magic number 2, le, "RSC\x05" */
+
+static void __init place_modules(struct kernel_info *info, paddr_t kernbase,
+                                 paddr_t kernend)
+{
+    const struct boot_module *mod = info->bd.initrd;
Quoted 8 lines

xen/arch/riscv/kernel.c line 26

Quoted 5 lines
xen/arch/riscv/kernel.c new file +5 -0
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0,
+                                       KERNEL_LOAD_ADDR_ALIGNMENT);
+    const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt),
+                                    KERNEL_LOAD_ADDR_ALIGNMENT);

Why would modules need to be this strongly aligned?

No specific reason except to be aligned with similar alignment below, it could be lesser (PAGE_SIZE or even just unsigned long aligned) or even dropped, I think. It was just easier then to calculate aligned addresses. But I don't see any big issue to have such alignments except maybe that it will waste some memory.

Quoted 7 lines

xen/arch/riscv/kernel.c line 30

Quoted 5 lines
xen/arch/riscv/kernel.c new file +4 -0
+    const paddr_t modsize = initrd_len + dtb_len;
+    unsigned int bi = banks->nr_banks;
+
+    BUG_ON(modsize < initrd_len);

Where's the earlier check that allows this to be BUG_ON()?

There isn't one. I will replace that with:

if ( modsize < initrd_len )

    panic("Module size overflow: initrd + dtb size wraps paddr_t\n");
Quoted 17 lines
Quoted 5 lines
xen/arch/riscv/kernel.c new file +4 -0
+    /*
+     * Place modules as high in RAM as possible, scanning banks from
+     * last to first so that the end of the last bank is preferred.
+     */

xen/arch/riscv/kernel.c line 45

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    while ( bi-- > 0 )
+    {
+        const struct membank *bank = &banks->bank[bi];
+        const paddr_t bank_end = bank->start + bank->size;
+        paddr_t modbase;
+
+        if ( modsize > bank->size )
+            continue;
+
+        modbase = ROUNDDOWN(bank_end - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

Same question here.

I used KERNEL_LOAD_ADDR_ALIGNMENT to be sure that big page tables be potentially used in page table.

Quoted 11 lines

xen/arch/riscv/kernel.c line 52

Quoted 7 lines
xen/arch/riscv/kernel.c new file +6 -0
+        if ( modbase < bank->start )
+            continue;
+
+        /*
+         * If modules would overlap the kernel, try placing them below it.
+         */

With how kernel_image_place() works, and with the heavy alignment applied above, is this even possible to succeed? Oh, wait, yes - for the not- position-independent case.

I don't understand what is wrong with putting initrd and dtb to basically any place except where kernel is expected to be placed (as in case of position dependent case I assume it is pretty crucial).

Quoted 7 lines

xen/arch/riscv/kernel.c line 56

Quoted 5 lines
xen/arch/riscv/kernel.c new file +4 -0
+        if ( (modbase < ROUNDUP(kernend, KERNEL_LOAD_ADDR_ALIGNMENT)) &&
+             (modbase + modsize > kernbase) )
+        {
+            modbase = ROUNDDOWN(kernbase - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

What prevents this subtraction from underflowing?

I will put the following check at the start of the place_modules() function:
if ( kernbase < modsize )
panic("Underflow could happen between kernbase and modsize\n");

Quoted 12 lines

xen/arch/riscv/kernel.c line 78

Quoted 10 lines
xen/arch/riscv/kernel.c new file +9 -0
+static paddr_t __init kernel_image_place(struct kernel_info *info)
+{
+    paddr_t load_addr = INVALID_PADDR;
+    uint64_t image_size = info->image.image_size ?: info->image.len;
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    unsigned int nr_banks = banks->nr_banks;
+    unsigned int bi;
+
+    dprintk(XENLOG_DEBUG, "nr_banks(%u)\n", nr_banks);

Did you mean to drop this before submitting?

Sure, it should be dropped.

Quoted 40 lines
Quoted 26 lines
xen/arch/riscv/kernel.c new file +25 -0
+    /*
+     * At the moment, RISC-V's Linux kernel should be always position
+     * independent based on "Per-MMU execution" of boot.rst:
+     *   https://docs.kernel.org/arch/riscv/boot.html#pre-mmu-execution
+     *
+     * But just for the case when RISC-V's Linux kernel isn't position
+     * independent it is needed to take load address from
+     * info->image.start.
+     *
+     * If `start` is zero, the Image is position independent.
+     */
+    if ( likely(!info->image.start) )
+    {
+        for ( bi = 0; bi != nr_banks; bi++ )
+        {
+            const struct membank *bank = &banks->bank[bi];
+            paddr_t bank_start = bank->start;
+            /*
+             * According to boot.rst kernel load address should be properly
+             * aligned:
+             *   https://docs.kernel.org/arch/riscv/boot.html#kernel-location
+             *
+             * As Image in this case is PIC we can ignore
+             * info->image.text_offset.
+             */

xen/arch/riscv/kernel.c line 114

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+            paddr_t aligned_start = ROUNDUP(bank_start, KERNEL_LOAD_ADDR_ALIGNMENT);
+            paddr_t bank_end = bank_start + bank->size;
+            paddr_t bank_size;
+
+            if ( aligned_start > bank_end )
+                continue;
+
+            bank_size = bank_end - aligned_start;
+
+            dprintk(XENLOG_DEBUG, "bank[%u].start=%"PRIpaddr"\n", bi, bank->start);

And this one? (I also find it puzzling that ->start would be of (primary) interest here, when ...

Quoted 3 lines

xen/arch/riscv/kernel.c line 116

Quoted 1 line
xen/arch/riscv/kernel.c new file +1 -0
+            if ( image_size <= bank_size )

... bank_size is what's relevant.

This one should be dropped too.

Quoted 43 lines
Quoted 24 lines
xen/arch/riscv/kernel.c new file +23 -0
+/* Check if the image is a 64-bit Image */
+static int __init kernel_image64_probe(struct kernel_info *info,
+                                       paddr_t addr, paddr_t size)
+{
+    /* https://www.kernel.org/doc/Documentation/riscv/boot-image-header.rst */
+    struct {
+        uint32_t code0;         /* Executable code */
+        uint32_t code1;         /* Executable code */
+        uint64_t text_offset;   /* Image load offset, little endian */
+        uint64_t image_size;    /* Effective Image size, little endian */
+        uint64_t flags;         /* kernel flags, little endian */
+        uint32_t version;       /* Version of this header */
+        uint32_t res1;          /* Reserved */
+        uint64_t res2;          /* Reserved */
+        uint64_t magic;         /* Deprecated: Magic number, little endian, "RISCV" */
+        uint32_t magic2;        /* Magic number 2, little endian, "RSC\x05" */
+        uint32_t res3;          /* Reserved for PE COFF offset */
+    } image;
+    uint64_t effective_size;
+
+    if ( size < sizeof(image) )
+        return -EINVAL;
+

xen/arch/riscv/kernel.c line 219

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    copy_from_paddr(&image, addr, sizeof(image));
+
+    /* Magic v1 is deprecated and may be removed.  Only use v2 */
+    if ( le32_to_cpu(image.magic2) != IMAGE64_MAGIC_V2 )
+        return -EINVAL;
+
+    effective_size = le64_to_cpu(image.image_size);
+
+    if ( effective_size && size > effective_size )
+        return -EINVAL;

Is the rhs of the && the wrong way round? If effective_size > size, aren't you in trouble? Question of course is what "effective" really means. Yet in any event it seems dubious to me that effective_size < size would really be a problem. IOW this will want commenting upon if the check is to stay.

Actually ...

Quoted 7 lines

xen/arch/riscv/kernel.c line 225

Quoted 5 lines
xen/arch/riscv/kernel.c new file +5 -0
+    info->image.kernel_addr = addr;
+    /* Actual size in the binary file */
+    info->image.len = size;
+    /* Total memory the kernel occupies at runtime */
+    info->image.image_size = effective_size;

... this looks to suggest something .bss-like.

Yes, effective_size it is size which included .bss.

size it of LK after decompression of Image.gz and it doesn't include .bss so it should be lesser then effective_size.

I don't think that I am in trouble that effective_size is bigger then size if we allocate enough space in memory effective_size is fine to be bigger.

It is a good question if effective_size < size is a problem. I think it isn't but could it be really happen?
I think that I am okay to drop that part of if().

Quoted 13 lines
Quoted 13 lines
xen/include/xen/fdt-kernel.h +4 -2
@@ -59,8 +59,15 @@ struct kernel_info {
          struct {
              paddr_t kernel_addr;
              paddr_t len;
-#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV_64)
-            paddr_t text_offset; /* 64-bit Image only */
+#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV)
+            /*
+             * ARM: 64-bit Image only.
+             * RISC-V: both 32-bit and 64-bit Images.
Quoted 8 lines

xen/include/xen/fdt-kernel.h struct kernel_info { · line 70

Quoted 5 lines
xen/include/xen/fdt-kernel.h +5 -0
+             */
+            paddr_t text_offset;
+#endif
+#if defined(CONFIG_RISCV)
+            uint64_t image_size; /* Effective size of Image */

As this (apparently) is for both RV64 and RV32 - can the latter really have wider than 32-bit image sizes? If not - use size_t or unsigned long here?

Agree, unsigned long should be enough.

Thanks.

~ Oleksii

#28 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 33 lines · Oleksii Kurochko
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Oleksii Kurochko
xen/arch/riscv/kernel.c new file +26 -0
@@ -0,0 +1,242 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bug.h>
+#include <xen/compiler.h>
+#include <xen/errno.h>
+#include <xen/fdt-kernel.h>
+#include <xen/guest_access.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+#include <xen/types.h>
+#include <xen/vmap.h>
+
+#include <asm/setup.h>
+
+#define IMAGE64_MAGIC_V2 0x05435352 /* Magic number 2, le, "RSC\x05" */
+
+static void __init place_modules(struct kernel_info *info, paddr_t kernbase,
+                                 paddr_t kernend)
+{
+    const struct boot_module *mod = info->bd.initrd;
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0,
+                                       KERNEL_LOAD_ADDR_ALIGNMENT);
+    const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt),
+                                    KERNEL_LOAD_ADDR_ALIGNMENT);
Quoted 6 lines
Quoted 1 line

Why would modules need to be this strongly aligned?

No specific reason except to be aligned with similar alignment below, it could be lesser (PAGE_SIZE or even just unsigned long aligned) or even dropped, I think. It was just easier then to calculate aligned addresses. But I don't see any big issue to have such alignments except maybe that it will waste some memory.

Or result in there not being enough memory to hold everything.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/kernel.c new file +8 -0
+    /*
+     * Place modules as high in RAM as possible, scanning banks from
+     * last to first so that the end of the last bank is preferred.
+     */
+    while ( bi-- > 0 )
+    {
+        const struct membank *bank = &banks->bank[bi];
+        const paddr_t bank_end = bank->start + bank->size;
Quoted 8 lines

xen/arch/riscv/kernel.c line 45

Quoted 6 lines
xen/arch/riscv/kernel.c new file +6 -0
+        paddr_t modbase;
+
+        if ( modsize > bank->size )
+            continue;
+
+        modbase = ROUNDDOWN(bank_end - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

Same question here.

Quoted 2 lines

I used KERNEL_LOAD_ADDR_ALIGNMENT to be sure that big page tables be potentially used in page table.

I fear I'm lost. All the modules are temporary entities, aren't they?

Quoted 10 lines
Quoted 6 lines

xen/arch/riscv/kernel.c line 56

Quoted 4 lines
xen/arch/riscv/kernel.c new file +4 -0
+        if ( (modbase < ROUNDUP(kernend, KERNEL_LOAD_ADDR_ALIGNMENT)) &&
+             (modbase + modsize > kernbase) )
+        {
+            modbase = ROUNDDOWN(kernbase - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

What prevents this subtraction from underflowing?

I will put the following check at the start of the place_modules() function:
if ( kernbase < modsize )

    panic("Underflow could happen between kernbase and modsize\n");

Wait - why would this be a legitimate condition to panic?

Quoted 55 lines
Quoted 42 lines
Quoted 23 lines
xen/arch/riscv/kernel.c new file +23 -0
+/* Check if the image is a 64-bit Image */
+static int __init kernel_image64_probe(struct kernel_info *info,
+                                       paddr_t addr, paddr_t size)
+{
+    /* https://www.kernel.org/doc/Documentation/riscv/boot-image-header.rst */
+    struct {
+        uint32_t code0;         /* Executable code */
+        uint32_t code1;         /* Executable code */
+        uint64_t text_offset;   /* Image load offset, little endian */
+        uint64_t image_size;    /* Effective Image size, little endian */
+        uint64_t flags;         /* kernel flags, little endian */
+        uint32_t version;       /* Version of this header */
+        uint32_t res1;          /* Reserved */
+        uint64_t res2;          /* Reserved */
+        uint64_t magic;         /* Deprecated: Magic number, little endian, "RISCV" */
+        uint32_t magic2;        /* Magic number 2, little endian, "RSC\x05" */
+        uint32_t res3;          /* Reserved for PE COFF offset */
+    } image;
+    uint64_t effective_size;
+
+    if ( size < sizeof(image) )
+        return -EINVAL;
+

xen/arch/riscv/kernel.c line 219

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    copy_from_paddr(&image, addr, sizeof(image));
+
+    /* Magic v1 is deprecated and may be removed.  Only use v2 */
+    if ( le32_to_cpu(image.magic2) != IMAGE64_MAGIC_V2 )
+        return -EINVAL;
+
+    effective_size = le64_to_cpu(image.image_size);
+
+    if ( effective_size && size > effective_size )
+        return -EINVAL;

Is the rhs of the && the wrong way round? If effective_size > size, aren't you in trouble? Question of course is what "effective" really means. Yet in any event it seems dubious to me that effective_size < size would really be a problem. IOW this will want commenting upon if the check is to stay.

Actually ...

Quoted 7 lines

xen/arch/riscv/kernel.c line 225

Quoted 5 lines
xen/arch/riscv/kernel.c new file +5 -0
+    info->image.kernel_addr = addr;
+    /* Actual size in the binary file */
+    info->image.len = size;
+    /* Total memory the kernel occupies at runtime */
+    info->image.image_size = effective_size;

... this looks to suggest something .bss-like.

Yes, effective_size it is size which included .bss.

size it of LK after decompression of Image.gz and it doesn't include .bss so it should be lesser then effective_size.

Quoted 6 lines

I don't think that I am in trouble that effective_size is bigger then size if we allocate enough space in memory effective_size is fine to be bigger.

It is a good question if effective_size < size is a problem. I think it isn't but could it be really happen?

A kernel image (file) could have data appended to it, e.g. a certificate. With only small .bss that certificate could end up larger than the .bss size, and hence effective_size < size.

Quoted 1 line

I think that I am okay to drop that part of if().

Please do, unless there is a(nother) reason to have it.

Jan

#29 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 34 lines · Jan Beulich
Quoted 33 lines · Oleksii Kurochko
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Oleksii Kurochko
xen/arch/riscv/kernel.c new file +26 -0
@@ -0,0 +1,242 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bug.h>
+#include <xen/compiler.h>
+#include <xen/errno.h>
+#include <xen/fdt-kernel.h>
+#include <xen/guest_access.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+#include <xen/types.h>
+#include <xen/vmap.h>
+
+#include <asm/setup.h>
+
+#define IMAGE64_MAGIC_V2 0x05435352 /* Magic number 2, le, "RSC\x05" */
+
+static void __init place_modules(struct kernel_info *info, paddr_t kernbase,
+                                 paddr_t kernend)
+{
+    const struct boot_module *mod = info->bd.initrd;
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0,
+                                       KERNEL_LOAD_ADDR_ALIGNMENT);
+    const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt),
+                                    KERNEL_LOAD_ADDR_ALIGNMENT);
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Why would modules need to be this strongly aligned?

No specific reason except to be aligned with similar alignment below, it could be lesser (PAGE_SIZE or even just unsigned long aligned) or even dropped, I think. It was just easier then to calculate aligned addresses. But I don't see any big issue to have such alignments except maybe that it will waste some memory.

Or result in there not being enough memory to hold everything.

Do you prefer than not to have alignment at all?

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 5 lines
xen/arch/riscv/kernel.c new file +4 -0
+    /*
+     * Place modules as high in RAM as possible, scanning banks from
+     * last to first so that the end of the last bank is preferred.
+     */

xen/arch/riscv/kernel.c line 45

Quoted 10 lines
xen/arch/riscv/kernel.c new file +10 -0
+    while ( bi-- > 0 )
+    {
+        const struct membank *bank = &banks->bank[bi];
+        const paddr_t bank_end = bank->start + bank->size;
+        paddr_t modbase;
+
+        if ( modsize > bank->size )
+            continue;
+
+        modbase = ROUNDDOWN(bank_end - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

Same question here.

Quoted 4 lines
Quoted 2 lines

I used KERNEL_LOAD_ADDR_ALIGNMENT to be sure that big page tables be potentially used in page table.

I fear I'm lost. All the modules are temporary entities, aren't they?

They are temporary entities but they should be copied to guest memory, right?

So ioremap() should be called for paddr where module is located and so at least less cycles will be needed to add entries to Xen page tables.

I don't know if it makes sense to have such type of optimizations. If not then probably we don't need alignment here too. I don't see at the moment any alignment requirements for initrd and dtb.

The only theoretical reason why at least for dtb we need requirement is that what Arm mentioned in its booting.rst:

The device tree blob (dtb) must be placed on an 8-byte boundary and must not exceed 2 megabytes in size. Since the dtb will be mapped cacheable using blocks of up to 2 megabytes in size, it must not be placed within any 2M region which must be mapped with any specific attributes.

It likely should be true for RISC-V as I assume this part was copied from Arm port. On other side it doesn't mentioned explicitly in boot.rst of RISC-V in LK.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
xen/arch/riscv/kernel.c new file +4 -0
+        if ( (modbase < ROUNDUP(kernend, KERNEL_LOAD_ADDR_ALIGNMENT)) &&
+             (modbase + modsize > kernbase) )
+        {
+            modbase = ROUNDDOWN(kernbase - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

What prevents this subtraction from underflowing?

I will put the following check at the start of the place_modules() function:
if ( kernbase < modsize )

     panic("Underflow could happen between kernbase and modsize\n");

Wait - why would this be a legitimate condition to panic?

It is legitimate to panic() as common API which leads to place_module() has void in its return type (what should be changed in future, I have this in TODO) and so if something is going wrong in place_module() there is not better option except panic() for now.

But generally i think it was too much to panic and it would be just better to put:
if ( kernbase < modsize )

      continue;

above modbase = ROUNDDOWN(...) so it will just put modules in different bank.

Thanks.

~ Oleksii

#30 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 42 lines · Oleksii Kurochko
Quoted 41 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
xen/arch/riscv/kernel.c new file +21 -0
@@ -0,0 +1,242 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bug.h>
+#include <xen/compiler.h>
+#include <xen/errno.h>
+#include <xen/fdt-kernel.h>
+#include <xen/guest_access.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+#include <xen/types.h>
+#include <xen/vmap.h>
+
+#include <asm/setup.h>
+
+#define IMAGE64_MAGIC_V2 0x05435352 /* Magic number 2, le, "RSC\x05" */
+
+static void __init place_modules(struct kernel_info *info, paddr_t kernbase,
+                                 paddr_t kernend)
+{
+    const struct boot_module *mod = info->bd.initrd;
Quoted 8 lines

xen/arch/riscv/kernel.c line 26

Quoted 5 lines
xen/arch/riscv/kernel.c new file +5 -0
+    const struct membanks *banks = kernel_info_get_mem_const(info);
+    const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0,
+                                       KERNEL_LOAD_ADDR_ALIGNMENT);
+    const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt),
+                                    KERNEL_LOAD_ADDR_ALIGNMENT);

Why would modules need to be this strongly aligned?

No specific reason except to be aligned with similar alignment below, it could be lesser (PAGE_SIZE or even just unsigned long aligned) or even dropped, I think. It was just easier then to calculate aligned addresses. But I don't see any big issue to have such alignments except maybe that it will waste some memory.

Quoted 3 lines
Quoted 1 line

Or result in there not being enough memory to hold everything.

Do you prefer than not to have alignment at all?

Some minimal alignment (like to machine word size) may be helpful, for copying to be more efficient. There may also be reasons to use page alignment (e.g. if the pages were to be assigned directly to the domains, without any copying, as we do for PV Dom0 under certain conditions on x86). That in turn may even justify super-page alignment.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/kernel.c new file +8 -0
+    /*
+     * Place modules as high in RAM as possible, scanning banks from
+     * last to first so that the end of the last bank is preferred.
+     */
+    while ( bi-- > 0 )
+    {
+        const struct membank *bank = &banks->bank[bi];
+        const paddr_t bank_end = bank->start + bank->size;
Quoted 8 lines

xen/arch/riscv/kernel.c line 45

Quoted 6 lines
xen/arch/riscv/kernel.c new file +6 -0
+        paddr_t modbase;
+
+        if ( modsize > bank->size )
+            continue;
+
+        modbase = ROUNDDOWN(bank_end - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

Same question here.

I used KERNEL_LOAD_ADDR_ALIGNMENT to be sure that big page tables be potentially used in page table.

Quoted 7 lines
Quoted 1 line

I fear I'm lost. All the modules are temporary entities, aren't they?

They are temporary entities but they should be copied to guest memory, right?

So ioremap() should be called for paddr where module is located and so at least less cycles will be needed to add entries to Xen page tables.

Okay, that would be a small win in time for perhaps a boot failure when the modules would fit in memory if they weren't this heavily aligned. You're judgement, but please whatever higher-than-expected alignment you decide to use, please comment upon this.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

xen/arch/riscv/kernel.c line 56

Quoted 4 lines
xen/arch/riscv/kernel.c new file +4 -0
+        if ( (modbase < ROUNDUP(kernend, KERNEL_LOAD_ADDR_ALIGNMENT)) &&
+             (modbase + modsize > kernbase) )
+        {
+            modbase = ROUNDDOWN(kernbase - modsize, KERNEL_LOAD_ADDR_ALIGNMENT);

What prevents this subtraction from underflowing?

I will put the following check at the start of the place_modules() function:
if ( kernbase < modsize )

     panic("Underflow could happen between kernbase and modsize\n");
Quoted 6 lines
Quoted 1 line

Wait - why would this be a legitimate condition to panic?

It is legitimate to panic() as common API which leads to place_module() has void in its return type (what should be changed in future, I have this in TODO) and so if something is going wrong in place_module() there is not better option except panic() for now.

Feels like you're mixing two things here. I didn't ask whether panic() was appropriate to use here, but whether the condition is one upon which panic()ing is the only option (right now). And voila, ...

Quoted 6 lines

But generally i think it was too much to panic and it would be just better to put:
if ( kernbase < modsize )

       continue;

above modbase = ROUNDDOWN(...) so it will just put modules in different bank.

... looks like the situation can be handled without panic().

Jan

#31 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 11 lines
xen/include/xen/fdt-kernel.h +3 -2
@@ -59,8 +59,15 @@ struct kernel_info {
          struct {
              paddr_t kernel_addr;
              paddr_t len;
-#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV_64)
-            paddr_t text_offset; /* 64-bit Image only */
+#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV)
+            /*
+             * ARM: 64-bit Image only.
Quoted 8 lines

xen/include/xen/fdt-kernel.h struct kernel_info { · line 70

Quoted 6 lines
xen/include/xen/fdt-kernel.h +6 -0
+             * RISC-V: both 32-bit and 64-bit Images.
+             */
+            paddr_t text_offset;
+#endif
+#if defined(CONFIG_RISCV)
+            uint64_t image_size; /* Effective size of Image */

As this (apparently) is for both RV64 and RV32 - can the latter really have wider than 32-bit image sizes? If not - use size_t or unsigned long here?

It seems like we want to have uint64_t as it is explicitly mentioned in image header:

https://elixir.bootlin.com/linux/v7.0.1/source/arch/riscv/include/asm/image.h#L57

and also it is used .dword here:

https://elixir.bootlin.com/linux/v7.0.1/source/arch/riscv/kernel/head.S#L55

~ Oleksii

#32 lore raw

Re: [PATCH v4 07/11] xen/riscv: add Linux kernel loading support

Quoted 23 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich
Quoted 11 lines
xen/include/xen/fdt-kernel.h +3 -2
@@ -59,8 +59,15 @@ struct kernel_info {
          struct {
              paddr_t kernel_addr;
              paddr_t len;
-#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV_64)
-            paddr_t text_offset; /* 64-bit Image only */
+#if defined(CONFIG_ARM_64) || defined(CONFIG_RISCV)
+            /*
+             * ARM: 64-bit Image only.
Quoted 8 lines

xen/include/xen/fdt-kernel.h struct kernel_info { · line 70

Quoted 6 lines
xen/include/xen/fdt-kernel.h +6 -0
+             * RISC-V: both 32-bit and 64-bit Images.
+             */
+            paddr_t text_offset;
+#endif
+#if defined(CONFIG_RISCV)
+            uint64_t image_size; /* Effective size of Image */

As this (apparently) is for both RV64 and RV32 - can the latter really have wider than 32-bit image sizes? If not - use size_t or unsigned long here?

Quoted 8 lines

It seems like we want to have uint64_t as it is explicitly mentioned in image header:

https://elixir.bootlin.com/linux/v7.0.1/source/arch/riscv/include/asm/image.h#L57

and also it is used .dword here:

https://elixir.bootlin.com/linux/v7.0.1/source/arch/riscv/kernel/head.S#L55

Well. The image header having a 64-bit wide field still raises the question how the value being wider than 32 bits would work on RV32. IOW for Xen internal purposes I expect unsigned long or size_t would be more appropriate. When that value is taken from the image header and copied into the internal struct, it would need checking for absence of truncation.

Jan

#33 lore raw

PATCH v4 8/11 xen/riscv: rework G-stage mode handling

In reply to Oleksii Kurochko

Rework G-stage mode handling to make the selected mode descriptor reusable outside of p2m initialization, both for filling CPU nodes in the device tree passed to dom0less guests and for per-domain G-stage mode selection at domain creation time.

Promote gstage_modes[] from a local __initconst variable inside gstage_mode_detect() to a file-scope static const array, and convert max_gstage_mode from an embedded struct (assigned by value) to a global const pointer into gstage_modes[]. This allows referencing both the mode identifier and the mode name after init without copying the descriptor. Remove get_max_supported_mode(); its callers now dereference max_gstage_mode->mode directly.

Change struct p2m_domain::mode from an embedded gstage_mode_desc to a const pointer into gstage_modes[], so each domain shares the descriptor rather than carrying its own copy.

Adjust the gstage_modes[] entries in three ways:
- Use lowercase names without the "x4" suffix (e.g. "sv39" instead of "Sv39x4"). The DT mmu-type binding [1] does not include the suffix, so the name can now be passed directly to the guest without transformation. The suffix is appended only in the diagnostic printk, where it remains informative.
- Use "none" for Bare mode (HGATP_MODE_OFF) to match the DT binding.
- Change paging_levels to represent the root page-table level index (i.e. total paging levels minus one) rather than the total count. P2M_ROOT_LEVEL() now returns the correct VPN index directly, without requiring callers to subtract one or use hardcoded offsets.

Add gstage_mode to xen_arch_domainconfig so the toolstack can request a specific G-stage mode at domain creation time. Introduce find_gstage_mode() to resolve a mode descriptor by HGATP_MODE_*, capping the result at max_gstage_mode to prevent requesting a mode the hardware does not support. Update p2m_init() to accept a xen_domctl_createdomain pointer and call find_gstage_mode() instead of hardcoding Sv39x4.

Add arch_parse_dom0less_node() in a new dom0less-build.c to read the "mmu-type" DT property from a guest domain node and store it in boot_domain::create_cfg.arch.gstage_mode, falling back to maximum supported mode when the property is absent.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/riscv/cpus.yaml?h=v6.19-rc3#n82

---
Changes in v4:
- Stray blank after * in declaration of find_gstage_mode().
- Refactor find_gstage_mode(). Now it is find_gstage_mode_by_bits().
- Add __ro_after_init for static local variable m in p2m_init.
- s/ char gstage_mode[8];/unsigned char gstage_mode; in struct vcpu_guest_context.
- s/modes/gstage_modes in p2m.c.
- Introduce P2M_EXTRA_BITS_AT_LEVEL and re-use P2M_ROOT_EXTRA_BITS to encode magic constant 2.
---
Changes in v3:
- New patch.
---

---
 xen/arch/riscv/Makefile          |   1 +
 xen/arch/riscv/dom0less-build.c  |  70 ++++++++++++++++++
 xen/arch/riscv/include/asm/p2m.h |  18 +++--
 xen/arch/riscv/p2m.c             | 120 +++++++++++++++++++++----------
 xen/arch/riscv/vmid.c            |   2 +-
 xen/include/public/arch-riscv.h  |   5 ++
 6 files changed, 170 insertions(+), 46 deletions(-)
 create mode 100644 xen/arch/riscv/dom0less-build.c
xen/arch/riscv/Makefile +1 -0
index eecdcbc76867..8f7fd625dddd 100644
@@ -1,6 +1,7 @@
 obj-y += aplic.o
 obj-y += cpufeature.o
 obj-y += domain.o
+obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-build.init.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-y += entry.o
 obj-y += extable.o
xen/arch/riscv/dom0less-build.c new file +70 -0

70 lines not on this page · raw message

xen/arch/riscv/include/asm/p2m.h +12 -6
index 54ea67990f06..638c60ddc2f7 100644
@@ -13,7 +13,7 @@
 
 #define P2M_ROOT_ORDER  (ilog2(GSTAGE_ROOT_PAGE_TABLE_SIZE) - PAGE_SHIFT)
 #define P2M_ROOT_PAGES  BIT(P2M_ROOT_ORDER, U)
-#define P2M_ROOT_LEVEL(p2m) ((p2m)->mode.paging_levels)
+#define P2M_ROOT_LEVEL(p2m) ((p2m)->mode->paging_levels)
 
 /*
  * According to the RISC-V spec:
@@ -32,10 +32,13 @@
  */
 #define P2M_LEVEL_ORDER(lvl) XEN_PT_LEVEL_ORDER(lvl)
 
-#define P2M_ROOT_EXTRA_BITS(p2m, lvl) (2 * ((lvl) == P2M_ROOT_LEVEL(p2m)))
+#define P2M_ROOT_EXTRA_BITS 2
+
+#define P2M_LEVEL_EXTRA_BITS(p2m, lvl) \
+    (P2M_ROOT_EXTRA_BITS * ((lvl) == P2M_ROOT_LEVEL(p2m)))
 
 #define P2M_PAGETABLE_ENTRIES(p2m, lvl) \
-    (BIT(PAGETABLE_ORDER + P2M_ROOT_EXTRA_BITS(p2m, lvl), UL))
+    (BIT(PAGETABLE_ORDER + P2M_LEVEL_EXTRA_BITS(p2m, lvl), UL))
 
 #define P2M_TABLE_OFFSET(p2m, lvl) (P2M_PAGETABLE_ENTRIES(p2m, lvl) - 1UL)
 
@@ -55,6 +58,8 @@ struct gstage_mode_desc {
     char name[8];
 };
 
+extern const struct gstage_mode_desc *max_gstage_mode;
+
 /* Per-p2m-table state */
 struct p2m_domain {
     /*
@@ -68,7 +73,7 @@ struct p2m_domain {
     /* The root of the p2m tree. May be concatenated */
     struct page_info *root;
 
-    struct gstage_mode_desc mode;
+    const struct gstage_mode_desc *mode;
 
     /* Back pointer to domain */
     struct domain *domain;
@@ -215,9 +220,10 @@ static inline bool arch_acquire_resource_check(struct domain *d)
 }
 
 void guest_mm_init(void);
-unsigned char get_max_supported_mode(void);
 
-int p2m_init(struct domain *d);
+struct xen_domctl_createdomain;
+
+int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config);
 
 static inline void p2m_write_lock(struct p2m_domain *p2m)
 {
xen/arch/riscv/p2m.c +81 -39

184 lines not on this page · raw message

xen/arch/riscv/vmid.c +1 -1
index 8fbcd500f24d..11c7e9d6d6c8 100644
@@ -52,7 +52,7 @@ static DEFINE_PER_CPU(struct vmid_data, vmid_data);
 static unsigned int vmidlen_detect(void)
 {
     unsigned int vmid_bits;
-    unsigned char gstage_mode = get_max_supported_mode();
+    unsigned char gstage_mode = max_gstage_mode->mode;
 
     /*
      * According to the RISC-V Privileged Architecture Spec:
xen/include/public/arch-riscv.h +5 -0
index 360d8e6871ba..da4cc0cbfcfb 100644
@@ -56,6 +56,11 @@ typedef struct vcpu_guest_context vcpu_guest_context_t;
 DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
 
 struct xen_arch_domainconfig {
+    /*
+     * G-stage GPA address width in bits.
+     * Valid values: 34 (sv32x4), 41 (sv39x4), 50 (sv48x4), 59 (sv57x4).
+     */
+    unsigned char gstage_addr_bits;
 };
 
 #endif
Signature · 1 line
2.53.0
#34 lore raw

Re: [PATCH v4 08/11] xen/riscv: rework G-stage mode handling

Quoted 56 lines · Oleksii Kurochko
xen/arch/riscv/dom0less-build.c new file +53 -0
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bootfdt.h>
+#include <xen/device_tree.h>
+#include <xen/init.h>
+
+#include <asm/p2m.h>
+
+int __init arch_parse_dom0less_node(struct dt_device_node *node,
+                                    struct boot_domain *bd)
+{
+    const char *mmu_type;
+    unsigned long bits;
+    const char *end;
+
+    if ( dt_property_read_string(node, "mmu-type", &mmu_type) )
+    {
+        dprintk(XENLOG_WARNING, "mmu-type property is missing in guest domain "
+                "node. %s will be used as fallback\n", max_gstage_mode->name);
+
+        bits = P2M_GFN_LEVEL_SHIFT(max_gstage_mode->paging_levels + 1);
+
+        goto out;
+    }
+
+    if ( !strcasecmp(mmu_type, "riscv,none") )
+    {
+        dprintk(XENLOG_ERR, "Bare mode isn't supported by Xen\n");
+
+        return -EOPNOTSUPP;
+    }
+
+    if ( strncasecmp(mmu_type, "riscv,sv", 8) )
+    {
+        dprintk(XENLOG_ERR, "mmu-type value \"%s\" is incorrect\n", mmu_type);
+
+        return -EINVAL;
+    }
+
+    bits = simple_strtoul(mmu_type + 8, &end, 10);
+    if ( (*end != '\0') || (end == mmu_type + 8) )
+    {
+        dprintk(XENLOG_ERR, "mmu-type value \"%s\" is incorrect\n", mmu_type);
+
+        return -EINVAL;
+    }
+
+ out:
+    if ( bits > (UINT8_MAX - P2M_ROOT_EXTRA_BITS) )
+    {
+        dprintk(XENLOG_ERR, "gstage addr bits value overflows uint8\n");
+
+        return -EINVAL;

xen/arch/riscv/dom0less-build.c line 63

Quoted 10 lines
xen/arch/riscv/dom0less-build.c new file +10 -0
+    }
+    /*
+     * The correct value of bits will be checked in p2m_init() by call of
+     * find_gstage_mode_by_bits().
+     *
+     * As mmu-type property contains one of string:
+     *  - riscv,sv32
+     *  - riscv,sv39
+     *  - riscv,sv48
+     *  - riscv,sv57

Or about any other riscv,sv<N> with N up to somewhere around 250. I see that ...

xen/arch/riscv/dom0less-build.c line 67

Quoted 4 lines
xen/arch/riscv/dom0less-build.c new file +4 -0
+     * it is needed to add '+P2M_ROOT_EXTRA_BITS' as for G-stage mode GPAs
+     * are extended by P2M_ROOT_EXTRA_BITS.
+     */
+    bd->create_cfg.arch.gstage_addr_bits = bits + P2M_ROOT_EXTRA_BITS;

... the value calculated here is later checked for validity, so it's really only the comment which may want clarifying a little.

Quoted 20 lines
xen/arch/riscv/p2m.c +10 -4
@@ -45,12 +45,27 @@ struct p2m_pte_ctx {
     unsigned int level;          /* Paging level at which the PTE resides. */
 };
 
-static struct gstage_mode_desc __ro_after_init max_gstage_mode = {
-    .mode = HGATP_MODE_OFF,
-    .paging_levels = 0,
-    .name = "Bare",
+/* Values should be sorted by ->mode in this array */
+static const struct gstage_mode_desc gstage_modes[] = {
+    /*
+     * Based on the RISC-V spec:
+     *   Bare mode is always supported, regardless of SXLEN.
+     *   When SXLEN=32, the only other valid setting for MODE is Sv32.
+     *   When SXLEN=64, three paged virtual-memory schemes are defined:
+     *   Sv39, Sv48, and Sv57.
+     */
+    { HGATP_MODE_OFF,    0, "none" },

xen/arch/riscv/p2m.c struct p2m_pte_ctx { · line 67

Quoted 10 lines
xen/arch/riscv/p2m.c +8 -0
+#ifdef CONFIG_RISCV_32
+    { HGATP_MODE_SV32X4, 1, "sv32" },
+#else
+    { HGATP_MODE_SV39X4, 2, "sv39" },
+    { HGATP_MODE_SV48X4, 3, "sv48" },
+    { HGATP_MODE_SV57X4, 4, "sv57" },
+#endif
 };
 
+const struct gstage_mode_desc * __ro_after_init max_gstage_mode = &gstage_modes[0];

Nit: Overlong line (and, strictly speaking, a stray blank after *).

xen/arch/riscv/p2m.c static int p2m_alloc_root_table(struct p2m_domain *p2m) · line 327

Quoted 6 lines
xen/arch/riscv/p2m.c +1 -1
@@ -331,8 +324,35 @@ static int p2m_alloc_root_table(struct p2m_domain *p2m)
     return 0;
 }
 
-int p2m_init(struct domain *d)
+static const struct gstage_mode_desc *find_gstage_mode_by_bits(

Is "_by_bits" adding much value to the function name? Especially ...

xen/arch/riscv/p2m.c static int p2m_alloc_root_table(struct p2m_domain *p2m) · line 328

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned char gpa_bits)

... seeing that the parameter name is making things pretty clear?

Quoted 26 lines
xen/arch/riscv/p2m.c +18 -0
+int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config)
 {
+    /*
+     * TODO: This static is a temporary constraint: all guests must use the
+     * same MMU mode because p2m_gpa_bits is not yet per-domain.
+     * Drop this once per-domain p2m_gpa_bits is introduced.
+     */
+    static const struct gstage_mode_desc __ro_after_init *m = &gstage_modes[0];
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     /*
@@ -341,6 +361,33 @@ int p2m_init(struct domain *d)
      */
     p2m->domain = d;
 
+    if ( !config )
+    {
+        dprintk(XENLOG_ERR, "NULL config is passed\n");
+        return -EINVAL;
+    }
+
+    p2m->mode = find_gstage_mode_by_bits(config->arch.gstage_addr_bits);
+
+    if ( !p2m->mode )
+    {
+        dprintk(XENLOG_ERR,

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 384

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+                "Unsupported or unavailable gstage addr bits: %u\n",
+                config->arch.gstage_addr_bits);
+
+        return -EINVAL;
+    }
+
+    if ( m->mode == HGATP_MODE_OFF )
+        m = p2m->mode;
+
+    if ( m->mode != p2m->mode->mode )

Since m always points into gstage_modes[], do you really need the extra indirection to compare the two ->mode fields? You could simply compare the pointers, couldn't you?

xen/include/public/arch-riscv.h typedef struct vcpu_guest_context vcpu_guest_context_t; · line 63

Quoted 11 lines
xen/include/public/arch-riscv.h +5 -0
@@ -56,6 +56,11 @@ typedef struct vcpu_guest_context vcpu_guest_context_t;
 DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
 
 struct xen_arch_domainconfig {
+    /*
+     * G-stage GPA address width in bits.
+     * Valid values: 34 (sv32x4), 41 (sv39x4), 50 (sv48x4), 59 (sv57x4).
+     */
+    unsigned char gstage_addr_bits;

Fixed-width types only in the public interface please.

Also, isn't the field effectively describing the maximum width of a guest (physical) address? In which case - simply gaddr_bits?

Jan

#35 lore raw

Re: [PATCH v4 08/11] xen/riscv: rework G-stage mode handling

Quoted 71 lines · Jan Beulich
Quoted 56 lines · Oleksii Kurochko
xen/arch/riscv/dom0less-build.c new file +53 -0
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/bootfdt.h>
+#include <xen/device_tree.h>
+#include <xen/init.h>
+
+#include <asm/p2m.h>
+
+int __init arch_parse_dom0less_node(struct dt_device_node *node,
+                                    struct boot_domain *bd)
+{
+    const char *mmu_type;
+    unsigned long bits;
+    const char *end;
+
+    if ( dt_property_read_string(node, "mmu-type", &mmu_type) )
+    {
+        dprintk(XENLOG_WARNING, "mmu-type property is missing in guest domain "
+                "node. %s will be used as fallback\n", max_gstage_mode->name);
+
+        bits = P2M_GFN_LEVEL_SHIFT(max_gstage_mode->paging_levels + 1);
+
+        goto out;
+    }
+
+    if ( !strcasecmp(mmu_type, "riscv,none") )
+    {
+        dprintk(XENLOG_ERR, "Bare mode isn't supported by Xen\n");
+
+        return -EOPNOTSUPP;
+    }
+
+    if ( strncasecmp(mmu_type, "riscv,sv", 8) )
+    {
+        dprintk(XENLOG_ERR, "mmu-type value \"%s\" is incorrect\n", mmu_type);
+
+        return -EINVAL;
+    }
+
+    bits = simple_strtoul(mmu_type + 8, &end, 10);
+    if ( (*end != '\0') || (end == mmu_type + 8) )
+    {
+        dprintk(XENLOG_ERR, "mmu-type value \"%s\" is incorrect\n", mmu_type);
+
+        return -EINVAL;
+    }
+
+ out:
+    if ( bits > (UINT8_MAX - P2M_ROOT_EXTRA_BITS) )
+    {
+        dprintk(XENLOG_ERR, "gstage addr bits value overflows uint8\n");
+
+        return -EINVAL;

xen/arch/riscv/dom0less-build.c line 63

Quoted 10 lines
xen/arch/riscv/dom0less-build.c new file +10 -0
+    }
+    /*
+     * The correct value of bits will be checked in p2m_init() by call of
+     * find_gstage_mode_by_bits().
+     *
+     * As mmu-type property contains one of string:
+     *  - riscv,sv32
+     *  - riscv,sv39
+     *  - riscv,sv48
+     *  - riscv,sv57

Or about any other riscv,sv<N> with N up to somewhere around 250. I see that ...

Quoted 7 lines

xen/arch/riscv/dom0less-build.c line 67

Quoted 4 lines
xen/arch/riscv/dom0less-build.c new file +4 -0
+     * it is needed to add '+P2M_ROOT_EXTRA_BITS' as for G-stage mode GPAs
+     * are extended by P2M_ROOT_EXTRA_BITS.
+     */
+    bd->create_cfg.arch.gstage_addr_bits = bits + P2M_ROOT_EXTRA_BITS;

... the value calculated here is later checked for validity, so it's really only the comment which may want clarifying a little.

I will update the comment to:

/*
* The mmu-type property may specify any riscv,sv<N> string, but only the
* following are currently supported:

 *  - riscv,sv32
 *  - riscv,sv39
 *  - riscv,sv48
 *  - riscv,sv57

* Any other value will be rejected by find_gstage_mode_by_bits(). *
* P2M_ROOT_EXTRA_BITS is added because for G-stage mode, GPAs are
* extended by that many bits.
*/

Quoted 30 lines
Quoted 30 lines
xen/arch/riscv/p2m.c +17 -4
@@ -45,12 +45,27 @@ struct p2m_pte_ctx {
      unsigned int level;          /* Paging level at which the PTE resides. */
  };
 
-static struct gstage_mode_desc __ro_after_init max_gstage_mode = {
-    .mode = HGATP_MODE_OFF,
-    .paging_levels = 0,
-    .name = "Bare",
+/* Values should be sorted by ->mode in this array */
+static const struct gstage_mode_desc gstage_modes[] = {
+    /*
+     * Based on the RISC-V spec:
+     *   Bare mode is always supported, regardless of SXLEN.
+     *   When SXLEN=32, the only other valid setting for MODE is Sv32.
+     *   When SXLEN=64, three paged virtual-memory schemes are defined:
+     *   Sv39, Sv48, and Sv57.
+     */
+    { HGATP_MODE_OFF,    0, "none" },
+#ifdef CONFIG_RISCV_32
+    { HGATP_MODE_SV32X4, 1, "sv32" },
+#else
+    { HGATP_MODE_SV39X4, 2, "sv39" },
+    { HGATP_MODE_SV48X4, 3, "sv48" },
+    { HGATP_MODE_SV57X4, 4, "sv57" },
+#endif
  };
 
Quoted 3 lines

xen/arch/riscv/p2m.c struct p2m_pte_ctx { · line 67

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+const struct gstage_mode_desc * __ro_after_init max_gstage_mode = &gstage_modes[0];

Nit: Overlong line (and, strictly speaking, a stray blank after *).

I will put "&gstage_modes[0];" on next line.

Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/p2m.c +0 -0
@@ -331,8 +324,35 @@ static int p2m_alloc_root_table(struct p2m_domain *p2m)
      return 0;
  }
 
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_alloc_root_table(struct p2m_domain *p2m) · line 327

Quoted 2 lines
xen/arch/riscv/p2m.c +1 -1
-int p2m_init(struct domain *d)
+static const struct gstage_mode_desc *find_gstage_mode_by_bits(

Is "_by_bits" adding much value to the function name? Especially ...

xen/arch/riscv/p2m.c static int p2m_alloc_root_table(struct p2m_domain *p2m) · line 328

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned char gpa_bits)

... seeing that the parameter name is making things pretty clear?

Maybe not too much, I will drop that.

Quoted 33 lines
Quoted 33 lines
xen/arch/riscv/p2m.c +24 -0
+int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config)
  {
+    /*
+     * TODO: This static is a temporary constraint: all guests must use the
+     * same MMU mode because p2m_gpa_bits is not yet per-domain.
+     * Drop this once per-domain p2m_gpa_bits is introduced.
+     */
+    static const struct gstage_mode_desc __ro_after_init *m = &gstage_modes[0];
      struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
      /*
@@ -341,6 +361,33 @@ int p2m_init(struct domain *d)
       */
      p2m->domain = d;
 
+    if ( !config )
+    {
+        dprintk(XENLOG_ERR, "NULL config is passed\n");
+        return -EINVAL;
+    }
+
+    p2m->mode = find_gstage_mode_by_bits(config->arch.gstage_addr_bits);
+
+    if ( !p2m->mode )
+    {
+        dprintk(XENLOG_ERR,
+                "Unsupported or unavailable gstage addr bits: %u\n",
+                config->arch.gstage_addr_bits);
+
+        return -EINVAL;
+    }
+
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 384

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    if ( m->mode == HGATP_MODE_OFF )
+        m = p2m->mode;
+
+    if ( m->mode != p2m->mode->mode )

Since m always points into gstage_modes[], do you really need the extra indirection to compare the two ->mode fields? You could simply compare the pointers, couldn't you?

Agree, just "if ( m != p2m->mode )" will work.

Quoted 6 lines
Quoted 6 lines
xen/include/public/arch-riscv.h +0 -0
@@ -56,6 +56,11 @@ typedef struct vcpu_guest_context vcpu_guest_context_t;
  DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
 
Quoted 8 lines

xen/include/public/arch-riscv.h typedef struct vcpu_guest_context vcpu_guest_context_t; · line 63

Quoted 6 lines
xen/include/public/arch-riscv.h +5 -0
  struct xen_arch_domainconfig {
+    /*
+     * G-stage GPA address width in bits.
+     * Valid values: 34 (sv32x4), 41 (sv39x4), 50 (sv48x4), 59 (sv57x4).
+     */
+    unsigned char gstage_addr_bits;

Fixed-width types only in the public interface please.

I will use uint8_t then.

Quoted 3 lines

Also, isn't the field effectively describing the maximum width of a guest (physical) address? In which case - simply gaddr_bits?

gaddr_bits would be okay in this case.

Thanks.

~ Oleksii

#36 lore raw

PATCH v4 9/11 xen/riscv: introduce p2m_gpa_bits

In reply to Oleksii Kurochko

common/device-tree/domain-build.c uses p2m_gpa_bits to determine the upper bound of the GPA space when searching for unused regions, so it must be defined when CONFIG_DOMAIN_BUILD_HELPERS=y.

The variable is initialised to PADDR_BITS and narrowed in p2m_init() to the GPA width of the selected G-stage mode, allowing an external entity (e.g. an IOMMU) to restrict it further if needed.

p2m_gpa_bits is a global rather than a per-domain value, which is acceptable for now because all domains are required to use the same G-stage MMU mode, as dom0less common code allocates it per all domains.

---
Changes in v4:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in v3:
- Update initialization of p2m_gpa_bits and the comment above.
- Rework how p2m_gpa_bits is limited.
- Update the commit message.
---
Changes in v2:
- New patch.
---

---
 xen/arch/riscv/include/asm/p2m.h |  3 +++
 xen/arch/riscv/p2m.c             | 15 +++++++++++++++
 2 files changed, 18 insertions(+)
xen/arch/riscv/include/asm/p2m.h +3 -0
index 638c60ddc2f7..0d1dace1a0d8 100644
@@ -47,6 +47,9 @@
 #define P2M_LEVEL_MASK(p2m, lvl) \
     (P2M_TABLE_OFFSET(p2m, lvl) << P2M_GFN_LEVEL_SHIFT(lvl))
 
+/* Holds the bit size of GPAs in p2m tables */
+extern unsigned int p2m_gpa_bits;
+
 #define paddr_bits PADDR_BITS
 
 /* Get host p2m table */
xen/arch/riscv/p2m.c +15 -0
index 13846bee6a4f..b7c36a75f175 100644
@@ -11,6 +11,8 @@
 #include <xen/sections.h>
 #include <xen/xvmalloc.h>
 
+#include <public/domctl.h>
+
 #include <asm/cpufeature.h>
 #include <asm/csr.h>
 #include <asm/flushtlb.h>
@@ -66,6 +68,12 @@ static const struct gstage_mode_desc gstage_modes[] = {
 
 const struct gstage_mode_desc * __ro_after_init max_gstage_mode = &gstage_modes[0];
 
+/*
+ * Set to the maximum configured support for GPA bits, so the number of GPA
+ * bits can be restricted by an external entity (e.g. IOMMU).
+ */
+unsigned int __ro_after_init p2m_gpa_bits = PADDR_BITS;
+
 static void p2m_free_page(struct p2m_domain *p2m, struct page_info *pg);
 
 static inline void p2m_free_metadata_page(struct p2m_domain *p2m,
@@ -354,6 +362,7 @@ int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config)
      */
     static const struct gstage_mode_desc __ro_after_init *m = &gstage_modes[0];
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    unsigned int gpa_bits;
 
     /*
      * "Trivial" initialisation is now complete.  Set the backpointer so the
@@ -409,6 +418,12 @@ int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config)
 #   error "Add init of p2m->clean_dcache"
 #endif
 
+    gpa_bits = P2M_GFN_LEVEL_SHIFT(p2m->mode->paging_levels + 1) +
+               P2M_ROOT_EXTRA_BITS;
+
+    if ( gpa_bits < p2m_gpa_bits )
+        p2m_gpa_bits = gpa_bits;
+
     return 0;
 }
 
Signature · 1 line
2.53.0
#37 lore raw

PATCH v4 10/11 xen/riscv: add definition of guest RAM banks

In reply to Oleksii Kurochko

The dom0less solution uses defined RAM banks as compile-time constants, so introduce macros to describe guest RAM banks.

The reason for 2 banks is that there is typically always a use case for low memory under 4 GB, but the bank under 4 GB ends up being small because there are other things under 4 GB it can conflict with (interrupt controller, PCI BARs, etc.). So a second bank is added above that MMIO region (starting at 8 GiB) to provide the remaining RAM; the gap between the two banks also exercises code paths handling discontiguous memory. For Sv32 guests (34-bit GPA, 16 GiB addressable), bank0 provides 2 GB (2–4 GB) and the first 8 GB of bank1 (8–16 GB) is accessible.

Extended regions are useful for RISC-V: they could be used to provide a "space" for Linux to map grant mappings.

Despite the fact that for every guest MMU mode the GPA could be up to 56 bits wide (except Sv32 whose GPA is 34 bits), the combined size of both banks is limited to 1018 GB as it is more than enough for most use cases.

Add inclusion of asm/guest-layout.h to asm/domain.h to make dom0less common code build happy.

---
Changes in v4:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in v3:
- Move GUEST_RAM*-related defines to asm/p2m.h instead of public header.
---
Changes in v2:
- New patch.
---

---
 xen/arch/riscv/include/asm/domain.h       |  1 +
 xen/arch/riscv/include/asm/guest-layout.h | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 xen/arch/riscv/include/asm/guest-layout.h
xen/arch/riscv/include/asm/domain.h +1 -0
index 6c48bf13111d..6044ce0feee0 100644
@@ -7,6 +7,7 @@
 #include <xen/xmalloc.h>
 #include <public/hvm/params.h>
 
+#include <asm/guest-layout.h>
 #include <asm/p2m.h>
 #include <asm/vtimer.h>
 
xen/arch/riscv/include/asm/guest-layout.h new file +23 -0
new file mode 100644
index 000000000000..68d95a09394c
@@ -0,0 +1,23 @@
+#ifndef ASM_RISCV_GUEST_LAYOUT_H
+#define ASM_RISCV_GUEST_LAYOUT_H
+
+#include <public/xen.h>
+
+#define GUEST_RAM_BANKS   2
+
+/*
+ * The way to find the extended regions (to be exposed to the guest as unused
+ * address space) relies on the fact that the regions reserved for the RAM
+ * below are big enough to also accommodate such regions.
+ */
+#define GUEST_RAM0_BASE   xen_mk_ullong(0x80000000) /* 2GB of low RAM @ 2GB */
+#define GUEST_RAM0_SIZE   xen_mk_ullong(0x80000000)
+
+#define GUEST_RAM1_BASE   xen_mk_ullong(0x0200000000) /* 1016 GB of RAM @ 8GB */
+#define GUEST_RAM1_SIZE   xen_mk_ullong(0xFE00000000)
+
+/* TODO: allocate these all dynamically */
+#define GUEST_RAM_BANK_BASES   { GUEST_RAM0_BASE, GUEST_RAM1_BASE }
+#define GUEST_RAM_BANK_SIZES   { GUEST_RAM0_SIZE, GUEST_RAM1_SIZE }
+
+#endif /* ASM_RISCV_GUEST_LAYOUT_H */
Signature · 1 line
2.53.0
#38 lore raw

PATCH v4 11/11 xen/riscv: enable DOMAIN_BUILD_HELPERS

In reply to Oleksii Kurochko

Everything is ready to enable DOMAIN_BUILD_HELPER which are necessary for dom0less common code. So enable it.

---
Changes in v4:
- Nothing changed. Only rebase.
---
Changes in v3:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in v2:
- Move introduction of p2m_ipa_bits to separate patch.
- Move to separate patch introduction of guest banks constansts.
---

---
 xen/arch/riscv/Kconfig | 1 +
 1 file changed, 1 insertion(+)
xen/arch/riscv/Kconfig +1 -0
index a5e87c1757f7..41426c205292 100644
@@ -1,5 +1,6 @@
 config RISCV
 	def_bool y
+	select DOMAIN_BUILD_HELPERS
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
 	select GENERIC_UART_INIT
Signature · 1 line
2.53.0