2 * Machine specific setup for xen
4 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
7 #include <linux/module.h>
8 #include <linux/sched.h>
11 #include <linux/memblock.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpufreq.h>
18 #include <asm/setup.h>
21 #include <asm/xen/hypervisor.h>
22 #include <asm/xen/hypercall.h>
26 #include <xen/interface/callback.h>
27 #include <xen/interface/memory.h>
28 #include <xen/interface/physdev.h>
29 #include <xen/features.h>
33 /* These are code, but not functions. Defined in entry.S */
34 extern const char xen_hypervisor_callback[];
35 extern const char xen_failsafe_callback[];
36 extern void xen_sysenter_target(void);
37 extern void xen_syscall_target(void);
38 extern void xen_syscall32_target(void);
40 /* Amount of extra memory space we add to the e820 ranges */
41 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
43 /* Number of pages released from the initial allocation. */
44 unsigned long xen_released_pages;
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
53 * 10x seems like a reasonable balance between scaling flexibility and
54 * leaving a practically usable system.
56 #define EXTRA_MEM_RATIO (10)
58 static void __init xen_add_extra_mem(u64 start, u64 size)
63 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
65 if (xen_extra_mem[i].size == 0) {
66 xen_extra_mem[i].start = start;
67 xen_extra_mem[i].size = size;
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;
76 if (i == XEN_EXTRA_MEM_MAX_REGIONS)
77 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
79 memblock_reserve(start, size);
81 xen_max_p2m_pfn = PFN_DOWN(start + size);
82 for (pfn = PFN_DOWN(start); pfn < xen_max_p2m_pfn; pfn++) {
83 unsigned long mfn = pfn_to_mfn(pfn);
85 if (WARN(mfn == pfn, "Trying to over-write 1-1 mapping (pfn: %lx)\n", pfn))
87 WARN(mfn != INVALID_P2M_ENTRY, "Trying to remove %lx which has %lx mfn!\n",
90 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
94 static unsigned long __init xen_do_chunk(unsigned long start,
95 unsigned long end, bool release)
97 struct xen_memory_reservation reservation = {
102 unsigned long len = 0;
106 for (pfn = start; pfn < end; pfn++) {
108 unsigned long mfn = pfn_to_mfn(pfn);
111 /* Make sure pfn exists to start with */
112 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
116 if (mfn != INVALID_P2M_ENTRY)
120 set_xen_guest_handle(reservation.extent_start, &frame);
121 reservation.nr_extents = 1;
123 ret = HYPERVISOR_memory_op(release ? XENMEM_decrease_reservation : XENMEM_populate_physmap,
125 WARN(ret != 1, "Failed to %s pfn %lx err=%d\n",
126 release ? "release" : "populate", pfn, ret);
129 if (!early_set_phys_to_machine(pfn, release ? INVALID_P2M_ENTRY : frame)) {
132 set_xen_guest_handle(reservation.extent_start, &frame);
133 reservation.nr_extents = 1;
134 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
143 printk(KERN_INFO "%s %lx-%lx pfn range: %lu pages %s\n",
144 release ? "Freeing" : "Populating",
146 release ? "freed" : "added");
151 static unsigned long __init xen_release_chunk(unsigned long start,
154 return xen_do_chunk(start, end, true);
157 static 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)
162 const struct e820entry *entry;
164 unsigned long done = 0;
165 unsigned long dest_pfn;
167 for (i = 0, entry = list; i < map_size; i++, entry++) {
173 if (credits_left <= 0)
176 if (entry->type != E820_RAM)
179 e_pfn = PFN_DOWN(entry->addr + entry->size);
181 /* We only care about E820 after the xen_start_info->nr_pages */
182 if (e_pfn <= max_pfn)
185 s_pfn = PFN_UP(entry->addr);
186 /* 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
190 if (s_pfn <= max_pfn) {
191 capacity = e_pfn - max_pfn;
194 capacity = e_pfn - s_pfn;
198 if (credits_left < capacity)
199 capacity = credits_left;
201 pfns = xen_do_chunk(dest_pfn, dest_pfn + capacity, false);
203 *last_pfn = (dest_pfn + pfns);
206 credits_left -= pfns;
211 static 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)
218 * If the PFNs are currently mapped, the VA mapping also needs
219 * to be updated to be 1:1.
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);
226 if (start_pfn < nr_pages)
227 *released += xen_release_chunk(
228 start_pfn, min(end_pfn, nr_pages));
230 *identity += set_phys_range_identity(start_pfn, end_pfn);
233 static unsigned long __init xen_set_identity_and_release(
234 const struct e820entry *list, size_t map_size, unsigned long nr_pages)
236 phys_addr_t start = 0;
237 unsigned long released = 0;
238 unsigned long identity = 0;
239 const struct e820entry *entry;
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.
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.
253 for (i = 0, entry = list; i < map_size; i++, entry++) {
254 phys_addr_t end = entry->addr + entry->size;
255 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);
259 if (entry->type == E820_RAM)
260 end_pfn = PFN_UP(entry->addr);
262 if (start_pfn < end_pfn)
263 xen_set_identity_and_release_chunk(
264 start_pfn, end_pfn, nr_pages,
265 &released, &identity);
272 printk(KERN_INFO "Released %lu pages of unused memory\n", released);
274 printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
279 static unsigned long __init xen_get_max_pages(void)
281 unsigned long max_pages = MAX_DOMAIN_PAGES;
282 domid_t domid = DOMID_SELF;
286 * For the initial domain we use the maximum reservation as
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
294 if (xen_initial_domain()) {
295 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
300 return min(max_pages, MAX_DOMAIN_PAGES);
303 static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
305 u64 end = start + size;
307 /* Align RAM regions to page boundaries. */
308 if (type == E820_RAM) {
309 start = PAGE_ALIGN(start);
310 end &= ~((u64)PAGE_SIZE - 1);
313 e820_add_region(start, end - start, type);
316 void xen_ignore_unusable(struct e820entry *list, size_t map_size)
318 struct e820entry *entry;
321 for (i = 0, entry = list; i < map_size; i++, entry++) {
322 if (entry->type == E820_UNUSABLE)
323 entry->type = E820_RAM;
328 * machine_specific_memory_setup - Hook for machine specific memory setup.
330 char * __init xen_memory_setup(void)
332 static struct e820entry map[E820MAX] __initdata;
334 unsigned long max_pfn = xen_start_info->nr_pages;
335 unsigned long long mem_end;
337 struct xen_memory_map memmap;
338 unsigned long max_pages;
339 unsigned long last_pfn = 0;
340 unsigned long extra_pages = 0;
341 unsigned long populated;
345 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
346 mem_end = PFN_PHYS(max_pfn);
348 memmap.nr_entries = E820MAX;
349 set_xen_guest_handle(memmap.buffer, map);
351 op = xen_initial_domain() ?
352 XENMEM_machine_memory_map :
354 rc = HYPERVISOR_memory_op(op, &memmap);
356 BUG_ON(xen_initial_domain());
357 memmap.nr_entries = 1;
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;
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.
372 * UNUSABLE regions in domUs are not handled and will need
373 * a patch in the future.
375 if (xen_initial_domain())
376 xen_ignore_unusable(map, memmap.nr_entries);
378 /* Make sure the Xen-supplied memory map is well-ordered. */
379 sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
381 max_pages = xen_get_max_pages();
382 if (max_pages > max_pfn)
383 extra_pages += max_pages - max_pfn;
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.
390 xen_released_pages = xen_set_identity_and_release(
391 map, memmap.nr_entries, max_pfn);
394 * Populate back the non-RAM pages and E820 gaps that had been
396 populated = xen_populate_chunk(map, memmap.nr_entries,
397 max_pfn, &last_pfn, xen_released_pages);
399 xen_released_pages -= populated;
400 extra_pages += xen_released_pages;
402 if (last_pfn > max_pfn) {
403 max_pfn = min(MAX_DOMAIN_PAGES, last_pfn);
404 mem_end = PFN_PHYS(max_pfn);
407 * 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.
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.
417 extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
420 while (i < memmap.nr_entries) {
421 u64 addr = map[i].addr;
422 u64 size = map[i].size;
423 u32 type = map[i].type;
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);
433 type = E820_UNUSABLE;
436 xen_align_and_add_e820_region(addr, size, type);
440 if (map[i].size == 0)
445 * In domU, the ISA region is normal, usable memory, but we
446 * reserve ISA memory anyway because too many things poke
449 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
456 * See comment above "struct start_info" in <xen/interface/xen.h>
457 * 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.
476 memblock_reserve(__pa(xen_start_info->mfn_list),
477 xen_start_info->pt_base - xen_start_info->mfn_list);
479 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
485 * Set the bit indicating "nosegneg" library variants should be used.
486 * 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.
489 static void __init fiddle_vdso(void)
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);
496 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
500 static int register_callback(unsigned type, const void *func)
502 struct callback_register callback = {
504 .address = XEN_CALLBACK(__KERNEL_CS, func),
505 .flags = CALLBACKF_mask_events,
508 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
511 void xen_enable_sysenter(void)
514 unsigned sysenter_feature;
517 sysenter_feature = X86_FEATURE_SEP;
519 sysenter_feature = X86_FEATURE_SYSENTER32;
522 if (!boot_cpu_has(sysenter_feature))
525 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
527 setup_clear_cpu_cap(sysenter_feature);
530 void xen_enable_syscall(void)
535 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
537 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
538 /* Pretty fatal; 64-bit userspace has no other
539 mechanism for syscalls. */
542 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
543 ret = register_callback(CALLBACKTYPE_syscall32,
544 xen_syscall32_target);
546 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
548 #endif /* CONFIG_X86_64 */
551 void __init xen_arch_setup(void)
553 xen_panic_handler_init();
555 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
556 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
558 if (!xen_feature(XENFEAT_auto_translated_physmap))
559 HYPERVISOR_vm_assist(VMASST_CMD_enable,
560 VMASST_TYPE_pae_extended_cr3);
562 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
563 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
566 xen_enable_sysenter();
567 xen_enable_syscall();
570 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
571 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
576 memcpy(boot_command_line, xen_start_info->cmd_line,
577 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
578 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
580 /* Set up idle, making sure it calls safe_halt() pvop */
583 WARN_ON(xen_set_default_idle());