kvm_pgtable.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Google LLC
  4. * Author: Will Deacon <[email protected]>
  5. */
  6. #ifndef __ARM64_KVM_PGTABLE_H__
  7. #define __ARM64_KVM_PGTABLE_H__
  8. #include <linux/bits.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/types.h>
  11. #define KVM_PGTABLE_MAX_LEVELS 4U
  12. /*
  13. * The largest supported block sizes for KVM (no 52-bit PA support):
  14. * - 4K (level 1): 1GB
  15. * - 16K (level 2): 32MB
  16. * - 64K (level 2): 512MB
  17. */
  18. #ifdef CONFIG_ARM64_4K_PAGES
  19. #define KVM_PGTABLE_MIN_BLOCK_LEVEL 1U
  20. #else
  21. #define KVM_PGTABLE_MIN_BLOCK_LEVEL 2U
  22. #endif
  23. static inline u64 kvm_get_parange(u64 mmfr0)
  24. {
  25. u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
  26. ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  27. if (parange > ID_AA64MMFR0_EL1_PARANGE_MAX)
  28. parange = ID_AA64MMFR0_EL1_PARANGE_MAX;
  29. return parange;
  30. }
  31. typedef u64 kvm_pte_t;
  32. #define KVM_PTE_VALID BIT(0)
  33. #define KVM_PTE_ADDR_MASK GENMASK(47, PAGE_SHIFT)
  34. #define KVM_PTE_ADDR_51_48 GENMASK(15, 12)
  35. #define KVM_PHYS_INVALID (-1ULL)
  36. #define KVM_PTE_TYPE BIT(1)
  37. #define KVM_PTE_TYPE_BLOCK 0
  38. #define KVM_PTE_TYPE_PAGE 1
  39. #define KVM_PTE_TYPE_TABLE 1
  40. #define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2)
  41. #define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2)
  42. #define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6)
  43. #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO 3
  44. #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW 1
  45. #define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8)
  46. #define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3
  47. #define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10)
  48. #define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2)
  49. #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6)
  50. #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7)
  51. #define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8)
  52. #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3
  53. #define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10)
  54. #define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 51)
  55. #define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55)
  56. #define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54)
  57. #define KVM_PTE_LEAF_ATTR_HI_S2_XN_PXN 1
  58. #define KVM_PTE_LEAF_ATTR_HI_S2_XN_UXN 3
  59. #define KVM_PTE_LEAF_ATTR_HI_S2_XN_XN 2
  60. #define KVM_PTE_LEAF_ATTR_HI_S2_XN GENMASK(54, 53)
  61. static inline bool kvm_pte_valid(kvm_pte_t pte)
  62. {
  63. return pte & KVM_PTE_VALID;
  64. }
  65. static inline u64 kvm_pte_to_phys(kvm_pte_t pte)
  66. {
  67. u64 pa = pte & KVM_PTE_ADDR_MASK;
  68. if (PAGE_SHIFT == 16)
  69. pa |= FIELD_GET(KVM_PTE_ADDR_51_48, pte) << 48;
  70. return pa;
  71. }
  72. static inline kvm_pte_t kvm_phys_to_pte(u64 pa)
  73. {
  74. kvm_pte_t pte = pa & KVM_PTE_ADDR_MASK;
  75. if (PAGE_SHIFT == 16) {
  76. pa &= GENMASK(51, 48);
  77. pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48);
  78. }
  79. return pte;
  80. }
  81. static inline u64 kvm_granule_shift(u32 level)
  82. {
  83. /* Assumes KVM_PGTABLE_MAX_LEVELS is 4 */
  84. return ARM64_HW_PGTABLE_LEVEL_SHIFT(level);
  85. }
  86. static inline u64 kvm_granule_size(u32 level)
  87. {
  88. return BIT(kvm_granule_shift(level));
  89. }
  90. static inline bool kvm_level_supports_block_mapping(u32 level)
  91. {
  92. return level >= KVM_PGTABLE_MIN_BLOCK_LEVEL;
  93. }
  94. static inline bool kvm_pte_table(kvm_pte_t pte, u32 level)
  95. {
  96. if (level == KVM_PGTABLE_MAX_LEVELS - 1)
  97. return false;
  98. if (!kvm_pte_valid(pte))
  99. return false;
  100. return FIELD_GET(KVM_PTE_TYPE, pte) == KVM_PTE_TYPE_TABLE;
  101. }
  102. /**
  103. * struct kvm_pgtable_mm_ops - Memory management callbacks.
  104. * @zalloc_page: Allocate a single zeroed memory page.
  105. * The @arg parameter can be used by the walker
  106. * to pass a memcache. The initial refcount of
  107. * the page is 1.
  108. * @zalloc_pages_exact: Allocate an exact number of zeroed memory pages.
  109. * The @size parameter is in bytes, and is rounded
  110. * up to the next page boundary. The resulting
  111. * allocation is physically contiguous.
  112. * @free_pages_exact: Free an exact number of memory pages previously
  113. * allocated by zalloc_pages_exact.
  114. * @get_page: Increment the refcount on a page.
  115. * @put_page: Decrement the refcount on a page. When the
  116. * refcount reaches 0 the page is automatically
  117. * freed.
  118. * @page_count: Return the refcount of a page.
  119. * @phys_to_virt: Convert a physical address into a virtual
  120. * address mapped in the current context.
  121. * @virt_to_phys: Convert a virtual address mapped in the current
  122. * context into a physical address.
  123. * @dcache_clean_inval_poc: Clean and invalidate the data cache to the PoC
  124. * for the specified memory address range.
  125. * @icache_inval_pou: Invalidate the instruction cache to the PoU
  126. * for the specified memory address range.
  127. */
  128. struct kvm_pgtable_mm_ops {
  129. void* (*zalloc_page)(void *arg);
  130. void* (*zalloc_pages_exact)(size_t size);
  131. void (*free_pages_exact)(void *addr, size_t size);
  132. void (*get_page)(void *addr);
  133. void (*put_page)(void *addr);
  134. int (*page_count)(void *addr);
  135. void* (*phys_to_virt)(phys_addr_t phys);
  136. phys_addr_t (*virt_to_phys)(void *addr);
  137. void (*dcache_clean_inval_poc)(void *addr, size_t size);
  138. void (*icache_inval_pou)(void *addr, size_t size);
  139. };
  140. static inline kvm_pte_t *kvm_pte_follow(kvm_pte_t pte, struct kvm_pgtable_mm_ops *mm_ops)
  141. {
  142. return mm_ops->phys_to_virt(kvm_pte_to_phys(pte));
  143. }
  144. /**
  145. * enum kvm_pgtable_stage2_flags - Stage-2 page-table flags.
  146. * @KVM_PGTABLE_S2_NOFWB: Don't enforce Normal-WB even if the CPUs have
  147. * ARM64_HAS_STAGE2_FWB.
  148. * @KVM_PGTABLE_S2_IDMAP: Only use identity mappings.
  149. */
  150. enum kvm_pgtable_stage2_flags {
  151. KVM_PGTABLE_S2_NOFWB = BIT(0),
  152. KVM_PGTABLE_S2_IDMAP = BIT(1),
  153. };
  154. /**
  155. * enum kvm_pgtable_prot - Page-table permissions and attributes.
  156. * @KVM_PGTABLE_PROT_X: Execute permission.
  157. * @KVM_PGTABLE_PROT_W: Write permission.
  158. * @KVM_PGTABLE_PROT_R: Read permission.
  159. * @KVM_PGTABLE_PROT_DEVICE: Device attributes.
  160. * @KVM_PGTABLE_PROT_NC: Normal non-cacheable attributes.
  161. * @KVM_PGTABLE_PROT_PXN: Privileged execute-never.
  162. * @KVM_PGTABLE_PROT_UXN: Unprivileged execute-never.
  163. * @KVM_PGTABLE_PROT_SW0: Software bit 0.
  164. * @KVM_PGTABLE_PROT_SW1: Software bit 1.
  165. * @KVM_PGTABLE_PROT_SW2: Software bit 2.
  166. * @KVM_PGTABLE_PROT_SW3: Software bit 3.
  167. */
  168. enum kvm_pgtable_prot {
  169. KVM_PGTABLE_PROT_X = BIT(0),
  170. KVM_PGTABLE_PROT_W = BIT(1),
  171. KVM_PGTABLE_PROT_R = BIT(2),
  172. KVM_PGTABLE_PROT_DEVICE = BIT(3),
  173. KVM_PGTABLE_PROT_NC = BIT(4),
  174. KVM_PGTABLE_PROT_PXN = BIT(5),
  175. KVM_PGTABLE_PROT_UXN = BIT(6),
  176. KVM_PGTABLE_PROT_SW0 = BIT(55),
  177. KVM_PGTABLE_PROT_SW1 = BIT(56),
  178. KVM_PGTABLE_PROT_SW2 = BIT(57),
  179. KVM_PGTABLE_PROT_SW3 = BIT(58),
  180. };
  181. #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W)
  182. #define KVM_PGTABLE_PROT_RWX (KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X)
  183. #define PKVM_HOST_MEM_PROT KVM_PGTABLE_PROT_RWX
  184. #define PKVM_HOST_MMIO_PROT KVM_PGTABLE_PROT_RW
  185. #define KVM_HOST_S2_DEFAULT_MASK (KVM_PTE_LEAF_ATTR_HI | \
  186. KVM_PTE_LEAF_ATTR_LO)
  187. #define KVM_HOST_S2_DEFAULT_MEM_PTE \
  188. (PTE_S2_MEMATTR(MT_S2_NORMAL) | \
  189. KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
  190. KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
  191. KVM_PTE_LEAF_ATTR_LO_S2_AF | \
  192. FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, KVM_PTE_LEAF_ATTR_LO_S2_SH_IS))
  193. #define KVM_HOST_S2_DEFAULT_MMIO_PTE \
  194. (KVM_HOST_S2_DEFAULT_MEM_PTE | \
  195. FIELD_PREP(KVM_PTE_LEAF_ATTR_HI_S2_XN, KVM_PTE_LEAF_ATTR_HI_S2_XN_XN))
  196. #define PAGE_HYP KVM_PGTABLE_PROT_RW
  197. #define PAGE_HYP_EXEC (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_X)
  198. #define PAGE_HYP_RO (KVM_PGTABLE_PROT_R)
  199. #define PAGE_HYP_DEVICE (PAGE_HYP | KVM_PGTABLE_PROT_DEVICE)
  200. typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,
  201. enum kvm_pgtable_prot prot);
  202. typedef bool (*kvm_pgtable_pte_is_counted_cb_t)(kvm_pte_t pte, u32 level);
  203. /**
  204. * struct kvm_pgtable_pte_ops - PTE callbacks.
  205. * @force_pte_cb: Force the mapping granularity to pages and
  206. * return true if we support this instead of
  207. * block mappings.
  208. * @pte_is_counted_cb Verify the attributes of the @pte argument
  209. * and return true if the descriptor needs to be
  210. * refcounted, otherwise return false.
  211. */
  212. struct kvm_pgtable_pte_ops {
  213. kvm_pgtable_force_pte_cb_t force_pte_cb;
  214. kvm_pgtable_pte_is_counted_cb_t pte_is_counted_cb;
  215. };
  216. /**
  217. * struct kvm_pgtable - KVM page-table.
  218. * @ia_bits: Maximum input address size, in bits.
  219. * @start_level: Level at which the page-table walk starts.
  220. * @pgd: Pointer to the first top-level entry of the page-table.
  221. * @mm_ops: Memory management callbacks.
  222. * @mmu: Stage-2 KVM MMU struct. Unused for stage-1 page-tables.
  223. * @flags: Stage-2 page-table flags.
  224. * @pte_ops: PTE callbacks.
  225. */
  226. struct kvm_pgtable {
  227. u32 ia_bits;
  228. u32 start_level;
  229. kvm_pte_t *pgd;
  230. struct kvm_pgtable_mm_ops *mm_ops;
  231. /* Stage-2 only */
  232. struct kvm_s2_mmu *mmu;
  233. enum kvm_pgtable_stage2_flags flags;
  234. struct kvm_pgtable_pte_ops *pte_ops;
  235. };
  236. /**
  237. * enum kvm_pgtable_walk_flags - Flags to control a depth-first page-table walk.
  238. * @KVM_PGTABLE_WALK_LEAF: Visit leaf entries, including invalid
  239. * entries.
  240. * @KVM_PGTABLE_WALK_TABLE_PRE: Visit table entries before their
  241. * children.
  242. * @KVM_PGTABLE_WALK_TABLE_POST: Visit table entries after their
  243. * children.
  244. */
  245. enum kvm_pgtable_walk_flags {
  246. KVM_PGTABLE_WALK_LEAF = BIT(0),
  247. KVM_PGTABLE_WALK_TABLE_PRE = BIT(1),
  248. KVM_PGTABLE_WALK_TABLE_POST = BIT(2),
  249. };
  250. typedef int (*kvm_pgtable_visitor_fn_t)(u64 addr, u64 end, u32 level,
  251. kvm_pte_t *ptep,
  252. enum kvm_pgtable_walk_flags flag,
  253. void * const arg);
  254. /**
  255. * struct kvm_pgtable_walker - Hook into a page-table walk.
  256. * @cb: Callback function to invoke during the walk.
  257. * @arg: Argument passed to the callback function.
  258. * @flags: Bitwise-OR of flags to identify the entry types on which to
  259. * invoke the callback function.
  260. */
  261. struct kvm_pgtable_walker {
  262. const kvm_pgtable_visitor_fn_t cb;
  263. void * const arg;
  264. const enum kvm_pgtable_walk_flags flags;
  265. };
  266. /**
  267. * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table.
  268. * @pgt: Uninitialised page-table structure to initialise.
  269. * @va_bits: Maximum virtual address bits.
  270. * @mm_ops: Memory management callbacks.
  271. *
  272. * Return: 0 on success, negative error code on failure.
  273. */
  274. int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
  275. struct kvm_pgtable_mm_ops *mm_ops);
  276. /**
  277. * kvm_pgtable_hyp_destroy() - Destroy an unused hypervisor stage-1 page-table.
  278. * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
  279. *
  280. * The page-table is assumed to be unreachable by any hardware walkers prior
  281. * to freeing and therefore no TLB invalidation is performed.
  282. */
  283. void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt);
  284. /**
  285. * kvm_pgtable_hyp_map() - Install a mapping in a hypervisor stage-1 page-table.
  286. * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
  287. * @addr: Virtual address at which to place the mapping.
  288. * @size: Size of the mapping.
  289. * @phys: Physical address of the memory to map.
  290. * @prot: Permissions and attributes for the mapping.
  291. *
  292. * The offset of @addr within a page is ignored, @size is rounded-up to
  293. * the next page boundary and @phys is rounded-down to the previous page
  294. * boundary.
  295. *
  296. * If device attributes are not explicitly requested in @prot, then the
  297. * mapping will be normal, cacheable. Attempts to install a new mapping
  298. * for a virtual address that is already mapped will be rejected with an
  299. * error and a WARN().
  300. *
  301. * Return: 0 on success, negative error code on failure.
  302. */
  303. int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
  304. enum kvm_pgtable_prot prot);
  305. /**
  306. * kvm_pgtable_hyp_unmap() - Remove a mapping from a hypervisor stage-1 page-table.
  307. * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
  308. * @addr: Virtual address from which to remove the mapping.
  309. * @size: Size of the mapping.
  310. *
  311. * The offset of @addr within a page is ignored, @size is rounded-up to
  312. * the next page boundary and @phys is rounded-down to the previous page
  313. * boundary.
  314. *
  315. * TLB invalidation is performed for each page-table entry cleared during the
  316. * unmapping operation and the reference count for the page-table page
  317. * containing the cleared entry is decremented, with unreferenced pages being
  318. * freed. The unmapping operation will stop early if it encounters either an
  319. * invalid page-table entry or a valid block mapping which maps beyond the range
  320. * being unmapped.
  321. *
  322. * Return: Number of bytes unmapped, which may be 0.
  323. */
  324. u64 kvm_pgtable_hyp_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
  325. /**
  326. * kvm_get_vtcr() - Helper to construct VTCR_EL2
  327. * @mmfr0: Sanitized value of SYS_ID_AA64MMFR0_EL1 register.
  328. * @mmfr1: Sanitized value of SYS_ID_AA64MMFR1_EL1 register.
  329. * @phys_shfit: Value to set in VTCR_EL2.T0SZ.
  330. *
  331. * The VTCR value is common across all the physical CPUs on the system.
  332. * We use system wide sanitised values to fill in different fields,
  333. * except for Hardware Management of Access Flags. HA Flag is set
  334. * unconditionally on all CPUs, as it is safe to run with or without
  335. * the feature and the bit is RES0 on CPUs that don't support it.
  336. *
  337. * Return: VTCR_EL2 value
  338. */
  339. u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift);
  340. /**
  341. * kvm_pgtable_stage2_pgd_size() - Helper to compute size of a stage-2 PGD
  342. * @vtcr: Content of the VTCR register.
  343. *
  344. * Return: the size (in bytes) of the stage-2 PGD
  345. */
  346. size_t kvm_pgtable_stage2_pgd_size(u64 vtcr);
  347. /**
  348. * __kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table.
  349. * @pgt: Uninitialised page-table structure to initialise.
  350. * @mmu: S2 MMU context for this S2 translation
  351. * @mm_ops: Memory management callbacks.
  352. * @flags: Stage-2 configuration flags.
  353. * @pte_ops: PTE callbacks.
  354. *
  355. * Return: 0 on success, negative error code on failure.
  356. */
  357. int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
  358. struct kvm_pgtable_mm_ops *mm_ops,
  359. enum kvm_pgtable_stage2_flags flags,
  360. struct kvm_pgtable_pte_ops *pte_ops);
  361. #define kvm_pgtable_stage2_init(pgt, mmu, mm_ops, pte_ops) \
  362. __kvm_pgtable_stage2_init(pgt, mmu, mm_ops, 0, pte_ops)
  363. /**
  364. * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
  365. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  366. *
  367. * The page-table is assumed to be unreachable by any hardware walkers prior
  368. * to freeing and therefore no TLB invalidation is performed.
  369. */
  370. void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);
  371. /**
  372. * kvm_pgtable_stage2_map() - Install a mapping in a guest stage-2 page-table.
  373. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  374. * @addr: Intermediate physical address at which to place the mapping.
  375. * @size: Size of the mapping.
  376. * @phys: Physical address of the memory to map.
  377. * @prot: Permissions and attributes for the mapping.
  378. * @mc: Cache of pre-allocated and zeroed memory from which to allocate
  379. * page-table pages.
  380. *
  381. * The offset of @addr within a page is ignored, @size is rounded-up to
  382. * the next page boundary and @phys is rounded-down to the previous page
  383. * boundary.
  384. *
  385. * If device attributes are not explicitly requested in @prot, then the
  386. * mapping will be normal, cacheable.
  387. *
  388. * Note that the update of a valid leaf PTE in this function will be aborted,
  389. * if it's trying to recreate the exact same mapping or only change the access
  390. * permissions. Instead, the vCPU will exit one more time from guest if still
  391. * needed and then go through the path of relaxing permissions.
  392. *
  393. * Note that this function will both coalesce existing table entries and split
  394. * existing block mappings, relying on page-faults to fault back areas outside
  395. * of the new mapping lazily.
  396. *
  397. * Return: 0 on success, negative error code on failure.
  398. */
  399. int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
  400. u64 phys, enum kvm_pgtable_prot prot,
  401. void *mc);
  402. /**
  403. * kvm_pgtable_stage2_annotate() - Unmap and annotate pages in the IPA space
  404. * to track ownership (and more).
  405. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  406. * @addr: Base intermediate physical address to annotate.
  407. * @size: Size of the annotated range.
  408. * @mc: Cache of pre-allocated and zeroed memory from which to allocate
  409. * page-table pages.
  410. * @annotation: A 63 bit value that will be stored in the page tables.
  411. * @annotation[0] must be 0, and @annotation[63:1] is stored
  412. * in the page tables.
  413. *
  414. * By default, all page-tables are owned by identifier 0. This function can be
  415. * used to mark portions of the IPA space as owned by other entities. When a
  416. * stage 2 is used with identity-mappings, these annotations allow to use the
  417. * page-table data structure as a simple rmap.
  418. *
  419. * Return: 0 on success, negative error code on failure.
  420. */
  421. int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
  422. void *mc, kvm_pte_t annotation);
  423. /**
  424. * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table.
  425. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  426. * @addr: Intermediate physical address from which to remove the mapping.
  427. * @size: Size of the mapping.
  428. *
  429. * The offset of @addr within a page is ignored and @size is rounded-up to
  430. * the next page boundary.
  431. *
  432. * TLB invalidation is performed for each page-table entry cleared during the
  433. * unmapping operation and the reference count for the page-table page
  434. * containing the cleared entry is decremented, with unreferenced pages being
  435. * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
  436. * FWB is not supported by the CPU.
  437. *
  438. * Return: 0 on success, negative error code on failure.
  439. */
  440. int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
  441. /**
  442. * kvm_pgtable_stage2_reclaim_leaves() - Attempt to reclaim leaf page-table
  443. * pages by coalescing table entries into
  444. * block mappings.
  445. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  446. * @addr: Intermediate physical address from which to reclaim leaves.
  447. * @size: Size of the range.
  448. *
  449. * The offset of @addr within a page is ignored and @size is rounded-up to
  450. * the next page boundary.
  451. *
  452. * Return: 0 on success, negative error code on failure.
  453. */
  454. int kvm_pgtable_stage2_reclaim_leaves(struct kvm_pgtable *pgt, u64 addr, u64 size);
  455. /**
  456. * kvm_pgtable_stage2_wrprotect() - Write-protect guest stage-2 address range
  457. * without TLB invalidation.
  458. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  459. * @addr: Intermediate physical address from which to write-protect,
  460. * @size: Size of the range.
  461. *
  462. * The offset of @addr within a page is ignored and @size is rounded-up to
  463. * the next page boundary.
  464. *
  465. * Note that it is the caller's responsibility to invalidate the TLB after
  466. * calling this function to ensure that the updated permissions are visible
  467. * to the CPUs.
  468. *
  469. * Return: 0 on success, negative error code on failure.
  470. */
  471. int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
  472. /**
  473. * kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry.
  474. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  475. * @addr: Intermediate physical address to identify the page-table entry.
  476. *
  477. * The offset of @addr within a page is ignored.
  478. *
  479. * If there is a valid, leaf page-table entry used to translate @addr, then
  480. * set the access flag in that entry.
  481. *
  482. * Return: The old page-table entry prior to setting the flag, 0 on failure.
  483. */
  484. kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr);
  485. /**
  486. * kvm_pgtable_stage2_mkold() - Clear the access flag in a page-table entry.
  487. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  488. * @addr: Intermediate physical address to identify the page-table entry.
  489. *
  490. * The offset of @addr within a page is ignored.
  491. *
  492. * If there is a valid, leaf page-table entry used to translate @addr, then
  493. * clear the access flag in that entry.
  494. *
  495. * Note that it is the caller's responsibility to invalidate the TLB after
  496. * calling this function to ensure that the updated permissions are visible
  497. * to the CPUs.
  498. *
  499. * Return: The old page-table entry prior to clearing the flag, 0 on failure.
  500. */
  501. kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr);
  502. /**
  503. * kvm_pgtable_stage2_relax_perms() - Relax the permissions enforced by a
  504. * page-table entry.
  505. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  506. * @addr: Intermediate physical address to identify the page-table entry.
  507. * @prot: Additional permissions to grant for the mapping.
  508. *
  509. * The offset of @addr within a page is ignored.
  510. *
  511. * If there is a valid, leaf page-table entry used to translate @addr, then
  512. * relax the permissions in that entry according to the read, write and
  513. * execute permissions specified by @prot. No permissions are removed, and
  514. * TLB invalidation is performed after updating the entry. Software bits cannot
  515. * be set or cleared using kvm_pgtable_stage2_relax_perms().
  516. *
  517. * Return: 0 on success, negative error code on failure.
  518. */
  519. int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
  520. enum kvm_pgtable_prot prot);
  521. /**
  522. * kvm_pgtable_stage2_is_young() - Test whether a page-table entry has the
  523. * access flag set.
  524. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  525. * @addr: Intermediate physical address to identify the page-table entry.
  526. *
  527. * The offset of @addr within a page is ignored.
  528. *
  529. * Return: True if the page-table entry has the access flag set, false otherwise.
  530. */
  531. bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr);
  532. /**
  533. * kvm_pgtable_stage2_flush_range() - Clean and invalidate data cache to Point
  534. * of Coherency for guest stage-2 address
  535. * range.
  536. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
  537. * @addr: Intermediate physical address from which to flush.
  538. * @size: Size of the range.
  539. *
  540. * The offset of @addr within a page is ignored and @size is rounded-up to
  541. * the next page boundary.
  542. *
  543. * Return: 0 on success, negative error code on failure.
  544. */
  545. int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
  546. /**
  547. * kvm_pgtable_walk() - Walk a page-table.
  548. * @pgt: Page-table structure initialised by kvm_pgtable_*_init().
  549. * @addr: Input address for the start of the walk.
  550. * @size: Size of the range to walk.
  551. * @walker: Walker callback description.
  552. *
  553. * The offset of @addr within a page is ignored and @size is rounded-up to
  554. * the next page boundary.
  555. *
  556. * The walker will walk the page-table entries corresponding to the input
  557. * address range specified, visiting entries according to the walker flags.
  558. * Invalid entries are treated as leaf entries. Leaf entries are reloaded
  559. * after invoking the walker callback, allowing the walker to descend into
  560. * a newly installed table.
  561. *
  562. * Returning a negative error code from the walker callback function will
  563. * terminate the walk immediately with the same error code.
  564. *
  565. * Return: 0 on success, negative error code on failure.
  566. */
  567. int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
  568. struct kvm_pgtable_walker *walker);
  569. /**
  570. * kvm_pgtable_get_leaf() - Walk a page-table and retrieve the leaf entry
  571. * with its level.
  572. * @pgt: Page-table structure initialised by kvm_pgtable_*_init()
  573. * or a similar initialiser.
  574. * @addr: Input address for the start of the walk.
  575. * @ptep: Pointer to storage for the retrieved PTE.
  576. * @level: Pointer to storage for the level of the retrieved PTE.
  577. *
  578. * The offset of @addr within a page is ignored.
  579. *
  580. * The walker will walk the page-table entries corresponding to the input
  581. * address specified, retrieving the leaf corresponding to this address.
  582. * Invalid entries are treated as leaf entries.
  583. *
  584. * Return: 0 on success, negative error code on failure.
  585. */
  586. int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
  587. kvm_pte_t *ptep, u32 *level);
  588. /**
  589. * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a
  590. * stage-2 Page-Table Entry.
  591. * @pte: Page-table entry
  592. *
  593. * Return: protection attributes of the page-table entry in the enum
  594. * kvm_pgtable_prot format.
  595. */
  596. enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte);
  597. /**
  598. * kvm_pgtable_hyp_pte_prot() - Retrieve the protection attributes of a stage-1
  599. * Page-Table Entry.
  600. * @pte: Page-table entry
  601. *
  602. * Return: protection attributes of the page-table entry in the enum
  603. * kvm_pgtable_prot format.
  604. */
  605. enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte);
  606. #endif /* __ARM64_KVM_PGTABLE_H__ */