paravirt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #ifdef CONFIG_PARAVIRT
  8. /* These are the most performance critical ops, so we want to be able to patch
  9. * callers */
  10. #define PARAVIRT_IRQ_DISABLE 0
  11. #define PARAVIRT_IRQ_ENABLE 1
  12. #define PARAVIRT_RESTORE_FLAGS 2
  13. #define PARAVIRT_SAVE_FLAGS 3
  14. #define PARAVIRT_SAVE_FLAGS_IRQ_DISABLE 4
  15. #define PARAVIRT_INTERRUPT_RETURN 5
  16. #define PARAVIRT_STI_SYSEXIT 6
  17. /* Bitmask of what can be clobbered: usually at least eax. */
  18. #define CLBR_NONE 0x0
  19. #define CLBR_EAX 0x1
  20. #define CLBR_ECX 0x2
  21. #define CLBR_EDX 0x4
  22. #define CLBR_ANY 0x7
  23. #ifndef __ASSEMBLY__
  24. struct thread_struct;
  25. struct Xgt_desc_struct;
  26. struct tss_struct;
  27. struct paravirt_ops
  28. {
  29. unsigned int kernel_rpl;
  30. int paravirt_enabled;
  31. const char *name;
  32. /*
  33. * Patch may replace one of the defined code sequences with arbitrary
  34. * code, subject to the same register constraints. This generally
  35. * means the code is not free to clobber any registers other than EAX.
  36. * The patch function should return the number of bytes of code
  37. * generated, as we nop pad the rest in generic code.
  38. */
  39. unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
  40. void (*arch_setup)(void);
  41. char *(*memory_setup)(void);
  42. void (*init_IRQ)(void);
  43. void (*banner)(void);
  44. unsigned long (*get_wallclock)(void);
  45. int (*set_wallclock)(unsigned long);
  46. void (*time_init)(void);
  47. /* All the function pointers here are declared as "fastcall"
  48. so that we get a specific register-based calling
  49. convention. This makes it easier to implement inline
  50. assembler replacements. */
  51. void (fastcall *cpuid)(unsigned int *eax, unsigned int *ebx,
  52. unsigned int *ecx, unsigned int *edx);
  53. unsigned long (fastcall *get_debugreg)(int regno);
  54. void (fastcall *set_debugreg)(int regno, unsigned long value);
  55. void (fastcall *clts)(void);
  56. unsigned long (fastcall *read_cr0)(void);
  57. void (fastcall *write_cr0)(unsigned long);
  58. unsigned long (fastcall *read_cr2)(void);
  59. void (fastcall *write_cr2)(unsigned long);
  60. unsigned long (fastcall *read_cr3)(void);
  61. void (fastcall *write_cr3)(unsigned long);
  62. unsigned long (fastcall *read_cr4_safe)(void);
  63. unsigned long (fastcall *read_cr4)(void);
  64. void (fastcall *write_cr4)(unsigned long);
  65. unsigned long (fastcall *save_fl)(void);
  66. void (fastcall *restore_fl)(unsigned long);
  67. void (fastcall *irq_disable)(void);
  68. void (fastcall *irq_enable)(void);
  69. void (fastcall *safe_halt)(void);
  70. void (fastcall *halt)(void);
  71. void (fastcall *wbinvd)(void);
  72. /* err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
  73. u64 (fastcall *read_msr)(unsigned int msr, int *err);
  74. int (fastcall *write_msr)(unsigned int msr, u64 val);
  75. u64 (fastcall *read_tsc)(void);
  76. u64 (fastcall *read_pmc)(void);
  77. void (fastcall *load_tr_desc)(void);
  78. void (fastcall *load_gdt)(const struct Xgt_desc_struct *);
  79. void (fastcall *load_idt)(const struct Xgt_desc_struct *);
  80. void (fastcall *store_gdt)(struct Xgt_desc_struct *);
  81. void (fastcall *store_idt)(struct Xgt_desc_struct *);
  82. void (fastcall *set_ldt)(const void *desc, unsigned entries);
  83. unsigned long (fastcall *store_tr)(void);
  84. void (fastcall *load_tls)(struct thread_struct *t, unsigned int cpu);
  85. void (fastcall *write_ldt_entry)(void *dt, int entrynum,
  86. u32 low, u32 high);
  87. void (fastcall *write_gdt_entry)(void *dt, int entrynum,
  88. u32 low, u32 high);
  89. void (fastcall *write_idt_entry)(void *dt, int entrynum,
  90. u32 low, u32 high);
  91. void (fastcall *load_esp0)(struct tss_struct *tss,
  92. struct thread_struct *thread);
  93. void (fastcall *set_iopl_mask)(unsigned mask);
  94. void (fastcall *io_delay)(void);
  95. void (*const_udelay)(unsigned long loops);
  96. /* These two are jmp to, not actually called. */
  97. void (fastcall *irq_enable_sysexit)(void);
  98. void (fastcall *iret)(void);
  99. };
  100. extern struct paravirt_ops paravirt_ops;
  101. #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
  102. static inline void load_esp0(struct tss_struct *tss,
  103. struct thread_struct *thread)
  104. {
  105. paravirt_ops.load_esp0(tss, thread);
  106. }
  107. #define ARCH_SETUP paravirt_ops.arch_setup();
  108. static inline unsigned long get_wallclock(void)
  109. {
  110. return paravirt_ops.get_wallclock();
  111. }
  112. static inline int set_wallclock(unsigned long nowtime)
  113. {
  114. return paravirt_ops.set_wallclock(nowtime);
  115. }
  116. static inline void do_time_init(void)
  117. {
  118. return paravirt_ops.time_init();
  119. }
  120. /* The paravirtualized CPUID instruction. */
  121. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  122. unsigned int *ecx, unsigned int *edx)
  123. {
  124. paravirt_ops.cpuid(eax, ebx, ecx, edx);
  125. }
  126. /*
  127. * These special macros can be used to get or set a debugging register
  128. */
  129. #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
  130. #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
  131. #define clts() paravirt_ops.clts()
  132. #define read_cr0() paravirt_ops.read_cr0()
  133. #define write_cr0(x) paravirt_ops.write_cr0(x)
  134. #define read_cr2() paravirt_ops.read_cr2()
  135. #define write_cr2(x) paravirt_ops.write_cr2(x)
  136. #define read_cr3() paravirt_ops.read_cr3()
  137. #define write_cr3(x) paravirt_ops.write_cr3(x)
  138. #define read_cr4() paravirt_ops.read_cr4()
  139. #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
  140. #define write_cr4(x) paravirt_ops.write_cr4(x)
  141. static inline void raw_safe_halt(void)
  142. {
  143. paravirt_ops.safe_halt();
  144. }
  145. static inline void halt(void)
  146. {
  147. paravirt_ops.safe_halt();
  148. }
  149. #define wbinvd() paravirt_ops.wbinvd()
  150. #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
  151. #define rdmsr(msr,val1,val2) do { \
  152. int _err; \
  153. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  154. val1 = (u32)_l; \
  155. val2 = _l >> 32; \
  156. } while(0)
  157. #define wrmsr(msr,val1,val2) do { \
  158. u64 _l = ((u64)(val2) << 32) | (val1); \
  159. paravirt_ops.write_msr((msr), _l); \
  160. } while(0)
  161. #define rdmsrl(msr,val) do { \
  162. int _err; \
  163. val = paravirt_ops.read_msr((msr),&_err); \
  164. } while(0)
  165. #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
  166. #define wrmsr_safe(msr,a,b) ({ \
  167. u64 _l = ((u64)(b) << 32) | (a); \
  168. paravirt_ops.write_msr((msr),_l); \
  169. })
  170. /* rdmsr with exception handling */
  171. #define rdmsr_safe(msr,a,b) ({ \
  172. int _err; \
  173. u64 _l = paravirt_ops.read_msr(msr,&_err); \
  174. (*a) = (u32)_l; \
  175. (*b) = _l >> 32; \
  176. _err; })
  177. #define rdtsc(low,high) do { \
  178. u64 _l = paravirt_ops.read_tsc(); \
  179. low = (u32)_l; \
  180. high = _l >> 32; \
  181. } while(0)
  182. #define rdtscl(low) do { \
  183. u64 _l = paravirt_ops.read_tsc(); \
  184. low = (int)_l; \
  185. } while(0)
  186. #define rdtscll(val) (val = paravirt_ops.read_tsc())
  187. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  188. #define rdpmc(counter,low,high) do { \
  189. u64 _l = paravirt_ops.read_pmc(); \
  190. low = (u32)_l; \
  191. high = _l >> 32; \
  192. } while(0)
  193. #define load_TR_desc() (paravirt_ops.load_tr_desc())
  194. #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
  195. #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
  196. #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
  197. #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
  198. #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
  199. #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
  200. #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
  201. #define write_ldt_entry(dt, entry, low, high) \
  202. (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
  203. #define write_gdt_entry(dt, entry, low, high) \
  204. (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
  205. #define write_idt_entry(dt, entry, low, high) \
  206. (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
  207. #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
  208. /* The paravirtualized I/O functions */
  209. static inline void slow_down_io(void) {
  210. paravirt_ops.io_delay();
  211. #ifdef REALLY_SLOW_IO
  212. paravirt_ops.io_delay();
  213. paravirt_ops.io_delay();
  214. paravirt_ops.io_delay();
  215. #endif
  216. }
  217. /* These all sit in the .parainstructions section to tell us what to patch. */
  218. struct paravirt_patch {
  219. u8 *instr; /* original instructions */
  220. u8 instrtype; /* type of this instruction */
  221. u8 len; /* length of original instruction */
  222. u16 clobbers; /* what registers you may clobber */
  223. };
  224. #define paravirt_alt(insn_string, typenum, clobber) \
  225. "771:\n\t" insn_string "\n" "772:\n" \
  226. ".pushsection .parainstructions,\"a\"\n" \
  227. " .long 771b\n" \
  228. " .byte " __stringify(typenum) "\n" \
  229. " .byte 772b-771b\n" \
  230. " .short " __stringify(clobber) "\n" \
  231. ".popsection"
  232. static inline unsigned long __raw_local_save_flags(void)
  233. {
  234. unsigned long f;
  235. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  236. "call *%1;"
  237. "popl %%edx; popl %%ecx",
  238. PARAVIRT_SAVE_FLAGS, CLBR_NONE)
  239. : "=a"(f): "m"(paravirt_ops.save_fl)
  240. : "memory", "cc");
  241. return f;
  242. }
  243. static inline void raw_local_irq_restore(unsigned long f)
  244. {
  245. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  246. "call *%1;"
  247. "popl %%edx; popl %%ecx",
  248. PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
  249. : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
  250. : "memory", "cc");
  251. }
  252. static inline void raw_local_irq_disable(void)
  253. {
  254. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  255. "call *%0;"
  256. "popl %%edx; popl %%ecx",
  257. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  258. : : "m" (paravirt_ops.irq_disable)
  259. : "memory", "eax", "cc");
  260. }
  261. static inline void raw_local_irq_enable(void)
  262. {
  263. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  264. "call *%0;"
  265. "popl %%edx; popl %%ecx",
  266. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  267. : : "m" (paravirt_ops.irq_enable)
  268. : "memory", "eax", "cc");
  269. }
  270. static inline unsigned long __raw_local_irq_save(void)
  271. {
  272. unsigned long f;
  273. __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
  274. "call *%1; pushl %%eax;"
  275. "call *%2; popl %%eax;"
  276. "popl %%edx; popl %%ecx",
  277. PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
  278. CLBR_NONE)
  279. : "=a"(f)
  280. : "m" (paravirt_ops.save_fl),
  281. "m" (paravirt_ops.irq_disable)
  282. : "memory", "cc");
  283. return f;
  284. }
  285. #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  286. "call *paravirt_ops+%c[irq_disable];" \
  287. "popl %%edx; popl %%ecx", \
  288. PARAVIRT_IRQ_DISABLE, CLBR_EAX)
  289. #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
  290. "call *paravirt_ops+%c[irq_enable];" \
  291. "popl %%edx; popl %%ecx", \
  292. PARAVIRT_IRQ_ENABLE, CLBR_EAX)
  293. #define CLI_STI_CLOBBERS , "%eax"
  294. #define CLI_STI_INPUT_ARGS \
  295. , \
  296. [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
  297. [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
  298. #else /* __ASSEMBLY__ */
  299. #define PARA_PATCH(ptype, clobbers, ops) \
  300. 771:; \
  301. ops; \
  302. 772:; \
  303. .pushsection .parainstructions,"a"; \
  304. .long 771b; \
  305. .byte ptype; \
  306. .byte 772b-771b; \
  307. .short clobbers; \
  308. .popsection
  309. #define INTERRUPT_RETURN \
  310. PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
  311. jmp *%cs:paravirt_ops+PARAVIRT_iret)
  312. #define DISABLE_INTERRUPTS(clobbers) \
  313. PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
  314. pushl %ecx; pushl %edx; \
  315. call *paravirt_ops+PARAVIRT_irq_disable; \
  316. popl %edx; popl %ecx) \
  317. #define ENABLE_INTERRUPTS(clobbers) \
  318. PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
  319. pushl %ecx; pushl %edx; \
  320. call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
  321. popl %edx; popl %ecx)
  322. #define ENABLE_INTERRUPTS_SYSEXIT \
  323. PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
  324. jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
  325. #define GET_CR0_INTO_EAX \
  326. call *paravirt_ops+PARAVIRT_read_cr0
  327. #endif /* __ASSEMBLY__ */
  328. #endif /* CONFIG_PARAVIRT */
  329. #endif /* __ASM_PARAVIRT_H */