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 int cpu_openrisc_get_phys_nommu(OpenRISCCPU
*cpu
,
34 int *prot
, target_ulong address
, int rw
)
37 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
41 int cpu_openrisc_get_phys_code(OpenRISCCPU
*cpu
,
43 int *prot
, target_ulong address
, int rw
)
45 int vpn
= address
>> TARGET_PAGE_BITS
;
46 int idx
= vpn
& ITLB_MASK
;
49 if ((cpu
->env
.tlb
->itlb
[0][idx
].mr
>> TARGET_PAGE_BITS
) != vpn
) {
50 return TLBRET_NOMATCH
;
52 if (!(cpu
->env
.tlb
->itlb
[0][idx
].mr
& 1)) {
53 return TLBRET_INVALID
;
56 if (cpu
->env
.sr
& SR_SM
) { /* supervisor mode */
57 if (cpu
->env
.tlb
->itlb
[0][idx
].tr
& SXE
) {
61 if (cpu
->env
.tlb
->itlb
[0][idx
].tr
& UXE
) {
66 if ((rw
& 2) && ((right
& PAGE_EXEC
) == 0)) {
67 return TLBRET_BADADDR
;
70 *physical
= (cpu
->env
.tlb
->itlb
[0][idx
].tr
& TARGET_PAGE_MASK
) |
71 (address
& (TARGET_PAGE_SIZE
-1));
76 int cpu_openrisc_get_phys_data(OpenRISCCPU
*cpu
,
78 int *prot
, target_ulong address
, int rw
)
80 int vpn
= address
>> TARGET_PAGE_BITS
;
81 int idx
= vpn
& DTLB_MASK
;
84 if ((cpu
->env
.tlb
->dtlb
[0][idx
].mr
>> TARGET_PAGE_BITS
) != vpn
) {
85 return TLBRET_NOMATCH
;
87 if (!(cpu
->env
.tlb
->dtlb
[0][idx
].mr
& 1)) {
88 return TLBRET_INVALID
;
91 if (cpu
->env
.sr
& SR_SM
) { /* supervisor mode */
92 if (cpu
->env
.tlb
->dtlb
[0][idx
].tr
& SRE
) {
95 if (cpu
->env
.tlb
->dtlb
[0][idx
].tr
& SWE
) {
99 if (cpu
->env
.tlb
->dtlb
[0][idx
].tr
& URE
) {
102 if (cpu
->env
.tlb
->dtlb
[0][idx
].tr
& UWE
) {
107 if (!(rw
& 1) && ((right
& PAGE_READ
) == 0)) {
108 return TLBRET_BADADDR
;
110 if ((rw
& 1) && ((right
& PAGE_WRITE
) == 0)) {
111 return TLBRET_BADADDR
;
114 *physical
= (cpu
->env
.tlb
->dtlb
[0][idx
].tr
& TARGET_PAGE_MASK
) |
115 (address
& (TARGET_PAGE_SIZE
-1));
120 static int cpu_openrisc_get_phys_addr(OpenRISCCPU
*cpu
,
122 int *prot
, target_ulong address
,
125 int ret
= TLBRET_MATCH
;
127 if (rw
== MMU_INST_FETCH
) { /* ITLB */
129 ret
= cpu
->env
.tlb
->cpu_openrisc_map_address_code(cpu
, physical
,
132 ret
= cpu
->env
.tlb
->cpu_openrisc_map_address_data(cpu
, physical
,
140 static void cpu_openrisc_raise_mmu_exception(OpenRISCCPU
*cpu
,
141 target_ulong address
,
142 int rw
, int tlb_error
)
144 CPUState
*cs
= CPU(cpu
);
150 exception
= EXCP_IPF
;
152 exception
= EXCP_DPF
;
155 #ifndef CONFIG_USER_ONLY
158 exception
= EXCP_IPF
;
160 exception
= EXCP_DPF
;
165 /* No TLB match for a mapped address */
167 exception
= EXCP_ITLBMISS
;
169 exception
= EXCP_DTLBMISS
;
175 cs
->exception_index
= exception
;
176 cpu
->env
.eear
= address
;
177 cpu
->env
.lock_addr
= -1;
180 #ifndef CONFIG_USER_ONLY
181 int openrisc_cpu_handle_mmu_fault(CPUState
*cs
,
182 vaddr address
, int rw
, int mmu_idx
)
184 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
189 ret
= cpu_openrisc_get_phys_addr(cpu
, &physical
, &prot
,
192 if (ret
== TLBRET_MATCH
) {
193 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
194 physical
& TARGET_PAGE_MASK
, prot
,
195 mmu_idx
, TARGET_PAGE_SIZE
);
197 } else if (ret
< 0) {
198 cpu_openrisc_raise_mmu_exception(cpu
, address
, rw
, ret
);
205 int openrisc_cpu_handle_mmu_fault(CPUState
*cs
,
206 vaddr address
, int rw
, int mmu_idx
)
208 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
211 cpu_openrisc_raise_mmu_exception(cpu
, address
, rw
, ret
);
218 #ifndef CONFIG_USER_ONLY
219 hwaddr
openrisc_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
221 OpenRISCCPU
*cpu
= OPENRISC_CPU(cs
);
226 /* Check memory for any kind of address, since during debug the
227 gdb can ask for anything, check data tlb for address */
228 miss
= cpu_openrisc_get_phys_addr(cpu
, &phys_addr
, &prot
, addr
, 0);
230 /* Check instruction tlb */
232 miss
= cpu_openrisc_get_phys_addr(cpu
, &phys_addr
, &prot
, addr
,
236 /* Last, fall back to a plain address */
238 miss
= cpu_openrisc_get_phys_nommu(cpu
, &phys_addr
, &prot
, addr
, 0);
248 void cpu_openrisc_mmu_init(OpenRISCCPU
*cpu
)
250 cpu
->env
.tlb
= g_malloc0(sizeof(CPUOpenRISCTLBContext
));
252 cpu
->env
.tlb
->cpu_openrisc_map_address_code
= &cpu_openrisc_get_phys_nommu
;
253 cpu
->env
.tlb
->cpu_openrisc_map_address_data
= &cpu_openrisc_get_phys_nommu
;