setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Machine specific setup for xen
  4. *
  5. * Jeremy Fitzhardinge <[email protected]>, XenSource Inc, 2007
  6. */
  7. #include <linux/init.h>
  8. #include <linux/sched.h>
  9. #include <linux/kstrtox.h>
  10. #include <linux/mm.h>
  11. #include <linux/pm.h>
  12. #include <linux/memblock.h>
  13. #include <linux/cpuidle.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/memory_hotplug.h>
  16. #include <asm/elf.h>
  17. #include <asm/vdso.h>
  18. #include <asm/e820/api.h>
  19. #include <asm/setup.h>
  20. #include <asm/acpi.h>
  21. #include <asm/numa.h>
  22. #include <asm/idtentry.h>
  23. #include <asm/xen/hypervisor.h>
  24. #include <asm/xen/hypercall.h>
  25. #include <xen/xen.h>
  26. #include <xen/page.h>
  27. #include <xen/interface/callback.h>
  28. #include <xen/interface/memory.h>
  29. #include <xen/interface/physdev.h>
  30. #include <xen/features.h>
  31. #include <xen/hvc-console.h>
  32. #include "xen-ops.h"
  33. #include "mmu.h"
  34. #define GB(x) ((uint64_t)(x) * 1024 * 1024 * 1024)
  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. /* E820 map used during setting up memory. */
  40. static struct e820_table xen_e820_table __initdata;
  41. /*
  42. * Buffer used to remap identity mapped pages. We only need the virtual space.
  43. * The physical page behind this address is remapped as needed to different
  44. * buffer pages.
  45. */
  46. #define REMAP_SIZE (P2M_PER_PAGE - 3)
  47. static struct {
  48. unsigned long next_area_mfn;
  49. unsigned long target_pfn;
  50. unsigned long size;
  51. unsigned long mfns[REMAP_SIZE];
  52. } xen_remap_buf __initdata __aligned(PAGE_SIZE);
  53. static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
  54. /*
  55. * The maximum amount of extra memory compared to the base size. The
  56. * main scaling factor is the size of struct page. At extreme ratios
  57. * of base:extra, all the base memory can be filled with page
  58. * structures for the extra memory, leaving no space for anything
  59. * else.
  60. *
  61. * 10x seems like a reasonable balance between scaling flexibility and
  62. * leaving a practically usable system.
  63. */
  64. #define EXTRA_MEM_RATIO (10)
  65. static bool xen_512gb_limit __initdata = IS_ENABLED(CONFIG_XEN_512GB);
  66. static void __init xen_parse_512gb(void)
  67. {
  68. bool val = false;
  69. char *arg;
  70. arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit");
  71. if (!arg)
  72. return;
  73. arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit=");
  74. if (!arg)
  75. val = true;
  76. else if (kstrtobool(arg + strlen("xen_512gb_limit="), &val))
  77. return;
  78. xen_512gb_limit = val;
  79. }
  80. static void __init xen_add_extra_mem(unsigned long start_pfn,
  81. unsigned long n_pfns)
  82. {
  83. int i;
  84. /*
  85. * No need to check for zero size, should happen rarely and will only
  86. * write a new entry regarded to be unused due to zero size.
  87. */
  88. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  89. /* Add new region. */
  90. if (xen_extra_mem[i].n_pfns == 0) {
  91. xen_extra_mem[i].start_pfn = start_pfn;
  92. xen_extra_mem[i].n_pfns = n_pfns;
  93. break;
  94. }
  95. /* Append to existing region. */
  96. if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
  97. start_pfn) {
  98. xen_extra_mem[i].n_pfns += n_pfns;
  99. break;
  100. }
  101. }
  102. if (i == XEN_EXTRA_MEM_MAX_REGIONS)
  103. printk(KERN_WARNING "Warning: not enough extra memory regions\n");
  104. memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
  105. }
  106. static void __init xen_del_extra_mem(unsigned long start_pfn,
  107. unsigned long n_pfns)
  108. {
  109. int i;
  110. unsigned long start_r, size_r;
  111. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  112. start_r = xen_extra_mem[i].start_pfn;
  113. size_r = xen_extra_mem[i].n_pfns;
  114. /* Start of region. */
  115. if (start_r == start_pfn) {
  116. BUG_ON(n_pfns > size_r);
  117. xen_extra_mem[i].start_pfn += n_pfns;
  118. xen_extra_mem[i].n_pfns -= n_pfns;
  119. break;
  120. }
  121. /* End of region. */
  122. if (start_r + size_r == start_pfn + n_pfns) {
  123. BUG_ON(n_pfns > size_r);
  124. xen_extra_mem[i].n_pfns -= n_pfns;
  125. break;
  126. }
  127. /* Mid of region. */
  128. if (start_pfn > start_r && start_pfn < start_r + size_r) {
  129. BUG_ON(start_pfn + n_pfns > start_r + size_r);
  130. xen_extra_mem[i].n_pfns = start_pfn - start_r;
  131. /* Calling memblock_reserve() again is okay. */
  132. xen_add_extra_mem(start_pfn + n_pfns, start_r + size_r -
  133. (start_pfn + n_pfns));
  134. break;
  135. }
  136. }
  137. memblock_phys_free(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
  138. }
  139. /*
  140. * Called during boot before the p2m list can take entries beyond the
  141. * hypervisor supplied p2m list. Entries in extra mem are to be regarded as
  142. * invalid.
  143. */
  144. unsigned long __ref xen_chk_extra_mem(unsigned long pfn)
  145. {
  146. int i;
  147. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  148. if (pfn >= xen_extra_mem[i].start_pfn &&
  149. pfn < xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns)
  150. return INVALID_P2M_ENTRY;
  151. }
  152. return IDENTITY_FRAME(pfn);
  153. }
  154. /*
  155. * Mark all pfns of extra mem as invalid in p2m list.
  156. */
  157. void __init xen_inv_extra_mem(void)
  158. {
  159. unsigned long pfn, pfn_s, pfn_e;
  160. int i;
  161. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  162. if (!xen_extra_mem[i].n_pfns)
  163. continue;
  164. pfn_s = xen_extra_mem[i].start_pfn;
  165. pfn_e = pfn_s + xen_extra_mem[i].n_pfns;
  166. for (pfn = pfn_s; pfn < pfn_e; pfn++)
  167. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  168. }
  169. }
  170. /*
  171. * Finds the next RAM pfn available in the E820 map after min_pfn.
  172. * This function updates min_pfn with the pfn found and returns
  173. * the size of that range or zero if not found.
  174. */
  175. static unsigned long __init xen_find_pfn_range(unsigned long *min_pfn)
  176. {
  177. const struct e820_entry *entry = xen_e820_table.entries;
  178. unsigned int i;
  179. unsigned long done = 0;
  180. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  181. unsigned long s_pfn;
  182. unsigned long e_pfn;
  183. if (entry->type != E820_TYPE_RAM)
  184. continue;
  185. e_pfn = PFN_DOWN(entry->addr + entry->size);
  186. /* We only care about E820 after this */
  187. if (e_pfn <= *min_pfn)
  188. continue;
  189. s_pfn = PFN_UP(entry->addr);
  190. /* If min_pfn falls within the E820 entry, we want to start
  191. * at the min_pfn PFN.
  192. */
  193. if (s_pfn <= *min_pfn) {
  194. done = e_pfn - *min_pfn;
  195. } else {
  196. done = e_pfn - s_pfn;
  197. *min_pfn = s_pfn;
  198. }
  199. break;
  200. }
  201. return done;
  202. }
  203. static int __init xen_free_mfn(unsigned long mfn)
  204. {
  205. struct xen_memory_reservation reservation = {
  206. .address_bits = 0,
  207. .extent_order = 0,
  208. .domid = DOMID_SELF
  209. };
  210. set_xen_guest_handle(reservation.extent_start, &mfn);
  211. reservation.nr_extents = 1;
  212. return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  213. }
  214. /*
  215. * This releases a chunk of memory and then does the identity map. It's used
  216. * as a fallback if the remapping fails.
  217. */
  218. static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn,
  219. unsigned long end_pfn, unsigned long nr_pages)
  220. {
  221. unsigned long pfn, end;
  222. int ret;
  223. WARN_ON(start_pfn > end_pfn);
  224. /* Release pages first. */
  225. end = min(end_pfn, nr_pages);
  226. for (pfn = start_pfn; pfn < end; pfn++) {
  227. unsigned long mfn = pfn_to_mfn(pfn);
  228. /* Make sure pfn exists to start with */
  229. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  230. continue;
  231. ret = xen_free_mfn(mfn);
  232. WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
  233. if (ret == 1) {
  234. xen_released_pages++;
  235. if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY))
  236. break;
  237. } else
  238. break;
  239. }
  240. set_phys_range_identity(start_pfn, end_pfn);
  241. }
  242. /*
  243. * Helper function to update the p2m and m2p tables and kernel mapping.
  244. */
  245. static void __init xen_update_mem_tables(unsigned long pfn, unsigned long mfn)
  246. {
  247. struct mmu_update update = {
  248. .ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
  249. .val = pfn
  250. };
  251. /* Update p2m */
  252. if (!set_phys_to_machine(pfn, mfn)) {
  253. WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n",
  254. pfn, mfn);
  255. BUG();
  256. }
  257. /* Update m2p */
  258. if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) {
  259. WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n",
  260. mfn, pfn);
  261. BUG();
  262. }
  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_warn("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, their VA mappings need to be
  365. * zapped.
  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. native_make_pte(0), 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 independent 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. limit = MAXMEM / PAGE_SIZE;
  462. if (!xen_initial_domain() && xen_512gb_limit)
  463. limit = GB(512) / PAGE_SIZE;
  464. return limit;
  465. }
  466. static unsigned long __init xen_get_max_pages(void)
  467. {
  468. unsigned long max_pages, limit;
  469. domid_t domid = DOMID_SELF;
  470. long ret;
  471. limit = xen_get_pages_limit();
  472. max_pages = limit;
  473. /*
  474. * For the initial domain we use the maximum reservation as
  475. * the maximum page.
  476. *
  477. * For guest domains the current maximum reservation reflects
  478. * the current maximum rather than the static maximum. In this
  479. * case the e820 map provided to us will cover the static
  480. * maximum region.
  481. */
  482. if (xen_initial_domain()) {
  483. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  484. if (ret > 0)
  485. max_pages = ret;
  486. }
  487. return min(max_pages, limit);
  488. }
  489. static void __init xen_align_and_add_e820_region(phys_addr_t start,
  490. phys_addr_t size, int type)
  491. {
  492. phys_addr_t end = start + size;
  493. /* Align RAM regions to page boundaries. */
  494. if (type == E820_TYPE_RAM) {
  495. start = PAGE_ALIGN(start);
  496. end &= ~((phys_addr_t)PAGE_SIZE - 1);
  497. #ifdef CONFIG_MEMORY_HOTPLUG
  498. /*
  499. * Don't allow adding memory not in E820 map while booting the
  500. * system. Once the balloon driver is up it will remove that
  501. * restriction again.
  502. */
  503. max_mem_size = end;
  504. #endif
  505. }
  506. e820__range_add(start, end - start, type);
  507. }
  508. static void __init xen_ignore_unusable(void)
  509. {
  510. struct e820_entry *entry = xen_e820_table.entries;
  511. unsigned int i;
  512. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  513. if (entry->type == E820_TYPE_UNUSABLE)
  514. entry->type = E820_TYPE_RAM;
  515. }
  516. }
  517. bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
  518. {
  519. struct e820_entry *entry;
  520. unsigned mapcnt;
  521. phys_addr_t end;
  522. if (!size)
  523. return false;
  524. end = start + size;
  525. entry = xen_e820_table.entries;
  526. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
  527. if (entry->type == E820_TYPE_RAM && entry->addr <= start &&
  528. (entry->addr + entry->size) >= end)
  529. return false;
  530. entry++;
  531. }
  532. return true;
  533. }
  534. /*
  535. * Find a free area in physical memory not yet reserved and compliant with
  536. * E820 map.
  537. * Used to relocate pre-allocated areas like initrd or p2m list which are in
  538. * conflict with the to be used E820 map.
  539. * In case no area is found, return 0. Otherwise return the physical address
  540. * of the area which is already reserved for convenience.
  541. */
  542. phys_addr_t __init xen_find_free_area(phys_addr_t size)
  543. {
  544. unsigned mapcnt;
  545. phys_addr_t addr, start;
  546. struct e820_entry *entry = xen_e820_table.entries;
  547. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++, entry++) {
  548. if (entry->type != E820_TYPE_RAM || entry->size < size)
  549. continue;
  550. start = entry->addr;
  551. for (addr = start; addr < start + size; addr += PAGE_SIZE) {
  552. if (!memblock_is_reserved(addr))
  553. continue;
  554. start = addr + PAGE_SIZE;
  555. if (start + size > entry->addr + entry->size)
  556. break;
  557. }
  558. if (addr >= start + size) {
  559. memblock_reserve(start, size);
  560. return start;
  561. }
  562. }
  563. return 0;
  564. }
  565. /*
  566. * Like memcpy, but with physical addresses for dest and src.
  567. */
  568. static void __init xen_phys_memcpy(phys_addr_t dest, phys_addr_t src,
  569. phys_addr_t n)
  570. {
  571. phys_addr_t dest_off, src_off, dest_len, src_len, len;
  572. void *from, *to;
  573. while (n) {
  574. dest_off = dest & ~PAGE_MASK;
  575. src_off = src & ~PAGE_MASK;
  576. dest_len = n;
  577. if (dest_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off)
  578. dest_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off;
  579. src_len = n;
  580. if (src_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off)
  581. src_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off;
  582. len = min(dest_len, src_len);
  583. to = early_memremap(dest - dest_off, dest_len + dest_off);
  584. from = early_memremap(src - src_off, src_len + src_off);
  585. memcpy(to, from, len);
  586. early_memunmap(to, dest_len + dest_off);
  587. early_memunmap(from, src_len + src_off);
  588. n -= len;
  589. dest += len;
  590. src += len;
  591. }
  592. }
  593. /*
  594. * Reserve Xen mfn_list.
  595. */
  596. static void __init xen_reserve_xen_mfnlist(void)
  597. {
  598. phys_addr_t start, size;
  599. if (xen_start_info->mfn_list >= __START_KERNEL_map) {
  600. start = __pa(xen_start_info->mfn_list);
  601. size = PFN_ALIGN(xen_start_info->nr_pages *
  602. sizeof(unsigned long));
  603. } else {
  604. start = PFN_PHYS(xen_start_info->first_p2m_pfn);
  605. size = PFN_PHYS(xen_start_info->nr_p2m_frames);
  606. }
  607. memblock_reserve(start, size);
  608. if (!xen_is_e820_reserved(start, size))
  609. return;
  610. xen_relocate_p2m();
  611. memblock_phys_free(start, size);
  612. }
  613. /**
  614. * xen_memory_setup - Hook for machine specific memory setup.
  615. **/
  616. char * __init xen_memory_setup(void)
  617. {
  618. unsigned long max_pfn, pfn_s, n_pfns;
  619. phys_addr_t mem_end, addr, size, chunk_size;
  620. u32 type;
  621. int rc;
  622. struct xen_memory_map memmap;
  623. unsigned long max_pages;
  624. unsigned long extra_pages = 0;
  625. int i;
  626. int op;
  627. xen_parse_512gb();
  628. max_pfn = xen_get_pages_limit();
  629. max_pfn = min(max_pfn, xen_start_info->nr_pages);
  630. mem_end = PFN_PHYS(max_pfn);
  631. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  632. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  633. #if defined(CONFIG_MEMORY_HOTPLUG) && defined(CONFIG_XEN_BALLOON)
  634. xen_saved_max_mem_size = max_mem_size;
  635. #endif
  636. op = xen_initial_domain() ?
  637. XENMEM_machine_memory_map :
  638. XENMEM_memory_map;
  639. rc = HYPERVISOR_memory_op(op, &memmap);
  640. if (rc == -ENOSYS) {
  641. BUG_ON(xen_initial_domain());
  642. memmap.nr_entries = 1;
  643. xen_e820_table.entries[0].addr = 0ULL;
  644. xen_e820_table.entries[0].size = mem_end;
  645. /* 8MB slack (to balance backend allocations). */
  646. xen_e820_table.entries[0].size += 8ULL << 20;
  647. xen_e820_table.entries[0].type = E820_TYPE_RAM;
  648. rc = 0;
  649. }
  650. BUG_ON(rc);
  651. BUG_ON(memmap.nr_entries == 0);
  652. xen_e820_table.nr_entries = memmap.nr_entries;
  653. /*
  654. * Xen won't allow a 1:1 mapping to be created to UNUSABLE
  655. * regions, so if we're using the machine memory map leave the
  656. * region as RAM as it is in the pseudo-physical map.
  657. *
  658. * UNUSABLE regions in domUs are not handled and will need
  659. * a patch in the future.
  660. */
  661. if (xen_initial_domain())
  662. xen_ignore_unusable();
  663. /* Make sure the Xen-supplied memory map is well-ordered. */
  664. e820__update_table(&xen_e820_table);
  665. max_pages = xen_get_max_pages();
  666. /* How many extra pages do we need due to remapping? */
  667. max_pages += xen_foreach_remap_area(max_pfn, xen_count_remap_pages);
  668. if (max_pages > max_pfn)
  669. extra_pages += max_pages - max_pfn;
  670. /*
  671. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  672. * factor the base size.
  673. *
  674. * Make sure we have no memory above max_pages, as this area
  675. * isn't handled by the p2m management.
  676. */
  677. extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  678. extra_pages, max_pages - max_pfn);
  679. i = 0;
  680. addr = xen_e820_table.entries[0].addr;
  681. size = xen_e820_table.entries[0].size;
  682. while (i < xen_e820_table.nr_entries) {
  683. bool discard = false;
  684. chunk_size = size;
  685. type = xen_e820_table.entries[i].type;
  686. if (type == E820_TYPE_RAM) {
  687. if (addr < mem_end) {
  688. chunk_size = min(size, mem_end - addr);
  689. } else if (extra_pages) {
  690. chunk_size = min(size, PFN_PHYS(extra_pages));
  691. pfn_s = PFN_UP(addr);
  692. n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s;
  693. extra_pages -= n_pfns;
  694. xen_add_extra_mem(pfn_s, n_pfns);
  695. xen_max_p2m_pfn = pfn_s + n_pfns;
  696. } else
  697. discard = true;
  698. }
  699. if (!discard)
  700. xen_align_and_add_e820_region(addr, chunk_size, type);
  701. addr += chunk_size;
  702. size -= chunk_size;
  703. if (size == 0) {
  704. i++;
  705. if (i < xen_e820_table.nr_entries) {
  706. addr = xen_e820_table.entries[i].addr;
  707. size = xen_e820_table.entries[i].size;
  708. }
  709. }
  710. }
  711. /*
  712. * Set the rest as identity mapped, in case PCI BARs are
  713. * located here.
  714. */
  715. set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
  716. /*
  717. * In domU, the ISA region is normal, usable memory, but we
  718. * reserve ISA memory anyway because too many things poke
  719. * about in there.
  720. */
  721. e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED);
  722. e820__update_table(e820_table);
  723. /*
  724. * Check whether the kernel itself conflicts with the target E820 map.
  725. * Failing now is better than running into weird problems later due
  726. * to relocating (and even reusing) pages with kernel text or data.
  727. */
  728. if (xen_is_e820_reserved(__pa_symbol(_text),
  729. __pa_symbol(__bss_stop) - __pa_symbol(_text))) {
  730. xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n");
  731. BUG();
  732. }
  733. /*
  734. * Check for a conflict of the hypervisor supplied page tables with
  735. * the target E820 map.
  736. */
  737. xen_pt_check_e820();
  738. xen_reserve_xen_mfnlist();
  739. /* Check for a conflict of the initrd with the target E820 map. */
  740. if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
  741. boot_params.hdr.ramdisk_size)) {
  742. phys_addr_t new_area, start, size;
  743. new_area = xen_find_free_area(boot_params.hdr.ramdisk_size);
  744. if (!new_area) {
  745. xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
  746. BUG();
  747. }
  748. start = boot_params.hdr.ramdisk_image;
  749. size = boot_params.hdr.ramdisk_size;
  750. xen_phys_memcpy(new_area, start, size);
  751. pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
  752. start, start + size, new_area, new_area + size);
  753. memblock_phys_free(start, size);
  754. boot_params.hdr.ramdisk_image = new_area;
  755. boot_params.ext_ramdisk_image = new_area >> 32;
  756. }
  757. /*
  758. * Set identity map on non-RAM pages and prepare remapping the
  759. * underlying RAM.
  760. */
  761. xen_foreach_remap_area(max_pfn, xen_set_identity_and_remap_chunk);
  762. pr_info("Released %ld page(s)\n", xen_released_pages);
  763. return "Xen";
  764. }
  765. static int register_callback(unsigned type, const void *func)
  766. {
  767. struct callback_register callback = {
  768. .type = type,
  769. .address = XEN_CALLBACK(__KERNEL_CS, func),
  770. .flags = CALLBACKF_mask_events,
  771. };
  772. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  773. }
  774. void xen_enable_sysenter(void)
  775. {
  776. if (cpu_feature_enabled(X86_FEATURE_SYSENTER32) &&
  777. register_callback(CALLBACKTYPE_sysenter, xen_entry_SYSENTER_compat))
  778. setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
  779. }
  780. void xen_enable_syscall(void)
  781. {
  782. int ret;
  783. ret = register_callback(CALLBACKTYPE_syscall, xen_entry_SYSCALL_64);
  784. if (ret != 0) {
  785. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  786. /* Pretty fatal; 64-bit userspace has no other
  787. mechanism for syscalls. */
  788. }
  789. if (cpu_feature_enabled(X86_FEATURE_SYSCALL32) &&
  790. register_callback(CALLBACKTYPE_syscall32, xen_entry_SYSCALL_compat))
  791. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  792. }
  793. static void __init xen_pvmmu_arch_setup(void)
  794. {
  795. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  796. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  797. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  798. VMASST_TYPE_pae_extended_cr3);
  799. if (register_callback(CALLBACKTYPE_event,
  800. xen_asm_exc_xen_hypervisor_callback) ||
  801. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  802. BUG();
  803. xen_enable_sysenter();
  804. xen_enable_syscall();
  805. }
  806. /* This function is not called for HVM domains */
  807. void __init xen_arch_setup(void)
  808. {
  809. xen_panic_handler_init();
  810. xen_pvmmu_arch_setup();
  811. #ifdef CONFIG_ACPI
  812. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  813. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  814. disable_acpi();
  815. }
  816. #endif
  817. memcpy(boot_command_line, xen_start_info->cmd_line,
  818. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  819. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  820. /* Set up idle, making sure it calls safe_halt() pvop */
  821. disable_cpuidle();
  822. disable_cpufreq();
  823. WARN_ON(xen_set_default_idle());
  824. #ifdef CONFIG_NUMA
  825. numa_off = 1;
  826. #endif
  827. }