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>
16 #include <asm/setup.h>
18 #include <asm/xen/hypervisor.h>
19 #include <asm/xen/hypercall.h>
22 #include <xen/interface/callback.h>
23 #include <xen/interface/physdev.h>
24 #include <xen/interface/memory.h>
25 #include <xen/features.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 static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
40 struct xen_memory_reservation reservation = {
45 unsigned long start, end;
46 unsigned long len = 0;
50 start = PFN_UP(start_addr);
51 end = PFN_DOWN(end_addr);
56 printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
58 for(pfn = start; pfn < end; pfn++) {
59 unsigned long mfn = pfn_to_mfn(pfn);
61 /* Make sure pfn exists to start with */
62 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
65 set_xen_guest_handle(reservation.extent_start, &mfn);
66 reservation.nr_extents = 1;
68 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
70 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
73 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
77 printk(KERN_CONT "%ld pages freed\n", len);
82 static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
83 const struct e820map *e820)
85 phys_addr_t max_addr = PFN_PHYS(max_pfn);
86 phys_addr_t last_end = 0;
87 unsigned long released = 0;
90 for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
91 phys_addr_t end = e820->map[i].addr;
92 end = min(max_addr, end);
94 released += xen_release_chunk(last_end, end);
95 last_end = e820->map[i].addr + e820->map[i].size;
98 if (last_end < max_addr)
99 released += xen_release_chunk(last_end, max_addr);
101 printk(KERN_INFO "released %ld pages of unused memory\n", released);
106 * machine_specific_memory_setup - Hook for machine specific memory setup.
109 char * __init xen_memory_setup(void)
111 unsigned long max_pfn = xen_start_info->nr_pages;
113 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
117 e820_add_region(0, PFN_PHYS((u64)max_pfn), E820_RAM);
120 * Even though this is normal, usable memory under Xen, reserve
121 * ISA memory anyway because too many things think they can poke
124 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
131 * See comment above "struct start_info" in <xen/interface/xen.h>
133 memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
134 __pa(xen_start_info->pt_base),
137 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
139 xen_return_unused_memory(xen_start_info->nr_pages, &e820);
144 static void xen_idle(void)
151 current_thread_info()->status &= ~TS_POLLING;
152 smp_mb__after_clear_bit();
154 current_thread_info()->status |= TS_POLLING;
159 * Set the bit indicating "nosegneg" library variants should be used.
160 * We only need to bother in pure 32-bit mode; compat 32-bit processes
161 * can have un-truncated segments, so wrapping around is allowed.
163 static void __init fiddle_vdso(void)
167 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
168 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
169 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
170 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
174 static __cpuinit int register_callback(unsigned type, const void *func)
176 struct callback_register callback = {
178 .address = XEN_CALLBACK(__KERNEL_CS, func),
179 .flags = CALLBACKF_mask_events,
182 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
185 void __cpuinit xen_enable_sysenter(void)
188 unsigned sysenter_feature;
191 sysenter_feature = X86_FEATURE_SEP;
193 sysenter_feature = X86_FEATURE_SYSENTER32;
196 if (!boot_cpu_has(sysenter_feature))
199 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
201 setup_clear_cpu_cap(sysenter_feature);
204 void __cpuinit xen_enable_syscall(void)
209 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
211 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
212 /* Pretty fatal; 64-bit userspace has no other
213 mechanism for syscalls. */
216 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
217 ret = register_callback(CALLBACKTYPE_syscall32,
218 xen_syscall32_target);
220 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
222 #endif /* CONFIG_X86_64 */
225 void __init xen_arch_setup(void)
227 struct physdev_set_iopl set_iopl;
230 xen_panic_handler_init();
232 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
233 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
235 if (!xen_feature(XENFEAT_auto_translated_physmap))
236 HYPERVISOR_vm_assist(VMASST_CMD_enable,
237 VMASST_TYPE_pae_extended_cr3);
239 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
240 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
243 xen_enable_sysenter();
244 xen_enable_syscall();
247 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
249 printk(KERN_INFO "physdev_op failed %d\n", rc);
252 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
253 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
258 memcpy(boot_command_line, xen_start_info->cmd_line,
259 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
260 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
264 paravirt_disable_iospace();