paravirt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. */
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/efi.h>
  18. #include <linux/bcd.h>
  19. #include <linux/start_kernel.h>
  20. #include <asm/bug.h>
  21. #include <asm/paravirt.h>
  22. #include <asm/desc.h>
  23. #include <asm/setup.h>
  24. #include <asm/arch_hooks.h>
  25. #include <asm/time.h>
  26. #include <asm/irq.h>
  27. #include <asm/delay.h>
  28. #include <asm/fixmap.h>
  29. #include <asm/apic.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/timer.h>
  32. /* nop stub */
  33. static void native_nop(void)
  34. {
  35. }
  36. static void __init default_banner(void)
  37. {
  38. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  39. paravirt_ops.name);
  40. }
  41. char *memory_setup(void)
  42. {
  43. return paravirt_ops.memory_setup();
  44. }
  45. /* Simple instruction patching code. */
  46. #define DEF_NATIVE(name, code) \
  47. extern const char start_##name[], end_##name[]; \
  48. asm("start_" #name ": " code "; end_" #name ":")
  49. DEF_NATIVE(cli, "cli");
  50. DEF_NATIVE(sti, "sti");
  51. DEF_NATIVE(popf, "push %eax; popf");
  52. DEF_NATIVE(pushf, "pushf; pop %eax");
  53. DEF_NATIVE(pushf_cli, "pushf; pop %eax; cli");
  54. DEF_NATIVE(iret, "iret");
  55. DEF_NATIVE(sti_sysexit, "sti; sysexit");
  56. static const struct native_insns
  57. {
  58. const char *start, *end;
  59. } native_insns[] = {
  60. [PARAVIRT_IRQ_DISABLE] = { start_cli, end_cli },
  61. [PARAVIRT_IRQ_ENABLE] = { start_sti, end_sti },
  62. [PARAVIRT_RESTORE_FLAGS] = { start_popf, end_popf },
  63. [PARAVIRT_SAVE_FLAGS] = { start_pushf, end_pushf },
  64. [PARAVIRT_SAVE_FLAGS_IRQ_DISABLE] = { start_pushf_cli, end_pushf_cli },
  65. [PARAVIRT_INTERRUPT_RETURN] = { start_iret, end_iret },
  66. [PARAVIRT_STI_SYSEXIT] = { start_sti_sysexit, end_sti_sysexit },
  67. };
  68. static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
  69. {
  70. unsigned int insn_len;
  71. /* Don't touch it if we don't have a replacement */
  72. if (type >= ARRAY_SIZE(native_insns) || !native_insns[type].start)
  73. return len;
  74. insn_len = native_insns[type].end - native_insns[type].start;
  75. /* Similarly if we can't fit replacement. */
  76. if (len < insn_len)
  77. return len;
  78. memcpy(insns, native_insns[type].start, insn_len);
  79. return insn_len;
  80. }
  81. void init_IRQ(void)
  82. {
  83. paravirt_ops.init_IRQ();
  84. }
  85. static void native_flush_tlb(void)
  86. {
  87. __native_flush_tlb();
  88. }
  89. /*
  90. * Global pages have to be flushed a bit differently. Not a real
  91. * performance problem because this does not happen often.
  92. */
  93. static void native_flush_tlb_global(void)
  94. {
  95. __native_flush_tlb_global();
  96. }
  97. static void native_flush_tlb_single(u32 addr)
  98. {
  99. __native_flush_tlb_single(addr);
  100. }
  101. #ifndef CONFIG_X86_PAE
  102. static void native_set_pte(pte_t *ptep, pte_t pteval)
  103. {
  104. *ptep = pteval;
  105. }
  106. static void native_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
  107. {
  108. *ptep = pteval;
  109. }
  110. static void native_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  111. {
  112. *pmdp = pmdval;
  113. }
  114. #else /* CONFIG_X86_PAE */
  115. static void native_set_pte(pte_t *ptep, pte_t pte)
  116. {
  117. ptep->pte_high = pte.pte_high;
  118. smp_wmb();
  119. ptep->pte_low = pte.pte_low;
  120. }
  121. static void native_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pte)
  122. {
  123. ptep->pte_high = pte.pte_high;
  124. smp_wmb();
  125. ptep->pte_low = pte.pte_low;
  126. }
  127. static void native_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  128. {
  129. ptep->pte_low = 0;
  130. smp_wmb();
  131. ptep->pte_high = pte.pte_high;
  132. smp_wmb();
  133. ptep->pte_low = pte.pte_low;
  134. }
  135. static void native_set_pte_atomic(pte_t *ptep, pte_t pteval)
  136. {
  137. set_64bit((unsigned long long *)ptep,pte_val(pteval));
  138. }
  139. static void native_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  140. {
  141. set_64bit((unsigned long long *)pmdp,pmd_val(pmdval));
  142. }
  143. static void native_set_pud(pud_t *pudp, pud_t pudval)
  144. {
  145. *pudp = pudval;
  146. }
  147. static void native_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  148. {
  149. ptep->pte_low = 0;
  150. smp_wmb();
  151. ptep->pte_high = 0;
  152. }
  153. static void native_pmd_clear(pmd_t *pmd)
  154. {
  155. u32 *tmp = (u32 *)pmd;
  156. *tmp = 0;
  157. smp_wmb();
  158. *(tmp + 1) = 0;
  159. }
  160. #endif /* CONFIG_X86_PAE */
  161. /* These are in entry.S */
  162. extern void native_iret(void);
  163. extern void native_irq_enable_sysexit(void);
  164. static int __init print_banner(void)
  165. {
  166. paravirt_ops.banner();
  167. return 0;
  168. }
  169. core_initcall(print_banner);
  170. struct paravirt_ops paravirt_ops = {
  171. .name = "bare hardware",
  172. .paravirt_enabled = 0,
  173. .kernel_rpl = 0,
  174. .patch = native_patch,
  175. .banner = default_banner,
  176. .arch_setup = native_nop,
  177. .memory_setup = machine_specific_memory_setup,
  178. .get_wallclock = native_get_wallclock,
  179. .set_wallclock = native_set_wallclock,
  180. .time_init = hpet_time_init,
  181. .init_IRQ = native_init_IRQ,
  182. .cpuid = native_cpuid,
  183. .get_debugreg = native_get_debugreg,
  184. .set_debugreg = native_set_debugreg,
  185. .clts = native_clts,
  186. .read_cr0 = native_read_cr0,
  187. .write_cr0 = native_write_cr0,
  188. .read_cr2 = native_read_cr2,
  189. .write_cr2 = native_write_cr2,
  190. .read_cr3 = native_read_cr3,
  191. .write_cr3 = native_write_cr3,
  192. .read_cr4 = native_read_cr4,
  193. .read_cr4_safe = native_read_cr4_safe,
  194. .write_cr4 = native_write_cr4,
  195. .save_fl = native_save_fl,
  196. .restore_fl = native_restore_fl,
  197. .irq_disable = native_irq_disable,
  198. .irq_enable = native_irq_enable,
  199. .safe_halt = native_safe_halt,
  200. .halt = native_halt,
  201. .wbinvd = native_wbinvd,
  202. .read_msr = native_read_msr_safe,
  203. .write_msr = native_write_msr_safe,
  204. .read_tsc = native_read_tsc,
  205. .read_pmc = native_read_pmc,
  206. .get_scheduled_cycles = native_read_tsc,
  207. .get_cpu_khz = native_calculate_cpu_khz,
  208. .load_tr_desc = native_load_tr_desc,
  209. .set_ldt = native_set_ldt,
  210. .load_gdt = native_load_gdt,
  211. .load_idt = native_load_idt,
  212. .store_gdt = native_store_gdt,
  213. .store_idt = native_store_idt,
  214. .store_tr = native_store_tr,
  215. .load_tls = native_load_tls,
  216. .write_ldt_entry = write_dt_entry,
  217. .write_gdt_entry = write_dt_entry,
  218. .write_idt_entry = write_dt_entry,
  219. .load_esp0 = native_load_esp0,
  220. .set_iopl_mask = native_set_iopl_mask,
  221. .io_delay = native_io_delay,
  222. #ifdef CONFIG_X86_LOCAL_APIC
  223. .apic_write = native_apic_write,
  224. .apic_write_atomic = native_apic_write_atomic,
  225. .apic_read = native_apic_read,
  226. .setup_boot_clock = setup_boot_APIC_clock,
  227. .setup_secondary_clock = setup_secondary_APIC_clock,
  228. #endif
  229. .set_lazy_mode = (void *)native_nop,
  230. .flush_tlb_user = native_flush_tlb,
  231. .flush_tlb_kernel = native_flush_tlb_global,
  232. .flush_tlb_single = native_flush_tlb_single,
  233. .map_pt_hook = (void *)native_nop,
  234. .alloc_pt = (void *)native_nop,
  235. .alloc_pd = (void *)native_nop,
  236. .alloc_pd_clone = (void *)native_nop,
  237. .release_pt = (void *)native_nop,
  238. .release_pd = (void *)native_nop,
  239. .set_pte = native_set_pte,
  240. .set_pte_at = native_set_pte_at,
  241. .set_pmd = native_set_pmd,
  242. .pte_update = (void *)native_nop,
  243. .pte_update_defer = (void *)native_nop,
  244. #ifdef CONFIG_X86_PAE
  245. .set_pte_atomic = native_set_pte_atomic,
  246. .set_pte_present = native_set_pte_present,
  247. .set_pud = native_set_pud,
  248. .pte_clear = native_pte_clear,
  249. .pmd_clear = native_pmd_clear,
  250. #endif
  251. .irq_enable_sysexit = native_irq_enable_sysexit,
  252. .iret = native_iret,
  253. .startup_ipi_hook = (void *)native_nop,
  254. };
  255. /*
  256. * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops
  257. * semantics are subject to change. Hence we only do this
  258. * internal-only export of this, until it gets sorted out and
  259. * all lowlevel CPU ops used by modules are separately exported.
  260. */
  261. EXPORT_SYMBOL_GPL(paravirt_ops);