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.
29 #include "dyngen-exec.h"
31 #include "host-utils.h"
33 static void do_unaligned_access(target_ulong addr
, int is_write
, int is_user
,
37 #define MMUSUFFIX _mmu
40 #include "softmmu_template.h"
43 #include "softmmu_template.h"
46 #include "softmmu_template.h"
49 #include "softmmu_template.h"
51 static void do_restore_state(void *pc_ptr
)
54 uint32_t pc
= (uint32_t)(intptr_t)pc_ptr
;
58 cpu_restore_state(tb
, env
, pc
);
62 static void do_unaligned_access(target_ulong addr
, int is_write
, int is_user
,
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(target_ulong vaddr
, int is_write
, int mmu_idx
, void *retaddr
)
75 CPUState
*saved_env
= env
;
82 int ret
= xtensa_get_physical_addr(env
, vaddr
, is_write
, mmu_idx
,
83 &paddr
, &page_size
, &access
);
85 qemu_log("%s(%08x, %d, %d) -> %08x, ret = %d\n", __func__
,
86 vaddr
, is_write
, mmu_idx
, paddr
, ret
);
90 vaddr
& TARGET_PAGE_MASK
,
91 paddr
& TARGET_PAGE_MASK
,
92 access
, mmu_idx
, page_size
);
94 do_restore_state(retaddr
);
95 HELPER(exception_cause_vaddr
)(env
->pc
, ret
, vaddr
);
101 void HELPER(exception
)(uint32_t excp
)
103 env
->exception_index
= excp
;
107 void HELPER(exception_cause
)(uint32_t pc
, uint32_t cause
)
112 if (env
->sregs
[PS
] & PS_EXCM
) {
113 if (env
->config
->ndepc
) {
114 env
->sregs
[DEPC
] = pc
;
116 env
->sregs
[EPC1
] = pc
;
120 env
->sregs
[EPC1
] = pc
;
121 vector
= (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
124 env
->sregs
[EXCCAUSE
] = cause
;
125 env
->sregs
[PS
] |= PS_EXCM
;
127 HELPER(exception
)(vector
);
130 void HELPER(exception_cause_vaddr
)(uint32_t pc
, uint32_t cause
, uint32_t vaddr
)
132 env
->sregs
[EXCVADDR
] = vaddr
;
133 HELPER(exception_cause
)(pc
, cause
);
136 uint32_t HELPER(nsa
)(uint32_t v
)
138 if (v
& 0x80000000) {
141 return v
? clz32(v
) - 1 : 31;
144 uint32_t HELPER(nsau
)(uint32_t v
)
146 return v
? clz32(v
) : 32;
149 static void copy_window_from_phys(CPUState
*env
,
150 uint32_t window
, uint32_t phys
, uint32_t n
)
152 assert(phys
< env
->config
->nareg
);
153 if (phys
+ n
<= env
->config
->nareg
) {
154 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
155 n
* sizeof(uint32_t));
157 uint32_t n1
= env
->config
->nareg
- phys
;
158 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
159 n1
* sizeof(uint32_t));
160 memcpy(env
->regs
+ window
+ n1
, env
->phys_regs
,
161 (n
- n1
) * sizeof(uint32_t));
165 static void copy_phys_from_window(CPUState
*env
,
166 uint32_t phys
, uint32_t window
, uint32_t n
)
168 assert(phys
< env
->config
->nareg
);
169 if (phys
+ n
<= env
->config
->nareg
) {
170 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
171 n
* sizeof(uint32_t));
173 uint32_t n1
= env
->config
->nareg
- phys
;
174 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
175 n1
* sizeof(uint32_t));
176 memcpy(env
->phys_regs
, env
->regs
+ window
+ n1
,
177 (n
- n1
) * sizeof(uint32_t));
182 static inline unsigned windowbase_bound(unsigned a
, const CPUState
*env
)
184 return a
& (env
->config
->nareg
/ 4 - 1);
187 static inline unsigned windowstart_bit(unsigned a
, const CPUState
*env
)
189 return 1 << windowbase_bound(a
, env
);
192 void xtensa_sync_window_from_phys(CPUState
*env
)
194 copy_window_from_phys(env
, 0, env
->sregs
[WINDOW_BASE
] * 4, 16);
197 void xtensa_sync_phys_from_window(CPUState
*env
)
199 copy_phys_from_window(env
, env
->sregs
[WINDOW_BASE
] * 4, 0, 16);
202 static void rotate_window_abs(uint32_t position
)
204 xtensa_sync_phys_from_window(env
);
205 env
->sregs
[WINDOW_BASE
] = windowbase_bound(position
, env
);
206 xtensa_sync_window_from_phys(env
);
209 static void rotate_window(uint32_t delta
)
211 rotate_window_abs(env
->sregs
[WINDOW_BASE
] + delta
);
214 void HELPER(wsr_windowbase
)(uint32_t v
)
216 rotate_window_abs(v
);
219 void HELPER(entry
)(uint32_t pc
, uint32_t s
, uint32_t imm
)
221 int callinc
= (env
->sregs
[PS
] & PS_CALLINC
) >> PS_CALLINC_SHIFT
;
222 if (s
> 3 || ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
223 qemu_log("Illegal entry instruction(pc = %08x), PS = %08x\n",
225 HELPER(exception_cause
)(pc
, ILLEGAL_INSTRUCTION_CAUSE
);
227 env
->regs
[(callinc
<< 2) | (s
& 3)] = env
->regs
[s
] - (imm
<< 3);
228 rotate_window(callinc
);
229 env
->sregs
[WINDOW_START
] |=
230 windowstart_bit(env
->sregs
[WINDOW_BASE
], env
);
234 void HELPER(window_check
)(uint32_t pc
, uint32_t w
)
236 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
237 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
240 if ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) {
248 if (windowstart
& windowstart_bit(windowbase
+ n
, env
)) {
253 m
= windowbase_bound(windowbase
+ n
, env
);
255 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
256 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
257 env
->sregs
[EPC1
] = env
->pc
= pc
;
259 if (windowstart
& windowstart_bit(m
+ 1, env
)) {
260 HELPER(exception
)(EXC_WINDOW_OVERFLOW4
);
261 } else if (windowstart
& windowstart_bit(m
+ 2, env
)) {
262 HELPER(exception
)(EXC_WINDOW_OVERFLOW8
);
264 HELPER(exception
)(EXC_WINDOW_OVERFLOW12
);
268 uint32_t HELPER(retw
)(uint32_t pc
)
270 int n
= (env
->regs
[0] >> 30) & 0x3;
272 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
273 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
276 if (windowstart
& windowstart_bit(windowbase
- 1, env
)) {
278 } else if (windowstart
& windowstart_bit(windowbase
- 2, env
)) {
280 } else if (windowstart
& windowstart_bit(windowbase
- 3, env
)) {
284 if (n
== 0 || (m
!= 0 && m
!= n
) ||
285 ((env
->sregs
[PS
] & (PS_WOE
| PS_EXCM
)) ^ PS_WOE
) != 0) {
286 qemu_log("Illegal retw instruction(pc = %08x), "
287 "PS = %08x, m = %d, n = %d\n",
288 pc
, env
->sregs
[PS
], m
, n
);
289 HELPER(exception_cause
)(pc
, ILLEGAL_INSTRUCTION_CAUSE
);
291 int owb
= windowbase
;
293 ret_pc
= (pc
& 0xc0000000) | (env
->regs
[0] & 0x3fffffff);
296 if (windowstart
& windowstart_bit(env
->sregs
[WINDOW_BASE
], env
)) {
297 env
->sregs
[WINDOW_START
] &= ~windowstart_bit(owb
, env
);
299 /* window underflow */
300 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
301 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
302 env
->sregs
[EPC1
] = env
->pc
= pc
;
305 HELPER(exception
)(EXC_WINDOW_UNDERFLOW4
);
307 HELPER(exception
)(EXC_WINDOW_UNDERFLOW8
);
309 HELPER(exception
)(EXC_WINDOW_UNDERFLOW12
);
316 void HELPER(rotw
)(uint32_t imm4
)
321 void HELPER(restore_owb
)(void)
323 rotate_window_abs((env
->sregs
[PS
] & PS_OWB
) >> PS_OWB_SHIFT
);
326 void HELPER(movsp
)(uint32_t pc
)
328 if ((env
->sregs
[WINDOW_START
] &
329 (windowstart_bit(env
->sregs
[WINDOW_BASE
] - 3, env
) |
330 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 2, env
) |
331 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 1, env
))) == 0) {
332 HELPER(exception_cause
)(pc
, ALLOCA_CAUSE
);
336 void HELPER(wsr_lbeg
)(uint32_t v
)
338 if (env
->sregs
[LBEG
] != v
) {
339 tb_invalidate_phys_page_range(
340 env
->sregs
[LEND
] - 1, env
->sregs
[LEND
], 0);
341 env
->sregs
[LBEG
] = v
;
345 void HELPER(wsr_lend
)(uint32_t v
)
347 if (env
->sregs
[LEND
] != v
) {
348 tb_invalidate_phys_page_range(
349 env
->sregs
[LEND
] - 1, env
->sregs
[LEND
], 0);
350 env
->sregs
[LEND
] = v
;
351 tb_invalidate_phys_page_range(
352 env
->sregs
[LEND
] - 1, env
->sregs
[LEND
], 0);
356 void HELPER(dump_state
)(void)
358 cpu_dump_state(env
, stderr
, fprintf
, 0);
361 void HELPER(waiti
)(uint32_t pc
, uint32_t intlevel
)
364 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_INTLEVEL
) |
365 (intlevel
<< PS_INTLEVEL_SHIFT
);
366 check_interrupts(env
);
367 if (env
->pending_irq_level
) {
372 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_TIMER_INTERRUPT
)) {
374 uint32_t wake_ccount
= env
->sregs
[CCOUNT
] - 1;
376 for (i
= 0; i
< env
->config
->nccompare
; ++i
) {
377 if (env
->sregs
[CCOMPARE
+ i
] - env
->sregs
[CCOUNT
] <
378 wake_ccount
- env
->sregs
[CCOUNT
]) {
379 wake_ccount
= env
->sregs
[CCOMPARE
+ i
];
382 env
->wake_ccount
= wake_ccount
;
383 qemu_mod_timer(env
->ccompare_timer
, qemu_get_clock_ns(vm_clock
) +
384 muldiv64(wake_ccount
- env
->sregs
[CCOUNT
],
385 1000000, env
->config
->clock_freq_khz
));
387 env
->halt_clock
= qemu_get_clock_ns(vm_clock
);
389 HELPER(exception
)(EXCP_HLT
);
392 void HELPER(timer_irq
)(uint32_t id
, uint32_t active
)
394 xtensa_timer_irq(env
, id
, active
);
397 void HELPER(advance_ccount
)(uint32_t d
)
399 xtensa_advance_ccount(env
, d
);
402 void HELPER(check_interrupts
)(CPUState
*env
)
404 check_interrupts(env
);
407 void HELPER(wsr_rasid
)(uint32_t v
)
409 v
= (v
& 0xffffff00) | 0x1;
410 if (v
!= env
->sregs
[RASID
]) {
411 env
->sregs
[RASID
] = v
;
416 static uint32_t get_page_size(const CPUState
*env
, bool dtlb
, uint32_t way
)
418 uint32_t tlbcfg
= env
->sregs
[dtlb
? DTLBCFG
: ITLBCFG
];
422 return (tlbcfg
>> 16) & 0x3;
425 return (tlbcfg
>> 20) & 0x1;
428 return (tlbcfg
>> 24) & 0x1;
436 * Get bit mask for the virtual address bits translated by the TLB way
438 uint32_t xtensa_tlb_get_addr_mask(const CPUState
*env
, bool dtlb
, uint32_t way
)
440 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
441 bool varway56
= dtlb
?
442 env
->config
->dtlb
.varway56
:
443 env
->config
->itlb
.varway56
;
447 return 0xfff00000 << get_page_size(env
, dtlb
, way
) * 2;
451 return 0xf8000000 << get_page_size(env
, dtlb
, way
);
458 return 0xf0000000 << (1 - get_page_size(env
, dtlb
, way
));
467 return REGION_PAGE_MASK
;
472 * Get bit mask for the 'VPN without index' field.
473 * See ISA, 4.6.5.6, data format for RxTLB0
475 static uint32_t get_vpn_mask(const CPUState
*env
, bool dtlb
, uint32_t way
)
479 env
->config
->dtlb
.nrefillentries
:
480 env
->config
->itlb
.nrefillentries
) == 32;
481 return is32
? 0xffff8000 : 0xffffc000;
482 } else if (way
== 4) {
483 return xtensa_tlb_get_addr_mask(env
, dtlb
, way
) << 2;
484 } else if (way
<= 6) {
485 uint32_t mask
= xtensa_tlb_get_addr_mask(env
, dtlb
, way
);
486 bool varway56
= dtlb
?
487 env
->config
->dtlb
.varway56
:
488 env
->config
->itlb
.varway56
;
491 return mask
<< (way
== 5 ? 2 : 3);
501 * Split virtual address into VPN (with index) and entry index
502 * for the given TLB way
504 void split_tlb_entry_spec_way(const CPUState
*env
, uint32_t v
, bool dtlb
,
505 uint32_t *vpn
, uint32_t wi
, uint32_t *ei
)
507 bool varway56
= dtlb
?
508 env
->config
->dtlb
.varway56
:
509 env
->config
->itlb
.varway56
;
517 env
->config
->dtlb
.nrefillentries
:
518 env
->config
->itlb
.nrefillentries
) == 32;
519 *ei
= (v
>> 12) & (is32
? 0x7 : 0x3);
524 uint32_t eibase
= 20 + get_page_size(env
, dtlb
, wi
) * 2;
525 *ei
= (v
>> eibase
) & 0x3;
531 uint32_t eibase
= 27 + get_page_size(env
, dtlb
, wi
);
532 *ei
= (v
>> eibase
) & 0x3;
534 *ei
= (v
>> 27) & 0x1;
540 uint32_t eibase
= 29 - get_page_size(env
, dtlb
, wi
);
541 *ei
= (v
>> eibase
) & 0x7;
543 *ei
= (v
>> 28) & 0x1;
552 *vpn
= v
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
556 * Split TLB address into TLB way, entry index and VPN (with index).
557 * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
559 static void split_tlb_entry_spec(uint32_t v
, bool dtlb
,
560 uint32_t *vpn
, uint32_t *wi
, uint32_t *ei
)
562 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
563 *wi
= v
& (dtlb
? 0xf : 0x7);
564 split_tlb_entry_spec_way(env
, v
, dtlb
, vpn
, *wi
, ei
);
566 *vpn
= v
& REGION_PAGE_MASK
;
568 *ei
= (v
>> 29) & 0x7;
572 static xtensa_tlb_entry
*get_tlb_entry(uint32_t v
, bool dtlb
, uint32_t *pwi
)
578 split_tlb_entry_spec(v
, dtlb
, &vpn
, &wi
, &ei
);
582 return xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
585 uint32_t HELPER(rtlb0
)(uint32_t v
, uint32_t dtlb
)
587 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
589 const xtensa_tlb_entry
*entry
= get_tlb_entry(v
, dtlb
, &wi
);
590 return (entry
->vaddr
& get_vpn_mask(env
, dtlb
, wi
)) | entry
->asid
;
592 return v
& REGION_PAGE_MASK
;
596 uint32_t HELPER(rtlb1
)(uint32_t v
, uint32_t dtlb
)
598 const xtensa_tlb_entry
*entry
= get_tlb_entry(v
, dtlb
, NULL
);
599 return entry
->paddr
| entry
->attr
;
602 void HELPER(itlb
)(uint32_t v
, uint32_t dtlb
)
604 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
606 xtensa_tlb_entry
*entry
= get_tlb_entry(v
, dtlb
, &wi
);
607 if (entry
->variable
&& entry
->asid
) {
608 tlb_flush_page(env
, entry
->vaddr
);
614 uint32_t HELPER(ptlb
)(uint32_t v
, uint32_t dtlb
)
616 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
620 int res
= xtensa_tlb_lookup(env
, v
, dtlb
, &wi
, &ei
, &ring
);
624 if (ring
>= xtensa_get_ring(env
)) {
625 return (v
& 0xfffff000) | wi
| (dtlb
? 0x10 : 0x8);
629 case INST_TLB_MULTI_HIT_CAUSE
:
630 case LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
631 HELPER(exception_cause_vaddr
)(env
->pc
, res
, v
);
636 return (v
& REGION_PAGE_MASK
) | 0x1;
640 void xtensa_tlb_set_entry(CPUState
*env
, bool dtlb
,
641 unsigned wi
, unsigned ei
, uint32_t vpn
, uint32_t pte
)
643 xtensa_tlb_entry
*entry
= xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
645 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
646 if (entry
->variable
) {
648 tlb_flush_page(env
, entry
->vaddr
);
651 entry
->paddr
= pte
& xtensa_tlb_get_addr_mask(env
, dtlb
, wi
);
652 entry
->asid
= (env
->sregs
[RASID
] >> ((pte
>> 1) & 0x18)) & 0xff;
653 entry
->attr
= pte
& 0xf;
655 qemu_log("%s %d, %d, %d trying to set immutable entry\n",
656 __func__
, dtlb
, wi
, ei
);
659 tlb_flush_page(env
, entry
->vaddr
);
660 if (xtensa_option_enabled(env
->config
,
661 XTENSA_OPTION_REGION_TRANSLATION
)) {
662 entry
->paddr
= pte
& REGION_PAGE_MASK
;
664 entry
->attr
= pte
& 0xf;
668 void HELPER(wtlb
)(uint32_t p
, uint32_t v
, uint32_t dtlb
)
673 split_tlb_entry_spec(v
, dtlb
, &vpn
, &wi
, &ei
);
674 xtensa_tlb_set_entry(env
, dtlb
, wi
, ei
, vpn
, p
);