[2/2] x86/setup: Preserve _ENC flag when initrd is being relocated
Commit Message
Commit 107cd2532181 ("Encrypt the initrd earlier for BSP microcode update")
when SME is enabled, initrd will be encrypted at earlier stage. If
initrd is located at e820 reserved area the initrd will be copied to
direct mapping area in relocate_initrd().
In this case source address of initrd should be mapped as encrypted
while copy_from_early_mem() will clear encrypted attribute as the source
address is not in kernel usable area, therefore relocated initrd is
encrypted data and is not able to be unpacked later.
Add new function copy_early_initrd() to preserve _ENC flag in setup.c
and remove copy_from_early_mem() as it's only used once here by x86.
Signed-off-by: Zelin Deng <zelin.deng@linux.alibaba.com>
---
arch/x86/kernel/setup.c | 30 ++++++++++++++++++++++++++++-
include/asm-generic/early_ioremap.h | 6 ------
mm/early_ioremap.c | 21 --------------------
3 files changed, 29 insertions(+), 28 deletions(-)
Comments
在 2022/11/24 17:12, Zelin Deng 写道:
> Commit 107cd2532181 ("Encrypt the initrd earlier for BSP microcode update")
> when SME is enabled, initrd will be encrypted at earlier stage. If
> initrd is located at e820 reserved area the initrd will be copied to
> direct mapping area in relocate_initrd().
>
> In this case source address of initrd should be mapped as encrypted
> while copy_from_early_mem() will clear encrypted attribute as the source
> address is not in kernel usable area, therefore relocated initrd is
> encrypted data and is not able to be unpacked later.
>
> Add new function copy_early_initrd() to preserve _ENC flag in setup.c
> and remove copy_from_early_mem() as it's only used once here by x86.
>
> Signed-off-by: Zelin Deng <zelin.deng@linux.alibaba.com>
> ---
> arch/x86/kernel/setup.c | 30 ++++++++++++++++++++++++++++-
> include/asm-generic/early_ioremap.h | 6 ------
> mm/early_ioremap.c | 21 --------------------
> 3 files changed, 29 insertions(+), 28 deletions(-)
>
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 56deaf37e508..f9996982f026 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -260,6 +260,34 @@ static u64 __init get_ramdisk_size(void)
> return ramdisk_size;
> }
>
> +#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
> +
> +static void __init copy_early_initrd(void *dest, phys_addr_t src,
> + unsigned long size)
> +{
> + unsigned long slop, clen;
> + char *p;
> +
> + while (size) {
> + slop = offset_in_page(src);
> + clen = size;
> + if (clen > MAX_MAP_CHUNK - slop)
> + clen = MAX_MAP_CHUNK - slop;
> + /*
> + * _ENC flag should be preserved so that when SME is enabled initrd
> + * can be mapped as encrypted, as it had been encrypted earlier.
> + * This flag won't impact on other platforms like TDX/SEV enabled.
> + */
> + p = early_memremap_prot(src & PAGE_MASK, clen + slop,
> + pgprot_val(FIXMAP_PAGE_NORMAL));
Sorry my mistake, perhaps cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)
should be added here to determine if we are in guest to adjust the pgprot.
The scenarios in which initrd should not be encrypted are "SME not
enabled" and in tdx host? Looks like _ENC flag is not enough.
> + memcpy(dest, p + slop, clen);
> + early_memunmap(p, clen + slop);
> + dest += clen;
> + src += clen;
> + size -= clen;
> + }
> +}
> +
> static void __init relocate_initrd(void)
> {
> /* Assume only end is not page aligned */
> @@ -279,7 +307,7 @@ static void __init relocate_initrd(void)
> printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
> relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
>
> - copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
> + copy_early_initrd((void *)initrd_start, ramdisk_image, ramdisk_size);
>
> printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
> " [mem %#010llx-%#010llx]\n",
> diff --git a/include/asm-generic/early_ioremap.h b/include/asm-generic/early_ioremap.h
> index 9d0479f50f97..be1ce406f481 100644
> --- a/include/asm-generic/early_ioremap.h
> +++ b/include/asm-generic/early_ioremap.h
> @@ -32,12 +32,6 @@ extern void early_ioremap_setup(void);
> */
> extern void early_ioremap_reset(void);
>
> -/*
> - * Early copy from unmapped memory to kernel mapped memory.
> - */
> -extern void copy_from_early_mem(void *dest, phys_addr_t src,
> - unsigned long size);
> -
> #else
> static inline void early_ioremap_init(void) { }
> static inline void early_ioremap_setup(void) { }
> diff --git a/mm/early_ioremap.c b/mm/early_ioremap.c
> index 9bc12e526ed0..86b68d63ad35 100644
> --- a/mm/early_ioremap.c
> +++ b/mm/early_ioremap.c
> @@ -245,27 +245,6 @@ early_memremap_prot(resource_size_t phys_addr, unsigned long size,
> }
> #endif
>
> -#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
> -
> -void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
> -{
> - unsigned long slop, clen;
> - char *p;
> -
> - while (size) {
> - slop = offset_in_page(src);
> - clen = size;
> - if (clen > MAX_MAP_CHUNK - slop)
> - clen = MAX_MAP_CHUNK - slop;
> - p = early_memremap(src & PAGE_MASK, clen + slop);
> - memcpy(dest, p + slop, clen);
> - early_memunmap(p, clen + slop);
> - dest += clen;
> - src += clen;
> - size -= clen;
> - }
> -}
> -
> #else /* CONFIG_MMU */
>
> void __init __iomem *
@@ -260,6 +260,34 @@ static u64 __init get_ramdisk_size(void)
return ramdisk_size;
}
+#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
+
+static void __init copy_early_initrd(void *dest, phys_addr_t src,
+ unsigned long size)
+{
+ unsigned long slop, clen;
+ char *p;
+
+ while (size) {
+ slop = offset_in_page(src);
+ clen = size;
+ if (clen > MAX_MAP_CHUNK - slop)
+ clen = MAX_MAP_CHUNK - slop;
+ /*
+ * _ENC flag should be preserved so that when SME is enabled initrd
+ * can be mapped as encrypted, as it had been encrypted earlier.
+ * This flag won't impact on other platforms like TDX/SEV enabled.
+ */
+ p = early_memremap_prot(src & PAGE_MASK, clen + slop,
+ pgprot_val(FIXMAP_PAGE_NORMAL));
+ memcpy(dest, p + slop, clen);
+ early_memunmap(p, clen + slop);
+ dest += clen;
+ src += clen;
+ size -= clen;
+ }
+}
+
static void __init relocate_initrd(void)
{
/* Assume only end is not page aligned */
@@ -279,7 +307,7 @@ static void __init relocate_initrd(void)
printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
- copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
+ copy_early_initrd((void *)initrd_start, ramdisk_image, ramdisk_size);
printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
" [mem %#010llx-%#010llx]\n",
@@ -32,12 +32,6 @@ extern void early_ioremap_setup(void);
*/
extern void early_ioremap_reset(void);
-/*
- * Early copy from unmapped memory to kernel mapped memory.
- */
-extern void copy_from_early_mem(void *dest, phys_addr_t src,
- unsigned long size);
-
#else
static inline void early_ioremap_init(void) { }
static inline void early_ioremap_setup(void) { }
@@ -245,27 +245,6 @@ early_memremap_prot(resource_size_t phys_addr, unsigned long size,
}
#endif
-#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
-
-void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
-{
- unsigned long slop, clen;
- char *p;
-
- while (size) {
- slop = offset_in_page(src);
- clen = size;
- if (clen > MAX_MAP_CHUNK - slop)
- clen = MAX_MAP_CHUNK - slop;
- p = early_memremap(src & PAGE_MASK, clen + slop);
- memcpy(dest, p + slop, clen);
- early_memunmap(p, clen + slop);
- dest += clen;
- src += clen;
- size -= clen;
- }
-}
-
#else /* CONFIG_MMU */
void __init __iomem *