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 #include <linux/cpuidle.h>
13 #include <linux/cpufreq.h>
14
15 #include <asm/elf.h>
16 #include <asm/vdso.h>
17 #include <asm/e820.h>
18 #include <asm/setup.h>
19 #include <asm/acpi.h>
20 #include <asm/numa.h>
21 #include <asm/xen/hypervisor.h>
22 #include <asm/xen/hypercall.h>
23
24 #include <xen/xen.h>
25 #include <xen/page.h>
26 #include <xen/interface/callback.h>
27 #include <xen/interface/memory.h>
28 #include <xen/interface/physdev.h>
29 #include <xen/features.h>
30
31 #include "xen-ops.h"
32 #include "vdso.h"
33
34 /* These are code, but not functions. Defined in entry.S */
35 extern const char xen_hypervisor_callback[];
36 extern const char xen_failsafe_callback[];
37 extern void xen_sysenter_target(void);
38 extern void xen_syscall_target(void);
39 extern void xen_syscall32_target(void);
40
41 /* Amount of extra memory space we add to the e820 ranges */
42 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
43
44 /* Number of pages released from the initial allocation. */
45 unsigned long xen_released_pages;
46
47 /*
48 * The maximum amount of extra memory compared to the base size. The
49 * main scaling factor is the size of struct page. At extreme ratios
50 * of base:extra, all the base memory can be filled with page
51 * structures for the extra memory, leaving no space for anything
52 * else.
53 *
54 * 10x seems like a reasonable balance between scaling flexibility and
55 * leaving a practically usable system.
56 */
57 #define EXTRA_MEM_RATIO (10)
58
xen_add_extra_mem(u64 start,u64 size)59 static void __init xen_add_extra_mem(u64 start, u64 size)
60 {
61 unsigned long pfn;
62 int i;
63
64 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
65 /* Add new region. */
66 if (xen_extra_mem[i].size == 0) {
67 xen_extra_mem[i].start = start;
68 xen_extra_mem[i].size = size;
69 break;
70 }
71 /* Append to existing region. */
72 if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
73 xen_extra_mem[i].size += size;
74 break;
75 }
76 }
77 if (i == XEN_EXTRA_MEM_MAX_REGIONS)
78 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
79
80 memblock_reserve(start, size);
81
82 xen_max_p2m_pfn = PFN_DOWN(start + size);
83 for (pfn = PFN_DOWN(start); pfn < xen_max_p2m_pfn; pfn++) {
84 unsigned long mfn = pfn_to_mfn(pfn);
85
86 if (WARN(mfn == pfn, "Trying to over-write 1-1 mapping (pfn: %lx)\n", pfn))
87 continue;
88 WARN(mfn != INVALID_P2M_ENTRY, "Trying to remove %lx which has %lx mfn!\n",
89 pfn, mfn);
90
91 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
92 }
93 }
94
xen_release_chunk(unsigned long start,unsigned long end)95 static unsigned long __init xen_release_chunk(unsigned long start,
96 unsigned long end)
97 {
98 struct xen_memory_reservation reservation = {
99 .address_bits = 0,
100 .extent_order = 0,
101 .domid = DOMID_SELF
102 };
103 unsigned long len = 0;
104 unsigned long pfn;
105 int ret;
106
107 for(pfn = start; pfn < end; pfn++) {
108 unsigned long mfn = pfn_to_mfn(pfn);
109
110 /* Make sure pfn exists to start with */
111 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
112 continue;
113
114 set_xen_guest_handle(reservation.extent_start, &mfn);
115 reservation.nr_extents = 1;
116
117 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
118 &reservation);
119 WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
120 if (ret == 1) {
121 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
122 len++;
123 }
124 }
125 printk(KERN_INFO "Freeing %lx-%lx pfn range: %lu pages freed\n",
126 start, end, len);
127
128 return len;
129 }
130
xen_set_identity_and_release(const struct e820entry * list,size_t map_size,unsigned long nr_pages)131 static unsigned long __init xen_set_identity_and_release(
132 const struct e820entry *list, size_t map_size, unsigned long nr_pages)
133 {
134 phys_addr_t start = 0;
135 unsigned long released = 0;
136 unsigned long identity = 0;
137 const struct e820entry *entry;
138 int i;
139
140 /*
141 * Combine non-RAM regions and gaps until a RAM region (or the
142 * end of the map) is reached, then set the 1:1 map and
143 * release the pages (if available) in those non-RAM regions.
144 *
145 * The combined non-RAM regions are rounded to a whole number
146 * of pages so any partial pages are accessible via the 1:1
147 * mapping. This is needed for some BIOSes that put (for
148 * example) the DMI tables in a reserved region that begins on
149 * a non-page boundary.
150 */
151 for (i = 0, entry = list; i < map_size; i++, entry++) {
152 phys_addr_t end = entry->addr + entry->size;
153
154 if (entry->type == E820_RAM || i == map_size - 1) {
155 unsigned long start_pfn = PFN_DOWN(start);
156 unsigned long end_pfn = PFN_UP(end);
157
158 if (entry->type == E820_RAM)
159 end_pfn = PFN_UP(entry->addr);
160
161 if (start_pfn < end_pfn) {
162 if (start_pfn < nr_pages)
163 released += xen_release_chunk(
164 start_pfn, min(end_pfn, nr_pages));
165
166 identity += set_phys_range_identity(
167 start_pfn, end_pfn);
168 }
169 start = end;
170 }
171 }
172
173 printk(KERN_INFO "Released %lu pages of unused memory\n", released);
174 printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
175
176 return released;
177 }
178
xen_get_max_pages(void)179 static unsigned long __init xen_get_max_pages(void)
180 {
181 unsigned long max_pages = MAX_DOMAIN_PAGES;
182 domid_t domid = DOMID_SELF;
183 int ret;
184
185 /*
186 * For the initial domain we use the maximum reservation as
187 * the maximum page.
188 *
189 * For guest domains the current maximum reservation reflects
190 * the current maximum rather than the static maximum. In this
191 * case the e820 map provided to us will cover the static
192 * maximum region.
193 */
194 if (xen_initial_domain()) {
195 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
196 if (ret > 0)
197 max_pages = ret;
198 }
199
200 return min(max_pages, MAX_DOMAIN_PAGES);
201 }
202
xen_align_and_add_e820_region(u64 start,u64 size,int type)203 static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
204 {
205 u64 end = start + size;
206
207 /* Align RAM regions to page boundaries. */
208 if (type == E820_RAM) {
209 start = PAGE_ALIGN(start);
210 end &= ~((u64)PAGE_SIZE - 1);
211 }
212
213 e820_add_region(start, end - start, type);
214 }
215
xen_ignore_unusable(struct e820entry * list,size_t map_size)216 void xen_ignore_unusable(struct e820entry *list, size_t map_size)
217 {
218 struct e820entry *entry;
219 unsigned int i;
220
221 for (i = 0, entry = list; i < map_size; i++, entry++) {
222 if (entry->type == E820_UNUSABLE)
223 entry->type = E820_RAM;
224 }
225 }
226
227 /**
228 * machine_specific_memory_setup - Hook for machine specific memory setup.
229 **/
xen_memory_setup(void)230 char * __init xen_memory_setup(void)
231 {
232 static struct e820entry map[E820MAX] __initdata;
233
234 unsigned long max_pfn = xen_start_info->nr_pages;
235 unsigned long long mem_end;
236 int rc;
237 struct xen_memory_map memmap;
238 unsigned long max_pages;
239 unsigned long extra_pages = 0;
240 int i;
241 int op;
242
243 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
244 mem_end = PFN_PHYS(max_pfn);
245
246 memmap.nr_entries = E820MAX;
247 set_xen_guest_handle(memmap.buffer, map);
248
249 op = xen_initial_domain() ?
250 XENMEM_machine_memory_map :
251 XENMEM_memory_map;
252 rc = HYPERVISOR_memory_op(op, &memmap);
253 if (rc == -ENOSYS) {
254 BUG_ON(xen_initial_domain());
255 memmap.nr_entries = 1;
256 map[0].addr = 0ULL;
257 map[0].size = mem_end;
258 /* 8MB slack (to balance backend allocations). */
259 map[0].size += 8ULL << 20;
260 map[0].type = E820_RAM;
261 rc = 0;
262 }
263 BUG_ON(rc);
264
265 /*
266 * Xen won't allow a 1:1 mapping to be created to UNUSABLE
267 * regions, so if we're using the machine memory map leave the
268 * region as RAM as it is in the pseudo-physical map.
269 *
270 * UNUSABLE regions in domUs are not handled and will need
271 * a patch in the future.
272 */
273 if (xen_initial_domain())
274 xen_ignore_unusable(map, memmap.nr_entries);
275
276 /* Make sure the Xen-supplied memory map is well-ordered. */
277 sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
278
279 max_pages = xen_get_max_pages();
280 if (max_pages > max_pfn)
281 extra_pages += max_pages - max_pfn;
282
283 /*
284 * Set P2M for all non-RAM pages and E820 gaps to be identity
285 * type PFNs. Any RAM pages that would be made inaccesible by
286 * this are first released.
287 */
288 xen_released_pages = xen_set_identity_and_release(
289 map, memmap.nr_entries, max_pfn);
290 extra_pages += xen_released_pages;
291
292 /*
293 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
294 * factor the base size. On non-highmem systems, the base
295 * size is the full initial memory allocation; on highmem it
296 * is limited to the max size of lowmem, so that it doesn't
297 * get completely filled.
298 *
299 * In principle there could be a problem in lowmem systems if
300 * the initial memory is also very large with respect to
301 * lowmem, but we won't try to deal with that here.
302 */
303 extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
304 extra_pages);
305
306 i = 0;
307 while (i < memmap.nr_entries) {
308 u64 addr = map[i].addr;
309 u64 size = map[i].size;
310 u32 type = map[i].type;
311
312 if (type == E820_RAM) {
313 if (addr < mem_end) {
314 size = min(size, mem_end - addr);
315 } else if (extra_pages) {
316 size = min(size, (u64)extra_pages * PAGE_SIZE);
317 extra_pages -= size / PAGE_SIZE;
318 xen_add_extra_mem(addr, size);
319 } else
320 type = E820_UNUSABLE;
321 }
322
323 xen_align_and_add_e820_region(addr, size, type);
324
325 map[i].addr += size;
326 map[i].size -= size;
327 if (map[i].size == 0)
328 i++;
329 }
330
331 /*
332 * In domU, the ISA region is normal, usable memory, but we
333 * reserve ISA memory anyway because too many things poke
334 * about in there.
335 */
336 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
337 E820_RESERVED);
338
339 /*
340 * Reserve Xen bits:
341 * - mfn_list
342 * - xen_start_info
343 * See comment above "struct start_info" in <xen/interface/xen.h>
344 */
345 memblock_reserve(__pa(xen_start_info->mfn_list),
346 xen_start_info->pt_base - xen_start_info->mfn_list);
347
348 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
349
350 return "Xen";
351 }
352
353 /*
354 * Set the bit indicating "nosegneg" library variants should be used.
355 * We only need to bother in pure 32-bit mode; compat 32-bit processes
356 * can have un-truncated segments, so wrapping around is allowed.
357 */
fiddle_vdso(void)358 static void __init fiddle_vdso(void)
359 {
360 #ifdef CONFIG_X86_32
361 u32 *mask;
362 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
363 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
364 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
365 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
366 #endif
367 }
368
register_callback(unsigned type,const void * func)369 static int __cpuinit register_callback(unsigned type, const void *func)
370 {
371 struct callback_register callback = {
372 .type = type,
373 .address = XEN_CALLBACK(__KERNEL_CS, func),
374 .flags = CALLBACKF_mask_events,
375 };
376
377 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
378 }
379
xen_enable_sysenter(void)380 void __cpuinit xen_enable_sysenter(void)
381 {
382 int ret;
383 unsigned sysenter_feature;
384
385 #ifdef CONFIG_X86_32
386 sysenter_feature = X86_FEATURE_SEP;
387 #else
388 sysenter_feature = X86_FEATURE_SYSENTER32;
389 #endif
390
391 if (!boot_cpu_has(sysenter_feature))
392 return;
393
394 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
395 if(ret != 0)
396 setup_clear_cpu_cap(sysenter_feature);
397 }
398
xen_enable_syscall(void)399 void __cpuinit xen_enable_syscall(void)
400 {
401 #ifdef CONFIG_X86_64
402 int ret;
403
404 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
405 if (ret != 0) {
406 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
407 /* Pretty fatal; 64-bit userspace has no other
408 mechanism for syscalls. */
409 }
410
411 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
412 ret = register_callback(CALLBACKTYPE_syscall32,
413 xen_syscall32_target);
414 if (ret != 0)
415 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
416 }
417 #endif /* CONFIG_X86_64 */
418 }
419
xen_arch_setup(void)420 void __init xen_arch_setup(void)
421 {
422 xen_panic_handler_init();
423
424 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
425 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
426
427 if (!xen_feature(XENFEAT_auto_translated_physmap))
428 HYPERVISOR_vm_assist(VMASST_CMD_enable,
429 VMASST_TYPE_pae_extended_cr3);
430
431 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
432 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
433 BUG();
434
435 xen_enable_sysenter();
436 xen_enable_syscall();
437
438 #ifdef CONFIG_ACPI
439 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
440 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
441 disable_acpi();
442 }
443 #endif
444
445 memcpy(boot_command_line, xen_start_info->cmd_line,
446 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
447 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
448
449 /* Set up idle, making sure it calls safe_halt() pvop */
450 #ifdef CONFIG_X86_32
451 boot_cpu_data.hlt_works_ok = 1;
452 #endif
453 disable_cpuidle();
454 disable_cpufreq();
455 WARN_ON(set_pm_idle_to_default());
456 fiddle_vdso();
457 #ifdef CONFIG_NUMA
458 numa_off = 1;
459 #endif
460 }
461