paravirt.c 11 KB

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