setup.c 9.4 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
#include <xen/interface/physdev.h>
26
#include <xen/interface/memory.h>
27 28 29
#include <xen/features.h>

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

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

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

42 43 44 45 46 47 48 49 50 51 52 53
/* 
 * 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)

54 55 56
static __init void xen_add_extra_mem(unsigned long pages)
{
	u64 size = (u64)pages * PAGE_SIZE;
57
	u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
58 59 60 61

	if (!pages)
		return;

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

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

	xen_extra_mem_size += size;
68

69
	xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
70 71
}

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

	start = PFN_UP(start_addr);
86
	end = PFN_DOWN(end_addr);
87 88 89 90

	if (end <= start)
		return 0;

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	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) {
			set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
			len++;
		}
	}
	printk(KERN_CONT "%ld pages freed\n", len);
113 114 115 116

	return len;
}

117 118
static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
						     const struct e820map *e820)
119
{
120
	phys_addr_t max_addr = PFN_PHYS(max_pfn);
121
	phys_addr_t last_end = ISA_END_ADDRESS;
122 123 124
	unsigned long released = 0;
	int i;

125
	/* Free any unused memory above the low 1Mbyte. */
126 127 128 129
	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);

130 131 132
		if (last_end < end)
			released += xen_release_chunk(last_end, end);
		last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
133 134
	}

135 136
	if (last_end < max_addr)
		released += xen_release_chunk(last_end, max_addr);
137 138 139 140

	printk(KERN_INFO "released %ld pages of unused memory\n", released);
	return released;
}
141 142 143 144 145 146

/**
 * machine_specific_memory_setup - Hook for machine specific memory setup.
 **/
char * __init xen_memory_setup(void)
{
I
Ian Campbell 已提交
147 148
	static struct e820entry map[E820MAX] __initdata;

149
	unsigned long max_pfn = xen_start_info->nr_pages;
I
Ian Campbell 已提交
150 151 152
	unsigned long long mem_end;
	int rc;
	struct xen_memory_map memmap;
153
	unsigned long extra_pages = 0;
154
	unsigned long extra_limit;
I
Ian Campbell 已提交
155
	int i;
I
Ian Campbell 已提交
156
	int op;
157

158
	max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
I
Ian Campbell 已提交
159 160 161 162 163
	mem_end = PFN_PHYS(max_pfn);

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

I
Ian Campbell 已提交
164 165 166 167
	op = xen_initial_domain() ?
		XENMEM_machine_memory_map :
		XENMEM_memory_map;
	rc = HYPERVISOR_memory_op(op, &memmap);
I
Ian Campbell 已提交
168
	if (rc == -ENOSYS) {
169
		BUG_ON(xen_initial_domain());
I
Ian Campbell 已提交
170 171 172 173 174 175 176 177 178
		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);
179

180
	e820.nr_map = 0;
181
	xen_extra_mem_start = mem_end;
I
Ian Campbell 已提交
182 183
	for (i = 0; i < memmap.nr_entries; i++) {
		unsigned long long end = map[i].addr + map[i].size;
184

I
Ian Campbell 已提交
185
		if (map[i].type == E820_RAM) {
186
			if (map[i].addr < mem_end && end > mem_end) {
I
Ian Campbell 已提交
187
				/* Truncate region to max_mem. */
188
				u64 delta = end - mem_end;
189

190 191 192 193
				map[i].size -= delta;
				extra_pages += PFN_DOWN(delta);

				end = mem_end;
I
Ian Campbell 已提交
194
			}
195 196 197
		}

		if (end > xen_extra_mem_start)
198
			xen_extra_mem_start = end;
199

200
		/* If region is non-RAM or below mem_end, add what remains */
201 202
		if ((map[i].type != E820_RAM || map[i].addr < mem_end) &&
		    map[i].size > 0)
I
Ian Campbell 已提交
203 204
			e820_add_region(map[i].addr, map[i].size, map[i].type);
	}
205 206

	/*
207 208
	 * In domU, the ISA region is normal, usable memory, but we
	 * reserve ISA memory anyway because too many things poke
209
	 * about in there.
210
	 *
211 212 213
	 * 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.
214 215 216
	 */
	e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
			E820_RESERVED);
217

218 219 220 221 222 223
	/*
	 * Reserve Xen bits:
	 *  - mfn_list
	 *  - xen_start_info
	 * See comment above "struct start_info" in <xen/interface/xen.h>
	 */
224
	memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
225 226
		      __pa(xen_start_info->pt_base),
			"XEN START INFO");
227 228 229

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

230 231
	extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	/*
	 * 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;

251
	xen_add_extra_mem(extra_pages);
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	return "Xen";
}

static void xen_idle(void)
{
	local_irq_disable();

	if (need_resched())
		local_irq_enable();
	else {
		current_thread_info()->status &= ~TS_POLLING;
		smp_mb__after_clear_bit();
		safe_halt();
		current_thread_info()->status |= TS_POLLING;
	}
}

270 271
/*
 * Set the bit indicating "nosegneg" library variants should be used.
272 273
 * We only need to bother in pure 32-bit mode; compat 32-bit processes
 * can have un-truncated segments, so wrapping around is allowed.
274
 */
275
static void __init fiddle_vdso(void)
276
{
277 278 279 280 281
#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);
282
	*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
283
#endif
284 285
}

286
static __cpuinit int register_callback(unsigned type, const void *func)
287
{
288 289 290
	struct callback_register callback = {
		.type = type,
		.address = XEN_CALLBACK(__KERNEL_CS, func),
291 292 293
		.flags = CALLBACKF_mask_events,
	};

294 295 296 297 298
	return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
}

void __cpuinit xen_enable_sysenter(void)
{
299
	int ret;
300
	unsigned sysenter_feature;
301 302

#ifdef CONFIG_X86_32
303
	sysenter_feature = X86_FEATURE_SEP;
304
#else
305
	sysenter_feature = X86_FEATURE_SYSENTER32;
306
#endif
307

308 309 310
	if (!boot_cpu_has(sysenter_feature))
		return;

311
	ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
312 313
	if(ret != 0)
		setup_clear_cpu_cap(sysenter_feature);
314 315
}

316 317 318 319 320 321 322
void __cpuinit xen_enable_syscall(void)
{
#ifdef CONFIG_X86_64
	int ret;

	ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
	if (ret != 0) {
323
		printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
324 325 326 327 328
		/* Pretty fatal; 64-bit userspace has no other
		   mechanism for syscalls. */
	}

	if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
329 330
		ret = register_callback(CALLBACKTYPE_syscall32,
					xen_syscall32_target);
331
		if (ret != 0)
332
			setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
333 334 335 336
	}
#endif /* CONFIG_X86_64 */
}

337 338
void __init xen_arch_setup(void)
{
339 340
	xen_panic_handler_init();

341 342 343 344
	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 已提交
345 346
		HYPERVISOR_vm_assist(VMASST_CMD_enable,
				     VMASST_TYPE_pae_extended_cr3);
347

348 349 350
	if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
	    register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
		BUG();
351

352
	xen_enable_sysenter();
353
	xen_enable_syscall();
354

355 356 357 358 359 360 361 362 363 364 365 366
#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);

	pm_idle = xen_idle;
J
Jeremy Fitzhardinge 已提交
367

368
	fiddle_vdso();
369
}