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 static struct XtensaConfigList
*xtensa_cores
;
38 void xtensa_register_core(XtensaConfigList
*node
)
40 node
->next
= xtensa_cores
;
44 static uint32_t check_hw_breakpoints(CPUXtensaState
*env
)
48 for (i
= 0; i
< env
->config
->ndbreak
; ++i
) {
49 if (env
->cpu_watchpoint
[i
] &&
50 env
->cpu_watchpoint
[i
]->flags
& BP_WATCHPOINT_HIT
) {
51 return DEBUGCAUSE_DB
| (i
<< DEBUGCAUSE_DBNUM_SHIFT
);
57 static void breakpoint_handler(CPUXtensaState
*env
)
59 if (env
->watchpoint_hit
) {
60 if (env
->watchpoint_hit
->flags
& BP_CPU
) {
63 env
->watchpoint_hit
= NULL
;
64 cause
= check_hw_breakpoints(env
);
66 debug_exception_env(env
, cause
);
68 cpu_resume_from_signal(env
, NULL
);
73 XtensaCPU
*cpu_xtensa_init(const char *cpu_model
)
75 static int tcg_inited
;
76 static int debug_handler_inited
;
79 const XtensaConfig
*config
= NULL
;
80 XtensaConfigList
*core
= xtensa_cores
;
82 for (; core
; core
= core
->next
)
83 if (strcmp(core
->config
->name
, cpu_model
) == 0) {
84 config
= core
->config
;
92 cpu
= XTENSA_CPU(object_new(TYPE_XTENSA_CPU
));
98 xtensa_translate_init();
101 if (!debug_handler_inited
&& tcg_enabled()) {
102 debug_handler_inited
= 1;
103 cpu_set_debug_excp_handler(breakpoint_handler
);
106 xtensa_irq_init(env
);
112 void xtensa_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
114 XtensaConfigList
*core
= xtensa_cores
;
115 cpu_fprintf(f
, "Available CPUs:\n");
116 for (; core
; core
= core
->next
) {
117 cpu_fprintf(f
, " %s\n", core
->config
->name
);
121 hwaddr
cpu_get_phys_page_debug(CPUXtensaState
*env
, target_ulong addr
)
127 if (xtensa_get_physical_addr(env
, false, addr
, 0, 0,
128 &paddr
, &page_size
, &access
) == 0) {
131 if (xtensa_get_physical_addr(env
, false, addr
, 2, 0,
132 &paddr
, &page_size
, &access
) == 0) {
138 static uint32_t relocated_vector(CPUXtensaState
*env
, uint32_t vector
)
140 if (xtensa_option_enabled(env
->config
,
141 XTENSA_OPTION_RELOCATABLE_VECTOR
)) {
142 return vector
- env
->config
->vecbase
+ env
->sregs
[VECBASE
];
149 * Handle penging IRQ.
150 * For the high priority interrupt jump to the corresponding interrupt vector.
151 * For the level-1 interrupt convert it to either user, kernel or double
152 * exception with the 'level-1 interrupt' exception cause.
154 static void handle_interrupt(CPUXtensaState
*env
)
156 int level
= env
->pending_irq_level
;
158 if (level
> xtensa_get_cintlevel(env
) &&
159 level
<= env
->config
->nlevel
&&
160 (env
->config
->level_mask
[level
] &
162 env
->sregs
[INTENABLE
])) {
164 env
->sregs
[EPC1
+ level
- 1] = env
->pc
;
165 env
->sregs
[EPS2
+ level
- 2] = env
->sregs
[PS
];
167 (env
->sregs
[PS
] & ~PS_INTLEVEL
) | level
| PS_EXCM
;
168 env
->pc
= relocated_vector(env
,
169 env
->config
->interrupt_vector
[level
]);
171 env
->sregs
[EXCCAUSE
] = LEVEL1_INTERRUPT_CAUSE
;
173 if (env
->sregs
[PS
] & PS_EXCM
) {
174 if (env
->config
->ndepc
) {
175 env
->sregs
[DEPC
] = env
->pc
;
177 env
->sregs
[EPC1
] = env
->pc
;
179 env
->exception_index
= EXC_DOUBLE
;
181 env
->sregs
[EPC1
] = env
->pc
;
182 env
->exception_index
=
183 (env
->sregs
[PS
] & PS_UM
) ? EXC_USER
: EXC_KERNEL
;
185 env
->sregs
[PS
] |= PS_EXCM
;
187 env
->exception_taken
= 1;
191 void do_interrupt(CPUXtensaState
*env
)
193 if (env
->exception_index
== EXC_IRQ
) {
194 qemu_log_mask(CPU_LOG_INT
,
195 "%s(EXC_IRQ) level = %d, cintlevel = %d, "
196 "pc = %08x, a0 = %08x, ps = %08x, "
197 "intset = %08x, intenable = %08x, "
199 __func__
, env
->pending_irq_level
, xtensa_get_cintlevel(env
),
200 env
->pc
, env
->regs
[0], env
->sregs
[PS
],
201 env
->sregs
[INTSET
], env
->sregs
[INTENABLE
],
203 handle_interrupt(env
);
206 switch (env
->exception_index
) {
207 case EXC_WINDOW_OVERFLOW4
:
208 case EXC_WINDOW_UNDERFLOW4
:
209 case EXC_WINDOW_OVERFLOW8
:
210 case EXC_WINDOW_UNDERFLOW8
:
211 case EXC_WINDOW_OVERFLOW12
:
212 case EXC_WINDOW_UNDERFLOW12
:
217 qemu_log_mask(CPU_LOG_INT
, "%s(%d) "
218 "pc = %08x, a0 = %08x, ps = %08x, ccount = %08x\n",
219 __func__
, env
->exception_index
,
220 env
->pc
, env
->regs
[0], env
->sregs
[PS
], env
->sregs
[CCOUNT
]);
221 if (env
->config
->exception_vector
[env
->exception_index
]) {
222 env
->pc
= relocated_vector(env
,
223 env
->config
->exception_vector
[env
->exception_index
]);
224 env
->exception_taken
= 1;
226 qemu_log("%s(pc = %08x) bad exception_index: %d\n",
227 __func__
, env
->pc
, env
->exception_index
);
235 qemu_log("%s(pc = %08x) unknown exception_index: %d\n",
236 __func__
, env
->pc
, env
->exception_index
);
239 check_interrupts(env
);
242 static void reset_tlb_mmu_all_ways(CPUXtensaState
*env
,
243 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
247 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
248 for (ei
= 0; ei
< tlb
->way_size
[wi
]; ++ei
) {
249 entry
[wi
][ei
].asid
= 0;
250 entry
[wi
][ei
].variable
= true;
255 static void reset_tlb_mmu_ways56(CPUXtensaState
*env
,
256 const xtensa_tlb
*tlb
, xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
258 if (!tlb
->varway56
) {
259 static const xtensa_tlb_entry way5
[] = {
274 static const xtensa_tlb_entry way6
[] = {
289 memcpy(entry
[5], way5
, sizeof(way5
));
290 memcpy(entry
[6], way6
, sizeof(way6
));
293 for (ei
= 0; ei
< 8; ++ei
) {
294 entry
[6][ei
].vaddr
= ei
<< 29;
295 entry
[6][ei
].paddr
= ei
<< 29;
296 entry
[6][ei
].asid
= 1;
297 entry
[6][ei
].attr
= 3;
302 static void reset_tlb_region_way0(CPUXtensaState
*env
,
303 xtensa_tlb_entry entry
[][MAX_TLB_WAY_SIZE
])
307 for (ei
= 0; ei
< 8; ++ei
) {
308 entry
[0][ei
].vaddr
= ei
<< 29;
309 entry
[0][ei
].paddr
= ei
<< 29;
310 entry
[0][ei
].asid
= 1;
311 entry
[0][ei
].attr
= 2;
312 entry
[0][ei
].variable
= true;
316 void reset_mmu(CPUXtensaState
*env
)
318 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
319 env
->sregs
[RASID
] = 0x04030201;
320 env
->sregs
[ITLBCFG
] = 0;
321 env
->sregs
[DTLBCFG
] = 0;
322 env
->autorefill_idx
= 0;
323 reset_tlb_mmu_all_ways(env
, &env
->config
->itlb
, env
->itlb
);
324 reset_tlb_mmu_all_ways(env
, &env
->config
->dtlb
, env
->dtlb
);
325 reset_tlb_mmu_ways56(env
, &env
->config
->itlb
, env
->itlb
);
326 reset_tlb_mmu_ways56(env
, &env
->config
->dtlb
, env
->dtlb
);
328 reset_tlb_region_way0(env
, env
->itlb
);
329 reset_tlb_region_way0(env
, env
->dtlb
);
333 static unsigned get_ring(const CPUXtensaState
*env
, uint8_t asid
)
336 for (i
= 0; i
< 4; ++i
) {
337 if (((env
->sregs
[RASID
] >> i
* 8) & 0xff) == asid
) {
345 * Lookup xtensa TLB for the given virtual address.
348 * \param pwi: [out] way index
349 * \param pei: [out] entry index
350 * \param pring: [out] access ring
351 * \return 0 if ok, exception cause code otherwise
353 int xtensa_tlb_lookup(const CPUXtensaState
*env
, uint32_t addr
, bool dtlb
,
354 uint32_t *pwi
, uint32_t *pei
, uint8_t *pring
)
356 const xtensa_tlb
*tlb
= dtlb
?
357 &env
->config
->dtlb
: &env
->config
->itlb
;
358 const xtensa_tlb_entry (*entry
)[MAX_TLB_WAY_SIZE
] = dtlb
?
359 env
->dtlb
: env
->itlb
;
364 for (wi
= 0; wi
< tlb
->nways
; ++wi
) {
367 split_tlb_entry_spec_way(env
, addr
, dtlb
, &vpn
, wi
, &ei
);
368 if (entry
[wi
][ei
].vaddr
== vpn
&& entry
[wi
][ei
].asid
) {
369 unsigned ring
= get_ring(env
, entry
[wi
][ei
].asid
);
373 LOAD_STORE_TLB_MULTI_HIT_CAUSE
:
374 INST_TLB_MULTI_HIT_CAUSE
;
383 (dtlb
? LOAD_STORE_TLB_MISS_CAUSE
: INST_TLB_MISS_CAUSE
);
387 * Convert MMU ATTR to PAGE_{READ,WRITE,EXEC} mask.
390 static unsigned mmu_attr_to_access(uint32_t attr
)
399 access
|= PAGE_WRITE
;
401 } else if (attr
== 13) {
402 access
|= PAGE_READ
| PAGE_WRITE
;
408 * Convert region protection ATTR to PAGE_{READ,WRITE,EXEC} mask.
411 static unsigned region_attr_to_access(uint32_t attr
)
414 if ((attr
< 6 && attr
!= 3) || attr
== 14) {
415 access
|= PAGE_READ
| PAGE_WRITE
;
417 if (attr
> 0 && attr
< 6) {
423 static bool is_access_granted(unsigned access
, int is_write
)
427 return access
& PAGE_READ
;
430 return access
& PAGE_WRITE
;
433 return access
& PAGE_EXEC
;
440 static int get_pte(CPUXtensaState
*env
, uint32_t vaddr
, uint32_t *pte
);
442 static int get_physical_addr_mmu(CPUXtensaState
*env
, bool update_tlb
,
443 uint32_t vaddr
, int is_write
, int mmu_idx
,
444 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
,
447 bool dtlb
= is_write
!= 2;
453 const xtensa_tlb_entry
*entry
= NULL
;
454 xtensa_tlb_entry tmp_entry
;
455 int ret
= xtensa_tlb_lookup(env
, vaddr
, dtlb
, &wi
, &ei
, &ring
);
457 if ((ret
== INST_TLB_MISS_CAUSE
|| ret
== LOAD_STORE_TLB_MISS_CAUSE
) &&
458 may_lookup_pt
&& get_pte(env
, vaddr
, &pte
) == 0) {
459 ring
= (pte
>> 4) & 0x3;
461 split_tlb_entry_spec_way(env
, vaddr
, dtlb
, &vpn
, wi
, &ei
);
464 wi
= ++env
->autorefill_idx
& 0x3;
465 xtensa_tlb_set_entry(env
, dtlb
, wi
, ei
, vpn
, pte
);
466 env
->sregs
[EXCVADDR
] = vaddr
;
467 qemu_log("%s: autorefill(%08x): %08x -> %08x\n",
468 __func__
, vaddr
, vpn
, pte
);
470 xtensa_tlb_set_entry_mmu(env
, &tmp_entry
, dtlb
, wi
, ei
, vpn
, pte
);
480 entry
= xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
483 if (ring
< mmu_idx
) {
485 LOAD_STORE_PRIVILEGE_CAUSE
:
486 INST_FETCH_PRIVILEGE_CAUSE
;
489 *access
= mmu_attr_to_access(entry
->attr
);
490 if (!is_access_granted(*access
, is_write
)) {
493 STORE_PROHIBITED_CAUSE
:
494 LOAD_PROHIBITED_CAUSE
) :
495 INST_FETCH_PROHIBITED_CAUSE
;
498 *paddr
= entry
->paddr
| (vaddr
& ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
));
499 *page_size
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
504 static int get_pte(CPUXtensaState
*env
, uint32_t vaddr
, uint32_t *pte
)
510 (env
->sregs
[PTEVADDR
] | (vaddr
>> 10)) & 0xfffffffc;
511 int ret
= get_physical_addr_mmu(env
, false, pt_vaddr
, 0, 0,
512 &paddr
, &page_size
, &access
, false);
514 qemu_log("%s: trying autorefill(%08x) -> %08x\n", __func__
,
515 vaddr
, ret
? ~0 : paddr
);
518 *pte
= ldl_phys(paddr
);
523 static int get_physical_addr_region(CPUXtensaState
*env
,
524 uint32_t vaddr
, int is_write
, int mmu_idx
,
525 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
527 bool dtlb
= is_write
!= 2;
529 uint32_t ei
= (vaddr
>> 29) & 0x7;
530 const xtensa_tlb_entry
*entry
=
531 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
533 *access
= region_attr_to_access(entry
->attr
);
534 if (!is_access_granted(*access
, is_write
)) {
537 STORE_PROHIBITED_CAUSE
:
538 LOAD_PROHIBITED_CAUSE
) :
539 INST_FETCH_PROHIBITED_CAUSE
;
542 *paddr
= entry
->paddr
| (vaddr
& ~REGION_PAGE_MASK
);
543 *page_size
= ~REGION_PAGE_MASK
+ 1;
549 * Convert virtual address to physical addr.
550 * MMU may issue pagewalk and change xtensa autorefill TLB way entry.
552 * \return 0 if ok, exception cause code otherwise
554 int xtensa_get_physical_addr(CPUXtensaState
*env
, bool update_tlb
,
555 uint32_t vaddr
, int is_write
, int mmu_idx
,
556 uint32_t *paddr
, uint32_t *page_size
, unsigned *access
)
558 if (xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
)) {
559 return get_physical_addr_mmu(env
, update_tlb
,
560 vaddr
, is_write
, mmu_idx
, paddr
, page_size
, access
, true);
561 } else if (xtensa_option_bits_enabled(env
->config
,
562 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
563 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
))) {
564 return get_physical_addr_region(env
, vaddr
, is_write
, mmu_idx
,
565 paddr
, page_size
, access
);
568 *page_size
= TARGET_PAGE_SIZE
;
569 *access
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
574 static void dump_tlb(FILE *f
, fprintf_function cpu_fprintf
,
575 CPUXtensaState
*env
, bool dtlb
)
578 const xtensa_tlb
*conf
=
579 dtlb
? &env
->config
->dtlb
: &env
->config
->itlb
;
580 unsigned (*attr_to_access
)(uint32_t) =
581 xtensa_option_enabled(env
->config
, XTENSA_OPTION_MMU
) ?
582 mmu_attr_to_access
: region_attr_to_access
;
584 for (wi
= 0; wi
< conf
->nways
; ++wi
) {
585 uint32_t sz
= ~xtensa_tlb_get_addr_mask(env
, dtlb
, wi
) + 1;
587 bool print_header
= true;
589 if (sz
>= 0x100000) {
597 for (ei
= 0; ei
< conf
->way_size
[wi
]; ++ei
) {
598 const xtensa_tlb_entry
*entry
=
599 xtensa_tlb_get_entry(env
, dtlb
, wi
, ei
);
602 unsigned access
= attr_to_access(entry
->attr
);
605 print_header
= false;
606 cpu_fprintf(f
, "Way %u (%d %s)\n", wi
, sz
, sz_text
);
608 "\tVaddr Paddr ASID Attr RWX\n"
609 "\t---------- ---------- ---- ---- ---\n");
612 "\t0x%08x 0x%08x 0x%02x 0x%02x %c%c%c\n",
617 (access
& PAGE_READ
) ? 'R' : '-',
618 (access
& PAGE_WRITE
) ? 'W' : '-',
619 (access
& PAGE_EXEC
) ? 'X' : '-');
625 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUXtensaState
*env
)
627 if (xtensa_option_bits_enabled(env
->config
,
628 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
629 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
) |
630 XTENSA_OPTION_BIT(XTENSA_OPTION_MMU
))) {
632 cpu_fprintf(f
, "ITLB:\n");
633 dump_tlb(f
, cpu_fprintf
, env
, false);
634 cpu_fprintf(f
, "\nDTLB:\n");
635 dump_tlb(f
, cpu_fprintf
, env
, true);
637 cpu_fprintf(f
, "No TLB for this CPU core\n");