From c0a16056b096b97200736878b44308cce5bec339 Mon Sep 17 00:00:00 2001 From: wangguokun Date: Fri, 22 Nov 2024 18:38:24 +0800 Subject: [PATCH 1/3] plat/d9: Introduce `pg_off` and `pg_count` memregion fields To make memory region management easier w.r.t. alignment handling, define two additional fields for `struct ukplat_memregion_desc`: - `pg_off` to represent the in-page offset from where the actual resource this memory region is dedicated to starts - `pg_count` to represent the length of the entire, end-to-end page-aligned, memory region in number of pages Signed-off-by: wangguokun --- d9_bpt64.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/d9_bpt64.S b/d9_bpt64.S index ed49b92..47f565f 100644 --- a/d9_bpt64.S +++ b/d9_bpt64.S @@ -22,10 +22,12 @@ bpt_unmap_mrd: .quad 0x0000000040000000 /* 1GB */ .quad 0x0000000040000000 /* 1GB */ + .quad 0x0000000040000000 /* Page-aligned */ /* Used for struct ukplat_memregion_desc * Unmapping starts at 1GB and ends at 4GB */ .quad 0x00000000c0000000 + .quad 0x00000000000c0000 /* Page count */ .short 0x0000000000000000 .short 0x0000000000000010 /* UKPLAT_MEMRF_UNMAP */ .space 36 -- Gitee From b64d46b68feed542776450e34e58ab482333c380 Mon Sep 17 00:00:00 2001 From: wangguokun Date: Fri, 29 Nov 2024 18:15:19 +0800 Subject: [PATCH 2/3] plat/d9: rework paging init Rework the initialization of paged memory to provide a more flexible implementation that is capable of handling regions beyond the limits defined in the boot pagetables. Under the new scheme bootinfo is reduced to only contain mrds that correspond to valid memory regions. This deprecates the unmap_mrd region and the UKPLAT_MEMRF_MAP / UKPLAT_MEMRF_UNMAP mrd flags. Signed-off-by: wangguokun --- setup.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/setup.c b/setup.c index 48c3c96..cd18d30 100644 --- a/setup.c +++ b/setup.c @@ -37,8 +37,7 @@ static inline int cmdline_init(struct ukplat_bootinfo *bi) * resource instead. */ cmdline = ukplat_memregion_alloc(cmdline_len + 1, UKPLAT_MEMRT_KERNEL, - UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE | - UKPLAT_MEMRF_MAP); + UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE); if (unlikely(!cmdline)) return -ENOMEM; @@ -76,8 +75,7 @@ void _libd9plat_entry(void *fdtp) /* Allocate boot stack */ bstack = ukplat_memregion_alloc(__STACK_SIZE, UKPLAT_MEMRT_STACK, - UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE | - UKPLAT_MEMRF_MAP); + UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE); if (unlikely(!bstack)) UK_CRASH("Boot stack alloc failed\n"); bstack = (void *)((__uptr)bstack + __STACK_SIZE); -- Gitee From 39c26f51f5c0a6c888dcb4cd312ab2f7fa1e3c1a Mon Sep 17 00:00:00 2001 From: wangguokun Date: Fri, 29 Nov 2024 18:18:50 +0800 Subject: [PATCH 3/3] plat/d9: ukplat_memregion_alloc retrieves address via parameter When ukplat_memregion_alloc returns a void * type, if it allocates a memory region (memregion) starting at address 0, it may be mistakenly interpreted as a failed allocation due to the overlap in meaning between address 0 and NULL. Signed-off-by: wangguokun --- setup.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/setup.c b/setup.c index cd18d30..b4e3a7b 100644 --- a/setup.c +++ b/setup.c @@ -15,12 +15,13 @@ #include #endif /* CONFIG_ENFORCE_W_XOR_X && CONFIG_PAGING */ -static char *cmdline; +static __u64 cmdline; static __sz cmdline_len; static inline int cmdline_init(struct ukplat_bootinfo *bi) { char *cmdl; + int rc; if (bi->cmdline_len) { cmdl = (char *)bi->cmdline; @@ -36,20 +37,22 @@ static inline int cmdline_init(struct ukplat_bootinfo *bi) * by `ukplat_entry_argp` to obtain argc/argv. So mark it as a kernel * resource instead. */ - cmdline = ukplat_memregion_alloc(cmdline_len + 1, UKPLAT_MEMRT_KERNEL, - UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE); - if (unlikely(!cmdline)) + rc = ukplat_memregion_alloc(cmdline_len + 1, UKPLAT_MEMRT_KERNEL, + UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE, + &cmdline); + if (unlikely(rc)) return -ENOMEM; - memcpy(cmdline, cmdl, cmdline_len); - cmdline[cmdline_len] = 0; + memcpy((void *)cmdline, cmdl, cmdline_len); + cmdl = (char *)cmdline; + cmdl[cmdline_len] = 0; return 0; } static void __noreturn _ukplat_entry2(void) { - ukplat_entry_argp(NULL, cmdline, cmdline_len); + ukplat_entry_argp(NULL, (char *)cmdline, cmdline_len); ukplat_lcpu_halt(); } @@ -57,7 +60,7 @@ static void __noreturn _ukplat_entry2(void) void _libd9plat_entry(void *fdtp) { int rc; - void *bstack; + __u64 bstack; struct ukplat_bootinfo *bi; ukplat_bootinfo_fdt_setup(fdtp); @@ -74,11 +77,12 @@ void _libd9plat_entry(void *fdtp) UK_CRASH("Failed to initialize command-line\n"); /* Allocate boot stack */ - bstack = ukplat_memregion_alloc(__STACK_SIZE, UKPLAT_MEMRT_STACK, - UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE); - if (unlikely(!bstack)) + rc = ukplat_memregion_alloc(__STACK_SIZE, UKPLAT_MEMRT_STACK, + UKPLAT_MEMRF_READ | UKPLAT_MEMRF_WRITE, + &bstack); + if (unlikely(rc)) UK_CRASH("Boot stack alloc failed\n"); - bstack = (void *)((__uptr)bstack + __STACK_SIZE); + bstack = (__u64)((__uptr)bstack + __STACK_SIZE); /* Initialize paging */ rc = ukplat_mem_init(); @@ -109,7 +113,7 @@ void _libd9plat_entry(void *fdtp) /* * Switch away from the bootstrap stack as early as possible. */ - uk_pr_info("Switch from bootstrap stack to stack @%p\n", bstack); + uk_pr_info("Switch from bootstrap stack to stack @%lx\n", bstack); /* Print boot information */ uk_pr_info("Print bootinfo before jump to entry:\n"); @@ -118,5 +122,5 @@ void _libd9plat_entry(void *fdtp) /* * Enter Unikraft with new allocated stack. */ - lcpu_arch_jump_to(bstack, _ukplat_entry2); + lcpu_arch_jump_to((void *)bstack, _ukplat_entry2); } -- Gitee