Merge remote-tracking branch 'afaerber/cocoa-for-upstream' into staging
[qemu/qmp-unstable.git] / target-xtensa / op_helper.c
blob060561103109f645962369e462b7760341adbbf6
1 /*
2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "cpu.h"
29 #include "dyngen-exec.h"
30 #include "helpers.h"
31 #include "host-utils.h"
33 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
34 void *retaddr);
36 #define ALIGNED_ONLY
37 #define MMUSUFFIX _mmu
39 #define SHIFT 0
40 #include "softmmu_template.h"
42 #define SHIFT 1
43 #include "softmmu_template.h"
45 #define SHIFT 2
46 #include "softmmu_template.h"
48 #define SHIFT 3
49 #include "softmmu_template.h"
51 static void do_restore_state(void *pc_ptr)
53 TranslationBlock *tb;
54 uint32_t pc = (uint32_t)(intptr_t)pc_ptr;
56 tb = tb_find_pc(pc);
57 if (tb) {
58 cpu_restore_state(tb, env, pc);
62 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
63 void *retaddr)
65 if (xtensa_option_enabled(env->config, XTENSA_OPTION_UNALIGNED_EXCEPTION) &&
66 !xtensa_option_enabled(env->config, XTENSA_OPTION_HW_ALIGNMENT)) {
67 do_restore_state(retaddr);
68 HELPER(exception_cause_vaddr)(
69 env->pc, LOAD_STORE_ALIGNMENT_CAUSE, addr);
73 void tlb_fill(CPUState *env1, target_ulong vaddr, int is_write, int mmu_idx,
74 void *retaddr)
76 CPUState *saved_env = env;
78 env = env1;
80 uint32_t paddr;
81 uint32_t page_size;
82 unsigned access;
83 int ret = xtensa_get_physical_addr(env, vaddr, is_write, mmu_idx,
84 &paddr, &page_size, &access);
86 qemu_log("%s(%08x, %d, %d) -> %08x, ret = %d\n", __func__,
87 vaddr, is_write, mmu_idx, paddr, ret);
89 if (ret == 0) {
90 tlb_set_page(env,
91 vaddr & TARGET_PAGE_MASK,
92 paddr & TARGET_PAGE_MASK,
93 access, mmu_idx, page_size);
94 } else {
95 do_restore_state(retaddr);
96 HELPER(exception_cause_vaddr)(env->pc, ret, vaddr);
99 env = saved_env;
102 void HELPER(exception)(uint32_t excp)
104 env->exception_index = excp;
105 cpu_loop_exit(env);
108 void HELPER(exception_cause)(uint32_t pc, uint32_t cause)
110 uint32_t vector;
112 env->pc = pc;
113 if (env->sregs[PS] & PS_EXCM) {
114 if (env->config->ndepc) {
115 env->sregs[DEPC] = pc;
116 } else {
117 env->sregs[EPC1] = pc;
119 vector = EXC_DOUBLE;
120 } else {
121 env->sregs[EPC1] = pc;
122 vector = (env->sregs[PS] & PS_UM) ? EXC_USER : EXC_KERNEL;
125 env->sregs[EXCCAUSE] = cause;
126 env->sregs[PS] |= PS_EXCM;
128 HELPER(exception)(vector);
131 void HELPER(exception_cause_vaddr)(uint32_t pc, uint32_t cause, uint32_t vaddr)
133 env->sregs[EXCVADDR] = vaddr;
134 HELPER(exception_cause)(pc, cause);
137 uint32_t HELPER(nsa)(uint32_t v)
139 if (v & 0x80000000) {
140 v = ~v;
142 return v ? clz32(v) - 1 : 31;
145 uint32_t HELPER(nsau)(uint32_t v)
147 return v ? clz32(v) : 32;
150 static void copy_window_from_phys(CPUState *env,
151 uint32_t window, uint32_t phys, uint32_t n)
153 assert(phys < env->config->nareg);
154 if (phys + n <= env->config->nareg) {
155 memcpy(env->regs + window, env->phys_regs + phys,
156 n * sizeof(uint32_t));
157 } else {
158 uint32_t n1 = env->config->nareg - phys;
159 memcpy(env->regs + window, env->phys_regs + phys,
160 n1 * sizeof(uint32_t));
161 memcpy(env->regs + window + n1, env->phys_regs,
162 (n - n1) * sizeof(uint32_t));
166 static void copy_phys_from_window(CPUState *env,
167 uint32_t phys, uint32_t window, uint32_t n)
169 assert(phys < env->config->nareg);
170 if (phys + n <= env->config->nareg) {
171 memcpy(env->phys_regs + phys, env->regs + window,
172 n * sizeof(uint32_t));
173 } else {
174 uint32_t n1 = env->config->nareg - phys;
175 memcpy(env->phys_regs + phys, env->regs + window,
176 n1 * sizeof(uint32_t));
177 memcpy(env->phys_regs, env->regs + window + n1,
178 (n - n1) * sizeof(uint32_t));
183 static inline unsigned windowbase_bound(unsigned a, const CPUState *env)
185 return a & (env->config->nareg / 4 - 1);
188 static inline unsigned windowstart_bit(unsigned a, const CPUState *env)
190 return 1 << windowbase_bound(a, env);
193 void xtensa_sync_window_from_phys(CPUState *env)
195 copy_window_from_phys(env, 0, env->sregs[WINDOW_BASE] * 4, 16);
198 void xtensa_sync_phys_from_window(CPUState *env)
200 copy_phys_from_window(env, env->sregs[WINDOW_BASE] * 4, 0, 16);
203 static void rotate_window_abs(uint32_t position)
205 xtensa_sync_phys_from_window(env);
206 env->sregs[WINDOW_BASE] = windowbase_bound(position, env);
207 xtensa_sync_window_from_phys(env);
210 static void rotate_window(uint32_t delta)
212 rotate_window_abs(env->sregs[WINDOW_BASE] + delta);
215 void HELPER(wsr_windowbase)(uint32_t v)
217 rotate_window_abs(v);
220 void HELPER(entry)(uint32_t pc, uint32_t s, uint32_t imm)
222 int callinc = (env->sregs[PS] & PS_CALLINC) >> PS_CALLINC_SHIFT;
223 if (s > 3 || ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) != 0) {
224 qemu_log("Illegal entry instruction(pc = %08x), PS = %08x\n",
225 pc, env->sregs[PS]);
226 HELPER(exception_cause)(pc, ILLEGAL_INSTRUCTION_CAUSE);
227 } else {
228 env->regs[(callinc << 2) | (s & 3)] = env->regs[s] - (imm << 3);
229 rotate_window(callinc);
230 env->sregs[WINDOW_START] |=
231 windowstart_bit(env->sregs[WINDOW_BASE], env);
235 void HELPER(window_check)(uint32_t pc, uint32_t w)
237 uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
238 uint32_t windowstart = env->sregs[WINDOW_START];
239 uint32_t m, n;
241 if ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) {
242 return;
245 for (n = 1; ; ++n) {
246 if (n > w) {
247 return;
249 if (windowstart & windowstart_bit(windowbase + n, env)) {
250 break;
254 m = windowbase_bound(windowbase + n, env);
255 rotate_window(n);
256 env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
257 (windowbase << PS_OWB_SHIFT) | PS_EXCM;
258 env->sregs[EPC1] = env->pc = pc;
260 if (windowstart & windowstart_bit(m + 1, env)) {
261 HELPER(exception)(EXC_WINDOW_OVERFLOW4);
262 } else if (windowstart & windowstart_bit(m + 2, env)) {
263 HELPER(exception)(EXC_WINDOW_OVERFLOW8);
264 } else {
265 HELPER(exception)(EXC_WINDOW_OVERFLOW12);
269 uint32_t HELPER(retw)(uint32_t pc)
271 int n = (env->regs[0] >> 30) & 0x3;
272 int m = 0;
273 uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
274 uint32_t windowstart = env->sregs[WINDOW_START];
275 uint32_t ret_pc = 0;
277 if (windowstart & windowstart_bit(windowbase - 1, env)) {
278 m = 1;
279 } else if (windowstart & windowstart_bit(windowbase - 2, env)) {
280 m = 2;
281 } else if (windowstart & windowstart_bit(windowbase - 3, env)) {
282 m = 3;
285 if (n == 0 || (m != 0 && m != n) ||
286 ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) != 0) {
287 qemu_log("Illegal retw instruction(pc = %08x), "
288 "PS = %08x, m = %d, n = %d\n",
289 pc, env->sregs[PS], m, n);
290 HELPER(exception_cause)(pc, ILLEGAL_INSTRUCTION_CAUSE);
291 } else {
292 int owb = windowbase;
294 ret_pc = (pc & 0xc0000000) | (env->regs[0] & 0x3fffffff);
296 rotate_window(-n);
297 if (windowstart & windowstart_bit(env->sregs[WINDOW_BASE], env)) {
298 env->sregs[WINDOW_START] &= ~windowstart_bit(owb, env);
299 } else {
300 /* window underflow */
301 env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
302 (windowbase << PS_OWB_SHIFT) | PS_EXCM;
303 env->sregs[EPC1] = env->pc = pc;
305 if (n == 1) {
306 HELPER(exception)(EXC_WINDOW_UNDERFLOW4);
307 } else if (n == 2) {
308 HELPER(exception)(EXC_WINDOW_UNDERFLOW8);
309 } else if (n == 3) {
310 HELPER(exception)(EXC_WINDOW_UNDERFLOW12);
314 return ret_pc;
317 void HELPER(rotw)(uint32_t imm4)
319 rotate_window(imm4);
322 void HELPER(restore_owb)(void)
324 rotate_window_abs((env->sregs[PS] & PS_OWB) >> PS_OWB_SHIFT);
327 void HELPER(movsp)(uint32_t pc)
329 if ((env->sregs[WINDOW_START] &
330 (windowstart_bit(env->sregs[WINDOW_BASE] - 3, env) |
331 windowstart_bit(env->sregs[WINDOW_BASE] - 2, env) |
332 windowstart_bit(env->sregs[WINDOW_BASE] - 1, env))) == 0) {
333 HELPER(exception_cause)(pc, ALLOCA_CAUSE);
337 void HELPER(wsr_lbeg)(uint32_t v)
339 if (env->sregs[LBEG] != v) {
340 tb_invalidate_phys_page_range(
341 env->sregs[LEND] - 1, env->sregs[LEND], 0);
342 env->sregs[LBEG] = v;
346 void HELPER(wsr_lend)(uint32_t v)
348 if (env->sregs[LEND] != v) {
349 tb_invalidate_phys_page_range(
350 env->sregs[LEND] - 1, env->sregs[LEND], 0);
351 env->sregs[LEND] = v;
352 tb_invalidate_phys_page_range(
353 env->sregs[LEND] - 1, env->sregs[LEND], 0);
357 void HELPER(dump_state)(void)
359 cpu_dump_state(env, stderr, fprintf, 0);
362 void HELPER(waiti)(uint32_t pc, uint32_t intlevel)
364 env->pc = pc;
365 env->sregs[PS] = (env->sregs[PS] & ~PS_INTLEVEL) |
366 (intlevel << PS_INTLEVEL_SHIFT);
367 check_interrupts(env);
368 if (env->pending_irq_level) {
369 cpu_loop_exit(env);
370 return;
373 env->halt_clock = qemu_get_clock_ns(vm_clock);
374 env->halted = 1;
375 if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT)) {
376 xtensa_rearm_ccompare_timer(env);
378 HELPER(exception)(EXCP_HLT);
381 void HELPER(timer_irq)(uint32_t id, uint32_t active)
383 xtensa_timer_irq(env, id, active);
386 void HELPER(advance_ccount)(uint32_t d)
388 xtensa_advance_ccount(env, d);
391 void HELPER(check_interrupts)(CPUState *env)
393 check_interrupts(env);
396 void HELPER(wsr_rasid)(uint32_t v)
398 v = (v & 0xffffff00) | 0x1;
399 if (v != env->sregs[RASID]) {
400 env->sregs[RASID] = v;
401 tlb_flush(env, 1);
405 static uint32_t get_page_size(const CPUState *env, bool dtlb, uint32_t way)
407 uint32_t tlbcfg = env->sregs[dtlb ? DTLBCFG : ITLBCFG];
409 switch (way) {
410 case 4:
411 return (tlbcfg >> 16) & 0x3;
413 case 5:
414 return (tlbcfg >> 20) & 0x1;
416 case 6:
417 return (tlbcfg >> 24) & 0x1;
419 default:
420 return 0;
425 * Get bit mask for the virtual address bits translated by the TLB way
427 uint32_t xtensa_tlb_get_addr_mask(const CPUState *env, bool dtlb, uint32_t way)
429 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
430 bool varway56 = dtlb ?
431 env->config->dtlb.varway56 :
432 env->config->itlb.varway56;
434 switch (way) {
435 case 4:
436 return 0xfff00000 << get_page_size(env, dtlb, way) * 2;
438 case 5:
439 if (varway56) {
440 return 0xf8000000 << get_page_size(env, dtlb, way);
441 } else {
442 return 0xf8000000;
445 case 6:
446 if (varway56) {
447 return 0xf0000000 << (1 - get_page_size(env, dtlb, way));
448 } else {
449 return 0xf0000000;
452 default:
453 return 0xfffff000;
455 } else {
456 return REGION_PAGE_MASK;
461 * Get bit mask for the 'VPN without index' field.
462 * See ISA, 4.6.5.6, data format for RxTLB0
464 static uint32_t get_vpn_mask(const CPUState *env, bool dtlb, uint32_t way)
466 if (way < 4) {
467 bool is32 = (dtlb ?
468 env->config->dtlb.nrefillentries :
469 env->config->itlb.nrefillentries) == 32;
470 return is32 ? 0xffff8000 : 0xffffc000;
471 } else if (way == 4) {
472 return xtensa_tlb_get_addr_mask(env, dtlb, way) << 2;
473 } else if (way <= 6) {
474 uint32_t mask = xtensa_tlb_get_addr_mask(env, dtlb, way);
475 bool varway56 = dtlb ?
476 env->config->dtlb.varway56 :
477 env->config->itlb.varway56;
479 if (varway56) {
480 return mask << (way == 5 ? 2 : 3);
481 } else {
482 return mask << 1;
484 } else {
485 return 0xfffff000;
490 * Split virtual address into VPN (with index) and entry index
491 * for the given TLB way
493 void split_tlb_entry_spec_way(const CPUState *env, uint32_t v, bool dtlb,
494 uint32_t *vpn, uint32_t wi, uint32_t *ei)
496 bool varway56 = dtlb ?
497 env->config->dtlb.varway56 :
498 env->config->itlb.varway56;
500 if (!dtlb) {
501 wi &= 7;
504 if (wi < 4) {
505 bool is32 = (dtlb ?
506 env->config->dtlb.nrefillentries :
507 env->config->itlb.nrefillentries) == 32;
508 *ei = (v >> 12) & (is32 ? 0x7 : 0x3);
509 } else {
510 switch (wi) {
511 case 4:
513 uint32_t eibase = 20 + get_page_size(env, dtlb, wi) * 2;
514 *ei = (v >> eibase) & 0x3;
516 break;
518 case 5:
519 if (varway56) {
520 uint32_t eibase = 27 + get_page_size(env, dtlb, wi);
521 *ei = (v >> eibase) & 0x3;
522 } else {
523 *ei = (v >> 27) & 0x1;
525 break;
527 case 6:
528 if (varway56) {
529 uint32_t eibase = 29 - get_page_size(env, dtlb, wi);
530 *ei = (v >> eibase) & 0x7;
531 } else {
532 *ei = (v >> 28) & 0x1;
534 break;
536 default:
537 *ei = 0;
538 break;
541 *vpn = v & xtensa_tlb_get_addr_mask(env, dtlb, wi);
545 * Split TLB address into TLB way, entry index and VPN (with index).
546 * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
548 static void split_tlb_entry_spec(uint32_t v, bool dtlb,
549 uint32_t *vpn, uint32_t *wi, uint32_t *ei)
551 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
552 *wi = v & (dtlb ? 0xf : 0x7);
553 split_tlb_entry_spec_way(env, v, dtlb, vpn, *wi, ei);
554 } else {
555 *vpn = v & REGION_PAGE_MASK;
556 *wi = 0;
557 *ei = (v >> 29) & 0x7;
561 static xtensa_tlb_entry *get_tlb_entry(uint32_t v, bool dtlb, uint32_t *pwi)
563 uint32_t vpn;
564 uint32_t wi;
565 uint32_t ei;
567 split_tlb_entry_spec(v, dtlb, &vpn, &wi, &ei);
568 if (pwi) {
569 *pwi = wi;
571 return xtensa_tlb_get_entry(env, dtlb, wi, ei);
574 uint32_t HELPER(rtlb0)(uint32_t v, uint32_t dtlb)
576 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
577 uint32_t wi;
578 const xtensa_tlb_entry *entry = get_tlb_entry(v, dtlb, &wi);
579 return (entry->vaddr & get_vpn_mask(env, dtlb, wi)) | entry->asid;
580 } else {
581 return v & REGION_PAGE_MASK;
585 uint32_t HELPER(rtlb1)(uint32_t v, uint32_t dtlb)
587 const xtensa_tlb_entry *entry = get_tlb_entry(v, dtlb, NULL);
588 return entry->paddr | entry->attr;
591 void HELPER(itlb)(uint32_t v, uint32_t dtlb)
593 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
594 uint32_t wi;
595 xtensa_tlb_entry *entry = get_tlb_entry(v, dtlb, &wi);
596 if (entry->variable && entry->asid) {
597 tlb_flush_page(env, entry->vaddr);
598 entry->asid = 0;
603 uint32_t HELPER(ptlb)(uint32_t v, uint32_t dtlb)
605 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
606 uint32_t wi;
607 uint32_t ei;
608 uint8_t ring;
609 int res = xtensa_tlb_lookup(env, v, dtlb, &wi, &ei, &ring);
611 switch (res) {
612 case 0:
613 if (ring >= xtensa_get_ring(env)) {
614 return (v & 0xfffff000) | wi | (dtlb ? 0x10 : 0x8);
616 break;
618 case INST_TLB_MULTI_HIT_CAUSE:
619 case LOAD_STORE_TLB_MULTI_HIT_CAUSE:
620 HELPER(exception_cause_vaddr)(env->pc, res, v);
621 break;
623 return 0;
624 } else {
625 return (v & REGION_PAGE_MASK) | 0x1;
629 void xtensa_tlb_set_entry(CPUState *env, bool dtlb,
630 unsigned wi, unsigned ei, uint32_t vpn, uint32_t pte)
632 xtensa_tlb_entry *entry = xtensa_tlb_get_entry(env, dtlb, wi, ei);
634 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
635 if (entry->variable) {
636 if (entry->asid) {
637 tlb_flush_page(env, entry->vaddr);
639 entry->vaddr = vpn;
640 entry->paddr = pte & xtensa_tlb_get_addr_mask(env, dtlb, wi);
641 entry->asid = (env->sregs[RASID] >> ((pte >> 1) & 0x18)) & 0xff;
642 entry->attr = pte & 0xf;
643 } else {
644 qemu_log("%s %d, %d, %d trying to set immutable entry\n",
645 __func__, dtlb, wi, ei);
647 } else {
648 tlb_flush_page(env, entry->vaddr);
649 if (xtensa_option_enabled(env->config,
650 XTENSA_OPTION_REGION_TRANSLATION)) {
651 entry->paddr = pte & REGION_PAGE_MASK;
653 entry->attr = pte & 0xf;
657 void HELPER(wtlb)(uint32_t p, uint32_t v, uint32_t dtlb)
659 uint32_t vpn;
660 uint32_t wi;
661 uint32_t ei;
662 split_tlb_entry_spec(v, dtlb, &vpn, &wi, &ei);
663 xtensa_tlb_set_entry(env, dtlb, wi, ei, vpn, p);