paravirt.h 42 KB

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