Skip to contentSkip to conversation

Fix RMRR

RFC PATCH Superseded Tiejun Chen 7 participants 125 messages Apr 10, 2015 – May 20, 2015 Arm/actions semi architecture Page 2 of 2

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#79 lore raw

RFC PATCH 9/13 xen: enable XENMEM_set_memory_map in hvm

In reply to Tiejun Chen

This patch enables XENMEM_set_memory_map in hvm. So we can use it to setup the e820 mappings.

---
 xen/arch/x86/hvm/hvm.c | 2 --
 xen/arch/x86/mm.c      | 6 ------
 2 files changed, 8 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -2
index 4734d71..0e05e47 100644
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     switch ( cmd & MEMOP_CMD_MASK )
     {
-    case XENMEM_memory_map:
     case XENMEM_machine_memory_map:
     case XENMEM_machphys_mapping:
         return -ENOSYS;
@@ -4805,7 +4804,6 @@ static long hvm_memory_op_compat32(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     switch ( cmd & MEMOP_CMD_MASK )
     {
-    case XENMEM_memory_map:
     case XENMEM_machine_memory_map:
     case XENMEM_machphys_mapping:
         return -ENOSYS;
xen/arch/x86/mm.c +0 -6
index 45acfb5..c75a8bc 100644
@@ -4693,12 +4693,6 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             return rc;
         }
 
-        if ( is_hvm_domain(d) )
-        {
-            rcu_unlock_domain(d);
-            return -EPERM;
-        }
-
         e820 = xmalloc_array(e820entry_t, fmap.map.nr_entries);
         if ( e820 == NULL )
         {
Signature · 1 line
1.9.1
#81 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

In reply to Tiejun Chen

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4732

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -1
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     switch ( cmd & MEMOP_CMD_MASK )
     {
-    case XENMEM_memory_map:

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map on itself? If not, it should probably replace XENMEM_memory_map here.

Jan

#82 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
Quoted 7 lines

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4732

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -1
      switch ( cmd & MEMOP_CMD_MASK )
      {
-    case XENMEM_memory_map:

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map

Yes. Actually we need to enable two hypercalls here,

#1. XENMEM_set_memory_map --> Set
#2. XENMEM_memory_map --> Get

Quoted 2 lines

on itself? If not, it should probably replace XENMEM_memory_map here.

Just rephrase,

xen: enable XENMEM set/get memory_map in hvm

This patch enables XENMEM_set_memory_map in hvm and then we can use it to setup the e820 mappings, and finally hvmloader can get these mappings with XENMEM_memory_map.

Thanks
Tiejun

#83 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:33, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd, 

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0 -1
      switch ( cmd & MEMOP_CMD_MASK )
      {
-    case XENMEM_memory_map:
Quoted 8 lines
Quoted 3 lines

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map

Yes. Actually we need to enable two hypercalls here,

#1. XENMEM_set_memory_map --> Set
#2. XENMEM_memory_map --> Get

You say "yes" without saying why, and ...

Quoted 10 lines
Quoted 2 lines

on itself? If not, it should probably replace XENMEM_memory_map here.

Just rephrase,

xen: enable XENMEM set/get memory_map in hvm

This patch enables XENMEM_set_memory_map in hvm and then we can use it to setup the e820 mappings, and finally hvmloader can get these mappings with XENMEM_memory_map.

... according to this wording of yours it's not needed.

Jan

#84 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:33, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd,

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 8 lines

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4732

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0 -1
       switch ( cmd & MEMOP_CMD_MASK )
       {
-    case XENMEM_memory_map:

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map

Quoted 6 lines
Quoted 4 lines

Yes. Actually we need to enable two hypercalls here,

#1. XENMEM_set_memory_map --> Set
#2. XENMEM_memory_map --> Get

You say "yes" without saying why, and ...

Instead of constructing e820 in the case of hvmloader, now we'd like to set up a basic e820 while building hvm, so we need to enable XENMEM_set_memory_map/XENMEM_memory_map to own this approach in hvm case.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

on itself? If not, it should probably replace XENMEM_memory_map here.

Just rephrase,

Quoted 8 lines
Quoted 5 lines

xen: enable XENMEM set/get memory_map in hvm

This patch enables XENMEM_set_memory_map in hvm and then we can use it to setup the e820 mappings, and finally hvmloader can get these mappings with XENMEM_memory_map.

... according to this wording of yours it's not needed.

Sorry, anything confound you or me?

Thanks
Tiejun

#85 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:24, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:33, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd,

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 8 lines

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4732

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0 -1
       switch ( cmd & MEMOP_CMD_MASK )
       {
-    case XENMEM_memory_map:

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map

Yes. Actually we need to enable two hypercalls here,

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

#1. XENMEM_set_memory_map --> Set
#2. XENMEM_memory_map --> Get

You say "yes" without saying why, and ...

Instead of constructing e820 in the case of hvmloader, now we'd like to set up a basic e820 while building hvm, so we need to enable XENMEM_set_memory_map/XENMEM_memory_map to own this approach in hvm case.

You continue to ignore ...

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

on itself? If not, it should probably replace XENMEM_memory_map here.

... the "on itself" here. Of course the tool stack needs to be able to invoke this. But does the guest itself need to?

Jan

#86 lore raw

Re: [RFC][PATCH 09/13] xen: enable XENMEM_set_memory_map in hvm

Quoted 29 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:24, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:33, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4729,7 +4729,6 @@ static long hvm_memory_op(int cmd,

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 8 lines

xen/arch/x86/hvm/hvm.c static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4732

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0 -1
        switch ( cmd & MEMOP_CMD_MASK )
        {
-    case XENMEM_memory_map:

Title and description talk about XENMEM_set_memory_map only. As I think the implementation is right, the former will need updating. Do you actually need a HVM domain to be able to XENMEM_set_memory_map

Yes. Actually we need to enable two hypercalls here,

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

#1. XENMEM_set_memory_map --> Set
#2. XENMEM_memory_map --> Get

You say "yes" without saying why, and ...

Instead of constructing e820 in the case of hvmloader, now we'd like to set up a basic e820 while building hvm, so we need to enable XENMEM_set_memory_map/XENMEM_memory_map to own this approach in hvm case.

Quoted 6 lines

You continue to ignore ...

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

on itself? If not, it should probably replace XENMEM_memory_map here.

... the "on itself" here. Of course the tool stack needs to be able to invoke this. But does the guest itself need to?

No, so you're advising I just update the patch head,

xen: enable XENMEM_memory_map in hvm

This patch enables XENMEM_memory_map in hvm. So we can use it to setup the e820 mappings.

Thanks
Tiejun

#87 lore raw

RFC PATCH 10/13 tools: extend XENMEM_set_memory_map

In reply to Tiejun Chen

Here we'll construct a basic guest e820 table via XENMEM_set_memory_map. This table includes lowmem, highmem and RDMs if they exist. And hvmloader would need this info later.

---
 tools/libxl/libxl_dom.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
tools/libxl/libxl_dom.c +72 -0
index ee67282..82468be 100644
@@ -787,6 +787,70 @@ out:
     return rc;
 }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }
+
+    /* High memory */
+    if (highmem_size) {
+        e820[nr].addr = ((uint64_t)1 << 32);
+        e820[nr].size = highmem_size;
+        e820[nr].type = E820_RAM;
+    }
+
+    if (xc_domain_set_memory_map(ctx->xch, domid, e820, e820_entries) != 0) {
+        free(e820);
+        return -1;
+    }
+
+    free(e820);
+    return 0;
+}
+
 int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_config *d_config,
               libxl__domain_build_state *state)
@@ -836,6 +900,14 @@ int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
         goto out;
     }
 
+    if (libxl__domain_construct_memmap(ctx, d_config, domid,
+                                       &args,
+                                       d_config->num_pcidevs,
+                                       d_config->pcidevs)) {
+        LOG(ERROR, "setting domain rdm memory map failed");
+        goto out;
+    }
+
     if (libxl__domain_firmware(gc, info, &args)) {
         LOG(ERROR, "initializing domain firmware failed");
         goto out;
Signature · 1 line
1.9.1
#88 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 15 lines · Tiejun Chen

Here we'll construct a basic guest e820 table via XENMEM_set_memory_map. This table includes lowmem, highmem and RDMs if they exist. And hvmloader would need this info later.

---
 tools/libxl/libxl_dom.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
tools/libxl/libxl_dom.c +0 -0
index ee67282..82468be 100644

tools/libxl/libxl_dom.c out: · line 790

Quoted 4 lines
tools/libxl/libxl_dom.c +1 -0
@@ -787,6 +787,70 @@ out:
     return rc;
 }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,

Internal function should take libxl__gc *gc.

tools/libxl/libxl_dom.c out: · line 795

Quoted 5 lines
tools/libxl/libxl_dom.c +5 -0
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)

I think domid, num_pcidevs and pcidevs should be in d_config by the time you call this function? At least num_pcidevs and pcidevs should be available.

That said, I don't see pci stuff being used anywhere in the function, so please just delete them.

tools/libxl/libxl_dom.c out: · line 800

Quoted 5 lines
tools/libxl/libxl_dom.c +5 -0
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;

FWIW there are some new output fields called lowmem_end, highmem_end and mmio_start in xc_hvm_build_args. Those might be useful as well?

Note that they are only available *after* calling xc_hvm_build, which seems to *not* be the case for you.

So if you somehow find those fields useful you might want to consider moving the callsite a bit later.

tools/libxl/libxl_dom.c out: · line 813

Quoted 13 lines
tools/libxl/libxl_dom.c +13 -0
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);

You can use libxl__malloc(gc, ...).

tools/libxl/libxl_dom.c out: · line 816

Quoted 3 lines
tools/libxl/libxl_dom.c +3 -0
+    if (!e820) {
+        return -1;
+    }

No need to check if you use libxl__malloc.

tools/libxl/libxl_dom.c out: · line 819

Quoted 3 lines
tools/libxl/libxl_dom.c +3 -0
+
+    /* Low memory */
+    e820[nr].addr = 0x100000;

Hardcoded value?

Quoted 17 lines
tools/libxl/libxl_dom.c +17 -0
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

tools/libxl/libxl_dom.c out: · line 846

Quoted 10 lines
tools/libxl/libxl_dom.c +10 -0
+
+    /* High memory */
+    if (highmem_size) {
+        e820[nr].addr = ((uint64_t)1 << 32);
+        e820[nr].size = highmem_size;
+        e820[nr].type = E820_RAM;
+    }
+
+    if (xc_domain_set_memory_map(ctx->xch, domid, e820, e820_entries) != 0) {
+        free(e820);

No need to free if you use libxl__malloc(gc, ...).

tools/libxl/libxl_dom.c out: · line 846

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+        return -1;
+    }
+
+    free(e820);

Ditto.

Wei.

Quoted 23 lines
tools/libxl/libxl_dom.c +11 -0
+    return 0;
+}
+
 int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_config *d_config,
               libxl__domain_build_state *state)
@@ -836,6 +900,14 @@ int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
         goto out;
     }
 
+    if (libxl__domain_construct_memmap(ctx, d_config, domid,
+                                       &args,
+                                       d_config->num_pcidevs,
+                                       d_config->pcidevs)) {
+        LOG(ERROR, "setting domain rdm memory map failed");
+        goto out;
+    }
+
     if (libxl__domain_firmware(gc, info, &args)) {
         LOG(ERROR, "initializing domain firmware failed");
         goto out;
Signature · 1 line
1.9.1
#89 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Wei,

Thanks for your quick comment.

Quoted 4 lines
Quoted 2 lines

+static int libxl__domain_construct_memmap(libxl_ctx *ctx,

Internal function should take libxl__gc *gc.

Right.

Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 795

Quoted 6 lines
tools/libxl/libxl_dom.c +5 -0
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)

I think domid, num_pcidevs and pcidevs should be in d_config by the time you call this function? At least num_pcidevs and pcidevs should be

Yes, but looks 'domid' is still needed.

Quoted 4 lines

available.

That said, I don't see pci stuff being used anywhere in the function, so please just delete them.

Sorry I really should clean these stuffs.

Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 800

Quoted 6 lines
tools/libxl/libxl_dom.c +5 -0
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;

FWIW there are some new output fields called lowmem_end, highmem_end and mmio_start in xc_hvm_build_args. Those might be useful as well?

No. These e820 entries will be used in hvmloader.

Note these constructed e820 info are based on args->mem_size, args->lowmem_size and args->mmio_size. And looks these fields are never changed inside xc_hvm_build_args().

Quoted 6 lines

Note that they are only available *after* calling xc_hvm_build, which seems to *not* be the case for you.

So if you somehow find those fields useful you might want to consider moving the callsite a bit later.

But I think I'd better follow your logic here since we can't guarantee all related fields wouldn't be modified in the future.

So I will push libxl__domain_construct_memmap() after xc_hvm_build_args().

Quoted 16 lines
Quoted 4 lines
tools/libxl/libxl_dom.c +3 -0
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */

tools/libxl/libxl_dom.c out: · line 813

Quoted 10 lines
tools/libxl/libxl_dom.c +10 -0
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);

You can use libxl__malloc(gc, ...).

Good simplification.

Quoted 6 lines

tools/libxl/libxl_dom.c out: · line 816

Quoted 4 lines
tools/libxl/libxl_dom.c +3 -0
+    if (!e820) {
+        return -1;
+    }

No need to check if you use libxl__malloc.

Sure.

Quoted 6 lines

tools/libxl/libxl_dom.c out: · line 819

Quoted 4 lines
tools/libxl/libxl_dom.c +3 -0
+
+    /* Low memory */
+    e820[nr].addr = 0x100000;

Hardcoded value?

Yes. Actually, we intend to use this to present that lowmem entry,

tools/firmware/hvmloader/e820.c:

    /* Low RAM goes here. Reserve space for special pages. */
    ...
    e820[nr].addr = 0x100000;
Quoted 22 lines
Quoted 22 lines
tools/libxl/libxl_dom.c +21 -0
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }
+
+    /* High memory */
+    if (highmem_size) {
+        e820[nr].addr = ((uint64_t)1 << 32);
Quoted 8 lines

tools/libxl/libxl_dom.c out: · line 846

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+        e820[nr].size = highmem_size;
+        e820[nr].type = E820_RAM;
+    }
+
+    if (xc_domain_set_memory_map(ctx->xch, domid, e820, e820_entries) != 0) {
+        free(e820);

No need to free if you use libxl__malloc(gc, ...).

Sure.

Quoted 8 lines

tools/libxl/libxl_dom.c out: · line 846

Quoted 5 lines
tools/libxl/libxl_dom.c +4 -0
+        return -1;
+    }
+
+    free(e820);

Ditto.

Sure.

Thanks
Tiejun

#90 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

On Mon, Apr 13, 2015 at 10:09:51AM +0800, Chen, Tiejun wrote:
[...]

Quoted 10 lines
Quoted 1 line

Hardcoded value?

Yes. Actually, we intend to use this to present that lowmem entry,

tools/firmware/hvmloader/e820.c:

    /* Low RAM goes here. Reserve space for special pages. */
    ...
    e820[nr].addr = 0x100000;

I don't like the idea of having two hardcoded values in different locations. Please put this value into a header file and reference it here and in hvmloader.

Wei.

#91 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 6 lines · Wei Liu

On Mon, Apr 13, 2015 at 10:09:51AM +0800, Chen, Tiejun wrote:
[...]

Quoted 4 lines
Quoted 1 line

Hardcoded value?

Yes. Actually, we intend to use this to present that lowmem entry,

Quoted 8 lines
Quoted 6 lines

tools/firmware/hvmloader/e820.c:

     /* Low RAM goes here. Reserve space for special pages. */
     ...
     e820[nr].addr = 0x100000;

I don't like the idea of having two hardcoded values in different

Just one place since based on our logic, hvmloader doesn't have this setting anymore and actually it really grab that info from here. Please refer to patch #13.

Quoted 2 lines

locations. Please put this value into a header file and reference it here and in hvmloader.

Anyway, I'd like to define this here directly since no one consumes this again.

tools/libxl/libxl_dom.c +3 -2
index 5134b33..af747e6 100644
@@ -787,6 +787,7 @@ out:
     return rc;
 }
 
+#define GUEST_LOW_MEM_START_DEFAULT 0x100000
 static int libxl__domain_construct_memmap(libxl__gc *gc,
                                           libxl_domain_config *d_config,
                                           uint32_t domid,
@@ -812,8 +813,8 @@ static int libxl__domain_construct_memmap(libxl__gc *gc,
     e820 = libxl__malloc(gc, sizeof(struct e820entry) * e820_entries);
 
     /* Low memory */
-    e820[nr].addr = 0x100000;
-    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].addr = GUEST_LOW_MEM_START_DEFAULT;
+    e820[nr].size = args->lowmem_size - GUEST_LOW_MEM_START_DEFAULT;
     e820[nr].type = E820_RAM;
     nr++;
 

Is this fine to you?

Thanks
Tiejun

#92 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 49 lines · Tiejun
Quoted 6 lines · Wei Liu

On Mon, Apr 13, 2015 at 10:09:51AM +0800, Chen, Tiejun wrote:
[...]

Quoted 4 lines
Quoted 1 line

Hardcoded value?

Yes. Actually, we intend to use this to present that lowmem entry,

Quoted 8 lines
Quoted 6 lines

tools/firmware/hvmloader/e820.c:

    /* Low RAM goes here. Reserve space for special pages. */
    ...
    e820[nr].addr = 0x100000;

I don't like the idea of having two hardcoded values in different

Just one place since based on our logic, hvmloader doesn't have this setting anymore and actually it really grab that info from here. Please refer to patch #13.

Quoted 2 lines

locations. Please put this value into a header file and reference it here and in hvmloader.

Anyway, I'd like to define this here directly since no one consumes this again.

tools/libxl/libxl_dom.c +3 -2
index 5134b33..af747e6 100644
@@ -787,6 +787,7 @@ out:
     return rc;
 }
 
+#define GUEST_LOW_MEM_START_DEFAULT 0x100000
 static int libxl__domain_construct_memmap(libxl__gc *gc,
                                           libxl_domain_config *d_config,
                                           uint32_t domid,
@@ -812,8 +813,8 @@ static int libxl__domain_construct_memmap(libxl__gc *gc,
     e820 = libxl__malloc(gc, sizeof(struct e820entry) * e820_entries);
 
     /* Low memory */
-    e820[nr].addr = 0x100000;
-    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].addr = GUEST_LOW_MEM_START_DEFAULT;
+    e820[nr].size = args->lowmem_size - GUEST_LOW_MEM_START_DEFAULT;
     e820[nr].type = E820_RAM;
     nr++;
 
Quoted 2 lines

Is this fine to you?

Having a #define without explaining why it is chosen is still fragile. As Jan pointed out this needs more explanation.

Wei.

Quoted 2 lines

Thanks
Tiejun

#93 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

In reply to Tiejun Chen
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +22 -0
@@ -787,6 +787,70 @@ out:
     return rc;
 }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }

tools/libxl/libxl_dom.c out: · line 821

Quoted 10 lines
tools/libxl/libxl_dom.c +10 -0
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Quoted 5 lines
tools/libxl/libxl_dom.c +5 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*

tools/libxl/libxl_dom.c out: · line 836

Quoted 10 lines
tools/libxl/libxl_dom.c +10 -0
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Jan

#94 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +28 -0
@@ -787,6 +787,70 @@ out:
      return rc;
  }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 821

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Okay, here may need this,

/*

* Low RAM starts at least from 1M to make sure all standard regions

* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,

* have enough space.
*/
#define GUEST_LOW_MEM_START_DEFAULT 0x100000

On the consuming side, I should clarify that we always preserve 1M.

Quoted 9 lines
Quoted 9 lines
tools/libxl/libxl_dom.c +9 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 836

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Right, I would add this at the beginning,

    if (e820_entries >= E820MAX) {
        LOG(ERROR, "Ooops! Too many entries in the memory map!\n");
        return -1;
    }

Thanks
Tiejun

#95 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 51 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:57, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +28 -0
@@ -787,6 +787,70 @@ out:
      return rc;
  }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 821

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Okay, here may need this,

/*

Quoted 7 lines

* Low RAM starts at least from 1M to make sure all standard regions

* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,

* have enough space.
*/
#define GUEST_LOW_MEM_START_DEFAULT 0x100000

But this only states a generic fact, but doesn't explain why you can lump together all the different things below 1Mb into a single E820 entry.

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
tools/libxl/libxl_dom.c +9 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 836

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Quoted 6 lines

Right, I would add this at the beginning,

     if (e820_entries >= E820MAX) {
         LOG(ERROR, "Ooops! Too many entries in the memory map!\n");
         return -1;
     }

That would be a protection against too many entries, but not against overlapping ones.

Jan

#96 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 55 lines · Jan Beulich
Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:57, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +28 -0
@@ -787,6 +787,70 @@ out:
       return rc;
   }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 821

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Okay, here may need this,

/*

* Low RAM starts at least from 1M to make sure all standard regions

* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,

Quoted 7 lines
Quoted 3 lines

* have enough space.
*/
#define GUEST_LOW_MEM_START_DEFAULT 0x100000

But this only states a generic fact, but doesn't explain why you can lump together all the different things below 1Mb into a single E820 entry.

I'm not sure if I'm misleading you. All different things below 1M is not in a single entry. Here we just construct these mappings:

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Those stuffs below 1M are still constructed with multiple e820 entries by hvmloader. At this point I don't change anything.

Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_dom.c +9 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 836

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Right, I would add this at the beginning,

Quoted 8 lines
Quoted 4 lines
      if (e820_entries >= E820MAX) {
          LOG(ERROR, "Ooops! Too many entries in the memory map!\n");
          return -1;
      }

That would be a protection against too many entries, but not against overlapping ones.

Are you saying these kinds of mapping?

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Before we call this function we already finish handling RDM with our policy. This means there's no any overlapping here.

Thanks
TIejun

#97 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 68 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:09, <[email protected]> wrote:
Quoted 55 lines · Jan Beulich
Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:57, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +28 -0
@@ -787,6 +787,70 @@ out:
       return rc;
   }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 821

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Okay, here may need this,

/*

* Low RAM starts at least from 1M to make sure all standard regions

* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,

Quoted 7 lines
Quoted 3 lines

* have enough space.
*/
#define GUEST_LOW_MEM_START_DEFAULT 0x100000

But this only states a generic fact, but doesn't explain why you can lump together all the different things below 1Mb into a single E820 entry.

I'm not sure if I'm misleading you. All different things below 1M is not in a single entry. Here we just construct these mappings:

Quoted 6 lines

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Those stuffs below 1M are still constructed with multiple e820 entries by hvmloader. At this point I don't change anything.

Then _that_ is what the comment needs to say.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 9 lines
tools/libxl/libxl_dom.c +9 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 836

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Right, I would add this at the beginning,

Quoted 8 lines
Quoted 4 lines
      if (e820_entries >= E820MAX) {
          LOG(ERROR, "Ooops! Too many entries in the memory map!\n");
          return -1;
      }

That would be a protection against too many entries, but not against overlapping ones.

Quoted 8 lines

Are you saying these kinds of mapping?

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Before we call this function we already finish handling RDM with our policy. This means there's no any overlapping here.

That would be fine then. Note what I had asked: "Is this guaranteed not to produce overlapping entries?" I.e. if it is guaranteed (which afaict isn't obvious from the code itself), then please again say why in a brief comment.

Jan

#98 lore raw

Re: [RFC][PATCH 10/13] tools: extend XENMEM_set_memory_map

Quoted 113 lines · Jan Beulich
Quoted 68 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:09, <[email protected]> wrote:
Quoted 55 lines · Jan Beulich
Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 04:57, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/libxl/libxl_dom.c +28 -0
@@ -787,6 +787,70 @@ out:
        return rc;
    }
 
+static int libxl__domain_construct_memmap(libxl_ctx *ctx,
+                                          libxl_domain_config *d_config,
+                                          uint32_t domid,
+                                          struct xc_hvm_build_args *args,
+                                          int num_pcidevs,
+                                          libxl_device_pci *pcidevs)
+{
+    unsigned int nr = 0, i;
+    /* We always own at least one lowmem entry. */
+    unsigned int e820_entries = 1;
+    uint64_t highmem_end = 0, highmem_size = args->mem_size - args->lowmem_size;
+    struct e820entry *e820 = NULL;
+
+    /* Add all rdm entries. */
+    e820_entries += d_config->num_rdms;
+
+    /* If we should have a highmem range. */
+    if (highmem_size)
+    {
+        highmem_end = (1ull<<32) + highmem_size;
+        e820_entries++;
+    }
+
+    e820 = malloc(sizeof(struct e820entry) * e820_entries);
+    if (!e820) {
+        return -1;
+    }
+
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 821

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    /* Low memory */
+    e820[nr].addr = 0x100000;
+    e820[nr].size = args->lowmem_size - 0x100000;
+    e820[nr].type = E820_RAM;

If you really mean it to be this lax (not covering the low 1Mb), then you need to explain why in a comment (and the consuming side should also have a similar explanation then).

Okay, here may need this,

/*

* Low RAM starts at least from 1M to make sure all standard regions

* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,

Quoted 7 lines
Quoted 3 lines

* have enough space.
*/
#define GUEST_LOW_MEM_START_DEFAULT 0x100000

But this only states a generic fact, but doesn't explain why you can lump together all the different things below 1Mb into a single E820 entry.

I'm not sure if I'm misleading you. All different things below 1M is not in a single entry. Here we just construct these mappings:

Quoted 6 lines

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Those stuffs below 1M are still constructed with multiple e820 entries by hvmloader. At this point I don't change anything.

Then _that_ is what the comment needs to say.

Quoted 36 lines
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 9 lines
tools/libxl/libxl_dom.c +9 -0
+    nr++;
+
+    /* RDM mapping */
+    for (i = 0; i < d_config->num_rdms; i++) {
+        /*
+         * We should drop this kind of rdm entry.
+         */
+        if (d_config->rdms[i].flag == LIBXL_RDM_RESERVE_FLAG_INVALID)
+            continue;
Quoted 9 lines

tools/libxl/libxl_dom.c out: · line 836

Quoted 6 lines
tools/libxl/libxl_dom.c +6 -0
+
+        e820[nr].addr = d_config->rdms[i].start;
+        e820[nr].size = d_config->rdms[i].size;
+        e820[nr].type = E820_RESERVED;
+        nr++;
+    }

Is this guaranteed not to produce overlapping entries?

Right, I would add this at the beginning,

Quoted 8 lines
Quoted 4 lines
       if (e820_entries >= E820MAX) {
           LOG(ERROR, "Ooops! Too many entries in the memory map!\n");
           return -1;
       }

That would be a protection against too many entries, but not against overlapping ones.

Are you saying these kinds of mapping?

#1. [1M, lowmem_end]
#2. [RDM]
#3. [4G, highmem_end]

Quoted 8 lines
Quoted 2 lines

Before we call this function we already finish handling RDM with our policy. This means there's no any overlapping here.

That would be fine then. Note what I had asked: "Is this guaranteed not to produce overlapping entries?" I.e. if it is guaranteed (which afaict isn't obvious from the code itself), then please again say why in a brief comment.

Sorry for this unclear reply previously.

I would summary current reply as a comment.

Thanks
Tiejun

#99 lore raw

RFC PATCH 11/13 hvmloader: get guest memory map into memory_map[]

In reply to Tiejun Chen

Now we get this map layout by call XENMEM_memory_map then save them into one global variable memory_map[]. It should include lowmem range, rdm range and highmem range. Note rdm range and highmem range may not exist in some cases.

And here we need to check if any reserved memory conflicts with [RESERVED_MEMORY_DYNAMIC_START - 1, RESERVED_MEMORY_DYNAMIC_END]. This range is used to allocate memory in hvmloder level, and we would lead hvmloader failed in case of conflict since its another rare possibility in real world.

---
 tools/firmware/hvmloader/e820.h      |  7 +++++++
 tools/firmware/hvmloader/hvmloader.c | 36 ++++++++++++++++++++++++++++++++++++
 tools/firmware/hvmloader/util.c      | 25 +++++++++++++++++++++++++
 tools/firmware/hvmloader/util.h      | 10 ++++++++++
 4 files changed, 78 insertions(+)
tools/firmware/hvmloader/e820.h +7 -0
index b2ead7f..8b5a9e0 100644
@@ -15,6 +15,13 @@ struct e820entry {
     uint32_t type;
 } __attribute__((packed));
 
+#define E820MAX	128
+
+struct e820map {
+    unsigned int nr_map;
+    struct e820entry map[E820MAX];
+};
+
 #endif /* __HVMLOADER_E820_H__ */
 
 /*
tools/firmware/hvmloader/hvmloader.c +36 -0
index 25b7f08..5595700 100644
@@ -107,6 +107,8 @@ asm (
     "    .text                       \n"
     );
 
+struct e820map memory_map;
+
 unsigned long scratch_start = SCRATCH_PHYSICAL_ADDRESS;
 
 static void init_hypercalls(void)
@@ -199,6 +201,38 @@ static void apic_setup(void)
     ioapic_write(0x11, SET_APIC_ID(LAPIC_ID(0)));
 }
 
+void memory_map_setup(void)
+{
+    unsigned int nr_entries = E820MAX, i;
+    int rc;
+    uint64_t alloc_addr = RESERVED_MEMORY_DYNAMIC_START - 1;
+    uint64_t alloc_size = RESERVED_MEMORY_DYNAMIC_END - alloc_addr;
+
+    rc = get_mem_mapping_layout(memory_map.map, &nr_entries);
+
+    if ( rc )
+    {
+        printf("Failed to get guest memory map.\n");
+        BUG();
+    }
+
+    memory_map.nr_map = nr_entries;
+
+    for ( i = 0; i < nr_entries; i++ )
+    {
+        if ( memory_map.map[i].type == E820_RESERVED )
+        {
+            if ( check_hole_conflict(alloc_addr, alloc_size,
+                                     memory_map.map[i].addr,
+                                     memory_map.map[i].size) )
+            {
+                printf("RDM conflicts Memory allocation.\n");
+                BUG();
+            }
+        }
+    }
+}
+
 struct bios_info {
     const char *key;
     const struct bios_config *bios;
@@ -262,6 +296,8 @@ int main(void)
 
     init_hypercalls();
 
+    memory_map_setup();
+
     xenbus_setup();
 
     bios = detect_bios();
tools/firmware/hvmloader/util.c +25 -0
index 80d822f..0e08cb8 100644
@@ -27,6 +27,16 @@
 #include <xen/memory.h>
 #include <xen/sched.h>
 
+int check_hole_conflict(uint64_t start, uint64_t size,
+                        uint64_t reserved_start, uint64_t reserved_size)
+{
+    if ( start + size <= reserved_start ||
+            start >= reserved_start + reserved_size )
+        return 0;
+    else
+        return 1;
+}
+
 void wrmsr(uint32_t idx, uint64_t v)
 {
     asm volatile (
@@ -368,6 +378,21 @@ uuid_to_string(char *dest, uint8_t *uuid)
     *p = '\0';
 }
 
+int get_mem_mapping_layout(struct e820entry entries[], uint32_t *max_entries)
+{
+    int rc;
+    struct xen_memory_map memmap = {
+        .nr_entries = *max_entries
+    };
+
+    set_xen_guest_handle(memmap.buffer, entries);
+
+    rc = hypercall_memory_op(XENMEM_memory_map, &memmap);
+    *max_entries = memmap.nr_entries;
+
+    return rc;
+}
+
 void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns)
 {
     static int over_allocated;
tools/firmware/hvmloader/util.h +10 -0
index a70e4aa..1121a65 100644
@@ -6,6 +6,7 @@
 #include <stddef.h>
 #include <xen/xen.h>
 #include <xen/hvm/hvm_info_table.h>
+#include "e820.h"
 
 #define __STR(...) #__VA_ARGS__
 #define STR(...) __STR(__VA_ARGS__)
@@ -222,6 +223,9 @@ int hvm_param_set(uint32_t index, uint64_t value);
 /* Setup PCI bus */
 void pci_setup(void);
 
+/* Setup memory map  */
+void memory_map_setup(void);
+
 /* Prepare the 32bit BIOS */
 uint32_t rombios_highbios_setup(void);
 
@@ -249,6 +253,12 @@ void perform_tests(void);
 
 extern char _start[], _end[];
 
+int get_mem_mapping_layout(struct e820entry entries[], uint32_t *max_entries);
+
+extern struct e820map memory_map;
+int check_hole_conflict(uint64_t start, uint64_t size,
+                        uint64_t reserved_start, uint64_t reserved_size);
+
 #endif /* __HVMLOADER_UTIL_H__ */
 
 /*
Signature · 1 line
1.9.1
#100 lore raw

Re: [RFC][PATCH 11/13] hvmloader: get guest memory map into memory_map[]

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/util.c +0 -0
@@ -27,6 +27,16 @@
 #include <xen/memory.h>
 #include <xen/sched.h>

tools/firmware/hvmloader/util.c line 38

Quoted 10 lines
tools/firmware/hvmloader/util.c +9 -0
 
+int check_hole_conflict(uint64_t start, uint64_t size,
+                        uint64_t reserved_start, uint64_t reserved_size)
+{
+    if ( start + size <= reserved_start ||
+            start >= reserved_start + reserved_size )
+        return 0;
+    else
+        return 1;
+}

See the comments on the similar tool stack function. Also please get indentation right.

Jan

#101 lore raw

Re: [RFC][PATCH 11/13] hvmloader: get guest memory map into memory_map[]

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/util.c +0 -0
@@ -27,6 +27,16 @@
  #include <xen/memory.h>
  #include <xen/sched.h>
 
Quoted 13 lines

tools/firmware/hvmloader/util.c line 38

Quoted 9 lines
tools/firmware/hvmloader/util.c +9 -0
+int check_hole_conflict(uint64_t start, uint64_t size,
+                        uint64_t reserved_start, uint64_t reserved_size)
+{
+    if ( start + size <= reserved_start ||
+            start >= reserved_start + reserved_size )
+        return 0;
+    else
+        return 1;
+}

See the comments on the similar tool stack function. Also please get indentation right.

Okay.

Thanks
Tiejun

#102 lore raw

RFC PATCH 12/13 hvmloader/pci: skip reserved ranges

In reply to Tiejun Chen

When allocating mmio address for PCI bars, we need to make sure they don't overlap with reserved regions.

---
 tools/firmware/hvmloader/pci.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
tools/firmware/hvmloader/pci.c +17 -2
index 4e8d803..51aa65d 100644
@@ -59,8 +59,8 @@ void pci_setup(void)
         uint32_t bar_reg;
         uint64_t bar_sz;
     } *bars = (struct bars *)scratch_start;
-    unsigned int i, nr_bars = 0;
-    uint64_t mmio_hole_size = 0;
+    unsigned int i, j, nr_bars = 0;
+    uint64_t mmio_hole_size = 0, reserved_end;
 
     const char *s;
     /*
@@ -393,8 +393,23 @@ void pci_setup(void)
         }
 
         base = (resource->base  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+ reallocate_mmio:
         bar_data |= (uint32_t)base;
         bar_data_upper = (uint32_t)(base >> 32);
+        for ( j = 0; j < memory_map.nr_map ; j++ )
+        {
+            if ( memory_map.map[j].type != E820_RAM )
+            {
+                reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+                if ( check_hole_conflict(base, bar_sz,
+                                         memory_map.map[j].addr,
+                                         memory_map.map[j].size) )
+                {
+                    base = (reserved_end  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+                    goto reallocate_mmio;
+                }
+            }
+        }
         base += bar_sz;
 
         if ( (base < resource->base) || (base > resource->max) )
Signature · 1 line
1.9.1
#103 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/pci.c +10 -2
@@ -59,8 +59,8 @@ void pci_setup(void)
         uint32_t bar_reg;
         uint64_t bar_sz;
     } *bars = (struct bars *)scratch_start;
-    unsigned int i, nr_bars = 0;
-    uint64_t mmio_hole_size = 0;
+    unsigned int i, j, nr_bars = 0;
+    uint64_t mmio_hole_size = 0, reserved_end;
 
     const char *s;
     /*
@@ -393,8 +393,23 @@ void pci_setup(void)
         }
 
         base = (resource->base  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+ reallocate_mmio:
         bar_data |= (uint32_t)base;
         bar_data_upper = (uint32_t)(base >> 32);
+        for ( j = 0; j < memory_map.nr_map ; j++ )
+        {
+            if ( memory_map.map[j].type != E820_RAM )
+            {
+                reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+                if ( check_hole_conflict(base, bar_sz,
+                                         memory_map.map[j].addr,

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 415

Quoted 10 lines
tools/firmware/hvmloader/pci.c +7 -0
+                                         memory_map.map[j].size) )
+                {
+                    base = (reserved_end  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+                    goto reallocate_mmio;
+                }
+            }
+        }
         base += bar_sz;
 
         if ( (base < resource->base) || (base > resource->max) )

But you do nothing to make sure the MMIO regions all fit in the available window (see the code ahead of this relocating RAM if necessary).

Jan

#104 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 37 lines · Jan Beulich
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/pci.c +17 -2
@@ -59,8 +59,8 @@ void pci_setup(void)
          uint32_t bar_reg;
          uint64_t bar_sz;
      } *bars = (struct bars *)scratch_start;
-    unsigned int i, nr_bars = 0;
-    uint64_t mmio_hole_size = 0;
+    unsigned int i, j, nr_bars = 0;
+    uint64_t mmio_hole_size = 0, reserved_end;
 
      const char *s;
      /*
@@ -393,8 +393,23 @@ void pci_setup(void)
          }
 
          base = (resource->base  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+ reallocate_mmio:
          bar_data |= (uint32_t)base;
          bar_data_upper = (uint32_t)(base >> 32);
+        for ( j = 0; j < memory_map.nr_map ; j++ )
+        {
+            if ( memory_map.map[j].type != E820_RAM )
+            {
+                reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+                if ( check_hole_conflict(base, bar_sz,
+                                         memory_map.map[j].addr,
+                                         memory_map.map[j].size) )
+                {
+                    base = (reserved_end  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+                    goto reallocate_mmio;
+                }
+            }
+        }
          base += bar_sz;
 
Quoted 2 lines

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 415

Quoted 1 line
tools/firmware/hvmloader/pci.c +0 -0
          if ( (base < resource->base) || (base > resource->max) )

Actually some original codes are missing here,

        if ( (base < resource->base) || (base > resource->max) )
        {
            printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                   "resource!\n", devfn>>3, devfn&7, bar_reg,
                   PRIllx_arg(bar_sz));
            continue;
        }

I think this can guarantee the MMIO regions just fit in the available RAM.

Or am I wrong?

Thanks
Tiejun

Quoted 7 lines

But you do nothing to make sure the MMIO regions all fit in the available window (see the code ahead of this relocating RAM if necessary).

Jan

#105 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 54 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 05:18, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/pci.c +2 -2
@@ -59,8 +59,8 @@ void pci_setup(void)
          uint32_t bar_reg;
          uint64_t bar_sz;
      } *bars = (struct bars *)scratch_start;
-    unsigned int i, nr_bars = 0;
-    uint64_t mmio_hole_size = 0;
+    unsigned int i, j, nr_bars = 0;
+    uint64_t mmio_hole_size = 0, reserved_end;
 
      const char *s;
      /*
@@ -393,8 +393,23 @@ void pci_setup(void)
          }
 
Quoted 9 lines

tools/firmware/hvmloader/pci.c void pci_setup(void)

Quoted 9 lines
tools/firmware/hvmloader/pci.c +6 -0
          base = (resource->base  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+ reallocate_mmio:
          bar_data |= (uint32_t)base;
          bar_data_upper = (uint32_t)(base >> 32);
+        for ( j = 0; j < memory_map.nr_map ; j++ )
+        {
+            if ( memory_map.map[j].type != E820_RAM )
+            {
+                reserved_end = memory_map.map[j].addr + 

memory_map.map[j].size;

Quoted 5 lines

tools/firmware/hvmloader/pci.c void pci_setup(void)

Quoted 5 lines
tools/firmware/hvmloader/pci.c +5 -0
+                if ( check_hole_conflict(base, bar_sz,
+                                         memory_map.map[j].addr,
+                                         memory_map.map[j].size) )
+                {
+                    base = (reserved_end  + bar_sz - 1) & ~(uint64_t)(bar_sz - 

1);

Quoted 8 lines

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 415

Quoted 7 lines
tools/firmware/hvmloader/pci.c +4 -0
+                    goto reallocate_mmio;
+                }
+            }
+        }
          base += bar_sz;
 
          if ( (base < resource->base) || (base > resource->max) )

Actually some original codes are missing here,

         if ( (base < resource->base) || (base > resource->max) )
         {
             printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                    "resource!\n", devfn>>3, devfn&7, bar_reg,
                    PRIllx_arg(bar_sz));
             continue;
         }
Quoted 3 lines

I think this can guarantee the MMIO regions just fit in the available RAM.

Or am I wrong?

The code you cite guarantees almost nothing, it simply skips assigning resources. Your changes potentially growing the space needed to fit all MMIO BARs therefore also needs to adjust the up front calculation, such that if necessary more RAM can be relocated to make the hole large enough.

Jan

#106 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 56 lines · Jan Beulich
Quoted 56 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 05:18, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/pci.c +2 -2
@@ -59,8 +59,8 @@ void pci_setup(void)
           uint32_t bar_reg;
           uint64_t bar_sz;
       } *bars = (struct bars *)scratch_start;
-    unsigned int i, nr_bars = 0;
-    uint64_t mmio_hole_size = 0;
+    unsigned int i, j, nr_bars = 0;
+    uint64_t mmio_hole_size = 0, reserved_end;
 
       const char *s;
       /*
@@ -393,8 +393,23 @@ void pci_setup(void)
           }
 
Quoted 9 lines

tools/firmware/hvmloader/pci.c void pci_setup(void)

Quoted 9 lines
tools/firmware/hvmloader/pci.c +6 -0
           base = (resource->base  + bar_sz - 1) & ~(uint64_t)(bar_sz - 1);
+ reallocate_mmio:
           bar_data |= (uint32_t)base;
           bar_data_upper = (uint32_t)(base >> 32);
+        for ( j = 0; j < memory_map.nr_map ; j++ )
+        {
+            if ( memory_map.map[j].type != E820_RAM )
+            {
+                reserved_end = memory_map.map[j].addr +

memory_map.map[j].size;

Quoted 5 lines

tools/firmware/hvmloader/pci.c void pci_setup(void)

Quoted 5 lines
tools/firmware/hvmloader/pci.c +5 -0
+                if ( check_hole_conflict(base, bar_sz,
+                                         memory_map.map[j].addr,
+                                         memory_map.map[j].size) )
+                {
+                    base = (reserved_end  + bar_sz - 1) & ~(uint64_t)(bar_sz -

1);

Quoted 8 lines

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 415

Quoted 7 lines
tools/firmware/hvmloader/pci.c +4 -0
+                    goto reallocate_mmio;
+                }
+            }
+        }
           base += bar_sz;
 
           if ( (base < resource->base) || (base > resource->max) )

Actually some original codes are missing here,

          if ( (base < resource->base) || (base > resource->max) )
          {
              printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                     "resource!\n", devfn>>3, devfn&7, bar_reg,
                     PRIllx_arg(bar_sz));
              continue;
          }

I think this can guarantee the MMIO regions just fit in the available RAM.

Quoted 8 lines
Quoted 1 line

Or am I wrong?

The code you cite guarantees almost nothing, it simply skips assigning resources. Your changes potentially growing the space needed to fit all MMIO BARs therefore also needs to adjust the up front calculation, such that if necessary more RAM can be relocated to make the hole large enough.

You're right.

Just think about we're always trying to check pci_mem_start to populate more RAM to obtain enough PCI mempry,

    /* Relocate RAM that overlaps PCI space (in 64k-page chunks). */
    while ( (pci_mem_start >> PAGE_SHIFT) < hvm_info->low_mem_pgend )
    {
        struct xen_add_to_physmap xatp;
        unsigned int nr_pages = min_t(
            unsigned int,
            hvm_info->low_mem_pgend - (pci_mem_start >> PAGE_SHIFT),
            (1u << 16) - 1);
        if ( hvm_info->high_mem_pgend == 0 )
            hvm_info->high_mem_pgend = 1ull << (32 - PAGE_SHIFT);
        hvm_info->low_mem_pgend -= nr_pages;
        printf("Relocating 0x%x pages from "PRIllx" to "PRIllx\
               " for lowmem MMIO hole\n",
               nr_pages,
               PRIllx_arg(((uint64_t)hvm_info->low_mem_pgend)<<PAGE_SHIFT),
PRIllx_arg(((uint64_t)hvm_info->high_mem_pgend)<<PAGE_SHIFT));
        xatp.domid = DOMID_SELF;
        xatp.space = XENMAPSPACE_gmfn_range;
        xatp.idx   = hvm_info->low_mem_pgend;
        xatp.gpfn  = hvm_info->high_mem_pgend;
        xatp.size  = nr_pages;
        if ( hypercall_memory_op(XENMEM_add_to_physmap, &xatp) != 0 )
            BUG();
        hvm_info->high_mem_pgend += nr_pages;
    }

So I think we may need to adjust pci_mem_start like this,

@@ -301,6 +301,19 @@ void pci_setup(void)

             pci_mem_start <<= 1;
     }
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {

+ if ( memory_map.map[j].type != E820_RAM )
+ {
+ reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+ if ( check_overlap(pci_mem_start, pci_mem_end,
+ memory_map.map[j].addr,
+ memory_map.map[j].size) )
+ pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+ }
+ }
+
if ( mmio_total > (pci_mem_end - pci_mem_start) ) {
printf("Low MMIO hole not large enough for all devices,"

Right?

Thanks
Tiejun

#107 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:34, <[email protected]> wrote:

So I think we may need to adjust pci_mem_start like this,

tools/firmware/hvmloader/pci.c +13 -0
@@ -301,6 +301,19 @@ void pci_setup(void)
              pci_mem_start <<= 1;
      }
 
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {
+        if ( memory_map.map[j].type != E820_RAM )
+        {
+            reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+            if ( check_overlap(pci_mem_start, pci_mem_end,
+                               memory_map.map[j].addr,
+                               memory_map.map[j].size) )
+                pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+        }
+    }
+
      if ( mmio_total > (pci_mem_end - pci_mem_start) )
      {
          printf("Low MMIO hole not large enough for all devices,"
Quoted 1 line

Right?

I think that gets you in the right direction, but isn't enough, as it doesn't account for (unavoidable) gaps (BARs are always a power of 2 in size and accordingly aligned).

Jan

#108 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:34, <[email protected]> wrote:

So I think we may need to adjust pci_mem_start like this,

tools/firmware/hvmloader/pci.c +13 -0
@@ -301,6 +301,19 @@ void pci_setup(void)
               pci_mem_start <<= 1;
       }
 
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {
+        if ( memory_map.map[j].type != E820_RAM )
+        {
+            reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+            if ( check_overlap(pci_mem_start, pci_mem_end,
+                               memory_map.map[j].addr,
+                               memory_map.map[j].size) )
+                pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+        }
+    }
+
       if ( mmio_total > (pci_mem_end - pci_mem_start) )
       {
           printf("Low MMIO hole not large enough for all devices,"
Quoted 5 lines
Quoted 1 line

Right?

I think that gets you in the right direction, but isn't enough, as it doesn't account for (unavoidable) gaps (BARs are always a power of 2 in size and accordingly aligned).

Right.

But as you see, we always take this action, >> PAGE_SHIFT, so this means its always a sort of power of 2.

Additionally, lets go back here,

        if ( (base < resource->base) || (base > resource->max) )
        {
            printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                   "resource!\n", devfn>>3, devfn&7, bar_reg,
                   PRIllx_arg(bar_sz));
            continue;
        }

I mean even without rdm, the original codes don't consider handling this lack of space from a alignment in advance, right?

Thanks
Tiejun

#109 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:16, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:34, <[email protected]> wrote:

So I think we may need to adjust pci_mem_start like this,

(quoted diff) +13 -0
@@ -301,6 +301,19 @@ void pci_setup(void)
               pci_mem_start <<= 1;
       }
 
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {
+        if ( memory_map.map[j].type != E820_RAM )
+        {
+            reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+            if ( check_overlap(pci_mem_start, pci_mem_end,
+                               memory_map.map[j].addr,
+                               memory_map.map[j].size) )
+                pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+        }
+    }
+
       if ( mmio_total > (pci_mem_end - pci_mem_start) )
       {
           printf("Low MMIO hole not large enough for all devices,"
Quoted 5 lines
Quoted 1 line

Right?

I think that gets you in the right direction, but isn't enough, as it doesn't account for (unavoidable) gaps (BARs are always a power of 2 in size and accordingly aligned).

Quoted 4 lines

Right.

But as you see, we always take this action, >> PAGE_SHIFT, so this means its always a sort of power of 2.

No, certainly not.

Quoted 10 lines

Additionally, lets go back here,

         if ( (base < resource->base) || (base > resource->max) )
         {
             printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                    "resource!\n", devfn>>3, devfn&7, bar_reg,
                    PRIllx_arg(bar_sz));
             continue;
         }
Quoted 2 lines

I mean even without rdm, the original codes don't consider handling this lack of space from a alignment in advance, right?

Correct. But your change increases the chances of this getting used. Also I think you may want to carefully look at under what conditions this path gets taken without and with your patches.

Jan

#110 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 32 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:16, <[email protected]> wrote:
Quoted 29 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:34, <[email protected]> wrote:

So I think we may need to adjust pci_mem_start like this,

(quoted diff) +13 -0
@@ -301,6 +301,19 @@ void pci_setup(void)
                pci_mem_start <<= 1;
        }
 
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {
+        if ( memory_map.map[j].type != E820_RAM )
+        {
+            reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+            if ( check_overlap(pci_mem_start, pci_mem_end,
+                               memory_map.map[j].addr,
+                               memory_map.map[j].size) )
+                pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+        }
+    }
+
        if ( mmio_total > (pci_mem_end - pci_mem_start) )
        {
            printf("Low MMIO hole not large enough for all devices,"
Quoted 1 line

Right?

I think that gets you in the right direction, but isn't enough, as it doesn't account for (unavoidable) gaps (BARs are always a power of 2 in size and accordingly aligned).

Quoted 6 lines
Quoted 4 lines

Right.

But as you see, we always take this action, >> PAGE_SHIFT, so this means its always a sort of power of 2.

No, certainly not.

rdm start or size? Or anything else I'm missing?

Quoted 11 lines
Quoted 11 lines

Additionally, lets go back here,

          if ( (base < resource->base) || (base > resource->max) )
          {
              printf("pci dev %02x:%x bar %02x size "PRIllx": no space for "
                     "resource!\n", devfn>>3, devfn&7, bar_reg,
                     PRIllx_arg(bar_sz));
              continue;
          }
Quoted 7 lines
Quoted 2 lines

I mean even without rdm, the original codes don't consider handling this lack of space from a alignment in advance, right?

Correct. But your change increases the chances of this getting used. Also I think you may want to carefully look at under what conditions this path gets taken without and with your patches.

Sure.

I think I can record that max bar_sz to improve this like,

@@ -60,7 +60,7 @@ void pci_setup(void)

         uint64_t bar_sz;
     } *bars = (struct bars *)scratch_start;
     unsigned int i, j, nr_bars = 0;

- uint64_t mmio_hole_size = 0, reserved_end;
+ uint64_t mmio_hole_size = 0, reserved_end, max_bar_sz = 0;

     const char *s;
     /*
@@ -226,6 +226,8 @@ void pci_setup(void)
             bars[i].devfn   = devfn;
             bars[i].bar_reg = bar_reg;
             bars[i].bar_sz  = bar_sz;

+ if ( bar_sz > max_bar_sz )
+ max_bar_sz = bar_sz;

             if ( ((bar_data & PCI_BASE_ADDRESS_SPACE) ==
                   PCI_BASE_ADDRESS_SPACE_MEMORY) ||
@@ -311,6 +313,8 @@ void pci_setup(void)
                                memory_map.map[j].addr,
                                memory_map.map[j].size) )
                 pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+                pci_mem_start = (pci_mem_start + max_bar_sz - 1) &
+                                    ~(uint64_t)(max_bar_sz - 1);
         }
     }

Note you also can take close look at this change in next revision if this is not that bad with your glance :)

Thanks
Tiejun

#111 lore raw

Re: [RFC][PATCH 12/13] hvmloader/pci: skip reserved ranges

Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 11:21, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:16, <[email protected]> wrote:
Quoted 29 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:34, <[email protected]> wrote:

So I think we may need to adjust pci_mem_start like this,

(quoted diff) +13 -0
@@ -301,6 +301,19 @@ void pci_setup(void)
                pci_mem_start <<= 1;
        }
 
+    /* Relocate PCI memory that overlaps reserved space, like RDM. */
+    for ( j = 0; j < memory_map.nr_map ; j++ )
+    {
+        if ( memory_map.map[j].type != E820_RAM )
+        {
+            reserved_end = memory_map.map[j].addr + memory_map.map[j].size;
+            if ( check_overlap(pci_mem_start, pci_mem_end,
+                               memory_map.map[j].addr,
+                               memory_map.map[j].size) )
+                pci_mem_start -= memory_map.map[j].size >> PAGE_SHIFT;
+        }
+    }
+
        if ( mmio_total > (pci_mem_end - pci_mem_start) )
        {
            printf("Low MMIO hole not large enough for all devices,"
Quoted 1 line

Right?

I think that gets you in the right direction, but isn't enough, as it doesn't account for (unavoidable) gaps (BARs are always a power of 2 in size and accordingly aligned).

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Right.

But as you see, we always take this action, >> PAGE_SHIFT, so this means its always a sort of power of 2.

No, certainly not.

rdm start or size? Or anything else I'm missing?

Both. How can an arbitrary value shifted right be PAGE_SHIFT be guaranteed to be a power of two?

Jan

#112 lore raw

RFC PATCH 13/13 hvmloader/e820: construct guest e820 table

In reply to Tiejun Chen

Now we can use that memory map to build our final
e820 table but it may need to reorder all e820
entries.

---
 tools/firmware/hvmloader/e820.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)
tools/firmware/hvmloader/e820.c +29 -11
index 2e05e93..3aa0be4 100644
@@ -73,7 +73,8 @@ int build_e820_table(struct e820entry *e820,
                      unsigned int lowmem_reserved_base,
                      unsigned int bios_image_base)
 {
-    unsigned int nr = 0;
+    unsigned int nr = 0, i, j;
+    struct e820entry tmp;
 
     if ( !lowmem_reserved_base )
             lowmem_reserved_base = 0xA0000;
@@ -119,10 +120,6 @@ int build_e820_table(struct e820entry *e820,
 
     /* Low RAM goes here. Reserve space for special pages. */
     BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-    e820[nr].addr = 0x100000;
-    e820[nr].size = (hvm_info->low_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-    e820[nr].type = E820_RAM;
-    nr++;
 
     /*
      * Explicitly reserve space for special pages.
@@ -159,16 +156,37 @@ int build_e820_table(struct e820entry *e820,
         nr++;
     }
 
-
-    if ( hvm_info->high_mem_pgend )
+    /* Construct the remaings according memory_map[]. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
     {
-        e820[nr].addr = ((uint64_t)1 << 32);
-        e820[nr].size =
-            ((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-        e820[nr].type = E820_RAM;
+        e820[nr].addr = memory_map.map[i].addr;
+        e820[nr].size = memory_map.map[i].size;
+        e820[nr].type = memory_map.map[i].type;
         nr++;
     }
 
+    /* May need to reorder all e820 entries. */
+    for ( j = 0; j < nr-1; j++ )
+    {
+        for ( i = j+1; i < nr; i++ )
+        {
+            if ( e820[j].addr > e820[i].addr )
+            {
+                tmp.addr = e820[j].addr;
+                tmp.size = e820[j].size;
+                tmp.type = e820[j].type;
+
+                e820[j].addr = e820[i].addr;
+                e820[j].size = e820[i].size;
+                e820[j].type = e820[i].type;
+
+                e820[i].addr = tmp.addr;
+                e820[i].size = tmp.size;
+                e820[i].type = tmp.type;
+            }
+        }
+    }
+
     return nr;
 }
 
Signature · 1 line
1.9.1
#113 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 77

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/e820.c +2 -1
@@ -73,7 +73,8 @@ int build_e820_table(struct e820entry *e820,
                      unsigned int lowmem_reserved_base,
                      unsigned int bios_image_base)
 {
-    unsigned int nr = 0;
+    unsigned int nr = 0, i, j;
+    struct e820entry tmp;

The declaration of "tmp" belongs in the most narrow scope you need it in.

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 125

Quoted 8 lines
tools/firmware/hvmloader/e820.c +0 -4
@@ -119,10 +120,6 @@ int build_e820_table(struct e820entry *e820,
 
     /* Low RAM goes here. Reserve space for special pages. */
     BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-    e820[nr].addr = 0x100000;
-    e820[nr].size = (hvm_info->low_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-    e820[nr].type = E820_RAM;
-    nr++;

I think the above comment needs adjustment with all this code removed. I also wonder how meaningful the BUG_ON() is with ->low_mem_pgend no longer used for E820 table construction. Perhaps this needs another BUG_ON() validating that the field matches some value from memory_map.map[]?

Quoted 6 lines
tools/firmware/hvmloader/e820.c +0 -2
@@ -159,16 +156,37 @@ int build_e820_table(struct e820entry *e820,
         nr++;
     }
 
-
-    if ( hvm_info->high_mem_pgend )

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 164

Quoted 10 lines
tools/firmware/hvmloader/e820.c +5 -4
+    /* Construct the remaings according memory_map[]. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
     {
-        e820[nr].addr = ((uint64_t)1 << 32);
-        e820[nr].size =
-            ((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-        e820[nr].type = E820_RAM;
+        e820[nr].addr = memory_map.map[i].addr;
+        e820[nr].size = memory_map.map[i].size;
+        e820[nr].type = memory_map.map[i].type;

Afaict you could use structure assignment here to make this more readable.

Quoted 11 lines
tools/firmware/hvmloader/e820.c +8 -0
         nr++;
     }
 
+    /* May need to reorder all e820 entries. */
+    for ( j = 0; j < nr-1; j++ )
+    {
+        for ( i = j+1; i < nr; i++ )
+        {
+            if ( e820[j].addr > e820[i].addr )
+            {
+                tmp.addr = e820[j].addr;

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 185

Quoted 10 lines
tools/firmware/hvmloader/e820.c +10 -0
+                tmp.size = e820[j].size;
+                tmp.type = e820[j].type;
+
+                e820[j].addr = e820[i].addr;
+                e820[j].size = e820[i].size;
+                e820[j].type = e820[i].type;
+
+                e820[i].addr = tmp.addr;
+                e820[i].size = tmp.size;
+                e820[i].type = tmp.type;

Please again use structure assignments to make this more readable.

Jan

#114 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 13 lines · Jan Beulich

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 77

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/e820.c +2 -1
@@ -73,7 +73,8 @@ int build_e820_table(struct e820entry *e820,
                       unsigned int lowmem_reserved_base,
                       unsigned int bios_image_base)
  {
-    unsigned int nr = 0;
+    unsigned int nr = 0, i, j;
+    struct e820entry tmp;

The declaration of "tmp" belongs in the most narrow scope you need it in.

Right.

Quoted 15 lines

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 125

Quoted 9 lines
tools/firmware/hvmloader/e820.c +0 -4
@@ -119,10 +120,6 @@ int build_e820_table(struct e820entry *e820,
 
      /* Low RAM goes here. Reserve space for special pages. */
      BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-    e820[nr].addr = 0x100000;
-    e820[nr].size = (hvm_info->low_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-    e820[nr].type = E820_RAM;
-    nr++;

I think the above comment needs adjustment with all this code removed. I also wonder how meaningful the BUG_ON() is with ->low_mem_pgend no longer used for E820 table construction. Perhaps this needs another BUG_ON() validating that the field matches some value from memory_map.map[]?

But I think hvm_info->low_mem_pgend is still correct, right? And additionally, there's no any obvious flag to indicate which memory_map.map[x] is that last low memory map. Even we may separate the low memory to construct memory_map.map[]...

Quoted 5 lines
Quoted 5 lines
tools/firmware/hvmloader/e820.c +0 -0
@@ -159,16 +156,37 @@ int build_e820_table(struct e820entry *e820,
          nr++;
      }
 
Quoted 15 lines

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 164

Quoted 12 lines
tools/firmware/hvmloader/e820.c +5 -6
-
-    if ( hvm_info->high_mem_pgend )
+    /* Construct the remaining according memory_map[]. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
      {
-        e820[nr].addr = ((uint64_t)1 << 32);
-        e820[nr].size =
-            ((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-        e820[nr].type = E820_RAM;
+        e820[nr].addr = memory_map.map[i].addr;
+        e820[nr].size = memory_map.map[i].size;
+        e820[nr].type = memory_map.map[i].type;

Afaict you could use structure assignment here to make this more readable.

Sorry, are you saying this?

memcpy(&e820[nr], &memory_map.map[i], sizeof(struct e820entry));

Quoted 16 lines
Quoted 16 lines
tools/firmware/hvmloader/e820.c +12 -0
          nr++;
      }
 
+    /* May need to reorder all e820 entries. */
+    for ( j = 0; j < nr-1; j++ )
+    {
+        for ( i = j+1; i < nr; i++ )
+        {
+            if ( e820[j].addr > e820[i].addr )
+            {
+                tmp.addr = e820[j].addr;
+                tmp.size = e820[j].size;
+                tmp.type = e820[j].type;
+
+                e820[j].addr = e820[i].addr;
Quoted 9 lines

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 185

Quoted 6 lines
tools/firmware/hvmloader/e820.c +6 -0
+                e820[j].size = e820[i].size;
+                e820[j].type = e820[i].type;
+
+                e820[i].addr = tmp.addr;
+                e820[i].size = tmp.size;
+                e820[i].type = tmp.type;

Please again use structure assignments to make this more readable.

And here,

    for ( j = 0; j < nr-1; j++ )
    {
        for ( i = j+1; i < nr; i++ )
        {
            if ( e820[j].addr > e820[i].addr )
            {
                struct e820entry tmp;

                memcpy(&tmp, &e820[j], sizeof(struct e820entry));

                memcpy(&e820[j], &e820[i], sizeof(struct e820entry));

                memcpy(&e820[i], &tmp, sizeof(struct e820entry));
            }
        }
    }

If I'm wrong please correct me.

Thanks
Tiejun

#115 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 125

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/e820.c +0 -4
@@ -119,10 +120,6 @@ int build_e820_table(struct e820entry *e820,
 
      /* Low RAM goes here. Reserve space for special pages. */
      BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-    e820[nr].addr = 0x100000;
-    e820[nr].size = (hvm_info->low_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-    e820[nr].type = E820_RAM;
-    nr++;

I think the above comment needs adjustment with all this code removed. I also wonder how meaningful the BUG_ON() is with ->low_mem_pgend no longer used for E820 table construction. Perhaps this needs another BUG_ON() validating that the field matches some value from memory_map.map[]?

Quoted 1 line

But I think hvm_info->low_mem_pgend is still correct, right?

I think so, but as said it's becoming less used and hence less relevant here.

Quoted 3 lines

And
additionally, there's no any obvious flag to indicate which memory_map.map[x] is that last low memory map.

I didn't imply it would be immediately obvious _how_ to do this. I'm merely wanting to avoid leaving meaningless BUG_ON()s in the code, while meaningful ones are amiss.

Quoted 2 lines

Even we may separate the
low memory to construct memory_map.map[]...

???

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -159,16 +156,37 @@ int build_e820_table(struct e820entry *e820,
          nr++;
      }
 
Quoted 15 lines

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 164

Quoted 12 lines
tools/firmware/hvmloader/e820.c +5 -6
-
-    if ( hvm_info->high_mem_pgend )
+    /* Construct the remaining according memory_map[]. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
      {
-        e820[nr].addr = ((uint64_t)1 << 32);
-        e820[nr].size =
-            ((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-        e820[nr].type = E820_RAM;
+        e820[nr].addr = memory_map.map[i].addr;
+        e820[nr].size = memory_map.map[i].size;
+        e820[nr].type = memory_map.map[i].type;

Afaict you could use structure assignment here to make this more readable.

Quoted 3 lines

Sorry, are you saying this?

memcpy(&e820[nr], &memory_map.map[i], sizeof(struct e820entry));

No, structure assignment (which, other than memcpy(), is type safe):

    e820[nr] = memory_map.map[i];

Jan

#116 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 125

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 11:22, <[email protected]> wrote:
tools/firmware/hvmloader/e820.c +0 -4
@@ -119,10 +120,6 @@ int build_e820_table(struct e820entry *e820,
 
       /* Low RAM goes here. Reserve space for special pages. */
       BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-    e820[nr].addr = 0x100000;
-    e820[nr].size = (hvm_info->low_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-    e820[nr].type = E820_RAM;
-    nr++;

I think the above comment needs adjustment with all this code removed. I also wonder how meaningful the BUG_ON() is with ->low_mem_pgend no longer used for E820 table construction. Perhaps this needs another BUG_ON() validating that the field matches some value from memory_map.map[]?

Quoted 4 lines
Quoted 1 line

But I think hvm_info->low_mem_pgend is still correct, right?

I think so, but as said it's becoming less used and hence less relevant here.

Understood.

Quoted 8 lines
Quoted 4 lines

And
additionally, there's no any obvious flag to indicate which memory_map.map[x] is that last low memory map.

I didn't imply it would be immediately obvious _how_ to do this. I'm merely wanting to avoid leaving meaningless BUG_ON()s in the code, while meaningful ones are amiss.

Maybe we should lookup all .map[] to get the lowest memory map and then BUG_ON?

Quoted 5 lines
Quoted 3 lines

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases. Is it impossible? Even in the future? Or actually we always consider the lowest memory map?

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 7 lines
tools/firmware/hvmloader/e820.c +0 -2
@@ -159,16 +156,37 @@ int build_e820_table(struct e820entry *e820,
           nr++;
       }
 
-
-    if ( hvm_info->high_mem_pgend )

tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 164

Quoted 10 lines
tools/firmware/hvmloader/e820.c +5 -4
+    /* Construct the remaining according memory_map[]. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
       {
-        e820[nr].addr = ((uint64_t)1 << 32);
-        e820[nr].size =
-            ((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) - e820[nr].addr;
-        e820[nr].type = E820_RAM;
+        e820[nr].addr = memory_map.map[i].addr;
+        e820[nr].size = memory_map.map[i].size;
+        e820[nr].type = memory_map.map[i].type;

Afaict you could use structure assignment here to make this more readable.

Quoted 8 lines
Quoted 3 lines

Sorry, are you saying this?

memcpy(&e820[nr], &memory_map.map[i], sizeof(struct e820entry));

No, structure assignment (which, other than memcpy(), is type safe):

     e820[nr] = memory_map.map[i];

Understood.

Thanks
Tiejun

#117 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

That's correct.

Jan

#118 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

That's correct.

So just lets keep that original BUG_ON()?

Thanks
Tiejun

#119 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Jan

#120 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Sorry just let me clear this.

We still need to check this,

(hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20)

Right? I agree the original is really less relevant as you said. But what is that meaningful BUG_ON() as you expect?

I have no better ideas and just show one draft thought in previous email.

Thanks
Tiejun

#121 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:00, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Quoted 7 lines

Sorry just let me clear this.

We still need to check this,

(hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20)

Right? I agree the original is really less relevant as you said.

And I didn't ask you to drop it. All I asked it to amend it with another BUG_ON() checking what the one above won't cover anymore.

Jan

#122 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:00, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Sorry just let me clear this.

Quoted 9 lines
Quoted 5 lines

We still need to check this,

(hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20)

Right? I agree the original is really less relevant as you said.

And I didn't ask you to drop it. All I asked it to amend it with another BUG_ON() checking what the one above won't cover anymore.

Another point hits me in this case while we're discussing MMIO in another email.

We may populate RAM to get enough MMIO in pci_setup() so this means hvm_info->low_mem_pgend would be changed. Furthermore, low_mem_pgend isn't going to keep recording our original lowmem while building domain. So I think this original BUG_ON() is still good to cover this case. But obviously, we need to adjust its associated memory_map.map[x] right now.

So what about this?

@@ -74,6 +74,7 @@ int build_e820_table(struct e820entry *e820,
unsigned int bios_image_base)

 {
     unsigned int nr = 0, i, j;

+ uint64_t low_mem_pgend = hvm_info->low_mem_pgend << PAGE_SHIFT;

if ( !lowmem_reserved_base )
lowmem_reserved_base = 0xA0000;
@@ -117,9 +118,6 @@ int build_e820_table(struct e820entry *e820,

     e820[nr].type = E820_RESERVED;
     nr++;

- /* Low RAM goes here. Reserve space for special pages. */
- BUG_ON((hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20));
-
/*
* Explicitly reserve space for special pages.
* This space starts at RESERVED_MEMBASE an extends to cover various
@@ -162,6 +160,17 @@ int build_e820_table(struct e820entry *e820,

         nr++;
     }
+    /* Low RAM goes here. Reserve space for special pages. */
+    BUG_ON(low_mem_pgend < (2u << 20));
+    /* We may need to adjust lowmem end. */
+    for ( i = 0; i < memory_map.nr_map; i++ )
+    {

+ uint64_t end = e820[i].addr + e820[i].size;
+ if ( e820[i].type == E820_RAM &&
+ low_mem_pgend > e820[i].addr && low_mem_pgend < end )
+ e820[i].size = low_mem_pgend - e820[i].addr;
+ }
+

     /* May need to reorder all e820 entries. */
     for ( j = 0; j < nr-1; j++ )
     {

Thanks
Tiejun

#123 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:47, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:00, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Sorry just let me clear this.

Quoted 9 lines
Quoted 5 lines

We still need to check this,

(hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20)

Right? I agree the original is really less relevant as you said.

And I didn't ask you to drop it. All I asked it to amend it with another BUG_ON() checking what the one above won't cover anymore.

Another point hits me in this case while we're discussing MMIO in another email.

Quoted 7 lines

We may populate RAM to get enough MMIO in pci_setup() so this means hvm_info->low_mem_pgend would be changed. Furthermore, low_mem_pgend isn't going to keep recording our original lowmem while building domain. So I think this original BUG_ON() is still good to cover this case. But obviously, we need to adjust its associated memory_map.map[x] right now.

So what about this?

I don't think I have enough context anymore of all the other changes that you have pending to be able to reasonably judge on such code fragments. This will need looking at in the context of the next patch series revision.

Jan

#124 lore raw

Re: [RFC][PATCH 13/13] hvmloader/e820: construct guest e820 table

Quoted 47 lines · Jan Beulich
Quoted 47 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:47, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 10:00, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 09:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:39, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.05.15 at 08:11, <[email protected]> wrote:

Even we may separate the
low memory to construct memory_map.map[]...

???

Sorry I just mean that the low memory is not represented with only one memory_map.map[] in some cases.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That's correct.

So just lets keep that original BUG_ON()?

In your previous reply you seemed to agree that the BUG_ON() is becoming meaningless. Why do you now suggest to keep it then?

Sorry just let me clear this.

Quoted 9 lines
Quoted 5 lines

We still need to check this,

(hvm_info->low_mem_pgend << PAGE_SHIFT) < (2u << 20)

Right? I agree the original is really less relevant as you said.

And I didn't ask you to drop it. All I asked it to amend it with another BUG_ON() checking what the one above won't cover anymore.

Another point hits me in this case while we're discussing MMIO in another email.

We may populate RAM to get enough MMIO in pci_setup() so this means hvm_info->low_mem_pgend would be changed. Furthermore, low_mem_pgend isn't going to keep recording our original lowmem while building domain. So I think this original BUG_ON() is still good to cover this case. But obviously, we need to adjust its associated memory_map.map[x] right now.

Quoted 7 lines
Quoted 1 line

So what about this?

I don't think I have enough context anymore of all the other changes that you have pending to be able to reasonably judge on such code fragments. This will need looking at in the context of the next patch series revision.

Okay. Currently I'm still addressing some comments from tools maintainer. Once that is fine enough to step next, I'll send out next revision.

Thanks for your review.

Thanks
Tiejun