4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define raise_exception_err(a, b)\
27 fprintf(logfile, "raise_exception line=%d\n", __LINE__);\
28 (raise_exception_err)(a, b);\
32 /* Sparc MMU emulation */
33 int cpu_sparc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
34 int is_user, int is_softmmu);
39 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
43 spin_lock(&global_cpu_lock);
48 spin_unlock(&global_cpu_lock);
52 void cpu_loop_exit(void)
54 /* NOTE: the register at this point must be saved by hand because
55 longjmp restore them */
56 longjmp(env->jmp_env, 1);
60 #if !defined(CONFIG_USER_ONLY)
62 #define MMUSUFFIX _mmu
63 #define GETPC() (__builtin_return_address(0))
66 #include "softmmu_template.h"
69 #include "softmmu_template.h"
72 #include "softmmu_template.h"
75 #include "softmmu_template.h"
78 /* try to fill the TLB and return an exception if error. If retaddr is
79 NULL, it means that the function was called in C code (i.e. not
80 from generated code or from helper.c) */
81 /* XXX: fix it to restore all registers */
82 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
89 /* XXX: hack to restore env in all cases, even if not called from
94 ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, is_user, 1);
97 /* now we have a real cpu fault */
98 pc = (unsigned long)retaddr;
101 /* the PC is inside the translated code. It means that we have
102 a virtual CPU fault */
103 cpu_restore_state(tb, env, pc, NULL);
106 raise_exception_err(ret, env->error_code);
112 static const int access_table[8][8] = {
113 { 0, 0, 0, 0, 2, 0, 3, 3 },
114 { 0, 0, 0, 0, 2, 0, 0, 0 },
115 { 2, 2, 0, 0, 0, 2, 3, 3 },
116 { 2, 2, 0, 0, 0, 2, 0, 0 },
117 { 2, 0, 2, 0, 2, 2, 3, 3 },
118 { 2, 0, 2, 0, 2, 0, 2, 0 },
119 { 2, 2, 2, 0, 2, 2, 3, 3 },
120 { 2, 2, 2, 0, 2, 2, 2, 0 }
124 static const int rw_table[2][8] = {
125 { 0, 1, 0, 1, 0, 1, 0, 1 },
126 { 0, 1, 0, 1, 0, 0, 0, 0 }
130 /* Perform address translation */
131 int cpu_sparc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
132 int is_user, int is_softmmu)
135 int access_type, access_perms = 0, access_index = 0;
137 uint32_t pde, virt_addr;
138 int error_code = 0, is_dirty, prot, ret = 0;
139 unsigned long paddr, vaddr, page_offset;
141 access_type = env->access_type;
142 if (env->user_mode_only) {
143 /* user mode only emulation */
148 virt_addr = address & TARGET_PAGE_MASK;
149 if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
151 page_offset = address & (TARGET_PAGE_SIZE - 1);
152 prot = PAGE_READ | PAGE_WRITE;
156 /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
157 /* Context base + context number */
158 pde_ptr = phys_ram_base + (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
159 env->access_type = ACCESS_MMU;
160 pde = ldl_raw(pde_ptr);
163 switch (pde & PTE_ENTRYTYPE_MASK) {
164 case 0: /* Invalid */
167 case 2: /* PTE, maybe should not happen? */
168 case 3: /* Reserved */
172 pde_ptr = phys_ram_base + ((address >> 22) & ~3) + ((pde & ~3) << 4);
173 pde = ldl_raw(pde_ptr);
175 switch (pde & PTE_ENTRYTYPE_MASK) {
176 case 0: /* Invalid */
179 case 3: /* Reserved */
183 pde_ptr = phys_ram_base + ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
184 pde = ldl_raw(pde_ptr);
186 switch (pde & PTE_ENTRYTYPE_MASK) {
187 case 0: /* Invalid */
190 case 3: /* Reserved */
194 pde_ptr = phys_ram_base + ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
195 pde = ldl_raw(pde_ptr);
197 switch (pde & PTE_ENTRYTYPE_MASK) {
198 case 0: /* Invalid */
201 case 1: /* PDE, should not happen */
202 case 3: /* Reserved */
206 virt_addr = address & TARGET_PAGE_MASK;
207 page_offset = (address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1);
211 virt_addr = address & ~0x3ffff;
212 page_offset = address & 0x3ffff;
216 virt_addr = address & ~0xffffff;
217 page_offset = address & 0xffffff;
221 /* update page modified and dirty bits */
222 is_dirty = rw && !(pde & PG_MODIFIED_MASK);
223 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
224 pde |= PG_ACCESSED_MASK;
226 pde |= PG_MODIFIED_MASK;
227 stl_raw(pde_ptr, pde);
231 access_index = (rw << 2) | ((access_type == ACCESS_CODE)? 2 : 0) | (is_user? 0 : 1);
232 access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
233 error_code = access_table[access_index][access_perms];
237 /* the page can be put in the TLB */
239 if (pde & PG_MODIFIED_MASK) {
240 /* only set write access if already dirty... otherwise wait
242 if (rw_table[is_user][access_perms])
246 /* Even if large ptes, we map only one 4KB page in the cache to
247 avoid filling it too fast */
248 virt_addr = address & TARGET_PAGE_MASK;
249 paddr = ((pde & PTE_ADDR_MASK) << 4) + page_offset;
252 env->access_type = access_type;
253 vaddr = virt_addr + ((address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1));
255 ret = tlb_set_page(env, vaddr, paddr, prot, is_user, is_softmmu);
259 env->access_type = access_type;
260 if (env->mmuregs[3]) /* Fault status register */
261 env->mmuregs[3] = 1; /* overflow (not read before another fault) */
262 env->mmuregs[3] |= (access_index << 5) | (error_code << 2) | 2;
263 env->mmuregs[4] = address; /* Fault address register */
265 if (env->mmuregs[0] & MMU_NF) // No fault
268 env->exception_index = exception;
269 env->error_code = error_code;
273 void memcpy32(uint32_t *dst, const uint32_t *src)
285 void set_cwp(int new_cwp)
287 /* put the modified wrap registers at their proper location */
288 if (env->cwp == (NWINDOWS - 1))
289 memcpy32(env->regbase, env->regbase + NWINDOWS * 16);
291 /* put the wrap registers at their temporary location */
292 if (new_cwp == (NWINDOWS - 1))
293 memcpy32(env->regbase + NWINDOWS * 16, env->regbase);
294 env->regwptr = env->regbase + (new_cwp * 16);
298 * Begin execution of an interruption. is_int is TRUE if coming from
299 * the int instruction. next_eip is the EIP value AFTER the interrupt
300 * instruction. It is only relevant if is_int is TRUE.
302 void do_interrupt(int intno, int is_int, int error_code,
303 unsigned int next_eip, int is_hw)
308 if (loglevel & CPU_LOG_INT) {
310 fprintf(logfile, "%6d: v=%02x e=%04x i=%d pc=%08x npc=%08x SP=%08x\n",
311 count, intno, error_code, is_int,
313 env->npc, env->gregs[7]);
315 cpu_sparc_dump_state(env, logfile, 0);
319 fprintf(logfile, " code=");
321 for(i = 0; i < 16; i++) {
322 fprintf(logfile, " %02x", ldub(ptr + i));
324 fprintf(logfile, "\n");
331 cwp = (env->cwp - 1) & (NWINDOWS - 1);
333 env->regwptr[9] = env->pc;
334 env->regwptr[10] = env->npc;
335 env->psrps = env->psrs;
337 env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
339 env->npc = env->pc + 4;
340 env->exception_index = 0;
343 void raise_exception_err(int exception_index, int error_code)
345 raise_exception(exception_index);