setup.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Machine specific setup for xen
 *
 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
 */

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/pm.h>
11
#include <linux/memblock.h>
12 13

#include <asm/elf.h>
R
Roland McGrath 已提交
14
#include <asm/vdso.h>
15 16
#include <asm/e820.h>
#include <asm/setup.h>
17
#include <asm/acpi.h>
18 19 20
#include <asm/xen/hypervisor.h>
#include <asm/xen/hypercall.h>

21
#include <xen/xen.h>
22
#include <xen/page.h>
23
#include <xen/interface/callback.h>
I
Ian Campbell 已提交
24
#include <xen/interface/memory.h>
25 26 27 28
#include <xen/interface/physdev.h>
#include <xen/features.h>

#include "xen-ops.h"
29
#include "vdso.h"
30 31 32 33

/* These are code, but not functions.  Defined in entry.S */
extern const char xen_hypervisor_callback[];
extern const char xen_failsafe_callback[];
T
Tej 已提交
34 35 36
extern void xen_sysenter_target(void);
extern void xen_syscall_target(void);
extern void xen_syscall32_target(void);
37

38 39 40
/* Amount of extra memory space we add to the e820 ranges */
phys_addr_t xen_extra_mem_start, xen_extra_mem_size;

41 42 43 44 45 46 47 48 49 50 51 52
/* 
 * The maximum amount of extra memory compared to the base size.  The
 * main scaling factor is the size of struct page.  At extreme ratios
 * of base:extra, all the base memory can be filled with page
 * structures for the extra memory, leaving no space for anything
 * else.
 * 
 * 10x seems like a reasonable balance between scaling flexibility and
 * leaving a practically usable system.
 */
#define EXTRA_MEM_RATIO		(10)

53
static void __init xen_add_extra_mem(unsigned long pages)
54
{
55 56
	unsigned long pfn;

57
	u64 size = (u64)pages * PAGE_SIZE;
58
	u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
59 60 61 62

	if (!pages)
		return;

63
	e820_add_region(extra_start, size, E820_RAM);
64 65
	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);

66
	memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA");
67 68

	xen_extra_mem_size += size;
69

70
	xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
71 72 73

	for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++)
		__set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
74 75
}

76 77
static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
					      phys_addr_t end_addr)
78 79 80 81 82 83 84
{
	struct xen_memory_reservation reservation = {
		.address_bits = 0,
		.extent_order = 0,
		.domid        = DOMID_SELF
	};
	unsigned long start, end;
85
	unsigned long len = 0;
86 87 88 89
	unsigned long pfn;
	int ret;

	start = PFN_UP(start_addr);
90
	end = PFN_DOWN(end_addr);
91 92 93 94

	if (end <= start)
		return 0;

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
	       start, end);
	for(pfn = start; pfn < end; pfn++) {
		unsigned long mfn = pfn_to_mfn(pfn);

		/* Make sure pfn exists to start with */
		if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
			continue;

		set_xen_guest_handle(reservation.extent_start, &mfn);
		reservation.nr_extents = 1;

		ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
					   &reservation);
		WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
		     start, end, ret);
		if (ret == 1) {
112
			__set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
113 114 115 116
			len++;
		}
	}
	printk(KERN_CONT "%ld pages freed\n", len);
117 118 119 120

	return len;
}

121 122
static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
						     const struct e820map *e820)
123
{
124
	phys_addr_t max_addr = PFN_PHYS(max_pfn);
125
	phys_addr_t last_end = ISA_END_ADDRESS;
126 127 128
	unsigned long released = 0;
	int i;

129
	/* Free any unused memory above the low 1Mbyte. */
130 131 132 133
	for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
		phys_addr_t end = e820->map[i].addr;
		end = min(max_addr, end);

134 135 136
		if (last_end < end)
			released += xen_release_chunk(last_end, end);
		last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
137 138
	}

139 140
	if (last_end < max_addr)
		released += xen_release_chunk(last_end, max_addr);
141 142 143 144

	printk(KERN_INFO "released %ld pages of unused memory\n", released);
	return released;
}
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
static unsigned long __init xen_set_identity(const struct e820entry *list,
					     ssize_t map_size)
{
	phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS;
	phys_addr_t start_pci = last;
	const struct e820entry *entry;
	unsigned long identity = 0;
	int i;

	for (i = 0, entry = list; i < map_size; i++, entry++) {
		phys_addr_t start = entry->addr;
		phys_addr_t end = start + entry->size;

		if (start < last)
			start = last;

		if (end <= start)
			continue;

		/* Skip over the 1MB region. */
		if (last > end)
			continue;

169
		if ((entry->type == E820_RAM) || (entry->type == E820_UNUSABLE)) {
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
			if (start > start_pci)
				identity += set_phys_range_identity(
						PFN_UP(start_pci), PFN_DOWN(start));

			/* Without saving 'last' we would gooble RAM too
			 * at the end of the loop. */
			last = end;
			start_pci = end;
			continue;
		}
		start_pci = min(start, start_pci);
		last = end;
	}
	if (last > start_pci)
		identity += set_phys_range_identity(
					PFN_UP(start_pci), PFN_DOWN(last));
	return identity;
}
188 189 190 191 192
/**
 * machine_specific_memory_setup - Hook for machine specific memory setup.
 **/
char * __init xen_memory_setup(void)
{
I
Ian Campbell 已提交
193
	static struct e820entry map[E820MAX] __initdata;
194
	static struct e820entry map_raw[E820MAX] __initdata;
I
Ian Campbell 已提交
195

196
	unsigned long max_pfn = xen_start_info->nr_pages;
I
Ian Campbell 已提交
197 198 199
	unsigned long long mem_end;
	int rc;
	struct xen_memory_map memmap;
200
	unsigned long extra_pages = 0;
201
	unsigned long extra_limit;
202
	unsigned long identity_pages = 0;
I
Ian Campbell 已提交
203
	int i;
I
Ian Campbell 已提交
204
	int op;
205

206
	max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
I
Ian Campbell 已提交
207 208 209 210 211
	mem_end = PFN_PHYS(max_pfn);

	memmap.nr_entries = E820MAX;
	set_xen_guest_handle(memmap.buffer, map);

I
Ian Campbell 已提交
212 213 214 215
	op = xen_initial_domain() ?
		XENMEM_machine_memory_map :
		XENMEM_memory_map;
	rc = HYPERVISOR_memory_op(op, &memmap);
I
Ian Campbell 已提交
216
	if (rc == -ENOSYS) {
217
		BUG_ON(xen_initial_domain());
I
Ian Campbell 已提交
218 219 220 221 222 223 224 225 226
		memmap.nr_entries = 1;
		map[0].addr = 0ULL;
		map[0].size = mem_end;
		/* 8MB slack (to balance backend allocations). */
		map[0].size += 8ULL << 20;
		map[0].type = E820_RAM;
		rc = 0;
	}
	BUG_ON(rc);
227

228
	memcpy(map_raw, map, sizeof(map));
229
	e820.nr_map = 0;
230
#ifdef CONFIG_X86_32
231
	xen_extra_mem_start = mem_end;
232
#else
233
	xen_extra_mem_start = max((1ULL << 32), mem_end);
234
#endif
I
Ian Campbell 已提交
235
	for (i = 0; i < memmap.nr_entries; i++) {
236
		unsigned long long end;
237

238 239 240 241 242
		/* Guard against non-page aligned E820 entries. */
		if (map[i].type == E820_RAM)
			map[i].size -= (map[i].size + map[i].addr) % PAGE_SIZE;

		end = map[i].addr + map[i].size;
243 244 245
		if (map[i].type == E820_RAM && end > mem_end) {
			/* RAM off the end - may be partially included */
			u64 delta = min(map[i].size, end - mem_end);
246

247 248
			map[i].size -= delta;
			end -= delta;
249

250
			extra_pages += PFN_DOWN(delta);
251 252 253 254 255 256
			/*
			 * Set RAM below 4GB that is not for us to be unusable.
			 * This prevents "System RAM" address space from being
			 * used as potential resource for I/O address (happens
			 * when 'allocate_resource' is called).
			 */
257 258
			if (delta &&
				(xen_initial_domain() && end < 0x100000000ULL))
259
				e820_add_region(end, delta, E820_UNUSABLE);
260 261
		}

262
		if (map[i].size > 0 && end > xen_extra_mem_start)
263
			xen_extra_mem_start = end;
264

265 266
		/* Add region if any remains */
		if (map[i].size > 0)
I
Ian Campbell 已提交
267 268
			e820_add_region(map[i].addr, map[i].size, map[i].type);
	}
269 270

	/*
271 272
	 * In domU, the ISA region is normal, usable memory, but we
	 * reserve ISA memory anyway because too many things poke
273
	 * about in there.
274
	 *
275 276 277
	 * In Dom0, the host E820 information can leave gaps in the
	 * ISA range, which would cause us to release those pages.  To
	 * avoid this, we unconditionally reserve them here.
278 279 280
	 */
	e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
			E820_RESERVED);
281

282 283 284 285 286 287
	/*
	 * Reserve Xen bits:
	 *  - mfn_list
	 *  - xen_start_info
	 * See comment above "struct start_info" in <xen/interface/xen.h>
	 */
288
	memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
289 290
		      __pa(xen_start_info->pt_base),
			"XEN START INFO");
291 292 293

	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);

294 295
	extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	/*
	 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
	 * factor the base size.  On non-highmem systems, the base
	 * size is the full initial memory allocation; on highmem it
	 * is limited to the max size of lowmem, so that it doesn't
	 * get completely filled.
	 *
	 * In principle there could be a problem in lowmem systems if
	 * the initial memory is also very large with respect to
	 * lowmem, but we won't try to deal with that here.
	 */
	extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
			  max_pfn + extra_pages);

	if (extra_limit >= max_pfn)
		extra_pages = extra_limit - max_pfn;
	else
		extra_pages = 0;

315
	xen_add_extra_mem(extra_pages);
316

317 318 319 320 321 322 323
	/*
	 * Set P2M for all non-RAM pages and E820 gaps to be identity
	 * type PFNs. We supply it with the non-sanitized version
	 * of the E820.
	 */
	identity_pages = xen_set_identity(map_raw, memmap.nr_entries);
	printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages);
324 325 326
	return "Xen";
}

327 328
/*
 * Set the bit indicating "nosegneg" library variants should be used.
329 330
 * We only need to bother in pure 32-bit mode; compat 32-bit processes
 * can have un-truncated segments, so wrapping around is allowed.
331
 */
332
static void __init fiddle_vdso(void)
333
{
334 335 336 337 338
#ifdef CONFIG_X86_32
	u32 *mask;
	mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
	*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
	mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
339
	*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
340
#endif
341 342
}

343
static int __cpuinit register_callback(unsigned type, const void *func)
344
{
345 346 347
	struct callback_register callback = {
		.type = type,
		.address = XEN_CALLBACK(__KERNEL_CS, func),
348 349 350
		.flags = CALLBACKF_mask_events,
	};

351 352 353 354 355
	return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
}

void __cpuinit xen_enable_sysenter(void)
{
356
	int ret;
357
	unsigned sysenter_feature;
358 359

#ifdef CONFIG_X86_32
360
	sysenter_feature = X86_FEATURE_SEP;
361
#else
362
	sysenter_feature = X86_FEATURE_SYSENTER32;
363
#endif
364

365 366 367
	if (!boot_cpu_has(sysenter_feature))
		return;

368
	ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
369 370
	if(ret != 0)
		setup_clear_cpu_cap(sysenter_feature);
371 372
}

373 374 375 376 377 378 379
void __cpuinit xen_enable_syscall(void)
{
#ifdef CONFIG_X86_64
	int ret;

	ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
	if (ret != 0) {
380
		printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
381 382 383 384 385
		/* Pretty fatal; 64-bit userspace has no other
		   mechanism for syscalls. */
	}

	if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
386 387
		ret = register_callback(CALLBACKTYPE_syscall32,
					xen_syscall32_target);
388
		if (ret != 0)
389
			setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
390 391 392 393
	}
#endif /* CONFIG_X86_64 */
}

394 395
void __init xen_arch_setup(void)
{
396 397
	xen_panic_handler_init();

398 399 400 401
	HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
	HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);

	if (!xen_feature(XENFEAT_auto_translated_physmap))
T
Tej 已提交
402 403
		HYPERVISOR_vm_assist(VMASST_CMD_enable,
				     VMASST_TYPE_pae_extended_cr3);
404

405 406 407
	if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
	    register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
		BUG();
408

409
	xen_enable_sysenter();
410
	xen_enable_syscall();
411

412 413 414 415 416 417 418 419 420 421 422
#ifdef CONFIG_ACPI
	if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
		printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
		disable_acpi();
	}
#endif

	memcpy(boot_command_line, xen_start_info->cmd_line,
	       MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
	       COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);

J
Jeremy Fitzhardinge 已提交
423 424 425 426 427
	/* Set up idle, making sure it calls safe_halt() pvop */
#ifdef CONFIG_X86_32
	boot_cpu_data.hlt_works_ok = 1;
#endif
	pm_idle = default_idle;
428
	boot_option_idle_override = IDLE_HALT;
J
Jeremy Fitzhardinge 已提交
429

430
	fiddle_vdso();
431
}