setup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. BUG_ON(xen_feature(XENFEAT_auto_translated_physmap));
  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, mfn, 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. mfn = xen_remap_buf.mfns[i];
  439. xen_update_mem_tables(pfn, mfn);
  440. remapped++;
  441. pfn++;
  442. }
  443. if (pfn_s == ~0UL || pfn == pfn_s) {
  444. pfn_s = xen_remap_buf.target_pfn;
  445. len += xen_remap_buf.size;
  446. } else if (pfn_s + len == xen_remap_buf.target_pfn) {
  447. len += xen_remap_buf.size;
  448. } else {
  449. xen_del_extra_mem(pfn_s, len);
  450. pfn_s = xen_remap_buf.target_pfn;
  451. len = xen_remap_buf.size;
  452. }
  453. mfn = xen_remap_mfn;
  454. xen_remap_mfn = xen_remap_buf.next_area_mfn;
  455. }
  456. if (pfn_s != ~0UL && len)
  457. xen_del_extra_mem(pfn_s, len);
  458. set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
  459. pr_info("Remapped %ld page(s)\n", remapped);
  460. }
  461. static unsigned long __init xen_get_pages_limit(void)
  462. {
  463. unsigned long limit;
  464. #ifdef CONFIG_X86_32
  465. limit = GB(64) / PAGE_SIZE;
  466. #else
  467. limit = MAXMEM / PAGE_SIZE;
  468. if (!xen_initial_domain() && xen_512gb_limit)
  469. limit = GB(512) / PAGE_SIZE;
  470. #endif
  471. return limit;
  472. }
  473. static unsigned long __init xen_get_max_pages(void)
  474. {
  475. unsigned long max_pages, limit;
  476. domid_t domid = DOMID_SELF;
  477. long ret;
  478. limit = xen_get_pages_limit();
  479. max_pages = limit;
  480. /*
  481. * For the initial domain we use the maximum reservation as
  482. * the maximum page.
  483. *
  484. * For guest domains the current maximum reservation reflects
  485. * the current maximum rather than the static maximum. In this
  486. * case the e820 map provided to us will cover the static
  487. * maximum region.
  488. */
  489. if (xen_initial_domain()) {
  490. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  491. if (ret > 0)
  492. max_pages = ret;
  493. }
  494. return min(max_pages, limit);
  495. }
  496. static void __init xen_align_and_add_e820_region(phys_addr_t start,
  497. phys_addr_t size, int type)
  498. {
  499. phys_addr_t end = start + size;
  500. /* Align RAM regions to page boundaries. */
  501. if (type == E820_TYPE_RAM) {
  502. start = PAGE_ALIGN(start);
  503. end &= ~((phys_addr_t)PAGE_SIZE - 1);
  504. }
  505. e820__range_add(start, end - start, type);
  506. }
  507. static void __init xen_ignore_unusable(void)
  508. {
  509. struct e820_entry *entry = xen_e820_table.entries;
  510. unsigned int i;
  511. for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
  512. if (entry->type == E820_TYPE_UNUSABLE)
  513. entry->type = E820_TYPE_RAM;
  514. }
  515. }
  516. bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
  517. {
  518. struct e820_entry *entry;
  519. unsigned mapcnt;
  520. phys_addr_t end;
  521. if (!size)
  522. return false;
  523. end = start + size;
  524. entry = xen_e820_table.entries;
  525. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
  526. if (entry->type == E820_TYPE_RAM && entry->addr <= start &&
  527. (entry->addr + entry->size) >= end)
  528. return false;
  529. entry++;
  530. }
  531. return true;
  532. }
  533. /*
  534. * Find a free area in physical memory not yet reserved and compliant with
  535. * E820 map.
  536. * Used to relocate pre-allocated areas like initrd or p2m list which are in
  537. * conflict with the to be used E820 map.
  538. * In case no area is found, return 0. Otherwise return the physical address
  539. * of the area which is already reserved for convenience.
  540. */
  541. phys_addr_t __init xen_find_free_area(phys_addr_t size)
  542. {
  543. unsigned mapcnt;
  544. phys_addr_t addr, start;
  545. struct e820_entry *entry = xen_e820_table.entries;
  546. for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++, entry++) {
  547. if (entry->type != E820_TYPE_RAM || entry->size < size)
  548. continue;
  549. start = entry->addr;
  550. for (addr = start; addr < start + size; addr += PAGE_SIZE) {
  551. if (!memblock_is_reserved(addr))
  552. continue;
  553. start = addr + PAGE_SIZE;
  554. if (start + size > entry->addr + entry->size)
  555. break;
  556. }
  557. if (addr >= start + size) {
  558. memblock_reserve(start, size);
  559. return start;
  560. }
  561. }
  562. return 0;
  563. }
  564. /*
  565. * Like memcpy, but with physical addresses for dest and src.
  566. */
  567. static void __init xen_phys_memcpy(phys_addr_t dest, phys_addr_t src,
  568. phys_addr_t n)
  569. {
  570. phys_addr_t dest_off, src_off, dest_len, src_len, len;
  571. void *from, *to;
  572. while (n) {
  573. dest_off = dest & ~PAGE_MASK;
  574. src_off = src & ~PAGE_MASK;
  575. dest_len = n;
  576. if (dest_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off)
  577. dest_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off;
  578. src_len = n;
  579. if (src_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off)
  580. src_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off;
  581. len = min(dest_len, src_len);
  582. to = early_memremap(dest - dest_off, dest_len + dest_off);
  583. from = early_memremap(src - src_off, src_len + src_off);
  584. memcpy(to, from, len);
  585. early_memunmap(to, dest_len + dest_off);
  586. early_memunmap(from, src_len + src_off);
  587. n -= len;
  588. dest += len;
  589. src += len;
  590. }
  591. }
  592. /*
  593. * Reserve Xen mfn_list.
  594. */
  595. static void __init xen_reserve_xen_mfnlist(void)
  596. {
  597. phys_addr_t start, size;
  598. if (xen_start_info->mfn_list >= __START_KERNEL_map) {
  599. start = __pa(xen_start_info->mfn_list);
  600. size = PFN_ALIGN(xen_start_info->nr_pages *
  601. sizeof(unsigned long));
  602. } else {
  603. start = PFN_PHYS(xen_start_info->first_p2m_pfn);
  604. size = PFN_PHYS(xen_start_info->nr_p2m_frames);
  605. }
  606. memblock_reserve(start, size);
  607. if (!xen_is_e820_reserved(start, size))
  608. return;
  609. #ifdef CONFIG_X86_32
  610. /*
  611. * Relocating the p2m on 32 bit system to an arbitrary virtual address
  612. * is not supported, so just give up.
  613. */
  614. xen_raw_console_write("Xen hypervisor allocated p2m list conflicts with E820 map\n");
  615. BUG();
  616. #else
  617. xen_relocate_p2m();
  618. memblock_free(start, size);
  619. #endif
  620. }
  621. /**
  622. * machine_specific_memory_setup - Hook for machine specific memory setup.
  623. **/
  624. char * __init xen_memory_setup(void)
  625. {
  626. unsigned long max_pfn, pfn_s, n_pfns;
  627. phys_addr_t mem_end, addr, size, chunk_size;
  628. u32 type;
  629. int rc;
  630. struct xen_memory_map memmap;
  631. unsigned long max_pages;
  632. unsigned long extra_pages = 0;
  633. int i;
  634. int op;
  635. xen_parse_512gb();
  636. max_pfn = xen_get_pages_limit();
  637. max_pfn = min(max_pfn, xen_start_info->nr_pages);
  638. mem_end = PFN_PHYS(max_pfn);
  639. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  640. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  641. op = xen_initial_domain() ?
  642. XENMEM_machine_memory_map :
  643. XENMEM_memory_map;
  644. rc = HYPERVISOR_memory_op(op, &memmap);
  645. if (rc == -ENOSYS) {
  646. BUG_ON(xen_initial_domain());
  647. memmap.nr_entries = 1;
  648. xen_e820_table.entries[0].addr = 0ULL;
  649. xen_e820_table.entries[0].size = mem_end;
  650. /* 8MB slack (to balance backend allocations). */
  651. xen_e820_table.entries[0].size += 8ULL << 20;
  652. xen_e820_table.entries[0].type = E820_TYPE_RAM;
  653. rc = 0;
  654. }
  655. BUG_ON(rc);
  656. BUG_ON(memmap.nr_entries == 0);
  657. xen_e820_table.nr_entries = memmap.nr_entries;
  658. /*
  659. * Xen won't allow a 1:1 mapping to be created to UNUSABLE
  660. * regions, so if we're using the machine memory map leave the
  661. * region as RAM as it is in the pseudo-physical map.
  662. *
  663. * UNUSABLE regions in domUs are not handled and will need
  664. * a patch in the future.
  665. */
  666. if (xen_initial_domain())
  667. xen_ignore_unusable();
  668. /* Make sure the Xen-supplied memory map is well-ordered. */
  669. e820__update_table(&xen_e820_table);
  670. max_pages = xen_get_max_pages();
  671. /* How many extra pages do we need due to remapping? */
  672. max_pages += xen_foreach_remap_area(max_pfn, xen_count_remap_pages);
  673. if (max_pages > max_pfn)
  674. extra_pages += max_pages - max_pfn;
  675. /*
  676. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  677. * factor the base size. On non-highmem systems, the base
  678. * size is the full initial memory allocation; on highmem it
  679. * is limited to the max size of lowmem, so that it doesn't
  680. * get completely filled.
  681. *
  682. * Make sure we have no memory above max_pages, as this area
  683. * isn't handled by the p2m management.
  684. *
  685. * In principle there could be a problem in lowmem systems if
  686. * the initial memory is also very large with respect to
  687. * lowmem, but we won't try to deal with that here.
  688. */
  689. extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  690. extra_pages, max_pages - max_pfn);
  691. i = 0;
  692. addr = xen_e820_table.entries[0].addr;
  693. size = xen_e820_table.entries[0].size;
  694. while (i < xen_e820_table.nr_entries) {
  695. bool discard = false;
  696. chunk_size = size;
  697. type = xen_e820_table.entries[i].type;
  698. if (type == E820_TYPE_RAM) {
  699. if (addr < mem_end) {
  700. chunk_size = min(size, mem_end - addr);
  701. } else if (extra_pages) {
  702. chunk_size = min(size, PFN_PHYS(extra_pages));
  703. pfn_s = PFN_UP(addr);
  704. n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s;
  705. extra_pages -= n_pfns;
  706. xen_add_extra_mem(pfn_s, n_pfns);
  707. xen_max_p2m_pfn = pfn_s + n_pfns;
  708. } else
  709. discard = true;
  710. }
  711. if (!discard)
  712. xen_align_and_add_e820_region(addr, chunk_size, type);
  713. addr += chunk_size;
  714. size -= chunk_size;
  715. if (size == 0) {
  716. i++;
  717. if (i < xen_e820_table.nr_entries) {
  718. addr = xen_e820_table.entries[i].addr;
  719. size = xen_e820_table.entries[i].size;
  720. }
  721. }
  722. }
  723. /*
  724. * Set the rest as identity mapped, in case PCI BARs are
  725. * located here.
  726. */
  727. set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
  728. /*
  729. * In domU, the ISA region is normal, usable memory, but we
  730. * reserve ISA memory anyway because too many things poke
  731. * about in there.
  732. */
  733. e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED);
  734. e820__update_table(e820_table);
  735. /*
  736. * Check whether the kernel itself conflicts with the target E820 map.
  737. * Failing now is better than running into weird problems later due
  738. * to relocating (and even reusing) pages with kernel text or data.
  739. */
  740. if (xen_is_e820_reserved(__pa_symbol(_text),
  741. __pa_symbol(__bss_stop) - __pa_symbol(_text))) {
  742. xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n");
  743. BUG();
  744. }
  745. /*
  746. * Check for a conflict of the hypervisor supplied page tables with
  747. * the target E820 map.
  748. */
  749. xen_pt_check_e820();
  750. xen_reserve_xen_mfnlist();
  751. /* Check for a conflict of the initrd with the target E820 map. */
  752. if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
  753. boot_params.hdr.ramdisk_size)) {
  754. phys_addr_t new_area, start, size;
  755. new_area = xen_find_free_area(boot_params.hdr.ramdisk_size);
  756. if (!new_area) {
  757. xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
  758. BUG();
  759. }
  760. start = boot_params.hdr.ramdisk_image;
  761. size = boot_params.hdr.ramdisk_size;
  762. xen_phys_memcpy(new_area, start, size);
  763. pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
  764. start, start + size, new_area, new_area + size);
  765. memblock_free(start, size);
  766. boot_params.hdr.ramdisk_image = new_area;
  767. boot_params.ext_ramdisk_image = new_area >> 32;
  768. }
  769. /*
  770. * Set identity map on non-RAM pages and prepare remapping the
  771. * underlying RAM.
  772. */
  773. xen_foreach_remap_area(max_pfn, xen_set_identity_and_remap_chunk);
  774. pr_info("Released %ld page(s)\n", xen_released_pages);
  775. return "Xen";
  776. }
  777. /*
  778. * Machine specific memory setup for auto-translated guests.
  779. */
  780. char * __init xen_auto_xlated_memory_setup(void)
  781. {
  782. struct xen_memory_map memmap;
  783. int i;
  784. int rc;
  785. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  786. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  787. rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
  788. if (rc < 0)
  789. panic("No memory map (%d)\n", rc);
  790. xen_e820_table.nr_entries = memmap.nr_entries;
  791. e820__update_table(&xen_e820_table);
  792. for (i = 0; i < xen_e820_table.nr_entries; i++)
  793. e820__range_add(xen_e820_table.entries[i].addr, xen_e820_table.entries[i].size, xen_e820_table.entries[i].type);
  794. /* Remove p2m info, it is not needed. */
  795. xen_start_info->mfn_list = 0;
  796. xen_start_info->first_p2m_pfn = 0;
  797. xen_start_info->nr_p2m_frames = 0;
  798. return "Xen";
  799. }
  800. /*
  801. * Set the bit indicating "nosegneg" library variants should be used.
  802. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  803. * can have un-truncated segments, so wrapping around is allowed.
  804. */
  805. static void __init fiddle_vdso(void)
  806. {
  807. #ifdef CONFIG_X86_32
  808. u32 *mask = vdso_image_32.data +
  809. vdso_image_32.sym_VDSO32_NOTE_MASK;
  810. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  811. #endif
  812. }
  813. static int register_callback(unsigned type, const void *func)
  814. {
  815. struct callback_register callback = {
  816. .type = type,
  817. .address = XEN_CALLBACK(__KERNEL_CS, func),
  818. .flags = CALLBACKF_mask_events,
  819. };
  820. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  821. }
  822. void xen_enable_sysenter(void)
  823. {
  824. int ret;
  825. unsigned sysenter_feature;
  826. #ifdef CONFIG_X86_32
  827. sysenter_feature = X86_FEATURE_SEP;
  828. #else
  829. sysenter_feature = X86_FEATURE_SYSENTER32;
  830. #endif
  831. if (!boot_cpu_has(sysenter_feature))
  832. return;
  833. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  834. if(ret != 0)
  835. setup_clear_cpu_cap(sysenter_feature);
  836. }
  837. void xen_enable_syscall(void)
  838. {
  839. #ifdef CONFIG_X86_64
  840. int ret;
  841. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  842. if (ret != 0) {
  843. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  844. /* Pretty fatal; 64-bit userspace has no other
  845. mechanism for syscalls. */
  846. }
  847. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  848. ret = register_callback(CALLBACKTYPE_syscall32,
  849. xen_syscall32_target);
  850. if (ret != 0)
  851. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  852. }
  853. #endif /* CONFIG_X86_64 */
  854. }
  855. void __init xen_pvmmu_arch_setup(void)
  856. {
  857. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  858. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  859. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  860. VMASST_TYPE_pae_extended_cr3);
  861. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  862. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  863. BUG();
  864. xen_enable_sysenter();
  865. xen_enable_syscall();
  866. }
  867. /* This function is not called for HVM domains */
  868. void __init xen_arch_setup(void)
  869. {
  870. xen_panic_handler_init();
  871. if (!xen_feature(XENFEAT_auto_translated_physmap))
  872. xen_pvmmu_arch_setup();
  873. #ifdef CONFIG_ACPI
  874. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  875. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  876. disable_acpi();
  877. }
  878. #endif
  879. memcpy(boot_command_line, xen_start_info->cmd_line,
  880. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  881. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  882. /* Set up idle, making sure it calls safe_halt() pvop */
  883. disable_cpuidle();
  884. disable_cpufreq();
  885. WARN_ON(xen_set_default_idle());
  886. fiddle_vdso();
  887. #ifdef CONFIG_NUMA
  888. numa_off = 1;
  889. #endif
  890. }