2015-02-12 20:09:18 +03:00
|
|
|
/*
|
|
|
|
* S390x MMU related functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Alexander Graf
|
|
|
|
* Copyright (c) 2015 Thomas Huth, IBM Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:00 +03:00
|
|
|
#include "qemu/osdep.h"
|
2015-02-12 20:09:31 +03:00
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "exec/address-spaces.h"
|
2015-02-12 20:09:18 +03:00
|
|
|
#include "cpu.h"
|
2021-07-07 13:53:16 +03:00
|
|
|
#include "s390x-internal.h"
|
2021-07-07 13:53:23 +03:00
|
|
|
#include "kvm/kvm_s390x.h"
|
2015-07-03 17:18:24 +03:00
|
|
|
#include "sysemu/kvm.h"
|
2019-05-23 17:35:05 +03:00
|
|
|
#include "sysemu/tcg.h"
|
2017-11-30 19:27:35 +03:00
|
|
|
#include "exec/exec-all.h"
|
2023-12-06 22:27:32 +03:00
|
|
|
#include "exec/page-protection.h"
|
2019-08-12 08:23:48 +03:00
|
|
|
#include "hw/hw.h"
|
2015-06-26 21:01:00 +03:00
|
|
|
#include "hw/s390x/storage-keys.h"
|
2020-10-28 13:17:07 +03:00
|
|
|
#include "hw/boards.h"
|
2015-02-12 20:09:18 +03:00
|
|
|
|
2015-02-12 20:09:23 +03:00
|
|
|
/* Fetch/store bits in the translation exception code: */
|
|
|
|
#define FS_READ 0x800
|
|
|
|
#define FS_WRITE 0x400
|
2015-02-12 20:09:18 +03:00
|
|
|
|
2015-02-12 20:09:30 +03:00
|
|
|
static void trigger_access_exception(CPUS390XState *env, uint32_t type,
|
2019-10-01 20:16:12 +03:00
|
|
|
uint64_t tec)
|
2015-02-12 20:09:30 +03:00
|
|
|
{
|
2019-03-23 05:21:48 +03:00
|
|
|
S390CPU *cpu = env_archcpu(env);
|
2015-02-12 20:09:30 +03:00
|
|
|
|
|
|
|
if (kvm_enabled()) {
|
|
|
|
kvm_s390_access_exception(cpu, type, tec);
|
|
|
|
} else {
|
2019-03-23 05:21:48 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2017-11-30 19:27:36 +03:00
|
|
|
if (type != PGM_ADDRESSING) {
|
|
|
|
stq_phys(cs->as, env->psa + offsetof(LowCore, trans_exc_code), tec);
|
|
|
|
}
|
2019-10-01 20:16:13 +03:00
|
|
|
trigger_pgm_exception(env, type);
|
2015-02-12 20:09:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 23:23:58 +03:00
|
|
|
/* check whether the address would be proteted by Low-Address Protection */
|
|
|
|
static bool is_low_address(uint64_t addr)
|
|
|
|
{
|
|
|
|
return addr <= 511 || (addr >= 4096 && addr <= 4607);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check whether Low-Address Protection is enabled for mmu_translate() */
|
|
|
|
static bool lowprot_enabled(const CPUS390XState *env, uint64_t asc)
|
|
|
|
{
|
|
|
|
if (!(env->cregs[0] & CR0_LOWPROT)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(env->psw.mask & PSW_MASK_DAT)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the private-space control bit */
|
|
|
|
switch (asc) {
|
|
|
|
case PSW_ASC_PRIMARY:
|
2018-03-05 08:16:58 +03:00
|
|
|
return !(env->cregs[1] & ASCE_PRIVATE_SPACE);
|
2017-10-16 23:23:58 +03:00
|
|
|
case PSW_ASC_SECONDARY:
|
2018-03-05 08:16:58 +03:00
|
|
|
return !(env->cregs[7] & ASCE_PRIVATE_SPACE);
|
2017-10-16 23:23:58 +03:00
|
|
|
case PSW_ASC_HOME:
|
2018-03-05 08:16:58 +03:00
|
|
|
return !(env->cregs[13] & ASCE_PRIVATE_SPACE);
|
2017-10-16 23:23:58 +03:00
|
|
|
default:
|
|
|
|
/* We don't support access register mode */
|
|
|
|
error_report("unsupported addressing mode");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:18 +03:00
|
|
|
/**
|
|
|
|
* Translate real address to absolute (= physical)
|
|
|
|
* address by taking care of the prefix mapping.
|
|
|
|
*/
|
2017-05-18 20:26:40 +03:00
|
|
|
target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr)
|
2015-02-12 20:09:18 +03:00
|
|
|
{
|
|
|
|
if (raddr < 0x2000) {
|
|
|
|
return raddr + env->psa; /* Map the lowcore. */
|
|
|
|
} else if (raddr >= env->psa && raddr < env->psa + 0x2000) {
|
|
|
|
return raddr - env->psa; /* Map the 0 page. */
|
|
|
|
}
|
|
|
|
return raddr;
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:55:05 +03:00
|
|
|
bool mmu_absolute_addr_valid(target_ulong addr, bool is_write)
|
|
|
|
{
|
|
|
|
return address_space_access_valid(&address_space_memory,
|
|
|
|
addr & TARGET_PAGE_MASK,
|
|
|
|
TARGET_PAGE_SIZE, is_write,
|
|
|
|
MEMTXATTRS_UNSPECIFIED);
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:08:19 +03:00
|
|
|
static inline bool read_table_entry(CPUS390XState *env, hwaddr gaddr,
|
|
|
|
uint64_t *entry)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* According to the PoP, these table addresses are "unpredictably real
|
|
|
|
* or absolute". Also, "it is unpredictable whether the address wraps
|
|
|
|
* or an addressing exception is recognized".
|
|
|
|
*
|
|
|
|
* We treat them as absolute addresses and don't wrap them.
|
|
|
|
*/
|
|
|
|
if (unlikely(address_space_read(cs->as, gaddr, MEMTXATTRS_UNSPECIFIED,
|
2020-02-19 22:28:22 +03:00
|
|
|
entry, sizeof(*entry)) !=
|
2019-09-25 13:08:19 +03:00
|
|
|
MEMTX_OK)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*entry = be64_to_cpu(*entry);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:29 +03:00
|
|
|
static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
|
|
|
|
uint64_t asc, uint64_t asce, target_ulong *raddr,
|
2021-09-03 18:55:06 +03:00
|
|
|
int *flags)
|
2015-02-12 20:09:18 +03:00
|
|
|
{
|
2019-09-25 15:24:42 +03:00
|
|
|
const bool edat1 = (env->cregs[0] & CR0_EDAT) &&
|
|
|
|
s390_has_feat(S390_FEAT_EDAT);
|
2018-01-15 02:04:07 +03:00
|
|
|
const bool edat2 = edat1 && s390_has_feat(S390_FEAT_EDAT_2);
|
2018-01-15 02:29:22 +03:00
|
|
|
const bool iep = (env->cregs[0] & CR0_IEP) &&
|
|
|
|
s390_has_feat(S390_FEAT_INSTRUCTION_EXEC_PROT);
|
2019-09-25 15:04:45 +03:00
|
|
|
const int asce_tl = asce & ASCE_TABLE_LENGTH;
|
2019-09-25 15:24:42 +03:00
|
|
|
const int asce_p = asce & ASCE_PRIVATE_SPACE;
|
|
|
|
hwaddr gaddr = asce & ASCE_ORIGIN;
|
|
|
|
uint64_t entry;
|
2015-02-12 20:09:18 +03:00
|
|
|
|
2018-03-05 08:16:58 +03:00
|
|
|
if (asce & ASCE_REAL_SPACE) {
|
2015-02-12 20:09:19 +03:00
|
|
|
/* direct mapping */
|
|
|
|
*raddr = vaddr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:24:42 +03:00
|
|
|
switch (asce & ASCE_TYPE_MASK) {
|
2018-03-05 08:16:58 +03:00
|
|
|
case ASCE_TYPE_REGION1:
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION1_TL(vaddr) > asce_tl) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_REG_FIRST_TRANS;
|
2015-02-12 20:09:21 +03:00
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
gaddr += VADDR_REGION1_TX(vaddr) * 8;
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
2018-03-05 08:16:58 +03:00
|
|
|
case ASCE_TYPE_REGION2:
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION1_TX(vaddr)) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_ASCE_TYPE;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION2_TL(vaddr) > asce_tl) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_REG_SEC_TRANS;
|
2015-02-12 20:09:21 +03:00
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
gaddr += VADDR_REGION2_TX(vaddr) * 8;
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
2018-03-05 08:16:58 +03:00
|
|
|
case ASCE_TYPE_REGION3:
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION1_TX(vaddr) || VADDR_REGION2_TX(vaddr)) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_ASCE_TYPE;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION3_TL(vaddr) > asce_tl) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_REG_THIRD_TRANS;
|
2015-02-12 20:09:21 +03:00
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
gaddr += VADDR_REGION3_TX(vaddr) * 8;
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
2018-03-05 08:16:58 +03:00
|
|
|
case ASCE_TYPE_SEGMENT:
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_REGION1_TX(vaddr) || VADDR_REGION2_TX(vaddr) ||
|
|
|
|
VADDR_REGION3_TX(vaddr)) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_ASCE_TYPE;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
2019-09-25 15:04:45 +03:00
|
|
|
if (VADDR_SEGMENT_TL(vaddr) > asce_tl) {
|
2019-09-25 12:59:14 +03:00
|
|
|
return PGM_SEGMENT_TRANS;
|
2015-02-12 20:09:21 +03:00
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
gaddr += VADDR_SEGMENT_TX(vaddr) * 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (asce & ASCE_TYPE_MASK) {
|
|
|
|
case ASCE_TYPE_REGION1:
|
|
|
|
if (!read_table_entry(env, gaddr, &entry)) {
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
if (entry & REGION_ENTRY_I) {
|
|
|
|
return PGM_REG_FIRST_TRANS;
|
|
|
|
}
|
|
|
|
if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION1) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
|
|
|
if (VADDR_REGION2_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
|
|
|
|
VADDR_REGION2_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
|
|
|
|
return PGM_REG_SEC_TRANS;
|
|
|
|
}
|
|
|
|
if (edat1 && (entry & REGION_ENTRY_P)) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
|
|
|
gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_REGION2_TX(vaddr) * 8;
|
|
|
|
/* fall through */
|
|
|
|
case ASCE_TYPE_REGION2:
|
|
|
|
if (!read_table_entry(env, gaddr, &entry)) {
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
if (entry & REGION_ENTRY_I) {
|
|
|
|
return PGM_REG_SEC_TRANS;
|
|
|
|
}
|
|
|
|
if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION2) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
|
|
|
if (VADDR_REGION3_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
|
|
|
|
VADDR_REGION3_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
|
|
|
|
return PGM_REG_THIRD_TRANS;
|
|
|
|
}
|
|
|
|
if (edat1 && (entry & REGION_ENTRY_P)) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
|
|
|
gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_REGION3_TX(vaddr) * 8;
|
|
|
|
/* fall through */
|
|
|
|
case ASCE_TYPE_REGION3:
|
|
|
|
if (!read_table_entry(env, gaddr, &entry)) {
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
if (entry & REGION_ENTRY_I) {
|
|
|
|
return PGM_REG_THIRD_TRANS;
|
|
|
|
}
|
|
|
|
if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION3) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
2018-01-15 02:04:07 +03:00
|
|
|
if (edat2 && (entry & REGION3_ENTRY_CR) && asce_p) {
|
|
|
|
return PGM_TRANS_SPEC;
|
2019-09-25 15:24:42 +03:00
|
|
|
}
|
|
|
|
if (edat1 && (entry & REGION_ENTRY_P)) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
2018-01-15 02:04:07 +03:00
|
|
|
if (edat2 && (entry & REGION3_ENTRY_FC)) {
|
2018-01-15 02:29:22 +03:00
|
|
|
if (iep && (entry & REGION3_ENTRY_IEP)) {
|
|
|
|
*flags &= ~PAGE_EXEC;
|
|
|
|
}
|
2018-01-15 02:04:07 +03:00
|
|
|
*raddr = (entry & REGION3_ENTRY_RFAA) |
|
|
|
|
(vaddr & ~REGION3_ENTRY_RFAA);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (VADDR_SEGMENT_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
|
|
|
|
VADDR_SEGMENT_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
|
|
|
|
return PGM_SEGMENT_TRANS;
|
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_SEGMENT_TX(vaddr) * 8;
|
|
|
|
/* fall through */
|
|
|
|
case ASCE_TYPE_SEGMENT:
|
|
|
|
if (!read_table_entry(env, gaddr, &entry)) {
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
if (entry & SEGMENT_ENTRY_I) {
|
|
|
|
return PGM_SEGMENT_TRANS;
|
|
|
|
}
|
|
|
|
if ((entry & SEGMENT_ENTRY_TT) != SEGMENT_ENTRY_TT_SEGMENT) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
|
|
|
if ((entry & SEGMENT_ENTRY_CS) && asce_p) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
|
|
|
if (entry & SEGMENT_ENTRY_P) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
|
|
|
if (edat1 && (entry & SEGMENT_ENTRY_FC)) {
|
2018-01-15 02:29:22 +03:00
|
|
|
if (iep && (entry & SEGMENT_ENTRY_IEP)) {
|
|
|
|
*flags &= ~PAGE_EXEC;
|
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
*raddr = (entry & SEGMENT_ENTRY_SFAA) |
|
|
|
|
(vaddr & ~SEGMENT_ENTRY_SFAA);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
gaddr = (entry & SEGMENT_ENTRY_ORIGIN) + VADDR_PAGE_TX(vaddr) * 8;
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:24:42 +03:00
|
|
|
if (!read_table_entry(env, gaddr, &entry)) {
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
if (entry & PAGE_ENTRY_I) {
|
|
|
|
return PGM_PAGE_TRANS;
|
|
|
|
}
|
|
|
|
if (entry & PAGE_ENTRY_0) {
|
|
|
|
return PGM_TRANS_SPEC;
|
|
|
|
}
|
|
|
|
if (entry & PAGE_ENTRY_P) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
2018-01-15 02:29:22 +03:00
|
|
|
if (iep && (entry & PAGE_ENTRY_IEP)) {
|
|
|
|
*flags &= ~PAGE_EXEC;
|
|
|
|
}
|
2019-09-25 15:24:42 +03:00
|
|
|
|
|
|
|
*raddr = entry & TARGET_PAGE_MASK;
|
|
|
|
return 0;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:47:08 +03:00
|
|
|
static void mmu_handle_skey(target_ulong addr, int rw, int *flags)
|
|
|
|
{
|
|
|
|
static S390SKeysClass *skeyclass;
|
|
|
|
static S390SKeysState *ss;
|
2021-09-03 18:55:09 +03:00
|
|
|
uint8_t key, old_key;
|
2019-08-16 11:47:08 +03:00
|
|
|
|
2021-09-03 18:55:08 +03:00
|
|
|
/*
|
|
|
|
* We expect to be called with an absolute address that has already been
|
|
|
|
* validated, such that we can reliably use it to lookup the storage key.
|
|
|
|
*/
|
2019-08-16 11:47:08 +03:00
|
|
|
if (unlikely(!ss)) {
|
|
|
|
ss = s390_get_skeys_device();
|
|
|
|
skeyclass = S390_SKEYS_GET_CLASS(ss);
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:55:14 +03:00
|
|
|
/*
|
|
|
|
* Don't enable storage keys if they are still disabled, i.e., no actual
|
|
|
|
* storage key instruction was issued yet.
|
|
|
|
*/
|
|
|
|
if (!skeyclass->skeys_are_enabled(ss)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:47:08 +03:00
|
|
|
/*
|
|
|
|
* Whenever we create a new TLB entry, we set the storage key reference
|
|
|
|
* bit. In case we allow write accesses, we set the storage key change
|
|
|
|
* bit. Whenever the guest changes the storage key, we have to flush the
|
|
|
|
* TLBs of all CPUs (the whole TLB or all affected entries), so that the
|
|
|
|
* next reference/change will result in an MMU fault and make us properly
|
|
|
|
* update the storage key here.
|
|
|
|
*
|
|
|
|
* Note 1: "record of references ... is not necessarily accurate",
|
|
|
|
* "change bit may be set in case no storing has occurred".
|
|
|
|
* -> We can set reference/change bits even on exceptions.
|
|
|
|
* Note 2: certain accesses seem to ignore storage keys. For example,
|
|
|
|
* DAT translation does not set reference bits for table accesses.
|
|
|
|
*
|
|
|
|
* TODO: key-controlled protection. Only CPU accesses make use of the
|
|
|
|
* PSW key. CSS accesses are different - we have to pass in the key.
|
|
|
|
*
|
|
|
|
* TODO: we have races between getting and setting the key.
|
|
|
|
*/
|
2024-06-13 13:30:00 +03:00
|
|
|
if (s390_skeys_get(ss, addr / TARGET_PAGE_SIZE, 1, &key)) {
|
2019-08-16 11:47:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-09-03 18:55:09 +03:00
|
|
|
old_key = key;
|
2019-08-16 11:47:08 +03:00
|
|
|
|
|
|
|
switch (rw) {
|
|
|
|
case MMU_DATA_LOAD:
|
|
|
|
case MMU_INST_FETCH:
|
|
|
|
/*
|
|
|
|
* The TLB entry has to remain write-protected on read-faults if
|
|
|
|
* the storage key does not indicate a change already. Otherwise
|
|
|
|
* we might miss setting the change bit on write accesses.
|
|
|
|
*/
|
|
|
|
if (!(key & SK_C)) {
|
|
|
|
*flags &= ~PAGE_WRITE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MMU_DATA_STORE:
|
|
|
|
key |= SK_C;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any store/fetch sets the reference bit */
|
|
|
|
key |= SK_R;
|
|
|
|
|
2021-09-03 18:55:09 +03:00
|
|
|
if (key != old_key) {
|
2024-06-13 13:30:00 +03:00
|
|
|
s390_skeys_set(ss, addr / TARGET_PAGE_SIZE, 1, &key);
|
2019-08-16 11:47:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:22 +03:00
|
|
|
/**
|
|
|
|
* Translate a virtual (logical) address into a physical (absolute) address.
|
|
|
|
* @param vaddr the virtual address
|
2021-09-03 18:55:08 +03:00
|
|
|
* @param rw 0 = read, 1 = write, 2 = code fetch, < 0 = load real address
|
2015-02-12 20:09:22 +03:00
|
|
|
* @param asc address space control (one of the PSW_ASC_* modes)
|
|
|
|
* @param raddr the translated address is stored to this pointer
|
|
|
|
* @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
|
2021-09-03 18:55:07 +03:00
|
|
|
* @param tec the translation exception code if stored to this pointer if
|
|
|
|
* there is an exception to raise
|
2019-10-01 20:16:05 +03:00
|
|
|
* @return 0 = success, != 0, the exception to raise
|
2015-02-12 20:09:22 +03:00
|
|
|
*/
|
2015-02-12 20:09:18 +03:00
|
|
|
int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
|
2019-10-01 20:16:05 +03:00
|
|
|
target_ulong *raddr, int *flags, uint64_t *tec)
|
2015-02-12 20:09:18 +03:00
|
|
|
{
|
2019-08-16 11:47:05 +03:00
|
|
|
uint64_t asce;
|
|
|
|
int r;
|
2015-06-26 21:01:00 +03:00
|
|
|
|
2019-10-01 20:16:05 +03:00
|
|
|
*tec = (vaddr & TARGET_PAGE_MASK) | (asc >> 46) |
|
|
|
|
(rw == MMU_DATA_STORE ? FS_WRITE : FS_READ);
|
2015-02-12 20:09:18 +03:00
|
|
|
*flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
|
2019-10-01 20:16:05 +03:00
|
|
|
|
2017-10-16 23:23:58 +03:00
|
|
|
if (is_low_address(vaddr & TARGET_PAGE_MASK) && lowprot_enabled(env, asc)) {
|
|
|
|
/*
|
|
|
|
* If any part of this page is currently protected, make sure the
|
|
|
|
* TLB entry will not be reused.
|
|
|
|
*
|
|
|
|
* As the protected range is always the first 512 bytes of the
|
|
|
|
* two first pages, we are able to catch all writes to these areas
|
|
|
|
* just by looking at the start address (triggering the tlb miss).
|
|
|
|
*/
|
|
|
|
*flags |= PAGE_WRITE_INV;
|
|
|
|
if (is_low_address(vaddr) && rw == MMU_DATA_STORE) {
|
2019-10-01 20:16:05 +03:00
|
|
|
/* LAP sets bit 56 */
|
|
|
|
*tec |= 0x80;
|
|
|
|
return PGM_PROTECTION;
|
2017-10-16 23:23:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:18 +03:00
|
|
|
vaddr &= TARGET_PAGE_MASK;
|
|
|
|
|
2023-07-04 11:12:30 +03:00
|
|
|
if (rw != MMU_S390_LRA && !(env->psw.mask & PSW_MASK_DAT)) {
|
2015-02-12 20:09:18 +03:00
|
|
|
*raddr = vaddr;
|
2019-08-16 11:47:05 +03:00
|
|
|
goto nodat;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (asc) {
|
|
|
|
case PSW_ASC_PRIMARY:
|
2019-08-16 11:47:05 +03:00
|
|
|
asce = env->cregs[1];
|
2015-02-12 20:09:29 +03:00
|
|
|
break;
|
2015-02-12 20:09:18 +03:00
|
|
|
case PSW_ASC_HOME:
|
2019-08-16 11:47:05 +03:00
|
|
|
asce = env->cregs[13];
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
|
|
|
case PSW_ASC_SECONDARY:
|
2019-08-16 11:47:05 +03:00
|
|
|
asce = env->cregs[7];
|
2015-02-12 20:09:18 +03:00
|
|
|
break;
|
|
|
|
case PSW_ASC_ACCREG:
|
|
|
|
default:
|
|
|
|
hw_error("guest switched to unknown asc mode\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:47:05 +03:00
|
|
|
/* perform the DAT translation */
|
2021-09-03 18:55:06 +03:00
|
|
|
r = mmu_translate_asce(env, vaddr, asc, asce, raddr, flags);
|
2019-09-25 12:59:14 +03:00
|
|
|
if (unlikely(r)) {
|
2019-10-01 20:16:05 +03:00
|
|
|
return r;
|
2019-08-16 11:47:05 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 12:53:52 +03:00
|
|
|
/* check for DAT protection */
|
|
|
|
if (unlikely(rw == MMU_DATA_STORE && !(*flags & PAGE_WRITE))) {
|
2019-10-01 20:16:05 +03:00
|
|
|
/* DAT sets bit 61 only */
|
|
|
|
*tec |= 0x4;
|
|
|
|
return PGM_PROTECTION;
|
2019-09-25 12:53:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-15 02:29:22 +03:00
|
|
|
/* check for Instruction-Execution-Protection */
|
|
|
|
if (unlikely(rw == MMU_INST_FETCH && !(*flags & PAGE_EXEC))) {
|
2019-10-01 20:16:05 +03:00
|
|
|
/* IEP sets bit 56 and 61 */
|
|
|
|
*tec |= 0x84;
|
|
|
|
return PGM_PROTECTION;
|
2018-01-15 02:29:22 +03:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:47:05 +03:00
|
|
|
nodat:
|
2021-09-03 18:55:08 +03:00
|
|
|
if (rw >= 0) {
|
|
|
|
/* Convert real address -> absolute address */
|
|
|
|
*raddr = mmu_real2abs(env, *raddr);
|
2015-02-12 20:09:18 +03:00
|
|
|
|
2021-09-03 18:55:08 +03:00
|
|
|
if (!mmu_absolute_addr_valid(*raddr, rw == MMU_DATA_STORE)) {
|
|
|
|
*tec = 0; /* unused */
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
mmu_handle_skey(*raddr, rw, flags);
|
|
|
|
}
|
2019-08-16 11:47:05 +03:00
|
|
|
return 0;
|
2015-02-12 20:09:18 +03:00
|
|
|
}
|
2015-02-12 20:09:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* translate_pages: Translate a set of consecutive logical page addresses
|
2017-11-30 19:27:36 +03:00
|
|
|
* to absolute addresses. This function is used for TCG and old KVM without
|
|
|
|
* the MEMOP interface.
|
2015-02-12 20:09:31 +03:00
|
|
|
*/
|
|
|
|
static int translate_pages(S390CPU *cpu, vaddr addr, int nr_pages,
|
2019-10-01 20:16:06 +03:00
|
|
|
target_ulong *pages, bool is_write, uint64_t *tec)
|
2015-02-12 20:09:31 +03:00
|
|
|
{
|
|
|
|
uint64_t asc = cpu->env.psw.mask & PSW_MASK_ASC;
|
|
|
|
CPUS390XState *env = &cpu->env;
|
|
|
|
int ret, i, pflags;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
2019-10-01 20:16:06 +03:00
|
|
|
ret = mmu_translate(env, addr, is_write, asc, &pages[i], &pflags, tec);
|
2015-02-12 20:09:31 +03:00
|
|
|
if (ret) {
|
2019-10-01 20:16:06 +03:00
|
|
|
return ret;
|
2015-02-12 20:09:31 +03:00
|
|
|
}
|
|
|
|
addr += TARGET_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:19:12 +03:00
|
|
|
int s390_cpu_pv_mem_rw(S390CPU *cpu, unsigned int offset, void *hostbuf,
|
|
|
|
int len, bool is_write)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (kvm_enabled()) {
|
|
|
|
ret = kvm_s390_mem_op_pv(cpu, offset, hostbuf, len, is_write);
|
|
|
|
} else {
|
|
|
|
/* Protected Virtualization is a KVM/Hardware only feature */
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:31 +03:00
|
|
|
/**
|
|
|
|
* s390_cpu_virt_mem_rw:
|
|
|
|
* @laddr: the logical start address
|
2015-03-05 12:36:48 +03:00
|
|
|
* @ar: the access register number
|
2015-02-12 20:09:31 +03:00
|
|
|
* @hostbuf: buffer in host memory. NULL = do only checks w/o copying
|
2015-04-09 21:32:39 +03:00
|
|
|
* @len: length that should be transferred
|
2015-02-12 20:09:31 +03:00
|
|
|
* @is_write: true = write, false = read
|
2015-04-09 21:32:39 +03:00
|
|
|
* Returns: 0 on success, non-zero if an exception occurred
|
2015-02-12 20:09:31 +03:00
|
|
|
*
|
|
|
|
* Copy from/to guest memory using logical addresses. Note that we inject a
|
|
|
|
* program interrupt in case there is an error while accessing the memory.
|
2017-11-30 19:27:35 +03:00
|
|
|
*
|
|
|
|
* This function will always return (also for TCG), make sure to call
|
|
|
|
* s390_cpu_virt_mem_handle_exc() to properly exit the CPU loop.
|
2015-02-12 20:09:31 +03:00
|
|
|
*/
|
2015-03-05 12:36:48 +03:00
|
|
|
int s390_cpu_virt_mem_rw(S390CPU *cpu, vaddr laddr, uint8_t ar, void *hostbuf,
|
2015-02-12 20:09:31 +03:00
|
|
|
int len, bool is_write)
|
|
|
|
{
|
|
|
|
int currlen, nr_pages, i;
|
|
|
|
target_ulong *pages;
|
2019-10-01 20:16:06 +03:00
|
|
|
uint64_t tec;
|
2015-02-12 20:09:31 +03:00
|
|
|
int ret;
|
|
|
|
|
2015-02-06 17:54:58 +03:00
|
|
|
if (kvm_enabled()) {
|
2015-03-05 12:36:48 +03:00
|
|
|
ret = kvm_s390_mem_op(cpu, laddr, ar, hostbuf, len, is_write);
|
2015-02-06 17:54:58 +03:00
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:09:31 +03:00
|
|
|
nr_pages = (((laddr & ~TARGET_PAGE_MASK) + len - 1) >> TARGET_PAGE_BITS)
|
|
|
|
+ 1;
|
|
|
|
pages = g_malloc(nr_pages * sizeof(*pages));
|
|
|
|
|
2019-10-01 20:16:06 +03:00
|
|
|
ret = translate_pages(cpu, laddr, nr_pages, pages, is_write, &tec);
|
|
|
|
if (ret) {
|
2019-10-01 20:16:12 +03:00
|
|
|
trigger_access_exception(&cpu->env, ret, tec);
|
2019-10-01 20:16:06 +03:00
|
|
|
} else if (hostbuf != NULL) {
|
2015-02-12 20:09:31 +03:00
|
|
|
/* Copy data by stepping through the area page by page */
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
currlen = MIN(len, TARGET_PAGE_SIZE - (laddr % TARGET_PAGE_SIZE));
|
|
|
|
cpu_physical_memory_rw(pages[i] | (laddr & ~TARGET_PAGE_MASK),
|
|
|
|
hostbuf, currlen, is_write);
|
|
|
|
laddr += currlen;
|
|
|
|
hostbuf += currlen;
|
|
|
|
len -= currlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(pages);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-26 21:33:14 +03:00
|
|
|
|
2017-11-30 19:27:35 +03:00
|
|
|
void s390_cpu_virt_mem_handle_exc(S390CPU *cpu, uintptr_t ra)
|
|
|
|
{
|
|
|
|
/* KVM will handle the interrupt automatically, TCG has to exit the TB */
|
|
|
|
#ifdef CONFIG_TCG
|
|
|
|
if (tcg_enabled()) {
|
|
|
|
cpu_loop_exit_restore(CPU(cpu), ra);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-26 21:33:14 +03:00
|
|
|
/**
|
|
|
|
* Translate a real address into a physical (absolute) address.
|
|
|
|
* @param raddr the real address
|
|
|
|
* @param rw 0 = read, 1 = write, 2 = code fetch
|
|
|
|
* @param addr the translated address is stored to this pointer
|
|
|
|
* @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
|
2019-10-01 20:16:03 +03:00
|
|
|
* @return 0 = success, != 0, the exception to raise
|
2017-09-26 21:33:14 +03:00
|
|
|
*/
|
|
|
|
int mmu_translate_real(CPUS390XState *env, target_ulong raddr, int rw,
|
2019-10-01 20:16:03 +03:00
|
|
|
target_ulong *addr, int *flags, uint64_t *tec)
|
2017-09-26 21:33:14 +03:00
|
|
|
{
|
2017-10-16 23:23:58 +03:00
|
|
|
const bool lowprot_enabled = env->cregs[0] & CR0_LOWPROT;
|
|
|
|
|
2018-02-13 19:12:40 +03:00
|
|
|
*flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
|
2017-10-16 23:23:58 +03:00
|
|
|
if (is_low_address(raddr & TARGET_PAGE_MASK) && lowprot_enabled) {
|
|
|
|
/* see comment in mmu_translate() how this works */
|
|
|
|
*flags |= PAGE_WRITE_INV;
|
|
|
|
if (is_low_address(raddr) && rw == MMU_DATA_STORE) {
|
2018-01-15 03:17:34 +03:00
|
|
|
/* LAP sets bit 56 */
|
2019-10-17 15:19:22 +03:00
|
|
|
*tec = (raddr & TARGET_PAGE_MASK) | FS_WRITE | 0x80;
|
2019-10-01 20:16:03 +03:00
|
|
|
return PGM_PROTECTION;
|
2017-10-16 23:23:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = mmu_real2abs(env, raddr & TARGET_PAGE_MASK);
|
2017-09-26 21:33:14 +03:00
|
|
|
|
2021-09-03 18:55:08 +03:00
|
|
|
if (!mmu_absolute_addr_valid(*addr, rw == MMU_DATA_STORE)) {
|
|
|
|
/* unused */
|
|
|
|
*tec = 0;
|
|
|
|
return PGM_ADDRESSING;
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:47:08 +03:00
|
|
|
mmu_handle_skey(*addr, rw, flags);
|
2017-09-26 21:33:14 +03:00
|
|
|
return 0;
|
|
|
|
}
|