2
0

cputlb.c 11 KB

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