2012-07-20 15:50:39 +08: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-27 02:17:22 +08:00
|
|
|
#include "qemu/osdep.h"
|
2012-07-20 15:50:39 +08:00
|
|
|
#include "cpu.h"
|
2016-03-15 20:18:37 +08:00
|
|
|
#include "exec/exec-all.h"
|
2012-07-20 15:50:39 +08:00
|
|
|
#include "qemu-common.h"
|
2012-12-18 01:19:49 +08:00
|
|
|
#include "exec/gdbstub.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/host-utils.h"
|
2012-07-20 15:50:39 +08:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
#include "hw/loader.h"
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 15:50:40 +08:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2018-05-23 07:51:19 +08:00
|
|
|
static inline int get_phys_nommu(hwaddr *physical, int *prot,
|
|
|
|
target_ulong address)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
|
|
|
*physical = address;
|
2013-10-22 08:12:39 +08:00
|
|
|
*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
2018-05-23 07:51:19 +08:00
|
|
|
static int get_phys_code(OpenRISCCPU *cpu, hwaddr *physical, int *prot,
|
|
|
|
target_ulong address, int rw, bool supervisor)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
|
|
|
int vpn = address >> TARGET_PAGE_BITS;
|
2018-05-23 11:18:20 +08:00
|
|
|
int idx = vpn & TLB_MASK;
|
2012-07-20 15:50:40 +08:00
|
|
|
int right = 0;
|
2018-05-23 10:36:27 +08:00
|
|
|
uint32_t mr = cpu->env.tlb.itlb[idx].mr;
|
|
|
|
uint32_t tr = cpu->env.tlb.itlb[idx].tr;
|
2012-07-20 15:50:40 +08:00
|
|
|
|
2018-05-23 10:36:27 +08:00
|
|
|
if ((mr >> TARGET_PAGE_BITS) != vpn) {
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_NOMATCH;
|
|
|
|
}
|
2018-05-23 10:36:27 +08:00
|
|
|
if (!(mr & 1)) {
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_INVALID;
|
|
|
|
}
|
2018-05-23 07:51:19 +08:00
|
|
|
if (supervisor) {
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & SXE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_EXEC;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & UXE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_EXEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((rw & 2) && ((right & PAGE_EXEC) == 0)) {
|
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:36:27 +08:00
|
|
|
*physical = (tr & TARGET_PAGE_MASK) | (address & ~TARGET_PAGE_MASK);
|
2012-07-20 15:50:40 +08:00
|
|
|
*prot = right;
|
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
2018-05-23 07:51:19 +08:00
|
|
|
static int get_phys_data(OpenRISCCPU *cpu, hwaddr *physical, int *prot,
|
|
|
|
target_ulong address, int rw, bool supervisor)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
|
|
|
int vpn = address >> TARGET_PAGE_BITS;
|
2018-05-23 11:18:20 +08:00
|
|
|
int idx = vpn & TLB_MASK;
|
2012-07-20 15:50:40 +08:00
|
|
|
int right = 0;
|
2018-05-23 10:36:27 +08:00
|
|
|
uint32_t mr = cpu->env.tlb.dtlb[idx].mr;
|
|
|
|
uint32_t tr = cpu->env.tlb.dtlb[idx].tr;
|
2012-07-20 15:50:40 +08:00
|
|
|
|
2018-05-23 10:36:27 +08:00
|
|
|
if ((mr >> TARGET_PAGE_BITS) != vpn) {
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_NOMATCH;
|
|
|
|
}
|
2018-05-23 10:36:27 +08:00
|
|
|
if (!(mr & 1)) {
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_INVALID;
|
|
|
|
}
|
2018-05-23 07:51:19 +08:00
|
|
|
if (supervisor) {
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & SRE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_READ;
|
|
|
|
}
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & SWE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_WRITE;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & URE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_READ;
|
|
|
|
}
|
2018-05-23 10:36:27 +08:00
|
|
|
if (tr & UWE) {
|
2012-07-20 15:50:40 +08:00
|
|
|
right |= PAGE_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 16:04:46 +08:00
|
|
|
if (!(rw & 1) && ((right & PAGE_READ) == 0)) {
|
2012-07-20 15:50:40 +08:00
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
if ((rw & 1) && ((right & PAGE_WRITE) == 0)) {
|
|
|
|
return TLBRET_BADADDR;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:36:27 +08:00
|
|
|
*physical = (tr & TARGET_PAGE_MASK) | (address & ~TARGET_PAGE_MASK);
|
2012-07-20 15:50:40 +08:00
|
|
|
*prot = right;
|
|
|
|
return TLBRET_MATCH;
|
|
|
|
}
|
|
|
|
|
2018-05-23 07:51:19 +08:00
|
|
|
static int get_phys_addr(OpenRISCCPU *cpu, hwaddr *physical,
|
|
|
|
int *prot, target_ulong address, int rw)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
2018-05-23 07:51:19 +08:00
|
|
|
bool supervisor = (cpu->env.sr & SR_SM) != 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Assume nommu results for a moment. */
|
|
|
|
ret = get_phys_nommu(physical, prot, address);
|
|
|
|
|
|
|
|
/* Overwrite with TLB lookup if enabled. */
|
|
|
|
if (rw == MMU_INST_FETCH) {
|
|
|
|
if (cpu->env.sr & SR_IME) {
|
|
|
|
ret = get_phys_code(cpu, physical, prot, address, rw, supervisor);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cpu->env.sr & SR_DME) {
|
|
|
|
ret = get_phys_data(cpu, physical, prot, address, rw, supervisor);
|
|
|
|
}
|
2012-07-20 15:50:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void cpu_openrisc_raise_mmu_exception(OpenRISCCPU *cpu,
|
|
|
|
target_ulong address,
|
|
|
|
int rw, int tlb_error)
|
|
|
|
{
|
2013-08-26 14:31:06 +08:00
|
|
|
CPUState *cs = CPU(cpu);
|
2012-07-20 15:50:40 +08: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 14:31:06 +08:00
|
|
|
cs->exception_index = exception;
|
2012-07-20 15:50:40 +08:00
|
|
|
cpu->env.eear = address;
|
2015-02-19 14:19:18 +08:00
|
|
|
cpu->env.lock_addr = -1;
|
2012-07-20 15:50:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
2018-01-19 03:38:40 +08:00
|
|
|
int openrisc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size,
|
|
|
|
int rw, int mmu_idx)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
2013-08-26 09:01:33 +08:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-07-20 15:50:40 +08:00
|
|
|
int ret = 0;
|
2012-10-23 18:30:10 +08:00
|
|
|
hwaddr physical = 0;
|
2012-07-20 15:50:40 +08:00
|
|
|
int prot = 0;
|
|
|
|
|
2018-05-23 07:51:19 +08:00
|
|
|
ret = get_phys_addr(cpu, &physical, &prot, address, rw);
|
2012-07-20 15:50:40 +08:00
|
|
|
|
|
|
|
if (ret == TLBRET_MATCH) {
|
2013-09-03 19:59:37 +08:00
|
|
|
tlb_set_page(cs, address & TARGET_PAGE_MASK,
|
2013-10-22 08:12:39 +08:00
|
|
|
physical & TARGET_PAGE_MASK, prot,
|
2012-07-20 15:50:40 +08: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
|
2018-01-19 03:38:40 +08:00
|
|
|
int openrisc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size,
|
|
|
|
int rw, int mmu_idx)
|
2012-07-20 15:50:40 +08:00
|
|
|
{
|
2013-08-26 09:01:33 +08:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-07-20 15:50:40 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
cpu_openrisc_raise_mmu_exception(cpu, address, rw, ret);
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 15:50:39 +08:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2013-06-30 00:55:54 +08:00
|
|
|
hwaddr openrisc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
|
2012-07-20 15:50:39 +08:00
|
|
|
{
|
2013-06-30 00:55:54 +08:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2012-10-23 18:30:10 +08:00
|
|
|
hwaddr phys_addr;
|
2012-07-20 15:50:40 +08:00
|
|
|
int prot;
|
2017-03-13 22:53:29 +08:00
|
|
|
int miss;
|
2012-07-20 15:50:40 +08:00
|
|
|
|
2017-03-13 22:53:29 +08:00
|
|
|
/* Check memory for any kind of address, since during debug the
|
|
|
|
gdb can ask for anything, check data tlb for address */
|
2018-05-23 07:51:19 +08:00
|
|
|
miss = get_phys_addr(cpu, &phys_addr, &prot, addr, 0);
|
2017-03-13 22:53:29 +08:00
|
|
|
|
|
|
|
/* Check instruction tlb */
|
|
|
|
if (miss) {
|
2018-05-23 07:51:19 +08:00
|
|
|
miss = get_phys_addr(cpu, &phys_addr, &prot, addr, MMU_INST_FETCH);
|
2017-03-13 22:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Last, fall back to a plain address */
|
|
|
|
if (miss) {
|
2018-05-23 07:51:19 +08:00
|
|
|
miss = get_phys_nommu(&phys_addr, &prot, addr);
|
2012-07-20 15:50:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:53:29 +08:00
|
|
|
if (miss) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return phys_addr;
|
|
|
|
}
|
2012-07-20 15:50:39 +08:00
|
|
|
}
|
2018-05-23 09:21:21 +08:00
|
|
|
|
|
|
|
void tlb_fill(CPUState *cs, target_ulong addr, int size,
|
|
|
|
MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
|
|
|
|
{
|
2018-05-23 10:51:00 +08:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
|
|
|
int ret, prot = 0;
|
|
|
|
hwaddr physical = 0;
|
|
|
|
|
|
|
|
if (mmu_idx == MMU_NOMMU_IDX) {
|
|
|
|
ret = get_phys_nommu(&physical, &prot, addr);
|
|
|
|
} else {
|
|
|
|
bool super = mmu_idx == MMU_SUPERVISOR_IDX;
|
|
|
|
if (access_type == MMU_INST_FETCH) {
|
|
|
|
ret = get_phys_code(cpu, &physical, &prot, addr, 2, super);
|
|
|
|
} else {
|
|
|
|
ret = get_phys_data(cpu, &physical, &prot, addr,
|
|
|
|
access_type == MMU_DATA_STORE, super);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == TLBRET_MATCH) {
|
|
|
|
tlb_set_page(cs, addr & TARGET_PAGE_MASK,
|
|
|
|
physical & TARGET_PAGE_MASK, prot,
|
|
|
|
mmu_idx, TARGET_PAGE_SIZE);
|
|
|
|
} else if (ret < 0) {
|
|
|
|
int rw;
|
|
|
|
if (access_type == MMU_INST_FETCH) {
|
|
|
|
rw = 2;
|
|
|
|
} else if (access_type == MMU_DATA_STORE) {
|
|
|
|
rw = 1;
|
|
|
|
} else {
|
|
|
|
rw = 0;
|
|
|
|
}
|
|
|
|
cpu_openrisc_raise_mmu_exception(cpu, addr, rw, ret);
|
2018-05-23 09:21:21 +08:00
|
|
|
/* Raise Exception. */
|
|
|
|
cpu_loop_exit_restore(cs, retaddr);
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 15:50:39 +08:00
|
|
|
#endif
|