paravirt.h 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. #include <asm/paravirt_types.h>
  9. #ifndef __ASSEMBLY__
  10. #include <linux/bug.h>
  11. #include <linux/types.h>
  12. #include <linux/cpumask.h>
  13. static inline int paravirt_enabled(void)
  14. {
  15. return pv_info.paravirt_enabled;
  16. }
  17. static inline void load_sp0(struct tss_struct *tss,
  18. struct thread_struct *thread)
  19. {
  20. PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
  21. }
  22. /* The paravirtualized CPUID instruction. */
  23. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  24. unsigned int *ecx, unsigned int *edx)
  25. {
  26. PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
  27. }
  28. /*
  29. * These special macros can be used to get or set a debugging register
  30. */
  31. static inline unsigned long paravirt_get_debugreg(int reg)
  32. {
  33. return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
  34. }
  35. #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
  36. static inline void set_debugreg(unsigned long val, int reg)
  37. {
  38. PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
  39. }
  40. static inline void clts(void)
  41. {
  42. PVOP_VCALL0(pv_cpu_ops.clts);
  43. }
  44. static inline unsigned long read_cr0(void)
  45. {
  46. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
  47. }
  48. static inline void write_cr0(unsigned long x)
  49. {
  50. PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
  51. }
  52. static inline unsigned long read_cr2(void)
  53. {
  54. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
  55. }
  56. static inline void write_cr2(unsigned long x)
  57. {
  58. PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
  59. }
  60. static inline unsigned long read_cr3(void)
  61. {
  62. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
  63. }
  64. static inline void write_cr3(unsigned long x)
  65. {
  66. PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
  67. }
  68. static inline unsigned long read_cr4(void)
  69. {
  70. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
  71. }
  72. static inline unsigned long read_cr4_safe(void)
  73. {
  74. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
  75. }
  76. static inline void write_cr4(unsigned long x)
  77. {
  78. PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
  79. }
  80. #ifdef CONFIG_X86_64
  81. static inline unsigned long read_cr8(void)
  82. {
  83. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr8);
  84. }
  85. static inline void write_cr8(unsigned long x)
  86. {
  87. PVOP_VCALL1(pv_cpu_ops.write_cr8, x);
  88. }
  89. #endif
  90. static inline void arch_safe_halt(void)
  91. {
  92. PVOP_VCALL0(pv_irq_ops.safe_halt);
  93. }
  94. static inline void halt(void)
  95. {
  96. PVOP_VCALL0(pv_irq_ops.halt);
  97. }
  98. static inline void wbinvd(void)
  99. {
  100. PVOP_VCALL0(pv_cpu_ops.wbinvd);
  101. }
  102. #define get_kernel_rpl() (pv_info.kernel_rpl)
  103. static inline u64 paravirt_read_msr(unsigned msr, int *err)
  104. {
  105. return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
  106. }
  107. static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
  108. {
  109. return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
  110. }
  111. /* These should all do BUG_ON(_err), but our headers are too tangled. */
  112. #define rdmsr(msr, val1, val2) \
  113. do { \
  114. int _err; \
  115. u64 _l = paravirt_read_msr(msr, &_err); \
  116. val1 = (u32)_l; \
  117. val2 = _l >> 32; \
  118. } while (0)
  119. #define wrmsr(msr, val1, val2) \
  120. do { \
  121. paravirt_write_msr(msr, val1, val2); \
  122. } while (0)
  123. #define rdmsrl(msr, val) \
  124. do { \
  125. int _err; \
  126. val = paravirt_read_msr(msr, &_err); \
  127. } while (0)
  128. #define wrmsrl(msr, val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
  129. #define wrmsr_safe(msr, a, b) paravirt_write_msr(msr, a, b)
  130. /* rdmsr with exception handling */
  131. #define rdmsr_safe(msr, a, b) \
  132. ({ \
  133. int _err; \
  134. u64 _l = paravirt_read_msr(msr, &_err); \
  135. (*a) = (u32)_l; \
  136. (*b) = _l >> 32; \
  137. _err; \
  138. })
  139. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  140. {
  141. int err;
  142. *p = paravirt_read_msr(msr, &err);
  143. return err;
  144. }
  145. static inline u64 paravirt_read_tsc(void)
  146. {
  147. return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
  148. }
  149. #define rdtscl(low) \
  150. do { \
  151. u64 _l = paravirt_read_tsc(); \
  152. low = (int)_l; \
  153. } while (0)
  154. #define rdtscll(val) (val = paravirt_read_tsc())
  155. static inline unsigned long long paravirt_sched_clock(void)
  156. {
  157. return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
  158. }
  159. struct static_key;
  160. extern struct static_key paravirt_steal_enabled;
  161. extern struct static_key paravirt_steal_rq_enabled;
  162. static inline u64 paravirt_steal_clock(int cpu)
  163. {
  164. return PVOP_CALL1(u64, pv_time_ops.steal_clock, cpu);
  165. }
  166. static inline unsigned long long paravirt_read_pmc(int counter)
  167. {
  168. return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
  169. }
  170. #define rdpmc(counter, low, high) \
  171. do { \
  172. u64 _l = paravirt_read_pmc(counter); \
  173. low = (u32)_l; \
  174. high = _l >> 32; \
  175. } while (0)
  176. #define rdpmcl(counter, val) ((val) = paravirt_read_pmc(counter))
  177. static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
  178. {
  179. return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
  180. }
  181. #define rdtscp(low, high, aux) \
  182. do { \
  183. int __aux; \
  184. unsigned long __val = paravirt_rdtscp(&__aux); \
  185. (low) = (u32)__val; \
  186. (high) = (u32)(__val >> 32); \
  187. (aux) = __aux; \
  188. } while (0)
  189. #define rdtscpll(val, aux) \
  190. do { \
  191. unsigned long __aux; \
  192. val = paravirt_rdtscp(&__aux); \
  193. (aux) = __aux; \
  194. } while (0)
  195. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  196. {
  197. PVOP_VCALL2(pv_cpu_ops.alloc_ldt, ldt, entries);
  198. }
  199. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  200. {
  201. PVOP_VCALL2(pv_cpu_ops.free_ldt, ldt, entries);
  202. }
  203. static inline void load_TR_desc(void)
  204. {
  205. PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
  206. }
  207. static inline void load_gdt(const struct desc_ptr *dtr)
  208. {
  209. PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
  210. }
  211. static inline void load_idt(const struct desc_ptr *dtr)
  212. {
  213. PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
  214. }
  215. static inline void set_ldt(const void *addr, unsigned entries)
  216. {
  217. PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
  218. }
  219. static inline void store_gdt(struct desc_ptr *dtr)
  220. {
  221. PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
  222. }
  223. static inline void store_idt(struct desc_ptr *dtr)
  224. {
  225. PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
  226. }
  227. static inline unsigned long paravirt_store_tr(void)
  228. {
  229. return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
  230. }
  231. #define store_tr(tr) ((tr) = paravirt_store_tr())
  232. static inline void load_TLS(struct thread_struct *t, unsigned cpu)
  233. {
  234. PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
  235. }
  236. #ifdef CONFIG_X86_64
  237. static inline void load_gs_index(unsigned int gs)
  238. {
  239. PVOP_VCALL1(pv_cpu_ops.load_gs_index, gs);
  240. }
  241. #endif
  242. static inline void write_ldt_entry(struct desc_struct *dt, int entry,
  243. const void *desc)
  244. {
  245. PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
  246. }
  247. static inline void write_gdt_entry(struct desc_struct *dt, int entry,
  248. void *desc, int type)
  249. {
  250. PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
  251. }
  252. static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
  253. {
  254. PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
  255. }
  256. static inline void set_iopl_mask(unsigned mask)
  257. {
  258. PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
  259. }
  260. /* The paravirtualized I/O functions */
  261. static inline void slow_down_io(void)
  262. {
  263. pv_cpu_ops.io_delay();
  264. #ifdef REALLY_SLOW_IO
  265. pv_cpu_ops.io_delay();
  266. pv_cpu_ops.io_delay();
  267. pv_cpu_ops.io_delay();
  268. #endif
  269. }
  270. #ifdef CONFIG_SMP
  271. static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  272. unsigned long start_esp)
  273. {
  274. PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
  275. phys_apicid, start_eip, start_esp);
  276. }
  277. #endif
  278. static inline void paravirt_activate_mm(struct mm_struct *prev,
  279. struct mm_struct *next)
  280. {
  281. PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
  282. }
  283. static inline void arch_dup_mmap(struct mm_struct *oldmm,
  284. struct mm_struct *mm)
  285. {
  286. PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
  287. }
  288. static inline void arch_exit_mmap(struct mm_struct *mm)
  289. {
  290. PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
  291. }
  292. static inline void __flush_tlb(void)
  293. {
  294. PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
  295. }
  296. static inline void __flush_tlb_global(void)
  297. {
  298. PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
  299. }
  300. static inline void __flush_tlb_single(unsigned long addr)
  301. {
  302. PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
  303. }
  304. static inline void flush_tlb_others(const struct cpumask *cpumask,
  305. struct mm_struct *mm,
  306. unsigned long start,
  307. unsigned long end)
  308. {
  309. PVOP_VCALL4(pv_mmu_ops.flush_tlb_others, cpumask, mm, start, end);
  310. }
  311. static inline int paravirt_pgd_alloc(struct mm_struct *mm)
  312. {
  313. return PVOP_CALL1(int, pv_mmu_ops.pgd_alloc, mm);
  314. }
  315. static inline void paravirt_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  316. {
  317. PVOP_VCALL2(pv_mmu_ops.pgd_free, mm, pgd);
  318. }
  319. static inline void paravirt_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  320. {
  321. PVOP_VCALL2(pv_mmu_ops.alloc_pte, mm, pfn);
  322. }
  323. static inline void paravirt_release_pte(unsigned long pfn)
  324. {
  325. PVOP_VCALL1(pv_mmu_ops.release_pte, pfn);
  326. }
  327. static inline void paravirt_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  328. {
  329. PVOP_VCALL2(pv_mmu_ops.alloc_pmd, mm, pfn);
  330. }
  331. static inline void paravirt_release_pmd(unsigned long pfn)
  332. {
  333. PVOP_VCALL1(pv_mmu_ops.release_pmd, pfn);
  334. }
  335. static inline void paravirt_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  336. {
  337. PVOP_VCALL2(pv_mmu_ops.alloc_pud, mm, pfn);
  338. }
  339. static inline void paravirt_release_pud(unsigned long pfn)
  340. {
  341. PVOP_VCALL1(pv_mmu_ops.release_pud, pfn);
  342. }
  343. static inline void pte_update(struct mm_struct *mm, unsigned long addr,
  344. pte_t *ptep)
  345. {
  346. PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
  347. }
  348. static inline void pmd_update(struct mm_struct *mm, unsigned long addr,
  349. pmd_t *pmdp)
  350. {
  351. PVOP_VCALL3(pv_mmu_ops.pmd_update, mm, addr, pmdp);
  352. }
  353. static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
  354. pte_t *ptep)
  355. {
  356. PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
  357. }
  358. static inline void pmd_update_defer(struct mm_struct *mm, unsigned long addr,
  359. pmd_t *pmdp)
  360. {
  361. PVOP_VCALL3(pv_mmu_ops.pmd_update_defer, mm, addr, pmdp);
  362. }
  363. static inline pte_t __pte(pteval_t val)
  364. {
  365. pteval_t ret;
  366. if (sizeof(pteval_t) > sizeof(long))
  367. ret = PVOP_CALLEE2(pteval_t,
  368. pv_mmu_ops.make_pte,
  369. val, (u64)val >> 32);
  370. else
  371. ret = PVOP_CALLEE1(pteval_t,
  372. pv_mmu_ops.make_pte,
  373. val);
  374. return (pte_t) { .pte = ret };
  375. }
  376. static inline pteval_t pte_val(pte_t pte)
  377. {
  378. pteval_t ret;
  379. if (sizeof(pteval_t) > sizeof(long))
  380. ret = PVOP_CALLEE2(pteval_t, pv_mmu_ops.pte_val,
  381. pte.pte, (u64)pte.pte >> 32);
  382. else
  383. ret = PVOP_CALLEE1(pteval_t, pv_mmu_ops.pte_val,
  384. pte.pte);
  385. return ret;
  386. }
  387. static inline pgd_t __pgd(pgdval_t val)
  388. {
  389. pgdval_t ret;
  390. if (sizeof(pgdval_t) > sizeof(long))
  391. ret = PVOP_CALLEE2(pgdval_t, pv_mmu_ops.make_pgd,
  392. val, (u64)val >> 32);
  393. else
  394. ret = PVOP_CALLEE1(pgdval_t, pv_mmu_ops.make_pgd,
  395. val);
  396. return (pgd_t) { ret };
  397. }
  398. static inline pgdval_t pgd_val(pgd_t pgd)
  399. {
  400. pgdval_t ret;
  401. if (sizeof(pgdval_t) > sizeof(long))
  402. ret = PVOP_CALLEE2(pgdval_t, pv_mmu_ops.pgd_val,
  403. pgd.pgd, (u64)pgd.pgd >> 32);
  404. else
  405. ret = PVOP_CALLEE1(pgdval_t, pv_mmu_ops.pgd_val,
  406. pgd.pgd);
  407. return ret;
  408. }
  409. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  410. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
  411. pte_t *ptep)
  412. {
  413. pteval_t ret;
  414. ret = PVOP_CALL3(pteval_t, pv_mmu_ops.ptep_modify_prot_start,
  415. mm, addr, ptep);
  416. return (pte_t) { .pte = ret };
  417. }
  418. static inline void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  419. pte_t *ptep, pte_t pte)
  420. {
  421. if (sizeof(pteval_t) > sizeof(long))
  422. /* 5 arg words */
  423. pv_mmu_ops.ptep_modify_prot_commit(mm, addr, ptep, pte);
  424. else
  425. PVOP_VCALL4(pv_mmu_ops.ptep_modify_prot_commit,
  426. mm, addr, ptep, pte.pte);
  427. }
  428. static inline void set_pte(pte_t *ptep, pte_t pte)
  429. {
  430. if (sizeof(pteval_t) > sizeof(long))
  431. PVOP_VCALL3(pv_mmu_ops.set_pte, ptep,
  432. pte.pte, (u64)pte.pte >> 32);
  433. else
  434. PVOP_VCALL2(pv_mmu_ops.set_pte, ptep,
  435. pte.pte);
  436. }
  437. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  438. pte_t *ptep, pte_t pte)
  439. {
  440. if (sizeof(pteval_t) > sizeof(long))
  441. /* 5 arg words */
  442. pv_mmu_ops.set_pte_at(mm, addr, ptep, pte);
  443. else
  444. PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pte.pte);
  445. }
  446. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  447. static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  448. pmd_t *pmdp, pmd_t pmd)
  449. {
  450. if (sizeof(pmdval_t) > sizeof(long))
  451. /* 5 arg words */
  452. pv_mmu_ops.set_pmd_at(mm, addr, pmdp, pmd);
  453. else
  454. PVOP_VCALL4(pv_mmu_ops.set_pmd_at, mm, addr, pmdp,
  455. native_pmd_val(pmd));
  456. }
  457. #endif
  458. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  459. {
  460. pmdval_t val = native_pmd_val(pmd);
  461. if (sizeof(pmdval_t) > sizeof(long))
  462. PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
  463. else
  464. PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
  465. }
  466. #if PAGETABLE_LEVELS >= 3
  467. static inline pmd_t __pmd(pmdval_t val)
  468. {
  469. pmdval_t ret;
  470. if (sizeof(pmdval_t) > sizeof(long))
  471. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.make_pmd,
  472. val, (u64)val >> 32);
  473. else
  474. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.make_pmd,
  475. val);
  476. return (pmd_t) { ret };
  477. }
  478. static inline pmdval_t pmd_val(pmd_t pmd)
  479. {
  480. pmdval_t ret;
  481. if (sizeof(pmdval_t) > sizeof(long))
  482. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.pmd_val,
  483. pmd.pmd, (u64)pmd.pmd >> 32);
  484. else
  485. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.pmd_val,
  486. pmd.pmd);
  487. return ret;
  488. }
  489. static inline void set_pud(pud_t *pudp, pud_t pud)
  490. {
  491. pudval_t val = native_pud_val(pud);
  492. if (sizeof(pudval_t) > sizeof(long))
  493. PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
  494. val, (u64)val >> 32);
  495. else
  496. PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
  497. val);
  498. }
  499. #if PAGETABLE_LEVELS == 4
  500. static inline pud_t __pud(pudval_t val)
  501. {
  502. pudval_t ret;
  503. if (sizeof(pudval_t) > sizeof(long))
  504. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.make_pud,
  505. val, (u64)val >> 32);
  506. else
  507. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.make_pud,
  508. val);
  509. return (pud_t) { ret };
  510. }
  511. static inline pudval_t pud_val(pud_t pud)
  512. {
  513. pudval_t ret;
  514. if (sizeof(pudval_t) > sizeof(long))
  515. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.pud_val,
  516. pud.pud, (u64)pud.pud >> 32);
  517. else
  518. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.pud_val,
  519. pud.pud);
  520. return ret;
  521. }
  522. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  523. {
  524. pgdval_t val = native_pgd_val(pgd);
  525. if (sizeof(pgdval_t) > sizeof(long))
  526. PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
  527. val, (u64)val >> 32);
  528. else
  529. PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
  530. val);
  531. }
  532. static inline void pgd_clear(pgd_t *pgdp)
  533. {
  534. set_pgd(pgdp, __pgd(0));
  535. }
  536. static inline void pud_clear(pud_t *pudp)
  537. {
  538. set_pud(pudp, __pud(0));
  539. }
  540. #endif /* PAGETABLE_LEVELS == 4 */
  541. #endif /* PAGETABLE_LEVELS >= 3 */
  542. #ifdef CONFIG_X86_PAE
  543. /* Special-case pte-setting operations for PAE, which can't update a
  544. 64-bit pte atomically */
  545. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  546. {
  547. PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
  548. pte.pte, pte.pte >> 32);
  549. }
  550. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  551. pte_t *ptep)
  552. {
  553. PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
  554. }
  555. static inline void pmd_clear(pmd_t *pmdp)
  556. {
  557. PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
  558. }
  559. #else /* !CONFIG_X86_PAE */
  560. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  561. {
  562. set_pte(ptep, pte);
  563. }
  564. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  565. pte_t *ptep)
  566. {
  567. set_pte_at(mm, addr, ptep, __pte(0));
  568. }
  569. static inline void pmd_clear(pmd_t *pmdp)
  570. {
  571. set_pmd(pmdp, __pmd(0));
  572. }
  573. #endif /* CONFIG_X86_PAE */
  574. #define __HAVE_ARCH_START_CONTEXT_SWITCH
  575. static inline void arch_start_context_switch(struct task_struct *prev)
  576. {
  577. PVOP_VCALL1(pv_cpu_ops.start_context_switch, prev);
  578. }
  579. static inline void arch_end_context_switch(struct task_struct *next)
  580. {
  581. PVOP_VCALL1(pv_cpu_ops.end_context_switch, next);
  582. }
  583. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  584. static inline void arch_enter_lazy_mmu_mode(void)
  585. {
  586. PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
  587. }
  588. static inline void arch_leave_lazy_mmu_mode(void)
  589. {
  590. PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
  591. }
  592. void arch_flush_lazy_mmu_mode(void);
  593. static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx,
  594. phys_addr_t phys, pgprot_t flags)
  595. {
  596. pv_mmu_ops.set_fixmap(idx, phys, flags);
  597. }
  598. #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  599. static inline int arch_spin_is_locked(struct arch_spinlock *lock)
  600. {
  601. return PVOP_CALL1(int, pv_lock_ops.spin_is_locked, lock);
  602. }
  603. static inline int arch_spin_is_contended(struct arch_spinlock *lock)
  604. {
  605. return PVOP_CALL1(int, pv_lock_ops.spin_is_contended, lock);
  606. }
  607. #define arch_spin_is_contended arch_spin_is_contended
  608. static __always_inline void arch_spin_lock(struct arch_spinlock *lock)
  609. {
  610. PVOP_VCALL1(pv_lock_ops.spin_lock, lock);
  611. }
  612. static __always_inline void arch_spin_lock_flags(struct arch_spinlock *lock,
  613. unsigned long flags)
  614. {
  615. PVOP_VCALL2(pv_lock_ops.spin_lock_flags, lock, flags);
  616. }
  617. static __always_inline int arch_spin_trylock(struct arch_spinlock *lock)
  618. {
  619. return PVOP_CALL1(int, pv_lock_ops.spin_trylock, lock);
  620. }
  621. static __always_inline void arch_spin_unlock(struct arch_spinlock *lock)
  622. {
  623. PVOP_VCALL1(pv_lock_ops.spin_unlock, lock);
  624. }
  625. #endif
  626. #ifdef CONFIG_X86_32
  627. #define PV_SAVE_REGS "pushl %ecx; pushl %edx;"
  628. #define PV_RESTORE_REGS "popl %edx; popl %ecx;"
  629. /* save and restore all caller-save registers, except return value */
  630. #define PV_SAVE_ALL_CALLER_REGS "pushl %ecx;"
  631. #define PV_RESTORE_ALL_CALLER_REGS "popl %ecx;"
  632. #define PV_FLAGS_ARG "0"
  633. #define PV_EXTRA_CLOBBERS
  634. #define PV_VEXTRA_CLOBBERS
  635. #else
  636. /* save and restore all caller-save registers, except return value */
  637. #define PV_SAVE_ALL_CALLER_REGS \
  638. "push %rcx;" \
  639. "push %rdx;" \
  640. "push %rsi;" \
  641. "push %rdi;" \
  642. "push %r8;" \
  643. "push %r9;" \
  644. "push %r10;" \
  645. "push %r11;"
  646. #define PV_RESTORE_ALL_CALLER_REGS \
  647. "pop %r11;" \
  648. "pop %r10;" \
  649. "pop %r9;" \
  650. "pop %r8;" \
  651. "pop %rdi;" \
  652. "pop %rsi;" \
  653. "pop %rdx;" \
  654. "pop %rcx;"
  655. /* We save some registers, but all of them, that's too much. We clobber all
  656. * caller saved registers but the argument parameter */
  657. #define PV_SAVE_REGS "pushq %%rdi;"
  658. #define PV_RESTORE_REGS "popq %%rdi;"
  659. #define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx", "rsi"
  660. #define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx", "rsi"
  661. #define PV_FLAGS_ARG "D"
  662. #endif
  663. /*
  664. * Generate a thunk around a function which saves all caller-save
  665. * registers except for the return value. This allows C functions to
  666. * be called from assembler code where fewer than normal registers are
  667. * available. It may also help code generation around calls from C
  668. * code if the common case doesn't use many registers.
  669. *
  670. * When a callee is wrapped in a thunk, the caller can assume that all
  671. * arg regs and all scratch registers are preserved across the
  672. * call. The return value in rax/eax will not be saved, even for void
  673. * functions.
  674. */
  675. #define PV_CALLEE_SAVE_REGS_THUNK(func) \
  676. extern typeof(func) __raw_callee_save_##func; \
  677. static void *__##func##__ __used = func; \
  678. \
  679. asm(".pushsection .text;" \
  680. "__raw_callee_save_" #func ": " \
  681. PV_SAVE_ALL_CALLER_REGS \
  682. "call " #func ";" \
  683. PV_RESTORE_ALL_CALLER_REGS \
  684. "ret;" \
  685. ".popsection")
  686. /* Get a reference to a callee-save function */
  687. #define PV_CALLEE_SAVE(func) \
  688. ((struct paravirt_callee_save) { __raw_callee_save_##func })
  689. /* Promise that "func" already uses the right calling convention */
  690. #define __PV_IS_CALLEE_SAVE(func) \
  691. ((struct paravirt_callee_save) { func })
  692. static inline notrace unsigned long arch_local_save_flags(void)
  693. {
  694. return PVOP_CALLEE0(unsigned long, pv_irq_ops.save_fl);
  695. }
  696. static inline notrace void arch_local_irq_restore(unsigned long f)
  697. {
  698. PVOP_VCALLEE1(pv_irq_ops.restore_fl, f);
  699. }
  700. static inline notrace void arch_local_irq_disable(void)
  701. {
  702. PVOP_VCALLEE0(pv_irq_ops.irq_disable);
  703. }
  704. static inline notrace void arch_local_irq_enable(void)
  705. {
  706. PVOP_VCALLEE0(pv_irq_ops.irq_enable);
  707. }
  708. static inline notrace unsigned long arch_local_irq_save(void)
  709. {
  710. unsigned long f;
  711. f = arch_local_save_flags();
  712. arch_local_irq_disable();
  713. return f;
  714. }
  715. /* Make sure as little as possible of this mess escapes. */
  716. #undef PARAVIRT_CALL
  717. #undef __PVOP_CALL
  718. #undef __PVOP_VCALL
  719. #undef PVOP_VCALL0
  720. #undef PVOP_CALL0
  721. #undef PVOP_VCALL1
  722. #undef PVOP_CALL1
  723. #undef PVOP_VCALL2
  724. #undef PVOP_CALL2
  725. #undef PVOP_VCALL3
  726. #undef PVOP_CALL3
  727. #undef PVOP_VCALL4
  728. #undef PVOP_CALL4
  729. extern void default_banner(void);
  730. #else /* __ASSEMBLY__ */
  731. #define _PVSITE(ptype, clobbers, ops, word, algn) \
  732. 771:; \
  733. ops; \
  734. 772:; \
  735. .pushsection .parainstructions,"a"; \
  736. .align algn; \
  737. word 771b; \
  738. .byte ptype; \
  739. .byte 772b-771b; \
  740. .short clobbers; \
  741. .popsection
  742. #define COND_PUSH(set, mask, reg) \
  743. .if ((~(set)) & mask); push %reg; .endif
  744. #define COND_POP(set, mask, reg) \
  745. .if ((~(set)) & mask); pop %reg; .endif
  746. #ifdef CONFIG_X86_64
  747. #define PV_SAVE_REGS(set) \
  748. COND_PUSH(set, CLBR_RAX, rax); \
  749. COND_PUSH(set, CLBR_RCX, rcx); \
  750. COND_PUSH(set, CLBR_RDX, rdx); \
  751. COND_PUSH(set, CLBR_RSI, rsi); \
  752. COND_PUSH(set, CLBR_RDI, rdi); \
  753. COND_PUSH(set, CLBR_R8, r8); \
  754. COND_PUSH(set, CLBR_R9, r9); \
  755. COND_PUSH(set, CLBR_R10, r10); \
  756. COND_PUSH(set, CLBR_R11, r11)
  757. #define PV_RESTORE_REGS(set) \
  758. COND_POP(set, CLBR_R11, r11); \
  759. COND_POP(set, CLBR_R10, r10); \
  760. COND_POP(set, CLBR_R9, r9); \
  761. COND_POP(set, CLBR_R8, r8); \
  762. COND_POP(set, CLBR_RDI, rdi); \
  763. COND_POP(set, CLBR_RSI, rsi); \
  764. COND_POP(set, CLBR_RDX, rdx); \
  765. COND_POP(set, CLBR_RCX, rcx); \
  766. COND_POP(set, CLBR_RAX, rax)
  767. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
  768. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
  769. #define PARA_INDIRECT(addr) *addr(%rip)
  770. #else
  771. #define PV_SAVE_REGS(set) \
  772. COND_PUSH(set, CLBR_EAX, eax); \
  773. COND_PUSH(set, CLBR_EDI, edi); \
  774. COND_PUSH(set, CLBR_ECX, ecx); \
  775. COND_PUSH(set, CLBR_EDX, edx)
  776. #define PV_RESTORE_REGS(set) \
  777. COND_POP(set, CLBR_EDX, edx); \
  778. COND_POP(set, CLBR_ECX, ecx); \
  779. COND_POP(set, CLBR_EDI, edi); \
  780. COND_POP(set, CLBR_EAX, eax)
  781. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
  782. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
  783. #define PARA_INDIRECT(addr) *%cs:addr
  784. #endif
  785. #define INTERRUPT_RETURN \
  786. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
  787. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_iret))
  788. #define DISABLE_INTERRUPTS(clobbers) \
  789. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
  790. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  791. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_disable); \
  792. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  793. #define ENABLE_INTERRUPTS(clobbers) \
  794. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
  795. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  796. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable); \
  797. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  798. #define USERGS_SYSRET32 \
  799. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32), \
  800. CLBR_NONE, \
  801. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
  802. #ifdef CONFIG_X86_32
  803. #define GET_CR0_INTO_EAX \
  804. push %ecx; push %edx; \
  805. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0); \
  806. pop %edx; pop %ecx
  807. #define ENABLE_INTERRUPTS_SYSEXIT \
  808. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  809. CLBR_NONE, \
  810. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  811. #else /* !CONFIG_X86_32 */
  812. /*
  813. * If swapgs is used while the userspace stack is still current,
  814. * there's no way to call a pvop. The PV replacement *must* be
  815. * inlined, or the swapgs instruction must be trapped and emulated.
  816. */
  817. #define SWAPGS_UNSAFE_STACK \
  818. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  819. swapgs)
  820. /*
  821. * Note: swapgs is very special, and in practise is either going to be
  822. * implemented with a single "swapgs" instruction or something very
  823. * special. Either way, we don't need to save any registers for
  824. * it.
  825. */
  826. #define SWAPGS \
  827. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  828. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_swapgs) \
  829. )
  830. #define GET_CR2_INTO_RAX \
  831. call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2)
  832. #define PARAVIRT_ADJUST_EXCEPTION_FRAME \
  833. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_adjust_exception_frame), \
  834. CLBR_NONE, \
  835. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_adjust_exception_frame))
  836. #define USERGS_SYSRET64 \
  837. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
  838. CLBR_NONE, \
  839. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
  840. #define ENABLE_INTERRUPTS_SYSEXIT32 \
  841. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  842. CLBR_NONE, \
  843. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  844. #endif /* CONFIG_X86_32 */
  845. #endif /* __ASSEMBLY__ */
  846. #else /* CONFIG_PARAVIRT */
  847. # define default_banner x86_init_noop
  848. #endif /* !CONFIG_PARAVIRT */
  849. #endif /* _ASM_X86_PARAVIRT_H */