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>
12 #include <linux/cpuidle.h>
13 #include <linux/cpufreq.h>
18 #include <asm/setup.h>
20 #include <asm/xen/hypervisor.h>
21 #include <asm/xen/hypercall.h>
25 #include <xen/interface/callback.h>
26 #include <xen/interface/memory.h>
27 #include <xen/interface/physdev.h>
28 #include <xen/features.h>
33 /* These are code, but not functions. Defined in entry.S */
34 extern const char xen_hypervisor_callback[];
35 extern const char xen_failsafe_callback[];
36 extern void xen_sysenter_target(void);
37 extern void xen_syscall_target(void);
38 extern void xen_syscall32_target(void);
40 /* Amount of extra memory space we add to the e820 ranges */
41 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
43 /* Number of pages released from the initial allocation. */
44 unsigned long xen_released_pages;
47 * The maximum amount of extra memory compared to the base size. The
48 * main scaling factor is the size of struct page. At extreme ratios
49 * of base:extra, all the base memory can be filled with page
50 * structures for the extra memory, leaving no space for anything
53 * 10x seems like a reasonable balance between scaling flexibility and
54 * leaving a practically usable system.
56 #define EXTRA_MEM_RATIO (10)
58 static void __init xen_add_extra_mem(u64 start, u64 size)
63 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
65 if (xen_extra_mem[i].size == 0) {
66 xen_extra_mem[i].start = start;
67 xen_extra_mem[i].size = size;
70 /* Append to existing region. */
71 if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
72 xen_extra_mem[i].size += size;
76 if (i == XEN_EXTRA_MEM_MAX_REGIONS)
77 printk(KERN_WARNING "Warning: not enough extra memory regions\n");
79 memblock_reserve(start, size);
81 xen_max_p2m_pfn = PFN_DOWN(start + size);
83 for (pfn = PFN_DOWN(start); pfn <= xen_max_p2m_pfn; pfn++)
84 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
87 static unsigned long __init xen_release_chunk(unsigned long start,
90 struct xen_memory_reservation reservation = {
95 unsigned long len = 0;
99 for(pfn = start; pfn < end; pfn++) {
100 unsigned long mfn = pfn_to_mfn(pfn);
102 /* Make sure pfn exists to start with */
103 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
106 set_xen_guest_handle(reservation.extent_start, &mfn);
107 reservation.nr_extents = 1;
109 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
111 WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
113 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
117 printk(KERN_INFO "Freeing %lx-%lx pfn range: %lu pages freed\n",
123 static unsigned long __init xen_set_identity_and_release(
124 const struct e820entry *list, size_t map_size, unsigned long nr_pages)
126 phys_addr_t start = 0;
127 unsigned long released = 0;
128 unsigned long identity = 0;
129 const struct e820entry *entry;
133 * Combine non-RAM regions and gaps until a RAM region (or the
134 * end of the map) is reached, then set the 1:1 map and
135 * release the pages (if available) in those non-RAM regions.
137 * The combined non-RAM regions are rounded to a whole number
138 * of pages so any partial pages are accessible via the 1:1
139 * mapping. This is needed for some BIOSes that put (for
140 * example) the DMI tables in a reserved region that begins on
141 * a non-page boundary.
143 for (i = 0, entry = list; i < map_size; i++, entry++) {
144 phys_addr_t end = entry->addr + entry->size;
146 if (entry->type == E820_RAM || i == map_size - 1) {
147 unsigned long start_pfn = PFN_DOWN(start);
148 unsigned long end_pfn = PFN_UP(end);
150 if (entry->type == E820_RAM)
151 end_pfn = PFN_UP(entry->addr);
153 if (start_pfn < end_pfn) {
154 if (start_pfn < nr_pages)
155 released += xen_release_chunk(
156 start_pfn, min(end_pfn, nr_pages));
158 identity += set_phys_range_identity(
165 printk(KERN_INFO "Released %lu pages of unused memory\n", released);
166 printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
171 static unsigned long __init xen_get_max_pages(void)
173 unsigned long max_pages = MAX_DOMAIN_PAGES;
174 domid_t domid = DOMID_SELF;
178 * For the initial domain we use the maximum reservation as
181 * For guest domains the current maximum reservation reflects
182 * the current maximum rather than the static maximum. In this
183 * case the e820 map provided to us will cover the static
186 if (xen_initial_domain()) {
187 ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
192 return min(max_pages, MAX_DOMAIN_PAGES);
195 static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
197 u64 end = start + size;
199 /* Align RAM regions to page boundaries. */
200 if (type == E820_RAM) {
201 start = PAGE_ALIGN(start);
202 end &= ~((u64)PAGE_SIZE - 1);
205 e820_add_region(start, end - start, type);
209 * machine_specific_memory_setup - Hook for machine specific memory setup.
211 char * __init xen_memory_setup(void)
213 static struct e820entry map[E820MAX] __initdata;
215 unsigned long max_pfn = xen_start_info->nr_pages;
216 unsigned long long mem_end;
218 struct xen_memory_map memmap;
219 unsigned long max_pages;
220 unsigned long extra_pages = 0;
224 max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
225 mem_end = PFN_PHYS(max_pfn);
227 memmap.nr_entries = E820MAX;
228 set_xen_guest_handle(memmap.buffer, map);
230 op = xen_initial_domain() ?
231 XENMEM_machine_memory_map :
233 rc = HYPERVISOR_memory_op(op, &memmap);
235 BUG_ON(xen_initial_domain());
236 memmap.nr_entries = 1;
238 map[0].size = mem_end;
239 /* 8MB slack (to balance backend allocations). */
240 map[0].size += 8ULL << 20;
241 map[0].type = E820_RAM;
246 /* Make sure the Xen-supplied memory map is well-ordered. */
247 sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
249 max_pages = xen_get_max_pages();
250 if (max_pages > max_pfn)
251 extra_pages += max_pages - max_pfn;
254 * Set P2M for all non-RAM pages and E820 gaps to be identity
255 * type PFNs. Any RAM pages that would be made inaccesible by
256 * this are first released.
258 xen_released_pages = xen_set_identity_and_release(
259 map, memmap.nr_entries, max_pfn);
260 extra_pages += xen_released_pages;
263 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
264 * factor the base size. On non-highmem systems, the base
265 * size is the full initial memory allocation; on highmem it
266 * is limited to the max size of lowmem, so that it doesn't
267 * get completely filled.
269 * In principle there could be a problem in lowmem systems if
270 * the initial memory is also very large with respect to
271 * lowmem, but we won't try to deal with that here.
273 extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
277 while (i < memmap.nr_entries) {
278 u64 addr = map[i].addr;
279 u64 size = map[i].size;
280 u32 type = map[i].type;
282 if (type == E820_RAM) {
283 if (addr < mem_end) {
284 size = min(size, mem_end - addr);
285 } else if (extra_pages) {
286 size = min(size, (u64)extra_pages * PAGE_SIZE);
287 extra_pages -= size / PAGE_SIZE;
288 xen_add_extra_mem(addr, size);
290 type = E820_UNUSABLE;
293 xen_align_and_add_e820_region(addr, size, type);
297 if (map[i].size == 0)
302 * In domU, the ISA region is normal, usable memory, but we
303 * reserve ISA memory anyway because too many things poke
306 e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
313 * See comment above "struct start_info" in <xen/interface/xen.h>
315 memblock_reserve(__pa(xen_start_info->mfn_list),
316 xen_start_info->pt_base - xen_start_info->mfn_list);
318 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
324 * Set the bit indicating "nosegneg" library variants should be used.
325 * We only need to bother in pure 32-bit mode; compat 32-bit processes
326 * can have un-truncated segments, so wrapping around is allowed.
328 static void __init fiddle_vdso(void)
332 mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
333 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
334 mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
335 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
339 static int __cpuinit register_callback(unsigned type, const void *func)
341 struct callback_register callback = {
343 .address = XEN_CALLBACK(__KERNEL_CS, func),
344 .flags = CALLBACKF_mask_events,
347 return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
350 void __cpuinit xen_enable_sysenter(void)
353 unsigned sysenter_feature;
356 sysenter_feature = X86_FEATURE_SEP;
358 sysenter_feature = X86_FEATURE_SYSENTER32;
361 if (!boot_cpu_has(sysenter_feature))
364 ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
366 setup_clear_cpu_cap(sysenter_feature);
369 void __cpuinit xen_enable_syscall(void)
374 ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
376 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
377 /* Pretty fatal; 64-bit userspace has no other
378 mechanism for syscalls. */
381 if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
382 ret = register_callback(CALLBACKTYPE_syscall32,
383 xen_syscall32_target);
385 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
387 #endif /* CONFIG_X86_64 */
390 void __init xen_arch_setup(void)
392 xen_panic_handler_init();
394 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
395 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
397 if (!xen_feature(XENFEAT_auto_translated_physmap))
398 HYPERVISOR_vm_assist(VMASST_CMD_enable,
399 VMASST_TYPE_pae_extended_cr3);
401 if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
402 register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
405 xen_enable_sysenter();
406 xen_enable_syscall();
409 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
410 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
415 memcpy(boot_command_line, xen_start_info->cmd_line,
416 MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
417 COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
419 /* Set up idle, making sure it calls safe_halt() pvop */
421 boot_cpu_data.hlt_works_ok = 1;
425 WARN_ON(set_pm_idle_to_default());