2
0

cputlb.c 12 KB

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