]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
plat/common: Error out if free regions not aligned for paging init
authorSergiu Moga <sergiu@unikraft.io>
Sat, 19 Aug 2023 15:56:01 +0000 (18:56 +0300)
committerRazvan Deaconescu <razvand@unikraft.io>
Fri, 20 Oct 2023 16:35:55 +0000 (19:35 +0300)
If the early code did everything right when setting up the memory
regions, then no free memory region should be misaligned by this
point. Otherwise, something bad happened so throw an exception.

Signed-off-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
GitHub-Closes: #1060

plat/common/paging.c

index 85916163a3d659a9131caa24cfbff942bb138a33..c046293af95140c82b0244cec884488e5bb7579c 100644 (file)
@@ -1453,22 +1453,22 @@ int ukplat_paging_init(void)
         */
        rc = -ENOMEM; /* In case there is no region */
        ukplat_memregion_foreach(&mrd, UKPLAT_MEMRT_FREE, 0, 0) {
-               paddr  = PAGE_ALIGN_UP(mrd->pbase);
-               len    = PAGE_ALIGN_DOWN(mrd->len - (paddr - mrd->pbase));
+               UK_ASSERT(mrd->vbase == mrd->pbase);
+               UK_ASSERT(!(mrd->pbase & ~PAGE_MASK));
+               UK_ASSERT(mrd->len);
+               UK_ASSERT(!(mrd->len & ~PAGE_MASK));
 
                /* Not mapped */
                mrd->vbase = __U64_MAX;
                mrd->flags &= ~UKPLAT_MEMRF_PERMS;
 
-               if (unlikely(len == 0))
-                       continue;
-
                if (!kernel_pt.fa) {
-                       rc = ukplat_pt_init(&kernel_pt, paddr, len);
+                       rc = ukplat_pt_init(&kernel_pt, mrd->pbase, mrd->len);
                        if (unlikely(rc))
                                kernel_pt.fa = NULL;
                } else {
-                       rc = ukplat_pt_add_mem(&kernel_pt, paddr, len);
+                       rc = ukplat_pt_add_mem(&kernel_pt, mrd->pbase,
+                                              mrd->len);
                }
 
                /* We do not fail if we cannot add this memory region to the
@@ -1477,9 +1477,14 @@ int ukplat_paging_init(void)
                 */
                if (unlikely(rc && rc != -ENOMEM))
                        uk_pr_err("Cannot add %12lx-%12lx to paging: %d\n",
-                                 paddr, paddr + len, rc);
+                                 mrd->pbase, mrd->pbase + mrd->len, rc);
        }
 
+       /* The frame allocator should've only had page-aligned memory regions
+        * added to it. Make sure nothing happened in the meantime.
+        */
+       UK_ASSERT(!(kernel_pt.fa->free_memory & ~PAGE_MASK));
+
        if (unlikely(!kernel_pt.fa))
                return rc;