From: Sebastian Ene <sebastianene@google.com>
To: alexandru.elisei@arm.com, kvmarm@lists.linux.dev,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, android-kvm@google.com
Cc: catalin.marinas@arm.com, dbrazdil@google.com, joey.gouly@arm.com,
kees@kernel.org, mark.rutland@arm.com, maz@kernel.org,
oupton@kernel.org, perlarsen@google.com, qperret@google.com,
rananta@google.com, sebastianene@google.com,
smostafa@google.com, suzuki.poulose@arm.com, tabba@google.com,
tglx@kernel.org, vdonnefort@google.com, bgrzesik@google.com,
will@kernel.org, yuzenghui@huawei.com
Subject: [PATCH 08/14] KVM: arm64: Trap & emulate the ITS MAPD command
Date: Tue, 10 Mar 2026 12:49:27 +0000 [thread overview]
Message-ID: <20260310124933.830025-9-sebastianene@google.com> (raw)
In-Reply-To: <20260310124933.830025-1-sebastianene@google.com>
Parse the MAPD command and extract the ITT address to
sanitize it. When the command has the valid bit set,
share the memory that holds the ITT table
with the hypervisor to prevent it from being used
by someone else and track the pages in an array.
When the valid bit is cleared, check if the pages
are tracked and then remove the sharing with the
hypervisor.
Check if we need to do any shadow table updates
in case the device table is configured with an
indirect layout.
Signed-off-by: Sebastian Ene <sebastianene@google.com>
---
arch/arm64/kvm/hyp/nvhe/its_emulate.c | 182 ++++++++++++++++++++++++++
drivers/irqchip/irq-gic-v3-its.c | 12 --
include/linux/irqchip/arm-gic-v3.h | 12 ++
3 files changed, 194 insertions(+), 12 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/its_emulate.c b/arch/arm64/kvm/hyp/nvhe/its_emulate.c
index 865a5d6353ed..722fe80dc2e5 100644
--- a/arch/arm64/kvm/hyp/nvhe/its_emulate.c
+++ b/arch/arm64/kvm/hyp/nvhe/its_emulate.c
@@ -12,8 +12,13 @@ struct its_priv_state {
void *cmd_host_cwriter;
struct its_shadow_tables *shadow;
hyp_spinlock_t its_lock;
+ u16 empty_idx;
+ u64 tracked_pfns[];
};
+#define MAX_TRACKED_PFNS ((PAGE_SIZE - offsetof(struct its_priv_state, \
+ tracked_pfns)) / sizeof(u64))
+
struct its_handler {
u64 offset;
u8 access_size;
@@ -23,6 +28,178 @@ struct its_handler {
DEFINE_HYP_SPINLOCK(its_setup_lock);
+static int track_pfn_add(struct its_priv_state *its, u64 pfn)
+{
+ int ret, i;
+
+ if (its->empty_idx + 1 >= MAX_TRACKED_PFNS)
+ return -ENOSPC;
+
+ ret = __pkvm_host_share_hyp(pfn);
+ if (ret)
+ return ret;
+
+ its->tracked_pfns[its->empty_idx] = pfn;
+ for (i = 0; i < MAX_TRACKED_PFNS; i++) {
+ if (!its->tracked_pfns[i])
+ break;
+ }
+
+ its->empty_idx = i;
+ return 0;
+}
+
+static int track_pfn_remove(struct its_priv_state *its, u64 pfn)
+{
+ int i, ret;
+
+ for (i = 0; i < MAX_TRACKED_PFNS; i++) {
+ if (its->tracked_pfns[i] != pfn)
+ continue;
+
+ ret = __pkvm_host_unshare_hyp(pfn);
+ if (ret)
+ return ret;
+
+ its->tracked_pfns[i] = 0;
+ its->empty_idx = i;
+ }
+
+ return 0;
+}
+
+static int get_num_itt_pages(struct its_priv_state *its, u8 num_bits)
+{
+ int nr_ites = 1 << (num_bits + 1);
+ u64 size, gits_typer = readq_relaxed(its->base + GITS_TYPER);
+
+ size = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, gits_typer) + 1);
+ size = max(size, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
+
+ return PAGE_ALIGN(size) >> PAGE_SHIFT;
+}
+
+static int track_pfn(struct its_priv_state *its, u64 start_pfn, int num_pages, bool remove)
+{
+ int i, ret;
+
+ for (i = 0; i < num_pages; i++) {
+ if (remove)
+ ret = track_pfn_remove(its, start_pfn + i);
+ else
+ ret = track_pfn_add(its, start_pfn + i);
+
+ if (ret)
+ goto err_track;
+ }
+
+ return 0;
+err_track:
+ for (i = i - 1; i >= 0; i--) {
+ if (remove)
+ track_pfn_add(its, start_pfn + i);
+ else
+ track_pfn_remove(its, start_pfn + i);
+ }
+
+ return ret;
+}
+
+static struct its_baser *get_table(struct its_priv_state *its, u64 type)
+{
+ int i;
+ struct its_shadow_tables *shadow = its->shadow;
+
+ for (i = 0; i < GITS_BASER_NR_REGS; i++) {
+ if (GITS_BASER_TYPE(shadow->tables[i].val) == type)
+ return &shadow->tables[i];
+ }
+
+ return NULL;
+}
+
+static int check_table_update(struct its_priv_state *its, u32 id, u64 type)
+{
+ u32 lvl1_idx;
+ u64 esz, *host_table, *hyp_table, new_entry, update;
+ struct its_baser *table = get_table(its, type);
+ int ret;
+ phys_addr_t new_lvl2_table, lvl2_table;
+
+ if (!table)
+ return -EINVAL;
+
+ if (!(table->val & GITS_BASER_INDIRECT))
+ return 0;
+
+ esz = GITS_BASER_ENTRY_SIZE(table->val);
+ lvl1_idx = id / (table->psz / esz);
+
+ host_table = kern_hyp_va(table->shadow);
+ hyp_table = kern_hyp_va(table->base);
+
+ new_entry = host_table[id];
+ update = new_entry ^ hyp_table[id];
+ if (!update || !(update & GITS_BASER_VALID))
+ return 0;
+
+ new_lvl2_table = hyp_phys_to_pfn(new_entry & PHYS_MASK_SHIFT);
+ lvl2_table = hyp_phys_to_pfn(hyp_table[id] & PHYS_MASK_SHIFT);
+ if (new_entry & GITS_BASER_VALID)
+ ret = __pkvm_host_donate_hyp(new_lvl2_table, table->psz >> PAGE_SHIFT);
+ else
+ ret = __pkvm_hyp_donate_host(lvl2_table, table->psz >> PAGE_SHIFT);
+ if (ret)
+ return ret;
+
+ hyp_table[id] = new_entry;
+ return 0;
+}
+
+static int process_its_mapd(struct its_priv_state *its, struct its_cmd_block *cmd)
+{
+ phys_addr_t itt_addr = cmd->raw_cmd[2] & GENMASK(51, 8);
+ u8 size = cmd->raw_cmd[1] & GENMASK(4, 0);
+ bool remove = !(cmd->raw_cmd[2] & BIT(63));
+ u32 device_id = cmd->raw_cmd[0] >> 32;
+ int num_pages, ret;
+ u64 base_pfn;
+
+ if (PAGE_ALIGNED(itt_addr))
+ return -EINVAL;
+
+ base_pfn = hyp_phys_to_pfn(itt_addr);
+ num_pages = get_num_itt_pages(its, size);
+
+ ret = check_table_update(its, device_id, GITS_BASER_TYPE_DEVICE);
+ if (ret)
+ return ret;
+
+ return track_pfn(its, base_pfn, num_pages, remove);
+}
+
+static int parse_its_cmdq(struct its_priv_state *its, int offset, ssize_t len)
+{
+ struct its_cmd_block *cmd = its->cmd_hyp_base + offset;
+ u8 req_type;
+ int ret = 0;
+
+ while (len > 0 && !ret) {
+ req_type = cmd->raw_cmd[0] & GENMASK(7, 0);
+
+ switch (req_type) {
+ case GITS_CMD_MAPD:
+ ret = process_its_mapd(its, cmd);
+ break;
+ }
+
+ cmd++;
+ len -= sizeof(struct its_cmd_block);
+ }
+
+ return ret;
+}
+
static void cwriter_write(struct its_priv_state *its, u64 offset, u64 value)
{
u64 cwriter_offset = value & GENMASK(19, 5);
@@ -41,11 +218,15 @@ static void cwriter_write(struct its_priv_state *its, u64 offset, u64 value)
return;
memcpy(its->cmd_hyp_base + cmd_offset, its->cmd_host_cwriter, cmd_len);
+ if (parse_its_cmdq(its, cmd_offset, cmd_len))
+ return;
its->cmd_host_cwriter = its->cmd_host_base +
(cmd_offset + cmd_len) % cmdq_sz;
if (its->cmd_host_cwriter == its->cmd_host_base) {
memcpy(its->cmd_hyp_base, its->cmd_host_base, cwriter_offset);
+ if (parse_its_cmdq(its, cmd_offset, cmd_len))
+ return;
its->cmd_host_cwriter = its->cmd_host_base + cwriter_offset;
}
@@ -357,6 +538,7 @@ int pkvm_init_gic_its_emulation(phys_addr_t dev_addr, void *host_priv_state,
priv_state->cmd_hyp_base = kern_hyp_va(shadow->cmd_original);
priv_state->cmd_host_base = kern_hyp_va(shadow->cmd_shadow);
priv_state->cmd_host_cwriter = priv_state->cmd_host_base;
+ priv_state->empty_idx = 0;
hyp_spin_unlock(&its_setup_lock);
diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index 278dbc56f962..be78f7dccb9f 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -121,8 +121,6 @@ static DEFINE_PER_CPU(struct its_node *, local_4_1_its);
#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
-#define ITS_ITT_ALIGN SZ_256
-
/* The maximum number of VPEID bits supported by VLPI commands */
#define ITS_MAX_VPEID_BITS \
({ \
@@ -515,16 +513,6 @@ struct its_cmd_desc {
};
};
-/*
- * The ITS command block, which is what the ITS actually parses.
- */
-struct its_cmd_block {
- union {
- u64 raw_cmd[4];
- __le64 raw_cmd_le[4];
- };
-};
-
#define ITS_CMD_QUEUE_SZ SZ_64K
#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
diff --git a/include/linux/irqchip/arm-gic-v3.h b/include/linux/irqchip/arm-gic-v3.h
index 40457a4375d4..4f7d47f3d970 100644
--- a/include/linux/irqchip/arm-gic-v3.h
+++ b/include/linux/irqchip/arm-gic-v3.h
@@ -612,6 +612,8 @@
*/
#define GIC_IRQ_TYPE_LPI 0xa110c8ed
+#define ITS_ITT_ALIGN SZ_256
+
struct rdists {
struct {
raw_spinlock_t rd_lock;
@@ -634,6 +636,16 @@ struct rdists {
bool has_vpend_valid_dirty;
};
+/*
+ * The ITS command block, which is what the ITS actually parses.
+ */
+struct its_cmd_block {
+ union {
+ u64 raw_cmd[4];
+ __le64 raw_cmd_le[4];
+ };
+};
+
struct irq_domain;
struct fwnode_handle;
int __init its_lpi_memreserve_init(void);
--
2.53.0.473.g4a7958ca14-goog
next prev parent reply other threads:[~2026-03-10 12:50 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 12:49 [RFC PATCH 00/14] KVM: ITS hardening for pKVM Sebastian Ene
2026-03-10 12:49 ` [PATCH 01/14] KVM: arm64: Donate MMIO to the hypervisor Sebastian Ene
2026-03-12 17:57 ` Fuad Tabba
2026-03-13 10:40 ` Suzuki K Poulose
2026-03-24 10:39 ` Vincent Donnefort
2026-03-10 12:49 ` [PATCH 02/14] KVM: arm64: Track host-unmapped MMIO regions in a static array Sebastian Ene
2026-03-12 19:05 ` Fuad Tabba
2026-03-24 10:46 ` Vincent Donnefort
2026-03-10 12:49 ` [PATCH 03/14] KVM: arm64: Support host MMIO trap handlers for unmapped devices Sebastian Ene
2026-03-13 9:31 ` Fuad Tabba
2026-03-24 10:59 ` Vincent Donnefort
2026-03-10 12:49 ` [PATCH 04/14] KVM: arm64: Mediate host access to GIC/ITS MMIO via unmapping Sebastian Ene
2026-03-13 9:58 ` Fuad Tabba
2026-03-10 12:49 ` [PATCH 05/14] irqchip/gic-v3-its: Prepare shadow structures for KVM host deprivilege Sebastian Ene
2026-03-13 11:26 ` Fuad Tabba
2026-03-13 13:10 ` Fuad Tabba
2026-03-20 15:11 ` Sebastian Ene
2026-03-24 14:36 ` Fuad Tabba
2026-03-10 12:49 ` [PATCH 06/14] KVM: arm64: Add infrastructure for ITS emulation setup Sebastian Ene
2026-03-16 10:46 ` Fuad Tabba
2026-03-17 9:40 ` Fuad Tabba
2026-03-10 12:49 ` [PATCH 07/14] KVM: arm64: Restrict host access to the ITS tables Sebastian Ene
2026-03-16 16:13 ` Fuad Tabba
2026-03-10 12:49 ` Sebastian Ene [this message]
2026-03-17 10:20 ` [PATCH 08/14] KVM: arm64: Trap & emulate the ITS MAPD command Fuad Tabba
2026-03-10 12:49 ` [PATCH 09/14] KVM: arm64: Trap & emulate the ITS VMAPP command Sebastian Ene
2026-03-10 12:49 ` [PATCH 10/14] KVM: arm64: Trap & emulate the ITS MAPC command Sebastian Ene
2026-03-10 12:49 ` [PATCH 11/14] KVM: arm64: Restrict host updates to GITS_CTLR Sebastian Ene
2026-03-10 12:49 ` [PATCH 12/14] KVM: arm64: Restrict host updates to GITS_CBASER Sebastian Ene
2026-03-10 12:49 ` [PATCH 13/14] KVM: arm64: Restrict host updates to GITS_BASER Sebastian Ene
2026-03-10 12:49 ` [PATCH 14/14] KVM: arm64: Implement HVC interface for ITS emulation setup Sebastian Ene
2026-03-12 17:56 ` [RFC PATCH 00/14] KVM: ITS hardening for pKVM Fuad Tabba
2026-03-20 14:42 ` Sebastian Ene
2026-03-13 15:18 ` Mostafa Saleh
2026-03-15 13:24 ` Fuad Tabba
2026-03-25 16:26 ` Sebastian Ene
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260310124933.830025-9-sebastianene@google.com \
--to=sebastianene@google.com \
--cc=alexandru.elisei@arm.com \
--cc=android-kvm@google.com \
--cc=bgrzesik@google.com \
--cc=catalin.marinas@arm.com \
--cc=dbrazdil@google.com \
--cc=joey.gouly@arm.com \
--cc=kees@kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=perlarsen@google.com \
--cc=qperret@google.com \
--cc=rananta@google.com \
--cc=smostafa@google.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=tglx@kernel.org \
--cc=vdonnefort@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox