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.
28 #include "qemu/osdep.h"
29 #include "qemu/main-loop.h"
31 #include "exec/helper-proto.h"
32 #include "qemu/host-utils.h"
33 #include "exec/exec-all.h"
34 #include "exec/cpu_ldst.h"
35 #include "exec/address-spaces.h"
36 #include "qemu/timer.h"
38 void xtensa_cpu_do_unaligned_access(CPUState
*cs
,
39 vaddr addr
, MMUAccessType access_type
,
40 int mmu_idx
, uintptr_t retaddr
)
42 XtensaCPU
*cpu
= XTENSA_CPU(cs
);
43 CPUXtensaState
*env
= &cpu
->env
;
45 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_UNALIGNED_EXCEPTION
) &&
46 !xtensa_option_enabled(env
->config
, XTENSA_OPTION_HW_ALIGNMENT
)) {
47 cpu_restore_state(CPU(cpu
), retaddr
);
48 HELPER(exception_cause_vaddr
)(env
,
49 env
->pc
, LOAD_STORE_ALIGNMENT_CAUSE
, addr
);
53 void tlb_fill(CPUState
*cs
, target_ulong vaddr
, MMUAccessType access_type
,
54 int mmu_idx
, uintptr_t retaddr
)
56 XtensaCPU
*cpu
= XTENSA_CPU(cs
);
57 CPUXtensaState
*env
= &cpu
->env
;
61 int ret
= xtensa_get_physical_addr(env
, true, vaddr
, access_type
, mmu_idx
,
62 &paddr
, &page_size
, &access
);
64 qemu_log_mask(CPU_LOG_MMU
, "%s(%08x, %d, %d) -> %08x, ret = %d\n",
65 __func__
, vaddr
, access_type
, mmu_idx
, paddr
, ret
);
69 vaddr
& TARGET_PAGE_MASK
,
70 paddr
& TARGET_PAGE_MASK
,
71 access
, mmu_idx
, page_size
);
73 cpu_restore_state(cs
, retaddr
);
74 HELPER(exception_cause_vaddr
)(env
, env
->pc
, ret
, vaddr
);
78 void xtensa_cpu_do_unassigned_access(CPUState
*cs
, hwaddr addr
,
79 bool is_write
, bool is_exec
, int opaque
,
82 XtensaCPU
*cpu
= XTENSA_CPU(cs
);
83 CPUXtensaState
*env
= &cpu
->env
;
85 HELPER(exception_cause_vaddr
)(env
, env
->pc
,
87 INSTR_PIF_ADDR_ERROR_CAUSE
:
88 LOAD_STORE_PIF_ADDR_ERROR_CAUSE
,
89 is_exec
? addr
: cs
->mem_io_vaddr
);
92 static void tb_invalidate_virtual_addr(CPUXtensaState
*env
, uint32_t vaddr
)
97 int ret
= xtensa_get_physical_addr(env
, false, vaddr
, 2, 0,
98 &paddr
, &page_size
, &access
);
100 tb_invalidate_phys_addr(&address_space_memory
, paddr
);
104 void HELPER(exception
)(CPUXtensaState
*env
, uint32_t excp
)
106 CPUState
*cs
= CPU(xtensa_env_get_cpu(env
));
108 cs
->exception_index
= excp
;
109 if (excp
== EXCP_YIELD
) {
110 env
->yield_needed
= 0;
112 if (excp
== EXCP_DEBUG
) {
113 env
->exception_taken
= 0;
118 void HELPER(exception_cause
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
123 if (env
->sregs
[PS
] & PS_EXCM
) {
124 if (env
->config
->ndepc
) {
125 env
->sregs
[DEPC
] = pc
;
127 env
->sregs
[EPC1
] = pc
;
131 env
->sregs
[EPC1
] = pc
;
132 vector
= (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
135 env
->sregs
[EXCCAUSE
] = cause
;
136 env
->sregs
[PS
] |= PS_EXCM
;
138 HELPER(exception
)(env
, vector
);
141 void HELPER(exception_cause_vaddr
)(CPUXtensaState
*env
,
142 uint32_t pc
, uint32_t cause
, uint32_t vaddr
)
144 env
->sregs
[EXCVADDR
] = vaddr
;
145 HELPER(exception_cause
)(env
, pc
, cause
);
148 void debug_exception_env(CPUXtensaState
*env
, uint32_t cause
)
150 if (xtensa_get_cintlevel(env
) < env
->config
->debug_level
) {
151 HELPER(debug_exception
)(env
, env
->pc
, cause
);
155 void HELPER(debug_exception
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t cause
)
157 unsigned level
= env
->config
->debug_level
;
160 env
->sregs
[DEBUGCAUSE
] = cause
;
161 env
->sregs
[EPC1
+ level
- 1] = pc
;
162 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
163 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) | PS_EXCM
|
164 (level
<< PS_INTLEVEL_SHIFT
);
165 HELPER(exception
)(env
, EXC_DEBUG
);
168 static void copy_window_from_phys(CPUXtensaState
*env
,
169 uint32_t window
, uint32_t phys
, uint32_t n
)
171 assert(phys
< env
->config
->nareg
);
172 if (phys
+ n
<= env
->config
->nareg
) {
173 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
174 n
* sizeof(uint32_t));
176 uint32_t n1
= env
->config
->nareg
- phys
;
177 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
178 n1
* sizeof(uint32_t));
179 memcpy(env
->regs
+ window
+ n1
, env
->phys_regs
,
180 (n
- n1
) * sizeof(uint32_t));
184 static void copy_phys_from_window(CPUXtensaState
*env
,
185 uint32_t phys
, uint32_t window
, uint32_t n
)
187 assert(phys
< env
->config
->nareg
);
188 if (phys
+ n
<= env
->config
->nareg
) {
189 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
190 n
* sizeof(uint32_t));
192 uint32_t n1
= env
->config
->nareg
- phys
;
193 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
194 n1
* sizeof(uint32_t));
195 memcpy(env
->phys_regs
, env
->regs
+ window
+ n1
,
196 (n
- n1
) * sizeof(uint32_t));
201 static inline unsigned windowbase_bound(unsigned a
, const CPUXtensaState
*env
)
203 return a
& (env
->config
->nareg
/ 4 - 1);
206 static inline unsigned windowstart_bit(unsigned a
, const CPUXtensaState
*env
)
208 return 1 << windowbase_bound(a
, env
);
211 void xtensa_sync_window_from_phys(CPUXtensaState
*env
)
213 copy_window_from_phys(env
, 0, env
->sregs
[WINDOW_BASE
] * 4, 16);
216 void xtensa_sync_phys_from_window(CPUXtensaState
*env
)
218 copy_phys_from_window(env
, env
->sregs
[WINDOW_BASE
] * 4, 0, 16);
221 static void rotate_window_abs(CPUXtensaState
*env
, uint32_t position
)
223 xtensa_sync_phys_from_window(env
);
224 env
->sregs
[WINDOW_BASE
] = windowbase_bound(position
, env
);
225 xtensa_sync_window_from_phys(env
);
228 static void rotate_window(CPUXtensaState
*env
, uint32_t delta
)
230 rotate_window_abs(env
, env
->sregs
[WINDOW_BASE
] + delta
);
233 void HELPER(wsr_windowbase
)(CPUXtensaState
*env
, uint32_t v
)
235 rotate_window_abs(env
, v
);
238 void HELPER(entry
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t s
, uint32_t imm
)
240 int callinc
= (env
->sregs
[PS
] & PS_CALLINC
) >> PS_CALLINC_SHIFT
;
241 if (s
> 3 || ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
242 qemu_log_mask(LOG_GUEST_ERROR
, "Illegal entry instruction(pc = %08x), PS = %08x\n",
244 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
246 uint32_t windowstart
= xtensa_replicate_windowstart(env
) >>
247 (env
->sregs
[WINDOW_BASE
] + 1);
249 if (windowstart
& ((1 << callinc
) - 1)) {
250 HELPER(window_check
)(env
, pc
, callinc
);
252 env
->regs
[(callinc
<< 2) | (s
& 3)] = env
->regs
[s
] - (imm
<< 3);
253 rotate_window(env
, callinc
);
254 env
->sregs
[WINDOW_START
] |=
255 windowstart_bit(env
->sregs
[WINDOW_BASE
], env
);
259 void HELPER(window_check
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t w
)
261 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
262 uint32_t windowstart
= xtensa_replicate_windowstart(env
) >>
263 (env
->sregs
[WINDOW_BASE
] + 1);
264 uint32_t n
= ctz32(windowstart
) + 1;
268 rotate_window(env
, n
);
269 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
270 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
271 env
->sregs
[EPC1
] = env
->pc
= pc
;
273 switch (ctz32(windowstart
>> n
)) {
275 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW4
);
278 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW8
);
281 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW12
);
286 uint32_t HELPER(retw
)(CPUXtensaState
*env
, uint32_t pc
)
288 int n
= (env
->regs
[0] >> 30) & 0x3;
290 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
291 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
294 if (windowstart
& windowstart_bit(windowbase
- 1, env
)) {
296 } else if (windowstart
& windowstart_bit(windowbase
- 2, env
)) {
298 } else if (windowstart
& windowstart_bit(windowbase
- 3, env
)) {
302 if (n
== 0 || (m
!= 0 && m
!= n
) ||
303 ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
304 qemu_log_mask(LOG_GUEST_ERROR
, "Illegal retw instruction(pc = %08x), "
305 "PS = %08x, m = %d, n = %d\n",
306 pc
, env
->sregs
[PS
], m
, n
);
307 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
309 int owb
= windowbase
;
311 ret_pc
= (pc
& 0xc0000000) | (env
->regs
[0] & 0x3fffffff);
313 rotate_window(env
, -n
);
314 if (windowstart
& windowstart_bit(env
->sregs
[WINDOW_BASE
], env
)) {
315 env
->sregs
[WINDOW_START
] &= ~windowstart_bit(owb
, env
);
317 /* window underflow */
318 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
319 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
320 env
->sregs
[EPC1
] = env
->pc
= pc
;
323 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW4
);
325 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW8
);
327 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW12
);
334 void HELPER(rotw
)(CPUXtensaState
*env
, uint32_t imm4
)
336 rotate_window(env
, imm4
);
339 void HELPER(restore_owb
)(CPUXtensaState
*env
)
341 rotate_window_abs(env
, (env
->sregs
[PS
] & PS_OWB
) >> PS_OWB_SHIFT
);
344 void HELPER(movsp
)(CPUXtensaState
*env
, uint32_t pc
)
346 if ((env
->sregs
[WINDOW_START
] &
347 (windowstart_bit(env
->sregs
[WINDOW_BASE
] - 3, env
) |
348 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 2, env
) |
349 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 1, env
))) == 0) {
350 HELPER(exception_cause
)(env
, pc
, ALLOCA_CAUSE
);
354 void HELPER(wsr_lbeg
)(CPUXtensaState
*env
, uint32_t v
)
356 if (env
->sregs
[LBEG
] != v
) {
357 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
358 env
->sregs
[LBEG
] = v
;
362 void HELPER(wsr_lend
)(CPUXtensaState
*env
, uint32_t v
)
364 if (env
->sregs
[LEND
] != v
) {
365 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
366 env
->sregs
[LEND
] = v
;
367 tb_invalidate_virtual_addr(env
, env
->sregs
[LEND
] - 1);
371 void HELPER(dump_state
)(CPUXtensaState
*env
)
373 XtensaCPU
*cpu
= xtensa_env_get_cpu(env
);
375 cpu_dump_state(CPU(cpu
), stderr
, fprintf
, 0);
378 void HELPER(waiti
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t intlevel
)
383 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) |
384 (intlevel
<< PS_INTLEVEL_SHIFT
);
386 qemu_mutex_lock_iothread();
387 check_interrupts(env
);
388 qemu_mutex_unlock_iothread();
390 if (env
->pending_irq_level
) {
391 cpu_loop_exit(CPU(xtensa_env_get_cpu(env
)));
395 cpu
= CPU(xtensa_env_get_cpu(env
));
397 HELPER(exception
)(env
, EXCP_HLT
);
400 void HELPER(update_ccount
)(CPUXtensaState
*env
)
402 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
404 env
->ccount_time
= now
;
405 env
->sregs
[CCOUNT
] = env
->ccount_base
+
406 (uint32_t)((now
- env
->time_base
) *
407 env
->config
->clock_freq_khz
/ 1000000);
410 void HELPER(wsr_ccount
)(CPUXtensaState
*env
, uint32_t v
)
414 HELPER(update_ccount
)(env
);
415 env
->ccount_base
+= v
- env
->sregs
[CCOUNT
];
416 for (i
= 0; i
< env
->config
->nccompare
; ++i
) {
417 HELPER(update_ccompare
)(env
, i
);
421 void HELPER(update_ccompare
)(CPUXtensaState
*env
, uint32_t i
)
425 HELPER(update_ccount
)(env
);
426 dcc
= (uint64_t)(env
->sregs
[CCOMPARE
+ i
] - env
->sregs
[CCOUNT
] - 1) + 1;
427 timer_mod(env
->ccompare
[i
].timer
,
428 env
->ccount_time
+ (dcc
* 1000000) / env
->config
->clock_freq_khz
);
429 env
->yield_needed
= 1;
432 void HELPER(check_interrupts
)(CPUXtensaState
*env
)
434 qemu_mutex_lock_iothread();
435 check_interrupts(env
);
436 qemu_mutex_unlock_iothread();
439 void HELPER(itlb_hit_test
)(CPUXtensaState
*env
, uint32_t vaddr
)
441 get_page_addr_code(env
, vaddr
);
445 * Check vaddr accessibility/cache attributes and raise an exception if
446 * specified by the ATOMCTL SR.
448 * Note: local memory exclusion is not implemented
450 void HELPER(check_atomctl
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t vaddr
)
452 uint32_t paddr
, page_size
, access
;
453 uint32_t atomctl
= env
->sregs
[ATOMCTL
];
454 int rc
= xtensa_get_physical_addr(env
, true, vaddr
, 1,
455 xtensa_get_cring(env
), &paddr
, &page_size
, &access
);
458 * s32c1i never causes LOAD_PROHIBITED_CAUSE exceptions,
459 * see opcode description in the ISA
462 (access
& (PAGE_READ
| PAGE_WRITE
)) != (PAGE_READ
| PAGE_WRITE
)) {
463 rc
= STORE_PROHIBITED_CAUSE
;
467 HELPER(exception_cause_vaddr
)(env
, pc
, rc
, vaddr
);
471 * When data cache is not configured use ATOMCTL bypass field.
472 * See ISA, 4.3.12.4 The Atomic Operation Control Register (ATOMCTL)
473 * under the Conditional Store Option.
475 if (!xtensa_option_enabled(env
->config
, XTENSA_OPTION_DCACHE
)) {
476 access
= PAGE_CACHE_BYPASS
;
479 switch (access
& PAGE_CACHE_MASK
) {
486 case PAGE_CACHE_BYPASS
:
487 if ((atomctl
& 0x3) == 0) {
488 HELPER(exception_cause_vaddr
)(env
, pc
,
489 LOAD_STORE_ERROR_CAUSE
, vaddr
);
493 case PAGE_CACHE_ISOLATE
:
494 HELPER(exception_cause_vaddr
)(env
, pc
,
495 LOAD_STORE_ERROR_CAUSE
, vaddr
);
503 void HELPER(wsr_memctl
)(CPUXtensaState
*env
, uint32_t v
)
505 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_ICACHE
)) {
506 if (extract32(v
, MEMCTL_IUSEWAYS_SHIFT
, MEMCTL_IUSEWAYS_LEN
) >
507 env
->config
->icache_ways
) {
508 deposit32(v
, MEMCTL_IUSEWAYS_SHIFT
, MEMCTL_IUSEWAYS_LEN
,
509 env
->config
->icache_ways
);
512 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_DCACHE
)) {
513 if (extract32(v
, MEMCTL_DUSEWAYS_SHIFT
, MEMCTL_DUSEWAYS_LEN
) >
514 env
->config
->dcache_ways
) {
515 deposit32(v
, MEMCTL_DUSEWAYS_SHIFT
, MEMCTL_DUSEWAYS_LEN
,
516 env
->config
->dcache_ways
);
518 if (extract32(v
, MEMCTL_DALLOCWAYS_SHIFT
, MEMCTL_DALLOCWAYS_LEN
) >
519 env
->config
->dcache_ways
) {
520 deposit32(v
, MEMCTL_DALLOCWAYS_SHIFT
, MEMCTL_DALLOCWAYS_LEN
,
521 env
->config
->dcache_ways
);
524 env
->sregs
[MEMCTL
] = v
& env
->config
->memctl_mask
;
527 void HELPER(wsr_rasid
)(CPUXtensaState
*env
, uint32_t v
)
529 XtensaCPU
*cpu
= xtensa_env_get_cpu(env
);
531 v
= (v
& 0xffffff00) | 0x1;
532 if (v
!= env
->sregs
[RASID
]) {
533 env
->sregs
[RASID
] = v
;
538 static uint32_t get_page_size(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
540 uint32_t tlbcfg
= env
->sregs
[dtlb
? DTLBCFG
: ITLBCFG
];
544 return (tlbcfg
>> 16) & 0x3;
547 return (tlbcfg
>> 20) & 0x1;
550 return (tlbcfg
>> 24) & 0x1;
558 * Get bit mask for the virtual address bits translated by the TLB way
560 uint32_t xtensa_tlb_get_addr_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
562 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
563 bool varway56
= dtlb
?
564 env
->config
->dtlb
.varway56
:
565 env
->config
->itlb
.varway56
;
569 return 0xfff00000 << get_page_size(env
, dtlb
, way
) * 2;
573 return 0xf8000000 << get_page_size(env
, dtlb
, way
);
580 return 0xf0000000 << (1 - get_page_size(env
, dtlb
, way
));
589 return REGION_PAGE_MASK
;
594 * Get bit mask for the 'VPN without index' field.
595 * See ISA, 4.6.5.6, data format for RxTLB0
597 static uint32_t get_vpn_mask(const CPUXtensaState
*env
, bool dtlb
, uint32_t way
)
601 env
->config
->dtlb
.nrefillentries
:
602 env
->config
->itlb
.nrefillentries
) == 32;
603 return is32
? 0xffff8000 : 0xffffc000;
604 } else if (way
== 4) {
605 return xtensa_tlb_get_addr_mask(env
, dtlb
, way
) << 2;
606 } else if (way
<= 6) {
607 uint32_t mask
= xtensa_tlb_get_addr_mask(env
, dtlb
, way
);
608 bool varway56
= dtlb
?
609 env
->config
->dtlb
.varway56
:
610 env
->config
->itlb
.varway56
;
613 return mask
<< (way
== 5 ? 2 : 3);
623 * Split virtual address into VPN (with index) and entry index
624 * for the given TLB way
626 void split_tlb_entry_spec_way(const CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
627 uint32_t *vpn
, uint32_t wi
, uint32_t *ei
)
629 bool varway56
= dtlb
?
630 env
->config
->dtlb
.varway56
:
631 env
->config
->itlb
.varway56
;
639 env
->config
->dtlb
.nrefillentries
:
640 env
->config
->itlb
.nrefillentries
) == 32;
641 *ei
= (v
>> 12) & (is32
? 0x7 : 0x3);
646 uint32_t eibase
= 20 + get_page_size(env
, dtlb
, wi
) * 2;
647 *ei
= (v
>> eibase
) & 0x3;
653 uint32_t eibase
= 27 + get_page_size(env
, dtlb
, wi
);
654 *ei
= (v
>> eibase
) & 0x3;
656 *ei
= (v
>> 27) & 0x1;
662 uint32_t eibase
= 29 - get_page_size(env
, dtlb
, wi
);
663 *ei
= (v
>> eibase
) & 0x7;
665 *ei
= (v
>> 28) & 0x1;
674 *vpn
= v
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
678 * Split TLB address into TLB way, entry index and VPN (with index).
679 * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
681 static void split_tlb_entry_spec(CPUXtensaState
*env
, uint32_t v
, bool dtlb
,
682 uint32_t *vpn
, uint32_t *wi
, uint32_t *ei
)
684 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
685 *wi
= v
& (dtlb
? 0xf : 0x7);
686 split_tlb_entry_spec_way(env
, v
, dtlb
, vpn
, *wi
, ei
);
688 *vpn
= v
& REGION_PAGE_MASK
;
690 *ei
= (v
>> 29) & 0x7;
694 static xtensa_tlb_entry
*get_tlb_entry(CPUXtensaState
*env
,
695 uint32_t v
, bool dtlb
, uint32_t *pwi
)
701 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
705 return xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
708 uint32_t HELPER(rtlb0
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
710 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
712 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
713 return (entry
->vaddr
& get_vpn_mask(env
, dtlb
, wi
)) | entry
->asid
;
715 return v
& REGION_PAGE_MASK
;
719 uint32_t HELPER(rtlb1
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
721 const xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, NULL
);
722 return entry
->paddr
| entry
->attr
;
725 void HELPER(itlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
727 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
729 xtensa_tlb_entry
*entry
= get_tlb_entry(env
, v
, dtlb
, &wi
);
730 if (entry
->variable
&& entry
->asid
) {
731 tlb_flush_page(CPU(xtensa_env_get_cpu(env
)), entry
->vaddr
);
737 uint32_t HELPER(ptlb
)(CPUXtensaState
*env
, uint32_t v
, uint32_t dtlb
)
739 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
743 int res
= xtensa_tlb_lookup(env
, v
, dtlb
, &wi
, &ei
, &ring
);
747 if (ring
>= xtensa_get_ring(env
)) {
748 return (v
& 0xfffff000) | wi
| (dtlb
? 0x10 : 0x8);
752 case INST_TLB_MULTI_HIT_CAUSE
:
753 case LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
754 HELPER(exception_cause_vaddr
)(env
, env
->pc
, res
, v
);
759 return (v
& REGION_PAGE_MASK
) | 0x1;
763 void xtensa_tlb_set_entry_mmu(const CPUXtensaState
*env
,
764 xtensa_tlb_entry
*entry
, bool dtlb
,
765 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
768 entry
->paddr
= pte
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
769 entry
->asid
= (env
->sregs
[RASID
] >> ((pte
>> 1) & 0x18)) & 0xff;
770 entry
->attr
= pte
& 0xf;
773 void xtensa_tlb_set_entry(CPUXtensaState
*env
, bool dtlb
,
774 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
776 XtensaCPU
*cpu
= xtensa_env_get_cpu(env
);
777 CPUState
*cs
= CPU(cpu
);
778 xtensa_tlb_entry
*entry
= xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
780 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
781 if (entry
->variable
) {
783 tlb_flush_page(cs
, entry
->vaddr
);
785 xtensa_tlb_set_entry_mmu(env
, entry
, dtlb
, wi
, ei
, vpn
, pte
);
786 tlb_flush_page(cs
, entry
->vaddr
);
788 qemu_log_mask(LOG_GUEST_ERROR
, "%s %d, %d, %d trying to set immutable entry\n",
789 __func__
, dtlb
, wi
, ei
);
792 tlb_flush_page(cs
, entry
->vaddr
);
793 if (xtensa_option_enabled(env
->config
,
794 XTENSA_OPTION_REGION_TRANSLATION
)) {
795 entry
->paddr
= pte
& REGION_PAGE_MASK
;
797 entry
->attr
= pte
& 0xf;
801 void HELPER(wtlb
)(CPUXtensaState
*env
, uint32_t p
, uint32_t v
, uint32_t dtlb
)
806 split_tlb_entry_spec(env
, v
, dtlb
, &vpn
, &wi
, &ei
);
807 xtensa_tlb_set_entry(env
, dtlb
, wi
, ei
, vpn
, p
);
811 void HELPER(wsr_ibreakenable
)(CPUXtensaState
*env
, uint32_t v
)
813 uint32_t change
= v
^ env
->sregs
[IBREAKENABLE
];
816 for (i
= 0; i
< env
->config
->nibreak
; ++i
) {
817 if (change
& (1 << i
)) {
818 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
821 env
->sregs
[IBREAKENABLE
] = v
& ((1 << env
->config
->nibreak
) - 1);
824 void HELPER(wsr_ibreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
826 if (env
->sregs
[IBREAKENABLE
] & (1 << i
) && env
->sregs
[IBREAKA
+ i
] != v
) {
827 tb_invalidate_virtual_addr(env
, env
->sregs
[IBREAKA
+ i
]);
828 tb_invalidate_virtual_addr(env
, v
);
830 env
->sregs
[IBREAKA
+ i
] = v
;
833 static void set_dbreak(CPUXtensaState
*env
, unsigned i
, uint32_t dbreaka
,
836 CPUState
*cs
= CPU(xtensa_env_get_cpu(env
));
837 int flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
;
838 uint32_t mask
= dbreakc
| ~DBREAKC_MASK
;
840 if (env
->cpu_watchpoint
[i
]) {
841 cpu_watchpoint_remove_by_ref(cs
, env
->cpu_watchpoint
[i
]);
843 if (dbreakc
& DBREAKC_SB
) {
844 flags
|= BP_MEM_WRITE
;
846 if (dbreakc
& DBREAKC_LB
) {
847 flags
|= BP_MEM_READ
;
849 /* contiguous mask after inversion is one less than some power of 2 */
850 if ((~mask
+ 1) & ~mask
) {
851 qemu_log_mask(LOG_GUEST_ERROR
, "DBREAKC mask is not contiguous: 0x%08x\n", dbreakc
);
852 /* cut mask after the first zero bit */
853 mask
= 0xffffffff << (32 - clo32(mask
));
855 if (cpu_watchpoint_insert(cs
, dbreaka
& mask
, ~mask
+ 1,
856 flags
, &env
->cpu_watchpoint
[i
])) {
857 env
->cpu_watchpoint
[i
] = NULL
;
858 qemu_log_mask(LOG_GUEST_ERROR
, "Failed to set data breakpoint at 0x%08x/%d\n",
859 dbreaka
& mask
, ~mask
+ 1);
863 void HELPER(wsr_dbreaka
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
865 uint32_t dbreakc
= env
->sregs
[DBREAKC
+ i
];
867 if ((dbreakc
& DBREAKC_SB_LB
) &&
868 env
->sregs
[DBREAKA
+ i
] != v
) {
869 set_dbreak(env
, i
, v
, dbreakc
);
871 env
->sregs
[DBREAKA
+ i
] = v
;
874 void HELPER(wsr_dbreakc
)(CPUXtensaState
*env
, uint32_t i
, uint32_t v
)
876 if ((env
->sregs
[DBREAKC
+ i
] ^ v
) & (DBREAKC_SB_LB
| DBREAKC_MASK
)) {
877 if (v
& DBREAKC_SB_LB
) {
878 set_dbreak(env
, i
, env
->sregs
[DBREAKA
+ i
], v
);
880 if (env
->cpu_watchpoint
[i
]) {
881 CPUState
*cs
= CPU(xtensa_env_get_cpu(env
));
883 cpu_watchpoint_remove_by_ref(cs
, env
->cpu_watchpoint
[i
]);
884 env
->cpu_watchpoint
[i
] = NULL
;
888 env
->sregs
[DBREAKC
+ i
] = v
;
891 void HELPER(wur_fcr
)(CPUXtensaState
*env
, uint32_t v
)
893 static const int rounding_mode
[] = {
894 float_round_nearest_even
,
900 env
->uregs
[FCR
] = v
& 0xfffff07f;
901 set_float_rounding_mode(rounding_mode
[v
& 3], &env
->fp_status
);
904 float32
HELPER(abs_s
)(float32 v
)
906 return float32_abs(v
);
909 float32
HELPER(neg_s
)(float32 v
)
911 return float32_chs(v
);
914 float32
HELPER(add_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
916 return float32_add(a
, b
, &env
->fp_status
);
919 float32
HELPER(sub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
921 return float32_sub(a
, b
, &env
->fp_status
);
924 float32
HELPER(mul_s
)(CPUXtensaState
*env
, float32 a
, float32 b
)
926 return float32_mul(a
, b
, &env
->fp_status
);
929 float32
HELPER(madd_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
931 return float32_muladd(b
, c
, a
, 0,
935 float32
HELPER(msub_s
)(CPUXtensaState
*env
, float32 a
, float32 b
, float32 c
)
937 return float32_muladd(b
, c
, a
, float_muladd_negate_product
,
941 uint32_t HELPER(ftoi
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
943 float_status fp_status
= {0};
945 set_float_rounding_mode(rounding_mode
, &fp_status
);
946 return float32_to_int32(
947 float32_scalbn(v
, scale
, &fp_status
), &fp_status
);
950 uint32_t HELPER(ftoui
)(float32 v
, uint32_t rounding_mode
, uint32_t scale
)
952 float_status fp_status
= {0};
955 set_float_rounding_mode(rounding_mode
, &fp_status
);
957 res
= float32_scalbn(v
, scale
, &fp_status
);
959 if (float32_is_neg(v
) && !float32_is_any_nan(v
)) {
960 return float32_to_int32(res
, &fp_status
);
962 return float32_to_uint32(res
, &fp_status
);
966 float32
HELPER(itof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
968 return float32_scalbn(int32_to_float32(v
, &env
->fp_status
),
969 (int32_t)scale
, &env
->fp_status
);
972 float32
HELPER(uitof
)(CPUXtensaState
*env
, uint32_t v
, uint32_t scale
)
974 return float32_scalbn(uint32_to_float32(v
, &env
->fp_status
),
975 (int32_t)scale
, &env
->fp_status
);
978 static inline void set_br(CPUXtensaState
*env
, bool v
, uint32_t br
)
981 env
->sregs
[BR
] |= br
;
983 env
->sregs
[BR
] &= ~br
;
987 void HELPER(un_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
989 set_br(env
, float32_unordered_quiet(a
, b
, &env
->fp_status
), br
);
992 void HELPER(oeq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
994 set_br(env
, float32_eq_quiet(a
, b
, &env
->fp_status
), br
);
997 void HELPER(ueq_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
999 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
1000 set_br(env
, v
== float_relation_equal
|| v
== float_relation_unordered
, br
);
1003 void HELPER(olt_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
1005 set_br(env
, float32_lt_quiet(a
, b
, &env
->fp_status
), br
);
1008 void HELPER(ult_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
1010 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
1011 set_br(env
, v
== float_relation_less
|| v
== float_relation_unordered
, br
);
1014 void HELPER(ole_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
1016 set_br(env
, float32_le_quiet(a
, b
, &env
->fp_status
), br
);
1019 void HELPER(ule_s
)(CPUXtensaState
*env
, uint32_t br
, float32 a
, float32 b
)
1021 int v
= float32_compare_quiet(a
, b
, &env
->fp_status
);
1022 set_br(env
, v
!= float_relation_greater
, br
);
1025 uint32_t HELPER(rer
)(CPUXtensaState
*env
, uint32_t addr
)
1027 return address_space_ldl(env
->address_space_er
, addr
,
1028 MEMTXATTRS_UNSPECIFIED
, NULL
);
1031 void HELPER(wer
)(CPUXtensaState
*env
, uint32_t data
, uint32_t addr
)
1033 address_space_stl(env
->address_space_er
, addr
, data
,
1034 MEMTXATTRS_UNSPECIFIED
, NULL
);