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