2012-07-20 11:50:39 +04: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 21:17:22 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-07-20 11:50:39 +04:00
|
|
|
#include "cpu.h"
|
2016-03-15 15:18:37 +03:00
|
|
|
#include "exec/exec-all.h"
|
2012-07-20 11:50:39 +04:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "exec/gdbstub.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/host-utils.h"
|
2012-07-20 11:50:39 +04:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
#include "hw/loader.h"
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 11:50:40 +04:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2018-05-23 09:14:02 +03:00
|
|
|
static inline void get_phys_nommu(hwaddr *phys_addr, int *prot,
|
|
|
|
target_ulong address)
|
2012-07-20 11:50:40 +04:00
|
|
|
{
|
2018-05-23 09:14:02 +03:00
|
|
|
*phys_addr = address;
|
2013-10-22 04:12:39 +04:00
|
|
|
*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
|
2012-07-20 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
static int get_phys_mmu(OpenRISCCPU *cpu, hwaddr *phys_addr, int *prot,
|
|
|
|
target_ulong addr, int need, bool super)
|
2012-07-20 11:50:40 +04:00
|
|
|
{
|
2018-05-23 09:14:02 +03:00
|
|
|
int idx = (addr >> TARGET_PAGE_BITS) & TLB_MASK;
|
|
|
|
uint32_t imr = cpu->env.tlb.itlb[idx].mr;
|
|
|
|
uint32_t itr = cpu->env.tlb.itlb[idx].tr;
|
|
|
|
uint32_t dmr = cpu->env.tlb.dtlb[idx].mr;
|
|
|
|
uint32_t dtr = cpu->env.tlb.dtlb[idx].tr;
|
|
|
|
int right, match, valid;
|
|
|
|
|
|
|
|
/* If the ITLB and DTLB indexes map to the same page, we want to
|
|
|
|
load all permissions all at once. If the destination pages do
|
|
|
|
not match, zap the one we don't need. */
|
|
|
|
if (unlikely((itr ^ dtr) & TARGET_PAGE_MASK)) {
|
|
|
|
if (need & PAGE_EXEC) {
|
|
|
|
dmr = dtr = 0;
|
|
|
|
} else {
|
|
|
|
imr = itr = 0;
|
2012-07-20 11:50:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Check if either of the entries matches the source address. */
|
|
|
|
match = (imr ^ addr) & TARGET_PAGE_MASK ? 0 : PAGE_EXEC;
|
|
|
|
match |= (dmr ^ addr) & TARGET_PAGE_MASK ? 0 : PAGE_READ | PAGE_WRITE;
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Check if either of the entries is valid. */
|
|
|
|
valid = imr & 1 ? PAGE_EXEC : 0;
|
|
|
|
valid |= dmr & 1 ? PAGE_READ | PAGE_WRITE : 0;
|
|
|
|
valid &= match;
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Collect the permissions from the entries. */
|
|
|
|
right = itr & (super ? SXE : UXE) ? PAGE_EXEC : 0;
|
|
|
|
right |= dtr & (super ? SRE : URE) ? PAGE_READ : 0;
|
|
|
|
right |= dtr & (super ? SWE : UWE) ? PAGE_WRITE : 0;
|
|
|
|
right &= valid;
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Note that above we validated that itr and dtr match on page.
|
|
|
|
So oring them together changes nothing without having to
|
|
|
|
check which one we needed. We also want to store to these
|
|
|
|
variables even on failure, as it avoids compiler warnings. */
|
|
|
|
*phys_addr = ((itr | dtr) & TARGET_PAGE_MASK) | (addr & ~TARGET_PAGE_MASK);
|
2012-07-20 11:50:40 +04:00
|
|
|
*prot = right;
|
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
qemu_log_mask(CPU_LOG_MMU,
|
|
|
|
"MMU lookup: need %d match %d valid %d right %d -> %s\n",
|
|
|
|
need, match, valid, right, (need & right) ? "OK" : "FAIL");
|
2018-05-23 02:51:19 +03:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Check the collective permissions are present. */
|
|
|
|
if (likely(need & right)) {
|
|
|
|
return 0; /* success! */
|
|
|
|
}
|
2018-05-23 02:51:19 +03:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
/* Determine what kind of failure we have. */
|
|
|
|
if (need & valid) {
|
|
|
|
return need & PAGE_EXEC ? EXCP_IPF : EXCP_DPF;
|
2018-05-23 02:51:19 +03:00
|
|
|
} else {
|
2018-05-23 09:14:02 +03:00
|
|
|
return need & PAGE_EXEC ? EXCP_ITLBMISS : EXCP_DTLBMISS;
|
2012-07-20 11:50:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
static void raise_mmu_exception(OpenRISCCPU *cpu, target_ulong address,
|
|
|
|
int exception)
|
2012-07-20 11:50:40 +04:00
|
|
|
{
|
2013-08-26 10:31:06 +04:00
|
|
|
CPUState *cs = CPU(cpu);
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = exception;
|
2012-07-20 11:50:40 +04:00
|
|
|
cpu->env.eear = address;
|
2015-02-19 09:19:18 +03:00
|
|
|
cpu->env.lock_addr = -1;
|
2012-07-20 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:38:40 +03:00
|
|
|
int openrisc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size,
|
|
|
|
int rw, int mmu_idx)
|
2012-07-20 11:50:40 +04:00
|
|
|
{
|
2018-05-23 06:43:25 +03:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
2013-08-26 05:01:33 +04:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2018-05-23 09:14:02 +03:00
|
|
|
raise_mmu_exception(cpu, address, EXCP_DPF);
|
2018-05-23 06:43:25 +03:00
|
|
|
return 1;
|
2012-07-20 11:50:40 +04:00
|
|
|
#else
|
2018-05-23 06:43:25 +03:00
|
|
|
g_assert_not_reached();
|
2012-07-20 11:50:40 +04:00
|
|
|
#endif
|
2018-05-23 06:43:25 +03:00
|
|
|
}
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2012-07-20 11:50:39 +04:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2013-06-29 20:55:54 +04:00
|
|
|
hwaddr openrisc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
|
2012-07-20 11:50:39 +04:00
|
|
|
{
|
2013-06-29 20:55:54 +04:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2018-05-23 09:14:02 +03:00
|
|
|
int prot, excp, sr = cpu->env.sr;
|
2012-10-23 14:30:10 +04:00
|
|
|
hwaddr phys_addr;
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
switch (sr & (SR_DME | SR_IME)) {
|
|
|
|
case SR_DME | SR_IME:
|
|
|
|
/* The mmu is definitely enabled. */
|
|
|
|
excp = get_phys_mmu(cpu, &phys_addr, &prot, addr,
|
|
|
|
PAGE_EXEC | PAGE_READ | PAGE_WRITE,
|
|
|
|
(sr & SR_SM) != 0);
|
|
|
|
return excp ? -1 : phys_addr;
|
2017-03-13 17:53:29 +03:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
default:
|
|
|
|
/* The mmu is partially enabled, and we don't really have
|
|
|
|
a "real" access type. Begin by trying the mmu, but if
|
|
|
|
that fails try again without. */
|
|
|
|
excp = get_phys_mmu(cpu, &phys_addr, &prot, addr,
|
|
|
|
PAGE_EXEC | PAGE_READ | PAGE_WRITE,
|
|
|
|
(sr & SR_SM) != 0);
|
|
|
|
if (!excp) {
|
|
|
|
return phys_addr;
|
|
|
|
}
|
|
|
|
/* fallthru */
|
2012-07-20 11:50:40 +04:00
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
case 0:
|
|
|
|
/* The mmu is definitely disabled; lookups never fail. */
|
|
|
|
get_phys_nommu(&phys_addr, &prot, addr);
|
2017-03-13 17:53:29 +03:00
|
|
|
return phys_addr;
|
|
|
|
}
|
2012-07-20 11:50:39 +04:00
|
|
|
}
|
2018-05-23 04:21:21 +03:00
|
|
|
|
|
|
|
void tlb_fill(CPUState *cs, target_ulong addr, int size,
|
|
|
|
MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
|
|
|
|
{
|
2018-05-23 05:51:00 +03:00
|
|
|
OpenRISCCPU *cpu = OPENRISC_CPU(cs);
|
2018-05-23 09:14:02 +03:00
|
|
|
int prot, excp;
|
|
|
|
hwaddr phys_addr;
|
2018-05-23 05:51:00 +03:00
|
|
|
|
|
|
|
if (mmu_idx == MMU_NOMMU_IDX) {
|
2018-05-23 09:14:02 +03:00
|
|
|
/* The mmu is disabled; lookups never fail. */
|
|
|
|
get_phys_nommu(&phys_addr, &prot, addr);
|
|
|
|
excp = 0;
|
2018-05-23 05:51:00 +03:00
|
|
|
} else {
|
|
|
|
bool super = mmu_idx == MMU_SUPERVISOR_IDX;
|
2018-05-23 09:14:02 +03:00
|
|
|
int need = (access_type == MMU_INST_FETCH ? PAGE_EXEC
|
|
|
|
: access_type == MMU_DATA_STORE ? PAGE_WRITE
|
|
|
|
: PAGE_READ);
|
|
|
|
excp = get_phys_mmu(cpu, &phys_addr, &prot, addr, need, super);
|
2018-05-23 05:51:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 09:14:02 +03:00
|
|
|
if (unlikely(excp)) {
|
|
|
|
raise_mmu_exception(cpu, addr, excp);
|
2018-05-23 04:21:21 +03:00
|
|
|
cpu_loop_exit_restore(cs, retaddr);
|
|
|
|
}
|
2018-05-23 09:14:02 +03:00
|
|
|
|
|
|
|
tlb_set_page(cs, addr & TARGET_PAGE_MASK,
|
|
|
|
phys_addr & TARGET_PAGE_MASK, prot,
|
|
|
|
mmu_idx, TARGET_PAGE_SIZE);
|
2018-05-23 04:21:21 +03:00
|
|
|
}
|
2012-07-20 11:50:39 +04:00
|
|
|
#endif
|