/* SPDX-License-Identifier: GPL-2.0 */12/*3* Copyright C 2016, Oracle and/or its affiliates. All rights reserved.4*/56.code327.text8#ifdef CONFIG_X86_329#define _pa(x) ((x) - __START_KERNEL_map)10#endif11#define rva(x) ((x) - pvh_start_xen)1213#include <linux/elfnote.h>14#include <linux/init.h>15#include <linux/linkage.h>16#include <asm/desc_defs.h>17#include <asm/segment.h>18#include <asm/asm.h>19#include <asm/boot.h>20#include <asm/pgtable.h>21#include <asm/processor-flags.h>22#include <asm/msr.h>23#include <asm/nospec-branch.h>24#include <xen/interface/elfnote.h>2526__HEAD2728/*29* Entry point for PVH guests.30*31* Xen ABI specifies the following register state when we come here:32*33* - `ebx`: contains the physical memory address where the loader has placed34* the boot start info structure.35* - `cr0`: bit 0 (PE) must be set. All the other writeable bits are cleared.36* - `cr4`: all bits are cleared.37* - `cs `: must be a 32-bit read/execute code segment with a base of `0`38* and a limit of `0xFFFFFFFF`. The selector value is unspecified.39* - `ds`, `es`: must be a 32-bit read/write data segment with a base of40* `0` and a limit of `0xFFFFFFFF`. The selector values are all41* unspecified.42* - `tr`: must be a 32-bit TSS (active) with a base of '0' and a limit43* of '0x67'.44* - `eflags`: bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared.45* Bit 8 (TF) must be cleared. Other bits are all unspecified.46*47* All other processor registers and flag bits are unspecified. The OS is in48* charge of setting up its own stack, GDT and IDT.49*/5051#define PVH_GDT_ENTRY_CS 152#define PVH_GDT_ENTRY_DS 253#define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8)54#define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8)5556SYM_CODE_START(pvh_start_xen)57UNWIND_HINT_END_OF_STACK58cld5960/*61* See the comment for startup_32 for more details. We need to62* execute a call to get the execution address to be position63* independent, but we don't have a stack. Save and restore the64* magic field of start_info in ebx, and use that as the stack.65*/66mov (%ebx), %eax67leal 4(%ebx), %esp68ANNOTATE_INTRA_FUNCTION_CALL69call 1f701: popl %ebp71mov %eax, (%ebx)72subl $rva(1b), %ebp73movl $0, %esp7475leal rva(gdt)(%ebp), %eax76addl %eax, 2(%eax)77lgdt (%eax)7879mov $PVH_DS_SEL,%eax80mov %eax,%ds81mov %eax,%es82mov %eax,%ss8384/* Stash hvm_start_info. */85leal rva(pvh_start_info)(%ebp), %edi86mov %ebx, %esi87movl rva(pvh_start_info_sz)(%ebp), %ecx88shr $2,%ecx89rep movsl9091leal rva(early_stack_end)(%ebp), %esp9293/* Enable PAE mode. */94mov %cr4, %eax95orl $X86_CR4_PAE, %eax96mov %eax, %cr49798#ifdef CONFIG_X86_6499/* Enable Long mode. */100mov $MSR_EFER, %ecx101rdmsr102btsl $_EFER_LME, %eax103wrmsr104105/*106* Reuse the non-relocatable symbol emitted for the ELF note to107* subtract the build time physical address of pvh_start_xen() from108* its actual runtime address, without relying on absolute 32-bit ELF109* relocations, as these are not supported by the linker when running110* in -pie mode, and should be avoided in .head.text in general.111*/112mov %ebp, %ebx113subl rva(xen_elfnote_phys32_entry)(%ebp), %ebx114jz .Lpagetable_done115116/*117* Store the resulting load offset in phys_base. __pa() needs118* phys_base set to calculate the hypercall page in xen_pvh_init().119*/120movl %ebx, rva(phys_base)(%ebp)121122/* Fixup page-tables for relocation. */123leal rva(pvh_init_top_pgt)(%ebp), %edi124movl $PTRS_PER_PGD, %ecx1252:126testl $_PAGE_PRESENT, 0x00(%edi)127jz 1f128addl %ebx, 0x00(%edi)1291:130addl $8, %edi131decl %ecx132jnz 2b133134/* L3 ident has a single entry. */135leal rva(pvh_level3_ident_pgt)(%ebp), %edi136addl %ebx, 0x00(%edi)137138leal rva(pvh_level3_kernel_pgt)(%ebp), %edi139addl %ebx, (PAGE_SIZE - 16)(%edi)140addl %ebx, (PAGE_SIZE - 8)(%edi)141142/* pvh_level2_ident_pgt is fine - large pages */143144/* pvh_level2_kernel_pgt needs adjustment - large pages */145leal rva(pvh_level2_kernel_pgt)(%ebp), %edi146movl $PTRS_PER_PMD, %ecx1472:148testl $_PAGE_PRESENT, 0x00(%edi)149jz 1f150addl %ebx, 0x00(%edi)1511:152addl $8, %edi153decl %ecx154jnz 2b155156.Lpagetable_done:157/* Enable pre-constructed page tables. */158leal rva(pvh_init_top_pgt)(%ebp), %eax159mov %eax, %cr3160mov $(X86_CR0_PG | X86_CR0_PE), %eax161mov %eax, %cr0162163/* Jump to 64-bit mode. */164pushl $PVH_CS_SEL165leal rva(1f)(%ebp), %eax166pushl %eax167lretl168169/* 64-bit entry point. */170.code641711:172UNWIND_HINT_END_OF_STACK173174/*175* Set up GSBASE.176* Note that on SMP the boot CPU uses the init data section until177* the per-CPU areas are set up.178*/179movl $MSR_GS_BASE,%ecx180xorl %eax, %eax181xorl %edx, %edx182wrmsr183184/* Call xen_prepare_pvh() via the kernel virtual mapping */185leaq xen_prepare_pvh(%rip), %rax186subq phys_base(%rip), %rax187addq $__START_KERNEL_map, %rax188ANNOTATE_RETPOLINE_SAFE189call *%rax190191/* startup_64 expects boot_params in %rsi. */192lea pvh_bootparams(%rip), %rsi193jmp startup_64194195#else /* CONFIG_X86_64 */196197call mk_early_pgtbl_32198199mov $_pa(initial_page_table), %eax200mov %eax, %cr3201202mov %cr0, %eax203or $(X86_CR0_PG | X86_CR0_PE), %eax204mov %eax, %cr0205206ljmp $PVH_CS_SEL, $1f2071:208call xen_prepare_pvh209mov $_pa(pvh_bootparams), %esi210211/* startup_32 doesn't expect paging and PAE to be on. */212ljmp $PVH_CS_SEL, $_pa(2f)2132:214mov %cr0, %eax215and $~X86_CR0_PG, %eax216mov %eax, %cr0217mov %cr4, %eax218and $~X86_CR4_PAE, %eax219mov %eax, %cr4220221ljmp $PVH_CS_SEL, $_pa(startup_32)222#endif223SYM_CODE_END(pvh_start_xen)224225.section ".init.data","aw"226.balign 8227SYM_DATA_START_LOCAL(gdt)228.word gdt_end - gdt_start - 1229.long gdt_start - gdt230.word 0231SYM_DATA_END(gdt)232SYM_DATA_START_LOCAL(gdt_start)233.quad 0x0000000000000000 /* NULL descriptor */234#ifdef CONFIG_X86_64235.quad GDT_ENTRY(DESC_CODE64, 0, 0xfffff) /* PVH_CS_SEL */236#else237.quad GDT_ENTRY(DESC_CODE32, 0, 0xfffff) /* PVH_CS_SEL */238#endif239.quad GDT_ENTRY(DESC_DATA32, 0, 0xfffff) /* PVH_DS_SEL */240SYM_DATA_END_LABEL(gdt_start, SYM_L_LOCAL, gdt_end)241242.balign 16243SYM_DATA_START_LOCAL(early_stack)244.fill BOOT_STACK_SIZE, 1, 0245SYM_DATA_END_LABEL(early_stack, SYM_L_LOCAL, early_stack_end)246247#ifdef CONFIG_X86_64248/*249* Xen PVH needs a set of identity mapped and kernel high mapping250* page tables. pvh_start_xen starts running on the identity mapped251* page tables, but xen_prepare_pvh calls into the high mapping.252* These page tables need to be relocatable and are only used until253* startup_64 transitions to init_top_pgt.254*/255SYM_DATA_START_PAGE_ALIGNED(pvh_init_top_pgt)256.quad pvh_level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC257.org pvh_init_top_pgt + L4_PAGE_OFFSET * 8, 0258.quad pvh_level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC259.org pvh_init_top_pgt + L4_START_KERNEL * 8, 0260/* (2^48-(2*1024*1024*1024))/(2^39) = 511 */261.quad pvh_level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC262SYM_DATA_END(pvh_init_top_pgt)263264SYM_DATA_START_PAGE_ALIGNED(pvh_level3_ident_pgt)265.quad pvh_level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC266.fill 511, 8, 0267SYM_DATA_END(pvh_level3_ident_pgt)268SYM_DATA_START_PAGE_ALIGNED(pvh_level2_ident_pgt)269/*270* Since I easily can, map the first 1G.271* Don't set NX because code runs from these pages.272*273* Note: This sets _PAGE_GLOBAL despite whether274* the CPU supports it or it is enabled. But,275* the CPU should ignore the bit.276*/277PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)278SYM_DATA_END(pvh_level2_ident_pgt)279SYM_DATA_START_PAGE_ALIGNED(pvh_level3_kernel_pgt)280.fill L3_START_KERNEL, 8, 0281/* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */282.quad pvh_level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC283.quad 0 /* no fixmap */284SYM_DATA_END(pvh_level3_kernel_pgt)285286SYM_DATA_START_PAGE_ALIGNED(pvh_level2_kernel_pgt)287/*288* Kernel high mapping.289*290* The kernel code+data+bss must be located below KERNEL_IMAGE_SIZE in291* virtual address space, which is 1 GiB if RANDOMIZE_BASE is enabled,292* 512 MiB otherwise.293*294* (NOTE: after that starts the module area, see MODULES_VADDR.)295*296* This table is eventually used by the kernel during normal runtime.297* Care must be taken to clear out undesired bits later, like _PAGE_RW298* or _PAGE_GLOBAL in some cases.299*/300PMDS(0, __PAGE_KERNEL_LARGE_EXEC, KERNEL_IMAGE_SIZE / PMD_SIZE)301SYM_DATA_END(pvh_level2_kernel_pgt)302303ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_RELOC,304.long CONFIG_PHYSICAL_ALIGN;305.long LOAD_PHYSICAL_ADDR;306.long KERNEL_IMAGE_SIZE - 1)307#endif308309ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .global xen_elfnote_phys32_entry;310xen_elfnote_phys32_entry: _ASM_PTR xen_elfnote_phys32_entry_value - .)311312313