2012-07-20 09:50:39 +02:00
|
|
|
/*
|
|
|
|
* OpenRISC MMU.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
|
|
|
|
* Zhizhou Zhang <etouzh@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:22 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-07-20 09:50:39 +02:00
|
|
|
#include "cpu.h"
|
2016-03-15 13:18:37 +01:00
|
|
|
#include "exec/exec-all.h"
|
2012-07-20 09:50:39 +02:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/gdbstub.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/host-utils.h"
|
2012-07-20 09:50:39 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
#include "hw/loader.h"
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 09:50:40 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
int cpu_openrisc_get_phys_nommu(OpenRISCCPU *cpu,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr *physical,
|
2012-07-20 09:50:40 +02:00
|
|
|
int *prot, target_ulong address, int rw)
|
|
|
|
{
|
|
|
|
*physical = address;
|
2013-10-22 02:12:39 +02:00
|
|
|
*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
|
2012-07-20 09:50:40 +02:00
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cpu_openrisc_get_phys_code(OpenRISCCPU *cpu,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr *physical,
|
2012-07-20 09:50:40 +02:00
|
|
|
int *prot, target_ulong address, int rw)
|
|
|
|
{
|
|
|
|
int vpn = address >> TARGET_PAGE_BITS;
|
|
|
|
int idx = vpn & ITLB_MASK;
|
|
|
|
int right = 0;
|
|
|
|
|
|
|
|
if ((cpu->env.tlb->itlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
|
|
|
|
return TLBRET_NOMATCH;
|
|
|
|
}
|
|
|
|
if (!(cpu->env.tlb->itlb[0][idx].mr & 1)) {
|
|
|
|
return TLBRET_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu->env.sr & SR_SM) { /* supervisor mode */
|
|
|
|
if (cpu->env.tlb->itlb[0][idx].tr & SXE) {
|
|
|
|
right |= PAGE_EXEC;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cpu->env.tlb->itlb[0][idx].tr & UXE) {
|
|
|
|
right |= PAGE_EXEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rw & 2) && ((right & PAGE_EXEC) == 0)) {
|
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*physical = (cpu->env.tlb->itlb[0][idx].tr & TARGET_PAGE_MASK) |
|
|
|
|
(address & (TARGET_PAGE_SIZE-1));
|
|
|
|
*prot = right;
|
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cpu_openrisc_get_phys_data(OpenRISCCPU *cpu,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr *physical,
|
2012-07-20 09:50:40 +02:00
|
|
|
int *prot, target_ulong address, int rw)
|
|
|
|
{
|
|
|
|
int vpn = address >> TARGET_PAGE_BITS;
|
|
|
|
int idx = vpn & DTLB_MASK;
|
|
|
|
int right = 0;
|
|
|
|
|
|
|
|
if ((cpu->env.tlb->dtlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
|
|
|
|
return TLBRET_NOMATCH;
|
|
|
|
}
|
|
|
|
if (!(cpu->env.tlb->dtlb[0][idx].mr & 1)) {
|
|
|
|
return TLBRET_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu->env.sr & SR_SM) { /* supervisor mode */
|
|
|
|
if (cpu->env.tlb->dtlb[0][idx].tr & SRE) {
|
|
|
|
right |= PAGE_READ;
|
|
|
|
}
|
|
|
|
if (cpu->env.tlb->dtlb[0][idx].tr & SWE) {
|
|
|
|
right |= PAGE_WRITE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cpu->env.tlb->dtlb[0][idx].tr & URE) {
|
|
|
|
right |= PAGE_READ;
|
|
|
|
}
|
|
|
|
if (cpu->env.tlb->dtlb[0][idx].tr & UWE) {
|
|
|
|
right |= PAGE_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 10:04:46 +02:00
|
|
|
if (!(rw & 1) && ((right & PAGE_READ) == 0)) {
|
2012-07-20 09:50:40 +02:00
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
if ((rw & 1) && ((right & PAGE_WRITE) == 0)) {
|
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*physical = (cpu->env.tlb->dtlb[0][idx].tr & TARGET_PAGE_MASK) |
|
|
|
|
(address & (TARGET_PAGE_SIZE-1));
|
|
|
|
*prot = right;
|
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cpu_openrisc_get_phys_addr(OpenRISCCPU *cpu,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr *physical,
|
2012-07-20 09:50:40 +02:00
|
|
|
int *prot, target_ulong address,
|
|
|
|
int rw)
|
|
|
|
{
|
|
|
|
int ret = TLBRET_MATCH;
|
|
|
|
|
|
|
|
if (rw == 2) { /* ITLB */
|
|
|
|
*physical = 0;
|
|
|
|
ret = cpu->env.tlb->cpu_openrisc_map_address_code(cpu, physical,
|
|
|
|
prot, address, rw);
|
|
|
|
} else { /* DTLB */
|
|
|
|
ret = cpu->env.tlb->cpu_openrisc_map_address_data(cpu, physical,
|
|
|
|
prot, address, rw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void cpu_openrisc_raise_mmu_exception(OpenRISCCPU *cpu,
|
|
|
|
target_ulong address,
|
|
|
|
int rw, int tlb_error)
|
|
|
|
{
|
2013-08-26 08:31:06 +02:00
|
|
|
CPUState *cs = CPU(cpu);
|
2012-07-20 09:50:40 +02:00
|
|
|
int exception = 0;
|
|
|
|
|
|
|
|
switch (tlb_error) {
|
|
|
|
default:
|
|
|
|
if (rw == 2) {
|
|
|
|
exception = EXCP_IPF;
|
|
|
|
} else {
|
|
|
|
exception = EXCP_DPF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
case TLBRET_BADADDR:
|
|
|
|
if (rw == 2) {
|
|
|
|
exception = EXCP_IPF;
|
|
|
|
} else {
|
|
|
|
exception = EXCP_DPF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TLBRET_INVALID:
|
|
|
|
case TLBRET_NOMATCH:
|
|
|
|
/* No TLB match for a mapped address */
|
|
|
|
if (rw == 2) {
|
|
|
|
exception = EXCP_ITLBMISS;
|
|
|
|
} else {
|
|
|
|
exception = EXCP_DTLBMISS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-26 08:31:06 +02:00
|
|
|
cs->exception_index = exception;
|
2012-07-20 09:50:40 +02:00
|
|
|
cpu->env.eear = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
2013-08-26 03:01:33 +02:00
|
|
|
int openrisc_cpu_handle_mmu_fault(CPUState *cs,
|
|
|
|
vaddr address, int rw, int mmu_idx)
|
2012-07-20 09:50:40 +02:00
|
|
|
{
|
2013-08-26 03:01:33 +02:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-07-20 09:50:40 +02:00
|
|
|
int ret = 0;
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr physical = 0;
|
2012-07-20 09:50:40 +02:00
|
|
|
int prot = 0;
|
|
|
|
|
|
|
|
ret = cpu_openrisc_get_phys_addr(cpu, &physical, &prot,
|
|
|
|
address, rw);
|
|
|
|
|
|
|
|
if (ret == TLBRET_MATCH) {
|
2013-09-03 13:59:37 +02:00
|
|
|
tlb_set_page(cs, address & TARGET_PAGE_MASK,
|
2013-10-22 02:12:39 +02:00
|
|
|
physical & TARGET_PAGE_MASK, prot,
|
2012-07-20 09:50:40 +02:00
|
|
|
mmu_idx, TARGET_PAGE_SIZE);
|
|
|
|
ret = 0;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
cpu_openrisc_raise_mmu_exception(cpu, address, rw, ret);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2013-08-26 03:01:33 +02:00
|
|
|
int openrisc_cpu_handle_mmu_fault(CPUState *cs,
|
|
|
|
vaddr address, int rw, int mmu_idx)
|
2012-07-20 09:50:40 +02:00
|
|
|
{
|
2013-08-26 03:01:33 +02:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-07-20 09:50:40 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
cpu_openrisc_raise_mmu_exception(cpu, address, rw, ret);
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 09:50:39 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2013-06-29 18:55:54 +02:00
|
|
|
hwaddr openrisc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
|
2012-07-20 09:50:39 +02:00
|
|
|
{
|
2013-06-29 18:55:54 +02:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr phys_addr;
|
2012-07-20 09:50:40 +02:00
|
|
|
int prot;
|
|
|
|
|
|
|
|
if (cpu_openrisc_get_phys_addr(cpu, &phys_addr, &prot, addr, 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return phys_addr;
|
2012-07-20 09:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_openrisc_mmu_init(OpenRISCCPU *cpu)
|
|
|
|
{
|
2012-07-20 09:50:40 +02:00
|
|
|
cpu->env.tlb = g_malloc0(sizeof(CPUOpenRISCTLBContext));
|
|
|
|
|
|
|
|
cpu->env.tlb->cpu_openrisc_map_address_code = &cpu_openrisc_get_phys_nommu;
|
|
|
|
cpu->env.tlb->cpu_openrisc_map_address_data = &cpu_openrisc_get_phys_nommu;
|
2012-07-20 09:50:39 +02:00
|
|
|
}
|
|
|
|
#endif
|