2017-01-19 01:01:41 +03:00
|
|
|
/*
|
|
|
|
* Altera Nios II MMU emulation for qemu.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Chris Wulff <crwulff@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.1 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/lgpl-2.1.html>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2019-04-17 22:17:58 +03:00
|
|
|
#include "qemu/qemu-print.h"
|
2017-01-19 01:01:41 +03:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "exec/exec-all.h"
|
|
|
|
#include "mmu.h"
|
2022-02-26 14:27:32 +03:00
|
|
|
#include "exec/helper-proto.h"
|
2022-02-26 10:06:20 +03:00
|
|
|
#include "trace/trace-target_nios2.h"
|
2017-01-19 01:01:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* rw - 0 = read, 1 = write, 2 = fetch. */
|
|
|
|
unsigned int mmu_translate(CPUNios2State *env,
|
|
|
|
Nios2MMULookup *lu,
|
|
|
|
target_ulong vaddr, int rw, int mmu_idx)
|
|
|
|
{
|
2019-03-23 04:44:44 +03:00
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2022-04-21 18:17:00 +03:00
|
|
|
int pid = FIELD_EX32(env->mmu.tlbmisc_wr, CR_TLBMISC, PID);
|
2017-01-19 01:01:41 +03:00
|
|
|
int vpn = vaddr >> 12;
|
2022-02-26 10:06:20 +03:00
|
|
|
int way, n_ways = cpu->tlb_num_ways;
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-02-26 10:06:20 +03:00
|
|
|
for (way = 0; way < n_ways; way++) {
|
|
|
|
uint32_t index = (way * n_ways) + (vpn & env->mmu.tlb_entry_mask);
|
|
|
|
Nios2TLBEntry *entry = &env->mmu.tlb[index];
|
2017-01-19 01:01:41 +03:00
|
|
|
|
|
|
|
if (((entry->tag >> 12) != vpn) ||
|
|
|
|
(((entry->tag & (1 << 11)) == 0) &&
|
|
|
|
((entry->tag & ((1 << cpu->pid_num_bits) - 1)) != pid))) {
|
2022-02-26 10:06:20 +03:00
|
|
|
trace_nios2_mmu_translate_miss(vaddr, pid, index, entry->tag);
|
2017-01-19 01:01:41 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-26 10:06:20 +03:00
|
|
|
|
2017-01-19 01:01:41 +03:00
|
|
|
lu->vaddr = vaddr & TARGET_PAGE_MASK;
|
2022-04-21 18:16:58 +03:00
|
|
|
lu->paddr = FIELD_EX32(entry->data, CR_TLBACC, PFN) << TARGET_PAGE_BITS;
|
2017-01-19 01:01:41 +03:00
|
|
|
lu->prot = ((entry->data & CR_TLBACC_R) ? PAGE_READ : 0) |
|
|
|
|
((entry->data & CR_TLBACC_W) ? PAGE_WRITE : 0) |
|
|
|
|
((entry->data & CR_TLBACC_X) ? PAGE_EXEC : 0);
|
|
|
|
|
2022-02-26 10:06:20 +03:00
|
|
|
trace_nios2_mmu_translate_hit(vaddr, pid, index, lu->paddr, lu->prot);
|
2017-01-19 01:01:41 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mmu_flush_pid(CPUNios2State *env, uint32_t pid)
|
|
|
|
{
|
2019-03-23 02:07:18 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2019-03-23 04:44:44 +03:00
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2017-01-19 01:01:41 +03:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
for (idx = 0; idx < cpu->tlb_num_entries; idx++) {
|
|
|
|
Nios2TLBEntry *entry = &env->mmu.tlb[idx];
|
|
|
|
|
|
|
|
if ((entry->tag & (1 << 10)) && (!(entry->tag & (1 << 11))) &&
|
|
|
|
((entry->tag & ((1 << cpu->pid_num_bits) - 1)) == pid)) {
|
|
|
|
uint32_t vaddr = entry->tag & TARGET_PAGE_MASK;
|
|
|
|
|
2022-02-26 10:06:20 +03:00
|
|
|
trace_nios2_mmu_flush_pid_hit(pid, idx, vaddr);
|
2017-01-19 01:01:41 +03:00
|
|
|
tlb_flush_page(cs, vaddr);
|
2022-02-26 10:06:20 +03:00
|
|
|
} else {
|
|
|
|
trace_nios2_mmu_flush_pid_miss(pid, idx, entry->tag);
|
2017-01-19 01:01:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
void helper_mmu_write_tlbacc(CPUNios2State *env, uint32_t v)
|
2017-01-19 01:01:41 +03:00
|
|
|
{
|
2019-03-23 02:07:18 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2019-03-23 04:44:44 +03:00
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-04-21 18:16:58 +03:00
|
|
|
trace_nios2_mmu_write_tlbacc(FIELD_EX32(v, CR_TLBACC, IG),
|
2022-02-26 14:27:32 +03:00
|
|
|
(v & CR_TLBACC_C) ? 'C' : '.',
|
|
|
|
(v & CR_TLBACC_R) ? 'R' : '.',
|
|
|
|
(v & CR_TLBACC_W) ? 'W' : '.',
|
|
|
|
(v & CR_TLBACC_X) ? 'X' : '.',
|
|
|
|
(v & CR_TLBACC_G) ? 'G' : '.',
|
2022-04-21 18:16:58 +03:00
|
|
|
FIELD_EX32(v, CR_TLBACC, PFN));
|
2022-02-26 14:27:32 +03:00
|
|
|
|
|
|
|
/* if tlbmisc.WE == 1 then trigger a TLB write on writes to TLBACC */
|
2022-04-21 18:16:59 +03:00
|
|
|
if (env->ctrl[CR_TLBMISC] & CR_TLBMISC_WE) {
|
2022-04-21 18:17:00 +03:00
|
|
|
int way = FIELD_EX32(env->ctrl[CR_TLBMISC], CR_TLBMISC, WAY);
|
2022-04-21 18:16:57 +03:00
|
|
|
int vpn = FIELD_EX32(env->mmu.pteaddr_wr, CR_PTEADDR, VPN);
|
2022-04-21 18:17:00 +03:00
|
|
|
int pid = FIELD_EX32(env->mmu.tlbmisc_wr, CR_TLBMISC, PID);
|
2022-04-21 18:16:58 +03:00
|
|
|
int g = FIELD_EX32(v, CR_TLBACC, G);
|
|
|
|
int valid = FIELD_EX32(vpn, CR_TLBACC, PFN) < 0xC0000;
|
2022-02-26 14:27:32 +03:00
|
|
|
Nios2TLBEntry *entry =
|
|
|
|
&env->mmu.tlb[(way * cpu->tlb_num_ways) +
|
|
|
|
(vpn & env->mmu.tlb_entry_mask)];
|
|
|
|
uint32_t newTag = (vpn << 12) | (g << 11) | (valid << 10) | pid;
|
|
|
|
uint32_t newData = v & (CR_TLBACC_C | CR_TLBACC_R | CR_TLBACC_W |
|
2022-04-21 18:16:58 +03:00
|
|
|
CR_TLBACC_X | R_CR_TLBACC_PFN_MASK);
|
2022-02-26 14:27:32 +03:00
|
|
|
|
|
|
|
if ((entry->tag != newTag) || (entry->data != newData)) {
|
|
|
|
if (entry->tag & (1 << 10)) {
|
|
|
|
/* Flush existing entry */
|
|
|
|
tlb_flush_page(cs, entry->tag & TARGET_PAGE_MASK);
|
2017-01-19 01:01:41 +03:00
|
|
|
}
|
2022-02-26 14:27:32 +03:00
|
|
|
entry->tag = newTag;
|
|
|
|
entry->data = newData;
|
2017-01-19 01:01:41 +03:00
|
|
|
}
|
2022-02-26 14:27:32 +03:00
|
|
|
/* Auto-increment tlbmisc.WAY */
|
2022-04-21 18:17:00 +03:00
|
|
|
env->ctrl[CR_TLBMISC] = FIELD_DP32(env->ctrl[CR_TLBMISC],
|
|
|
|
CR_TLBMISC, WAY,
|
|
|
|
(way + 1) & (cpu->tlb_num_ways - 1));
|
2022-02-26 14:27:32 +03:00
|
|
|
}
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
/* Writes to TLBACC don't change the read-back value */
|
|
|
|
env->mmu.tlbacc_wr = v;
|
|
|
|
}
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
void helper_mmu_write_tlbmisc(CPUNios2State *env, uint32_t v)
|
|
|
|
{
|
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2022-04-21 18:17:00 +03:00
|
|
|
uint32_t new_pid = FIELD_EX32(v, CR_TLBMISC, PID);
|
|
|
|
uint32_t old_pid = FIELD_EX32(env->mmu.tlbmisc_wr, CR_TLBMISC, PID);
|
|
|
|
uint32_t way = FIELD_EX32(v, CR_TLBMISC, WAY);
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-04-21 18:17:00 +03:00
|
|
|
trace_nios2_mmu_write_tlbmisc(way,
|
2022-02-26 14:27:32 +03:00
|
|
|
(v & CR_TLBMISC_RD) ? 'R' : '.',
|
2022-04-21 18:16:59 +03:00
|
|
|
(v & CR_TLBMISC_WE) ? 'W' : '.',
|
2022-02-26 14:27:32 +03:00
|
|
|
(v & CR_TLBMISC_DBL) ? '2' : '.',
|
|
|
|
(v & CR_TLBMISC_BAD) ? 'B' : '.',
|
|
|
|
(v & CR_TLBMISC_PERM) ? 'P' : '.',
|
|
|
|
(v & CR_TLBMISC_D) ? 'D' : '.',
|
2022-04-21 18:17:00 +03:00
|
|
|
new_pid);
|
2022-02-26 14:27:32 +03:00
|
|
|
|
2022-04-21 18:17:00 +03:00
|
|
|
if (new_pid != old_pid) {
|
|
|
|
mmu_flush_pid(env, old_pid);
|
2022-02-26 14:27:32 +03:00
|
|
|
}
|
2022-04-21 18:17:00 +03:00
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
/* if tlbmisc.RD == 1 then trigger a TLB read on writes to TLBMISC */
|
|
|
|
if (v & CR_TLBMISC_RD) {
|
2022-04-21 18:16:57 +03:00
|
|
|
int vpn = FIELD_EX32(env->mmu.pteaddr_wr, CR_PTEADDR, VPN);
|
2022-02-26 14:27:32 +03:00
|
|
|
Nios2TLBEntry *entry =
|
|
|
|
&env->mmu.tlb[(way * cpu->tlb_num_ways) +
|
|
|
|
(vpn & env->mmu.tlb_entry_mask)];
|
|
|
|
|
2022-04-21 18:16:58 +03:00
|
|
|
env->ctrl[CR_TLBACC] &= R_CR_TLBACC_IG_MASK;
|
2022-04-21 18:16:53 +03:00
|
|
|
env->ctrl[CR_TLBACC] |= entry->data;
|
|
|
|
env->ctrl[CR_TLBACC] |= (entry->tag & (1 << 11)) ? CR_TLBACC_G : 0;
|
2022-04-21 18:17:00 +03:00
|
|
|
env->ctrl[CR_TLBMISC] = FIELD_DP32(v, CR_TLBMISC, PID,
|
|
|
|
entry->tag &
|
|
|
|
((1 << cpu->pid_num_bits) - 1));
|
2022-04-21 18:16:57 +03:00
|
|
|
env->ctrl[CR_PTEADDR] = FIELD_DP32(env->ctrl[CR_PTEADDR],
|
|
|
|
CR_PTEADDR, VPN,
|
|
|
|
entry->tag >> TARGET_PAGE_BITS);
|
2022-02-26 14:27:32 +03:00
|
|
|
} else {
|
2022-04-21 18:16:53 +03:00
|
|
|
env->ctrl[CR_TLBMISC] = v;
|
2022-02-26 14:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
env->mmu.tlbmisc_wr = v;
|
|
|
|
}
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
void helper_mmu_write_pteaddr(CPUNios2State *env, uint32_t v)
|
|
|
|
{
|
2022-04-21 18:16:57 +03:00
|
|
|
trace_nios2_mmu_write_pteaddr(FIELD_EX32(v, CR_PTEADDR, PTBASE),
|
|
|
|
FIELD_EX32(v, CR_PTEADDR, VPN));
|
2017-01-19 01:01:41 +03:00
|
|
|
|
2022-02-26 14:27:32 +03:00
|
|
|
/* Writes to PTEADDR don't change the read-back VPN value */
|
2022-04-21 18:16:57 +03:00
|
|
|
env->ctrl[CR_PTEADDR] = ((v & ~R_CR_PTEADDR_VPN_MASK) |
|
|
|
|
(env->ctrl[CR_PTEADDR] & R_CR_PTEADDR_VPN_MASK));
|
2022-02-26 14:27:32 +03:00
|
|
|
env->mmu.pteaddr_wr = v;
|
2017-01-19 01:01:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_init(CPUNios2State *env)
|
|
|
|
{
|
2019-03-23 04:44:44 +03:00
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2017-01-19 01:01:41 +03:00
|
|
|
Nios2MMU *mmu = &env->mmu;
|
|
|
|
|
|
|
|
mmu->tlb_entry_mask = (cpu->tlb_num_entries / cpu->tlb_num_ways) - 1;
|
|
|
|
mmu->tlb = g_new0(Nios2TLBEntry, cpu->tlb_num_entries);
|
|
|
|
}
|
|
|
|
|
2019-04-17 22:17:58 +03:00
|
|
|
void dump_mmu(CPUNios2State *env)
|
2017-01-19 01:01:41 +03:00
|
|
|
{
|
2019-03-23 04:44:44 +03:00
|
|
|
Nios2CPU *cpu = env_archcpu(env);
|
2017-01-19 01:01:41 +03:00
|
|
|
int i;
|
|
|
|
|
2019-04-17 22:17:58 +03:00
|
|
|
qemu_printf("MMU: ways %d, entries %d, pid bits %d\n",
|
2017-01-19 01:01:41 +03:00
|
|
|
cpu->tlb_num_ways, cpu->tlb_num_entries,
|
|
|
|
cpu->pid_num_bits);
|
|
|
|
|
|
|
|
for (i = 0; i < cpu->tlb_num_entries; i++) {
|
|
|
|
Nios2TLBEntry *entry = &env->mmu.tlb[i];
|
2019-04-17 22:17:58 +03:00
|
|
|
qemu_printf("TLB[%d] = %08X %08X %c VPN %05X "
|
2017-01-19 01:01:41 +03:00
|
|
|
"PID %02X %c PFN %05X %c%c%c%c\n",
|
|
|
|
i, entry->tag, entry->data,
|
|
|
|
(entry->tag & (1 << 10)) ? 'V' : '-',
|
|
|
|
entry->tag >> 12,
|
|
|
|
entry->tag & ((1 << cpu->pid_num_bits) - 1),
|
|
|
|
(entry->tag & (1 << 11)) ? 'G' : '-',
|
2022-04-21 18:16:58 +03:00
|
|
|
FIELD_EX32(entry->data, CR_TLBACC, PFN),
|
2017-01-19 01:01:41 +03:00
|
|
|
(entry->data & CR_TLBACC_C) ? 'C' : '-',
|
|
|
|
(entry->data & CR_TLBACC_R) ? 'R' : '-',
|
|
|
|
(entry->data & CR_TLBACC_W) ? 'W' : '-',
|
|
|
|
(entry->data & CR_TLBACC_X) ? 'X' : '-');
|
|
|
|
}
|
|
|
|
}
|