paging_tmpl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * MMU support
  8. *
  9. * Copyright (C) 2006 Qumranet, Inc.
  10. *
  11. * Authors:
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Avi Kivity <avi@qumranet.com>
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2. See
  16. * the COPYING file in the top-level directory.
  17. *
  18. */
  19. /*
  20. * We need the mmu code to access both 32-bit and 64-bit guest ptes,
  21. * so the code in this file is compiled twice, once per pte size.
  22. */
  23. #if PTTYPE == 64
  24. #define pt_element_t u64
  25. #define guest_walker guest_walker64
  26. #define FNAME(name) paging##64_##name
  27. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  28. #define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK
  29. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  30. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  31. #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level)
  32. #define PT_PTE_COPY_MASK PT64_PTE_COPY_MASK
  33. #define PT_NON_PTE_COPY_MASK PT64_NON_PTE_COPY_MASK
  34. #elif PTTYPE == 32
  35. #define pt_element_t u32
  36. #define guest_walker guest_walker32
  37. #define FNAME(name) paging##32_##name
  38. #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
  39. #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
  40. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  41. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  42. #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
  43. #define PT_PTE_COPY_MASK PT32_PTE_COPY_MASK
  44. #define PT_NON_PTE_COPY_MASK PT32_NON_PTE_COPY_MASK
  45. #else
  46. #error Invalid PTTYPE value
  47. #endif
  48. /*
  49. * The guest_walker structure emulates the behavior of the hardware page
  50. * table walker.
  51. */
  52. struct guest_walker {
  53. int level;
  54. pt_element_t *table;
  55. pt_element_t inherited_ar;
  56. };
  57. static void FNAME(init_walker)(struct guest_walker *walker,
  58. struct kvm_vcpu *vcpu)
  59. {
  60. hpa_t hpa;
  61. struct kvm_memory_slot *slot;
  62. walker->level = vcpu->mmu.root_level;
  63. slot = gfn_to_memslot(vcpu->kvm,
  64. (vcpu->cr3 & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
  65. hpa = safe_gpa_to_hpa(vcpu, vcpu->cr3 & PT64_BASE_ADDR_MASK);
  66. walker->table = kmap_atomic(pfn_to_page(hpa >> PAGE_SHIFT), KM_USER0);
  67. ASSERT((!kvm_arch_ops->is_long_mode(vcpu) && is_pae(vcpu)) ||
  68. (vcpu->cr3 & ~(PAGE_MASK | CR3_FLAGS_MASK)) == 0);
  69. walker->table = (pt_element_t *)( (unsigned long)walker->table |
  70. (unsigned long)(vcpu->cr3 & ~(PAGE_MASK | CR3_FLAGS_MASK)) );
  71. walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK;
  72. }
  73. static void FNAME(release_walker)(struct guest_walker *walker)
  74. {
  75. kunmap_atomic(walker->table, KM_USER0);
  76. }
  77. static void FNAME(set_pte)(struct kvm_vcpu *vcpu, u64 guest_pte,
  78. u64 *shadow_pte, u64 access_bits)
  79. {
  80. ASSERT(*shadow_pte == 0);
  81. access_bits &= guest_pte;
  82. *shadow_pte = (guest_pte & PT_PTE_COPY_MASK);
  83. set_pte_common(vcpu, shadow_pte, guest_pte & PT_BASE_ADDR_MASK,
  84. guest_pte & PT_DIRTY_MASK, access_bits);
  85. }
  86. static void FNAME(set_pde)(struct kvm_vcpu *vcpu, u64 guest_pde,
  87. u64 *shadow_pte, u64 access_bits,
  88. int index)
  89. {
  90. gpa_t gaddr;
  91. ASSERT(*shadow_pte == 0);
  92. access_bits &= guest_pde;
  93. gaddr = (guest_pde & PT_DIR_BASE_ADDR_MASK) + PAGE_SIZE * index;
  94. if (PTTYPE == 32 && is_cpuid_PSE36())
  95. gaddr |= (guest_pde & PT32_DIR_PSE36_MASK) <<
  96. (32 - PT32_DIR_PSE36_SHIFT);
  97. *shadow_pte = (guest_pde & (PT_NON_PTE_COPY_MASK | PT_GLOBAL_MASK)) |
  98. ((guest_pde & PT_DIR_PAT_MASK) >>
  99. (PT_DIR_PAT_SHIFT - PT_PAT_SHIFT));
  100. set_pte_common(vcpu, shadow_pte, gaddr,
  101. guest_pde & PT_DIRTY_MASK, access_bits);
  102. }
  103. /*
  104. * Fetch a guest pte from a specific level in the paging hierarchy.
  105. */
  106. static pt_element_t *FNAME(fetch_guest)(struct kvm_vcpu *vcpu,
  107. struct guest_walker *walker,
  108. int level,
  109. gva_t addr)
  110. {
  111. ASSERT(level > 0 && level <= walker->level);
  112. for (;;) {
  113. int index = PT_INDEX(addr, walker->level);
  114. hpa_t paddr;
  115. ASSERT(((unsigned long)walker->table & PAGE_MASK) ==
  116. ((unsigned long)&walker->table[index] & PAGE_MASK));
  117. if (level == walker->level ||
  118. !is_present_pte(walker->table[index]) ||
  119. (walker->level == PT_DIRECTORY_LEVEL &&
  120. (walker->table[index] & PT_PAGE_SIZE_MASK) &&
  121. (PTTYPE == 64 || is_pse(vcpu))))
  122. return &walker->table[index];
  123. if (walker->level != 3 || kvm_arch_ops->is_long_mode(vcpu))
  124. walker->inherited_ar &= walker->table[index];
  125. paddr = safe_gpa_to_hpa(vcpu, walker->table[index] & PT_BASE_ADDR_MASK);
  126. kunmap_atomic(walker->table, KM_USER0);
  127. walker->table = kmap_atomic(pfn_to_page(paddr >> PAGE_SHIFT),
  128. KM_USER0);
  129. --walker->level;
  130. }
  131. }
  132. /*
  133. * Fetch a shadow pte for a specific level in the paging hierarchy.
  134. */
  135. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  136. struct guest_walker *walker)
  137. {
  138. hpa_t shadow_addr;
  139. int level;
  140. u64 *prev_shadow_ent = NULL;
  141. shadow_addr = vcpu->mmu.root_hpa;
  142. level = vcpu->mmu.shadow_root_level;
  143. for (; ; level--) {
  144. u32 index = SHADOW_PT_INDEX(addr, level);
  145. u64 *shadow_ent = ((u64 *)__va(shadow_addr)) + index;
  146. pt_element_t *guest_ent;
  147. if (is_present_pte(*shadow_ent) || is_io_pte(*shadow_ent)) {
  148. if (level == PT_PAGE_TABLE_LEVEL)
  149. return shadow_ent;
  150. shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
  151. prev_shadow_ent = shadow_ent;
  152. continue;
  153. }
  154. if (PTTYPE == 32 && level > PT32_ROOT_LEVEL) {
  155. ASSERT(level == PT32E_ROOT_LEVEL);
  156. guest_ent = FNAME(fetch_guest)(vcpu, walker,
  157. PT32_ROOT_LEVEL, addr);
  158. } else
  159. guest_ent = FNAME(fetch_guest)(vcpu, walker,
  160. level, addr);
  161. if (!is_present_pte(*guest_ent))
  162. return NULL;
  163. /* Don't set accessed bit on PAE PDPTRs */
  164. if (vcpu->mmu.root_level != 3 || walker->level != 3)
  165. *guest_ent |= PT_ACCESSED_MASK;
  166. if (level == PT_PAGE_TABLE_LEVEL) {
  167. if (walker->level == PT_DIRECTORY_LEVEL) {
  168. if (prev_shadow_ent)
  169. *prev_shadow_ent |= PT_SHADOW_PS_MARK;
  170. FNAME(set_pde)(vcpu, *guest_ent, shadow_ent,
  171. walker->inherited_ar,
  172. PT_INDEX(addr, PT_PAGE_TABLE_LEVEL));
  173. } else {
  174. ASSERT(walker->level == PT_PAGE_TABLE_LEVEL);
  175. FNAME(set_pte)(vcpu, *guest_ent, shadow_ent, walker->inherited_ar);
  176. }
  177. return shadow_ent;
  178. }
  179. shadow_addr = kvm_mmu_alloc_page(vcpu, shadow_ent);
  180. if (!VALID_PAGE(shadow_addr))
  181. return ERR_PTR(-ENOMEM);
  182. if (!kvm_arch_ops->is_long_mode(vcpu) && level == 3)
  183. *shadow_ent = shadow_addr |
  184. (*guest_ent & (PT_PRESENT_MASK | PT_PWT_MASK | PT_PCD_MASK));
  185. else {
  186. *shadow_ent = shadow_addr |
  187. (*guest_ent & PT_NON_PTE_COPY_MASK);
  188. *shadow_ent |= (PT_WRITABLE_MASK | PT_USER_MASK);
  189. }
  190. prev_shadow_ent = shadow_ent;
  191. }
  192. }
  193. /*
  194. * The guest faulted for write. We need to
  195. *
  196. * - check write permissions
  197. * - update the guest pte dirty bit
  198. * - update our own dirty page tracking structures
  199. */
  200. static int FNAME(fix_write_pf)(struct kvm_vcpu *vcpu,
  201. u64 *shadow_ent,
  202. struct guest_walker *walker,
  203. gva_t addr,
  204. int user)
  205. {
  206. pt_element_t *guest_ent;
  207. int writable_shadow;
  208. gfn_t gfn;
  209. if (is_writeble_pte(*shadow_ent))
  210. return 0;
  211. writable_shadow = *shadow_ent & PT_SHADOW_WRITABLE_MASK;
  212. if (user) {
  213. /*
  214. * User mode access. Fail if it's a kernel page or a read-only
  215. * page.
  216. */
  217. if (!(*shadow_ent & PT_SHADOW_USER_MASK) || !writable_shadow)
  218. return 0;
  219. ASSERT(*shadow_ent & PT_USER_MASK);
  220. } else
  221. /*
  222. * Kernel mode access. Fail if it's a read-only page and
  223. * supervisor write protection is enabled.
  224. */
  225. if (!writable_shadow) {
  226. if (is_write_protection(vcpu))
  227. return 0;
  228. *shadow_ent &= ~PT_USER_MASK;
  229. }
  230. guest_ent = FNAME(fetch_guest)(vcpu, walker, PT_PAGE_TABLE_LEVEL, addr);
  231. if (!is_present_pte(*guest_ent)) {
  232. *shadow_ent = 0;
  233. return 0;
  234. }
  235. gfn = (*guest_ent & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  236. mark_page_dirty(vcpu->kvm, gfn);
  237. *shadow_ent |= PT_WRITABLE_MASK;
  238. *guest_ent |= PT_DIRTY_MASK;
  239. return 1;
  240. }
  241. /*
  242. * Page fault handler. There are several causes for a page fault:
  243. * - there is no shadow pte for the guest pte
  244. * - write access through a shadow pte marked read only so that we can set
  245. * the dirty bit
  246. * - write access to a shadow pte marked read only so we can update the page
  247. * dirty bitmap, when userspace requests it
  248. * - mmio access; in this case we will never install a present shadow pte
  249. * - normal guest page fault due to the guest pte marked not present, not
  250. * writable, or not executable
  251. *
  252. * Returns: 1 if we need to emulate the instruction, 0 otherwise
  253. */
  254. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  255. u32 error_code)
  256. {
  257. int write_fault = error_code & PFERR_WRITE_MASK;
  258. int pte_present = error_code & PFERR_PRESENT_MASK;
  259. int user_fault = error_code & PFERR_USER_MASK;
  260. struct guest_walker walker;
  261. u64 *shadow_pte;
  262. int fixed;
  263. /*
  264. * Look up the shadow pte for the faulting address.
  265. */
  266. for (;;) {
  267. FNAME(init_walker)(&walker, vcpu);
  268. shadow_pte = FNAME(fetch)(vcpu, addr, &walker);
  269. if (IS_ERR(shadow_pte)) { /* must be -ENOMEM */
  270. nonpaging_flush(vcpu);
  271. FNAME(release_walker)(&walker);
  272. continue;
  273. }
  274. break;
  275. }
  276. /*
  277. * The page is not mapped by the guest. Let the guest handle it.
  278. */
  279. if (!shadow_pte) {
  280. inject_page_fault(vcpu, addr, error_code);
  281. FNAME(release_walker)(&walker);
  282. return 0;
  283. }
  284. /*
  285. * Update the shadow pte.
  286. */
  287. if (write_fault)
  288. fixed = FNAME(fix_write_pf)(vcpu, shadow_pte, &walker, addr,
  289. user_fault);
  290. else
  291. fixed = fix_read_pf(shadow_pte);
  292. FNAME(release_walker)(&walker);
  293. /*
  294. * mmio: emulate if accessible, otherwise its a guest fault.
  295. */
  296. if (is_io_pte(*shadow_pte)) {
  297. if (may_access(*shadow_pte, write_fault, user_fault))
  298. return 1;
  299. pgprintk("%s: io work, no access\n", __FUNCTION__);
  300. inject_page_fault(vcpu, addr,
  301. error_code | PFERR_PRESENT_MASK);
  302. return 0;
  303. }
  304. /*
  305. * pte not present, guest page fault.
  306. */
  307. if (pte_present && !fixed) {
  308. inject_page_fault(vcpu, addr, error_code);
  309. return 0;
  310. }
  311. ++kvm_stat.pf_fixed;
  312. return 0;
  313. }
  314. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
  315. {
  316. struct guest_walker walker;
  317. pt_element_t guest_pte;
  318. gpa_t gpa;
  319. FNAME(init_walker)(&walker, vcpu);
  320. guest_pte = *FNAME(fetch_guest)(vcpu, &walker, PT_PAGE_TABLE_LEVEL,
  321. vaddr);
  322. FNAME(release_walker)(&walker);
  323. if (!is_present_pte(guest_pte))
  324. return UNMAPPED_GVA;
  325. if (walker.level == PT_DIRECTORY_LEVEL) {
  326. ASSERT((guest_pte & PT_PAGE_SIZE_MASK));
  327. ASSERT(PTTYPE == 64 || is_pse(vcpu));
  328. gpa = (guest_pte & PT_DIR_BASE_ADDR_MASK) | (vaddr &
  329. (PT_LEVEL_MASK(PT_PAGE_TABLE_LEVEL) | ~PAGE_MASK));
  330. if (PTTYPE == 32 && is_cpuid_PSE36())
  331. gpa |= (guest_pte & PT32_DIR_PSE36_MASK) <<
  332. (32 - PT32_DIR_PSE36_SHIFT);
  333. } else {
  334. gpa = (guest_pte & PT_BASE_ADDR_MASK);
  335. gpa |= (vaddr & ~PAGE_MASK);
  336. }
  337. return gpa;
  338. }
  339. #undef pt_element_t
  340. #undef guest_walker
  341. #undef FNAME
  342. #undef PT_BASE_ADDR_MASK
  343. #undef PT_INDEX
  344. #undef SHADOW_PT_INDEX
  345. #undef PT_LEVEL_MASK
  346. #undef PT_PTE_COPY_MASK
  347. #undef PT_NON_PTE_COPY_MASK
  348. #undef PT_DIR_BASE_ADDR_MASK