paravirt.h 43 KB

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