mmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Microblaze MMU emulation for qemu.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias
  5. * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/log.h"
  22. #include "cpu.h"
  23. #include "exec/cputlb.h"
  24. #include "exec/page-protection.h"
  25. static unsigned int tlb_decode_size(unsigned int f)
  26. {
  27. static const unsigned int sizes[] = {
  28. 1 * 1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024,
  29. 1 * 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024
  30. };
  31. assert(f < ARRAY_SIZE(sizes));
  32. return sizes[f];
  33. }
  34. static void mmu_flush_idx(CPUMBState *env, unsigned int idx)
  35. {
  36. CPUState *cs = env_cpu(env);
  37. MicroBlazeMMU *mmu = &env->mmu;
  38. unsigned int tlb_size;
  39. uint32_t tlb_tag, end, t;
  40. t = mmu->rams[RAM_TAG][idx];
  41. if (!(t & TLB_VALID))
  42. return;
  43. tlb_tag = t & TLB_EPN_MASK;
  44. tlb_size = tlb_decode_size((t & TLB_PAGESZ_MASK) >> 7);
  45. end = tlb_tag + tlb_size;
  46. while (tlb_tag < end) {
  47. tlb_flush_page(cs, tlb_tag);
  48. tlb_tag += TARGET_PAGE_SIZE;
  49. }
  50. }
  51. static void mmu_change_pid(CPUMBState *env, unsigned int newpid)
  52. {
  53. MicroBlazeMMU *mmu = &env->mmu;
  54. unsigned int i;
  55. uint32_t t;
  56. if (newpid & ~0xff)
  57. qemu_log_mask(LOG_GUEST_ERROR, "Illegal rpid=%x\n", newpid);
  58. for (i = 0; i < ARRAY_SIZE(mmu->rams[RAM_TAG]); i++) {
  59. /* Lookup and decode. */
  60. t = mmu->rams[RAM_TAG][i];
  61. if (t & TLB_VALID) {
  62. if (mmu->tids[i] && ((mmu->regs[MMU_R_PID] & 0xff) == mmu->tids[i]))
  63. mmu_flush_idx(env, i);
  64. }
  65. }
  66. }
  67. /* rw - 0 = read, 1 = write, 2 = fetch. */
  68. unsigned int mmu_translate(MicroBlazeCPU *cpu, MicroBlazeMMULookup *lu,
  69. target_ulong vaddr, MMUAccessType rw, int mmu_idx)
  70. {
  71. MicroBlazeMMU *mmu = &cpu->env.mmu;
  72. unsigned int i, hit = 0;
  73. unsigned int tlb_ex = 0, tlb_wr = 0, tlb_zsel;
  74. uint64_t tlb_tag, tlb_rpn, mask;
  75. uint32_t tlb_size, t0;
  76. lu->err = ERR_MISS;
  77. for (i = 0; i < ARRAY_SIZE(mmu->rams[RAM_TAG]); i++) {
  78. uint64_t t, d;
  79. /* Lookup and decode. */
  80. t = mmu->rams[RAM_TAG][i];
  81. if (t & TLB_VALID) {
  82. tlb_size = tlb_decode_size((t & TLB_PAGESZ_MASK) >> 7);
  83. if (tlb_size < TARGET_PAGE_SIZE) {
  84. qemu_log_mask(LOG_UNIMP, "%d pages not supported\n", tlb_size);
  85. abort();
  86. }
  87. mask = ~((uint64_t)tlb_size - 1);
  88. tlb_tag = t & TLB_EPN_MASK;
  89. if ((vaddr & mask) != (tlb_tag & mask)) {
  90. continue;
  91. }
  92. if (mmu->tids[i]
  93. && ((mmu->regs[MMU_R_PID] & 0xff) != mmu->tids[i])) {
  94. continue;
  95. }
  96. /* Bring in the data part. */
  97. d = mmu->rams[RAM_DATA][i];
  98. tlb_ex = d & TLB_EX;
  99. tlb_wr = d & TLB_WR;
  100. /* Now let's see if there is a zone that overrides the protbits. */
  101. tlb_zsel = (d >> 4) & 0xf;
  102. t0 = mmu->regs[MMU_R_ZPR] >> (30 - (tlb_zsel * 2));
  103. t0 &= 0x3;
  104. if (tlb_zsel > cpu->cfg.mmu_zones) {
  105. qemu_log_mask(LOG_GUEST_ERROR,
  106. "tlb zone select out of range! %d\n", tlb_zsel);
  107. t0 = 1; /* Ignore. */
  108. }
  109. if (cpu->cfg.mmu == 1) {
  110. t0 = 1; /* Zones are disabled. */
  111. }
  112. switch (t0) {
  113. case 0:
  114. if (mmu_idx == MMU_USER_IDX)
  115. continue;
  116. break;
  117. case 2:
  118. if (mmu_idx != MMU_USER_IDX) {
  119. tlb_ex = 1;
  120. tlb_wr = 1;
  121. }
  122. break;
  123. case 3:
  124. tlb_ex = 1;
  125. tlb_wr = 1;
  126. break;
  127. default: break;
  128. }
  129. lu->err = ERR_PROT;
  130. lu->prot = PAGE_READ;
  131. if (tlb_wr)
  132. lu->prot |= PAGE_WRITE;
  133. else if (rw == 1)
  134. goto done;
  135. if (tlb_ex)
  136. lu->prot |=PAGE_EXEC;
  137. else if (rw == 2) {
  138. goto done;
  139. }
  140. tlb_rpn = d & TLB_RPN_MASK;
  141. lu->vaddr = tlb_tag;
  142. lu->paddr = tlb_rpn & cpu->cfg.addr_mask;
  143. lu->size = tlb_size;
  144. lu->err = ERR_HIT;
  145. lu->idx = i;
  146. hit = 1;
  147. goto done;
  148. }
  149. }
  150. done:
  151. qemu_log_mask(CPU_LOG_MMU,
  152. "MMU vaddr=%" PRIx64 " rw=%d tlb_wr=%d tlb_ex=%d hit=%d\n",
  153. vaddr, rw, tlb_wr, tlb_ex, hit);
  154. return hit;
  155. }
  156. /* Writes/reads to the MMU's special regs end up here. */
  157. uint32_t mmu_read(CPUMBState *env, bool ext, uint32_t rn)
  158. {
  159. MicroBlazeCPU *cpu = env_archcpu(env);
  160. unsigned int i;
  161. uint32_t r = 0;
  162. if (cpu->cfg.mmu < 2 || !cpu->cfg.mmu_tlb_access) {
  163. qemu_log_mask(LOG_GUEST_ERROR, "MMU access on MMU-less system\n");
  164. return 0;
  165. }
  166. if (ext && rn != MMU_R_TLBLO) {
  167. qemu_log_mask(LOG_GUEST_ERROR, "Extended access only to TLBLO.\n");
  168. return 0;
  169. }
  170. switch (rn) {
  171. /* Reads to HI/LO trig reads from the mmu rams. */
  172. case MMU_R_TLBLO:
  173. case MMU_R_TLBHI:
  174. if (!(cpu->cfg.mmu_tlb_access & 1)) {
  175. qemu_log_mask(LOG_GUEST_ERROR,
  176. "Invalid access to MMU reg %d\n", rn);
  177. return 0;
  178. }
  179. i = env->mmu.regs[MMU_R_TLBX] & 0xff;
  180. r = extract64(env->mmu.rams[rn & 1][i], ext * 32, 32);
  181. if (rn == MMU_R_TLBHI)
  182. env->mmu.regs[MMU_R_PID] = env->mmu.tids[i];
  183. break;
  184. case MMU_R_PID:
  185. case MMU_R_ZPR:
  186. if (!(cpu->cfg.mmu_tlb_access & 1)) {
  187. qemu_log_mask(LOG_GUEST_ERROR,
  188. "Invalid access to MMU reg %d\n", rn);
  189. return 0;
  190. }
  191. r = env->mmu.regs[rn];
  192. break;
  193. case MMU_R_TLBX:
  194. r = env->mmu.regs[rn];
  195. break;
  196. case MMU_R_TLBSX:
  197. qemu_log_mask(LOG_GUEST_ERROR, "TLBSX is write-only.\n");
  198. break;
  199. default:
  200. qemu_log_mask(LOG_GUEST_ERROR, "Invalid MMU register %d.\n", rn);
  201. break;
  202. }
  203. qemu_log_mask(CPU_LOG_MMU, "%s rn=%d=%x\n", __func__, rn, r);
  204. return r;
  205. }
  206. void mmu_write(CPUMBState *env, bool ext, uint32_t rn, uint32_t v)
  207. {
  208. MicroBlazeCPU *cpu = env_archcpu(env);
  209. uint64_t tmp64;
  210. unsigned int i;
  211. qemu_log_mask(CPU_LOG_MMU,
  212. "%s rn=%d=%x old=%x\n", __func__, rn, v,
  213. rn < 3 ? env->mmu.regs[rn] : env->mmu.regs[MMU_R_TLBX]);
  214. if (cpu->cfg.mmu < 2 || !cpu->cfg.mmu_tlb_access) {
  215. qemu_log_mask(LOG_GUEST_ERROR, "MMU access on MMU-less system\n");
  216. return;
  217. }
  218. if (ext && rn != MMU_R_TLBLO) {
  219. qemu_log_mask(LOG_GUEST_ERROR, "Extended access only to TLBLO.\n");
  220. return;
  221. }
  222. switch (rn) {
  223. /* Writes to HI/LO trig writes to the mmu rams. */
  224. case MMU_R_TLBLO:
  225. case MMU_R_TLBHI:
  226. i = env->mmu.regs[MMU_R_TLBX] & 0xff;
  227. if (rn == MMU_R_TLBHI) {
  228. if (i < 3 && !(v & TLB_VALID) && qemu_loglevel_mask(~0))
  229. qemu_log_mask(LOG_GUEST_ERROR,
  230. "invalidating index %x at pc=%x\n",
  231. i, env->pc);
  232. env->mmu.tids[i] = env->mmu.regs[MMU_R_PID] & 0xff;
  233. mmu_flush_idx(env, i);
  234. }
  235. tmp64 = env->mmu.rams[rn & 1][i];
  236. env->mmu.rams[rn & 1][i] = deposit64(tmp64, ext * 32, 32, v);
  237. break;
  238. case MMU_R_ZPR:
  239. if (cpu->cfg.mmu_tlb_access <= 1) {
  240. qemu_log_mask(LOG_GUEST_ERROR,
  241. "Invalid access to MMU reg %d\n", rn);
  242. return;
  243. }
  244. /* Changes to the zone protection reg flush the QEMU TLB.
  245. Fortunately, these are very uncommon. */
  246. if (v != env->mmu.regs[rn]) {
  247. tlb_flush(env_cpu(env));
  248. }
  249. env->mmu.regs[rn] = v;
  250. break;
  251. case MMU_R_PID:
  252. if (cpu->cfg.mmu_tlb_access <= 1) {
  253. qemu_log_mask(LOG_GUEST_ERROR,
  254. "Invalid access to MMU reg %d\n", rn);
  255. return;
  256. }
  257. if (v != env->mmu.regs[rn]) {
  258. mmu_change_pid(env, v);
  259. env->mmu.regs[rn] = v;
  260. }
  261. break;
  262. case MMU_R_TLBX:
  263. /* Bit 31 is read-only. */
  264. env->mmu.regs[rn] = deposit32(env->mmu.regs[rn], 0, 31, v);
  265. break;
  266. case MMU_R_TLBSX:
  267. {
  268. MicroBlazeMMULookup lu;
  269. int hit;
  270. if (cpu->cfg.mmu_tlb_access <= 1) {
  271. qemu_log_mask(LOG_GUEST_ERROR,
  272. "Invalid access to MMU reg %d\n", rn);
  273. return;
  274. }
  275. hit = mmu_translate(cpu, &lu, v & TLB_EPN_MASK,
  276. 0, cpu_mmu_index(env_cpu(env), false));
  277. if (hit) {
  278. env->mmu.regs[MMU_R_TLBX] = lu.idx;
  279. } else {
  280. env->mmu.regs[MMU_R_TLBX] |= R_TBLX_MISS_MASK;
  281. }
  282. break;
  283. }
  284. default:
  285. qemu_log_mask(LOG_GUEST_ERROR, "Invalid MMU register %d.\n", rn);
  286. break;
  287. }
  288. }
  289. void mmu_init(MicroBlazeMMU *mmu)
  290. {
  291. int i;
  292. for (i = 0; i < ARRAY_SIZE(mmu->regs); i++) {
  293. mmu->regs[i] = 0;
  294. }
  295. }