blob: 0a9fb7a0b4521970c50d558595906bfca7724e50 [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
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 Lua9ce6bc2010-08-25 13:39:17 -070011#include <linux/memblock.h>
Len Brownd91ee582011-04-01 18:28:35 -040012#include <linux/cpuidle.h>
Konrad Rzeszutek Wilk48cdd822012-03-13 20:06:57 -040013#include <linux/cpufreq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070014
15#include <asm/elf.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010016#include <asm/vdso.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070017#include <asm/e820.h>
18#include <asm/setup.h>
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -070019#include <asm/acpi.h>
Konrad Rzeszutek Wilk8d54db792012-08-17 10:22:37 -040020#include <asm/numa.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070021#include <asm/xen/hypervisor.h>
22#include <asm/xen/hypercall.h>
23
Ian Campbell45263cb2010-10-25 16:32:29 -070024#include <xen/xen.h>
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010025#include <xen/page.h>
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070026#include <xen/interface/callback.h>
Ian Campbell35ae11f2009-02-06 19:09:48 -080027#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070028#include <xen/interface/physdev.h>
29#include <xen/features.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070030#include "xen-ops.h"
Roland McGrathd2eea682007-07-20 00:31:43 -070031#include "vdso.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070032
33/* These are code, but not functions. Defined in entry.S */
34extern const char xen_hypervisor_callback[];
35extern const char xen_failsafe_callback[];
Tejf63c2f22008-12-16 11:56:06 -080036extern void xen_sysenter_target(void);
37extern void xen_syscall_target(void);
38extern void xen_syscall32_target(void);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070039
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070040/* Amount of extra memory space we add to the e820 ranges */
David Vrabel8b5d44a2011-09-28 17:46:34 +010041struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070042
David Vrabelaa244112011-09-28 17:46:32 +010043/* Number of pages released from the initial allocation. */
44unsigned long xen_released_pages;
45
Jeremy Fitzhardinge698bb8d2010-09-14 10:19:14 -070046/*
47 * The maximum amount of extra memory compared to the base size. The
48 * main scaling factor is the size of struct page. At extreme ratios
49 * of base:extra, all the base memory can be filled with page
50 * structures for the extra memory, leaving no space for anything
51 * else.
52 *
53 * 10x seems like a reasonable balance between scaling flexibility and
54 * leaving a practically usable system.
55 */
56#define EXTRA_MEM_RATIO (10)
57
David Vrabeldc91c722011-09-29 12:26:19 +010058static void __init xen_add_extra_mem(u64 start, u64 size)
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070059{
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050060 unsigned long pfn;
David Vrabeldc91c722011-09-29 12:26:19 +010061 int i;
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050062
David Vrabeldc91c722011-09-29 12:26:19 +010063 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
64 /* Add new region. */
65 if (xen_extra_mem[i].size == 0) {
66 xen_extra_mem[i].start = start;
67 xen_extra_mem[i].size = size;
68 break;
69 }
70 /* Append to existing region. */
71 if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
72 xen_extra_mem[i].size += size;
73 break;
74 }
75 }
76 if (i == XEN_EXTRA_MEM_MAX_REGIONS)
77 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070078
Tejun Heod4bbf7e2011-11-28 09:46:22 -080079 memblock_reserve(start, size);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070080
David Vrabeldc91c722011-09-29 12:26:19 +010081 xen_max_p2m_pfn = PFN_DOWN(start + size);
Konrad Rzeszutek Wilkc96aae12012-08-17 16:43:28 -040082 for (pfn = PFN_DOWN(start); pfn < xen_max_p2m_pfn; pfn++) {
83 unsigned long mfn = pfn_to_mfn(pfn);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070084
Konrad Rzeszutek Wilkc96aae12012-08-17 16:43:28 -040085 if (WARN(mfn == pfn, "Trying to over-write 1-1 mapping (pfn: %lx)\n", pfn))
86 continue;
87 WARN(mfn != INVALID_P2M_ENTRY, "Trying to remove %lx which has %lx mfn!\n",
88 pfn, mfn);
89
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050090 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Konrad Rzeszutek Wilkc96aae12012-08-17 16:43:28 -040091 }
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070092}
93
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -040094static unsigned long __init xen_do_chunk(unsigned long start,
95 unsigned long end, bool release)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040096{
97 struct xen_memory_reservation reservation = {
98 .address_bits = 0,
99 .extent_order = 0,
100 .domid = DOMID_SELF
101 };
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700102 unsigned long len = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400103 unsigned long pfn;
104 int ret;
105
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400106 for (pfn = start; pfn < end; pfn++) {
107 unsigned long frame;
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400108 unsigned long mfn = pfn_to_mfn(pfn);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400109
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400110 if (release) {
111 /* Make sure pfn exists to start with */
112 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
113 continue;
114 frame = mfn;
115 } else {
116 if (mfn != INVALID_P2M_ENTRY)
117 continue;
118 frame = pfn;
119 }
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400120 set_xen_guest_handle(reservation.extent_start, &frame);
121 reservation.nr_extents = 1;
122
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400123 ret = HYPERVISOR_memory_op(release ? XENMEM_decrease_reservation : XENMEM_populate_physmap,
124 &reservation);
125 WARN(ret != 1, "Failed to %s pfn %lx err=%d\n",
126 release ? "release" : "populate", pfn, ret);
127
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400128 if (ret == 1) {
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400129 if (!early_set_phys_to_machine(pfn, release ? INVALID_P2M_ENTRY : frame)) {
130 if (release)
131 break;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400132 set_xen_guest_handle(reservation.extent_start, &frame);
133 reservation.nr_extents = 1;
134 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400135 &reservation);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400136 break;
137 }
138 len++;
139 } else
140 break;
141 }
142 if (len)
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400143 printk(KERN_INFO "%s %lx-%lx pfn range: %lu pages %s\n",
144 release ? "Freeing" : "Populating",
145 start, end, len,
146 release ? "freed" : "added");
147
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400148 return len;
149}
David Vrabel83d51ab2012-05-03 16:15:42 +0100150
151static unsigned long __init xen_release_chunk(unsigned long start,
152 unsigned long end)
153{
154 return xen_do_chunk(start, end, true);
155}
156
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400157static unsigned long __init xen_populate_chunk(
158 const struct e820entry *list, size_t map_size,
159 unsigned long max_pfn, unsigned long *last_pfn,
160 unsigned long credits_left)
161{
162 const struct e820entry *entry;
163 unsigned int i;
164 unsigned long done = 0;
165 unsigned long dest_pfn;
166
167 for (i = 0, entry = list; i < map_size; i++, entry++) {
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400168 unsigned long s_pfn;
169 unsigned long e_pfn;
170 unsigned long pfns;
171 long capacity;
172
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800173 if (credits_left <= 0)
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400174 break;
175
176 if (entry->type != E820_RAM)
177 continue;
178
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800179 e_pfn = PFN_DOWN(entry->addr + entry->size);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400180
181 /* We only care about E820 after the xen_start_info->nr_pages */
182 if (e_pfn <= max_pfn)
183 continue;
184
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800185 s_pfn = PFN_UP(entry->addr);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400186 /* If the E820 falls within the nr_pages, we want to start
187 * at the nr_pages PFN.
188 * If that would mean going past the E820 entry, skip it
189 */
190 if (s_pfn <= max_pfn) {
191 capacity = e_pfn - max_pfn;
192 dest_pfn = max_pfn;
193 } else {
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400194 capacity = e_pfn - s_pfn;
195 dest_pfn = s_pfn;
196 }
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400197
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800198 if (credits_left < capacity)
199 capacity = credits_left;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400200
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800201 pfns = xen_do_chunk(dest_pfn, dest_pfn + capacity, false);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400202 done += pfns;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400203 *last_pfn = (dest_pfn + pfns);
zhenzhong.duanc3d93f82012-07-18 13:06:39 +0800204 if (pfns < capacity)
205 break;
206 credits_left -= pfns;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400207 }
208 return done;
209}
David Vrabel83d51ab2012-05-03 16:15:42 +0100210
211static void __init xen_set_identity_and_release_chunk(
212 unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
213 unsigned long *released, unsigned long *identity)
214{
215 unsigned long pfn;
216
217 /*
218 * If the PFNs are currently mapped, the VA mapping also needs
219 * to be updated to be 1:1.
220 */
221 for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
222 (void)HYPERVISOR_update_va_mapping(
223 (unsigned long)__va(pfn << PAGE_SHIFT),
224 mfn_pte(pfn, PAGE_KERNEL_IO), 0);
225
226 if (start_pfn < nr_pages)
227 *released += xen_release_chunk(
228 start_pfn, min(end_pfn, nr_pages));
229
230 *identity += set_phys_range_identity(start_pfn, end_pfn);
231}
232
David Vrabelf3f436e2011-09-28 17:46:36 +0100233static unsigned long __init xen_set_identity_and_release(
234 const struct e820entry *list, size_t map_size, unsigned long nr_pages)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400235{
David Vrabelf3f436e2011-09-28 17:46:36 +0100236 phys_addr_t start = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400237 unsigned long released = 0;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500238 unsigned long identity = 0;
David Vrabelf3f436e2011-09-28 17:46:36 +0100239 const struct e820entry *entry;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500240 int i;
241
David Vrabelf3f436e2011-09-28 17:46:36 +0100242 /*
243 * Combine non-RAM regions and gaps until a RAM region (or the
244 * end of the map) is reached, then set the 1:1 map and
245 * release the pages (if available) in those non-RAM regions.
246 *
247 * The combined non-RAM regions are rounded to a whole number
248 * of pages so any partial pages are accessible via the 1:1
249 * mapping. This is needed for some BIOSes that put (for
250 * example) the DMI tables in a reserved region that begins on
251 * a non-page boundary.
252 */
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500253 for (i = 0, entry = list; i < map_size; i++, entry++) {
David Vrabelf3f436e2011-09-28 17:46:36 +0100254 phys_addr_t end = entry->addr + entry->size;
David Vrabelf3f436e2011-09-28 17:46:36 +0100255 if (entry->type == E820_RAM || i == map_size - 1) {
256 unsigned long start_pfn = PFN_DOWN(start);
257 unsigned long end_pfn = PFN_UP(end);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500258
David Vrabelf3f436e2011-09-28 17:46:36 +0100259 if (entry->type == E820_RAM)
260 end_pfn = PFN_UP(entry->addr);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500261
David Vrabel83d51ab2012-05-03 16:15:42 +0100262 if (start_pfn < end_pfn)
263 xen_set_identity_and_release_chunk(
264 start_pfn, end_pfn, nr_pages,
265 &released, &identity);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500266
David Vrabelf3f436e2011-09-28 17:46:36 +0100267 start = end;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500268 }
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500269 }
David Vrabelf3f436e2011-09-28 17:46:36 +0100270
Konrad Rzeszutek Wilkca118232012-03-30 15:37:07 -0400271 if (released)
272 printk(KERN_INFO "Released %lu pages of unused memory\n", released);
273 if (identity)
274 printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
David Vrabelf3f436e2011-09-28 17:46:36 +0100275
276 return released;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500277}
David Vrabeld312ae872011-08-19 15:57:16 +0100278
279static unsigned long __init xen_get_max_pages(void)
280{
281 unsigned long max_pages = MAX_DOMAIN_PAGES;
282 domid_t domid = DOMID_SELF;
283 int ret;
284
Ian Campbelld3db7282011-12-14 12:16:08 +0000285 /*
286 * For the initial domain we use the maximum reservation as
287 * the maximum page.
288 *
289 * For guest domains the current maximum reservation reflects
290 * the current maximum rather than the static maximum. In this
291 * case the e820 map provided to us will cover the static
292 * maximum region.
293 */
294 if (xen_initial_domain()) {
295 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
296 if (ret > 0)
297 max_pages = ret;
298 }
299
David Vrabeld312ae872011-08-19 15:57:16 +0100300 return min(max_pages, MAX_DOMAIN_PAGES);
301}
302
David Vrabeldc91c722011-09-29 12:26:19 +0100303static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
304{
305 u64 end = start + size;
306
307 /* Align RAM regions to page boundaries. */
308 if (type == E820_RAM) {
309 start = PAGE_ALIGN(start);
310 end &= ~((u64)PAGE_SIZE - 1);
311 }
312
313 e820_add_region(start, end - start, type);
314}
315
David Vrabel061cc242013-08-16 15:42:55 +0100316void xen_ignore_unusable(struct e820entry *list, size_t map_size)
317{
318 struct e820entry *entry;
319 unsigned int i;
320
321 for (i = 0, entry = list; i < map_size; i++, entry++) {
322 if (entry->type == E820_UNUSABLE)
323 entry->type = E820_RAM;
324 }
325}
326
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700327/**
328 * machine_specific_memory_setup - Hook for machine specific memory setup.
329 **/
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700330char * __init xen_memory_setup(void)
331{
Ian Campbell35ae11f2009-02-06 19:09:48 -0800332 static struct e820entry map[E820MAX] __initdata;
333
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700334 unsigned long max_pfn = xen_start_info->nr_pages;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800335 unsigned long long mem_end;
336 int rc;
337 struct xen_memory_map memmap;
David Vrabeldc91c722011-09-29 12:26:19 +0100338 unsigned long max_pages;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400339 unsigned long last_pfn = 0;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700340 unsigned long extra_pages = 0;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400341 unsigned long populated;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800342 int i;
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100343 int op;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700344
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100345 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800346 mem_end = PFN_PHYS(max_pfn);
347
348 memmap.nr_entries = E820MAX;
349 set_xen_guest_handle(memmap.buffer, map);
350
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100351 op = xen_initial_domain() ?
352 XENMEM_machine_memory_map :
353 XENMEM_memory_map;
354 rc = HYPERVISOR_memory_op(op, &memmap);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800355 if (rc == -ENOSYS) {
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700356 BUG_ON(xen_initial_domain());
Ian Campbell35ae11f2009-02-06 19:09:48 -0800357 memmap.nr_entries = 1;
358 map[0].addr = 0ULL;
359 map[0].size = mem_end;
360 /* 8MB slack (to balance backend allocations). */
361 map[0].size += 8ULL << 20;
362 map[0].type = E820_RAM;
363 rc = 0;
364 }
365 BUG_ON(rc);
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100366
David Vrabel061cc242013-08-16 15:42:55 +0100367 /*
368 * Xen won't allow a 1:1 mapping to be created to UNUSABLE
369 * regions, so if we're using the machine memory map leave the
370 * region as RAM as it is in the pseudo-physical map.
371 *
372 * UNUSABLE regions in domUs are not handled and will need
373 * a patch in the future.
374 */
375 if (xen_initial_domain())
376 xen_ignore_unusable(map, memmap.nr_entries);
377
David Vrabeldc91c722011-09-29 12:26:19 +0100378 /* Make sure the Xen-supplied memory map is well-ordered. */
379 sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700380
David Vrabeldc91c722011-09-29 12:26:19 +0100381 max_pages = xen_get_max_pages();
382 if (max_pages > max_pfn)
383 extra_pages += max_pages - max_pfn;
Stefano Stabellini7cb31b72011-01-27 10:13:25 -0500384
David Vrabelf3f436e2011-09-28 17:46:36 +0100385 /*
386 * Set P2M for all non-RAM pages and E820 gaps to be identity
387 * type PFNs. Any RAM pages that would be made inaccesible by
388 * this are first released.
389 */
390 xen_released_pages = xen_set_identity_and_release(
391 map, memmap.nr_entries, max_pfn);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700392
David Vrabeldc91c722011-09-29 12:26:19 +0100393 /*
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400394 * Populate back the non-RAM pages and E820 gaps that had been
395 * released. */
396 populated = xen_populate_chunk(map, memmap.nr_entries,
397 max_pfn, &last_pfn, xen_released_pages);
398
Konrad Rzeszutek Wilk58b7b532012-05-29 12:36:43 -0400399 xen_released_pages -= populated;
400 extra_pages += xen_released_pages;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400401
402 if (last_pfn > max_pfn) {
403 max_pfn = min(MAX_DOMAIN_PAGES, last_pfn);
404 mem_end = PFN_PHYS(max_pfn);
405 }
406 /*
David Vrabeldc91c722011-09-29 12:26:19 +0100407 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
408 * factor the base size. On non-highmem systems, the base
409 * size is the full initial memory allocation; on highmem it
410 * is limited to the max size of lowmem, so that it doesn't
411 * get completely filled.
412 *
413 * In principle there could be a problem in lowmem systems if
414 * the initial memory is also very large with respect to
415 * lowmem, but we won't try to deal with that here.
416 */
417 extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
418 extra_pages);
David Vrabeldc91c722011-09-29 12:26:19 +0100419 i = 0;
420 while (i < memmap.nr_entries) {
421 u64 addr = map[i].addr;
422 u64 size = map[i].size;
423 u32 type = map[i].type;
424
425 if (type == E820_RAM) {
426 if (addr < mem_end) {
427 size = min(size, mem_end - addr);
428 } else if (extra_pages) {
429 size = min(size, (u64)extra_pages * PAGE_SIZE);
430 extra_pages -= size / PAGE_SIZE;
431 xen_add_extra_mem(addr, size);
432 } else
433 type = E820_UNUSABLE;
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -0700434 }
435
David Vrabeldc91c722011-09-29 12:26:19 +0100436 xen_align_and_add_e820_region(addr, size, type);
Jeremy Fitzhardingeb5b43ce2010-09-02 17:10:12 -0700437
David Vrabeldc91c722011-09-29 12:26:19 +0100438 map[i].addr += size;
439 map[i].size -= size;
440 if (map[i].size == 0)
441 i++;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800442 }
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700443
444 /*
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700445 * In domU, the ISA region is normal, usable memory, but we
446 * reserve ISA memory anyway because too many things poke
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700447 * about in there.
448 */
449 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
450 E820_RESERVED);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700451
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700452 /*
453 * Reserve Xen bits:
454 * - mfn_list
455 * - xen_start_info
456 * See comment above "struct start_info" in <xen/interface/xen.h>
Konrad Rzeszutek Wilk51faaf22012-08-22 13:00:10 -0400457 * We tried to make the the memblock_reserve more selective so
458 * that it would be clear what region is reserved. Sadly we ran
459 * in the problem wherein on a 64-bit hypervisor with a 32-bit
460 * initial domain, the pt_base has the cr3 value which is not
461 * neccessarily where the pagetable starts! As Jan put it: "
462 * Actually, the adjustment turns out to be correct: The page
463 * tables for a 32-on-64 dom0 get allocated in the order "first L1",
464 * "first L2", "first L3", so the offset to the page table base is
465 * indeed 2. When reading xen/include/public/xen.h's comment
466 * very strictly, this is not a violation (since there nothing is said
467 * that the first thing in the page table space is pointed to by
468 * pt_base; I admit that this seems to be implied though, namely
469 * do I think that it is implied that the page table space is the
470 * range [pt_base, pt_base + nt_pt_frames), whereas that
471 * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames),
472 * which - without a priori knowledge - the kernel would have
473 * difficulty to figure out)." - so lets just fall back to the
474 * easy way and reserve the whole region.
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700475 */
Tejun Heo24aa0782011-07-12 11:16:06 +0200476 memblock_reserve(__pa(xen_start_info->mfn_list),
477 xen_start_info->pt_base - xen_start_info->mfn_list);
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700478
479 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
480
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700481 return "Xen";
482}
483
Roland McGrathd2eea682007-07-20 00:31:43 -0700484/*
485 * Set the bit indicating "nosegneg" library variants should be used.
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700486 * We only need to bother in pure 32-bit mode; compat 32-bit processes
487 * can have un-truncated segments, so wrapping around is allowed.
Roland McGrathd2eea682007-07-20 00:31:43 -0700488 */
Sam Ravnborg08b6d292008-01-30 13:33:25 +0100489static void __init fiddle_vdso(void)
Roland McGrathd2eea682007-07-20 00:31:43 -0700490{
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700491#ifdef CONFIG_X86_32
492 u32 *mask;
493 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
494 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
495 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
Roland McGrathd2eea682007-07-20 00:31:43 -0700496 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700497#endif
Roland McGrathd2eea682007-07-20 00:31:43 -0700498}
499
Daniel Kiperae15a3b2011-05-04 20:17:21 +0200500static int __cpuinit register_callback(unsigned type, const void *func)
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700501{
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700502 struct callback_register callback = {
503 .type = type,
504 .address = XEN_CALLBACK(__KERNEL_CS, func),
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700505 .flags = CALLBACKF_mask_events,
506 };
507
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700508 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
509}
510
511void __cpuinit xen_enable_sysenter(void)
512{
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700513 int ret;
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700514 unsigned sysenter_feature;
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700515
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700516#ifdef CONFIG_X86_32
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700517 sysenter_feature = X86_FEATURE_SEP;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700518#else
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700519 sysenter_feature = X86_FEATURE_SYSENTER32;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700520#endif
521
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700522 if (!boot_cpu_has(sysenter_feature))
523 return;
524
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700525 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700526 if(ret != 0)
527 setup_clear_cpu_cap(sysenter_feature);
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700528}
529
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700530void __cpuinit xen_enable_syscall(void)
531{
532#ifdef CONFIG_X86_64
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700533 int ret;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700534
535 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
536 if (ret != 0) {
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700537 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700538 /* Pretty fatal; 64-bit userspace has no other
539 mechanism for syscalls. */
540 }
541
542 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700543 ret = register_callback(CALLBACKTYPE_syscall32,
544 xen_syscall32_target);
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700545 if (ret != 0)
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700546 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700547 }
548#endif /* CONFIG_X86_64 */
549}
550
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700551void __init xen_arch_setup(void)
552{
Donald Dutilef09f6d12010-07-15 14:56:49 -0400553 xen_panic_handler_init();
554
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700555 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
556 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
557
558 if (!xen_feature(XENFEAT_auto_translated_physmap))
Tejf63c2f22008-12-16 11:56:06 -0800559 HYPERVISOR_vm_assist(VMASST_CMD_enable,
560 VMASST_TYPE_pae_extended_cr3);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700561
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700562 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
563 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
564 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700565
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700566 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700567 xen_enable_syscall();
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700568
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700569#ifdef CONFIG_ACPI
570 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
571 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
572 disable_acpi();
573 }
574#endif
575
576 memcpy(boot_command_line, xen_start_info->cmd_line,
577 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
578 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
579
Jeremy Fitzhardingebc15fde2010-11-22 17:17:50 -0800580 /* Set up idle, making sure it calls safe_halt() pvop */
Len Brownd91ee582011-04-01 18:28:35 -0400581 disable_cpuidle();
Konrad Rzeszutek Wilk48cdd822012-03-13 20:06:57 -0400582 disable_cpufreq();
Len Brown6a377dd2013-02-09 23:08:07 -0500583 WARN_ON(xen_set_default_idle());
Roland McGrathd2eea682007-07-20 00:31:43 -0700584 fiddle_vdso();
Konrad Rzeszutek Wilk8d54db792012-08-17 10:22:37 -0400585#ifdef CONFIG_NUMA
586 numa_off = 1;
587#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700588}