paravirt.h 37 KB

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