setup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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.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 e820entry xen_e820_map[E820MAX] __initdata;
  38. static u32 xen_e820_map_entries __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 e820entry *entry = xen_e820_map;
  176. unsigned int i;
  177. unsigned long done = 0;
  178. for (i = 0; i < xen_e820_map_entries; i++, entry++) {
  179. unsigned long s_pfn;
  180. unsigned long e_pfn;
  181. if (entry->type != E820_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. BUG_ON(xen_feature(XENFEAT_auto_translated_physmap));
  294. mfn_save = virt_to_mfn(buf);
  295. for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn;
  296. ident_pfn_iter < ident_end_pfn;
  297. ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) {
  298. chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE;
  299. /* Map first pfn to xen_remap_buf */
  300. mfn = pfn_to_mfn(ident_pfn_iter);
  301. set_pte_mfn(buf, mfn, PAGE_KERNEL);
  302. /* Save mapping information in page */
  303. xen_remap_buf.next_area_mfn = xen_remap_mfn;
  304. xen_remap_buf.target_pfn = remap_pfn_iter;
  305. xen_remap_buf.size = chunk;
  306. for (i = 0; i < chunk; i++)
  307. xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i);
  308. /* Put remap buf into list. */
  309. xen_remap_mfn = mfn;
  310. /* Set identity map */
  311. set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk);
  312. left -= chunk;
  313. }
  314. /* Restore old xen_remap_buf mapping */
  315. set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
  316. }
  317. /*
  318. * This function takes a contiguous pfn range that needs to be identity mapped
  319. * and:
  320. *
  321. * 1) Finds a new range of pfns to use to remap based on E820 and remap_pfn.
  322. * 2) Calls the do_ function to actually do the mapping/remapping work.
  323. *
  324. * The goal is to not allocate additional memory but to remap the existing
  325. * pages. In the case of an error the underlying memory is simply released back
  326. * to Xen and not remapped.
  327. */
  328. static unsigned long __init xen_set_identity_and_remap_chunk(
  329. unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
  330. unsigned long remap_pfn)
  331. {
  332. unsigned long pfn;
  333. unsigned long i = 0;
  334. unsigned long n = end_pfn - start_pfn;
  335. if (remap_pfn == 0)
  336. remap_pfn = nr_pages;
  337. while (i < n) {
  338. unsigned long cur_pfn = start_pfn + i;
  339. unsigned long left = n - i;
  340. unsigned long size = left;
  341. unsigned long remap_range_size;
  342. /* Do not remap pages beyond the current allocation */
  343. if (cur_pfn >= nr_pages) {
  344. /* Identity map remaining pages */
  345. set_phys_range_identity(cur_pfn, cur_pfn + size);
  346. break;
  347. }
  348. if (cur_pfn + size > nr_pages)
  349. size = nr_pages - cur_pfn;
  350. remap_range_size = xen_find_pfn_range(&remap_pfn);
  351. if (!remap_range_size) {
  352. pr_warning("Unable to find available pfn range, not remapping identity pages\n");
  353. xen_set_identity_and_release_chunk(cur_pfn,
  354. cur_pfn + left, nr_pages);
  355. break;
  356. }
  357. /* Adjust size to fit in current e820 RAM region */
  358. if (size > remap_range_size)
  359. size = remap_range_size;
  360. xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn);
  361. /* Update variables to reflect new mappings. */
  362. i += size;
  363. remap_pfn += size;
  364. }
  365. /*
  366. * If the PFNs are currently mapped, the VA mapping also needs
  367. * to be updated to be 1:1.
  368. */
  369. for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
  370. (void)HYPERVISOR_update_va_mapping(
  371. (unsigned long)__va(pfn << PAGE_SHIFT),
  372. mfn_pte(pfn, PAGE_KERNEL_IO), 0);
  373. return remap_pfn;
  374. }
  375. static unsigned long __init xen_count_remap_pages(
  376. unsigned long start_pfn, unsigned long end_pfn, unsigned long nr_pages,
  377. unsigned long remap_pages)
  378. {
  379. if (start_pfn >= nr_pages)
  380. return remap_pages;
  381. return remap_pages + min(end_pfn, nr_pages) - start_pfn;
  382. }
  383. static unsigned long __init xen_foreach_remap_area(unsigned long nr_pages,
  384. unsigned long (*func)(unsigned long start_pfn, unsigned long end_pfn,
  385. unsigned long nr_pages, unsigned long last_val))
  386. {
  387. phys_addr_t start = 0;
  388. unsigned long ret_val = 0;
  389. const struct e820entry *entry = xen_e820_map;
  390. int i;
  391. /*
  392. * Combine non-RAM regions and gaps until a RAM region (or the
  393. * end of the map) is reached, then call the provided function
  394. * to perform its duty on the non-RAM region.
  395. *
  396. * The combined non-RAM regions are rounded to a whole number
  397. * of pages so any partial pages are accessible via the 1:1
  398. * mapping. This is needed for some BIOSes that put (for
  399. * example) the DMI tables in a reserved region that begins on
  400. * a non-page boundary.
  401. */
  402. for (i = 0; i < xen_e820_map_entries; i++, entry++) {
  403. phys_addr_t end = entry->addr + entry->size;
  404. if (entry->type == E820_RAM || i == xen_e820_map_entries - 1) {
  405. unsigned long start_pfn = PFN_DOWN(start);
  406. unsigned long end_pfn = PFN_UP(end);
  407. if (entry->type == E820_RAM)
  408. end_pfn = PFN_UP(entry->addr);
  409. if (start_pfn < end_pfn)
  410. ret_val = func(start_pfn, end_pfn, nr_pages,
  411. ret_val);
  412. start = end;
  413. }
  414. }
  415. return ret_val;
  416. }
  417. /*
  418. * Remap the memory prepared in xen_do_set_identity_and_remap_chunk().
  419. * The remap information (which mfn remap to which pfn) is contained in the
  420. * to be remapped memory itself in a linked list anchored at xen_remap_mfn.
  421. * This scheme allows to remap the different chunks in arbitrary order while
  422. * the resulting mapping will be independant from the order.
  423. */
  424. void __init xen_remap_memory(void)
  425. {
  426. unsigned long buf = (unsigned long)&xen_remap_buf;
  427. unsigned long mfn_save, mfn, pfn;
  428. unsigned long remapped = 0;
  429. unsigned int i;
  430. unsigned long pfn_s = ~0UL;
  431. unsigned long len = 0;
  432. mfn_save = virt_to_mfn(buf);
  433. while (xen_remap_mfn != INVALID_P2M_ENTRY) {
  434. /* Map the remap information */
  435. set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
  436. BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
  437. pfn = xen_remap_buf.target_pfn;
  438. for (i = 0; i < xen_remap_buf.size; i++) {
  439. mfn = xen_remap_buf.mfns[i];
  440. xen_update_mem_tables(pfn, mfn);
  441. remapped++;
  442. pfn++;
  443. }
  444. if (pfn_s == ~0UL || pfn == pfn_s) {
  445. pfn_s = xen_remap_buf.target_pfn;
  446. len += xen_remap_buf.size;
  447. } else if (pfn_s + len == xen_remap_buf.target_pfn) {
  448. len += xen_remap_buf.size;
  449. } else {
  450. xen_del_extra_mem(pfn_s, len);
  451. pfn_s = xen_remap_buf.target_pfn;
  452. len = xen_remap_buf.size;
  453. }
  454. mfn = xen_remap_mfn;
  455. xen_remap_mfn = xen_remap_buf.next_area_mfn;
  456. }
  457. if (pfn_s != ~0UL && len)
  458. xen_del_extra_mem(pfn_s, len);
  459. set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
  460. pr_info("Remapped %ld page(s)\n", remapped);
  461. }
  462. static unsigned long __init xen_get_pages_limit(void)
  463. {
  464. unsigned long limit;
  465. #ifdef CONFIG_X86_32
  466. limit = GB(64) / PAGE_SIZE;
  467. #else
  468. limit = MAXMEM / PAGE_SIZE;
  469. if (!xen_initial_domain() && xen_512gb_limit)
  470. limit = GB(512) / PAGE_SIZE;
  471. #endif
  472. return limit;
  473. }
  474. static unsigned long __init xen_get_max_pages(void)
  475. {
  476. unsigned long max_pages, limit;
  477. domid_t domid = DOMID_SELF;
  478. long ret;
  479. limit = xen_get_pages_limit();
  480. max_pages = limit;
  481. /*
  482. * For the initial domain we use the maximum reservation as
  483. * the maximum page.
  484. *
  485. * For guest domains the current maximum reservation reflects
  486. * the current maximum rather than the static maximum. In this
  487. * case the e820 map provided to us will cover the static
  488. * maximum region.
  489. */
  490. if (xen_initial_domain()) {
  491. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  492. if (ret > 0)
  493. max_pages = ret;
  494. }
  495. return min(max_pages, limit);
  496. }
  497. static void __init xen_align_and_add_e820_region(phys_addr_t start,
  498. phys_addr_t size, int type)
  499. {
  500. phys_addr_t end = start + size;
  501. /* Align RAM regions to page boundaries. */
  502. if (type == E820_RAM) {
  503. start = PAGE_ALIGN(start);
  504. end &= ~((phys_addr_t)PAGE_SIZE - 1);
  505. }
  506. e820_add_region(start, end - start, type);
  507. }
  508. static void __init xen_ignore_unusable(void)
  509. {
  510. struct e820entry *entry = xen_e820_map;
  511. unsigned int i;
  512. for (i = 0; i < xen_e820_map_entries; i++, entry++) {
  513. if (entry->type == E820_UNUSABLE)
  514. entry->type = E820_RAM;
  515. }
  516. }
  517. bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
  518. {
  519. struct e820entry *entry;
  520. unsigned mapcnt;
  521. phys_addr_t end;
  522. if (!size)
  523. return false;
  524. end = start + size;
  525. entry = xen_e820_map;
  526. for (mapcnt = 0; mapcnt < xen_e820_map_entries; mapcnt++) {
  527. if (entry->type == E820_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 e820entry *entry = xen_e820_map;
  547. for (mapcnt = 0; mapcnt < xen_e820_map_entries; mapcnt++, entry++) {
  548. if (entry->type != E820_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. #ifdef CONFIG_X86_32
  611. /*
  612. * Relocating the p2m on 32 bit system to an arbitrary virtual address
  613. * is not supported, so just give up.
  614. */
  615. xen_raw_console_write("Xen hypervisor allocated p2m list conflicts with E820 map\n");
  616. BUG();
  617. #else
  618. xen_relocate_p2m();
  619. memblock_free(start, size);
  620. #endif
  621. }
  622. /**
  623. * machine_specific_memory_setup - Hook for machine specific memory setup.
  624. **/
  625. char * __init xen_memory_setup(void)
  626. {
  627. unsigned long max_pfn, pfn_s, n_pfns;
  628. phys_addr_t mem_end, addr, size, chunk_size;
  629. u32 type;
  630. int rc;
  631. struct xen_memory_map memmap;
  632. unsigned long max_pages;
  633. unsigned long extra_pages = 0;
  634. int i;
  635. int op;
  636. xen_parse_512gb();
  637. max_pfn = xen_get_pages_limit();
  638. max_pfn = min(max_pfn, xen_start_info->nr_pages);
  639. mem_end = PFN_PHYS(max_pfn);
  640. memmap.nr_entries = E820MAX;
  641. set_xen_guest_handle(memmap.buffer, xen_e820_map);
  642. op = xen_initial_domain() ?
  643. XENMEM_machine_memory_map :
  644. XENMEM_memory_map;
  645. rc = HYPERVISOR_memory_op(op, &memmap);
  646. if (rc == -ENOSYS) {
  647. BUG_ON(xen_initial_domain());
  648. memmap.nr_entries = 1;
  649. xen_e820_map[0].addr = 0ULL;
  650. xen_e820_map[0].size = mem_end;
  651. /* 8MB slack (to balance backend allocations). */
  652. xen_e820_map[0].size += 8ULL << 20;
  653. xen_e820_map[0].type = E820_RAM;
  654. rc = 0;
  655. }
  656. BUG_ON(rc);
  657. BUG_ON(memmap.nr_entries == 0);
  658. xen_e820_map_entries = memmap.nr_entries;
  659. /*
  660. * Xen won't allow a 1:1 mapping to be created to UNUSABLE
  661. * regions, so if we're using the machine memory map leave the
  662. * region as RAM as it is in the pseudo-physical map.
  663. *
  664. * UNUSABLE regions in domUs are not handled and will need
  665. * a patch in the future.
  666. */
  667. if (xen_initial_domain())
  668. xen_ignore_unusable();
  669. /* Make sure the Xen-supplied memory map is well-ordered. */
  670. sanitize_e820_map(xen_e820_map, ARRAY_SIZE(xen_e820_map),
  671. &xen_e820_map_entries);
  672. max_pages = xen_get_max_pages();
  673. /* How many extra pages do we need due to remapping? */
  674. max_pages += xen_foreach_remap_area(max_pfn, xen_count_remap_pages);
  675. if (max_pages > max_pfn)
  676. extra_pages += max_pages - max_pfn;
  677. /*
  678. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  679. * factor the base size. On non-highmem systems, the base
  680. * size is the full initial memory allocation; on highmem it
  681. * is limited to the max size of lowmem, so that it doesn't
  682. * get completely filled.
  683. *
  684. * Make sure we have no memory above max_pages, as this area
  685. * isn't handled by the p2m management.
  686. *
  687. * In principle there could be a problem in lowmem systems if
  688. * the initial memory is also very large with respect to
  689. * lowmem, but we won't try to deal with that here.
  690. */
  691. extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  692. extra_pages, max_pages - max_pfn);
  693. i = 0;
  694. addr = xen_e820_map[0].addr;
  695. size = xen_e820_map[0].size;
  696. while (i < xen_e820_map_entries) {
  697. bool discard = false;
  698. chunk_size = size;
  699. type = xen_e820_map[i].type;
  700. if (type == E820_RAM) {
  701. if (addr < mem_end) {
  702. chunk_size = min(size, mem_end - addr);
  703. } else if (extra_pages) {
  704. chunk_size = min(size, PFN_PHYS(extra_pages));
  705. pfn_s = PFN_UP(addr);
  706. n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s;
  707. extra_pages -= n_pfns;
  708. xen_add_extra_mem(pfn_s, n_pfns);
  709. xen_max_p2m_pfn = pfn_s + n_pfns;
  710. } else
  711. discard = true;
  712. }
  713. if (!discard)
  714. xen_align_and_add_e820_region(addr, chunk_size, type);
  715. addr += chunk_size;
  716. size -= chunk_size;
  717. if (size == 0) {
  718. i++;
  719. if (i < xen_e820_map_entries) {
  720. addr = xen_e820_map[i].addr;
  721. size = xen_e820_map[i].size;
  722. }
  723. }
  724. }
  725. /*
  726. * Set the rest as identity mapped, in case PCI BARs are
  727. * located here.
  728. */
  729. set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
  730. /*
  731. * In domU, the ISA region is normal, usable memory, but we
  732. * reserve ISA memory anyway because too many things poke
  733. * about in there.
  734. */
  735. e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
  736. E820_RESERVED);
  737. sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
  738. /*
  739. * Check whether the kernel itself conflicts with the target E820 map.
  740. * Failing now is better than running into weird problems later due
  741. * to relocating (and even reusing) pages with kernel text or data.
  742. */
  743. if (xen_is_e820_reserved(__pa_symbol(_text),
  744. __pa_symbol(__bss_stop) - __pa_symbol(_text))) {
  745. xen_raw_console_write("Xen hypervisor allocated kernel memory conflicts with E820 map\n");
  746. BUG();
  747. }
  748. /*
  749. * Check for a conflict of the hypervisor supplied page tables with
  750. * the target E820 map.
  751. */
  752. xen_pt_check_e820();
  753. xen_reserve_xen_mfnlist();
  754. /* Check for a conflict of the initrd with the target E820 map. */
  755. if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
  756. boot_params.hdr.ramdisk_size)) {
  757. phys_addr_t new_area, start, size;
  758. new_area = xen_find_free_area(boot_params.hdr.ramdisk_size);
  759. if (!new_area) {
  760. xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
  761. BUG();
  762. }
  763. start = boot_params.hdr.ramdisk_image;
  764. size = boot_params.hdr.ramdisk_size;
  765. xen_phys_memcpy(new_area, start, size);
  766. pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
  767. start, start + size, new_area, new_area + size);
  768. memblock_free(start, size);
  769. boot_params.hdr.ramdisk_image = new_area;
  770. boot_params.ext_ramdisk_image = new_area >> 32;
  771. }
  772. /*
  773. * Set identity map on non-RAM pages and prepare remapping the
  774. * underlying RAM.
  775. */
  776. xen_foreach_remap_area(max_pfn, xen_set_identity_and_remap_chunk);
  777. pr_info("Released %ld page(s)\n", xen_released_pages);
  778. return "Xen";
  779. }
  780. /*
  781. * Machine specific memory setup for auto-translated guests.
  782. */
  783. char * __init xen_auto_xlated_memory_setup(void)
  784. {
  785. struct xen_memory_map memmap;
  786. int i;
  787. int rc;
  788. memmap.nr_entries = E820MAX;
  789. set_xen_guest_handle(memmap.buffer, xen_e820_map);
  790. rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
  791. if (rc < 0)
  792. panic("No memory map (%d)\n", rc);
  793. xen_e820_map_entries = memmap.nr_entries;
  794. sanitize_e820_map(xen_e820_map, ARRAY_SIZE(xen_e820_map),
  795. &xen_e820_map_entries);
  796. for (i = 0; i < xen_e820_map_entries; i++)
  797. e820_add_region(xen_e820_map[i].addr, xen_e820_map[i].size,
  798. xen_e820_map[i].type);
  799. /* Remove p2m info, it is not needed. */
  800. xen_start_info->mfn_list = 0;
  801. xen_start_info->first_p2m_pfn = 0;
  802. xen_start_info->nr_p2m_frames = 0;
  803. return "Xen";
  804. }
  805. /*
  806. * Set the bit indicating "nosegneg" library variants should be used.
  807. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  808. * can have un-truncated segments, so wrapping around is allowed.
  809. */
  810. static void __init fiddle_vdso(void)
  811. {
  812. #ifdef CONFIG_X86_32
  813. u32 *mask = vdso_image_32.data +
  814. vdso_image_32.sym_VDSO32_NOTE_MASK;
  815. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  816. #endif
  817. }
  818. static int register_callback(unsigned type, const void *func)
  819. {
  820. struct callback_register callback = {
  821. .type = type,
  822. .address = XEN_CALLBACK(__KERNEL_CS, func),
  823. .flags = CALLBACKF_mask_events,
  824. };
  825. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  826. }
  827. void xen_enable_sysenter(void)
  828. {
  829. int ret;
  830. unsigned sysenter_feature;
  831. #ifdef CONFIG_X86_32
  832. sysenter_feature = X86_FEATURE_SEP;
  833. #else
  834. sysenter_feature = X86_FEATURE_SYSENTER32;
  835. #endif
  836. if (!boot_cpu_has(sysenter_feature))
  837. return;
  838. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  839. if(ret != 0)
  840. setup_clear_cpu_cap(sysenter_feature);
  841. }
  842. void xen_enable_syscall(void)
  843. {
  844. #ifdef CONFIG_X86_64
  845. int ret;
  846. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  847. if (ret != 0) {
  848. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  849. /* Pretty fatal; 64-bit userspace has no other
  850. mechanism for syscalls. */
  851. }
  852. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  853. ret = register_callback(CALLBACKTYPE_syscall32,
  854. xen_syscall32_target);
  855. if (ret != 0)
  856. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  857. }
  858. #endif /* CONFIG_X86_64 */
  859. }
  860. void __init xen_pvmmu_arch_setup(void)
  861. {
  862. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  863. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  864. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  865. VMASST_TYPE_pae_extended_cr3);
  866. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  867. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  868. BUG();
  869. xen_enable_sysenter();
  870. xen_enable_syscall();
  871. }
  872. /* This function is not called for HVM domains */
  873. void __init xen_arch_setup(void)
  874. {
  875. xen_panic_handler_init();
  876. if (!xen_feature(XENFEAT_auto_translated_physmap))
  877. xen_pvmmu_arch_setup();
  878. #ifdef CONFIG_ACPI
  879. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  880. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  881. disable_acpi();
  882. }
  883. #endif
  884. memcpy(boot_command_line, xen_start_info->cmd_line,
  885. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  886. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  887. /* Set up idle, making sure it calls safe_halt() pvop */
  888. disable_cpuidle();
  889. disable_cpufreq();
  890. WARN_ON(xen_set_default_idle());
  891. fiddle_vdso();
  892. #ifdef CONFIG_NUMA
  893. numa_off = 1;
  894. #endif
  895. }