initial sparc64 support - sparc fixes (Blue Swirl)
[qemu] / target-sparc / helper.c
1 /*
2  *  sparc helpers
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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "exec.h"
21
22 //#define DEBUG_PCALL
23 //#define DEBUG_MMU
24
25 /* Sparc MMU emulation */
26 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
27                               int is_user, int is_softmmu);
28
29 /* thread support */
30
31 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
32
33 void cpu_lock(void)
34 {
35     spin_lock(&global_cpu_lock);
36 }
37
38 void cpu_unlock(void)
39 {
40     spin_unlock(&global_cpu_lock);
41 }
42
43 #if !defined(CONFIG_USER_ONLY) 
44
45 #define MMUSUFFIX _mmu
46 #define GETPC() (__builtin_return_address(0))
47
48 #define SHIFT 0
49 #include "softmmu_template.h"
50
51 #define SHIFT 1
52 #include "softmmu_template.h"
53
54 #define SHIFT 2
55 #include "softmmu_template.h"
56
57 #define SHIFT 3
58 #include "softmmu_template.h"
59
60
61 /* try to fill the TLB and return an exception if error. If retaddr is
62    NULL, it means that the function was called in C code (i.e. not
63    from generated code or from helper.c) */
64 /* XXX: fix it to restore all registers */
65 void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
66 {
67     TranslationBlock *tb;
68     int ret;
69     unsigned long pc;
70     CPUState *saved_env;
71
72     /* XXX: hack to restore env in all cases, even if not called from
73        generated code */
74     saved_env = env;
75     env = cpu_single_env;
76
77     ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, is_user, 1);
78     if (ret) {
79         if (retaddr) {
80             /* now we have a real cpu fault */
81             pc = (unsigned long)retaddr;
82             tb = tb_find_pc(pc);
83             if (tb) {
84                 /* the PC is inside the translated code. It means that we have
85                    a virtual CPU fault */
86                 cpu_restore_state(tb, env, pc, NULL);
87             }
88         }
89         raise_exception_err(ret, env->error_code);
90     }
91     env = saved_env;
92 }
93 #endif
94
95 static const int access_table[8][8] = {
96     { 0, 0, 0, 0, 2, 0, 3, 3 },
97     { 0, 0, 0, 0, 2, 0, 0, 0 },
98     { 2, 2, 0, 0, 0, 2, 3, 3 },
99     { 2, 2, 0, 0, 0, 2, 0, 0 },
100     { 2, 0, 2, 0, 2, 2, 3, 3 },
101     { 2, 0, 2, 0, 2, 0, 2, 0 },
102     { 2, 2, 2, 0, 2, 2, 3, 3 },
103     { 2, 2, 2, 0, 2, 2, 2, 0 }
104 };
105
106 /* 1 = write OK */
107 static const int rw_table[2][8] = {
108     { 0, 1, 0, 1, 0, 1, 0, 1 },
109     { 0, 1, 0, 1, 0, 0, 0, 0 }
110 };
111
112 int get_physical_address (CPUState *env, target_phys_addr_t *physical, int *prot,
113                           int *access_index, target_ulong address, int rw,
114                           int is_user)
115 {
116     int access_perms = 0;
117     target_phys_addr_t pde_ptr;
118     uint32_t pde;
119     target_ulong virt_addr;
120     int error_code = 0, is_dirty;
121     unsigned long page_offset;
122
123     virt_addr = address & TARGET_PAGE_MASK;
124     if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
125         *physical = address;
126         *prot = PAGE_READ | PAGE_WRITE;
127         return 0;
128     }
129
130     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
131     /* Context base + context number */
132     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
133     pde = ldl_phys(pde_ptr);
134
135     /* Ctx pde */
136     switch (pde & PTE_ENTRYTYPE_MASK) {
137     default:
138     case 0: /* Invalid */
139         return 1;
140     case 2: /* L0 PTE, maybe should not happen? */
141     case 3: /* Reserved */
142         return 4;
143     case 1: /* L0 PDE */
144         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
145         pde = ldl_phys(pde_ptr);
146
147         switch (pde & PTE_ENTRYTYPE_MASK) {
148         default:
149         case 0: /* Invalid */
150             return 1;
151         case 3: /* Reserved */
152             return 4;
153         case 1: /* L1 PDE */
154             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
155             pde = ldl_phys(pde_ptr);
156
157             switch (pde & PTE_ENTRYTYPE_MASK) {
158             default:
159             case 0: /* Invalid */
160                 return 1;
161             case 3: /* Reserved */
162                 return 4;
163             case 1: /* L2 PDE */
164                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
165                 pde = ldl_phys(pde_ptr);
166
167                 switch (pde & PTE_ENTRYTYPE_MASK) {
168                 default:
169                 case 0: /* Invalid */
170                     return 1;
171                 case 1: /* PDE, should not happen */
172                 case 3: /* Reserved */
173                     return 4;
174                 case 2: /* L3 PTE */
175                     virt_addr = address & TARGET_PAGE_MASK;
176                     page_offset = (address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1);
177                 }
178                 break;
179             case 2: /* L2 PTE */
180                 virt_addr = address & ~0x3ffff;
181                 page_offset = address & 0x3ffff;
182             }
183             break;
184         case 2: /* L1 PTE */
185             virt_addr = address & ~0xffffff;
186             page_offset = address & 0xffffff;
187         }
188     }
189
190     /* update page modified and dirty bits */
191     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
192     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
193         pde |= PG_ACCESSED_MASK;
194         if (is_dirty)
195             pde |= PG_MODIFIED_MASK;
196         stl_phys_notdirty(pde_ptr, pde);
197     }
198     /* check access */
199     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
200     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
201     error_code = access_table[*access_index][access_perms];
202     if (error_code)
203         return error_code;
204
205     /* the page can be put in the TLB */
206     *prot = PAGE_READ;
207     if (pde & PG_MODIFIED_MASK) {
208         /* only set write access if already dirty... otherwise wait
209            for dirty access */
210         if (rw_table[is_user][access_perms])
211                 *prot |= PAGE_WRITE;
212     }
213
214     /* Even if large ptes, we map only one 4KB page in the cache to
215        avoid filling it too fast */
216     *physical = ((pde & PTE_ADDR_MASK) << 4) + page_offset;
217     return 0;
218 }
219
220 /* Perform address translation */
221 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
222                               int is_user, int is_softmmu)
223 {
224     int exception = 0;
225     target_ulong virt_addr;
226     target_phys_addr_t paddr;
227     unsigned long vaddr;
228     int error_code = 0, prot, ret = 0, access_index;
229
230     if (env->user_mode_only) {
231         /* user mode only emulation */
232         error_code = -2;
233         goto do_fault_user;
234     }
235
236     error_code = get_physical_address(env, &paddr, &prot, &access_index, address, rw, is_user);
237     if (error_code == 0) {
238         virt_addr = address & TARGET_PAGE_MASK;
239         vaddr = virt_addr + ((address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1));
240         ret = tlb_set_page(env, vaddr, paddr, prot, is_user, is_softmmu);
241         return ret;
242     }
243
244     if (env->mmuregs[3]) /* Fault status register */
245         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
246     env->mmuregs[3] |= (access_index << 5) | (error_code << 2) | 2;
247     env->mmuregs[4] = address; /* Fault address register */
248
249     if (env->mmuregs[0] & MMU_NF || env->psret == 0) // No fault
250         return 0;
251  do_fault_user:
252     env->exception_index = exception;
253     env->error_code = error_code;
254     return error_code;
255 }
256
257 void memcpy32(target_ulong *dst, const target_ulong *src)
258 {
259     dst[0] = src[0];
260     dst[1] = src[1];
261     dst[2] = src[2];
262     dst[3] = src[3];
263     dst[4] = src[4];
264     dst[5] = src[5];
265     dst[6] = src[6];
266     dst[7] = src[7];
267 }
268
269 void set_cwp(int new_cwp)
270 {
271     /* put the modified wrap registers at their proper location */
272     if (env->cwp == (NWINDOWS - 1))
273         memcpy32(env->regbase, env->regbase + NWINDOWS * 16);
274     env->cwp = new_cwp;
275     /* put the wrap registers at their temporary location */
276     if (new_cwp == (NWINDOWS - 1))
277         memcpy32(env->regbase + NWINDOWS * 16, env->regbase);
278     env->regwptr = env->regbase + (new_cwp * 16);
279 }
280
281 void cpu_set_cwp(CPUState *env1, int new_cwp)
282 {
283     CPUState *saved_env;
284     saved_env = env;
285     env = env1;
286     set_cwp(new_cwp);
287     env = saved_env;
288 }
289
290 /*
291  * Begin execution of an interruption. is_int is TRUE if coming from
292  * the int instruction. next_eip is the EIP value AFTER the interrupt
293  * instruction. It is only relevant if is_int is TRUE.  
294  */
295 void do_interrupt(int intno, int is_int, int error_code, 
296                   unsigned int next_eip, int is_hw)
297 {
298     int cwp;
299
300 #ifdef DEBUG_PCALL
301     if (loglevel & CPU_LOG_INT) {
302         static int count;
303         fprintf(logfile, "%6d: v=%02x e=%04x i=%d pc=%08x npc=%08x SP=%08x\n",
304                     count, intno, error_code, is_int,
305                     env->pc,
306                     env->npc, env->regwptr[6]);
307 #if 1
308         cpu_dump_state(env, logfile, fprintf, 0);
309         {
310             int i;
311             uint8_t *ptr;
312
313             fprintf(logfile, "       code=");
314             ptr = (uint8_t *)env->pc;
315             for(i = 0; i < 16; i++) {
316                 fprintf(logfile, " %02x", ldub(ptr + i));
317             }
318             fprintf(logfile, "\n");
319         }
320 #endif
321         count++;
322     }
323 #endif
324 #if !defined(CONFIG_USER_ONLY) 
325     if (env->psret == 0) {
326         cpu_abort(cpu_single_env, "Trap while interrupts disabled, Error state");
327         return;
328     }
329 #endif
330     env->psret = 0;
331     cwp = (env->cwp - 1) & (NWINDOWS - 1); 
332     set_cwp(cwp);
333     if (intno & 0x80) {
334         env->regwptr[9] = env->pc;
335         env->regwptr[10] = env->npc;
336     } else {
337         env->regwptr[9] = env->pc - 4; // XXX?
338         env->regwptr[10] = env->pc;
339     }
340     env->psrps = env->psrs;
341     env->psrs = 1;
342     env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
343     env->pc = env->tbr;
344     env->npc = env->pc + 4;
345     env->exception_index = 0;
346 }
347
348 void raise_exception_err(int exception_index, int error_code)
349 {
350     raise_exception(exception_index);
351 }
352
353 target_ulong mmu_probe(target_ulong address, int mmulev)
354 {
355     target_phys_addr_t pde_ptr;
356     uint32_t pde;
357
358     /* Context base + context number */
359     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
360     pde = ldl_phys(pde_ptr);
361
362     switch (pde & PTE_ENTRYTYPE_MASK) {
363     default:
364     case 0: /* Invalid */
365     case 2: /* PTE, maybe should not happen? */
366     case 3: /* Reserved */
367         return 0;
368     case 1: /* L1 PDE */
369         if (mmulev == 3)
370             return pde;
371         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
372         pde = ldl_phys(pde_ptr);
373
374         switch (pde & PTE_ENTRYTYPE_MASK) {
375         default:
376         case 0: /* Invalid */
377         case 3: /* Reserved */
378             return 0;
379         case 2: /* L1 PTE */
380             return pde;
381         case 1: /* L2 PDE */
382             if (mmulev == 2)
383                 return pde;
384             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
385             pde = ldl_phys(pde_ptr);
386
387             switch (pde & PTE_ENTRYTYPE_MASK) {
388             default:
389             case 0: /* Invalid */
390             case 3: /* Reserved */
391                 return 0;
392             case 2: /* L2 PTE */
393                 return pde;
394             case 1: /* L3 PDE */
395                 if (mmulev == 1)
396                     return pde;
397                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
398                 pde = ldl_phys(pde_ptr);
399
400                 switch (pde & PTE_ENTRYTYPE_MASK) {
401                 default:
402                 case 0: /* Invalid */
403                 case 1: /* PDE, should not happen */
404                 case 3: /* Reserved */
405                     return 0;
406                 case 2: /* L3 PTE */
407                     return pde;
408                 }
409             }
410         }
411     }
412     return 0;
413 }
414
415 void dump_mmu(void)
416 {
417 #ifdef DEBUG_MMU
418      target_ulong va, va1, va2;
419      unsigned int n, m, o;
420      target_phys_addr_t pde_ptr, pa;
421     uint32_t pde;
422
423     printf("MMU dump:\n");
424     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
425     pde = ldl_phys(pde_ptr);
426     printf("Root ptr: " TARGET_FMT_lx ", ctx: %d\n", env->mmuregs[1] << 4, env->mmuregs[2]);
427     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
428         pde_ptr = mmu_probe(va, 2);
429         if (pde_ptr) {
430             pa = cpu_get_phys_page_debug(env, va);
431             printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PDE: " TARGET_FMT_lx "\n", va, pa, pde_ptr);
432             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
433                 pde_ptr = mmu_probe(va1, 1);
434                 if (pde_ptr) {
435                     pa = cpu_get_phys_page_debug(env, va1);
436                     printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PDE: " TARGET_FMT_lx "\n", va1, pa, pde_ptr);
437                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
438                         pde_ptr = mmu_probe(va2, 0);
439                         if (pde_ptr) {
440                             pa = cpu_get_phys_page_debug(env, va2);
441                             printf("  VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PTE: " TARGET_FMT_lx "\n", va2, pa, pde_ptr);
442                         }
443                     }
444                 }
445             }
446         }
447     }
448     printf("MMU dump ends\n");
449 #endif
450 }