paravirt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* Paravirtualization interfaces
  2. Copyright (C) 2006 Rusty Russell IBM Corporation
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. 2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. #include <linux/efi.h>
  19. #include <linux/bcd.h>
  20. #include <linux/highmem.h>
  21. #include <asm/bug.h>
  22. #include <asm/paravirt.h>
  23. #include <asm/desc.h>
  24. #include <asm/setup.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/time.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/irq.h>
  29. #include <asm/delay.h>
  30. #include <asm/fixmap.h>
  31. #include <asm/apic.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/timer.h>
  34. /* nop stub */
  35. void _paravirt_nop(void)
  36. {
  37. }
  38. /* identity function, which can be inlined */
  39. u32 _paravirt_ident_32(u32 x)
  40. {
  41. return x;
  42. }
  43. u64 _paravirt_ident_64(u64 x)
  44. {
  45. return x;
  46. }
  47. static void __init default_banner(void)
  48. {
  49. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  50. pv_info.name);
  51. }
  52. char *memory_setup(void)
  53. {
  54. return pv_init_ops.memory_setup();
  55. }
  56. /* Simple instruction patching code. */
  57. #define DEF_NATIVE(ops, name, code) \
  58. extern const char start_##ops##_##name[], end_##ops##_##name[]; \
  59. asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
  60. /* Undefined instruction for dealing with missing ops pointers. */
  61. static const unsigned char ud2a[] = { 0x0f, 0x0b };
  62. unsigned paravirt_patch_nop(void)
  63. {
  64. return 0;
  65. }
  66. unsigned paravirt_patch_ignore(unsigned len)
  67. {
  68. return len;
  69. }
  70. struct branch {
  71. unsigned char opcode;
  72. u32 delta;
  73. } __attribute__((packed));
  74. unsigned paravirt_patch_call(void *insnbuf,
  75. const void *target, u16 tgt_clobbers,
  76. unsigned long addr, u16 site_clobbers,
  77. unsigned len)
  78. {
  79. struct branch *b = insnbuf;
  80. unsigned long delta = (unsigned long)target - (addr+5);
  81. if (tgt_clobbers & ~site_clobbers)
  82. return len; /* target would clobber too much for this site */
  83. if (len < 5)
  84. return len; /* call too long for patch site */
  85. b->opcode = 0xe8; /* call */
  86. b->delta = delta;
  87. BUILD_BUG_ON(sizeof(*b) != 5);
  88. return 5;
  89. }
  90. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  91. unsigned long addr, unsigned len)
  92. {
  93. struct branch *b = insnbuf;
  94. unsigned long delta = (unsigned long)target - (addr+5);
  95. if (len < 5)
  96. return len; /* call too long for patch site */
  97. b->opcode = 0xe9; /* jmp */
  98. b->delta = delta;
  99. return 5;
  100. }
  101. /* Neat trick to map patch type back to the call within the
  102. * corresponding structure. */
  103. static void *get_call_destination(u8 type)
  104. {
  105. struct paravirt_patch_template tmpl = {
  106. .pv_init_ops = pv_init_ops,
  107. .pv_time_ops = pv_time_ops,
  108. .pv_cpu_ops = pv_cpu_ops,
  109. .pv_irq_ops = pv_irq_ops,
  110. .pv_apic_ops = pv_apic_ops,
  111. .pv_mmu_ops = pv_mmu_ops,
  112. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  113. .pv_lock_ops = pv_lock_ops,
  114. #endif
  115. };
  116. return *((void **)&tmpl + type);
  117. }
  118. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  119. unsigned long addr, unsigned len)
  120. {
  121. void *opfunc = get_call_destination(type);
  122. unsigned ret;
  123. if (opfunc == NULL)
  124. /* If there's no function, patch it with a ud2a (BUG) */
  125. ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
  126. else if (opfunc == _paravirt_nop)
  127. /* If the operation is a nop, then nop the callsite */
  128. ret = paravirt_patch_nop();
  129. /* identity functions just return their single argument */
  130. else if (opfunc == _paravirt_ident_32)
  131. ret = paravirt_patch_ident_32(insnbuf, len);
  132. else if (opfunc == _paravirt_ident_64)
  133. ret = paravirt_patch_ident_64(insnbuf, len);
  134. else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
  135. type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit) ||
  136. type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret32) ||
  137. type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret64))
  138. /* If operation requires a jmp, then jmp */
  139. ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
  140. else
  141. /* Otherwise call the function; assume target could
  142. clobber any caller-save reg */
  143. ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
  144. addr, clobbers, len);
  145. return ret;
  146. }
  147. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  148. const char *start, const char *end)
  149. {
  150. unsigned insn_len = end - start;
  151. if (insn_len > len || start == NULL)
  152. insn_len = len;
  153. else
  154. memcpy(insnbuf, start, insn_len);
  155. return insn_len;
  156. }
  157. void init_IRQ(void)
  158. {
  159. pv_irq_ops.init_IRQ();
  160. }
  161. static void native_flush_tlb(void)
  162. {
  163. __native_flush_tlb();
  164. }
  165. /*
  166. * Global pages have to be flushed a bit differently. Not a real
  167. * performance problem because this does not happen often.
  168. */
  169. static void native_flush_tlb_global(void)
  170. {
  171. __native_flush_tlb_global();
  172. }
  173. static void native_flush_tlb_single(unsigned long addr)
  174. {
  175. __native_flush_tlb_single(addr);
  176. }
  177. /* These are in entry.S */
  178. extern void native_iret(void);
  179. extern void native_irq_enable_sysexit(void);
  180. extern void native_usergs_sysret32(void);
  181. extern void native_usergs_sysret64(void);
  182. static int __init print_banner(void)
  183. {
  184. pv_init_ops.banner();
  185. return 0;
  186. }
  187. core_initcall(print_banner);
  188. static struct resource reserve_ioports = {
  189. .start = 0,
  190. .end = IO_SPACE_LIMIT,
  191. .name = "paravirt-ioport",
  192. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  193. };
  194. /*
  195. * Reserve the whole legacy IO space to prevent any legacy drivers
  196. * from wasting time probing for their hardware. This is a fairly
  197. * brute-force approach to disabling all non-virtual drivers.
  198. *
  199. * Note that this must be called very early to have any effect.
  200. */
  201. int paravirt_disable_iospace(void)
  202. {
  203. return request_resource(&ioport_resource, &reserve_ioports);
  204. }
  205. static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  206. static inline void enter_lazy(enum paravirt_lazy_mode mode)
  207. {
  208. BUG_ON(__get_cpu_var(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
  209. BUG_ON(preemptible());
  210. __get_cpu_var(paravirt_lazy_mode) = mode;
  211. }
  212. void paravirt_leave_lazy(enum paravirt_lazy_mode mode)
  213. {
  214. BUG_ON(__get_cpu_var(paravirt_lazy_mode) != mode);
  215. BUG_ON(preemptible());
  216. __get_cpu_var(paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  217. }
  218. void paravirt_enter_lazy_mmu(void)
  219. {
  220. enter_lazy(PARAVIRT_LAZY_MMU);
  221. }
  222. void paravirt_leave_lazy_mmu(void)
  223. {
  224. paravirt_leave_lazy(PARAVIRT_LAZY_MMU);
  225. }
  226. void paravirt_enter_lazy_cpu(void)
  227. {
  228. enter_lazy(PARAVIRT_LAZY_CPU);
  229. }
  230. void paravirt_leave_lazy_cpu(void)
  231. {
  232. paravirt_leave_lazy(PARAVIRT_LAZY_CPU);
  233. }
  234. enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
  235. {
  236. return __get_cpu_var(paravirt_lazy_mode);
  237. }
  238. void arch_flush_lazy_mmu_mode(void)
  239. {
  240. preempt_disable();
  241. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  242. WARN_ON(preempt_count() == 1);
  243. arch_leave_lazy_mmu_mode();
  244. arch_enter_lazy_mmu_mode();
  245. }
  246. preempt_enable();
  247. }
  248. void arch_flush_lazy_cpu_mode(void)
  249. {
  250. preempt_disable();
  251. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  252. WARN_ON(preempt_count() == 1);
  253. arch_leave_lazy_cpu_mode();
  254. arch_enter_lazy_cpu_mode();
  255. }
  256. preempt_enable();
  257. }
  258. struct pv_info pv_info = {
  259. .name = "bare hardware",
  260. .paravirt_enabled = 0,
  261. .kernel_rpl = 0,
  262. .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
  263. };
  264. struct pv_init_ops pv_init_ops = {
  265. .patch = native_patch,
  266. .banner = default_banner,
  267. .arch_setup = paravirt_nop,
  268. .memory_setup = machine_specific_memory_setup,
  269. };
  270. struct pv_time_ops pv_time_ops = {
  271. .time_init = hpet_time_init,
  272. .get_wallclock = native_get_wallclock,
  273. .set_wallclock = native_set_wallclock,
  274. .sched_clock = native_sched_clock,
  275. .get_tsc_khz = native_calibrate_tsc,
  276. };
  277. struct pv_irq_ops pv_irq_ops = {
  278. .init_IRQ = native_init_IRQ,
  279. .save_fl = __PV_IS_CALLEE_SAVE(native_save_fl),
  280. .restore_fl = __PV_IS_CALLEE_SAVE(native_restore_fl),
  281. .irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable),
  282. .irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable),
  283. .safe_halt = native_safe_halt,
  284. .halt = native_halt,
  285. #ifdef CONFIG_X86_64
  286. .adjust_exception_frame = paravirt_nop,
  287. #endif
  288. };
  289. struct pv_cpu_ops pv_cpu_ops = {
  290. .cpuid = native_cpuid,
  291. .get_debugreg = native_get_debugreg,
  292. .set_debugreg = native_set_debugreg,
  293. .clts = native_clts,
  294. .read_cr0 = native_read_cr0,
  295. .write_cr0 = native_write_cr0,
  296. .read_cr4 = native_read_cr4,
  297. .read_cr4_safe = native_read_cr4_safe,
  298. .write_cr4 = native_write_cr4,
  299. #ifdef CONFIG_X86_64
  300. .read_cr8 = native_read_cr8,
  301. .write_cr8 = native_write_cr8,
  302. #endif
  303. .wbinvd = native_wbinvd,
  304. .read_msr = native_read_msr_safe,
  305. .read_msr_amd = native_read_msr_amd_safe,
  306. .write_msr = native_write_msr_safe,
  307. .read_tsc = native_read_tsc,
  308. .read_pmc = native_read_pmc,
  309. .read_tscp = native_read_tscp,
  310. .load_tr_desc = native_load_tr_desc,
  311. .set_ldt = native_set_ldt,
  312. .load_gdt = native_load_gdt,
  313. .load_idt = native_load_idt,
  314. .store_gdt = native_store_gdt,
  315. .store_idt = native_store_idt,
  316. .store_tr = native_store_tr,
  317. .load_tls = native_load_tls,
  318. #ifdef CONFIG_X86_64
  319. .load_gs_index = native_load_gs_index,
  320. #endif
  321. .write_ldt_entry = native_write_ldt_entry,
  322. .write_gdt_entry = native_write_gdt_entry,
  323. .write_idt_entry = native_write_idt_entry,
  324. .alloc_ldt = paravirt_nop,
  325. .free_ldt = paravirt_nop,
  326. .load_sp0 = native_load_sp0,
  327. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  328. .irq_enable_sysexit = native_irq_enable_sysexit,
  329. #endif
  330. #ifdef CONFIG_X86_64
  331. #ifdef CONFIG_IA32_EMULATION
  332. .usergs_sysret32 = native_usergs_sysret32,
  333. #endif
  334. .usergs_sysret64 = native_usergs_sysret64,
  335. #endif
  336. .iret = native_iret,
  337. .swapgs = native_swapgs,
  338. .set_iopl_mask = native_set_iopl_mask,
  339. .io_delay = native_io_delay,
  340. .lazy_mode = {
  341. .enter = paravirt_nop,
  342. .leave = paravirt_nop,
  343. },
  344. };
  345. struct pv_apic_ops pv_apic_ops = {
  346. #ifdef CONFIG_X86_LOCAL_APIC
  347. .setup_boot_clock = setup_boot_APIC_clock,
  348. .setup_secondary_clock = setup_secondary_APIC_clock,
  349. .startup_ipi_hook = paravirt_nop,
  350. #endif
  351. };
  352. #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
  353. /* 32-bit pagetable entries */
  354. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_32)
  355. #else
  356. /* 64-bit pagetable entries */
  357. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
  358. #endif
  359. struct pv_mmu_ops pv_mmu_ops = {
  360. #ifndef CONFIG_X86_64
  361. .pagetable_setup_start = native_pagetable_setup_start,
  362. .pagetable_setup_done = native_pagetable_setup_done,
  363. #else
  364. .pagetable_setup_start = paravirt_nop,
  365. .pagetable_setup_done = paravirt_nop,
  366. #endif
  367. .read_cr2 = native_read_cr2,
  368. .write_cr2 = native_write_cr2,
  369. .read_cr3 = native_read_cr3,
  370. .write_cr3 = native_write_cr3,
  371. .flush_tlb_user = native_flush_tlb,
  372. .flush_tlb_kernel = native_flush_tlb_global,
  373. .flush_tlb_single = native_flush_tlb_single,
  374. .flush_tlb_others = native_flush_tlb_others,
  375. .pgd_alloc = __paravirt_pgd_alloc,
  376. .pgd_free = paravirt_nop,
  377. .alloc_pte = paravirt_nop,
  378. .alloc_pmd = paravirt_nop,
  379. .alloc_pmd_clone = paravirt_nop,
  380. .alloc_pud = paravirt_nop,
  381. .release_pte = paravirt_nop,
  382. .release_pmd = paravirt_nop,
  383. .release_pud = paravirt_nop,
  384. .set_pte = native_set_pte,
  385. .set_pte_at = native_set_pte_at,
  386. .set_pmd = native_set_pmd,
  387. .pte_update = paravirt_nop,
  388. .pte_update_defer = paravirt_nop,
  389. .ptep_modify_prot_start = __ptep_modify_prot_start,
  390. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  391. #ifdef CONFIG_HIGHPTE
  392. .kmap_atomic_pte = kmap_atomic,
  393. #endif
  394. #if PAGETABLE_LEVELS >= 3
  395. #ifdef CONFIG_X86_PAE
  396. .set_pte_atomic = native_set_pte_atomic,
  397. .pte_clear = native_pte_clear,
  398. .pmd_clear = native_pmd_clear,
  399. #endif
  400. .set_pud = native_set_pud,
  401. .pmd_val = PTE_IDENT,
  402. .make_pmd = PTE_IDENT,
  403. #if PAGETABLE_LEVELS == 4
  404. .pud_val = PTE_IDENT,
  405. .make_pud = PTE_IDENT,
  406. .set_pgd = native_set_pgd,
  407. #endif
  408. #endif /* PAGETABLE_LEVELS >= 3 */
  409. .pte_val = PTE_IDENT,
  410. .pgd_val = PTE_IDENT,
  411. .make_pte = PTE_IDENT,
  412. .make_pgd = PTE_IDENT,
  413. .dup_mmap = paravirt_nop,
  414. .exit_mmap = paravirt_nop,
  415. .activate_mm = paravirt_nop,
  416. .lazy_mode = {
  417. .enter = paravirt_nop,
  418. .leave = paravirt_nop,
  419. },
  420. .set_fixmap = native_set_fixmap,
  421. };
  422. EXPORT_SYMBOL_GPL(pv_time_ops);
  423. EXPORT_SYMBOL (pv_cpu_ops);
  424. EXPORT_SYMBOL (pv_mmu_ops);
  425. EXPORT_SYMBOL_GPL(pv_apic_ops);
  426. EXPORT_SYMBOL_GPL(pv_info);
  427. EXPORT_SYMBOL (pv_irq_ops);