paravirt_32.c 12 KB

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