blob: 1e85e26efa69a4f33d1fb75f5b24bf2b95e3b4bc [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>
11
12#include <asm/elf.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010013#include <asm/vdso.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070014#include <asm/e820.h>
15#include <asm/setup.h>
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -070016#include <asm/acpi.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070017#include <asm/xen/hypervisor.h>
18#include <asm/xen/hypercall.h>
19
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010020#include <xen/page.h>
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070021#include <xen/interface/callback.h>
Ian Campbell35ae11f2009-02-06 19:09:48 -080022#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070023#include <xen/interface/physdev.h>
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040024#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070025#include <xen/features.h>
26
27#include "xen-ops.h"
Roland McGrathd2eea682007-07-20 00:31:43 -070028#include "vdso.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070029
30/* These are code, but not functions. Defined in entry.S */
31extern const char xen_hypervisor_callback[];
32extern const char xen_failsafe_callback[];
Tejf63c2f22008-12-16 11:56:06 -080033extern void xen_sysenter_target(void);
34extern void xen_syscall_target(void);
35extern void xen_syscall32_target(void);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070036
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -070037/* Amount of extra memory space we add to the e820 ranges */
38phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
39
40static __init void xen_add_extra_mem(unsigned long pages)
41{
42 u64 size = (u64)pages * PAGE_SIZE;
43
44 if (!pages)
45 return;
46
47 e820_add_region(xen_extra_mem_start + xen_extra_mem_size, size, E820_RAM);
48 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
49
50 reserve_early(xen_extra_mem_start + xen_extra_mem_size,
51 xen_extra_mem_start + xen_extra_mem_size + size,
52 "XEN EXTRA");
53
54 xen_extra_mem_size += size;
55}
56
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070057static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
58 phys_addr_t end_addr)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040059{
60 struct xen_memory_reservation reservation = {
61 .address_bits = 0,
62 .extent_order = 0,
63 .domid = DOMID_SELF
64 };
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040065 unsigned long start, end;
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070066 unsigned long len = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040067 unsigned long pfn;
68 int ret;
69
70 start = PFN_UP(start_addr);
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070071 end = PFN_DOWN(end_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040072
73 if (end <= start)
74 return 0;
75
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070076 printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
77 start, end);
78 for(pfn = start; pfn < end; pfn++) {
79 unsigned long mfn = pfn_to_mfn(pfn);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040080
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070081 /* Make sure pfn exists to start with */
82 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
83 continue;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040084
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070085 set_xen_guest_handle(reservation.extent_start, &mfn);
86 reservation.nr_extents = 1;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040087
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070088 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
89 &reservation);
90 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
91 start, end, ret);
92 if (ret == 1) {
93 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
94 len++;
95 }
96 }
97 printk(KERN_CONT "%ld pages freed\n", len);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040098
99 return len;
100}
101
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700102static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
103 const struct e820map *e820)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400104{
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700105 phys_addr_t max_addr = PFN_PHYS(max_pfn);
106 phys_addr_t last_end = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400107 unsigned long released = 0;
108 int i;
109
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700110 for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
111 phys_addr_t end = e820->map[i].addr;
112 end = min(max_addr, end);
113
114 released += xen_release_chunk(last_end, end);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400115 last_end = e820->map[i].addr + e820->map[i].size;
116 }
117
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700118 if (last_end < max_addr)
119 released += xen_release_chunk(last_end, max_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400120
121 printk(KERN_INFO "released %ld pages of unused memory\n", released);
122 return released;
123}
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700124
125/**
126 * machine_specific_memory_setup - Hook for machine specific memory setup.
127 **/
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700128char * __init xen_memory_setup(void)
129{
Ian Campbell35ae11f2009-02-06 19:09:48 -0800130 static struct e820entry map[E820MAX] __initdata;
131
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700132 unsigned long max_pfn = xen_start_info->nr_pages;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800133 unsigned long long mem_end;
134 int rc;
135 struct xen_memory_map memmap;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700136 unsigned long extra_pages = 0;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800137 int i;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700138
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100139 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800140 mem_end = PFN_PHYS(max_pfn);
141
142 memmap.nr_entries = E820MAX;
143 set_xen_guest_handle(memmap.buffer, map);
144
145 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
146 if (rc == -ENOSYS) {
147 memmap.nr_entries = 1;
148 map[0].addr = 0ULL;
149 map[0].size = mem_end;
150 /* 8MB slack (to balance backend allocations). */
151 map[0].size += 8ULL << 20;
152 map[0].type = E820_RAM;
153 rc = 0;
154 }
155 BUG_ON(rc);
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100156
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700157 e820.nr_map = 0;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700158 xen_extra_mem_start = mem_end;
Ian Campbell35ae11f2009-02-06 19:09:48 -0800159 for (i = 0; i < memmap.nr_entries; i++) {
160 unsigned long long end = map[i].addr + map[i].size;
Jeremy Fitzhardingeb5b43ce2010-09-02 17:10:12 -0700161
Ian Campbell35ae11f2009-02-06 19:09:48 -0800162 if (map[i].type == E820_RAM) {
Ian Campbell35ae11f2009-02-06 19:09:48 -0800163 if (end > mem_end) {
164 /* Truncate region to max_mem. */
165 map[i].size -= end - mem_end;
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700166
167 extra_pages += PFN_DOWN(end - mem_end);
Ian Campbell35ae11f2009-02-06 19:09:48 -0800168 }
Jeremy Fitzhardinge36bc2512010-09-02 17:07:03 -0700169 } else if (map[i].type != E820_RAM)
170 xen_extra_mem_start = end;
Jeremy Fitzhardingeb5b43ce2010-09-02 17:10:12 -0700171
172 if ((map[i].type != E820_RAM || map[i].addr < mem_end) &&
173 map[i].size > 0)
Ian Campbell35ae11f2009-02-06 19:09:48 -0800174 e820_add_region(map[i].addr, map[i].size, map[i].type);
175 }
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700176
177 /*
178 * Even though this is normal, usable memory under Xen, reserve
179 * ISA memory anyway because too many things think they can poke
180 * about in there.
181 */
182 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
183 E820_RESERVED);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700184
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700185 /*
186 * Reserve Xen bits:
187 * - mfn_list
188 * - xen_start_info
189 * See comment above "struct start_info" in <xen/interface/xen.h>
190 */
Jeremy Fitzhardinge6b2e8522009-05-07 11:56:49 -0700191 reserve_early(__pa(xen_start_info->mfn_list),
192 __pa(xen_start_info->pt_base),
193 "XEN START INFO");
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700194
195 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
196
Jeremy Fitzhardinge42ee1472010-08-30 16:41:02 -0700197 extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
198
199 xen_add_extra_mem(extra_pages);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400200
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700201 return "Xen";
202}
203
204static void xen_idle(void)
205{
206 local_irq_disable();
207
208 if (need_resched())
209 local_irq_enable();
210 else {
211 current_thread_info()->status &= ~TS_POLLING;
212 smp_mb__after_clear_bit();
213 safe_halt();
214 current_thread_info()->status |= TS_POLLING;
215 }
216}
217
Roland McGrathd2eea682007-07-20 00:31:43 -0700218/*
219 * Set the bit indicating "nosegneg" library variants should be used.
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700220 * We only need to bother in pure 32-bit mode; compat 32-bit processes
221 * can have un-truncated segments, so wrapping around is allowed.
Roland McGrathd2eea682007-07-20 00:31:43 -0700222 */
Sam Ravnborg08b6d292008-01-30 13:33:25 +0100223static void __init fiddle_vdso(void)
Roland McGrathd2eea682007-07-20 00:31:43 -0700224{
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700225#ifdef CONFIG_X86_32
226 u32 *mask;
227 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
228 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
229 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
Roland McGrathd2eea682007-07-20 00:31:43 -0700230 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700231#endif
Roland McGrathd2eea682007-07-20 00:31:43 -0700232}
233
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700234static __cpuinit int register_callback(unsigned type, const void *func)
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700235{
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700236 struct callback_register callback = {
237 .type = type,
238 .address = XEN_CALLBACK(__KERNEL_CS, func),
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700239 .flags = CALLBACKF_mask_events,
240 };
241
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700242 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
243}
244
245void __cpuinit xen_enable_sysenter(void)
246{
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700247 int ret;
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700248 unsigned sysenter_feature;
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700249
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700250#ifdef CONFIG_X86_32
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700251 sysenter_feature = X86_FEATURE_SEP;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700252#else
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700253 sysenter_feature = X86_FEATURE_SYSENTER32;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700254#endif
255
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700256 if (!boot_cpu_has(sysenter_feature))
257 return;
258
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700259 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700260 if(ret != 0)
261 setup_clear_cpu_cap(sysenter_feature);
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700262}
263
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700264void __cpuinit xen_enable_syscall(void)
265{
266#ifdef CONFIG_X86_64
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700267 int ret;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700268
269 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
270 if (ret != 0) {
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700271 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700272 /* Pretty fatal; 64-bit userspace has no other
273 mechanism for syscalls. */
274 }
275
276 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700277 ret = register_callback(CALLBACKTYPE_syscall32,
278 xen_syscall32_target);
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700279 if (ret != 0)
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700280 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700281 }
282#endif /* CONFIG_X86_64 */
283}
284
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700285void __init xen_arch_setup(void)
286{
287 struct physdev_set_iopl set_iopl;
288 int rc;
289
Donald Dutilef09f6d12010-07-15 14:56:49 -0400290 xen_panic_handler_init();
291
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700292 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
293 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
294
295 if (!xen_feature(XENFEAT_auto_translated_physmap))
Tejf63c2f22008-12-16 11:56:06 -0800296 HYPERVISOR_vm_assist(VMASST_CMD_enable,
297 VMASST_TYPE_pae_extended_cr3);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700298
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700299 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
300 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
301 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700302
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700303 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700304 xen_enable_syscall();
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700305
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700306 set_iopl.iopl = 1;
307 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
308 if (rc != 0)
309 printk(KERN_INFO "physdev_op failed %d\n", rc);
310
311#ifdef CONFIG_ACPI
312 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
313 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
314 disable_acpi();
315 }
316#endif
317
318 memcpy(boot_command_line, xen_start_info->cmd_line,
319 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
320 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
321
322 pm_idle = xen_idle;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700323
Jeremy Fitzhardingedfdcdd42007-07-17 18:37:07 -0700324 paravirt_disable_iospace();
Roland McGrathd2eea682007-07-20 00:31:43 -0700325
326 fiddle_vdso();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700327}