setup.c 16 KB

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