blob: 62ceb7864017167659527f92f5d5f97f46028aea [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>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070022#include <xen/interface/physdev.h>
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040023#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070024#include <xen/features.h>
25
26#include "xen-ops.h"
Roland McGrathd2eea682007-07-20 00:31:43 -070027#include "vdso.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070028
29/* These are code, but not functions. Defined in entry.S */
30extern const char xen_hypervisor_callback[];
31extern const char xen_failsafe_callback[];
Tejf63c2f22008-12-16 11:56:06 -080032extern void xen_sysenter_target(void);
33extern void xen_syscall_target(void);
34extern void xen_syscall32_target(void);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070035
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070036static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
37 phys_addr_t end_addr)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040038{
39 struct xen_memory_reservation reservation = {
40 .address_bits = 0,
41 .extent_order = 0,
42 .domid = DOMID_SELF
43 };
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040044 unsigned long start, end;
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070045 unsigned long len = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040046 unsigned long pfn;
47 int ret;
48
49 start = PFN_UP(start_addr);
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070050 end = PFN_DOWN(end_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040051
52 if (end <= start)
53 return 0;
54
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070055 printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
56 start, end);
57 for(pfn = start; pfn < end; pfn++) {
58 unsigned long mfn = pfn_to_mfn(pfn);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040059
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070060 /* Make sure pfn exists to start with */
61 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
62 continue;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040063
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070064 set_xen_guest_handle(reservation.extent_start, &mfn);
65 reservation.nr_extents = 1;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040066
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070067 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
68 &reservation);
69 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
70 start, end, ret);
71 if (ret == 1) {
72 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
73 len++;
74 }
75 }
76 printk(KERN_CONT "%ld pages freed\n", len);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040077
78 return len;
79}
80
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070081static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
82 const struct e820map *e820)
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040083{
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070084 phys_addr_t max_addr = PFN_PHYS(max_pfn);
85 phys_addr_t last_end = 0;
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040086 unsigned long released = 0;
87 int i;
88
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070089 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);
92
93 released += xen_release_chunk(last_end, end);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040094 last_end = e820->map[i].addr + e820->map[i].size;
95 }
96
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -070097 if (last_end < max_addr)
98 released += xen_release_chunk(last_end, max_addr);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -040099
100 printk(KERN_INFO "released %ld pages of unused memory\n", released);
101 return released;
102}
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700103
104/**
105 * machine_specific_memory_setup - Hook for machine specific memory setup.
106 **/
107
108char * __init xen_memory_setup(void)
109{
110 unsigned long max_pfn = xen_start_info->nr_pages;
111
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +0100112 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
113
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700114 e820.nr_map = 0;
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700115
Jeremy Fitzhardinge5670a432008-09-14 07:42:23 -0700116 e820_add_region(0, PFN_PHYS((u64)max_pfn), E820_RAM);
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700117
118 /*
119 * Even though this is normal, usable memory under Xen, reserve
120 * ISA memory anyway because too many things think they can poke
121 * about in there.
Juan Quintela4ec53872010-09-02 15:45:43 +0100122 *
123 * In a dom0 kernel, this region is identity mapped with the
124 * hardware ISA area, so it really is out of bounds.
Jeremy Fitzhardingeb792c752008-06-16 14:54:49 -0700125 */
126 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
127 E820_RESERVED);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700128
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700129 /*
130 * Reserve Xen bits:
131 * - mfn_list
132 * - xen_start_info
133 * See comment above "struct start_info" in <xen/interface/xen.h>
134 */
Jeremy Fitzhardinge6b2e8522009-05-07 11:56:49 -0700135 reserve_early(__pa(xen_start_info->mfn_list),
136 __pa(xen_start_info->pt_base),
137 "XEN START INFO");
Jeremy Fitzhardingebe5bf9f2008-06-16 14:54:46 -0700138
139 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
140
Jeremy Fitzhardingef89e0482009-09-16 12:38:33 -0700141 xen_return_unused_memory(xen_start_info->nr_pages, &e820);
Miroslav Rezanina093d7b42009-09-16 03:56:17 -0400142
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700143 return "Xen";
144}
145
146static void xen_idle(void)
147{
148 local_irq_disable();
149
150 if (need_resched())
151 local_irq_enable();
152 else {
153 current_thread_info()->status &= ~TS_POLLING;
154 smp_mb__after_clear_bit();
155 safe_halt();
156 current_thread_info()->status |= TS_POLLING;
157 }
158}
159
Roland McGrathd2eea682007-07-20 00:31:43 -0700160/*
161 * Set the bit indicating "nosegneg" library variants should be used.
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700162 * We only need to bother in pure 32-bit mode; compat 32-bit processes
163 * can have un-truncated segments, so wrapping around is allowed.
Roland McGrathd2eea682007-07-20 00:31:43 -0700164 */
Sam Ravnborg08b6d292008-01-30 13:33:25 +0100165static void __init fiddle_vdso(void)
Roland McGrathd2eea682007-07-20 00:31:43 -0700166{
Jeremy Fitzhardinge6a52e4b2008-07-12 02:22:00 -0700167#ifdef CONFIG_X86_32
168 u32 *mask;
169 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
170 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
171 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
Roland McGrathd2eea682007-07-20 00:31:43 -0700172 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700173#endif
Roland McGrathd2eea682007-07-20 00:31:43 -0700174}
175
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700176static __cpuinit int register_callback(unsigned type, const void *func)
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700177{
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700178 struct callback_register callback = {
179 .type = type,
180 .address = XEN_CALLBACK(__KERNEL_CS, func),
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700181 .flags = CALLBACKF_mask_events,
182 };
183
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700184 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
185}
186
187void __cpuinit xen_enable_sysenter(void)
188{
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700189 int ret;
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700190 unsigned sysenter_feature;
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700191
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700192#ifdef CONFIG_X86_32
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700193 sysenter_feature = X86_FEATURE_SEP;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700194#else
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700195 sysenter_feature = X86_FEATURE_SYSENTER32;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700196#endif
197
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700198 if (!boot_cpu_has(sysenter_feature))
199 return;
200
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700201 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700202 if(ret != 0)
203 setup_clear_cpu_cap(sysenter_feature);
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700204}
205
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700206void __cpuinit xen_enable_syscall(void)
207{
208#ifdef CONFIG_X86_64
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700209 int ret;
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700210
211 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
212 if (ret != 0) {
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700213 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700214 /* Pretty fatal; 64-bit userspace has no other
215 mechanism for syscalls. */
216 }
217
218 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700219 ret = register_callback(CALLBACKTYPE_syscall32,
220 xen_syscall32_target);
Jeremy Fitzhardinged5303b82008-07-12 02:22:06 -0700221 if (ret != 0)
Jeremy Fitzhardinge62541c32008-07-10 16:24:08 -0700222 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700223 }
224#endif /* CONFIG_X86_64 */
225}
226
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700227void __init xen_arch_setup(void)
228{
229 struct physdev_set_iopl set_iopl;
230 int rc;
231
Donald Dutilef09f6d12010-07-15 14:56:49 -0400232 xen_panic_handler_init();
233
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700234 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
235 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
236
237 if (!xen_feature(XENFEAT_auto_translated_physmap))
Tejf63c2f22008-12-16 11:56:06 -0800238 HYPERVISOR_vm_assist(VMASST_CMD_enable,
239 VMASST_TYPE_pae_extended_cr3);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700240
Jeremy Fitzhardinge88459d42008-07-08 15:07:02 -0700241 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
242 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
243 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700244
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700245 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700246 xen_enable_syscall();
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700247
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700248 set_iopl.iopl = 1;
249 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
250 if (rc != 0)
251 printk(KERN_INFO "physdev_op failed %d\n", rc);
252
253#ifdef CONFIG_ACPI
254 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
255 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
256 disable_acpi();
257 }
258#endif
259
260 memcpy(boot_command_line, xen_start_info->cmd_line,
261 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
262 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
263
264 pm_idle = xen_idle;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700265
Roland McGrathd2eea682007-07-20 00:31:43 -0700266 fiddle_vdso();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700267}