paravirt.h 44 KB

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