paravirt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 *set_pte)(pte_t *ptep, pte_t pteval);
  107. void (fastcall *set_pte_at)(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval);
  108. void (fastcall *set_pmd)(pmd_t *pmdp, pmd_t pmdval);
  109. void (fastcall *pte_update)(struct mm_struct *mm, u32 addr, pte_t *ptep);
  110. void (fastcall *pte_update_defer)(struct mm_struct *mm, u32 addr, pte_t *ptep);
  111. #ifdef CONFIG_X86_PAE
  112. void (fastcall *set_pte_atomic)(pte_t *ptep, pte_t pteval);
  113. void (fastcall *set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
  114. void (fastcall *set_pud)(pud_t *pudp, pud_t pudval);
  115. void (fastcall *pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  116. void (fastcall *pmd_clear)(pmd_t *pmdp);
  117. #endif
  118. /* These two are jmp to, not actually called. */
  119. void (fastcall *irq_enable_sysexit)(void);
  120. void (fastcall *iret)(void);
  121. };
  122. /* Mark a paravirt probe function. */
  123. #define paravirt_probe(fn) \
  124. static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
  125. __attribute__((__section__(".paravirtprobe"))) = fn
  126. extern struct paravirt_ops paravirt_ops;
  127. #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
  128. static inline void load_esp0(struct tss_struct *tss,
  129. struct thread_struct *thread)
  130. {
  131. paravirt_ops.load_esp0(tss, thread);
  132. }
  133. #define ARCH_SETUP paravirt_ops.arch_setup();
  134. static inline unsigned long get_wallclock(void)
  135. {
  136. return paravirt_ops.get_wallclock();
  137. }
  138. static inline int set_wallclock(unsigned long nowtime)
  139. {
  140. return paravirt_ops.set_wallclock(nowtime);
  141. }
  142. static inline void do_time_init(void)
  143. {
  144. return paravirt_ops.time_init();
  145. }
  146. /* The paravirtualized CPUID instruction. */
  147. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  148. unsigned int *ecx, unsigned int *edx)
  149. {
  150. paravirt_ops.cpuid(eax, ebx, ecx, edx);
  151. }
  152. /*
  153. * These special macros can be used to get or set a debugging register
  154. */
  155. #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
  156. #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
  157. #define clts() paravirt_ops.clts()
  158. #define read_cr0() paravirt_ops.read_cr0()
  159. #define write_cr0(x) paravirt_ops.write_cr0(x)
  160. #define read_cr2() paravirt_ops.read_cr2()
  161. #define write_cr2(x) paravirt_ops.write_cr2(x)
  162. #define read_cr3() paravirt_ops.read_cr3()
  163. #define write_cr3(x) paravirt_ops.write_cr3(x)
  164. #define read_cr4() paravirt_ops.read_cr4()
  165. #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
  166. #define write_cr4(x) paravirt_ops.write_cr4(x)
  167. static inline void raw_safe_halt(void)
  168. {
  169. paravirt_ops.safe_halt();
  170. }
  171. static inline void halt(void)
  172. {
  173. paravirt_ops.safe_halt();
  174. }
  175. #define wbinvd() paravirt_ops.wbinvd()
  176. #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
  177. #define rdmsr(msr,val1,val2) do { \
  178. int _err; \
  179. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  180. val1 = (u32)_l; \
  181. val2 = _l >> 32; \
  182. } while(0)
  183. #define wrmsr(msr,val1,val2) do { \
  184. u64 _l = ((u64)(val2) << 32) | (val1); \
  185. paravirt_ops.write_msr((msr), _l); \
  186. } while(0)
  187. #define rdmsrl(msr,val) do { \
  188. int _err; \
  189. val = paravirt_ops.read_msr((msr),&_err); \
  190. } while(0)
  191. #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
  192. #define wrmsr_safe(msr,a,b) ({ \
  193. u64 _l = ((u64)(b) << 32) | (a); \
  194. paravirt_ops.write_msr((msr),_l); \
  195. })
  196. /* rdmsr with exception handling */
  197. #define rdmsr_safe(msr,a,b) ({ \
  198. int _err; \
  199. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  200. (*a) = (u32)_l; \
  201. (*b) = _l >> 32; \
  202. _err; })
  203. #define rdtsc(low,high) do { \
  204. u64 _l = paravirt_ops.read_tsc(); \
  205. low = (u32)_l; \
  206. high = _l >> 32; \
  207. } while(0)
  208. #define rdtscl(low) do { \
  209. u64 _l = paravirt_ops.read_tsc(); \
  210. low = (int)_l; \
  211. } while(0)
  212. #define rdtscll(val) (val = paravirt_ops.read_tsc())
  213. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  214. #define rdpmc(counter,low,high) do { \
  215. u64 _l = paravirt_ops.read_pmc(); \
  216. low = (u32)_l; \
  217. high = _l >> 32; \
  218. } while(0)
  219. #define load_TR_desc() (paravirt_ops.load_tr_desc())
  220. #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
  221. #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
  222. #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
  223. #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
  224. #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
  225. #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
  226. #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
  227. #define write_ldt_entry(dt, entry, low, high) \
  228. (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
  229. #define write_gdt_entry(dt, entry, low, high) \
  230. (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
  231. #define write_idt_entry(dt, entry, low, high) \
  232. (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
  233. #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
  234. /* The paravirtualized I/O functions */
  235. static inline void slow_down_io(void) {
  236. paravirt_ops.io_delay();
  237. #ifdef REALLY_SLOW_IO
  238. paravirt_ops.io_delay();
  239. paravirt_ops.io_delay();
  240. paravirt_ops.io_delay();
  241. #endif
  242. }
  243. #ifdef CONFIG_X86_LOCAL_APIC
  244. /*
  245. * Basic functions accessing APICs.
  246. */
  247. static inline void apic_write(unsigned long reg, unsigned long v)
  248. {
  249. paravirt_ops.apic_write(reg,v);
  250. }
  251. static inline void apic_write_atomic(unsigned long reg, unsigned long v)
  252. {
  253. paravirt_ops.apic_write_atomic(reg,v);
  254. }
  255. static inline unsigned long apic_read(unsigned long reg)
  256. {
  257. return paravirt_ops.apic_read(reg);
  258. }
  259. #endif
  260. #define __flush_tlb() paravirt_ops.flush_tlb_user()
  261. #define __flush_tlb_global() paravirt_ops.flush_tlb_kernel()
  262. #define __flush_tlb_single(addr) paravirt_ops.flush_tlb_single(addr)
  263. static inline void set_pte(pte_t *ptep, pte_t pteval)
  264. {
  265. paravirt_ops.set_pte(ptep, pteval);
  266. }
  267. static inline void set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
  268. {
  269. paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
  270. }
  271. static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
  272. {
  273. paravirt_ops.set_pmd(pmdp, pmdval);
  274. }
  275. static inline void pte_update(struct mm_struct *mm, u32 addr, pte_t *ptep)
  276. {
  277. paravirt_ops.pte_update(mm, addr, ptep);
  278. }
  279. static inline void pte_update_defer(struct mm_struct *mm, u32 addr, pte_t *ptep)
  280. {
  281. paravirt_ops.pte_update_defer(mm, addr, ptep);
  282. }
  283. #ifdef CONFIG_X86_PAE
  284. static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
  285. {
  286. paravirt_ops.set_pte_atomic(ptep, pteval);
  287. }
  288. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  289. {
  290. paravirt_ops.set_pte_present(mm, addr, ptep, pte);
  291. }
  292. static inline void set_pud(pud_t *pudp, pud_t pudval)
  293. {
  294. paravirt_ops.set_pud(pudp, pudval);
  295. }
  296. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  297. {
  298. paravirt_ops.pte_clear(mm, addr, ptep);
  299. }
  300. static inline void pmd_clear(pmd_t *pmdp)
  301. {
  302. paravirt_ops.pmd_clear(pmdp);
  303. }
  304. #endif
  305. /* These all sit in the .parainstructions section to tell us what to patch. */
  306. struct paravirt_patch {
  307. u8 *instr; /* original instructions */
  308. u8 instrtype; /* type of this instruction */
  309. u8 len; /* length of original instruction */
  310. u16 clobbers; /* what registers you may clobber */
  311. };
  312. #define paravirt_alt(insn_string, typenum, clobber) \
  313. "771:\n\t" insn_string "\n" "772:\n" \
  314. ".pushsection .parainstructions,\"a\"\n" \
  315. " .long 771b\n" \
  316. " .byte " __stringify(typenum) "\n" \
  317. " .byte 772b-771b\n" \
  318. " .short " __stringify(clobber) "\n" \
  319. ".popsection"
  320. static inline unsigned long __raw_local_save_flags(void)
  321. {
  322. unsigned long f;
  323. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  324. "call *%1;"
  325. "popl %%edx; popl %%ecx",
  326. PARAVIRT_SAVE_FLAGS, CLBR_NONE)
  327. : "=a"(f): "m"(paravirt_ops.save_fl)
  328. : "memory", "cc");
  329. return f;
  330. }
  331. static inline void raw_local_irq_restore(unsigned long f)
  332. {
  333. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  334. "call *%1;"
  335. "popl %%edx; popl %%ecx",
  336. PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
  337. : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
  338. : "memory", "cc");
  339. }
  340. static inline void raw_local_irq_disable(void)
  341. {
  342. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  343. "call *%0;"
  344. "popl %%edx; popl %%ecx",
  345. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  346. : : "m" (paravirt_ops.irq_disable)
  347. : "memory", "eax", "cc");
  348. }
  349. static inline void raw_local_irq_enable(void)
  350. {
  351. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  352. "call *%0;"
  353. "popl %%edx; popl %%ecx",
  354. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  355. : : "m" (paravirt_ops.irq_enable)
  356. : "memory", "eax", "cc");
  357. }
  358. static inline unsigned long __raw_local_irq_save(void)
  359. {
  360. unsigned long f;
  361. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  362. "call *%1; pushl %%eax;"
  363. "call *%2; popl %%eax;"
  364. "popl %%edx; popl %%ecx",
  365. PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
  366. CLBR_NONE)
  367. : "=a"(f)
  368. : "m" (paravirt_ops.save_fl),
  369. "m" (paravirt_ops.irq_disable)
  370. : "memory", "cc");
  371. return f;
  372. }
  373. #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  374. "call *paravirt_ops+%c[irq_disable];" \
  375. "popl %%edx; popl %%ecx", \
  376. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  377. #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  378. "call *paravirt_ops+%c[irq_enable];" \
  379. "popl %%edx; popl %%ecx", \
  380. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  381. #define CLI_STI_CLOBBERS , "%eax"
  382. #define CLI_STI_INPUT_ARGS \
  383. , \
  384. [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
  385. [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
  386. #else /* __ASSEMBLY__ */
  387. #define PARA_PATCH(ptype, clobbers, ops) \
  388. 771:; \
  389. ops; \
  390. 772:; \
  391. .pushsection .parainstructions,"a"; \
  392. .long 771b; \
  393. .byte ptype; \
  394. .byte 772b-771b; \
  395. .short clobbers; \
  396. .popsection
  397. #define INTERRUPT_RETURN \
  398. PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
  399. jmp *%cs:paravirt_ops+PARAVIRT_iret)
  400. #define DISABLE_INTERRUPTS(clobbers) \
  401. PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
  402. pushl %ecx; pushl %edx; \
  403. call *paravirt_ops+PARAVIRT_irq_disable; \
  404. popl %edx; popl %ecx) \
  405. #define ENABLE_INTERRUPTS(clobbers) \
  406. PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
  407. pushl %ecx; pushl %edx; \
  408. call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
  409. popl %edx; popl %ecx)
  410. #define ENABLE_INTERRUPTS_SYSEXIT \
  411. PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
  412. jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
  413. #define GET_CR0_INTO_EAX \
  414. call *paravirt_ops+PARAVIRT_read_cr0
  415. #endif /* __ASSEMBLY__ */
  416. #endif /* CONFIG_PARAVIRT */
  417. #endif /* __ASM_PARAVIRT_H */