/* 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__INIT2728/*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#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)94/* Enable PAE mode. */95mov %cr4, %eax96orl $X86_CR4_PAE, %eax97mov %eax, %cr498#endif99100#ifdef CONFIG_X86_64101/* Enable Long mode. */102mov $MSR_EFER, %ecx103rdmsr104btsl $_EFER_LME, %eax105wrmsr106107/*108* Reuse the non-relocatable symbol emitted for the ELF note to109* subtract the build time physical address of pvh_start_xen() from110* its actual runtime address, without relying on absolute 32-bit ELF111* relocations, as these are not supported by the linker when running112* in -pie mode, and should be avoided in .head.text in general.113*/114mov %ebp, %ebx115subl rva(xen_elfnote_phys32_entry)(%ebp), %ebx116jz .Lpagetable_done117118/*119* Store the resulting load offset in phys_base. __pa() needs120* phys_base set to calculate the hypercall page in xen_pvh_init().121*/122movl %ebx, rva(phys_base)(%ebp)123124/* Fixup page-tables for relocation. */125leal rva(pvh_init_top_pgt)(%ebp), %edi126movl $PTRS_PER_PGD, %ecx1272:128testl $_PAGE_PRESENT, 0x00(%edi)129jz 1f130addl %ebx, 0x00(%edi)1311:132addl $8, %edi133decl %ecx134jnz 2b135136/* L3 ident has a single entry. */137leal rva(pvh_level3_ident_pgt)(%ebp), %edi138addl %ebx, 0x00(%edi)139140leal rva(pvh_level3_kernel_pgt)(%ebp), %edi141addl %ebx, (PAGE_SIZE - 16)(%edi)142addl %ebx, (PAGE_SIZE - 8)(%edi)143144/* pvh_level2_ident_pgt is fine - large pages */145146/* pvh_level2_kernel_pgt needs adjustment - large pages */147leal rva(pvh_level2_kernel_pgt)(%ebp), %edi148movl $PTRS_PER_PMD, %ecx1492:150testl $_PAGE_PRESENT, 0x00(%edi)151jz 1f152addl %ebx, 0x00(%edi)1531:154addl $8, %edi155decl %ecx156jnz 2b157158.Lpagetable_done:159/* Enable pre-constructed page tables. */160leal rva(pvh_init_top_pgt)(%ebp), %eax161mov %eax, %cr3162mov $(X86_CR0_PG | X86_CR0_PE), %eax163mov %eax, %cr0164165/* Jump to 64-bit mode. */166pushl $PVH_CS_SEL167leal rva(1f)(%ebp), %eax168pushl %eax169lretl170171/* 64-bit entry point. */172.code641731:174UNWIND_HINT_END_OF_STACK175176/*177* Set up GSBASE.178* Note that on SMP the boot CPU uses the init data section until179* the per-CPU areas are set up.180*/181movl $MSR_GS_BASE,%ecx182xorl %eax, %eax183xorl %edx, %edx184wrmsr185186/* Call xen_prepare_pvh() via the kernel virtual mapping */187leaq xen_prepare_pvh(%rip), %rax188subq phys_base(%rip), %rax189addq $__START_KERNEL_map, %rax190ANNOTATE_RETPOLINE_SAFE191call *%rax192193/* startup_64 expects boot_params in %rsi. */194lea pvh_bootparams(%rip), %rsi195jmp startup_64196197#else /* CONFIG_X86_64 */198199call mk_early_pgtbl_32200201mov $_pa(initial_page_table), %eax202mov %eax, %cr3203204mov %cr0, %eax205or $(X86_CR0_PG | X86_CR0_PE), %eax206mov %eax, %cr0207208ljmp $PVH_CS_SEL, $1f2091:210call xen_prepare_pvh211mov $_pa(pvh_bootparams), %esi212213/* startup_32 doesn't expect paging and PAE to be on. */214ljmp $PVH_CS_SEL, $_pa(2f)2152:216mov %cr0, %eax217and $~X86_CR0_PG, %eax218mov %eax, %cr0219mov %cr4, %eax220and $~X86_CR4_PAE, %eax221mov %eax, %cr4222223ljmp $PVH_CS_SEL, $_pa(startup_32)224#endif225SYM_CODE_END(pvh_start_xen)226227.section ".init.data","aw"228.balign 8229SYM_DATA_START_LOCAL(gdt)230.word gdt_end - gdt_start - 1231.long gdt_start - gdt232.word 0233SYM_DATA_END(gdt)234SYM_DATA_START_LOCAL(gdt_start)235.quad 0x0000000000000000 /* NULL descriptor */236#ifdef CONFIG_X86_64237.quad GDT_ENTRY(DESC_CODE64, 0, 0xfffff) /* PVH_CS_SEL */238#else239.quad GDT_ENTRY(DESC_CODE32, 0, 0xfffff) /* PVH_CS_SEL */240#endif241.quad GDT_ENTRY(DESC_DATA32, 0, 0xfffff) /* PVH_DS_SEL */242SYM_DATA_END_LABEL(gdt_start, SYM_L_LOCAL, gdt_end)243244.balign 16245SYM_DATA_START_LOCAL(early_stack)246.fill BOOT_STACK_SIZE, 1, 0247SYM_DATA_END_LABEL(early_stack, SYM_L_LOCAL, early_stack_end)248249#ifdef CONFIG_X86_64250/*251* Xen PVH needs a set of identity mapped and kernel high mapping252* page tables. pvh_start_xen starts running on the identity mapped253* page tables, but xen_prepare_pvh calls into the high mapping.254* These page tables need to be relocatable and are only used until255* startup_64 transitions to init_top_pgt.256*/257SYM_DATA_START_PAGE_ALIGNED(pvh_init_top_pgt)258.quad pvh_level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC259.org pvh_init_top_pgt + L4_PAGE_OFFSET * 8, 0260.quad pvh_level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC261.org pvh_init_top_pgt + L4_START_KERNEL * 8, 0262/* (2^48-(2*1024*1024*1024))/(2^39) = 511 */263.quad pvh_level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC264SYM_DATA_END(pvh_init_top_pgt)265266SYM_DATA_START_PAGE_ALIGNED(pvh_level3_ident_pgt)267.quad pvh_level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC268.fill 511, 8, 0269SYM_DATA_END(pvh_level3_ident_pgt)270SYM_DATA_START_PAGE_ALIGNED(pvh_level2_ident_pgt)271/*272* Since I easily can, map the first 1G.273* Don't set NX because code runs from these pages.274*275* Note: This sets _PAGE_GLOBAL despite whether276* the CPU supports it or it is enabled. But,277* the CPU should ignore the bit.278*/279PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)280SYM_DATA_END(pvh_level2_ident_pgt)281SYM_DATA_START_PAGE_ALIGNED(pvh_level3_kernel_pgt)282.fill L3_START_KERNEL, 8, 0283/* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */284.quad pvh_level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC285.quad 0 /* no fixmap */286SYM_DATA_END(pvh_level3_kernel_pgt)287288SYM_DATA_START_PAGE_ALIGNED(pvh_level2_kernel_pgt)289/*290* Kernel high mapping.291*292* The kernel code+data+bss must be located below KERNEL_IMAGE_SIZE in293* virtual address space, which is 1 GiB if RANDOMIZE_BASE is enabled,294* 512 MiB otherwise.295*296* (NOTE: after that starts the module area, see MODULES_VADDR.)297*298* This table is eventually used by the kernel during normal runtime.299* Care must be taken to clear out undesired bits later, like _PAGE_RW300* or _PAGE_GLOBAL in some cases.301*/302PMDS(0, __PAGE_KERNEL_LARGE_EXEC, KERNEL_IMAGE_SIZE / PMD_SIZE)303SYM_DATA_END(pvh_level2_kernel_pgt)304305ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_RELOC,306.long CONFIG_PHYSICAL_ALIGN;307.long LOAD_PHYSICAL_ADDR;308.long KERNEL_IMAGE_SIZE - 1)309#endif310311ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .global xen_elfnote_phys32_entry;312xen_elfnote_phys32_entry: _ASM_PTR xen_elfnote_phys32_entry_value - .)313314315