2
0

cputlb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Common CPU TLB handling
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "cpu.h"
  21. #include "exec/exec-all.h"
  22. #include "exec/memory.h"
  23. #include "exec/address-spaces.h"
  24. #include "exec/cpu_ldst.h"
  25. #include "exec/cputlb.h"
  26. #include "exec/memory-internal.h"
  27. #include "exec/ram_addr.h"
  28. #include "tcg/tcg.h"
  29. //#define DEBUG_TLB
  30. //#define DEBUG_TLB_CHECK
  31. /* statistics */
  32. int tlb_flush_count;
  33. /* NOTE:
  34. * If flush_global is true (the usual case), flush all tlb entries.
  35. * If flush_global is false, flush (at least) all tlb entries not
  36. * marked global.
  37. *
  38. * Since QEMU doesn't currently implement a global/not-global flag
  39. * for tlb entries, at the moment tlb_flush() will also flush all
  40. * tlb entries in the flush_global == false case. This is OK because
  41. * CPU architectures generally permit an implementation to drop
  42. * entries from the TLB at any time, so flushing more entries than
  43. * required is only an efficiency issue, not a correctness issue.
  44. */
  45. void tlb_flush(CPUState *cpu, int flush_global)
  46. {
  47. CPUArchState *env = cpu->env_ptr;
  48. #if defined(DEBUG_TLB)
  49. printf("tlb_flush:\n");
  50. #endif
  51. /* must reset current TB so that interrupts cannot modify the
  52. links while we are modifying them */
  53. cpu->current_tb = NULL;
  54. memset(env->tlb_table, -1, sizeof(env->tlb_table));
  55. memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache));
  56. env->tlb_flush_addr = -1;
  57. env->tlb_flush_mask = 0;
  58. tlb_flush_count++;
  59. }
  60. static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
  61. {
  62. if (addr == (tlb_entry->addr_read &
  63. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  64. addr == (tlb_entry->addr_write &
  65. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  66. addr == (tlb_entry->addr_code &
  67. (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
  68. memset(tlb_entry, -1, sizeof(*tlb_entry));
  69. }
  70. }
  71. void tlb_flush_page(CPUState *cpu, target_ulong addr)
  72. {
  73. CPUArchState *env = cpu->env_ptr;
  74. int i;
  75. int mmu_idx;
  76. #if defined(DEBUG_TLB)
  77. printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
  78. #endif
  79. /* Check if we need to flush due to large pages. */
  80. if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
  81. #if defined(DEBUG_TLB)
  82. printf("tlb_flush_page: forced full flush ("
  83. TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
  84. env->tlb_flush_addr, env->tlb_flush_mask);
  85. #endif
  86. tlb_flush(cpu, 1);
  87. return;
  88. }
  89. /* must reset current TB so that interrupts cannot modify the
  90. links while we are modifying them */
  91. cpu->current_tb = NULL;
  92. addr &= TARGET_PAGE_MASK;
  93. i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  94. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  95. tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
  96. }
  97. tb_flush_jmp_cache(cpu, addr);
  98. }
  99. /* update the TLBs so that writes to code in the virtual page 'addr'
  100. can be detected */
  101. void tlb_protect_code(ram_addr_t ram_addr)
  102. {
  103. cpu_physical_memory_reset_dirty(ram_addr, TARGET_PAGE_SIZE,
  104. DIRTY_MEMORY_CODE);
  105. }
  106. /* update the TLB so that writes in physical page 'phys_addr' are no longer
  107. tested for self modifying code */
  108. void tlb_unprotect_code_phys(CPUState *cpu, ram_addr_t ram_addr,
  109. target_ulong vaddr)
  110. {
  111. cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
  112. }
  113. static bool tlb_is_dirty_ram(CPUTLBEntry *tlbe)
  114. {
  115. return (tlbe->addr_write & (TLB_INVALID_MASK|TLB_MMIO|TLB_NOTDIRTY)) == 0;
  116. }
  117. void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, uintptr_t start,
  118. uintptr_t length)
  119. {
  120. uintptr_t addr;
  121. if (tlb_is_dirty_ram(tlb_entry)) {
  122. addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
  123. if ((addr - start) < length) {
  124. tlb_entry->addr_write |= TLB_NOTDIRTY;
  125. }
  126. }
  127. }
  128. static inline ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
  129. {
  130. ram_addr_t ram_addr;
  131. if (qemu_ram_addr_from_host(ptr, &ram_addr) == NULL) {
  132. fprintf(stderr, "Bad ram pointer %p\n", ptr);
  133. abort();
  134. }
  135. return ram_addr;
  136. }
  137. void cpu_tlb_reset_dirty_all(ram_addr_t start1, ram_addr_t length)
  138. {
  139. CPUState *cpu;
  140. CPUArchState *env;
  141. CPU_FOREACH(cpu) {
  142. int mmu_idx;
  143. env = cpu->env_ptr;
  144. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  145. unsigned int i;
  146. for (i = 0; i < CPU_TLB_SIZE; i++) {
  147. tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
  148. start1, length);
  149. }
  150. }
  151. }
  152. }
  153. static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
  154. {
  155. if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) {
  156. tlb_entry->addr_write = vaddr;
  157. }
  158. }
  159. /* update the TLB corresponding to virtual page vaddr
  160. so that it is no longer dirty */
  161. void tlb_set_dirty(CPUArchState *env, target_ulong vaddr)
  162. {
  163. int i;
  164. int mmu_idx;
  165. vaddr &= TARGET_PAGE_MASK;
  166. i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  167. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  168. tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
  169. }
  170. }
  171. /* Our TLB does not support large pages, so remember the area covered by
  172. large pages and trigger a full TLB flush if these are invalidated. */
  173. static void tlb_add_large_page(CPUArchState *env, target_ulong vaddr,
  174. target_ulong size)
  175. {
  176. target_ulong mask = ~(size - 1);
  177. if (env->tlb_flush_addr == (target_ulong)-1) {
  178. env->tlb_flush_addr = vaddr & mask;
  179. env->tlb_flush_mask = mask;
  180. return;
  181. }
  182. /* Extend the existing region to include the new page.
  183. This is a compromise between unnecessary flushes and the cost
  184. of maintaining a full variable size TLB. */
  185. mask &= env->tlb_flush_mask;
  186. while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
  187. mask <<= 1;
  188. }
  189. env->tlb_flush_addr &= mask;
  190. env->tlb_flush_mask = mask;
  191. }
  192. /* Add a new TLB entry. At most one entry for a given virtual address
  193. is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
  194. supplied size is only used by tlb_flush_page. */
  195. void tlb_set_page(CPUState *cpu, target_ulong vaddr,
  196. hwaddr paddr, int prot,
  197. int mmu_idx, target_ulong size)
  198. {
  199. CPUArchState *env = cpu->env_ptr;
  200. MemoryRegionSection *section;
  201. unsigned int index;
  202. target_ulong address;
  203. target_ulong code_address;
  204. uintptr_t addend;
  205. CPUTLBEntry *te;
  206. hwaddr iotlb, xlat, sz;
  207. assert(size >= TARGET_PAGE_SIZE);
  208. if (size != TARGET_PAGE_SIZE) {
  209. tlb_add_large_page(env, vaddr, size);
  210. }
  211. sz = size;
  212. section = address_space_translate_for_iotlb(cpu->as, paddr,
  213. &xlat, &sz);
  214. assert(sz >= TARGET_PAGE_SIZE);
  215. #if defined(DEBUG_TLB)
  216. printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
  217. " prot=%x idx=%d\n",
  218. vaddr, paddr, prot, mmu_idx);
  219. #endif
  220. address = vaddr;
  221. if (!memory_region_is_ram(section->mr) && !memory_region_is_romd(section->mr)) {
  222. /* IO memory case */
  223. address |= TLB_MMIO;
  224. addend = 0;
  225. } else {
  226. /* TLB_MMIO for rom/romd handled below */
  227. addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
  228. }
  229. code_address = address;
  230. iotlb = memory_region_section_get_iotlb(cpu, section, vaddr, paddr, xlat,
  231. prot, &address);
  232. index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  233. env->iotlb[mmu_idx][index] = iotlb - vaddr;
  234. te = &env->tlb_table[mmu_idx][index];
  235. te->addend = addend - vaddr;
  236. if (prot & PAGE_READ) {
  237. te->addr_read = address;
  238. } else {
  239. te->addr_read = -1;
  240. }
  241. if (prot & PAGE_EXEC) {
  242. te->addr_code = code_address;
  243. } else {
  244. te->addr_code = -1;
  245. }
  246. if (prot & PAGE_WRITE) {
  247. if ((memory_region_is_ram(section->mr) && section->readonly)
  248. || memory_region_is_romd(section->mr)) {
  249. /* Write access calls the I/O callback. */
  250. te->addr_write = address | TLB_MMIO;
  251. } else if (memory_region_is_ram(section->mr)
  252. && cpu_physical_memory_is_clean(section->mr->ram_addr
  253. + xlat)) {
  254. te->addr_write = address | TLB_NOTDIRTY;
  255. } else {
  256. te->addr_write = address;
  257. }
  258. } else {
  259. te->addr_write = -1;
  260. }
  261. }
  262. /* NOTE: this function can trigger an exception */
  263. /* NOTE2: the returned address is not exactly the physical address: it
  264. * is actually a ram_addr_t (in system mode; the user mode emulation
  265. * version of this function returns a guest virtual address).
  266. */
  267. tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
  268. {
  269. int mmu_idx, page_index, pd;
  270. void *p;
  271. MemoryRegion *mr;
  272. CPUState *cpu = ENV_GET_CPU(env1);
  273. page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  274. mmu_idx = cpu_mmu_index(env1);
  275. if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
  276. (addr & TARGET_PAGE_MASK))) {
  277. cpu_ldub_code(env1, addr);
  278. }
  279. pd = env1->iotlb[mmu_idx][page_index] & ~TARGET_PAGE_MASK;
  280. mr = iotlb_to_region(cpu->as, pd);
  281. if (memory_region_is_unassigned(mr)) {
  282. CPUClass *cc = CPU_GET_CLASS(cpu);
  283. if (cc->do_unassigned_access) {
  284. cc->do_unassigned_access(cpu, addr, false, true, 0, 4);
  285. } else {
  286. cpu_abort(cpu, "Trying to execute code outside RAM or ROM at 0x"
  287. TARGET_FMT_lx "\n", addr);
  288. }
  289. }
  290. p = (void *)((uintptr_t)addr + env1->tlb_table[mmu_idx][page_index].addend);
  291. return qemu_ram_addr_from_host_nofail(p);
  292. }
  293. #define MMUSUFFIX _mmu
  294. #define SHIFT 0
  295. #include "softmmu_template.h"
  296. #define SHIFT 1
  297. #include "softmmu_template.h"
  298. #define SHIFT 2
  299. #include "softmmu_template.h"
  300. #define SHIFT 3
  301. #include "softmmu_template.h"
  302. #undef MMUSUFFIX
  303. #define MMUSUFFIX _cmmu
  304. #undef GETPC_ADJ
  305. #define GETPC_ADJ 0
  306. #undef GETRA
  307. #define GETRA() ((uintptr_t)0)
  308. #define SOFTMMU_CODE_ACCESS
  309. #define SHIFT 0
  310. #include "softmmu_template.h"
  311. #define SHIFT 1
  312. #include "softmmu_template.h"
  313. #define SHIFT 2
  314. #include "softmmu_template.h"
  315. #define SHIFT 3
  316. #include "softmmu_template.h"