setup.c 14 KB

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