From d40e8ede8e166165ef45f3b488e00a48917c17e4 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 12 Feb 2021 12:36:49 -0800 Subject: [PATCH] x86: gen_gdt: add address translation if needed When the kernel is mapped into virtual address space that is different than the physical address space, the dynamic GDT generation uses the virtual addresses. However, the GDT table is required at boot before page table is loaded where the virtual addresses are invalid. So make sure GDT generation is using physical addresses. Signed-off-by: Daniel Leung --- arch/x86/gen_gdt.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/arch/x86/gen_gdt.py b/arch/x86/gen_gdt.py index 3465499b5f5..c93e891be1d 100755 --- a/arch/x86/gen_gdt.py +++ b/arch/x86/gen_gdt.py @@ -187,7 +187,24 @@ def main(): # x86_64 does not use descriptor for thread local storage num_entries += 1 - gdt_base = syms["_gdt"] + if "CONFIG_X86_MMU" in syms: + vm_base = syms["CONFIG_KERNEL_VM_BASE"] + vm_offset = syms["CONFIG_KERNEL_VM_OFFSET"] + + sram_base = syms["CONFIG_SRAM_BASE_ADDRESS"] + + if "CONFIG_SRAM_OFFSET" in syms: + sram_offset = syms["CONFIG_SRAM_OFFSET"] + else: + sram_offset = 0 + + # Figure out if there is any need to do virtual-to-physical + # address translation + virt_to_phys_offset = (sram_base + sram_offset) - (vm_base + vm_offset) + else: + virt_to_phys_offset = 0 + + gdt_base = syms["_gdt"] + virt_to_phys_offset with open(args.output_gdt, "wb") as fp: # The pseudo descriptor is stuffed into the NULL descriptor @@ -203,8 +220,8 @@ def main(): FLAGS_GRAN, ACCESS_RW)) if num_entries >= 5: - main_tss = syms["_main_tss"] - df_tss = syms["_df_tss"] + main_tss = syms["_main_tss"] + virt_to_phys_offset + df_tss = syms["_df_tss"] + virt_to_phys_offset # Selector 0x18: main TSS fp.write(create_tss_entry(main_tss, 0x67, 0))