paravirt.h 35 KB

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