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
;
109 xtensa_translate_init();
112 if (!debug_handler_inited
&& tcg_enabled()) {
113 debug_handler_inited
= 1;
114 prev_debug_excp_handler
=
115 cpu_set_debug_excp_handler(breakpoint_handler
);
118 xtensa_irq_init(env
);
124 void xtensa_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
126 XtensaConfigList
*core
= xtensa_cores
;
127 cpu_fprintf(f
, "Available CPUs:\n");
128 for (; core
; core
= core
->next
) {
129 cpu_fprintf(f
, " %s\n", core
->config
->name
);
133 target_phys_addr_t
cpu_get_phys_page_debug(CPUXtensaState
*env
, target_ulong addr
)
139 if (xtensa_get_physical_addr(env
, addr
, 0, 0,
140 &paddr
, &page_size
, &access
) == 0) {
143 if (xtensa_get_physical_addr(env
, addr
, 2, 0,
144 &paddr
, &page_size
, &access
) == 0) {
150 static uint32_t relocated_vector(CPUXtensaState
*env
, uint32_t vector
)
152 if (xtensa_option_enabled(env
->config
,
153 XTENSA_OPTION_RELOCATABLE_VECTOR
)) {
154 return vector
- env
->config
->vecbase
+ env
->sregs
[VECBASE
];
161 * Handle penging IRQ.
162 * For the high priority interrupt jump to the corresponding interrupt vector.
163 * For the level-1 interrupt convert it to either user, kernel or double
164 * exception with the 'level-1 interrupt' exception cause.
166 static void handle_interrupt(CPUXtensaState
*env
)
168 int level
= env
->pending_irq_level
;
170 if (level
> xtensa_get_cintlevel(env
) &&
171 level
<= env
->config
->nlevel
&&
172 (env
->config
->level_mask
[level
] &
174 env
->sregs
[INTENABLE
])) {
176 env
->sregs
[EPC1
+ level
- 1] = env
->pc
;
177 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
179 (env
->sregs
[PS
] & ~PS_INTLEVEL
) | level
| PS_EXCM
;
180 env
->pc
= relocated_vector(env
,
181 env
->config
->interrupt_vector
[level
]);
183 env
->sregs
[EXCCAUSE
] = LEVEL1_INTERRUPT_CAUSE
;
185 if (env
->sregs
[PS
] & PS_EXCM
) {
186 if (env
->config
->ndepc
) {
187 env
->sregs
[DEPC
] = env
->pc
;
189 env
->sregs
[EPC1
] = env
->pc
;
191 env
->exception_index
= EXC_DOUBLE
;
193 env
->sregs
[EPC1
] = env
->pc
;
194 env
->exception_index
=
195 (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
197 env
->sregs
[PS
] |= PS_EXCM
;
199 env
->exception_taken
= 1;
203 void do_interrupt(CPUXtensaState
*env
)
205 if (env
->exception_index
== EXC_IRQ
) {
206 qemu_log_mask(CPU_LOG_INT
,
207 "%s(EXC_IRQ) level = %d, cintlevel = %d, "
208 "pc = %08x, a0 = %08x, ps = %08x, "
209 "intset = %08x, intenable = %08x, "
211 __func__
, env
->pending_irq_level
, xtensa_get_cintlevel(env
),
212 env
->pc
, env
->regs
[0], env
->sregs
[PS
],
213 env
->sregs
[INTSET
], env
->sregs
[INTENABLE
],
215 handle_interrupt(env
);
218 switch (env
->exception_index
) {
219 case EXC_WINDOW_OVERFLOW4
:
220 case EXC_WINDOW_UNDERFLOW4
:
221 case EXC_WINDOW_OVERFLOW8
:
222 case EXC_WINDOW_UNDERFLOW8
:
223 case EXC_WINDOW_OVERFLOW12
:
224 case EXC_WINDOW_UNDERFLOW12
:
229 qemu_log_mask(CPU_LOG_INT
, "%s(%d) "
230 "pc = %08x, a0 = %08x, ps = %08x, ccount = %08x\n",
231 __func__
, env
->exception_index
,
232 env
->pc
, env
->regs
[0], env
->sregs
[PS
], env
->sregs
[CCOUNT
]);
233 if (env
->config
->exception_vector
[env
->exception_index
]) {
234 env
->pc
= relocated_vector(env
,
235 env
->config
->exception_vector
[env
->exception_index
]);
236 env
->exception_taken
= 1;
238 qemu_log("%s(pc = %08x) bad exception_index: %d\n",
239 __func__
, env
->pc
, env
->exception_index
);
247 qemu_log("%s(pc = %08x) unknown exception_index: %d\n",
248 __func__
, env
->pc
, env
->exception_index
);
251 check_interrupts(env
);
254 static void reset_tlb_mmu_all_ways(CPUXtensaState
*env
,
255 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
259 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
260 for (ei
= 0; ei
< tlb
->way_size
[wi
]; ++ei
) {
261 entry
[wi
][ei
].asid
= 0;
262 entry
[wi
][ei
].variable
= true;
267 static void reset_tlb_mmu_ways56(CPUXtensaState
*env
,
268 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
270 if (!tlb
->varway56
) {
271 static const xtensa_tlb_entry way5
[] = {
286 static const xtensa_tlb_entry way6
[] = {
301 memcpy(entry
[5], way5
, sizeof(way5
));
302 memcpy(entry
[6], way6
, sizeof(way6
));
305 for (ei
= 0; ei
< 8; ++ei
) {
306 entry
[6][ei
].vaddr
= ei
<< 29;
307 entry
[6][ei
].paddr
= ei
<< 29;
308 entry
[6][ei
].asid
= 1;
309 entry
[6][ei
].attr
= 3;
314 static void reset_tlb_region_way0(CPUXtensaState
*env
,
315 xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
319 for (ei
= 0; ei
< 8; ++ei
) {
320 entry
[0][ei
].vaddr
= ei
<< 29;
321 entry
[0][ei
].paddr
= ei
<< 29;
322 entry
[0][ei
].asid
= 1;
323 entry
[0][ei
].attr
= 2;
324 entry
[0][ei
].variable
= true;
328 void reset_mmu(CPUXtensaState
*env
)
330 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
331 env
->sregs
[RASID
] = 0x04030201;
332 env
->sregs
[ITLBCFG
] = 0;
333 env
->sregs
[DTLBCFG
] = 0;
334 env
->autorefill_idx
= 0;
335 reset_tlb_mmu_all_ways(env
, &env
->config
->itlb
, env
->itlb
);
336 reset_tlb_mmu_all_ways(env
, &env
->config
->dtlb
, env
->dtlb
);
337 reset_tlb_mmu_ways56(env
, &env
->config
->itlb
, env
->itlb
);
338 reset_tlb_mmu_ways56(env
, &env
->config
->dtlb
, env
->dtlb
);
340 reset_tlb_region_way0(env
, env
->itlb
);
341 reset_tlb_region_way0(env
, env
->dtlb
);
345 static unsigned get_ring(const CPUXtensaState
*env
, uint8_t asid
)
348 for (i
= 0; i
< 4; ++i
) {
349 if (((env
->sregs
[RASID
] >> i
* 8) & 0xff) == asid
) {
357 * Lookup xtensa TLB for the given virtual address.
360 * \param pwi: [out] way index
361 * \param pei: [out] entry index
362 * \param pring: [out] access ring
363 * \return 0 if ok, exception cause code otherwise
365 int xtensa_tlb_lookup(const CPUXtensaState
*env
, uint32_t addr
, bool dtlb
,
366 uint32_t *pwi
, uint32_t *pei
, uint8_t *pring
)
368 const xtensa_tlb
*tlb
= dtlb
?
369 &env
->config
->dtlb
: &env
->config
->itlb
;
370 const xtensa_tlb_entry (*entry
)[MAX_TLB_WAY_SIZE
] = dtlb
?
371 env
->dtlb
: env
->itlb
;
376 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
379 split_tlb_entry_spec_way(env
, addr
, dtlb
, &vpn
, wi
, &ei
);
380 if (entry
[wi
][ei
].vaddr
== vpn
&& entry
[wi
][ei
].asid
) {
381 unsigned ring
= get_ring(env
, entry
[wi
][ei
].asid
);
385 LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
386 INST_TLB_MULTI_HIT_CAUSE
;
395 (dtlb
? LOAD_STORE_TLB_MISS_CAUSE
: INST_TLB_MISS_CAUSE
);
399 * Convert MMU ATTR to PAGE_{READ,WRITE,EXEC} mask.
402 static unsigned mmu_attr_to_access(uint32_t attr
)
411 access
|= PAGE_WRITE
;
413 } else if (attr
== 13) {
414 access
|= PAGE_READ
| PAGE_WRITE
;
420 * Convert region protection ATTR to PAGE_{READ,WRITE,EXEC} mask.
423 static unsigned region_attr_to_access(uint32_t attr
)
426 if ((attr
< 6 && attr
!= 3) || attr
== 14) {
427 access
|= PAGE_READ
| PAGE_WRITE
;
429 if (attr
> 0 && attr
< 6) {
435 static bool is_access_granted(unsigned access
, int is_write
)
439 return access
& PAGE_READ
;
442 return access
& PAGE_WRITE
;
445 return access
& PAGE_EXEC
;
452 static int autorefill_mmu(CPUXtensaState
*env
, uint32_t vaddr
, bool dtlb
,
453 uint32_t *wi
, uint32_t *ei
, uint8_t *ring
);
455 static int get_physical_addr_mmu(CPUXtensaState
*env
,
456 uint32_t vaddr
, int is_write
, int mmu_idx
,
457 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
459 bool dtlb
= is_write
!= 2;
463 int ret
= xtensa_tlb_lookup(env
, vaddr
, dtlb
, &wi
, &ei
, &ring
);
465 if ((ret
== INST_TLB_MISS_CAUSE
|| ret
== LOAD_STORE_TLB_MISS_CAUSE
) &&
466 (mmu_idx
!= 0 || ((vaddr
^ env
->sregs
[PTEVADDR
]) & 0xffc00000)) &&
467 autorefill_mmu(env
, vaddr
, dtlb
, &wi
, &ei
, &ring
) == 0) {
474 const xtensa_tlb_entry
*entry
=
475 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
477 if (ring
< mmu_idx
) {
479 LOAD_STORE_PRIVILEGE_CAUSE
:
480 INST_FETCH_PRIVILEGE_CAUSE
;
483 *access
= mmu_attr_to_access(entry
->attr
);
484 if (!is_access_granted(*access
, is_write
)) {
487 STORE_PROHIBITED_CAUSE
:
488 LOAD_PROHIBITED_CAUSE
) :
489 INST_FETCH_PROHIBITED_CAUSE
;
492 *paddr
= entry
->paddr
| (vaddr
& ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
));
493 *page_size
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
498 static int autorefill_mmu(CPUXtensaState
*env
, uint32_t vaddr
, bool dtlb
,
499 uint32_t *wi
, uint32_t *ei
, uint8_t *ring
)
505 (env
->sregs
[PTEVADDR
] | (vaddr
>> 10)) & 0xfffffffc;
506 int ret
= get_physical_addr_mmu(env
, pt_vaddr
, 0, 0,
507 &paddr
, &page_size
, &access
);
509 qemu_log("%s: trying autorefill(%08x) -> %08x\n", __func__
,
510 vaddr
, ret
? ~0 : paddr
);
514 uint32_t pte
= ldl_phys(paddr
);
516 *ring
= (pte
>> 4) & 0x3;
517 *wi
= (++env
->autorefill_idx
) & 0x3;
518 split_tlb_entry_spec_way(env
, vaddr
, dtlb
, &vpn
, *wi
, ei
);
519 xtensa_tlb_set_entry(env
, dtlb
, *wi
, *ei
, vpn
, pte
);
520 qemu_log("%s: autorefill(%08x): %08x -> %08x\n",
521 __func__
, vaddr
, vpn
, pte
);
526 static int get_physical_addr_region(CPUXtensaState
*env
,
527 uint32_t vaddr
, int is_write
, int mmu_idx
,
528 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
530 bool dtlb
= is_write
!= 2;
532 uint32_t ei
= (vaddr
>> 29) & 0x7;
533 const xtensa_tlb_entry
*entry
=
534 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
536 *access
= region_attr_to_access(entry
->attr
);
537 if (!is_access_granted(*access
, is_write
)) {
540 STORE_PROHIBITED_CAUSE
:
541 LOAD_PROHIBITED_CAUSE
) :
542 INST_FETCH_PROHIBITED_CAUSE
;
545 *paddr
= entry
->paddr
| (vaddr
& ~REGION_PAGE_MASK
);
546 *page_size
= ~REGION_PAGE_MASK
+ 1;
552 * Convert virtual address to physical addr.
553 * MMU may issue pagewalk and change xtensa autorefill TLB way entry.
555 * \return 0 if ok, exception cause code otherwise
557 int xtensa_get_physical_addr(CPUXtensaState
*env
,
558 uint32_t vaddr
, int is_write
, int mmu_idx
,
559 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
561 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
562 return get_physical_addr_mmu(env
, vaddr
, is_write
, mmu_idx
,
563 paddr
, page_size
, access
);
564 } else if (xtensa_option_bits_enabled(env
->config
,
565 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
566 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
))) {
567 return get_physical_addr_region(env
, vaddr
, is_write
, mmu_idx
,
568 paddr
, page_size
, access
);
571 *page_size
= TARGET_PAGE_SIZE
;
572 *access
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
577 static void dump_tlb(FILE *f
, fprintf_function cpu_fprintf
,
578 CPUXtensaState
*env
, bool dtlb
)
581 const xtensa_tlb
*conf
=
582 dtlb
? &env
->config
->dtlb
: &env
->config
->itlb
;
583 unsigned (*attr_to_access
)(uint32_t) =
584 xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
) ?
585 mmu_attr_to_access
: region_attr_to_access
;
587 for (wi
= 0; wi
< conf
->nways
; ++wi
) {
588 uint32_t sz
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
590 bool print_header
= true;
592 if (sz
>= 0x100000) {
600 for (ei
= 0; ei
< conf
->way_size
[wi
]; ++ei
) {
601 const xtensa_tlb_entry
*entry
=
602 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
605 unsigned access
= attr_to_access(entry
->attr
);
608 print_header
= false;
609 cpu_fprintf(f
, "Way %u (%d %s)\n", wi
, sz
, sz_text
);
611 "\tVaddr Paddr ASID Attr RWX\n"
612 "\t---------- ---------- ---- ---- ---\n");
615 "\t0x%08x 0x%08x 0x%02x 0x%02x %c%c%c\n",
620 (access
& PAGE_READ
) ? 'R' : '-',
621 (access
& PAGE_WRITE
) ? 'W' : '-',
622 (access
& PAGE_EXEC
) ? 'X' : '-');
628 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUXtensaState
*env
)
630 if (xtensa_option_bits_enabled(env
->config
,
631 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
632 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
) |
633 XTENSA_OPTION_BIT(XTENSA_OPTION_MMU
))) {
635 cpu_fprintf(f
, "ITLB:\n");
636 dump_tlb(f
, cpu_fprintf
, env
, false);
637 cpu_fprintf(f
, "\nDTLB:\n");
638 dump_tlb(f
, cpu_fprintf
, env
, true);
640 cpu_fprintf(f
, "No TLB for this CPU core\n");