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.
31 #include "host-utils.h"
32 #if !defined(CONFIG_USER_ONLY)
33 #include "hw/loader.h"
36 void cpu_state_reset(CPUXtensaState
*env
)
38 cpu_reset(ENV_GET_CPU(env
));
41 static struct XtensaConfigList
*xtensa_cores
;
43 void xtensa_register_core(XtensaConfigList
*node
)
45 node
->next
= xtensa_cores
;
49 static uint32_t check_hw_breakpoints(CPUXtensaState
*env
)
53 for (i
= 0; i
< env
->config
->ndbreak
; ++i
) {
54 if (env
->cpu_watchpoint
[i
] &&
55 env
->cpu_watchpoint
[i
]->flags
& BP_WATCHPOINT_HIT
) {
56 return DEBUGCAUSE_DB
| (i
<< DEBUGCAUSE_DBNUM_SHIFT
);
62 static CPUDebugExcpHandler
*prev_debug_excp_handler
;
64 static void breakpoint_handler(CPUXtensaState
*env
)
66 if (env
->watchpoint_hit
) {
67 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
70 env
->watchpoint_hit
= NULL
;
71 cause
= check_hw_breakpoints(env
);
73 debug_exception_env(env
, cause
);
75 cpu_resume_from_signal(env
, NULL
);
78 if (prev_debug_excp_handler
) {
79 prev_debug_excp_handler(env
);
83 CPUXtensaState
*cpu_xtensa_init(const char *cpu_model
)
85 static int tcg_inited
;
86 static int debug_handler_inited
;
89 const XtensaConfig
*config
= NULL
;
90 XtensaConfigList
*core
= xtensa_cores
;
92 for (; core
; core
= core
->next
)
93 if (strcmp(core
->config
->name
, cpu_model
) == 0) {
94 config
= core
->config
;
102 cpu
= XTENSA_CPU(object_new(TYPE_XTENSA_CPU
));
104 env
->config
= config
;
108 xtensa_translate_init();
111 if (!debug_handler_inited
&& tcg_enabled()) {
112 debug_handler_inited
= 1;
113 prev_debug_excp_handler
=
114 cpu_set_debug_excp_handler(breakpoint_handler
);
117 xtensa_irq_init(env
);
123 void xtensa_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
125 XtensaConfigList
*core
= xtensa_cores
;
126 cpu_fprintf(f
, "Available CPUs:\n");
127 for (; core
; core
= core
->next
) {
128 cpu_fprintf(f
, " %s\n", core
->config
->name
);
132 target_phys_addr_t
cpu_get_phys_page_debug(CPUXtensaState
*env
, target_ulong addr
)
138 if (xtensa_get_physical_addr(env
, addr
, 0, 0,
139 &paddr
, &page_size
, &access
) == 0) {
142 if (xtensa_get_physical_addr(env
, addr
, 2, 0,
143 &paddr
, &page_size
, &access
) == 0) {
149 static uint32_t relocated_vector(CPUXtensaState
*env
, uint32_t vector
)
151 if (xtensa_option_enabled(env
->config
,
152 XTENSA_OPTION_RELOCATABLE_VECTOR
)) {
153 return vector
- env
->config
->vecbase
+ env
->sregs
[VECBASE
];
160 * Handle penging IRQ.
161 * For the high priority interrupt jump to the corresponding interrupt vector.
162 * For the level-1 interrupt convert it to either user, kernel or double
163 * exception with the 'level-1 interrupt' exception cause.
165 static void handle_interrupt(CPUXtensaState
*env
)
167 int level
= env
->pending_irq_level
;
169 if (level
> xtensa_get_cintlevel(env
) &&
170 level
<= env
->config
->nlevel
&&
171 (env
->config
->level_mask
[level
] &
173 env
->sregs
[INTENABLE
])) {
175 env
->sregs
[EPC1
+ level
- 1] = env
->pc
;
176 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
178 (env
->sregs
[PS
] & ~PS_INTLEVEL
) | level
| PS_EXCM
;
179 env
->pc
= relocated_vector(env
,
180 env
->config
->interrupt_vector
[level
]);
182 env
->sregs
[EXCCAUSE
] = LEVEL1_INTERRUPT_CAUSE
;
184 if (env
->sregs
[PS
] & PS_EXCM
) {
185 if (env
->config
->ndepc
) {
186 env
->sregs
[DEPC
] = env
->pc
;
188 env
->sregs
[EPC1
] = env
->pc
;
190 env
->exception_index
= EXC_DOUBLE
;
192 env
->sregs
[EPC1
] = env
->pc
;
193 env
->exception_index
=
194 (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
196 env
->sregs
[PS
] |= PS_EXCM
;
198 env
->exception_taken
= 1;
202 void do_interrupt(CPUXtensaState
*env
)
204 if (env
->exception_index
== EXC_IRQ
) {
205 qemu_log_mask(CPU_LOG_INT
,
206 "%s(EXC_IRQ) level = %d, cintlevel = %d, "
207 "pc = %08x, a0 = %08x, ps = %08x, "
208 "intset = %08x, intenable = %08x, "
210 __func__
, env
->pending_irq_level
, xtensa_get_cintlevel(env
),
211 env
->pc
, env
->regs
[0], env
->sregs
[PS
],
212 env
->sregs
[INTSET
], env
->sregs
[INTENABLE
],
214 handle_interrupt(env
);
217 switch (env
->exception_index
) {
218 case EXC_WINDOW_OVERFLOW4
:
219 case EXC_WINDOW_UNDERFLOW4
:
220 case EXC_WINDOW_OVERFLOW8
:
221 case EXC_WINDOW_UNDERFLOW8
:
222 case EXC_WINDOW_OVERFLOW12
:
223 case EXC_WINDOW_UNDERFLOW12
:
228 qemu_log_mask(CPU_LOG_INT
, "%s(%d) "
229 "pc = %08x, a0 = %08x, ps = %08x, ccount = %08x\n",
230 __func__
, env
->exception_index
,
231 env
->pc
, env
->regs
[0], env
->sregs
[PS
], env
->sregs
[CCOUNT
]);
232 if (env
->config
->exception_vector
[env
->exception_index
]) {
233 env
->pc
= relocated_vector(env
,
234 env
->config
->exception_vector
[env
->exception_index
]);
235 env
->exception_taken
= 1;
237 qemu_log("%s(pc = %08x) bad exception_index: %d\n",
238 __func__
, env
->pc
, env
->exception_index
);
246 qemu_log("%s(pc = %08x) unknown exception_index: %d\n",
247 __func__
, env
->pc
, env
->exception_index
);
250 check_interrupts(env
);
253 static void reset_tlb_mmu_all_ways(CPUXtensaState
*env
,
254 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
258 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
259 for (ei
= 0; ei
< tlb
->way_size
[wi
]; ++ei
) {
260 entry
[wi
][ei
].asid
= 0;
261 entry
[wi
][ei
].variable
= true;
266 static void reset_tlb_mmu_ways56(CPUXtensaState
*env
,
267 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
269 if (!tlb
->varway56
) {
270 static const xtensa_tlb_entry way5
[] = {
285 static const xtensa_tlb_entry way6
[] = {
300 memcpy(entry
[5], way5
, sizeof(way5
));
301 memcpy(entry
[6], way6
, sizeof(way6
));
304 for (ei
= 0; ei
< 8; ++ei
) {
305 entry
[6][ei
].vaddr
= ei
<< 29;
306 entry
[6][ei
].paddr
= ei
<< 29;
307 entry
[6][ei
].asid
= 1;
308 entry
[6][ei
].attr
= 3;
313 static void reset_tlb_region_way0(CPUXtensaState
*env
,
314 xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
318 for (ei
= 0; ei
< 8; ++ei
) {
319 entry
[0][ei
].vaddr
= ei
<< 29;
320 entry
[0][ei
].paddr
= ei
<< 29;
321 entry
[0][ei
].asid
= 1;
322 entry
[0][ei
].attr
= 2;
323 entry
[0][ei
].variable
= true;
327 void reset_mmu(CPUXtensaState
*env
)
329 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
330 env
->sregs
[RASID
] = 0x04030201;
331 env
->sregs
[ITLBCFG
] = 0;
332 env
->sregs
[DTLBCFG
] = 0;
333 env
->autorefill_idx
= 0;
334 reset_tlb_mmu_all_ways(env
, &env
->config
->itlb
, env
->itlb
);
335 reset_tlb_mmu_all_ways(env
, &env
->config
->dtlb
, env
->dtlb
);
336 reset_tlb_mmu_ways56(env
, &env
->config
->itlb
, env
->itlb
);
337 reset_tlb_mmu_ways56(env
, &env
->config
->dtlb
, env
->dtlb
);
339 reset_tlb_region_way0(env
, env
->itlb
);
340 reset_tlb_region_way0(env
, env
->dtlb
);
344 static unsigned get_ring(const CPUXtensaState
*env
, uint8_t asid
)
347 for (i
= 0; i
< 4; ++i
) {
348 if (((env
->sregs
[RASID
] >> i
* 8) & 0xff) == asid
) {
356 * Lookup xtensa TLB for the given virtual address.
359 * \param pwi: [out] way index
360 * \param pei: [out] entry index
361 * \param pring: [out] access ring
362 * \return 0 if ok, exception cause code otherwise
364 int xtensa_tlb_lookup(const CPUXtensaState
*env
, uint32_t addr
, bool dtlb
,
365 uint32_t *pwi
, uint32_t *pei
, uint8_t *pring
)
367 const xtensa_tlb
*tlb
= dtlb
?
368 &env
->config
->dtlb
: &env
->config
->itlb
;
369 const xtensa_tlb_entry (*entry
)[MAX_TLB_WAY_SIZE
] = dtlb
?
370 env
->dtlb
: env
->itlb
;
375 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
378 split_tlb_entry_spec_way(env
, addr
, dtlb
, &vpn
, wi
, &ei
);
379 if (entry
[wi
][ei
].vaddr
== vpn
&& entry
[wi
][ei
].asid
) {
380 unsigned ring
= get_ring(env
, entry
[wi
][ei
].asid
);
384 LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
385 INST_TLB_MULTI_HIT_CAUSE
;
394 (dtlb
? LOAD_STORE_TLB_MISS_CAUSE
: INST_TLB_MISS_CAUSE
);
398 * Convert MMU ATTR to PAGE_{READ,WRITE,EXEC} mask.
401 static unsigned mmu_attr_to_access(uint32_t attr
)
410 access
|= PAGE_WRITE
;
412 } else if (attr
== 13) {
413 access
|= PAGE_READ
| PAGE_WRITE
;
419 * Convert region protection ATTR to PAGE_{READ,WRITE,EXEC} mask.
422 static unsigned region_attr_to_access(uint32_t attr
)
425 if ((attr
< 6 && attr
!= 3) || attr
== 14) {
426 access
|= PAGE_READ
| PAGE_WRITE
;
428 if (attr
> 0 && attr
< 6) {
434 static bool is_access_granted(unsigned access
, int is_write
)
438 return access
& PAGE_READ
;
441 return access
& PAGE_WRITE
;
444 return access
& PAGE_EXEC
;
451 static int autorefill_mmu(CPUXtensaState
*env
, uint32_t vaddr
, bool dtlb
,
452 uint32_t *wi
, uint32_t *ei
, uint8_t *ring
);
454 static int get_physical_addr_mmu(CPUXtensaState
*env
,
455 uint32_t vaddr
, int is_write
, int mmu_idx
,
456 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
458 bool dtlb
= is_write
!= 2;
462 int ret
= xtensa_tlb_lookup(env
, vaddr
, dtlb
, &wi
, &ei
, &ring
);
464 if ((ret
== INST_TLB_MISS_CAUSE
|| ret
== LOAD_STORE_TLB_MISS_CAUSE
) &&
465 (mmu_idx
!= 0 || ((vaddr
^ env
->sregs
[PTEVADDR
]) & 0xffc00000)) &&
466 autorefill_mmu(env
, vaddr
, dtlb
, &wi
, &ei
, &ring
) == 0) {
473 const xtensa_tlb_entry
*entry
=
474 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
476 if (ring
< mmu_idx
) {
478 LOAD_STORE_PRIVILEGE_CAUSE
:
479 INST_FETCH_PRIVILEGE_CAUSE
;
482 *access
= mmu_attr_to_access(entry
->attr
);
483 if (!is_access_granted(*access
, is_write
)) {
486 STORE_PROHIBITED_CAUSE
:
487 LOAD_PROHIBITED_CAUSE
) :
488 INST_FETCH_PROHIBITED_CAUSE
;
491 *paddr
= entry
->paddr
| (vaddr
& ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
));
492 *page_size
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
497 static int autorefill_mmu(CPUXtensaState
*env
, uint32_t vaddr
, bool dtlb
,
498 uint32_t *wi
, uint32_t *ei
, uint8_t *ring
)
504 (env
->sregs
[PTEVADDR
] | (vaddr
>> 10)) & 0xfffffffc;
505 int ret
= get_physical_addr_mmu(env
, pt_vaddr
, 0, 0,
506 &paddr
, &page_size
, &access
);
508 qemu_log("%s: trying autorefill(%08x) -> %08x\n", __func__
,
509 vaddr
, ret
? ~0 : paddr
);
513 uint32_t pte
= ldl_phys(paddr
);
515 *ring
= (pte
>> 4) & 0x3;
516 *wi
= (++env
->autorefill_idx
) & 0x3;
517 split_tlb_entry_spec_way(env
, vaddr
, dtlb
, &vpn
, *wi
, ei
);
518 xtensa_tlb_set_entry(env
, dtlb
, *wi
, *ei
, vpn
, pte
);
519 qemu_log("%s: autorefill(%08x): %08x -> %08x\n",
520 __func__
, vaddr
, vpn
, pte
);
525 static int get_physical_addr_region(CPUXtensaState
*env
,
526 uint32_t vaddr
, int is_write
, int mmu_idx
,
527 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
529 bool dtlb
= is_write
!= 2;
531 uint32_t ei
= (vaddr
>> 29) & 0x7;
532 const xtensa_tlb_entry
*entry
=
533 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
535 *access
= region_attr_to_access(entry
->attr
);
536 if (!is_access_granted(*access
, is_write
)) {
539 STORE_PROHIBITED_CAUSE
:
540 LOAD_PROHIBITED_CAUSE
) :
541 INST_FETCH_PROHIBITED_CAUSE
;
544 *paddr
= entry
->paddr
| (vaddr
& ~REGION_PAGE_MASK
);
545 *page_size
= ~REGION_PAGE_MASK
+ 1;
551 * Convert virtual address to physical addr.
552 * MMU may issue pagewalk and change xtensa autorefill TLB way entry.
554 * \return 0 if ok, exception cause code otherwise
556 int xtensa_get_physical_addr(CPUXtensaState
*env
,
557 uint32_t vaddr
, int is_write
, int mmu_idx
,
558 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
560 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
561 return get_physical_addr_mmu(env
, vaddr
, is_write
, mmu_idx
,
562 paddr
, page_size
, access
);
563 } else if (xtensa_option_bits_enabled(env
->config
,
564 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
565 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
))) {
566 return get_physical_addr_region(env
, vaddr
, is_write
, mmu_idx
,
567 paddr
, page_size
, access
);
570 *page_size
= TARGET_PAGE_SIZE
;
571 *access
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
576 static void dump_tlb(FILE *f
, fprintf_function cpu_fprintf
,
577 CPUXtensaState
*env
, bool dtlb
)
580 const xtensa_tlb
*conf
=
581 dtlb
? &env
->config
->dtlb
: &env
->config
->itlb
;
582 unsigned (*attr_to_access
)(uint32_t) =
583 xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
) ?
584 mmu_attr_to_access
: region_attr_to_access
;
586 for (wi
= 0; wi
< conf
->nways
; ++wi
) {
587 uint32_t sz
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
589 bool print_header
= true;
591 if (sz
>= 0x100000) {
599 for (ei
= 0; ei
< conf
->way_size
[wi
]; ++ei
) {
600 const xtensa_tlb_entry
*entry
=
601 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
604 unsigned access
= attr_to_access(entry
->attr
);
607 print_header
= false;
608 cpu_fprintf(f
, "Way %u (%d %s)\n", wi
, sz
, sz_text
);
610 "\tVaddr Paddr ASID Attr RWX\n"
611 "\t---------- ---------- ---- ---- ---\n");
614 "\t0x%08x 0x%08x 0x%02x 0x%02x %c%c%c\n",
619 (access
& PAGE_READ
) ? 'R' : '-',
620 (access
& PAGE_WRITE
) ? 'W' : '-',
621 (access
& PAGE_EXEC
) ? 'X' : '-');
627 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUXtensaState
*env
)
629 if (xtensa_option_bits_enabled(env
->config
,
630 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
631 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
) |
632 XTENSA_OPTION_BIT(XTENSA_OPTION_MMU
))) {
634 cpu_fprintf(f
, "ITLB:\n");
635 dump_tlb(f
, cpu_fprintf
, env
, false);
636 cpu_fprintf(f
, "\nDTLB:\n");
637 dump_tlb(f
, cpu_fprintf
, env
, true);
639 cpu_fprintf(f
, "No TLB for this CPU core\n");