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>
15 #include <asm/setup.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/hypercall.h>
21 #include <xen/interface/callback.h>
22 #include <xen/interface/physdev.h>
23 #include <xen/interface/memory.h>
24 #include <xen/features.h>
29 /* These are code, but not functions. Defined in entry.S */
30 extern const char xen_hypervisor_callback[];
31 extern const char xen_failsafe_callback[];
32 extern void xen_sysenter_target(void);
33 extern void xen_syscall_target(void);
34 extern void xen_syscall32_target(void);
36 static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
39 struct xen_memory_reservation reservation = {
44 unsigned long start, end;
45 unsigned long len = 0;
49 start = PFN_UP(start_addr);
50 end = PFN_DOWN(end_addr);
55 printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
57 for(pfn = start; pfn < end; pfn++) {
58 unsigned long mfn = pfn_to_mfn(pfn);
60 /* Make sure pfn exists to start with */
61 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
64 set_xen_guest_handle(reservation.extent_start, &mfn);
65 reservation.nr_extents = 1;
67 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
69 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
72 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
76 printk(KERN_CONT "%ld pages freed\n", len);
81 static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
82 const struct e820map *e820)
84 phys_addr_t max_addr = PFN_PHYS(max_pfn);
85 phys_addr_t last_end = 0;
86 unsigned long released = 0;
89 for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
90 phys_addr_t end = e820->map[i].addr;
91 end = min(max_addr, end);
93 released += xen_release_chunk(last_end, end);
94 last_end = e820->map[i].addr + e820->map[i].size;
97 if (last_end < max_addr)
98 released += xen_release_chunk(last_end, max_addr);
100 printk(KERN_INFO "released %ld pages of unused memory\n", released);
105 * machine_specific_memory_setup - Hook for machine specific memory setup.
108 char * __init xen_memory_setup(void)
110 unsigned long max_pfn = xen_start_info->nr_pages;
112 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
116 e820_add_region(0, PFN_PHYS((u64)max_pfn), E820_RAM);
119 * Even though this is normal, usable memory under Xen, reserve
120 * ISA memory anyway because too many things think they can poke
123 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
130 * See comment above "struct start_info" in <xen/interface/xen.h>
132 reserve_early(__pa(xen_start_info->mfn_list),
133 __pa(xen_start_info->pt_base),
136 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
138 xen_return_unused_memory(xen_start_info->nr_pages, &e820);
143 static void xen_idle(void)
150 current_thread_info()->status &= ~TS_POLLING;
151 smp_mb__after_clear_bit();
153 current_thread_info()->status |= TS_POLLING;
158 * Set the bit indicating "nosegneg" library variants should be used.
159 * We only need to bother in pure 32-bit mode; compat 32-bit processes
160 * can have un-truncated segments, so wrapping around is allowed.
162 static void __init fiddle_vdso(void)
166 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
167 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
168 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
169 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
173 static __cpuinit int register_callback(unsigned type, const void *func)
175 struct callback_register callback = {
177 .address = XEN_CALLBACK(__KERNEL_CS, func),
178 .flags = CALLBACKF_mask_events,
181 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
184 void __cpuinit xen_enable_sysenter(void)
187 unsigned sysenter_feature;
190 sysenter_feature = X86_FEATURE_SEP;
192 sysenter_feature = X86_FEATURE_SYSENTER32;
195 if (!boot_cpu_has(sysenter_feature))
198 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
200 setup_clear_cpu_cap(sysenter_feature);
203 void __cpuinit xen_enable_syscall(void)
208 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
210 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
211 /* Pretty fatal; 64-bit userspace has no other
212 mechanism for syscalls. */
215 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
216 ret = register_callback(CALLBACKTYPE_syscall32,
217 xen_syscall32_target);
219 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
221 #endif /* CONFIG_X86_64 */
224 void __init xen_arch_setup(void)
226 struct physdev_set_iopl set_iopl;
229 xen_panic_handler_init();
231 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
232 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
234 if (!xen_feature(XENFEAT_auto_translated_physmap))
235 HYPERVISOR_vm_assist(VMASST_CMD_enable,
236 VMASST_TYPE_pae_extended_cr3);
238 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
239 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
242 xen_enable_sysenter();
243 xen_enable_syscall();
246 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
248 printk(KERN_INFO "physdev_op failed %d\n", rc);
251 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
252 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
257 memcpy(boot_command_line, xen_start_info->cmd_line,
258 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
259 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
263 paravirt_disable_iospace();