blob: 95fb68a8c20d5260b1f20f4ad8f3b91148d917fe [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>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070012
13#include <asm/elf.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010014#include <asm/vdso.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070015#include <asm/e820.h>
16#include <asm/setup.h>
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -070017#include <asm/acpi.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070018#include <asm/xen/hypervisor.h>
19#include <asm/xen/hypercall.h>
20
Ian Campbell45263cb2010-10-25 16:32:29 -070021#include <xen/xen.h>
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010022#include <xen/page.h>
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070023#include <xen/interface/callback.h>
Ian Campbell35ae11f2009-02-06 19:09:48 -080024#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070025#include <xen/interface/physdev.h>
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040026#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070027#include <xen/features.h>
28
29#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 */
40phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
41
Jeremy Fitzhardinge698bb8d2010-09-14 10:19:14 -070042/*
43 * The maximum amount of extra memory compared to the base size. The
44 * main scaling factor is the size of struct page. At extreme ratios
45 * of base:extra, all the base memory can be filled with page
46 * structures for the extra memory, leaving no space for anything
47 * else.
48 *
49 * 10x seems like a reasonable balance between scaling flexibility and
50 * leaving a practically usable system.
51 */
52#define EXTRA_MEM_RATIO (10)
53
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070054static __init void xen_add_extra_mem(unsigned long pages)
55{
56 u64 size = (u64)pages * PAGE_SIZE;
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -070057 u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070058
59 if (!pages)
60 return;
61
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -070062 e820_add_region(extra_start, size, E820_RAM);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070063 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
64
Linus Torvalds520045d2010-10-26 18:20:19 -070065 memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA");
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070066
67 xen_extra_mem_size += size;
Jeremy Fitzhardinge2f7acb22010-09-15 13:32:49 -070068
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -070069 xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070070}
71
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070072static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
73 phys_addr_t end_addr)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040074{
75 struct xen_memory_reservation reservation = {
76 .address_bits = 0,
77 .extent_order = 0,
78 .domid = DOMID_SELF
79 };
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040080 unsigned long start, end;
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070081 unsigned long len = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040082 unsigned long pfn;
83 int ret;
84
85 start = PFN_UP(start_addr);
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070086 end = PFN_DOWN(end_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040087
88 if (end <= start)
89 return 0;
90
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070091 printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
92 start, end);
93 for(pfn = start; pfn < end; pfn++) {
94 unsigned long mfn = pfn_to_mfn(pfn);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040095
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070096 /* Make sure pfn exists to start with */
97 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
98 continue;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040099
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700100 set_xen_guest_handle(reservation.extent_start, &mfn);
101 reservation.nr_extents = 1;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400102
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700103 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
104 &reservation);
105 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
106 start, end, ret);
107 if (ret == 1) {
108 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
109 len++;
110 }
111 }
112 printk(KERN_CONT "%ld pages freed\n", len);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400113
114 return len;
115}
116
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700117static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
118 const struct e820map *e820)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400119{
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700120 phys_addr_t max_addr = PFN_PHYS(max_pfn);
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700121 phys_addr_t last_end = ISA_END_ADDRESS;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400122 unsigned long released = 0;
123 int i;
124
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700125 /* Free any unused memory above the low 1Mbyte. */
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700126 for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
127 phys_addr_t end = e820->map[i].addr;
128 end = min(max_addr, end);
129
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700130 if (last_end < end)
131 released += xen_release_chunk(last_end, end);
132 last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400133 }
134
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700135 if (last_end < max_addr)
136 released += xen_release_chunk(last_end, max_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400137
138 printk(KERN_INFO "released %ld pages of unused memory\n", released);
139 return released;
140}
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700141
142/**
143 * machine_specific_memory_setup - Hook for machine specific memory setup.
144 **/
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700145char * __init xen_memory_setup(void)
146{
Ian Campbell35ae11f2009-02-06 19:09:48 -0800147 static struct e820entry map[E820MAX] __initdata;
148
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700149 unsigned long max_pfn = xen_start_info->nr_pages;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800150 unsigned long long mem_end;
151 int rc;
152 struct xen_memory_map memmap;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700153 unsigned long extra_pages = 0;
Jeremy Fitzhardinge698bb8d2010-09-14 10:19:14 -0700154 unsigned long extra_limit;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800155 int i;
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100156 int op;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700157
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100158 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800159 mem_end = PFN_PHYS(max_pfn);
160
161 memmap.nr_entries = E820MAX;
162 set_xen_guest_handle(memmap.buffer, map);
163
Ian Campbell9e9a5fc2010-09-02 16:16:00 +0100164 op = xen_initial_domain() ?
165 XENMEM_machine_memory_map :
166 XENMEM_memory_map;
167 rc = HYPERVISOR_memory_op(op, &memmap);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800168 if (rc == -ENOSYS) {
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700169 BUG_ON(xen_initial_domain());
Ian Campbell35ae11f2009-02-06 19:09:48 -0800170 memmap.nr_entries = 1;
171 map[0].addr = 0ULL;
172 map[0].size = mem_end;
173 /* 8MB slack (to balance backend allocations). */
174 map[0].size += 8ULL << 20;
175 map[0].type = E820_RAM;
176 rc = 0;
177 }
178 BUG_ON(rc);
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100179
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700180 e820.nr_map = 0;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700181 xen_extra_mem_start = mem_end;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800182 for (i = 0; i < memmap.nr_entries; i++) {
183 unsigned long long end = map[i].addr + map[i].size;
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700184
Jeremy Fitzhardingec2d08792010-11-22 16:31:35 -0800185 if (map[i].type == E820_RAM && end > mem_end) {
186 /* RAM off the end - may be partially included */
187 u64 delta = min(map[i].size, end - mem_end);
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700188
Jeremy Fitzhardingec2d08792010-11-22 16:31:35 -0800189 map[i].size -= delta;
190 end -= delta;
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -0700191
Jeremy Fitzhardingec2d08792010-11-22 16:31:35 -0800192 extra_pages += PFN_DOWN(delta);
Jeremy Fitzhardinge36545812010-09-29 16:54:33 -0700193 }
194
Jeremy Fitzhardingec2d08792010-11-22 16:31:35 -0800195 if (map[i].size > 0 && end > xen_extra_mem_start)
Jeremy Fitzhardinge36bc2512010-09-02 17:07:03 -0700196 xen_extra_mem_start = end;
Jeremy Fitzhardingeb5b43ce2010-09-02 17:10:12 -0700197
Jeremy Fitzhardingec2d08792010-11-22 16:31:35 -0800198 /* Add region if any remains */
199 if (map[i].size > 0)
Ian Campbell35ae11f2009-02-06 19:09:48 -0800200 e820_add_region(map[i].addr, map[i].size, map[i].type);
201 }
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700202
203 /*
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700204 * In domU, the ISA region is normal, usable memory, but we
205 * reserve ISA memory anyway because too many things poke
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700206 * about in there.
Juan Quintela4ec53872010-09-02 15:45:43 +0100207 *
Ian Campbell9ec23a7f2010-10-28 11:32:29 -0700208 * In Dom0, the host E820 information can leave gaps in the
209 * ISA range, which would cause us to release those pages. To
210 * avoid this, we unconditionally reserve them here.
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700211 */
212 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
213 E820_RESERVED);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700214
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700215 /*
216 * Reserve Xen bits:
217 * - mfn_list
218 * - xen_start_info
219 * See comment above "struct start_info" in <xen/interface/xen.h>
220 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700221 memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
Jeremy Fitzhardinge6b2e8522009-05-07 11:56:49 -0700222 __pa(xen_start_info->pt_base),
223 "XEN START INFO");
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700224
225 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
226
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700227 extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
228
Jeremy Fitzhardinge698bb8d2010-09-14 10:19:14 -0700229 /*
230 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
231 * factor the base size. On non-highmem systems, the base
232 * size is the full initial memory allocation; on highmem it
233 * is limited to the max size of lowmem, so that it doesn't
234 * get completely filled.
235 *
236 * In principle there could be a problem in lowmem systems if
237 * the initial memory is also very large with respect to
238 * lowmem, but we won't try to deal with that here.
239 */
240 extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
241 max_pfn + extra_pages);
242
243 if (extra_limit >= max_pfn)
244 extra_pages = extra_limit - max_pfn;
245 else
246 extra_pages = 0;
247
Jeremy Fitzhardinged2a81712010-11-19 23:27:06 -0800248 xen_add_extra_mem(extra_pages);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400249
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700250 return "Xen";
251}
252
Roland McGrathd2eea682007-07-20 00:31:43 -0700253/*
254 * Set the bit indicating "nosegneg" library variants should be used.
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700255 * We only need to bother in pure 32-bit mode; compat 32-bit processes
256 * can have un-truncated segments, so wrapping around is allowed.
Roland McGrathd2eea682007-07-20 00:31:43 -0700257 */
Sam Ravnborg08b6d292008-01-30 13:33:25 +0100258static void __init fiddle_vdso(void)
Roland McGrathd2eea682007-07-20 00:31:43 -0700259{
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700260#ifdef CONFIG_X86_32
261 u32 *mask;
262 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
263 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
264 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
Roland McGrathd2eea682007-07-20 00:31:43 -0700265 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700266#endif
Roland McGrathd2eea682007-07-20 00:31:43 -0700267}
268
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700269static __cpuinit int register_callback(unsigned type, const void *func)
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700270{
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700271 struct callback_register callback = {
272 .type = type,
273 .address = XEN_CALLBACK(__KERNEL_CS, func),
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700274 .flags = CALLBACKF_mask_events,
275 };
276
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700277 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
278}
279
280void __cpuinit xen_enable_sysenter(void)
281{
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700282 int ret;
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700283 unsigned sysenter_feature;
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700284
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700285#ifdef CONFIG_X86_32
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700286 sysenter_feature = X86_FEATURE_SEP;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700287#else
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700288 sysenter_feature = X86_FEATURE_SYSENTER32;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700289#endif
290
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700291 if (!boot_cpu_has(sysenter_feature))
292 return;
293
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700294 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700295 if(ret != 0)
296 setup_clear_cpu_cap(sysenter_feature);
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700297}
298
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700299void __cpuinit xen_enable_syscall(void)
300{
301#ifdef CONFIG_X86_64
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700302 int ret;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700303
304 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
305 if (ret != 0) {
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700306 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700307 /* Pretty fatal; 64-bit userspace has no other
308 mechanism for syscalls. */
309 }
310
311 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700312 ret = register_callback(CALLBACKTYPE_syscall32,
313 xen_syscall32_target);
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700314 if (ret != 0)
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700315 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700316 }
317#endif /* CONFIG_X86_64 */
318}
319
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700320void __init xen_arch_setup(void)
321{
Donald Dutilef09f6d12010-07-15 14:56:49 -0400322 xen_panic_handler_init();
323
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700324 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
325 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
326
327 if (!xen_feature(XENFEAT_auto_translated_physmap))
Tejf63c2f22008-12-16 11:56:06 -0800328 HYPERVISOR_vm_assist(VMASST_CMD_enable,
329 VMASST_TYPE_pae_extended_cr3);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700330
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700331 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
332 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
333 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700334
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700335 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700336 xen_enable_syscall();
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700337
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700338#ifdef CONFIG_ACPI
339 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
340 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
341 disable_acpi();
342 }
343#endif
344
345 memcpy(boot_command_line, xen_start_info->cmd_line,
346 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
347 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
348
Jeremy Fitzhardingebc15fde2010-11-22 17:17:50 -0800349 /* Set up idle, making sure it calls safe_halt() pvop */
350#ifdef CONFIG_X86_32
351 boot_cpu_data.hlt_works_ok = 1;
352#endif
353 pm_idle = default_idle;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700354
Roland McGrathd2eea682007-07-20 00:31:43 -0700355 fiddle_vdso();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700356}