setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <linux/cpuidle.h>
  12. #include <asm/elf.h>
  13. #include <asm/vdso.h>
  14. #include <asm/e820.h>
  15. #include <asm/setup.h>
  16. #include <asm/acpi.h>
  17. #include <asm/xen/hypervisor.h>
  18. #include <asm/xen/hypercall.h>
  19. #include <xen/xen.h>
  20. #include <xen/page.h>
  21. #include <xen/interface/callback.h>
  22. #include <xen/interface/memory.h>
  23. #include <xen/interface/physdev.h>
  24. #include <xen/features.h>
  25. #include "xen-ops.h"
  26. #include "vdso.h"
  27. /* These are code, but not functions. Defined in entry.S */
  28. extern const char xen_hypervisor_callback[];
  29. extern const char xen_failsafe_callback[];
  30. extern void xen_sysenter_target(void);
  31. extern void xen_syscall_target(void);
  32. extern void xen_syscall32_target(void);
  33. /* Amount of extra memory space we add to the e820 ranges */
  34. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  35. /* Number of pages released from the initial allocation. */
  36. unsigned long xen_released_pages;
  37. /*
  38. * The maximum amount of extra memory compared to the base size. The
  39. * main scaling factor is the size of struct page. At extreme ratios
  40. * of base:extra, all the base memory can be filled with page
  41. * structures for the extra memory, leaving no space for anything
  42. * else.
  43. *
  44. * 10x seems like a reasonable balance between scaling flexibility and
  45. * leaving a practically usable system.
  46. */
  47. #define EXTRA_MEM_RATIO (10)
  48. static void __init xen_add_extra_mem(u64 start, u64 size)
  49. {
  50. unsigned long pfn;
  51. int i;
  52. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  53. /* Add new region. */
  54. if (xen_extra_mem[i].size == 0) {
  55. xen_extra_mem[i].start = start;
  56. xen_extra_mem[i].size = size;
  57. break;
  58. }
  59. /* Append to existing region. */
  60. if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
  61. xen_extra_mem[i].size += size;
  62. break;
  63. }
  64. }
  65. if (i == XEN_EXTRA_MEM_MAX_REGIONS)
  66. printk(KERN_WARNING "Warning: not enough extra memory regions\n");
  67. memblock_reserve(start, size);
  68. xen_max_p2m_pfn = PFN_DOWN(start + size);
  69. for (pfn = PFN_DOWN(start); pfn <= xen_max_p2m_pfn; pfn++)
  70. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  71. }
  72. static unsigned long __init xen_release_chunk(unsigned long start,
  73. unsigned long end)
  74. {
  75. struct xen_memory_reservation reservation = {
  76. .address_bits = 0,
  77. .extent_order = 0,
  78. .domid = DOMID_SELF
  79. };
  80. unsigned long len = 0;
  81. unsigned long pfn;
  82. int ret;
  83. for(pfn = start; pfn < end; pfn++) {
  84. unsigned long mfn = pfn_to_mfn(pfn);
  85. /* Make sure pfn exists to start with */
  86. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  87. continue;
  88. set_xen_guest_handle(reservation.extent_start, &mfn);
  89. reservation.nr_extents = 1;
  90. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  91. &reservation);
  92. WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
  93. if (ret == 1) {
  94. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  95. len++;
  96. }
  97. }
  98. printk(KERN_INFO "Freeing %lx-%lx pfn range: %lu pages freed\n",
  99. start, end, len);
  100. return len;
  101. }
  102. static unsigned long __init xen_set_identity_and_release(
  103. const struct e820entry *list, size_t map_size, unsigned long nr_pages)
  104. {
  105. phys_addr_t start = 0;
  106. unsigned long released = 0;
  107. unsigned long identity = 0;
  108. const struct e820entry *entry;
  109. int i;
  110. /*
  111. * Combine non-RAM regions and gaps until a RAM region (or the
  112. * end of the map) is reached, then set the 1:1 map and
  113. * release the pages (if available) in those non-RAM regions.
  114. *
  115. * The combined non-RAM regions are rounded to a whole number
  116. * of pages so any partial pages are accessible via the 1:1
  117. * mapping. This is needed for some BIOSes that put (for
  118. * example) the DMI tables in a reserved region that begins on
  119. * a non-page boundary.
  120. */
  121. for (i = 0, entry = list; i < map_size; i++, entry++) {
  122. phys_addr_t end = entry->addr + entry->size;
  123. if (entry->type == E820_RAM || i == map_size - 1) {
  124. unsigned long start_pfn = PFN_DOWN(start);
  125. unsigned long end_pfn = PFN_UP(end);
  126. if (entry->type == E820_RAM)
  127. end_pfn = PFN_UP(entry->addr);
  128. if (start_pfn < end_pfn) {
  129. if (start_pfn < nr_pages)
  130. released += xen_release_chunk(
  131. start_pfn, min(end_pfn, nr_pages));
  132. identity += set_phys_range_identity(
  133. start_pfn, end_pfn);
  134. }
  135. start = end;
  136. }
  137. }
  138. printk(KERN_INFO "Released %lu pages of unused memory\n", released);
  139. printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
  140. return released;
  141. }
  142. static unsigned long __init xen_get_max_pages(void)
  143. {
  144. unsigned long max_pages = MAX_DOMAIN_PAGES;
  145. domid_t domid = DOMID_SELF;
  146. int ret;
  147. /*
  148. * For the initial domain we use the maximum reservation as
  149. * the maximum page.
  150. *
  151. * For guest domains the current maximum reservation reflects
  152. * the current maximum rather than the static maximum. In this
  153. * case the e820 map provided to us will cover the static
  154. * maximum region.
  155. */
  156. if (xen_initial_domain()) {
  157. ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
  158. if (ret > 0)
  159. max_pages = ret;
  160. }
  161. return min(max_pages, MAX_DOMAIN_PAGES);
  162. }
  163. static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
  164. {
  165. u64 end = start + size;
  166. /* Align RAM regions to page boundaries. */
  167. if (type == E820_RAM) {
  168. start = PAGE_ALIGN(start);
  169. end &= ~((u64)PAGE_SIZE - 1);
  170. }
  171. e820_add_region(start, end - start, type);
  172. }
  173. /**
  174. * machine_specific_memory_setup - Hook for machine specific memory setup.
  175. **/
  176. char * __init xen_memory_setup(void)
  177. {
  178. static struct e820entry map[E820MAX] __initdata;
  179. unsigned long max_pfn = xen_start_info->nr_pages;
  180. unsigned long long mem_end;
  181. int rc;
  182. struct xen_memory_map memmap;
  183. unsigned long max_pages;
  184. unsigned long extra_pages = 0;
  185. int i;
  186. int op;
  187. max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
  188. mem_end = PFN_PHYS(max_pfn);
  189. memmap.nr_entries = E820MAX;
  190. set_xen_guest_handle(memmap.buffer, map);
  191. op = xen_initial_domain() ?
  192. XENMEM_machine_memory_map :
  193. XENMEM_memory_map;
  194. rc = HYPERVISOR_memory_op(op, &memmap);
  195. if (rc == -ENOSYS) {
  196. BUG_ON(xen_initial_domain());
  197. memmap.nr_entries = 1;
  198. map[0].addr = 0ULL;
  199. map[0].size = mem_end;
  200. /* 8MB slack (to balance backend allocations). */
  201. map[0].size += 8ULL << 20;
  202. map[0].type = E820_RAM;
  203. rc = 0;
  204. }
  205. BUG_ON(rc);
  206. /* Make sure the Xen-supplied memory map is well-ordered. */
  207. sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
  208. max_pages = xen_get_max_pages();
  209. if (max_pages > max_pfn)
  210. extra_pages += max_pages - max_pfn;
  211. /*
  212. * Set P2M for all non-RAM pages and E820 gaps to be identity
  213. * type PFNs. Any RAM pages that would be made inaccesible by
  214. * this are first released.
  215. */
  216. xen_released_pages = xen_set_identity_and_release(
  217. map, memmap.nr_entries, max_pfn);
  218. extra_pages += xen_released_pages;
  219. /*
  220. * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
  221. * factor the base size. On non-highmem systems, the base
  222. * size is the full initial memory allocation; on highmem it
  223. * is limited to the max size of lowmem, so that it doesn't
  224. * get completely filled.
  225. *
  226. * In principle there could be a problem in lowmem systems if
  227. * the initial memory is also very large with respect to
  228. * lowmem, but we won't try to deal with that here.
  229. */
  230. extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
  231. extra_pages);
  232. i = 0;
  233. while (i < memmap.nr_entries) {
  234. u64 addr = map[i].addr;
  235. u64 size = map[i].size;
  236. u32 type = map[i].type;
  237. if (type == E820_RAM) {
  238. if (addr < mem_end) {
  239. size = min(size, mem_end - addr);
  240. } else if (extra_pages) {
  241. size = min(size, (u64)extra_pages * PAGE_SIZE);
  242. extra_pages -= size / PAGE_SIZE;
  243. xen_add_extra_mem(addr, size);
  244. } else
  245. type = E820_UNUSABLE;
  246. }
  247. xen_align_and_add_e820_region(addr, size, type);
  248. map[i].addr += size;
  249. map[i].size -= size;
  250. if (map[i].size == 0)
  251. i++;
  252. }
  253. /*
  254. * In domU, the ISA region is normal, usable memory, but we
  255. * reserve ISA memory anyway because too many things poke
  256. * about in there.
  257. */
  258. e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
  259. E820_RESERVED);
  260. /*
  261. * Reserve Xen bits:
  262. * - mfn_list
  263. * - xen_start_info
  264. * See comment above "struct start_info" in <xen/interface/xen.h>
  265. */
  266. memblock_reserve(__pa(xen_start_info->mfn_list),
  267. xen_start_info->pt_base - xen_start_info->mfn_list);
  268. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  269. return "Xen";
  270. }
  271. /*
  272. * Set the bit indicating "nosegneg" library variants should be used.
  273. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  274. * can have un-truncated segments, so wrapping around is allowed.
  275. */
  276. static void __init fiddle_vdso(void)
  277. {
  278. #ifdef CONFIG_X86_32
  279. u32 *mask;
  280. mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
  281. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  282. mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
  283. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  284. #endif
  285. }
  286. static int __cpuinit register_callback(unsigned type, const void *func)
  287. {
  288. struct callback_register callback = {
  289. .type = type,
  290. .address = XEN_CALLBACK(__KERNEL_CS, func),
  291. .flags = CALLBACKF_mask_events,
  292. };
  293. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  294. }
  295. void __cpuinit xen_enable_sysenter(void)
  296. {
  297. int ret;
  298. unsigned sysenter_feature;
  299. #ifdef CONFIG_X86_32
  300. sysenter_feature = X86_FEATURE_SEP;
  301. #else
  302. sysenter_feature = X86_FEATURE_SYSENTER32;
  303. #endif
  304. if (!boot_cpu_has(sysenter_feature))
  305. return;
  306. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  307. if(ret != 0)
  308. setup_clear_cpu_cap(sysenter_feature);
  309. }
  310. void __cpuinit xen_enable_syscall(void)
  311. {
  312. #ifdef CONFIG_X86_64
  313. int ret;
  314. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  315. if (ret != 0) {
  316. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  317. /* Pretty fatal; 64-bit userspace has no other
  318. mechanism for syscalls. */
  319. }
  320. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  321. ret = register_callback(CALLBACKTYPE_syscall32,
  322. xen_syscall32_target);
  323. if (ret != 0)
  324. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  325. }
  326. #endif /* CONFIG_X86_64 */
  327. }
  328. void __init xen_arch_setup(void)
  329. {
  330. xen_panic_handler_init();
  331. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  332. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  333. if (!xen_feature(XENFEAT_auto_translated_physmap))
  334. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  335. VMASST_TYPE_pae_extended_cr3);
  336. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  337. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  338. BUG();
  339. xen_enable_sysenter();
  340. xen_enable_syscall();
  341. #ifdef CONFIG_ACPI
  342. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  343. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  344. disable_acpi();
  345. }
  346. #endif
  347. memcpy(boot_command_line, xen_start_info->cmd_line,
  348. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  349. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  350. /* Set up idle, making sure it calls safe_halt() pvop */
  351. #ifdef CONFIG_X86_32
  352. boot_cpu_data.hlt_works_ok = 1;
  353. #endif
  354. disable_cpuidle();
  355. WARN_ON(set_pm_idle_to_default());
  356. fiddle_vdso();
  357. }