xen: implement "extra" memory to reserve space for pages not present at boot
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / xen / setup.c
blobf9a99eaddcdc990d8987f06207b6ec8407b7de04
1 /*
2 * Machine specific setup for xen
4 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
5 */
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/mm.h>
10 #include <linux/pm.h>
12 #include <asm/elf.h>
13 #include <asm/vdso.h>
14 #include <asm/e820.h>
15 #include <asm/setup.h>
16 #include <asm/acpi.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/hypercall.h>
20 #include <xen/page.h>
21 #include <xen/interface/callback.h>
22 #include <xen/interface/memory.h>
23 #include <xen/interface/physdev.h>
24 #include <xen/interface/memory.h>
25 #include <xen/features.h>
27 #include "xen-ops.h"
28 #include "vdso.h"
30 /* These are code, but not functions. Defined in entry.S */
31 extern const char xen_hypervisor_callback[];
32 extern const char xen_failsafe_callback[];
33 extern void xen_sysenter_target(void);
34 extern void xen_syscall_target(void);
35 extern void xen_syscall32_target(void);
37 /* Amount of extra memory space we add to the e820 ranges */
38 phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
40 static __init void xen_add_extra_mem(unsigned long pages)
42 u64 size = (u64)pages * PAGE_SIZE;
44 if (!pages)
45 return;
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);
50 reserve_early(xen_extra_mem_start + xen_extra_mem_size,
51 xen_extra_mem_start + xen_extra_mem_size + size,
52 "XEN EXTRA");
54 xen_extra_mem_size += size;
57 static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
58 phys_addr_t end_addr)
60 struct xen_memory_reservation reservation = {
61 .address_bits = 0,
62 .extent_order = 0,
63 .domid = DOMID_SELF
65 unsigned long start, end;
66 unsigned long len = 0;
67 unsigned long pfn;
68 int ret;
70 start = PFN_UP(start_addr);
71 end = PFN_DOWN(end_addr);
73 if (end <= start)
74 return 0;
76 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);
81 /* Make sure pfn exists to start with */
82 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
83 continue;
85 set_xen_guest_handle(reservation.extent_start, &mfn);
86 reservation.nr_extents = 1;
88 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++;
97 printk(KERN_CONT "%ld pages freed\n", len);
99 return len;
102 static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
103 const struct e820map *e820)
105 phys_addr_t max_addr = PFN_PHYS(max_pfn);
106 phys_addr_t last_end = 0;
107 unsigned long released = 0;
108 int i;
110 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);
114 released += xen_release_chunk(last_end, end);
115 last_end = e820->map[i].addr + e820->map[i].size;
118 if (last_end < max_addr)
119 released += xen_release_chunk(last_end, max_addr);
121 printk(KERN_INFO "released %ld pages of unused memory\n", released);
122 return released;
126 * machine_specific_memory_setup - Hook for machine specific memory setup.
128 char * __init xen_memory_setup(void)
130 static struct e820entry map[E820MAX] __initdata;
132 unsigned long max_pfn = xen_start_info->nr_pages;
133 unsigned long long mem_end;
134 int rc;
135 struct xen_memory_map memmap;
136 unsigned long extra_pages = 0;
137 int i;
139 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
140 mem_end = PFN_PHYS(max_pfn);
142 memmap.nr_entries = E820MAX;
143 set_xen_guest_handle(memmap.buffer, map);
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;
155 BUG_ON(rc);
157 e820.nr_map = 0;
158 xen_extra_mem_start = mem_end;
159 for (i = 0; i < memmap.nr_entries; i++) {
160 unsigned long long end = map[i].addr + map[i].size;
161 if (map[i].type == E820_RAM) {
162 if (map[i].addr > mem_end)
163 continue;
164 if (end > mem_end) {
165 /* Truncate region to max_mem. */
166 map[i].size -= end - mem_end;
168 extra_pages += PFN_DOWN(end - mem_end);
171 if (map[i].size > 0)
172 e820_add_region(map[i].addr, map[i].size, map[i].type);
176 * Even though this is normal, usable memory under Xen, reserve
177 * ISA memory anyway because too many things think they can poke
178 * about in there.
180 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
181 E820_RESERVED);
184 * Reserve Xen bits:
185 * - mfn_list
186 * - xen_start_info
187 * See comment above "struct start_info" in <xen/interface/xen.h>
189 reserve_early(__pa(xen_start_info->mfn_list),
190 __pa(xen_start_info->pt_base),
191 "XEN START INFO");
193 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
195 extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
197 xen_add_extra_mem(extra_pages);
199 return "Xen";
202 static void xen_idle(void)
204 local_irq_disable();
206 if (need_resched())
207 local_irq_enable();
208 else {
209 current_thread_info()->status &= ~TS_POLLING;
210 smp_mb__after_clear_bit();
211 safe_halt();
212 current_thread_info()->status |= TS_POLLING;
217 * Set the bit indicating "nosegneg" library variants should be used.
218 * We only need to bother in pure 32-bit mode; compat 32-bit processes
219 * can have un-truncated segments, so wrapping around is allowed.
221 static void __init fiddle_vdso(void)
223 #ifdef CONFIG_X86_32
224 u32 *mask;
225 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
226 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
227 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
228 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
229 #endif
232 static __cpuinit int register_callback(unsigned type, const void *func)
234 struct callback_register callback = {
235 .type = type,
236 .address = XEN_CALLBACK(__KERNEL_CS, func),
237 .flags = CALLBACKF_mask_events,
240 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
243 void __cpuinit xen_enable_sysenter(void)
245 int ret;
246 unsigned sysenter_feature;
248 #ifdef CONFIG_X86_32
249 sysenter_feature = X86_FEATURE_SEP;
250 #else
251 sysenter_feature = X86_FEATURE_SYSENTER32;
252 #endif
254 if (!boot_cpu_has(sysenter_feature))
255 return;
257 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
258 if(ret != 0)
259 setup_clear_cpu_cap(sysenter_feature);
262 void __cpuinit xen_enable_syscall(void)
264 #ifdef CONFIG_X86_64
265 int ret;
267 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
268 if (ret != 0) {
269 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
270 /* Pretty fatal; 64-bit userspace has no other
271 mechanism for syscalls. */
274 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
275 ret = register_callback(CALLBACKTYPE_syscall32,
276 xen_syscall32_target);
277 if (ret != 0)
278 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
280 #endif /* CONFIG_X86_64 */
283 void __init xen_arch_setup(void)
285 struct physdev_set_iopl set_iopl;
286 int rc;
288 xen_panic_handler_init();
290 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
291 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
293 if (!xen_feature(XENFEAT_auto_translated_physmap))
294 HYPERVISOR_vm_assist(VMASST_CMD_enable,
295 VMASST_TYPE_pae_extended_cr3);
297 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
298 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
299 BUG();
301 xen_enable_sysenter();
302 xen_enable_syscall();
304 set_iopl.iopl = 1;
305 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
306 if (rc != 0)
307 printk(KERN_INFO "physdev_op failed %d\n", rc);
309 #ifdef CONFIG_ACPI
310 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
311 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
312 disable_acpi();
314 #endif
316 memcpy(boot_command_line, xen_start_info->cmd_line,
317 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
318 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
320 pm_idle = xen_idle;
322 paravirt_disable_iospace();
324 fiddle_vdso();