Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Machine specific setup for xen |
| 3 | * |
| 4 | * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007 |
| 5 | */ |
| 6 | |
Paul Gortmaker | 7a2463d | 2016-07-13 20:18:59 -0400 | [diff] [blame] | 7 | #include <linux/init.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/pm.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 11 | #include <linux/memblock.h> |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 12 | #include <linux/cpuidle.h> |
Konrad Rzeszutek Wilk | 48cdd82 | 2012-03-13 20:06:57 -0400 | [diff] [blame] | 13 | #include <linux/cpufreq.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 14 | |
| 15 | #include <asm/elf.h> |
Roland McGrath | 6c3652e | 2008-01-30 13:30:42 +0100 | [diff] [blame] | 16 | #include <asm/vdso.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 17 | #include <asm/e820/api.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 18 | #include <asm/setup.h> |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 19 | #include <asm/acpi.h> |
Konrad Rzeszutek Wilk | 8d54db79 | 2012-08-17 10:22:37 -0400 | [diff] [blame] | 20 | #include <asm/numa.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 21 | #include <asm/xen/hypervisor.h> |
| 22 | #include <asm/xen/hypercall.h> |
| 23 | |
Ian Campbell | 45263cb | 2010-10-25 16:32:29 -0700 | [diff] [blame] | 24 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 25 | #include <xen/page.h> |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 26 | #include <xen/interface/callback.h> |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 27 | #include <xen/interface/memory.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 28 | #include <xen/interface/physdev.h> |
| 29 | #include <xen/features.h> |
Juergen Gross | 808fdb7 | 2015-07-17 06:51:30 +0200 | [diff] [blame] | 30 | #include <xen/hvc-console.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 31 | #include "xen-ops.h" |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 32 | #include "vdso.h" |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 33 | #include "mmu.h" |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 34 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 35 | #define GB(x) ((uint64_t)(x) * 1024 * 1024 * 1024) |
| 36 | |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 37 | /* Amount of extra memory space we add to the e820 ranges */ |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 38 | struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 39 | |
David Vrabel | aa24411 | 2011-09-28 17:46:32 +0100 | [diff] [blame] | 40 | /* Number of pages released from the initial allocation. */ |
| 41 | unsigned long xen_released_pages; |
| 42 | |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 43 | /* E820 map used during setting up memory. */ |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 44 | static struct e820_table xen_e820_table __initdata; |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 45 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 46 | /* |
| 47 | * Buffer used to remap identity mapped pages. We only need the virtual space. |
| 48 | * The physical page behind this address is remapped as needed to different |
| 49 | * buffer pages. |
| 50 | */ |
| 51 | #define REMAP_SIZE (P2M_PER_PAGE - 3) |
| 52 | static struct { |
| 53 | unsigned long next_area_mfn; |
| 54 | unsigned long target_pfn; |
| 55 | unsigned long size; |
| 56 | unsigned long mfns[REMAP_SIZE]; |
| 57 | } xen_remap_buf __initdata __aligned(PAGE_SIZE); |
| 58 | static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 59 | |
Jeremy Fitzhardinge | 698bb8d | 2010-09-14 10:19:14 -0700 | [diff] [blame] | 60 | /* |
| 61 | * The maximum amount of extra memory compared to the base size. The |
| 62 | * main scaling factor is the size of struct page. At extreme ratios |
| 63 | * of base:extra, all the base memory can be filled with page |
| 64 | * structures for the extra memory, leaving no space for anything |
| 65 | * else. |
| 66 | * |
| 67 | * 10x seems like a reasonable balance between scaling flexibility and |
| 68 | * leaving a practically usable system. |
| 69 | */ |
| 70 | #define EXTRA_MEM_RATIO (10) |
| 71 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 72 | static bool xen_512gb_limit __initdata = IS_ENABLED(CONFIG_XEN_512GB); |
| 73 | |
| 74 | static void __init xen_parse_512gb(void) |
| 75 | { |
| 76 | bool val = false; |
| 77 | char *arg; |
| 78 | |
| 79 | arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit"); |
| 80 | if (!arg) |
| 81 | return; |
| 82 | |
| 83 | arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit="); |
| 84 | if (!arg) |
| 85 | val = true; |
| 86 | else if (strtobool(arg + strlen("xen_512gb_limit="), &val)) |
| 87 | return; |
| 88 | |
| 89 | xen_512gb_limit = val; |
| 90 | } |
| 91 | |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 92 | static void __init xen_add_extra_mem(unsigned long start_pfn, |
| 93 | unsigned long n_pfns) |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 94 | { |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 95 | int i; |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 96 | |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 97 | /* |
| 98 | * No need to check for zero size, should happen rarely and will only |
| 99 | * write a new entry regarded to be unused due to zero size. |
| 100 | */ |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 101 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
| 102 | /* Add new region. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 103 | if (xen_extra_mem[i].n_pfns == 0) { |
| 104 | xen_extra_mem[i].start_pfn = start_pfn; |
| 105 | xen_extra_mem[i].n_pfns = n_pfns; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | /* Append to existing region. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 109 | if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns == |
| 110 | start_pfn) { |
| 111 | xen_extra_mem[i].n_pfns += n_pfns; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | } |
| 115 | if (i == XEN_EXTRA_MEM_MAX_REGIONS) |
| 116 | printk(KERN_WARNING "Warning: not enough extra memory regions\n"); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 117 | |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 118 | memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns)); |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 119 | } |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 120 | |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 121 | static void __init xen_del_extra_mem(unsigned long start_pfn, |
| 122 | unsigned long n_pfns) |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 123 | { |
| 124 | int i; |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 125 | unsigned long start_r, size_r; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 126 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 127 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 128 | start_r = xen_extra_mem[i].start_pfn; |
| 129 | size_r = xen_extra_mem[i].n_pfns; |
Konrad Rzeszutek Wilk | c96aae1 | 2012-08-17 16:43:28 -0400 | [diff] [blame] | 130 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 131 | /* Start of region. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 132 | if (start_r == start_pfn) { |
| 133 | BUG_ON(n_pfns > size_r); |
| 134 | xen_extra_mem[i].start_pfn += n_pfns; |
| 135 | xen_extra_mem[i].n_pfns -= n_pfns; |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | /* End of region. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 139 | if (start_r + size_r == start_pfn + n_pfns) { |
| 140 | BUG_ON(n_pfns > size_r); |
| 141 | xen_extra_mem[i].n_pfns -= n_pfns; |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | /* Mid of region. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 145 | if (start_pfn > start_r && start_pfn < start_r + size_r) { |
| 146 | BUG_ON(start_pfn + n_pfns > start_r + size_r); |
| 147 | xen_extra_mem[i].n_pfns = start_pfn - start_r; |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 148 | /* Calling memblock_reserve() again is okay. */ |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 149 | xen_add_extra_mem(start_pfn + n_pfns, start_r + size_r - |
| 150 | (start_pfn + n_pfns)); |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 154 | memblock_free(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns)); |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Called during boot before the p2m list can take entries beyond the |
| 159 | * hypervisor supplied p2m list. Entries in extra mem are to be regarded as |
| 160 | * invalid. |
| 161 | */ |
| 162 | unsigned long __ref xen_chk_extra_mem(unsigned long pfn) |
| 163 | { |
| 164 | int i; |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 165 | |
| 166 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 167 | if (pfn >= xen_extra_mem[i].start_pfn && |
| 168 | pfn < xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns) |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 169 | return INVALID_P2M_ENTRY; |
| 170 | } |
| 171 | |
| 172 | return IDENTITY_FRAME(pfn); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Mark all pfns of extra mem as invalid in p2m list. |
| 177 | */ |
| 178 | void __init xen_inv_extra_mem(void) |
| 179 | { |
| 180 | unsigned long pfn, pfn_s, pfn_e; |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 184 | if (!xen_extra_mem[i].n_pfns) |
Juergen Gross | 9a17ad7 | 2015-01-12 06:05:10 +0100 | [diff] [blame] | 185 | continue; |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 186 | pfn_s = xen_extra_mem[i].start_pfn; |
| 187 | pfn_e = pfn_s + xen_extra_mem[i].n_pfns; |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 188 | for (pfn = pfn_s; pfn < pfn_e; pfn++) |
| 189 | set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Konrad Rzeszutek Wilk | c96aae1 | 2012-08-17 16:43:28 -0400 | [diff] [blame] | 190 | } |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Finds the next RAM pfn available in the E820 map after min_pfn. |
| 195 | * This function updates min_pfn with the pfn found and returns |
| 196 | * the size of that range or zero if not found. |
| 197 | */ |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 198 | static unsigned long __init xen_find_pfn_range(unsigned long *min_pfn) |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 199 | { |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 200 | const struct e820_entry *entry = xen_e820_table.entries; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 201 | unsigned int i; |
| 202 | unsigned long done = 0; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 203 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 204 | for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) { |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 205 | unsigned long s_pfn; |
| 206 | unsigned long e_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 207 | |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 208 | if (entry->type != E820_TYPE_RAM) |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 209 | continue; |
| 210 | |
zhenzhong.duan | c3d93f8 | 2012-07-18 13:06:39 +0800 | [diff] [blame] | 211 | e_pfn = PFN_DOWN(entry->addr + entry->size); |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 212 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 213 | /* We only care about E820 after this */ |
Zhenzhong Duan | abed7d0 | 2015-10-27 15:19:52 -0400 | [diff] [blame] | 214 | if (e_pfn <= *min_pfn) |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 215 | continue; |
| 216 | |
zhenzhong.duan | c3d93f8 | 2012-07-18 13:06:39 +0800 | [diff] [blame] | 217 | s_pfn = PFN_UP(entry->addr); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 218 | |
| 219 | /* If min_pfn falls within the E820 entry, we want to start |
| 220 | * at the min_pfn PFN. |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 221 | */ |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 222 | if (s_pfn <= *min_pfn) { |
| 223 | done = e_pfn - *min_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 224 | } else { |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 225 | done = e_pfn - s_pfn; |
| 226 | *min_pfn = s_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 227 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 228 | break; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 229 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 230 | |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 231 | return done; |
| 232 | } |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 233 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 234 | static int __init xen_free_mfn(unsigned long mfn) |
| 235 | { |
| 236 | struct xen_memory_reservation reservation = { |
| 237 | .address_bits = 0, |
| 238 | .extent_order = 0, |
| 239 | .domid = DOMID_SELF |
| 240 | }; |
| 241 | |
| 242 | set_xen_guest_handle(reservation.extent_start, &mfn); |
| 243 | reservation.nr_extents = 1; |
| 244 | |
| 245 | return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); |
| 246 | } |
| 247 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 248 | /* |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 249 | * This releases a chunk of memory and then does the identity map. It's used |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 250 | * as a fallback if the remapping fails. |
| 251 | */ |
| 252 | static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn, |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 253 | unsigned long end_pfn, unsigned long nr_pages) |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 254 | { |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 255 | unsigned long pfn, end; |
| 256 | int ret; |
| 257 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 258 | WARN_ON(start_pfn > end_pfn); |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 259 | |
David Vrabel | bc7142c | 2015-01-07 11:01:08 +0000 | [diff] [blame] | 260 | /* Release pages first. */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 261 | end = min(end_pfn, nr_pages); |
| 262 | for (pfn = start_pfn; pfn < end; pfn++) { |
| 263 | unsigned long mfn = pfn_to_mfn(pfn); |
| 264 | |
| 265 | /* Make sure pfn exists to start with */ |
| 266 | if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn) |
| 267 | continue; |
| 268 | |
| 269 | ret = xen_free_mfn(mfn); |
| 270 | WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret); |
| 271 | |
| 272 | if (ret == 1) { |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 273 | xen_released_pages++; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 274 | if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY)) |
| 275 | break; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 276 | } else |
| 277 | break; |
| 278 | } |
| 279 | |
David Vrabel | bc7142c | 2015-01-07 11:01:08 +0000 | [diff] [blame] | 280 | set_phys_range_identity(start_pfn, end_pfn); |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 283 | /* |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 284 | * Helper function to update the p2m and m2p tables and kernel mapping. |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 285 | */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 286 | static void __init xen_update_mem_tables(unsigned long pfn, unsigned long mfn) |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 287 | { |
| 288 | struct mmu_update update = { |
Juergen Gross | 3ba5c86 | 2015-01-28 07:44:22 +0100 | [diff] [blame] | 289 | .ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE, |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 290 | .val = pfn |
| 291 | }; |
| 292 | |
| 293 | /* Update p2m */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 294 | if (!set_phys_to_machine(pfn, mfn)) { |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 295 | WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n", |
| 296 | pfn, mfn); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 297 | BUG(); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* Update m2p */ |
| 301 | if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) { |
| 302 | WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n", |
| 303 | mfn, pfn); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 304 | BUG(); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 307 | /* Update kernel mapping, but not for highmem. */ |
Juergen Gross | e86f949 | 2015-01-12 06:05:09 +0100 | [diff] [blame] | 308 | if (pfn >= PFN_UP(__pa(high_memory - 1))) |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 309 | return; |
| 310 | |
| 311 | if (HYPERVISOR_update_va_mapping((unsigned long)__va(pfn << PAGE_SHIFT), |
| 312 | mfn_pte(mfn, PAGE_KERNEL), 0)) { |
| 313 | WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n", |
| 314 | mfn, pfn); |
| 315 | BUG(); |
| 316 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | * This function updates the p2m and m2p tables with an identity map from |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 321 | * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the |
| 322 | * original allocation at remap_pfn. The information needed for remapping is |
| 323 | * saved in the memory itself to avoid the need for allocating buffers. The |
| 324 | * complete remap information is contained in a list of MFNs each containing |
| 325 | * up to REMAP_SIZE MFNs and the start target PFN for doing the remap. |
| 326 | * This enables us to preserve the original mfn sequence while doing the |
| 327 | * remapping at a time when the memory management is capable of allocating |
| 328 | * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and |
| 329 | * its callers. |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 330 | */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 331 | static void __init xen_do_set_identity_and_remap_chunk( |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 332 | unsigned long start_pfn, unsigned long size, unsigned long remap_pfn) |
| 333 | { |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 334 | unsigned long buf = (unsigned long)&xen_remap_buf; |
| 335 | unsigned long mfn_save, mfn; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 336 | unsigned long ident_pfn_iter, remap_pfn_iter; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 337 | unsigned long ident_end_pfn = start_pfn + size; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 338 | unsigned long left = size; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 339 | unsigned int i, chunk; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 340 | |
| 341 | WARN_ON(size == 0); |
| 342 | |
| 343 | BUG_ON(xen_feature(XENFEAT_auto_translated_physmap)); |
| 344 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 345 | mfn_save = virt_to_mfn(buf); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 346 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 347 | for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn; |
| 348 | ident_pfn_iter < ident_end_pfn; |
| 349 | ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) { |
| 350 | chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 351 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 352 | /* Map first pfn to xen_remap_buf */ |
| 353 | mfn = pfn_to_mfn(ident_pfn_iter); |
| 354 | set_pte_mfn(buf, mfn, PAGE_KERNEL); |
| 355 | |
| 356 | /* Save mapping information in page */ |
| 357 | xen_remap_buf.next_area_mfn = xen_remap_mfn; |
| 358 | xen_remap_buf.target_pfn = remap_pfn_iter; |
| 359 | xen_remap_buf.size = chunk; |
| 360 | for (i = 0; i < chunk; i++) |
| 361 | xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i); |
| 362 | |
| 363 | /* Put remap buf into list. */ |
| 364 | xen_remap_mfn = mfn; |
| 365 | |
| 366 | /* Set identity map */ |
David Vrabel | bc7142c | 2015-01-07 11:01:08 +0000 | [diff] [blame] | 367 | set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 368 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 369 | left -= chunk; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 372 | /* Restore old xen_remap_buf mapping */ |
| 373 | set_pte_mfn(buf, mfn_save, PAGE_KERNEL); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
| 377 | * This function takes a contiguous pfn range that needs to be identity mapped |
| 378 | * and: |
| 379 | * |
| 380 | * 1) Finds a new range of pfns to use to remap based on E820 and remap_pfn. |
| 381 | * 2) Calls the do_ function to actually do the mapping/remapping work. |
| 382 | * |
| 383 | * The goal is to not allocate additional memory but to remap the existing |
| 384 | * pages. In the case of an error the underlying memory is simply released back |
| 385 | * to Xen and not remapped. |
| 386 | */ |
Juergen Gross | 76f0a48 | 2014-12-08 06:32:19 +0100 | [diff] [blame] | 387 | static unsigned long __init xen_set_identity_and_remap_chunk( |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 388 | unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages, |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 389 | unsigned long remap_pfn) |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 390 | { |
| 391 | unsigned long pfn; |
| 392 | unsigned long i = 0; |
| 393 | unsigned long n = end_pfn - start_pfn; |
| 394 | |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 395 | if (remap_pfn == 0) |
| 396 | remap_pfn = nr_pages; |
| 397 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 398 | while (i < n) { |
| 399 | unsigned long cur_pfn = start_pfn + i; |
| 400 | unsigned long left = n - i; |
| 401 | unsigned long size = left; |
| 402 | unsigned long remap_range_size; |
| 403 | |
| 404 | /* Do not remap pages beyond the current allocation */ |
| 405 | if (cur_pfn >= nr_pages) { |
| 406 | /* Identity map remaining pages */ |
David Vrabel | bc7142c | 2015-01-07 11:01:08 +0000 | [diff] [blame] | 407 | set_phys_range_identity(cur_pfn, cur_pfn + size); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | if (cur_pfn + size > nr_pages) |
| 411 | size = nr_pages - cur_pfn; |
| 412 | |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 413 | remap_range_size = xen_find_pfn_range(&remap_pfn); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 414 | if (!remap_range_size) { |
| 415 | pr_warning("Unable to find available pfn range, not remapping identity pages\n"); |
| 416 | xen_set_identity_and_release_chunk(cur_pfn, |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 417 | cur_pfn + left, nr_pages); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | /* Adjust size to fit in current e820 RAM region */ |
| 421 | if (size > remap_range_size) |
| 422 | size = remap_range_size; |
| 423 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 424 | xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 425 | |
| 426 | /* Update variables to reflect new mappings. */ |
| 427 | i += size; |
| 428 | remap_pfn += size; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
| 432 | * If the PFNs are currently mapped, the VA mapping also needs |
| 433 | * to be updated to be 1:1. |
| 434 | */ |
| 435 | for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++) |
| 436 | (void)HYPERVISOR_update_va_mapping( |
| 437 | (unsigned long)__va(pfn << PAGE_SHIFT), |
| 438 | mfn_pte(pfn, PAGE_KERNEL_IO), 0); |
| 439 | |
| 440 | return remap_pfn; |
| 441 | } |
| 442 | |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 443 | static unsigned long __init xen_count_remap_pages( |
| 444 | unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages, |
| 445 | unsigned long remap_pages) |
| 446 | { |
| 447 | if (start_pfn >= nr_pages) |
| 448 | return remap_pages; |
| 449 | |
| 450 | return remap_pages + min(end_pfn, nr_pages) - start_pfn; |
| 451 | } |
| 452 | |
| 453 | static unsigned long __init xen_foreach_remap_area(unsigned long nr_pages, |
| 454 | unsigned long (*func)(unsigned long start_pfn, unsigned long end_pfn, |
| 455 | unsigned long nr_pages, unsigned long last_val)) |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 456 | { |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 457 | phys_addr_t start = 0; |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 458 | unsigned long ret_val = 0; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 459 | const struct e820_entry *entry = xen_e820_table.entries; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 460 | int i; |
| 461 | |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 462 | /* |
| 463 | * Combine non-RAM regions and gaps until a RAM region (or the |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 464 | * end of the map) is reached, then call the provided function |
| 465 | * to perform its duty on the non-RAM region. |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 466 | * |
| 467 | * The combined non-RAM regions are rounded to a whole number |
| 468 | * of pages so any partial pages are accessible via the 1:1 |
| 469 | * mapping. This is needed for some BIOSes that put (for |
| 470 | * example) the DMI tables in a reserved region that begins on |
| 471 | * a non-page boundary. |
| 472 | */ |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 473 | for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) { |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 474 | phys_addr_t end = entry->addr + entry->size; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 475 | if (entry->type == E820_TYPE_RAM || i == xen_e820_table.nr_entries - 1) { |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 476 | unsigned long start_pfn = PFN_DOWN(start); |
| 477 | unsigned long end_pfn = PFN_UP(end); |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 478 | |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 479 | if (entry->type == E820_TYPE_RAM) |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 480 | end_pfn = PFN_UP(entry->addr); |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 481 | |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 482 | if (start_pfn < end_pfn) |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 483 | ret_val = func(start_pfn, end_pfn, nr_pages, |
| 484 | ret_val); |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 485 | start = end; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 486 | } |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 487 | } |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 488 | |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 489 | return ret_val; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 490 | } |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * Remap the memory prepared in xen_do_set_identity_and_remap_chunk(). |
| 494 | * The remap information (which mfn remap to which pfn) is contained in the |
| 495 | * to be remapped memory itself in a linked list anchored at xen_remap_mfn. |
| 496 | * This scheme allows to remap the different chunks in arbitrary order while |
| 497 | * the resulting mapping will be independant from the order. |
| 498 | */ |
| 499 | void __init xen_remap_memory(void) |
| 500 | { |
| 501 | unsigned long buf = (unsigned long)&xen_remap_buf; |
| 502 | unsigned long mfn_save, mfn, pfn; |
| 503 | unsigned long remapped = 0; |
| 504 | unsigned int i; |
| 505 | unsigned long pfn_s = ~0UL; |
| 506 | unsigned long len = 0; |
| 507 | |
| 508 | mfn_save = virt_to_mfn(buf); |
| 509 | |
| 510 | while (xen_remap_mfn != INVALID_P2M_ENTRY) { |
| 511 | /* Map the remap information */ |
| 512 | set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL); |
| 513 | |
| 514 | BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]); |
| 515 | |
| 516 | pfn = xen_remap_buf.target_pfn; |
| 517 | for (i = 0; i < xen_remap_buf.size; i++) { |
| 518 | mfn = xen_remap_buf.mfns[i]; |
| 519 | xen_update_mem_tables(pfn, mfn); |
| 520 | remapped++; |
| 521 | pfn++; |
| 522 | } |
| 523 | if (pfn_s == ~0UL || pfn == pfn_s) { |
| 524 | pfn_s = xen_remap_buf.target_pfn; |
| 525 | len += xen_remap_buf.size; |
| 526 | } else if (pfn_s + len == xen_remap_buf.target_pfn) { |
| 527 | len += xen_remap_buf.size; |
| 528 | } else { |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 529 | xen_del_extra_mem(pfn_s, len); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 530 | pfn_s = xen_remap_buf.target_pfn; |
| 531 | len = xen_remap_buf.size; |
| 532 | } |
| 533 | |
| 534 | mfn = xen_remap_mfn; |
| 535 | xen_remap_mfn = xen_remap_buf.next_area_mfn; |
| 536 | } |
| 537 | |
| 538 | if (pfn_s != ~0UL && len) |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 539 | xen_del_extra_mem(pfn_s, len); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 540 | |
| 541 | set_pte_mfn(buf, mfn_save, PAGE_KERNEL); |
| 542 | |
| 543 | pr_info("Remapped %ld page(s)\n", remapped); |
| 544 | } |
| 545 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 546 | static unsigned long __init xen_get_pages_limit(void) |
| 547 | { |
| 548 | unsigned long limit; |
| 549 | |
| 550 | #ifdef CONFIG_X86_32 |
| 551 | limit = GB(64) / PAGE_SIZE; |
| 552 | #else |
Juergen Gross | cb9e444 | 2015-09-04 14:18:08 +0200 | [diff] [blame] | 553 | limit = MAXMEM / PAGE_SIZE; |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 554 | if (!xen_initial_domain() && xen_512gb_limit) |
| 555 | limit = GB(512) / PAGE_SIZE; |
| 556 | #endif |
| 557 | return limit; |
| 558 | } |
| 559 | |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 560 | static unsigned long __init xen_get_max_pages(void) |
| 561 | { |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 562 | unsigned long max_pages, limit; |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 563 | domid_t domid = DOMID_SELF; |
Juergen Gross | 24f775a | 2015-09-04 14:50:33 +0200 | [diff] [blame] | 564 | long ret; |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 565 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 566 | limit = xen_get_pages_limit(); |
| 567 | max_pages = limit; |
| 568 | |
Ian Campbell | d3db728 | 2011-12-14 12:16:08 +0000 | [diff] [blame] | 569 | /* |
| 570 | * For the initial domain we use the maximum reservation as |
| 571 | * the maximum page. |
| 572 | * |
| 573 | * For guest domains the current maximum reservation reflects |
| 574 | * the current maximum rather than the static maximum. In this |
| 575 | * case the e820 map provided to us will cover the static |
| 576 | * maximum region. |
| 577 | */ |
| 578 | if (xen_initial_domain()) { |
| 579 | ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid); |
| 580 | if (ret > 0) |
| 581 | max_pages = ret; |
| 582 | } |
| 583 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 584 | return min(max_pages, limit); |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Juergen Gross | a3f5239 | 2015-01-28 07:44:23 +0100 | [diff] [blame] | 587 | static void __init xen_align_and_add_e820_region(phys_addr_t start, |
| 588 | phys_addr_t size, int type) |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 589 | { |
Juergen Gross | 3ba5c86 | 2015-01-28 07:44:22 +0100 | [diff] [blame] | 590 | phys_addr_t end = start + size; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 591 | |
| 592 | /* Align RAM regions to page boundaries. */ |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 593 | if (type == E820_TYPE_RAM) { |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 594 | start = PAGE_ALIGN(start); |
Juergen Gross | 3ba5c86 | 2015-01-28 07:44:22 +0100 | [diff] [blame] | 595 | end &= ~((phys_addr_t)PAGE_SIZE - 1); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Ingo Molnar | ab6bc04 | 2017-01-28 14:19:36 +0100 | [diff] [blame] | 598 | e820__range_add(start, end - start, type); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 601 | static void __init xen_ignore_unusable(void) |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 602 | { |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 603 | struct e820_entry *entry = xen_e820_table.entries; |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 604 | unsigned int i; |
| 605 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 606 | for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) { |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 607 | if (entry->type == E820_TYPE_UNUSABLE) |
| 608 | entry->type = E820_TYPE_RAM; |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Juergen Gross | e612b4a | 2015-07-17 06:51:28 +0200 | [diff] [blame] | 612 | bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size) |
| 613 | { |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 614 | struct e820_entry *entry; |
Juergen Gross | e612b4a | 2015-07-17 06:51:28 +0200 | [diff] [blame] | 615 | unsigned mapcnt; |
| 616 | phys_addr_t end; |
| 617 | |
| 618 | if (!size) |
| 619 | return false; |
| 620 | |
| 621 | end = start + size; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 622 | entry = xen_e820_table.entries; |
Juergen Gross | e612b4a | 2015-07-17 06:51:28 +0200 | [diff] [blame] | 623 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 624 | for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) { |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 625 | if (entry->type == E820_TYPE_RAM && entry->addr <= start && |
Juergen Gross | e612b4a | 2015-07-17 06:51:28 +0200 | [diff] [blame] | 626 | (entry->addr + entry->size) >= end) |
| 627 | return false; |
| 628 | |
| 629 | entry++; |
| 630 | } |
| 631 | |
| 632 | return true; |
| 633 | } |
| 634 | |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 635 | /* |
Juergen Gross | 9ddac5b | 2015-07-17 06:51:29 +0200 | [diff] [blame] | 636 | * Find a free area in physical memory not yet reserved and compliant with |
| 637 | * E820 map. |
| 638 | * Used to relocate pre-allocated areas like initrd or p2m list which are in |
| 639 | * conflict with the to be used E820 map. |
| 640 | * In case no area is found, return 0. Otherwise return the physical address |
| 641 | * of the area which is already reserved for convenience. |
| 642 | */ |
| 643 | phys_addr_t __init xen_find_free_area(phys_addr_t size) |
| 644 | { |
| 645 | unsigned mapcnt; |
| 646 | phys_addr_t addr, start; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 647 | struct e820_entry *entry = xen_e820_table.entries; |
Juergen Gross | 9ddac5b | 2015-07-17 06:51:29 +0200 | [diff] [blame] | 648 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 649 | for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++, entry++) { |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 650 | if (entry->type != E820_TYPE_RAM || entry->size < size) |
Juergen Gross | 9ddac5b | 2015-07-17 06:51:29 +0200 | [diff] [blame] | 651 | continue; |
| 652 | start = entry->addr; |
| 653 | for (addr = start; addr < start + size; addr += PAGE_SIZE) { |
| 654 | if (!memblock_is_reserved(addr)) |
| 655 | continue; |
| 656 | start = addr + PAGE_SIZE; |
| 657 | if (start + size > entry->addr + entry->size) |
| 658 | break; |
| 659 | } |
| 660 | if (addr >= start + size) { |
| 661 | memblock_reserve(start, size); |
| 662 | return start; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /* |
Juergen Gross | 4b9c153 | 2015-07-17 06:51:32 +0200 | [diff] [blame] | 670 | * Like memcpy, but with physical addresses for dest and src. |
| 671 | */ |
| 672 | static void __init xen_phys_memcpy(phys_addr_t dest, phys_addr_t src, |
| 673 | phys_addr_t n) |
| 674 | { |
| 675 | phys_addr_t dest_off, src_off, dest_len, src_len, len; |
| 676 | void *from, *to; |
| 677 | |
| 678 | while (n) { |
| 679 | dest_off = dest & ~PAGE_MASK; |
| 680 | src_off = src & ~PAGE_MASK; |
| 681 | dest_len = n; |
| 682 | if (dest_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off) |
| 683 | dest_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off; |
| 684 | src_len = n; |
| 685 | if (src_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off) |
| 686 | src_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off; |
| 687 | len = min(dest_len, src_len); |
| 688 | to = early_memremap(dest - dest_off, dest_len + dest_off); |
| 689 | from = early_memremap(src - src_off, src_len + src_off); |
| 690 | memcpy(to, from, len); |
| 691 | early_memunmap(to, dest_len + dest_off); |
| 692 | early_memunmap(from, src_len + src_off); |
| 693 | n -= len; |
| 694 | dest += len; |
| 695 | src += len; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 700 | * Reserve Xen mfn_list. |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 701 | */ |
| 702 | static void __init xen_reserve_xen_mfnlist(void) |
| 703 | { |
Juergen Gross | 70e6119 | 2015-07-17 06:51:35 +0200 | [diff] [blame] | 704 | phys_addr_t start, size; |
| 705 | |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 706 | if (xen_start_info->mfn_list >= __START_KERNEL_map) { |
Juergen Gross | 70e6119 | 2015-07-17 06:51:35 +0200 | [diff] [blame] | 707 | start = __pa(xen_start_info->mfn_list); |
| 708 | size = PFN_ALIGN(xen_start_info->nr_pages * |
| 709 | sizeof(unsigned long)); |
| 710 | } else { |
| 711 | start = PFN_PHYS(xen_start_info->first_p2m_pfn); |
| 712 | size = PFN_PHYS(xen_start_info->nr_p2m_frames); |
| 713 | } |
| 714 | |
Ross Lagerwall | 7ecec85 | 2016-12-12 14:35:13 +0000 | [diff] [blame] | 715 | memblock_reserve(start, size); |
| 716 | if (!xen_is_e820_reserved(start, size)) |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 717 | return; |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 718 | |
Juergen Gross | 70e6119 | 2015-07-17 06:51:35 +0200 | [diff] [blame] | 719 | #ifdef CONFIG_X86_32 |
| 720 | /* |
| 721 | * Relocating the p2m on 32 bit system to an arbitrary virtual address |
| 722 | * is not supported, so just give up. |
| 723 | */ |
| 724 | xen_raw_console_write("Xen hypervisor allocated p2m list conflicts with E820 map\n"); |
| 725 | BUG(); |
| 726 | #else |
| 727 | xen_relocate_p2m(); |
Ross Lagerwall | 7ecec85 | 2016-12-12 14:35:13 +0000 | [diff] [blame] | 728 | memblock_free(start, size); |
Juergen Gross | 70e6119 | 2015-07-17 06:51:35 +0200 | [diff] [blame] | 729 | #endif |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 732 | /** |
| 733 | * machine_specific_memory_setup - Hook for machine specific memory setup. |
| 734 | **/ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 735 | char * __init xen_memory_setup(void) |
| 736 | { |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 737 | unsigned long max_pfn, pfn_s, n_pfns; |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 738 | phys_addr_t mem_end, addr, size, chunk_size; |
| 739 | u32 type; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 740 | int rc; |
| 741 | struct xen_memory_map memmap; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 742 | unsigned long max_pages; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 743 | unsigned long extra_pages = 0; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 744 | int i; |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 745 | int op; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 746 | |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 747 | xen_parse_512gb(); |
| 748 | max_pfn = xen_get_pages_limit(); |
| 749 | max_pfn = min(max_pfn, xen_start_info->nr_pages); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 750 | mem_end = PFN_PHYS(max_pfn); |
| 751 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 752 | memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries); |
| 753 | set_xen_guest_handle(memmap.buffer, xen_e820_table.entries); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 754 | |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 755 | op = xen_initial_domain() ? |
| 756 | XENMEM_machine_memory_map : |
| 757 | XENMEM_memory_map; |
| 758 | rc = HYPERVISOR_memory_op(op, &memmap); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 759 | if (rc == -ENOSYS) { |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 760 | BUG_ON(xen_initial_domain()); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 761 | memmap.nr_entries = 1; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 762 | xen_e820_table.entries[0].addr = 0ULL; |
| 763 | xen_e820_table.entries[0].size = mem_end; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 764 | /* 8MB slack (to balance backend allocations). */ |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 765 | xen_e820_table.entries[0].size += 8ULL << 20; |
| 766 | xen_e820_table.entries[0].type = E820_TYPE_RAM; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 767 | rc = 0; |
| 768 | } |
| 769 | BUG_ON(rc); |
Martin Kelly | 1ea644c | 2014-10-16 20:48:11 -0700 | [diff] [blame] | 770 | BUG_ON(memmap.nr_entries == 0); |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 771 | xen_e820_table.nr_entries = memmap.nr_entries; |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 772 | |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 773 | /* |
| 774 | * Xen won't allow a 1:1 mapping to be created to UNUSABLE |
| 775 | * regions, so if we're using the machine memory map leave the |
| 776 | * region as RAM as it is in the pseudo-physical map. |
| 777 | * |
| 778 | * UNUSABLE regions in domUs are not handled and will need |
| 779 | * a patch in the future. |
| 780 | */ |
| 781 | if (xen_initial_domain()) |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 782 | xen_ignore_unusable(); |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 783 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 784 | /* Make sure the Xen-supplied memory map is well-ordered. */ |
Ingo Molnar | f9748fa | 2017-01-28 18:00:35 +0100 | [diff] [blame] | 785 | e820__update_table(&xen_e820_table); |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 786 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 787 | max_pages = xen_get_max_pages(); |
Stefano Stabellini | 7cb31b7 | 2011-01-27 10:13:25 -0500 | [diff] [blame] | 788 | |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 789 | /* How many extra pages do we need due to remapping? */ |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 790 | max_pages += xen_foreach_remap_area(max_pfn, xen_count_remap_pages); |
Juergen Gross | eafd72e | 2015-08-19 18:52:34 +0200 | [diff] [blame] | 791 | |
| 792 | if (max_pages > max_pfn) |
| 793 | extra_pages += max_pages - max_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 794 | |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 795 | /* |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 796 | * Clamp the amount of extra memory to a EXTRA_MEM_RATIO |
| 797 | * factor the base size. On non-highmem systems, the base |
| 798 | * size is the full initial memory allocation; on highmem it |
| 799 | * is limited to the max size of lowmem, so that it doesn't |
| 800 | * get completely filled. |
| 801 | * |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 802 | * Make sure we have no memory above max_pages, as this area |
| 803 | * isn't handled by the p2m management. |
| 804 | * |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 805 | * In principle there could be a problem in lowmem systems if |
| 806 | * the initial memory is also very large with respect to |
| 807 | * lowmem, but we won't try to deal with that here. |
| 808 | */ |
Juergen Gross | c70727a | 2015-07-17 06:51:36 +0200 | [diff] [blame] | 809 | extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)), |
| 810 | extra_pages, max_pages - max_pfn); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 811 | i = 0; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 812 | addr = xen_e820_table.entries[0].addr; |
| 813 | size = xen_e820_table.entries[0].size; |
| 814 | while (i < xen_e820_table.nr_entries) { |
David Vrabel | f5775e0 | 2015-01-19 11:08:05 +0000 | [diff] [blame] | 815 | bool discard = false; |
| 816 | |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 817 | chunk_size = size; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 818 | type = xen_e820_table.entries[i].type; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 819 | |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 820 | if (type == E820_TYPE_RAM) { |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 821 | if (addr < mem_end) { |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 822 | chunk_size = min(size, mem_end - addr); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 823 | } else if (extra_pages) { |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 824 | chunk_size = min(size, PFN_PHYS(extra_pages)); |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 825 | pfn_s = PFN_UP(addr); |
| 826 | n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s; |
| 827 | extra_pages -= n_pfns; |
| 828 | xen_add_extra_mem(pfn_s, n_pfns); |
| 829 | xen_max_p2m_pfn = pfn_s + n_pfns; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 830 | } else |
David Vrabel | f5775e0 | 2015-01-19 11:08:05 +0000 | [diff] [blame] | 831 | discard = true; |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 832 | } |
| 833 | |
David Vrabel | f5775e0 | 2015-01-19 11:08:05 +0000 | [diff] [blame] | 834 | if (!discard) |
| 835 | xen_align_and_add_e820_region(addr, chunk_size, type); |
Jeremy Fitzhardinge | b5b43ce | 2010-09-02 17:10:12 -0700 | [diff] [blame] | 836 | |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 837 | addr += chunk_size; |
| 838 | size -= chunk_size; |
| 839 | if (size == 0) { |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 840 | i++; |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 841 | if (i < xen_e820_table.nr_entries) { |
| 842 | addr = xen_e820_table.entries[i].addr; |
| 843 | size = xen_e820_table.entries[i].size; |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 844 | } |
| 845 | } |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 846 | } |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 847 | |
| 848 | /* |
David Vrabel | 25b884a | 2014-01-03 15:46:10 +0000 | [diff] [blame] | 849 | * Set the rest as identity mapped, in case PCI BARs are |
| 850 | * located here. |
David Vrabel | 25b884a | 2014-01-03 15:46:10 +0000 | [diff] [blame] | 851 | */ |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 852 | set_phys_range_identity(addr / PAGE_SIZE, ~0ul); |
David Vrabel | 25b884a | 2014-01-03 15:46:10 +0000 | [diff] [blame] | 853 | |
| 854 | /* |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 855 | * In domU, the ISA region is normal, usable memory, but we |
| 856 | * reserve ISA memory anyway because too many things poke |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 857 | * about in there. |
| 858 | */ |
Ingo Molnar | f9748fa | 2017-01-28 18:00:35 +0100 | [diff] [blame] | 859 | e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 860 | |
Ingo Molnar | f9748fa | 2017-01-28 18:00:35 +0100 | [diff] [blame] | 861 | e820__update_table(e820_table); |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 862 | |
Juergen Gross | 808fdb7 | 2015-07-17 06:51:30 +0200 | [diff] [blame] | 863 | /* |
| 864 | * Check whether the kernel itself conflicts with the target E820 map. |
| 865 | * Failing now is better than running into weird problems later due |
| 866 | * to relocating (and even reusing) pages with kernel text or data. |
| 867 | */ |
| 868 | if (xen_is_e820_reserved(__pa_symbol(_text), |
| 869 | __pa_symbol(__bss_stop) - __pa_symbol(_text))) { |
| 870 | xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n"); |
| 871 | BUG(); |
| 872 | } |
| 873 | |
Juergen Gross | 04414ba | 2015-07-17 06:51:31 +0200 | [diff] [blame] | 874 | /* |
| 875 | * Check for a conflict of the hypervisor supplied page tables with |
| 876 | * the target E820 map. |
| 877 | */ |
| 878 | xen_pt_check_e820(); |
| 879 | |
Juergen Gross | 8f5b0c6 | 2015-07-17 06:51:25 +0200 | [diff] [blame] | 880 | xen_reserve_xen_mfnlist(); |
| 881 | |
Juergen Gross | 4b9c153 | 2015-07-17 06:51:32 +0200 | [diff] [blame] | 882 | /* Check for a conflict of the initrd with the target E820 map. */ |
| 883 | if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image, |
| 884 | boot_params.hdr.ramdisk_size)) { |
| 885 | phys_addr_t new_area, start, size; |
| 886 | |
| 887 | new_area = xen_find_free_area(boot_params.hdr.ramdisk_size); |
| 888 | if (!new_area) { |
| 889 | xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n"); |
| 890 | BUG(); |
| 891 | } |
| 892 | |
| 893 | start = boot_params.hdr.ramdisk_image; |
| 894 | size = boot_params.hdr.ramdisk_size; |
| 895 | xen_phys_memcpy(new_area, start, size); |
| 896 | pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n", |
| 897 | start, start + size, new_area, new_area + size); |
| 898 | memblock_free(start, size); |
| 899 | boot_params.hdr.ramdisk_image = new_area; |
| 900 | boot_params.ext_ramdisk_image = new_area >> 32; |
| 901 | } |
| 902 | |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 903 | /* |
| 904 | * Set identity map on non-RAM pages and prepare remapping the |
| 905 | * underlying RAM. |
| 906 | */ |
Juergen Gross | dd14be9 | 2016-05-18 16:44:54 +0200 | [diff] [blame] | 907 | xen_foreach_remap_area(max_pfn, xen_set_identity_and_remap_chunk); |
| 908 | |
| 909 | pr_info("Released %ld page(s)\n", xen_released_pages); |
Juergen Gross | 5097cdf | 2015-07-17 06:51:27 +0200 | [diff] [blame] | 910 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 911 | return "Xen"; |
| 912 | } |
| 913 | |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 914 | /* |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 915 | * Machine specific memory setup for auto-translated guests. |
| 916 | */ |
| 917 | char * __init xen_auto_xlated_memory_setup(void) |
| 918 | { |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 919 | struct xen_memory_map memmap; |
| 920 | int i; |
| 921 | int rc; |
| 922 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 923 | memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries); |
| 924 | set_xen_guest_handle(memmap.buffer, xen_e820_table.entries); |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 925 | |
| 926 | rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); |
| 927 | if (rc < 0) |
| 928 | panic("No memory map (%d)\n", rc); |
| 929 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 930 | xen_e820_table.nr_entries = memmap.nr_entries; |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 931 | |
Ingo Molnar | f9748fa | 2017-01-28 18:00:35 +0100 | [diff] [blame] | 932 | e820__update_table(&xen_e820_table); |
Juergen Gross | 69632ec | 2015-07-17 06:51:26 +0200 | [diff] [blame] | 933 | |
Ingo Molnar | e7dbf7a | 2017-01-28 18:19:01 +0100 | [diff] [blame] | 934 | for (i = 0; i < xen_e820_table.nr_entries; i++) |
| 935 | e820__range_add(xen_e820_table.entries[i].addr, xen_e820_table.entries[i].size, xen_e820_table.entries[i].type); |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 936 | |
Juergen Gross | 70e6119 | 2015-07-17 06:51:35 +0200 | [diff] [blame] | 937 | /* Remove p2m info, it is not needed. */ |
| 938 | xen_start_info->mfn_list = 0; |
| 939 | xen_start_info->first_p2m_pfn = 0; |
| 940 | xen_start_info->nr_p2m_frames = 0; |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 941 | |
| 942 | return "Xen"; |
| 943 | } |
| 944 | |
| 945 | /* |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 946 | * Set the bit indicating "nosegneg" library variants should be used. |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 947 | * We only need to bother in pure 32-bit mode; compat 32-bit processes |
| 948 | * can have un-truncated segments, so wrapping around is allowed. |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 949 | */ |
Sam Ravnborg | 08b6d29 | 2008-01-30 13:33:25 +0100 | [diff] [blame] | 950 | static void __init fiddle_vdso(void) |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 951 | { |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 952 | #ifdef CONFIG_X86_32 |
Andy Lutomirski | 0a6d1fa | 2015-10-05 17:47:56 -0700 | [diff] [blame] | 953 | u32 *mask = vdso_image_32.data + |
| 954 | vdso_image_32.sym_VDSO32_NOTE_MASK; |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 955 | *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 956 | #endif |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 959 | static int register_callback(unsigned type, const void *func) |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 960 | { |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 961 | struct callback_register callback = { |
| 962 | .type = type, |
| 963 | .address = XEN_CALLBACK(__KERNEL_CS, func), |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 964 | .flags = CALLBACKF_mask_events, |
| 965 | }; |
| 966 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 967 | return HYPERVISOR_callback_op(CALLBACKOP_register, &callback); |
| 968 | } |
| 969 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 970 | void xen_enable_sysenter(void) |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 971 | { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 972 | int ret; |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 973 | unsigned sysenter_feature; |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 974 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 975 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 976 | sysenter_feature = X86_FEATURE_SEP; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 977 | #else |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 978 | sysenter_feature = X86_FEATURE_SYSENTER32; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 979 | #endif |
| 980 | |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 981 | if (!boot_cpu_has(sysenter_feature)) |
| 982 | return; |
| 983 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 984 | ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 985 | if(ret != 0) |
| 986 | setup_clear_cpu_cap(sysenter_feature); |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 989 | void xen_enable_syscall(void) |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 990 | { |
| 991 | #ifdef CONFIG_X86_64 |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 992 | int ret; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 993 | |
| 994 | ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target); |
| 995 | if (ret != 0) { |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 996 | printk(KERN_ERR "Failed to set syscall callback: %d\n", ret); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 997 | /* Pretty fatal; 64-bit userspace has no other |
| 998 | mechanism for syscalls. */ |
| 999 | } |
| 1000 | |
| 1001 | if (boot_cpu_has(X86_FEATURE_SYSCALL32)) { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 1002 | ret = register_callback(CALLBACKTYPE_syscall32, |
| 1003 | xen_syscall32_target); |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 1004 | if (ret != 0) |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 1005 | setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 1006 | } |
| 1007 | #endif /* CONFIG_X86_64 */ |
| 1008 | } |
David Vrabel | ea9f927 | 2014-06-16 13:07:00 +0200 | [diff] [blame] | 1009 | |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 1010 | void __init xen_pvmmu_arch_setup(void) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1011 | { |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1012 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments); |
| 1013 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables); |
| 1014 | |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 1015 | HYPERVISOR_vm_assist(VMASST_CMD_enable, |
| 1016 | VMASST_TYPE_pae_extended_cr3); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1017 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 1018 | if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) || |
| 1019 | register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback)) |
| 1020 | BUG(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1021 | |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 1022 | xen_enable_sysenter(); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 1023 | xen_enable_syscall(); |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* This function is not called for HVM domains */ |
| 1027 | void __init xen_arch_setup(void) |
| 1028 | { |
| 1029 | xen_panic_handler_init(); |
| 1030 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 1031 | xen_pvmmu_arch_setup(); |
| 1032 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1033 | #ifdef CONFIG_ACPI |
| 1034 | if (!(xen_start_info->flags & SIF_INITDOMAIN)) { |
| 1035 | printk(KERN_INFO "ACPI in unprivileged domain disabled\n"); |
| 1036 | disable_acpi(); |
| 1037 | } |
| 1038 | #endif |
| 1039 | |
| 1040 | memcpy(boot_command_line, xen_start_info->cmd_line, |
| 1041 | MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ? |
| 1042 | COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE); |
| 1043 | |
Jeremy Fitzhardinge | bc15fde | 2010-11-22 17:17:50 -0800 | [diff] [blame] | 1044 | /* Set up idle, making sure it calls safe_halt() pvop */ |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 1045 | disable_cpuidle(); |
Konrad Rzeszutek Wilk | 48cdd82 | 2012-03-13 20:06:57 -0400 | [diff] [blame] | 1046 | disable_cpufreq(); |
Len Brown | 6a377dd | 2013-02-09 23:08:07 -0500 | [diff] [blame] | 1047 | WARN_ON(xen_set_default_idle()); |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 1048 | fiddle_vdso(); |
Konrad Rzeszutek Wilk | 8d54db79 | 2012-08-17 10:22:37 -0400 | [diff] [blame] | 1049 | #ifdef CONFIG_NUMA |
| 1050 | numa_off = 1; |
| 1051 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1052 | } |