paravirt.h 43 KB

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