paravirt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. static unsigned long native_get_debugreg(int regno)
  82. {
  83. unsigned long val = 0; /* Damn you, gcc! */
  84. switch (regno) {
  85. case 0:
  86. asm("movl %%db0, %0" :"=r" (val)); break;
  87. case 1:
  88. asm("movl %%db1, %0" :"=r" (val)); break;
  89. case 2:
  90. asm("movl %%db2, %0" :"=r" (val)); break;
  91. case 3:
  92. asm("movl %%db3, %0" :"=r" (val)); break;
  93. case 6:
  94. asm("movl %%db6, %0" :"=r" (val)); break;
  95. case 7:
  96. asm("movl %%db7, %0" :"=r" (val)); break;
  97. default:
  98. BUG();
  99. }
  100. return val;
  101. }
  102. static void native_set_debugreg(int regno, unsigned long value)
  103. {
  104. switch (regno) {
  105. case 0:
  106. asm("movl %0,%%db0" : /* no output */ :"r" (value));
  107. break;
  108. case 1:
  109. asm("movl %0,%%db1" : /* no output */ :"r" (value));
  110. break;
  111. case 2:
  112. asm("movl %0,%%db2" : /* no output */ :"r" (value));
  113. break;
  114. case 3:
  115. asm("movl %0,%%db3" : /* no output */ :"r" (value));
  116. break;
  117. case 6:
  118. asm("movl %0,%%db6" : /* no output */ :"r" (value));
  119. break;
  120. case 7:
  121. asm("movl %0,%%db7" : /* no output */ :"r" (value));
  122. break;
  123. default:
  124. BUG();
  125. }
  126. }
  127. void init_IRQ(void)
  128. {
  129. paravirt_ops.init_IRQ();
  130. }
  131. static void native_clts(void)
  132. {
  133. asm volatile ("clts");
  134. }
  135. static unsigned long native_read_cr0(void)
  136. {
  137. unsigned long val;
  138. asm volatile("movl %%cr0,%0\n\t" :"=r" (val));
  139. return val;
  140. }
  141. static void native_write_cr0(unsigned long val)
  142. {
  143. asm volatile("movl %0,%%cr0": :"r" (val));
  144. }
  145. static unsigned long native_read_cr2(void)
  146. {
  147. unsigned long val;
  148. asm volatile("movl %%cr2,%0\n\t" :"=r" (val));
  149. return val;
  150. }
  151. static void native_write_cr2(unsigned long val)
  152. {
  153. asm volatile("movl %0,%%cr2": :"r" (val));
  154. }
  155. static unsigned long native_read_cr3(void)
  156. {
  157. unsigned long val;
  158. asm volatile("movl %%cr3,%0\n\t" :"=r" (val));
  159. return val;
  160. }
  161. static void native_write_cr3(unsigned long val)
  162. {
  163. asm volatile("movl %0,%%cr3": :"r" (val));
  164. }
  165. static unsigned long native_read_cr4(void)
  166. {
  167. unsigned long val;
  168. asm volatile("movl %%cr4,%0\n\t" :"=r" (val));
  169. return val;
  170. }
  171. static unsigned long native_read_cr4_safe(void)
  172. {
  173. unsigned long val;
  174. /* This could fault if %cr4 does not exist */
  175. asm("1: movl %%cr4, %0 \n"
  176. "2: \n"
  177. ".section __ex_table,\"a\" \n"
  178. ".long 1b,2b \n"
  179. ".previous \n"
  180. : "=r" (val): "0" (0));
  181. return val;
  182. }
  183. static void native_write_cr4(unsigned long val)
  184. {
  185. asm volatile("movl %0,%%cr4": :"r" (val));
  186. }
  187. static unsigned long native_save_fl(void)
  188. {
  189. unsigned long f;
  190. asm volatile("pushfl ; popl %0":"=g" (f): /* no input */);
  191. return f;
  192. }
  193. static void native_restore_fl(unsigned long f)
  194. {
  195. asm volatile("pushl %0 ; popfl": /* no output */
  196. :"g" (f)
  197. :"memory", "cc");
  198. }
  199. static void native_irq_disable(void)
  200. {
  201. asm volatile("cli": : :"memory");
  202. }
  203. static void native_irq_enable(void)
  204. {
  205. asm volatile("sti": : :"memory");
  206. }
  207. static void native_safe_halt(void)
  208. {
  209. asm volatile("sti; hlt": : :"memory");
  210. }
  211. static void native_halt(void)
  212. {
  213. asm volatile("hlt": : :"memory");
  214. }
  215. static void native_wbinvd(void)
  216. {
  217. asm volatile("wbinvd": : :"memory");
  218. }
  219. static unsigned long long native_read_msr(unsigned int msr, int *err)
  220. {
  221. unsigned long long val;
  222. asm volatile("2: rdmsr ; xorl %0,%0\n"
  223. "1:\n\t"
  224. ".section .fixup,\"ax\"\n\t"
  225. "3: movl %3,%0 ; jmp 1b\n\t"
  226. ".previous\n\t"
  227. ".section __ex_table,\"a\"\n"
  228. " .align 4\n\t"
  229. " .long 2b,3b\n\t"
  230. ".previous"
  231. : "=r" (*err), "=A" (val)
  232. : "c" (msr), "i" (-EFAULT));
  233. return val;
  234. }
  235. static int native_write_msr(unsigned int msr, unsigned long long val)
  236. {
  237. int err;
  238. asm volatile("2: wrmsr ; xorl %0,%0\n"
  239. "1:\n\t"
  240. ".section .fixup,\"ax\"\n\t"
  241. "3: movl %4,%0 ; jmp 1b\n\t"
  242. ".previous\n\t"
  243. ".section __ex_table,\"a\"\n"
  244. " .align 4\n\t"
  245. " .long 2b,3b\n\t"
  246. ".previous"
  247. : "=a" (err)
  248. : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
  249. "i" (-EFAULT));
  250. return err;
  251. }
  252. static unsigned long long native_read_tsc(void)
  253. {
  254. unsigned long long val;
  255. asm volatile("rdtsc" : "=A" (val));
  256. return val;
  257. }
  258. static unsigned long long native_read_pmc(void)
  259. {
  260. unsigned long long val;
  261. asm volatile("rdpmc" : "=A" (val));
  262. return val;
  263. }
  264. static void native_load_tr_desc(void)
  265. {
  266. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  267. }
  268. static void native_load_gdt(const struct Xgt_desc_struct *dtr)
  269. {
  270. asm volatile("lgdt %0"::"m" (*dtr));
  271. }
  272. static void native_load_idt(const struct Xgt_desc_struct *dtr)
  273. {
  274. asm volatile("lidt %0"::"m" (*dtr));
  275. }
  276. static void native_store_gdt(struct Xgt_desc_struct *dtr)
  277. {
  278. asm ("sgdt %0":"=m" (*dtr));
  279. }
  280. static void native_store_idt(struct Xgt_desc_struct *dtr)
  281. {
  282. asm ("sidt %0":"=m" (*dtr));
  283. }
  284. static unsigned long native_store_tr(void)
  285. {
  286. unsigned long tr;
  287. asm ("str %0":"=r" (tr));
  288. return tr;
  289. }
  290. static void native_load_tls(struct thread_struct *t, unsigned int cpu)
  291. {
  292. #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
  293. C(0); C(1); C(2);
  294. #undef C
  295. }
  296. static inline void native_write_dt_entry(void *dt, int entry, u32 entry_low, u32 entry_high)
  297. {
  298. u32 *lp = (u32 *)((char *)dt + entry*8);
  299. lp[0] = entry_low;
  300. lp[1] = entry_high;
  301. }
  302. static void native_write_ldt_entry(void *dt, int entrynum, u32 low, u32 high)
  303. {
  304. native_write_dt_entry(dt, entrynum, low, high);
  305. }
  306. static void native_write_gdt_entry(void *dt, int entrynum, u32 low, u32 high)
  307. {
  308. native_write_dt_entry(dt, entrynum, low, high);
  309. }
  310. static void native_write_idt_entry(void *dt, int entrynum, u32 low, u32 high)
  311. {
  312. native_write_dt_entry(dt, entrynum, low, high);
  313. }
  314. static void native_load_esp0(struct tss_struct *tss,
  315. struct thread_struct *thread)
  316. {
  317. tss->esp0 = thread->esp0;
  318. /* This can only happen when SEP is enabled, no need to test "SEP"arately */
  319. if (unlikely(tss->ss1 != thread->sysenter_cs)) {
  320. tss->ss1 = thread->sysenter_cs;
  321. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  322. }
  323. }
  324. static void native_io_delay(void)
  325. {
  326. asm volatile("outb %al,$0x80");
  327. }
  328. static void native_flush_tlb(void)
  329. {
  330. __native_flush_tlb();
  331. }
  332. /*
  333. * Global pages have to be flushed a bit differently. Not a real
  334. * performance problem because this does not happen often.
  335. */
  336. static void native_flush_tlb_global(void)
  337. {
  338. __native_flush_tlb_global();
  339. }
  340. static void native_flush_tlb_single(u32 addr)
  341. {
  342. __native_flush_tlb_single(addr);
  343. }
  344. #ifndef CONFIG_X86_PAE
  345. static void native_set_pte(pte_t *ptep, pte_t pteval)
  346. {
  347. *ptep = pteval;
  348. }
  349. static void native_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
  350. {
  351. *ptep = pteval;
  352. }
  353. static void native_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  354. {
  355. *pmdp = pmdval;
  356. }
  357. #else /* CONFIG_X86_PAE */
  358. static void native_set_pte(pte_t *ptep, pte_t pte)
  359. {
  360. ptep->pte_high = pte.pte_high;
  361. smp_wmb();
  362. ptep->pte_low = pte.pte_low;
  363. }
  364. static void native_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pte)
  365. {
  366. ptep->pte_high = pte.pte_high;
  367. smp_wmb();
  368. ptep->pte_low = pte.pte_low;
  369. }
  370. static void native_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  371. {
  372. ptep->pte_low = 0;
  373. smp_wmb();
  374. ptep->pte_high = pte.pte_high;
  375. smp_wmb();
  376. ptep->pte_low = pte.pte_low;
  377. }
  378. static void native_set_pte_atomic(pte_t *ptep, pte_t pteval)
  379. {
  380. set_64bit((unsigned long long *)ptep,pte_val(pteval));
  381. }
  382. static void native_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  383. {
  384. set_64bit((unsigned long long *)pmdp,pmd_val(pmdval));
  385. }
  386. static void native_set_pud(pud_t *pudp, pud_t pudval)
  387. {
  388. *pudp = pudval;
  389. }
  390. static void native_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  391. {
  392. ptep->pte_low = 0;
  393. smp_wmb();
  394. ptep->pte_high = 0;
  395. }
  396. static void native_pmd_clear(pmd_t *pmd)
  397. {
  398. u32 *tmp = (u32 *)pmd;
  399. *tmp = 0;
  400. smp_wmb();
  401. *(tmp + 1) = 0;
  402. }
  403. #endif /* CONFIG_X86_PAE */
  404. /* These are in entry.S */
  405. extern void native_iret(void);
  406. extern void native_irq_enable_sysexit(void);
  407. static int __init print_banner(void)
  408. {
  409. paravirt_ops.banner();
  410. return 0;
  411. }
  412. core_initcall(print_banner);
  413. struct paravirt_ops paravirt_ops = {
  414. .name = "bare hardware",
  415. .paravirt_enabled = 0,
  416. .kernel_rpl = 0,
  417. .patch = native_patch,
  418. .banner = default_banner,
  419. .arch_setup = native_nop,
  420. .memory_setup = machine_specific_memory_setup,
  421. .get_wallclock = native_get_wallclock,
  422. .set_wallclock = native_set_wallclock,
  423. .time_init = time_init_hook,
  424. .init_IRQ = native_init_IRQ,
  425. .cpuid = native_cpuid,
  426. .get_debugreg = native_get_debugreg,
  427. .set_debugreg = native_set_debugreg,
  428. .clts = native_clts,
  429. .read_cr0 = native_read_cr0,
  430. .write_cr0 = native_write_cr0,
  431. .read_cr2 = native_read_cr2,
  432. .write_cr2 = native_write_cr2,
  433. .read_cr3 = native_read_cr3,
  434. .write_cr3 = native_write_cr3,
  435. .read_cr4 = native_read_cr4,
  436. .read_cr4_safe = native_read_cr4_safe,
  437. .write_cr4 = native_write_cr4,
  438. .save_fl = native_save_fl,
  439. .restore_fl = native_restore_fl,
  440. .irq_disable = native_irq_disable,
  441. .irq_enable = native_irq_enable,
  442. .safe_halt = native_safe_halt,
  443. .halt = native_halt,
  444. .wbinvd = native_wbinvd,
  445. .read_msr = native_read_msr,
  446. .write_msr = native_write_msr,
  447. .read_tsc = native_read_tsc,
  448. .read_pmc = native_read_pmc,
  449. .get_scheduled_cycles = native_read_tsc,
  450. .get_cpu_khz = native_calculate_cpu_khz,
  451. .load_tr_desc = native_load_tr_desc,
  452. .set_ldt = native_set_ldt,
  453. .load_gdt = native_load_gdt,
  454. .load_idt = native_load_idt,
  455. .store_gdt = native_store_gdt,
  456. .store_idt = native_store_idt,
  457. .store_tr = native_store_tr,
  458. .load_tls = native_load_tls,
  459. .write_ldt_entry = native_write_ldt_entry,
  460. .write_gdt_entry = native_write_gdt_entry,
  461. .write_idt_entry = native_write_idt_entry,
  462. .load_esp0 = native_load_esp0,
  463. .set_iopl_mask = native_set_iopl_mask,
  464. .io_delay = native_io_delay,
  465. .const_udelay = __const_udelay,
  466. #ifdef CONFIG_X86_LOCAL_APIC
  467. .apic_write = native_apic_write,
  468. .apic_write_atomic = native_apic_write_atomic,
  469. .apic_read = native_apic_read,
  470. .setup_boot_clock = setup_boot_APIC_clock,
  471. .setup_secondary_clock = setup_secondary_APIC_clock,
  472. #endif
  473. .set_lazy_mode = (void *)native_nop,
  474. .flush_tlb_user = native_flush_tlb,
  475. .flush_tlb_kernel = native_flush_tlb_global,
  476. .flush_tlb_single = native_flush_tlb_single,
  477. .map_pt_hook = (void *)native_nop,
  478. .alloc_pt = (void *)native_nop,
  479. .alloc_pd = (void *)native_nop,
  480. .alloc_pd_clone = (void *)native_nop,
  481. .release_pt = (void *)native_nop,
  482. .release_pd = (void *)native_nop,
  483. .set_pte = native_set_pte,
  484. .set_pte_at = native_set_pte_at,
  485. .set_pmd = native_set_pmd,
  486. .pte_update = (void *)native_nop,
  487. .pte_update_defer = (void *)native_nop,
  488. #ifdef CONFIG_X86_PAE
  489. .set_pte_atomic = native_set_pte_atomic,
  490. .set_pte_present = native_set_pte_present,
  491. .set_pud = native_set_pud,
  492. .pte_clear = native_pte_clear,
  493. .pmd_clear = native_pmd_clear,
  494. #endif
  495. .irq_enable_sysexit = native_irq_enable_sysexit,
  496. .iret = native_iret,
  497. .startup_ipi_hook = (void *)native_nop,
  498. };
  499. /*
  500. * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops
  501. * semantics are subject to change. Hence we only do this
  502. * internal-only export of this, until it gets sorted out and
  503. * all lowlevel CPU ops used by modules are separately exported.
  504. */
  505. EXPORT_SYMBOL_GPL(paravirt_ops);