Skip to content

Commit b320e21

Browse files
cohuckpm215
authored andcommitted
arm/kvm: add support for MTE
Extend the 'mte' property for the virt machine to cover KVM as well. For KVM, we don't allocate tag memory, but instead enable the capability. If MTE has been enabled, we need to disable migration, as we do not yet have a way to migrate the tags as well. Therefore, MTE will stay off with KVM unless requested explicitly. Signed-off-by: Cornelia Huck <cohuck@redhat.com> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-id: 20230428095533.21747-2-cohuck@redhat.com Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
1 parent 96e6d25 commit b320e21

File tree

6 files changed

+107
-34
lines changed

6 files changed

+107
-34
lines changed

hw/arm/virt.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ static void machvirt_init(MachineState *machine)
21462146
exit(1);
21472147
}
21482148

2149-
if (vms->mte && (kvm_enabled() || hvf_enabled())) {
2149+
if (vms->mte && hvf_enabled()) {
21502150
error_report("mach-virt: %s does not support providing "
21512151
"MTE to the guest CPU",
21522152
current_accel_name());
@@ -2216,39 +2216,48 @@ static void machvirt_init(MachineState *machine)
22162216
}
22172217

22182218
if (vms->mte) {
2219-
/* Create the memory region only once, but link to all cpus. */
2220-
if (!tag_sysmem) {
2221-
/*
2222-
* The property exists only if MemTag is supported.
2223-
* If it is, we must allocate the ram to back that up.
2224-
*/
2225-
if (!object_property_find(cpuobj, "tag-memory")) {
2226-
error_report("MTE requested, but not supported "
2227-
"by the guest CPU");
2228-
exit(1);
2219+
if (tcg_enabled()) {
2220+
/* Create the memory region only once, but link to all cpus. */
2221+
if (!tag_sysmem) {
2222+
/*
2223+
* The property exists only if MemTag is supported.
2224+
* If it is, we must allocate the ram to back that up.
2225+
*/
2226+
if (!object_property_find(cpuobj, "tag-memory")) {
2227+
error_report("MTE requested, but not supported "
2228+
"by the guest CPU");
2229+
exit(1);
2230+
}
2231+
2232+
tag_sysmem = g_new(MemoryRegion, 1);
2233+
memory_region_init(tag_sysmem, OBJECT(machine),
2234+
"tag-memory", UINT64_MAX / 32);
2235+
2236+
if (vms->secure) {
2237+
secure_tag_sysmem = g_new(MemoryRegion, 1);
2238+
memory_region_init(secure_tag_sysmem, OBJECT(machine),
2239+
"secure-tag-memory",
2240+
UINT64_MAX / 32);
2241+
2242+
/* As with ram, secure-tag takes precedence over tag. */
2243+
memory_region_add_subregion_overlap(secure_tag_sysmem,
2244+
0, tag_sysmem, -1);
2245+
}
22292246
}
22302247

2231-
tag_sysmem = g_new(MemoryRegion, 1);
2232-
memory_region_init(tag_sysmem, OBJECT(machine),
2233-
"tag-memory", UINT64_MAX / 32);
2234-
2248+
object_property_set_link(cpuobj, "tag-memory",
2249+
OBJECT(tag_sysmem), &error_abort);
22352250
if (vms->secure) {
2236-
secure_tag_sysmem = g_new(MemoryRegion, 1);
2237-
memory_region_init(secure_tag_sysmem, OBJECT(machine),
2238-
"secure-tag-memory", UINT64_MAX / 32);
2239-
2240-
/* As with ram, secure-tag takes precedence over tag. */
2241-
memory_region_add_subregion_overlap(secure_tag_sysmem, 0,
2242-
tag_sysmem, -1);
2251+
object_property_set_link(cpuobj, "secure-tag-memory",
2252+
OBJECT(secure_tag_sysmem),
2253+
&error_abort);
22432254
}
2244-
}
2245-
2246-
object_property_set_link(cpuobj, "tag-memory", OBJECT(tag_sysmem),
2247-
&error_abort);
2248-
if (vms->secure) {
2249-
object_property_set_link(cpuobj, "secure-tag-memory",
2250-
OBJECT(secure_tag_sysmem),
2251-
&error_abort);
2255+
} else if (kvm_enabled()) {
2256+
if (!kvm_arm_mte_supported()) {
2257+
error_report("MTE requested, but not supported by KVM");
2258+
exit(1);
2259+
}
2260+
kvm_arm_enable_mte(cpuobj, &error_abort);
22522261
}
22532262
}
22542263

target/arm/cpu.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,7 @@ void arm_cpu_post_init(Object *obj)
14801480
qdev_prop_allow_set_link_before_realize,
14811481
OBJ_PROP_LINK_STRONG);
14821482
}
1483+
cpu->has_mte = true;
14831484
}
14841485
#endif
14851486
}
@@ -1616,7 +1617,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
16161617
}
16171618
if (cpu->tag_memory) {
16181619
error_setg(errp,
1619-
"Cannot enable %s when guest CPUs has MTE enabled",
1620+
"Cannot enable %s when guest CPUs has tag memory enabled",
16201621
current_accel_name());
16211622
return;
16221623
}
@@ -1996,10 +1997,10 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
19961997
}
19971998

19981999
#ifndef CONFIG_USER_ONLY
1999-
if (cpu->tag_memory == NULL && cpu_isar_feature(aa64_mte, cpu)) {
2000+
if (!cpu->has_mte && cpu_isar_feature(aa64_mte, cpu)) {
20002001
/*
2001-
* Disable the MTE feature bits if we do not have tag-memory
2002-
* provided by the machine.
2002+
* Disable the MTE feature bits if we do not have the feature
2003+
* setup by the machine.
20032004
*/
20042005
cpu->isar.id_aa64pfr1 =
20052006
FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 0);

target/arm/cpu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,9 @@ struct ArchCPU {
935935
*/
936936
uint32_t psci_conduit;
937937

938+
/* CPU has Memory Tag Extension */
939+
bool has_mte;
940+
938941
/* For v8M, initial value of the Secure VTOR */
939942
uint32_t init_svtor;
940943
/* For v8M, initial value of the Non-secure VTOR */
@@ -1053,6 +1056,7 @@ struct ArchCPU {
10531056
bool prop_pauth;
10541057
bool prop_pauth_impdef;
10551058
bool prop_lpa2;
1059+
OnOffAuto prop_mte;
10561060

10571061
/* DCZ blocksize, in log_2(words), ie low 4 bits of DCZID_EL0 */
10581062
uint32_t dcz_blocksize;

target/arm/kvm.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "hw/boards.h"
3232
#include "hw/irq.h"
3333
#include "qemu/log.h"
34+
#include "migration/blocker.h"
3435

3536
const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
3637
KVM_CAP_LAST_INFO
@@ -1064,3 +1065,37 @@ bool kvm_arch_cpu_check_are_resettable(void)
10641065
void kvm_arch_accel_class_init(ObjectClass *oc)
10651066
{
10661067
}
1068+
1069+
void kvm_arm_enable_mte(Object *cpuobj, Error **errp)
1070+
{
1071+
static bool tried_to_enable;
1072+
static bool succeeded_to_enable;
1073+
Error *mte_migration_blocker = NULL;
1074+
int ret;
1075+
1076+
if (!tried_to_enable) {
1077+
/*
1078+
* MTE on KVM is enabled on a per-VM basis (and retrying doesn't make
1079+
* sense), and we only want a single migration blocker as well.
1080+
*/
1081+
tried_to_enable = true;
1082+
1083+
ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_ARM_MTE, 0);
1084+
if (ret) {
1085+
error_setg_errno(errp, -ret, "Failed to enable KVM_CAP_ARM_MTE");
1086+
return;
1087+
}
1088+
1089+
/* TODO: add proper migration support with MTE enabled */
1090+
error_setg(&mte_migration_blocker,
1091+
"Live migration disabled due to MTE enabled");
1092+
if (migrate_add_blocker(mte_migration_blocker, errp)) {
1093+
error_free(mte_migration_blocker);
1094+
return;
1095+
}
1096+
succeeded_to_enable = true;
1097+
}
1098+
if (succeeded_to_enable) {
1099+
object_property_set_bool(cpuobj, "has_mte", true, NULL);
1100+
}
1101+
}

target/arm/kvm64.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ bool kvm_arm_steal_time_supported(void)
756756
return kvm_check_extension(kvm_state, KVM_CAP_STEAL_TIME);
757757
}
758758

759+
bool kvm_arm_mte_supported(void)
760+
{
761+
return kvm_check_extension(kvm_state, KVM_CAP_ARM_MTE);
762+
}
763+
759764
QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN != 1);
760765

761766
uint32_t kvm_arm_sve_get_vls(CPUState *cs)

target/arm/kvm_arm.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ bool kvm_arm_pmu_supported(void);
313313
*/
314314
bool kvm_arm_sve_supported(void);
315315

316+
/**
317+
* kvm_arm_mte_supported:
318+
*
319+
* Returns: true if KVM can enable MTE, and false otherwise.
320+
*/
321+
bool kvm_arm_mte_supported(void);
322+
316323
/**
317324
* kvm_arm_get_max_vm_ipa_size:
318325
* @ms: Machine state handle
@@ -377,6 +384,8 @@ void kvm_arm_pvtime_init(CPUState *cs, uint64_t ipa);
377384

378385
int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level);
379386

387+
void kvm_arm_enable_mte(Object *cpuobj, Error **errp);
388+
380389
#else
381390

382391
/*
@@ -403,6 +412,11 @@ static inline bool kvm_arm_steal_time_supported(void)
403412
return false;
404413
}
405414

415+
static inline bool kvm_arm_mte_supported(void)
416+
{
417+
return false;
418+
}
419+
406420
/*
407421
* These functions should never actually be called without KVM support.
408422
*/
@@ -451,6 +465,11 @@ static inline uint32_t kvm_arm_sve_get_vls(CPUState *cs)
451465
g_assert_not_reached();
452466
}
453467

468+
static inline void kvm_arm_enable_mte(Object *cpuobj, Error **errp)
469+
{
470+
g_assert_not_reached();
471+
}
472+
454473
#endif
455474

456475
static inline const char *gic_class_name(void)

0 commit comments

Comments
 (0)