Merge branches 'upstream/xenfs' and 'upstream/core' of git://git.kernel.org/pub/scm...
[linux-2.6.git] / arch / x86 / xen / setup.c
1 /*
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 #include <linux/memblock.h>
12
13 #include <asm/elf.h>
14 #include <asm/vdso.h>
15 #include <asm/e820.h>
16 #include <asm/setup.h>
17 #include <asm/acpi.h>
18 #include <asm/xen/hypervisor.h>
19 #include <asm/xen/hypercall.h>
20
21 #include <xen/xen.h>
22 #include <xen/page.h>
23 #include <xen/interface/callback.h>
24 #include <xen/interface/memory.h>
25 #include <xen/interface/physdev.h>
26 #include <xen/interface/memory.h>
27 #include <xen/features.h>
28
29 #include "xen-ops.h"
30 #include "vdso.h"
31
32 /* These are code, but not functions.  Defined in entry.S */
33 extern const char xen_hypervisor_callback[];
34 extern const char xen_failsafe_callback[];
35 extern void xen_sysenter_target(void);
36 extern void xen_syscall_target(void);
37 extern void xen_syscall32_target(void);
38
39 /* Amount of extra memory space we add to the e820 ranges */
40 phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
41
42 /* 
43  * The maximum amount of extra memory compared to the base size.  The
44  * main scaling factor is the size of struct page.  At extreme ratios
45  * of base:extra, all the base memory can be filled with page
46  * structures for the extra memory, leaving no space for anything
47  * else.
48  * 
49  * 10x seems like a reasonable balance between scaling flexibility and
50  * leaving a practically usable system.
51  */
52 #define EXTRA_MEM_RATIO         (10)
53
54 static __init void xen_add_extra_mem(unsigned long pages)
55 {
56         u64 size = (u64)pages * PAGE_SIZE;
57         u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
58
59         if (!pages)
60                 return;
61
62         e820_add_region(extra_start, size, E820_RAM);
63         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
64
65         memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA");
66
67         xen_extra_mem_size += size;
68
69         xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
70 }
71
72 static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
73                                               phys_addr_t end_addr)
74 {
75         struct xen_memory_reservation reservation = {
76                 .address_bits = 0,
77                 .extent_order = 0,
78                 .domid        = DOMID_SELF
79         };
80         unsigned long start, end;
81         unsigned long len = 0;
82         unsigned long pfn;
83         int ret;
84
85         start = PFN_UP(start_addr);
86         end = PFN_DOWN(end_addr);
87
88         if (end <= start)
89                 return 0;
90
91         printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
92                start, end);
93         for(pfn = start; pfn < end; pfn++) {
94                 unsigned long mfn = pfn_to_mfn(pfn);
95
96                 /* Make sure pfn exists to start with */
97                 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
98                         continue;
99
100                 set_xen_guest_handle(reservation.extent_start, &mfn);
101                 reservation.nr_extents = 1;
102
103                 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
104                                            &reservation);
105                 WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
106                      start, end, ret);
107                 if (ret == 1) {
108                         set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
109                         len++;
110                 }
111         }
112         printk(KERN_CONT "%ld pages freed\n", len);
113
114         return len;
115 }
116
117 static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
118                                                      const struct e820map *e820)
119 {
120         phys_addr_t max_addr = PFN_PHYS(max_pfn);
121         phys_addr_t last_end = 0;
122         unsigned long released = 0;
123         int i;
124
125         for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
126                 phys_addr_t end = e820->map[i].addr;
127                 end = min(max_addr, end);
128
129                 released += xen_release_chunk(last_end, end);
130                 last_end = e820->map[i].addr + e820->map[i].size;
131         }
132
133         if (last_end < max_addr)
134                 released += xen_release_chunk(last_end, max_addr);
135
136         printk(KERN_INFO "released %ld pages of unused memory\n", released);
137         return released;
138 }
139
140 /**
141  * machine_specific_memory_setup - Hook for machine specific memory setup.
142  **/
143 char * __init xen_memory_setup(void)
144 {
145         static struct e820entry map[E820MAX] __initdata;
146
147         unsigned long max_pfn = xen_start_info->nr_pages;
148         unsigned long long mem_end;
149         int rc;
150         struct xen_memory_map memmap;
151         unsigned long extra_pages = 0;
152         unsigned long extra_limit;
153         int i;
154         int op;
155
156         max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
157         mem_end = PFN_PHYS(max_pfn);
158
159         memmap.nr_entries = E820MAX;
160         set_xen_guest_handle(memmap.buffer, map);
161
162         op = xen_initial_domain() ?
163                 XENMEM_machine_memory_map :
164                 XENMEM_memory_map;
165         rc = HYPERVISOR_memory_op(op, &memmap);
166         if (rc == -ENOSYS) {
167                 memmap.nr_entries = 1;
168                 map[0].addr = 0ULL;
169                 map[0].size = mem_end;
170                 /* 8MB slack (to balance backend allocations). */
171                 map[0].size += 8ULL << 20;
172                 map[0].type = E820_RAM;
173                 rc = 0;
174         }
175         BUG_ON(rc);
176
177         e820.nr_map = 0;
178         xen_extra_mem_start = mem_end;
179         for (i = 0; i < memmap.nr_entries; i++) {
180                 unsigned long long end = map[i].addr + map[i].size;
181
182                 if (map[i].type == E820_RAM) {
183                         if (map[i].addr < mem_end && end > mem_end) {
184                                 /* Truncate region to max_mem. */
185                                 u64 delta = end - mem_end;
186
187                                 map[i].size -= delta;
188                                 extra_pages += PFN_DOWN(delta);
189
190                                 end = mem_end;
191                         }
192                 }
193
194                 if (end > xen_extra_mem_start)
195                         xen_extra_mem_start = end;
196
197                 /* If region is non-RAM or below mem_end, add what remains */
198                 if ((map[i].type != E820_RAM || map[i].addr < mem_end) &&
199                     map[i].size > 0)
200                         e820_add_region(map[i].addr, map[i].size, map[i].type);
201         }
202
203         /*
204          * Even though this is normal, usable memory under Xen, reserve
205          * ISA memory anyway because too many things think they can poke
206          * about in there.
207          */
208         e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
209                         E820_RESERVED);
210
211         /*
212          * Reserve Xen bits:
213          *  - mfn_list
214          *  - xen_start_info
215          * See comment above "struct start_info" in <xen/interface/xen.h>
216          */
217         memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
218                       __pa(xen_start_info->pt_base),
219                         "XEN START INFO");
220
221         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
222
223         extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
224
225         /*
226          * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
227          * factor the base size.  On non-highmem systems, the base
228          * size is the full initial memory allocation; on highmem it
229          * is limited to the max size of lowmem, so that it doesn't
230          * get completely filled.
231          *
232          * In principle there could be a problem in lowmem systems if
233          * the initial memory is also very large with respect to
234          * lowmem, but we won't try to deal with that here.
235          */
236         extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
237                           max_pfn + extra_pages);
238
239         if (extra_limit >= max_pfn)
240                 extra_pages = extra_limit - max_pfn;
241         else
242                 extra_pages = 0;
243
244         if (!xen_initial_domain())
245                 xen_add_extra_mem(extra_pages);
246
247         return "Xen";
248 }
249
250 static void xen_idle(void)
251 {
252         local_irq_disable();
253
254         if (need_resched())
255                 local_irq_enable();
256         else {
257                 current_thread_info()->status &= ~TS_POLLING;
258                 smp_mb__after_clear_bit();
259                 safe_halt();
260                 current_thread_info()->status |= TS_POLLING;
261         }
262 }
263
264 /*
265  * Set the bit indicating "nosegneg" library variants should be used.
266  * We only need to bother in pure 32-bit mode; compat 32-bit processes
267  * can have un-truncated segments, so wrapping around is allowed.
268  */
269 static void __init fiddle_vdso(void)
270 {
271 #ifdef CONFIG_X86_32
272         u32 *mask;
273         mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
274         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
275         mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
276         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
277 #endif
278 }
279
280 static __cpuinit int register_callback(unsigned type, const void *func)
281 {
282         struct callback_register callback = {
283                 .type = type,
284                 .address = XEN_CALLBACK(__KERNEL_CS, func),
285                 .flags = CALLBACKF_mask_events,
286         };
287
288         return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
289 }
290
291 void __cpuinit xen_enable_sysenter(void)
292 {
293         int ret;
294         unsigned sysenter_feature;
295
296 #ifdef CONFIG_X86_32
297         sysenter_feature = X86_FEATURE_SEP;
298 #else
299         sysenter_feature = X86_FEATURE_SYSENTER32;
300 #endif
301
302         if (!boot_cpu_has(sysenter_feature))
303                 return;
304
305         ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
306         if(ret != 0)
307                 setup_clear_cpu_cap(sysenter_feature);
308 }
309
310 void __cpuinit xen_enable_syscall(void)
311 {
312 #ifdef CONFIG_X86_64
313         int ret;
314
315         ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
316         if (ret != 0) {
317                 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
318                 /* Pretty fatal; 64-bit userspace has no other
319                    mechanism for syscalls. */
320         }
321
322         if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
323                 ret = register_callback(CALLBACKTYPE_syscall32,
324                                         xen_syscall32_target);
325                 if (ret != 0)
326                         setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
327         }
328 #endif /* CONFIG_X86_64 */
329 }
330
331 void __init xen_arch_setup(void)
332 {
333         struct physdev_set_iopl set_iopl;
334         int rc;
335
336         xen_panic_handler_init();
337
338         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
339         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
340
341         if (!xen_feature(XENFEAT_auto_translated_physmap))
342                 HYPERVISOR_vm_assist(VMASST_CMD_enable,
343                                      VMASST_TYPE_pae_extended_cr3);
344
345         if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
346             register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
347                 BUG();
348
349         xen_enable_sysenter();
350         xen_enable_syscall();
351
352         set_iopl.iopl = 1;
353         rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
354         if (rc != 0)
355                 printk(KERN_INFO "physdev_op failed %d\n", rc);
356
357 #ifdef CONFIG_ACPI
358         if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
359                 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
360                 disable_acpi();
361         }
362 #endif
363
364         memcpy(boot_command_line, xen_start_info->cmd_line,
365                MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
366                COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
367
368         pm_idle = xen_idle;
369
370         paravirt_disable_iospace();
371
372         fiddle_vdso();
373 }