paging_tmpl.h 11 KB

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