4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5 * Zhizhou Zhang <etouzh@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "qemu-common.h"
25 #include "exec/gdbstub.h"
26 #include "qemu/host-utils.h"
27 #ifndef CONFIG_USER_ONLY
28 #include "hw/loader.h"
31 #ifndef CONFIG_USER_ONLY
32 static inline int get_phys_nommu(hwaddr
*physical
, int *prot
,
36 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
40 static int get_phys_code(OpenRISCCPU
*cpu
, hwaddr
*physical
, int *prot
,
41 target_ulong address
, int rw
, bool supervisor
)
43 int vpn
= address
>> TARGET_PAGE_BITS
;
44 int idx
= vpn
& ITLB_MASK
;
47 if ((cpu
->env
.tlb
.itlb
[0][idx
].mr
>> TARGET_PAGE_BITS
) != vpn
) {
48 return TLBRET_NOMATCH
;
50 if (!(cpu
->env
.tlb
.itlb
[0][idx
].mr
& 1)) {
51 return TLBRET_INVALID
;
54 if (cpu
->env
.tlb
.itlb
[0][idx
].tr
& SXE
) {
58 if (cpu
->env
.tlb
.itlb
[0][idx
].tr
& UXE
) {
62 if ((rw
& 2) && ((right
& PAGE_EXEC
) == 0)) {
63 return TLBRET_BADADDR
;
66 *physical
= (cpu
->env
.tlb
.itlb
[0][idx
].tr
& TARGET_PAGE_MASK
) |
67 (address
& (TARGET_PAGE_SIZE
-1));
72 static int get_phys_data(OpenRISCCPU
*cpu
, hwaddr
*physical
, int *prot
,
73 target_ulong address
, int rw
, bool supervisor
)
75 int vpn
= address
>> TARGET_PAGE_BITS
;
76 int idx
= vpn
& DTLB_MASK
;
79 if ((cpu
->env
.tlb
.dtlb
[0][idx
].mr
>> TARGET_PAGE_BITS
) != vpn
) {
80 return TLBRET_NOMATCH
;
82 if (!(cpu
->env
.tlb
.dtlb
[0][idx
].mr
& 1)) {
83 return TLBRET_INVALID
;
86 if (cpu
->env
.tlb
.dtlb
[0][idx
].tr
& SRE
) {
89 if (cpu
->env
.tlb
.dtlb
[0][idx
].tr
& SWE
) {
93 if (cpu
->env
.tlb
.dtlb
[0][idx
].tr
& URE
) {
96 if (cpu
->env
.tlb
.dtlb
[0][idx
].tr
& UWE
) {
101 if (!(rw
& 1) && ((right
& PAGE_READ
) == 0)) {
102 return TLBRET_BADADDR
;
104 if ((rw
& 1) && ((right
& PAGE_WRITE
) == 0)) {
105 return TLBRET_BADADDR
;
108 *physical
= (cpu
->env
.tlb
.dtlb
[0][idx
].tr
& TARGET_PAGE_MASK
) |
109 (address
& (TARGET_PAGE_SIZE
-1));
114 static int get_phys_addr(OpenRISCCPU
*cpu
, hwaddr
*physical
,
115 int *prot
, target_ulong address
, int rw
)
117 bool supervisor
= (cpu
->env
.sr
& SR_SM
) != 0;
120 /* Assume nommu results for a moment. */
121 ret
= get_phys_nommu(physical
, prot
, address
);
123 /* Overwrite with TLB lookup if enabled. */
124 if (rw
== MMU_INST_FETCH
) {
125 if (cpu
->env
.sr
& SR_IME
) {
126 ret
= get_phys_code(cpu
, physical
, prot
, address
, rw
, supervisor
);
129 if (cpu
->env
.sr
& SR_DME
) {
130 ret
= get_phys_data(cpu
, physical
, prot
, address
, rw
, supervisor
);
138 static void cpu_openrisc_raise_mmu_exception(OpenRISCCPU
*cpu
,
139 target_ulong address
,
140 int rw
, int tlb_error
)
142 CPUState
*cs
= CPU(cpu
);
148 exception
= EXCP_IPF
;
150 exception
= EXCP_DPF
;
153 #ifndef CONFIG_USER_ONLY
156 exception
= EXCP_IPF
;
158 exception
= EXCP_DPF
;
163 /* No TLB match for a mapped address */
165 exception
= EXCP_ITLBMISS
;
167 exception
= EXCP_DTLBMISS
;
173 cs
->exception_index
= exception
;
174 cpu
->env
.eear
= address
;
175 cpu
->env
.lock_addr
= -1;
178 #ifndef CONFIG_USER_ONLY
179 int openrisc_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
,
182 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
187 ret
= get_phys_addr(cpu
, &physical
, &prot
, address
, rw
);
189 if (ret
== TLBRET_MATCH
) {
190 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
191 physical
& TARGET_PAGE_MASK
, prot
,
192 mmu_idx
, TARGET_PAGE_SIZE
);
194 } else if (ret
< 0) {
195 cpu_openrisc_raise_mmu_exception(cpu
, address
, rw
, ret
);
202 int openrisc_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
,
205 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
208 cpu_openrisc_raise_mmu_exception(cpu
, address
, rw
, ret
);
215 #ifndef CONFIG_USER_ONLY
216 hwaddr
openrisc_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
218 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
223 /* Check memory for any kind of address, since during debug the
224 gdb can ask for anything, check data tlb for address */
225 miss
= get_phys_addr(cpu
, &phys_addr
, &prot
, addr
, 0);
227 /* Check instruction tlb */
229 miss
= get_phys_addr(cpu
, &phys_addr
, &prot
, addr
, MMU_INST_FETCH
);
232 /* Last, fall back to a plain address */
234 miss
= get_phys_nommu(&phys_addr
, &prot
, addr
);
244 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
245 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
247 int ret
= openrisc_cpu_handle_mmu_fault(cs
, addr
, size
,
248 access_type
, mmu_idx
);
250 /* Raise Exception. */
251 cpu_loop_exit_restore(cs
, retaddr
);