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