cputlb.c 11 KB

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