paravirt.h 48 KB

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