setup.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Machine specific setup for xen
  3. *
  4. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  5. */
  6. #include <linux/init.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/api.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/hvc-console.h>
  28. #include "xen-ops.h"
  29. #include "vdso.h"
  30. #include "mmu.h"
  31. #define GB(x) ((uint64_t)(x) * 1024 * 1024 * 1024)
  32. /* Amount of extra memory space we add to the e820 ranges */
  33. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  34. /* Number of pages released from the initial allocation. */
  35. unsigned long xen_released_pages;
  36. /* E820 map used during setting up memory. */
  37. static struct e820_table xen_e820_table __initdata;
  38. /*
  39. * Buffer used to remap identity mapped pages. We only need the virtual space.
  40. * The physical page behind this address is remapped as needed to different
  41. * buffer pages.
  42. */
  43. #define REMAP_SIZE (P2M_PER_PAGE - 3)
  44. static struct {
  45. unsigned long next_area_mfn;
  46. unsigned long target_pfn;
  47. unsigned long size;
  48. unsigned long mfns[REMAP_SIZE];
  49. } xen_remap_buf __initdata __aligned(PAGE_SIZE);
  50. static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
  51. /*
  52. * The maximum amount of extra memory compared to the base size. The
  53. * main scaling factor is the size of struct page. At extreme ratios
  54. * of base:extra, all the base memory can be filled with page
  55. * structures for the extra memory, leaving no space for anything
  56. * else.
  57. *
  58. * 10x seems like a reasonable balance between scaling flexibility and
  59. * leaving a practically usable system.
  60. */
  61. #define EXTRA_MEM_RATIO (10)
  62. static bool xen_512gb_limit __initdata = IS_ENABLED(CONFIG_XEN_512GB);
  63. static void __init xen_parse_512gb(void)
  64. {
  65. bool val = false;
  66. char *arg;
  67. arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit");
  68. if (!arg)
  69. return;
  70. arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit=");
  71. if (!arg)
  72. val = true;
  73. else if (strtobool(arg + strlen("xen_512gb_limit="), &val))
  74. return;
  75. xen_512gb_limit = val;
  76. }
  77. static void __init xen_add_extra_mem(unsigned long start_pfn,
  78. unsigned long n_pfns)
  79. {
  80. int i;
  81. /*
  82. * No need to check for zero size, should happen rarely and will only
  83. * write a new entry regarded to be unused due to zero size.
  84. */
  85. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  86. /* Add new region. */
  87. if (xen_extra_mem[i].n_pfns == 0) {
  88. xen_extra_mem[i].start_pfn = start_pfn;
  89. xen_extra_mem[i].n_pfns = n_pfns;
  90. break;
  91. }
  92. /* Append to existing region. */
  93. if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
  94. start_pfn) {
  95. xen_extra_mem[i].n_pfns += n_pfns;
  96. break;
  97. }
  98. }
  99. if (i == XEN_EXTRA_MEM_MAX_REGIONS)
  100. printk(KERN_WARNING "Warning: not enough extra memory regions\n");
  101. memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
  102. }
  103. static void __init xen_del_extra_mem(unsigned long start_pfn,
  104. unsigned long n_pfns)
  105. {
  106. int i;
  107. unsigned long start_r, size_r;
  108. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  109. start_r = xen_extra_mem[i].start_pfn;
  110. size_r = xen_extra_mem[i].n_pfns;
  111. /* Start of region. */
  112. if (start_r == start_pfn) {
  113. BUG_ON(n_pfns > size_r);
  114. xen_extra_mem[i].start_pfn += n_pfns;
  115. xen_extra_mem[i].n_pfns -= n_pfns;
  116. break;
  117. }
  118. /* End of region. */
  119. if (start_r + size_r == start_pfn + n_pfns) {
  120. BUG_ON(n_pfns > size_r);
  121. xen_extra_mem[i].n_pfns -= n_pfns;
  122. break;
  123. }
  124. /* Mid of region. */
  125. if (start_pfn > start_r && start_pfn < start_r + size_r) {
  126. BUG_ON(start_pfn + n_pfns > start_r + size_r);
  127. xen_extra_mem[i].n_pfns = start_pfn - start_r;
  128. /* Calling memblock_reserve() again is okay. */
  129. xen_add_extra_mem(start_pfn + n_pfns, start_r + size_r -
  130. (start_pfn + n_pfns));
  131. break;
  132. }
  133. }
  134. memblock_free(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
  135. }
  136. /*
  137. * Called during boot before the p2m list can take entries beyond the
  138. * hypervisor supplied p2m list. Entries in extra mem are to be regarded as
  139. * invalid.
  140. */
  141. unsigned long __ref xen_chk_extra_mem(unsigned long pfn)
  142. {
  143. int i;
  144. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  145. if (pfn >= xen_extra_mem[i].start_pfn &&
  146. pfn < xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns)
  147. return INVALID_P2M_ENTRY;
  148. }
  149. return IDENTITY_FRAME(pfn);
  150. }
  151. /*
  152. * Mark all pfns of extra mem as invalid in p2m list.
  153. */
  154. void __init xen_inv_extra_mem(void)
  155. {
  156. unsigned long pfn, pfn_s, pfn_e;
  157. int i;
  158. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  159. if (!xen_extra_mem[i].n_pfns)
  160. continue;
  161. pfn_s = xen_extra_mem[i].start_pfn;
  162. pfn_e = pfn_s + xen_extra_mem[i].n_pfns;
  163. for (pfn = pfn_s; pfn < pfn_e; pfn++)
  164. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  165. }
  166. }
  167. /*
  168. * Finds the next RAM pfn available in the E820 map after min_pfn.
  169. * This function updates min_pfn with the pfn found and returns
  170. * the size of that range or zero if not found.
  171. */
  172. static unsigned long __init xen_find_pfn_range(unsigned long *min_pfn)
  173. {
  174. const struct e820_entry *entry = xen_e820_table.entries;
  175. unsigned int i;
  176. unsigned long done = 0;
  177. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  178. unsigned long s_pfn;
  179. unsigned long e_pfn;
  180. if (entry->type != E820_TYPE_RAM)
  181. continue;
  182. e_pfn = PFN_DOWN(entry->addr + entry->size);
  183. /* We only care about E820 after this */
  184. if (e_pfn <= *min_pfn)
  185. continue;
  186. s_pfn = PFN_UP(entry->addr);
  187. /* If min_pfn falls within the E820 entry, we want to start
  188. * at the min_pfn PFN.
  189. */
  190. if (s_pfn <= *min_pfn) {
  191. done = e_pfn - *min_pfn;
  192. } else {
  193. done = e_pfn - s_pfn;
  194. *min_pfn = s_pfn;
  195. }
  196. break;
  197. }
  198. return done;
  199. }
  200. static int __init xen_free_mfn(unsigned long mfn)
  201. {
  202. struct xen_memory_reservation reservation = {
  203. .address_bits = 0,
  204. .extent_order = 0,
  205. .domid = DOMID_SELF
  206. };
  207. set_xen_guest_handle(reservation.extent_start, &mfn);
  208. reservation.nr_extents = 1;
  209. return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  210. }
  211. /*
  212. * This releases a chunk of memory and then does the identity map. It's used
  213. * as a fallback if the remapping fails.
  214. */
  215. static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn,
  216. unsigned long end_pfn, unsigned long nr_pages)
  217. {
  218. unsigned long pfn, end;
  219. int ret;
  220. WARN_ON(start_pfn > end_pfn);
  221. /* Release pages first. */
  222. end = min(end_pfn, nr_pages);
  223. for (pfn = start_pfn; pfn < end; pfn++) {
  224. unsigned long mfn = pfn_to_mfn(pfn);
  225. /* Make sure pfn exists to start with */
  226. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  227. continue;
  228. ret = xen_free_mfn(mfn);
  229. WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
  230. if (ret == 1) {
  231. xen_released_pages++;
  232. if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY))
  233. break;
  234. } else
  235. break;
  236. }
  237. set_phys_range_identity(start_pfn, end_pfn);
  238. }
  239. /*
  240. * Helper function to update the p2m and m2p tables and kernel mapping.
  241. */
  242. static void __init xen_update_mem_tables(unsigned long pfn, unsigned long mfn)
  243. {
  244. struct mmu_update update = {
  245. .ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
  246. .val = pfn
  247. };
  248. /* Update p2m */
  249. if (!set_phys_to_machine(pfn, mfn)) {
  250. WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n",
  251. pfn, mfn);
  252. BUG();
  253. }
  254. /* Update m2p */
  255. if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) {
  256. WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n",
  257. mfn, pfn);
  258. BUG();
  259. }
  260. /* Update kernel mapping, but not for highmem. */
  261. if (pfn >= PFN_UP(__pa(high_memory - 1)))
  262. return;
  263. if (HYPERVISOR_update_va_mapping((unsigned long)__va(pfn << PAGE_SHIFT),
  264. mfn_pte(mfn, PAGE_KERNEL), 0)) {
  265. WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n",
  266. mfn, pfn);
  267. BUG();
  268. }
  269. }
  270. /*
  271. * This function updates the p2m and m2p tables with an identity map from
  272. * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the
  273. * original allocation at remap_pfn. The information needed for remapping is
  274. * saved in the memory itself to avoid the need for allocating buffers. The
  275. * complete remap information is contained in a list of MFNs each containing
  276. * up to REMAP_SIZE MFNs and the start target PFN for doing the remap.
  277. * This enables us to preserve the original mfn sequence while doing the
  278. * remapping at a time when the memory management is capable of allocating
  279. * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and
  280. * its callers.
  281. */
  282. static void __init xen_do_set_identity_and_remap_chunk(
  283. unsigned long start_pfn, unsigned long size, unsigned long remap_pfn)
  284. {
  285. unsigned long buf = (unsigned long)&xen_remap_buf;
  286. unsigned long mfn_save, mfn;
  287. unsigned long ident_pfn_iter, remap_pfn_iter;
  288. unsigned long ident_end_pfn = start_pfn + size;
  289. unsigned long left = size;
  290. unsigned int i, chunk;
  291. WARN_ON(size == 0);
  292. mfn_save = virt_to_mfn(buf);
  293. for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn;
  294. ident_pfn_iter < ident_end_pfn;
  295. ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) {
  296. chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE;
  297. /* Map first pfn to xen_remap_buf */
  298. mfn = pfn_to_mfn(ident_pfn_iter);
  299. set_pte_mfn(buf, mfn, PAGE_KERNEL);
  300. /* Save mapping information in page */
  301. xen_remap_buf.next_area_mfn = xen_remap_mfn;
  302. xen_remap_buf.target_pfn = remap_pfn_iter;
  303. xen_remap_buf.size = chunk;
  304. for (i = 0; i < chunk; i++)
  305. xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i);
  306. /* Put remap buf into list. */
  307. xen_remap_mfn = mfn;
  308. /* Set identity map */
  309. set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk);
  310. left -= chunk;
  311. }
  312. /* Restore old xen_remap_buf mapping */
  313. set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
  314. }
  315. /*
  316. * This function takes a contiguous pfn range that needs to be identity mapped
  317. * and:
  318. *
  319. * 1) Finds a new range of pfns to use to remap based on E820 and remap_pfn.
  320. * 2) Calls the do_ function to actually do the mapping/remapping work.
  321. *
  322. * The goal is to not allocate additional memory but to remap the existing
  323. * pages. In the case of an error the underlying memory is simply released back
  324. * to Xen and not remapped.
  325. */
  326. static unsigned long __init xen_set_identity_and_remap_chunk(
  327. unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
  328. unsigned long remap_pfn)
  329. {
  330. unsigned long pfn;
  331. unsigned long i = 0;
  332. unsigned long n = end_pfn - start_pfn;
  333. if (remap_pfn == 0)
  334. remap_pfn = nr_pages;
  335. while (i < n) {
  336. unsigned long cur_pfn = start_pfn + i;
  337. unsigned long left = n - i;
  338. unsigned long size = left;
  339. unsigned long remap_range_size;
  340. /* Do not remap pages beyond the current allocation */
  341. if (cur_pfn >= nr_pages) {
  342. /* Identity map remaining pages */
  343. set_phys_range_identity(cur_pfn, cur_pfn + size);
  344. break;
  345. }
  346. if (cur_pfn + size > nr_pages)
  347. size = nr_pages - cur_pfn;
  348. remap_range_size = xen_find_pfn_range(&remap_pfn);
  349. if (!remap_range_size) {
  350. pr_warning("Unable to find available pfn range, not remapping identity pages\n");
  351. xen_set_identity_and_release_chunk(cur_pfn,
  352. cur_pfn + left, nr_pages);
  353. break;
  354. }
  355. /* Adjust size to fit in current e820 RAM region */
  356. if (size > remap_range_size)
  357. size = remap_range_size;
  358. xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn);
  359. /* Update variables to reflect new mappings. */
  360. i += size;
  361. remap_pfn += size;
  362. }
  363. /*
  364. * If the PFNs are currently mapped, the VA mapping also needs
  365. * to be updated to be 1:1.
  366. */
  367. for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
  368. (void)HYPERVISOR_update_va_mapping(
  369. (unsigned long)__va(pfn << PAGE_SHIFT),
  370. mfn_pte(pfn, PAGE_KERNEL_IO), 0);
  371. return remap_pfn;
  372. }
  373. static unsigned long __init xen_count_remap_pages(
  374. unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
  375. unsigned long remap_pages)
  376. {
  377. if (start_pfn >= nr_pages)
  378. return remap_pages;
  379. return remap_pages + min(end_pfn, nr_pages) - start_pfn;
  380. }
  381. static unsigned long __init xen_foreach_remap_area(unsigned long nr_pages,
  382. unsigned long (*func)(unsigned long start_pfn, unsigned long end_pfn,
  383. unsigned long nr_pages, unsigned long last_val))
  384. {
  385. phys_addr_t start = 0;
  386. unsigned long ret_val = 0;
  387. const struct e820_entry *entry = xen_e820_table.entries;
  388. int i;
  389. /*
  390. * Combine non-RAM regions and gaps until a RAM region (or the
  391. * end of the map) is reached, then call the provided function
  392. * to perform its duty on the non-RAM region.
  393. *
  394. * The combined non-RAM regions are rounded to a whole number
  395. * of pages so any partial pages are accessible via the 1:1
  396. * mapping. This is needed for some BIOSes that put (for
  397. * example) the DMI tables in a reserved region that begins on
  398. * a non-page boundary.
  399. */
  400. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  401. phys_addr_t end = entry->addr + entry->size;
  402. if (entry->type == E820_TYPE_RAM || i == xen_e820_table.nr_entries - 1) {
  403. unsigned long start_pfn = PFN_DOWN(start);
  404. unsigned long end_pfn = PFN_UP(end);
  405. if (entry->type == E820_TYPE_RAM)
  406. end_pfn = PFN_UP(entry->addr);
  407. if (start_pfn < end_pfn)
  408. ret_val = func(start_pfn, end_pfn, nr_pages,
  409. ret_val);
  410. start = end;
  411. }
  412. }
  413. return ret_val;
  414. }
  415. /*
  416. * Remap the memory prepared in xen_do_set_identity_and_remap_chunk().
  417. * The remap information (which mfn remap to which pfn) is contained in the
  418. * to be remapped memory itself in a linked list anchored at xen_remap_mfn.
  419. * This scheme allows to remap the different chunks in arbitrary order while
  420. * the resulting mapping will be independant from the order.
  421. */
  422. void __init xen_remap_memory(void)
  423. {
  424. unsigned long buf = (unsigned long)&xen_remap_buf;
  425. unsigned long mfn_save, pfn;
  426. unsigned long remapped = 0;
  427. unsigned int i;
  428. unsigned long pfn_s = ~0UL;
  429. unsigned long len = 0;
  430. mfn_save = virt_to_mfn(buf);
  431. while (xen_remap_mfn != INVALID_P2M_ENTRY) {
  432. /* Map the remap information */
  433. set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
  434. BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
  435. pfn = xen_remap_buf.target_pfn;
  436. for (i = 0; i < xen_remap_buf.size; i++) {
  437. xen_update_mem_tables(pfn, xen_remap_buf.mfns[i]);
  438. remapped++;
  439. pfn++;
  440. }
  441. if (pfn_s == ~0UL || pfn == pfn_s) {
  442. pfn_s = xen_remap_buf.target_pfn;
  443. len += xen_remap_buf.size;
  444. } else if (pfn_s + len == xen_remap_buf.target_pfn) {
  445. len += xen_remap_buf.size;
  446. } else {
  447. xen_del_extra_mem(pfn_s, len);
  448. pfn_s = xen_remap_buf.target_pfn;
  449. len = xen_remap_buf.size;
  450. }
  451. xen_remap_mfn = xen_remap_buf.next_area_mfn;
  452. }
  453. if (pfn_s != ~0UL && len)
  454. xen_del_extra_mem(pfn_s, len);
  455. set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
  456. pr_info("Remapped %ld page(s)\n", remapped);
  457. }
  458. static unsigned long __init xen_get_pages_limit(void)
  459. {
  460. unsigned long limit;
  461. #ifdef CONFIG_X86_32
  462. limit = GB(64) / PAGE_SIZE;
  463. #else
  464. limit = MAXMEM / PAGE_SIZE;
  465. if (!xen_initial_domain() && xen_512gb_limit)
  466. limit = GB(512) / PAGE_SIZE;
  467. #endif
  468. return limit;
  469. }
  470. static unsigned long __init xen_get_max_pages(void)
  471. {
  472. unsigned long max_pages, limit;
  473. domid_t domid = DOMID_SELF;
  474. long ret;
  475. limit = xen_get_pages_limit();
  476. max_pages = limit;
  477. /*
  478. * For the initial domain we use the maximum reservation as
  479. * the maximum page.
  480. *
  481. * For guest domains the current maximum reservation reflects
  482. * the current maximum rather than the static maximum. In this
  483. * case the e820 map provided to us will cover the static
  484. * maximum region.
  485. */
  486. if (xen_initial_domain()) {
  487. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  488. if (ret > 0)
  489. max_pages = ret;
  490. }
  491. return min(max_pages, limit);
  492. }
  493. static void __init xen_align_and_add_e820_region(phys_addr_t start,
  494. phys_addr_t size, int type)
  495. {
  496. phys_addr_t end = start + size;
  497. /* Align RAM regions to page boundaries. */
  498. if (type == E820_TYPE_RAM) {
  499. start = PAGE_ALIGN(start);
  500. end &= ~((phys_addr_t)PAGE_SIZE - 1);
  501. }
  502. e820__range_add(start, end - start, type);
  503. }
  504. static void __init xen_ignore_unusable(void)
  505. {
  506. struct e820_entry *entry = xen_e820_table.entries;
  507. unsigned int i;
  508. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  509. if (entry->type == E820_TYPE_UNUSABLE)
  510. entry->type = E820_TYPE_RAM;
  511. }
  512. }
  513. bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
  514. {
  515. struct e820_entry *entry;
  516. unsigned mapcnt;
  517. phys_addr_t end;
  518. if (!size)
  519. return false;
  520. end = start + size;
  521. entry = xen_e820_table.entries;
  522. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
  523. if (entry->type == E820_TYPE_RAM && entry->addr <= start &&
  524. (entry->addr + entry->size) >= end)
  525. return false;
  526. entry++;
  527. }
  528. return true;
  529. }
  530. /*
  531. * Find a free area in physical memory not yet reserved and compliant with
  532. * E820 map.
  533. * Used to relocate pre-allocated areas like initrd or p2m list which are in
  534. * conflict with the to be used E820 map.
  535. * In case no area is found, return 0. Otherwise return the physical address
  536. * of the area which is already reserved for convenience.
  537. */
  538. phys_addr_t __init xen_find_free_area(phys_addr_t size)
  539. {
  540. unsigned mapcnt;
  541. phys_addr_t addr, start;
  542. struct e820_entry *entry = xen_e820_table.entries;
  543. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++, entry++) {
  544. if (entry->type != E820_TYPE_RAM || entry->size < size)
  545. continue;
  546. start = entry->addr;
  547. for (addr = start; addr < start + size; addr += PAGE_SIZE) {
  548. if (!memblock_is_reserved(addr))
  549. continue;
  550. start = addr + PAGE_SIZE;
  551. if (start + size > entry->addr + entry->size)
  552. break;
  553. }
  554. if (addr >= start + size) {
  555. memblock_reserve(start, size);
  556. return start;
  557. }
  558. }
  559. return 0;
  560. }
  561. /*
  562. * Like memcpy, but with physical addresses for dest and src.
  563. */
  564. static void __init xen_phys_memcpy(phys_addr_t dest, phys_addr_t src,
  565. phys_addr_t n)
  566. {
  567. phys_addr_t dest_off, src_off, dest_len, src_len, len;
  568. void *from, *to;
  569. while (n) {
  570. dest_off = dest & ~PAGE_MASK;
  571. src_off = src & ~PAGE_MASK;
  572. dest_len = n;
  573. if (dest_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off)
  574. dest_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off;
  575. src_len = n;
  576. if (src_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off)
  577. src_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off;
  578. len = min(dest_len, src_len);
  579. to = early_memremap(dest - dest_off, dest_len + dest_off);
  580. from = early_memremap(src - src_off, src_len + src_off);
  581. memcpy(to, from, len);
  582. early_memunmap(to, dest_len + dest_off);
  583. early_memunmap(from, src_len + src_off);
  584. n -= len;
  585. dest += len;
  586. src += len;
  587. }
  588. }
  589. /*
  590. * Reserve Xen mfn_list.
  591. */
  592. static void __init xen_reserve_xen_mfnlist(void)
  593. {
  594. phys_addr_t start, size;
  595. if (xen_start_info->mfn_list >= __START_KERNEL_map) {
  596. start = __pa(xen_start_info->mfn_list);
  597. size = PFN_ALIGN(xen_start_info->nr_pages *
  598. sizeof(unsigned long));
  599. } else {
  600. start = PFN_PHYS(xen_start_info->first_p2m_pfn);
  601. size = PFN_PHYS(xen_start_info->nr_p2m_frames);
  602. }
  603. memblock_reserve(start, size);
  604. if (!xen_is_e820_reserved(start, size))
  605. return;
  606. #ifdef CONFIG_X86_32
  607. /*
  608. * Relocating the p2m on 32 bit system to an arbitrary virtual address
  609. * is not supported, so just give up.
  610. */
  611. xen_raw_console_write("Xen hypervisor allocated p2m list conflicts with E820 map\n");
  612. BUG();
  613. #else
  614. xen_relocate_p2m();
  615. memblock_free(start, size);
  616. #endif
  617. }
  618. /**
  619. * machine_specific_memory_setup - Hook for machine specific memory setup.
  620. **/
  621. char * __init xen_memory_setup(void)
  622. {
  623. unsigned long max_pfn, pfn_s, n_pfns;
  624. phys_addr_t mem_end, addr, size, chunk_size;
  625. u32 type;
  626. int rc;
  627. struct xen_memory_map memmap;
  628. unsigned long max_pages;
  629. unsigned long extra_pages = 0;
  630. int i;
  631. int op;
  632. xen_parse_512gb();
  633. max_pfn = xen_get_pages_limit();
  634. max_pfn = min(max_pfn, xen_start_info->nr_pages);
  635. mem_end = PFN_PHYS(max_pfn);
  636. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  637. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  638. op = xen_initial_domain() ?
  639. XENMEM_machine_memory_map :
  640. XENMEM_memory_map;
  641. rc = HYPERVISOR_memory_op(op, &memmap);
  642. if (rc == -ENOSYS) {
  643. BUG_ON(xen_initial_domain());
  644. memmap.nr_entries = 1;
  645. xen_e820_table.entries[0].addr = 0ULL;
  646. xen_e820_table.entries[0].size = mem_end;
  647. /* 8MB slack (to balance backend allocations). */
  648. xen_e820_table.entries[0].size += 8ULL << 20;
  649. xen_e820_table.entries[0].type = E820_TYPE_RAM;
  650. rc = 0;
  651. }
  652. BUG_ON(rc);
  653. BUG_ON(memmap.nr_entries == 0);
  654. xen_e820_table.nr_entries = memmap.nr_entries;
  655. /*
  656. * Xen won't allow a 1:1 mapping to be created to UNUSABLE
  657. * regions, so if we're using the machine memory map leave the
  658. * region as RAM as it is in the pseudo-physical map.
  659. *
  660. * UNUSABLE regions in domUs are not handled and will need
  661. * a patch in the future.
  662. */
  663. if (xen_initial_domain())
  664. xen_ignore_unusable();
  665. /* Make sure the Xen-supplied memory map is well-ordered. */
  666. e820__update_table(&xen_e820_table);
  667. max_pages = xen_get_max_pages();
  668. /* How many extra pages do we need due to remapping? */
  669. max_pages += xen_foreach_remap_area(max_pfn, xen_count_remap_pages);
  670. if (max_pages > max_pfn)
  671. extra_pages += max_pages - max_pfn;
  672. /*
  673. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  674. * factor the base size. On non-highmem systems, the base
  675. * size is the full initial memory allocation; on highmem it
  676. * is limited to the max size of lowmem, so that it doesn't
  677. * get completely filled.
  678. *
  679. * Make sure we have no memory above max_pages, as this area
  680. * isn't handled by the p2m management.
  681. *
  682. * In principle there could be a problem in lowmem systems if
  683. * the initial memory is also very large with respect to
  684. * lowmem, but we won't try to deal with that here.
  685. */
  686. extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  687. extra_pages, max_pages - max_pfn);
  688. i = 0;
  689. addr = xen_e820_table.entries[0].addr;
  690. size = xen_e820_table.entries[0].size;
  691. while (i < xen_e820_table.nr_entries) {
  692. chunk_size = size;
  693. type = xen_e820_table.entries[i].type;
  694. if (type == E820_TYPE_RAM) {
  695. if (addr < mem_end) {
  696. chunk_size = min(size, mem_end - addr);
  697. } else if (extra_pages) {
  698. chunk_size = min(size, PFN_PHYS(extra_pages));
  699. pfn_s = PFN_UP(addr);
  700. n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s;
  701. extra_pages -= n_pfns;
  702. xen_add_extra_mem(pfn_s, n_pfns);
  703. xen_max_p2m_pfn = pfn_s + n_pfns;
  704. } else
  705. type = E820_TYPE_UNUSABLE;
  706. }
  707. xen_align_and_add_e820_region(addr, chunk_size, type);
  708. addr += chunk_size;
  709. size -= chunk_size;
  710. if (size == 0) {
  711. i++;
  712. if (i < xen_e820_table.nr_entries) {
  713. addr = xen_e820_table.entries[i].addr;
  714. size = xen_e820_table.entries[i].size;
  715. }
  716. }
  717. }
  718. /*
  719. * Set the rest as identity mapped, in case PCI BARs are
  720. * located here.
  721. */
  722. set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
  723. /*
  724. * In domU, the ISA region is normal, usable memory, but we
  725. * reserve ISA memory anyway because too many things poke
  726. * about in there.
  727. */
  728. e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED);
  729. e820__update_table(e820_table);
  730. /*
  731. * Check whether the kernel itself conflicts with the target E820 map.
  732. * Failing now is better than running into weird problems later due
  733. * to relocating (and even reusing) pages with kernel text or data.
  734. */
  735. if (xen_is_e820_reserved(__pa_symbol(_text),
  736. __pa_symbol(__bss_stop) - __pa_symbol(_text))) {
  737. xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n");
  738. BUG();
  739. }
  740. /*
  741. * Check for a conflict of the hypervisor supplied page tables with
  742. * the target E820 map.
  743. */
  744. xen_pt_check_e820();
  745. xen_reserve_xen_mfnlist();
  746. /* Check for a conflict of the initrd with the target E820 map. */
  747. if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
  748. boot_params.hdr.ramdisk_size)) {
  749. phys_addr_t new_area, start, size;
  750. new_area = xen_find_free_area(boot_params.hdr.ramdisk_size);
  751. if (!new_area) {
  752. xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
  753. BUG();
  754. }
  755. start = boot_params.hdr.ramdisk_image;
  756. size = boot_params.hdr.ramdisk_size;
  757. xen_phys_memcpy(new_area, start, size);
  758. pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
  759. start, start + size, new_area, new_area + size);
  760. memblock_free(start, size);
  761. boot_params.hdr.ramdisk_image = new_area;
  762. boot_params.ext_ramdisk_image = new_area >> 32;
  763. }
  764. /*
  765. * Set identity map on non-RAM pages and prepare remapping the
  766. * underlying RAM.
  767. */
  768. xen_foreach_remap_area(max_pfn, xen_set_identity_and_remap_chunk);
  769. pr_info("Released %ld page(s)\n", xen_released_pages);
  770. return "Xen";
  771. }
  772. /*
  773. * Machine specific memory setup for auto-translated guests.
  774. */
  775. char * __init xen_auto_xlated_memory_setup(void)
  776. {
  777. struct xen_memory_map memmap;
  778. int i;
  779. int rc;
  780. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  781. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  782. rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
  783. if (rc < 0)
  784. panic("No memory map (%d)\n", rc);
  785. xen_e820_table.nr_entries = memmap.nr_entries;
  786. e820__update_table(&xen_e820_table);
  787. for (i = 0; i < xen_e820_table.nr_entries; i++)
  788. e820__range_add(xen_e820_table.entries[i].addr, xen_e820_table.entries[i].size, xen_e820_table.entries[i].type);
  789. /* Remove p2m info, it is not needed. */
  790. xen_start_info->mfn_list = 0;
  791. xen_start_info->first_p2m_pfn = 0;
  792. xen_start_info->nr_p2m_frames = 0;
  793. return "Xen";
  794. }
  795. /*
  796. * Set the bit indicating "nosegneg" library variants should be used.
  797. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  798. * can have un-truncated segments, so wrapping around is allowed.
  799. */
  800. static void __init fiddle_vdso(void)
  801. {
  802. #ifdef CONFIG_X86_32
  803. u32 *mask = vdso_image_32.data +
  804. vdso_image_32.sym_VDSO32_NOTE_MASK;
  805. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  806. #endif
  807. }
  808. static int register_callback(unsigned type, const void *func)
  809. {
  810. struct callback_register callback = {
  811. .type = type,
  812. .address = XEN_CALLBACK(__KERNEL_CS, func),
  813. .flags = CALLBACKF_mask_events,
  814. };
  815. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  816. }
  817. void xen_enable_sysenter(void)
  818. {
  819. int ret;
  820. unsigned sysenter_feature;
  821. #ifdef CONFIG_X86_32
  822. sysenter_feature = X86_FEATURE_SEP;
  823. #else
  824. sysenter_feature = X86_FEATURE_SYSENTER32;
  825. #endif
  826. if (!boot_cpu_has(sysenter_feature))
  827. return;
  828. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  829. if(ret != 0)
  830. setup_clear_cpu_cap(sysenter_feature);
  831. }
  832. void xen_enable_syscall(void)
  833. {
  834. #ifdef CONFIG_X86_64
  835. int ret;
  836. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  837. if (ret != 0) {
  838. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  839. /* Pretty fatal; 64-bit userspace has no other
  840. mechanism for syscalls. */
  841. }
  842. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  843. ret = register_callback(CALLBACKTYPE_syscall32,
  844. xen_syscall32_target);
  845. if (ret != 0)
  846. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  847. }
  848. #endif /* CONFIG_X86_64 */
  849. }
  850. void __init xen_pvmmu_arch_setup(void)
  851. {
  852. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  853. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  854. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  855. VMASST_TYPE_pae_extended_cr3);
  856. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  857. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  858. BUG();
  859. xen_enable_sysenter();
  860. xen_enable_syscall();
  861. }
  862. /* This function is not called for HVM domains */
  863. void __init xen_arch_setup(void)
  864. {
  865. xen_panic_handler_init();
  866. xen_pvmmu_arch_setup();
  867. #ifdef CONFIG_ACPI
  868. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  869. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  870. disable_acpi();
  871. }
  872. #endif
  873. memcpy(boot_command_line, xen_start_info->cmd_line,
  874. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  875. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  876. /* Set up idle, making sure it calls safe_halt() pvop */
  877. disable_cpuidle();
  878. disable_cpufreq();
  879. WARN_ON(xen_set_default_idle());
  880. fiddle_vdso();
  881. #ifdef CONFIG_NUMA
  882. numa_off = 1;
  883. #endif
  884. }