blob: 740517be4da528efeea9c64fa19cb618d926e8f4 [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>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070020#include <asm/xen/hypervisor.h>
21#include <asm/xen/hypercall.h>
22
Ian Campbell45263cb2010-10-25 16:32:29 -070023#include <xen/xen.h>
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010024#include <xen/page.h>
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070025#include <xen/interface/callback.h>
Ian Campbell35ae11f2009-02-06 19:09:48 -080026#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070027#include <xen/interface/physdev.h>
28#include <xen/features.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070029#include "xen-ops.h"
Roland McGrathd2eea682007-07-20 00:31:43 -070030#include "vdso.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070031
32/* These are code, but not functions. Defined in entry.S */
33extern const char xen_hypervisor_callback[];
34extern const char xen_failsafe_callback[];
Tejf63c2f22008-12-16 11:56:06 -080035extern void xen_sysenter_target(void);
36extern void xen_syscall_target(void);
37extern void xen_syscall32_target(void);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070038
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070039/* Amount of extra memory space we add to the e820 ranges */
David Vrabel8b5d44a2011-09-28 17:46:34 +010040struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070041
David Vrabelaa244112011-09-28 17:46:32 +010042/* Number of pages released from the initial allocation. */
43unsigned long xen_released_pages;
44
Jeremy Fitzhardinge698bb8d2010-09-14 10:19:14 -070045/*
46 * The maximum amount of extra memory compared to the base size. The
47 * main scaling factor is the size of struct page. At extreme ratios
48 * of base:extra, all the base memory can be filled with page
49 * structures for the extra memory, leaving no space for anything
50 * else.
51 *
52 * 10x seems like a reasonable balance between scaling flexibility and
53 * leaving a practically usable system.
54 */
55#define EXTRA_MEM_RATIO (10)
56
David Vrabeldc91c722011-09-29 12:26:19 +010057static void __init xen_add_extra_mem(u64 start, u64 size)
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070058{
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050059 unsigned long pfn;
David Vrabeldc91c722011-09-29 12:26:19 +010060 int i;
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050061
David Vrabeldc91c722011-09-29 12:26:19 +010062 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
63 /* Add new region. */
64 if (xen_extra_mem[i].size == 0) {
65 xen_extra_mem[i].start = start;
66 xen_extra_mem[i].size = size;
67 break;
68 }
69 /* Append to existing region. */
70 if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
71 xen_extra_mem[i].size += size;
72 break;
73 }
74 }
75 if (i == XEN_EXTRA_MEM_MAX_REGIONS)
76 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070077
Tejun Heod4bbf7e2011-11-28 09:46:22 -080078 memblock_reserve(start, size);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070079
David Vrabeldc91c722011-09-29 12:26:19 +010080 xen_max_p2m_pfn = PFN_DOWN(start + size);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070081
David Vrabeldc91c722011-09-29 12:26:19 +010082 for (pfn = PFN_DOWN(start); pfn <= xen_max_p2m_pfn; pfn++)
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050083 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070084}
85
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -040086static unsigned long __init xen_do_chunk(unsigned long start,
87 unsigned long end, bool release)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040088{
89 struct xen_memory_reservation reservation = {
90 .address_bits = 0,
91 .extent_order = 0,
92 .domid = DOMID_SELF
93 };
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070094 unsigned long len = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040095 unsigned long pfn;
96 int ret;
97
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -040098 for (pfn = start; pfn < end; pfn++) {
99 unsigned long frame;
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400100 unsigned long mfn = pfn_to_mfn(pfn);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400101
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400102 if (release) {
103 /* Make sure pfn exists to start with */
104 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
105 continue;
106 frame = mfn;
107 } else {
108 if (mfn != INVALID_P2M_ENTRY)
109 continue;
110 frame = pfn;
111 }
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400112 set_xen_guest_handle(reservation.extent_start, &frame);
113 reservation.nr_extents = 1;
114
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400115 ret = HYPERVISOR_memory_op(release ? XENMEM_decrease_reservation : XENMEM_populate_physmap,
116 &reservation);
117 WARN(ret != 1, "Failed to %s pfn %lx err=%d\n",
118 release ? "release" : "populate", pfn, ret);
119
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400120 if (ret == 1) {
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400121 if (!early_set_phys_to_machine(pfn, release ? INVALID_P2M_ENTRY : frame)) {
122 if (release)
123 break;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400124 set_xen_guest_handle(reservation.extent_start, &frame);
125 reservation.nr_extents = 1;
126 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400127 &reservation);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400128 break;
129 }
130 len++;
131 } else
132 break;
133 }
134 if (len)
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400135 printk(KERN_INFO "%s %lx-%lx pfn range: %lu pages %s\n",
136 release ? "Freeing" : "Populating",
137 start, end, len,
138 release ? "freed" : "added");
139
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400140 return len;
141}
David Vrabel83d51ab2012-05-03 16:15:42 +0100142
143static unsigned long __init xen_release_chunk(unsigned long start,
144 unsigned long end)
145{
146 return xen_do_chunk(start, end, true);
147}
148
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400149static unsigned long __init xen_populate_chunk(
150 const struct e820entry *list, size_t map_size,
151 unsigned long max_pfn, unsigned long *last_pfn,
152 unsigned long credits_left)
153{
154 const struct e820entry *entry;
155 unsigned int i;
156 unsigned long done = 0;
157 unsigned long dest_pfn;
158
159 for (i = 0, entry = list; i < map_size; i++, entry++) {
160 unsigned long credits = credits_left;
161 unsigned long s_pfn;
162 unsigned long e_pfn;
163 unsigned long pfns;
164 long capacity;
165
166 if (credits <= 0)
167 break;
168
169 if (entry->type != E820_RAM)
170 continue;
171
172 e_pfn = PFN_UP(entry->addr + entry->size);
173
174 /* We only care about E820 after the xen_start_info->nr_pages */
175 if (e_pfn <= max_pfn)
176 continue;
177
178 s_pfn = PFN_DOWN(entry->addr);
179 /* If the E820 falls within the nr_pages, we want to start
180 * at the nr_pages PFN.
181 * If that would mean going past the E820 entry, skip it
182 */
183 if (s_pfn <= max_pfn) {
184 capacity = e_pfn - max_pfn;
185 dest_pfn = max_pfn;
186 } else {
187 /* last_pfn MUST be within E820_RAM regions */
188 if (*last_pfn && e_pfn >= *last_pfn)
189 s_pfn = *last_pfn;
190 capacity = e_pfn - s_pfn;
191 dest_pfn = s_pfn;
192 }
193 /* If we had filled this E820_RAM entry, go to the next one. */
194 if (capacity <= 0)
195 continue;
196
197 if (credits > capacity)
198 credits = capacity;
199
Konrad Rzeszutek Wilk96dc08b2012-04-06 16:10:20 -0400200 pfns = xen_do_chunk(dest_pfn, dest_pfn + credits, false);
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400201 done += pfns;
202 credits_left -= pfns;
203 *last_pfn = (dest_pfn + pfns);
204 }
205 return done;
206}
David Vrabel83d51ab2012-05-03 16:15:42 +0100207
208static void __init xen_set_identity_and_release_chunk(
209 unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
210 unsigned long *released, unsigned long *identity)
211{
212 unsigned long pfn;
213
214 /*
215 * If the PFNs are currently mapped, the VA mapping also needs
216 * to be updated to be 1:1.
217 */
218 for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
219 (void)HYPERVISOR_update_va_mapping(
220 (unsigned long)__va(pfn << PAGE_SHIFT),
221 mfn_pte(pfn, PAGE_KERNEL_IO), 0);
222
223 if (start_pfn < nr_pages)
224 *released += xen_release_chunk(
225 start_pfn, min(end_pfn, nr_pages));
226
227 *identity += set_phys_range_identity(start_pfn, end_pfn);
228}
229
David Vrabelf3f436e2011-09-28 17:46:36 +0100230static unsigned long __init xen_set_identity_and_release(
231 const struct e820entry *list, size_t map_size, unsigned long nr_pages)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400232{
David Vrabelf3f436e2011-09-28 17:46:36 +0100233 phys_addr_t start = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400234 unsigned long released = 0;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500235 unsigned long identity = 0;
David Vrabelf3f436e2011-09-28 17:46:36 +0100236 const struct e820entry *entry;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500237 int i;
238
David Vrabelf3f436e2011-09-28 17:46:36 +0100239 /*
240 * Combine non-RAM regions and gaps until a RAM region (or the
241 * end of the map) is reached, then set the 1:1 map and
242 * release the pages (if available) in those non-RAM regions.
243 *
244 * The combined non-RAM regions are rounded to a whole number
245 * of pages so any partial pages are accessible via the 1:1
246 * mapping. This is needed for some BIOSes that put (for
247 * example) the DMI tables in a reserved region that begins on
248 * a non-page boundary.
249 */
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500250 for (i = 0, entry = list; i < map_size; i++, entry++) {
David Vrabelf3f436e2011-09-28 17:46:36 +0100251 phys_addr_t end = entry->addr + entry->size;
David Vrabelf3f436e2011-09-28 17:46:36 +0100252 if (entry->type == E820_RAM || i == map_size - 1) {
253 unsigned long start_pfn = PFN_DOWN(start);
254 unsigned long end_pfn = PFN_UP(end);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500255
David Vrabelf3f436e2011-09-28 17:46:36 +0100256 if (entry->type == E820_RAM)
257 end_pfn = PFN_UP(entry->addr);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500258
David Vrabel83d51ab2012-05-03 16:15:42 +0100259 if (start_pfn < end_pfn)
260 xen_set_identity_and_release_chunk(
261 start_pfn, end_pfn, nr_pages,
262 &released, &identity);
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500263
David Vrabelf3f436e2011-09-28 17:46:36 +0100264 start = end;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500265 }
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500266 }
David Vrabelf3f436e2011-09-28 17:46:36 +0100267
Konrad Rzeszutek Wilkca118232012-03-30 15:37:07 -0400268 if (released)
269 printk(KERN_INFO "Released %lu pages of unused memory\n", released);
270 if (identity)
271 printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
David Vrabelf3f436e2011-09-28 17:46:36 +0100272
273 return released;
Konrad Rzeszutek Wilk68df0da2011-02-01 17:15:30 -0500274}
David Vrabeld312ae872011-08-19 15:57:16 +0100275
276static unsigned long __init xen_get_max_pages(void)
277{
278 unsigned long max_pages = MAX_DOMAIN_PAGES;
279 domid_t domid = DOMID_SELF;
280 int ret;
281
Ian Campbelld3db7282011-12-14 12:16:08 +0000282 /*
283 * For the initial domain we use the maximum reservation as
284 * the maximum page.
285 *
286 * For guest domains the current maximum reservation reflects
287 * the current maximum rather than the static maximum. In this
288 * case the e820 map provided to us will cover the static
289 * maximum region.
290 */
291 if (xen_initial_domain()) {
292 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
293 if (ret > 0)
294 max_pages = ret;
295 }
296
David Vrabeld312ae872011-08-19 15:57:16 +0100297 return min(max_pages, MAX_DOMAIN_PAGES);
298}
299
David Vrabeldc91c722011-09-29 12:26:19 +0100300static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
301{
302 u64 end = start + size;
303
304 /* Align RAM regions to page boundaries. */
305 if (type == E820_RAM) {
306 start = PAGE_ALIGN(start);
307 end &= ~((u64)PAGE_SIZE - 1);
308 }
309
310 e820_add_region(start, end - start, type);
311}
312
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700313/**
314 * machine_specific_memory_setup - Hook for machine specific memory setup.
315 **/
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700316char * __init xen_memory_setup(void)
317{
Ian Campbell35ae11f2009-02-06 19:09:48 -0800318 static struct e820entry map[E820MAX] __initdata;
319
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700320 unsigned long max_pfn = xen_start_info->nr_pages;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800321 unsigned long long mem_end;
322 int rc;
323 struct xen_memory_map memmap;
David Vrabeldc91c722011-09-29 12:26:19 +0100324 unsigned long max_pages;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400325 unsigned long last_pfn = 0;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700326 unsigned long extra_pages = 0;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400327 unsigned long populated;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800328 int i;
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100329 int op;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700330
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100331 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800332 mem_end = PFN_PHYS(max_pfn);
333
334 memmap.nr_entries = E820MAX;
335 set_xen_guest_handle(memmap.buffer, map);
336
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100337 op = xen_initial_domain() ?
338 XENMEM_machine_memory_map :
339 XENMEM_memory_map;
340 rc = HYPERVISOR_memory_op(op, &memmap);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800341 if (rc == -ENOSYS) {
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700342 BUG_ON(xen_initial_domain());
Ian Campbell35ae11f2009-02-06 19:09:48 -0800343 memmap.nr_entries = 1;
344 map[0].addr = 0ULL;
345 map[0].size = mem_end;
346 /* 8MB slack (to balance backend allocations). */
347 map[0].size += 8ULL << 20;
348 map[0].type = E820_RAM;
349 rc = 0;
350 }
351 BUG_ON(rc);
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100352
David Vrabeldc91c722011-09-29 12:26:19 +0100353 /* Make sure the Xen-supplied memory map is well-ordered. */
354 sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700355
David Vrabeldc91c722011-09-29 12:26:19 +0100356 max_pages = xen_get_max_pages();
357 if (max_pages > max_pfn)
358 extra_pages += max_pages - max_pfn;
Stefano Stabellini7cb31b72011-01-27 10:13:25 -0500359
David Vrabelf3f436e2011-09-28 17:46:36 +0100360 /*
361 * Set P2M for all non-RAM pages and E820 gaps to be identity
362 * type PFNs. Any RAM pages that would be made inaccesible by
363 * this are first released.
364 */
365 xen_released_pages = xen_set_identity_and_release(
366 map, memmap.nr_entries, max_pfn);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700367
David Vrabeldc91c722011-09-29 12:26:19 +0100368 /*
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400369 * Populate back the non-RAM pages and E820 gaps that had been
370 * released. */
371 populated = xen_populate_chunk(map, memmap.nr_entries,
372 max_pfn, &last_pfn, xen_released_pages);
373
Konrad Rzeszutek Wilk58b7b532012-05-29 12:36:43 -0400374 xen_released_pages -= populated;
375 extra_pages += xen_released_pages;
Konrad Rzeszutek Wilk2e2fb752012-04-06 10:07:11 -0400376
377 if (last_pfn > max_pfn) {
378 max_pfn = min(MAX_DOMAIN_PAGES, last_pfn);
379 mem_end = PFN_PHYS(max_pfn);
380 }
381 /*
David Vrabeldc91c722011-09-29 12:26:19 +0100382 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
383 * factor the base size. On non-highmem systems, the base
384 * size is the full initial memory allocation; on highmem it
385 * is limited to the max size of lowmem, so that it doesn't
386 * get completely filled.
387 *
388 * In principle there could be a problem in lowmem systems if
389 * the initial memory is also very large with respect to
390 * lowmem, but we won't try to deal with that here.
391 */
392 extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
393 extra_pages);
David Vrabeldc91c722011-09-29 12:26:19 +0100394 i = 0;
395 while (i < memmap.nr_entries) {
396 u64 addr = map[i].addr;
397 u64 size = map[i].size;
398 u32 type = map[i].type;
399
400 if (type == E820_RAM) {
401 if (addr < mem_end) {
402 size = min(size, mem_end - addr);
403 } else if (extra_pages) {
404 size = min(size, (u64)extra_pages * PAGE_SIZE);
405 extra_pages -= size / PAGE_SIZE;
406 xen_add_extra_mem(addr, size);
407 } else
408 type = E820_UNUSABLE;
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -0700409 }
410
David Vrabeldc91c722011-09-29 12:26:19 +0100411 xen_align_and_add_e820_region(addr, size, type);
Jeremy Fitzhardingeb5b43ce2010-09-02 17:10:12 -0700412
David Vrabeldc91c722011-09-29 12:26:19 +0100413 map[i].addr += size;
414 map[i].size -= size;
415 if (map[i].size == 0)
416 i++;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800417 }
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700418
419 /*
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700420 * In domU, the ISA region is normal, usable memory, but we
421 * reserve ISA memory anyway because too many things poke
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700422 * about in there.
423 */
424 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
425 E820_RESERVED);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700426
Konrad Rzeszutek Wilk51faaf22012-08-22 13:00:10 -0400427 /*
428 * Reserve Xen bits:
429 * - mfn_list
430 * - xen_start_info
431 * See comment above "struct start_info" in <xen/interface/xen.h>
432 * We tried to make the the memblock_reserve more selective so
433 * that it would be clear what region is reserved. Sadly we ran
434 * in the problem wherein on a 64-bit hypervisor with a 32-bit
435 * initial domain, the pt_base has the cr3 value which is not
436 * neccessarily where the pagetable starts! As Jan put it: "
437 * Actually, the adjustment turns out to be correct: The page
438 * tables for a 32-on-64 dom0 get allocated in the order "first L1",
439 * "first L2", "first L3", so the offset to the page table base is
440 * indeed 2. When reading xen/include/public/xen.h's comment
441 * very strictly, this is not a violation (since there nothing is said
442 * that the first thing in the page table space is pointed to by
443 * pt_base; I admit that this seems to be implied though, namely
444 * do I think that it is implied that the page table space is the
445 * range [pt_base, pt_base + nt_pt_frames), whereas that
446 * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames),
447 * which - without a priori knowledge - the kernel would have
448 * difficulty to figure out)." - so lets just fall back to the
449 * easy way and reserve the whole region.
450 */
451 memblock_reserve(__pa(xen_start_info->mfn_list),
452 xen_start_info->pt_base - xen_start_info->mfn_list);
453
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700454 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
455
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700456 return "Xen";
457}
458
Roland McGrathd2eea682007-07-20 00:31:43 -0700459/*
460 * Set the bit indicating "nosegneg" library variants should be used.
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700461 * We only need to bother in pure 32-bit mode; compat 32-bit processes
462 * can have un-truncated segments, so wrapping around is allowed.
Roland McGrathd2eea682007-07-20 00:31:43 -0700463 */
Sam Ravnborg08b6d292008-01-30 13:33:25 +0100464static void __init fiddle_vdso(void)
Roland McGrathd2eea682007-07-20 00:31:43 -0700465{
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700466#ifdef CONFIG_X86_32
467 u32 *mask;
468 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
469 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
470 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
Roland McGrathd2eea682007-07-20 00:31:43 -0700471 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700472#endif
Roland McGrathd2eea682007-07-20 00:31:43 -0700473}
474
Daniel Kiperae15a3b2011-05-04 20:17:21 +0200475static int __cpuinit register_callback(unsigned type, const void *func)
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700476{
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700477 struct callback_register callback = {
478 .type = type,
479 .address = XEN_CALLBACK(__KERNEL_CS, func),
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700480 .flags = CALLBACKF_mask_events,
481 };
482
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700483 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
484}
485
486void __cpuinit xen_enable_sysenter(void)
487{
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700488 int ret;
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700489 unsigned sysenter_feature;
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700490
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700491#ifdef CONFIG_X86_32
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700492 sysenter_feature = X86_FEATURE_SEP;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700493#else
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700494 sysenter_feature = X86_FEATURE_SYSENTER32;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700495#endif
496
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700497 if (!boot_cpu_has(sysenter_feature))
498 return;
499
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700500 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700501 if(ret != 0)
502 setup_clear_cpu_cap(sysenter_feature);
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700503}
504
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700505void __cpuinit xen_enable_syscall(void)
506{
507#ifdef CONFIG_X86_64
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700508 int ret;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700509
510 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
511 if (ret != 0) {
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700512 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700513 /* Pretty fatal; 64-bit userspace has no other
514 mechanism for syscalls. */
515 }
516
517 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700518 ret = register_callback(CALLBACKTYPE_syscall32,
519 xen_syscall32_target);
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700520 if (ret != 0)
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700521 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700522 }
523#endif /* CONFIG_X86_64 */
524}
525
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700526void __init xen_arch_setup(void)
527{
Donald Dutilef09f6d12010-07-15 14:56:49 -0400528 xen_panic_handler_init();
529
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700530 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
531 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
532
533 if (!xen_feature(XENFEAT_auto_translated_physmap))
Tejf63c2f22008-12-16 11:56:06 -0800534 HYPERVISOR_vm_assist(VMASST_CMD_enable,
535 VMASST_TYPE_pae_extended_cr3);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700536
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700537 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
538 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
539 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700540
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700541 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700542 xen_enable_syscall();
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700543
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700544#ifdef CONFIG_ACPI
545 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
546 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
547 disable_acpi();
548 }
549#endif
550
551 memcpy(boot_command_line, xen_start_info->cmd_line,
552 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
553 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
554
Jeremy Fitzhardingebc15fde2010-11-22 17:17:50 -0800555 /* Set up idle, making sure it calls safe_halt() pvop */
556#ifdef CONFIG_X86_32
557 boot_cpu_data.hlt_works_ok = 1;
558#endif
Len Brownd91ee582011-04-01 18:28:35 -0400559 disable_cpuidle();
Konrad Rzeszutek Wilk48cdd822012-03-13 20:06:57 -0400560 disable_cpufreq();
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500561 WARN_ON(set_pm_idle_to_default());
Roland McGrathd2eea682007-07-20 00:31:43 -0700562 fiddle_vdso();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700563}