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"
32 static void do_unaligned_access(CPUXtensaState
*env
,
33 target_ulong addr
, int is_write
, int is_user
, uintptr_t retaddr
);
36 #define MMUSUFFIX _mmu
39 #include "exec/softmmu_template.h"
42 #include "exec/softmmu_template.h"
45 #include "exec/softmmu_template.h"
48 #include "exec/softmmu_template.h"
50 static void do_unaligned_access(CPUXtensaState
*env
,
51 target_ulong addr
, int is_write
, int is_user
, uintptr_t retaddr
)
53 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_UNALIGNED_EXCEPTION
) &&
54 !xtensa_option_enabled(env
->config
, XTENSA_OPTION_HW_ALIGNMENT
)) {
55 cpu_restore_state(env
, retaddr
);
56 HELPER(exception_cause_vaddr
)(env
,
57 env
->pc
, LOAD_STORE_ALIGNMENT_CAUSE
, addr
);
61 void tlb_fill(CPUXtensaState
*env
,
62 target_ulong vaddr
, int is_write
, int mmu_idx
, uintptr_t retaddr
)
67 int ret
= xtensa_get_physical_addr(env
, true, vaddr
, is_write
, mmu_idx
,
68 &paddr
, &page_size
, &access
);
70 qemu_log("%s(%08x, %d, %d) -> %08x, ret = %d\n", __func__
,
71 vaddr
, is_write
, mmu_idx
, paddr
, ret
);
75 vaddr
& TARGET_PAGE_MASK
,
76 paddr
& TARGET_PAGE_MASK
,
77 access
, mmu_idx
, page_size
);
79 cpu_restore_state(env
, retaddr
);
80 HELPER(exception_cause_vaddr
)(env
, env
->pc
, ret
, vaddr
);
84 static void tb_invalidate_virtual_addr(CPUXtensaState
*env
, uint32_t vaddr
)
89 int ret
= xtensa_get_physical_addr(env
, false, vaddr
, 2, 0,
90 &paddr
, &page_size
, &access
);
92 tb_invalidate_phys_addr(paddr
);
96 void HELPER(exception
)(CPUXtensaState
*env
, uint32_t excp
)
98 env
->exception_index
= excp
;
102 void HELPER(exception_cause
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
107 if (env
->sregs
[PS
] & PS_EXCM
) {
108 if (env
->config
->ndepc
) {
109 env
->sregs
[DEPC
] = pc
;
111 env
->sregs
[EPC1
] = pc
;
115 env
->sregs
[EPC1
] = pc
;
116 vector
= (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
119 env
->sregs
[EXCCAUSE
] = cause
;
120 env
->sregs
[PS
] |= PS_EXCM
;
122 HELPER(exception
)(env
, vector
);
125 void HELPER(exception_cause_vaddr
)(CPUXtensaState
*env
,
126 uint32_t pc
, uint32_t cause
, uint32_t vaddr
)
128 env
->sregs
[EXCVADDR
] = vaddr
;
129 HELPER(exception_cause
)(env
, pc
, cause
);
132 void debug_exception_env(CPUXtensaState
*env
, uint32_t cause
)
134 if (xtensa_get_cintlevel(env
) < env
->config
->debug_level
) {
135 HELPER(debug_exception
)(env
, env
->pc
, cause
);
139 void HELPER(debug_exception
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
141 unsigned level
= env
->config
->debug_level
;
144 env
->sregs
[DEBUGCAUSE
] = cause
;
145 env
->sregs
[EPC1
+ level
- 1] = pc
;
146 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
147 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) | PS_EXCM
|
148 (level
<< PS_INTLEVEL_SHIFT
);
149 HELPER(exception
)(env
, EXC_DEBUG
);
152 uint32_t HELPER(nsa
)(uint32_t v
)
154 if (v
& 0x80000000) {
157 return v
? clz32(v
) - 1 : 31;
160 uint32_t HELPER(nsau
)(uint32_t v
)
162 return v
? clz32(v
) : 32;
165 static void copy_window_from_phys(CPUXtensaState
*env
,
166 uint32_t window
, uint32_t phys
, uint32_t n
)
168 assert(phys
< env
->config
->nareg
);
169 if (phys
+ n
<= env
->config
->nareg
) {
170 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
171 n
* sizeof(uint32_t));
173 uint32_t n1
= env
->config
->nareg
- phys
;
174 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
175 n1
* sizeof(uint32_t));
176 memcpy(env
->regs
+ window
+ n1
, env
->phys_regs
,
177 (n
- n1
) * sizeof(uint32_t));
181 static void copy_phys_from_window(CPUXtensaState
*env
,
182 uint32_t phys
, uint32_t window
, uint32_t n
)
184 assert(phys
< env
->config
->nareg
);
185 if (phys
+ n
<= env
->config
->nareg
) {
186 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
187 n
* sizeof(uint32_t));
189 uint32_t n1
= env
->config
->nareg
- phys
;
190 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
191 n1
* sizeof(uint32_t));
192 memcpy(env
->phys_regs
, env
->regs
+ window
+ n1
,
193 (n
- n1
) * sizeof(uint32_t));
198 static inline unsigned windowbase_bound(unsigned a
, const CPUXtensaState
*env
)
200 return a
& (env
->config
->nareg
/ 4 - 1);
203 static inline unsigned windowstart_bit(unsigned a
, const CPUXtensaState
*env
)
205 return 1 << windowbase_bound(a
, env
);
208 void xtensa_sync_window_from_phys(CPUXtensaState
*env
)
210 copy_window_from_phys(env
, 0, env
->sregs
[WINDOW_BASE
] * 4, 16);
213 void xtensa_sync_phys_from_window(CPUXtensaState
*env
)
215 copy_phys_from_window(env
, env
->sregs
[WINDOW_BASE
] * 4, 0, 16);
218 static void rotate_window_abs(CPUXtensaState
*env
, uint32_t position
)
220 xtensa_sync_phys_from_window(env
);
221 env
->sregs
[WINDOW_BASE
] = windowbase_bound(position
, env
);
222 xtensa_sync_window_from_phys(env
);
225 static void rotate_window(CPUXtensaState
*env
, uint32_t delta
)
227 rotate_window_abs(env
, env
->sregs
[WINDOW_BASE
] + delta
);
230 void HELPER(wsr_windowbase
)(CPUXtensaState
*env
, uint32_t v
)
232 rotate_window_abs(env
, v
);
235 void HELPER(entry
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t s
, uint32_t imm
)
237 int callinc
= (env
->sregs
[PS
] & PS_CALLINC
) >> PS_CALLINC_SHIFT
;
238 if (s
> 3 || ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
239 qemu_log("Illegal entry instruction(pc = %08x), PS = %08x\n",
241 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
243 env
->regs
[(callinc
<< 2) | (s
& 3)] = env
->regs
[s
] - (imm
<< 3);
244 rotate_window(env
, callinc
);
245 env
->sregs
[WINDOW_START
] |=
246 windowstart_bit(env
->sregs
[WINDOW_BASE
], env
);
250 void HELPER(window_check
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t w
)
252 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
253 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
256 if ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) {
264 if (windowstart
& windowstart_bit(windowbase
+ n
, env
)) {
269 m
= windowbase_bound(windowbase
+ n
, env
);
270 rotate_window(env
, n
);
271 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
272 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
273 env
->sregs
[EPC1
] = env
->pc
= pc
;
275 if (windowstart
& windowstart_bit(m
+ 1, env
)) {
276 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW4
);
277 } else if (windowstart
& windowstart_bit(m
+ 2, env
)) {
278 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW8
);
280 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW12
);
284 uint32_t HELPER(retw
)(CPUXtensaState
*env
, uint32_t pc
)
286 int n
= (env
->regs
[0] >> 30) & 0x3;
288 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
289 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
292 if (windowstart
& windowstart_bit(windowbase
- 1, env
)) {
294 } else if (windowstart
& windowstart_bit(windowbase
- 2, env
)) {
296 } else if (windowstart
& windowstart_bit(windowbase
- 3, env
)) {
300 if (n
== 0 || (m
!= 0 && m
!= n
) ||
301 ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
302 qemu_log("Illegal retw instruction(pc = %08x), "
303 "PS = %08x, m = %d, n = %d\n",
304 pc
, env
->sregs
[PS
], m
, n
);
305 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
307 int owb
= windowbase
;
309 ret_pc
= (pc
& 0xc0000000) | (env
->regs
[0] & 0x3fffffff);
311 rotate_window(env
, -n
);
312 if (windowstart
& windowstart_bit(env
->sregs
[WINDOW_BASE
], env
)) {
313 env
->sregs
[WINDOW_START
] &= ~windowstart_bit(owb
, env
);
315 /* window underflow */
316 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
317 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
318 env
->sregs
[EPC1
] = env
->pc
= pc
;
321 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW4
);
323 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW8
);
325 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW12
);
332 void HELPER(rotw
)(CPUXtensaState
*env
, uint32_t imm4
)
334 rotate_window(env
, imm4
);
337 void HELPER(restore_owb
)(CPUXtensaState
*env
)
339 rotate_window_abs(env
, (env
->sregs
[PS
] & PS_OWB
) >> PS_OWB_SHIFT
);
342 void HELPER(movsp
)(CPUXtensaState
*env
, uint32_t pc
)
344 if ((env
->sregs
[WINDOW_START
] &
345 (windowstart_bit(env
->sregs
[WINDOW_BASE
] - 3, env
) |
346 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 2, env
) |
347 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 1, env
))) == 0) {
348 HELPER(exception_cause
)(env
, pc
, ALLOCA_CAUSE
);
352 void HELPER(wsr_lbeg
)(CPUXtensaState
*env
, uint32_t v
)
354 if (env
->sregs
[LBEG
] != v
) {
355 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
356 env
->sregs
[LBEG
] = v
;
360 void HELPER(wsr_lend
)(CPUXtensaState
*env
, uint32_t v
)
362 if (env
->sregs
[LEND
] != v
) {
363 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
364 env
->sregs
[LEND
] = v
;
365 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
369 void HELPER(dump_state
)(CPUXtensaState
*env
)
371 XtensaCPU
*cpu
= xtensa_env_get_cpu(env
);
373 cpu_dump_state(CPU(cpu
), stderr
, fprintf
, 0);
376 void HELPER(waiti
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t intlevel
)
381 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) |
382 (intlevel
<< PS_INTLEVEL_SHIFT
);
383 check_interrupts(env
);
384 if (env
->pending_irq_level
) {
389 cpu
= CPU(xtensa_env_get_cpu(env
));
390 env
->halt_clock
= qemu_get_clock_ns(vm_clock
);
392 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_TIMER_INTERRUPT
)) {
393 xtensa_rearm_ccompare_timer(env
);
395 HELPER(exception
)(env
, EXCP_HLT
);
398 void HELPER(timer_irq
)(CPUXtensaState
*env
, uint32_t id
, uint32_t active
)
400 xtensa_timer_irq(env
, id
, active
);
403 void HELPER(advance_ccount
)(CPUXtensaState
*env
, uint32_t d
)
405 xtensa_advance_ccount(env
, d
);
408 void HELPER(check_interrupts
)(CPUXtensaState
*env
)
410 check_interrupts(env
);
414 * Check vaddr accessibility/cache attributes and raise an exception if
415 * specified by the ATOMCTL SR.
417 * Note: local memory exclusion is not implemented
419 void HELPER(check_atomctl
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t vaddr
)
421 uint32_t paddr
, page_size
, access
;
422 uint32_t atomctl
= env
->sregs
[ATOMCTL
];
423 int rc
= xtensa_get_physical_addr(env
, true, vaddr
, 1,
424 xtensa_get_cring(env
), &paddr
, &page_size
, &access
);
427 * s32c1i never causes LOAD_PROHIBITED_CAUSE exceptions,
428 * see opcode description in the ISA
431 (access
& (PAGE_READ
| PAGE_WRITE
)) != (PAGE_READ
| PAGE_WRITE
)) {
432 rc
= STORE_PROHIBITED_CAUSE
;
436 HELPER(exception_cause_vaddr
)(env
, pc
, rc
, vaddr
);
440 * When data cache is not configured use ATOMCTL bypass field.
441 * See ISA, 4.3.12.4 The Atomic Operation Control Register (ATOMCTL)
442 * under the Conditional Store Option.
444 if (!xtensa_option_enabled(env
->config
, XTENSA_OPTION_DCACHE
)) {
445 access
= PAGE_CACHE_BYPASS
;
448 switch (access
& PAGE_CACHE_MASK
) {
453 case PAGE_CACHE_BYPASS
:
454 if ((atomctl
& 0x3) == 0) {
455 HELPER(exception_cause_vaddr
)(env
, pc
,
456 LOAD_STORE_ERROR_CAUSE
, vaddr
);
460 case PAGE_CACHE_ISOLATE
:
461 HELPER(exception_cause_vaddr
)(env
, pc
,
462 LOAD_STORE_ERROR_CAUSE
, vaddr
);
470 void HELPER(wsr_rasid
)(CPUXtensaState
*env
, uint32_t v
)
472 v
= (v
& 0xffffff00) | 0x1;
473 if (v
!= env
->sregs
[RASID
]) {
474 env
->sregs
[RASID
] = v
;
479 static uint32_t get_page_size(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
481 uint32_t tlbcfg
= env
->sregs
[dtlb
? DTLBCFG
: ITLBCFG
];
485 return (tlbcfg
>> 16) & 0x3;
488 return (tlbcfg
>> 20) & 0x1;
491 return (tlbcfg
>> 24) & 0x1;
499 * Get bit mask for the virtual address bits translated by the TLB way
501 uint32_t xtensa_tlb_get_addr_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
503 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
504 bool varway56
= dtlb
?
505 env
->config
->dtlb
.varway56
:
506 env
->config
->itlb
.varway56
;
510 return 0xfff00000 << get_page_size(env
, dtlb
, way
) * 2;
514 return 0xf8000000 << get_page_size(env
, dtlb
, way
);
521 return 0xf0000000 << (1 - get_page_size(env
, dtlb
, way
));
530 return REGION_PAGE_MASK
;
535 * Get bit mask for the 'VPN without index' field.
536 * See ISA, 4.6.5.6, data format for RxTLB0
538 static uint32_t get_vpn_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
542 env
->config
->dtlb
.nrefillentries
:
543 env
->config
->itlb
.nrefillentries
) == 32;
544 return is32
? 0xffff8000 : 0xffffc000;
545 } else if (way
== 4) {
546 return xtensa_tlb_get_addr_mask(env
, dtlb
, way
) << 2;
547 } else if (way
<= 6) {
548 uint32_t mask
= xtensa_tlb_get_addr_mask(env
, dtlb
, way
);
549 bool varway56
= dtlb
?
550 env
->config
->dtlb
.varway56
:
551 env
->config
->itlb
.varway56
;
554 return mask
<< (way
== 5 ? 2 : 3);
564 * Split virtual address into VPN (with index) and entry index
565 * for the given TLB way
567 void split_tlb_entry_spec_way(const CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
568 uint32_t *vpn
, uint32_t wi
, uint32_t *ei
)
570 bool varway56
= dtlb
?
571 env
->config
->dtlb
.varway56
:
572 env
->config
->itlb
.varway56
;
580 env
->config
->dtlb
.nrefillentries
:
581 env
->config
->itlb
.nrefillentries
) == 32;
582 *ei
= (v
>> 12) & (is32
? 0x7 : 0x3);
587 uint32_t eibase
= 20 + get_page_size(env
, dtlb
, wi
) * 2;
588 *ei
= (v
>> eibase
) & 0x3;
594 uint32_t eibase
= 27 + get_page_size(env
, dtlb
, wi
);
595 *ei
= (v
>> eibase
) & 0x3;
597 *ei
= (v
>> 27) & 0x1;
603 uint32_t eibase
= 29 - get_page_size(env
, dtlb
, wi
);
604 *ei
= (v
>> eibase
) & 0x7;
606 *ei
= (v
>> 28) & 0x1;
615 *vpn
= v
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
619 * Split TLB address into TLB way, entry index and VPN (with index).
620 * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
622 static void split_tlb_entry_spec(CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
623 uint32_t *vpn
, uint32_t *wi
, uint32_t *ei
)
625 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
626 *wi
= v
& (dtlb
? 0xf : 0x7);
627 split_tlb_entry_spec_way(env
, v
, dtlb
, vpn
, *wi
, ei
);
629 *vpn
= v
& REGION_PAGE_MASK
;
631 *ei
= (v
>> 29) & 0x7;
635 static xtensa_tlb_entry
*get_tlb_entry(CPUXtensaState
*env
,
636 uint32_t v
, bool dtlb
, uint32_t *pwi
)
642 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
646 return xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
649 uint32_t HELPER(rtlb0
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
651 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
653 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
654 return (entry
->vaddr
& get_vpn_mask(env
, dtlb
, wi
)) | entry
->asid
;
656 return v
& REGION_PAGE_MASK
;
660 uint32_t HELPER(rtlb1
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
662 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, NULL
);
663 return entry
->paddr
| entry
->attr
;
666 void HELPER(itlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
668 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
670 xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
671 if (entry
->variable
&& entry
->asid
) {
672 tlb_flush_page(env
, entry
->vaddr
);
678 uint32_t HELPER(ptlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
680 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
684 int res
= xtensa_tlb_lookup(env
, v
, dtlb
, &wi
, &ei
, &ring
);
688 if (ring
>= xtensa_get_ring(env
)) {
689 return (v
& 0xfffff000) | wi
| (dtlb
? 0x10 : 0x8);
693 case INST_TLB_MULTI_HIT_CAUSE
:
694 case LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
695 HELPER(exception_cause_vaddr
)(env
, env
->pc
, res
, v
);
700 return (v
& REGION_PAGE_MASK
) | 0x1;
704 void xtensa_tlb_set_entry_mmu(const CPUXtensaState
*env
,
705 xtensa_tlb_entry
*entry
, bool dtlb
,
706 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
709 entry
->paddr
= pte
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
710 entry
->asid
= (env
->sregs
[RASID
] >> ((pte
>> 1) & 0x18)) & 0xff;
711 entry
->attr
= pte
& 0xf;
714 void xtensa_tlb_set_entry(CPUXtensaState
*env
, bool dtlb
,
715 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
717 xtensa_tlb_entry
*entry
= xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
719 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
720 if (entry
->variable
) {
722 tlb_flush_page(env
, entry
->vaddr
);
724 xtensa_tlb_set_entry_mmu(env
, entry
, dtlb
, wi
, ei
, vpn
, pte
);
725 tlb_flush_page(env
, entry
->vaddr
);
727 qemu_log("%s %d, %d, %d trying to set immutable entry\n",
728 __func__
, dtlb
, wi
, ei
);
731 tlb_flush_page(env
, entry
->vaddr
);
732 if (xtensa_option_enabled(env
->config
,
733 XTENSA_OPTION_REGION_TRANSLATION
)) {
734 entry
->paddr
= pte
& REGION_PAGE_MASK
;
736 entry
->attr
= pte
& 0xf;
740 void HELPER(wtlb
)(CPUXtensaState
*env
, uint32_t p
, uint32_t v
, uint32_t dtlb
)
745 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
746 xtensa_tlb_set_entry(env
, dtlb
, wi
, ei
, vpn
, p
);
750 void HELPER(wsr_ibreakenable
)(CPUXtensaState
*env
, uint32_t v
)
752 uint32_t change
= v
^ env
->sregs
[IBREAKENABLE
];
755 for (i
= 0; i
< env
->config
->nibreak
; ++i
) {
756 if (change
& (1 << i
)) {
757 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
760 env
->sregs
[IBREAKENABLE
] = v
& ((1 << env
->config
->nibreak
) - 1);
763 void HELPER(wsr_ibreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
765 if (env
->sregs
[IBREAKENABLE
] & (1 << i
) && env
->sregs
[IBREAKA
+ i
] != v
) {
766 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
767 tb_invalidate_virtual_addr(env
, v
);
769 env
->sregs
[IBREAKA
+ i
] = v
;
772 static void set_dbreak(CPUXtensaState
*env
, unsigned i
, uint32_t dbreaka
,
775 int flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
;
776 uint32_t mask
= dbreakc
| ~DBREAKC_MASK
;
778 if (env
->cpu_watchpoint
[i
]) {
779 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[i
]);
781 if (dbreakc
& DBREAKC_SB
) {
782 flags
|= BP_MEM_WRITE
;
784 if (dbreakc
& DBREAKC_LB
) {
785 flags
|= BP_MEM_READ
;
787 /* contiguous mask after inversion is one less than some power of 2 */
788 if ((~mask
+ 1) & ~mask
) {
789 qemu_log("DBREAKC mask is not contiguous: 0x%08x\n", dbreakc
);
790 /* cut mask after the first zero bit */
791 mask
= 0xffffffff << (32 - clo32(mask
));
793 if (cpu_watchpoint_insert(env
, dbreaka
& mask
, ~mask
+ 1,
794 flags
, &env
->cpu_watchpoint
[i
])) {
795 env
->cpu_watchpoint
[i
] = NULL
;
796 qemu_log("Failed to set data breakpoint at 0x%08x/%d\n",
797 dbreaka
& mask
, ~mask
+ 1);
801 void HELPER(wsr_dbreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
803 uint32_t dbreakc
= env
->sregs
[DBREAKC
+ i
];
805 if ((dbreakc
& DBREAKC_SB_LB
) &&
806 env
->sregs
[DBREAKA
+ i
] != v
) {
807 set_dbreak(env
, i
, v
, dbreakc
);
809 env
->sregs
[DBREAKA
+ i
] = v
;
812 void HELPER(wsr_dbreakc
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
814 if ((env
->sregs
[DBREAKC
+ i
] ^ v
) & (DBREAKC_SB_LB
| DBREAKC_MASK
)) {
815 if (v
& DBREAKC_SB_LB
) {
816 set_dbreak(env
, i
, env
->sregs
[DBREAKA
+ i
], v
);
818 if (env
->cpu_watchpoint
[i
]) {
819 cpu_watchpoint_remove_by_ref(env
, env
->cpu_watchpoint
[i
]);
820 env
->cpu_watchpoint
[i
] = NULL
;
824 env
->sregs
[DBREAKC
+ i
] = v
;
827 void HELPER(wur_fcr
)(CPUXtensaState
*env
, uint32_t v
)
829 static const int rounding_mode
[] = {
830 float_round_nearest_even
,
836 env
->uregs
[FCR
] = v
& 0xfffff07f;
837 set_float_rounding_mode(rounding_mode
[v
& 3], &env
->fp_status
);
840 float32
HELPER(abs_s
)(float32 v
)
842 return float32_abs(v
);
845 float32
HELPER(neg_s
)(float32 v
)
847 return float32_chs(v
);
850 float32
HELPER(add_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
852 return float32_add(a
, b
, &env
->fp_status
);
855 float32
HELPER(sub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
857 return float32_sub(a
, b
, &env
->fp_status
);
860 float32
HELPER(mul_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
862 return float32_mul(a
, b
, &env
->fp_status
);
865 float32
HELPER(madd_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
867 return float32_muladd(b
, c
, a
, 0,
871 float32
HELPER(msub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
873 return float32_muladd(b
, c
, a
, float_muladd_negate_product
,
877 uint32_t HELPER(ftoi
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
879 float_status fp_status
= {0};
881 set_float_rounding_mode(rounding_mode
, &fp_status
);
882 return float32_to_int32(
883 float32_scalbn(v
, scale
, &fp_status
), &fp_status
);
886 uint32_t HELPER(ftoui
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
888 float_status fp_status
= {0};
891 set_float_rounding_mode(rounding_mode
, &fp_status
);
893 res
= float32_scalbn(v
, scale
, &fp_status
);
895 if (float32_is_neg(v
) && !float32_is_any_nan(v
)) {
896 return float32_to_int32(res
, &fp_status
);
898 return float32_to_uint32(res
, &fp_status
);
902 float32
HELPER(itof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
904 return float32_scalbn(int32_to_float32(v
, &env
->fp_status
),
905 (int32_t)scale
, &env
->fp_status
);
908 float32
HELPER(uitof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
910 return float32_scalbn(uint32_to_float32(v
, &env
->fp_status
),
911 (int32_t)scale
, &env
->fp_status
);
914 static inline void set_br(CPUXtensaState
*env
, bool v
, uint32_t br
)
917 env
->sregs
[BR
] |= br
;
919 env
->sregs
[BR
] &= ~br
;
923 void HELPER(un_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
925 set_br(env
, float32_unordered_quiet(a
, b
, &env
->fp_status
), br
);
928 void HELPER(oeq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
930 set_br(env
, float32_eq_quiet(a
, b
, &env
->fp_status
), br
);
933 void HELPER(ueq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
935 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
936 set_br(env
, v
== float_relation_equal
|| v
== float_relation_unordered
, br
);
939 void HELPER(olt_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
941 set_br(env
, float32_lt_quiet(a
, b
, &env
->fp_status
), br
);
944 void HELPER(ult_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
946 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
947 set_br(env
, v
== float_relation_less
|| v
== float_relation_unordered
, br
);
950 void HELPER(ole_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
952 set_br(env
, float32_le_quiet(a
, b
, &env
->fp_status
), br
);
955 void HELPER(ule_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
957 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
958 set_br(env
, v
!= float_relation_greater
, br
);