setup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <asm/elf.h>
  11. #include <asm/vdso.h>
  12. #include <asm/e820.h>
  13. #include <asm/setup.h>
  14. #include <asm/acpi.h>
  15. #include <asm/xen/hypervisor.h>
  16. #include <asm/xen/hypercall.h>
  17. #include <xen/page.h>
  18. #include <xen/interface/callback.h>
  19. #include <xen/interface/physdev.h>
  20. #include <xen/interface/memory.h>
  21. #include <xen/features.h>
  22. #include "xen-ops.h"
  23. #include "vdso.h"
  24. /* These are code, but not functions. Defined in entry.S */
  25. extern const char xen_hypervisor_callback[];
  26. extern const char xen_failsafe_callback[];
  27. extern void xen_sysenter_target(void);
  28. extern void xen_syscall_target(void);
  29. extern void xen_syscall32_target(void);
  30. static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
  31. phys_addr_t end_addr)
  32. {
  33. struct xen_memory_reservation reservation = {
  34. .address_bits = 0,
  35. .extent_order = 0,
  36. .domid = DOMID_SELF
  37. };
  38. unsigned long start, end;
  39. unsigned long len = 0;
  40. unsigned long pfn;
  41. int ret;
  42. start = PFN_UP(start_addr);
  43. end = PFN_DOWN(end_addr);
  44. if (end <= start)
  45. return 0;
  46. printk(KERN_INFO "xen_release_chunk: looking at area pfn %lx-%lx: ",
  47. start, end);
  48. for(pfn = start; pfn < end; pfn++) {
  49. unsigned long mfn = pfn_to_mfn(pfn);
  50. /* Make sure pfn exists to start with */
  51. if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
  52. continue;
  53. set_xen_guest_handle(reservation.extent_start, &mfn);
  54. reservation.nr_extents = 1;
  55. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  56. &reservation);
  57. WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
  58. start, end, ret);
  59. if (ret == 1) {
  60. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  61. len++;
  62. }
  63. }
  64. printk(KERN_CONT "%ld pages freed\n", len);
  65. return len;
  66. }
  67. static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
  68. const struct e820map *e820)
  69. {
  70. phys_addr_t max_addr = PFN_PHYS(max_pfn);
  71. phys_addr_t last_end = 0;
  72. unsigned long released = 0;
  73. int i;
  74. for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
  75. phys_addr_t end = e820->map[i].addr;
  76. end = min(max_addr, end);
  77. released += xen_release_chunk(last_end, end);
  78. last_end = e820->map[i].addr + e820->map[i].size;
  79. }
  80. if (last_end < max_addr)
  81. released += xen_release_chunk(last_end, max_addr);
  82. printk(KERN_INFO "released %ld pages of unused memory\n", released);
  83. return released;
  84. }
  85. /**
  86. * machine_specific_memory_setup - Hook for machine specific memory setup.
  87. **/
  88. char * __init xen_memory_setup(void)
  89. {
  90. unsigned long max_pfn = xen_start_info->nr_pages;
  91. max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
  92. e820.nr_map = 0;
  93. e820_add_region(0, PFN_PHYS((u64)max_pfn), E820_RAM);
  94. /*
  95. * Even though this is normal, usable memory under Xen, reserve
  96. * ISA memory anyway because too many things think they can poke
  97. * about in there.
  98. */
  99. e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
  100. E820_RESERVED);
  101. /*
  102. * Reserve Xen bits:
  103. * - mfn_list
  104. * - xen_start_info
  105. * See comment above "struct start_info" in <xen/interface/xen.h>
  106. */
  107. reserve_early(__pa(xen_start_info->mfn_list),
  108. __pa(xen_start_info->pt_base),
  109. "XEN START INFO");
  110. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  111. xen_return_unused_memory(xen_start_info->nr_pages, &e820);
  112. return "Xen";
  113. }
  114. static void xen_idle(void)
  115. {
  116. local_irq_disable();
  117. if (need_resched())
  118. local_irq_enable();
  119. else {
  120. current_thread_info()->status &= ~TS_POLLING;
  121. smp_mb__after_clear_bit();
  122. safe_halt();
  123. current_thread_info()->status |= TS_POLLING;
  124. }
  125. }
  126. /*
  127. * Set the bit indicating "nosegneg" library variants should be used.
  128. * We only need to bother in pure 32-bit mode; compat 32-bit processes
  129. * can have un-truncated segments, so wrapping around is allowed.
  130. */
  131. static void __init fiddle_vdso(void)
  132. {
  133. #ifdef CONFIG_X86_32
  134. u32 *mask;
  135. mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
  136. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  137. mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
  138. *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
  139. #endif
  140. }
  141. static __cpuinit int register_callback(unsigned type, const void *func)
  142. {
  143. struct callback_register callback = {
  144. .type = type,
  145. .address = XEN_CALLBACK(__KERNEL_CS, func),
  146. .flags = CALLBACKF_mask_events,
  147. };
  148. return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
  149. }
  150. void __cpuinit xen_enable_sysenter(void)
  151. {
  152. int ret;
  153. unsigned sysenter_feature;
  154. #ifdef CONFIG_X86_32
  155. sysenter_feature = X86_FEATURE_SEP;
  156. #else
  157. sysenter_feature = X86_FEATURE_SYSENTER32;
  158. #endif
  159. if (!boot_cpu_has(sysenter_feature))
  160. return;
  161. ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
  162. if(ret != 0)
  163. setup_clear_cpu_cap(sysenter_feature);
  164. }
  165. void __cpuinit xen_enable_syscall(void)
  166. {
  167. #ifdef CONFIG_X86_64
  168. int ret;
  169. ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
  170. if (ret != 0) {
  171. printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
  172. /* Pretty fatal; 64-bit userspace has no other
  173. mechanism for syscalls. */
  174. }
  175. if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
  176. ret = register_callback(CALLBACKTYPE_syscall32,
  177. xen_syscall32_target);
  178. if (ret != 0)
  179. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  180. }
  181. #endif /* CONFIG_X86_64 */
  182. }
  183. void __init xen_arch_setup(void)
  184. {
  185. struct physdev_set_iopl set_iopl;
  186. int rc;
  187. xen_panic_handler_init();
  188. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
  189. HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
  190. if (!xen_feature(XENFEAT_auto_translated_physmap))
  191. HYPERVISOR_vm_assist(VMASST_CMD_enable,
  192. VMASST_TYPE_pae_extended_cr3);
  193. if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
  194. register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
  195. BUG();
  196. xen_enable_sysenter();
  197. xen_enable_syscall();
  198. set_iopl.iopl = 1;
  199. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  200. if (rc != 0)
  201. printk(KERN_INFO "physdev_op failed %d\n", rc);
  202. #ifdef CONFIG_ACPI
  203. if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
  204. printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
  205. disable_acpi();
  206. }
  207. #endif
  208. memcpy(boot_command_line, xen_start_info->cmd_line,
  209. MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
  210. COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
  211. pm_idle = xen_idle;
  212. paravirt_disable_iospace();
  213. fiddle_vdso();
  214. }