1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/export.h>
4 #include <linux/mm.h>
5
6 #include <xen/hvc-console.h>
7
8 #include <asm/io_apic.h>
9 #include <asm/hypervisor.h>
10 #include <asm/e820/api.h>
11
12 #include <xen/xen.h>
13 #include <asm/xen/interface.h>
14 #include <asm/xen/hypercall.h>
15
16 #include <xen/interface/memory.h>
17
18 #include "xen-ops.h"
19
20 /*
21 * PVH variables.
22 *
23 * The variable xen_pvh needs to live in a data segment since it is used
24 * after startup_{32|64} is invoked, which will clear the .bss segment.
25 */
26 bool __ro_after_init xen_pvh;
27 EXPORT_SYMBOL_GPL(xen_pvh);
28
xen_pvh_init(struct boot_params * boot_params)29 void __init xen_pvh_init(struct boot_params *boot_params)
30 {
31 xen_pvh = 1;
32 xen_domain_type = XEN_HVM_DOMAIN;
33 xen_start_flags = pvh_start_info.flags;
34
35 if (xen_initial_domain())
36 x86_init.oem.arch_setup = xen_add_preferred_consoles;
37 x86_init.oem.banner = xen_banner;
38
39 xen_efi_init(boot_params);
40
41 if (xen_initial_domain()) {
42 struct xen_platform_op op = {
43 .cmd = XENPF_get_dom0_console,
44 };
45 int ret = HYPERVISOR_platform_op(&op);
46
47 if (ret > 0)
48 xen_init_vga(&op.u.dom0_console,
49 min(ret * sizeof(char),
50 sizeof(op.u.dom0_console)),
51 &boot_params->screen_info);
52 }
53 }
54
mem_map_via_hcall(struct boot_params * boot_params_p)55 void __init mem_map_via_hcall(struct boot_params *boot_params_p)
56 {
57 struct xen_memory_map memmap;
58 int rc;
59
60 memmap.nr_entries = ARRAY_SIZE(boot_params_p->e820_table);
61 set_xen_guest_handle(memmap.buffer, boot_params_p->e820_table);
62 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
63 if (rc) {
64 xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc);
65 BUG();
66 }
67 boot_params_p->e820_entries = memmap.nr_entries;
68 }
69
70 /*
71 * Reserve e820 UNUSABLE regions to inflate the memory balloon.
72 *
73 * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
74 * located as the same place as in the native memory map, but since dom0 gets
75 * less memory than the total amount of host RAM the ranges that can't be
76 * populated are converted from RAM -> UNUSABLE. Use such regions (up to the
77 * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
78 * boot. Doing so prevents the guest (even if just temporary) from using holes
79 * in the memory map in order to map grants or foreign addresses, and
80 * hopefully limits the risk of a clash with a device MMIO region. Ideally the
81 * hypervisor should notify us which memory ranges are suitable for creating
82 * foreign mappings, but that's not yet implemented.
83 */
xen_reserve_extra_memory(struct boot_params * bootp)84 void __init xen_reserve_extra_memory(struct boot_params *bootp)
85 {
86 unsigned int i, ram_pages = 0, extra_pages;
87
88 for (i = 0; i < bootp->e820_entries; i++) {
89 struct boot_e820_entry *e = &bootp->e820_table[i];
90
91 if (e->type != E820_TYPE_RAM)
92 continue;
93 ram_pages += PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr);
94 }
95
96 /* Max amount of extra memory. */
97 extra_pages = EXTRA_MEM_RATIO * ram_pages;
98
99 /*
100 * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
101 * purposes.
102 */
103 for (i = 0; i < bootp->e820_entries && extra_pages; i++) {
104 struct boot_e820_entry *e = &bootp->e820_table[i];
105 unsigned long pages;
106
107 if (e->type != E820_TYPE_UNUSABLE)
108 continue;
109
110 pages = min(extra_pages,
111 PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr));
112
113 if (pages != (PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr))) {
114 struct boot_e820_entry *next;
115
116 if (bootp->e820_entries ==
117 ARRAY_SIZE(bootp->e820_table))
118 /* No space left to split - skip region. */
119 continue;
120
121 /* Split entry. */
122 next = e + 1;
123 memmove(next, e,
124 (bootp->e820_entries - i) * sizeof(*e));
125 bootp->e820_entries++;
126 next->addr = PAGE_ALIGN(e->addr) + PFN_PHYS(pages);
127 e->size = next->addr - e->addr;
128 next->size -= e->size;
129 }
130 e->type = E820_TYPE_RAM;
131 extra_pages -= pages;
132
133 xen_add_extra_mem(PFN_UP(e->addr), pages);
134 }
135 }
136