xref: /linux/arch/x86/platform/pvh/enlighten.c (revision 0f912c8917e810a4aa81d122a8e7d0a918505ab9)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 
4 #include <xen/hvc-console.h>
5 
6 #include <asm/bootparam.h>
7 #include <asm/io_apic.h>
8 #include <asm/hypervisor.h>
9 #include <asm/e820/api.h>
10 #include <asm/x86_init.h>
11 
12 #include <asm/xen/interface.h>
13 
14 #include <xen/xen.h>
15 #include <xen/interface/hvm/start_info.h>
16 
17 /*
18  * PVH variables.
19  *
20  * pvh_bootparams and pvh_start_info need to live in a data segment since
21  * they are used after startup_{32|64}, which clear .bss, are invoked.
22  */
23 struct boot_params __initdata pvh_bootparams;
24 struct hvm_start_info __initdata pvh_start_info;
25 
26 const unsigned int __initconst pvh_start_info_sz = sizeof(pvh_start_info);
27 
28 /*
29  * Xen guests are able to obtain the memory map from the hypervisor via the
30  * HYPERVISOR_memory_op hypercall.
31  * If we are trying to boot a Xen PVH guest, it is expected that the kernel
32  * will have been configured to provide an override for this routine to do
33  * just that.
34  */
mem_map_via_hcall(struct boot_params * ptr __maybe_unused)35 void __init __weak mem_map_via_hcall(struct boot_params *ptr __maybe_unused)
36 {
37 	xen_raw_printk("Error: Could not find memory map\n");
38 	BUG();
39 }
40 
init_pvh_bootparams(bool xen_guest)41 static void __init init_pvh_bootparams(bool xen_guest)
42 {
43 	if ((pvh_start_info.version > 0) && (pvh_start_info.memmap_entries)) {
44 		struct hvm_memmap_table_entry *ep;
45 		int i;
46 
47 		ep = __va(pvh_start_info.memmap_paddr);
48 		pvh_bootparams.e820_entries = pvh_start_info.memmap_entries;
49 
50 		for (i = 0; i < pvh_bootparams.e820_entries ; i++, ep++) {
51 			pvh_bootparams.e820_table[i].addr = ep->addr;
52 			pvh_bootparams.e820_table[i].size = ep->size;
53 			pvh_bootparams.e820_table[i].type = ep->type;
54 		}
55 	} else if (xen_guest) {
56 		mem_map_via_hcall(&pvh_bootparams);
57 	} else {
58 		/* Non-xen guests are not supported by version 0 */
59 		BUG();
60 	}
61 
62 	if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) {
63 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr =
64 			ISA_START_ADDRESS;
65 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size =
66 			ISA_END_ADDRESS - ISA_START_ADDRESS;
67 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type =
68 			E820_TYPE_RESERVED;
69 		pvh_bootparams.e820_entries++;
70 	} else
71 		xen_raw_printk("Warning: Can fit ISA range into e820\n");
72 
73 	pvh_bootparams.hdr.cmd_line_ptr =
74 		pvh_start_info.cmdline_paddr;
75 
76 	/* The first module is always ramdisk. */
77 	if (pvh_start_info.nr_modules) {
78 		struct hvm_modlist_entry *modaddr =
79 			__va(pvh_start_info.modlist_paddr);
80 		pvh_bootparams.hdr.ramdisk_image = modaddr->paddr;
81 		pvh_bootparams.hdr.ramdisk_size = modaddr->size;
82 	}
83 
84 	/*
85 	 * See Documentation/arch/x86/boot.rst.
86 	 *
87 	 * Version 2.12 supports Xen entry point but we will use default x86/PC
88 	 * environment (i.e. hardware_subarch 0).
89 	 */
90 	pvh_bootparams.hdr.version = (2 << 8) | 12;
91 	pvh_bootparams.hdr.type_of_loader = ((xen_guest ? 0x9 : 0xb) << 4) | 0;
92 
93 	pvh_bootparams.acpi_rsdp_addr = pvh_start_info.rsdp_paddr;
94 }
95 
96 /*
97  * If we are trying to boot a Xen PVH guest, it is expected that the kernel
98  * will have been configured to provide the required override for this routine.
99  */
xen_pvh_init(struct boot_params * boot_params)100 void __init __weak xen_pvh_init(struct boot_params *boot_params)
101 {
102 	xen_raw_printk("Error: Missing xen PVH initialization\n");
103 	BUG();
104 }
105 
hypervisor_specific_init(bool xen_guest)106 static void __init hypervisor_specific_init(bool xen_guest)
107 {
108 	if (xen_guest)
109 		xen_pvh_init(&pvh_bootparams);
110 }
111 
112 /*
113  * This routine (and those that it might call) should not use
114  * anything that lives in .bss since that segment will be cleared later.
115  */
xen_prepare_pvh(void)116 void __init xen_prepare_pvh(void)
117 {
118 
119 	u32 msr = xen_cpuid_base();
120 	bool xen_guest = !!msr;
121 
122 	if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) {
123 		xen_raw_printk("Error: Unexpected magic value (0x%08x)\n",
124 				pvh_start_info.magic);
125 		BUG();
126 	}
127 
128 	/*
129 	 * This must not compile to "call memset" because memset() may be
130 	 * instrumented.
131 	 */
132 	__builtin_memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
133 
134 	hypervisor_specific_init(xen_guest);
135 
136 	init_pvh_bootparams(xen_guest);
137 }
138