vITS save/restore
Subsystems (6)
PATCH v6 13/24 KVM: arm64: vgic-its: Check the device id matches TYPER DEVBITS range
In reply to Eric AugerOn MAPD we currently check the device id can be stored in the device table. Let's first check it can be encoded within the range defined by TYPER DEVBITS.
Also check the collection ID belongs to the 16 bit range as GITS_TYPER CIL field equals to 0.
---
v4 -> v5:
- use GIC_ENCODE_SZ macro
v3 -> v4:
- VITS_TYPER_DEVBITS set to 16 for homogeneity
- use BIT_ULL
--- virt/kvm/arm/vgic/vgic-its.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +20 -5
@@ -204,6 +204,7 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define GIC_LPI_OFFSET 8192#define VITS_TYPER_IDBITS 16 +#define VITS_TYPER_DEVBITS 16/* * Finds and returns a collection in the ITS collection table.
@@ -404,7 +405,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,* To avoid memory waste in the guest, we keep the number of IDBits and * DevBits low - as least for the time being. */ - reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (VITS_TYPER_DEVBITS ,5 ) <<GITS_TYPER_DEVBITS_SHIFT ; reg |=GIC_ENCODE_SZ (VITS_TYPER_IDBITS ,5 ) <<GITS_TYPER_IDBITS_SHIFT ; reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;
@@ -646,16 +647,30 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,* Check whether an ID can be stored into the corresponding guest table. * For a direct table this is pretty easy, but gets a bit nasty for * indirect tables. We check whether the resulting guest physical address -* is actually valid (covered by a memslot and guest accessbible). +* is actually valid (covered by a memslot and guest accessible). * For this we have to read the respective first level entry. */ -static bool vgic_its_check_id (struct vgic_its *its,u64 baser,int id) +static bool vgic_its_check_id (struct vgic_its *its,u64 baser,u32 id) {int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +u64 indirect_ptr, type =GITS_BASER_TYPE (baser); +int esz =GITS_BASER_ENTRY_SIZE (baser);int index; -u64 indirect_ptr;gfn_t gfn; -int esz =GITS_BASER_ENTRY_SIZE (baser); + +switch (type) { +case GITS_BASER_TYPE_DEVICE : +if (id >=BIT_ULL (VITS_TYPER_DEVBITS )) +return false; +break ; +case GITS_BASER_TYPE_COLLECTION : +/* as GITS_TYPER.CID == 0, ITS supports 16-bit collection ID */ +if (id >=BIT_ULL (16 )) +return false; +break ; +default : +return false; + }if (!(baser &GITS_BASER_INDIRECT )) {phys_addr_t addr;
Signature · 1 line
2.5.5
PATCH v6 19/24 KVM: arm64: vgic-its: Collection table save/restore
In reply to Eric AugerThe save path copies the collection entries into guest RAM at the GPA specified in the BASER register. This obviously requires the BASER to be set. The last written element is a dummy collection table entry.
We do not index by collection ID as the collection entry can fit into 8 bytes while containing the collection ID.
On restore path we re-allocate the collection objects.
---
v5 -> v6:
- remove the valid pointer from vgic_its_restore_cte and return +1 if no error and last elt not found
- check the BASER valid bit
- add BUG_ON to check esz versus val size
v4 -> v5:
- add macros for field encoding/decoding
- use abi->cte_esz
- rename flush into save
- check the target_addr is valid
v3 -> v4:
- replaced u64 *ptr by gpa_t gpa
- check the collection does not exist before allocating it
v1 -> v2:
- reword commit message and directly use 8 as entry size
- no kvm parameter anymore
- add helper for flush/restore cte
- table size computed here
- add le64/cpu conversions
--- virt/kvm/arm/vgic/vgic-its.c | 102 ++++++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 9 ++++ 2 files changed, 109 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +100 -2
@@ -1804,13 +1804,91 @@ static int vgic_its_restore_device_tables(struct vgic_its *its)return -ENXIO ; } +static int vgic_its_save_cte (struct vgic_its *its, +struct its_collection *collection, +gpa_t gpa,int esz) +{ +u64 val; + + val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | + ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | + collection->collection_id); + val =cpu_to_le64 (val); +return kvm_write_guest (its->dev->kvm, gpa, &val, esz); +} + +static int vgic_its_restore_cte (struct vgic_its *its,gpa_t gpa,int esz) +{ +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +u32 target_addr, coll_id; +u64 val; +int ret; + +BUG_ON (esz >sizeof (val)); + ret =kvm_read_guest (kvm, gpa, &val, esz); +if (ret) +return ret; + val =le64_to_cpu (val); +if (!(val &KVM_ITS_CTE_VALID_MASK )) +return 0 ; + + target_addr = (u32)(val >>KVM_ITS_CTE_RDBASE_SHIFT ); + coll_id = val &KVM_ITS_CTE_ICID_MASK ; + +if (target_addr >=atomic_read (&kvm->online_vcpus)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (collection) +return -EEXIST ; + ret =vgic_its_alloc_collection (its, &collection, coll_id); +if (ret) +return ret; + collection->target_addr = target_addr; +return 1 ; +} +/** * vgic_its_save_collection_table - Save the collection table into * guest RAM */ static int vgic_its_save_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_collection *collection; +u64 val; +gpa_t gpa; +size_t max_size, filled =0 ; +int ret, cte_esz = abi->cte_esz; + + gpa =BASER_ADDRESS (its->baser_coll_table); +if (!gpa) +return 0 ; + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +list_for_each_entry (collection, &its->collection_list, coll_list) { +if (filled == max_size) +return -ENOSPC ; + ret =vgic_its_save_cte (its, collection, gpa, cte_esz); +if (ret) +return ret; + gpa += cte_esz; + filled += cte_esz; + } + +if (filled == max_size) +return 0 ; + +/* +* table is not fully filled, add a last dummy element +* with valid bit unset +*/ + val =0 ; +BUG_ON (cte_esz >sizeof (val)); + ret =kvm_write_guest (its->dev->kvm, gpa, &val, cte_esz); +return ret; }/**
@@ -1820,7 +1898,27 @@ static int vgic_its_save_collection_table(struct vgic_its *its)*/ static int vgic_its_restore_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int cte_esz = abi->cte_esz; +size_t max_size, read =0 ; +gpa_t gpa; +int ret; + +if (!(its->baser_coll_table &GITS_BASER_VALID )) +return 0 ; + + gpa =BASER_ADDRESS (its->baser_coll_table); + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +while (read < max_size) { + ret =vgic_its_restore_cte (its, gpa, cte_esz); +if (ret <=0 ) +break ; + gpa += cte_esz; + read += cte_esz; + } +return ret; }/**
virt/kvm/arm/vgic/vgic.h +9 -0
@@ -73,6 +73,15 @@ KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \ KVM_REG_ARM_VGIC_SYSREG_OP2_MASK) +/* +* As per Documentation/virtual/kvm/devices/arm-vgic-its.txt, +* below macros are defined for ITS table entry encoding. +*/ +#define KVM_ITS_CTE_VALID_SHIFT 63 +#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_CTE_RDBASE_SHIFT 16 +#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +static inline bool irq_is_pending (struct vgic_irq *irq) {if (irq->config ==VGIC_CONFIG_EDGE )
Signature · 1 line
2.5.5
PATCH v6 20/24 KVM: arm64: vgic-its: vgic_its_check_id returns the entry's GPA
In reply to Eric AugerAs vgic_its_check_id() computes the device/collection entry's GPA, let's return it so that new callers can retrieve it easily.
---
v5 -> v6:
- add Christoffer's A-b
v3 -> v4:
- check eaddr is not NULL to allow passing NULL eaddr parameter to vgic_its_check_id
v2: new
--- virt/kvm/arm/vgic/vgic-its.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +8 -3
@@ -668,7 +668,8 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,* is actually valid (covered by a memslot and guest accessible). * For this we have to read the respective first level entry. */ -static bool vgic_its_check_id (struct vgic_its *its,u64 baser,u32 id) +static bool vgic_its_check_id (struct vgic_its *its,u64 baser,u32 id, +gpa_t *eaddr) {int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ;u64 indirect_ptr, type =GITS_BASER_TYPE (baser);
@@ -699,6 +700,8 @@ static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id) addr =BASER_ADDRESS (baser) + id * esz; gfn = addr >>PAGE_SHIFT ; +if (eaddr) + *eaddr = addr;return kvm_is_visible_gfn (its->dev->kvm, gfn); }
@@ -731,6 +734,8 @@ static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id) indirect_ptr += index * esz; gfn = indirect_ptr >>PAGE_SHIFT ; +if (eaddr) + *eaddr = indirect_ptr;return kvm_is_visible_gfn (its->dev->kvm, gfn); }
@@ -740,7 +745,7 @@ static int vgic_its_alloc_collection(struct vgic_its *its, {struct its_collection *collection; -if (!vgic_its_check_id (its, its->baser_coll_table, coll_id)) +if (!vgic_its_check_id (its, its->baser_coll_table, coll_id,NULL ))return E_ITS_MAPC_COLLECTION_OOR ; collection =kzalloc (sizeof (*collection),GFP_KERNEL );
@@ -913,7 +918,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,gpa_t itt_addr =its_cmd_get_ittaddr (its_cmd);struct its_device *device; -if (!vgic_its_check_id (its, its->baser_device_table, device_id)) +if (!vgic_its_check_id (its, its->baser_device_table, device_id,NULL ))return E_ITS_MAPD_DEVICE_OOR ;if (valid && num_eventid_bits >VITS_TYPER_IDBITS )
Signature · 1 line
2.5.5
PATCH v6 21/24 KVM: arm64: vgic-its: Device table save/restore
In reply to Eric AugerThis patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.
For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch.
On restore, devices are re-allocated and their itt are re-built.
---
v5 -> v6:
- accomodate vgic_its_alloc_device change of proto
- define bit fields for L1 entries
- s/handle_l1_entry/handle_l1_dte
- s/ite_esz/dte_esz in handle_l1_dte
- check BASER valid bit
- s/nb_eventid_bits/num_eventid_bits
- new convention for returned values
- itt functions implemented in subsequent patch
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables
v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling
v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch
v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing
--- virt/kvm/arm/vgic/vgic-its.c | 194 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 10 +++ 2 files changed, 199 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +189 -5
@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/list.h> #include <linux/uaccess.h> +#include <linux/list_sort.h> #include <linux/irqchip/arm-gic-v3.h>
@@ -1701,7 +1702,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +static u32 compute_next_devid_offset (struct list_head *h, +struct its_device *dev) {struct its_device *next;u32 next_offset;
@@ -1755,8 +1757,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,* Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables) */ -int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, -int start_id,entry_fn_t fn,void *opaque) +static int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) {void *entry =kzalloc (esz,GFP_KERNEL );struct kvm *kvm = its->dev->kvm;
@@ -1791,13 +1793,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) +{ +return -ENXIO ; +} + +static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_dte - Save a device table entry at a given GPA +* +* @its: ITS handle +* @dev: ITS device +* @ptr: GPA +*/ +static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, +gpa_t ptr,int dte_esz) +{ +struct kvm *kvm = its->dev->kvm; +u64 val, itt_addr_field; +u32 next_offset; + + itt_addr_field = dev->itt_addr >>8 ; + next_offset =compute_next_devid_offset (&its->device_list, dev); + val = (1ULL <<KVM_ITS_DTE_VALID_SHIFT | + ((u64 )next_offset <<KVM_ITS_DTE_NEXT_SHIFT ) | + (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | + (dev->num_eventid_bits -1 )); + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, ptr, &val, dte_esz); +} + +/** +* vgic_its_restore_dte - restore a device table entry +* +* @its: its handle +* @id: device id the DTE corresponds to +* @ptr: kernel VA where the 8 byte DTE is located +* @opaque: unused +* +* Return: < 0 on error, 0 if the dte is the last one, id offset to the +* next dte otherwise +*/ +static int vgic_its_restore_dte (struct vgic_its *its,u32 id, +void *ptr,void *opaque) +{ +struct its_device *dev; +gpa_t itt_addr; +u8 num_eventid_bits; +u64 entry = *(u64 *)ptr; +bool valid; +u32 offset; +int ret; + + entry =le64_to_cpu (entry); + + valid = entry >>KVM_ITS_DTE_VALID_SHIFT ; + num_eventid_bits = (entry &KVM_ITS_DTE_SIZE_MASK ) +1 ; + itt_addr = ((entry &KVM_ITS_DTE_ITTADDR_MASK ) + >>KVM_ITS_DTE_ITTADDR_SHIFT ) <<8 ; + +if (!valid) +return 1 ; + +/* dte entry is valid */ + offset = (entry &KVM_ITS_DTE_NEXT_MASK ) >>KVM_ITS_DTE_NEXT_SHIFT ; + + dev =vgic_its_alloc_device (its, id, itt_addr, num_eventid_bits); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ret =vgic_its_restore_itt (its, dev); +if (ret) +return ret; + +return offset; +} + +static int vgic_its_device_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_device *deva = container_of(a, struct its_device, dev_list); + struct its_device *devb = container_of(b, struct its_device, dev_list); + +if (deva->device_id < devb->device_id) +return -1 ; +else +return 1 ; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM +* +* L1/L2 handling is hidden by vgic_its_check_id() helper which directly +* returns the GPA of the device entry */ static int vgic_its_save_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_device *dev; +int dte_esz = abi->dte_esz; +u64 baser; + + baser = its->baser_device_table; + +list_sort (NULL , &its->device_list, vgic_its_device_cmp); + +list_for_each_entry (dev, &its->device_list, dev_list) { +int ret; +gpa_t eaddr; + +if (!vgic_its_check_id (its, baser, + dev->device_id, &eaddr)) +return -EINVAL ; + + ret =vgic_its_save_itt (its, dev); +if (ret) +return ret; + + ret =vgic_its_save_dte (its, dev, eaddr, dte_esz); +if (ret) +return ret; + } +return 0 ; +} + +/** +* handle_l1_dte - callback used for L1 device table entries (2 stage case) +* +* @its: its handle +* @id: index of the entry in the L1 table +* @addr: kernel VA +* @opaque: unused +* +* L1 table entries are scanned by steps of 1 entry +* Return < 0 if error, 0 if last dte was found when scanning the L2 +* table, +1 otherwise (meaning next L1 entry must be scanned) +*/ +static int handle_l1_dte (struct vgic_its *its,u32 id,void *addr, +void *opaque) +{ +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int l2_start_id = id * (SZ_64K / abi->dte_esz); +u64 entry = *(u64 *)addr; +int dte_esz = abi->dte_esz; +gpa_t gpa; +int ret; + + entry =le64_to_cpu (entry); + +if (!(entry &KVM_ITS_L1E_VALID_MASK )) +return 1 ; + + gpa = entry &KVM_ITS_L1E_ADDR_MASK ; + + ret =scan_its_table (its, gpa,SZ_64K , dte_esz, + l2_start_id, vgic_its_restore_dte,NULL ); + +if (ret <=0 ) +return ret; + +return 1 ; }/**
@@ -1806,7 +1966,31 @@ static int vgic_its_save_device_tables(struct vgic_its *its)*/ static int vgic_its_restore_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +u64 baser = its->baser_device_table; +int l1_esz, ret; +int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +gpa_t l1_gpa; + +if (!(baser &GITS_BASER_VALID )) +return 0 ; + + l1_gpa =BASER_ADDRESS (baser); + +if (baser &GITS_BASER_INDIRECT ) { + l1_esz =GITS_LVL1_ENTRY_SIZE ; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + handle_l1_dte,NULL ); + }else { + l1_esz = abi->dte_esz; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + vgic_its_restore_dte,NULL ); + } + +if (ret >0 ) + ret = -EINVAL ; + +return ret; } static int vgic_its_save_cte(struct vgic_its *its,
virt/kvm/arm/vgic/vgic.h +10 -0
@@ -81,6 +81,16 @@#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_CTE_RDBASE_SHIFT 16#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +#define KVM_ITS_DTE_VALID_SHIFT 63 +#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_DTE_NEXT_SHIFT 49 +#define KVM_ITS_DTE_NEXT_MASK GENMASK_ULL(62, 49) +#define KVM_ITS_DTE_ITTADDR_SHIFT 5 +#define KVM_ITS_DTE_ITTADDR_MASK GENMASK_ULL(48, 5) +#define KVM_ITS_DTE_SIZE_MASK GENMASK_ULL(4, 0) +#define KVM_ITS_L1E_VALID_MASK BIT_ULL(63) +/* we only support 64 kB translation table page size */ +#define KVM_ITS_L1E_ADDR_MASK GENMASK_ULL(51, 16)static inline bool irq_is_pending (struct vgic_irq *irq) {
Signature · 1 line
2.5.5
PATCH v6 22/24 KVM: arm64: vgic-its: ITT save and restore
In reply to Eric AugerImplement routines to save and restore device ITT and their interrupt table entries (ITE).
---
v5 -> v6:
- accomodate vgic_its_alloc_ite change of proto
- check LPI ID on restore, check eventid offset
- initializations on separate line
- coming after device save/restore
- add_lpi does config and pending bit sync
v4 -> v5:
- ITE are now sorted by eventid on the flush
- rename *flush* into *save*
- use macros for shits and masks
- pass ite_esz to vgic_its_save_ite
v3 -> v4:
- lookup_table and compute_next_eventid_offset become static in this patch
- remove static along with vgic_its_flush/restore_itt to avoid compilation warnings
- next field only computed with a shift (mask removed)
- handle the case where the last element has not been found
v2 -> v3:
- add return 0 in vgic_its_restore_ite (was in subsequent patch)
v2: creation
--- virt/kvm/arm/vgic/vgic-its.c | 116 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 4 ++ 2 files changed, 117 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +113 -3
@@ -1716,7 +1716,7 @@ static u32 compute_next_devid_offset(struct list_head *h,return min_t (u32, next_offset,VITS_DTE_MAX_DEVID_OFFSET ); } -u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) +static u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) {struct its_ite *next;u32 next_offset;
@@ -1793,14 +1793,124 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +/** +* vgic_its_save_ite - Save an interrupt translation entry at @gpa +*/ +static int vgic_its_save_ite (struct vgic_its *its,struct its_device *dev, +struct its_ite *ite,gpa_t gpa,int ite_esz) +{ +struct kvm *kvm = its->dev->kvm; +u32 next_offset; +u64 val; + + next_offset =compute_next_eventid_offset (&dev->itt_head, ite); + val = ((u64 )next_offset <<KVM_ITS_ITE_NEXT_SHIFT ) | + ((u64 )ite->lpi <<KVM_ITS_ITE_PINTID_SHIFT ) | + ite->collection->collection_id; + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, gpa, &val, ite_esz); +} + +/** +* vgic_its_restore_ite - restore an interrupt translation entry +* @event_id: id used for indexing +* @ptr: pointer to the ITE entry +* @opaque: pointer to the its_device +*/ +static int vgic_its_restore_ite (struct vgic_its *its,u32 event_id, +void *ptr,void *opaque) +{ +struct its_device *dev = (struct its_device *)opaque; +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +struct kvm_vcpu *vcpu =NULL ; +u64 val; +u64 *p = (u64 *)ptr; +struct vgic_irq *irq; +u32 coll_id, lpi_id; +struct its_ite *ite; +u32 offset; + + val = *p; + + val =le64_to_cpu (val); + + coll_id = val &KVM_ITS_ITE_ICID_MASK ; + lpi_id = (val &KVM_ITS_ITE_PINTID_MASK ) >>KVM_ITS_ITE_PINTID_SHIFT ; + +if (!lpi_id) +return 1 ;/* invalid entry, no choice but to scan next entry */ + +if (lpi_id <VGIC_MIN_LPI ) +return -EINVAL ; + + offset = val >>KVM_ITS_ITE_NEXT_SHIFT ; +if (event_id + offset >=BIT_ULL (dev->num_eventid_bits)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (!collection) +return -EINVAL ; + + ite =vgic_its_alloc_ite (dev, collection, lpi_id, event_id); +if (IS_ERR (ite)) +return PTR_ERR (ite); + +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_id, vcpu); +if (IS_ERR (irq)) +return PTR_ERR (irq); + ite->irq = irq; + +return offset; +} + +static int vgic_its_ite_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_ite *itea = container_of(a, struct its_ite, ite_list); + struct its_ite *iteb = container_of(b, struct its_ite, ite_list); + +if (itea->event_id < iteb->event_id) +return -1 ; +else +return 1 ; +} +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +gpa_t base = device->itt_addr; +struct its_ite *ite; +int ret; +int ite_esz = abi->ite_esz; + +list_sort (NULL , &device->itt_head, vgic_its_ite_cmp); + +list_for_each_entry (ite, &device->itt_head, ite_list) { +gpa_t gpa = base + ite->event_id * ite_esz; + + ret =vgic_its_save_ite (its, device, ite, gpa, ite_esz); +if (ret) +return ret; + } +return 0 ; }static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +gpa_t base = dev->itt_addr; +int ret; +int ite_esz = abi->ite_esz; +size_t max_size =BIT_ULL (dev->num_eventid_bits) * ite_esz; + + ret =scan_its_table (its, base, max_size, ite_esz,0 , + vgic_its_restore_ite, dev); + +return ret; }/**
virt/kvm/arm/vgic/vgic.h +4 -0
@@ -81,6 +81,10 @@#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_CTE_RDBASE_SHIFT 16#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +#define KVM_ITS_ITE_NEXT_SHIFT 48 +#define KVM_ITS_ITE_PINTID_SHIFT 16 +#define KVM_ITS_ITE_PINTID_MASK GENMASK_ULL(47, 16) +#define KVM_ITS_ITE_ICID_MASK GENMASK_ULL(15, 0)#define KVM_ITS_DTE_VALID_SHIFT 63#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_DTE_NEXT_SHIFT 49
Signature · 1 line
2.5.5
PATCH v6 23/24 KVM: arm64: vgic-its: Fix pending table sync
In reply to Eric AugerIn its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.
Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.
This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.
--- virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +12 -12
@@ -278,13 +278,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, }/* -* Create a snapshot of the current LPI list, so that we can enumerate all -* LPIs without holding any lock. -* Returns the array length and puts the kmalloc'ed array into intid_ptr. +* Create a snapshot of the current LPIs targeting @vcpu, so that we can +* enumerate those LPIs without holding any lock. +* Returns their number and puts the kmalloc'ed array into intid_ptr. */ -static int vgic_copy_lpi_list (struct kvm *kvm,u32 **intid_ptr) +static int vgic_copy_lpi_list (struct kvm_vcpu *vcpu,u32 **intid_ptr) { -struct vgic_dist *dist = &kvm->arch.vgic; +struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct vgic_irq *irq;u32 *intids;int irq_count = dist->lpi_list_count, i =0 ;
@@ -303,14 +303,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)spin_lock (&dist->lpi_list_lock);list_for_each_entry (irq, &dist->lpi_list_head, lpi_list) {/* We don't need to "get" the IRQ, as we hold the list lock. */ - intids[i] = irq->intid; -if (++i == irq_count) -break ; +if (irq->target_vcpu != vcpu) +continue ; + intids[i++] = irq->intid; }spin_unlock (&dist->lpi_list_lock); *intid_ptr = intids; -return irq_count; +return i; }/*
@@ -359,7 +359,7 @@ static u32 max_lpis_propbaser(u64 propbaser) }/* -* Scan the whole LPI pending table and sync the pending bit in there +* Sync the pending table pending bit of LPIs targeting @vcpu * with our own data structures. This relies on the LPI being * mapped before. */
@@ -372,7 +372,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)u32 *intids;int nr_irqs, i; - nr_irqs =vgic_copy_lpi_list (vcpu->kvm, &intids); + nr_irqs =vgic_copy_lpi_list (vcpu, &intids);if (nr_irqs <0 )return nr_irqs;
@@ -1055,7 +1055,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, vcpu =kvm_get_vcpu (kvm, collection->target_addr); - irq_count =vgic_copy_lpi_list (kvm, &intids); + irq_count =vgic_copy_lpi_list (vcpu, &intids);if (irq_count <0 )return irq_count;
Signature · 1 line
2.5.5
v6 cover vITS save/restore
This series specifies and implements an API aimed at saving and restoring the state of the in-kernel emulated ITS device.
The ITS is programmed through registers and tables. Those latter are allocated by the guest. Their base address is programmed in registers or table entries before the ITS is enabled.
The ITS is free to use some of them to flush its internal caches. This is likely to be used when entering low power state.
Therefore, for save/restore use case, it looks natural to use this guest RAM allocated space to save the table related data. However, currently, The ITS in-kernel emulated device does not use all of those tables and for those it uses, it does not always sync them with its cached data. Additional sync must happen for:
- the collection table
- the device table
- the per-device translation tables
- the LPI pending tables.
The LPI configation table and the command queues do not need extra syncs.
Best Regards
Eric
Git: complete series available at
https://github.com/eauger/linux/tree/v4.11-its-mig-v6
* Testing:
- on Cavium ThunderX using a virtio-net-pci guest, virsh save/restore commands and virt-manager live migration. Tested with 1 and 2 stage device table.
History:
v5 -> v6:
- address Christoffer's comments:
- add restore sequence in the doc
- sync with pending and config tables in its_add_lpi
- change in return values when scanning the tables
- several bug fixes pointed out by Christoffer
- see full details in individual logs
v4 -> v5:
- user API changes:
- ITS table save/restore triggered through ITS KVM device KVM_DEV_ARM_VGIC_GRP_CTRL group, KVM_DEV_ARM_ITS_SAVE_TABLES, KVM_DEV_ARM_ITS_RESTORE_TABLES
- RDIST pending table flush triggered through GICV3 KVM device KVM_DEV_ARM_VGIC_GRP_CTRL/KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
- Introduce an ABI infrastructure, entry size report using this infra
- IIDR reports ABI chosen set by userspace if any
- pending table save moved to vgic-v3.c
- use optimisation in pending table save
- check target_addr when restore cte
- pending table sync called from restore_tables
- added KVM: arm64: vgic-its: Fix pending table sync
- simplify loopup_table and use kvm_read_guest()
- sort the device and ITE list on save
- add defines for shifts and masks, GIC_ENCODE_SZ macro
v3 -> v4:
- update the DTE format (ITT_addr 52 bit support, validity bit addition)
- Document ABI revision and implement check
- iidr save/restore (including a new patch for iidr user write access)
- changed locking: kvm lock + vcpu lock
- fix nb_eventid_bits mixup
- 2 new patches aiming at exposing next_segment() and lock_all_vcpus()
- rework errror handling of lookup_table functions
- I took into account all Andre's comments/suggestions except:
- trigger the save/restore of pending tables from GICV3 KVM device instead of ITS KVM device
- implement ITS flush/restore in KVM_DEV_ARM_VGIC_GRP_CTRL ITS group
See the ML replies for current justifications. In case other people strongly disagree of course I will change the code.
v2 -> v3:
- fix restore ITS ITT_addr bit masking
v1 -> v2:
- rebased on Vijaya's v11
- all entries now are 8 byte large
- devid/eventid indexing for device table and ITT
- support 2 stage device table
- common infra to read indexed tables
- add cpu <-> le64 conversions
- itte renamed into ite
- do not care anymore about pending table 1st KB
(not needed at the moment for coarse mapping)
RFC v1
- creation
Eric Auger (24):
KVM: arm/arm64: Add ITS save/restore API documentation
KVM: arm/arm64: Add GICV3 pending table save API documentation
KVM: arm/arm64: vgic-its: rename itte into ite
arm/arm64: vgic: turn vgic_find_mmio_region into public
KVM: arm64: vgic-its: KVM_DEV_ARM_VGIC_GRP_ITS_REGS group
KVM: arm/arm64: vgic: expose (un)lock_all_vcpus
KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and
attr_regs_access
KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
KVM: arm64: vgic-its: Introduce migration ABI infrastructure
KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr
KVM: arm64: vgic-its: Interpret MAPD Size field and check related
errors
KVM: arm64: vgic-its: Interpret MAPD ITT_addr field
KVM: arm64: vgic-its: Check the device id matches TYPER DEVBITS range
KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
KVM: arm64: vgic-its: KVM_DEV_ARM_ITS_SAVE/RESTORE_TABLES
KVM: arm64: vgic-its: vgic_its_alloc_ite/device
KVM: arm64: vgic-its: Add infrastructure for table lookup
KVM: arm64: vgic-its: Collection table save/restore
KVM: arm64: vgic-its: vgic_its_check_id returns the entry's GPA
KVM: arm64: vgic-its: Device table save/restore
KVM: arm64: vgic-its: ITT save and restore
KVM: arm64: vgic-its: Fix pending table sync
KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
Documentation/virtual/kvm/devices/arm-vgic-its.txt | 117 ++ Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 6 + arch/arm/include/uapi/asm/kvm.h | 6 +- arch/arm64/include/uapi/asm/kvm.h | 6 +- include/kvm/arm_vgic.h | 3 + include/linux/irqchip/arm-gic-v3.h | 14 + virt/kvm/arm/vgic/vgic-its.c | 1148 +++++++++++++++++--- virt/kvm/arm/vgic/vgic-kvm-device.c | 24 +- virt/kvm/arm/vgic/vgic-mmio.c | 11 +- virt/kvm/arm/vgic/vgic-mmio.h | 14 +- virt/kvm/arm/vgic/vgic-v3.c | 95 ++ virt/kvm/arm/vgic/vgic.h | 28 + 12 files changed, 1330 insertions(+), 142 deletions(-)
Signature · 1 line
2.5.5
PATCH v6 1/24 KVM: arm/arm64: Add ITS save/restore API documentation
Add description for how to access ITS registers and how to save/restore ITS tables into/from memory.
---
v5 -> v6:
- add restoration ordering
- 256B -> 256 Byte aligned
- DTE Size is number of bits for the EVENTID
- s/GITS_READR/GITS_CREADR
v4 -> v5:
- take into account Christoffer's comments
- pending table save on GICV3 side now
v3 -> v4:
- take into account Peter's comments:
- typos
- KVM_DEV_ARM_VGIC_GRP_ITS_TABLES kvm_device_attr = 0
- add a validity bit in DTE
- document all fields in CTE and ITE
- document ABI revision
- take into account Andre's comments:
- document restrictions about GITS_CREADR writing and GITS_IIDR
- document -EBUSY error if one or more VCPUS are runnning
- document 64b registers only can be accessed with 64b access
- itt_addr field matches bits [51:8] of the itt_addr
v1 -> v2:
- DTE and ITE now are 8 bytes
- DTE and ITE now indexed by deviceid/eventid
- use ITE name instead of ITTE
- mentions ITT_addr matches bits [51:8] of the actual address
- mentions LE layout
--- Documentation/virtual/kvm/devices/arm-vgic-its.txt | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+)
Documentation/virtual/kvm/devices/arm-vgic-its.txt +117 -0
@@ -32,7 +32,124 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the ITS, no additional parameter in kvm_device_attr.addr. + + KVM_DEV_ARM_ITS_SAVE_TABLES + save the ITS table data into guest RAM, at the location provisioned + by the guest in corresponding registers/table entries. + + The layout of the tables in guest memory defines an ABI. The entries + are laid out in little endian format as described in the last paragraph. + + KVM_DEV_ARM_ITS_RESTORE_TABLES + restore the ITS tables from guest RAM to ITS internal structures. + + The GICV3 must be restored before the ITS and all ITS registers but + the GITS_CTLR must be restored before restoring the ITS tables. + + The GITS_IIDR read-only register must also be restored before + the table restore as the IIDR revision field encodes the ABI revision. + + The expected ordering when restoring the GICv3/ITS is described in section + "ITS Restoration Sequence". + Errors: -ENXIO: ITS not properly configured as required prior to setting this attribute -ENOMEM: Memory shortage when allocating ITS internal data + -EINVAL: Inconsistent restored data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running + + KVM_DEV_ARM_VGIC_GRP_ITS_REGS + Attributes: + The attr field of kvm_device_attr encodes the offset of the + ITS register, relative to the ITS control frame base address + (ITS_base). + + kvm_device_attr.addr points to a __u64 value whatever the width + of the addressed register (32/64 bits). 64 bit registers can only + be accessed with full length. + + Writes to read-only registers are ignored by the kernel except for: + - GITS_CREADR. It needs to be restored otherwise commands in the queue + will be re-executed after restoring CWRITER. GITS_CREADR must be + restored before restoring the GITS_CTLR which is likely to enable the + ITS. Also it needs to be restored after GITS_CBASER since a write to + GITS_CBASER resets GITS_CREADR. + - GITS_IIDR. Its Revision field encodes the table layout ABI revision. + In the future we might implement direct injection of virtual LPIS. + This will require an upgrade of the table layout and an evolution of + the ABI. GITS_IIDR must be restored before the table restoration. + + For other registers, getting or setting a register has the same + effect as reading/writing the register on real hardware. + Errors: + -ENXIO: Offset does not correspond to any supported register + -EFAULT: Invalid user pointer for attr->addr + -EINVAL: Offset is not 64-bit aligned + -EBUSY: one or more VCPUS are running + + ITS Restoration Sequence: + ------------------------- + +The following ordering must be followed when restoring the GIC and the ITS: +a) restore all guest memory and create vcpus +b) initialize the GIC and ITS (KVM_DEV_ARM_VGIC_CTRL_INIT) +c) provide GIC and ITS base addresses (KVM_DEV_ARM_VGIC_GRP_ADDR) +d) restore all redistributors +e) restore the ITS in the following order: + 1. Restore GITS_CBASER + 2. Restore all other GITS_ registers, except GITS_CTLR! + 3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES) + 4. Restore GITS_CTLR +f) start vcpus + + ITS Table ABI REV0: + ------------------- + + Revision 0 of the ABI only supports physical LPIs. + + The device table and ITT are indexed by the deviceid and eventid, + respectively. The collection table is not indexed by collectionid: + CTE are written in the table in the order of collection creation. All + entries are 8 bytes. + + Device Table Entry (DTE): + + bits: | 63| 62 ... 49 | 48 ... 5 | 4 ... 0 | + values: | V | next | ITT_addr | Size | + + where; + - V indicates whether the entry is valid. If not, other fields + are not meaningful. + - next: equals to 0 if this entry is the last one; otherwise it + corresponds to the deviceid offset to the next DTE, capped by + 2^14 -1. + - ITT_addr matches bits [51:8] of the ITT address (256 Byte aligned). + - Size specifies the supported number of bits for the eventid, + minus one + + Collection Table Entry (CTE): + + bits: | 63| 62 .. 52 | 51 ... 16 | 15 ... 0 | + values: | V | RES0 | RDBase | ICID | + + where: + - V indicates whether the entry is valid. If not, other fields are + not meaningful. + - RES0: reserved field with Should-Be-Zero-or-Preserved behavior. + - RDBase is the PE number (GICR_TYPER.Processor_Number semantic), + - ICID is the collection ID + + Interrupt Translation Entry (ITE): + + bits: | 63 ... 48 | 47 ... 16 | 15 ... 0 | + values: | next | pINTID | ICID | + + where: + - next: equals to 0 if this entry is the last one; otherwise it corresponds + to the eventid offset to the next ITE capped by 2^16 -1. + - pINTID is the physical LPI ID; if zero, it means the entry is not valid + and other fields are not meaningful. + - ICID is the collection ID +
Signature · 1 line
2.5.5
PATCH v6 2/24 KVM: arm/arm64: Add GICV3 pending table save API documentation
In reply to Eric AugerAdd description for how to save GICV3 LPI pending bit into guest RAM pending tables.
---
v5: creation
--- Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +6 -0
@@ -167,11 +167,17 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the VGIC, no additional parameter in kvm_device_attr.addr. + KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES + save all LPI pending bits into guest RAM pending tables. + + The first kB of the pending table is not altered by this operation. Errors: -ENXIO: VGIC not properly configured as required prior to calling this attribute -ENODEV: no online VCPU -ENOMEM: memory shortage when allocating vgic internal data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO
Signature · 1 line
2.5.5
PATCH v6 3/24 KVM: arm/arm64: vgic-its: rename itte into ite
In reply to Eric AugerThe actual abbreviation for the interrupt translation table entry is ITE. Let's rename all itte instances by ite.
---
v5: Add Marc's A-b
--- virt/kvm/arm/vgic/vgic-its.c | 148 +++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 74 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +74 -74
@@ -114,8 +114,8 @@ struct its_collection {#define its_is_collection_mapped (coll) ((coll) && \ ((coll)->target_addr != COLLECTION_NOT_MAPPED)) -struct its_itte { -struct list_head itte_list; +struct its_ite { +struct list_head ite_list;struct vgic_irq *irq;struct its_collection *collection;
@@ -143,27 +143,27 @@ static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)* Device ID/Event ID pair on an ITS. * Must be called with the its_lock mutex held. */ -static struct its_itte *find_itte (struct vgic_its *its,u32 device_id, +static struct its_ite *find_ite (struct vgic_its *its,u32 device_id,u32 event_id) {struct its_device *device; -struct its_itte *itte; +struct its_ite *ite; device =find_its_device (its, device_id);if (device ==NULL )return NULL ; -list_for_each_entry (itte, &device->itt_head, itte_list) -if (itte->event_id == event_id) -return itte; +list_for_each_entry (ite, &device->itt_head, ite_list) +if (ite->event_id == event_id) +return ite;return NULL ; }/* To be used as an iterator this macro misses the enclosing parentheses */ -#define for_each_lpi_its (dev, itte, its) \ +#define for_each_lpi_its (dev, ite, its) \ list_for_each_entry(dev, &(its)->device_list, dev_list) \ - list_for_each_entry(itte, &(dev)->itt_head, itte_list) + list_for_each_entry(ite, &(dev)->itt_head, ite_list)/* * We only implement 48 bits of PA at the moment, although the ITS
@@ -270,18 +270,18 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)* Needs to be called whenever either the collection for a LPIs has * changed or the collection itself got retargeted. */ -static void update_affinity_itte (struct kvm *kvm,struct its_itte *itte) +static void update_affinity_ite (struct kvm *kvm,struct its_ite *ite) {struct kvm_vcpu *vcpu; -if (!its_is_collection_mapped (itte->collection)) +if (!its_is_collection_mapped (ite->collection))return ; - vcpu =kvm_get_vcpu (kvm, itte->collection->target_addr); + vcpu =kvm_get_vcpu (kvm, ite->collection->target_addr); -spin_lock (&itte->irq->irq_lock); - itte->irq->target_vcpu = vcpu; -spin_unlock (&itte->irq->irq_lock); +spin_lock (&ite->irq->irq_lock); + ite->irq->target_vcpu = vcpu; +spin_unlock (&ite->irq->irq_lock); }/*
@@ -292,13 +292,13 @@ static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its, struct its_collection *coll) {struct its_device *device; -struct its_itte *itte; +struct its_ite *ite; -for_each_lpi_its (device, itte, its) { -if (!itte->collection || coll != itte->collection) +for_each_lpi_its (device, ite, its) { +if (!ite->collection || coll != ite->collection)continue ; -update_affinity_itte (kvm, itte); +update_affinity_ite (kvm, ite); } }
@@ -425,25 +425,25 @@ static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, u32 devid, u32 eventid) {struct kvm_vcpu *vcpu; -struct its_itte *itte; +struct its_ite *ite;if (!its->enabled)return -EBUSY ; - itte =find_itte (its, devid, eventid); -if (!itte || !its_is_collection_mapped (itte->collection)) + ite =find_ite (its, devid, eventid); +if (!ite || !its_is_collection_mapped (ite->collection))return E_ITS_INT_UNMAPPED_INTERRUPT ; - vcpu =kvm_get_vcpu (kvm, itte->collection->target_addr); + vcpu =kvm_get_vcpu (kvm, ite->collection->target_addr);if (!vcpu)return E_ITS_INT_UNMAPPED_INTERRUPT ;if (!vcpu->arch.vgic_cpu.lpis_enabled)return -EBUSY ; -spin_lock (&itte->irq->irq_lock); - itte->irq->pending_latch = true; -vgic_queue_irq_unlock (kvm, itte->irq); +spin_lock (&ite->irq->irq_lock); + ite->irq->pending_latch = true; +vgic_queue_irq_unlock (kvm, ite->irq);return 0 ; }
@@ -511,15 +511,15 @@ int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) }/* Requires the its_lock to be held. */ -static void its_free_itte (struct kvm *kvm,struct its_itte *itte) +static void its_free_ite (struct kvm *kvm,struct its_ite *ite) { -list_del (&itte->itte_list); +list_del (&ite->ite_list);/* This put matches the get in vgic_add_lpi. */ -if (itte->irq) -vgic_put_irq (kvm, itte->irq); +if (ite->irq) +vgic_put_irq (kvm, ite->irq); -kfree (itte); +kfree (ite); }static u64 its_cmd_mask_field (u64 *its_cmd,int word,int shift,int size)
@@ -544,17 +544,17 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, {u32 device_id =its_cmd_get_deviceid (its_cmd);u32 event_id =its_cmd_get_id (its_cmd); -struct its_itte *itte; +struct its_ite *ite; - itte =find_itte (its, device_id, event_id); -if (itte && itte->collection) { + ite =find_ite (its, device_id, event_id); +if (ite && ite->collection) {/* * Though the spec talks about removing the pending state, we * don't bother here since we clear the ITTE anyway and the * pending state is a property of the ITTE struct. */ -its_free_itte (kvm, itte); +its_free_ite (kvm, ite);return 0 ; }
@@ -572,26 +572,26 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct kvm_vcpu *vcpu; -struct its_itte *itte; +struct its_ite *ite;struct its_collection *collection; - itte =find_itte (its, device_id, event_id); -if (!itte) + ite =find_ite (its, device_id, event_id); +if (!ite)return E_ITS_MOVI_UNMAPPED_INTERRUPT ; -if (!its_is_collection_mapped (itte->collection)) +if (!its_is_collection_mapped (ite->collection))return E_ITS_MOVI_UNMAPPED_COLLECTION ; collection =find_collection (its, coll_id);if (!its_is_collection_mapped (collection))return E_ITS_MOVI_UNMAPPED_COLLECTION ; - itte->collection = collection; + ite->collection = collection; vcpu =kvm_get_vcpu (kvm, collection->target_addr); -spin_lock (&itte->irq->irq_lock); - itte->irq->target_vcpu = vcpu; -spin_unlock (&itte->irq->irq_lock); +spin_lock (&ite->irq->irq_lock); + ite->irq->target_vcpu = vcpu; +spin_unlock (&ite->irq->irq_lock);return 0 ; }
@@ -679,7 +679,7 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id) {struct its_collection *collection;struct its_device *device; -struct its_itte *itte; +struct its_ite *ite;/* * Clearing the mapping for that collection ID removes the
@@ -690,10 +690,10 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)if (!collection)return ; -for_each_lpi_its (device, itte, its) -if (itte->collection && - itte->collection->collection_id == coll_id) - itte->collection =NULL ; +for_each_lpi_its (device, ite, its) +if (ite->collection && + ite->collection->collection_id == coll_id) + ite->collection =NULL ;list_del (&collection->coll_list);kfree (collection);
@@ -709,7 +709,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 device_id =its_cmd_get_deviceid (its_cmd);u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd); -struct its_itte *itte; +struct its_ite *ite;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;
@@ -728,7 +728,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,return E_ITS_MAPTI_PHYSICALID_OOR ;/* If there is an existing mapping, behavior is UNPREDICTABLE. */ -if (find_itte (its, device_id, event_id)) +if (find_ite (its, device_id, event_id))return 0 ; collection =find_collection (its, coll_id);
@@ -739,36 +739,36 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, new_coll = collection; } - itte =kzalloc (sizeof (struct its_itte ),GFP_KERNEL ); -if (!itte) { + ite =kzalloc (sizeof (struct its_ite ),GFP_KERNEL ); +if (!ite) {if (new_coll)vgic_its_free_collection (its, coll_id);return -ENOMEM ; } - itte->event_id = event_id; -list_add_tail (&itte->itte_list, &device->itt_head); + ite->event_id = event_id; +list_add_tail (&ite->ite_list, &device->itt_head); - itte->collection = collection; - itte->lpi = lpi_nr; + ite->collection = collection; + ite->lpi = lpi_nr; irq =vgic_add_lpi (kvm, lpi_nr);if (IS_ERR (irq)) {if (new_coll)vgic_its_free_collection (its, coll_id); -its_free_itte (kvm, itte); +its_free_ite (kvm, ite);return PTR_ERR (irq); } - itte->irq = irq; + ite->irq = irq; -update_affinity_itte (kvm, itte); +update_affinity_ite (kvm, ite);/* * We "cache" the configuration table entries in out struct vgic_irq's. * However we only have those structs for mapped IRQs, so we read in * the respective config data from memory here upon mapping the LPI. */ -update_lpi_config (kvm, itte->irq,NULL ); +update_lpi_config (kvm, ite->irq,NULL );return 0 ; }
@@ -776,15 +776,15 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,/* Requires the its_lock to be held. */ static void vgic_its_unmap_device (struct kvm *kvm,struct its_device *device) { -struct its_itte *itte, *temp; +struct its_ite *ite, *temp;/* * The spec says that unmapping a device with still valid * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, * since we cannot leave the memory unreferenced. */ -list_for_each_entry_safe (itte, temp, &device->itt_head, itte_list) -its_free_itte (kvm, itte); +list_for_each_entry_safe (ite, temp, &device->itt_head, ite_list) +its_free_ite (kvm, ite);list_del (&device->dev_list);kfree (device);
@@ -883,14 +883,14 @@ static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its, {u32 device_id =its_cmd_get_deviceid (its_cmd);u32 event_id =its_cmd_get_id (its_cmd); -struct its_itte *itte; +struct its_ite *ite; - itte =find_itte (its, device_id, event_id); -if (!itte) + ite =find_ite (its, device_id, event_id); +if (!ite)return E_ITS_CLEAR_UNMAPPED_INTERRUPT ; - itte->irq->pending_latch = false; + ite->irq->pending_latch = false;return 0 ; }
@@ -904,14 +904,14 @@ static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its, {u32 device_id =its_cmd_get_deviceid (its_cmd);u32 event_id =its_cmd_get_id (its_cmd); -struct its_itte *itte; +struct its_ite *ite; - itte =find_itte (its, device_id, event_id); -if (!itte) + ite =find_ite (its, device_id, event_id); +if (!ite)return E_ITS_INV_UNMAPPED_INTERRUPT ; -return update_lpi_config (kvm, itte->irq,NULL ); +return update_lpi_config (kvm, ite->irq,NULL ); }/*
@@ -1435,7 +1435,7 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)struct kvm *kvm = kvm_dev->kvm;struct vgic_its *its = kvm_dev->private;struct its_device *dev; -struct its_itte *itte; +struct its_ite *ite;struct list_head *dev_cur, *dev_temp;struct list_head *cur, *temp;
@@ -1450,8 +1450,8 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)list_for_each_safe (dev_cur, dev_temp, &its->device_list) { dev = container_of(dev_cur, struct its_device, dev_list);list_for_each_safe (cur, temp, &dev->itt_head) { - itte = (container_of(cur, struct its_itte, itte_list)); -its_free_itte (kvm, itte); + ite = (container_of(cur, struct its_ite, ite_list)); +its_free_ite (kvm, ite); }list_del (dev_cur);kfree (dev);
Signature · 1 line
2.5.5
PATCH v6 4/24 arm/arm64: vgic: turn vgic_find_mmio_region into public
In reply to Eric AugerWe plan to use vgic_find_mmio_region in vgic-its.c so let's turn it into a public function.
Also let's take the opportunity to rename the region parameter into regions to emphasize this latter is an array of regions.
---
v5 -> v6:
- add Christoffer's A-b
v4 -> v5:
- add Marc's A-b
v3 -> v4:
- rename region parameter into regions
- add Andre's R-b
--- virt/kvm/arm/vgic/vgic-mmio.c | 11 +++++------ virt/kvm/arm/vgic/vgic-mmio.h | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-)
virt/kvm/arm/vgic/vgic-mmio.c +5 -6
@@ -446,13 +446,12 @@ static int match_region(const void *key, const void *elt)return 0 ; } -/* Find the proper register handler entry given a certain address offset. */ -static const struct vgic_register_region * -vgic_find_mmio_region (const struct vgic_register_region *region,int nr_regions, -unsigned int offset) +const struct vgic_register_region * +vgic_find_mmio_region (const struct vgic_register_region *regions, +int nr_regions,unsigned int offset) { -return bsearch ((void *)(uintptr_t )offset, region, nr_regions, -sizeof (region[0 ]), match_region); +return bsearch ((void *)(uintptr_t )offset, regions, nr_regions, +sizeof (regions[0 ]), match_region); }void vgic_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr)
virt/kvm/arm/vgic/vgic-mmio.h +5 -0
@@ -192,4 +192,9 @@ u64 vgic_sanitise_shareability(u64 reg);u64 vgic_sanitise_field (u64 reg,u64 field_mask,int field_shift,u64 (*sanitise_fn)(u64 )); +/* Find the proper register handler entry given a certain address offset */ +const struct vgic_register_region * +vgic_find_mmio_region (const struct vgic_register_region *regions, +int nr_regions,unsigned int offset); +#endif
Signature · 1 line
2.5.5
PATCH v6 5/24 KVM: arm64: vgic-its: KVM_DEV_ARM_VGIC_GRP_ITS_REGS group
In reply to Eric AugerThe ITS KVM device exposes a new KVM_DEV_ARM_VGIC_GRP_ITS_REGS group which allows the userspace to save/restore ITS registers.
At this stage the get/set/has operations are not yet implemented.
---
v5 -> v6:
- Add Christoffer's R-b
v4 -> v5:
- Add Marc's A-b
v3 -> v4:
- added Andre's R-b
--- arch/arm/include/uapi/asm/kvm.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 1 + virt/kvm/arm/vgic/vgic-its.c | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-)
arch/arm/include/uapi/asm/kvm.h +1 -0
@@ -192,6 +192,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5#define KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS 6#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7 +#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -212,6 +212,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5#define KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS 6#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7 +#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
virt/kvm/arm/vgic/vgic-its.c +35 -1
@@ -1466,6 +1466,19 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)kfree (its); } +int vgic_its_has_attr_regs (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +int vgic_its_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +u64 *reg,bool is_write) +{ +return -ENXIO ; +} +static int vgic_its_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {
@@ -1482,6 +1495,8 @@ static int vgic_its_has_attr(struct kvm_device *dev,return 0 ; }break ; +case KVM_DEV_ARM_VGIC_GRP_ITS_REGS : +return vgic_its_has_attr_regs (dev, attr); }return -ENXIO ; }
@@ -1521,6 +1536,15 @@ static int vgic_its_set_attr(struct kvm_device *dev,return 0 ; }break ; +case KVM_DEV_ARM_VGIC_GRP_ITS_REGS : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 reg; + +if (get_user (reg, uaddr)) +return -EFAULT ; + +return vgic_its_attr_regs_access (dev, attr, ®, true); + } }return -ENXIO ; }
@@ -1541,10 +1565,20 @@ static int vgic_its_get_attr(struct kvm_device *dev,if (copy_to_user (uaddr, &addr,sizeof (addr)))return -EFAULT ;break ; + } +case KVM_DEV_ARM_VGIC_GRP_ITS_REGS : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 reg; +int ret; + + ret =vgic_its_attr_regs_access (dev, attr, ®, false); +if (ret) +return ret; +return put_user (reg, uaddr); + }default :return -ENXIO ; } - }return 0 ; }
Signature · 1 line
2.5.5
PATCH v6 6/24 KVM: arm/arm64: vgic: expose (un)lock_all_vcpus
In reply to Eric AugerWe need to use those helpers in vgic-its.c so let's expose them in the private vgic header.
---
v5 -> v6:
- Add Christoffer's A-b
v4 -> v5:
- Add Marc's A-b
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 4 ++-- virt/kvm/arm/vgic/vgic.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +2 -2
@@ -259,13 +259,13 @@ static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) } } -static void unlock_all_vcpus (struct kvm *kvm) +void unlock_all_vcpus (struct kvm *kvm) {unlock_vcpus (kvm,atomic_read (&kvm->online_vcpus) -1 ); }/* Returns true if all vcpus were locked, false otherwise */ -static bool lock_all_vcpus (struct kvm *kvm) +bool lock_all_vcpus (struct kvm *kvm) {struct kvm_vcpu *tmp_vcpu;int c;
virt/kvm/arm/vgic/vgic.h +3 -0
@@ -184,4 +184,7 @@ int vgic_init(struct kvm *kvm);int vgic_debug_init (struct kvm *kvm);int vgic_debug_destroy (struct kvm *kvm); +bool lock_all_vcpus (struct kvm *kvm); +void unlock_all_vcpus (struct kvm *kvm); +#endif
Signature · 1 line
2.5.5
PATCH v6 7/24 KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access
In reply to Eric AugerThis patch implements vgic_its_has_attr_regs and vgic_its_attr_regs_access upon the MMIO framework. VGIC ITS KVM device KVM_DEV_ARM_VGIC_GRP_ITS_REGS group becomes functional.
At least GITS_CREADR and GITS_IIDR require to differentiate a guest write action from a user access. As such let's introduce a new uaccess_its_write vgic_register_region callback.
---
v6 -> v6:
- fix GITS_PIDR/CIDR accesses
- remove vgic_data_mmio_bus_to_host/vgic_data_host_to_mmio_bus
- 64b full reg access comment rewording
- vgic_its_attr_regs_access declaration block rework including removal of iodev
v4 -> v5:
- use GITS_TYPER instead of offset 0x8
- uaccess_its_write now can return an error
v3 -> v4:
- remove changes to the REGISTER_ITS_DESC macro. This will be handled in subsequent patch with the introduction of a new REGISTER_ITS_DESC_UACCESS macro
- fix IIDR access and add a comment wrt full length access
- handle endianness
- add kvm lock and vcpus lock
--- virt/kvm/arm/vgic/vgic-its.c | 70 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio.h | 9 ++++-- 2 files changed, 75 insertions(+), 4 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +68 -2
@@ -1469,14 +1469,80 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)int vgic_its_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +const struct vgic_register_region *region; +gpa_t offset = attr->attr; + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) +return -ENXIO ; + +return 0 ; }int vgic_its_attr_regs_access (struct kvm_device *dev,struct kvm_device_attr *attr,u64 *reg,bool is_write) { -return -ENXIO ; +const struct vgic_register_region *region; +struct vgic_its *its; +gpa_t addr, offset; +unsigned int len; +int align, ret =0 ; + + its = dev->private; + offset = attr->attr; + +/* +* Although the spec supports upper/lower 32-bit accesses to +* 64-bit ITS registers, the userspace ABI requires 64-bit +* accesses to all 64-bit wide registers. We therefore only +* support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID +* registers +*/ + align = (offset <GITS_TYPER ) || (offset >=GITS_PIDR4 ) ?0x3 :0x7 ; + +if (offset & align) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (IS_VGIC_ADDR_UNDEF (its->vgic_its_base)) { + ret = -ENXIO ; + goto out; + } + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) { + ret = -ENXIO ; + goto out; + } + +if (!lock_all_vcpus (dev->kvm)) { + ret = -EBUSY ; + goto out; + } + + addr = its->vgic_its_base + offset; + + len = region->access_flags & VGIC_ACCESS_64bit ?8 :4 ; + +if (is_write) { +if (region->uaccess_its_write) + ret = region->uaccess_its_write (dev->kvm, its, addr, + len, *reg); +else + region->its_write (dev->kvm, its, addr, len, *reg); + }else { + *reg = region->its_read (dev->kvm, its, addr, len); + } +unlock_all_vcpus (dev->kvm); +out: +mutex_unlock (&dev->kvm->lock); +return ret; } static int vgic_its_has_attr(struct kvm_device *dev,
virt/kvm/arm/vgic/vgic-mmio.h +7 -2
@@ -36,8 +36,13 @@ struct vgic_register_region { };unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); -void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, -unsigned int len,unsigned long val); +union { +void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); +int (*uaccess_its_write)(struct kvm *kvm,struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val); + }; };extern struct kvm_io_device_ops kvm_io_gic_ops;
Signature · 1 line
2.5.5
PATCH v6 8/24 KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
In reply to Eric AugerGITS_CREADR needs to be restored so let's implement the associated uaccess_write_its callback. The write only is allowed if the its is disabled.
---
v5 -> v6:
- remove usage of update_64bit_reg and check alignment of cmd offset
v4 -> v5:
- keep Stalled bit
- vgic_mmio_uaccess_write_its_creadr can now return an error
v3 -> v4:
- REGISTER_ITS_DESC_UACCESS now introduced in this patch
- we now check the its is disabled
--- virt/kvm/arm/vgic/vgic-its.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +41 -2
@@ -1213,6 +1213,34 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,return extract_bytes (its->creadr, addr &0x7 , len); } +static int vgic_mmio_uaccess_write_its_creadr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 cmd_offset; +int ret =0 ; + +mutex_lock (&its->cmd_lock); + +if (its->enabled) { + ret = -EBUSY ; + goto out; + } + + cmd_offset =ITS_CMD_OFFSET (val); +if ((cmd_offset >=ITS_CMD_BUFFER_SIZE (its->cbaser)) || + (cmd_offset &0x1F )) { + ret = -EINVAL ; + goto out; + } + + its->creadr = val; +out: +mutex_unlock (&its->cmd_lock); +return ret; +} +#define BASER_INDEX (addr) (((addr) / sizeof(u64)) & 0x7) static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, struct vgic_its *its,
@@ -1317,6 +1345,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, .its_write = wr, \ } +#define REGISTER_ITS_DESC_UACCESS (off, rd, wr, uwr, length, acc)\ +{ \ + .reg_offset = off, \ + .len = length, \ + .access_flags = acc, \ + .its_read = rd, \ + .its_write = wr, \ + .uaccess_its_write = uwr, \ +} +static void its_mmio_write_wi (struct kvm *kvm,struct vgic_its *its,gpa_t addr,unsigned int len,unsigned long val) {
@@ -1339,8 +1377,9 @@ static struct vgic_register_region its_registers[] = {REGISTER_ITS_DESC (GITS_CWRITER , vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_CREADR , - vgic_mmio_read_its_creadr, its_mmio_write_wi,8 , +REGISTER_ITS_DESC_UACCESS (GITS_CREADR , + vgic_mmio_read_its_creadr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_creadr,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_BASER, vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Signature · 1 line
2.5.5
PATCH v6 9/24 KVM: arm64: vgic-its: Introduce migration ABI infrastructure
In reply to Eric AugerWe plan to support different migration ABIs, ie. characterizing the ITS table layout format in guest RAM. For example, a new ABI will be needed if vLPIs get supported for nested use case.
So let's introduce an array of supported ABIs (at the moment a single ABI is supported though). The following characteristics are foreseen to vary with the ABI: size of table entries, save/restore operation, the way abi settings are applied.
By default the MAX_ABI_REV is applied on its creation. In subsequent patches we will introduce a way for the userspace to change the ABI in use.
The entry sizes now are set according to the ABI version and not hardcoded anymore.
---
v6 -> v7:
- rename abi into its_table_abi_versions
- kernel doc comments for struct vgic_its_abi
- comment GIC_ENCODE_SZ
- slighly rephrase the commit message
v5: creation and squash KVM: arm64: ITS: Report the ITE size in
GITS_TYPER
--- include/kvm/arm_vgic.h | 3 ++ include/linux/irqchip/arm-gic-v3.h | 5 +++ virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-)
include/kvm/arm_vgic.h +3 -0
@@ -162,6 +162,9 @@ struct vgic_its {u32 creadr;u32 cwriter; +/* migration ABI revision in use */ +u32 abi_rev; +/* Protects the device and collection lists */ struct mutex its_lock;struct list_head device_list;
include/linux/irqchip/arm-gic-v3.h +5 -0
@@ -132,6 +132,9 @@#define GIC_BASER_SHAREABILITY (reg, type) \ (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) +/* encode a size field of width @w containing @n - 1 units */ +#define GIC_ENCODE_SZ (n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) +#define GICR_PROPBASER_SHAREABILITY_SHIFT (10)#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7)#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56)
@@ -232,6 +235,7 @@#define GITS_CTLR_QUIESCENT (1U << 31)#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)
@@ -290,6 +294,7 @@#define GITS_BASER_TYPE (r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7)#define GITS_BASER_ENTRY_SIZE_SHIFT (48)#define GITS_BASER_ENTRY_SIZE (r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) +#define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48)#define GITS_BASER_SHAREABILITY_SHIFT (10)#define GITS_BASER_InnerShareable \ GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)
virt/kvm/arm/vgic/vgic-its.c +88 -4
150 lines not on this page · raw message
Signature · 1 line
2.5.5
PATCH v6 10/24 KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr
In reply to Eric AugerThe GITS_IIDR revision field is used to encode the migration ABI revision. So we need to restore it to check the table layout is readable by the destination.
By writing the IIDR, userspace thus forces the ABI revision to be used and this must be less than or equal to the max revision KVM supports.
---
v5 -> v6:
- fix typos in the commit message
- dont't use update_64bit_reg anymore
v4 -> v5
- rename user_revision into abi_rev and REV into MAX_ABI_REV
- IIDR reports abi_rev set by userspace if any.
- If value set by userspace exceeds the max supported revision, an error is reported.
- add some defines
v4: creation
--- include/linux/irqchip/arm-gic-v3.h | 4 ++++ virt/kvm/arm/vgic/vgic-its.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-)
include/linux/irqchip/arm-gic-v3.h +4 -0
@@ -242,6 +242,10 @@#define GITS_TYPER_PTA (1UL << 19)#define GITS_TYPER_HWCOLLCNT_SHIFT 24 +#define GITS_IIDR_REV_SHIFT 12 +#define GITS_IIDR_REV (r) (((r) >> GITS_IIDR_REV_SHIFT) & 0xf) +#define GITS_IIDR_PRODUCTID_SHIFT 24 +#define GITS_CBASER_VALID (1ULL << 63)#define GITS_CBASER_SHAREABILITY_SHIFT (10)#define GITS_CBASER_INNER_CACHEABILITY_SHIFT (59)
virt/kvm/arm/vgic/vgic-its.c +17 -3
@@ -411,7 +411,20 @@ static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,struct vgic_its *its, gpa_t addr, unsigned int len) { - return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); + return (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | + (its->abi_rev << GITS_IIDR_REV_SHIFT) | IMPLEMENTER_ARM; +} + +static int vgic_mmio_uaccess_write_its_iidr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 rev =GITS_IIDR_REV (val); + +if (rev >MAX_ABI_REV ) +return -EINVAL ; +return vgic_its_set_abi (its, rev); } static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
@@ -1393,8 +1406,9 @@ static struct vgic_register_region its_registers[] = {REGISTER_ITS_DESC (GITS_CTLR , vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr,4 , VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_IIDR , - vgic_mmio_read_its_iidr, its_mmio_write_wi,4 , +REGISTER_ITS_DESC_UACCESS (GITS_IIDR , + vgic_mmio_read_its_iidr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_iidr,4 , VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_TYPER, vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Signature · 1 line
2.5.5
PATCH v6 11/24 KVM: arm64: vgic-its: Interpret MAPD Size field and check related errors
In reply to Eric AugerUp to now the MAPD's ITT size field has been ignored. It encodes the number of eventid bit minus 1. It should be used to check the eventid when a MAPTI command is issued on a device. Let's store the number of eventid bits in the its_device and do the check on MAPTI. Also make sure the ITT size field does not exceed the GITS_TYPER IDBITS field.
---
v5 -> v6:
- add Christoffer's R-b
- rename nb_eventid_bits into num_eventid_bits
v4 -> v5:
- its_cmd_get_size macro now returns the actual number of eventid bits
- use GIC_ENCODE_SZ macro to encode ID_bits
v3 -> v4:
- VITS_TYPER_IDBITS set to 16 to be homogeneous with VITS_ESZ definition and correct users
- nb_eventid_bits correspond to the actual number of eventid bits
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
include/linux/irqchip/arm-gic-v3.h +2 -0
@@ -346,9 +346,11 @@#define E_ITS_INT_UNMAPPED_INTERRUPT 0x010307#define E_ITS_CLEAR_UNMAPPED_INTERRUPT 0x010507#define E_ITS_MAPD_DEVICE_OOR 0x010801 +#define E_ITS_MAPD_ITTSIZE_OOR 0x010802#define E_ITS_MAPC_PROCNUM_OOR 0x010902#define E_ITS_MAPC_COLLECTION_OOR 0x010903#define E_ITS_MAPTI_UNMAPPED_DEVICE 0x010a04 +#define E_ITS_MAPTI_ID_OOR 0x010a05#define E_ITS_MAPTI_PHYSICALID_OOR 0x010a06#define E_ITS_INV_UNMAPPED_INTERRUPT 0x010c07#define E_ITS_INVALL_UNMAPPED_COLLECTION 0x010d09
virt/kvm/arm/vgic/vgic-its.c +14 -1
@@ -105,6 +105,7 @@ struct its_device {/* the head for the list of ITTEs */ struct list_head itt_head; +u32 num_eventid_bits;u32 device_id; };
@@ -201,6 +202,8 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define GIC_LPI_OFFSET 8192 +#define VITS_TYPER_IDBITS 16 +/* * Finds and returns a collection in the ITS collection table. * Must be called with the its_lock mutex held.
@@ -401,7 +404,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,* DevBits low - as least for the time being. */ reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; - reg |=0x0f <<GITS_TYPER_IDBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (VITS_TYPER_IDBITS ,5 ) <<GITS_TYPER_IDBITS_SHIFT ; reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;return extract_bytes (reg, addr &7 , len);
@@ -569,6 +572,7 @@ static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)#define its_cmd_get_command (cmd) its_cmd_mask_field(cmd, 0, 0, 8)#define its_cmd_get_deviceid (cmd) its_cmd_mask_field(cmd, 0, 32, 32) +#define its_cmd_get_size (cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1)#define its_cmd_get_id (cmd) its_cmd_mask_field(cmd, 1, 0, 32)#define its_cmd_get_physical_id (cmd) its_cmd_mask_field(cmd, 1, 32, 32)#define its_cmd_get_collection (cmd) its_cmd_mask_field(cmd, 2, 0, 16)
@@ -759,6 +763,9 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,if (!device)return E_ITS_MAPTI_UNMAPPED_DEVICE ; +if (event_id >=BIT_ULL (device->num_eventid_bits)) +return E_ITS_MAPTI_ID_OOR ; +if (its_cmd_get_command (its_cmd) ==GITS_CMD_MAPTI ) lpi_nr =its_cmd_get_physical_id (its_cmd);else
@@ -839,11 +846,15 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, {u32 device_id =its_cmd_get_deviceid (its_cmd);bool valid =its_cmd_get_validbit (its_cmd); +u8 num_eventid_bits =its_cmd_get_size (its_cmd);struct its_device *device;if (!vgic_its_check_id (its, its->baser_device_table, device_id))return E_ITS_MAPD_DEVICE_OOR ; +if (valid && num_eventid_bits >VITS_TYPER_IDBITS ) +return E_ITS_MAPD_ITTSIZE_OOR ; + device =find_its_device (its, device_id);/*
@@ -866,6 +877,8 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,return -ENOMEM ; device->device_id = device_id; + device->num_eventid_bits = num_eventid_bits; +INIT_LIST_HEAD (&device->itt_head);list_add_tail (&device->dev_list, &its->device_list);
Signature · 1 line
2.5.5
PATCH v6 12/24 KVM: arm64: vgic-its: Interpret MAPD ITT_addr field
In reply to Eric AugerUp to now the MAPD ITT_addr had been ignored. We will need it for save/restore. Let's record it in the its_device struct.
---
v5 -> v6:
- fix its_cmd_get_ittaddr and pass 44 to its_cmd_mask_field
v4 -> v5:
- its_cmd_get_ittaddr macro now returns the actual ITT GPA
v3 -> v4:
- in vgic_its_cmd_handle_mapd, itt_addr directly is shifted
- correct ittaddr bitmask to support 48bit GPA
--- virt/kvm/arm/vgic/vgic-its.c | 4 ++++ 1 file changed, 4 insertions(+)
virt/kvm/arm/vgic/vgic-its.c +4 -0
@@ -106,6 +106,7 @@ struct its_device {/* the head for the list of ITTEs */ struct list_head itt_head;u32 num_eventid_bits; +gpa_t itt_addr;u32 device_id; };
@@ -576,6 +577,7 @@ static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)#define its_cmd_get_id (cmd) its_cmd_mask_field(cmd, 1, 0, 32)#define its_cmd_get_physical_id (cmd) its_cmd_mask_field(cmd, 1, 32, 32)#define its_cmd_get_collection (cmd) its_cmd_mask_field(cmd, 2, 0, 16) +#define its_cmd_get_ittaddr (cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8)#define its_cmd_get_target_addr (cmd) its_cmd_mask_field(cmd, 2, 16, 32)#define its_cmd_get_validbit (cmd) its_cmd_mask_field(cmd, 2, 63, 1)
@@ -847,6 +849,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,u32 device_id =its_cmd_get_deviceid (its_cmd);bool valid =its_cmd_get_validbit (its_cmd);u8 num_eventid_bits =its_cmd_get_size (its_cmd); +gpa_t itt_addr =its_cmd_get_ittaddr (its_cmd);struct its_device *device;if (!vgic_its_check_id (its, its->baser_device_table, device_id))
@@ -878,6 +881,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, device->device_id = device_id; device->num_eventid_bits = num_eventid_bits; + device->itt_addr = itt_addr;INIT_LIST_HEAD (&device->itt_head);
Signature · 1 line
2.5.5
PATCH v6 14/24 KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Eric Augerthis new helper synchronizes the irq pending_latch with the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.
As we need the PENDBASER_ADDRESS() in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same for PROPBASER_ADDRESS with full field length of [51:12].
---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)
include/linux/irqchip/arm-gic-v3.h +2 -0
@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)
virt/kvm/arm/vgic/vgic-its.c +2 -4
@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192
@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;
@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;
virt/kvm/arm/vgic/vgic-v3.c +44 -0
50 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -157,6 +157,7 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v3_enable (struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm); +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);
Signature · 1 line
2.5.5
PATCH v6 15/24 KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
In reply to Eric AugerWhen creating the lpi we now ask the redistributor what is the state of the LPI (priority, enabled, pending).
---
v6: creation
--- virt/kvm/arm/vgic/vgic-its.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +24 -11
@@ -38,6 +38,8 @@static int vgic_its_set_abi (struct vgic_its *its,int rev);static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq, +struct kvm_vcpu *filter_vcpu);/* * Creates a new (reference to a) struct vgic_irq for a given LPI.
@@ -46,10 +48,12 @@ static const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its);* If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure. */ -static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid) +static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid, +struct kvm_vcpu *vcpu) {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm,NULL , intid), *oldirq; +int ret;/* In this case there is no put, since we keep the reference. */ if (irq)
@@ -66,6 +70,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) irq->config =VGIC_CONFIG_EDGE ;kref_init (&irq->refcount); irq->intid = intid; + irq->target_vcpu = vcpu;spin_lock (&dist->lpi_list_lock);
@@ -97,6 +102,19 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) out_unlock:spin_unlock (&dist->lpi_list_lock); +/* +* We "cache" the configuration table entries in out struct vgic_irq's. +* However we only have those structs for mapped IRQs, so we read in +* the respective config data from memory here upon mapping the LPI. +*/ + ret =update_lpi_config (kvm, irq,NULL ); +if (ret) +return ERR_PTR (ret); + + ret =vgic_v3_lpi_sync_pending_status (kvm, irq); +if (ret) +return ERR_PTR (ret); +return irq; }
@@ -769,6 +787,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct its_ite *ite; +struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;
@@ -814,7 +833,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, ite->collection = collection; ite->lpi = lpi_nr; - irq =vgic_add_lpi (kvm, lpi_nr); +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_nr, vcpu);if (IS_ERR (irq)) {if (new_coll)vgic_its_free_collection (its, coll_id);
@@ -823,15 +845,6 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, } ite->irq = irq; -update_affinity_ite (kvm, ite); - -/* -* We "cache" the configuration table entries in out struct vgic_irq's. -* However we only have those structs for mapped IRQs, so we read in -* the respective config data from memory here upon mapping the LPI. -*/ -update_lpi_config (kvm, ite->irq,NULL ); -return 0 ; }
Signature · 1 line
2.5.5
PATCH v6 16/24 KVM: arm64: vgic-its: KVM_DEV_ARM_ITS_SAVE/RESTORE_TABLES
In reply to Eric AugerIntroduce new attributes in KVM_DEV_ARM_VGIC_GRP_CTRL group:
- KVM_DEV_ARM_ITS_SAVE_TABLES: saves the ITS tables into guest RAM
- KVM_DEV_ARM_ITS_RESTORE_TABLES: restores them into VGIC internal structures.
We hold the vcpus lock during the save and restore to make sure no vcpu is running.
At this stage the functionality is not yet implemented. Only the skeleton is put in place.
---
v5 -> v6:
- remove the pending table sync from the ITS table restore
v4 -> v5:
- use KVM_DEV_ARM_ITS_SAVE_TABLES and KVM_DEV_ARM_ITS_RESTORE_TABLES
- rename *flush* into *save*
- call its_sync_lpi_pending_table at the end of restore
- use abi framework
v3 -> v4:
- pass kvm struct handle to vgic_its_flush/restore_pending_tables
- take the kvm lock and vcpu locks
- ABI revision check
- check attr->attr is null
v1 -> v2:
- remove useless kvm parameter
--- arch/arm/include/uapi/asm/kvm.h | 4 +- arch/arm64/include/uapi/asm/kvm.h | 4 +- virt/kvm/arm/vgic/vgic-its.c | 101 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 103 insertions(+), 6 deletions(-)
arch/arm/include/uapi/asm/kvm.h +3 -1
@@ -199,7 +199,9 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 -#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_ITS_SAVE_TABLES 1 +#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24
arch/arm64/include/uapi/asm/kvm.h +3 -1
@@ -219,7 +219,9 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 -#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_ITS_SAVE_TABLES 1 +#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0
virt/kvm/arm/vgic/vgic-its.c +97 -4
136 lines not on this page · raw message
Signature · 1 line
2.5.5
PATCH v6 17/24 KVM: arm64: vgic-its: vgic_its_alloc_ite/device
In reply to Eric AugerAdd two new helpers to allocate an its ite and an its device. This will avoid duplication on restore path.
---
v5 -> v6:
- remove Marc's A-b
- both functions now return the newly allocated struct
v4 -> v5:
- add Marc's A-b
v3 -> v4:
- fix allocation
- add comment about its_lock mutex hold
v1 -> v2:
- report itt_size fix and remove ITE_SIZE
- s/itte/ite/g
--- virt/kvm/arm/vgic/vgic-its.c | 68 ++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 21 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +47 -21
@@ -776,6 +776,25 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)kfree (collection); } +/* Must be called with its_lock mutex held */ +static struct its_ite *vgic_its_alloc_ite (struct its_device *device, +struct its_collection *collection, +u32 lpi_id,u32 event_id) +{ +struct its_ite *ite; + + ite =kzalloc (sizeof (*ite),GFP_KERNEL ); +if (!ite) +return ERR_PTR (-ENOMEM ); + + ite->event_id = event_id; + ite->collection = collection; + ite->lpi = lpi_id; + +list_add_tail (&ite->ite_list, &device->itt_head); +return ite; +} +/* * The MAPTI and MAPI commands map LPIs to ITTEs. * Must be called with its_lock mutex held.
@@ -790,8 +809,8 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ; -int lpi_nr;struct vgic_irq *irq; +int lpi_nr; device =find_its_device (its, device_id);if (!device)
@@ -820,19 +839,13 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, new_coll = collection; } - ite =kzalloc (sizeof (struct its_ite ),GFP_KERNEL ); -if (!ite) { + ite =vgic_its_alloc_ite (device, collection, lpi_nr, event_id); +if (IS_ERR (ite)) {if (new_coll)vgic_its_free_collection (its, coll_id); -return -ENOMEM ; +return PTR_ERR (ite); } - ite->event_id = event_id; -list_add_tail (&ite->ite_list, &device->itt_head); - - ite->collection = collection; - ite->lpi = lpi_nr; -if (its_is_collection_mapped (collection)) vcpu =kvm_get_vcpu (kvm, collection->target_addr);
@@ -865,6 +878,26 @@ static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)kfree (device); } +/* Must be called with its_lock mutex held */ +static struct its_device *vgic_its_alloc_device (struct vgic_its *its, +u32 device_id,gpa_t itt_addr, +u8 num_eventid_bits) +{ +struct its_device *device; + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) +return ERR_PTR (-ENOMEM ); + + device->device_id = device_id; + device->itt_addr = itt_addr; + device->num_eventid_bits = num_eventid_bits; +INIT_LIST_HEAD (&device->itt_head); + +list_add_tail (&device->dev_list, &its->device_list); +return device; +} +/* * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). * Must be called with the its_lock mutex held.
@@ -901,17 +934,10 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,if (!valid)return 0 ; - device =kzalloc (sizeof (struct its_device ),GFP_KERNEL ); -if (!device) -return -ENOMEM ; - - device->device_id = device_id; - device->num_eventid_bits = num_eventid_bits; - device->itt_addr = itt_addr; - -INIT_LIST_HEAD (&device->itt_head); - -list_add_tail (&device->dev_list, &its->device_list); + device =vgic_its_alloc_device (its, device_id, itt_addr, + num_eventid_bits); +if (IS_ERR (device)) +return PTR_ERR (device);return 0 ; }
Signature · 1 line
2.5.5
PATCH v6 18/24 KVM: arm64: vgic-its: Add infrastructure for table lookup
In reply to Eric AugerAdd a generic scan_its_table() helper whose role consists in scanning a contiguous table located in guest RAM and applying a callback on each entry. Entries can be handled as linked lists since the callback may return an id offset to the next entry and also indicate whether the entry is the last one.
Helper functions also are added to compute the device/event ID offset to the next DTE/ITE.
compute_next_devid_offset, compute_next_eventid_offset and scan_table will become static in subsequent patches
---
v5 -> v6:
- add comments about 2d level table scan as suggested by Christoffer
- s/lookup_table/scan_its_table
- scan_table now return 0 if last elt found and +1 if not found
- use int for id
- simplify compute_next_devid/eventid_offset
v4 -> v5:
- use kvm_read_guest
v3 -> v4:
- remove static to avoid compilation warning
- correct size computation in looup_table()
- defines now encode the number of bits used for devid and eventid offsets
- use BIT() - 1 to encode the max offets
--- virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)
virt/kvm/arm/vgic/vgic-its.c +92 -0
@@ -221,6 +221,8 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define VITS_TYPER_IDBITS 16#define VITS_TYPER_DEVBITS 16 +#define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) +#define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1)/* * Finds and returns a collection in the ITS collection table.
@@ -1694,6 +1696,96 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } +u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +{ +struct its_device *next; +u32 next_offset; + +if (list_is_last (&dev->dev_list, h)) +return 0 ; + next =list_next_entry (dev, dev_list); + next_offset = next->device_id - dev->device_id; + +return min_t (u32, next_offset,VITS_DTE_MAX_DEVID_OFFSET ); +} + +u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) +{ +struct its_ite *next; +u32 next_offset; + +if (list_is_last (&ite->ite_list, h)) +return 0 ; + next =list_next_entry (ite, ite_list); + next_offset = next->event_id - ite->event_id; + +return min_t (u32, next_offset,VITS_ITE_MAX_EVENTID_OFFSET ); +} + +/** +* entry_fn_t - Callback called on a table entry restore path +* @its: its handle +* @id: id of the entry +* @entry: pointer to the entry +* @opaque: pointer to an opaque data +* +* Return: < 0 on error, 0 if last element was identified, id offset to next +* element otherwise +*/ +typedef int (*entry_fn_t )(struct vgic_its *its,u32 id,void *entry, +void *opaque); + +/** +* scan_its_table - Scan a contiguous table in guest RAM and applies a function +* to each entry +* +* @its: its handle +* @base: base gpa of the table +* @size: size of the table in bytes +* @esz: entry size in bytes +* @start_id: the ID of the first entry in the table +* (non zero for 2d level tables) +* @fn: function to apply on each entry +* +* Return: < 0 on error, 0 if last element was identified, 1 otherwise +* (the last element may not be found on second level tables) +*/ +int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) +{ +void *entry =kzalloc (esz,GFP_KERNEL ); +struct kvm *kvm = its->dev->kvm; +unsigned long len = size; +int id = start_id; +gpa_t gpa = base; +int ret; + +while (len >0 ) { +int next_offset; +size_t byte_offset; + + ret =kvm_read_guest (kvm, gpa, entry, esz); +if (ret) + goto out; + + next_offset =fn (its, id, entry, opaque); +if (next_offset <=0 ) { + ret = next_offset; + goto out; + } + + byte_offset = next_offset * esz; + id += next_offset; + gpa += byte_offset; + len -= byte_offset; + } + ret =1 ; + +out: +kfree (entry); +return ret; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM
Signature · 1 line
2.5.5
PATCH v6 24/24 KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
In reply to Eric AugerThis patch adds a new attribute to GICV3 KVM device KVM_DEV_ARM_VGIC_GRP_CTRL group. This allows userspace to flush all GICR pending tables into guest RAM.
---
v5 -> v6:
- fix stored
- GICR_PEND/PROPBASER_ADDRESS were already introduced
v4 -> v5:
- move pending table save code/ctrl into VGICv3 instead of ITS
- remove useless gpa_t cast
- use the same optimization as in its_sync_lpi_pending_table
v3 -> v4:
- remove the wrong comment about locking
- pass kvm struct instead of its handle
- add comment about restore method
- remove GITR_PENDABASER.PTZ check
- continue if target_vcpu == NULL
- new locking strategy
v1 -> v2:
- do not care about the 1st KB which should be zeroed according to the spec.
--- arch/arm/include/uapi/asm/kvm.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 1 + include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-kvm-device.c | 20 +++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 51 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 6 files changed, 75 insertions(+)
arch/arm/include/uapi/asm/kvm.h +1 -0
@@ -202,6 +202,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -222,6 +222,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0
include/linux/irqchip/arm-gic-v3.h +1 -0
@@ -186,6 +186,7 @@#define GICR_PENDBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWaWb)#define GICR_PENDBASER_PTZ BIT_ULL(62) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))/* * Re-Distributor registers, offsets from SGI_base
virt/kvm/arm/vgic/vgic-kvm-device.c +20 -0
@@ -580,6 +580,24 @@ static int vgic_v3_set_attr(struct kvm_device *dev, reg = tmp32;return vgic_v3_attr_regs_access (dev, attr, ®, true); } +case KVM_DEV_ARM_VGIC_GRP_CTRL : { +int ret; + +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +mutex_lock (&dev->kvm->lock); + +if (!lock_all_vcpus (dev->kvm)) { +mutex_unlock (&dev->kvm->lock); +return -EBUSY ; + } + ret =vgic_v3_save_pending_tables (dev->kvm); +unlock_all_vcpus (dev->kvm); +mutex_unlock (&dev->kvm->lock); +return ret; + } +break ; + } }return -ENXIO ; }
@@ -658,6 +676,8 @@ static int vgic_v3_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_ARM_VGIC_CTRL_INIT :return 0 ; +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +return 0 ; } }return -ENXIO ;
virt/kvm/arm/vgic/vgic-v3.c +51 -0
57 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -181,6 +181,7 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm);int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq); +int vgic_v3_save_pending_tables (struct kvm *kvm);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);
Signature · 1 line
2.5.5
Re: [PATCH v6 02/24] KVM: arm/arm64: Add GICV3 pending table save API documentation
In reply to Eric AugerQuoted 5 lines · Eric Auger
Add description for how to save GICV3 LPI pending bit into guest RAM pending tables.
Signed-off-by Eric Auger <[email protected]>
Quoted 31 lines
---
v5: creation--- Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 6 ++++++ 1 file changed, 6 insertions(+)Documentation/virtual/kvm/devices/arm-vgic-v3.txt +6
-0@@ -167,11 +167,17 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the VGIC, no additional parameter in kvm_device_attr.addr. + KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES + save all LPI pending bits into guest RAM pending tables. + + The first kB of the pending table is not altered by this operation. Errors: -ENXIO: VGIC not properly configured as required prior to calling this attribute -ENODEV: no online VCPU -ENOMEM: memory shortage when allocating vgic internal data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running KVM_DEV_ARM_VGIC_GRP_LEVEL_INFOSignature · 1 line
2.5.5
Re: [PATCH v6 01/24] KVM: arm/arm64: Add ITS save/restore API documentation
In reply to Eric AugerQuoted 53 lines · Eric Auger
Add description for how to access ITS registers and how to save/restore ITS tables into/from memory.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- add restoration ordering
- 256B -> 256 Byte aligned
- DTE Size is number of bits for the EVENTID
- s/GITS_READR/GITS_CREADRv4 -> v5:
- take into account Christoffer's comments
- pending table save on GICV3 side nowv3 -> v4:
- take into account Peter's comments:
- typos
-KVM_DEV_ARM_VGIC_GRP_ITS_TABLESkvm_device_attr= 0
- add a validity bit in DTE
- document all fields in CTE and ITE
- document ABI revision
- take into account Andre's comments:
- document restrictions aboutGITS_CREADRwriting andGITS_IIDR
- document-EBUSYerror if one or more VCPUS are runnning
- document 64b registers only can be accessed with 64b access
-itt_addrfield matches bits [51:8] of theitt_addrv1 -> v2:
- DTE and ITE now are 8 bytes
- DTE and ITE now indexed by deviceid/eventid
- use ITE name instead of ITTE
- mentionsITT_addrmatches bits [51:8] of the actual address
- mentions LE layout--- Documentation/virtual/kvm/devices/arm-vgic-its.txt | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+)Documentation/virtual/kvm/devices/arm-vgic-its.txt +6
-0@@ -32,7 +32,124 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the ITS, no additional parameter in kvm_device_attr.addr. + + KVM_DEV_ARM_ITS_SAVE_TABLES + save the ITS table data into guest RAM, at the location provisioned + by the guest in corresponding registers/table entries. + + The layout of the tables in guest memory defines an ABI. The entries
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 50
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +10
-0+ are laid out in little endian format as described in the last paragraph. + + KVM_DEV_ARM_ITS_RESTORE_TABLES + restore the ITS tables from guest RAM to ITS internal structures. + + The GICV3 must be restored before the ITS and all ITS registers but + the GITS_CTLR must be restored before restoring the ITS tables. + + The GITS_IIDR read-only register must also be restored before + the table restore as the IIDR revision field encodes the ABI revision.
the table restore? "before calling KVM_DEV_ARM_ITS_RESTORE_TABLES" ? (there's a potential confusion between calling this ioctl and simply restoring the memory containing the tables).
Quoted 18 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +14
-0+ + The expected ordering when restoring the GICv3/ITS is described in section + "ITS Restoration Sequence". + Errors: -ENXIO: ITS not properly configured as required prior to setting this attribute -ENOMEM: Memory shortage when allocating ITS internal data + -EINVAL: Inconsistent restored data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running + + KVM_DEV_ARM_VGIC_GRP_ITS_REGS + Attributes: + The attr field of kvm_device_attr encodes the offset of the + ITS register, relative to the ITS control frame base address + (ITS_base). +
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 78
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +10
-0+ kvm_device_attr.addr points to a __u64 value whatever the width + of the addressed register (32/64 bits). 64 bit registers can only + be accessed with full length. + + Writes to read-only registers are ignored by the kernel except for: + - GITS_CREADR. It needs to be restored otherwise commands in the queue + will be re-executed after restoring CWRITER. GITS_CREADR must be + restored before restoring the GITS_CTLR which is likely to enable the + ITS. Also it needs to be restored after GITS_CBASER since a write to + GITS_CBASER resets GITS_CREADR.
uber nit: s/needs to be/must be/g in all occurences above.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 79
Quoted 1 line
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ - GITS_IIDR. Its Revision field encodes the table layout ABI revision.
s/Its/The/
the revision of the table layout ABI.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 80
Quoted 1 line
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ In the future we might implement direct injection of virtual LPIS.
LPIs
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 82
Quoted 2 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +2
-0+ This will require an upgrade of the table layout and an evolution of + the ABI. GITS_IIDR must be restored before the table restoration.
before calling KVM_DEV_ARM_ITS_RESTORE_TABLES.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 92
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +10
-0+ + For other registers, getting or setting a register has the same + effect as reading/writing the register on real hardware. + Errors: + -ENXIO: Offset does not correspond to any supported register + -EFAULT: Invalid user pointer for attr->addr + -EINVAL: Offset is not 64-bit aligned + -EBUSY: one or more VCPUS are running + + ITS Restoration Sequence:
s/Restoration/restore/
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 97
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +5
-0+ ------------------------- + +The following ordering must be followed when restoring the GIC and the ITS: +a) restore all guest memory and create vcpus +b) initialize the GIC and ITS (KVM_DEV_ARM_VGIC_CTRL_INIT)
are these really coupled or can you do everything related to the gic first, and then everything related to the ITS afterwards?
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 98
Quoted 1 line
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+c) provide GIC and ITS base addresses (KVM_DEV_ARM_VGIC_GRP_ADDR)
same
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 105
Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +7
-0+d) restore all redistributors +e) restore the ITS in the following order: + 1. Restore GITS_CBASER + 2. Restore all other GITS_ registers, except GITS_CTLR! + 3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES) + 4. Restore GITS_CTLR +f) start vcpus
I would omit (f) from here.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 114
Quoted 9 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +9
-0+ + ITS Table ABI REV0: + ------------------- + + Revision 0 of the ABI only supports physical LPIs. + + The device table and ITT are indexed by the deviceid and eventid, + respectively. The collection table is not indexed by collectionid: + CTE are written in the table in the order of collection creation. All
nit: CTEs
Quoted 44 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +41
-0+ entries are 8 bytes. + + Device Table Entry (DTE): + + bits: | 63| 62 ... 49 | 48 ... 5 | 4 ... 0 | + values: | V | next | ITT_addr | Size | + + where; + - V indicates whether the entry is valid. If not, other fields + are not meaningful. + - next: equals to 0 if this entry is the last one; otherwise it + corresponds to the deviceid offset to the next DTE, capped by + 2^14 -1. + - ITT_addr matches bits [51:8] of the ITT address (256 Byte aligned). + - Size specifies the supported number of bits for the eventid, + minus one + + Collection Table Entry (CTE): + + bits: | 63| 62 .. 52 | 51 ... 16 | 15 ... 0 | + values: | V | RES0 | RDBase | ICID | + + where: + - V indicates whether the entry is valid. If not, other fields are + not meaningful. + - RES0: reserved field with Should-Be-Zero-or-Preserved behavior. + - RDBase is the PE number (GICR_TYPER.Processor_Number semantic), + - ICID is the collection ID + + Interrupt Translation Entry (ITE): + + bits: | 63 ... 48 | 47 ... 16 | 15 ... 0 | + values: | next | pINTID | ICID | + + where: + - next: equals to 0 if this entry is the last one; otherwise it corresponds + to the eventid offset to the next ITE capped by 2^16 -1. + - pINTID is the physical LPI ID; if zero, it means the entry is not valid + and other fields are not meaningful. + - ICID is the collection ID +Signature · 1 line
2.5.5
The only outstanding issue here is the restore order part. I don't wont to specify a stronger order than absolutely necessary here.
Otherwise this generally looks good.
Thanks,
-Christoffer
Re: [PATCH v6 07/24] KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access
In reply to Eric AugerQuoted 39 lines · Eric Auger
This patch implements
vgic_its_has_attr_regsandvgic_its_attr_regs_accessupon the MMIO framework. VGIC ITS KVM deviceKVM_DEV_ARM_VGIC_GRP_ITS_REGSgroup becomes functional.At least
GITS_CREADRandGITS_IIDRrequire to differentiate a guest write action from a user access. As such let's introduce a newuaccess_its_writevgic_register_regioncallback.Signed-off-by Eric Auger <[email protected]>---
v6 -> v6:
- fix GITS_PIDR/CIDR accesses
- remove vgic_data_mmio_bus_to_host/vgic_data_host_to_mmio_bus
- 64b full reg access comment rewording
-vgic_its_attr_regs_accessdeclaration block rework including removal of iodevv4 -> v5:
- useGITS_TYPERinstead of offset0x8
-uaccess_its_writenow can return an errorv3 -> v4:
- remove changes to theREGISTER_ITS_DESCmacro. This will be handled in subsequent patch with the introduction of a newREGISTER_ITS_DESC_UACCESSmacro
- fix IIDR access and add a comment wrt full length access
- handle endianness
- add kvm lock and vcpus lock--- virt/kvm/arm/vgic/vgic-its.c | 70 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio.h | 9 ++++-- 2 files changed, 75 insertions(+), 4 deletions(-)virt/kvm/arm/vgic/vgic-its.c +0
-0
virt/kvm/arm/vgic/vgic-its.c static void vgic_its_destroy(struct kvm_device *kvm_dev) · line 1474
Quoted 7 lines
virt/kvm/arm/vgic/vgic-its.c +3
-1@@ -1469,14 +1469,80 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)int vgic_its_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +const struct vgic_register_region *region; +gpa_t offset = attr->attr; +
Shouldn't you check the alignment in the same way you do it below? It seems a bit odd that one succeeds while the other fails with the same input.
Quoted 86 lines
virt/kvm/arm/vgic/vgic-its.c +65
-1+ region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) +return -ENXIO ; + +return 0 ; }int vgic_its_attr_regs_access (struct kvm_device *dev,struct kvm_device_attr *attr,u64 *reg,bool is_write) { -return -ENXIO ; +const struct vgic_register_region *region; +struct vgic_its *its; +gpa_t addr, offset; +unsigned int len; +int align, ret =0 ; + + its = dev->private; + offset = attr->attr; + +/* +* Although the spec supports upper/lower 32-bit accesses to +* 64-bit ITS registers, the userspace ABI requires 64-bit +* accesses to all 64-bit wide registers. We therefore only +* support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID +* registers +*/ + align = (offset <GITS_TYPER ) || (offset >=GITS_PIDR4 ) ?0x3 :0x7 ; + +if (offset & align) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (IS_VGIC_ADDR_UNDEF (its->vgic_its_base)) { + ret = -ENXIO ; + goto out; + } + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) { + ret = -ENXIO ; + goto out; + } + +if (!lock_all_vcpus (dev->kvm)) { + ret = -EBUSY ; + goto out; + } + + addr = its->vgic_its_base + offset; + + len = region->access_flags & VGIC_ACCESS_64bit ?8 :4 ; + +if (is_write) { +if (region->uaccess_its_write) + ret = region->uaccess_its_write (dev->kvm, its, addr, + len, *reg); +else + region->its_write (dev->kvm, its, addr, len, *reg); + }else { + *reg = region->its_read (dev->kvm, its, addr, len); + } +unlock_all_vcpus (dev->kvm); +out: +mutex_unlock (&dev->kvm->lock); +return ret; } static int vgic_its_has_attr(struct kvm_device *dev,virt/kvm/arm/vgic/vgic-mmio.h +0
-2@@ -36,8 +36,13 @@ struct vgic_register_region { };unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); -void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, -unsigned int len,unsigned long val);
virt/kvm/arm/vgic/vgic-mmio.h struct vgic_register_region { · line 48
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h +7
-0+union { +void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); +int (*uaccess_its_write)(struct kvm *kvm,struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val); + }; };extern struct kvm_io_device_ops kvm_io_gic_ops;
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 08/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
In reply to Eric AugerQuoted 37 lines · Eric Auger
GITS_CREADRneeds to be restored so let's implement the associateduaccess_write_itscallback. The write only is allowed if the its is disabled.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove usage ofupdate_64bit_regand check alignment of cmd offsetv4 -> v5:
- keep Stalled bit
-vgic_mmio_uaccess_write_its_creadrcan now return an errorv3 -> v4:
-REGISTER_ITS_DESC_UACCESSnow introduced in this patch
- we now check the its is disabled--- virt/kvm/arm/vgic/vgic-its.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +8
-0@@ -1213,6 +1213,34 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,return extract_bytes (its->creadr, addr &0x7 , len); } +static int vgic_mmio_uaccess_write_its_creadr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 cmd_offset; +int ret =0 ; +
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, · line 1233
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+mutex_lock (&its->cmd_lock); + +if (its->enabled) { + ret = -EBUSY ; + goto out; + } + + cmd_offset =ITS_CMD_OFFSET (val); +if ((cmd_offset >=ITS_CMD_BUFFER_SIZE (its->cbaser)) || + (cmd_offset &0x1F )) {
nit: In general, and for alignments that aren't completely obvious, you can write something like IS_ALIGNED(cmd_offset, sizeof(struct its_cmd)).
Unfortunately, we don't have a struct its_cmd, and maybe we should fix that at a later time.
Quoted 33 lines
virt/kvm/arm/vgic/vgic-its.c +20
-0+ ret = -EINVAL ; + goto out; + } + + its->creadr = val; +out: +mutex_unlock (&its->cmd_lock); +return ret; +} +#define BASER_INDEX (addr) (((addr) / sizeof(u64)) & 0x7) static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, struct vgic_its *its,@@ -1317,6 +1345,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, .its_write = wr, \ } +#define REGISTER_ITS_DESC_UACCESS (off, rd, wr, uwr, length, acc)\ +{ \ + .reg_offset = off, \ + .len = length, \ + .access_flags = acc, \ + .its_read = rd, \ + .its_write = wr, \ + .uaccess_its_write = uwr, \ +} +static void its_mmio_write_wi (struct kvm *kvm,struct vgic_its *its,gpa_t addr,unsigned int len,unsigned long val) {@@ -1339,8 +1377,9 @@ static struct vgic_register_region its_registers[] = { REGISTER_ITS_DESC(GITS_CWRITER,
virt/kvm/arm/vgic/vgic-its.c static struct vgic_register_region its_registers[] = { · line 1385
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +3
-2vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_CREADR , - vgic_mmio_read_its_creadr, its_mmio_write_wi,8 , +REGISTER_ITS_DESC_UACCESS (GITS_CREADR , + vgic_mmio_read_its_creadr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_creadr,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_BASER, vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Otherwise:
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 09/24] KVM: arm64: vgic-its: Introduce migration ABI infrastructure
In reply to Eric AugerQuoted 82 lines · Eric Auger
We plan to support different migration ABIs, ie. characterizing the ITS table layout format in guest RAM. For example, a new ABI will be needed if vLPIs get supported for nested use case.
So let's introduce an array of supported ABIs (at the moment a single ABI is supported though). The following characteristics are foreseen to vary with the ABI: size of table entries, save/restore operation, the way abi settings are applied.
By default the
MAX_ABI_REVis applied on its creation. In subsequent patches we will introduce a way for the userspace to change the ABI in use.The entry sizes now are set according to the ABI version and not hardcoded anymore.
Signed-off-by Eric Auger <[email protected]>---
v6 -> v7:
- rename abi intoits_table_abi_versions
- kernel doc comments for structvgic_its_abi
- commentGIC_ENCODE_SZ
- slighly rephrase the commit messagev5: creation and squash KVM: arm64: ITS: Report the ITE size in
GITS_TYPER--- include/kvm/arm_vgic.h | 3 ++ include/linux/irqchip/arm-gic-v3.h | 5 +++ virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-)include/kvm/arm_vgic.h +3
-0@@ -162,6 +162,9 @@ struct vgic_its {u32 creadr;u32 cwriter; +/* migration ABI revision in use */ +u32 abi_rev; +/* Protects the device and collection lists */ struct mutex its_lock;struct list_head device_list;include/linux/irqchip/arm-gic-v3.h +5
-0@@ -132,6 +132,9 @@#define GIC_BASER_SHAREABILITY (reg, type) \ (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) +/* encode a size field of width @w containing @n - 1 units */ +#define GIC_ENCODE_SZ (n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) +#define GICR_PROPBASER_SHAREABILITY_SHIFT (10)#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7)#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56)@@ -232,6 +235,7 @@#define GITS_CTLR_QUIESCENT (1U << 31)#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)@@ -290,6 +294,7 @@#define GITS_BASER_TYPE (r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7)#define GITS_BASER_ENTRY_SIZE_SHIFT (48)#define GITS_BASER_ENTRY_SIZE (r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) +#define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48)#define GITS_BASER_SHAREABILITY_SHIFT (10)#define GITS_BASER_InnerShareable \ GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)virt/kvm/arm/vgic/vgic-its.c +0
-0
virt/kvm/arm/vgic/vgic-its.c line 37
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +2
-0@@ -33,6 +33,12 @@#include "vgic.h" #include "vgic-mmio.h" +/* Highest migration ABI revision supported by this code */ +#define MAX_ABI_REV 0
Can this be defined as
#define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions)
instead?
Quoted 120 lines
virt/kvm/arm/vgic/vgic-its.c +56
-4+ +static int vgic_its_set_abi (struct vgic_its *its,int rev); +static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +/* * Creates a new (reference to a) struct vgic_irq for a given LPI. * If this LPI is already mapped on another ITS, we increase its refcount @@ -123,6 +129,25 @@ struct its_ite {u32 event_id; }; +/** +* struct vgic_its_abi - ITS abi ops and settings +* @cte_esz: collection table entry size +* @dte_esz: device table entry size +* @ite_esz: interrupt translation table entry size +* @save tables: save the ITS tables into guest RAM +* @restore_tables: restore the ITS internal structs from +* tables stored in guest RAM +* @commit: apply the ABI settings to the ITS +*/ +struct vgic_its_abi { +int cte_esz; +int dte_esz; +int ite_esz; +int (*save_tables)(struct vgic_its *its); +int (*restore_tables)(struct vgic_its *its); +int (*commit)(struct vgic_its *its); +}; +/* * Find and returns a device in the device table for an ITS. * Must be called with the its_lock mutex held. @@ -364,6 +389,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, struct vgic_its *its, gpa_t addr, unsigned int len) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 reg =GITS_TYPER_PLPIS ;/* @@ -376,6 +402,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,*/ reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; reg |=0x0f <<GITS_TYPER_IDBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;return extract_bytes (reg, addr &7 , len); }@@ -1269,6 +1296,7 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm, gpa_t addr, unsigned int len, unsigned long val) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 entry_size, device_type;u64 reg, *regptr, clearbits =0 ;@@ -1279,12 +1307,12 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,switch (BASER_INDEX (addr)) {case 0 : regptr = &its->baser_device_table; - entry_size =8 ; + entry_size = abi->dte_esz; device_type =GITS_BASER_TYPE_DEVICE ;break ;case 1 : regptr = &its->baser_coll_table; - entry_size =8 ; + entry_size = abi->cte_esz; device_type =GITS_BASER_TYPE_COLLECTION ; clearbits =GITS_BASER_INDIRECT ;break ;@@ -1426,7 +1454,6 @@ static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its) (GIC_BASER_CACHEABILITY (GITS_BASER ,INNER , RaWb) | \GIC_BASER_CACHEABILITY (GITS_BASER ,OUTER , SameAsInner) | \GIC_BASER_SHAREABILITY (GITS_BASER , InnerShareable) | \ - ((8ULL -1 ) <<GITS_BASER_ENTRY_SIZE_SHIFT ) | \ GITS_BASER_PAGE_SIZE_64K)#define INITIAL_PROPBASER_VALUE \@@ -1466,7 +1493,7 @@ static int vgic_its_create(struct kvm_device *dev, u32 type) dev->private = its; -return 0 ; +return vgic_its_set_abi (its,MAX_ABI_REV ); }static void vgic_its_destroy (struct kvm_device *kvm_dev)@@ -1584,6 +1611,63 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } +/** +* vgic_its_save_tables_v0 - Save the ITS tables into guest ARM +* according to v0 ABI +*/ +static int vgic_its_save_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM +* to internal data structs according to V0 ABI +* +*/ +static int vgic_its_restore_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +static int vgic_its_commit_v0 (struct vgic_its *its) +{ +const struct vgic_its_abi *abi; + + abi =vgic_its_get_abi (its); + its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; + its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; +
virt/kvm/arm/vgic/vgic-its.c int vgic_its_attr_regs_access(struct kvm_device *dev, · line 1650
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ its->baser_coll_table |= (GIC_ENCODE_SZ (abi->cte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); + + its->baser_device_table |= (GIC_ENCODE_SZ (abi->dte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); +return 0 ; +} + +static const struct vgic_its_abi its_table_abi_versions[MAX_ABI_REV +1 ] = { + {.cte_esz =8 , .dte_esz =8 , .ite_esz =8 ,
And this becomes:
static const struct vgic_its_abi its_table_abi_versions[] = {
[0] = {
.cte_esz = ...,
},
};Quoted 14 lines
virt/kvm/arm/vgic/vgic-its.c +13
-0+ .save_tables = vgic_its_save_tables_v0, + .restore_tables = vgic_its_restore_tables_v0, + .commit = vgic_its_commit_v0, + }, +}; + +inline const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its) +{ +return &its_table_abi_versions[its->abi_rev]; +} + +int vgic_its_set_abi (struct vgic_its *its,int rev) +{
virt/kvm/arm/vgic/vgic-its.c int vgic_its_attr_regs_access(struct kvm_device *dev, · line 1673
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +7
-0+const struct vgic_its_abi *abi; + + its->abi_rev = rev; + abi =vgic_its_get_abi (its); +return abi->commit (its); +} +static int vgic_its_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 01/24] KVM: arm/arm64: Add ITS save/restore API documentation
In reply to Christoffer DallHi,
Quoted 60 lines · Christoffer Dall
Quoted 59 lines · Eric Auger
Add description for how to access ITS registers and how to save/restore ITS tables into/from memory.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- add restoration ordering
- 256B -> 256 Byte aligned
- DTE Size is number of bits for the EVENTID
- s/GITS_READR/GITS_CREADRv4 -> v5:
- take into account Christoffer's comments
- pending table save on GICV3 side nowv3 -> v4:
- take into account Peter's comments:
- typos
-KVM_DEV_ARM_VGIC_GRP_ITS_TABLESkvm_device_attr= 0
- add a validity bit in DTE
- document all fields in CTE and ITE
- document ABI revision
- take into account Andre's comments:
- document restrictions aboutGITS_CREADRwriting andGITS_IIDR
- document-EBUSYerror if one or more VCPUS are runnning
- document 64b registers only can be accessed with 64b access
-itt_addrfield matches bits [51:8] of theitt_addrv1 -> v2:
- DTE and ITE now are 8 bytes
- DTE and ITE now indexed by deviceid/eventid
- use ITE name instead of ITTE
- mentionsITT_addrmatches bits [51:8] of the actual address
- mentions LE layout--- Documentation/virtual/kvm/devices/arm-vgic-its.txt | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+)Documentation/virtual/kvm/devices/arm-vgic-its.txt +12
-0@@ -32,7 +32,124 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the ITS, no additional parameter in kvm_device_attr.addr. + + KVM_DEV_ARM_ITS_SAVE_TABLES + save the ITS table data into guest RAM, at the location provisioned + by the guest in corresponding registers/table entries. + + The layout of the tables in guest memory defines an ABI. The entries + are laid out in little endian format as described in the last paragraph. + + KVM_DEV_ARM_ITS_RESTORE_TABLES + restore the ITS tables from guest RAM to ITS internal structures. + + The GICV3 must be restored before the ITS and all ITS registers but
Quoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 50
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +4
-0+ the GITS_CTLR must be restored before restoring the ITS tables. + + The GITS_IIDR read-only register must also be restored before + the table restore as the IIDR revision field encodes the ABI revision.the table restore? "before calling
KVM_DEV_ARM_ITS_RESTORE_TABLES" ? (there's a potential confusion between calling this ioctl and simply restoring the memory containing the tables).
OK
Quoted 23 lines
Quoted 23 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +18
-0+ + The expected ordering when restoring the GICv3/ITS is described in section + "ITS Restoration Sequence". + Errors: -ENXIO: ITS not properly configured as required prior to setting this attribute -ENOMEM: Memory shortage when allocating ITS internal data + -EINVAL: Inconsistent restored data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running + + KVM_DEV_ARM_VGIC_GRP_ITS_REGS + Attributes: + The attr field of kvm_device_attr encodes the offset of the + ITS register, relative to the ITS control frame base address + (ITS_base). + + kvm_device_attr.addr points to a __u64 value whatever the width + of the addressed register (32/64 bits). 64 bit registers can only + be accessed with full length. +
Quoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 78
Quoted 6 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +6
-0+ Writes to read-only registers are ignored by the kernel except for: + - GITS_CREADR. It needs to be restored otherwise commands in the queue + will be re-executed after restoring CWRITER. GITS_CREADR must be + restored before restoring the GITS_CTLR which is likely to enable the + ITS. Also it needs to be restored after GITS_CBASER since a write to + GITS_CBASER resets GITS_CREADR.uber nit: s/needs to be/must be/g in all occurences above.
OK
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 79
Quoted 2 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ - GITS_IIDR. Its Revision field encodes the table layout ABI revision.s/Its/The/
OK. Argh, I meant its (~his) ;-)
Quoted 5 lines
the revision of the table layout ABI.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 80
Quoted 1 line
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ In the future we might implement direct injection of virtual LPIS.LPIs
OK
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 82
Quoted 3 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +2
-0+ This will require an upgrade of the table layout and an evolution of + the ABI. GITS_IIDR must be restored before the table restoration.before calling
KVM_DEV_ARM_ITS_RESTORE_TABLES.
OK
Quoted 13 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 92
Quoted 11 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +10
-0+ + For other registers, getting or setting a register has the same + effect as reading/writing the register on real hardware. + Errors: + -ENXIO: Offset does not correspond to any supported register + -EFAULT: Invalid user pointer for attr->addr + -EINVAL: Offset is not 64-bit aligned + -EBUSY: one or more VCPUS are running + + ITS Restoration Sequence:s/Restoration/restore/
OK
Quoted 9 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 97
Quoted 6 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +5
-0+ ------------------------- + +The following ordering must be followed when restoring the GIC and the ITS: +a) restore all guest memory and create vcpus +b) initialize the GIC and ITS (KVM_DEV_ARM_VGIC_CTRL_INIT)are these really coupled or can you do everything related to the gic first, and then everything related to the ITS afterwards?
no they aren't.
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 98
Quoted 2 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+c) provide GIC and ITS base addresses (KVM_DEV_ARM_VGIC_GRP_ADDR)same
no they aren't
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 105
Quoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +7
-0+d) restore all redistributors +e) restore the ITS in the following order: + 1. Restore GITS_CBASER + 2. Restore all other GITS_ registers, except GITS_CTLR! + 3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES) + 4. Restore GITS_CTLR +f) start vcpusI would omit (f) from here.
In that case I think it would make sense to describe the restore sequence for GICV3 RDIST in GICV3 device doc and not here. This would alleviate this very description.
restoring RDIST regs require a previous GICV3 KVM_DEV_ARM_VGIC_CTRL_INIT and KVM_DEV_ARM_VGIC_GRP_ADDR I think.
So description here would become
"
a) restore all guest memory and create vcpus
b) restore all redistributors
c) initialize the ITS and then provide its base address (KVM_DEV_ARM_VGIC_CTRL_INIT, KVM_DEV_ARM_VGIC_GRP_ADDR)
c) restore the ITS in the following order:
1. Restore GITS_CBASER
2. Restore all other GITS_ registers, except GITS_CTLR!
3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES)
4. Restore GITS_CTLR
Then vcpus can be started.
"
What do you think?
Quoted 12 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 114
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +9
-0+ + ITS Table ABI REV0: + ------------------- + + Revision 0 of the ABI only supports physical LPIs. + + The device table and ITT are indexed by the deviceid and eventid, + respectively. The collection table is not indexed by collectionid: + CTE are written in the table in the order of collection creation. Allnit: CTEs
OK
Quoted 40 lines
Quoted 40 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +39
-0+ entries are 8 bytes. + + Device Table Entry (DTE): + + bits: | 63| 62 ... 49 | 48 ... 5 | 4 ... 0 | + values: | V | next | ITT_addr | Size | + + where; + - V indicates whether the entry is valid. If not, other fields + are not meaningful. + - next: equals to 0 if this entry is the last one; otherwise it + corresponds to the deviceid offset to the next DTE, capped by + 2^14 -1. + - ITT_addr matches bits [51:8] of the ITT address (256 Byte aligned). + - Size specifies the supported number of bits for the eventid, + minus one + + Collection Table Entry (CTE): + + bits: | 63| 62 .. 52 | 51 ... 16 | 15 ... 0 | + values: | V | RES0 | RDBase | ICID | + + where: + - V indicates whether the entry is valid. If not, other fields are + not meaningful. + - RES0: reserved field with Should-Be-Zero-or-Preserved behavior. + - RDBase is the PE number (GICR_TYPER.Processor_Number semantic), + - ICID is the collection ID + + Interrupt Translation Entry (ITE): + + bits: | 63 ... 48 | 47 ... 16 | 15 ... 0 | + values: | next | pINTID | ICID | + + where: + - next: equals to 0 if this entry is the last one; otherwise it corresponds + to the eventid offset to the next ITE capped by 2^16 -1. + - pINTID is the physical LPI ID; if zero, it means the entry is not valid + and other fields are not meaningful.
Quoted 8 lines
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +2
-0+ - ICID is the collection ID +Signature · 1 line
2.5.5The only outstanding issue here is the restore order part. I don't wont to specify a stronger order than absolutely necessary here.
see above
Thanks
Eric
Quoted 11 lines
Otherwise this generally looks good.
Thanks,
-Christoffer_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 07/24] KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access
In reply to Marc ZyngierHi Marc,
Quoted 35 lines · Marc Zyngier
Quoted 34 lines · Eric Auger
This patch implements
vgic_its_has_attr_regsandvgic_its_attr_regs_accessupon the MMIO framework. VGIC ITS KVM deviceKVM_DEV_ARM_VGIC_GRP_ITS_REGSgroup becomes functional.At least
GITS_CREADRandGITS_IIDRrequire to differentiate a guest write action from a user access. As such let's introduce a newuaccess_its_writevgic_register_regioncallback.Signed-off-by Eric Auger <[email protected]>---
v6 -> v6:
- fix GITS_PIDR/CIDR accesses
- remove vgic_data_mmio_bus_to_host/vgic_data_host_to_mmio_bus
- 64b full reg access comment rewording
-vgic_its_attr_regs_accessdeclaration block rework including removal of iodevv4 -> v5:
- useGITS_TYPERinstead of offset0x8
-uaccess_its_writenow can return an errorv3 -> v4:
- remove changes to theREGISTER_ITS_DESCmacro. This will be handled in subsequent patch with the introduction of a newREGISTER_ITS_DESC_UACCESSmacro
- fix IIDR access and add a comment wrt full length access
- handle endianness
- add kvm lock and vcpus lock--- virt/kvm/arm/vgic/vgic-its.c | 70 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio.h | 9 ++++-- 2 files changed, 75 insertions(+), 4 deletions(-)
Quoted 15 lines
virt/kvm/arm/vgic/vgic-its.c static void vgic_its_destroy(struct kvm_device *kvm_dev) · line 1474
Quoted 12 lines
virt/kvm/arm/vgic/vgic-its.c +3
-1@@ -1469,14 +1469,80 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)int vgic_its_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +const struct vgic_register_region *region; +gpa_t offset = attr->attr; +Shouldn't you check the alignment in the same way you do it below? It seems a bit odd that one succeeds while the other fails with the same input.
yes makes sense
Thanks
Eric
Quoted 102 lines
Quoted 87 lines
virt/kvm/arm/vgic/vgic-its.c +65
-1+ region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) +return -ENXIO ; + +return 0 ; }int vgic_its_attr_regs_access (struct kvm_device *dev,struct kvm_device_attr *attr,u64 *reg,bool is_write) { -return -ENXIO ; +const struct vgic_register_region *region; +struct vgic_its *its; +gpa_t addr, offset; +unsigned int len; +int align, ret =0 ; + + its = dev->private; + offset = attr->attr; + +/* +* Although the spec supports upper/lower 32-bit accesses to +* 64-bit ITS registers, the userspace ABI requires 64-bit +* accesses to all 64-bit wide registers. We therefore only +* support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID +* registers +*/ + align = (offset <GITS_TYPER ) || (offset >=GITS_PIDR4 ) ?0x3 :0x7 ; + +if (offset & align) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (IS_VGIC_ADDR_UNDEF (its->vgic_its_base)) { + ret = -ENXIO ; + goto out; + } + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) { + ret = -ENXIO ; + goto out; + } + +if (!lock_all_vcpus (dev->kvm)) { + ret = -EBUSY ; + goto out; + } + + addr = its->vgic_its_base + offset; + + len = region->access_flags & VGIC_ACCESS_64bit ?8 :4 ; + +if (is_write) { +if (region->uaccess_its_write) + ret = region->uaccess_its_write (dev->kvm, its, addr, + len, *reg); +else + region->its_write (dev->kvm, its, addr, len, *reg); + }else { + *reg = region->its_read (dev->kvm, its, addr, len); + } +unlock_all_vcpus (dev->kvm); +out: +mutex_unlock (&dev->kvm->lock); +return ret; } static int vgic_its_has_attr(struct kvm_device *dev,virt/kvm/arm/vgic/vgic-mmio.h +0
-2@@ -36,8 +36,13 @@ struct vgic_register_region { };unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); -void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, -unsigned int len,unsigned long val);virt/kvm/arm/vgic/vgic-mmio.h struct vgic_register_region { · line 48
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h +7
-0+union { +void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); +int (*uaccess_its_write)(struct kvm *kvm,struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val); + }; };extern struct kvm_io_device_ops kvm_io_gic_ops;Thanks,
M.
Re: [PATCH v6 08/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
In reply to Marc ZyngierHi Marc,
Quoted 52 lines · Marc Zyngier
Quoted 37 lines · Eric Auger
GITS_CREADRneeds to be restored so let's implement the associateduaccess_write_itscallback. The write only is allowed if the its is disabled.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove usage ofupdate_64bit_regand check alignment of cmd offsetv4 -> v5:
- keep Stalled bit
-vgic_mmio_uaccess_write_its_creadrcan now return an errorv3 -> v4:
-REGISTER_ITS_DESC_UACCESSnow introduced in this patch
- we now check the its is disabled--- virt/kvm/arm/vgic/vgic-its.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +8
-0@@ -1213,6 +1213,34 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,return extract_bytes (its->creadr, addr &0x7 , len); } +static int vgic_mmio_uaccess_write_its_creadr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 cmd_offset; +int ret =0 ; +virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, · line 1233
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+mutex_lock (&its->cmd_lock); + +if (its->enabled) { + ret = -EBUSY ; + goto out; + } + + cmd_offset =ITS_CMD_OFFSET (val); +if ((cmd_offset >=ITS_CMD_BUFFER_SIZE (its->cbaser)) || + (cmd_offset &0x1F )) {nit: In general, and for alignments that aren't completely obvious, you can write something like
IS_ALIGNED(cmd_offset, sizeof(struct its_cmd)).
Quoted 2 lines
Unfortunately, we don't have a struct
its_cmd, and maybe we should fix that at a later time.
OK
Quoted 47 lines
Quoted 34 lines
virt/kvm/arm/vgic/vgic-its.c +20
-0+ ret = -EINVAL ; + goto out; + } + + its->creadr = val; +out: +mutex_unlock (&its->cmd_lock); +return ret; +} +#define BASER_INDEX (addr) (((addr) / sizeof(u64)) & 0x7) static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, struct vgic_its *its,@@ -1317,6 +1345,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, .its_write = wr, \ } +#define REGISTER_ITS_DESC_UACCESS (off, rd, wr, uwr, length, acc)\ +{ \ + .reg_offset = off, \ + .len = length, \ + .access_flags = acc, \ + .its_read = rd, \ + .its_write = wr, \ + .uaccess_its_write = uwr, \ +} +static void its_mmio_write_wi (struct kvm *kvm,struct vgic_its *its,gpa_t addr,unsigned int len,unsigned long val) {@@ -1339,8 +1377,9 @@ static struct vgic_register_region its_registers[] = { REGISTER_ITS_DESC(GITS_CWRITER,virt/kvm/arm/vgic/vgic-its.c static struct vgic_register_region its_registers[] = { · line 1385
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +3
-2vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_CREADR , - vgic_mmio_read_its_creadr, its_mmio_write_wi,8 , +REGISTER_ITS_DESC_UACCESS (GITS_CREADR , + vgic_mmio_read_its_creadr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_creadr,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_BASER, vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,Otherwise:
Quoted 1 line
Acked-by Marc Zyngier <[email protected]>
Thanks!
Eric
Quoted 5 lines
Thanks,
M.
Re: [PATCH v6 10/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr
In reply to Eric AugerQuoted 48 lines · Eric Auger
The
GITS_IIDRrevision field is used to encode the migration ABI revision. So we need to restore it to check the table layout is readable by the destination.By writing the IIDR, userspace thus forces the ABI revision to be used and this must be less than or equal to the max revision KVM supports.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- fix typos in the commit message
- dont't useupdate_64bit_reganymorev4 -> v5
- renameuser_revisionintoabi_revand REV intoMAX_ABI_REV
- IIDR reportsabi_revset by userspace if any.
- If value set by userspace exceeds the max supported revision, an error is reported.
- add some definesv4: creation
--- include/linux/irqchip/arm-gic-v3.h | 4 ++++ virt/kvm/arm/vgic/vgic-its.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-)include/linux/irqchip/arm-gic-v3.h +4
-0@@ -242,6 +242,10 @@#define GITS_TYPER_PTA (1UL << 19)#define GITS_TYPER_HWCOLLCNT_SHIFT 24 +#define GITS_IIDR_REV_SHIFT 12 +#define GITS_IIDR_REV (r) (((r) >> GITS_IIDR_REV_SHIFT) & 0xf) +#define GITS_IIDR_PRODUCTID_SHIFT 24 +#define GITS_CBASER_VALID (1ULL << 63)#define GITS_CBASER_SHAREABILITY_SHIFT (10)#define GITS_CBASER_INNER_CACHEABILITY_SHIFT (59)virt/kvm/arm/vgic/vgic-its.c +0
-0
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm, · line 415
Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +2
-1@@ -411,7 +411,20 @@ static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,struct vgic_its *its, gpa_t addr, unsigned int len) { - return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); + return (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | + (its->abi_rev << GITS_IIDR_REV_SHIFT) | IMPLEMENTER_ARM;
Given that abi_rev is a 32bit quantity, and the REV field only only 4 bit wide, I'd feel safer with a mask somewhere...
And hopefully, we'll never have more than 16 ABIs! ;-)
Quoted 18 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+} + +static int vgic_mmio_uaccess_write_its_iidr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 rev =GITS_IIDR_REV (val); + +if (rev >MAX_ABI_REV ) +return -EINVAL ; +return vgic_its_set_abi (its, rev); } static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,@@ -1393,8 +1406,9 @@ static struct vgic_register_region its_registers[] = { REGISTER_ITS_DESC(GITS_CTLR,
virt/kvm/arm/vgic/vgic-its.c static struct vgic_register_region its_registers[] = { · line 1414
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +3
-2vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_IIDR , - vgic_mmio_read_its_iidr, its_mmio_write_wi,4 , +REGISTER_ITS_DESC_UACCESS (GITS_IIDR , + vgic_mmio_read_its_iidr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_iidr,4 , VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_TYPER, vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 07/24] KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access
In reply to Eric AugerQuoted 67 lines · Eric Auger
This patch implements
vgic_its_has_attr_regsandvgic_its_attr_regs_accessupon the MMIO framework. VGIC ITS KVM deviceKVM_DEV_ARM_VGIC_GRP_ITS_REGSgroup becomes functional.At least
GITS_CREADRandGITS_IIDRrequire to differentiate a guest write action from a user access. As such let's introduce a newuaccess_its_writevgic_register_regioncallback.Signed-off-by Eric Auger <[email protected]>---
v6 -> v6:
- fix GITS_PIDR/CIDR accesses
- remove vgic_data_mmio_bus_to_host/vgic_data_host_to_mmio_bus
- 64b full reg access comment rewording
-vgic_its_attr_regs_accessdeclaration block rework including removal of iodevv4 -> v5:
- useGITS_TYPERinstead of offset0x8
-uaccess_its_writenow can return an errorv3 -> v4:
- remove changes to theREGISTER_ITS_DESCmacro. This will be handled in subsequent patch with the introduction of a newREGISTER_ITS_DESC_UACCESSmacro
- fix IIDR access and add a comment wrt full length access
- handle endianness
- add kvm lock and vcpus lock--- virt/kvm/arm/vgic/vgic-its.c | 70 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio.h | 9 ++++-- 2 files changed, 75 insertions(+), 4 deletions(-)virt/kvm/arm/vgic/vgic-its.c +17
-2@@ -1469,14 +1469,80 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)int vgic_its_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +const struct vgic_register_region *region; +gpa_t offset = attr->attr; + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) +return -ENXIO ; + +return 0 ; }int vgic_its_attr_regs_access (struct kvm_device *dev,struct kvm_device_attr *attr,u64 *reg,bool is_write) { -return -ENXIO ; +const struct vgic_register_region *region; +struct vgic_its *its; +gpa_t addr, offset; +unsigned int len; +int align, ret =0 ; + + its = dev->private;
virt/kvm/arm/vgic/vgic-its.c static void vgic_its_destroy(struct kvm_device *kvm_dev) · line 1504
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ offset = attr->attr; + +/* +* Although the spec supports upper/lower 32-bit accesses to +* 64-bit ITS registers, the userspace ABI requires 64-bit +* accesses to all 64-bit wide registers. We therefore only +* support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID +* registers +*/ + align = (offset <GITS_TYPER ) || (offset >=GITS_PIDR4 ) ?0x3 :0x7 ;
nit: this should be written out instead of using the ternary conditional.
Quoted 67 lines
virt/kvm/arm/vgic/vgic-its.c +41
-0+ +if (offset & align) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (IS_VGIC_ADDR_UNDEF (its->vgic_its_base)) { + ret = -ENXIO ; + goto out; + } + + region =vgic_find_mmio_region (its_registers, +ARRAY_SIZE (its_registers), + offset); +if (!region) { + ret = -ENXIO ; + goto out; + } + +if (!lock_all_vcpus (dev->kvm)) { + ret = -EBUSY ; + goto out; + } + + addr = its->vgic_its_base + offset; + + len = region->access_flags & VGIC_ACCESS_64bit ?8 :4 ; + +if (is_write) { +if (region->uaccess_its_write) + ret = region->uaccess_its_write (dev->kvm, its, addr, + len, *reg); +else + region->its_write (dev->kvm, its, addr, len, *reg); + }else { + *reg = region->its_read (dev->kvm, its, addr, len); + } +unlock_all_vcpus (dev->kvm); +out: +mutex_unlock (&dev->kvm->lock); +return ret; } static int vgic_its_has_attr(struct kvm_device *dev,virt/kvm/arm/vgic/vgic-mmio.h +7
-2@@ -36,8 +36,13 @@ struct vgic_register_region { };unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); -void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, -unsigned int len,unsigned long val); +union { +void (*uaccess_write)(struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); +int (*uaccess_its_write)(struct kvm *kvm,struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val); + }; };extern struct kvm_io_device_ops kvm_io_gic_ops;Signature · 1 line
2.5.5
Otherwise:
Re: [PATCH v6 01/24] KVM: arm/arm64: Add ITS save/restore API documentation
In reply to Auger EricQuoted 103 lines · Auger Eric
Hi,
Quoted 60 lines · Christoffer Dall
Quoted 59 lines · Eric Auger
Add description for how to access ITS registers and how to save/restore ITS tables into/from memory.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- add restoration ordering
- 256B -> 256 Byte aligned
- DTE Size is number of bits for the EVENTID
- s/GITS_READR/GITS_CREADRv4 -> v5:
- take into account Christoffer's comments
- pending table save on GICV3 side nowv3 -> v4:
- take into account Peter's comments:
- typos
-KVM_DEV_ARM_VGIC_GRP_ITS_TABLESkvm_device_attr= 0
- add a validity bit in DTE
- document all fields in CTE and ITE
- document ABI revision
- take into account Andre's comments:
- document restrictions aboutGITS_CREADRwriting andGITS_IIDR
- document-EBUSYerror if one or more VCPUS are runnning
- document 64b registers only can be accessed with 64b access
-itt_addrfield matches bits [51:8] of theitt_addrv1 -> v2:
- DTE and ITE now are 8 bytes
- DTE and ITE now indexed by deviceid/eventid
- use ITE name instead of ITTE
- mentionsITT_addrmatches bits [51:8] of the actual address
- mentions LE layout--- Documentation/virtual/kvm/devices/arm-vgic-its.txt | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+)Documentation/virtual/kvm/devices/arm-vgic-its.txt +12
-0@@ -32,7 +32,124 @@ Groups: KVM_DEV_ARM_VGIC_CTRL_INIT request the initialization of the ITS, no additional parameter in kvm_device_attr.addr. + + KVM_DEV_ARM_ITS_SAVE_TABLES + save the ITS table data into guest RAM, at the location provisioned + by the guest in corresponding registers/table entries. + + The layout of the tables in guest memory defines an ABI. The entries + are laid out in little endian format as described in the last paragraph. + + KVM_DEV_ARM_ITS_RESTORE_TABLES + restore the ITS tables from guest RAM to ITS internal structures. + + The GICV3 must be restored before the ITS and all ITS registers butQuoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 50
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +4
-0+ the GITS_CTLR must be restored before restoring the ITS tables. + + The GITS_IIDR read-only register must also be restored before + the table restore as the IIDR revision field encodes the ABI revision.the table restore? "before calling
KVM_DEV_ARM_ITS_RESTORE_TABLES" ? (there's a potential confusion between calling this ioctl and simply restoring the memory containing the tables).OK
Quoted 23 lines
Quoted 23 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +18
-0+ + The expected ordering when restoring the GICv3/ITS is described in section + "ITS Restoration Sequence". + Errors: -ENXIO: ITS not properly configured as required prior to setting this attribute -ENOMEM: Memory shortage when allocating ITS internal data + -EINVAL: Inconsistent restored data + -EFAULT: Invalid guest ram access + -EBUSY: One or more VCPUS are running + + KVM_DEV_ARM_VGIC_GRP_ITS_REGS + Attributes: + The attr field of kvm_device_attr encodes the offset of the + ITS register, relative to the ITS control frame base address + (ITS_base). + + kvm_device_attr.addr points to a __u64 value whatever the width + of the addressed register (32/64 bits). 64 bit registers can only + be accessed with full length. +Quoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 78
Quoted 6 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +6
-0+ Writes to read-only registers are ignored by the kernel except for: + - GITS_CREADR. It needs to be restored otherwise commands in the queue + will be re-executed after restoring CWRITER. GITS_CREADR must be + restored before restoring the GITS_CTLR which is likely to enable the + ITS. Also it needs to be restored after GITS_CBASER since a write to + GITS_CBASER resets GITS_CREADR.uber nit: s/needs to be/must be/g in all occurences above.
Quoted 6 lines
OK
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 79
Quoted 2 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ - GITS_IIDR. Its Revision field encodes the table layout ABI revision.s/Its/The/
OK. Argh, I meant its (~his) ;-)
You wrote it correctly, it's just that it can be misleading, so I'm really in the business of beutifying the ABI text here :)
Quoted 75 lines
Quoted 5 lines
the revision of the table layout ABI.
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 80
Quoted 1 line
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+ In the future we might implement direct injection of virtual LPIS.LPIs
OK
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 82
Quoted 3 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +2
-0+ This will require an upgrade of the table layout and an evolution of + the ABI. GITS_IIDR must be restored before the table restoration.before calling
KVM_DEV_ARM_ITS_RESTORE_TABLES.OK
Quoted 13 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 92
Quoted 11 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +10
-0+ + For other registers, getting or setting a register has the same + effect as reading/writing the register on real hardware. + Errors: + -ENXIO: Offset does not correspond to any supported register + -EFAULT: Invalid user pointer for attr->addr + -EINVAL: Offset is not 64-bit aligned + -EBUSY: one or more VCPUS are running + + ITS Restoration Sequence:s/Restoration/restore/
OK
Quoted 9 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 97
Quoted 6 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +5
-0+ ------------------------- + +The following ordering must be followed when restoring the GIC and the ITS: +a) restore all guest memory and create vcpus +b) initialize the GIC and ITS (KVM_DEV_ARM_VGIC_CTRL_INIT)are these really coupled or can you do everything related to the gic first, and then everything related to the ITS afterwards?
no they aren't.
Quoted 4 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 98
Quoted 2 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +1
-0+c) provide GIC and ITS base addresses (KVM_DEV_ARM_VGIC_GRP_ADDR)same
no they aren't
Quoted 10 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 105
Quoted 8 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +7
-0+d) restore all redistributors +e) restore the ITS in the following order: + 1. Restore GITS_CBASER + 2. Restore all other GITS_ registers, except GITS_CTLR! + 3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES) + 4. Restore GITS_CTLR +f) start vcpusI would omit (f) from here.
In that case I think it would make sense to describe the restore sequence for GICV3 RDIST in GICV3 device doc and not here. This would alleviate this very description.
restoring RDIST regs require a previous GICV3
KVM_DEV_ARM_VGIC_CTRL_INITandKVM_DEV_ARM_VGIC_GRP_ADDRI think.So description here would become
"
a) restore all guest memory and create vcpus
b) restore all redistributors
c) initialize the ITS and then provide its base address (KVM_DEV_ARM_VGIC_CTRL_INIT,KVM_DEV_ARM_VGIC_GRP_ADDR)
c) restore the ITS in the following order:
1. RestoreGITS_CBASER
2. Restore all otherGITS_registers, exceptGITS_CTLR!
3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES)
4. RestoreGITS_CTLR
Quoted 4 lines
Then vcpus can be started.
"What do you think?
Yes, looks good.
Quoted 63 lines
Quoted 14 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt Groups: · line 114
Quoted 12 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +9
-0+ + ITS Table ABI REV0: + ------------------- + + Revision 0 of the ABI only supports physical LPIs. + + The device table and ITT are indexed by the deviceid and eventid, + respectively. The collection table is not indexed by collectionid: + CTE are written in the table in the order of collection creation. Allnit: CTEs
OK
Quoted 40 lines
Quoted 40 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +39
-0+ entries are 8 bytes. + + Device Table Entry (DTE): + + bits: | 63| 62 ... 49 | 48 ... 5 | 4 ... 0 | + values: | V | next | ITT_addr | Size | + + where; + - V indicates whether the entry is valid. If not, other fields + are not meaningful. + - next: equals to 0 if this entry is the last one; otherwise it + corresponds to the deviceid offset to the next DTE, capped by + 2^14 -1. + - ITT_addr matches bits [51:8] of the ITT address (256 Byte aligned). + - Size specifies the supported number of bits for the eventid, + minus one + + Collection Table Entry (CTE): + + bits: | 63| 62 .. 52 | 51 ... 16 | 15 ... 0 | + values: | V | RES0 | RDBase | ICID | + + where: + - V indicates whether the entry is valid. If not, other fields are + not meaningful. + - RES0: reserved field with Should-Be-Zero-or-Preserved behavior. + - RDBase is the PE number (GICR_TYPER.Processor_Number semantic), + - ICID is the collection ID + + Interrupt Translation Entry (ITE): + + bits: | 63 ... 48 | 47 ... 16 | 15 ... 0 | + values: | next | pINTID | ICID | + + where: + - next: equals to 0 if this entry is the last one; otherwise it corresponds + to the eventid offset to the next ITE capped by 2^16 -1. + - pINTID is the physical LPI ID; if zero, it means the entry is not valid + and other fields are not meaningful.Quoted 8 lines
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-its.txt +2
-0+ - ICID is the collection ID +Signature · 1 line
2.5.5The only outstanding issue here is the restore order part. I don't wont to specify a stronger order than absolutely necessary here.
Quoted 2 lines
see above
Yep, I think we're there.
Thanks,
-Christoffer
Re: [PATCH v6 08/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
In reply to Eric AugerQuoted 42 lines · Eric Auger
GITS_CREADRneeds to be restored so let's implement the associateduaccess_write_itscallback. The write only is allowed if the its is disabled.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove usage ofupdate_64bit_regand check alignment of cmd offsetv4 -> v5:
- keep Stalled bit
-vgic_mmio_uaccess_write_its_creadrcan now return an errorv3 -> v4:
-REGISTER_ITS_DESC_UACCESSnow introduced in this patch
- we now check the its is disabled--- virt/kvm/arm/vgic/vgic-its.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +13
-0@@ -1213,6 +1213,34 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,return extract_bytes (its->creadr, addr &0x7 , len); } +static int vgic_mmio_uaccess_write_its_creadr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 cmd_offset; +int ret =0 ; + +mutex_lock (&its->cmd_lock); + +if (its->enabled) { + ret = -EBUSY ; + goto out;
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, · line 1238
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ } + + cmd_offset =ITS_CMD_OFFSET (val); +if ((cmd_offset >=ITS_CMD_BUFFER_SIZE (its->cbaser)) || + (cmd_offset &0x1F )) { + ret = -EINVAL ; + goto out; + } + + its->creadr = val;
do we let userspace decide the stalled bit or should we use ITS_CMD_OFFSET() when assigning the field as well?
Quoted 40 lines
virt/kvm/arm/vgic/vgic-its.c +18
-2+out: +mutex_unlock (&its->cmd_lock); +return ret; +} +#define BASER_INDEX (addr) (((addr) / sizeof(u64)) & 0x7) static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, struct vgic_its *its,@@ -1317,6 +1345,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, .its_write = wr, \ } +#define REGISTER_ITS_DESC_UACCESS (off, rd, wr, uwr, length, acc)\ +{ \ + .reg_offset = off, \ + .len = length, \ + .access_flags = acc, \ + .its_read = rd, \ + .its_write = wr, \ + .uaccess_its_write = uwr, \ +} +static void its_mmio_write_wi (struct kvm *kvm,struct vgic_its *its,gpa_t addr,unsigned int len,unsigned long val) {@@ -1339,8 +1377,9 @@ static struct vgic_register_region its_registers[] = {REGISTER_ITS_DESC (GITS_CWRITER , vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_CREADR , - vgic_mmio_read_its_creadr, its_mmio_write_wi,8 , +REGISTER_ITS_DESC_UACCESS (GITS_CREADR , + vgic_mmio_read_its_creadr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_creadr,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_BASER, vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,Signature · 1 line
2.5.5
Otherwise:
Re: [PATCH v6 10/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr
In reply to Eric AugerQuoted 10 lines · Eric Auger
The
GITS_IIDRrevision field is used to encode the migration ABI revision. So we need to restore it to check the table layout is readable by the destination.By writing the IIDR, userspace thus forces the ABI revision to be used and this must be less than or equal to the max revision KVM supports.
Signed-off-by Eric Auger <[email protected]>
Quoted 74 lines
---
v5 -> v6:
- fix typos in the commit message
- dont't useupdate_64bit_reganymorev4 -> v5
- renameuser_revisionintoabi_revand REV intoMAX_ABI_REV
- IIDR reportsabi_revset by userspace if any.
- If value set by userspace exceeds the max supported revision, an error is reported.
- add some definesv4: creation
--- include/linux/irqchip/arm-gic-v3.h | 4 ++++ virt/kvm/arm/vgic/vgic-its.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-)include/linux/irqchip/arm-gic-v3.h +4
-0@@ -242,6 +242,10 @@#define GITS_TYPER_PTA (1UL << 19)#define GITS_TYPER_HWCOLLCNT_SHIFT 24 +#define GITS_IIDR_REV_SHIFT 12 +#define GITS_IIDR_REV (r) (((r) >> GITS_IIDR_REV_SHIFT) & 0xf) +#define GITS_IIDR_PRODUCTID_SHIFT 24 +#define GITS_CBASER_VALID (1ULL << 63)#define GITS_CBASER_SHAREABILITY_SHIFT (10)#define GITS_CBASER_INNER_CACHEABILITY_SHIFT (59)virt/kvm/arm/vgic/vgic-its.c +17
-3@@ -411,7 +411,20 @@ static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,struct vgic_its *its, gpa_t addr, unsigned int len) { - return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); + return (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | + (its->abi_rev << GITS_IIDR_REV_SHIFT) | IMPLEMENTER_ARM; +} + +static int vgic_mmio_uaccess_write_its_iidr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 rev =GITS_IIDR_REV (val); + +if (rev >MAX_ABI_REV ) +return -EINVAL ; +return vgic_its_set_abi (its, rev); } static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,@@ -1393,8 +1406,9 @@ static struct vgic_register_region its_registers[] = {REGISTER_ITS_DESC (GITS_CTLR , vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr,4 , VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_IIDR , - vgic_mmio_read_its_iidr, its_mmio_write_wi,4 , +REGISTER_ITS_DESC_UACCESS (GITS_IIDR , + vgic_mmio_read_its_iidr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_iidr,4 , VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_TYPER, vgic_mmio_read_its_typer, its_mmio_write_wi, 8,Signature · 1 line
2.5.5
Re: [PATCH v6 09/24] KVM: arm64: vgic-its: Introduce migration ABI infrastructure
In reply to Eric AugerQuoted 96 lines · Eric Auger
We plan to support different migration ABIs, ie. characterizing the ITS table layout format in guest RAM. For example, a new ABI will be needed if vLPIs get supported for nested use case.
So let's introduce an array of supported ABIs (at the moment a single ABI is supported though). The following characteristics are foreseen to vary with the ABI: size of table entries, save/restore operation, the way abi settings are applied.
By default the
MAX_ABI_REVis applied on its creation. In subsequent patches we will introduce a way for the userspace to change the ABI in use.The entry sizes now are set according to the ABI version and not hardcoded anymore.
Signed-off-by Eric Auger <[email protected]>---
v6 -> v7:
- rename abi intoits_table_abi_versions
- kernel doc comments for structvgic_its_abi
- commentGIC_ENCODE_SZ
- slighly rephrase the commit messagev5: creation and squash KVM: arm64: ITS: Report the ITE size in
GITS_TYPER--- include/kvm/arm_vgic.h | 3 ++ include/linux/irqchip/arm-gic-v3.h | 5 +++ virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-)include/kvm/arm_vgic.h +3
-0@@ -162,6 +162,9 @@ struct vgic_its {u32 creadr;u32 cwriter; +/* migration ABI revision in use */ +u32 abi_rev; +/* Protects the device and collection lists */ struct mutex its_lock;struct list_head device_list;include/linux/irqchip/arm-gic-v3.h +5
-0@@ -132,6 +132,9 @@#define GIC_BASER_SHAREABILITY (reg, type) \ (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) +/* encode a size field of width @w containing @n - 1 units */ +#define GIC_ENCODE_SZ (n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) +#define GICR_PROPBASER_SHAREABILITY_SHIFT (10)#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7)#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56)@@ -232,6 +235,7 @@#define GITS_CTLR_QUIESCENT (1U << 31)#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)@@ -290,6 +294,7 @@#define GITS_BASER_TYPE (r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7)#define GITS_BASER_ENTRY_SIZE_SHIFT (48)#define GITS_BASER_ENTRY_SIZE (r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) +#define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48)#define GITS_BASER_SHAREABILITY_SHIFT (10)#define GITS_BASER_InnerShareable \ GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)virt/kvm/arm/vgic/vgic-its.c +6
-0@@ -33,6 +33,12 @@#include "vgic.h" #include "vgic-mmio.h" +/* Highest migration ABI revision supported by this code */ +#define MAX_ABI_REV 0 + +static int vgic_its_set_abi (struct vgic_its *its,int rev); +static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +/* * Creates a new (reference to a) struct vgic_irq for a given LPI. * If this LPI is already mapped on another ITS, we increase its refcount @@ -123,6 +129,25 @@ struct its_ite {u32 event_id;
virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 139
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-0}; +/** +* struct vgic_its_abi - ITS abi ops and settings +* @cte_esz: collection table entry size +* @dte_esz: device table entry size +* @ite_esz: interrupt translation table entry size +* @save tables: save the ITS tables into guest RAM +* @restore_tables: restore the ITS internal structs from +* tables stored in guest RAM
nit: indent to align with the beginning of 'restore...'
virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 140
Quoted 1 line
virt/kvm/arm/vgic/vgic-its.c +1
-0+* @commit: apply the ABI settings to the ITS
hmm, I already forgot what the commit function does, and reading this text doesn't seem to help me, unfortunately. When is this called, what does it do?
Thanks,
-Christoffer
Quoted 136 lines
virt/kvm/arm/vgic/vgic-its.c +73
-4+*/ +struct vgic_its_abi { +int cte_esz; +int dte_esz; +int ite_esz; +int (*save_tables)(struct vgic_its *its); +int (*restore_tables)(struct vgic_its *its); +int (*commit)(struct vgic_its *its); +}; +/* * Find and returns a device in the device table for an ITS. * Must be called with the its_lock mutex held. @@ -364,6 +389,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, struct vgic_its *its, gpa_t addr, unsigned int len) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 reg =GITS_TYPER_PLPIS ;/* @@ -376,6 +402,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,*/ reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; reg |=0x0f <<GITS_TYPER_IDBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;return extract_bytes (reg, addr &7 , len); }@@ -1269,6 +1296,7 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm, gpa_t addr, unsigned int len, unsigned long val) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 entry_size, device_type;u64 reg, *regptr, clearbits =0 ;@@ -1279,12 +1307,12 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,switch (BASER_INDEX (addr)) {case 0 : regptr = &its->baser_device_table; - entry_size =8 ; + entry_size = abi->dte_esz; device_type =GITS_BASER_TYPE_DEVICE ;break ;case 1 : regptr = &its->baser_coll_table; - entry_size =8 ; + entry_size = abi->cte_esz; device_type =GITS_BASER_TYPE_COLLECTION ; clearbits =GITS_BASER_INDIRECT ;break ;@@ -1426,7 +1454,6 @@ static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its) (GIC_BASER_CACHEABILITY (GITS_BASER ,INNER , RaWb) | \GIC_BASER_CACHEABILITY (GITS_BASER ,OUTER , SameAsInner) | \GIC_BASER_SHAREABILITY (GITS_BASER , InnerShareable) | \ - ((8ULL -1 ) <<GITS_BASER_ENTRY_SIZE_SHIFT ) | \ GITS_BASER_PAGE_SIZE_64K)#define INITIAL_PROPBASER_VALUE \@@ -1466,7 +1493,7 @@ static int vgic_its_create(struct kvm_device *dev, u32 type) dev->private = its; -return 0 ; +return vgic_its_set_abi (its,MAX_ABI_REV ); }static void vgic_its_destroy (struct kvm_device *kvm_dev)@@ -1584,6 +1611,63 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } +/** +* vgic_its_save_tables_v0 - Save the ITS tables into guest ARM +* according to v0 ABI +*/ +static int vgic_its_save_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM +* to internal data structs according to V0 ABI +* +*/ +static int vgic_its_restore_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +static int vgic_its_commit_v0 (struct vgic_its *its) +{ +const struct vgic_its_abi *abi; + + abi =vgic_its_get_abi (its); + its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; + its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; + + its->baser_coll_table |= (GIC_ENCODE_SZ (abi->cte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); + + its->baser_device_table |= (GIC_ENCODE_SZ (abi->dte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); +return 0 ; +} + +static const struct vgic_its_abi its_table_abi_versions[MAX_ABI_REV +1 ] = { + {.cte_esz =8 , .dte_esz =8 , .ite_esz =8 , + .save_tables = vgic_its_save_tables_v0, + .restore_tables = vgic_its_restore_tables_v0, + .commit = vgic_its_commit_v0, + }, +}; + +inline const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its) +{ +return &its_table_abi_versions[its->abi_rev]; +} + +int vgic_its_set_abi (struct vgic_its *its,int rev) +{ +const struct vgic_its_abi *abi; + + its->abi_rev = rev; + abi =vgic_its_get_abi (its); +return abi->commit (its); +} +static int vgic_its_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {Signature · 1 line
2.5.5
Re: [PATCH v6 12/24] KVM: arm64: vgic-its: Interpret MAPD ITT_addr field
In reply to Eric AugerQuoted 5 lines · Eric Auger
Up to now the MAPD
ITT_addrhad been ignored. We will need it for save/restore. Let's record it in theits_devicestruct.Signed-off-by Eric Auger <[email protected]>
Quoted 53 lines
---
v5 -> v6:
- fixits_cmd_get_ittaddrand pass 44 toits_cmd_mask_fieldv4 -> v5:
-its_cmd_get_ittaddrmacro now returns the actual ITT GPAv3 -> v4:
- invgic_its_cmd_handle_mapd,itt_addrdirectly is shifted
- correct ittaddr bitmask to support 48bit GPA--- virt/kvm/arm/vgic/vgic-its.c | 4 ++++ 1 file changed, 4 insertions(+)virt/kvm/arm/vgic/vgic-its.c +4
-0@@ -106,6 +106,7 @@ struct its_device {/* the head for the list of ITTEs */ struct list_head itt_head;u32 num_eventid_bits; +gpa_t itt_addr;u32 device_id; };@@ -576,6 +577,7 @@ static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)#define its_cmd_get_id (cmd) its_cmd_mask_field(cmd, 1, 0, 32)#define its_cmd_get_physical_id (cmd) its_cmd_mask_field(cmd, 1, 32, 32)#define its_cmd_get_collection (cmd) its_cmd_mask_field(cmd, 2, 0, 16) +#define its_cmd_get_ittaddr (cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8)#define its_cmd_get_target_addr (cmd) its_cmd_mask_field(cmd, 2, 16, 32)#define its_cmd_get_validbit (cmd) its_cmd_mask_field(cmd, 2, 63, 1)@@ -847,6 +849,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,u32 device_id =its_cmd_get_deviceid (its_cmd);bool valid =its_cmd_get_validbit (its_cmd);u8 num_eventid_bits =its_cmd_get_size (its_cmd); +gpa_t itt_addr =its_cmd_get_ittaddr (its_cmd);struct its_device *device;if (!vgic_its_check_id (its, its->baser_device_table, device_id))@@ -878,6 +881,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, device->device_id = device_id; device->num_eventid_bits = num_eventid_bits; + device->itt_addr = itt_addr;INIT_LIST_HEAD (&device->itt_head);Signature · 1 line
2.5.5
Re: [PATCH v6 13/24] KVM: arm64: vgic-its: Check the device id matches TYPER DEVBITS range
In reply to Eric AugerQuoted 58 lines · Eric Auger
On MAPD we currently check the device id can be stored in the device table. Let's first check it can be encoded within the range defined by TYPER DEVBITS.
Also check the collection ID belongs to the 16 bit range as
GITS_TYPERCIL field equals to 0.Signed-off-by Eric Auger <[email protected]>---
v4 -> v5:
- useGIC_ENCODE_SZmacrov3 -> v4:
-VITS_TYPER_DEVBITSset to 16 for homogeneity
- useBIT_ULL--- virt/kvm/arm/vgic/vgic-its.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +6
-4@@ -204,6 +204,7 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define GIC_LPI_OFFSET 8192#define VITS_TYPER_IDBITS 16 +#define VITS_TYPER_DEVBITS 16/* * Finds and returns a collection in the ITS collection table. @@ -404,7 +405,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,* To avoid memory waste in the guest, we keep the number of IDBits and * DevBits low - as least for the time being. */ - reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (VITS_TYPER_DEVBITS ,5 ) <<GITS_TYPER_DEVBITS_SHIFT ; reg |=GIC_ENCODE_SZ (VITS_TYPER_IDBITS ,5 ) <<GITS_TYPER_IDBITS_SHIFT ; reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;@@ -646,16 +647,30 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,* Check whether an ID can be stored into the corresponding guest table. * For a direct table this is pretty easy, but gets a bit nasty for * indirect tables. We check whether the resulting guest physical address -* is actually valid (covered by a memslot and guest accessbible). +* is actually valid (covered by a memslot and guest accessible). * For this we have to read the respective first level entry. */ -static bool vgic_its_check_id (struct vgic_its *its,u64 baser,int id) +static bool vgic_its_check_id (struct vgic_its *its,u64 baser,u32 id) {int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +u64 indirect_ptr, type =GITS_BASER_TYPE (baser); +int esz =GITS_BASER_ENTRY_SIZE (baser);int index; -u64 indirect_ptr;
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, · line 667
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-1gfn_t gfn; -int esz =GITS_BASER_ENTRY_SIZE (baser); + +switch (type) { +case GITS_BASER_TYPE_DEVICE : +if (id >=BIT_ULL (VITS_TYPER_DEVBITS )) +return false; +break ; +case GITS_BASER_TYPE_COLLECTION : +/* as GITS_TYPER.CID == 0, ITS supports 16-bit collection ID */
nit: GITS_TYPE.CIL
Quoted 12 lines
virt/kvm/arm/vgic/vgic-its.c +6
-0+if (id >=BIT_ULL (16 )) +return false; +break ; +default : +return false; + }if (!(baser &GITS_BASER_INDIRECT )) {phys_addr_t addr;Signature · 1 line
2.5.5
Otherwise:
Re: [PATCH v6 13/24] KVM: arm64: vgic-its: Check the device id matches TYPER DEVBITS range
Quoted 63 lines · Christoffer Dall
Quoted 62 lines · Eric Auger
On MAPD we currently check the device id can be stored in the device table. Let's first check it can be encoded within the range defined by TYPER DEVBITS.
Also check the collection ID belongs to the 16 bit range as
GITS_TYPERCIL field equals to 0.Signed-off-by Eric Auger <[email protected]>---
v4 -> v5:
- useGIC_ENCODE_SZmacrov3 -> v4:
-VITS_TYPER_DEVBITSset to 16 for homogeneity
- useBIT_ULL--- virt/kvm/arm/vgic/vgic-its.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +8
-5@@ -204,6 +204,7 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define GIC_LPI_OFFSET 8192#define VITS_TYPER_IDBITS 16 +#define VITS_TYPER_DEVBITS 16/* * Finds and returns a collection in the ITS collection table. @@ -404,7 +405,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,* To avoid memory waste in the guest, we keep the number of IDBits and * DevBits low - as least for the time being. */ - reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (VITS_TYPER_DEVBITS ,5 ) <<GITS_TYPER_DEVBITS_SHIFT ; reg |=GIC_ENCODE_SZ (VITS_TYPER_IDBITS ,5 ) <<GITS_TYPER_IDBITS_SHIFT ; reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;@@ -646,16 +647,30 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,* Check whether an ID can be stored into the corresponding guest table. * For a direct table this is pretty easy, but gets a bit nasty for * indirect tables. We check whether the resulting guest physical address -* is actually valid (covered by a memslot and guest accessbible). +* is actually valid (covered by a memslot and guest accessible). * For this we have to read the respective first level entry. */ -static bool vgic_its_check_id (struct vgic_its *its,u64 baser,int id) +static bool vgic_its_check_id (struct vgic_its *its,u64 baser,u32 id) {int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +u64 indirect_ptr, type =GITS_BASER_TYPE (baser); +int esz =GITS_BASER_ENTRY_SIZE (baser);int index; -u64 indirect_ptr;gfn_t gfn; -int esz =GITS_BASER_ENTRY_SIZE (baser); + +switch (type) {
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, · line 667
Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6
-0+case GITS_BASER_TYPE_DEVICE : +if (id >=BIT_ULL (VITS_TYPER_DEVBITS )) +return false; +break ; +case GITS_BASER_TYPE_COLLECTION : +/* as GITS_TYPER.CID == 0, ITS supports 16-bit collection ID */ nit: GITS_TYPE.CIL
GITS_TYPER.CIL ;-)
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 08/24] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr
In reply to Christoffer DallHi Christoffer,
Quoted 48 lines · Christoffer Dall
Quoted 47 lines · Eric Auger
GITS_CREADRneeds to be restored so let's implement the associateduaccess_write_itscallback. The write only is allowed if the its is disabled.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove usage ofupdate_64bit_regand check alignment of cmd offsetv4 -> v5:
- keep Stalled bit
-vgic_mmio_uaccess_write_its_creadrcan now return an errorv3 -> v4:
-REGISTER_ITS_DESC_UACCESSnow introduced in this patch
- we now check the its is disabled--- virt/kvm/arm/vgic/vgic-its.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +18
-0@@ -1213,6 +1213,34 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,return extract_bytes (its->creadr, addr &0x7 , len); } +static int vgic_mmio_uaccess_write_its_creadr (struct kvm *kvm, +struct vgic_its *its, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 cmd_offset; +int ret =0 ; + +mutex_lock (&its->cmd_lock); + +if (its->enabled) { + ret = -EBUSY ; + goto out; + } + + cmd_offset =ITS_CMD_OFFSET (val); +if ((cmd_offset >=ITS_CMD_BUFFER_SIZE (its->cbaser)) || + (cmd_offset &0x1F )) {
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, · line 1238
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+ ret = -EINVAL ; + goto out; + } + + its->creadr = val;do we let userspace decide the stalled bit or should we use
ITS_CMD_OFFSET()when assigning the field as well?
yes better to use cmd_offset.
Also the alignment check eventually can be removed as cmd_offset is [19:5] of the offset and bits [4:0] are 0
Thanks
Eric
Quoted 51 lines
Quoted 41 lines
virt/kvm/arm/vgic/vgic-its.c +18
-2+out: +mutex_unlock (&its->cmd_lock); +return ret; +} +#define BASER_INDEX (addr) (((addr) / sizeof(u64)) & 0x7) static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, struct vgic_its *its,@@ -1317,6 +1345,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, .its_write = wr, \ } +#define REGISTER_ITS_DESC_UACCESS (off, rd, wr, uwr, length, acc)\ +{ \ + .reg_offset = off, \ + .len = length, \ + .access_flags = acc, \ + .its_read = rd, \ + .its_write = wr, \ + .uaccess_its_write = uwr, \ +} +static void its_mmio_write_wi (struct kvm *kvm,struct vgic_its *its,gpa_t addr,unsigned int len,unsigned long val) {@@ -1339,8 +1377,9 @@ static struct vgic_register_region its_registers[] = {REGISTER_ITS_DESC (GITS_CWRITER , vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), -REGISTER_ITS_DESC (GITS_CREADR , - vgic_mmio_read_its_creadr, its_mmio_write_wi,8 , +REGISTER_ITS_DESC_UACCESS (GITS_CREADR , + vgic_mmio_read_its_creadr, its_mmio_write_wi, + vgic_mmio_uaccess_write_its_creadr,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_ITS_DESC(GITS_BASER, vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,Signature · 1 line
2.5.5Otherwise:
Reviewed-by Christoffer Dall <[email protected]>_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Eric AugerQuoted 8 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].
why into irqchip and not just the vgic header file?
Quoted 85 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +20
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; +
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 10 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); + +spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;
Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 10 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;
Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Do we care?
Thanks,
-Christoffer
Quoted 22 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+ } +return 0 ; +} +/* check for overlapping regions and for regions crossing the end of memory */ static bool vgic_v3_check_base (struct kvm *kvm) {virt/kvm/arm/vgic/vgic.h +1
-0@@ -157,6 +157,7 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v3_enable (struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm); +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);Signature · 1 line
2.5.5
Re: [PATCH v6 09/24] KVM: arm64: vgic-its: Introduce migration ABI infrastructure
In reply to Christoffer DallHi Christoffer,
Quoted 111 lines · Christoffer Dall
Quoted 96 lines · Eric Auger
We plan to support different migration ABIs, ie. characterizing the ITS table layout format in guest RAM. For example, a new ABI will be needed if vLPIs get supported for nested use case.
So let's introduce an array of supported ABIs (at the moment a single ABI is supported though). The following characteristics are foreseen to vary with the ABI: size of table entries, save/restore operation, the way abi settings are applied.
By default the
MAX_ABI_REVis applied on its creation. In subsequent patches we will introduce a way for the userspace to change the ABI in use.The entry sizes now are set according to the ABI version and not hardcoded anymore.
Signed-off-by Eric Auger <[email protected]>---
v6 -> v7:
- rename abi intoits_table_abi_versions
- kernel doc comments for structvgic_its_abi
- commentGIC_ENCODE_SZ
- slighly rephrase the commit messagev5: creation and squash KVM: arm64: ITS: Report the ITE size in
GITS_TYPER--- include/kvm/arm_vgic.h | 3 ++ include/linux/irqchip/arm-gic-v3.h | 5 +++ virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-)include/kvm/arm_vgic.h +3
-0@@ -162,6 +162,9 @@ struct vgic_its {u32 creadr;u32 cwriter; +/* migration ABI revision in use */ +u32 abi_rev; +/* Protects the device and collection lists */ struct mutex its_lock;struct list_head device_list;include/linux/irqchip/arm-gic-v3.h +5
-0@@ -132,6 +132,9 @@#define GIC_BASER_SHAREABILITY (reg, type) \ (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) +/* encode a size field of width @w containing @n - 1 units */ +#define GIC_ENCODE_SZ (n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) +#define GICR_PROPBASER_SHAREABILITY_SHIFT (10)#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7)#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56)@@ -232,6 +235,7 @@#define GITS_CTLR_QUIESCENT (1U << 31)#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)@@ -290,6 +294,7 @@#define GITS_BASER_TYPE (r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7)#define GITS_BASER_ENTRY_SIZE_SHIFT (48)#define GITS_BASER_ENTRY_SIZE (r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) +#define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48)#define GITS_BASER_SHAREABILITY_SHIFT (10)#define GITS_BASER_InnerShareable \ GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)virt/kvm/arm/vgic/vgic-its.c +6
-0@@ -33,6 +33,12 @@#include "vgic.h" #include "vgic-mmio.h" +/* Highest migration ABI revision supported by this code */ +#define MAX_ABI_REV 0 + +static int vgic_its_set_abi (struct vgic_its *its,int rev); +static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +/* * Creates a new (reference to a) struct vgic_irq for a given LPI. * If this LPI is already mapped on another ITS, we increase its refcount @@ -123,6 +129,25 @@ struct its_ite {u32 event_id;virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 139
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-0}; +/** +* struct vgic_its_abi - ITS abi ops and settings +* @cte_esz: collection table entry size +* @dte_esz: device table entry size +* @ite_esz: interrupt translation table entry size +* @save tables: save the ITS tables into guest RAM +* @restore_tables: restore the ITS internal structs from +* tables stored in guest RAM nit: indent to align with the beginning of 'restore...'
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 140
Quoted 1 line
virt/kvm/arm/vgic/vgic-its.c +1
-0+* @commit: apply the ABI settings to the ITS hmm, I already forgot what the commit function does, and reading this text doesn't seem to help me, unfortunately. When is this called, what does it do?
what about:
* @commit: initialize the registers which expose the ABI settings,
* especially the entry sizes
Thanks
Eric
Quoted 146 lines
Thanks,
-ChristofferQuoted 136 lines
virt/kvm/arm/vgic/vgic-its.c +73
-4+*/ +struct vgic_its_abi { +int cte_esz; +int dte_esz; +int ite_esz; +int (*save_tables)(struct vgic_its *its); +int (*restore_tables)(struct vgic_its *its); +int (*commit)(struct vgic_its *its); +}; +/* * Find and returns a device in the device table for an ITS. * Must be called with the its_lock mutex held. @@ -364,6 +389,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, struct vgic_its *its, gpa_t addr, unsigned int len) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 reg =GITS_TYPER_PLPIS ;/* @@ -376,6 +402,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,*/ reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; reg |=0x0f <<GITS_TYPER_IDBITS_SHIFT ; + reg |=GIC_ENCODE_SZ (abi->ite_esz,4 ) <<GITS_TYPER_ITT_ENTRY_SIZE_SHIFT ;return extract_bytes (reg, addr &7 , len); }@@ -1269,6 +1296,7 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm, gpa_t addr, unsigned int len, unsigned long val) { +const struct vgic_its_abi *abi =vgic_its_get_abi (its);u64 entry_size, device_type;u64 reg, *regptr, clearbits =0 ;@@ -1279,12 +1307,12 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,switch (BASER_INDEX (addr)) {case 0 : regptr = &its->baser_device_table; - entry_size =8 ; + entry_size = abi->dte_esz; device_type =GITS_BASER_TYPE_DEVICE ;break ;case 1 : regptr = &its->baser_coll_table; - entry_size =8 ; + entry_size = abi->cte_esz; device_type =GITS_BASER_TYPE_COLLECTION ; clearbits =GITS_BASER_INDIRECT ;break ;@@ -1426,7 +1454,6 @@ static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its) (GIC_BASER_CACHEABILITY (GITS_BASER ,INNER , RaWb) | \GIC_BASER_CACHEABILITY (GITS_BASER ,OUTER , SameAsInner) | \GIC_BASER_SHAREABILITY (GITS_BASER , InnerShareable) | \ - ((8ULL -1 ) <<GITS_BASER_ENTRY_SIZE_SHIFT ) | \ GITS_BASER_PAGE_SIZE_64K)#define INITIAL_PROPBASER_VALUE \@@ -1466,7 +1493,7 @@ static int vgic_its_create(struct kvm_device *dev, u32 type) dev->private = its; -return 0 ; +return vgic_its_set_abi (its,MAX_ABI_REV ); }static void vgic_its_destroy (struct kvm_device *kvm_dev)@@ -1584,6 +1611,63 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } +/** +* vgic_its_save_tables_v0 - Save the ITS tables into guest ARM +* according to v0 ABI +*/ +static int vgic_its_save_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM +* to internal data structs according to V0 ABI +* +*/ +static int vgic_its_restore_tables_v0 (struct vgic_its *its) +{ +return -ENXIO ; +} + +static int vgic_its_commit_v0 (struct vgic_its *its) +{ +const struct vgic_its_abi *abi; + + abi =vgic_its_get_abi (its); + its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; + its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK ; + + its->baser_coll_table |= (GIC_ENCODE_SZ (abi->cte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); + + its->baser_device_table |= (GIC_ENCODE_SZ (abi->dte_esz,5 ) + <<GITS_BASER_ENTRY_SIZE_SHIFT ); +return 0 ; +} + +static const struct vgic_its_abi its_table_abi_versions[MAX_ABI_REV +1 ] = { + {.cte_esz =8 , .dte_esz =8 , .ite_esz =8 , + .save_tables = vgic_its_save_tables_v0, + .restore_tables = vgic_its_restore_tables_v0, + .commit = vgic_its_commit_v0, + }, +}; + +inline const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its) +{ +return &its_table_abi_versions[its->abi_rev]; +} + +int vgic_its_set_abi (struct vgic_its *its,int rev) +{ +const struct vgic_its_abi *abi; + + its->abi_rev = rev; + abi =vgic_its_get_abi (its); +return abi->commit (its); +} +static int vgic_its_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {Signature · 1 line
2.5.5_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 24/24] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
In reply to Eric AugerHi,
Quoted 65 lines · Eric Auger
This patch adds a new attribute to GICV3 KVM device
KVM_DEV_ARM_VGIC_GRP_CTRLgroup. This allows userspace to flush all GICR pending tables into guest RAM.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- fix stored
- GICR_PEND/PROPBASER_ADDRESS were already introducedv4 -> v5:
- move pending table save code/ctrl into VGICv3 instead of ITS
- remove uselessgpa_tcast
- use the same optimization as inits_sync_lpi_pending_tablev3 -> v4:
- remove the wrong comment about locking
- pass kvm struct instead of its handle
- add comment about restore method
- remove GITR_PENDABASER.PTZ check
- continue iftarget_vcpu== NULL
- new locking strategyv1 -> v2:
- do not care about the 1st KB which should be zeroed according to the spec.--- arch/arm/include/uapi/asm/kvm.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 1 + include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-kvm-device.c | 20 +++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 51 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 6 files changed, 75 insertions(+)arch/arm/include/uapi/asm/kvm.h +1
-0@@ -202,6 +202,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -222,6 +222,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0include/linux/irqchip/arm-gic-v3.h +0
-0
include/linux/irqchip/arm-gic-v3.h line 189
Quoted 4 lines
include/linux/irqchip/arm-gic-v3.h +1
-0@@ -186,6 +186,7 @@#define GICR_PENDBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWaWb)#define GICR_PENDBASER_PTZ BIT_ULL(62) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))
This should not be part of this patch.
Eric
Quoted 116 lines
include/linux/irqchip/arm-gic-v3.h +0
-0/* * Re-Distributor registers, offsets from SGI_base virt/kvm/arm/vgic/vgic-kvm-device.c +20
-0@@ -580,6 +580,24 @@ static int vgic_v3_set_attr(struct kvm_device *dev, reg = tmp32;return vgic_v3_attr_regs_access (dev, attr, ®, true); } +case KVM_DEV_ARM_VGIC_GRP_CTRL : { +int ret; + +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +mutex_lock (&dev->kvm->lock); + +if (!lock_all_vcpus (dev->kvm)) { +mutex_unlock (&dev->kvm->lock); +return -EBUSY ; + } + ret =vgic_v3_save_pending_tables (dev->kvm); +unlock_all_vcpus (dev->kvm); +mutex_unlock (&dev->kvm->lock); +return ret; + } +break ; + } }return -ENXIO ; }@@ -658,6 +676,8 @@ static int vgic_v3_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_ARM_VGIC_CTRL_INIT :return 0 ; +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +return 0 ; } }return -ENXIO ;virt/kvm/arm/vgic/vgic-v3.c +51
-0@@ -296,6 +296,57 @@ int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq)return 0 ; } +/** +* vgic_its_save_pending_tables - Save the pending tables into guest RAM +* kvm lock and all vcpu lock must be held +*/ +int vgic_v3_save_pending_tables (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +int last_byte_offset =-1 ; +struct vgic_irq *irq; +int ret; + +list_for_each_entry (irq, &dist->lpi_list_head, lpi_list) { +int byte_offset, bit_nr; +struct kvm_vcpu *vcpu; +gpa_t pendbase, ptr; +bool stored; +u8 val; + + vcpu = irq->target_vcpu; +if (!vcpu) +continue ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + +if (byte_offset != last_byte_offset) { + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + last_byte_offset = byte_offset; + } + + stored = val & (1U << bit_nr); +if (stored == irq->pending_latch) +continue ; + +if (irq->pending_latch) + val |=1 << bit_nr; +else + val &= ~(1 << bit_nr); + + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + } +return 0 ; +} +/* check for overlapping regions and for regions crossing the end of memory */ static bool vgic_v3_check_base (struct kvm *kvm) {virt/kvm/arm/vgic/vgic.h +1
-0@@ -181,6 +181,7 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm);int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq); +int vgic_v3_save_pending_tables (struct kvm *kvm);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);
Re: [PATCH v6 11/24] KVM: arm64: vgic-its: Interpret MAPD Size field and check related errors
In reply to Eric AugerQuoted 9 lines · Eric Auger
Up to now the MAPD's ITT size field has been ignored. It encodes the number of eventid bit minus 1. It should be used to check the eventid when a MAPTI command is issued on a device. Let's store the number of eventid bits in the
its_deviceand do the check on MAPTI. Also make sure the ITT size field does not exceed theGITS_TYPERIDBITS field.Signed-off-by Eric Auger <[email protected]>Reviewed-by Christoffer Dall <[email protected]>
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 09/24] KVM: arm64: vgic-its: Introduce migration ABI infrastructure
In reply to Auger EricQuoted 114 lines · Auger Eric
Hi Christoffer,
Quoted 111 lines · Christoffer Dall
Quoted 96 lines · Eric Auger
We plan to support different migration ABIs, ie. characterizing the ITS table layout format in guest RAM. For example, a new ABI will be needed if vLPIs get supported for nested use case.
So let's introduce an array of supported ABIs (at the moment a single ABI is supported though). The following characteristics are foreseen to vary with the ABI: size of table entries, save/restore operation, the way abi settings are applied.
By default the
MAX_ABI_REVis applied on its creation. In subsequent patches we will introduce a way for the userspace to change the ABI in use.The entry sizes now are set according to the ABI version and not hardcoded anymore.
Signed-off-by Eric Auger <[email protected]>---
v6 -> v7:
- rename abi intoits_table_abi_versions
- kernel doc comments for structvgic_its_abi
- commentGIC_ENCODE_SZ
- slighly rephrase the commit messagev5: creation and squash KVM: arm64: ITS: Report the ITE size in
GITS_TYPER--- include/kvm/arm_vgic.h | 3 ++ include/linux/irqchip/arm-gic-v3.h | 5 +++ virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-)include/kvm/arm_vgic.h +3
-0@@ -162,6 +162,9 @@ struct vgic_its {u32 creadr;u32 cwriter; +/* migration ABI revision in use */ +u32 abi_rev; +/* Protects the device and collection lists */ struct mutex its_lock;struct list_head device_list;include/linux/irqchip/arm-gic-v3.h +5
-0@@ -132,6 +132,9 @@#define GIC_BASER_SHAREABILITY (reg, type) \ (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) +/* encode a size field of width @w containing @n - 1 units */ +#define GIC_ENCODE_SZ (n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) +#define GICR_PROPBASER_SHAREABILITY_SHIFT (10)#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7)#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56)@@ -232,6 +235,7 @@#define GITS_CTLR_QUIESCENT (1U << 31)#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)@@ -290,6 +294,7 @@#define GITS_BASER_TYPE (r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7)#define GITS_BASER_ENTRY_SIZE_SHIFT (48)#define GITS_BASER_ENTRY_SIZE (r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) +#define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48)#define GITS_BASER_SHAREABILITY_SHIFT (10)#define GITS_BASER_InnerShareable \ GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)virt/kvm/arm/vgic/vgic-its.c +6
-0@@ -33,6 +33,12 @@#include "vgic.h" #include "vgic-mmio.h" +/* Highest migration ABI revision supported by this code */ +#define MAX_ABI_REV 0 + +static int vgic_its_set_abi (struct vgic_its *its,int rev); +static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +/* * Creates a new (reference to a) struct vgic_irq for a given LPI. * If this LPI is already mapped on another ITS, we increase its refcount @@ -123,6 +129,25 @@ struct its_ite {u32 event_id;virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 139
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-0}; +/** +* struct vgic_its_abi - ITS abi ops and settings +* @cte_esz: collection table entry size +* @dte_esz: device table entry size +* @ite_esz: interrupt translation table entry size +* @save tables: save the ITS tables into guest RAM +* @restore_tables: restore the ITS internal structs from +* tables stored in guest RAM nit: indent to align with the beginning of 'restore...'
Quoted 9 lines
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c struct its_ite { · line 140
Quoted 1 line
virt/kvm/arm/vgic/vgic-its.c +1
-0+* @commit: apply the ABI settings to the ITS hmm, I already forgot what the commit function does, and reading this text doesn't seem to help me, unfortunately. When is this called, what does it do?
what about:
* @commit: initialize the registers which expose the ABI settings,
* especially the entry sizes
Ah yes, much better.
Thanks,
-Christoffer
Re: [PATCH v6 12/24] KVM: arm64: vgic-its: Interpret MAPD ITT_addr field
In reply to Eric AugerQuoted 4 lines · Eric Auger
Up to now the MAPD
ITT_addrhad been ignored. We will need it for save/restore. Let's record it in theits_devicestruct.Signed-off-by Eric Auger <[email protected]>
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Christoffer DallHi,
Quoted 11 lines · Christoffer Dall
Quoted 8 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Quoted 92 lines
Quoted 92 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +26
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); +
Quoted 8 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Yes but on the other hand, there is a risk the target_vcpu has changed, isn't it? So the alternative you be to return -EBUSY. and caller, ie. its_add_lpi would return that error?
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
Thanks
Eric
Quoted 29 lines
Do we care?
Thanks,
-ChristofferQuoted 22 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+ } +return 0 ; +} +/* check for overlapping regions and for regions crossing the end of memory */ static bool vgic_v3_check_base (struct kvm *kvm) {virt/kvm/arm/vgic/vgic.h +1
-0@@ -157,6 +157,7 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v3_enable (struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm); +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);Signature · 1 line
2.5.5
Re: [PATCH v6 15/24] KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
In reply to Eric AugerQuoted 48 lines · Eric Auger
When creating the lpi we now ask the redistributor what is the state of the LPI (priority, enabled, pending).
Signed-off-by Eric Auger <[email protected]>---
v6: creation
--- virt/kvm/arm/vgic/vgic-its.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)virt/kvm/arm/vgic/vgic-its.c +6
-1@@ -38,6 +38,8 @@static int vgic_its_set_abi (struct vgic_its *its,int rev);static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq, +struct kvm_vcpu *filter_vcpu);/* * Creates a new (reference to a) struct vgic_irq for a given LPI. @@ -46,10 +48,12 @@ static const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its);* If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure. */ -static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid) +static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid, +struct kvm_vcpu *vcpu) {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm,NULL , intid), *oldirq; +int ret;/* In this case there is no put, since we keep the reference. */ if (irq)@@ -66,6 +70,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) irq->config =VGIC_CONFIG_EDGE ;kref_init (&irq->refcount); irq->intid = intid; + irq->target_vcpu = vcpu;spin_lock (&dist->lpi_list_lock);
virt/kvm/arm/vgic/vgic-its.c static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) · line 106
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +2
-0@@ -97,6 +102,19 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) out_unlock:spin_unlock (&dist->lpi_list_lock); +/* +* We "cache" the configuration table entries in out struct vgic_irq's.
s/out/our/ ?
Quoted 23 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+* However we only have those structs for mapped IRQs, so we read in +* the respective config data from memory here upon mapping the LPI. +*/ + ret =update_lpi_config (kvm, irq,NULL ); +if (ret) +return ERR_PTR (ret); + + ret =vgic_v3_lpi_sync_pending_status (kvm, irq); +if (ret) +return ERR_PTR (ret); +return irq; }@@ -769,6 +787,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct its_ite *ite; +struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · line 839
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +4
-1@@ -814,7 +833,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, ite->collection = collection; ite->lpi = lpi_nr; - irq =vgic_add_lpi (kvm, lpi_nr); +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_nr, vcpu);
So, if we don't have the collection and therefore end up with irq->vcpu == NULL, how do we ever read the pending bit from memory as the affinity may later change?
Is this a problem with our idea of only looking at the pending bit on vgic_add_lpi, or does it just mean that we should sample the pending bit whenever we move LPIs around to VCPUs and if the bit is set, then also set the pennding_latch (if not already set), or what should happen here?
I think in any case, we should have a comment here explaining that vcpu configuration data gets picked up later?
Thanks,
-Christoffer
Quoted 22 lines
virt/kvm/arm/vgic/vgic-its.c +0
-9if (IS_ERR (irq)) {if (new_coll)vgic_its_free_collection (its, coll_id);@@ -823,15 +845,6 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, } ite->irq = irq; -update_affinity_ite (kvm, ite); - -/* -* We "cache" the configuration table entries in out struct vgic_irq's. -* However we only have those structs for mapped IRQs, so we read in -* the respective config data from memory here upon mapping the LPI. -*/ -update_lpi_config (kvm, ite->irq,NULL ); -return 0 ; }Signature · 1 line
2.5.5
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Auger EricQuoted 8 lines · Auger Eric
Hi,
Quoted 5 lines · Christoffer Dall
Quoted 4 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Yeah, I'm fine with that. Having all of the HW description in one single place makes sense.
Quoted 100 lines
Quoted 92 lines
Quoted 92 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +26
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); +Quoted 8 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Quoted 3 lines
Yes but on the other hand, there is a risk the
target_vcpuhas changed, isn't it? So the alternative you be to return-EBUSY. and caller, ie.its_add_lpiwould return that error?
For the guest to be changing the LPI configuration, it would take a command to be issued. If there is a concern that we're racing against a command, can we take the command queue mutex?
Quoted 14 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Quoted 5 lines
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
1) the guest should never write to the pending table.
2) if there is an interrupt being injected, it will be made pending in the irq structure, and not in the PT.
So I don't really see what the race is here...
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
Quoted 121 lines · Marc Zyngier
Quoted 8 lines · Auger Eric
Hi,
Quoted 5 lines · Christoffer Dall
Quoted 4 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Yeah, I'm fine with that. Having all of the HW description in one single place makes sense.
Quoted 100 lines
Quoted 100 lines
Quoted 86 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +20
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; +virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 10 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); + +spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Quoted 8 lines
Quoted 3 lines
Yes but on the other hand, there is a risk the
target_vcpuhas changed, isn't it? So the alternative you be to return-EBUSY. and caller, ie.its_add_lpiwould return that error?For the guest to be changing the LPI configuration, it would take a command to be issued. If there is a concern that we're racing against a command, can we take the command queue mutex?
So this is a redistributor thing, not an ITS thing, so I'd prefer not solving it that way.
I was just thinking if we should do a limit of the number of times we'll do this or check if we have a pending signal and just return, but actually, I don't think this is a problem, because the path that keeps changing the configuration will eventually run out of CPU resources and this thread would have forward progress.
Quoted 17 lines
Quoted 17 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
Quoted 6 lines
Quoted 2 lines
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
1) the guest should never write to the pending table.
2) if there is an interrupt being injected, it will be made pending in the irq structure, and not in the PT.
If you have two separate cores running this function at the same time, trying to clear each a bit in the same word, wouldn't you loose one of the cleared bits?
Maybe that can never happen because the commands would be serialized and we should be restoring the system in serial as well?
Thanks,
-Christoffer
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
Quoted 126 lines · Christoffer Dall
Quoted 125 lines · Marc Zyngier
Quoted 8 lines · Auger Eric
Hi,
Quoted 5 lines · Christoffer Dall
Quoted 4 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Yeah, I'm fine with that. Having all of the HW description in one single place makes sense.
Quoted 103 lines
Quoted 92 lines
Quoted 92 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +26
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); +Quoted 8 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Yes but on the other hand, there is a risk the
target_vcpuhas changed, isn't it? So the alternative you be to return-EBUSY. and caller, ie.its_add_lpiwould return that error?
Quoted 7 lines
Quoted 4 lines
For the guest to be changing the LPI configuration, it would take a command to be issued. If there is a concern that we're racing against a command, can we take the command queue mutex?
So this is a redistributor thing, not an ITS thing, so I'd prefer not solving it that way.
Indeed.
Quoted 5 lines
I was just thinking if we should do a limit of the number of times we'll do this or check if we have a pending signal and just return, but actually, I don't think this is a problem, because the path that keeps changing the configuration will eventually run out of CPU resources and this thread would have forward progress.
Yes, assuming we're not holding any other spinlock on the same path. Looking at the code, we only seem to come here from handling MAPI/MAPTI, so we should be good.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
Quoted 7 lines
Quoted 3 lines
1) the guest should never write to the pending table.
2) if there is an interrupt being injected, it will be made pending in the irq structure, and not in the PT.If you have two separate cores running this function at the same time, trying to clear each a bit in the same word, wouldn't you loose one of the cleared bits?
Ah, I see what you mean now (I was obviously looking at the wrong end of the problem).
Quoted 2 lines
Maybe that can never happen because the commands would be serialized and we should be restoring the system in serial as well?
I think that should be the case. What does it mean to do two restore in parallel? We should probably enforce this. Eric?
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 16/24] KVM: arm64: vgic-its: KVM_DEV_ARM_ITS_SAVE/RESTORE_TABLES
In reply to Eric AugerQuoted 164 lines · Eric Auger
Introduce new attributes in
KVM_DEV_ARM_VGIC_GRP_CTRLgroup:
-KVM_DEV_ARM_ITS_SAVE_TABLES: saves the ITS tables into guest RAM
-KVM_DEV_ARM_ITS_RESTORE_TABLES: restores them into VGIC internal structures.We hold the vcpus lock during the save and restore to make sure no vcpu is running.
At this stage the functionality is not yet implemented. Only the skeleton is put in place.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove the pending table sync from the ITS table restorev4 -> v5:
- useKVM_DEV_ARM_ITS_SAVE_TABLESandKVM_DEV_ARM_ITS_RESTORE_TABLES
- rename *flush* into *save*
- callits_sync_lpi_pending_tableat the end of restore
- use abi frameworkv3 -> v4:
- pass kvm struct handle to vgic_its_flush/restore_pending_tables
- take the kvm lock and vcpu locks
- ABI revision check
- checkattr->attris nullv1 -> v2:
- remove useless kvm parameter--- arch/arm/include/uapi/asm/kvm.h | 4 +- arch/arm64/include/uapi/asm/kvm.h | 4 +- virt/kvm/arm/vgic/vgic-its.c | 101 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 103 insertions(+), 6 deletions(-)arch/arm/include/uapi/asm/kvm.h +3
-1@@ -199,7 +199,9 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 -#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_ITS_SAVE_TABLES 1 +#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24arch/arm64/include/uapi/asm/kvm.h +3
-1@@ -219,7 +219,9 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 -#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_ITS_SAVE_TABLES 1 +#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0virt/kvm/arm/vgic/vgic-its.c +75
-2@@ -1669,12 +1669,68 @@ int vgic_its_attr_regs_access(struct kvm_device *dev, }/** +* vgic_its_save_device_tables - Save the device table and all ITT +* into guest RAM +*/ +static int vgic_its_save_device_tables (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_restore_device_tables - Restore the device table and all ITT +* from guest RAM to internal data structs +*/ +static int vgic_its_restore_device_tables (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_collection_table - Save the collection table into +* guest RAM +*/ +static int vgic_its_save_collection_table (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** +* vgic_its_restore_collection_table - reads the collection table +* in guest memory and restores the ITS internal state. Requires the +* BASER registers to be restored before. +*/ +static int vgic_its_restore_collection_table (struct vgic_its *its) +{ +return -ENXIO ; +} + +/** * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM * according to v0 ABI */ static int vgic_its_save_tables_v0 (struct vgic_its *its) { -return -ENXIO ; +struct kvm *kvm = its->dev->kvm; +int ret; + +mutex_lock (&kvm->lock); + +if (!lock_all_vcpus (kvm)) { +mutex_unlock (&kvm->lock); +return -EBUSY ; + } + + ret =vgic_its_save_device_tables (its); +if (ret) + goto out; + + ret =vgic_its_save_collection_table (its); + +out: +unlock_all_vcpus (kvm); +mutex_unlock (&kvm->lock); +return ret; }/** @@ -1684,7 +1740,34 @@ static int vgic_its_save_tables_v0(struct vgic_its *its)*/ static int vgic_its_restore_tables_v0 (struct vgic_its *its) { -return -ENXIO ; +struct kvm *kvm = its->dev->kvm; +int ret; + +mutex_lock (&kvm->lock); + +if (!lock_all_vcpus (kvm)) { +mutex_unlock (&kvm->lock); +return -EBUSY ; + } + + ret =vgic_its_restore_collection_table (its); +if (ret) + goto out; + + ret =vgic_its_restore_device_tables (its); + +out: +unlock_all_vcpus (kvm);
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_tables_v0(struct vgic_its *its) · line 1770
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+mutex_unlock (&kvm->lock); + +if (ret) +return ret; + +/* +* On restore path, MSI injections can happen before the +* first VCPU run so let's complete the GIC init here. +*/ +return kvm_vgic_map_resources (its->dev->kvm);
So not sure where we left this one in our other discussion. I feel like this is a bit out of place currently, and registering the iodevs should rather be done when setting the base addresses used by that, but I think we can fix that later, or are we implying ordering towards userspace here?
Marc, any thoughts?
Thanks,
-Christoffer
Re: [PATCH v6 17/24] KVM: arm64: vgic-its: vgic_its_alloc_ite/device
In reply to Eric AugerQuoted 139 lines · Eric Auger
Add two new helpers to allocate an its ite and an its device. This will avoid duplication on restore path.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove Marc's A-b
- both functions now return the newly allocated structv4 -> v5:
- add Marc's A-bv3 -> v4:
- fix allocation
- add comment aboutits_lockmutex holdv1 -> v2:
- reportitt_sizefix and removeITE_SIZE
- s/itte/ite/g--- virt/kvm/arm/vgic/vgic-its.c | 68 ++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 21 deletions(-)virt/kvm/arm/vgic/vgic-its.c +47
-21@@ -776,6 +776,25 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)kfree (collection); } +/* Must be called with its_lock mutex held */ +static struct its_ite *vgic_its_alloc_ite (struct its_device *device, +struct its_collection *collection, +u32 lpi_id,u32 event_id) +{ +struct its_ite *ite; + + ite =kzalloc (sizeof (*ite),GFP_KERNEL ); +if (!ite) +return ERR_PTR (-ENOMEM ); + + ite->event_id = event_id; + ite->collection = collection; + ite->lpi = lpi_id; + +list_add_tail (&ite->ite_list, &device->itt_head); +return ite; +} +/* * The MAPTI and MAPI commands map LPIs to ITTEs. * Must be called with its_lock mutex held. @@ -790,8 +809,8 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ; -int lpi_nr;struct vgic_irq *irq; +int lpi_nr; device =find_its_device (its, device_id);if (!device)@@ -820,19 +839,13 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, new_coll = collection; } - ite =kzalloc (sizeof (struct its_ite ),GFP_KERNEL ); -if (!ite) { + ite =vgic_its_alloc_ite (device, collection, lpi_nr, event_id); +if (IS_ERR (ite)) {if (new_coll)vgic_its_free_collection (its, coll_id); -return -ENOMEM ; +return PTR_ERR (ite); } - ite->event_id = event_id; -list_add_tail (&ite->ite_list, &device->itt_head); - - ite->collection = collection; - ite->lpi = lpi_nr; -if (its_is_collection_mapped (collection)) vcpu =kvm_get_vcpu (kvm, collection->target_addr);@@ -865,6 +878,26 @@ static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)kfree (device); } +/* Must be called with its_lock mutex held */ +static struct its_device *vgic_its_alloc_device (struct vgic_its *its, +u32 device_id,gpa_t itt_addr, +u8 num_eventid_bits) +{ +struct its_device *device; + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) +return ERR_PTR (-ENOMEM ); + + device->device_id = device_id; + device->itt_addr = itt_addr; + device->num_eventid_bits = num_eventid_bits; +INIT_LIST_HEAD (&device->itt_head); + +list_add_tail (&device->dev_list, &its->device_list); +return device; +} +/* * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). * Must be called with the its_lock mutex held. @@ -901,17 +934,10 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,if (!valid)return 0 ; - device =kzalloc (sizeof (struct its_device ),GFP_KERNEL ); -if (!device) -return -ENOMEM ; - - device->device_id = device_id; - device->num_eventid_bits = num_eventid_bits; - device->itt_addr = itt_addr; - -INIT_LIST_HEAD (&device->itt_head); - -list_add_tail (&device->dev_list, &its->device_list); + device =vgic_its_alloc_device (its, device_id, itt_addr, + num_eventid_bits); +if (IS_ERR (device)) +return PTR_ERR (device);return 0 ; }Signature · 1 line
2.5.5
Re: [PATCH v6 18/24] KVM: arm64: vgic-its: Add infrastructure for table lookup
In reply to Eric AugerQuoted 147 lines · Eric Auger
Add a generic
scan_its_table()helper whose role consists in scanning a contiguous table located in guest RAM and applying a callback on each entry. Entries can be handled as linked lists since the callback may return an id offset to the next entry and also indicate whether the entry is the last one.Helper functions also are added to compute the device/event ID offset to the next DTE/ITE.
compute_next_devid_offset,compute_next_eventid_offsetandscan_tablewill become static in subsequent patchesSigned-off-by Eric Auger <[email protected]>---
v5 -> v6:
- add comments about 2d level table scan as suggested by Christoffer
- s/lookup_table/scan_its_table
-scan_tablenow return 0 if last elt found and +1 if not found
- use int for id
- simplify compute_next_devid/eventid_offsetv4 -> v5:
- usekvm_read_guestv3 -> v4:
- remove static to avoid compilation warning
- correct size computation inlooup_table()
- defines now encode the number of bits used for devid and eventid offsets
- useBIT()- 1 to encode the max offets--- virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)virt/kvm/arm/vgic/vgic-its.c +92
-0@@ -221,6 +221,8 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,#define VITS_TYPER_IDBITS 16#define VITS_TYPER_DEVBITS 16 +#define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) +#define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1)/* * Finds and returns a collection in the ITS collection table. @@ -1694,6 +1696,96 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } +u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +{ +struct its_device *next; +u32 next_offset; + +if (list_is_last (&dev->dev_list, h)) +return 0 ; + next =list_next_entry (dev, dev_list); + next_offset = next->device_id - dev->device_id; + +return min_t (u32, next_offset,VITS_DTE_MAX_DEVID_OFFSET ); +} + +u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) +{ +struct its_ite *next; +u32 next_offset; + +if (list_is_last (&ite->ite_list, h)) +return 0 ; + next =list_next_entry (ite, ite_list); + next_offset = next->event_id - ite->event_id; + +return min_t (u32, next_offset,VITS_ITE_MAX_EVENTID_OFFSET ); +} + +/** +* entry_fn_t - Callback called on a table entry restore path +* @its: its handle +* @id: id of the entry +* @entry: pointer to the entry +* @opaque: pointer to an opaque data +* +* Return: < 0 on error, 0 if last element was identified, id offset to next +* element otherwise +*/ +typedef int (*entry_fn_t )(struct vgic_its *its,u32 id,void *entry, +void *opaque); + +/** +* scan_its_table - Scan a contiguous table in guest RAM and applies a function +* to each entry +* +* @its: its handle +* @base: base gpa of the table +* @size: size of the table in bytes +* @esz: entry size in bytes +* @start_id: the ID of the first entry in the table +* (non zero for 2d level tables) +* @fn: function to apply on each entry +* +* Return: < 0 on error, 0 if last element was identified, 1 otherwise +* (the last element may not be found on second level tables) +*/ +int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) +{ +void *entry =kzalloc (esz,GFP_KERNEL ); +struct kvm *kvm = its->dev->kvm; +unsigned long len = size; +int id = start_id; +gpa_t gpa = base; +int ret; + +while (len >0 ) { +int next_offset; +size_t byte_offset; + + ret =kvm_read_guest (kvm, gpa, entry, esz); +if (ret) + goto out; + + next_offset =fn (its, id, entry, opaque); +if (next_offset <=0 ) { + ret = next_offset; + goto out; + } + + byte_offset = next_offset * esz; + id += next_offset; + gpa += byte_offset; + len -= byte_offset; + } + ret =1 ; + +out: +kfree (entry); +return ret; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM Signature · 1 line
2.5.5
This is much more readable than before. Thanks for making these changes.
Re: [PATCH v6 19/24] KVM: arm64: vgic-its: Collection table save/restore
In reply to Eric AugerQuoted 106 lines · Eric Auger
The save path copies the collection entries into guest RAM at the GPA specified in the BASER register. This obviously requires the BASER to be set. The last written element is a dummy collection table entry.
We do not index by collection ID as the collection entry can fit into 8 bytes while containing the collection ID.
On restore path we re-allocate the collection objects.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove the valid pointer fromvgic_its_restore_cteand return +1 if no error and last elt not found
- check the BASER valid bit
- addBUG_ONto check esz versus val sizev4 -> v5:
- add macros for field encoding/decoding
- useabi->cte_esz
- rename flush into save
- check thetarget_addris validv3 -> v4:
- replaced u64 *ptr bygpa_tgpa
- check the collection does not exist before allocating itv1 -> v2:
- reword commit message and directly use 8 as entry size
- no kvm parameter anymore
- add helper for flush/restore cte
- table size computed here
- add le64/cpu conversions--- virt/kvm/arm/vgic/vgic-its.c | 102 ++++++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 9 ++++ 2 files changed, 109 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +51
-1@@ -1804,13 +1804,91 @@ static int vgic_its_restore_device_tables(struct vgic_its *its)return -ENXIO ; } +static int vgic_its_save_cte (struct vgic_its *its, +struct its_collection *collection, +gpa_t gpa,int esz) +{ +u64 val; + + val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | + ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | + collection->collection_id); + val =cpu_to_le64 (val); +return kvm_write_guest (its->dev->kvm, gpa, &val, esz); +} + +static int vgic_its_restore_cte (struct vgic_its *its,gpa_t gpa,int esz) +{ +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +u32 target_addr, coll_id; +u64 val; +int ret; + +BUG_ON (esz >sizeof (val)); + ret =kvm_read_guest (kvm, gpa, &val, esz); +if (ret) +return ret; + val =le64_to_cpu (val); +if (!(val &KVM_ITS_CTE_VALID_MASK )) +return 0 ; + + target_addr = (u32)(val >>KVM_ITS_CTE_RDBASE_SHIFT ); + coll_id = val &KVM_ITS_CTE_ICID_MASK ; + +if (target_addr >=atomic_read (&kvm->online_vcpus)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (collection) +return -EEXIST ; + ret =vgic_its_alloc_collection (its, &collection, coll_id); +if (ret) +return ret; + collection->target_addr = target_addr; +return 1 ; +} +/** * vgic_its_save_collection_table - Save the collection table into * guest RAM */ static int vgic_its_save_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_collection *collection; +u64 val; +gpa_t gpa; +size_t max_size, filled =0 ; +int ret, cte_esz = abi->cte_esz;
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_restore_device_tables(struct vgic_its *its) · line 1873
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ + gpa =BASER_ADDRESS (its->baser_coll_table); +if (!gpa) +return 0 ; + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +list_for_each_entry (collection, &its->collection_list, coll_list) { +if (filled == max_size) +return -ENOSPC ;
should this ever happen? Shouldn't we check limit in vgic_its_cmd_handle_mapi() ?
We can fix that later, and it shouldn't block this patch, just asking the question here because I'm noticing it.
Quoted 73 lines
virt/kvm/arm/vgic/vgic-its.c +39
-1+ ret =vgic_its_save_cte (its, collection, gpa, cte_esz); +if (ret) +return ret; + gpa += cte_esz; + filled += cte_esz; + } + +if (filled == max_size) +return 0 ; + +/* +* table is not fully filled, add a last dummy element +* with valid bit unset +*/ + val =0 ; +BUG_ON (cte_esz >sizeof (val)); + ret =kvm_write_guest (its->dev->kvm, gpa, &val, cte_esz); +return ret; }/** @@ -1820,7 +1898,27 @@ static int vgic_its_save_collection_table(struct vgic_its *its)*/ static int vgic_its_restore_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int cte_esz = abi->cte_esz; +size_t max_size, read =0 ; +gpa_t gpa; +int ret; + +if (!(its->baser_coll_table &GITS_BASER_VALID )) +return 0 ; + + gpa =BASER_ADDRESS (its->baser_coll_table); + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +while (read < max_size) { + ret =vgic_its_restore_cte (its, gpa, cte_esz); +if (ret <=0 ) +break ; + gpa += cte_esz; + read += cte_esz; + } +return ret; }/** virt/kvm/arm/vgic/vgic.h +9
-0@@ -73,6 +73,15 @@ KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \ KVM_REG_ARM_VGIC_SYSREG_OP2_MASK) +/* +* As per Documentation/virtual/kvm/devices/arm-vgic-its.txt, +* below macros are defined for ITS table entry encoding. +*/ +#define KVM_ITS_CTE_VALID_SHIFT 63 +#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_CTE_RDBASE_SHIFT 16 +#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +static inline bool irq_is_pending (struct vgic_irq *irq) {if (irq->config ==VGIC_CONFIG_EDGE )Signature · 1 line
2.5.5
Re: [PATCH v6 21/24] KVM: arm64: vgic-its: Device table save/restore
In reply to Eric AugerQuoted 194 lines · Eric Auger
This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.
For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch.
On restore, devices are re-allocated and their itt are re-built.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- accomodatevgic_its_alloc_devicechange of proto
- define bit fields for L1 entries
- s/handle_l1_entry/handle_l1_dte
- s/ite_esz/dte_esz inhandle_l1_dte
- check BASER valid bit
- s/nb_eventid_bits/num_eventid_bits
- new convention for returned values
- itt functions implemented in subsequent patchv4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- useabi->dte_esz
- clatify entry sizes for L1 and L2 tablesv3 -> v4:
- use the new proto forits_alloc_device
-compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fixhandle_l1_entryentry handling
- correctvgic_its_table_restoreerror handlingv2 -> v3:
- fixitt_addrbitmask invgic_its_restore_dte
- addition of return 0 invgic_its_restore_itemoved to the ITE related patchv1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing--- virt/kvm/arm/vgic/vgic-its.c | 194 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 10 +++ 2 files changed, 199 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +100
-3@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/list.h> #include <linux/uaccess.h> +#include <linux/list_sort.h> #include <linux/irqchip/arm-gic-v3.h> @@ -1701,7 +1702,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +static u32 compute_next_devid_offset (struct list_head *h, +struct its_device *dev) {struct its_device *next;u32 next_offset;@@ -1755,8 +1757,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,* Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables) */ -int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, -int start_id,entry_fn_t fn,void *opaque) +static int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) {void *entry =kzalloc (esz,GFP_KERNEL );struct kvm *kvm = its->dev->kvm;@@ -1791,13 +1793,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) +{ +return -ENXIO ; +} + +static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_dte - Save a device table entry at a given GPA +* +* @its: ITS handle +* @dev: ITS device +* @ptr: GPA +*/ +static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, +gpa_t ptr,int dte_esz) +{ +struct kvm *kvm = its->dev->kvm; +u64 val, itt_addr_field; +u32 next_offset; + + itt_addr_field = dev->itt_addr >>8 ; + next_offset =compute_next_devid_offset (&its->device_list, dev); + val = (1ULL <<KVM_ITS_DTE_VALID_SHIFT | + ((u64 )next_offset <<KVM_ITS_DTE_NEXT_SHIFT ) | + (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | + (dev->num_eventid_bits -1 )); + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, ptr, &val, dte_esz); +} + +/** +* vgic_its_restore_dte - restore a device table entry +* +* @its: its handle +* @id: device id the DTE corresponds to +* @ptr: kernel VA where the 8 byte DTE is located +* @opaque: unused +* +* Return: < 0 on error, 0 if the dte is the last one, id offset to the +* next dte otherwise +*/ +static int vgic_its_restore_dte (struct vgic_its *its,u32 id, +void *ptr,void *opaque) +{ +struct its_device *dev; +gpa_t itt_addr; +u8 num_eventid_bits; +u64 entry = *(u64 *)ptr; +bool valid; +u32 offset; +int ret; + + entry =le64_to_cpu (entry); + + valid = entry >>KVM_ITS_DTE_VALID_SHIFT ; + num_eventid_bits = (entry &KVM_ITS_DTE_SIZE_MASK ) +1 ; + itt_addr = ((entry &KVM_ITS_DTE_ITTADDR_MASK ) + >>KVM_ITS_DTE_ITTADDR_SHIFT ) <<8 ; + +if (!valid) +return 1 ; + +/* dte entry is valid */ + offset = (entry &KVM_ITS_DTE_NEXT_MASK ) >>KVM_ITS_DTE_NEXT_SHIFT ; + + dev =vgic_its_alloc_device (its, id, itt_addr, num_eventid_bits); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ret =vgic_its_restore_itt (its, dev); +if (ret) +return ret; + +return offset; +} + +static int vgic_its_device_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_device *deva = container_of(a, struct its_device, dev_list); + struct its_device *devb = container_of(b, struct its_device, dev_list); + +if (deva->device_id < devb->device_id) +return -1 ; +else +return 1 ; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM +* +* L1/L2 handling is hidden by vgic_its_check_id() helper which directly +* returns the GPA of the device entry */ static int vgic_its_save_device_tables (struct vgic_its *its)
virt/kvm/arm/vgic/vgic-its.c int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1904
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-1{ -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_device *dev; +int dte_esz = abi->dte_esz; +u64 baser; + + baser = its->baser_device_table; + +list_sort (NULL , &its->device_list, vgic_its_device_cmp);
this list is protected by the ITS mutex but you seem to be only holding the KVM mutex here, so don't we have a potential exploit here?
Otherwise this patch looks good to me.
Thanks,
-Christoffer
Quoted 116 lines
virt/kvm/arm/vgic/vgic-its.c +81
-1+ +list_for_each_entry (dev, &its->device_list, dev_list) { +int ret; +gpa_t eaddr; + +if (!vgic_its_check_id (its, baser, + dev->device_id, &eaddr)) +return -EINVAL ; + + ret =vgic_its_save_itt (its, dev); +if (ret) +return ret; + + ret =vgic_its_save_dte (its, dev, eaddr, dte_esz); +if (ret) +return ret; + } +return 0 ; +} + +/** +* handle_l1_dte - callback used for L1 device table entries (2 stage case) +* +* @its: its handle +* @id: index of the entry in the L1 table +* @addr: kernel VA +* @opaque: unused +* +* L1 table entries are scanned by steps of 1 entry +* Return < 0 if error, 0 if last dte was found when scanning the L2 +* table, +1 otherwise (meaning next L1 entry must be scanned) +*/ +static int handle_l1_dte (struct vgic_its *its,u32 id,void *addr, +void *opaque) +{ +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int l2_start_id = id * (SZ_64K / abi->dte_esz); +u64 entry = *(u64 *)addr; +int dte_esz = abi->dte_esz; +gpa_t gpa; +int ret; + + entry =le64_to_cpu (entry); + +if (!(entry &KVM_ITS_L1E_VALID_MASK )) +return 1 ; + + gpa = entry &KVM_ITS_L1E_ADDR_MASK ; + + ret =scan_its_table (its, gpa,SZ_64K , dte_esz, + l2_start_id, vgic_its_restore_dte,NULL ); + +if (ret <=0 ) +return ret; + +return 1 ; }/** @@ -1806,7 +1966,31 @@ static int vgic_its_save_device_tables(struct vgic_its *its)*/ static int vgic_its_restore_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +u64 baser = its->baser_device_table; +int l1_esz, ret; +int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +gpa_t l1_gpa; + +if (!(baser &GITS_BASER_VALID )) +return 0 ; + + l1_gpa =BASER_ADDRESS (baser); + +if (baser &GITS_BASER_INDIRECT ) { + l1_esz =GITS_LVL1_ENTRY_SIZE ; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + handle_l1_dte,NULL ); + }else { + l1_esz = abi->dte_esz; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + vgic_its_restore_dte,NULL ); + } + +if (ret >0 ) + ret = -EINVAL ; + +return ret; } static int vgic_its_save_cte(struct vgic_its *its,virt/kvm/arm/vgic/vgic.h +10
-0@@ -81,6 +81,16 @@#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_CTE_RDBASE_SHIFT 16#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +#define KVM_ITS_DTE_VALID_SHIFT 63 +#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_DTE_NEXT_SHIFT 49 +#define KVM_ITS_DTE_NEXT_MASK GENMASK_ULL(62, 49) +#define KVM_ITS_DTE_ITTADDR_SHIFT 5 +#define KVM_ITS_DTE_ITTADDR_MASK GENMASK_ULL(48, 5) +#define KVM_ITS_DTE_SIZE_MASK GENMASK_ULL(4, 0) +#define KVM_ITS_L1E_VALID_MASK BIT_ULL(63) +/* we only support 64 kB translation table page size */ +#define KVM_ITS_L1E_ADDR_MASK GENMASK_ULL(51, 16)static inline bool irq_is_pending (struct vgic_irq *irq) {Signature · 1 line
2.5.5
Re: [PATCH v6 15/24] KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
In reply to Christoffer DallQuoted 93 lines · Christoffer Dall
Quoted 48 lines · Eric Auger
When creating the lpi we now ask the redistributor what is the state of the LPI (priority, enabled, pending).
Signed-off-by Eric Auger <[email protected]>---
v6: creation
--- virt/kvm/arm/vgic/vgic-its.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)virt/kvm/arm/vgic/vgic-its.c +6
-1@@ -38,6 +38,8 @@static int vgic_its_set_abi (struct vgic_its *its,int rev);static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq, +struct kvm_vcpu *filter_vcpu);/* * Creates a new (reference to a) struct vgic_irq for a given LPI. @@ -46,10 +48,12 @@ static const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its);* If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure. */ -static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid) +static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid, +struct kvm_vcpu *vcpu) {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm,NULL , intid), *oldirq; +int ret;/* In this case there is no put, since we keep the reference. */ if (irq)@@ -66,6 +70,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) irq->config =VGIC_CONFIG_EDGE ;kref_init (&irq->refcount); irq->intid = intid; + irq->target_vcpu = vcpu;spin_lock (&dist->lpi_list_lock);virt/kvm/arm/vgic/vgic-its.c static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) · line 106
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +2
-0@@ -97,6 +102,19 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) out_unlock:spin_unlock (&dist->lpi_list_lock); +/* +* We "cache" the configuration table entries in out struct vgic_irq's. s/out/our/ ?
Quoted 23 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+* However we only have those structs for mapped IRQs, so we read in +* the respective config data from memory here upon mapping the LPI. +*/ + ret =update_lpi_config (kvm, irq,NULL ); +if (ret) +return ERR_PTR (ret); + + ret =vgic_v3_lpi_sync_pending_status (kvm, irq); +if (ret) +return ERR_PTR (ret); +return irq; }@@ -769,6 +787,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct its_ite *ite; +struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · line 839
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +4
-1@@ -814,7 +833,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, ite->collection = collection; ite->lpi = lpi_nr; - irq =vgic_add_lpi (kvm, lpi_nr); +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_nr, vcpu);So, if we don't have the collection and therefore end up with
irq->vcpu== NULL, how do we ever read the pending bit from memory as the affinity may later change?
Quoted 4 lines
Is this a problem with our idea of only looking at the pending bit on
vgic_add_lpi, or does it just mean that we should sample the pending bit whenever we move LPIs around to VCPUs and if the bit is set, then also set thepennding_latch(if not already set), or what should happen here?
It means that we would need to sample that bit on MOVI and maybe MOVALL as well, but this feels a bit odd. How did that bit land there the first place?
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Marc ZyngierHi Christoffer, Marc,
Quoted 175 lines · Marc Zyngier
Quoted 126 lines · Christoffer Dall
Quoted 125 lines · Marc Zyngier
Quoted 8 lines · Auger Eric
Hi,
Quoted 5 lines · Christoffer Dall
Quoted 4 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Yeah, I'm fine with that. Having all of the HW description in one single place makes sense.
Quoted 103 lines
Quoted 92 lines
Quoted 92 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +26
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); +Quoted 8 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Yes but on the other hand, there is a risk the
target_vcpuhas changed, isn't it? So the alternative you be to return-EBUSY. and caller, ie.its_add_lpiwould return that error?Quoted 7 lines
Quoted 4 lines
For the guest to be changing the LPI configuration, it would take a command to be issued. If there is a concern that we're racing against a command, can we take the command queue mutex?
So this is a redistributor thing, not an ITS thing, so I'd prefer not solving it that way.
Indeed.
Quoted 5 lines
I was just thinking if we should do a limit of the number of times we'll do this or check if we have a pending signal and just return, but actually, I don't think this is a problem, because the path that keeps changing the configuration will eventually run out of CPU resources and this thread would have forward progress.
Yes, assuming we're not holding any other spinlock on the same path. Looking at the code, we only seem to come here from handling MAPI/MAPTI, so we should be good.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
Quoted 7 lines
Quoted 3 lines
1) the guest should never write to the pending table.
2) if there is an interrupt being injected, it will be made pending in the irq structure, and not in the PT.If you have tGwo separate cores running this function at the same time, trying to clear each a bit in the same word, wouldn't you loose one of the cleared bits?
Quoted 8 lines
Ah, I see what you mean now (I was obviously looking at the wrong end of the problem).
Quoted 2 lines
Maybe that can never happen because the commands would be serialized and we should be restoring the system in serial as well?
I think that should be the case. What does it mean to do two restore in parallel? We should probably enforce this. Eric?
We can't have 2 restores in parallel as we hold the kvm->lock all along. MAPI commands are serialized by its_cmd_lock. MAPI and restore cannot happen concurrently since vcpu is stopped and kvm_lock is held during GITS_CWRITER setting too. So my understanding is it can't have this race.
Thanks
Eric
Quoted 3 lines
M.
Re: [PATCH v6 19/24] KVM: arm64: vgic-its: Collection table save/restore
In reply to Christoffer DallHi
Quoted 112 lines · Christoffer Dall
Quoted 111 lines · Eric Auger
The save path copies the collection entries into guest RAM at the GPA specified in the BASER register. This obviously requires the BASER to be set. The last written element is a dummy collection table entry.
We do not index by collection ID as the collection entry can fit into 8 bytes while containing the collection ID.
On restore path we re-allocate the collection objects.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove the valid pointer fromvgic_its_restore_cteand return +1 if no error and last elt not found
- check the BASER valid bit
- addBUG_ONto check esz versus val sizev4 -> v5:
- add macros for field encoding/decoding
- useabi->cte_esz
- rename flush into save
- check thetarget_addris validv3 -> v4:
- replaced u64 *ptr bygpa_tgpa
- check the collection does not exist before allocating itv1 -> v2:
- reword commit message and directly use 8 as entry size
- no kvm parameter anymore
- add helper for flush/restore cte
- table size computed here
- add le64/cpu conversions--- virt/kvm/arm/vgic/vgic-its.c | 102 ++++++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 9 ++++ 2 files changed, 109 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +56
-1@@ -1804,13 +1804,91 @@ static int vgic_its_restore_device_tables(struct vgic_its *its)return -ENXIO ; } +static int vgic_its_save_cte (struct vgic_its *its, +struct its_collection *collection, +gpa_t gpa,int esz) +{ +u64 val; + + val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | + ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | + collection->collection_id); + val =cpu_to_le64 (val); +return kvm_write_guest (its->dev->kvm, gpa, &val, esz); +} + +static int vgic_its_restore_cte (struct vgic_its *its,gpa_t gpa,int esz) +{ +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +u32 target_addr, coll_id; +u64 val; +int ret; + +BUG_ON (esz >sizeof (val)); + ret =kvm_read_guest (kvm, gpa, &val, esz); +if (ret) +return ret; + val =le64_to_cpu (val); +if (!(val &KVM_ITS_CTE_VALID_MASK )) +return 0 ; + + target_addr = (u32)(val >>KVM_ITS_CTE_RDBASE_SHIFT ); + coll_id = val &KVM_ITS_CTE_ICID_MASK ; + +if (target_addr >=atomic_read (&kvm->online_vcpus)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (collection) +return -EEXIST ; + ret =vgic_its_alloc_collection (its, &collection, coll_id); +if (ret) +return ret; + collection->target_addr = target_addr; +return 1 ; +} +/** * vgic_its_save_collection_table - Save the collection table into * guest RAM */ static int vgic_its_save_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_collection *collection; +u64 val; +gpa_t gpa; +size_t max_size, filled =0 ; +int ret, cte_esz = abi->cte_esz; + + gpa =BASER_ADDRESS (its->baser_coll_table); +if (!gpa) +return 0 ; +
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_restore_device_tables(struct vgic_its *its) · line 1873
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+ max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +list_for_each_entry (collection, &its->collection_list, coll_list) { +if (filled == max_size) +return -ENOSPC ;should this ever happen? Shouldn't we check limit in
vgic_its_cmd_handle_mapi()?
Yes you're right. This should be done in vgic_its_cmd_handle_mapc()
Quoted 3 lines
We can fix that later, and it shouldn't block this patch, just asking the question here because I'm noticing it.
OK, I will fix that later then.
Thanks
Eric
Quoted 82 lines
Quoted 74 lines
virt/kvm/arm/vgic/vgic-its.c +39
-1+ ret =vgic_its_save_cte (its, collection, gpa, cte_esz); +if (ret) +return ret; + gpa += cte_esz; + filled += cte_esz; + } + +if (filled == max_size) +return 0 ; + +/* +* table is not fully filled, add a last dummy element +* with valid bit unset +*/ + val =0 ; +BUG_ON (cte_esz >sizeof (val)); + ret =kvm_write_guest (its->dev->kvm, gpa, &val, cte_esz); +return ret; }/** @@ -1820,7 +1898,27 @@ static int vgic_its_save_collection_table(struct vgic_its *its)*/ static int vgic_its_restore_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int cte_esz = abi->cte_esz; +size_t max_size, read =0 ; +gpa_t gpa; +int ret; + +if (!(its->baser_coll_table &GITS_BASER_VALID )) +return 0 ; + + gpa =BASER_ADDRESS (its->baser_coll_table); + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +while (read < max_size) { + ret =vgic_its_restore_cte (its, gpa, cte_esz); +if (ret <=0 ) +break ; + gpa += cte_esz; + read += cte_esz; + } +return ret; }/** virt/kvm/arm/vgic/vgic.h +9
-0@@ -73,6 +73,15 @@ KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \ KVM_REG_ARM_VGIC_SYSREG_OP2_MASK) +/* +* As per Documentation/virtual/kvm/devices/arm-vgic-its.txt, +* below macros are defined for ITS table entry encoding. +*/ +#define KVM_ITS_CTE_VALID_SHIFT 63 +#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_CTE_RDBASE_SHIFT 16 +#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +static inline bool irq_is_pending (struct vgic_irq *irq) {if (irq->config ==VGIC_CONFIG_EDGE )Signature · 1 line
2.5.5Reviewed-by Christoffer Dall <[email protected]>_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 19/24] KVM: arm64: vgic-its: Collection table save/restore
Hi,
Quoted 123 lines · Auger Eric
Hi
Quoted 112 lines · Christoffer Dall
Quoted 111 lines · Eric Auger
The save path copies the collection entries into guest RAM at the GPA specified in the BASER register. This obviously requires the BASER to be set. The last written element is a dummy collection table entry.
We do not index by collection ID as the collection entry can fit into 8 bytes while containing the collection ID.
On restore path we re-allocate the collection objects.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- remove the valid pointer fromvgic_its_restore_cteand return +1 if no error and last elt not found
- check the BASER valid bit
- addBUG_ONto check esz versus val sizev4 -> v5:
- add macros for field encoding/decoding
- useabi->cte_esz
- rename flush into save
- check thetarget_addris validv3 -> v4:
- replaced u64 *ptr bygpa_tgpa
- check the collection does not exist before allocating itv1 -> v2:
- reword commit message and directly use 8 as entry size
- no kvm parameter anymore
- add helper for flush/restore cte
- table size computed here
- add le64/cpu conversions--- virt/kvm/arm/vgic/vgic-its.c | 102 ++++++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 9 ++++ 2 files changed, 109 insertions(+), 2 deletions(-)virt/kvm/arm/vgic/vgic-its.c +56
-1@@ -1804,13 +1804,91 @@ static int vgic_its_restore_device_tables(struct vgic_its *its)return -ENXIO ; } +static int vgic_its_save_cte (struct vgic_its *its, +struct its_collection *collection, +gpa_t gpa,int esz) +{ +u64 val; + + val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | + ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | + collection->collection_id); + val =cpu_to_le64 (val); +return kvm_write_guest (its->dev->kvm, gpa, &val, esz); +} + +static int vgic_its_restore_cte (struct vgic_its *its,gpa_t gpa,int esz) +{ +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +u32 target_addr, coll_id; +u64 val; +int ret; + +BUG_ON (esz >sizeof (val)); + ret =kvm_read_guest (kvm, gpa, &val, esz); +if (ret) +return ret; + val =le64_to_cpu (val); +if (!(val &KVM_ITS_CTE_VALID_MASK )) +return 0 ; + + target_addr = (u32)(val >>KVM_ITS_CTE_RDBASE_SHIFT ); + coll_id = val &KVM_ITS_CTE_ICID_MASK ; + +if (target_addr >=atomic_read (&kvm->online_vcpus)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (collection) +return -EEXIST ; + ret =vgic_its_alloc_collection (its, &collection, coll_id); +if (ret) +return ret; + collection->target_addr = target_addr; +return 1 ; +} +/** * vgic_its_save_collection_table - Save the collection table into * guest RAM */ static int vgic_its_save_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_collection *collection; +u64 val; +gpa_t gpa; +size_t max_size, filled =0 ; +int ret, cte_esz = abi->cte_esz; + + gpa =BASER_ADDRESS (its->baser_coll_table); +if (!gpa) +return 0 ; +Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_restore_device_tables(struct vgic_its *its) · line 1873
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+ max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +list_for_each_entry (collection, &its->collection_list, coll_list) { +if (filled == max_size) +return -ENOSPC ;should this ever happen? Shouldn't we check limit in
vgic_its_cmd_handle_mapi()?
Quoted 6 lines
Yes you're right. This should be done in
vgic_its_cmd_handle_mapc()Quoted 3 lines
We can fix that later, and it shouldn't block this patch, just asking the question here because I'm noticing it.
OK, I will fix that later then.
actually the check already is done in vgic_its_alloc_collection/vgic_its_check_id called from both mapi and mapti.
so I can directly remove the above check.
Thanks
Eric
Quoted 86 lines
Thanks
Eric
Quoted 82 lines
Quoted 74 lines
virt/kvm/arm/vgic/vgic-its.c +39
-1+ ret =vgic_its_save_cte (its, collection, gpa, cte_esz); +if (ret) +return ret; + gpa += cte_esz; + filled += cte_esz; + } + +if (filled == max_size) +return 0 ; + +/* +* table is not fully filled, add a last dummy element +* with valid bit unset +*/ + val =0 ; +BUG_ON (cte_esz >sizeof (val)); + ret =kvm_write_guest (its->dev->kvm, gpa, &val, cte_esz); +return ret; }/** @@ -1820,7 +1898,27 @@ static int vgic_its_save_collection_table(struct vgic_its *its)*/ static int vgic_its_restore_collection_table (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int cte_esz = abi->cte_esz; +size_t max_size, read =0 ; +gpa_t gpa; +int ret; + +if (!(its->baser_coll_table &GITS_BASER_VALID )) +return 0 ; + + gpa =BASER_ADDRESS (its->baser_coll_table); + + max_size =GITS_BASER_NR_PAGES (its->baser_coll_table) *SZ_64K ; + +while (read < max_size) { + ret =vgic_its_restore_cte (its, gpa, cte_esz); +if (ret <=0 ) +break ; + gpa += cte_esz; + read += cte_esz; + } +return ret; }/** virt/kvm/arm/vgic/vgic.h +9
-0@@ -73,6 +73,15 @@ KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \ KVM_REG_ARM_VGIC_SYSREG_OP2_MASK) +/* +* As per Documentation/virtual/kvm/devices/arm-vgic-its.txt, +* below macros are defined for ITS table entry encoding. +*/ +#define KVM_ITS_CTE_VALID_SHIFT 63 +#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_CTE_RDBASE_SHIFT 16 +#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +static inline bool irq_is_pending (struct vgic_irq *irq) {if (irq->config ==VGIC_CONFIG_EDGE )Signature · 1 line
2.5.5Reviewed-by Christoffer Dall <[email protected]>_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 14/24] KVM: arm64: vgic-v3: vgic_v3_lpi_sync_pending_status
In reply to Auger EricQuoted 184 lines · Auger Eric
Hi Christoffer, Marc,
Quoted 181 lines · Marc Zyngier
Quoted 126 lines · Christoffer Dall
Quoted 125 lines · Marc Zyngier
Quoted 8 lines · Auger Eric
Hi,
Quoted 5 lines · Christoffer Dall
Quoted 4 lines · Eric Auger
this new helper synchronizes the irq
pending_latchwith the LPI pending bit status found in rdist pending table. As the status is consumed, we reset the bit in pending table.Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
As we need the
PENDBASER_ADDRESS()in vgic-v3, let's move its definition in the irqchip header. We restore the full length of the field, ie [51:16]. Same forPROPBASER_ADDRESSwith full field length of [51:12].why into irqchip and not just the vgic header file?
Well most register field shift/masks are located there. This may be useful as well for the ITS driver if power management gets implemented
Yeah, I'm fine with that. Having all of the HW description in one single place makes sense.
Quoted 103 lines
Quoted 92 lines
Quoted 92 lines
Signed-off-by Eric Auger <[email protected]>---
v6: new
--- include/linux/irqchip/arm-gic-v3.h | 2 ++ virt/kvm/arm/vgic/vgic-its.c | 6 ++---- virt/kvm/arm/vgic/vgic-v3.c | 44 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-)include/linux/irqchip/arm-gic-v3.h +2
-0@@ -159,6 +159,8 @@#define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)#define GICR_PROPBASER_IDBITS_MASK (0x1f) +#define GICR_PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 12)) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))#define GICR_PENDBASER_SHAREABILITY_SHIFT (10)#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7)virt/kvm/arm/vgic/vgic-its.c +2
-4@@ -198,8 +198,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,*/ #define BASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16))#define CBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12)) -#define PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 16)) -#define PROPBASER_ADDRESS (x) ((x) & GENMASK_ULL(47, 12))#define GIC_LPI_OFFSET 8192@@ -234,7 +232,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq,struct kvm_vcpu *filter_vcpu) { -u64 propbase =PROPBASER_ADDRESS (kvm->arch.vgic.propbaser); +u64 propbase =GICR_PROPBASER_ADDRESS (kvm->arch.vgic.propbaser);u8 prop;int ret;@@ -346,7 +344,7 @@ static u32 max_lpis_propbaser(u64 propbaser)*/ static int its_sync_lpi_pending_table (struct kvm_vcpu *vcpu) { -gpa_t pendbase =PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); +gpa_t pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);struct vgic_irq *irq;int last_byte_offset =-1 ;int ret =0 ;virt/kvm/arm/vgic/vgic-v3.c +26
-0@@ -252,6 +252,50 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_hcr =ICH_HCR_EN ; } +int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *vcpu; +int byte_offset, bit_nr; +gpa_t pendbase, ptr; +bool status; +u8 val; +int ret; + +retry: + vcpu = irq->target_vcpu; +if (!vcpu) +return 0 ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + + status = val & (1 << bit_nr); +Quoted 8 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 284
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+spin_lock (&irq->irq_lock); +if (irq->target_vcpu != vcpu) { +spin_unlock (&irq->irq_lock); + goto retry;Can the guest be continuously changing the configuration of the LPI and cause this function to be called, which will efficiently hog this CPU from the system, or am I being overly cautious here?
Yes but on the other hand, there is a risk the
target_vcpuhas changed, isn't it? So the alternative you be to return-EBUSY. and caller, ie.its_add_lpiwould return that error?Quoted 7 lines
Quoted 4 lines
For the guest to be changing the LPI configuration, it would take a command to be issued. If there is a concern that we're racing against a command, can we take the command queue mutex?
So this is a redistributor thing, not an ITS thing, so I'd prefer not solving it that way.
Indeed.
Quoted 5 lines
I was just thinking if we should do a limit of the number of times we'll do this or check if we have a pending signal and just return, but actually, I don't think this is a problem, because the path that keeps changing the configuration will eventually run out of CPU resources and this thread would have forward progress.
Yes, assuming we're not holding any other spinlock on the same path. Looking at the code, we only seem to come here from handling MAPI/MAPTI, so we should be good.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 294
Quoted 11 lines
virt/kvm/arm/vgic/vgic-v3.c +10
-0+ } + irq->pending_latch = status; +vgic_queue_irq_unlock (vcpu->kvm, irq); + +if (status) { +/* clear consumed data */ + val &= ~(1 << bit_nr); + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret;Do we have a problem that if this is done twice within the same byte (on different LPIs) then the data could be strangely out of sync?
Not sure I get what you mean here? I reset a single bit within the byte. Do you mean there could be a concurrency issue?
In principle we are not obliged to reset the bit, right? Why do we care? The table will be updated on next pending table save.
Quoted 7 lines
Quoted 3 lines
1) the guest should never write to the pending table.
2) if there is an interrupt being injected, it will be made pending in the irq structure, and not in the PT.If you have tGwo separate cores running this function at the same time, trying to clear each a bit in the same word, wouldn't you loose one of the cleared bits?
Ah, I see what you mean now (I was obviously looking at the wrong end of the problem).
Quoted 2 lines
Maybe that can never happen because the commands would be serialized and we should be restoring the system in serial as well?
Quoted 7 lines
Quoted 2 lines
I think that should be the case. What does it mean to do two restore in parallel? We should probably enforce this. Eric?
We can't have 2 restores in parallel as we hold the
kvm->lockall along. MAPI commands are serialized byits_cmd_lock. MAPI and restore cannot happen concurrently since vcpu is stopped andkvm_lockis held duringGITS_CWRITERsetting too. So my understanding is it can't have this race.
Agreed. Thanks for helping me figure this out.
-Christoffer
Re: [PATCH v6 15/24] KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
In reply to Marc ZyngierHi,
Quoted 94 lines · Marc Zyngier
Quoted 93 lines · Christoffer Dall
Quoted 48 lines · Eric Auger
When creating the lpi we now ask the redistributor what is the state of the LPI (priority, enabled, pending).
Signed-off-by Eric Auger <[email protected]>---
v6: creation
--- virt/kvm/arm/vgic/vgic-its.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)virt/kvm/arm/vgic/vgic-its.c +6
-1@@ -38,6 +38,8 @@static int vgic_its_set_abi (struct vgic_its *its,int rev);static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq, +struct kvm_vcpu *filter_vcpu);/* * Creates a new (reference to a) struct vgic_irq for a given LPI. @@ -46,10 +48,12 @@ static const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its);* If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure. */ -static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid) +static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid, +struct kvm_vcpu *vcpu) {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm,NULL , intid), *oldirq; +int ret;/* In this case there is no put, since we keep the reference. */ if (irq)@@ -66,6 +70,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) irq->config =VGIC_CONFIG_EDGE ;kref_init (&irq->refcount); irq->intid = intid; + irq->target_vcpu = vcpu;spin_lock (&dist->lpi_list_lock);virt/kvm/arm/vgic/vgic-its.c static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) · line 106
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +2
-0@@ -97,6 +102,19 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) out_unlock:spin_unlock (&dist->lpi_list_lock); +/* +* We "cache" the configuration table entries in out struct vgic_irq's. s/out/our/ ?
Quoted 23 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+* However we only have those structs for mapped IRQs, so we read in +* the respective config data from memory here upon mapping the LPI. +*/ + ret =update_lpi_config (kvm, irq,NULL ); +if (ret) +return ERR_PTR (ret); + + ret =vgic_v3_lpi_sync_pending_status (kvm, irq); +if (ret) +return ERR_PTR (ret); +return irq; }@@ -769,6 +787,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct its_ite *ite; +struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · line 839
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +4
-1@@ -814,7 +833,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, ite->collection = collection; ite->lpi = lpi_nr; - irq =vgic_add_lpi (kvm, lpi_nr); +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_nr, vcpu);So, if we don't have the collection and therefore end up with
irq->vcpu== NULL, how do we ever read the pending bit from memory as the affinity may later change?
Quoted 8 lines
Quoted 4 lines
Is this a problem with our idea of only looking at the pending bit on
vgic_add_lpi, or does it just mean that we should sample the pending bit whenever we move LPIs around to VCPUs and if the bit is set, then also set thepennding_latch(if not already set), or what should happen here?It means that we would need to sample that bit on MOVI and maybe MOVALL as well, but this feels a bit odd. How did that bit land there the first place?
Without talking about save/restore, before this series the pending table was sync'ed on RDIST LPI enable only and that's all. This is kept.
Now talking about save/restore, if we restore an LPI whose collection is not attached to any RDIST, we can't sync at that time. The problem exists if we check the pending bit on vgic_add_lpi or later, ie. at the end of the ITS table restore process (as I did before). I don't see in the spec we are supposed to read the pending table on MAPTI or MAPC.
Thanks
Eric
Quoted 5 lines
Thanks,
M.
Re: [PATCH v6 22/24] KVM: arm64: vgic-its: ITT save and restore
In reply to Eric AugerQuoted 194 lines · Eric Auger
Implement routines to save and restore device ITT and their interrupt table entries (ITE).
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- accomodatevgic_its_alloc_itechange of proto
- check LPI ID on restore, check eventid offset
- initializations on separate line
- coming after device save/restore
-add_lpidoes config and pending bit syncv4 -> v5:
- ITE are now sorted by eventid on the flush
- rename *flush* into *save*
- use macros for shits and masks
- passite_esztovgic_its_save_itev3 -> v4:
-lookup_tableandcompute_next_eventid_offsetbecome static in this patch
- remove static along with vgic_its_flush/restore_itt to avoid compilation warnings
- next field only computed with a shift (mask removed)
- handle the case where the last element has not been foundv2 -> v3:
- add return 0 invgic_its_restore_ite(was in subsequent patch)v2: creation
--- virt/kvm/arm/vgic/vgic-its.c | 116 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 4 ++ 2 files changed, 117 insertions(+), 3 deletions(-)virt/kvm/arm/vgic/vgic-its.c +113
-3@@ -1716,7 +1716,7 @@ static u32 compute_next_devid_offset(struct list_head *h,return min_t (u32, next_offset,VITS_DTE_MAX_DEVID_OFFSET ); } -u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) +static u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite) {struct its_ite *next;u32 next_offset;@@ -1793,14 +1793,124 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +/** +* vgic_its_save_ite - Save an interrupt translation entry at @gpa +*/ +static int vgic_its_save_ite (struct vgic_its *its,struct its_device *dev, +struct its_ite *ite,gpa_t gpa,int ite_esz) +{ +struct kvm *kvm = its->dev->kvm; +u32 next_offset; +u64 val; + + next_offset =compute_next_eventid_offset (&dev->itt_head, ite); + val = ((u64 )next_offset <<KVM_ITS_ITE_NEXT_SHIFT ) | + ((u64 )ite->lpi <<KVM_ITS_ITE_PINTID_SHIFT ) | + ite->collection->collection_id; + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, gpa, &val, ite_esz); +} + +/** +* vgic_its_restore_ite - restore an interrupt translation entry +* @event_id: id used for indexing +* @ptr: pointer to the ITE entry +* @opaque: pointer to the its_device +*/ +static int vgic_its_restore_ite (struct vgic_its *its,u32 event_id, +void *ptr,void *opaque) +{ +struct its_device *dev = (struct its_device *)opaque; +struct its_collection *collection; +struct kvm *kvm = its->dev->kvm; +struct kvm_vcpu *vcpu =NULL ; +u64 val; +u64 *p = (u64 *)ptr; +struct vgic_irq *irq; +u32 coll_id, lpi_id; +struct its_ite *ite; +u32 offset; + + val = *p; + + val =le64_to_cpu (val); + + coll_id = val &KVM_ITS_ITE_ICID_MASK ; + lpi_id = (val &KVM_ITS_ITE_PINTID_MASK ) >>KVM_ITS_ITE_PINTID_SHIFT ; + +if (!lpi_id) +return 1 ;/* invalid entry, no choice but to scan next entry */ + +if (lpi_id <VGIC_MIN_LPI ) +return -EINVAL ; + + offset = val >>KVM_ITS_ITE_NEXT_SHIFT ; +if (event_id + offset >=BIT_ULL (dev->num_eventid_bits)) +return -EINVAL ; + + collection =find_collection (its, coll_id); +if (!collection) +return -EINVAL ; + + ite =vgic_its_alloc_ite (dev, collection, lpi_id, event_id); +if (IS_ERR (ite)) +return PTR_ERR (ite); + +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_id, vcpu); +if (IS_ERR (irq)) +return PTR_ERR (irq); + ite->irq = irq; + +return offset; +} + +static int vgic_its_ite_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_ite *itea = container_of(a, struct its_ite, ite_list); + struct its_ite *iteb = container_of(b, struct its_ite, ite_list); + +if (itea->event_id < iteb->event_id) +return -1 ; +else +return 1 ; +} +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +gpa_t base = device->itt_addr; +struct its_ite *ite; +int ret; +int ite_esz = abi->ite_esz; + +list_sort (NULL , &device->itt_head, vgic_its_ite_cmp); + +list_for_each_entry (ite, &device->itt_head, ite_list) { +gpa_t gpa = base + ite->event_id * ite_esz; + + ret =vgic_its_save_ite (its, device, ite, gpa, ite_esz); +if (ret) +return ret; + } +return 0 ; }static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +gpa_t base = dev->itt_addr; +int ret; +int ite_esz = abi->ite_esz; +size_t max_size =BIT_ULL (dev->num_eventid_bits) * ite_esz; + + ret =scan_its_table (its, base, max_size, ite_esz,0 , + vgic_its_restore_ite, dev); + +return ret; }/** virt/kvm/arm/vgic/vgic.h +4
-0@@ -81,6 +81,10 @@#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_CTE_RDBASE_SHIFT 16#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +#define KVM_ITS_ITE_NEXT_SHIFT 48 +#define KVM_ITS_ITE_PINTID_SHIFT 16 +#define KVM_ITS_ITE_PINTID_MASK GENMASK_ULL(47, 16) +#define KVM_ITS_ITE_ICID_MASK GENMASK_ULL(15, 0)#define KVM_ITS_DTE_VALID_SHIFT 63#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_DTE_NEXT_SHIFT 49Signature · 1 line
2.5.5
Re: [PATCH v6 23/24] KVM: arm64: vgic-its: Fix pending table sync
In reply to Eric AugerQuoted 90 lines · Eric Auger
In
its_sync_lpi_pending_table()we currently ignore thetarget_vcpuof the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.Also in
vgic_its_cmd_handle_invall()we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.This patch passes a vpcu to
vgic_copy_lpi_list()so that this latter returns a snapshot of the LPIs targeting this CPU and only those.Signed-off-by Eric Auger <[email protected]>--- virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)virt/kvm/arm/vgic/vgic-its.c +12
-12@@ -278,13 +278,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, }/* -* Create a snapshot of the current LPI list, so that we can enumerate all -* LPIs without holding any lock. -* Returns the array length and puts the kmalloc'ed array into intid_ptr. +* Create a snapshot of the current LPIs targeting @vcpu, so that we can +* enumerate those LPIs without holding any lock. +* Returns their number and puts the kmalloc'ed array into intid_ptr. */ -static int vgic_copy_lpi_list (struct kvm *kvm,u32 **intid_ptr) +static int vgic_copy_lpi_list (struct kvm_vcpu *vcpu,u32 **intid_ptr) { -struct vgic_dist *dist = &kvm->arch.vgic; +struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct vgic_irq *irq;u32 *intids;int irq_count = dist->lpi_list_count, i =0 ;@@ -303,14 +303,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)spin_lock (&dist->lpi_list_lock);list_for_each_entry (irq, &dist->lpi_list_head, lpi_list) {/* We don't need to "get" the IRQ, as we hold the list lock. */ - intids[i] = irq->intid; -if (++i == irq_count) -break ; +if (irq->target_vcpu != vcpu) +continue ; + intids[i++] = irq->intid; }spin_unlock (&dist->lpi_list_lock); *intid_ptr = intids; -return irq_count; +return i; }/* @@ -359,7 +359,7 @@ static u32 max_lpis_propbaser(u64 propbaser) }/* -* Scan the whole LPI pending table and sync the pending bit in there +* Sync the pending table pending bit of LPIs targeting @vcpu * with our own data structures. This relies on the LPI being * mapped before. */ @@ -372,7 +372,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)u32 *intids;int nr_irqs, i; - nr_irqs =vgic_copy_lpi_list (vcpu->kvm, &intids); + nr_irqs =vgic_copy_lpi_list (vcpu, &intids);if (nr_irqs <0 )return nr_irqs;@@ -1055,7 +1055,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, vcpu =kvm_get_vcpu (kvm, collection->target_addr); - irq_count =vgic_copy_lpi_list (kvm, &intids); + irq_count =vgic_copy_lpi_list (vcpu, &intids);if (irq_count <0 )return irq_count;Signature · 1 line
2.5.5
Re: [PATCH v6 24/24] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
In reply to Eric AugerQuoted 187 lines · Eric Auger
This patch adds a new attribute to GICV3 KVM device
KVM_DEV_ARM_VGIC_GRP_CTRLgroup. This allows userspace to flush all GICR pending tables into guest RAM.Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- fix stored
- GICR_PEND/PROPBASER_ADDRESS were already introducedv4 -> v5:
- move pending table save code/ctrl into VGICv3 instead of ITS
- remove uselessgpa_tcast
- use the same optimization as inits_sync_lpi_pending_tablev3 -> v4:
- remove the wrong comment about locking
- pass kvm struct instead of its handle
- add comment about restore method
- remove GITR_PENDABASER.PTZ check
- continue iftarget_vcpu== NULL
- new locking strategyv1 -> v2:
- do not care about the 1st KB which should be zeroed according to the spec.--- arch/arm/include/uapi/asm/kvm.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 1 + include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-kvm-device.c | 20 +++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 51 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 6 files changed, 75 insertions(+)arch/arm/include/uapi/asm/kvm.h +1
-0@@ -202,6 +202,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -222,6 +222,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 +#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0include/linux/irqchip/arm-gic-v3.h +1
-0@@ -186,6 +186,7 @@#define GICR_PENDBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWaWb)#define GICR_PENDBASER_PTZ BIT_ULL(62) +#define GICR_PENDBASER_ADDRESS (x) ((x) & GENMASK_ULL(51, 16))/* * Re-Distributor registers, offsets from SGI_base virt/kvm/arm/vgic/vgic-kvm-device.c +20
-0@@ -580,6 +580,24 @@ static int vgic_v3_set_attr(struct kvm_device *dev, reg = tmp32;return vgic_v3_attr_regs_access (dev, attr, ®, true); } +case KVM_DEV_ARM_VGIC_GRP_CTRL : { +int ret; + +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +mutex_lock (&dev->kvm->lock); + +if (!lock_all_vcpus (dev->kvm)) { +mutex_unlock (&dev->kvm->lock); +return -EBUSY ; + } + ret =vgic_v3_save_pending_tables (dev->kvm); +unlock_all_vcpus (dev->kvm); +mutex_unlock (&dev->kvm->lock); +return ret; + } +break ; + } }return -ENXIO ; }@@ -658,6 +676,8 @@ static int vgic_v3_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_ARM_VGIC_CTRL_INIT :return 0 ; +case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES : +return 0 ; } }return -ENXIO ;virt/kvm/arm/vgic/vgic-v3.c +51
-0@@ -296,6 +296,57 @@ int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq)return 0 ; } +/** +* vgic_its_save_pending_tables - Save the pending tables into guest RAM +* kvm lock and all vcpu lock must be held +*/ +int vgic_v3_save_pending_tables (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +int last_byte_offset =-1 ; +struct vgic_irq *irq; +int ret; + +list_for_each_entry (irq, &dist->lpi_list_head, lpi_list) { +int byte_offset, bit_nr; +struct kvm_vcpu *vcpu; +gpa_t pendbase, ptr; +bool stored; +u8 val; + + vcpu = irq->target_vcpu; +if (!vcpu) +continue ; + + pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser); + + byte_offset = irq->intid /BITS_PER_BYTE ; + bit_nr = irq->intid %BITS_PER_BYTE ; + ptr = pendbase + byte_offset; + +if (byte_offset != last_byte_offset) { + ret =kvm_read_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + last_byte_offset = byte_offset; + } + + stored = val & (1U << bit_nr); +if (stored == irq->pending_latch) +continue ; + +if (irq->pending_latch) + val |=1 << bit_nr; +else + val &= ~(1 << bit_nr); + + ret =kvm_write_guest (kvm, ptr, &val,1 ); +if (ret) +return ret; + } +return 0 ; +} +/* check for overlapping regions and for regions crossing the end of memory */ static bool vgic_v3_check_base (struct kvm *kvm) {virt/kvm/arm/vgic/vgic.h +1
-0@@ -181,6 +181,7 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm);int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq); +int vgic_v3_save_pending_tables (struct kvm *kvm);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);int vgic_register_its_iodevs (struct kvm *kvm);Signature · 1 line
2.5.5
Re: [PATCH v6 21/24] KVM: arm64: vgic-its: Device table save/restore
In reply to Christoffer DallHi Christoffer,
Quoted 200 lines · Christoffer Dall
Quoted 199 lines · Eric Auger
This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.
For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch.
On restore, devices are re-allocated and their itt are re-built.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- accomodatevgic_its_alloc_devicechange of proto
- define bit fields for L1 entries
- s/handle_l1_entry/handle_l1_dte
- s/ite_esz/dte_esz inhandle_l1_dte
- check BASER valid bit
- s/nb_eventid_bits/num_eventid_bits
- new convention for returned values
- itt functions implemented in subsequent patchv4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- useabi->dte_esz
- clatify entry sizes for L1 and L2 tablesv3 -> v4:
- use the new proto forits_alloc_device
-compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fixhandle_l1_entryentry handling
- correctvgic_its_table_restoreerror handlingv2 -> v3:
- fixitt_addrbitmask invgic_its_restore_dte
- addition of return 0 invgic_its_restore_itemoved to the ITE related patchv1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing--- virt/kvm/arm/vgic/vgic-its.c | 194 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 10 +++ 2 files changed, 199 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +103
-4@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/list.h> #include <linux/uaccess.h> +#include <linux/list_sort.h> #include <linux/irqchip/arm-gic-v3.h> @@ -1701,7 +1702,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +static u32 compute_next_devid_offset (struct list_head *h, +struct its_device *dev) {struct its_device *next;u32 next_offset;@@ -1755,8 +1757,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,* Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables) */ -int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, -int start_id,entry_fn_t fn,void *opaque) +static int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) {void *entry =kzalloc (esz,GFP_KERNEL );struct kvm *kvm = its->dev->kvm;@@ -1791,13 +1793,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) +{ +return -ENXIO ; +} + +static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_dte - Save a device table entry at a given GPA +* +* @its: ITS handle +* @dev: ITS device +* @ptr: GPA +*/ +static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, +gpa_t ptr,int dte_esz) +{ +struct kvm *kvm = its->dev->kvm; +u64 val, itt_addr_field; +u32 next_offset; + + itt_addr_field = dev->itt_addr >>8 ; + next_offset =compute_next_devid_offset (&its->device_list, dev); + val = (1ULL <<KVM_ITS_DTE_VALID_SHIFT | + ((u64 )next_offset <<KVM_ITS_DTE_NEXT_SHIFT ) | + (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | + (dev->num_eventid_bits -1 )); + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, ptr, &val, dte_esz); +} + +/** +* vgic_its_restore_dte - restore a device table entry +* +* @its: its handle +* @id: device id the DTE corresponds to +* @ptr: kernel VA where the 8 byte DTE is located +* @opaque: unused +* +* Return: < 0 on error, 0 if the dte is the last one, id offset to the +* next dte otherwise +*/ +static int vgic_its_restore_dte (struct vgic_its *its,u32 id, +void *ptr,void *opaque) +{ +struct its_device *dev; +gpa_t itt_addr; +u8 num_eventid_bits; +u64 entry = *(u64 *)ptr; +bool valid; +u32 offset; +int ret; + + entry =le64_to_cpu (entry); + + valid = entry >>KVM_ITS_DTE_VALID_SHIFT ; + num_eventid_bits = (entry &KVM_ITS_DTE_SIZE_MASK ) +1 ; + itt_addr = ((entry &KVM_ITS_DTE_ITTADDR_MASK ) + >>KVM_ITS_DTE_ITTADDR_SHIFT ) <<8 ; + +if (!valid) +return 1 ; + +/* dte entry is valid */ + offset = (entry &KVM_ITS_DTE_NEXT_MASK ) >>KVM_ITS_DTE_NEXT_SHIFT ; + + dev =vgic_its_alloc_device (its, id, itt_addr, num_eventid_bits); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ret =vgic_its_restore_itt (its, dev); +if (ret) +return ret; + +return offset; +} + +static int vgic_its_device_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_device *deva = container_of(a, struct its_device, dev_list); + struct its_device *devb = container_of(b, struct its_device, dev_list); + +if (deva->device_id < devb->device_id) +return -1 ; +else +return 1 ; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM +* +* L1/L2 handling is hidden by vgic_its_check_id() helper which directly +* returns the GPA of the device entry */ static int vgic_its_save_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_device *dev; +int dte_esz = abi->dte_esz;
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1904
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+u64 baser; + + baser = its->baser_device_table; + +list_sort (NULL , &its->device_list, vgic_its_device_cmp);this list is protected by the ITS mutex but you seem to be only holding the KVM mutex here, so don't we have a potential exploit here?
Updates to the device, ite list are done when running commands. As we hold the KVM mutex, commands cannot run. Then there is vgic_its_destroy() which happens on kvm_put_kvm when all users have released their reference. So to me holding the kvm lock looks sufficient.
Thanks
Eric
Quoted 129 lines
Otherwise this patch looks good to me.
Thanks,
-ChristofferQuoted 116 lines
virt/kvm/arm/vgic/vgic-its.c +81
-1+ +list_for_each_entry (dev, &its->device_list, dev_list) { +int ret; +gpa_t eaddr; + +if (!vgic_its_check_id (its, baser, + dev->device_id, &eaddr)) +return -EINVAL ; + + ret =vgic_its_save_itt (its, dev); +if (ret) +return ret; + + ret =vgic_its_save_dte (its, dev, eaddr, dte_esz); +if (ret) +return ret; + } +return 0 ; +} + +/** +* handle_l1_dte - callback used for L1 device table entries (2 stage case) +* +* @its: its handle +* @id: index of the entry in the L1 table +* @addr: kernel VA +* @opaque: unused +* +* L1 table entries are scanned by steps of 1 entry +* Return < 0 if error, 0 if last dte was found when scanning the L2 +* table, +1 otherwise (meaning next L1 entry must be scanned) +*/ +static int handle_l1_dte (struct vgic_its *its,u32 id,void *addr, +void *opaque) +{ +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +int l2_start_id = id * (SZ_64K / abi->dte_esz); +u64 entry = *(u64 *)addr; +int dte_esz = abi->dte_esz; +gpa_t gpa; +int ret; + + entry =le64_to_cpu (entry); + +if (!(entry &KVM_ITS_L1E_VALID_MASK )) +return 1 ; + + gpa = entry &KVM_ITS_L1E_ADDR_MASK ; + + ret =scan_its_table (its, gpa,SZ_64K , dte_esz, + l2_start_id, vgic_its_restore_dte,NULL ); + +if (ret <=0 ) +return ret; + +return 1 ; }/** @@ -1806,7 +1966,31 @@ static int vgic_its_save_device_tables(struct vgic_its *its)*/ static int vgic_its_restore_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +u64 baser = its->baser_device_table; +int l1_esz, ret; +int l1_tbl_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ; +gpa_t l1_gpa; + +if (!(baser &GITS_BASER_VALID )) +return 0 ; + + l1_gpa =BASER_ADDRESS (baser); + +if (baser &GITS_BASER_INDIRECT ) { + l1_esz =GITS_LVL1_ENTRY_SIZE ; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + handle_l1_dte,NULL ); + }else { + l1_esz = abi->dte_esz; + ret =scan_its_table (its, l1_gpa, l1_tbl_size, l1_esz,0 , + vgic_its_restore_dte,NULL ); + } + +if (ret >0 ) + ret = -EINVAL ; + +return ret; } static int vgic_its_save_cte(struct vgic_its *its,virt/kvm/arm/vgic/vgic.h +10
-0@@ -81,6 +81,16 @@#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)#define KVM_ITS_CTE_RDBASE_SHIFT 16#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) +#define KVM_ITS_DTE_VALID_SHIFT 63 +#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63) +#define KVM_ITS_DTE_NEXT_SHIFT 49 +#define KVM_ITS_DTE_NEXT_MASK GENMASK_ULL(62, 49) +#define KVM_ITS_DTE_ITTADDR_SHIFT 5 +#define KVM_ITS_DTE_ITTADDR_MASK GENMASK_ULL(48, 5) +#define KVM_ITS_DTE_SIZE_MASK GENMASK_ULL(4, 0) +#define KVM_ITS_L1E_VALID_MASK BIT_ULL(63) +/* we only support 64 kB translation table page size */ +#define KVM_ITS_L1E_ADDR_MASK GENMASK_ULL(51, 16)static inline bool irq_is_pending (struct vgic_irq *irq) {Signature · 1 line
2.5.5_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 15/24] KVM: arm64: vgic-its: Read config and pending bit in add_lpi()
In reply to Auger EricQuoted 106 lines · Auger Eric
Hi,
Quoted 94 lines · Marc Zyngier
Quoted 93 lines · Christoffer Dall
Quoted 48 lines · Eric Auger
When creating the lpi we now ask the redistributor what is the state of the LPI (priority, enabled, pending).
Signed-off-by Eric Auger <[email protected]>---
v6: creation
--- virt/kvm/arm/vgic/vgic-its.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)virt/kvm/arm/vgic/vgic-its.c +6
-1@@ -38,6 +38,8 @@static int vgic_its_set_abi (struct vgic_its *its,int rev);static const struct vgic_its_abi *vgic_its_get_abi (struct vgic_its *its); +static int update_lpi_config (struct kvm *kvm,struct vgic_irq *irq, +struct kvm_vcpu *filter_vcpu);/* * Creates a new (reference to a) struct vgic_irq for a given LPI. @@ -46,10 +48,12 @@ static const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its);* If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure. */ -static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid) +static struct vgic_irq *vgic_add_lpi (struct kvm *kvm,u32 intid, +struct kvm_vcpu *vcpu) {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm,NULL , intid), *oldirq; +int ret;/* In this case there is no put, since we keep the reference. */ if (irq)@@ -66,6 +70,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) irq->config =VGIC_CONFIG_EDGE ;kref_init (&irq->refcount); irq->intid = intid; + irq->target_vcpu = vcpu;spin_lock (&dist->lpi_list_lock);virt/kvm/arm/vgic/vgic-its.c static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) · line 106
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +2
-0@@ -97,6 +102,19 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) out_unlock:spin_unlock (&dist->lpi_list_lock); +/* +* We "cache" the configuration table entries in out struct vgic_irq's. s/out/our/ ?
Quoted 23 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+* However we only have those structs for mapped IRQs, so we read in +* the respective config data from memory here upon mapping the LPI. +*/ + ret =update_lpi_config (kvm, irq,NULL ); +if (ret) +return ERR_PTR (ret); + + ret =vgic_v3_lpi_sync_pending_status (kvm, irq); +if (ret) +return ERR_PTR (ret); +return irq; }@@ -769,6 +787,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,u32 event_id =its_cmd_get_id (its_cmd);u32 coll_id =its_cmd_get_collection (its_cmd);struct its_ite *ite; +struct kvm_vcpu *vcpu =NULL ;struct its_device *device;struct its_collection *collection, *new_coll =NULL ;int lpi_nr;virt/kvm/arm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · line 839
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +4
-1@@ -814,7 +833,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, ite->collection = collection; ite->lpi = lpi_nr; - irq =vgic_add_lpi (kvm, lpi_nr); +if (its_is_collection_mapped (collection)) + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + irq =vgic_add_lpi (kvm, lpi_nr, vcpu);So, if we don't have the collection and therefore end up with
irq->vcpu== NULL, how do we ever read the pending bit from memory as the affinity may later change?Quoted 8 lines
Quoted 4 lines
Is this a problem with our idea of only looking at the pending bit on
vgic_add_lpi, or does it just mean that we should sample the pending bit whenever we move LPIs around to VCPUs and if the bit is set, then also set thepennding_latch(if not already set), or what should happen here?It means that we would need to sample that bit on MOVI and maybe MOVALL as well, but this feels a bit odd. How did that bit land there the first place?
Quoted 9 lines
Without talking about save/restore, before this series the pending table was sync'ed on RDIST LPI enable only and that's all. This is kept.
Now talking about save/restore, if we restore an LPI whose collection is not attached to any RDIST, we can't sync at that time. The problem exists if we check the pending bit on
vgic_add_lpior later, ie. at the end of the ITS table restore process (as I did before). I don't see in the spec we are supposed to read the pending table on MAPTI or MAPC.
Good point. If we don't have a collection for a translation that maps to an LPI then it must mean the mapping either never existed or was removed, and I don't see why we should have preserved any pending state after that.
Thanks,
-Christoffer
Re: [PATCH v6 21/24] KVM: arm64: vgic-its: Device table save/restore
In reply to Auger EricQuoted 212 lines · Auger Eric
Hi Christoffer,
Quoted 200 lines · Christoffer Dall
Quoted 199 lines · Eric Auger
This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.
For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch.
On restore, devices are re-allocated and their itt are re-built.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- accomodatevgic_its_alloc_devicechange of proto
- define bit fields for L1 entries
- s/handle_l1_entry/handle_l1_dte
- s/ite_esz/dte_esz inhandle_l1_dte
- check BASER valid bit
- s/nb_eventid_bits/num_eventid_bits
- new convention for returned values
- itt functions implemented in subsequent patchv4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- useabi->dte_esz
- clatify entry sizes for L1 and L2 tablesv3 -> v4:
- use the new proto forits_alloc_device
-compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fixhandle_l1_entryentry handling
- correctvgic_its_table_restoreerror handlingv2 -> v3:
- fixitt_addrbitmask invgic_its_restore_dte
- addition of return 0 invgic_its_restore_itemoved to the ITE related patchv1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing--- virt/kvm/arm/vgic/vgic-its.c | 194 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 10 +++ 2 files changed, 199 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +103
-4@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/list.h> #include <linux/uaccess.h> +#include <linux/list_sort.h> #include <linux/irqchip/arm-gic-v3.h> @@ -1701,7 +1702,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +static u32 compute_next_devid_offset (struct list_head *h, +struct its_device *dev) {struct its_device *next;u32 next_offset;@@ -1755,8 +1757,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,* Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables) */ -int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, -int start_id,entry_fn_t fn,void *opaque) +static int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) {void *entry =kzalloc (esz,GFP_KERNEL );struct kvm *kvm = its->dev->kvm;@@ -1791,13 +1793,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) +{ +return -ENXIO ; +} + +static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_dte - Save a device table entry at a given GPA +* +* @its: ITS handle +* @dev: ITS device +* @ptr: GPA +*/ +static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, +gpa_t ptr,int dte_esz) +{ +struct kvm *kvm = its->dev->kvm; +u64 val, itt_addr_field; +u32 next_offset; + + itt_addr_field = dev->itt_addr >>8 ; + next_offset =compute_next_devid_offset (&its->device_list, dev); + val = (1ULL <<KVM_ITS_DTE_VALID_SHIFT | + ((u64 )next_offset <<KVM_ITS_DTE_NEXT_SHIFT ) | + (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | + (dev->num_eventid_bits -1 )); + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, ptr, &val, dte_esz); +} + +/** +* vgic_its_restore_dte - restore a device table entry +* +* @its: its handle +* @id: device id the DTE corresponds to +* @ptr: kernel VA where the 8 byte DTE is located +* @opaque: unused +* +* Return: < 0 on error, 0 if the dte is the last one, id offset to the +* next dte otherwise +*/ +static int vgic_its_restore_dte (struct vgic_its *its,u32 id, +void *ptr,void *opaque) +{ +struct its_device *dev; +gpa_t itt_addr; +u8 num_eventid_bits; +u64 entry = *(u64 *)ptr; +bool valid; +u32 offset; +int ret; + + entry =le64_to_cpu (entry); + + valid = entry >>KVM_ITS_DTE_VALID_SHIFT ; + num_eventid_bits = (entry &KVM_ITS_DTE_SIZE_MASK ) +1 ; + itt_addr = ((entry &KVM_ITS_DTE_ITTADDR_MASK ) + >>KVM_ITS_DTE_ITTADDR_SHIFT ) <<8 ; + +if (!valid) +return 1 ; + +/* dte entry is valid */ + offset = (entry &KVM_ITS_DTE_NEXT_MASK ) >>KVM_ITS_DTE_NEXT_SHIFT ; + + dev =vgic_its_alloc_device (its, id, itt_addr, num_eventid_bits); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ret =vgic_its_restore_itt (its, dev); +if (ret) +return ret; + +return offset; +} + +static int vgic_its_device_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_device *deva = container_of(a, struct its_device, dev_list); + struct its_device *devb = container_of(b, struct its_device, dev_list); + +if (deva->device_id < devb->device_id) +return -1 ; +else +return 1 ; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM +* +* L1/L2 handling is hidden by vgic_its_check_id() helper which directly +* returns the GPA of the device entry */ static int vgic_its_save_device_tables (struct vgic_its *its) { -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_device *dev; +int dte_esz = abi->dte_esz;Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1904
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+u64 baser; + + baser = its->baser_device_table; + +list_sort (NULL , &its->device_list, vgic_its_device_cmp);this list is protected by the ITS mutex but you seem to be only holding the KVM mutex here, so don't we have a potential exploit here?
Quoted 5 lines
Updates to the device, ite list are done when running commands. As we hold the KVM mutex, commands cannot run. Then there is
vgic_its_destroy()which happens onkvm_put_kvmwhen all users have released their reference. So to me holding the kvm lock looks sufficient.
But we don't hold the KVM mutex when running commands, we run the its mutex? What am I missing?
Even worse, the vgic_its_trigger_msi also only takes the its->its_lock mutex (or rather its caller does) and that surely can run while we are saving the tables can it not?
Thanks,
-Christoffer
Re: [PATCH v6 21/24] KVM: arm64: vgic-its: Device table save/restore
Hi Christoffer,
Quoted 213 lines · Christoffer Dall
Quoted 212 lines · Auger Eric
Hi Christoffer,
Quoted 208 lines · Christoffer Dall
Quoted 194 lines · Eric Auger
This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.
For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch.
On restore, devices are re-allocated and their itt are re-built.
Signed-off-by Eric Auger <[email protected]>---
v5 -> v6:
- accomodatevgic_its_alloc_devicechange of proto
- define bit fields for L1 entries
- s/handle_l1_entry/handle_l1_dte
- s/ite_esz/dte_esz inhandle_l1_dte
- check BASER valid bit
- s/nb_eventid_bits/num_eventid_bits
- new convention for returned values
- itt functions implemented in subsequent patchv4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- useabi->dte_esz
- clatify entry sizes for L1 and L2 tablesv3 -> v4:
- use the new proto forits_alloc_device
-compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fixhandle_l1_entryentry handling
- correctvgic_its_table_restoreerror handlingv2 -> v3:
- fixitt_addrbitmask invgic_its_restore_dte
- addition of return 0 invgic_its_restore_itemoved to the ITE related patchv1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing--- virt/kvm/arm/vgic/vgic-its.c | 194 +++++++++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic.h | 10 +++ 2 files changed, 199 insertions(+), 5 deletions(-)virt/kvm/arm/vgic/vgic-its.c +100
-3@@ -23,6 +23,7 @@#include <linux/interrupt.h> #include <linux/list.h> #include <linux/uaccess.h> +#include <linux/list_sort.h> #include <linux/irqchip/arm-gic-v3.h> @@ -1701,7 +1702,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -u32 compute_next_devid_offset (struct list_head *h,struct its_device *dev) +static u32 compute_next_devid_offset (struct list_head *h, +struct its_device *dev) {struct its_device *next;u32 next_offset;@@ -1755,8 +1757,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,* Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables) */ -int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, -int start_id,entry_fn_t fn,void *opaque) +static int scan_its_table (struct vgic_its *its,gpa_t base,int size,int esz, +int start_id,entry_fn_t fn,void *opaque) {void *entry =kzalloc (esz,GFP_KERNEL );struct kvm *kvm = its->dev->kvm;@@ -1791,13 +1793,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,return ret; } +static int vgic_its_save_itt (struct vgic_its *its,struct its_device *device) +{ +return -ENXIO ; +} + +static int vgic_its_restore_itt (struct vgic_its *its,struct its_device *dev) +{ +return -ENXIO ; +} + +/** +* vgic_its_save_dte - Save a device table entry at a given GPA +* +* @its: ITS handle +* @dev: ITS device +* @ptr: GPA +*/ +static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, +gpa_t ptr,int dte_esz) +{ +struct kvm *kvm = its->dev->kvm; +u64 val, itt_addr_field; +u32 next_offset; + + itt_addr_field = dev->itt_addr >>8 ; + next_offset =compute_next_devid_offset (&its->device_list, dev); + val = (1ULL <<KVM_ITS_DTE_VALID_SHIFT | + ((u64 )next_offset <<KVM_ITS_DTE_NEXT_SHIFT ) | + (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | + (dev->num_eventid_bits -1 )); + val =cpu_to_le64 (val); +return kvm_write_guest (kvm, ptr, &val, dte_esz); +}protection + +/** +* vgic_its_restore_dte - restore a device table entry +* +* @its: its handle +* @id: device id the DTE corresponds to +* @ptr: kernel VA where the 8 byte DTE is located +* @opaque: unused +* +* Return: < 0 on error, 0 if the dte is the last one, id offset to the +* next dte otherwise +*/ +static int vgic_its_restore_dte(struct vgic_its *its, u32 id, +void *ptr,void *opaque) +{ +struct its_device *dev; +gpa_t itt_addr; +u8 num_eventid_bits; +u64 entry = *(u64 *)ptr; +bool valid; +u32 offset; +int ret; + + entry =le64_to_cpu (entry); + + valid = entry >>KVM_ITS_DTE_VALID_SHIFT ; + num_eventid_bits = (entry &KVM_ITS_DTE_SIZE_MASK ) +1 ; + itt_addr = ((entry &KVM_ITS_DTE_ITTADDR_MASK ) + >>KVM_ITS_DTE_ITTADDR_SHIFT ) <<8 ; + +if (!valid) +return 1 ; + +/* dte entry is valid */ + offset = (entry &KVM_ITS_DTE_NEXT_MASK ) >>KVM_ITS_DTE_NEXT_SHIFT ; + + dev =vgic_its_alloc_device (its, id, itt_addr, num_eventid_bits); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ret =vgic_its_restore_itt (its, dev); +if (ret) +return ret; + +return offset; +} + +static int vgic_its_device_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ + struct its_device *deva = container_of(a, struct its_device, dev_list); + struct its_device *devb = container_of(b, struct its_device, dev_list); + +if (deva->device_id < devb->device_id) +return -1 ; +else +return 1 ; +} +/** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM +* +* L1/L2 handling is hidden by vgic_its_check_id() helper which directly +* returns the GPA of the device entry */ static int vgic_its_save_device_tables (struct vgic_its *its)virt/kvm/arm/vgic/vgic-its.c int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1904
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-1{ -return -ENXIO ; +const struct vgic_its_abi *abi =vgic_its_get_abi (its); +struct its_device *dev; +int dte_esz = abi->dte_esz; +u64 baser; + + baser = its->baser_device_table; + +list_sort (NULL , &its->device_list, vgic_its_device_cmp);this list is protected by the ITS mutex but you seem to be only holding the KVM mutex here, so don't we have a potential exploit here?
Quoted 8 lines
Quoted 5 lines
Updates to the device, ite list are done when running commands. As we hold the KVM mutex, commands cannot run. Then there is
vgic_its_destroy()which happens onkvm_put_kvmwhen all users have released their reference. So to me holding the kvm lock looks sufficient.But we don't hold the KVM mutex when running commands, we run the its mutex? What am I missing?
Yes we do. The kvm lock is taken in vgic_its_attr_regs_access. Commands are processed on vgic_mmio_write_its_cwriter and vgic_mmio_write_its_ctlr
Quoted 4 lines
Even worse, the
vgic_its_trigger_msialso only takes theits->its_lockmutex (or rather its caller does) and that surely can run while we are saving the tables can it not?
Hum yes this can theoretically happen in a non qemu use case. Otherwise the VM being stopped at that time, injecting a new MSI at that point looks as invalid. Looks safer I take the its lock too then.
Thanks for spotting this!
Eric
Quoted 9 lines
Thanks,
-Christoffer_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel