paravirt_32.c 9.7 KB

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