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 | |
| 7 | #include <linux/module.h> |
| 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> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 17 | #include <asm/e820.h> |
| 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> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 30 | #include "xen-ops.h" |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 31 | #include "vdso.h" |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 32 | #include "p2m.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 | |
| 35 | /* These are code, but not functions. Defined in entry.S */ |
| 36 | extern const char xen_hypervisor_callback[]; |
| 37 | extern const char xen_failsafe_callback[]; |
Konrad Rzeszutek Wilk | 6efa20e | 2013-07-19 11:51:31 -0400 | [diff] [blame] | 38 | #ifdef CONFIG_X86_64 |
Andi Kleen | 07ba06d | 2013-10-22 09:07:59 -0700 | [diff] [blame] | 39 | extern asmlinkage void nmi(void); |
Konrad Rzeszutek Wilk | 6efa20e | 2013-07-19 11:51:31 -0400 | [diff] [blame] | 40 | #endif |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 41 | extern void xen_sysenter_target(void); |
| 42 | extern void xen_syscall_target(void); |
| 43 | extern void xen_syscall32_target(void); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 44 | |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 45 | /* Amount of extra memory space we add to the e820 ranges */ |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 46 | 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] | 47 | |
David Vrabel | aa24411 | 2011-09-28 17:46:32 +0100 | [diff] [blame] | 48 | /* Number of pages released from the initial allocation. */ |
| 49 | unsigned long xen_released_pages; |
| 50 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 51 | /* |
| 52 | * Buffer used to remap identity mapped pages. We only need the virtual space. |
| 53 | * The physical page behind this address is remapped as needed to different |
| 54 | * buffer pages. |
| 55 | */ |
| 56 | #define REMAP_SIZE (P2M_PER_PAGE - 3) |
| 57 | static struct { |
| 58 | unsigned long next_area_mfn; |
| 59 | unsigned long target_pfn; |
| 60 | unsigned long size; |
| 61 | unsigned long mfns[REMAP_SIZE]; |
| 62 | } xen_remap_buf __initdata __aligned(PAGE_SIZE); |
| 63 | static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 64 | |
Jeremy Fitzhardinge | 698bb8d | 2010-09-14 10:19:14 -0700 | [diff] [blame] | 65 | /* |
| 66 | * The maximum amount of extra memory compared to the base size. The |
| 67 | * main scaling factor is the size of struct page. At extreme ratios |
| 68 | * of base:extra, all the base memory can be filled with page |
| 69 | * structures for the extra memory, leaving no space for anything |
| 70 | * else. |
| 71 | * |
| 72 | * 10x seems like a reasonable balance between scaling flexibility and |
| 73 | * leaving a practically usable system. |
| 74 | */ |
| 75 | #define EXTRA_MEM_RATIO (10) |
| 76 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 77 | static void __init xen_add_extra_mem(u64 start, u64 size) |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 78 | { |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 79 | int i; |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 80 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 81 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
| 82 | /* Add new region. */ |
| 83 | if (xen_extra_mem[i].size == 0) { |
| 84 | xen_extra_mem[i].start = start; |
| 85 | xen_extra_mem[i].size = size; |
| 86 | break; |
| 87 | } |
| 88 | /* Append to existing region. */ |
| 89 | if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) { |
| 90 | xen_extra_mem[i].size += size; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | if (i == XEN_EXTRA_MEM_MAX_REGIONS) |
| 95 | printk(KERN_WARNING "Warning: not enough extra memory regions\n"); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 96 | |
Tejun Heo | d4bbf7e | 2011-11-28 09:46:22 -0800 | [diff] [blame] | 97 | memblock_reserve(start, size); |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 98 | } |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 99 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 100 | static void __init xen_del_extra_mem(u64 start, u64 size) |
| 101 | { |
| 102 | int i; |
| 103 | u64 start_r, size_r; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 104 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 105 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
| 106 | start_r = xen_extra_mem[i].start; |
| 107 | size_r = xen_extra_mem[i].size; |
Konrad Rzeszutek Wilk | c96aae1 | 2012-08-17 16:43:28 -0400 | [diff] [blame] | 108 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 109 | /* Start of region. */ |
| 110 | if (start_r == start) { |
| 111 | BUG_ON(size > size_r); |
| 112 | xen_extra_mem[i].start += size; |
| 113 | xen_extra_mem[i].size -= size; |
| 114 | break; |
| 115 | } |
| 116 | /* End of region. */ |
| 117 | if (start_r + size_r == start + size) { |
| 118 | BUG_ON(size > size_r); |
| 119 | xen_extra_mem[i].size -= size; |
| 120 | break; |
| 121 | } |
| 122 | /* Mid of region. */ |
| 123 | if (start > start_r && start < start_r + size_r) { |
| 124 | BUG_ON(start + size > start_r + size_r); |
| 125 | xen_extra_mem[i].size = start - start_r; |
| 126 | /* Calling memblock_reserve() again is okay. */ |
| 127 | xen_add_extra_mem(start + size, start_r + size_r - |
| 128 | (start + size)); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | memblock_free(start, size); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Called during boot before the p2m list can take entries beyond the |
| 137 | * hypervisor supplied p2m list. Entries in extra mem are to be regarded as |
| 138 | * invalid. |
| 139 | */ |
| 140 | unsigned long __ref xen_chk_extra_mem(unsigned long pfn) |
| 141 | { |
| 142 | int i; |
| 143 | unsigned long addr = PFN_PHYS(pfn); |
| 144 | |
| 145 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
| 146 | if (addr >= xen_extra_mem[i].start && |
| 147 | addr < xen_extra_mem[i].start + xen_extra_mem[i].size) |
| 148 | return INVALID_P2M_ENTRY; |
| 149 | } |
| 150 | |
| 151 | return IDENTITY_FRAME(pfn); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Mark all pfns of extra mem as invalid in p2m list. |
| 156 | */ |
| 157 | void __init xen_inv_extra_mem(void) |
| 158 | { |
| 159 | unsigned long pfn, pfn_s, pfn_e; |
| 160 | int i; |
| 161 | |
| 162 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { |
| 163 | pfn_s = PFN_DOWN(xen_extra_mem[i].start); |
| 164 | pfn_e = PFN_UP(xen_extra_mem[i].start + xen_extra_mem[i].size); |
| 165 | for (pfn = pfn_s; pfn < pfn_e; pfn++) |
| 166 | set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Konrad Rzeszutek Wilk | c96aae1 | 2012-08-17 16:43:28 -0400 | [diff] [blame] | 167 | } |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Finds the next RAM pfn available in the E820 map after min_pfn. |
| 172 | * This function updates min_pfn with the pfn found and returns |
| 173 | * the size of that range or zero if not found. |
| 174 | */ |
| 175 | static unsigned long __init xen_find_pfn_range( |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 176 | const struct e820entry *list, size_t map_size, |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 177 | unsigned long *min_pfn) |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 178 | { |
| 179 | const struct e820entry *entry; |
| 180 | unsigned int i; |
| 181 | unsigned long done = 0; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 182 | |
| 183 | for (i = 0, entry = list; i < map_size; i++, entry++) { |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 184 | unsigned long s_pfn; |
| 185 | unsigned long e_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 186 | |
| 187 | if (entry->type != E820_RAM) |
| 188 | continue; |
| 189 | |
zhenzhong.duan | c3d93f8 | 2012-07-18 13:06:39 +0800 | [diff] [blame] | 190 | e_pfn = PFN_DOWN(entry->addr + entry->size); |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 191 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 192 | /* We only care about E820 after this */ |
| 193 | if (e_pfn < *min_pfn) |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 194 | continue; |
| 195 | |
zhenzhong.duan | c3d93f8 | 2012-07-18 13:06:39 +0800 | [diff] [blame] | 196 | s_pfn = PFN_UP(entry->addr); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 197 | |
| 198 | /* If min_pfn falls within the E820 entry, we want to start |
| 199 | * at the min_pfn PFN. |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 200 | */ |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 201 | if (s_pfn <= *min_pfn) { |
| 202 | done = e_pfn - *min_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 203 | } else { |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 204 | done = e_pfn - s_pfn; |
| 205 | *min_pfn = s_pfn; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 206 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 207 | break; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 208 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 209 | |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 210 | return done; |
| 211 | } |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 212 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 213 | static int __init xen_free_mfn(unsigned long mfn) |
| 214 | { |
| 215 | struct xen_memory_reservation reservation = { |
| 216 | .address_bits = 0, |
| 217 | .extent_order = 0, |
| 218 | .domid = DOMID_SELF |
| 219 | }; |
| 220 | |
| 221 | set_xen_guest_handle(reservation.extent_start, &mfn); |
| 222 | reservation.nr_extents = 1; |
| 223 | |
| 224 | return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); |
| 225 | } |
| 226 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 227 | /* |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 228 | * 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] | 229 | * as a fallback if the remapping fails. |
| 230 | */ |
| 231 | static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn, |
| 232 | unsigned long end_pfn, unsigned long nr_pages, unsigned long *identity, |
| 233 | unsigned long *released) |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 234 | { |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 235 | unsigned long len = 0; |
| 236 | unsigned long pfn, end; |
| 237 | int ret; |
| 238 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 239 | WARN_ON(start_pfn > end_pfn); |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 240 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 241 | end = min(end_pfn, nr_pages); |
| 242 | for (pfn = start_pfn; pfn < end; pfn++) { |
| 243 | unsigned long mfn = pfn_to_mfn(pfn); |
| 244 | |
| 245 | /* Make sure pfn exists to start with */ |
| 246 | if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn) |
| 247 | continue; |
| 248 | |
| 249 | ret = xen_free_mfn(mfn); |
| 250 | WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret); |
| 251 | |
| 252 | if (ret == 1) { |
| 253 | if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY)) |
| 254 | break; |
| 255 | len++; |
| 256 | } else |
| 257 | break; |
| 258 | } |
| 259 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 260 | /* Need to release pages first */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 261 | *released += len; |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 262 | *identity += set_phys_range_identity(start_pfn, end_pfn); |
| 263 | } |
| 264 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 265 | /* |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 266 | * Helper function to update the p2m and m2p tables and kernel mapping. |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 267 | */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 268 | 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] | 269 | { |
| 270 | struct mmu_update update = { |
| 271 | .ptr = ((unsigned long long)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE, |
| 272 | .val = pfn |
| 273 | }; |
| 274 | |
| 275 | /* Update p2m */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 276 | if (!set_phys_to_machine(pfn, mfn)) { |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 277 | WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n", |
| 278 | pfn, mfn); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 279 | BUG(); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* Update m2p */ |
| 283 | if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) { |
| 284 | WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n", |
| 285 | mfn, pfn); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 286 | BUG(); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 289 | /* Update kernel mapping, but not for highmem. */ |
| 290 | if ((pfn << PAGE_SHIFT) >= __pa(high_memory)) |
| 291 | return; |
| 292 | |
| 293 | if (HYPERVISOR_update_va_mapping((unsigned long)__va(pfn << PAGE_SHIFT), |
| 294 | mfn_pte(mfn, PAGE_KERNEL), 0)) { |
| 295 | WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n", |
| 296 | mfn, pfn); |
| 297 | BUG(); |
| 298 | } |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * 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] | 303 | * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the |
| 304 | * original allocation at remap_pfn. The information needed for remapping is |
| 305 | * saved in the memory itself to avoid the need for allocating buffers. The |
| 306 | * complete remap information is contained in a list of MFNs each containing |
| 307 | * up to REMAP_SIZE MFNs and the start target PFN for doing the remap. |
| 308 | * This enables us to preserve the original mfn sequence while doing the |
| 309 | * remapping at a time when the memory management is capable of allocating |
| 310 | * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and |
| 311 | * its callers. |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 312 | */ |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 313 | static void __init xen_do_set_identity_and_remap_chunk( |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 314 | unsigned long start_pfn, unsigned long size, unsigned long remap_pfn) |
| 315 | { |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 316 | unsigned long buf = (unsigned long)&xen_remap_buf; |
| 317 | unsigned long mfn_save, mfn; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 318 | unsigned long ident_pfn_iter, remap_pfn_iter; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 319 | unsigned long ident_end_pfn = start_pfn + size; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 320 | unsigned long left = size; |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 321 | unsigned long ident_cnt = 0; |
| 322 | unsigned int i, chunk; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 323 | |
| 324 | WARN_ON(size == 0); |
| 325 | |
| 326 | BUG_ON(xen_feature(XENFEAT_auto_translated_physmap)); |
| 327 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 328 | mfn_save = virt_to_mfn(buf); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 329 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 330 | for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn; |
| 331 | ident_pfn_iter < ident_end_pfn; |
| 332 | ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) { |
| 333 | chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 334 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 335 | /* Map first pfn to xen_remap_buf */ |
| 336 | mfn = pfn_to_mfn(ident_pfn_iter); |
| 337 | set_pte_mfn(buf, mfn, PAGE_KERNEL); |
| 338 | |
| 339 | /* Save mapping information in page */ |
| 340 | xen_remap_buf.next_area_mfn = xen_remap_mfn; |
| 341 | xen_remap_buf.target_pfn = remap_pfn_iter; |
| 342 | xen_remap_buf.size = chunk; |
| 343 | for (i = 0; i < chunk; i++) |
| 344 | xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i); |
| 345 | |
| 346 | /* Put remap buf into list. */ |
| 347 | xen_remap_mfn = mfn; |
| 348 | |
| 349 | /* Set identity map */ |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 350 | ident_cnt += set_phys_range_identity(ident_pfn_iter, |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 351 | ident_pfn_iter + chunk); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 352 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 353 | left -= chunk; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 356 | /* Restore old xen_remap_buf mapping */ |
| 357 | set_pte_mfn(buf, mfn_save, PAGE_KERNEL); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * This function takes a contiguous pfn range that needs to be identity mapped |
| 362 | * and: |
| 363 | * |
| 364 | * 1) Finds a new range of pfns to use to remap based on E820 and remap_pfn. |
| 365 | * 2) Calls the do_ function to actually do the mapping/remapping work. |
| 366 | * |
| 367 | * The goal is to not allocate additional memory but to remap the existing |
| 368 | * pages. In the case of an error the underlying memory is simply released back |
| 369 | * to Xen and not remapped. |
| 370 | */ |
Juergen Gross | 76f0a48 | 2014-12-08 06:32:19 +0100 | [diff] [blame^] | 371 | static unsigned long __init xen_set_identity_and_remap_chunk( |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 372 | const struct e820entry *list, size_t map_size, unsigned long start_pfn, |
| 373 | unsigned long end_pfn, unsigned long nr_pages, unsigned long remap_pfn, |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 374 | unsigned long *identity, unsigned long *released) |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 375 | { |
| 376 | unsigned long pfn; |
| 377 | unsigned long i = 0; |
| 378 | unsigned long n = end_pfn - start_pfn; |
| 379 | |
| 380 | while (i < n) { |
| 381 | unsigned long cur_pfn = start_pfn + i; |
| 382 | unsigned long left = n - i; |
| 383 | unsigned long size = left; |
| 384 | unsigned long remap_range_size; |
| 385 | |
| 386 | /* Do not remap pages beyond the current allocation */ |
| 387 | if (cur_pfn >= nr_pages) { |
| 388 | /* Identity map remaining pages */ |
| 389 | *identity += set_phys_range_identity(cur_pfn, |
| 390 | cur_pfn + size); |
| 391 | break; |
| 392 | } |
| 393 | if (cur_pfn + size > nr_pages) |
| 394 | size = nr_pages - cur_pfn; |
| 395 | |
| 396 | remap_range_size = xen_find_pfn_range(list, map_size, |
| 397 | &remap_pfn); |
| 398 | if (!remap_range_size) { |
| 399 | pr_warning("Unable to find available pfn range, not remapping identity pages\n"); |
| 400 | xen_set_identity_and_release_chunk(cur_pfn, |
| 401 | cur_pfn + left, nr_pages, identity, released); |
| 402 | break; |
| 403 | } |
| 404 | /* Adjust size to fit in current e820 RAM region */ |
| 405 | if (size > remap_range_size) |
| 406 | size = remap_range_size; |
| 407 | |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 408 | xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 409 | |
| 410 | /* Update variables to reflect new mappings. */ |
| 411 | i += size; |
| 412 | remap_pfn += size; |
| 413 | *identity += size; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | * If the PFNs are currently mapped, the VA mapping also needs |
| 418 | * to be updated to be 1:1. |
| 419 | */ |
| 420 | for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++) |
| 421 | (void)HYPERVISOR_update_va_mapping( |
| 422 | (unsigned long)__va(pfn << PAGE_SHIFT), |
| 423 | mfn_pte(pfn, PAGE_KERNEL_IO), 0); |
| 424 | |
| 425 | return remap_pfn; |
| 426 | } |
| 427 | |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 428 | static void __init xen_set_identity_and_remap( |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 429 | const struct e820entry *list, size_t map_size, unsigned long nr_pages, |
| 430 | unsigned long *released) |
Miroslav Rezanina | 093d7b4 | 2009-09-16 03:56:17 -0400 | [diff] [blame] | 431 | { |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 432 | phys_addr_t start = 0; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 433 | unsigned long identity = 0; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 434 | unsigned long last_pfn = nr_pages; |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 435 | const struct e820entry *entry; |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 436 | unsigned long num_released = 0; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 437 | int i; |
| 438 | |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 439 | /* |
| 440 | * Combine non-RAM regions and gaps until a RAM region (or the |
| 441 | * end of the map) is reached, then set the 1:1 map and |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 442 | * remap the memory in those non-RAM regions. |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 443 | * |
| 444 | * The combined non-RAM regions are rounded to a whole number |
| 445 | * of pages so any partial pages are accessible via the 1:1 |
| 446 | * mapping. This is needed for some BIOSes that put (for |
| 447 | * example) the DMI tables in a reserved region that begins on |
| 448 | * a non-page boundary. |
| 449 | */ |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 450 | for (i = 0, entry = list; i < map_size; i++, entry++) { |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 451 | phys_addr_t end = entry->addr + entry->size; |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 452 | if (entry->type == E820_RAM || i == map_size - 1) { |
| 453 | unsigned long start_pfn = PFN_DOWN(start); |
| 454 | unsigned long end_pfn = PFN_UP(end); |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 455 | |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 456 | if (entry->type == E820_RAM) |
| 457 | end_pfn = PFN_UP(entry->addr); |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 458 | |
David Vrabel | 83d51ab | 2012-05-03 16:15:42 +0100 | [diff] [blame] | 459 | if (start_pfn < end_pfn) |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 460 | last_pfn = xen_set_identity_and_remap_chunk( |
| 461 | list, map_size, start_pfn, |
| 462 | end_pfn, nr_pages, last_pfn, |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 463 | &identity, &num_released); |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 464 | start = end; |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 465 | } |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 466 | } |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 467 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 468 | *released = num_released; |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 469 | |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 470 | pr_info("Set %ld page(s) to 1-1 mapping\n", identity); |
Matt Rushton | 4fbb67e3 | 2014-08-11 11:57:57 -0700 | [diff] [blame] | 471 | pr_info("Released %ld page(s)\n", num_released); |
Konrad Rzeszutek Wilk | 68df0da | 2011-02-01 17:15:30 -0500 | [diff] [blame] | 472 | } |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * Remap the memory prepared in xen_do_set_identity_and_remap_chunk(). |
| 476 | * The remap information (which mfn remap to which pfn) is contained in the |
| 477 | * to be remapped memory itself in a linked list anchored at xen_remap_mfn. |
| 478 | * This scheme allows to remap the different chunks in arbitrary order while |
| 479 | * the resulting mapping will be independant from the order. |
| 480 | */ |
| 481 | void __init xen_remap_memory(void) |
| 482 | { |
| 483 | unsigned long buf = (unsigned long)&xen_remap_buf; |
| 484 | unsigned long mfn_save, mfn, pfn; |
| 485 | unsigned long remapped = 0; |
| 486 | unsigned int i; |
| 487 | unsigned long pfn_s = ~0UL; |
| 488 | unsigned long len = 0; |
| 489 | |
| 490 | mfn_save = virt_to_mfn(buf); |
| 491 | |
| 492 | while (xen_remap_mfn != INVALID_P2M_ENTRY) { |
| 493 | /* Map the remap information */ |
| 494 | set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL); |
| 495 | |
| 496 | BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]); |
| 497 | |
| 498 | pfn = xen_remap_buf.target_pfn; |
| 499 | for (i = 0; i < xen_remap_buf.size; i++) { |
| 500 | mfn = xen_remap_buf.mfns[i]; |
| 501 | xen_update_mem_tables(pfn, mfn); |
| 502 | remapped++; |
| 503 | pfn++; |
| 504 | } |
| 505 | if (pfn_s == ~0UL || pfn == pfn_s) { |
| 506 | pfn_s = xen_remap_buf.target_pfn; |
| 507 | len += xen_remap_buf.size; |
| 508 | } else if (pfn_s + len == xen_remap_buf.target_pfn) { |
| 509 | len += xen_remap_buf.size; |
| 510 | } else { |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 511 | xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len)); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 512 | pfn_s = xen_remap_buf.target_pfn; |
| 513 | len = xen_remap_buf.size; |
| 514 | } |
| 515 | |
| 516 | mfn = xen_remap_mfn; |
| 517 | xen_remap_mfn = xen_remap_buf.next_area_mfn; |
| 518 | } |
| 519 | |
| 520 | if (pfn_s != ~0UL && len) |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 521 | xen_del_extra_mem(PFN_PHYS(pfn_s), PFN_PHYS(len)); |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 522 | |
| 523 | set_pte_mfn(buf, mfn_save, PAGE_KERNEL); |
| 524 | |
| 525 | pr_info("Remapped %ld page(s)\n", remapped); |
| 526 | } |
| 527 | |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 528 | static unsigned long __init xen_get_max_pages(void) |
| 529 | { |
| 530 | unsigned long max_pages = MAX_DOMAIN_PAGES; |
| 531 | domid_t domid = DOMID_SELF; |
| 532 | int ret; |
| 533 | |
Ian Campbell | d3db728 | 2011-12-14 12:16:08 +0000 | [diff] [blame] | 534 | /* |
| 535 | * For the initial domain we use the maximum reservation as |
| 536 | * the maximum page. |
| 537 | * |
| 538 | * For guest domains the current maximum reservation reflects |
| 539 | * the current maximum rather than the static maximum. In this |
| 540 | * case the e820 map provided to us will cover the static |
| 541 | * maximum region. |
| 542 | */ |
| 543 | if (xen_initial_domain()) { |
| 544 | ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid); |
| 545 | if (ret > 0) |
| 546 | max_pages = ret; |
| 547 | } |
| 548 | |
David Vrabel | d312ae87 | 2011-08-19 15:57:16 +0100 | [diff] [blame] | 549 | return min(max_pages, MAX_DOMAIN_PAGES); |
| 550 | } |
| 551 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 552 | static void xen_align_and_add_e820_region(u64 start, u64 size, int type) |
| 553 | { |
| 554 | u64 end = start + size; |
| 555 | |
| 556 | /* Align RAM regions to page boundaries. */ |
| 557 | if (type == E820_RAM) { |
| 558 | start = PAGE_ALIGN(start); |
| 559 | end &= ~((u64)PAGE_SIZE - 1); |
| 560 | } |
| 561 | |
| 562 | e820_add_region(start, end - start, type); |
| 563 | } |
| 564 | |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 565 | void xen_ignore_unusable(struct e820entry *list, size_t map_size) |
| 566 | { |
| 567 | struct e820entry *entry; |
| 568 | unsigned int i; |
| 569 | |
| 570 | for (i = 0, entry = list; i < map_size; i++, entry++) { |
| 571 | if (entry->type == E820_UNUSABLE) |
| 572 | entry->type = E820_RAM; |
| 573 | } |
| 574 | } |
| 575 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 576 | /** |
| 577 | * machine_specific_memory_setup - Hook for machine specific memory setup. |
| 578 | **/ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 579 | char * __init xen_memory_setup(void) |
| 580 | { |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 581 | static struct e820entry map[E820MAX] __initdata; |
| 582 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 583 | unsigned long max_pfn = xen_start_info->nr_pages; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 584 | unsigned long long mem_end; |
| 585 | int rc; |
| 586 | struct xen_memory_map memmap; |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 587 | unsigned long max_pages; |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 588 | unsigned long extra_pages = 0; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 589 | int i; |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 590 | int op; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 591 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 592 | max_pfn = min(MAX_DOMAIN_PAGES, max_pfn); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 593 | mem_end = PFN_PHYS(max_pfn); |
| 594 | |
| 595 | memmap.nr_entries = E820MAX; |
| 596 | set_xen_guest_handle(memmap.buffer, map); |
| 597 | |
Ian Campbell | 9e9a5fc | 2010-09-02 16:16:00 +0100 | [diff] [blame] | 598 | op = xen_initial_domain() ? |
| 599 | XENMEM_machine_memory_map : |
| 600 | XENMEM_memory_map; |
| 601 | rc = HYPERVISOR_memory_op(op, &memmap); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 602 | if (rc == -ENOSYS) { |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 603 | BUG_ON(xen_initial_domain()); |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 604 | memmap.nr_entries = 1; |
| 605 | map[0].addr = 0ULL; |
| 606 | map[0].size = mem_end; |
| 607 | /* 8MB slack (to balance backend allocations). */ |
| 608 | map[0].size += 8ULL << 20; |
| 609 | map[0].type = E820_RAM; |
| 610 | rc = 0; |
| 611 | } |
| 612 | BUG_ON(rc); |
Martin Kelly | 1ea644c | 2014-10-16 20:48:11 -0700 | [diff] [blame] | 613 | BUG_ON(memmap.nr_entries == 0); |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 614 | |
David Vrabel | 3bc38cb | 2013-08-16 15:42:55 +0100 | [diff] [blame] | 615 | /* |
| 616 | * Xen won't allow a 1:1 mapping to be created to UNUSABLE |
| 617 | * regions, so if we're using the machine memory map leave the |
| 618 | * region as RAM as it is in the pseudo-physical map. |
| 619 | * |
| 620 | * UNUSABLE regions in domUs are not handled and will need |
| 621 | * a patch in the future. |
| 622 | */ |
| 623 | if (xen_initial_domain()) |
| 624 | xen_ignore_unusable(map, memmap.nr_entries); |
| 625 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 626 | /* Make sure the Xen-supplied memory map is well-ordered. */ |
| 627 | sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries); |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 628 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 629 | max_pages = xen_get_max_pages(); |
| 630 | if (max_pages > max_pfn) |
| 631 | extra_pages += max_pages - max_pfn; |
Stefano Stabellini | 7cb31b7 | 2011-01-27 10:13:25 -0500 | [diff] [blame] | 632 | |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 633 | /* |
Juergen Gross | 1f3ac86 | 2014-11-28 11:53:53 +0100 | [diff] [blame] | 634 | * Set identity map on non-RAM pages and prepare remapping the |
| 635 | * underlying RAM. |
David Vrabel | f3f436e | 2011-09-28 17:46:36 +0100 | [diff] [blame] | 636 | */ |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 637 | xen_set_identity_and_remap(map, memmap.nr_entries, max_pfn, |
| 638 | &xen_released_pages); |
Jeremy Fitzhardinge | 42ee147 | 2010-08-30 16:41:02 -0700 | [diff] [blame] | 639 | |
Konrad Rzeszutek Wilk | 58b7b53 | 2012-05-29 12:36:43 -0400 | [diff] [blame] | 640 | extra_pages += xen_released_pages; |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 641 | |
Konrad Rzeszutek Wilk | 2e2fb75 | 2012-04-06 10:07:11 -0400 | [diff] [blame] | 642 | /* |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 643 | * Clamp the amount of extra memory to a EXTRA_MEM_RATIO |
| 644 | * factor the base size. On non-highmem systems, the base |
| 645 | * size is the full initial memory allocation; on highmem it |
| 646 | * is limited to the max size of lowmem, so that it doesn't |
| 647 | * get completely filled. |
| 648 | * |
| 649 | * In principle there could be a problem in lowmem systems if |
| 650 | * the initial memory is also very large with respect to |
| 651 | * lowmem, but we won't try to deal with that here. |
| 652 | */ |
| 653 | extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)), |
| 654 | extra_pages); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 655 | i = 0; |
| 656 | while (i < memmap.nr_entries) { |
| 657 | u64 addr = map[i].addr; |
| 658 | u64 size = map[i].size; |
| 659 | u32 type = map[i].type; |
| 660 | |
| 661 | if (type == E820_RAM) { |
| 662 | if (addr < mem_end) { |
| 663 | size = min(size, mem_end - addr); |
| 664 | } else if (extra_pages) { |
| 665 | size = min(size, (u64)extra_pages * PAGE_SIZE); |
| 666 | extra_pages -= size / PAGE_SIZE; |
| 667 | xen_add_extra_mem(addr, size); |
Juergen Gross | 5b8e7d8 | 2014-11-28 11:53:55 +0100 | [diff] [blame] | 668 | xen_max_p2m_pfn = PFN_DOWN(addr + size); |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 669 | } else |
| 670 | type = E820_UNUSABLE; |
Jeremy Fitzhardinge | 3654581 | 2010-09-29 16:54:33 -0700 | [diff] [blame] | 671 | } |
| 672 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 673 | xen_align_and_add_e820_region(addr, size, type); |
Jeremy Fitzhardinge | b5b43ce | 2010-09-02 17:10:12 -0700 | [diff] [blame] | 674 | |
David Vrabel | dc91c72 | 2011-09-29 12:26:19 +0100 | [diff] [blame] | 675 | map[i].addr += size; |
| 676 | map[i].size -= size; |
| 677 | if (map[i].size == 0) |
| 678 | i++; |
Ian Campbell | 35ae11f | 2009-02-06 19:09:48 -0800 | [diff] [blame] | 679 | } |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 680 | |
| 681 | /* |
David Vrabel | 25b884a | 2014-01-03 15:46:10 +0000 | [diff] [blame] | 682 | * Set the rest as identity mapped, in case PCI BARs are |
| 683 | * located here. |
| 684 | * |
| 685 | * PFNs above MAX_P2M_PFN are considered identity mapped as |
| 686 | * well. |
| 687 | */ |
| 688 | set_phys_range_identity(map[i-1].addr / PAGE_SIZE, ~0ul); |
| 689 | |
| 690 | /* |
Ian Campbell | 9ec23a7f | 2010-10-28 11:32:29 -0700 | [diff] [blame] | 691 | * In domU, the ISA region is normal, usable memory, but we |
| 692 | * reserve ISA memory anyway because too many things poke |
Jeremy Fitzhardinge | b792c75 | 2008-06-16 14:54:49 -0700 | [diff] [blame] | 693 | * about in there. |
| 694 | */ |
| 695 | e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, |
| 696 | E820_RESERVED); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 697 | |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 698 | /* |
| 699 | * Reserve Xen bits: |
| 700 | * - mfn_list |
| 701 | * - xen_start_info |
| 702 | * See comment above "struct start_info" in <xen/interface/xen.h> |
Konrad Rzeszutek Wilk | 51faaf2 | 2012-08-22 13:00:10 -0400 | [diff] [blame] | 703 | * We tried to make the the memblock_reserve more selective so |
| 704 | * that it would be clear what region is reserved. Sadly we ran |
| 705 | * in the problem wherein on a 64-bit hypervisor with a 32-bit |
| 706 | * initial domain, the pt_base has the cr3 value which is not |
| 707 | * neccessarily where the pagetable starts! As Jan put it: " |
| 708 | * Actually, the adjustment turns out to be correct: The page |
| 709 | * tables for a 32-on-64 dom0 get allocated in the order "first L1", |
| 710 | * "first L2", "first L3", so the offset to the page table base is |
| 711 | * indeed 2. When reading xen/include/public/xen.h's comment |
| 712 | * very strictly, this is not a violation (since there nothing is said |
| 713 | * that the first thing in the page table space is pointed to by |
| 714 | * pt_base; I admit that this seems to be implied though, namely |
| 715 | * do I think that it is implied that the page table space is the |
| 716 | * range [pt_base, pt_base + nt_pt_frames), whereas that |
| 717 | * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames), |
| 718 | * which - without a priori knowledge - the kernel would have |
| 719 | * difficulty to figure out)." - so lets just fall back to the |
| 720 | * easy way and reserve the whole region. |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 721 | */ |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 722 | memblock_reserve(__pa(xen_start_info->mfn_list), |
| 723 | xen_start_info->pt_base - xen_start_info->mfn_list); |
Jeremy Fitzhardinge | be5bf9f | 2008-06-16 14:54:46 -0700 | [diff] [blame] | 724 | |
| 725 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 726 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 727 | return "Xen"; |
| 728 | } |
| 729 | |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 730 | /* |
David Vrabel | abacaad | 2014-06-02 17:58:01 +0100 | [diff] [blame] | 731 | * Machine specific memory setup for auto-translated guests. |
| 732 | */ |
| 733 | char * __init xen_auto_xlated_memory_setup(void) |
| 734 | { |
| 735 | static struct e820entry map[E820MAX] __initdata; |
| 736 | |
| 737 | struct xen_memory_map memmap; |
| 738 | int i; |
| 739 | int rc; |
| 740 | |
| 741 | memmap.nr_entries = E820MAX; |
| 742 | set_xen_guest_handle(memmap.buffer, map); |
| 743 | |
| 744 | rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); |
| 745 | if (rc < 0) |
| 746 | panic("No memory map (%d)\n", rc); |
| 747 | |
| 748 | sanitize_e820_map(map, ARRAY_SIZE(map), &memmap.nr_entries); |
| 749 | |
| 750 | for (i = 0; i < memmap.nr_entries; i++) |
| 751 | e820_add_region(map[i].addr, map[i].size, map[i].type); |
| 752 | |
| 753 | memblock_reserve(__pa(xen_start_info->mfn_list), |
| 754 | xen_start_info->pt_base - xen_start_info->mfn_list); |
| 755 | |
| 756 | return "Xen"; |
| 757 | } |
| 758 | |
| 759 | /* |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 760 | * Set the bit indicating "nosegneg" library variants should be used. |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 761 | * We only need to bother in pure 32-bit mode; compat 32-bit processes |
| 762 | * can have un-truncated segments, so wrapping around is allowed. |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 763 | */ |
Sam Ravnborg | 08b6d29 | 2008-01-30 13:33:25 +0100 | [diff] [blame] | 764 | static void __init fiddle_vdso(void) |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 765 | { |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 766 | #ifdef CONFIG_X86_32 |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 767 | /* |
| 768 | * This could be called before selected_vdso32 is initialized, so |
| 769 | * just fiddle with both possible images. vdso_image_32_syscall |
| 770 | * can't be selected, since it only exists on 64-bit systems. |
| 771 | */ |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 772 | u32 *mask; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 773 | mask = vdso_image_32_int80.data + |
| 774 | vdso_image_32_int80.sym_VDSO32_NOTE_MASK; |
Jeremy Fitzhardinge | 6a52e4b | 2008-07-12 02:22:00 -0700 | [diff] [blame] | 775 | *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 776 | mask = vdso_image_32_sysenter.data + |
| 777 | vdso_image_32_sysenter.sym_VDSO32_NOTE_MASK; |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 778 | *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 779 | #endif |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 782 | static int register_callback(unsigned type, const void *func) |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 783 | { |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 784 | struct callback_register callback = { |
| 785 | .type = type, |
| 786 | .address = XEN_CALLBACK(__KERNEL_CS, func), |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 787 | .flags = CALLBACKF_mask_events, |
| 788 | }; |
| 789 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 790 | return HYPERVISOR_callback_op(CALLBACKOP_register, &callback); |
| 791 | } |
| 792 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 793 | void xen_enable_sysenter(void) |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 794 | { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 795 | int ret; |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 796 | unsigned sysenter_feature; |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 797 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 798 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 799 | sysenter_feature = X86_FEATURE_SEP; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 800 | #else |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 801 | sysenter_feature = X86_FEATURE_SYSENTER32; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 802 | #endif |
| 803 | |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 804 | if (!boot_cpu_has(sysenter_feature)) |
| 805 | return; |
| 806 | |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 807 | ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 808 | if(ret != 0) |
| 809 | setup_clear_cpu_cap(sysenter_feature); |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 812 | void xen_enable_syscall(void) |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 813 | { |
| 814 | #ifdef CONFIG_X86_64 |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 815 | int ret; |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 816 | |
| 817 | ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target); |
| 818 | if (ret != 0) { |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 819 | printk(KERN_ERR "Failed to set syscall callback: %d\n", ret); |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 820 | /* Pretty fatal; 64-bit userspace has no other |
| 821 | mechanism for syscalls. */ |
| 822 | } |
| 823 | |
| 824 | if (boot_cpu_has(X86_FEATURE_SYSCALL32)) { |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 825 | ret = register_callback(CALLBACKTYPE_syscall32, |
| 826 | xen_syscall32_target); |
Jeremy Fitzhardinge | d5303b8 | 2008-07-12 02:22:06 -0700 | [diff] [blame] | 827 | if (ret != 0) |
Jeremy Fitzhardinge | 62541c3 | 2008-07-10 16:24:08 -0700 | [diff] [blame] | 828 | setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 829 | } |
| 830 | #endif /* CONFIG_X86_64 */ |
| 831 | } |
David Vrabel | ea9f927 | 2014-06-16 13:07:00 +0200 | [diff] [blame] | 832 | |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 833 | void __init xen_pvmmu_arch_setup(void) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 834 | { |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 835 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments); |
| 836 | HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables); |
| 837 | |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 838 | HYPERVISOR_vm_assist(VMASST_CMD_enable, |
| 839 | VMASST_TYPE_pae_extended_cr3); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 840 | |
Jeremy Fitzhardinge | 88459d4 | 2008-07-08 15:07:02 -0700 | [diff] [blame] | 841 | if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) || |
| 842 | register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback)) |
| 843 | BUG(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 844 | |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 845 | xen_enable_sysenter(); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 846 | xen_enable_syscall(); |
Mukesh Rathor | d285d68 | 2013-12-13 12:45:31 -0500 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* This function is not called for HVM domains */ |
| 850 | void __init xen_arch_setup(void) |
| 851 | { |
| 852 | xen_panic_handler_init(); |
| 853 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 854 | xen_pvmmu_arch_setup(); |
| 855 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 856 | #ifdef CONFIG_ACPI |
| 857 | if (!(xen_start_info->flags & SIF_INITDOMAIN)) { |
| 858 | printk(KERN_INFO "ACPI in unprivileged domain disabled\n"); |
| 859 | disable_acpi(); |
| 860 | } |
| 861 | #endif |
| 862 | |
| 863 | memcpy(boot_command_line, xen_start_info->cmd_line, |
| 864 | MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ? |
| 865 | COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE); |
| 866 | |
Jeremy Fitzhardinge | bc15fde | 2010-11-22 17:17:50 -0800 | [diff] [blame] | 867 | /* Set up idle, making sure it calls safe_halt() pvop */ |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 868 | disable_cpuidle(); |
Konrad Rzeszutek Wilk | 48cdd82 | 2012-03-13 20:06:57 -0400 | [diff] [blame] | 869 | disable_cpufreq(); |
Len Brown | 6a377dd | 2013-02-09 23:08:07 -0500 | [diff] [blame] | 870 | WARN_ON(xen_set_default_idle()); |
Roland McGrath | d2eea68 | 2007-07-20 00:31:43 -0700 | [diff] [blame] | 871 | fiddle_vdso(); |
Konrad Rzeszutek Wilk | 8d54db79 | 2012-08-17 10:22:37 -0400 | [diff] [blame] | 872 | #ifdef CONFIG_NUMA |
| 873 | numa_off = 1; |
| 874 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 875 | } |