setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Machine specific setup for xen
  3. *
  4. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  5. */
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/pm.h>
  10. #include <linux/memblock.h>
  11. #include <asm/elf.h>
  12. #include <asm/vdso.h>
  13. #include <asm/e820.h>
  14. #include <asm/setup.h>
  15. #include <asm/acpi.h>
  16. #include <asm/xen/hypervisor.h>
  17. #include <asm/xen/hypercall.h>
  18. #include <xen/xen.h>
  19. #include <xen/page.h>
  20. #include <xen/interface/callback.h>
  21. #include <xen/interface/memory.h>
  22. #include <xen/interface/physdev.h>
  23. #include <xen/features.h>
  24. #include "xen-ops.h"
  25. #include "vdso.h"
  26. /* These are code, but not functions. Defined in entry.S */
  27. extern const char xen_hypervisor_callback[];
  28. extern const char xen_failsafe_callback[];
  29. extern void xen_sysenter_target(void);
  30. extern void xen_syscall_target(void);
  31. extern void xen_syscall32_target(void);
  32. /* Amount of extra memory space we add to the e820 ranges */
  33. phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
  34. /*
  35. * The maximum amount of extra memory compared to the base size. The
  36. * main scaling factor is the size of struct page. At extreme ratios
  37. * of base:extra, all the base memory can be filled with page
  38. * structures for the extra memory, leaving no space for anything
  39. * else.
  40. *
  41. * 10x seems like a reasonable balance between scaling flexibility and
  42. * leaving a practically usable system.
  43. */
  44. #define EXTRA_MEM_RATIO (10)
  45. static void __init xen_add_extra_mem(unsigned long pages)
  46. {
  47. unsigned long pfn;
  48. u64 size = (u64)pages * PAGE_SIZE;
  49. u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
  50. if (!pages)
  51. return;
  52. e820_add_region(extra_start, size, E820_RAM);
  53. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  54. memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA");
  55. xen_extra_mem_size += size;
  56. xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
  57. for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++)
  58. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  59. }
  60. static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
  61. phys_addr_t end_addr)
  62. {
  63. struct xen_memory_reservation reservation = {
  64. .address_bits = 0,
  65. .extent_order = 0,
  66. .domid = DOMID_SELF
  67. };
  68. unsigned long start, end;
  69. unsigned long len = 0;
  70. unsigned long pfn;
  71. int ret;
  72. start = PFN_UP(start_addr);
  73. end = PFN_DOWN(end_addr);
  74. if (end <= start)
  75. return 0;
  76. printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
  77. start, end);
  78. for(pfn = start; pfn < end; pfn++) {
  79. unsigned long mfn = pfn_to_mfn(pfn);
  80. /* Make sure pfn exists to start with */
  81. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  82. continue;
  83. set_xen_guest_handle(reservation.extent_start, &mfn);
  84. reservation.nr_extents = 1;
  85. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  86. &reservation);
  87. WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
  88. start, end, ret);
  89. if (ret == 1) {
  90. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  91. len++;
  92. }
  93. }
  94. printk(KERN_CONT "%ld pages freed\n", len);
  95. return len;
  96. }
  97. static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
  98. const struct e820map *e820)
  99. {
  100. phys_addr_t max_addr = PFN_PHYS(max_pfn);
  101. phys_addr_t last_end = ISA_END_ADDRESS;
  102. unsigned long released = 0;
  103. int i;
  104. /* Free any unused memory above the low 1Mbyte. */
  105. for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
  106. phys_addr_t end = e820->map[i].addr;
  107. end = min(max_addr, end);
  108. if (last_end < end)
  109. released += xen_release_chunk(last_end, end);
  110. last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
  111. }
  112. if (last_end < max_addr)
  113. released += xen_release_chunk(last_end, max_addr);
  114. printk(KERN_INFO "released %ld pages of unused memory\n", released);
  115. return released;
  116. }
  117. static unsigned long __init xen_set_identity(const struct e820entry *list,
  118. ssize_t map_size)
  119. {
  120. phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS;
  121. phys_addr_t start_pci = last;
  122. const struct e820entry *entry;
  123. unsigned long identity = 0;
  124. int i;
  125. for (i = 0, entry = list; i < map_size; i++, entry++) {
  126. phys_addr_t start = entry->addr;
  127. phys_addr_t end = start + entry->size;
  128. if (start < last)
  129. start = last;
  130. if (end <= start)
  131. continue;
  132. /* Skip over the 1MB region. */
  133. if (last > end)
  134. continue;
  135. if ((entry->type == E820_RAM) || (entry->type == E820_UNUSABLE)) {
  136. if (start > start_pci)
  137. identity += set_phys_range_identity(
  138. PFN_UP(start_pci), PFN_DOWN(start));
  139. /* Without saving 'last' we would gooble RAM too
  140. * at the end of the loop. */
  141. last = end;
  142. start_pci = end;
  143. continue;
  144. }
  145. start_pci = min(start, start_pci);
  146. last = end;
  147. }
  148. if (last > start_pci)
  149. identity += set_phys_range_identity(
  150. PFN_UP(start_pci), PFN_DOWN(last));
  151. return identity;
  152. }
  153. static unsigned long __init xen_get_max_pages(void)
  154. {
  155. unsigned long max_pages = MAX_DOMAIN_PAGES;
  156. domid_t domid = DOMID_SELF;
  157. int ret;
  158. /*
  159. * For the initial domain we use the maximum reservation as
  160. * the maximum page.
  161. *
  162. * For guest domains the current maximum reservation reflects
  163. * the current maximum rather than the static maximum. In this
  164. * case the e820 map provided to us will cover the static
  165. * maximum region.
  166. */
  167. if (xen_initial_domain()) {
  168. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  169. if (ret > 0)
  170. max_pages = ret;
  171. }
  172. return min(max_pages, MAX_DOMAIN_PAGES);
  173. }
  174. /**
  175. * machine_specific_memory_setup - Hook for machine specific memory setup.
  176. **/
  177. char * __init xen_memory_setup(void)
  178. {
  179. static struct e820entry map[E820MAX] __initdata;
  180. static struct e820entry map_raw[E820MAX] __initdata;
  181. unsigned long max_pfn = xen_start_info->nr_pages;
  182. unsigned long long mem_end;
  183. int rc;
  184. struct xen_memory_map memmap;
  185. unsigned long extra_pages = 0;
  186. unsigned long extra_limit;
  187. unsigned long identity_pages = 0;
  188. int i;
  189. int op;
  190. max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
  191. mem_end = PFN_PHYS(max_pfn);
  192. memmap.nr_entries = E820MAX;
  193. set_xen_guest_handle(memmap.buffer, map);
  194. op = xen_initial_domain() ?
  195. XENMEM_machine_memory_map :
  196. XENMEM_memory_map;
  197. rc = HYPERVISOR_memory_op(op, &memmap);
  198. if (rc == -ENOSYS) {
  199. BUG_ON(xen_initial_domain());
  200. memmap.nr_entries = 1;
  201. map[0].addr = 0ULL;
  202. map[0].size = mem_end;
  203. /* 8MB slack (to balance backend allocations). */
  204. map[0].size += 8ULL << 20;
  205. map[0].type = E820_RAM;
  206. rc = 0;
  207. }
  208. BUG_ON(rc);
  209. memcpy(map_raw, map, sizeof(map));
  210. e820.nr_map = 0;
  211. xen_extra_mem_start = mem_end;
  212. for (i = 0; i < memmap.nr_entries; i++) {
  213. unsigned long long end;
  214. /* Guard against non-page aligned E820 entries. */
  215. if (map[i].type == E820_RAM)
  216. map[i].size -= (map[i].size + map[i].addr) % PAGE_SIZE;
  217. end = map[i].addr + map[i].size;
  218. if (map[i].type == E820_RAM && end > mem_end) {
  219. /* RAM off the end - may be partially included */
  220. u64 delta = min(map[i].size, end - mem_end);
  221. map[i].size -= delta;
  222. end -= delta;
  223. extra_pages += PFN_DOWN(delta);
  224. /*
  225. * Set RAM below 4GB that is not for us to be unusable.
  226. * This prevents "System RAM" address space from being
  227. * used as potential resource for I/O address (happens
  228. * when 'allocate_resource' is called).
  229. */
  230. if (delta &&
  231. (xen_initial_domain() && end < 0x100000000ULL))
  232. e820_add_region(end, delta, E820_UNUSABLE);
  233. }
  234. if (map[i].size > 0 && end > xen_extra_mem_start)
  235. xen_extra_mem_start = end;
  236. /* Add region if any remains */
  237. if (map[i].size > 0)
  238. e820_add_region(map[i].addr, map[i].size, map[i].type);
  239. }
  240. /* Align the balloon area so that max_low_pfn does not get set
  241. * to be at the _end_ of the PCI gap at the far end (fee01000).
  242. * Note that xen_extra_mem_start gets set in the loop above to be
  243. * past the last E820 region. */
  244. if (xen_initial_domain() && (xen_extra_mem_start < (1ULL<<32)))
  245. xen_extra_mem_start = (1ULL<<32);
  246. /*
  247. * In domU, the ISA region is normal, usable memory, but we
  248. * reserve ISA memory anyway because too many things poke
  249. * about in there.
  250. *
  251. * In Dom0, the host E820 information can leave gaps in the
  252. * ISA range, which would cause us to release those pages. To
  253. * avoid this, we unconditionally reserve them here.
  254. */
  255. e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
  256. E820_RESERVED);
  257. /*
  258. * Reserve Xen bits:
  259. * - mfn_list
  260. * - xen_start_info
  261. * See comment above "struct start_info" in <xen/interface/xen.h>
  262. */
  263. memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
  264. __pa(xen_start_info->pt_base),
  265. "XEN START INFO");
  266. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  267. extra_limit = xen_get_max_pages();
  268. if (max_pfn + extra_pages > extra_limit) {
  269. if (extra_limit > max_pfn)
  270. extra_pages = extra_limit - max_pfn;
  271. else
  272. extra_pages = 0;
  273. }
  274. extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
  275. /*
  276. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  277. * factor the base size. On non-highmem systems, the base
  278. * size is the full initial memory allocation; on highmem it
  279. * is limited to the max size of lowmem, so that it doesn't
  280. * get completely filled.
  281. *
  282. * In principle there could be a problem in lowmem systems if
  283. * the initial memory is also very large with respect to
  284. * lowmem, but we won't try to deal with that here.
  285. */
  286. extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  287. max_pfn + extra_pages);
  288. if (extra_limit >= max_pfn)
  289. extra_pages = extra_limit - max_pfn;
  290. else
  291. extra_pages = 0;
  292. xen_add_extra_mem(extra_pages);
  293. /*
  294. * Set P2M for all non-RAM pages and E820 gaps to be identity
  295. * type PFNs. We supply it with the non-sanitized version
  296. * of the E820.
  297. */
  298. identity_pages = xen_set_identity(map_raw, memmap.nr_entries);
  299. printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages);
  300. return "Xen";
  301. }
  302. /*
  303. * Set the bit indicating "nosegneg" library variants should be used.
  304. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  305. * can have un-truncated segments, so wrapping around is allowed.
  306. */
  307. static void __init fiddle_vdso(void)
  308. {
  309. #ifdef CONFIG_X86_32
  310. u32 *mask;
  311. mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
  312. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  313. mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
  314. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  315. #endif
  316. }
  317. static int __cpuinit register_callback(unsigned type, const void *func)
  318. {
  319. struct callback_register callback = {
  320. .type = type,
  321. .address = XEN_CALLBACK(__KERNEL_CS, func),
  322. .flags = CALLBACKF_mask_events,
  323. };
  324. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  325. }
  326. void __cpuinit xen_enable_sysenter(void)
  327. {
  328. int ret;
  329. unsigned sysenter_feature;
  330. #ifdef CONFIG_X86_32
  331. sysenter_feature = X86_FEATURE_SEP;
  332. #else
  333. sysenter_feature = X86_FEATURE_SYSENTER32;
  334. #endif
  335. if (!boot_cpu_has(sysenter_feature))
  336. return;
  337. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  338. if(ret != 0)
  339. setup_clear_cpu_cap(sysenter_feature);
  340. }
  341. void __cpuinit xen_enable_syscall(void)
  342. {
  343. #ifdef CONFIG_X86_64
  344. int ret;
  345. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  346. if (ret != 0) {
  347. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  348. /* Pretty fatal; 64-bit userspace has no other
  349. mechanism for syscalls. */
  350. }
  351. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  352. ret = register_callback(CALLBACKTYPE_syscall32,
  353. xen_syscall32_target);
  354. if (ret != 0)
  355. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  356. }
  357. #endif /* CONFIG_X86_64 */
  358. }
  359. void __init xen_arch_setup(void)
  360. {
  361. xen_panic_handler_init();
  362. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  363. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  364. if (!xen_feature(XENFEAT_auto_translated_physmap))
  365. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  366. VMASST_TYPE_pae_extended_cr3);
  367. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  368. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  369. BUG();
  370. xen_enable_sysenter();
  371. xen_enable_syscall();
  372. #ifdef CONFIG_ACPI
  373. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  374. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  375. disable_acpi();
  376. }
  377. #endif
  378. memcpy(boot_command_line, xen_start_info->cmd_line,
  379. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  380. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  381. /* Set up idle, making sure it calls safe_halt() pvop */
  382. #ifdef CONFIG_X86_32
  383. boot_cpu_data.hlt_works_ok = 1;
  384. #endif
  385. pm_idle = default_idle;
  386. boot_option_idle_override = IDLE_HALT;
  387. fiddle_vdso();
  388. }