1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/export.h>
6 #include <xen/hvc-console.h>
8 #include <asm/bootparam.h>
9 #include <asm/io_apic.h>
10 #include <asm/hypervisor.h>
11 #include <asm/e820/api.h>
14 #include <asm/xen/interface.h>
15 #include <asm/xen/hypercall.h>
17 #include <xen/interface/memory.h>
24 * The variable xen_pvh needs to live in a data segment since it is used
25 * after startup_{32|64} is invoked, which will clear the .bss segment.
27 bool __ro_after_init xen_pvh;
28 EXPORT_SYMBOL_GPL(xen_pvh);
30 void __init xen_pvh_init(struct boot_params *boot_params)
36 xen_domain_type = XEN_HVM_DOMAIN;
37 xen_start_flags = pvh_start_info.flags;
39 msr = cpuid_ebx(xen_cpuid_base() + 2);
40 pfn = __pa(hypercall_page);
41 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
43 if (xen_initial_domain())
44 x86_init.oem.arch_setup = xen_add_preferred_consoles;
45 x86_init.oem.banner = xen_banner;
47 xen_efi_init(boot_params);
49 if (xen_initial_domain()) {
50 struct xen_platform_op op = {
51 .cmd = XENPF_get_dom0_console,
53 int ret = HYPERVISOR_platform_op(&op);
56 xen_init_vga(&op.u.dom0_console,
57 min(ret * sizeof(char),
58 sizeof(op.u.dom0_console)),
59 &boot_params->screen_info);
63 void __init mem_map_via_hcall(struct boot_params *boot_params_p)
65 struct xen_memory_map memmap;
68 memmap.nr_entries = ARRAY_SIZE(boot_params_p->e820_table);
69 set_xen_guest_handle(memmap.buffer, boot_params_p->e820_table);
70 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
72 xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc);
75 boot_params_p->e820_entries = memmap.nr_entries;
79 * Reserve e820 UNUSABLE regions to inflate the memory balloon.
81 * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
82 * located as the same place as in the native memory map, but since dom0 gets
83 * less memory than the total amount of host RAM the ranges that can't be
84 * populated are converted from RAM -> UNUSABLE. Use such regions (up to the
85 * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
86 * boot. Doing so prevents the guest (even if just temporary) from using holes
87 * in the memory map in order to map grants or foreign addresses, and
88 * hopefully limits the risk of a clash with a device MMIO region. Ideally the
89 * hypervisor should notify us which memory ranges are suitable for creating
90 * foreign mappings, but that's not yet implemented.
92 void __init xen_reserve_extra_memory(struct boot_params *bootp)
94 unsigned int i, ram_pages = 0, extra_pages;
96 for (i = 0; i < bootp->e820_entries; i++) {
97 struct boot_e820_entry *e = &bootp->e820_table[i];
99 if (e->type != E820_TYPE_RAM)
101 ram_pages += PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr);
104 /* Max amount of extra memory. */
105 extra_pages = EXTRA_MEM_RATIO * ram_pages;
108 * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
111 for (i = 0; i < bootp->e820_entries && extra_pages; i++) {
112 struct boot_e820_entry *e = &bootp->e820_table[i];
115 if (e->type != E820_TYPE_UNUSABLE)
118 pages = min(extra_pages,
119 PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr));
121 if (pages != (PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr))) {
122 struct boot_e820_entry *next;
124 if (bootp->e820_entries ==
125 ARRAY_SIZE(bootp->e820_table))
126 /* No space left to split - skip region. */
132 (bootp->e820_entries - i) * sizeof(*e));
133 bootp->e820_entries++;
134 next->addr = PAGE_ALIGN(e->addr) + PFN_PHYS(pages);
135 e->size = next->addr - e->addr;
136 next->size -= e->size;
138 e->type = E820_TYPE_RAM;
139 extra_pages -= pages;
141 xen_add_extra_mem(PFN_UP(e->addr), pages);