paravirt.h 42 KB

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