paravirt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. void __init default_banner(void)
  48. {
  49. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  50. pv_info.name);
  51. }
  52. /* Simple instruction patching code. */
  53. #define DEF_NATIVE(ops, name, code) \
  54. extern const char start_##ops##_##name[], end_##ops##_##name[]; \
  55. asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
  56. /* Undefined instruction for dealing with missing ops pointers. */
  57. static const unsigned char ud2a[] = { 0x0f, 0x0b };
  58. unsigned paravirt_patch_nop(void)
  59. {
  60. return 0;
  61. }
  62. unsigned paravirt_patch_ignore(unsigned len)
  63. {
  64. return len;
  65. }
  66. struct branch {
  67. unsigned char opcode;
  68. u32 delta;
  69. } __attribute__((packed));
  70. unsigned paravirt_patch_call(void *insnbuf,
  71. const void *target, u16 tgt_clobbers,
  72. unsigned long addr, u16 site_clobbers,
  73. unsigned len)
  74. {
  75. struct branch *b = insnbuf;
  76. unsigned long delta = (unsigned long)target - (addr+5);
  77. if (tgt_clobbers & ~site_clobbers)
  78. return len; /* target would clobber too much for this site */
  79. if (len < 5)
  80. return len; /* call too long for patch site */
  81. b->opcode = 0xe8; /* call */
  82. b->delta = delta;
  83. BUILD_BUG_ON(sizeof(*b) != 5);
  84. return 5;
  85. }
  86. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  87. unsigned long addr, unsigned len)
  88. {
  89. struct branch *b = insnbuf;
  90. unsigned long delta = (unsigned long)target - (addr+5);
  91. if (len < 5)
  92. return len; /* call too long for patch site */
  93. b->opcode = 0xe9; /* jmp */
  94. b->delta = delta;
  95. return 5;
  96. }
  97. /* Neat trick to map patch type back to the call within the
  98. * corresponding structure. */
  99. static void *get_call_destination(u8 type)
  100. {
  101. struct paravirt_patch_template tmpl = {
  102. .pv_init_ops = pv_init_ops,
  103. .pv_time_ops = pv_time_ops,
  104. .pv_cpu_ops = pv_cpu_ops,
  105. .pv_irq_ops = pv_irq_ops,
  106. .pv_apic_ops = pv_apic_ops,
  107. .pv_mmu_ops = pv_mmu_ops,
  108. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  109. .pv_lock_ops = pv_lock_ops,
  110. #endif
  111. };
  112. return *((void **)&tmpl + type);
  113. }
  114. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  115. unsigned long addr, unsigned len)
  116. {
  117. void *opfunc = get_call_destination(type);
  118. unsigned ret;
  119. if (opfunc == NULL)
  120. /* If there's no function, patch it with a ud2a (BUG) */
  121. ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
  122. else if (opfunc == _paravirt_nop)
  123. /* If the operation is a nop, then nop the callsite */
  124. ret = paravirt_patch_nop();
  125. /* identity functions just return their single argument */
  126. else if (opfunc == _paravirt_ident_32)
  127. ret = paravirt_patch_ident_32(insnbuf, len);
  128. else if (opfunc == _paravirt_ident_64)
  129. ret = paravirt_patch_ident_64(insnbuf, len);
  130. else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
  131. type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit) ||
  132. type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret32) ||
  133. type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret64))
  134. /* If operation requires a jmp, then jmp */
  135. ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
  136. else
  137. /* Otherwise call the function; assume target could
  138. clobber any caller-save reg */
  139. ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
  140. addr, clobbers, len);
  141. return ret;
  142. }
  143. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  144. const char *start, const char *end)
  145. {
  146. unsigned insn_len = end - start;
  147. if (insn_len > len || start == NULL)
  148. insn_len = len;
  149. else
  150. memcpy(insnbuf, start, insn_len);
  151. return insn_len;
  152. }
  153. static void native_flush_tlb(void)
  154. {
  155. __native_flush_tlb();
  156. }
  157. /*
  158. * Global pages have to be flushed a bit differently. Not a real
  159. * performance problem because this does not happen often.
  160. */
  161. static void native_flush_tlb_global(void)
  162. {
  163. __native_flush_tlb_global();
  164. }
  165. static void native_flush_tlb_single(unsigned long addr)
  166. {
  167. __native_flush_tlb_single(addr);
  168. }
  169. struct jump_label_key paravirt_steal_enabled;
  170. struct jump_label_key paravirt_steal_rq_enabled;
  171. static u64 native_steal_clock(int cpu)
  172. {
  173. return 0;
  174. }
  175. /* These are in entry.S */
  176. extern void native_iret(void);
  177. extern void native_irq_enable_sysexit(void);
  178. extern void native_usergs_sysret32(void);
  179. extern void native_usergs_sysret64(void);
  180. static struct resource reserve_ioports = {
  181. .start = 0,
  182. .end = IO_SPACE_LIMIT,
  183. .name = "paravirt-ioport",
  184. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  185. };
  186. /*
  187. * Reserve the whole legacy IO space to prevent any legacy drivers
  188. * from wasting time probing for their hardware. This is a fairly
  189. * brute-force approach to disabling all non-virtual drivers.
  190. *
  191. * Note that this must be called very early to have any effect.
  192. */
  193. int paravirt_disable_iospace(void)
  194. {
  195. return request_resource(&ioport_resource, &reserve_ioports);
  196. }
  197. static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  198. static inline void enter_lazy(enum paravirt_lazy_mode mode)
  199. {
  200. BUG_ON(percpu_read(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
  201. percpu_write(paravirt_lazy_mode, mode);
  202. }
  203. static void leave_lazy(enum paravirt_lazy_mode mode)
  204. {
  205. BUG_ON(percpu_read(paravirt_lazy_mode) != mode);
  206. percpu_write(paravirt_lazy_mode, PARAVIRT_LAZY_NONE);
  207. }
  208. void paravirt_enter_lazy_mmu(void)
  209. {
  210. enter_lazy(PARAVIRT_LAZY_MMU);
  211. }
  212. void paravirt_leave_lazy_mmu(void)
  213. {
  214. leave_lazy(PARAVIRT_LAZY_MMU);
  215. }
  216. void paravirt_start_context_switch(struct task_struct *prev)
  217. {
  218. BUG_ON(preemptible());
  219. if (percpu_read(paravirt_lazy_mode) == PARAVIRT_LAZY_MMU) {
  220. arch_leave_lazy_mmu_mode();
  221. set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
  222. }
  223. enter_lazy(PARAVIRT_LAZY_CPU);
  224. }
  225. void paravirt_end_context_switch(struct task_struct *next)
  226. {
  227. BUG_ON(preemptible());
  228. leave_lazy(PARAVIRT_LAZY_CPU);
  229. if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
  230. arch_enter_lazy_mmu_mode();
  231. }
  232. enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
  233. {
  234. if (in_interrupt())
  235. return PARAVIRT_LAZY_NONE;
  236. return percpu_read(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. arch_leave_lazy_mmu_mode();
  243. arch_enter_lazy_mmu_mode();
  244. }
  245. preempt_enable();
  246. }
  247. struct pv_info pv_info = {
  248. .name = "bare hardware",
  249. .paravirt_enabled = 0,
  250. .kernel_rpl = 0,
  251. .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
  252. };
  253. struct pv_init_ops pv_init_ops = {
  254. .patch = native_patch,
  255. };
  256. struct pv_time_ops pv_time_ops = {
  257. .sched_clock = native_sched_clock,
  258. .steal_clock = native_steal_clock,
  259. };
  260. struct pv_irq_ops pv_irq_ops = {
  261. .save_fl = __PV_IS_CALLEE_SAVE(native_save_fl),
  262. .restore_fl = __PV_IS_CALLEE_SAVE(native_restore_fl),
  263. .irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable),
  264. .irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable),
  265. .safe_halt = native_safe_halt,
  266. .halt = native_halt,
  267. #ifdef CONFIG_X86_64
  268. .adjust_exception_frame = paravirt_nop,
  269. #endif
  270. };
  271. struct pv_cpu_ops pv_cpu_ops = {
  272. .cpuid = native_cpuid,
  273. .get_debugreg = native_get_debugreg,
  274. .set_debugreg = native_set_debugreg,
  275. .clts = native_clts,
  276. .read_cr0 = native_read_cr0,
  277. .write_cr0 = native_write_cr0,
  278. .read_cr4 = native_read_cr4,
  279. .read_cr4_safe = native_read_cr4_safe,
  280. .write_cr4 = native_write_cr4,
  281. #ifdef CONFIG_X86_64
  282. .read_cr8 = native_read_cr8,
  283. .write_cr8 = native_write_cr8,
  284. #endif
  285. .wbinvd = native_wbinvd,
  286. .read_msr = native_read_msr_safe,
  287. .rdmsr_regs = native_rdmsr_safe_regs,
  288. .write_msr = native_write_msr_safe,
  289. .wrmsr_regs = native_wrmsr_safe_regs,
  290. .read_tsc = native_read_tsc,
  291. .read_pmc = native_read_pmc,
  292. .read_tscp = native_read_tscp,
  293. .load_tr_desc = native_load_tr_desc,
  294. .set_ldt = native_set_ldt,
  295. .load_gdt = native_load_gdt,
  296. .load_idt = native_load_idt,
  297. .store_gdt = native_store_gdt,
  298. .store_idt = native_store_idt,
  299. .store_tr = native_store_tr,
  300. .load_tls = native_load_tls,
  301. #ifdef CONFIG_X86_64
  302. .load_gs_index = native_load_gs_index,
  303. #endif
  304. .write_ldt_entry = native_write_ldt_entry,
  305. .write_gdt_entry = native_write_gdt_entry,
  306. .write_idt_entry = native_write_idt_entry,
  307. .alloc_ldt = paravirt_nop,
  308. .free_ldt = paravirt_nop,
  309. .load_sp0 = native_load_sp0,
  310. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  311. .irq_enable_sysexit = native_irq_enable_sysexit,
  312. #endif
  313. #ifdef CONFIG_X86_64
  314. #ifdef CONFIG_IA32_EMULATION
  315. .usergs_sysret32 = native_usergs_sysret32,
  316. #endif
  317. .usergs_sysret64 = native_usergs_sysret64,
  318. #endif
  319. .iret = native_iret,
  320. .swapgs = native_swapgs,
  321. .set_iopl_mask = native_set_iopl_mask,
  322. .io_delay = native_io_delay,
  323. .start_context_switch = paravirt_nop,
  324. .end_context_switch = paravirt_nop,
  325. };
  326. struct pv_apic_ops pv_apic_ops = {
  327. #ifdef CONFIG_X86_LOCAL_APIC
  328. .startup_ipi_hook = paravirt_nop,
  329. #endif
  330. };
  331. #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
  332. /* 32-bit pagetable entries */
  333. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_32)
  334. #else
  335. /* 64-bit pagetable entries */
  336. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
  337. #endif
  338. struct pv_mmu_ops pv_mmu_ops = {
  339. .read_cr2 = native_read_cr2,
  340. .write_cr2 = native_write_cr2,
  341. .read_cr3 = native_read_cr3,
  342. .write_cr3 = native_write_cr3,
  343. .flush_tlb_user = native_flush_tlb,
  344. .flush_tlb_kernel = native_flush_tlb_global,
  345. .flush_tlb_single = native_flush_tlb_single,
  346. .flush_tlb_others = native_flush_tlb_others,
  347. .pgd_alloc = __paravirt_pgd_alloc,
  348. .pgd_free = paravirt_nop,
  349. .alloc_pte = paravirt_nop,
  350. .alloc_pmd = paravirt_nop,
  351. .alloc_pud = paravirt_nop,
  352. .release_pte = paravirt_nop,
  353. .release_pmd = paravirt_nop,
  354. .release_pud = paravirt_nop,
  355. .set_pte = native_set_pte,
  356. .set_pte_at = native_set_pte_at,
  357. .set_pmd = native_set_pmd,
  358. .set_pmd_at = native_set_pmd_at,
  359. .pte_update = paravirt_nop,
  360. .pte_update_defer = paravirt_nop,
  361. .pmd_update = paravirt_nop,
  362. .pmd_update_defer = paravirt_nop,
  363. .ptep_modify_prot_start = __ptep_modify_prot_start,
  364. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  365. #if PAGETABLE_LEVELS >= 3
  366. #ifdef CONFIG_X86_PAE
  367. .set_pte_atomic = native_set_pte_atomic,
  368. .pte_clear = native_pte_clear,
  369. .pmd_clear = native_pmd_clear,
  370. #endif
  371. .set_pud = native_set_pud,
  372. .pmd_val = PTE_IDENT,
  373. .make_pmd = PTE_IDENT,
  374. #if PAGETABLE_LEVELS == 4
  375. .pud_val = PTE_IDENT,
  376. .make_pud = PTE_IDENT,
  377. .set_pgd = native_set_pgd,
  378. #endif
  379. #endif /* PAGETABLE_LEVELS >= 3 */
  380. .pte_val = PTE_IDENT,
  381. .pgd_val = PTE_IDENT,
  382. .make_pte = PTE_IDENT,
  383. .make_pgd = PTE_IDENT,
  384. .dup_mmap = paravirt_nop,
  385. .exit_mmap = paravirt_nop,
  386. .activate_mm = paravirt_nop,
  387. .lazy_mode = {
  388. .enter = paravirt_nop,
  389. .leave = paravirt_nop,
  390. },
  391. .set_fixmap = native_set_fixmap,
  392. };
  393. EXPORT_SYMBOL_GPL(pv_time_ops);
  394. EXPORT_SYMBOL (pv_cpu_ops);
  395. EXPORT_SYMBOL (pv_mmu_ops);
  396. EXPORT_SYMBOL_GPL(pv_apic_ops);
  397. EXPORT_SYMBOL_GPL(pv_info);
  398. EXPORT_SYMBOL (pv_irq_ops);