paravirt.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  447. pmd_t *pmdp, pmd_t pmd)
  448. {
  449. if (sizeof(pmdval_t) > sizeof(long))
  450. /* 5 arg words */
  451. pv_mmu_ops.set_pmd_at(mm, addr, pmdp, pmd);
  452. else
  453. PVOP_VCALL4(pv_mmu_ops.set_pmd_at, mm, addr, pmdp,
  454. native_pmd_val(pmd));
  455. }
  456. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  457. {
  458. pmdval_t val = native_pmd_val(pmd);
  459. if (sizeof(pmdval_t) > sizeof(long))
  460. PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
  461. else
  462. PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
  463. }
  464. #if PAGETABLE_LEVELS >= 3
  465. static inline pmd_t __pmd(pmdval_t val)
  466. {
  467. pmdval_t ret;
  468. if (sizeof(pmdval_t) > sizeof(long))
  469. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.make_pmd,
  470. val, (u64)val >> 32);
  471. else
  472. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.make_pmd,
  473. val);
  474. return (pmd_t) { ret };
  475. }
  476. static inline pmdval_t pmd_val(pmd_t pmd)
  477. {
  478. pmdval_t ret;
  479. if (sizeof(pmdval_t) > sizeof(long))
  480. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.pmd_val,
  481. pmd.pmd, (u64)pmd.pmd >> 32);
  482. else
  483. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.pmd_val,
  484. pmd.pmd);
  485. return ret;
  486. }
  487. static inline void set_pud(pud_t *pudp, pud_t pud)
  488. {
  489. pudval_t val = native_pud_val(pud);
  490. if (sizeof(pudval_t) > sizeof(long))
  491. PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
  492. val, (u64)val >> 32);
  493. else
  494. PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
  495. val);
  496. }
  497. #if PAGETABLE_LEVELS == 4
  498. static inline pud_t __pud(pudval_t val)
  499. {
  500. pudval_t ret;
  501. if (sizeof(pudval_t) > sizeof(long))
  502. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.make_pud,
  503. val, (u64)val >> 32);
  504. else
  505. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.make_pud,
  506. val);
  507. return (pud_t) { ret };
  508. }
  509. static inline pudval_t pud_val(pud_t pud)
  510. {
  511. pudval_t ret;
  512. if (sizeof(pudval_t) > sizeof(long))
  513. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.pud_val,
  514. pud.pud, (u64)pud.pud >> 32);
  515. else
  516. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.pud_val,
  517. pud.pud);
  518. return ret;
  519. }
  520. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  521. {
  522. pgdval_t val = native_pgd_val(pgd);
  523. if (sizeof(pgdval_t) > sizeof(long))
  524. PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
  525. val, (u64)val >> 32);
  526. else
  527. PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
  528. val);
  529. }
  530. static inline void pgd_clear(pgd_t *pgdp)
  531. {
  532. set_pgd(pgdp, __pgd(0));
  533. }
  534. static inline void pud_clear(pud_t *pudp)
  535. {
  536. set_pud(pudp, __pud(0));
  537. }
  538. #endif /* PAGETABLE_LEVELS == 4 */
  539. #endif /* PAGETABLE_LEVELS >= 3 */
  540. #ifdef CONFIG_X86_PAE
  541. /* Special-case pte-setting operations for PAE, which can't update a
  542. 64-bit pte atomically */
  543. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  544. {
  545. PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
  546. pte.pte, pte.pte >> 32);
  547. }
  548. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  549. pte_t *ptep)
  550. {
  551. PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
  552. }
  553. static inline void pmd_clear(pmd_t *pmdp)
  554. {
  555. PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
  556. }
  557. #else /* !CONFIG_X86_PAE */
  558. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  559. {
  560. set_pte(ptep, pte);
  561. }
  562. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  563. pte_t *ptep)
  564. {
  565. set_pte_at(mm, addr, ptep, __pte(0));
  566. }
  567. static inline void pmd_clear(pmd_t *pmdp)
  568. {
  569. set_pmd(pmdp, __pmd(0));
  570. }
  571. #endif /* CONFIG_X86_PAE */
  572. #define __HAVE_ARCH_START_CONTEXT_SWITCH
  573. static inline void arch_start_context_switch(struct task_struct *prev)
  574. {
  575. PVOP_VCALL1(pv_cpu_ops.start_context_switch, prev);
  576. }
  577. static inline void arch_end_context_switch(struct task_struct *next)
  578. {
  579. PVOP_VCALL1(pv_cpu_ops.end_context_switch, next);
  580. }
  581. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  582. static inline void arch_enter_lazy_mmu_mode(void)
  583. {
  584. PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
  585. }
  586. static inline void arch_leave_lazy_mmu_mode(void)
  587. {
  588. PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
  589. }
  590. void arch_flush_lazy_mmu_mode(void);
  591. static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx,
  592. phys_addr_t phys, pgprot_t flags)
  593. {
  594. pv_mmu_ops.set_fixmap(idx, phys, flags);
  595. }
  596. #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  597. static inline int arch_spin_is_locked(struct arch_spinlock *lock)
  598. {
  599. return PVOP_CALL1(int, pv_lock_ops.spin_is_locked, lock);
  600. }
  601. static inline int arch_spin_is_contended(struct arch_spinlock *lock)
  602. {
  603. return PVOP_CALL1(int, pv_lock_ops.spin_is_contended, lock);
  604. }
  605. #define arch_spin_is_contended arch_spin_is_contended
  606. static __always_inline void arch_spin_lock(struct arch_spinlock *lock)
  607. {
  608. PVOP_VCALL1(pv_lock_ops.spin_lock, lock);
  609. }
  610. static __always_inline void arch_spin_lock_flags(struct arch_spinlock *lock,
  611. unsigned long flags)
  612. {
  613. PVOP_VCALL2(pv_lock_ops.spin_lock_flags, lock, flags);
  614. }
  615. static __always_inline int arch_spin_trylock(struct arch_spinlock *lock)
  616. {
  617. return PVOP_CALL1(int, pv_lock_ops.spin_trylock, lock);
  618. }
  619. static __always_inline void arch_spin_unlock(struct arch_spinlock *lock)
  620. {
  621. PVOP_VCALL1(pv_lock_ops.spin_unlock, lock);
  622. }
  623. #endif
  624. #ifdef CONFIG_X86_32
  625. #define PV_SAVE_REGS "pushl %ecx; pushl %edx;"
  626. #define PV_RESTORE_REGS "popl %edx; popl %ecx;"
  627. /* save and restore all caller-save registers, except return value */
  628. #define PV_SAVE_ALL_CALLER_REGS "pushl %ecx;"
  629. #define PV_RESTORE_ALL_CALLER_REGS "popl %ecx;"
  630. #define PV_FLAGS_ARG "0"
  631. #define PV_EXTRA_CLOBBERS
  632. #define PV_VEXTRA_CLOBBERS
  633. #else
  634. /* save and restore all caller-save registers, except return value */
  635. #define PV_SAVE_ALL_CALLER_REGS \
  636. "push %rcx;" \
  637. "push %rdx;" \
  638. "push %rsi;" \
  639. "push %rdi;" \
  640. "push %r8;" \
  641. "push %r9;" \
  642. "push %r10;" \
  643. "push %r11;"
  644. #define PV_RESTORE_ALL_CALLER_REGS \
  645. "pop %r11;" \
  646. "pop %r10;" \
  647. "pop %r9;" \
  648. "pop %r8;" \
  649. "pop %rdi;" \
  650. "pop %rsi;" \
  651. "pop %rdx;" \
  652. "pop %rcx;"
  653. /* We save some registers, but all of them, that's too much. We clobber all
  654. * caller saved registers but the argument parameter */
  655. #define PV_SAVE_REGS "pushq %%rdi;"
  656. #define PV_RESTORE_REGS "popq %%rdi;"
  657. #define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx", "rsi"
  658. #define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx", "rsi"
  659. #define PV_FLAGS_ARG "D"
  660. #endif
  661. /*
  662. * Generate a thunk around a function which saves all caller-save
  663. * registers except for the return value. This allows C functions to
  664. * be called from assembler code where fewer than normal registers are
  665. * available. It may also help code generation around calls from C
  666. * code if the common case doesn't use many registers.
  667. *
  668. * When a callee is wrapped in a thunk, the caller can assume that all
  669. * arg regs and all scratch registers are preserved across the
  670. * call. The return value in rax/eax will not be saved, even for void
  671. * functions.
  672. */
  673. #define PV_CALLEE_SAVE_REGS_THUNK(func) \
  674. extern typeof(func) __raw_callee_save_##func; \
  675. static void *__##func##__ __used = func; \
  676. \
  677. asm(".pushsection .text;" \
  678. "__raw_callee_save_" #func ": " \
  679. PV_SAVE_ALL_CALLER_REGS \
  680. "call " #func ";" \
  681. PV_RESTORE_ALL_CALLER_REGS \
  682. "ret;" \
  683. ".popsection")
  684. /* Get a reference to a callee-save function */
  685. #define PV_CALLEE_SAVE(func) \
  686. ((struct paravirt_callee_save) { __raw_callee_save_##func })
  687. /* Promise that "func" already uses the right calling convention */
  688. #define __PV_IS_CALLEE_SAVE(func) \
  689. ((struct paravirt_callee_save) { func })
  690. static inline notrace unsigned long arch_local_save_flags(void)
  691. {
  692. return PVOP_CALLEE0(unsigned long, pv_irq_ops.save_fl);
  693. }
  694. static inline notrace void arch_local_irq_restore(unsigned long f)
  695. {
  696. PVOP_VCALLEE1(pv_irq_ops.restore_fl, f);
  697. }
  698. static inline notrace void arch_local_irq_disable(void)
  699. {
  700. PVOP_VCALLEE0(pv_irq_ops.irq_disable);
  701. }
  702. static inline notrace void arch_local_irq_enable(void)
  703. {
  704. PVOP_VCALLEE0(pv_irq_ops.irq_enable);
  705. }
  706. static inline notrace unsigned long arch_local_irq_save(void)
  707. {
  708. unsigned long f;
  709. f = arch_local_save_flags();
  710. arch_local_irq_disable();
  711. return f;
  712. }
  713. /* Make sure as little as possible of this mess escapes. */
  714. #undef PARAVIRT_CALL
  715. #undef __PVOP_CALL
  716. #undef __PVOP_VCALL
  717. #undef PVOP_VCALL0
  718. #undef PVOP_CALL0
  719. #undef PVOP_VCALL1
  720. #undef PVOP_CALL1
  721. #undef PVOP_VCALL2
  722. #undef PVOP_CALL2
  723. #undef PVOP_VCALL3
  724. #undef PVOP_CALL3
  725. #undef PVOP_VCALL4
  726. #undef PVOP_CALL4
  727. extern void default_banner(void);
  728. #else /* __ASSEMBLY__ */
  729. #define _PVSITE(ptype, clobbers, ops, word, algn) \
  730. 771:; \
  731. ops; \
  732. 772:; \
  733. .pushsection .parainstructions,"a"; \
  734. .align algn; \
  735. word 771b; \
  736. .byte ptype; \
  737. .byte 772b-771b; \
  738. .short clobbers; \
  739. .popsection
  740. #define COND_PUSH(set, mask, reg) \
  741. .if ((~(set)) & mask); push %reg; .endif
  742. #define COND_POP(set, mask, reg) \
  743. .if ((~(set)) & mask); pop %reg; .endif
  744. #ifdef CONFIG_X86_64
  745. #define PV_SAVE_REGS(set) \
  746. COND_PUSH(set, CLBR_RAX, rax); \
  747. COND_PUSH(set, CLBR_RCX, rcx); \
  748. COND_PUSH(set, CLBR_RDX, rdx); \
  749. COND_PUSH(set, CLBR_RSI, rsi); \
  750. COND_PUSH(set, CLBR_RDI, rdi); \
  751. COND_PUSH(set, CLBR_R8, r8); \
  752. COND_PUSH(set, CLBR_R9, r9); \
  753. COND_PUSH(set, CLBR_R10, r10); \
  754. COND_PUSH(set, CLBR_R11, r11)
  755. #define PV_RESTORE_REGS(set) \
  756. COND_POP(set, CLBR_R11, r11); \
  757. COND_POP(set, CLBR_R10, r10); \
  758. COND_POP(set, CLBR_R9, r9); \
  759. COND_POP(set, CLBR_R8, r8); \
  760. COND_POP(set, CLBR_RDI, rdi); \
  761. COND_POP(set, CLBR_RSI, rsi); \
  762. COND_POP(set, CLBR_RDX, rdx); \
  763. COND_POP(set, CLBR_RCX, rcx); \
  764. COND_POP(set, CLBR_RAX, rax)
  765. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
  766. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
  767. #define PARA_INDIRECT(addr) *addr(%rip)
  768. #else
  769. #define PV_SAVE_REGS(set) \
  770. COND_PUSH(set, CLBR_EAX, eax); \
  771. COND_PUSH(set, CLBR_EDI, edi); \
  772. COND_PUSH(set, CLBR_ECX, ecx); \
  773. COND_PUSH(set, CLBR_EDX, edx)
  774. #define PV_RESTORE_REGS(set) \
  775. COND_POP(set, CLBR_EDX, edx); \
  776. COND_POP(set, CLBR_ECX, ecx); \
  777. COND_POP(set, CLBR_EDI, edi); \
  778. COND_POP(set, CLBR_EAX, eax)
  779. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
  780. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
  781. #define PARA_INDIRECT(addr) *%cs:addr
  782. #endif
  783. #define INTERRUPT_RETURN \
  784. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
  785. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_iret))
  786. #define DISABLE_INTERRUPTS(clobbers) \
  787. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
  788. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  789. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_disable); \
  790. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  791. #define ENABLE_INTERRUPTS(clobbers) \
  792. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
  793. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  794. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable); \
  795. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  796. #define USERGS_SYSRET32 \
  797. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32), \
  798. CLBR_NONE, \
  799. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
  800. #ifdef CONFIG_X86_32
  801. #define GET_CR0_INTO_EAX \
  802. push %ecx; push %edx; \
  803. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0); \
  804. pop %edx; pop %ecx
  805. #define ENABLE_INTERRUPTS_SYSEXIT \
  806. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  807. CLBR_NONE, \
  808. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  809. #else /* !CONFIG_X86_32 */
  810. /*
  811. * If swapgs is used while the userspace stack is still current,
  812. * there's no way to call a pvop. The PV replacement *must* be
  813. * inlined, or the swapgs instruction must be trapped and emulated.
  814. */
  815. #define SWAPGS_UNSAFE_STACK \
  816. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  817. swapgs)
  818. /*
  819. * Note: swapgs is very special, and in practise is either going to be
  820. * implemented with a single "swapgs" instruction or something very
  821. * special. Either way, we don't need to save any registers for
  822. * it.
  823. */
  824. #define SWAPGS \
  825. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  826. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_swapgs) \
  827. )
  828. #define GET_CR2_INTO_RAX \
  829. call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2)
  830. #define PARAVIRT_ADJUST_EXCEPTION_FRAME \
  831. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_adjust_exception_frame), \
  832. CLBR_NONE, \
  833. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_adjust_exception_frame))
  834. #define USERGS_SYSRET64 \
  835. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
  836. CLBR_NONE, \
  837. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
  838. #define ENABLE_INTERRUPTS_SYSEXIT32 \
  839. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  840. CLBR_NONE, \
  841. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  842. #endif /* CONFIG_X86_32 */
  843. #endif /* __ASSEMBLY__ */
  844. #else /* CONFIG_PARAVIRT */
  845. # define default_banner x86_init_noop
  846. #endif /* !CONFIG_PARAVIRT */
  847. #endif /* _ASM_X86_PARAVIRT_H */