GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / x86 / xen / enlighten_pvh.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3
4 #include <xen/hvc-console.h>
5
6 #include <asm/io_apic.h>
7 #include <asm/hypervisor.h>
8 #include <asm/e820/api.h>
9 #include <asm/x86_init.h>
10
11 #include <asm/xen/interface.h>
12 #include <asm/xen/hypercall.h>
13
14 #include <xen/interface/memory.h>
15 #include <xen/interface/hvm/start_info.h>
16
17 #include "xen-ops.h"
18
19 /*
20  * PVH variables.
21  *
22  * xen_pvh pvh_bootparams and pvh_start_info need to live in data segment
23  * since they are used after startup_{32|64}, which clear .bss, are invoked.
24  */
25 bool xen_pvh __attribute__((section(".data"))) = 0;
26 struct boot_params pvh_bootparams __attribute__((section(".data")));
27 struct hvm_start_info pvh_start_info __attribute__((section(".data")));
28
29 unsigned int pvh_start_info_sz = sizeof(pvh_start_info);
30
31 static u64 pvh_get_root_pointer(void)
32 {
33         return pvh_start_info.rsdp_paddr;
34 }
35
36 static void __init init_pvh_bootparams(void)
37 {
38         struct xen_memory_map memmap;
39         int rc;
40
41         memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
42
43         memmap.nr_entries = ARRAY_SIZE(pvh_bootparams.e820_table);
44         set_xen_guest_handle(memmap.buffer, pvh_bootparams.e820_table);
45         rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
46         if (rc) {
47                 xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc);
48                 BUG();
49         }
50         pvh_bootparams.e820_entries = memmap.nr_entries;
51
52         if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) {
53                 pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr =
54                         ISA_START_ADDRESS;
55                 pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size =
56                         ISA_END_ADDRESS - ISA_START_ADDRESS;
57                 pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type =
58                         E820_TYPE_RESERVED;
59                 pvh_bootparams.e820_entries++;
60         } else
61                 xen_raw_printk("Warning: Can fit ISA range into e820\n");
62
63         pvh_bootparams.hdr.cmd_line_ptr =
64                 pvh_start_info.cmdline_paddr;
65
66         /* The first module is always ramdisk. */
67         if (pvh_start_info.nr_modules) {
68                 struct hvm_modlist_entry *modaddr =
69                         __va(pvh_start_info.modlist_paddr);
70                 pvh_bootparams.hdr.ramdisk_image = modaddr->paddr;
71                 pvh_bootparams.hdr.ramdisk_size = modaddr->size;
72         }
73
74         /*
75          * See Documentation/x86/boot.txt.
76          *
77          * Version 2.12 supports Xen entry point but we will use default x86/PC
78          * environment (i.e. hardware_subarch 0).
79          */
80         pvh_bootparams.hdr.version = (2 << 8) | 12;
81         pvh_bootparams.hdr.type_of_loader = (9 << 4) | 0; /* Xen loader */
82
83         x86_init.acpi.get_root_pointer = pvh_get_root_pointer;
84
85         xen_efi_init(&pvh_bootparams);
86 }
87
88 /*
89  * This routine (and those that it might call) should not use
90  * anything that lives in .bss since that segment will be cleared later.
91  */
92 void __init xen_prepare_pvh(void)
93 {
94         u32 msr;
95         u64 pfn;
96
97         if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) {
98                 xen_raw_printk("Error: Unexpected magic value (0x%08x)\n",
99                                 pvh_start_info.magic);
100                 BUG();
101         }
102
103         xen_pvh = 1;
104         xen_domain_type = XEN_HVM_DOMAIN;
105         xen_start_flags = pvh_start_info.flags;
106
107         msr = cpuid_ebx(xen_cpuid_base() + 2);
108         pfn = __pa(hypercall_page);
109         wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
110
111         init_pvh_bootparams();
112 }