PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/x86/xen/setup.c

https://bitbucket.org/advance38/linux
C | 566 lines | 372 code | 82 blank | 112 comment | 75 complexity | dd83cb962c89abf939d937ce3a42091d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Machine specific setup for xen
  3. *
  4. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  5. */
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/pm.h>
  10. #include <linux/memblock.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpufreq.h>
  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/numa.h>
  19. #include <asm/xen/hypervisor.h>
  20. #include <asm/xen/hypercall.h>
  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. #include "xen-ops.h"
  28. #include "vdso.h"
  29. /* These are code, but not functions. Defined in entry.S */
  30. extern const char xen_hypervisor_callback[];
  31. extern const char xen_failsafe_callback[];
  32. extern void xen_sysenter_target(void);
  33. extern void xen_syscall_target(void);
  34. extern void xen_syscall32_target(void);
  35. /* Amount of extra memory space we add to the e820 ranges */
  36. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  37. /* Number of pages released from the initial allocation. */
  38. unsigned long xen_released_pages;
  39. /*
  40. * The maximum amount of extra memory compared to the base size. The
  41. * main scaling factor is the size of struct page. At extreme ratios
  42. * of base:extra, all the base memory can be filled with page
  43. * structures for the extra memory, leaving no space for anything
  44. * else.
  45. *
  46. * 10x seems like a reasonable balance between scaling flexibility and
  47. * leaving a practically usable system.
  48. */
  49. #define EXTRA_MEM_RATIO (10)
  50. static void __init xen_add_extra_mem(u64 start, u64 size)
  51. {
  52. unsigned long pfn;
  53. int i;
  54. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  55. /* Add new region. */
  56. if (xen_extra_mem[i].size == 0) {
  57. xen_extra_mem[i].start = start;
  58. xen_extra_mem[i].size = size;
  59. break;
  60. }
  61. /* Append to existing region. */
  62. if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
  63. xen_extra_mem[i].size += size;
  64. break;
  65. }
  66. }
  67. if (i == XEN_EXTRA_MEM_MAX_REGIONS)
  68. printk(KERN_WARNING "Warning: not enough extra memory regions\n");
  69. memblock_reserve(start, size);
  70. xen_max_p2m_pfn = PFN_DOWN(start + size);
  71. for (pfn = PFN_DOWN(start); pfn < xen_max_p2m_pfn; pfn++) {
  72. unsigned long mfn = pfn_to_mfn(pfn);
  73. if (WARN(mfn == pfn, "Trying to over-write 1-1 mapping (pfn: %lx)\n", pfn))
  74. continue;
  75. WARN(mfn != INVALID_P2M_ENTRY, "Trying to remove %lx which has %lx mfn!\n",
  76. pfn, mfn);
  77. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  78. }
  79. }
  80. static unsigned long __init xen_do_chunk(unsigned long start,
  81. unsigned long end, bool release)
  82. {
  83. struct xen_memory_reservation reservation = {
  84. .address_bits = 0,
  85. .extent_order = 0,
  86. .domid = DOMID_SELF
  87. };
  88. unsigned long len = 0;
  89. unsigned long pfn;
  90. int ret;
  91. for (pfn = start; pfn < end; pfn++) {
  92. unsigned long frame;
  93. unsigned long mfn = pfn_to_mfn(pfn);
  94. if (release) {
  95. /* Make sure pfn exists to start with */
  96. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  97. continue;
  98. frame = mfn;
  99. } else {
  100. if (mfn != INVALID_P2M_ENTRY)
  101. continue;
  102. frame = pfn;
  103. }
  104. set_xen_guest_handle(reservation.extent_start, &frame);
  105. reservation.nr_extents = 1;
  106. ret = HYPERVISOR_memory_op(release ? XENMEM_decrease_reservation : XENMEM_populate_physmap,
  107. &reservation);
  108. WARN(ret != 1, "Failed to %s pfn %lx err=%d\n",
  109. release ? "release" : "populate", pfn, ret);
  110. if (ret == 1) {
  111. if (!early_set_phys_to_machine(pfn, release ? INVALID_P2M_ENTRY : frame)) {
  112. if (release)
  113. break;
  114. set_xen_guest_handle(reservation.extent_start, &frame);
  115. reservation.nr_extents = 1;
  116. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  117. &reservation);
  118. break;
  119. }
  120. len++;
  121. } else
  122. break;
  123. }
  124. if (len)
  125. printk(KERN_INFO "%s %lx-%lx pfn range: %lu pages %s\n",
  126. release ? "Freeing" : "Populating",
  127. start, end, len,
  128. release ? "freed" : "added");
  129. return len;
  130. }
  131. static unsigned long __init xen_release_chunk(unsigned long start,
  132. unsigned long end)
  133. {
  134. return xen_do_chunk(start, end, true);
  135. }
  136. static unsigned long __init xen_populate_chunk(
  137. const struct e820entry *list, size_t map_size,
  138. unsigned long max_pfn, unsigned long *last_pfn,
  139. unsigned long credits_left)
  140. {
  141. const struct e820entry *entry;
  142. unsigned int i;
  143. unsigned long done = 0;
  144. unsigned long dest_pfn;
  145. for (i = 0, entry = list; i < map_size; i++, entry++) {
  146. unsigned long s_pfn;
  147. unsigned long e_pfn;
  148. unsigned long pfns;
  149. long capacity;
  150. if (credits_left <= 0)
  151. break;
  152. if (entry->type != E820_RAM)
  153. continue;
  154. e_pfn = PFN_DOWN(entry->addr + entry->size);
  155. /* We only care about E820 after the xen_start_info->nr_pages */
  156. if (e_pfn <= max_pfn)
  157. continue;
  158. s_pfn = PFN_UP(entry->addr);
  159. /* If the E820 falls within the nr_pages, we want to start
  160. * at the nr_pages PFN.
  161. * If that would mean going past the E820 entry, skip it
  162. */
  163. if (s_pfn <= max_pfn) {
  164. capacity = e_pfn - max_pfn;
  165. dest_pfn = max_pfn;
  166. } else {
  167. capacity = e_pfn - s_pfn;
  168. dest_pfn = s_pfn;
  169. }
  170. if (credits_left < capacity)
  171. capacity = credits_left;
  172. pfns = xen_do_chunk(dest_pfn, dest_pfn + capacity, false);
  173. done += pfns;
  174. *last_pfn = (dest_pfn + pfns);
  175. if (pfns < capacity)
  176. break;
  177. credits_left -= pfns;
  178. }
  179. return done;
  180. }
  181. static void __init xen_set_identity_and_release_chunk(
  182. unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
  183. unsigned long *released, unsigned long *identity)
  184. {
  185. unsigned long pfn;
  186. /*
  187. * If the PFNs are currently mapped, the VA mapping also needs
  188. * to be updated to be 1:1.
  189. */
  190. for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
  191. (void)HYPERVISOR_update_va_mapping(
  192. (unsigned long)__va(pfn << PAGE_SHIFT),
  193. mfn_pte(pfn, PAGE_KERNEL_IO), 0);
  194. if (start_pfn < nr_pages)
  195. *released += xen_release_chunk(
  196. start_pfn, min(end_pfn, nr_pages));
  197. *identity += set_phys_range_identity(start_pfn, end_pfn);
  198. }
  199. static unsigned long __init xen_set_identity_and_release(
  200. const struct e820entry *list, size_t map_size, unsigned long nr_pages)
  201. {
  202. phys_addr_t start = 0;
  203. unsigned long released = 0;
  204. unsigned long identity = 0;
  205. const struct e820entry *entry;
  206. int i;
  207. /*
  208. * Combine non-RAM regions and gaps until a RAM region (or the
  209. * end of the map) is reached, then set the 1:1 map and
  210. * release the pages (if available) in those non-RAM regions.
  211. *
  212. * The combined non-RAM regions are rounded to a whole number
  213. * of pages so any partial pages are accessible via the 1:1
  214. * mapping. This is needed for some BIOSes that put (for
  215. * example) the DMI tables in a reserved region that begins on
  216. * a non-page boundary.
  217. */
  218. for (i = 0, entry = list; i < map_size; i++, entry++) {
  219. phys_addr_t end = entry->addr + entry->size;
  220. if (entry->type == E820_RAM || i == map_size - 1) {
  221. unsigned long start_pfn = PFN_DOWN(start);
  222. unsigned long end_pfn = PFN_UP(end);
  223. if (entry->type == E820_RAM)
  224. end_pfn = PFN_UP(entry->addr);
  225. if (start_pfn < end_pfn)
  226. xen_set_identity_and_release_chunk(
  227. start_pfn, end_pfn, nr_pages,
  228. &released, &identity);
  229. start = end;
  230. }
  231. }
  232. if (released)
  233. printk(KERN_INFO "Released %lu pages of unused memory\n", released);
  234. if (identity)
  235. printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
  236. return released;
  237. }
  238. static unsigned long __init xen_get_max_pages(void)
  239. {
  240. unsigned long max_pages = MAX_DOMAIN_PAGES;
  241. domid_t domid = DOMID_SELF;
  242. int ret;
  243. /*
  244. * For the initial domain we use the maximum reservation as
  245. * the maximum page.
  246. *
  247. * For guest domains the current maximum reservation reflects
  248. * the current maximum rather than the static maximum. In this
  249. * case the e820 map provided to us will cover the static
  250. * maximum region.
  251. */
  252. if (xen_initial_domain()) {
  253. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  254. if (ret > 0)
  255. max_pages = ret;
  256. }
  257. return min(max_pages, MAX_DOMAIN_PAGES);
  258. }
  259. static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
  260. {
  261. u64 end = start + size;
  262. /* Align RAM regions to page boundaries. */
  263. if (type == E820_RAM) {
  264. start = PAGE_ALIGN(start);
  265. end &= ~((u64)PAGE_SIZE - 1);
  266. }
  267. e820_add_region(start, end - start, type);
  268. }
  269. /**
  270. * machine_specific_memory_setup - Hook for machine specific memory setup.
  271. **/
  272. char * __init xen_memory_setup(void)
  273. {
  274. static struct e820entry map[E820MAX] __initdata;
  275. unsigned long max_pfn = xen_start_info->nr_pages;
  276. unsigned long long mem_end;
  277. int rc;
  278. struct xen_memory_map memmap;
  279. unsigned long max_pages;
  280. unsigned long last_pfn = 0;
  281. unsigned long extra_pages = 0;
  282. unsigned long populated;
  283. int i;
  284. int op;
  285. max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
  286. mem_end = PFN_PHYS(max_pfn);
  287. memmap.nr_entries = E820MAX;
  288. set_xen_guest_handle(memmap.buffer, map);
  289. op = xen_initial_domain() ?
  290. XENMEM_machine_memory_map :
  291. XENMEM_memory_map;
  292. rc = HYPERVISOR_memory_op(op, &memmap);
  293. if (rc == -ENOSYS) {
  294. BUG_ON(xen_initial_domain());
  295. memmap.nr_entries = 1;
  296. map[0].addr = 0ULL;
  297. map[0].size = mem_end;
  298. /* 8MB slack (to balance backend allocations). */
  299. map[0].size += 8ULL << 20;
  300. map[0].type = E820_RAM;
  301. rc = 0;
  302. }
  303. BUG_ON(rc);
  304. /* Make sure the Xen-supplied memory map is well-ordered. */
  305. sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
  306. max_pages = xen_get_max_pages();
  307. if (max_pages > max_pfn)
  308. extra_pages += max_pages - max_pfn;
  309. /*
  310. * Set P2M for all non-RAM pages and E820 gaps to be identity
  311. * type PFNs. Any RAM pages that would be made inaccesible by
  312. * this are first released.
  313. */
  314. xen_released_pages = xen_set_identity_and_release(
  315. map, memmap.nr_entries, max_pfn);
  316. /*
  317. * Populate back the non-RAM pages and E820 gaps that had been
  318. * released. */
  319. populated = xen_populate_chunk(map, memmap.nr_entries,
  320. max_pfn, &last_pfn, xen_released_pages);
  321. xen_released_pages -= populated;
  322. extra_pages += xen_released_pages;
  323. if (last_pfn > max_pfn) {
  324. max_pfn = min(MAX_DOMAIN_PAGES, last_pfn);
  325. mem_end = PFN_PHYS(max_pfn);
  326. }
  327. /*
  328. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  329. * factor the base size. On non-highmem systems, the base
  330. * size is the full initial memory allocation; on highmem it
  331. * is limited to the max size of lowmem, so that it doesn't
  332. * get completely filled.
  333. *
  334. * In principle there could be a problem in lowmem systems if
  335. * the initial memory is also very large with respect to
  336. * lowmem, but we won't try to deal with that here.
  337. */
  338. extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  339. extra_pages);
  340. i = 0;
  341. while (i < memmap.nr_entries) {
  342. u64 addr = map[i].addr;
  343. u64 size = map[i].size;
  344. u32 type = map[i].type;
  345. if (type == E820_RAM) {
  346. if (addr < mem_end) {
  347. size = min(size, mem_end - addr);
  348. } else if (extra_pages) {
  349. size = min(size, (u64)extra_pages * PAGE_SIZE);
  350. extra_pages -= size / PAGE_SIZE;
  351. xen_add_extra_mem(addr, size);
  352. } else
  353. type = E820_UNUSABLE;
  354. }
  355. xen_align_and_add_e820_region(addr, size, type);
  356. map[i].addr += size;
  357. map[i].size -= size;
  358. if (map[i].size == 0)
  359. i++;
  360. }
  361. /*
  362. * In domU, the ISA region is normal, usable memory, but we
  363. * reserve ISA memory anyway because too many things poke
  364. * about in there.
  365. */
  366. e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
  367. E820_RESERVED);
  368. /*
  369. * Reserve Xen bits:
  370. * - mfn_list
  371. * - xen_start_info
  372. * See comment above "struct start_info" in <xen/interface/xen.h>
  373. * We tried to make the the memblock_reserve more selective so
  374. * that it would be clear what region is reserved. Sadly we ran
  375. * in the problem wherein on a 64-bit hypervisor with a 32-bit
  376. * initial domain, the pt_base has the cr3 value which is not
  377. * neccessarily where the pagetable starts! As Jan put it: "
  378. * Actually, the adjustment turns out to be correct: The page
  379. * tables for a 32-on-64 dom0 get allocated in the order "first L1",
  380. * "first L2", "first L3", so the offset to the page table base is
  381. * indeed 2. When reading xen/include/public/xen.h's comment
  382. * very strictly, this is not a violation (since there nothing is said
  383. * that the first thing in the page table space is pointed to by
  384. * pt_base; I admit that this seems to be implied though, namely
  385. * do I think that it is implied that the page table space is the
  386. * range [pt_base, pt_base + nt_pt_frames), whereas that
  387. * range here indeed is [pt_base - 2, pt_base - 2 + nt_pt_frames),
  388. * which - without a priori knowledge - the kernel would have
  389. * difficulty to figure out)." - so lets just fall back to the
  390. * easy way and reserve the whole region.
  391. */
  392. memblock_reserve(__pa(xen_start_info->mfn_list),
  393. xen_start_info->pt_base - xen_start_info->mfn_list);
  394. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  395. return "Xen";
  396. }
  397. /*
  398. * Set the bit indicating "nosegneg" library variants should be used.
  399. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  400. * can have un-truncated segments, so wrapping around is allowed.
  401. */
  402. static void __init fiddle_vdso(void)
  403. {
  404. #ifdef CONFIG_X86_32
  405. u32 *mask;
  406. mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
  407. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  408. mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
  409. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  410. #endif
  411. }
  412. static int __cpuinit register_callback(unsigned type, const void *func)
  413. {
  414. struct callback_register callback = {
  415. .type = type,
  416. .address = XEN_CALLBACK(__KERNEL_CS, func),
  417. .flags = CALLBACKF_mask_events,
  418. };
  419. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  420. }
  421. void __cpuinit xen_enable_sysenter(void)
  422. {
  423. int ret;
  424. unsigned sysenter_feature;
  425. #ifdef CONFIG_X86_32
  426. sysenter_feature = X86_FEATURE_SEP;
  427. #else
  428. sysenter_feature = X86_FEATURE_SYSENTER32;
  429. #endif
  430. if (!boot_cpu_has(sysenter_feature))
  431. return;
  432. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  433. if(ret != 0)
  434. setup_clear_cpu_cap(sysenter_feature);
  435. }
  436. void __cpuinit xen_enable_syscall(void)
  437. {
  438. #ifdef CONFIG_X86_64
  439. int ret;
  440. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  441. if (ret != 0) {
  442. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  443. /* Pretty fatal; 64-bit userspace has no other
  444. mechanism for syscalls. */
  445. }
  446. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  447. ret = register_callback(CALLBACKTYPE_syscall32,
  448. xen_syscall32_target);
  449. if (ret != 0)
  450. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  451. }
  452. #endif /* CONFIG_X86_64 */
  453. }
  454. void __init xen_arch_setup(void)
  455. {
  456. xen_panic_handler_init();
  457. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  458. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  459. if (!xen_feature(XENFEAT_auto_translated_physmap))
  460. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  461. VMASST_TYPE_pae_extended_cr3);
  462. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  463. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  464. BUG();
  465. xen_enable_sysenter();
  466. xen_enable_syscall();
  467. #ifdef CONFIG_ACPI
  468. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  469. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  470. disable_acpi();
  471. }
  472. #endif
  473. memcpy(boot_command_line, xen_start_info->cmd_line,
  474. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  475. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  476. /* Set up idle, making sure it calls safe_halt() pvop */
  477. disable_cpuidle();
  478. disable_cpufreq();
  479. WARN_ON(xen_set_default_idle());
  480. fiddle_vdso();
  481. #ifdef CONFIG_NUMA
  482. numa_off = 1;
  483. #endif
  484. }