paging_tmpl.h 11 KB

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