setup.c 27 KB

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