2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
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.
30 #include "qemu/host-utils.h"
31 #include "exec/softmmu_exec.h"
33 static void do_unaligned_access(CPUXtensaState
*env
,
34 target_ulong addr
, int is_write
, int is_user
, uintptr_t retaddr
);
37 #define MMUSUFFIX _mmu
40 #include "exec/softmmu_template.h"
43 #include "exec/softmmu_template.h"
46 #include "exec/softmmu_template.h"
49 #include "exec/softmmu_template.h"
51 static void do_unaligned_access(CPUXtensaState
*env
,
52 target_ulong addr
, int is_write
, int is_user
, uintptr_t retaddr
)
54 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_UNALIGNED_EXCEPTION
) &&
55 !xtensa_option_enabled(env
->config
, XTENSA_OPTION_HW_ALIGNMENT
)) {
56 cpu_restore_state(env
, retaddr
);
57 HELPER(exception_cause_vaddr
)(env
,
58 env
->pc
, LOAD_STORE_ALIGNMENT_CAUSE
, addr
);
62 void tlb_fill(CPUXtensaState
*env
,
63 target_ulong vaddr
, int is_write
, int mmu_idx
, uintptr_t retaddr
)
68 int ret
= xtensa_get_physical_addr(env
, true, vaddr
, is_write
, mmu_idx
,
69 &paddr
, &page_size
, &access
);
71 qemu_log("%s(%08x, %d, %d) -> %08x, ret = %d\n", __func__
,
72 vaddr
, is_write
, mmu_idx
, paddr
, ret
);
76 vaddr
& TARGET_PAGE_MASK
,
77 paddr
& TARGET_PAGE_MASK
,
78 access
, mmu_idx
, page_size
);
80 cpu_restore_state(env
, retaddr
);
81 HELPER(exception_cause_vaddr
)(env
, env
->pc
, ret
, vaddr
);
85 static void tb_invalidate_virtual_addr(CPUXtensaState
*env
, uint32_t vaddr
)
90 int ret
= xtensa_get_physical_addr(env
, false, vaddr
, 2, 0,
91 &paddr
, &page_size
, &access
);
93 tb_invalidate_phys_addr(paddr
);
97 void HELPER(exception
)(CPUXtensaState
*env
, uint32_t excp
)
99 env
->exception_index
= excp
;
100 if (excp
== EXCP_DEBUG
) {
101 env
->exception_taken
= 0;
106 void HELPER(exception_cause
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
111 if (env
->sregs
[PS
] & PS_EXCM
) {
112 if (env
->config
->ndepc
) {
113 env
->sregs
[DEPC
] = pc
;
115 env
->sregs
[EPC1
] = pc
;
119 env
->sregs
[EPC1
] = pc
;
120 vector
= (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
123 env
->sregs
[EXCCAUSE
] = cause
;
124 env
->sregs
[PS
] |= PS_EXCM
;
126 HELPER(exception
)(env
, vector
);
129 void HELPER(exception_cause_vaddr
)(CPUXtensaState
*env
,
130 uint32_t pc
, uint32_t cause
, uint32_t vaddr
)
132 env
->sregs
[EXCVADDR
] = vaddr
;
133 HELPER(exception_cause
)(env
, pc
, cause
);
136 void debug_exception_env(CPUXtensaState
*env
, uint32_t cause
)
138 if (xtensa_get_cintlevel(env
) < env
->config
->debug_level
) {
139 HELPER(debug_exception
)(env
, env
->pc
, cause
);
143 void HELPER(debug_exception
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
145 unsigned level
= env
->config
->debug_level
;
148 env
->sregs
[DEBUGCAUSE
] = cause
;
149 env
->sregs
[EPC1
+ level
- 1] = pc
;
150 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
151 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) | PS_EXCM
|
152 (level
<< PS_INTLEVEL_SHIFT
);
153 HELPER(exception
)(env
, EXC_DEBUG
);
156 uint32_t HELPER(nsa
)(uint32_t v
)
158 if (v
& 0x80000000) {
161 return v
? clz32(v
) - 1 : 31;
164 uint32_t HELPER(nsau
)(uint32_t v
)
166 return v
? clz32(v
) : 32;
169 static void copy_window_from_phys(CPUXtensaState
*env
,
170 uint32_t window
, uint32_t phys
, uint32_t n
)
172 assert(phys
< env
->config
->nareg
);
173 if (phys
+ n
<= env
->config
->nareg
) {
174 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
175 n
* sizeof(uint32_t));
177 uint32_t n1
= env
->config
->nareg
- phys
;
178 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
179 n1
* sizeof(uint32_t));
180 memcpy(env
->regs
+ window
+ n1
, env
->phys_regs
,
181 (n
- n1
) * sizeof(uint32_t));
185 static void copy_phys_from_window(CPUXtensaState
*env
,
186 uint32_t phys
, uint32_t window
, uint32_t n
)
188 assert(phys
< env
->config
->nareg
);
189 if (phys
+ n
<= env
->config
->nareg
) {
190 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
191 n
* sizeof(uint32_t));
193 uint32_t n1
= env
->config
->nareg
- phys
;
194 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
195 n1
* sizeof(uint32_t));
196 memcpy(env
->phys_regs
, env
->regs
+ window
+ n1
,
197 (n
- n1
) * sizeof(uint32_t));
202 static inline unsigned windowbase_bound(unsigned a
, const CPUXtensaState
*env
)
204 return a
& (env
->config
->nareg
/ 4 - 1);
207 static inline unsigned windowstart_bit(unsigned a
, const CPUXtensaState
*env
)
209 return 1 << windowbase_bound(a
, env
);
212 void xtensa_sync_window_from_phys(CPUXtensaState
*env
)
214 copy_window_from_phys(env
, 0, env
->sregs
[WINDOW_BASE
] * 4, 16);
217 void xtensa_sync_phys_from_window(CPUXtensaState
*env
)
219 copy_phys_from_window(env
, env
->sregs
[WINDOW_BASE
] * 4, 0, 16);
222 static void rotate_window_abs(CPUXtensaState
*env
, uint32_t position
)
224 xtensa_sync_phys_from_window(env
);
225 env
->sregs
[WINDOW_BASE
] = windowbase_bound(position
, env
);
226 xtensa_sync_window_from_phys(env
);
229 static void rotate_window(CPUXtensaState
*env
, uint32_t delta
)
231 rotate_window_abs(env
, env
->sregs
[WINDOW_BASE
] + delta
);
234 void HELPER(wsr_windowbase
)(CPUXtensaState
*env
, uint32_t v
)
236 rotate_window_abs(env
, v
);
239 void HELPER(entry
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t s
, uint32_t imm
)
241 int callinc
= (env
->sregs
[PS
] & PS_CALLINC
) >> PS_CALLINC_SHIFT
;
242 if (s
> 3 || ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
243 qemu_log("Illegal entry instruction(pc = %08x), PS = %08x\n",
245 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
247 env
->regs
[(callinc
<< 2) | (s
& 3)] = env
->regs
[s
] - (imm
<< 3);
248 rotate_window(env
, callinc
);
249 env
->sregs
[WINDOW_START
] |=
250 windowstart_bit(env
->sregs
[WINDOW_BASE
], env
);
254 void HELPER(window_check
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t w
)
256 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
257 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
260 if ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) {
268 if (windowstart
& windowstart_bit(windowbase
+ n
, env
)) {
273 m
= windowbase_bound(windowbase
+ n
, env
);
274 rotate_window(env
, n
);
275 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
276 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
277 env
->sregs
[EPC1
] = env
->pc
= pc
;
279 if (windowstart
& windowstart_bit(m
+ 1, env
)) {
280 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW4
);
281 } else if (windowstart
& windowstart_bit(m
+ 2, env
)) {
282 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW8
);
284 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW12
);
288 uint32_t HELPER(retw
)(CPUXtensaState
*env
, uint32_t pc
)
290 int n
= (env
->regs
[0] >> 30) & 0x3;
292 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
293 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
296 if (windowstart
& windowstart_bit(windowbase
- 1, env
)) {
298 } else if (windowstart
& windowstart_bit(windowbase
- 2, env
)) {
300 } else if (windowstart
& windowstart_bit(windowbase
- 3, env
)) {
304 if (n
== 0 || (m
!= 0 && m
!= n
) ||
305 ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
306 qemu_log("Illegal retw instruction(pc = %08x), "
307 "PS = %08x, m = %d, n = %d\n",
308 pc
, env
->sregs
[PS
], m
, n
);
309 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
311 int owb
= windowbase
;
313 ret_pc
= (pc
& 0xc0000000) | (env
->regs
[0] & 0x3fffffff);
315 rotate_window(env
, -n
);
316 if (windowstart
& windowstart_bit(env
->sregs
[WINDOW_BASE
], env
)) {
317 env
->sregs
[WINDOW_START
] &= ~windowstart_bit(owb
, env
);
319 /* window underflow */
320 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
321 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
322 env
->sregs
[EPC1
] = env
->pc
= pc
;
325 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW4
);
327 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW8
);
329 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW12
);
336 void HELPER(rotw
)(CPUXtensaState
*env
, uint32_t imm4
)
338 rotate_window(env
, imm4
);
341 void HELPER(restore_owb
)(CPUXtensaState
*env
)
343 rotate_window_abs(env
, (env
->sregs
[PS
] & PS_OWB
) >> PS_OWB_SHIFT
);
346 void HELPER(movsp
)(CPUXtensaState
*env
, uint32_t pc
)
348 if ((env
->sregs
[WINDOW_START
] &
349 (windowstart_bit(env
->sregs
[WINDOW_BASE
] - 3, env
) |
350 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 2, env
) |
351 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 1, env
))) == 0) {
352 HELPER(exception_cause
)(env
, pc
, ALLOCA_CAUSE
);
356 void HELPER(wsr_lbeg
)(CPUXtensaState
*env
, uint32_t v
)
358 if (env
->sregs
[LBEG
] != v
) {
359 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
360 env
->sregs
[LBEG
] = v
;
364 void HELPER(wsr_lend
)(CPUXtensaState
*env
, uint32_t v
)
366 if (env
->sregs
[LEND
] != v
) {
367 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
368 env
->sregs
[LEND
] = v
;
369 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
373 void HELPER(dump_state
)(CPUXtensaState
*env
)
375 XtensaCPU
*cpu
= xtensa_env_get_cpu(env
);
377 cpu_dump_state(CPU(cpu
), stderr
, fprintf
, 0);
380 void HELPER(waiti
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t intlevel
)
385 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) |
386 (intlevel
<< PS_INTLEVEL_SHIFT
);
387 check_interrupts(env
);
388 if (env
->pending_irq_level
) {
393 cpu
= CPU(xtensa_env_get_cpu(env
));
394 env
->halt_clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
396 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_TIMER_INTERRUPT
)) {
397 xtensa_rearm_ccompare_timer(env
);
399 HELPER(exception
)(env
, EXCP_HLT
);
402 void HELPER(timer_irq
)(CPUXtensaState
*env
, uint32_t id
, uint32_t active
)
404 xtensa_timer_irq(env
, id
, active
);
407 void HELPER(advance_ccount
)(CPUXtensaState
*env
, uint32_t d
)
409 xtensa_advance_ccount(env
, d
);
412 void HELPER(check_interrupts
)(CPUXtensaState
*env
)
414 check_interrupts(env
);
418 * Check vaddr accessibility/cache attributes and raise an exception if
419 * specified by the ATOMCTL SR.
421 * Note: local memory exclusion is not implemented
423 void HELPER(check_atomctl
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t vaddr
)
425 uint32_t paddr
, page_size
, access
;
426 uint32_t atomctl
= env
->sregs
[ATOMCTL
];
427 int rc
= xtensa_get_physical_addr(env
, true, vaddr
, 1,
428 xtensa_get_cring(env
), &paddr
, &page_size
, &access
);
431 * s32c1i never causes LOAD_PROHIBITED_CAUSE exceptions,
432 * see opcode description in the ISA
435 (access
& (PAGE_READ
| PAGE_WRITE
)) != (PAGE_READ
| PAGE_WRITE
)) {
436 rc
= STORE_PROHIBITED_CAUSE
;
440 HELPER(exception_cause_vaddr
)(env
, pc
, rc
, vaddr
);
444 * When data cache is not configured use ATOMCTL bypass field.
445 * See ISA, 4.3.12.4 The Atomic Operation Control Register (ATOMCTL)
446 * under the Conditional Store Option.
448 if (!xtensa_option_enabled(env
->config
, XTENSA_OPTION_DCACHE
)) {
449 access
= PAGE_CACHE_BYPASS
;
452 switch (access
& PAGE_CACHE_MASK
) {
459 case PAGE_CACHE_BYPASS
:
460 if ((atomctl
& 0x3) == 0) {
461 HELPER(exception_cause_vaddr
)(env
, pc
,
462 LOAD_STORE_ERROR_CAUSE
, vaddr
);
466 case PAGE_CACHE_ISOLATE
:
467 HELPER(exception_cause_vaddr
)(env
, pc
,
468 LOAD_STORE_ERROR_CAUSE
, vaddr
);
476 void HELPER(wsr_rasid
)(CPUXtensaState
*env
, uint32_t v
)
478 v
= (v
& 0xffffff00) | 0x1;
479 if (v
!= env
->sregs
[RASID
]) {
480 env
->sregs
[RASID
] = v
;
485 static uint32_t get_page_size(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
487 uint32_t tlbcfg
= env
->sregs
[dtlb
? DTLBCFG
: ITLBCFG
];
491 return (tlbcfg
>> 16) & 0x3;
494 return (tlbcfg
>> 20) & 0x1;
497 return (tlbcfg
>> 24) & 0x1;
505 * Get bit mask for the virtual address bits translated by the TLB way
507 uint32_t xtensa_tlb_get_addr_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
509 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
510 bool varway56
= dtlb
?
511 env
->config
->dtlb
.varway56
:
512 env
->config
->itlb
.varway56
;
516 return 0xfff00000 << get_page_size(env
, dtlb
, way
) * 2;
520 return 0xf8000000 << get_page_size(env
, dtlb
, way
);
527 return 0xf0000000 << (1 - get_page_size(env
, dtlb
, way
));
536 return REGION_PAGE_MASK
;
541 * Get bit mask for the 'VPN without index' field.
542 * See ISA, 4.6.5.6, data format for RxTLB0
544 static uint32_t get_vpn_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
548 env
->config
->dtlb
.nrefillentries
:
549 env
->config
->itlb
.nrefillentries
) == 32;
550 return is32
? 0xffff8000 : 0xffffc000;
551 } else if (way
== 4) {
552 return xtensa_tlb_get_addr_mask(env
, dtlb
, way
) << 2;
553 } else if (way
<= 6) {
554 uint32_t mask
= xtensa_tlb_get_addr_mask(env
, dtlb
, way
);
555 bool varway56
= dtlb
?
556 env
->config
->dtlb
.varway56
:
557 env
->config
->itlb
.varway56
;
560 return mask
<< (way
== 5 ? 2 : 3);
570 * Split virtual address into VPN (with index) and entry index
571 * for the given TLB way
573 void split_tlb_entry_spec_way(const CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
574 uint32_t *vpn
, uint32_t wi
, uint32_t *ei
)
576 bool varway56
= dtlb
?
577 env
->config
->dtlb
.varway56
:
578 env
->config
->itlb
.varway56
;
586 env
->config
->dtlb
.nrefillentries
:
587 env
->config
->itlb
.nrefillentries
) == 32;
588 *ei
= (v
>> 12) & (is32
? 0x7 : 0x3);
593 uint32_t eibase
= 20 + get_page_size(env
, dtlb
, wi
) * 2;
594 *ei
= (v
>> eibase
) & 0x3;
600 uint32_t eibase
= 27 + get_page_size(env
, dtlb
, wi
);
601 *ei
= (v
>> eibase
) & 0x3;
603 *ei
= (v
>> 27) & 0x1;
609 uint32_t eibase
= 29 - get_page_size(env
, dtlb
, wi
);
610 *ei
= (v
>> eibase
) & 0x7;
612 *ei
= (v
>> 28) & 0x1;
621 *vpn
= v
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
625 * Split TLB address into TLB way, entry index and VPN (with index).
626 * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
628 static void split_tlb_entry_spec(CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
629 uint32_t *vpn
, uint32_t *wi
, uint32_t *ei
)
631 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
632 *wi
= v
& (dtlb
? 0xf : 0x7);
633 split_tlb_entry_spec_way(env
, v
, dtlb
, vpn
, *wi
, ei
);
635 *vpn
= v
& REGION_PAGE_MASK
;
637 *ei
= (v
>> 29) & 0x7;
641 static xtensa_tlb_entry
*get_tlb_entry(CPUXtensaState
*env
,
642 uint32_t v
, bool dtlb
, uint32_t *pwi
)
648 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
652 return xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
655 uint32_t HELPER(rtlb0
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
657 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
659 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
660 return (entry
->vaddr
& get_vpn_mask(env
, dtlb
, wi
)) | entry
->asid
;
662 return v
& REGION_PAGE_MASK
;
666 uint32_t HELPER(rtlb1
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
668 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, NULL
);
669 return entry
->paddr
| entry
->attr
;
672 void HELPER(itlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
674 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
676 xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
677 if (entry
->variable
&& entry
->asid
) {
678 tlb_flush_page(env
, entry
->vaddr
);
684 uint32_t HELPER(ptlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
686 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
690 int res
= xtensa_tlb_lookup(env
, v
, dtlb
, &wi
, &ei
, &ring
);
694 if (ring
>= xtensa_get_ring(env
)) {
695 return (v
& 0xfffff000) | wi
| (dtlb
? 0x10 : 0x8);
699 case INST_TLB_MULTI_HIT_CAUSE
:
700 case LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
701 HELPER(exception_cause_vaddr
)(env
, env
->pc
, res
, v
);
706 return (v
& REGION_PAGE_MASK
) | 0x1;
710 void xtensa_tlb_set_entry_mmu(const CPUXtensaState
*env
,
711 xtensa_tlb_entry
*entry
, bool dtlb
,
712 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
715 entry
->paddr
= pte
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
716 entry
->asid
= (env
->sregs
[RASID
] >> ((pte
>> 1) & 0x18)) & 0xff;
717 entry
->attr
= pte
& 0xf;
720 void xtensa_tlb_set_entry(CPUXtensaState
*env
, bool dtlb
,
721 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
723 xtensa_tlb_entry
*entry
= xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
725 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
726 if (entry
->variable
) {
728 tlb_flush_page(env
, entry
->vaddr
);
730 xtensa_tlb_set_entry_mmu(env
, entry
, dtlb
, wi
, ei
, vpn
, pte
);
731 tlb_flush_page(env
, entry
->vaddr
);
733 qemu_log("%s %d, %d, %d trying to set immutable entry\n",
734 __func__
, dtlb
, wi
, ei
);
737 tlb_flush_page(env
, entry
->vaddr
);
738 if (xtensa_option_enabled(env
->config
,
739 XTENSA_OPTION_REGION_TRANSLATION
)) {
740 entry
->paddr
= pte
& REGION_PAGE_MASK
;
742 entry
->attr
= pte
& 0xf;
746 void HELPER(wtlb
)(CPUXtensaState
*env
, uint32_t p
, uint32_t v
, uint32_t dtlb
)
751 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
752 xtensa_tlb_set_entry(env
, dtlb
, wi
, ei
, vpn
, p
);
756 void HELPER(wsr_ibreakenable
)(CPUXtensaState
*env
, uint32_t v
)
758 uint32_t change
= v
^ env
->sregs
[IBREAKENABLE
];
761 for (i
= 0; i
< env
->config
->nibreak
; ++i
) {
762 if (change
& (1 << i
)) {
763 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
766 env
->sregs
[IBREAKENABLE
] = v
& ((1 << env
->config
->nibreak
) - 1);
769 void HELPER(wsr_ibreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
771 if (env
->sregs
[IBREAKENABLE
] & (1 << i
) && env
->sregs
[IBREAKA
+ i
] != v
) {
772 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
773 tb_invalidate_virtual_addr(env
, v
);
775 env
->sregs
[IBREAKA
+ i
] = v
;
778 static void set_dbreak(CPUXtensaState
*env
, unsigned i
, uint32_t dbreaka
,
781 int flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
;
782 uint32_t mask
= dbreakc
| ~DBREAKC_MASK
;
784 if (env
->cpu_watchpoint
[i
]) {
785 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[i
]);
787 if (dbreakc
& DBREAKC_SB
) {
788 flags
|= BP_MEM_WRITE
;
790 if (dbreakc
& DBREAKC_LB
) {
791 flags
|= BP_MEM_READ
;
793 /* contiguous mask after inversion is one less than some power of 2 */
794 if ((~mask
+ 1) & ~mask
) {
795 qemu_log("DBREAKC mask is not contiguous: 0x%08x\n", dbreakc
);
796 /* cut mask after the first zero bit */
797 mask
= 0xffffffff << (32 - clo32(mask
));
799 if (cpu_watchpoint_insert(env
, dbreaka
& mask
, ~mask
+ 1,
800 flags
, &env
->cpu_watchpoint
[i
])) {
801 env
->cpu_watchpoint
[i
] = NULL
;
802 qemu_log("Failed to set data breakpoint at 0x%08x/%d\n",
803 dbreaka
& mask
, ~mask
+ 1);
807 void HELPER(wsr_dbreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
809 uint32_t dbreakc
= env
->sregs
[DBREAKC
+ i
];
811 if ((dbreakc
& DBREAKC_SB_LB
) &&
812 env
->sregs
[DBREAKA
+ i
] != v
) {
813 set_dbreak(env
, i
, v
, dbreakc
);
815 env
->sregs
[DBREAKA
+ i
] = v
;
818 void HELPER(wsr_dbreakc
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
820 if ((env
->sregs
[DBREAKC
+ i
] ^ v
) & (DBREAKC_SB_LB
| DBREAKC_MASK
)) {
821 if (v
& DBREAKC_SB_LB
) {
822 set_dbreak(env
, i
, env
->sregs
[DBREAKA
+ i
], v
);
824 if (env
->cpu_watchpoint
[i
]) {
825 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[i
]);
826 env
->cpu_watchpoint
[i
] = NULL
;
830 env
->sregs
[DBREAKC
+ i
] = v
;
833 void HELPER(wur_fcr
)(CPUXtensaState
*env
, uint32_t v
)
835 static const int rounding_mode
[] = {
836 float_round_nearest_even
,
842 env
->uregs
[FCR
] = v
& 0xfffff07f;
843 set_float_rounding_mode(rounding_mode
[v
& 3], &env
->fp_status
);
846 float32
HELPER(abs_s
)(float32 v
)
848 return float32_abs(v
);
851 float32
HELPER(neg_s
)(float32 v
)
853 return float32_chs(v
);
856 float32
HELPER(add_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
858 return float32_add(a
, b
, &env
->fp_status
);
861 float32
HELPER(sub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
863 return float32_sub(a
, b
, &env
->fp_status
);
866 float32
HELPER(mul_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
868 return float32_mul(a
, b
, &env
->fp_status
);
871 float32
HELPER(madd_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
873 return float32_muladd(b
, c
, a
, 0,
877 float32
HELPER(msub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
879 return float32_muladd(b
, c
, a
, float_muladd_negate_product
,
883 uint32_t HELPER(ftoi
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
885 float_status fp_status
= {0};
887 set_float_rounding_mode(rounding_mode
, &fp_status
);
888 return float32_to_int32(
889 float32_scalbn(v
, scale
, &fp_status
), &fp_status
);
892 uint32_t HELPER(ftoui
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
894 float_status fp_status
= {0};
897 set_float_rounding_mode(rounding_mode
, &fp_status
);
899 res
= float32_scalbn(v
, scale
, &fp_status
);
901 if (float32_is_neg(v
) && !float32_is_any_nan(v
)) {
902 return float32_to_int32(res
, &fp_status
);
904 return float32_to_uint32(res
, &fp_status
);
908 float32
HELPER(itof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
910 return float32_scalbn(int32_to_float32(v
, &env
->fp_status
),
911 (int32_t)scale
, &env
->fp_status
);
914 float32
HELPER(uitof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
916 return float32_scalbn(uint32_to_float32(v
, &env
->fp_status
),
917 (int32_t)scale
, &env
->fp_status
);
920 static inline void set_br(CPUXtensaState
*env
, bool v
, uint32_t br
)
923 env
->sregs
[BR
] |= br
;
925 env
->sregs
[BR
] &= ~br
;
929 void HELPER(un_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
931 set_br(env
, float32_unordered_quiet(a
, b
, &env
->fp_status
), br
);
934 void HELPER(oeq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
936 set_br(env
, float32_eq_quiet(a
, b
, &env
->fp_status
), br
);
939 void HELPER(ueq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
941 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
942 set_br(env
, v
== float_relation_equal
|| v
== float_relation_unordered
, br
);
945 void HELPER(olt_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
947 set_br(env
, float32_lt_quiet(a
, b
, &env
->fp_status
), br
);
950 void HELPER(ult_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
952 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
953 set_br(env
, v
== float_relation_less
|| v
== float_relation_unordered
, br
);
956 void HELPER(ole_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
958 set_br(env
, float32_le_quiet(a
, b
, &env
->fp_status
), br
);
961 void HELPER(ule_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
963 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
964 set_br(env
, v
!= float_relation_greater
, br
);