paravirt.h 42 KB

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