paravirt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #ifndef __ASM_PARAVIRT_H
  2. #define __ASM_PARAVIRT_H
  3. /* Various instructions on x86 need to be replaced for
  4. * para-virtualization: those hooks are defined here. */
  5. #include <linux/linkage.h>
  6. #include <linux/stringify.h>
  7. #include <asm/page.h>
  8. #ifdef CONFIG_PARAVIRT
  9. /* These are the most performance critical ops, so we want to be able to patch
  10. * callers */
  11. #define PARAVIRT_IRQ_DISABLE 0
  12. #define PARAVIRT_IRQ_ENABLE 1
  13. #define PARAVIRT_RESTORE_FLAGS 2
  14. #define PARAVIRT_SAVE_FLAGS 3
  15. #define PARAVIRT_SAVE_FLAGS_IRQ_DISABLE 4
  16. #define PARAVIRT_INTERRUPT_RETURN 5
  17. #define PARAVIRT_STI_SYSEXIT 6
  18. /* Bitmask of what can be clobbered: usually at least eax. */
  19. #define CLBR_NONE 0x0
  20. #define CLBR_EAX 0x1
  21. #define CLBR_ECX 0x2
  22. #define CLBR_EDX 0x4
  23. #define CLBR_ANY 0x7
  24. #ifndef __ASSEMBLY__
  25. struct thread_struct;
  26. struct Xgt_desc_struct;
  27. struct tss_struct;
  28. struct mm_struct;
  29. struct paravirt_ops
  30. {
  31. unsigned int kernel_rpl;
  32. int paravirt_enabled;
  33. const char *name;
  34. /*
  35. * Patch may replace one of the defined code sequences with arbitrary
  36. * code, subject to the same register constraints. This generally
  37. * means the code is not free to clobber any registers other than EAX.
  38. * The patch function should return the number of bytes of code
  39. * generated, as we nop pad the rest in generic code.
  40. */
  41. unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
  42. void (*arch_setup)(void);
  43. char *(*memory_setup)(void);
  44. void (*init_IRQ)(void);
  45. void (*banner)(void);
  46. unsigned long (*get_wallclock)(void);
  47. int (*set_wallclock)(unsigned long);
  48. void (*time_init)(void);
  49. /* All the function pointers here are declared as "fastcall"
  50. so that we get a specific register-based calling
  51. convention. This makes it easier to implement inline
  52. assembler replacements. */
  53. void (fastcall *cpuid)(unsigned int *eax, unsigned int *ebx,
  54. unsigned int *ecx, unsigned int *edx);
  55. unsigned long (fastcall *get_debugreg)(int regno);
  56. void (fastcall *set_debugreg)(int regno, unsigned long value);
  57. void (fastcall *clts)(void);
  58. unsigned long (fastcall *read_cr0)(void);
  59. void (fastcall *write_cr0)(unsigned long);
  60. unsigned long (fastcall *read_cr2)(void);
  61. void (fastcall *write_cr2)(unsigned long);
  62. unsigned long (fastcall *read_cr3)(void);
  63. void (fastcall *write_cr3)(unsigned long);
  64. unsigned long (fastcall *read_cr4_safe)(void);
  65. unsigned long (fastcall *read_cr4)(void);
  66. void (fastcall *write_cr4)(unsigned long);
  67. unsigned long (fastcall *save_fl)(void);
  68. void (fastcall *restore_fl)(unsigned long);
  69. void (fastcall *irq_disable)(void);
  70. void (fastcall *irq_enable)(void);
  71. void (fastcall *safe_halt)(void);
  72. void (fastcall *halt)(void);
  73. void (fastcall *wbinvd)(void);
  74. /* err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
  75. u64 (fastcall *read_msr)(unsigned int msr, int *err);
  76. int (fastcall *write_msr)(unsigned int msr, u64 val);
  77. u64 (fastcall *read_tsc)(void);
  78. u64 (fastcall *read_pmc)(void);
  79. void (fastcall *load_tr_desc)(void);
  80. void (fastcall *load_gdt)(const struct Xgt_desc_struct *);
  81. void (fastcall *load_idt)(const struct Xgt_desc_struct *);
  82. void (fastcall *store_gdt)(struct Xgt_desc_struct *);
  83. void (fastcall *store_idt)(struct Xgt_desc_struct *);
  84. void (fastcall *set_ldt)(const void *desc, unsigned entries);
  85. unsigned long (fastcall *store_tr)(void);
  86. void (fastcall *load_tls)(struct thread_struct *t, unsigned int cpu);
  87. void (fastcall *write_ldt_entry)(void *dt, int entrynum,
  88. u32 low, u32 high);
  89. void (fastcall *write_gdt_entry)(void *dt, int entrynum,
  90. u32 low, u32 high);
  91. void (fastcall *write_idt_entry)(void *dt, int entrynum,
  92. u32 low, u32 high);
  93. void (fastcall *load_esp0)(struct tss_struct *tss,
  94. struct thread_struct *thread);
  95. void (fastcall *set_iopl_mask)(unsigned mask);
  96. void (fastcall *io_delay)(void);
  97. void (*const_udelay)(unsigned long loops);
  98. #ifdef CONFIG_X86_LOCAL_APIC
  99. void (fastcall *apic_write)(unsigned long reg, unsigned long v);
  100. void (fastcall *apic_write_atomic)(unsigned long reg, unsigned long v);
  101. unsigned long (fastcall *apic_read)(unsigned long reg);
  102. #endif
  103. void (fastcall *flush_tlb_user)(void);
  104. void (fastcall *flush_tlb_kernel)(void);
  105. void (fastcall *flush_tlb_single)(u32 addr);
  106. void (fastcall *alloc_pt)(u32 pfn);
  107. void (fastcall *alloc_pd)(u32 pfn);
  108. void (fastcall *alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
  109. void (fastcall *release_pt)(u32 pfn);
  110. void (fastcall *release_pd)(u32 pfn);
  111. void (fastcall *set_pte)(pte_t *ptep, pte_t pteval);
  112. void (fastcall *set_pte_at)(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval);
  113. void (fastcall *set_pmd)(pmd_t *pmdp, pmd_t pmdval);
  114. void (fastcall *pte_update)(struct mm_struct *mm, u32 addr, pte_t *ptep);
  115. void (fastcall *pte_update_defer)(struct mm_struct *mm, u32 addr, pte_t *ptep);
  116. #ifdef CONFIG_X86_PAE
  117. void (fastcall *set_pte_atomic)(pte_t *ptep, pte_t pteval);
  118. void (fastcall *set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
  119. void (fastcall *set_pud)(pud_t *pudp, pud_t pudval);
  120. void (fastcall *pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  121. void (fastcall *pmd_clear)(pmd_t *pmdp);
  122. #endif
  123. void (fastcall *set_lazy_mode)(int mode);
  124. /* These two are jmp to, not actually called. */
  125. void (fastcall *irq_enable_sysexit)(void);
  126. void (fastcall *iret)(void);
  127. };
  128. /* Mark a paravirt probe function. */
  129. #define paravirt_probe(fn) \
  130. static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
  131. __attribute__((__section__(".paravirtprobe"))) = fn
  132. extern struct paravirt_ops paravirt_ops;
  133. #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
  134. static inline void load_esp0(struct tss_struct *tss,
  135. struct thread_struct *thread)
  136. {
  137. paravirt_ops.load_esp0(tss, thread);
  138. }
  139. #define ARCH_SETUP paravirt_ops.arch_setup();
  140. static inline unsigned long get_wallclock(void)
  141. {
  142. return paravirt_ops.get_wallclock();
  143. }
  144. static inline int set_wallclock(unsigned long nowtime)
  145. {
  146. return paravirt_ops.set_wallclock(nowtime);
  147. }
  148. static inline void do_time_init(void)
  149. {
  150. return paravirt_ops.time_init();
  151. }
  152. /* The paravirtualized CPUID instruction. */
  153. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  154. unsigned int *ecx, unsigned int *edx)
  155. {
  156. paravirt_ops.cpuid(eax, ebx, ecx, edx);
  157. }
  158. /*
  159. * These special macros can be used to get or set a debugging register
  160. */
  161. #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
  162. #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
  163. #define clts() paravirt_ops.clts()
  164. #define read_cr0() paravirt_ops.read_cr0()
  165. #define write_cr0(x) paravirt_ops.write_cr0(x)
  166. #define read_cr2() paravirt_ops.read_cr2()
  167. #define write_cr2(x) paravirt_ops.write_cr2(x)
  168. #define read_cr3() paravirt_ops.read_cr3()
  169. #define write_cr3(x) paravirt_ops.write_cr3(x)
  170. #define read_cr4() paravirt_ops.read_cr4()
  171. #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
  172. #define write_cr4(x) paravirt_ops.write_cr4(x)
  173. static inline void raw_safe_halt(void)
  174. {
  175. paravirt_ops.safe_halt();
  176. }
  177. static inline void halt(void)
  178. {
  179. paravirt_ops.safe_halt();
  180. }
  181. #define wbinvd() paravirt_ops.wbinvd()
  182. #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
  183. #define rdmsr(msr,val1,val2) do { \
  184. int _err; \
  185. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  186. val1 = (u32)_l; \
  187. val2 = _l >> 32; \
  188. } while(0)
  189. #define wrmsr(msr,val1,val2) do { \
  190. u64 _l = ((u64)(val2) << 32) | (val1); \
  191. paravirt_ops.write_msr((msr), _l); \
  192. } while(0)
  193. #define rdmsrl(msr,val) do { \
  194. int _err; \
  195. val = paravirt_ops.read_msr((msr),&_err); \
  196. } while(0)
  197. #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
  198. #define wrmsr_safe(msr,a,b) ({ \
  199. u64 _l = ((u64)(b) << 32) | (a); \
  200. paravirt_ops.write_msr((msr),_l); \
  201. })
  202. /* rdmsr with exception handling */
  203. #define rdmsr_safe(msr,a,b) ({ \
  204. int _err; \
  205. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  206. (*a) = (u32)_l; \
  207. (*b) = _l >> 32; \
  208. _err; })
  209. #define rdtsc(low,high) do { \
  210. u64 _l = paravirt_ops.read_tsc(); \
  211. low = (u32)_l; \
  212. high = _l >> 32; \
  213. } while(0)
  214. #define rdtscl(low) do { \
  215. u64 _l = paravirt_ops.read_tsc(); \
  216. low = (int)_l; \
  217. } while(0)
  218. #define rdtscll(val) (val = paravirt_ops.read_tsc())
  219. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  220. #define rdpmc(counter,low,high) do { \
  221. u64 _l = paravirt_ops.read_pmc(); \
  222. low = (u32)_l; \
  223. high = _l >> 32; \
  224. } while(0)
  225. #define load_TR_desc() (paravirt_ops.load_tr_desc())
  226. #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
  227. #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
  228. #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
  229. #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
  230. #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
  231. #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
  232. #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
  233. #define write_ldt_entry(dt, entry, low, high) \
  234. (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
  235. #define write_gdt_entry(dt, entry, low, high) \
  236. (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
  237. #define write_idt_entry(dt, entry, low, high) \
  238. (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
  239. #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
  240. /* The paravirtualized I/O functions */
  241. static inline void slow_down_io(void) {
  242. paravirt_ops.io_delay();
  243. #ifdef REALLY_SLOW_IO
  244. paravirt_ops.io_delay();
  245. paravirt_ops.io_delay();
  246. paravirt_ops.io_delay();
  247. #endif
  248. }
  249. #ifdef CONFIG_X86_LOCAL_APIC
  250. /*
  251. * Basic functions accessing APICs.
  252. */
  253. static inline void apic_write(unsigned long reg, unsigned long v)
  254. {
  255. paravirt_ops.apic_write(reg,v);
  256. }
  257. static inline void apic_write_atomic(unsigned long reg, unsigned long v)
  258. {
  259. paravirt_ops.apic_write_atomic(reg,v);
  260. }
  261. static inline unsigned long apic_read(unsigned long reg)
  262. {
  263. return paravirt_ops.apic_read(reg);
  264. }
  265. #endif
  266. #define __flush_tlb() paravirt_ops.flush_tlb_user()
  267. #define __flush_tlb_global() paravirt_ops.flush_tlb_kernel()
  268. #define __flush_tlb_single(addr) paravirt_ops.flush_tlb_single(addr)
  269. #define paravirt_alloc_pt(pfn) paravirt_ops.alloc_pt(pfn)
  270. #define paravirt_release_pt(pfn) paravirt_ops.release_pt(pfn)
  271. #define paravirt_alloc_pd(pfn) paravirt_ops.alloc_pd(pfn)
  272. #define paravirt_alloc_pd_clone(pfn, clonepfn, start, count) \
  273. paravirt_ops.alloc_pd_clone(pfn, clonepfn, start, count)
  274. #define paravirt_release_pd(pfn) paravirt_ops.release_pd(pfn)
  275. static inline void set_pte(pte_t *ptep, pte_t pteval)
  276. {
  277. paravirt_ops.set_pte(ptep, pteval);
  278. }
  279. static inline void set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
  280. {
  281. paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
  282. }
  283. static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
  284. {
  285. paravirt_ops.set_pmd(pmdp, pmdval);
  286. }
  287. static inline void pte_update(struct mm_struct *mm, u32 addr, pte_t *ptep)
  288. {
  289. paravirt_ops.pte_update(mm, addr, ptep);
  290. }
  291. static inline void pte_update_defer(struct mm_struct *mm, u32 addr, pte_t *ptep)
  292. {
  293. paravirt_ops.pte_update_defer(mm, addr, ptep);
  294. }
  295. #ifdef CONFIG_X86_PAE
  296. static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
  297. {
  298. paravirt_ops.set_pte_atomic(ptep, pteval);
  299. }
  300. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  301. {
  302. paravirt_ops.set_pte_present(mm, addr, ptep, pte);
  303. }
  304. static inline void set_pud(pud_t *pudp, pud_t pudval)
  305. {
  306. paravirt_ops.set_pud(pudp, pudval);
  307. }
  308. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  309. {
  310. paravirt_ops.pte_clear(mm, addr, ptep);
  311. }
  312. static inline void pmd_clear(pmd_t *pmdp)
  313. {
  314. paravirt_ops.pmd_clear(pmdp);
  315. }
  316. #endif
  317. /* Lazy mode for batching updates / context switch */
  318. #define PARAVIRT_LAZY_NONE 0
  319. #define PARAVIRT_LAZY_MMU 1
  320. #define PARAVIRT_LAZY_CPU 2
  321. #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
  322. #define arch_enter_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_CPU)
  323. #define arch_leave_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
  324. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  325. #define arch_enter_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_MMU)
  326. #define arch_leave_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
  327. /* These all sit in the .parainstructions section to tell us what to patch. */
  328. struct paravirt_patch {
  329. u8 *instr; /* original instructions */
  330. u8 instrtype; /* type of this instruction */
  331. u8 len; /* length of original instruction */
  332. u16 clobbers; /* what registers you may clobber */
  333. };
  334. #define paravirt_alt(insn_string, typenum, clobber) \
  335. "771:\n\t" insn_string "\n" "772:\n" \
  336. ".pushsection .parainstructions,\"a\"\n" \
  337. " .long 771b\n" \
  338. " .byte " __stringify(typenum) "\n" \
  339. " .byte 772b-771b\n" \
  340. " .short " __stringify(clobber) "\n" \
  341. ".popsection"
  342. static inline unsigned long __raw_local_save_flags(void)
  343. {
  344. unsigned long f;
  345. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  346. "call *%1;"
  347. "popl %%edx; popl %%ecx",
  348. PARAVIRT_SAVE_FLAGS, CLBR_NONE)
  349. : "=a"(f): "m"(paravirt_ops.save_fl)
  350. : "memory", "cc");
  351. return f;
  352. }
  353. static inline void raw_local_irq_restore(unsigned long f)
  354. {
  355. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  356. "call *%1;"
  357. "popl %%edx; popl %%ecx",
  358. PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
  359. : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
  360. : "memory", "cc");
  361. }
  362. static inline void raw_local_irq_disable(void)
  363. {
  364. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  365. "call *%0;"
  366. "popl %%edx; popl %%ecx",
  367. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  368. : : "m" (paravirt_ops.irq_disable)
  369. : "memory", "eax", "cc");
  370. }
  371. static inline void raw_local_irq_enable(void)
  372. {
  373. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  374. "call *%0;"
  375. "popl %%edx; popl %%ecx",
  376. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  377. : : "m" (paravirt_ops.irq_enable)
  378. : "memory", "eax", "cc");
  379. }
  380. static inline unsigned long __raw_local_irq_save(void)
  381. {
  382. unsigned long f;
  383. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  384. "call *%1; pushl %%eax;"
  385. "call *%2; popl %%eax;"
  386. "popl %%edx; popl %%ecx",
  387. PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
  388. CLBR_NONE)
  389. : "=a"(f)
  390. : "m" (paravirt_ops.save_fl),
  391. "m" (paravirt_ops.irq_disable)
  392. : "memory", "cc");
  393. return f;
  394. }
  395. #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  396. "call *paravirt_ops+%c[irq_disable];" \
  397. "popl %%edx; popl %%ecx", \
  398. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  399. #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  400. "call *paravirt_ops+%c[irq_enable];" \
  401. "popl %%edx; popl %%ecx", \
  402. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  403. #define CLI_STI_CLOBBERS , "%eax"
  404. #define CLI_STI_INPUT_ARGS \
  405. , \
  406. [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
  407. [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
  408. #else /* __ASSEMBLY__ */
  409. #define PARA_PATCH(ptype, clobbers, ops) \
  410. 771:; \
  411. ops; \
  412. 772:; \
  413. .pushsection .parainstructions,"a"; \
  414. .long 771b; \
  415. .byte ptype; \
  416. .byte 772b-771b; \
  417. .short clobbers; \
  418. .popsection
  419. #define INTERRUPT_RETURN \
  420. PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
  421. jmp *%cs:paravirt_ops+PARAVIRT_iret)
  422. #define DISABLE_INTERRUPTS(clobbers) \
  423. PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
  424. pushl %ecx; pushl %edx; \
  425. call *paravirt_ops+PARAVIRT_irq_disable; \
  426. popl %edx; popl %ecx) \
  427. #define ENABLE_INTERRUPTS(clobbers) \
  428. PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
  429. pushl %ecx; pushl %edx; \
  430. call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
  431. popl %edx; popl %ecx)
  432. #define ENABLE_INTERRUPTS_SYSEXIT \
  433. PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
  434. jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
  435. #define GET_CR0_INTO_EAX \
  436. call *paravirt_ops+PARAVIRT_read_cr0
  437. #endif /* __ASSEMBLY__ */
  438. #endif /* CONFIG_PARAVIRT */
  439. #endif /* __ASM_PARAVIRT_H */