paravirt.h 40 KB

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