paravirt.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. #ifdef CONFIG_PARAVIRT
  6. #include <asm/page.h>
  7. /* Bitmask of what can be clobbered: usually at least eax. */
  8. #define CLBR_NONE 0x0
  9. #define CLBR_EAX 0x1
  10. #define CLBR_ECX 0x2
  11. #define CLBR_EDX 0x4
  12. #define CLBR_ANY 0x7
  13. #ifndef __ASSEMBLY__
  14. #include <linux/types.h>
  15. #include <linux/cpumask.h>
  16. #include <asm/kmap_types.h>
  17. struct page;
  18. struct thread_struct;
  19. struct Xgt_desc_struct;
  20. struct tss_struct;
  21. struct mm_struct;
  22. struct desc_struct;
  23. /* Lazy mode for batching updates / context switch */
  24. enum paravirt_lazy_mode {
  25. PARAVIRT_LAZY_NONE = 0,
  26. PARAVIRT_LAZY_MMU = 1,
  27. PARAVIRT_LAZY_CPU = 2,
  28. PARAVIRT_LAZY_FLUSH = 3,
  29. };
  30. struct paravirt_ops
  31. {
  32. unsigned int kernel_rpl;
  33. int shared_kernel_pmd;
  34. int paravirt_enabled;
  35. const char *name;
  36. /*
  37. * Patch may replace one of the defined code sequences with arbitrary
  38. * code, subject to the same register constraints. This generally
  39. * means the code is not free to clobber any registers other than EAX.
  40. * The patch function should return the number of bytes of code
  41. * generated, as we nop pad the rest in generic code.
  42. */
  43. unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
  44. /* Basic arch-specific setup */
  45. void (*arch_setup)(void);
  46. char *(*memory_setup)(void);
  47. void (*init_IRQ)(void);
  48. void (*time_init)(void);
  49. /*
  50. * Called before/after init_mm pagetable setup. setup_start
  51. * may reset %cr3, and may pre-install parts of the pagetable;
  52. * pagetable setup is expected to preserve any existing
  53. * mapping.
  54. */
  55. void (*pagetable_setup_start)(pgd_t *pgd_base);
  56. void (*pagetable_setup_done)(pgd_t *pgd_base);
  57. /* Print a banner to identify the environment */
  58. void (*banner)(void);
  59. /* Set and set time of day */
  60. unsigned long (*get_wallclock)(void);
  61. int (*set_wallclock)(unsigned long);
  62. /* cpuid emulation, mostly so that caps bits can be disabled */
  63. void (*cpuid)(unsigned int *eax, unsigned int *ebx,
  64. unsigned int *ecx, unsigned int *edx);
  65. /* hooks for various privileged instructions */
  66. unsigned long (*get_debugreg)(int regno);
  67. void (*set_debugreg)(int regno, unsigned long value);
  68. void (*clts)(void);
  69. unsigned long (*read_cr0)(void);
  70. void (*write_cr0)(unsigned long);
  71. unsigned long (*read_cr2)(void);
  72. void (*write_cr2)(unsigned long);
  73. unsigned long (*read_cr3)(void);
  74. void (*write_cr3)(unsigned long);
  75. unsigned long (*read_cr4_safe)(void);
  76. unsigned long (*read_cr4)(void);
  77. void (*write_cr4)(unsigned long);
  78. /*
  79. * Get/set interrupt state. save_fl and restore_fl are only
  80. * expected to use X86_EFLAGS_IF; all other bits
  81. * returned from save_fl are undefined, and may be ignored by
  82. * restore_fl.
  83. */
  84. unsigned long (*save_fl)(void);
  85. void (*restore_fl)(unsigned long);
  86. void (*irq_disable)(void);
  87. void (*irq_enable)(void);
  88. void (*safe_halt)(void);
  89. void (*halt)(void);
  90. void (*wbinvd)(void);
  91. /* MSR, PMC and TSR operations.
  92. err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
  93. u64 (*read_msr)(unsigned int msr, int *err);
  94. int (*write_msr)(unsigned int msr, u64 val);
  95. u64 (*read_tsc)(void);
  96. u64 (*read_pmc)(void);
  97. u64 (*get_scheduled_cycles)(void);
  98. unsigned long (*get_cpu_khz)(void);
  99. /* Segment descriptor handling */
  100. void (*load_tr_desc)(void);
  101. void (*load_gdt)(const struct Xgt_desc_struct *);
  102. void (*load_idt)(const struct Xgt_desc_struct *);
  103. void (*store_gdt)(struct Xgt_desc_struct *);
  104. void (*store_idt)(struct Xgt_desc_struct *);
  105. void (*set_ldt)(const void *desc, unsigned entries);
  106. unsigned long (*store_tr)(void);
  107. void (*load_tls)(struct thread_struct *t, unsigned int cpu);
  108. void (*write_ldt_entry)(struct desc_struct *,
  109. int entrynum, u32 low, u32 high);
  110. void (*write_gdt_entry)(struct desc_struct *,
  111. int entrynum, u32 low, u32 high);
  112. void (*write_idt_entry)(struct desc_struct *,
  113. int entrynum, u32 low, u32 high);
  114. void (*load_esp0)(struct tss_struct *tss, struct thread_struct *t);
  115. void (*set_iopl_mask)(unsigned mask);
  116. void (*io_delay)(void);
  117. /*
  118. * Hooks for intercepting the creation/use/destruction of an
  119. * mm_struct.
  120. */
  121. void (*activate_mm)(struct mm_struct *prev,
  122. struct mm_struct *next);
  123. void (*dup_mmap)(struct mm_struct *oldmm,
  124. struct mm_struct *mm);
  125. void (*exit_mmap)(struct mm_struct *mm);
  126. #ifdef CONFIG_X86_LOCAL_APIC
  127. /*
  128. * Direct APIC operations, principally for VMI. Ideally
  129. * these shouldn't be in this interface.
  130. */
  131. void (*apic_write)(unsigned long reg, unsigned long v);
  132. void (*apic_write_atomic)(unsigned long reg, unsigned long v);
  133. unsigned long (*apic_read)(unsigned long reg);
  134. void (*setup_boot_clock)(void);
  135. void (*setup_secondary_clock)(void);
  136. void (*startup_ipi_hook)(int phys_apicid,
  137. unsigned long start_eip,
  138. unsigned long start_esp);
  139. #endif
  140. /* TLB operations */
  141. void (*flush_tlb_user)(void);
  142. void (*flush_tlb_kernel)(void);
  143. void (*flush_tlb_single)(unsigned long addr);
  144. void (*flush_tlb_others)(const cpumask_t *cpus, struct mm_struct *mm,
  145. unsigned long va);
  146. /* Hooks for allocating/releasing pagetable pages */
  147. void (*alloc_pt)(u32 pfn);
  148. void (*alloc_pd)(u32 pfn);
  149. void (*alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
  150. void (*release_pt)(u32 pfn);
  151. void (*release_pd)(u32 pfn);
  152. /* Pagetable manipulation functions */
  153. void (*set_pte)(pte_t *ptep, pte_t pteval);
  154. void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
  155. pte_t *ptep, pte_t pteval);
  156. void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
  157. void (*pte_update)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  158. void (*pte_update_defer)(struct mm_struct *mm,
  159. unsigned long addr, pte_t *ptep);
  160. pte_t (*ptep_get_and_clear)(pte_t *ptep);
  161. #ifdef CONFIG_HIGHPTE
  162. void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
  163. #endif
  164. #ifdef CONFIG_X86_PAE
  165. void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
  166. void (*set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
  167. void (*set_pud)(pud_t *pudp, pud_t pudval);
  168. void (*pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  169. void (*pmd_clear)(pmd_t *pmdp);
  170. unsigned long long (*pte_val)(pte_t);
  171. unsigned long long (*pmd_val)(pmd_t);
  172. unsigned long long (*pgd_val)(pgd_t);
  173. pte_t (*make_pte)(unsigned long long pte);
  174. pmd_t (*make_pmd)(unsigned long long pmd);
  175. pgd_t (*make_pgd)(unsigned long long pgd);
  176. #else
  177. unsigned long (*pte_val)(pte_t);
  178. unsigned long (*pgd_val)(pgd_t);
  179. pte_t (*make_pte)(unsigned long pte);
  180. pgd_t (*make_pgd)(unsigned long pgd);
  181. #endif
  182. /* Set deferred update mode, used for batching operations. */
  183. void (*set_lazy_mode)(enum paravirt_lazy_mode mode);
  184. /* These two are jmp to, not actually called. */
  185. void (*irq_enable_sysexit)(void);
  186. void (*iret)(void);
  187. };
  188. /* Mark a paravirt probe function. */
  189. #define paravirt_probe(fn) \
  190. static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
  191. __attribute__((__section__(".paravirtprobe"))) = fn
  192. extern struct paravirt_ops paravirt_ops;
  193. #define PARAVIRT_PATCH(x) \
  194. (offsetof(struct paravirt_ops, x) / sizeof(void *))
  195. #define paravirt_type(type) \
  196. [paravirt_typenum] "i" (PARAVIRT_PATCH(type))
  197. #define paravirt_clobber(clobber) \
  198. [paravirt_clobber] "i" (clobber)
  199. /*
  200. * Generate some code, and mark it as patchable by the
  201. * apply_paravirt() alternate instruction patcher.
  202. */
  203. #define _paravirt_alt(insn_string, type, clobber) \
  204. "771:\n\t" insn_string "\n" "772:\n" \
  205. ".pushsection .parainstructions,\"a\"\n" \
  206. " .long 771b\n" \
  207. " .byte " type "\n" \
  208. " .byte 772b-771b\n" \
  209. " .short " clobber "\n" \
  210. ".popsection\n"
  211. /* Generate patchable code, with the default asm parameters. */
  212. #define paravirt_alt(insn_string) \
  213. _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
  214. unsigned paravirt_patch_nop(void);
  215. unsigned paravirt_patch_ignore(unsigned len);
  216. unsigned paravirt_patch_call(void *target, u16 tgt_clobbers,
  217. void *site, u16 site_clobbers,
  218. unsigned len);
  219. unsigned paravirt_patch_jmp(void *target, void *site, unsigned len);
  220. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len);
  221. unsigned paravirt_patch_insns(void *site, unsigned len,
  222. const char *start, const char *end);
  223. /*
  224. * This generates an indirect call based on the operation type number.
  225. * The type number, computed in PARAVIRT_PATCH, is derived from the
  226. * offset into the paravirt_ops structure, and can therefore be freely
  227. * converted back into a structure offset.
  228. */
  229. #define PARAVIRT_CALL "call *(paravirt_ops+%c[paravirt_typenum]*4);"
  230. /*
  231. * These macros are intended to wrap calls into a paravirt_ops
  232. * operation, so that they can be later identified and patched at
  233. * runtime.
  234. *
  235. * Normally, a call to a pv_op function is a simple indirect call:
  236. * (paravirt_ops.operations)(args...).
  237. *
  238. * Unfortunately, this is a relatively slow operation for modern CPUs,
  239. * because it cannot necessarily determine what the destination
  240. * address is. In this case, the address is a runtime constant, so at
  241. * the very least we can patch the call to e a simple direct call, or
  242. * ideally, patch an inline implementation into the callsite. (Direct
  243. * calls are essentially free, because the call and return addresses
  244. * are completely predictable.)
  245. *
  246. * These macros rely on the standard gcc "regparm(3)" calling
  247. * convention, in which the first three arguments are placed in %eax,
  248. * %edx, %ecx (in that order), and the remaining arguments are placed
  249. * on the stack. All caller-save registers (eax,edx,ecx) are expected
  250. * to be modified (either clobbered or used for return values).
  251. *
  252. * The call instruction itself is marked by placing its start address
  253. * and size into the .parainstructions section, so that
  254. * apply_paravirt() in arch/i386/kernel/alternative.c can do the
  255. * appropriate patching under the control of the backend paravirt_ops
  256. * implementation.
  257. *
  258. * Unfortunately there's no way to get gcc to generate the args setup
  259. * for the call, and then allow the call itself to be generated by an
  260. * inline asm. Because of this, we must do the complete arg setup and
  261. * return value handling from within these macros. This is fairly
  262. * cumbersome.
  263. *
  264. * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
  265. * It could be extended to more arguments, but there would be little
  266. * to be gained from that. For each number of arguments, there are
  267. * the two VCALL and CALL variants for void and non-void functions.
  268. *
  269. * When there is a return value, the invoker of the macro must specify
  270. * the return type. The macro then uses sizeof() on that type to
  271. * determine whether its a 32 or 64 bit value, and places the return
  272. * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
  273. * 64-bit).
  274. *
  275. * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
  276. * in low,high order.
  277. *
  278. * Small structures are passed and returned in registers. The macro
  279. * calling convention can't directly deal with this, so the wrapper
  280. * functions must do this.
  281. *
  282. * These PVOP_* macros are only defined within this header. This
  283. * means that all uses must be wrapped in inline functions. This also
  284. * makes sure the incoming and outgoing types are always correct.
  285. */
  286. #define __PVOP_CALL(rettype, op, pre, post, ...) \
  287. ({ \
  288. rettype __ret; \
  289. unsigned long __eax, __edx, __ecx; \
  290. if (sizeof(rettype) > sizeof(unsigned long)) { \
  291. asm volatile(pre \
  292. paravirt_alt(PARAVIRT_CALL) \
  293. post \
  294. : "=a" (__eax), "=d" (__edx), \
  295. "=c" (__ecx) \
  296. : paravirt_type(op), \
  297. paravirt_clobber(CLBR_ANY), \
  298. ##__VA_ARGS__ \
  299. : "memory", "cc"); \
  300. __ret = (rettype)((((u64)__edx) << 32) | __eax); \
  301. } else { \
  302. asm volatile(pre \
  303. paravirt_alt(PARAVIRT_CALL) \
  304. post \
  305. : "=a" (__eax), "=d" (__edx), \
  306. "=c" (__ecx) \
  307. : paravirt_type(op), \
  308. paravirt_clobber(CLBR_ANY), \
  309. ##__VA_ARGS__ \
  310. : "memory", "cc"); \
  311. __ret = (rettype)__eax; \
  312. } \
  313. __ret; \
  314. })
  315. #define __PVOP_VCALL(op, pre, post, ...) \
  316. ({ \
  317. unsigned long __eax, __edx, __ecx; \
  318. asm volatile(pre \
  319. paravirt_alt(PARAVIRT_CALL) \
  320. post \
  321. : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
  322. : paravirt_type(op), \
  323. paravirt_clobber(CLBR_ANY), \
  324. ##__VA_ARGS__ \
  325. : "memory", "cc"); \
  326. })
  327. #define PVOP_CALL0(rettype, op) \
  328. __PVOP_CALL(rettype, op, "", "")
  329. #define PVOP_VCALL0(op) \
  330. __PVOP_VCALL(op, "", "")
  331. #define PVOP_CALL1(rettype, op, arg1) \
  332. __PVOP_CALL(rettype, op, "", "", "0" ((u32)(arg1)))
  333. #define PVOP_VCALL1(op, arg1) \
  334. __PVOP_VCALL(op, "", "", "0" ((u32)(arg1)))
  335. #define PVOP_CALL2(rettype, op, arg1, arg2) \
  336. __PVOP_CALL(rettype, op, "", "", "0" ((u32)(arg1)), "1" ((u32)(arg2)))
  337. #define PVOP_VCALL2(op, arg1, arg2) \
  338. __PVOP_VCALL(op, "", "", "0" ((u32)(arg1)), "1" ((u32)(arg2)))
  339. #define PVOP_CALL3(rettype, op, arg1, arg2, arg3) \
  340. __PVOP_CALL(rettype, op, "", "", "0" ((u32)(arg1)), \
  341. "1"((u32)(arg2)), "2"((u32)(arg3)))
  342. #define PVOP_VCALL3(op, arg1, arg2, arg3) \
  343. __PVOP_VCALL(op, "", "", "0" ((u32)(arg1)), "1"((u32)(arg2)), \
  344. "2"((u32)(arg3)))
  345. #define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
  346. __PVOP_CALL(rettype, op, \
  347. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  348. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  349. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  350. #define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
  351. __PVOP_VCALL(op, \
  352. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  353. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  354. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  355. static inline int paravirt_enabled(void)
  356. {
  357. return paravirt_ops.paravirt_enabled;
  358. }
  359. static inline void load_esp0(struct tss_struct *tss,
  360. struct thread_struct *thread)
  361. {
  362. PVOP_VCALL2(load_esp0, tss, thread);
  363. }
  364. #define ARCH_SETUP paravirt_ops.arch_setup();
  365. static inline unsigned long get_wallclock(void)
  366. {
  367. return PVOP_CALL0(unsigned long, get_wallclock);
  368. }
  369. static inline int set_wallclock(unsigned long nowtime)
  370. {
  371. return PVOP_CALL1(int, set_wallclock, nowtime);
  372. }
  373. static inline void (*choose_time_init(void))(void)
  374. {
  375. return paravirt_ops.time_init;
  376. }
  377. /* The paravirtualized CPUID instruction. */
  378. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  379. unsigned int *ecx, unsigned int *edx)
  380. {
  381. PVOP_VCALL4(cpuid, eax, ebx, ecx, edx);
  382. }
  383. /*
  384. * These special macros can be used to get or set a debugging register
  385. */
  386. static inline unsigned long paravirt_get_debugreg(int reg)
  387. {
  388. return PVOP_CALL1(unsigned long, get_debugreg, reg);
  389. }
  390. #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
  391. static inline void set_debugreg(unsigned long val, int reg)
  392. {
  393. PVOP_VCALL2(set_debugreg, reg, val);
  394. }
  395. static inline void clts(void)
  396. {
  397. PVOP_VCALL0(clts);
  398. }
  399. static inline unsigned long read_cr0(void)
  400. {
  401. return PVOP_CALL0(unsigned long, read_cr0);
  402. }
  403. static inline void write_cr0(unsigned long x)
  404. {
  405. PVOP_VCALL1(write_cr0, x);
  406. }
  407. static inline unsigned long read_cr2(void)
  408. {
  409. return PVOP_CALL0(unsigned long, read_cr2);
  410. }
  411. static inline void write_cr2(unsigned long x)
  412. {
  413. PVOP_VCALL1(write_cr2, x);
  414. }
  415. static inline unsigned long read_cr3(void)
  416. {
  417. return PVOP_CALL0(unsigned long, read_cr3);
  418. }
  419. static inline void write_cr3(unsigned long x)
  420. {
  421. PVOP_VCALL1(write_cr3, x);
  422. }
  423. static inline unsigned long read_cr4(void)
  424. {
  425. return PVOP_CALL0(unsigned long, read_cr4);
  426. }
  427. static inline unsigned long read_cr4_safe(void)
  428. {
  429. return PVOP_CALL0(unsigned long, read_cr4_safe);
  430. }
  431. static inline void write_cr4(unsigned long x)
  432. {
  433. PVOP_VCALL1(write_cr4, x);
  434. }
  435. static inline void raw_safe_halt(void)
  436. {
  437. PVOP_VCALL0(safe_halt);
  438. }
  439. static inline void halt(void)
  440. {
  441. PVOP_VCALL0(safe_halt);
  442. }
  443. static inline void wbinvd(void)
  444. {
  445. PVOP_VCALL0(wbinvd);
  446. }
  447. #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
  448. static inline u64 paravirt_read_msr(unsigned msr, int *err)
  449. {
  450. return PVOP_CALL2(u64, read_msr, msr, err);
  451. }
  452. static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
  453. {
  454. return PVOP_CALL3(int, write_msr, msr, low, high);
  455. }
  456. /* These should all do BUG_ON(_err), but our headers are too tangled. */
  457. #define rdmsr(msr,val1,val2) do { \
  458. int _err; \
  459. u64 _l = paravirt_read_msr(msr, &_err); \
  460. val1 = (u32)_l; \
  461. val2 = _l >> 32; \
  462. } while(0)
  463. #define wrmsr(msr,val1,val2) do { \
  464. paravirt_write_msr(msr, val1, val2); \
  465. } while(0)
  466. #define rdmsrl(msr,val) do { \
  467. int _err; \
  468. val = paravirt_read_msr(msr, &_err); \
  469. } while(0)
  470. #define wrmsrl(msr,val) ((void)paravirt_write_msr(msr, val, 0))
  471. #define wrmsr_safe(msr,a,b) paravirt_write_msr(msr, a, b)
  472. /* rdmsr with exception handling */
  473. #define rdmsr_safe(msr,a,b) ({ \
  474. int _err; \
  475. u64 _l = paravirt_read_msr(msr, &_err); \
  476. (*a) = (u32)_l; \
  477. (*b) = _l >> 32; \
  478. _err; })
  479. static inline u64 paravirt_read_tsc(void)
  480. {
  481. return PVOP_CALL0(u64, read_tsc);
  482. }
  483. #define rdtsc(low,high) do { \
  484. u64 _l = paravirt_read_tsc(); \
  485. low = (u32)_l; \
  486. high = _l >> 32; \
  487. } while(0)
  488. #define rdtscl(low) do { \
  489. u64 _l = paravirt_read_tsc(); \
  490. low = (int)_l; \
  491. } while(0)
  492. #define rdtscll(val) (val = paravirt_read_tsc())
  493. #define get_scheduled_cycles(val) (val = paravirt_ops.get_scheduled_cycles())
  494. #define calculate_cpu_khz() (paravirt_ops.get_cpu_khz())
  495. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  496. static inline unsigned long long paravirt_read_pmc(int counter)
  497. {
  498. return PVOP_CALL1(u64, read_pmc, counter);
  499. }
  500. #define rdpmc(counter,low,high) do { \
  501. u64 _l = paravirt_read_pmc(counter); \
  502. low = (u32)_l; \
  503. high = _l >> 32; \
  504. } while(0)
  505. static inline void load_TR_desc(void)
  506. {
  507. PVOP_VCALL0(load_tr_desc);
  508. }
  509. static inline void load_gdt(const struct Xgt_desc_struct *dtr)
  510. {
  511. PVOP_VCALL1(load_gdt, dtr);
  512. }
  513. static inline void load_idt(const struct Xgt_desc_struct *dtr)
  514. {
  515. PVOP_VCALL1(load_idt, dtr);
  516. }
  517. static inline void set_ldt(const void *addr, unsigned entries)
  518. {
  519. PVOP_VCALL2(set_ldt, addr, entries);
  520. }
  521. static inline void store_gdt(struct Xgt_desc_struct *dtr)
  522. {
  523. PVOP_VCALL1(store_gdt, dtr);
  524. }
  525. static inline void store_idt(struct Xgt_desc_struct *dtr)
  526. {
  527. PVOP_VCALL1(store_idt, dtr);
  528. }
  529. static inline unsigned long paravirt_store_tr(void)
  530. {
  531. return PVOP_CALL0(unsigned long, store_tr);
  532. }
  533. #define store_tr(tr) ((tr) = paravirt_store_tr())
  534. static inline void load_TLS(struct thread_struct *t, unsigned cpu)
  535. {
  536. PVOP_VCALL2(load_tls, t, cpu);
  537. }
  538. static inline void write_ldt_entry(void *dt, int entry, u32 low, u32 high)
  539. {
  540. PVOP_VCALL4(write_ldt_entry, dt, entry, low, high);
  541. }
  542. static inline void write_gdt_entry(void *dt, int entry, u32 low, u32 high)
  543. {
  544. PVOP_VCALL4(write_gdt_entry, dt, entry, low, high);
  545. }
  546. static inline void write_idt_entry(void *dt, int entry, u32 low, u32 high)
  547. {
  548. PVOP_VCALL4(write_idt_entry, dt, entry, low, high);
  549. }
  550. static inline void set_iopl_mask(unsigned mask)
  551. {
  552. PVOP_VCALL1(set_iopl_mask, mask);
  553. }
  554. /* The paravirtualized I/O functions */
  555. static inline void slow_down_io(void) {
  556. paravirt_ops.io_delay();
  557. #ifdef REALLY_SLOW_IO
  558. paravirt_ops.io_delay();
  559. paravirt_ops.io_delay();
  560. paravirt_ops.io_delay();
  561. #endif
  562. }
  563. #ifdef CONFIG_X86_LOCAL_APIC
  564. /*
  565. * Basic functions accessing APICs.
  566. */
  567. static inline void apic_write(unsigned long reg, unsigned long v)
  568. {
  569. PVOP_VCALL2(apic_write, reg, v);
  570. }
  571. static inline void apic_write_atomic(unsigned long reg, unsigned long v)
  572. {
  573. PVOP_VCALL2(apic_write_atomic, reg, v);
  574. }
  575. static inline unsigned long apic_read(unsigned long reg)
  576. {
  577. return PVOP_CALL1(unsigned long, apic_read, reg);
  578. }
  579. static inline void setup_boot_clock(void)
  580. {
  581. PVOP_VCALL0(setup_boot_clock);
  582. }
  583. static inline void setup_secondary_clock(void)
  584. {
  585. PVOP_VCALL0(setup_secondary_clock);
  586. }
  587. #endif
  588. static inline void paravirt_pagetable_setup_start(pgd_t *base)
  589. {
  590. if (paravirt_ops.pagetable_setup_start)
  591. (*paravirt_ops.pagetable_setup_start)(base);
  592. }
  593. static inline void paravirt_pagetable_setup_done(pgd_t *base)
  594. {
  595. if (paravirt_ops.pagetable_setup_done)
  596. (*paravirt_ops.pagetable_setup_done)(base);
  597. }
  598. #ifdef CONFIG_SMP
  599. static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  600. unsigned long start_esp)
  601. {
  602. PVOP_VCALL3(startup_ipi_hook, phys_apicid, start_eip, start_esp);
  603. }
  604. #endif
  605. static inline void paravirt_activate_mm(struct mm_struct *prev,
  606. struct mm_struct *next)
  607. {
  608. PVOP_VCALL2(activate_mm, prev, next);
  609. }
  610. static inline void arch_dup_mmap(struct mm_struct *oldmm,
  611. struct mm_struct *mm)
  612. {
  613. PVOP_VCALL2(dup_mmap, oldmm, mm);
  614. }
  615. static inline void arch_exit_mmap(struct mm_struct *mm)
  616. {
  617. PVOP_VCALL1(exit_mmap, mm);
  618. }
  619. static inline void __flush_tlb(void)
  620. {
  621. PVOP_VCALL0(flush_tlb_user);
  622. }
  623. static inline void __flush_tlb_global(void)
  624. {
  625. PVOP_VCALL0(flush_tlb_kernel);
  626. }
  627. static inline void __flush_tlb_single(unsigned long addr)
  628. {
  629. PVOP_VCALL1(flush_tlb_single, addr);
  630. }
  631. static inline void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  632. unsigned long va)
  633. {
  634. PVOP_VCALL3(flush_tlb_others, &cpumask, mm, va);
  635. }
  636. static inline void paravirt_alloc_pt(unsigned pfn)
  637. {
  638. PVOP_VCALL1(alloc_pt, pfn);
  639. }
  640. static inline void paravirt_release_pt(unsigned pfn)
  641. {
  642. PVOP_VCALL1(release_pt, pfn);
  643. }
  644. static inline void paravirt_alloc_pd(unsigned pfn)
  645. {
  646. PVOP_VCALL1(alloc_pd, pfn);
  647. }
  648. static inline void paravirt_alloc_pd_clone(unsigned pfn, unsigned clonepfn,
  649. unsigned start, unsigned count)
  650. {
  651. PVOP_VCALL4(alloc_pd_clone, pfn, clonepfn, start, count);
  652. }
  653. static inline void paravirt_release_pd(unsigned pfn)
  654. {
  655. PVOP_VCALL1(release_pd, pfn);
  656. }
  657. #ifdef CONFIG_HIGHPTE
  658. static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
  659. {
  660. unsigned long ret;
  661. ret = PVOP_CALL2(unsigned long, kmap_atomic_pte, page, type);
  662. return (void *)ret;
  663. }
  664. #endif
  665. static inline void pte_update(struct mm_struct *mm, unsigned long addr,
  666. pte_t *ptep)
  667. {
  668. PVOP_VCALL3(pte_update, mm, addr, ptep);
  669. }
  670. static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
  671. pte_t *ptep)
  672. {
  673. PVOP_VCALL3(pte_update_defer, mm, addr, ptep);
  674. }
  675. #ifdef CONFIG_X86_PAE
  676. static inline pte_t __pte(unsigned long long val)
  677. {
  678. unsigned long long ret = PVOP_CALL2(unsigned long long, make_pte,
  679. val, val >> 32);
  680. return (pte_t) { ret, ret >> 32 };
  681. }
  682. static inline pmd_t __pmd(unsigned long long val)
  683. {
  684. return (pmd_t) { PVOP_CALL2(unsigned long long, make_pmd, val, val >> 32) };
  685. }
  686. static inline pgd_t __pgd(unsigned long long val)
  687. {
  688. return (pgd_t) { PVOP_CALL2(unsigned long long, make_pgd, val, val >> 32) };
  689. }
  690. static inline unsigned long long pte_val(pte_t x)
  691. {
  692. return PVOP_CALL2(unsigned long long, pte_val, x.pte_low, x.pte_high);
  693. }
  694. static inline unsigned long long pmd_val(pmd_t x)
  695. {
  696. return PVOP_CALL2(unsigned long long, pmd_val, x.pmd, x.pmd >> 32);
  697. }
  698. static inline unsigned long long pgd_val(pgd_t x)
  699. {
  700. return PVOP_CALL2(unsigned long long, pgd_val, x.pgd, x.pgd >> 32);
  701. }
  702. static inline void set_pte(pte_t *ptep, pte_t pteval)
  703. {
  704. PVOP_VCALL3(set_pte, ptep, pteval.pte_low, pteval.pte_high);
  705. }
  706. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  707. pte_t *ptep, pte_t pteval)
  708. {
  709. /* 5 arg words */
  710. paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
  711. }
  712. static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
  713. {
  714. PVOP_VCALL3(set_pte_atomic, ptep, pteval.pte_low, pteval.pte_high);
  715. }
  716. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
  717. pte_t *ptep, pte_t pte)
  718. {
  719. /* 5 arg words */
  720. paravirt_ops.set_pte_present(mm, addr, ptep, pte);
  721. }
  722. static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
  723. {
  724. PVOP_VCALL3(set_pmd, pmdp, pmdval.pmd, pmdval.pmd >> 32);
  725. }
  726. static inline void set_pud(pud_t *pudp, pud_t pudval)
  727. {
  728. PVOP_VCALL3(set_pud, pudp, pudval.pgd.pgd, pudval.pgd.pgd >> 32);
  729. }
  730. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  731. {
  732. PVOP_VCALL3(pte_clear, mm, addr, ptep);
  733. }
  734. static inline void pmd_clear(pmd_t *pmdp)
  735. {
  736. PVOP_VCALL1(pmd_clear, pmdp);
  737. }
  738. static inline pte_t raw_ptep_get_and_clear(pte_t *p)
  739. {
  740. unsigned long long val = PVOP_CALL1(unsigned long long, ptep_get_and_clear, p);
  741. return (pte_t) { val, val >> 32 };
  742. }
  743. #else /* !CONFIG_X86_PAE */
  744. static inline pte_t __pte(unsigned long val)
  745. {
  746. return (pte_t) { PVOP_CALL1(unsigned long, make_pte, val) };
  747. }
  748. static inline pgd_t __pgd(unsigned long val)
  749. {
  750. return (pgd_t) { PVOP_CALL1(unsigned long, make_pgd, val) };
  751. }
  752. static inline unsigned long pte_val(pte_t x)
  753. {
  754. return PVOP_CALL1(unsigned long, pte_val, x.pte_low);
  755. }
  756. static inline unsigned long pgd_val(pgd_t x)
  757. {
  758. return PVOP_CALL1(unsigned long, pgd_val, x.pgd);
  759. }
  760. static inline void set_pte(pte_t *ptep, pte_t pteval)
  761. {
  762. PVOP_VCALL2(set_pte, ptep, pteval.pte_low);
  763. }
  764. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  765. pte_t *ptep, pte_t pteval)
  766. {
  767. PVOP_VCALL4(set_pte_at, mm, addr, ptep, pteval.pte_low);
  768. }
  769. static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
  770. {
  771. PVOP_VCALL2(set_pmd, pmdp, pmdval.pud.pgd.pgd);
  772. }
  773. static inline pte_t raw_ptep_get_and_clear(pte_t *p)
  774. {
  775. return (pte_t) { PVOP_CALL1(unsigned long, ptep_get_and_clear, p) };
  776. }
  777. #endif /* CONFIG_X86_PAE */
  778. #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
  779. static inline void arch_enter_lazy_cpu_mode(void)
  780. {
  781. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_CPU);
  782. }
  783. static inline void arch_leave_lazy_cpu_mode(void)
  784. {
  785. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_NONE);
  786. }
  787. static inline void arch_flush_lazy_cpu_mode(void)
  788. {
  789. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_FLUSH);
  790. }
  791. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  792. static inline void arch_enter_lazy_mmu_mode(void)
  793. {
  794. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_MMU);
  795. }
  796. static inline void arch_leave_lazy_mmu_mode(void)
  797. {
  798. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_NONE);
  799. }
  800. static inline void arch_flush_lazy_mmu_mode(void)
  801. {
  802. PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_FLUSH);
  803. }
  804. void _paravirt_nop(void);
  805. #define paravirt_nop ((void *)_paravirt_nop)
  806. /* These all sit in the .parainstructions section to tell us what to patch. */
  807. struct paravirt_patch_site {
  808. u8 *instr; /* original instructions */
  809. u8 instrtype; /* type of this instruction */
  810. u8 len; /* length of original instruction */
  811. u16 clobbers; /* what registers you may clobber */
  812. };
  813. extern struct paravirt_patch_site __parainstructions[],
  814. __parainstructions_end[];
  815. static inline unsigned long __raw_local_save_flags(void)
  816. {
  817. unsigned long f;
  818. asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
  819. PARAVIRT_CALL
  820. "popl %%edx; popl %%ecx")
  821. : "=a"(f)
  822. : paravirt_type(save_fl),
  823. paravirt_clobber(CLBR_EAX)
  824. : "memory", "cc");
  825. return f;
  826. }
  827. static inline void raw_local_irq_restore(unsigned long f)
  828. {
  829. asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
  830. PARAVIRT_CALL
  831. "popl %%edx; popl %%ecx")
  832. : "=a"(f)
  833. : "0"(f),
  834. paravirt_type(restore_fl),
  835. paravirt_clobber(CLBR_EAX)
  836. : "memory", "cc");
  837. }
  838. static inline void raw_local_irq_disable(void)
  839. {
  840. asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
  841. PARAVIRT_CALL
  842. "popl %%edx; popl %%ecx")
  843. :
  844. : paravirt_type(irq_disable),
  845. paravirt_clobber(CLBR_EAX)
  846. : "memory", "eax", "cc");
  847. }
  848. static inline void raw_local_irq_enable(void)
  849. {
  850. asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
  851. PARAVIRT_CALL
  852. "popl %%edx; popl %%ecx")
  853. :
  854. : paravirt_type(irq_enable),
  855. paravirt_clobber(CLBR_EAX)
  856. : "memory", "eax", "cc");
  857. }
  858. static inline unsigned long __raw_local_irq_save(void)
  859. {
  860. unsigned long f;
  861. f = __raw_local_save_flags();
  862. raw_local_irq_disable();
  863. return f;
  864. }
  865. #define CLI_STRING \
  866. _paravirt_alt("pushl %%ecx; pushl %%edx;" \
  867. "call *paravirt_ops+%c[paravirt_cli_type]*4;" \
  868. "popl %%edx; popl %%ecx", \
  869. "%c[paravirt_cli_type]", "%c[paravirt_clobber]")
  870. #define STI_STRING \
  871. _paravirt_alt("pushl %%ecx; pushl %%edx;" \
  872. "call *paravirt_ops+%c[paravirt_sti_type]*4;" \
  873. "popl %%edx; popl %%ecx", \
  874. "%c[paravirt_sti_type]", "%c[paravirt_clobber]")
  875. #define CLI_STI_CLOBBERS , "%eax"
  876. #define CLI_STI_INPUT_ARGS \
  877. , \
  878. [paravirt_cli_type] "i" (PARAVIRT_PATCH(irq_disable)), \
  879. [paravirt_sti_type] "i" (PARAVIRT_PATCH(irq_enable)), \
  880. paravirt_clobber(CLBR_EAX)
  881. /* Make sure as little as possible of this mess escapes. */
  882. #undef PARAVIRT_CALL
  883. #undef __PVOP_CALL
  884. #undef __PVOP_VCALL
  885. #undef PVOP_VCALL0
  886. #undef PVOP_CALL0
  887. #undef PVOP_VCALL1
  888. #undef PVOP_CALL1
  889. #undef PVOP_VCALL2
  890. #undef PVOP_CALL2
  891. #undef PVOP_VCALL3
  892. #undef PVOP_CALL3
  893. #undef PVOP_VCALL4
  894. #undef PVOP_CALL4
  895. #else /* __ASSEMBLY__ */
  896. #define PARA_PATCH(off) ((off) / 4)
  897. #define PARA_SITE(ptype, clobbers, ops) \
  898. 771:; \
  899. ops; \
  900. 772:; \
  901. .pushsection .parainstructions,"a"; \
  902. .long 771b; \
  903. .byte ptype; \
  904. .byte 772b-771b; \
  905. .short clobbers; \
  906. .popsection
  907. #define INTERRUPT_RETURN \
  908. PARA_SITE(PARA_PATCH(PARAVIRT_iret), CLBR_NONE, \
  909. jmp *%cs:paravirt_ops+PARAVIRT_iret)
  910. #define DISABLE_INTERRUPTS(clobbers) \
  911. PARA_SITE(PARA_PATCH(PARAVIRT_irq_disable), clobbers, \
  912. pushl %eax; pushl %ecx; pushl %edx; \
  913. call *%cs:paravirt_ops+PARAVIRT_irq_disable; \
  914. popl %edx; popl %ecx; popl %eax) \
  915. #define ENABLE_INTERRUPTS(clobbers) \
  916. PARA_SITE(PARA_PATCH(PARAVIRT_irq_enable), clobbers, \
  917. pushl %eax; pushl %ecx; pushl %edx; \
  918. call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
  919. popl %edx; popl %ecx; popl %eax)
  920. #define ENABLE_INTERRUPTS_SYSEXIT \
  921. PARA_SITE(PARA_PATCH(PARAVIRT_irq_enable_sysexit), CLBR_NONE, \
  922. jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
  923. #define GET_CR0_INTO_EAX \
  924. push %ecx; push %edx; \
  925. call *paravirt_ops+PARAVIRT_read_cr0; \
  926. pop %edx; pop %ecx
  927. #endif /* __ASSEMBLY__ */
  928. #endif /* CONFIG_PARAVIRT */
  929. #endif /* __ASM_PARAVIRT_H */