setup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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, 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. * Machine specific memory setup for auto-translated guests.
  777. */
  778. char * __init xen_auto_xlated_memory_setup(void)
  779. {
  780. struct xen_memory_map memmap;
  781. int i;
  782. int rc;
  783. memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
  784. set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
  785. rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
  786. if (rc < 0)
  787. panic("No memory map (%d)\n", rc);
  788. xen_e820_table.nr_entries = memmap.nr_entries;
  789. e820__update_table(&xen_e820_table);
  790. for (i = 0; i < xen_e820_table.nr_entries; i++)
  791. e820__range_add(xen_e820_table.entries[i].addr, xen_e820_table.entries[i].size, xen_e820_table.entries[i].type);
  792. /* Remove p2m info, it is not needed. */
  793. xen_start_info->mfn_list = 0;
  794. xen_start_info->first_p2m_pfn = 0;
  795. xen_start_info->nr_p2m_frames = 0;
  796. return "Xen";
  797. }
  798. /*
  799. * Set the bit indicating "nosegneg" library variants should be used.
  800. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  801. * can have un-truncated segments, so wrapping around is allowed.
  802. */
  803. static void __init fiddle_vdso(void)
  804. {
  805. #ifdef CONFIG_X86_32
  806. u32 *mask = vdso_image_32.data +
  807. vdso_image_32.sym_VDSO32_NOTE_MASK;
  808. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  809. #endif
  810. }
  811. static int register_callback(unsigned type, const void *func)
  812. {
  813. struct callback_register callback = {
  814. .type = type,
  815. .address = XEN_CALLBACK(__KERNEL_CS, func),
  816. .flags = CALLBACKF_mask_events,
  817. };
  818. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  819. }
  820. void xen_enable_sysenter(void)
  821. {
  822. int ret;
  823. unsigned sysenter_feature;
  824. #ifdef CONFIG_X86_32
  825. sysenter_feature = X86_FEATURE_SEP;
  826. #else
  827. sysenter_feature = X86_FEATURE_SYSENTER32;
  828. #endif
  829. if (!boot_cpu_has(sysenter_feature))
  830. return;
  831. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  832. if(ret != 0)
  833. setup_clear_cpu_cap(sysenter_feature);
  834. }
  835. void xen_enable_syscall(void)
  836. {
  837. #ifdef CONFIG_X86_64
  838. int ret;
  839. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  840. if (ret != 0) {
  841. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  842. /* Pretty fatal; 64-bit userspace has no other
  843. mechanism for syscalls. */
  844. }
  845. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  846. ret = register_callback(CALLBACKTYPE_syscall32,
  847. xen_syscall32_target);
  848. if (ret != 0)
  849. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  850. }
  851. #endif /* CONFIG_X86_64 */
  852. }
  853. void __init xen_pvmmu_arch_setup(void)
  854. {
  855. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  856. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  857. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  858. VMASST_TYPE_pae_extended_cr3);
  859. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  860. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  861. BUG();
  862. xen_enable_sysenter();
  863. xen_enable_syscall();
  864. }
  865. /* This function is not called for HVM domains */
  866. void __init xen_arch_setup(void)
  867. {
  868. xen_panic_handler_init();
  869. if (!xen_feature(XENFEAT_auto_translated_physmap))
  870. xen_pvmmu_arch_setup();
  871. #ifdef CONFIG_ACPI
  872. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  873. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  874. disable_acpi();
  875. }
  876. #endif
  877. memcpy(boot_command_line, xen_start_info->cmd_line,
  878. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  879. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  880. /* Set up idle, making sure it calls safe_halt() pvop */
  881. disable_cpuidle();
  882. disable_cpufreq();
  883. WARN_ON(xen_set_default_idle());
  884. fiddle_vdso();
  885. #ifdef CONFIG_NUMA
  886. numa_off = 1;
  887. #endif
  888. }