Introduce enablemenant of dom0less
PATCH v2 1/26 xen: arm: update p2m_set_allocation() prototype
In reply to Oleksii Kurochkop2m_set_allocation() signals preemption redundantly: via a bool *preempted out-argument (set to true) and via -ERESTART return code, both at the same time. This led to the caller-side ASSERT(preempted == (rc == -ERESTART)) added solely to document their agreement.
Drop the out-argument entirely. The return code alone is sufficient to distinguish preemption (-ERESTART) from success (0) or other failures. Replace the pointer with a plain bool can_preempt that controls whether the preemption check is performed at all, making the previous NULL-to-suppress-preemption calling convention explicit and type-safe.
Since p2m_set_allocation() is called by the common dom0less build code, move its declaration from the ARM-specific asm/p2m.h to xen/p2m-common.h.
---
Changes in v2:
- new patch
--- xen/arch/arm/include/asm/p2m.h | 1 - xen/arch/arm/mmu/p2m.c | 22 ++++++++-------------- xen/arch/riscv/include/asm/paging.h | 2 +- xen/arch/riscv/p2m.c | 7 ++++--- xen/arch/riscv/paging.c | 7 ++----- xen/common/device-tree/dom0less-build.c | 2 +- xen/include/xen/p2m-common.h | 2 ++ 7 files changed, 18 insertions(+), 25 deletions(-)
xen/arch/arm/include/asm/p2m.h +0 -1
@@ -232,7 +232,6 @@ void p2m_restore_state(struct vcpu *n);/* Print debugging/statistial info about a domain's p2m */ void p2m_dump_info (struct domain *d); -int p2m_set_allocation (struct domain *d,unsigned long pages,bool *preempted);int p2m_teardown_allocation (struct domain *d);static inline void p2m_write_lock (struct p2m_domain *p2m)
xen/arch/arm/mmu/p2m.c +8 -14
@@ -67,10 +67,11 @@ int arch_get_paging_mempool_size(struct domain *d, uint64_t *size)/* * Set the pool of pages to the required number of pages. -* Returns 0 for success, non-zero for failure. +* Returns 0 for success, -ERESTART if preempted, or a negative error code on +* failure. * Call with d->arch.paging.lock held. */ -int p2m_set_allocation (struct domain *d,unsigned long pages,bool *preempted) +int p2m_set_allocation (struct domain *d,unsigned long pages,bool can_preempt) {struct page_info *pg;
@@ -112,11 +113,8 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)break ;/* Check to see if we need to yield and try again */ -if ( preempted &&general_preempt_check () ) - { - *preempted = true; +if ( can_preempt &&general_preempt_check () )return -ERESTART ; - } }return 0 ;
@@ -125,7 +123,6 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)int arch_set_paging_mempool_size (struct domain *d,uint64_t size) {unsigned long pages = size >>PAGE_SHIFT ; -bool preempted = false;int rc; if ( (size & ~PAGE_MASK) || /* Non page-sized request? */
@@ -133,27 +130,24 @@ int arch_set_paging_mempool_size(struct domain *d, uint64_t size)return -EINVAL ;spin_lock (&d->arch.paging.lock); - rc =p2m_set_allocation (d, pages, &preempted); + rc =p2m_set_allocation (d, pages, true);spin_unlock (&d->arch.paging.lock); -ASSERT (preempted == (rc == -ERESTART )); -return rc; }int p2m_teardown_allocation (struct domain *d) {int ret =0 ; -bool preempted = false;spin_lock (&d->arch.paging.lock);if ( d->arch.paging.p2m_total_pages !=0 ) { - ret =p2m_set_allocation (d,0 , &preempted); -if ( preempted ) + ret =p2m_set_allocation (d,0 , true); +if ( ret == -ERESTART ) {spin_unlock (&d->arch.paging.lock); -return -ERESTART ; +return ret; }ASSERT (d->arch.paging.p2m_total_pages ==0 ); }
xen/arch/riscv/include/asm/paging.h +1 -1
@@ -9,7 +9,7 @@ struct page_info;int paging_domain_init (struct domain *d);int paging_freelist_adjust (struct domain *d,unsigned long pages, -bool *preempted); +bool can_preempt);int paging_ret_to_domheap (struct domain *d,unsigned int nr_pages);int paging_refill_from_domheap (struct domain *d,unsigned int nr_pages);
xen/arch/riscv/p2m.c +4 -3
@@ -430,15 +430,16 @@ int p2m_init(struct domain *d, const struct xen_domctl_createdomain *config)/* * Set the pool of pages to the required number of pages. -* Returns 0 for success, non-zero for failure. +* Returns 0 for success, -ERESTART if preempted, or a negative error code on +* failure. * Call with d->arch.paging.lock held. */ -int p2m_set_allocation (struct domain *d,unsigned long pages,bool *preempted) +int p2m_set_allocation (struct domain *d,unsigned long pages,bool can_preempt) {struct p2m_domain *p2m =p2m_get_hostp2m (d);int rc; -if ( (rc =paging_freelist_adjust (d, pages, preempted)) ) +if ( (rc =paging_freelist_adjust (d, pages, can_preempt)) )return rc;/*
xen/arch/riscv/paging.c +2 -5
@@ -47,7 +47,7 @@ static int _paging_add_to_freelist(struct domain *d) }int paging_freelist_adjust (struct domain *d,unsigned long pages, -bool *preempted) +bool can_preempt) {ASSERT (spin_is_locked (&d->arch.paging.lock));
@@ -66,11 +66,8 @@ int paging_freelist_adjust(struct domain *d, unsigned long pages,return rc;/* Check to see if we need to yield and try again */ -if ( preempted &&general_preempt_check () ) - { - *preempted = true; +if ( can_preempt &&general_preempt_check () )return -ERESTART ; - } }return 0 ;
xen/common/device-tree/dom0less-build.c +1 -1
@@ -747,7 +747,7 @@ static int __init domain_p2m_set_allocation(struct domain *d, uint64_t mem,domain_p2m_pages (mem, d->max_vcpus);spin_lock (&d->arch.paging.lock); - rc =p2m_set_allocation (d, p2m_pages,NULL ); + rc =p2m_set_allocation (d, p2m_pages, false);spin_unlock (&d->arch.paging.lock);return rc;
xen/include/xen/p2m-common.h +2 -0
@@ -43,5 +43,7 @@ int __must_check check_get_page_from_gfn(struct domain *d, gfn_t gfn, bool readonly, p2m_type_t *p2mt_p, struct page_info **page_p); +int p2m_set_allocation (struct domain *d,unsigned long pages, +bool can_preempt);#endif /* _XEN_P2M_COMMON_H */
Signature · 1 line
2.54.0
PATCH v2 2/26 xen/riscv: Implement ARCH_PAGING_MEMPOOL
In reply to Oleksii KurochkoThe p2m_freelist is used to allocate pages for the P2M. To initialize this list, domain_p2m_set_allocation() may be called from construct_domU() in the common Dom0less code, so RISC-V provides an implementation and enables CONFIG_ARCH_PAGING_MEMPOOL unconditionally.
Additionally, implement arch_{set,get}_paging_mempool_size(). They are not directly used yet, but are required to support the XEN_DOMCTL_{get,set}_paging_mempool_size hypercalls.
---
Changes in v2:
- Turn on CONFIG_ARCH_PAGING_MEMPOOL=y unconditionally and drop all ifdef-s related to this config.
- Optimize check inside arch_set_paging_mempool_size which verify size argument.
- Use pfn_to_paddr() inside arch_get_paging_mempool_size() instead of open coding the stuff.
- Drop ASSERT() from arch_set_paging_mempool_size() as it is impossible to have here preempted = true and rc != -ERESTART.
---
--- xen/arch/riscv/Kconfig | 1 + xen/arch/riscv/p2m.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
xen/arch/riscv/Kconfig +1 -0
@@ -1,5 +1,6 @@ config RISCV def_bool y + select ARCH_PAGING_MEMPOOL select DOMAIN_BUILD_HELPERS select FUNCTION_ALIGNMENT_16B select GENERIC_BUG_FRAME
xen/arch/riscv/p2m.c +24 -0
@@ -1606,3 +1606,27 @@ struct page_info *get_page_from_gfn(struct domain *d, unsigned long gfn,return p2m_get_page_from_gfn (p2m_get_hostp2m (d),_gfn (gfn), t); } + +int arch_set_paging_mempool_size (struct domain *d,uint64_t size) +{ +unsigned long pages =PFN_DOWN (size); +int rc; + +/* Non page-sized request or 32-bit overflow? */ +if (pfn_to_paddr (pages) != size ) +return -EINVAL ; + +spin_lock (&d->arch.paging.lock); + rc =p2m_set_allocation (d, pages, true); +spin_unlock (&d->arch.paging.lock); + +return rc; +} + +/* Return the size of the pool, in bytes. */ +int arch_get_paging_mempool_size (struct domain *d,uint64_t *size) +{ + *size =pfn_to_paddr (ACCESS_ONCE (d->arch.paging.total_pages)); + +return 0 ; +}
Signature · 1 line
2.54.0
v2 cover Introduce enablemenant of dom0less
This patch series reprensent a bunch of patches necessary to enable common part of Dom0less.
The stuff necessary to start/launch domains will be introduced separately.
This patch series is based on [1], but a lot of patch could go even without it.
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
CI tests: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2510638764
---
Changes in v2:
- Move patch "[PATCH v1 04/27] xen/riscv: rework G-stage mode handling" to patch series [1]
- Address the comments from ML.
- The following patches were folded into one:
# xen/riscv: implement init_intc_phandle()
# xen/riscv: call do_initcalls() in start_xen()
# xen/riscv: setup system domains
- The following patch were folded into one:
# xen/riscv: add vaplic access check
# xen/riscv: emulate guest writes to virtual APLIC MMIO
# xen/riscv: emulate guest reads from virtual APLIC MMIO
- Add new bug fix, not really necessary to this patch series:
xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable callbacks
---
Oleksii Kurochko (26):
xen: arm: update p2m_set_allocation() prototype
xen/riscv: Implement ARCH_PAGING_MEMPOOL
xen/riscv: Implement construct_domain()
xen/riscv: implement prerequisites for domain_create() xen/riscv: introduce guest riscv,isa string
xen/riscv: implement make_cpus_node()
xen/riscv: implement make_timer_node()
xen/riscv: implement make_arch_nodes()
xen/riscv: introduce init interrupt controller operations xen/riscv: implement make_intc_domU_node()
xen/riscv: introduce aia_init() and aia_usable() xen/riscv: add basic VGEIN management for AIA guests xen/riscv: introduce per-vCPU IMSIC state
xen/riscv: add very early virtual APLIC (vAPLIC) initialization
support
xen/riscv: introduce (de)initialization helpers for vINTC xen/riscv: create APLIC DT node for guest domains xen/riscv: generate IMSIC DT node for guest domains
xen: move declaration of map_device_irqs_to_domain() to common header xen/riscv: implement IRQ routing for device passthrough xen/riscv: add missing APLIC register offsets, masks to asm/aplic.h. xen/riscv: implement virtual APLIC MMIO emulation xen/riscv: implement init_intc_phandle()
xen/riscv: initialize RCU, scheduler, and system domains in
start_xen()
xen/riscv: provide init_vuart()
xen/riscv: add initial dom0less infrastructure support xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable
callbacks
automation/gitlab-ci/build.yaml | 1 + xen/arch/arm/include/asm/p2m.h | 1 - xen/arch/arm/include/asm/setup.h | 3 - xen/arch/arm/mmu/p2m.c | 22 +- xen/arch/riscv/Kconfig | 3 + xen/arch/riscv/Makefile | 4 + xen/arch/riscv/aia.c | 167 +++++++++ xen/arch/riscv/aplic.c | 49 ++- xen/arch/riscv/cpufeature.c | 59 ++- xen/arch/riscv/device.c | 108 ++++++ xen/arch/riscv/dom0less-build.c | 39 ++ xen/arch/riscv/domain-build.c | 179 +++++++++ xen/arch/riscv/domain.c | 44 ++- xen/arch/riscv/imsic.c | 167 +++++++++ xen/arch/riscv/include/asm/aia.h | 18 + xen/arch/riscv/include/asm/aplic.h | 53 +++ xen/arch/riscv/include/asm/cpufeature.h | 5 + xen/arch/riscv/include/asm/domain.h | 8 + xen/arch/riscv/include/asm/guest-layout.h | 7 + xen/arch/riscv/include/asm/imsic.h | 26 ++ xen/arch/riscv/include/asm/intc.h | 58 ++- xen/arch/riscv/include/asm/irq.h | 3 + xen/arch/riscv/include/asm/paging.h | 2 +- xen/arch/riscv/include/asm/setup.h | 8 +- xen/arch/riscv/include/asm/vaplic.h | 37 ++ xen/arch/riscv/intc.c | 103 ++++- xen/arch/riscv/irq.c | 178 ++++++++- xen/arch/riscv/p2m.c | 31 +- xen/arch/riscv/paging.c | 7 +- xen/arch/riscv/setup.c | 14 + xen/arch/riscv/stubs.c | 12 - xen/arch/riscv/vaplic.c | 436 ++++++++++++++++++++++ xen/common/device-tree/dom0less-build.c | 2 +- xen/include/xen/fdt-domain-build.h | 13 + xen/include/xen/p2m-common.h | 2 + 35 files changed, 1807 insertions(+), 62 deletions(-) create mode 100644 xen/arch/riscv/aia.c create mode 100644 xen/arch/riscv/device.c create mode 100644 xen/arch/riscv/domain-build.c create mode 100644 xen/arch/riscv/include/asm/aia.h create mode 100644 xen/arch/riscv/include/asm/vaplic.h create mode 100644 xen/arch/riscv/vaplic.c
Signature · 1 line
2.54.0
PATCH v2 3/26 xen/riscv: Implement construct_domain()
Implement construct_domain() function for RISC-V, which performs initial setup for the domain's first vCPU, loads the kernel, initrd, and device tree, and sets up guest CPU registers for boot.
It also creates additional vCPUs up to max_vcpus and assigns the device tree address and boot cpuid in registers.
---
Changes in v2:
- Rework construct_domain() to print that vCPU1...n are created using %pv.
- Use true instead of 1 for initialization of v->is_initialised.
- Drop unnessary BUG_ON() in construct_domain().
- Add TODO comment above *_load() functions.
---
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/domain-build.c | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 xen/arch/riscv/domain-build.c
xen/arch/riscv/Makefile +1 -0
@@ -1,6 +1,7 @@ obj-y += aplic.o obj-y += cpufeature.o obj-y += domain.o +obj-y += domain-build.init.o obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-build.init.o obj-$(CONFIG_EARLY_PRINTK) += early_printk.o obj-y += entry.o
xen/arch/riscv/domain-build.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v2 7/26 xen/riscv: implement make_timer_node()
In reply to Oleksii KurochkoGenerally, in DT for RISC-V there is a document which describes a timer node (riscv,timer.yaml or sifive,clint.yaml), but the Linux timer driver is declared with TIMER_OF_DECLARE(riscv_timer, "riscv", ...). It matches the CPU node (compatible "riscv"), not the timer node itself. It then calls of_find_compatible_node(NULL, NULL, "riscv,timer") only to read the optional riscv,timer-cannot-wake-cpu property.
Since Xen does not care about that property for now, make_timer_node() is implemented to return 0, as no timer node needs to be created for RISC-V guests.
---
Changes in v2:
- Acked-by: Jan Beulich <[email protected]>
- Update the commit message.
---
--- xen/arch/riscv/domain-build.c | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/arch/riscv/domain-build.c +8 -0
@@ -3,6 +3,7 @@#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/fdt-kernel.h> #include <xen/libfdt/libfdt.h> #include <xen/sched.h>
@@ -156,3 +157,10 @@ int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo)return fdt_end_node (fdt); } + +int __init make_timer_node (const struct kernel_info *kinfo) +{ +/* There is no need for timer node for RISC-V. */ + +return 0 ; +}
Signature · 1 line
2.54.0
PATCH v2 4/26 xen/riscv: implement prerequisites for domain_create()
In reply to Oleksii Kurochkoarch_domain_create() and arch_sanitise_domain_config() are prerequisites for domain_create().
arch_sanitise_domain_config() currently returns 0, as there is no specific work required at this stage.
arch_domain_create() performs basic initialization, such as setting up the P2M and initializing of next unused phandle.
---
Changes in v2:
- update the commit message.
- Drop vcpu_switch_to_aarch64_mode() from riscv/stubs. It shouldn't be under riscv/ at all.
- Drop next_phandle as it is now in common code.
--- xen/arch/riscv/domain.c | 25 +++++++++++++++++++++++++ xen/arch/riscv/stubs.c | 12 ------------ 2 files changed, 25 insertions(+), 12 deletions(-)
xen/arch/riscv/domain.c +25 -0
@@ -289,6 +289,31 @@ void sync_vcpu_execstate(struct vcpu *v)/* Nothing to do -- no lazy switching */ } +int arch_sanitise_domain_config (struct xen_domctl_createdomain *config) +{ +return 0 ; +} + +int arch_domain_create (struct domain *d, +struct xen_domctl_createdomain *config, +unsigned int flags) +{ +int rc =0 ; + +if (is_idle_domain (d) ) +return 0 ; + +if ( (rc =p2m_init (d, config)) !=0 ) + goto fail; + +return rc; + + fail: + d->is_dying = DOMDYING_dead; +arch_domain_destroy (d); +return rc; +} +static void __init __maybe_unused build_assertions (void ) {/*
xen/arch/riscv/stubs.c +0 -12
@@ -101,18 +101,6 @@ void dump_pageframe_info(struct domain *d)BUG_ON ("unimplemented" ); } -int arch_sanitise_domain_config (struct xen_domctl_createdomain *config) -{ -BUG_ON ("unimplemented" ); -} - -int arch_domain_create (struct domain *d, -struct xen_domctl_createdomain *config, -unsigned int flags) -{ -BUG_ON ("unimplemented" ); -} -int arch_domain_teardown (struct domain *d) {BUG_ON ("unimplemented" );
Signature · 1 line
2.54.0
PATCH v2 5/26 xen/riscv: introduce guest riscv,isa string
In reply to Oleksii KurochkoIntroduce generation of the riscv,isa string passed to the guest via the Device Tree riscv,isa property.
Introduce the per-domain isa string and guest isa bitmap, populated during domain creation by calling init_guest_isa().
Introduce guest_unsupp to filter out ISA extensions that should not be exposed to guests:
- f/d/q/v: FPU and vector context save/restore are not yet implemented for guests.
- h: Nested virtualisation is not supported.
- sstc: Xen owns the supervisor timer; guests must use SBI.
- svade: Xen manages hardware A/D bit updates in stage-2 page tables.
- svpbmt: Page-based memory types are not yet wired up in stage-2 code.
Drop __initconst for riscv_isa_ext() as it can be used after init stage by init_guest_isa().
---
Changes in v2:
- s/guest_unsupp_bmp/guest_unsupp.
- Drop guest_isa_str.
- Provide init_guest_isa() instead of polluting match_isa_ext().
- Drop xlen.
- Add the comment about guest_unsupp.
- Update the way how guest_unsupp is init-ed.
- Drop __initconst for riscv_isa_ext[] as it is used in init_guest_isa() which isn't marked as __init as it could be used after init stage.
---
--- xen/arch/riscv/cpufeature.c | 59 ++++++++++++++++++++++++- xen/arch/riscv/domain.c | 3 ++ xen/arch/riscv/include/asm/cpufeature.h | 5 +++ xen/arch/riscv/include/asm/domain.h | 4 ++ 4 files changed, 70 insertions(+), 1 deletion(-)
xen/arch/riscv/cpufeature.c +58 -1
@@ -14,6 +14,7 @@#include <xen/errno.h> #include <xen/init.h> #include <xen/lib.h> +#include <xen/sched.h> #include <xen/sections.h> #include <asm/cpufeature.h>
@@ -120,7 +121,7 @@ static int __init dt_get_cpuid_from_node(const struct dt_device_node *cpu,* and strncmp() is used in match_isa_ext() to compare extension names instead * of strncasecmp(). */ -const struct riscv_isa_ext_data __initconst riscv_isa_ext[] = { +const struct riscv_isa_ext_data riscv_isa_ext[] = {RISCV_ISA_EXT_DATA (i),RISCV_ISA_EXT_DATA (m),RISCV_ISA_EXT_DATA (a),
@@ -128,6 +129,7 @@ const struct riscv_isa_ext_data __initconst riscv_isa_ext[] = {RISCV_ISA_EXT_DATA (d),RISCV_ISA_EXT_DATA (q),RISCV_ISA_EXT_DATA (c), +RISCV_ISA_EXT_DATA (v),RISCV_ISA_EXT_DATA (h),RISCV_ISA_EXT_DATA (zicntr), RISCV_ISA_EXT_DATA(zicsr),
@@ -160,6 +162,12 @@ static const struct riscv_isa_ext_data __initconst required_extensions[] = { RISCV_ISA_EXT_DATA(svpbmt), }; +/* +* Everything in riscv_isa_ext[] which shouldn't be exposed to guests should +* appear here. +*/ +static __ro_after_init DECLARE_BITMAP(guest_unsupp, RISCV_ISA_EXT_MAX); +static bool __init is_lowercase_extension_name (const char *str) {/*
@@ -480,6 +488,53 @@ bool riscv_isa_extension_available(const unsigned long *isa_bitmap,return test_bit (id, isa_bitmap); } +int init_guest_isa (struct domain *d) +{ +char *buf = d->arch.guest_isa_str; +size_t len =sizeof (d->arch.guest_isa_str); + +bitmap_andnot (d->arch.guest_isa, riscv_isa, guest_unsupp, +RISCV_ISA_EXT_MAX ); + +#if defined(CONFIG_RISCV_32 ) +if (snprintf (buf, len,"rv32" ) >= len ) +return -ENOBUFS ; +#elif defined(CONFIG_RISCV_64 ) +if (snprintf (buf, len,"rv64" ) >= len ) +return -ENOBUFS ; +#else +# error "Unsupported RISC-V bitness" +#endif + +for (unsigned int i =0 ; i <ARRAY_SIZE (riscv_isa_ext); i++ ) + { +const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; + +if ( !riscv_isa_extension_available (d->arch.guest_isa, ext->id) ) +continue ; + +if ( ext->id >=RISCV_ISA_EXT_BASE &&strlcat (buf,"_" , len) >= len ) +return -ENOBUFS ; + +if (strlcat (buf, ext->name, len) >= len ) +return -ENOBUFS ; + } + +return 0 ; +} + +static void __init init_guest_unsupp (void ) +{ +set_bit (RISCV_ISA_EXT_f, guest_unsupp); +set_bit (RISCV_ISA_EXT_d, guest_unsupp); +set_bit (RISCV_ISA_EXT_q, guest_unsupp); +set_bit (RISCV_ISA_EXT_v, guest_unsupp); +set_bit (RISCV_ISA_EXT_h, guest_unsupp); +set_bit (RISCV_ISA_EXT_sstc, guest_unsupp); +set_bit (RISCV_ISA_EXT_svade, guest_unsupp); +set_bit (RISCV_ISA_EXT_svpbmt, guest_unsupp); +} +void __init riscv_fill_hwcap (void ) {unsigned int i;
@@ -527,4 +582,6 @@ void __init riscv_fill_hwcap(void)if ( !all_extns_available )panic ("Look why the extensions above are needed in " "https://xenbits.xenproject.org/docs/unstable/misc/riscv/booting.txt\n" ); + +init_guest_unsupp (); }
xen/arch/riscv/domain.c +3 -0
@@ -303,6 +303,9 @@ int arch_domain_create(struct domain *d,if (is_idle_domain (d) )return 0 ; +if ( (rc =init_guest_isa (d)) !=0 ) + goto fail; +if ( (rc =p2m_init (d, config)) !=0 ) goto fail;
xen/arch/riscv/include/asm/cpufeature.h +5 -0
@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256 +enum riscv_isa_ext_id { RISCV_ISA_EXT_a, RISCV_ISA_EXT_c,
@@ -44,7 +46,10 @@ enum riscv_isa_ext_id { RISCV_ISA_EXT_MAX }; +struct domain ; +void riscv_fill_hwcap (void ); +int init_guest_isa (struct domain *d);bool riscv_isa_extension_available (const unsigned long *isa_bitmap,enum riscv_isa_ext_id id);
xen/arch/riscv/include/asm/domain.h +4 -0
@@ -7,6 +7,7 @@#include <xen/xmalloc.h> #include <public/hvm/params.h> +#include <asm/cpufeature.h> #include <asm/guest-layout.h> #include <asm/p2m.h> #include <asm/vtimer.h>
@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ]; };#include <xen/sched.h>
Signature · 1 line
2.54.0
PATCH v2 8/26 xen/riscv: implement make_arch_nodes()
In reply to Oleksii KurochkoNo RISC-V-specific nodes need to be created at the moment, so make_arch_nodes() is implemented to simply return 0.
It is placed in dom0less-build.c as make_arch_nodes() is only used in the dom0less code path. In the future, it will be extended to create an emulated UART node.
---
Changes in v2:
- Update the commit message.
---
--- xen/arch/riscv/dom0less-build.c | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/arch/riscv/dom0less-build.c +8 -0
@@ -2,10 +2,18 @@#include <xen/bootfdt.h> #include <xen/device_tree.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <asm/p2m.h> +int __init make_arch_nodes (struct kernel_info *kinfo) +{ +/* No RISC-V specific nodes need to be made, at the moment. */ + +return 0 ; +} +int __init arch_parse_dom0less_node (struct dt_device_node *node,struct boot_domain *bd) {
Signature · 1 line
2.54.0
PATCH v2 6/26 xen/riscv: implement make_cpus_node()
In reply to Oleksii KurochkoImplement make_cpus_node() to create cpus node for a guest domain.
This function is going to be use by common dom0less code during construction domain.
---
Changes in v2:
- s/u32/uint32_t for timebase_frequency local variable.
- Drop +1 from BUILD_BUG_ON().
- return fdt_end_node(fdt); instead of res at the end of the function.
---
--- xen/arch/riscv/domain-build.c | 106 ++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+)
xen/arch/riscv/domain-build.c +106 -0
@@ -3,8 +3,10 @@#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/sched.h> +#include <asm/cpufeature.h> #include <asm/current.h> #include <asm/guest_access.h>
@@ -50,3 +52,107 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)return 0 ; } +int __init make_cpus_node (const struct domain *d,struct kernel_info *kinfo) +{ +int res; +const struct dt_device_node *cpus =dt_find_node_by_path ("/cpus" ); +unsigned int cpu; +uint32_t timebase_frequency; +bool frequency_valid; +void *fdt = kinfo->fdt; + +dt_dprintk ("Create cpus node\n" ); + +if ( !cpus ) + { +dprintk (XENLOG_ERR ,"Missing /cpus node in the device tree?\n" ); +return -ENOENT ; + } + + frequency_valid =dt_property_read_u32 (cpus,"timebase-frequency" , + &timebase_frequency); + + res =fdt_begin_node (fdt,"cpus" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#address-cells" ,1 ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#size-cells" ,0 ); +if ( res ) +return res; + +if ( frequency_valid ) + res =fdt_property_cell (fdt,"timebase-frequency" , timebase_frequency); + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ ) + { +char buf[64 ]; +uint32_t reg =cpu_to_fdt32 (cpu); + +snprintf (buf,sizeof (buf),"cpu@%u" , cpu); + res =fdt_begin_node (fdt, buf); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , ®,sizeof (reg)); +if ( res ) +return res; + + res =fdt_property_string (fdt,"status" ,"okay" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv" ); +if ( res ) +return res; + +BUILD_BUG_ON ((sizeof ("riscv," ) + + sizeof_field(struct gstage_mode_desc, name)) >= sizeof(buf)); +snprintf (buf,sizeof (buf),"riscv,%s" , max_gstage_mode->name); + res =fdt_property_string (fdt,"mmu-type" , buf); +if ( res ) +return res; + + res =fdt_property_string (fdt,"riscv,isa" , d->arch.guest_isa_str); +if ( res ) +return res; + + res =fdt_property_string (fdt,"device_type" ,"cpu" ); +if ( res ) +return res; + + res =fdt_begin_node (fdt,"interrupt-controller" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv,cpu-intc" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#interrupt-cells" ,1 ); +if ( res ) +return res; + + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_u32 (fdt,"phandle" ,alloc_phandle (kinfo)); +if ( res ) +return res; + +/* end of interrupt-controller */ + res =fdt_end_node (fdt); +if ( res ) +return res; + + res =fdt_end_node (fdt); +if ( res ) +return res; + } + +return fdt_end_node (fdt); +}
Signature · 1 line
2.54.0
PATCH v2 10/26 xen/riscv: implement make_intc_domU_node()
In reply to Oleksii KurochkoIntroduce a RISC-V specific function to create an interrupt controller Device Tree node for DomU domains during dom0less build.
Add make_intc_domU_node() to the dom0less build path and wire it to a new generic helper, intc_make_domu_dt_node(), which delegates DT node creation to the active interrupt controller implementation via vintc_init_ops.
---
Changes in v2:
- s/intc_make_domu_dt_node/make_intc_domU_node.
- introduce separate intc_hw_init_ops structure for init operations.
- Return -EOPNOTSUPP instead of -ENOSYS.
- Drop const for kinfo argument as it could be changed by interrupt controller node creation code.
- Refactor make_domu_dt_node().
- Make make_domu_dt_node part of vintc structure as it looks more logical to be there.
--- xen/arch/riscv/include/asm/domain.h | 2 ++ xen/arch/riscv/include/asm/intc.h | 12 ++++++++++-- xen/arch/riscv/intc.c | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-)
xen/arch/riscv/include/asm/domain.h +2 -0
@@ -98,6 +98,8 @@ struct arch_domain {DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX );char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ]; + +struct vintc *vintc; };#include <xen/sched.h>
xen/arch/riscv/include/asm/intc.h +10 -2
@@ -8,14 +8,13 @@ #ifndef ASM__RISCV__INTERRUPT_CONTOLLER_H #define ASM__RISCV__INTERRUPT_CONTOLLER_H -struct dt_device_node; - enum intc_version { INTC_APLIC, }; struct cpu_user_regs; struct irq_desc; +struct kernel_info; struct intc_info { enum intc_version hw_version;
@@ -46,6 +45,15 @@ struct intc_hw_init_ops {int (*init)(void ); }; +struct vintc_init_ops { +/* Create interrupt controller node for domain */ +int (*make_domu_dt_node)(struct kernel_info *kinfo); +}; + +struct vintc { +struct vintc_init_ops *init_ops; +}; +void intc_preinit (void ); void register_intc_ops(const struct intc_hw_operations *ops,
xen/arch/riscv/intc.c +11 -0
@@ -3,6 +3,7 @@#include <xen/acpi.h> #include <xen/bug.h> #include <xen/device_tree.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <xen/irq.h> #include <xen/lib.h>
@@ -73,3 +74,13 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)intc_set_irq_type (desc, desc->arch.type);intc_set_irq_priority (desc, priority); } + +int __init make_intc_domU_node (struct kernel_info *kinfo) +{ +struct vintc *vintc = kinfo->bd.d->arch.vintc; + +if ( intc_hw_ops && vintc->init_ops && vintc->init_ops->make_domu_dt_node ) +return vintc->init_ops->make_domu_dt_node (kinfo); + +return -EOPNOTSUPP ; +}
Signature · 1 line
2.54.0
PATCH v2 11/26 xen/riscv: introduce aia_init() and aia_usable()
In reply to Oleksii Kurochkoaia_init() is going to contain all the logic related to AIA initialization.
At the moment, it only checks whether the SSAIA extension is available, and if so, sets is_aia_usable (which indicates more than just the availability of the extension) to true; it also signifies that the necessary components (to be introduced in follow-up patches) have been initialized.
---
Changes in v2:
- s/is_aia_available/is_aia_usable.
- Drop return value for aia_init().
- s/aia_available()/aia_usable().
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/aia.c | 23 +++++++++++++++++++++++ xen/arch/riscv/include/asm/aia.h | 10 ++++++++++ xen/arch/riscv/intc.c | 3 +++ 4 files changed, 37 insertions(+) create mode 100644 xen/arch/riscv/aia.c create mode 100644 xen/arch/riscv/include/asm/aia.h
xen/arch/riscv/Makefile +1 -0
@@ -1,3 +1,4 @@ +obj-y += aia.o obj-y += aplic.o obj-y += cpufeature.o obj-y += domain.o
xen/arch/riscv/aia.c new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <xen/errno.h> +#include <xen/init.h> +#include <xen/sections.h> +#include <xen/types.h> + +#include <asm/cpufeature.h> + +static bool __ro_after_init is_aia_usable; + +bool aia_usable (void ) +{ +return is_aia_usable; +} + +void __init aia_init (void ) +{ +if ( !riscv_isa_extension_available (NULL , RISCV_ISA_EXT_ssaia) ) +return ; + + is_aia_usable = true; +}
xen/arch/riscv/include/asm/aia.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef ASM__RISCV__AIA_H +#define ASM__RISCV__AIA_H + +bool aia_usable (void ); + +void aia_init (void ); + +#endif /* ASM__RISCV__ACPI_H */
xen/arch/riscv/intc.c +3 -0
@@ -9,6 +9,7 @@#include <xen/lib.h> #include <xen/spinlock.h> +#include <asm/aia.h> #include <asm/intc.h> static const struct intc_hw_operations *__ro_after_init intc_hw_ops;
@@ -34,6 +35,8 @@ void __init intc_init(void) {ASSERT (intc_hw_init_ops && intc_hw_init_ops->init); +aia_init (); +if ( intc_hw_init_ops->init () )panic ("Failed to initialize the interrupt controller drivers\n" ); }
Signature · 1 line
2.54.0
PATCH v2 9/26 xen/riscv: introduce init interrupt controller operations
In reply to Oleksii KurochkoIntroduce intc_hw_init_ops structure to avoid risky mix of init function and non-init function.
---
Changes in v2:
- New patch.
--- xen/arch/riscv/aplic.c | 7 +++++-- xen/arch/riscv/include/asm/intc.h | 10 +++++++--- xen/arch/riscv/intc.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-)
xen/arch/riscv/aplic.c +5 -2
@@ -306,12 +306,15 @@ static const hw_irq_controller aplic_xen_irq_type = {static const struct intc_hw_operations aplic_ops = { .info = &aplic_info, - .init = aplic_init, .host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, }; +static const struct intc_hw_init_ops __initdata aplic_init_ops = { + .init = aplic_init, +}; + static int cf_check aplic_irq_xlate(const uint32_t *intspec, unsigned int intsize, unsigned int *out_hwirq,
@@ -347,7 +350,7 @@ static int __init aplic_preinit(struct dt_device_node *node, const void *dat) dt_irq_xlate = aplic_irq_xlate; -register_intc_ops (&aplic_ops); +register_intc_ops (&aplic_ops, &aplic_init_ops);/* Enable supervisor external interrupt */ csr_set (CSR_SIE ,BIT (IRQ_S_EXT ,UL ));
xen/arch/riscv/include/asm/intc.h +7 -3
@@ -28,8 +28,6 @@ struct intc_info {struct intc_hw_operations {/* Hold intc hw information */ const struct intc_info *info; -/* Initialize the intc and the boot CPU */ -int (*init)(void );/* hw_irq_controller to enable/disable/eoi host irq */ const struct hw_interrupt_type *host_irq_type;
@@ -43,9 +41,15 @@ struct intc_hw_operations {void (*handle_interrupt)(struct cpu_user_regs *regs); }; +struct intc_hw_init_ops { +/* Initialize the intc and the boot CPU */ +int (*init)(void ); +}; +void intc_preinit (void ); -void register_intc_ops (const struct intc_hw_operations *ops); +void register_intc_ops (const struct intc_hw_operations *ops, +const struct intc_hw_init_ops *init_ops);void intc_init (void );
xen/arch/riscv/intc.c +8 -2
@@ -12,9 +12,13 @@static const struct intc_hw_operations *__ro_after_init intc_hw_ops; -void __init register_intc_ops (const struct intc_hw_operations *ops) +static const struct intc_hw_init_ops *__initdata intc_hw_init_ops; + +void __init register_intc_ops (const struct intc_hw_operations *ops, +const struct intc_hw_init_ops *init_ops) { intc_hw_ops = ops; + intc_hw_init_ops = init_ops; }void __init intc_preinit (void )
@@ -27,7 +31,9 @@ void __init intc_preinit(void)void __init intc_init (void ) { -if ( intc_hw_ops->init () ) +ASSERT (intc_hw_init_ops && intc_hw_init_ops->init); + +if ( intc_hw_init_ops->init () )panic ("Failed to initialize the interrupt controller drivers\n" ); }
Signature · 1 line
2.54.0
PATCH v2 13/26 xen/riscv: introduce per-vCPU IMSIC state
In reply to Oleksii KurochkoEach vCPU interacting with the IMSIC requires state to track the associated guest interrupt file and its backing context.
Introduce a per-vCPU structure to hold IMSIC-related state, including the guest interrupt file identifier and the CPU providing the backing VS-file. Access to the guest file identifier is protected by a lock.
Initialize this structure during vCPU setup and store it in arch_vcpu. The initial state marks the VS-file as software-backed until it becomes associated with a physical CPU.
Add helpers to retrieve and update the guest interrupt file identifier:
- imsic_set_guest_file_id() is going to be used during vCPU creation to initialize IMSIC interrupt controller things for this vCPU.
- vcpu_guest_file_id() is going to be used during update of APLIC's target register with the pair of information <guest_file_id, cpu_id> (to have MSI delivery mode work properly) when guest is trying to access vAPLIC's target register.
It will be used in the follow up patches.
---
Changes in v2:
- Rename imsic_state to vimsic_state.
- Use 'unsigned int' for vsfile_pcpu.
- Drop initialzation of ->guest_file_id as it will be by default zero.
- Add the comment about ->guest_file_id field.
- Drop __init for vcpu_imsic_init() as it could be used during post-boot
vCPU creation.
- Update the commit message.
- Drop locks around ->guest_file_id() in vcpu_guest_file_id() and imsic_set_guest_file_id().
--- xen/arch/riscv/imsic.c | 35 +++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/domain.h | 2 ++ xen/arch/riscv/include/asm/imsic.h | 23 +++++++++++++++++++ 3 files changed, 60 insertions(+)
xen/arch/riscv/imsic.c +35 -0
@@ -16,6 +16,7 @@#include <xen/errno.h> #include <xen/init.h> #include <xen/macros.h> +#include <xen/sched.h> #include <xen/smp.h> #include <xen/spinlock.h> #include <xen/xvmalloc.h>
@@ -56,6 +57,16 @@ do { \csr_clear (CSR_SIREG , v); \ }while (0 ) +unsigned int vcpu_guest_file_id(const struct vcpu *v) +{ +return ACCESS_ONCE (v->arch.vimsic_state->guest_file_id); +} + +void imsic_set_guest_file_id (const struct vcpu *v,unsigned int guest_file_id) +{ +ACCESS_ONCE (v->arch.vimsic_state->guest_file_id) = guest_file_id; +} +void __init imsic_ids_local_delivery (bool enable) {if ( enable )
@@ -312,6 +323,30 @@ static int imsic_parse_node(const struct dt_device_node *node,return 0 ; } +int vcpu_imsic_init (struct vcpu *v) +{ +struct vimsic_state *imsic_state; + +/* Allocate IMSIC context */ + imsic_state = xvzalloc(struct vimsic_state); +if ( !imsic_state ) +return -ENOMEM ; + + v->arch.vimsic_state = imsic_state; + +/* Setup IMSIC context */ +rwlock_init (&imsic_state->vsfile_lock); + + imsic_state->vsfile_pcpu =NR_CPUS ; + +return 0 ; +} + +void vcpu_imsic_deinit (const struct vcpu *v) +{ +xvfree (v->arch.vimsic_state); +} +/* * Initialize the imsic_cfg structure based on the IMSIC DT node. *
xen/arch/riscv/include/asm/domain.h +2 -0
@@ -54,6 +54,8 @@ struct arch_vcpu {struct vtimer vtimer; +struct vimsic_state *vimsic_state; +register_t hcounteren;register_t hedeleg;register_t hideleg;
xen/arch/riscv/include/asm/imsic.h +23 -0
@@ -11,6 +11,7 @@#ifndef ASM_RISCV_IMSIC_H #define ASM_RISCV_IMSIC_H +#include <xen/rwlock.h> #include <xen/spinlock.h> #include <xen/stdbool.h> #include <xen/types.h>
@@ -61,7 +62,24 @@ struct imsic_config {spinlock_t lock; }; +struct vimsic_state { +/* IMSIC VS-file */ +rwlock_t vsfile_lock; +/* +* (guest_file_id == 0) -> s/w IMSIC SW-file +* (guest_file_id > 0) -> h/w IMSIC VS-file +*/ +unsigned int guest_file_id; +/* +* (vsfile_pcpu >= 0) => h/w IMSIC VS-file +* (vsfile_pcpu == NR_CPUS) => s/w IMSIC SW-file +*/ +unsigned int vsfile_pcpu; +}; +struct dt_device_node ; +struct vcpu ; +int imsic_init (const struct dt_device_node *node);const struct imsic_config *imsic_get_config (void );
@@ -71,4 +89,9 @@ void imsic_irq_disable(unsigned int hwirq);void imsic_ids_local_delivery (bool enable); +int vcpu_imsic_init (struct vcpu *v); +void vcpu_imsic_deinit (const struct vcpu *v); +unsigned int vcpu_guest_file_id (const struct vcpu *v); +void imsic_set_guest_file_id (const struct vcpu *v,unsigned int guest_file_id); +#endif /* ASM_RISCV_IMSIC_H */
Signature · 1 line
2.54.0
PATCH v2 12/26 xen/riscv: add basic VGEIN management for AIA guests
In reply to Oleksii KurochkoIt was decided to add support for IMSIC from the start instead of having APLIC operate in direct delivery mode, as it requires a trap-and-emulation approach, which is not optimal from a performance standpoint.
AIA provides a hardware-accelerated mechanism for delivering external interrupts to domains via "guest interrupt files" located in IMSIC. A single physical hart can implement multiple such files (up to GEILEN), allowing several virtual harts to receive interrupts directly from hardware.
Introduce per-CPU tracking of guest interrupt file identifiers (VGEIN) for systems implementing AIA specification. Each CPU maintains a bitmap describing which guest interrupt files are currently in use.
Add helpers to initialize the bitmap based on the number of available guest interrupt files (GEILEN), assign a VGEIN to a vCPU, and release it when no longer needed. When assigning a VGEIN, the corresponding value is written to the VGEIN field of the guest hstatus register so that VS-level external interrupts are delivered from the selected interrupt file.
---
Changes in v2:
- add static for defintion of vgein_bmp;
- Drop declarartion of vgein_bmp from aia.h.
- Move declaration of 'struct vgein_bmp' from aia.h to aia.c as all the management is inside aia.c.
- Instead of decrement of vgein->geilen just update the wait how it is initialized.
- Return -EOPNOTSUPP in vgein_init() instead of BUG_ON().
- Use %u to print unsigned int.
- make bmp field in vgein_bmp not a pointer.
- allocate owners dynamically.
- Drop unnessary blank lines.
- use find_first_zero_bit() instead of bitmap_weight() to find a free slot for vgein number.
- Drop the section number for the comment.
- Start to search from bitnum 1 for free vgein_id, as bitnum 0 is reserved to tell that no guest extrenal interrupt number is used. Thereby drop vgein_id++ at the end of vgein_assign().
- s/bitmap_set/__set_bit.
- s/bitmap_clear/__clear_bit.
- as vgein_init() is needed to be invoked once per CPU being brought up, drop __init for it.
- Return vgein_id == 0 if vgein_id is higher then maximun supported by h/w VGEIN.
- Add check in vgein_relase() that vgein is 0 and if it is there is nothing is needed to do.
- Use gdprintk instead of printk() in vgein_{assign,release}.
- Add the claryfing comment above geilen field.
- Drop ASSERT in vgein_assign() and return just vgein_id = 0 in the case when there is no aviablable h/w VGEINs.
- Make vgein_init() static.
--- xen/arch/riscv/aia.c | 144 +++++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/aia.h | 8 ++ 2 files changed, 152 insertions(+)
xen/arch/riscv/aia.c +144 -0
@@ -1,11 +1,33 @@/* SPDX-License-Identifier: GPL-2.0-only */ +#include <xen/bitmap.h> +#include <xen/cpu.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> +#include <xen/sched.h> +#include <xen/spinlock.h> #include <xen/types.h> +#include <xen/xvmalloc.h> +#include <asm/aia.h> #include <asm/cpufeature.h> +#include <asm/csr.h> +#include <asm/current.h> + +struct vgein_ctrl { +unsigned long bmp; +spinlock_t lock; +struct vcpu **owners; +/* The least-significant bits are implemented first, apart from bit 0 */ +unsigned int geilen; +}; + +/* +* Bitmap for each physical cpus to detect which VS (guest) +* interrupt file id was used. +*/ +static DEFINE_PER_CPU(struct vgein_ctrl, vgein);static bool __ro_after_init is_aia_usable;
@@ -14,10 +36,132 @@ bool aia_usable(void)return is_aia_usable; } +static int vgein_init (unsigned int cpu) +{ +struct vgein_ctrl *vgein = &per_cpu (vgein, cpu); + +csr_write (CSR_HGEIE ,-1UL ); + vgein->geilen =flsl (csr_read (CSR_HGEIE ) >>1 ); +csr_write (CSR_HGEIE ,0 ); + +printk ("cpu%d.geilen=%u\n" , cpu, vgein->geilen); + +if ( !vgein->geilen ) +return -EOPNOTSUPP ; + + vgein->owners = xvzalloc_array(struct vcpu *, vgein->geilen); +if ( !vgein->owners ) +return -ENOMEM ; + +spin_lock_init (&vgein->lock); + +return 0 ; +} + +static int cf_check cpu_callback(struct notifier_block *nfb, unsigned long action, +void *hcpu) +{ +unsigned int cpu = (unsigned long )hcpu; +int rc =0 ; + +switch ( action ) + { +case CPU_STARTING : + rc =vgein_init (cpu); +if ( rc ) +printk ("AIA: failed to init vgein for CPU%un" , cpu); +break ; + } + +return notifier_from_errno (rc); +} + +static struct notifier_block cpu_nfb = { + .notifier_call = cpu_callback, +}; +void __init aia_init (void ) { +int rc; +if ( !riscv_isa_extension_available (NULL , RISCV_ISA_EXT_ssaia) ) + { +dprintk (XENLOG_WARNING ,"SSAIA isn't present in riscv,isa\n" ); +return ; + } + +if ( (rc =vgein_init (0 )) ) + { +dprintk (XENLOG_ERR ,"vgein_init() failed with rc(%d)\n" , rc);return ; + } is_aia_usable = true; + +register_cpu_notifier (&cpu_nfb); +} + +unsigned int vgein_assign (struct vcpu *v) +{ +unsigned int vgein_id; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); +unsigned long *bmp = &vgein->bmp; +unsigned long flags; + +spin_lock_irqsave (&vgein->lock, flags); +/* +* The vgein_id shouldn't be zero, as it will indicate that no guest +* external interrupt source is selected for VS-level external interrupts +* according to RISC-V priviliged spec: +* Hypervisor Status Register (hstatus) in RISC-V priviliged spec: +* +* The VGEIN (Virtual Guest External Interrupt Number) field selects +* a guest external interrupt source for VS-level external interrupts. +* VGEIN is a WLRL field that must be able to hold values between zero +* and the maximum guest external interrupt number (known as GEILEN), +* inclusive. +* When VGEIN=0, no guest external interrupt source is selected for +* VS-level external interrupts. +* +* So start to search from bit number 1. +*/ + vgein_id =find_next_zero_bit (bmp, vgein->geilen +1 ,1 ); + +if ( vgein_id > vgein->geilen ) + vgein_id =0 ; +else +__set_bit (vgein_id, bmp); + +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: %pv: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, v, vgein_id, v->processor, *bmp); +#endif + +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ; +vcpu_guest_cpu_user_regs (v)->hstatus |= +MASK_INSR (vgein_id,HSTATUS_VGEIN ); + +return vgein_id; +} + +void vgein_release (struct vcpu *v,unsigned int vgen_id) +{ +unsigned long flags; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); + +if ( !vgen_id ) +return ; + +spin_lock_irqsave (&vgein->lock, flags); +__clear_bit (vgen_id, &vgein->bmp); +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, vgen_id, v->processor, vgein->bmp); +#endif + +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ; }
xen/arch/riscv/include/asm/aia.h +8 -0
@@ -3,8 +3,16 @@#ifndef ASM__RISCV__AIA_H #define ASM__RISCV__AIA_H +#include <xen/percpu.h> +#include <xen/spinlock.h> + +struct vcpu ; +bool aia_usable (void );void aia_init (void ); +unsigned int vgein_assign (struct vcpu *v); +void vgein_release (struct vcpu *v,unsigned int vgen_id); +#endif /* ASM__RISCV__ACPI_H */
Signature · 1 line
2.54.0
PATCH v2 15/26 xen/riscv: introduce (de)initialization helpers for vINTC
In reply to Oleksii KurochkoAdd common helpers domain_vintc_init() and domain_vintc_deinit() to allocate and deallocate a virtual interrupt controller (vINTC) structure and initialize basic virtual interrupt controller registers.
domain_vintc_deinit() isn't called at the momemnt as arch_domain_destroy() is implemented as stub at the moment.
---
Changes in v2:
- Drop __init for domain_vintc_(de)init().
- Update the commit message.
--- xen/arch/riscv/domain.c | 3 +++ xen/arch/riscv/include/asm/intc.h | 3 +++ xen/arch/riscv/intc.c | 36 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+)
xen/arch/riscv/domain.c +3 -0
@@ -306,6 +306,9 @@ int arch_domain_create(struct domain *d,if ( (rc =p2m_init (d, config)) !=0 ) goto fail; +if ( (rc =domain_vintc_init (d)) ) + goto fail; +return rc; fail:
xen/arch/riscv/include/asm/intc.h +3 -0
@@ -80,4 +80,7 @@ void intc_handle_external_irqs(struct cpu_user_regs *regs);unsigned int intc_irq_nums (void ); +int domain_vintc_init (struct domain *d); +void domain_vintc_deinit (struct domain *d); +#endif /* ASM__RISCV__INTERRUPT_CONTOLLER_H */
xen/arch/riscv/intc.c +36 -0
@@ -11,6 +11,7 @@#include <asm/aia.h> #include <asm/intc.h> +#include <asm/vaplic.h> static const struct intc_hw_operations *__ro_after_init intc_hw_ops;
@@ -94,3 +95,38 @@ int __init make_intc_domU_node(struct kernel_info *kinfo)return -EOPNOTSUPP ; } + +int domain_vintc_init (struct domain *d) +{ +int ret = -EOPNOTSUPP ; +const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC : + ret =domain_vaplic_init (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver); +break ; + } + +return ret; +} + +void domain_vintc_deinit (struct domain *d) +{ +const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC : +domain_vaplic_deinit (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver); +break ; + } +}
Signature · 1 line
2.54.0
PATCH v2 14/26 xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
In reply to Oleksii KurochkoAt the current development stage, only domain vINTC init and deinit operations are required, so implement those first.
Initialize vAPLIC's domaincfg to with the interrupt-enable bit set and MSI delivery mode selected as the current solution is exepcted to have always IMSIC, and initialize vintc->ops.
Other operations such as emulate_load(), emulate_store(), and is_access() will be needed once guests are running and MMIO accesses to APLIC MMIO range must be handled. These will be introduced separately later.
Introduce a structure to describe a virtual interrupt controller (vINTC) and a vintc_ops structure, which provides operations to emulate load and store accesses to interrupt controller MMIOs and to check whether a given address falls within the MMIO range of a specific virtual interrupt controller.
The vAPLIC implementation of these operations will be provided later once guests can be run and these operations are actually needed.
Introduce these structures here as they are required for the implementation of domain_vaplic_init() and domain_vaplic_alloc(). Also, introduce vcpu_vaplic_init() and init vintc_ops->vcpu_init() with it.
---
Changes in v2:
- s/vcpu/v for function arguments in struct vintc_ops().
- Update the comment above is_access() and drop const for addr argument.
- Update to_vaplic() to work with 'struct domain *'.
- Drop smsiaddrcfg{h} from vaplic_regs struct as they aren't used for now.
- Drop inclusion of xen/schec.h from intc.c.
- use result of xvzalloc() as initializer in vpalic_alloc().
- Drop goto in domain_vaplic_init().
- s/XVFREE/xvfree.
- s/aplic/vintc.
- Drop __init for vcpu_vaplic_init() as it could be called for secondary CPU bring up.
- Drop vaplic_alloc().
- Drop vintc_ops struct, embed callbacks iniside struct vintc.
- Introduce and init vintc irqs for vAPLIC.
- Introduce intc_irq_nums() to properly initialize number of vAPLIC's irqs.
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/aplic.c | 6 +++ xen/arch/riscv/domain.c | 13 ++--- xen/arch/riscv/include/asm/intc.h | 17 ++++++- xen/arch/riscv/include/asm/vaplic.h | 34 +++++++++++++ xen/arch/riscv/intc.c | 7 +++ xen/arch/riscv/vaplic.c | 78 +++++++++++++++++++++++++++++ 7 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 xen/arch/riscv/include/asm/vaplic.h create mode 100644 xen/arch/riscv/vaplic.c
xen/arch/riscv/Makefile +1 -0
@@ -25,6 +25,7 @@ obj-y += smpboot.o obj-y += stubs.o obj-y += time.o obj-y += traps.o +obj-y += vaplic.o obj-y += vmid.o obj-y += vm_event.o obj-y += vsbi/
xen/arch/riscv/aplic.c +6 -0
@@ -295,6 +295,11 @@ static void cf_check aplic_set_irq_type(struct irq_desc *desc,spin_unlock (&aplic.lock); } +static unsigned int cf_check aplic_irq_num(void) +{ +return aplic_info.num_irqs; +} +static const hw_irq_controller aplic_xen_irq_type = { .typename ="aplic" , .startup = aplic_irq_startup,
@@ -309,6 +314,7 @@ static const struct intc_hw_operations aplic_ops = { .host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, + .irq_nums = aplic_irq_num, };static const struct intc_hw_init_ops __initdata aplic_init_ops = {
xen/arch/riscv/domain.c +5 -8
@@ -11,6 +11,7 @@#include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> +#include <asm/intc.h> #include <asm/riscv_encoding.h> #include <asm/vtimer.h>
@@ -155,14 +156,10 @@ int arch_vcpu_create(struct vcpu *v)if ( (rc =vcpu_vtimer_init (v)) ) goto fail; -/* -* As interrupt controller (IC) is not yet implemented, -* return an error. -* -* TODO: Drop this once IC is implemented. -*/ - rc = -EOPNOTSUPP ; - goto fail; +ASSERT (v->domain->arch.vintc->ops->vcpu_init); + +if ( (rc = v->domain->arch.vintc->ops->vcpu_init (v)) ) + goto fail;return rc;
xen/arch/riscv/include/asm/intc.h +16 -1
@@ -15,6 +15,7 @@ enum intc_version {struct cpu_user_regs ;struct irq_desc ;struct kernel_info ; +struct vcpu ;struct intc_info {enum intc_version hw_version;
@@ -38,6 +39,8 @@ struct intc_hw_operations {/* handle external interrupt */ void (*handle_interrupt)(struct cpu_user_regs *regs); + +unsigned int (*irq_nums)(void ); };struct intc_hw_init_ops {
@@ -50,8 +53,18 @@ struct vintc_init_ops {int (*make_domu_dt_node)(struct kernel_info *kinfo); }; +struct vintc_ops { +/* Initialize some vINTC-related stuff for a vCPU */ +int (*vcpu_init)(struct vcpu *v); + +/* Check if a address is virtual interrupt controller MMIO */ +int (*is_access)(const struct vcpu *v,unsigned long addr); +}; +struct vintc { -struct vintc_init_ops *init_ops; +unsigned int irq_nums; +const struct vintc_init_ops *init_ops; +const struct vintc_ops *ops; };void intc_preinit (void );
@@ -65,4 +78,6 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority);void intc_handle_external_irqs (struct cpu_user_regs *regs); +unsigned int intc_irq_nums (void ); +#endif /* ASM__RISCV__INTERRUPT_CONTOLLER_H */
xen/arch/riscv/include/asm/vaplic.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: MIT */ +/* +* xen/arch/riscv/vaplic.c +* +* Virtual RISC-V Advanced Platform-Level Interrupt Controller support +* +* Copyright (c) Microchip. +*/ + +#ifndef ASM__RISCV__VAPLIC_H +#define ASM__RISCV__VAPLIC_H + +#include <xen/kernel.h> +#include <xen/types.h> + +#include <asm/intc.h> + +struct domain ; + +#define to_vaplic (d) container_of(d->arch.vintc, struct vaplic, vintc) + +struct vaplic_regs { +uint32_t domaincfg; +}; + +struct vaplic { +struct vintc vintc; +struct vaplic_regs regs; +}; + +int domain_vaplic_init (struct domain *d); +void domain_vaplic_deinit (struct domain *d); + +#endif /* ASM__RISCV__VAPLIC_H */
xen/arch/riscv/intc.c +7 -0
@@ -78,6 +78,13 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)intc_set_irq_priority (desc, priority); } +unsigned int intc_irq_nums (void ) +{ +ASSERT (intc_hw_ops && intc_hw_ops->irq_nums); + +return intc_hw_ops->irq_nums (); +} +int __init make_intc_domU_node (struct kernel_info *kinfo) {struct vintc *vintc = kinfo->bd.d->arch.vintc;
xen/arch/riscv/vaplic.c new file +78 -0
78 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v2 17/26 xen/riscv: generate IMSIC DT node for guest domains
In reply to Oleksii KurochkoGuests using the IMSIC interrupt controller require a corresponding Device Tree description.
Add support for generating an IMSIC node when building the guest DT. This allows guests to discover and use the IMSIC interrupt controller.
---
Changes in v2:
- s/imsic_make_reg_property/guest_imsic_make_reg_property.
- s/imsic_set_interrupt_extended_prop/guest_imsic_set_interrupt_extended_prop.
- Use initalizer for regs[] array in imsic_make_reg_property().
- Move buf[] insde the for() loop.
- Correct check of returned phandle.
- Drop local variable len.
- /s/XVFREE/xvfree in imsic_set_interrupt_extended_prop().
- Drop initializer for local variable data.
- s/uint32_t/unsinged int for pos and cpu in imsic_set_interrupt_extended_prop().
- Drop next_phandle as it is now in common code.
- Introduce vcpu_imsic_deinit.
- Refactor vimsic_make_domu_dt_node() to avoid usage of host IMSIC dt node.
--- xen/arch/riscv/imsic.c | 127 +++++++++++++++++++++- xen/arch/riscv/include/asm/guest-layout.h | 2 + 2 files changed, 128 insertions(+), 1 deletion(-)
xen/arch/riscv/imsic.c +126 -1
@@ -13,9 +13,12 @@#include <xen/const.h> #include <xen/cpumask.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/errno.h> +#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/macros.h> #include <xen/sched.h> #include <xen/smp.h>
@@ -35,6 +38,11 @@ static struct imsic_config imsic_cfg = { .lock = SPIN_LOCK_UNLOCKED, }; +static unsigned int __ro_after_init guest_num_msis; + +#define GUEST_IMSIC_COMPATIBLE "riscv,imsics" +#define GUEST_IMSIC_NUM_MSIS 255 +#define IMSIC_DISABLE_EIDELIVERY 0#define IMSIC_ENABLE_EIDELIVERY 1#define IMSIC_DISABLE_EITHRESHOLD 1
@@ -291,6 +299,11 @@ static int imsic_parse_node(const struct dt_device_node *node,return -ENOENT ; } +if (dt_property_read_u32 (node,"riscv,num-guest-ids" , &tmp) ) + guest_num_msis = tmp; +else + guest_num_msis = imsic_cfg.nr_ids; +if ( (imsic_cfg.nr_ids <IMSIC_MIN_ID ) || (imsic_cfg.nr_ids >IMSIC_MAX_ID ) ) {
@@ -524,8 +537,120 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } +static int __init guest_imsic_make_reg_property (struct domain *d,void *fdt) +{ +paddr_t base_addr =GUEST_IMSIC_S_BASE ; +__be32 regs[4 ] = { +cpu_to_be32 (base_addr >>32 ), +cpu_to_be32 (base_addr), +cpu_to_be32 ((IMSIC_MMIO_PAGE_SZ * d->max_vcpus) >>32 ), +cpu_to_be32 (IMSIC_MMIO_PAGE_SZ * d->max_vcpus), + }; + +return fdt_property (fdt,"reg" , regs,sizeof (regs)); +} + +static int __init guest_imsic_set_interrupt_extended_prop (struct domain *d, +void *fdt) +{ +unsigned int cpu, pos =0 ; +uint32_t phandle; +uint32_t *irq_ext; +int res; + + irq_ext =xvzalloc_array (uint32_t, d->max_vcpus *2 ); +if ( !irq_ext ) +return -ENOMEM ; + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ ) + { +char buf[64 ]; + +snprintf (buf,sizeof (buf),"/cpus/cpu@%u/interrupt-controller" , cpu); + phandle =fdt_get_phandle (fdt,fdt_path_offset (fdt, buf)); + +if ( !phandle ) + { + res = -ENODEV ; + goto out; + } + + irq_ext[pos++] =cpu_to_be32 (phandle); + irq_ext[pos++] =cpu_to_be32 (IRQ_S_EXT ); + } + + res =fdt_property (fdt,"interrupts-extended" , irq_ext, + d->max_vcpus *2 *sizeof (*irq_ext)); + + out: +xvfree (irq_ext); + +return res; +} +int __init vimsic_make_domu_dt_node (struct kernel_info *kinfo,unsigned int *phandle) { -return -EOPNOTSUPP ; +int res; +void *fdt = kinfo->fdt; +char vimsic_name[128 ]; +unsigned int vimsic_phandle; +unsigned int num_msis =min (GUEST_IMSIC_NUM_MSIS +0U , guest_num_msis); + + res =snprintf (vimsic_name,sizeof (vimsic_name),"/soc/imsic@%x" , +GUEST_IMSIC_S_BASE ); +if ( res >=sizeof (vimsic_name) ) + { +dprintk (XENLOG_DEBUG ,"vimsic name is truncated\n" ); +return -ENOBUFS ; + } + + res =fdt_begin_node (fdt, vimsic_name); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,GUEST_IMSIC_COMPATIBLE ); +if ( res ) +return res; + + res =guest_imsic_make_reg_property (kinfo->bd.d, fdt); +if ( res ) +return res; + + res =guest_imsic_set_interrupt_extended_prop (kinfo->bd.d, fdt); +if ( res ) +return res; + + res =fdt_property_u32 (fdt,"riscv,num-ids" , num_msis); +if ( res ) +return res; + + res =fdt_property (fdt,"msi-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_u32 (fdt,"#msi-cells" ,0 ); +if ( res ) +return res; + + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_u32 (fdt,"#interrupt-cells" ,0 ); +if ( res ) +return res; + + vimsic_phandle =alloc_phandle (kinfo); +if ( !vimsic_phandle ) +return -EOVERFLOW ; + + res =fdt_property_cell (fdt,"phandle" , vimsic_phandle); +if ( res ) +return res; + +if ( phandle ) + *phandle = vimsic_phandle; + +return fdt_end_node (fdt); }
xen/arch/riscv/include/asm/guest-layout.h +2 -0
@@ -5,6 +5,8 @@#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_IMSIC_S_BASE 0x28000000 +#define GUEST_RAM_BANKS 2/*
Signature · 1 line
2.54.0
PATCH v2 16/26 xen/riscv: create APLIC DT node for guest domains
In reply to Oleksii KurochkoGuests require a Device Tree description of the interrupt controller topology. Add support for creating an APLIC node when building the guest DT.
Provide stub for imsic_make_dt_node() it will be introduced properly in follow-up patch.
---
Changes in v2:
- Avoid as max as possible of host properties inheritance. Only number of APLIC's irqs are checked what leads to an introduction of get_aplic_irqs_num().
- Move this patch earlier what leads to an introduction of vimsic_make_domu_dt_node() stub.
- s/vimsic_make_domu_dt_node/imsic_make_domu_dt_node.
- Refactor vimsic_make_domu_dt_node() to avoid re-usage of APLIC host properties.
- Drop next_phandle as it is now in common code.
- Drop const for kinfo argument of vimsic_make_domu_dt_node() is is going to be updated inside vimsic_make_domu_dt_node().
- Use introduced before vintc->num_irqs.
--- xen/arch/riscv/aplic.c | 2 + xen/arch/riscv/imsic.c | 7 +++ xen/arch/riscv/include/asm/aplic.h | 9 +++ xen/arch/riscv/include/asm/guest-layout.h | 2 + xen/arch/riscv/include/asm/imsic.h | 3 + xen/arch/riscv/vaplic.c | 77 ++++++++++++++++++++++- 6 files changed, 98 insertions(+), 2 deletions(-)
xen/arch/riscv/aplic.c +2 -0
@@ -12,8 +12,10 @@#include <xen/const.h> #include <xen/device_tree.h> #include <xen/errno.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <xen/irq.h> +#include <xen/libfdt/libfdt.h> #include <xen/mm.h> #include <xen/sections.h> #include <xen/spinlock.h>
xen/arch/riscv/imsic.c +7 -0
@@ -14,6 +14,7 @@#include <xen/cpumask.h> #include <xen/device_tree.h> #include <xen/errno.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <xen/macros.h> #include <xen/sched.h>
@@ -522,3 +523,9 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } + +int __init vimsic_make_domu_dt_node (struct kernel_info *kinfo, +unsigned int *phandle) +{ +return -EOPNOTSUPP ; +}
xen/arch/riscv/include/asm/aplic.h +9 -0
@@ -15,6 +15,7 @@#include <asm/imsic.h> +#define APLIC_DOMAINCFG_RO80 (0x80U << 24)#define APLIC_DOMAINCFG_IE BIT(8, U)#define APLIC_DOMAINCFG_DM BIT(2, U)
@@ -27,6 +28,14 @@#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_IDC_SIZE 32 + +#define APLIC_MIN_SIZE 0x4000 +#define APLIC_SIZE_ALIGN (x) ROUNDUP(x, APLIC_MIN_SIZE) + +#define APLIC_SIZE (nr_cpus) (APLIC_MIN_SIZE + \ + APLIC_SIZE_ALIGN(APLIC_IDC_SIZE * (nr_cpus))) +struct aplic_regs {uint32_t domaincfg;/* 0x0000 */ uint32_t sourcecfg[1023 ];/* 0x0004 */
xen/arch/riscv/include/asm/guest-layout.h +2 -0
@@ -3,6 +3,8 @@#include <public/xen.h> +#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_RAM_BANKS 2/*
xen/arch/riscv/include/asm/imsic.h +3 -0
@@ -78,6 +78,7 @@ struct vimsic_state { };struct dt_device_node ; +struct kernel_info ;struct vcpu ;int imsic_init (const struct dt_device_node *node);
@@ -94,4 +95,6 @@ void vcpu_imsic_deinit(const struct vcpu *v);unsigned int vcpu_guest_file_id (const struct vcpu *v);void imsic_set_guest_file_id (const struct vcpu *v,unsigned int guest_file_id); +int vimsic_make_domu_dt_node (struct kernel_info *kinfo,unsigned int *phandle); +#endif /* ASM_RISCV_IMSIC_H */
xen/arch/riscv/vaplic.c +75 -2
108 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v2 18/26 xen: move declaration of map_device_irqs_to_domain() to common header
In reply to Oleksii KurochkoAs map_device_irqs_to_domain() is used unconditionally by common part of dom0less code it is moved to common header.
fdt-domain-build.h is chosen as map_device_irqs_to_domain() could be also called indirectly in Arm's DOM0-related code.
---
Changes in v2:
- New patch.
--- xen/arch/arm/include/asm/setup.h | 3 --- xen/include/xen/fdt-domain-build.h | 13 +++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -3
@@ -53,9 +53,6 @@ void init_traps(void);int handle_device (struct domain *d,struct dt_device_node *dev,p2m_type_t p2mt,struct rangeset *iomem_ranges,struct rangeset *irq_ranges); -int map_device_irqs_to_domain (struct domain *d,struct dt_device_node *dev, -bool need_mapping,struct rangeset *irq_ranges); -int map_irq_to_domain (struct domain *d,unsigned int irq,bool need_mapping,const char *devname);
xen/include/xen/fdt-domain-build.h +13 -0
@@ -12,6 +12,7 @@struct domain ;struct page_info ; +struct rangeset ;struct membanks ; typedef bool (*alloc_domheap_mem_cb)(struct domain *d, struct page_info *pg,
@@ -79,6 +80,18 @@ static inline void set_domain_type(struct domain *d, const struct kernel_info *k #endif } +/* +* Retrieves the interrupts configuration from a device tree node and maps +* those interrupts to the target domain. +* +* Returns: +* < 0 error +* 0 success +*/ +int map_device_irqs_to_domain (struct domain *d,struct dt_device_node *dev, +bool need_mapping, +struct rangeset *irq_ranges); +#endif /* __XEN_FDT_DOMAIN_BUILD_H__ */ /*
Signature · 1 line
2.54.0
PATCH v2 19/26 xen/riscv: implement IRQ routing for device passthrough
In reply to Oleksii Kurochkodom0less device passthrough requires granting guest domains access to device interrupts. Introduce map_device_irqs_to_domain() to enumerate a DT node's interrupt properties, skipping those not owned by the primary interrupt controller (as at the moment I haven't seen usages of it), and map_irq_to_domain() to grant domain access and configure Xen's interrupt descriptor accordingly. Sharing IRQ between domains is rejected.
route_irq_to_guest() and release_irq() manage irq_desc ownership for guest-assigned interrupts. Each assignment carries a small irq_guest structure as irqaction::dev_id, recording the owning domain and virtual IRQ number which is 1:1 mapped to physical IRQ number. A per-domain vIRQ allocation bitmap (allocated_irqs in struct vintc), managed by vintc_reserve_virq(), prevents the same vIRQ being claimed twice.
With APLIC+IMSIC, guest interrupts are delivered directly by hardware through the IMSIC, bypassing do_IRQ(). The _IRQ_GUEST branch in do_IRQ() is therefore left as BUG() until a platform without direct IMSIC delivery is encountered.
---
Changes in v2:
- Rework IRQ mapping in more common (similar approach to Arm).
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/aplic.c | 4 + xen/arch/riscv/device.c | 108 ++++++++++++++++++ xen/arch/riscv/include/asm/intc.h | 10 ++ xen/arch/riscv/include/asm/irq.h | 3 + xen/arch/riscv/include/asm/setup.h | 4 + xen/arch/riscv/intc.c | 36 ++++++ xen/arch/riscv/irq.c | 175 +++++++++++++++++++++++++++++ 8 files changed, 341 insertions(+) create mode 100644 xen/arch/riscv/device.c
xen/arch/riscv/Makefile +1 -0
@@ -1,6 +1,7 @@ obj-y += aia.o obj-y += aplic.o obj-y += cpufeature.o +obj-y += device.o obj-y += domain.o obj-y += domain-build.init.o obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-build.init.o
xen/arch/riscv/aplic.c +4 -0
@@ -311,9 +311,13 @@ static const hw_irq_controller aplic_xen_irq_type = { .set_affinity = aplic_set_irq_affinity, }; +/* At the moment there is no difference between guest and Xen ops */ +#define aplic_guest_irq_type aplic_xen_irq_type +static const struct intc_hw_operations aplic_ops = { .info = &aplic_info, .host_irq_type = &aplic_xen_irq_type, + .guest_irq_type = &aplic_guest_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, .irq_nums = aplic_irq_num,
xen/arch/riscv/device.c new file +108 -0
108 lines not on this page · raw message
xen/arch/riscv/include/asm/intc.h +10 -0
@@ -13,8 +13,11 @@ enum intc_version { };struct cpu_user_regs ; +struct domain ; +struct dt_device_node ;struct irq_desc ;struct kernel_info ; +struct rangeset ;struct vcpu ;struct intc_info {
@@ -32,6 +35,9 @@ struct intc_hw_operations {/* hw_irq_controller to enable/disable/eoi host irq */ const struct hw_interrupt_type *host_irq_type; +/* hw_irq_controller to enable/disable/eoi guest irq */ +const struct hw_interrupt_type *guest_irq_type; +/* Set IRQ type */ void (*set_irq_type)(struct irq_desc *desc,unsigned int type);/* Set IRQ priority */
@@ -63,6 +69,7 @@ struct vintc_ops {struct vintc {unsigned int irq_nums; +unsigned long *allocated_irqs;const struct vintc_init_ops *init_ops;const struct vintc_ops *ops; };
@@ -75,6 +82,7 @@ void register_intc_ops(const struct intc_hw_operations *ops,void intc_init (void );void intc_route_irq_to_xen (struct irq_desc *desc,unsigned int priority); +int intc_route_irq_to_guest (struct irq_desc *desc,unsigned int priority);void intc_handle_external_irqs (struct cpu_user_regs *regs);
@@ -83,4 +91,6 @@ unsigned int intc_irq_nums(void);int domain_vintc_init (struct domain *d);void domain_vintc_deinit (struct domain *d); +bool vintc_reserve_virq (const struct domain *d,unsigned int virq); +#endif /* ASM__RISCV__INTERRUPT_CONTOLLER_H */
xen/arch/riscv/include/asm/irq.h +3 -0
@@ -49,6 +49,9 @@ void init_IRQ(void);void do_IRQ (struct cpu_user_regs *regs,unsigned int irq); +int route_irq_to_guest (struct domain *d,unsigned int virq, +unsigned int irq,const char *devname); +#endif /* ASM__RISCV__IRQ_H */ /*
xen/arch/riscv/include/asm/setup.h +4 -0
@@ -5,6 +5,10 @@#include <xen/types.h> +struct domain ; +struct dt_device_node ; +struct rangeset ; +#define max_init_domid (0)void setup_mm (void );
xen/arch/riscv/intc.c +36 -0
@@ -7,7 +7,9 @@#include <xen/init.h> #include <xen/irq.h> #include <xen/lib.h> +#include <xen/sched.h> #include <xen/spinlock.h> +#include <xen/xvmalloc.h> #include <asm/aia.h> #include <asm/intc.h>
@@ -86,6 +88,22 @@ unsigned int intc_irq_nums(void)return intc_hw_ops->irq_nums (); } +int intc_route_irq_to_guest (struct irq_desc *desc, +unsigned int priority) +{ +ASSERT (spin_is_locked (&desc->lock)); + +ASSERT (intc_hw_ops->guest_irq_type); + + desc->handler = intc_hw_ops->guest_irq_type; +set_bit (_IRQ_GUEST, &desc->status); + +intc_set_irq_type (desc, desc->arch.type); +intc_set_irq_priority (desc, priority); + +return 0 ; +} +int __init make_intc_domU_node (struct kernel_info *kinfo) {struct vintc *vintc = kinfo->bd.d->arch.vintc;
@@ -112,6 +130,14 @@ int domain_vintc_init(struct domain *d)break ; } +if ( !ret ) + { + d->arch.vintc->allocated_irqs = + xvzalloc_array(unsigned long, BITS_TO_LONGS(d->arch.vintc->irq_nums)); +if ( !d->arch.vintc->allocated_irqs ) + ret = -ENOMEM ; + } +return ret; }
@@ -129,4 +155,14 @@ void domain_vintc_deinit(struct domain *d)printk ("vintc (ver:%d) isn't implemented\n" , ver);break ; } + +xvfree (d->arch.vintc->allocated_irqs); +} + +bool vintc_reserve_virq (const struct domain *d,unsigned int virq) +{ +if ( virq >= d->arch.vintc->irq_nums ) +return false; + +return !test_and_set_bit (virq, d->arch.vintc->allocated_irqs); }
xen/arch/riscv/irq.c +175 -0
195 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v2 20/26 xen/riscv: add missing APLIC register offsets, masks to asm/aplic.h.
In reply to Oleksii KurochkoThese definitions are required for correct decoding of APLIC MMIO accesses and target configuration, and will be used by both the physical and virtual APLIC implementations.
No functional change is intended by this patch; it only centralises hardware definitions that were previously missing.
--- xen/arch/riscv/include/asm/aplic.h | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
xen/arch/riscv/include/asm/aplic.h +35 -0
@@ -15,6 +15,11 @@#include <asm/imsic.h> +#define APLIC_REG_OFFSET_MASK 0x3fff +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_EIID_MASK 0x7ff +#define APLIC_DOMAINCFG_RO80 (0x80U << 24)#define APLIC_DOMAINCFG_IE BIT(8, U)#define APLIC_DOMAINCFG_DM BIT(2, U)
@@ -26,6 +31,36 @@#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_LAST 0x0ffc + +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIP_LAST 0x1c7c +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIP_LAST 0x1d7c +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIE_LAST 0x1e7c +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIE_LAST 0x1f7c +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_LAST 0x3ffc +#define APLIC_TARGET_HART_IDX_SHIFT 18#define APLIC_IDC_SIZE 32
Signature · 1 line
2.54.0
PATCH v2 21/26 xen/riscv: implement virtual APLIC MMIO emulation
In reply to Oleksii KurochkoGuests running under Xen program interrupt routing by writing to APLIC MMIO registers. Xen must intercept these accesses to enforce interrupt isolation between domains and to translate guest routing intent into the underlying physical MSI topology.
Writes are gated by the domain's authorised interrupt bitmap so that a guest cannot affect interrupts it does not own. TARGET register writes additionally require translation of the hart and IMSIC guest-file indices from virtual to physical, as the APLIC uses these fields directly to compute the MSI delivery address.
Delegation (APLIC_SOURCECFG_D) is not yet supported.
---
Changes in v2:
- Merge the following patches into one:
xen/riscv: add vaplic access check:
- Add check that address is properly aligned.
- Check vaplic range intead of APLIC one.
- Return bool from vaplic_is_access instead of int.
xen/riscv: emulate guest writes to virtual APLIC MMIO
- Drop CALC_REG_VALUE.
- Use unsigned int instead of uin32_t for offset.
- s/.../subtracting in the comment.
- start one line comments from the upper case.
- Check the value before being written to sourcecfg register.
- 'unsigned int' for loop index.
- Omit unneessary braces.
- s/vaplic_update_target/aplic_msi_target_gen.
- Use IMSIC_MMIO_PAGE_SHIFT instead of 12 in aplic_msi_target_gen().
- Drop explicit usage of APLIC register in store function.
- Drop APLIC_REG_{GET,SET} macros and introudce APLIC specific funtcions.
- Ignore write to SOURCECFG_BASE when value is out-of-range.
- Drop ASSERT(!target_vcpu) inside handler of targer register setting, just avoid such writings + debug message.
- domain_crash() instead of panic() in the case of default case.
- Drop ASSERT() in APLIC_SOURCE_CFG_BASE case and use domain_crash() instead.
xen/riscv: emulate guest reads from virtual APLIC MMIO:
- s/regval_to_irqn/regindx_to_irqn.
- pass to to_vaplic() a domain instead of vintc.
- add check that load access is aligned.
- instead of panic() just crash a domain().
- use 'unsigned int' for local variable offset.
- Return 0 in the case APLIC_CLRIE_BASE ...APLIC_CLRIE_LAST reading to follow AIA spec.
- Drop explicit usage of physical APLIC registers.
--- xen/arch/riscv/aplic.c | 25 +++ xen/arch/riscv/include/asm/aplic.h | 9 + xen/arch/riscv/include/asm/intc.h | 10 +- xen/arch/riscv/include/asm/vaplic.h | 3 + xen/arch/riscv/vaplic.c | 289 +++++++++++++++++++++++++++- 5 files changed, 333 insertions(+), 3 deletions(-)
xen/arch/riscv/aplic.c +25 -0
@@ -40,6 +40,31 @@ static struct intc_info __ro_after_init aplic_info = { .hw_version = INTC_APLIC, }; +uint32_t aplic_hw_read_reg (unsigned int offset,uint32_t mask) +{ +unsigned long flags; +uint32_t val; + +ASSERT (offset < aplic.size); + +spin_lock_irqsave (&aplic.lock, flags); + val =readl ((void __iomem *)((uintptr_t )aplic.regs + offset)) & mask; +spin_unlock_irqrestore (&aplic.lock, flags); + +return val; +} + +void aplic_hw_write_reg (unsigned int offset,uint32_t value) +{ +unsigned long flags; + +ASSERT (offset < aplic.size); + +spin_lock_irqsave (&aplic.lock, flags); +writel (value, (void __iomem *)((uintptr_t )aplic.regs + offset)); +spin_unlock_irqrestore (&aplic.lock, flags); +} +static void __init aplic_init_hw_interrupts (void ) {unsigned int i;
xen/arch/riscv/include/asm/aplic.h +9 -0
@@ -15,6 +15,8 @@#include <asm/imsic.h> +#define APLIC_NUM_REGS 32 +#define APLIC_REG_OFFSET_MASK 0x3fff#define APLIC_TARGET_IPRIO_MASK 0xff#define APLIC_TARGET_GUEST_IDX_SHIFT 12
@@ -24,6 +26,8 @@#define APLIC_DOMAINCFG_IE BIT(8, U)#define APLIC_DOMAINCFG_DM BIT(2, U) +#define APLIC_SOURCECFG_D BIT(10, U) +#define APLIC_SOURCECFG_SM_INACTIVE 0x0#define APLIC_SOURCECFG_SM_DETACH 0x1#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4
@@ -71,6 +75,8 @@#define APLIC_SIZE (nr_cpus) (APLIC_MIN_SIZE + \ APLIC_SIZE_ALIGN(APLIC_IDC_SIZE * (nr_cpus))) +#define APLIC_SETCLR_OFFSET_MASK ((32 * sizeof(uint32_t)) - 1) +struct aplic_regs {uint32_t domaincfg;/* 0x0000 */ uint32_t sourcecfg[1023 ];/* 0x0004 */
@@ -114,4 +120,7 @@ struct aplic_regs { uint32_t target[1023]; /* 0x3008 */ }; +uint32_t aplic_hw_read_reg (unsigned int offset,uint32_t mask); +void aplic_hw_write_reg (unsigned int offset,uint32_t value); +#endif /* ASM_RISCV_APLIC_H */
xen/arch/riscv/include/asm/intc.h +9 -1
@@ -64,7 +64,15 @@ struct vintc_ops {int (*vcpu_init)(struct vcpu *v);/* Check if a address is virtual interrupt controller MMIO */ -int (*is_access)(const struct vcpu *v,unsigned long addr); +bool (*is_access)(const struct vcpu *v,unsigned long addr); + +/* Emulate load to virtual interrupt controller MMIOs */ +int (*emulate_load)(const struct vcpu *vcpu,unsigned long addr, +uint32_t *out); + +/* Emulate store to virtual interrupt controller MMIOs */ +int (*emulate_store)(const struct vcpu *vcpu,unsigned long addr, +uint32_t in); };struct vintc {
xen/arch/riscv/include/asm/vaplic.h +3 -0
@@ -26,6 +26,9 @@ struct vaplic_regs {struct vaplic {struct vintc vintc;struct vaplic_regs regs; + +paddr_t regs_start; +paddr_t regs_size; };int domain_vaplic_init (struct domain *d);
xen/arch/riscv/vaplic.c +287 -2
318 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v2 22/26 xen/riscv: implement init_intc_phandle()
In reply to Oleksii KurochkoImplement init_intc_phandle() to read phandle of interrupt controller node and save it in kernel->phandle_intc for the future usage during creation of guest interrupt controller node.
--- xen/arch/riscv/dom0less-build.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
xen/arch/riscv/dom0less-build.c +17 -0
@@ -4,9 +4,26 @@#include <xen/device_tree.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <asm/p2m.h> +int __init init_intc_phandle (struct kernel_info *kinfo,const char *name, +const int node_next,const void *pfdt) +{ +if (dt_node_cmp (name,"intc" ) ==0 ) + { +uint32_t phandle_intc =fdt_get_phandle (pfdt, node_next); + +if ( phandle_intc !=0 ) + kinfo->phandle_intc = phandle_intc; + +return 0 ; + } + +return 1 ; +} +int __init make_arch_nodes (struct kernel_info *kinfo) {/* No RISC-V specific nodes need to be made, at the moment. */
Signature · 1 line
2.54.0
PATCH v2 23/26 xen/riscv: initialize RCU, scheduler, and system domains in start_xen()
In reply to Oleksii KurochkoWire up the missing early-boot initialization steps in start_xen().
The scheduler must be initialized prior to do_initcalls() because cpupool_create_pool() is called during initcalls; without it, BUG_ON(IS_ERR(pool)) is triggered inside cpupool_create_pool().
---
Changes in v2:
- New patch. Several patches were folded into one.
--- xen/arch/riscv/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
xen/arch/riscv/setup.c +12 -0
@@ -6,9 +6,12 @@#include <xen/compile.h> #include <xen/console.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/init.h> #include <xen/irq.h> #include <xen/mm.h> +#include <xen/rcupdate.h> +#include <xen/sched.h> #include <xen/serial.h> #include <xen/shutdown.h> #include <xen/smp.h>
@@ -156,12 +159,21 @@ void __init noreturn start_xen(unsigned long bootcpu_id,timer_init (); +rcu_init (); + +setup_system_domains (); +local_irq_enable ();console_init_postirq ();guest_mm_init (); +scheduler_init (); +set_current (idle_vcpu[0 ]); + +do_initcalls (); +printk ("All set up\n" );machine_halt ();
Signature · 1 line
2.54.0
PATCH v2 24/26 xen/riscv: provide init_vuart()
In reply to Oleksii KurochkoFor debug purpose is enough to have only print messages from guest what is now implemented in vsbi_legacy_ecall_handler().
For full guesst console support it will better to have something similar to [1], thereby there is nothing specific should be done, at least, for now and init_vuart() is provided to make dom0less code buildable.
---
Changes in v2:
- Add Acked-by: Jan Beulich <[email protected]>.
--- xen/arch/riscv/dom0less-build.c | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/arch/riscv/dom0less-build.c +8 -0
@@ -8,6 +8,14 @@#include <asm/p2m.h> +int __init init_vuart (struct domain *d,struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +/* Nothing to do at the moment */ + +return 0 ; +} +int __init init_intc_phandle (struct kernel_info *kinfo,const char *name,const int node_next,const void *pfdt) {
Signature · 1 line
2.54.0
PATCH v2 25/26 xen/riscv: add initial dom0less infrastructure support
In reply to Oleksii KurochkoEnable dom0less support for RISC-V by selecting HAS_DOM0LESS and providing the minimal architecture hooks required by the common dom0less infrastructure.
Add stub implementations for architecture-specific helpers used when building domains from the device tree. These currently perform no additional work but allow the generic dom0less code to build and run on RISC-V.
Introduce max_init_domid as a runtime variable rather than a constant so that it can be updated during dom0less domain creation.
Provide missing helpers and definitions required by the domain construction code, including domain bitness helpers and the p2m_set_allocation() prototype.
Additionally define the guest magic memory region in the public RISC-V interface.
As HAS_DOM0LESS is selected for RISC-V now it could be a compilation issue if CONFIG_STATIC_MEMORY=y as guest_physmap_add_pages() isn't yet provided.
---
Changes in v2:
- Move declaration of p2m_set_allocation() to p2m-common.h.
- Add __initdata for max_init_domid and drop initalizer for it.
- Add CONFIG_STATIC_MEMORY=n to CI's randconfig to avoid compilation error because of guest_physmap_add_pages() isn't provided.
- Select HAS_DOMAIN_TYPE for RISC-V and drop things which were introduced when HAS_DOMAIN_TYPE doesn't exist.
--- automation/gitlab-ci/build.yaml | 1 + xen/arch/riscv/Kconfig | 2 ++ xen/arch/riscv/dom0less-build.c | 6 ++++++ xen/arch/riscv/domain-build.c | 13 +++++++++++++ xen/arch/riscv/include/asm/guest-layout.h | 3 +++ xen/arch/riscv/include/asm/setup.h | 4 +++- xen/arch/riscv/setup.c | 2 ++ 7 files changed, 30 insertions(+), 1 deletion(-)
automation/gitlab-ci/build.yaml +1 -0
@@ -805,6 +805,7 @@ debian-13-riscv64-gcc: CONFIG_GRANT_TABLE=n CONFIG_LIVEPATCH=n CONFIG_QEMU_PLATFORM=y + CONFIG_STATIC_MEMORY=n CONFIG_VM_EVENT=n CONFIG_XSM=n
xen/arch/riscv/Kconfig +2 -0
@@ -6,6 +6,8 @@ config RISCV select GENERIC_BUG_FRAME select GENERIC_UART_INIT select HAS_DEVICE_TREE_DISCOVERY + select HAS_DOM0LESS + select HAS_DOMAIN_TYPE select HAS_EX_TABLE select HAS_PMAP select HAS_UBSAN
xen/arch/riscv/dom0less-build.c +6 -0
@@ -102,3 +102,9 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,return 0 ; } + +int __init arch_handle_passthrough_prop (struct kernel_info *kinfo, +struct dt_device_node *node) +{ +return 0 ; +}
xen/arch/riscv/domain-build.c +13 -0
@@ -158,9 +158,22 @@ int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo)return fdt_end_node (fdt); } +int __init construct_hwdom (struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +return -EOPNOTSUPP ; +} +int __init make_timer_node (const struct kernel_info *kinfo) {/* There is no need for timer node for RISC-V. */ return 0 ; } + +int __init make_hypervisor_node (struct domain *d, +const struct kernel_info *kinfo, +int addrcells,int sizecells) +{ +return -EOPNOTSUPP ; +}
xen/arch/riscv/include/asm/guest-layout.h +3 -0
@@ -24,4 +24,7 @@#define GUEST_RAM_BANK_BASES { GUEST_RAM0_BASE, GUEST_RAM1_BASE }#define GUEST_RAM_BANK_SIZES { GUEST_RAM0_SIZE, GUEST_RAM1_SIZE } +#define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000) +#define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000) +#endif /* ASM_RISCV_GUEST_LAYOUT_H */
xen/arch/riscv/include/asm/setup.h +3 -1
@@ -5,11 +5,13 @@#include <xen/types.h> +#include <public/xen.h> +struct domain ;struct dt_device_node ;struct rangeset ; -#define max_init_domid (0) +extern domid_t max_init_domid;void setup_mm (void );
xen/arch/riscv/setup.c +2 -0
@@ -33,6 +33,8 @@#include <asm/traps.h> #include <asm/vsbi.h> +domid_t __initdata max_init_domid; +/* Xen stack for bringing up the first CPU. */ unsigned char __initdata cpu0_boot_stack[STACK_SIZE ]__aligned(STACK_SIZE) ;
Signature · 1 line
2.54.0
PATCH v2 26/26 xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable callbacks
In reply to Oleksii Kurochkodesc->status is only set once during setup_irq(), but interrupts can be enabled/disabled at runtime, so update it in the corresponding callbacks.
wmb() in aplic_irq_enable() ensures do_IRQ(), which can fire immediately after the interrupt is enabled, sees the updated desc->status. No rmb() is needed on the do_IRQ() side because desc->status is read under a spinlock, which implies an acquire barrier.
No barrier is needed in aplic_irq_disable() because the hardware disables the interrupt before the status is updated, so do_IRQ() cannot fire, and spin_unlock() makes the updated value visible.
---
Changes in v2:
- New patch.
--- xen/arch/riscv/aplic.c | 5 +++++ xen/arch/riscv/irq.c | 3 --- 2 files changed, 5 insertions(+), 3 deletions(-)
xen/arch/riscv/aplic.c +5 -0
@@ -161,6 +161,9 @@ static void cf_check aplic_irq_enable(struct irq_desc *desc)spin_lock (&aplic.lock); + desc->status &= ~IRQ_DISABLED ; +wmb (); +/* Enable interrupt in IMSIC */ imsic_irq_enable (desc->irq);
@@ -189,6 +192,8 @@ static void cf_check aplic_irq_disable(struct irq_desc *desc)/* Disable interrupt in IMSIC */ imsic_irq_disable (desc->irq); + desc->status |=IRQ_DISABLED ; +spin_unlock (&aplic.lock); }
xen/arch/riscv/irq.c +0 -3
@@ -145,9 +145,6 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new) desc->handler->set_affinity (desc,cpumask_of (smp_processor_id ())); desc->handler->startup (desc); - -/* Enable irq */ - desc->status &= ~IRQ_DISABLED ; } err:
Signature · 1 line
2.54.0
Re: [PATCH v2 02/26] xen/riscv: Implement ARCH_PAGING_MEMPOOL
In reply to Oleksii KurochkoQuoted 10 lines · Oleksii Kurochko
The
p2m_freelistis used to allocate pages for the P2M. To initialize this list,domain_p2m_set_allocation()may be called fromconstruct_domU()in the common Dom0less code, so RISC-V provides an implementation and enablesCONFIG_ARCH_PAGING_MEMPOOLunconditionally.Additionally, implement arch_{set,get}_paging_mempool_size(). They are not directly used yet, but are required to support the XEN_DOMCTL_{get,set}_paging_mempool_size hypercalls.
Signed-off-by Oleksii Kurochko <[email protected]>
with ...
Quoted 17 lines
xen/arch/riscv/p2m.c +11
-0@@ -1606,3 +1606,27 @@ struct page_info *get_page_from_gfn(struct domain *d, unsigned long gfn,return p2m_get_page_from_gfn (p2m_get_hostp2m (d),_gfn (gfn), t); } + +int arch_set_paging_mempool_size (struct domain *d,uint64_t size) +{ +unsigned long pages =PFN_DOWN (size); +int rc; + +/* Non page-sized request or 32-bit overflow? */ +if (pfn_to_paddr (pages) != size ) +return -EINVAL ; + +spin_lock (&d->arch.paging.lock);
xen/arch/riscv/p2m.c struct page_info *get_page_from_gfn(struct domain *d, unsigned long gfn, · line 1629
Quoted 10 lines
xen/arch/riscv/p2m.c +10
-0+ rc =p2m_set_allocation (d, pages, true); +spin_unlock (&d->arch.paging.lock); + +return rc; +} + +/* Return the size of the pool, in bytes. */ +int arch_get_paging_mempool_size (struct domain *d,uint64_t *size) +{ + *size =pfn_to_paddr (ACCESS_ONCE (d->arch.paging.total_pages));
... the stray blank dropped from here. Will take care of this when committing.
Jan
Re: [PATCH v2 03/26] xen/riscv: Implement construct_domain()
In reply to Oleksii KurochkoQuoted 8 lines · Oleksii Kurochko
Implement
construct_domain()function for RISC-V, which performs initial setup for the domain's first vCPU, loads the kernel, initrd, and device tree, and sets up guest CPU registers for boot.It also creates additional vCPUs up to
max_vcpusand assigns the device tree address and boot cpuid in registers.Signed-off-by Oleksii Kurochko <[email protected]>
with ...
Quoted 32 lines
xen/arch/riscv/domain-build.c new file +29
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <xen/fdt-domain-build.h> +#include <xen/fdt-kernel.h> +#include <xen/init.h> +#include <xen/sched.h> + +#include <asm/current.h> +#include <asm/guest_access.h> + +int __init construct_domain (struct domain *d,struct kernel_info *kinfo) +{ +struct vcpu *v = d->vcpu[0 ]; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (v); + +BUG_ON (v->is_initialised); + +/* +* At the moment *_load() don't return value and will just panic() +* inside. +* TODO: it will be good to change that. +*/ +kernel_load (kinfo); +initrd_load (kinfo, copy_to_guest_phys); +dtb_load (kinfo, copy_to_guest_phys); + + regs->sepc = kinfo->entry; + +/* Guest boot cpuid = 0 */
xen/arch/riscv/domain-build.c line 39
Quoted 10 lines
xen/arch/riscv/domain-build.c new file +10
-0+ regs->a0 =0 ; + regs->a1 = kinfo->dtb_paddr; + +for (unsigned int i =1 ; i < d->max_vcpus; i++ ) + { +const struct vcpu *tmp_v =vcpu_create (d, i); + +if ( !tmp_v ) + { +printk ("Failed to allocate %pd v%d\n" , d, i);
... %u used here and ...
xen/arch/riscv/domain-build.c line 43
Quoted 4 lines
xen/arch/riscv/domain-build.c new file +4
-0+break ; + } + +dprintk (XENLOG_INFO ,"Created vcpu %pv\n" , tmp_v);
... this line preferably dropped. Can do when committing, provided you agree.
Jan
Quoted 6 lines · Oleksii Kurochko
This patch series reprensent a bunch of patches necessary to enable common part of Dom0less.
The stuff necessary to start/launch domains will be introduced separately.This patch series is based on [1], but a lot of patch could go even without it.
What do I do with this kind of statement? This still leaves it to the committer to figure out which ones could go in earlier. What helps in this specific case it that ...
Quoted 1 line
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
... the remainder of this looks to be ready to go in now (and I intend to commit that tomorrow).
Jan
Re: [PATCH v2 04/26] xen/riscv: implement prerequisites for domain_create()
In reply to Oleksii KurochkoQuoted 17 lines · Oleksii Kurochko
xen/arch/riscv/domain.c +11
-0@@ -289,6 +289,31 @@ void sync_vcpu_execstate(struct vcpu *v)/* Nothing to do -- no lazy switching */ } +int arch_sanitise_domain_config (struct xen_domctl_createdomain *config) +{ +return 0 ; +} + +int arch_domain_create (struct domain *d, +struct xen_domctl_createdomain *config, +unsigned int flags) +{ +int rc =0 ; +
xen/arch/riscv/domain.c void sync_vcpu_execstate(struct vcpu *v) · line 312
Quoted 10 lines
xen/arch/riscv/domain.c +10
-0+if (is_idle_domain (d) ) +return 0 ; + +if ( (rc =p2m_init (d, config)) !=0 ) + goto fail; + +return rc; + + fail: + d->is_dying = DOMDYING_dead;
domain_create() does this as well. Is it really needed here?
xen/arch/riscv/domain.c void sync_vcpu_execstate(struct vcpu *v) · line 313
Quoted 1 line
xen/arch/riscv/domain.c +1
-0+arch_domain_destroy (d);
This continues to be a stub, i.e. upon encountering any kind of error one would hit the BUG_ON() there.
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
In reply to Oleksii Kurochkoxen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 494
Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/cpufeature.c +4
-0@@ -480,6 +488,53 @@ bool riscv_isa_extension_available(const unsigned long *isa_bitmap,return test_bit (id, isa_bitmap); } +int init_guest_isa (struct domain *d) +{ +char *buf = d->arch.guest_isa_str; +size_t len =sizeof (d->arch.guest_isa_str);
Seeing these uses: Is the "guest" prefix really of much use here?
xen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 497
Quoted 2 lines
xen/arch/riscv/cpufeature.c +2
-0+bitmap_andnot (d->arch.guest_isa, riscv_isa, guest_unsupp, +RISCV_ISA_EXT_MAX );
Same question here, clearly.
Quoted 28 lines
xen/arch/riscv/cpufeature.c +28
-0+#if defined(CONFIG_RISCV_32 ) +if (snprintf (buf, len,"rv32" ) >= len ) +return -ENOBUFS ; +#elif defined(CONFIG_RISCV_64 ) +if (snprintf (buf, len,"rv64" ) >= len ) +return -ENOBUFS ; +#else +# error "Unsupported RISC-V bitness" +#endif + +for (unsigned int i =0 ; i <ARRAY_SIZE (riscv_isa_ext); i++ ) + { +const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; + +if ( !riscv_isa_extension_available (d->arch.guest_isa, ext->id) ) +continue ; + +if ( ext->id >=RISCV_ISA_EXT_BASE &&strlcat (buf,"_" , len) >= len ) +return -ENOBUFS ; + +if (strlcat (buf, ext->name, len) >= len ) +return -ENOBUFS ; + } + +return 0 ; +} + +static void __init init_guest_unsupp (void )
xen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 536
Quoted 10 lines
xen/arch/riscv/cpufeature.c +10
-0+{ +set_bit (RISCV_ISA_EXT_f, guest_unsupp); +set_bit (RISCV_ISA_EXT_d, guest_unsupp); +set_bit (RISCV_ISA_EXT_q, guest_unsupp); +set_bit (RISCV_ISA_EXT_v, guest_unsupp); +set_bit (RISCV_ISA_EXT_h, guest_unsupp); +set_bit (RISCV_ISA_EXT_sstc, guest_unsupp); +set_bit (RISCV_ISA_EXT_svade, guest_unsupp); +set_bit (RISCV_ISA_EXT_svpbmt, guest_unsupp); +}
These don't need to be atomic, do they? I.e. __set_bit() would suffice.
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256
This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];
... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
Jan
Re: [PATCH v2 06/26] xen/riscv: implement make_cpus_node()
In reply to Oleksii Kurochkoxen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 55
Quoted 5 lines · Oleksii Kurochko
xen/arch/riscv/domain-build.c +1
-0@@ -50,3 +52,107 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)return 0 ; } +int __init make_cpus_node (const struct domain *d,struct kernel_info *kinfo)
Nit: Blank line above here, please.
Quoted 25 lines
xen/arch/riscv/domain-build.c +25
-0+{ +int res; +const struct dt_device_node *cpus =dt_find_node_by_path ("/cpus" ); +unsigned int cpu; +uint32_t timebase_frequency; +bool frequency_valid; +void *fdt = kinfo->fdt; + +dt_dprintk ("Create cpus node\n" ); + +if ( !cpus ) + { +dprintk (XENLOG_ERR ,"Missing /cpus node in the device tree?\n" ); +return -ENOENT ; + } + + frequency_valid =dt_property_read_u32 (cpus,"timebase-frequency" , + &timebase_frequency); + + res =fdt_begin_node (fdt,"cpus" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#address-cells" ,1 ); +if ( res )
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 90
Quoted 10 lines
xen/arch/riscv/domain-build.c +10
-0+return res; + + res =fdt_property_cell (fdt,"#size-cells" ,0 ); +if ( res ) +return res; + +if ( frequency_valid ) + res =fdt_property_cell (fdt,"timebase-frequency" , timebase_frequency); + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ )
Limit cpu's scope to this loop?
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 93
Quoted 3 lines
xen/arch/riscv/domain-build.c +3
-0+ { +char buf[64 ]; +uint32_t reg =cpu_to_fdt32 (cpu);
Isn't this a byte-order adjustment? If so, how come ...
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 95
Quoted 1 line
xen/arch/riscv/domain-build.c +1
-0+snprintf (buf,sizeof (buf),"cpu@%u" , cpu);
... the result is passed to an entirely non-FDT function? (Most pre-existing uses of the function that I can spot store something in memory, i.e. adjusting byte- order makes sense there.)
Quoted 42 lines
xen/arch/riscv/domain-build.c +42
-0+ res =fdt_begin_node (fdt, buf); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , ®,sizeof (reg)); +if ( res ) +return res; + + res =fdt_property_string (fdt,"status" ,"okay" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv" ); +if ( res ) +return res; + +BUILD_BUG_ON ((sizeof ("riscv," ) + + sizeof_field(struct gstage_mode_desc, name)) >= sizeof(buf)); +snprintf (buf,sizeof (buf),"riscv,%s" , max_gstage_mode->name); + res =fdt_property_string (fdt,"mmu-type" , buf); +if ( res ) +return res; + + res =fdt_property_string (fdt,"riscv,isa" , d->arch.guest_isa_str); +if ( res ) +return res; + + res =fdt_property_string (fdt,"device_type" ,"cpu" ); +if ( res ) +return res; + + res =fdt_begin_node (fdt,"interrupt-controller" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv,cpu-intc" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#interrupt-cells" ,1 ); +if ( res ) +return res;
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 147
Quoted 10 lines
xen/arch/riscv/domain-build.c +10
-0+ + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_u32 (fdt,"phandle" ,alloc_phandle (kinfo)); +if ( res ) +return res; + +/* end of interrupt-controller */
Nit: Comment style. Also such a comment pretty clearly calls for a counterpart at the start.
Quoted 11 lines
xen/arch/riscv/domain-build.c +11
-0+ res =fdt_end_node (fdt); +if ( res ) +return res; + + res =fdt_end_node (fdt); +if ( res ) +return res; + } + +return fdt_end_node (fdt); +}
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
This patch series reprensent a bunch of patches necessary to enable common part of Dom0less.
The stuff necessary to start/launch domains will be introduced separately.This patch series is based on [1], but a lot of patch could go even without it.
Quoted 7 lines
What do I do with this kind of statement? This still leaves it to the committer to figure out which ones could go in earlier. What helps in this specific case it that ...
Quoted 1 line
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
... the remainder of this looks to be ready to go in now (and I intend to commit that tomorrow).
I will be more specific in the future and specify which patches can go in without waiting for the dependency patch series to be fully merged.
~ Oleksii
Re: [PATCH v2 02/26] xen/riscv: Implement ARCH_PAGING_MEMPOOL
In reply to Jan BeulichQuoted 36 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
The
p2m_freelistis used to allocate pages for the P2M. To initialize this list,domain_p2m_set_allocation()may be called fromconstruct_domU()in the common Dom0less code, so RISC-V provides an implementation and enablesCONFIG_ARCH_PAGING_MEMPOOLunconditionally.Additionally, implement arch_{set,get}_paging_mempool_size(). They are not directly used yet, but are required to support the XEN_DOMCTL_{get,set}_paging_mempool_size hypercalls.
Signed-off-by Oleksii Kurochko <[email protected]>Acked-by Jan Beulich <[email protected]>with ...
Quoted 21 lines
xen/arch/riscv/p2m.c +15
-0@@ -1606,3 +1606,27 @@ struct page_info *get_page_from_gfn(struct domain *d, unsigned long gfn,return p2m_get_page_from_gfn (p2m_get_hostp2m (d),_gfn (gfn), t); } + +int arch_set_paging_mempool_size (struct domain *d,uint64_t size) +{ +unsigned long pages =PFN_DOWN (size); +int rc; + +/* Non page-sized request or 32-bit overflow? */ +if (pfn_to_paddr (pages) != size ) +return -EINVAL ; + +spin_lock (&d->arch.paging.lock); + rc =p2m_set_allocation (d, pages, true); +spin_unlock (&d->arch.paging.lock); + +return rc;
Quoted 8 lines
xen/arch/riscv/p2m.c struct page_info *get_page_from_gfn(struct domain *d, unsigned long gfn, · line 1629
Quoted 6 lines
xen/arch/riscv/p2m.c +6
-0+} + +/* Return the size of the pool, in bytes. */ +int arch_get_paging_mempool_size (struct domain *d,uint64_t *size) +{ + *size =pfn_to_paddr (ACCESS_ONCE (d->arch.paging.total_pages));... the stray blank dropped from here. Will take care of this when committing.
I would be happy with that.
Thanks.
~ Oleksii
Re: [PATCH v2 03/26] xen/riscv: Implement construct_domain()
In reply to Jan BeulichQuoted 58 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko
Implement
construct_domain()function for RISC-V, which performs initial setup for the domain's first vCPU, loads the kernel, initrd, and device tree, and sets up guest CPU registers for boot.It also creates additional vCPUs up to
max_vcpusand assigns the device tree address and boot cpuid in registers.Signed-off-by Oleksii Kurochko <[email protected]>Acked-by Jan Beulich <[email protected]>with ...
Quoted 32 lines
xen/arch/riscv/domain-build.c new file +29
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <xen/fdt-domain-build.h> +#include <xen/fdt-kernel.h> +#include <xen/init.h> +#include <xen/sched.h> + +#include <asm/current.h> +#include <asm/guest_access.h> + +int __init construct_domain (struct domain *d,struct kernel_info *kinfo) +{ +struct vcpu *v = d->vcpu[0 ]; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (v); + +BUG_ON (v->is_initialised); + +/* +* At the moment *_load() don't return value and will just panic() +* inside. +* TODO: it will be good to change that. +*/ +kernel_load (kinfo); +initrd_load (kinfo, copy_to_guest_phys); +dtb_load (kinfo, copy_to_guest_phys); + + regs->sepc = kinfo->entry; + +/* Guest boot cpuid = 0 */ xen/arch/riscv/domain-build.c line 39
Quoted 10 lines
xen/arch/riscv/domain-build.c new file +10
-0+ regs->a0 =0 ; + regs->a1 = kinfo->dtb_paddr; + +for (unsigned int i =1 ; i < d->max_vcpus; i++ ) + { +const struct vcpu *tmp_v =vcpu_create (d, i); + +if ( !tmp_v ) + { +printk ("Failed to allocate %pd v%d\n" , d, i);... %u used here and ...
Quoted 6 lines
xen/arch/riscv/domain-build.c line 43
Quoted 4 lines
xen/arch/riscv/domain-build.c new file +4
-0+break ; + } + +dprintk (XENLOG_INFO ,"Created vcpu %pv\n" , tmp_v);... this line preferably dropped. Can do when committing, provided you agree.
I agree with these changes.
Thanks a lot.
~ Oleksii
Re: [PATCH v2 04/26] xen/riscv: implement prerequisites for domain_create()
In reply to Jan BeulichQuoted 22 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
xen/arch/riscv/domain.c +15
-0@@ -289,6 +289,31 @@ void sync_vcpu_execstate(struct vcpu *v)/* Nothing to do -- no lazy switching */ } +int arch_sanitise_domain_config (struct xen_domctl_createdomain *config) +{ +return 0 ; +} + +int arch_domain_create (struct domain *d, +struct xen_domctl_createdomain *config, +unsigned int flags) +{ +int rc =0 ; + +if (is_idle_domain (d) ) +return 0 ; + +if ( (rc =p2m_init (d, config)) !=0 )
Quoted 8 lines
xen/arch/riscv/domain.c void sync_vcpu_execstate(struct vcpu *v) · line 312
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+ goto fail; + +return rc; + + fail: + d->is_dying = DOMDYING_dead;
domain_create()does this as well. Is it really needed here?
Considering that domain wasn't created and thereby scheduled that nothing will use d->is_dying and so it could be dropped or moved to arch_domain_destroy().
Quoted 5 lines
xen/arch/riscv/domain.c void sync_vcpu_execstate(struct vcpu *v) · line 313
Quoted 2 lines
xen/arch/riscv/domain.c +1
-0+arch_domain_destroy (d);This continues to be a stub, i.e. upon encountering any kind of error one would hit the
BUG_ON()there.
I think that for current stage of development it is fine.
Would it be better change BUG_ON() to printk()? And add proper implementation a little bit later?
~ Oleksii
Re: [PATCH v2 04/26] xen/riscv: implement prerequisites for domain_create()
Quoted 39 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
xen/arch/riscv/domain.c +15
-0@@ -289,6 +289,31 @@ void sync_vcpu_execstate(struct vcpu *v)/* Nothing to do -- no lazy switching */ } +int arch_sanitise_domain_config (struct xen_domctl_createdomain *config) +{ +return 0 ; +} + +int arch_domain_create (struct domain *d, +struct xen_domctl_createdomain *config, +unsigned int flags) +{ +int rc =0 ; + +if (is_idle_domain (d) ) +return 0 ; + +if ( (rc =p2m_init (d, config)) !=0 )Quoted 8 lines
xen/arch/riscv/domain.c void sync_vcpu_execstate(struct vcpu *v) · line 312
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+ goto fail; + +return rc; + + fail: + d->is_dying = DOMDYING_dead;
domain_create()does this as well. Is it really needed here?Considering that domain wasn't created and thereby scheduled that nothing will use
d->is_dyingand so it could be dropped or moved toarch_domain_destroy().Quoted 3 lines
Quoted 2 lines
xen/arch/riscv/domain.c +1
-0+arch_domain_destroy (d);
Quoted 7 lines
Quoted 2 lines
This continues to be a stub, i.e. upon encountering any kind of error one would hit the
BUG_ON()there.I think that for current stage of development it is fine.
Would it be better change
BUG_ON()toprintk()? And add proper implementation a little bit later?
That may be acceptable, yes.
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
In reply to Jan BeulichQuoted 12 lines · Jan Beulich
xen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 494
Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/cpufeature.c +4
-0@@ -480,6 +488,53 @@ bool riscv_isa_extension_available(const unsigned long *isa_bitmap,return test_bit (id, isa_bitmap); } +int init_guest_isa (struct domain *d) +{ +char *buf = d->arch.guest_isa_str; +size_t len =sizeof (d->arch.guest_isa_str);Seeing these uses: Is the "guest" prefix really of much use here?
Quoted 4 lines
xen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 497
Quoted 2 lines
xen/arch/riscv/cpufeature.c +2
-0+bitmap_andnot (d->arch.guest_isa, riscv_isa, guest_unsupp, +RISCV_ISA_EXT_MAX );Same question here, clearly.
I will drop "guest" prefix for "d->arch.guest_isa_str".
Quoted 33 lines
Quoted 33 lines
xen/arch/riscv/cpufeature.c +32
-0+#if defined(CONFIG_RISCV_32 ) +if (snprintf (buf, len,"rv32" ) >= len ) +return -ENOBUFS ; +#elif defined(CONFIG_RISCV_64 ) +if (snprintf (buf, len,"rv64" ) >= len ) +return -ENOBUFS ; +#else +# error "Unsupported RISC-V bitness" +#endif + +for (unsigned int i =0 ; i <ARRAY_SIZE (riscv_isa_ext); i++ ) + { +const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; + +if ( !riscv_isa_extension_available (d->arch.guest_isa, ext->id) ) +continue ; + +if ( ext->id >=RISCV_ISA_EXT_BASE &&strlcat (buf,"_" , len) >= len ) +return -ENOBUFS ; + +if (strlcat (buf, ext->name, len) >= len ) +return -ENOBUFS ; + } + +return 0 ; +} + +static void __init init_guest_unsupp (void ) +{ +set_bit (RISCV_ISA_EXT_f, guest_unsupp); +set_bit (RISCV_ISA_EXT_d, guest_unsupp); +set_bit (RISCV_ISA_EXT_q, guest_unsupp);
Quoted 8 lines
xen/arch/riscv/cpufeature.c bool riscv_isa_extension_available(const unsigned long *isa_bitmap, · line 536
Quoted 6 lines
xen/arch/riscv/cpufeature.c +6
-0+set_bit (RISCV_ISA_EXT_v, guest_unsupp); +set_bit (RISCV_ISA_EXT_h, guest_unsupp); +set_bit (RISCV_ISA_EXT_sstc, guest_unsupp); +set_bit (RISCV_ISA_EXT_svade, guest_unsupp); +set_bit (RISCV_ISA_EXT_svpbmt, guest_unsupp); +}These don't need to be atomic, do they? I.e.
__set_bit()would suffice.
Agree, __set_bit() would be enough.
Quoted 15 lines
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 8 lines
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
Quoted 3 lines
xen/arch/riscv/include/asm/cpufeature.h +0
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 4 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
It could be allocated dynamically.
Does it make sense to evaluate in run-time what should be a buffer size? For this case I can't find analogue of realloc() in Xen. Or it would be fine just to take something bigger as a const (lets say 2048) and use it for dynamic allocation?
Thanks.
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 24 lines · Oleksii Kurochko
Quoted 15 lines · Jan Beulich
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
Quoted 3 lines
xen/arch/riscv/include/asm/cpufeature.h +0
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 4 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
Quoted 4 lines
It could be allocated dynamically.
Does it make sense to evaluate in run-time what should be a buffer size? For this case I can't find analogue of
realloc()in Xen.
Hmm, I see xrealloc_array(), and surely we could gain xvrealloc_array() which we'll need anyway once xrealloc_array() uses get converted. (I also see x{,v}realloc_flex_struct(), but that's of no use here as it looks.)
Quoted 3 lines
Or it would be
fine just to take something bigger as a const (lets say 2048) and use it for dynamic allocation?
I'd rather not. Can't you determine how much space the string is going to occupy?
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
Quoted 8 lines
Quoted 4 lines
It could be allocated dynamically.
Does it make sense to evaluate in run-time what should be a buffer size? For this case I can't find analogue of
realloc()in Xen.Hmm, I see
xrealloc_array(), and surely we could gainxvrealloc_array()which we'll need anyway oncexrealloc_array()uses get converted. (I also see x{,v}realloc_flex_struct(), but that's of no use here as it looks.)
Oh, I missed to turned off "full match" during search...
Quoted 7 lines
Quoted 4 lines
Or it would be
fine just to take something bigger as a const (lets say 2048) and use it for dynamic allocation?I'd rather not. Can't you determine how much space the string is going to occupy?
I thought about two options as alternatives:
1. Take as a length host RISC-V ISA string but theoretically we can emulate some extensions which aren't mentioned in host RISC-V ISA string so it could be longer. So not a good option.
2. Having two walks in init_guest_isa():
Introduce the following function:
static size_t guest_isa_str_len(const unsigned long *isa_bitmap)
{
size_t len = 4; /* rvX prefix */
for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ )
{
const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
if ( !riscv_isa_extension_available(isa_bitmap, ext->id) )
continue;
if ( ext->id >= RISCV_ISA_EXT_BASE )
len++; /* '_' separator */
len += strlen(ext->name);
}
return len + 1; /* NUL terminator */
}and then:
int init_guest_isa(struct domain *d)
{
bitmap_andnot(d->arch.guest_isa, riscv_isa, guest_unsupp,
RISCV_ISA_EXT_MAX);
size_t len = guest_isa_str_len(d->arch.guest_isa);
d->arch.guest_isa_str = xzalloc_array(char, len);
if ( !d->arch.guest_isa_str )
return -ENOMEM;
/* ... existing snprintf + strlcat loop unchanged ... */
}If approach 2 is a good one I can follow it.
~ Oleksii
Re: [PATCH v2 06/26] xen/riscv: implement make_cpus_node()
In reply to Jan BeulichQuoted 38 lines · Jan Beulich
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 55
Quoted 5 lines · Oleksii Kurochko
xen/arch/riscv/domain-build.c +1
-0@@ -50,3 +52,107 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)return 0 ; } +int __init make_cpus_node (const struct domain *d,struct kernel_info *kinfo)Nit: Blank line above here, please.
Quoted 29 lines
xen/arch/riscv/domain-build.c +29
-0+{ +int res; +const struct dt_device_node *cpus =dt_find_node_by_path ("/cpus" ); +unsigned int cpu; +uint32_t timebase_frequency; +bool frequency_valid; +void *fdt = kinfo->fdt; + +dt_dprintk ("Create cpus node\n" ); + +if ( !cpus ) + { +dprintk (XENLOG_ERR ,"Missing /cpus node in the device tree?\n" ); +return -ENOENT ; + } + + frequency_valid =dt_property_read_u32 (cpus,"timebase-frequency" , + &timebase_frequency); + + res =fdt_begin_node (fdt,"cpus" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#address-cells" ,1 ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#size-cells" ,0 ); +if ( res )
Quoted 8 lines
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 90
Quoted 6 lines
xen/arch/riscv/domain-build.c +6
-0+return res; + +if ( frequency_valid ) + res =fdt_property_cell (fdt,"timebase-frequency" , timebase_frequency); + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ )Limit cpu's scope to this loop?
I will remove 'unsigned int cpu' from the function-level declarations and moved it into the for loop.
Quoted 5 lines
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 93
Quoted 4 lines
xen/arch/riscv/domain-build.c +3
-0+ { +char buf[64 ]; +uint32_t reg =cpu_to_fdt32 (cpu);
Quoted 7 lines
Isn't this a byte-order adjustment? If so, how come ...
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 95
Quoted 1 line
xen/arch/riscv/domain-build.c +1
-0+snprintf (buf,sizeof (buf),"cpu@%u" , cpu);... the result is passed to an entirely non-FDT function? (Most pre-existing uses of the function that I can spot store something in memory, i.e. adjusting byte- order makes sense there.)
But here pure cpu is used instead of reg variable and reg variable is used here ...
Quoted 8 lines
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 102
Quoted 8 lines
xen/arch/riscv/domain-build.c +7
-0+ res =fdt_begin_node (fdt, buf); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , ®,sizeof (reg)); +if ( res ) +return res;
... but it we could drop it and use just:
res = fdt_property_cell(fdt, "reg", cpu);
fdt_property_cell will take care if a byte-order adjustment.
Quoted 40 lines
Quoted 40 lines
xen/arch/riscv/domain-build.c +40
-0+ + res =fdt_property_string (fdt,"status" ,"okay" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv" ); +if ( res ) +return res; + +BUILD_BUG_ON ((sizeof ("riscv," ) + + sizeof_field(struct gstage_mode_desc, name)) >= sizeof(buf)); +snprintf (buf,sizeof (buf),"riscv,%s" , max_gstage_mode->name); + res =fdt_property_string (fdt,"mmu-type" , buf); +if ( res ) +return res; + + res =fdt_property_string (fdt,"riscv,isa" , d->arch.guest_isa_str); +if ( res ) +return res; + + res =fdt_property_string (fdt,"device_type" ,"cpu" ); +if ( res ) +return res; + + res =fdt_begin_node (fdt,"interrupt-controller" ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,"riscv,cpu-intc" ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#interrupt-cells" ,1 ); +if ( res ) +return res; + + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; +
Quoted 8 lines
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 147
Quoted 5 lines
xen/arch/riscv/domain-build.c +5
-0+ res =fdt_property_u32 (fdt,"phandle" ,alloc_phandle (kinfo)); +if ( res ) +return res; + +/* end of interrupt-controller */ Nit: Comment style. Also such a comment pretty clearly calls for a counterpart at the start.
Sure, I will add one.
Thanks.
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
In reply to Oleksii KurochkoQuoted 90 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
Quoted 8 lines
Quoted 4 lines
It could be allocated dynamically.
Does it make sense to evaluate in run-time what should be a buffer size? For this case I can't find analogue of
realloc()in Xen.Hmm, I see
xrealloc_array(), and surely we could gainxvrealloc_array()which we'll need anyway oncexrealloc_array()uses get converted. (I also see x{,v}realloc_flex_struct(), but that's of no use here as it looks.)Oh, I missed to turned off "full match" during search...
Quoted 7 lines
Quoted 4 lines
Or it would be
fine just to take something bigger as a const (lets say 2048) and use it for dynamic allocation?I'd rather not. Can't you determine how much space the string is going to occupy?
I thought about two options as alternatives:
1. Take as a length host RISC-V ISA string but theoretically we can emulate some extensions which aren't mentioned in host RISC-V ISA string so it could be longer. So not a good option.
2. Having two walks in
init_guest_isa():
Introduce the following function:
staticsize_tguest_isa_str_len(const unsigned long *isa_bitmap){ size_t len = 4; /* rvX prefix */ for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ ) { const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; if ( ext->id >= RISCV_ISA_EXT_BASE ) len++; /* '_' separator */ len += strlen(ext->name); } return len + 1; /* NUL terminator */ }and then:
int
init_guest_isa(struct domain *d){ bitmap_andnot(d->arch.guest_isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); size_t len = guest_isa_str_len(d->arch.guest_isa); d->arch.guest_isa_str = xzalloc_array(char, len); if ( !d->arch.guest_isa_str ) return -ENOMEM; /* ... existing snprintf + strlcat loop unchanged ... */ }
Quoted 1 line
If approach 2 is a good one I can follow it.
This might be yet better with only a single function. Otherwise the two are always at risk of going out of sync. After all you can use snprintf() to determine just the size needed; if you go look, there may even be an example or two in the tree.
Jan
Re: [PATCH v2 06/26] xen/riscv: implement make_cpus_node()
In reply to Oleksii KurochkoQuoted 25 lines · Oleksii Kurochko
Quoted 5 lines · Jan Beulich
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 93
Quoted 3 lines · Oleksii Kurochko
xen/arch/riscv/domain-build.c +3
-0+ { +char buf[64 ]; +uint32_t reg =cpu_to_fdt32 (cpu);Quoted 7 lines
Isn't this a byte-order adjustment? If so, how come ...
Quoted 1 line
+
snprintf(buf, sizeof(buf), "cpu@%u", cpu);... the result is passed to an entirely non-FDT function? (Most pre-existing uses of the function that I can spot store something in memory, i.e. adjusting byte- order makes sense there.)
But here pure cpu is used instead of reg variable and reg variable is used here ...
Quoted 8 lines
xen/arch/riscv/domain-build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 102
Quoted 8 lines
xen/arch/riscv/domain-build.c +7
-0+ res =fdt_begin_node (fdt, buf); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , ®,sizeof (reg)); +if ( res ) +return res;
Quoted 4 lines
... but it we could drop it and use just:
res = fdt_property_cell(fdt, "reg", cpu);
fdt_property_cellwill take care if a byte-order adjustment.
I'm sorry, I clearly must have seen something that wasn't really there.
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
In reply to Jan BeulichQuoted 91 lines · Jan Beulich
Quoted 90 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich
xen/arch/riscv/include/asm/cpufeature.h line 20
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/cpufeature.h +1
-0@@ -17,6 +17,8 @@*/ #define RISCV_ISA_EXT_BASE 26 +#define RISCV_GUEST_ISA_STR_MAX 256This looks like it won't be good for very long, seeing how long ISA strings can get. I wonder anyway whether ...
xen/arch/riscv/include/asm/cpufeature.h struct arch_domain { · line 100
Quoted 7 lines
xen/arch/riscv/include/asm/cpufeature.h +3
-0@@ -94,6 +95,9 @@ struct arch_domain {struct p2m_domain p2m;struct paging_domain paging; + +DECLARE_BITMAP (guest_isa,RISCV_ISA_EXT_MAX ); +char guest_isa_str[RISCV_GUEST_ISA_STR_MAX ];... a compile-time sized buffer is suitable here. Can't you allocate a buffer just large enough to hold the string?
Quoted 8 lines
Quoted 4 lines
It could be allocated dynamically.
Does it make sense to evaluate in run-time what should be a buffer size? For this case I can't find analogue of
realloc()in Xen.Hmm, I see
xrealloc_array(), and surely we could gainxvrealloc_array()which we'll need anyway oncexrealloc_array()uses get converted. (I also see x{,v}realloc_flex_struct(), but that's of no use here as it looks.)Oh, I missed to turned off "full match" during search...
Quoted 7 lines
Quoted 4 lines
Or it would be
fine just to take something bigger as a const (lets say 2048) and use it for dynamic allocation?I'd rather not. Can't you determine how much space the string is going to occupy?
I thought about two options as alternatives:
1. Take as a length host RISC-V ISA string but theoretically we can emulate some extensions which aren't mentioned in host RISC-V ISA string so it could be longer. So not a good option.
2. Having two walks in
init_guest_isa():
Introduce the following function:
staticsize_tguest_isa_str_len(const unsigned long *isa_bitmap){ size_t len = 4; /* rvX prefix */ for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ ) { const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; if ( ext->id >= RISCV_ISA_EXT_BASE ) len++; /* '_' separator */ len += strlen(ext->name); } return len + 1; /* NUL terminator */ }and then:
int
init_guest_isa(struct domain *d){ bitmap_andnot(d->arch.guest_isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); size_t len = guest_isa_str_len(d->arch.guest_isa); d->arch.guest_isa_str = xzalloc_array(char, len); if ( !d->arch.guest_isa_str ) return -ENOMEM; /* ... existing snprintf + strlcat loop unchanged ... */ }
Quoted 6 lines
Quoted 1 line
If approach 2 is a good one I can follow it.
This might be yet better with only a single function. Otherwise the two are always at risk of going out of sync. After all you can use
snprintf()to determine just the size needed; if you go look, there may even be an example or two in the tree.
I will do than in the following way:
static int build_guest_isa_str(char *buf, size_t size,
const unsigned long *isa_bitmap)
{
int total = 0;
int ret;#if defined(CONFIG_RISCV_32)
ret = snprintf(buf, size, "rv32");
#elif defined(CONFIG_RISCV_64)
ret = snprintf(buf, size, "rv64");
#else
# error "Unsupported RISC-V bitness"
#endif
if ( ret < 0 )
return ret;
total += ret;
for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ )
{
const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
if ( !riscv_isa_extension_available(isa_bitmap, ext->id) )
continue;
ret = snprintf(buf ? buf + total : NULL,
buf ? size - total : 0, "%s%s",
ext->id >= RISCV_ISA_EXT_BASE ? "_" : "",
ext->name);
if ( ret < 0 )
return ret;
total += ret;
}
return total;
}int init_guest_isa(struct domain *d)
{
int len;
bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp,
RISCV_ISA_EXT_MAX);
len = build_guest_isa_str(NULL, 0, d->arch.isa);
if ( len < 0 )
return len;
d->arch.isa_str = xmalloc_array(char, len + 1);
if ( !d->arch.isa_str )
return -ENOMEM;
build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);
return 0;
}Thanks for suggestion.
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 60 lines · Oleksii Kurochko
Quoted 44 lines · Jan Beulich
Quoted 43 lines · Oleksii Kurochko
I thought about two options as alternatives:
1. Take as a length host RISC-V ISA string but theoretically we can emulate some extensions which aren't mentioned in host RISC-V ISA string so it could be longer. So not a good option.
2. Having two walks in
init_guest_isa():
Introduce the following function:
staticsize_tguest_isa_str_len(const unsigned long *isa_bitmap){ size_t len = 4; /* rvX prefix */ for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ ) { const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; if ( ext->id >= RISCV_ISA_EXT_BASE ) len++; /* '_' separator */ len += strlen(ext->name); } return len + 1; /* NUL terminator */ }and then:
int
init_guest_isa(struct domain *d){ bitmap_andnot(d->arch.guest_isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); size_t len = guest_isa_str_len(d->arch.guest_isa); d->arch.guest_isa_str = xzalloc_array(char, len); if ( !d->arch.guest_isa_str ) return -ENOMEM; /* ... existing snprintf + strlcat loop unchanged ... */ }Quoted 6 lines
Quoted 1 line
If approach 2 is a good one I can follow it.
This might be yet better with only a single function. Otherwise the two are always at risk of going out of sync. After all you can use
snprintf()to determine just the size needed; if you go look, there may even be an example or two in the tree.I will do than in the following way:
static int build_guest_isa_str(char *buf,
size_tsize,
const unsigned long*isa_bitmap){ int total = 0; int ret;
Quoted 9 lines
#if defined(CONFIG_RISCV_32)
ret = snprintf(buf, size, "rv32");
#elif defined(CONFIG_RISCV_64)
ret = snprintf(buf, size, "rv64");
#else
# error "Unsupported RISC-V bitness"
#endif
if ( ret < 0 )
return ret;You can use total here right away, and limit ...
Quoted 5 lines
total += ret;
for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ )
{
const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];... ret's scope to this loop. This then also justifies total to be of a signed type.
Quoted 16 lines
if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; ret = snprintf(buf ? buf + total : NULL, buf ? size - total : 0, "%s%s", ext->id >= RISCV_ISA_EXT_BASE ? "_" : "", ext->name); if ( ret < 0 ) return ret; total += ret; } return total; }int
init_guest_isa(struct domain *d)
Quoted 15 lines
{
int len;
bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp,
RISCV_ISA_EXT_MAX);
len = build_guest_isa_str(NULL, 0, d->arch.isa);
if ( len < 0 )
return len;
d->arch.isa_str = xmalloc_array(char, len + 1);
if ( !d->arch.isa_str )
return -ENOMEM;
build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);At least ASSERT() the success of this?
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 73 lines · Jan Beulich
Quoted 60 lines · Oleksii Kurochko
Quoted 44 lines · Jan Beulich
Quoted 43 lines · Oleksii Kurochko
I thought about two options as alternatives:
1. Take as a length host RISC-V ISA string but theoretically we can emulate some extensions which aren't mentioned in host RISC-V ISA string so it could be longer. So not a good option.
2. Having two walks in
init_guest_isa():
Introduce the following function:
staticsize_tguest_isa_str_len(const unsigned long *isa_bitmap){ size_t len = 4; /* rvX prefix */ for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ ) { const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; if ( ext->id >= RISCV_ISA_EXT_BASE ) len++; /* '_' separator */ len += strlen(ext->name); } return len + 1; /* NUL terminator */ }and then:
int
init_guest_isa(struct domain *d){ bitmap_andnot(d->arch.guest_isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); size_t len = guest_isa_str_len(d->arch.guest_isa); d->arch.guest_isa_str = xzalloc_array(char, len); if ( !d->arch.guest_isa_str ) return -ENOMEM; /* ... existing snprintf + strlcat loop unchanged ... */ }Quoted 6 lines
Quoted 1 line
If approach 2 is a good one I can follow it.
This might be yet better with only a single function. Otherwise the two are always at risk of going out of sync. After all you can use
snprintf()to determine just the size needed; if you go look, there may even be an example or two in the tree.I will do than in the following way:
static int build_guest_isa_str(char *buf,
size_tsize,
const unsigned long*isa_bitmap){ int total = 0; int ret;Quoted 9 lines
#if defined(CONFIG_RISCV_32) ret = snprintf(buf, size, "rv32"); #elif defined(CONFIG_RISCV_64) ret = snprintf(buf, size, "rv64"); #else # error "Unsupported RISC-V bitness" #endif if ( ret < 0 ) return ret;You can use total here right away, and limit ...
Quoted 8 lines
Quoted 5 lines
total += ret; for ( unsigned int i = 0; i < ARRAY_SIZE(riscv_isa_ext); i++ ) { const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];... ret's scope to this loop. This then also justifies total to be of a signed type.
It makes sense.
Quoted 27 lines
Quoted 27 lines
if ( !riscv_isa_extension_available(isa_bitmap, ext->id) ) continue; ret = snprintf(buf ? buf + total : NULL, buf ? size - total : 0, "%s%s", ext->id >= RISCV_ISA_EXT_BASE ? "_" : "", ext->name); if ( ret < 0 ) return ret; total += ret; } return total; }int
init_guest_isa(struct domain *d){ int len; bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); len = build_guest_isa_str(NULL, 0, d->arch.isa); if ( len < 0 ) return len;
Quoted 7 lines
Quoted 5 lines
d->arch.isa_str = xmalloc_array(char, len + 1); if ( !d->arch.isa_str ) return -ENOMEM; build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);At least
ASSERT()the success of this?
I will add:
ASSERT(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) == len);
but I expect that we won't be here if len is incorrect or d->arch.isa_str wasn't allocated properly.
Thanks.
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 17 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
int
init_guest_isa(struct domain *d){ int len; bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); len = build_guest_isa_str(NULL, 0, d->arch.isa); if ( len < 0 ) return len; d->arch.isa_str = xmalloc_array(char, len + 1); if ( !d->arch.isa_str ) return -ENOMEM;
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);At least
ASSERT()the success of this?I will add:
ASSERT(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) == len);
Ehem. Please check how ASSERT() works (and the difference to BUG_ON()).
Jan
Quoted 6 lines
but I expect that we won't be here if len is incorrect or
d->arch.isa_strwasn't allocated properly.Thanks.
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 16 lines · Oleksii Kurochko
int
init_guest_isa(struct domain *d){ int len; bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); len = build_guest_isa_str(NULL, 0, d->arch.isa); if ( len < 0 ) return len; d->arch.isa_str = xmalloc_array(char, len + 1); if ( !d->arch.isa_str ) return -ENOMEM; build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
At least
ASSERT()the success of this?I will add:
ASSERT(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) == len);Ehem. Please check how
ASSERT()works (and the difference toBUG_ON()).
Condition itself looks correct for ASSERT(). If build_guest_isa_str() returns value equal to len then assert_failed() shouldn't be called.
Maybe do you mean that it will never fire in release build then yes it should be changed to BUG_ON() and the condition should be inverted:
BUG_ON(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) != len);
~ Oleksii
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 24 lines · Oleksii Kurochko
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
int
init_guest_isa(struct domain *d){ int len; bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); len = build_guest_isa_str(NULL, 0, d->arch.isa); if ( len < 0 ) return len;Quoted 7 lines
Quoted 5 lines
d->arch.isa_str = xmalloc_array(char, len + 1); if ( !d->arch.isa_str ) return -ENOMEM; build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);At least
ASSERT()the success of this?I will add:
ASSERT(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) == len);
Quoted 8 lines
Quoted 1 line
Ehem. Please check how
ASSERT()works (and the difference toBUG_ON()).Condition itself looks correct for
ASSERT(). Ifbuild_guest_isa_str()returns value equal to len thenassert_failed()shouldn't be called.Maybe do you mean that it will never fire in release build then yes it should be changed to
BUG_ON()and the condition should be inverted:
BUG_ON(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) != len);
No. Unlike in BUG_ON(), you can't use expressions with side effects (i.e. also function calls, unless they're const/pure) in ASSERT(). That's true for standard C's assert() as well, i.e. not Xen specific at all. (We do, however, diverge from assert() in another aspect.)
Jan
Re: [PATCH v2 05/26] xen/riscv: introduce guest riscv,isa string
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Oleksii Kurochko
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 16 lines · Oleksii Kurochko
int
init_guest_isa(struct domain *d){ int len; bitmap_andnot(d->arch.isa, riscv_isa, guest_unsupp, RISCV_ISA_EXT_MAX); len = build_guest_isa_str(NULL, 0, d->arch.isa); if ( len < 0 ) return len; d->arch.isa_str = xmalloc_array(char, len + 1); if ( !d->arch.isa_str ) return -ENOMEM; build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa);Quoted 7 lines
Quoted 5 lines
Quoted 1 line
At least
ASSERT()the success of this?I will add:
ASSERT(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) == len);Ehem. Please check how
ASSERT()works (and the difference toBUG_ON()).Condition itself looks correct for
ASSERT(). Ifbuild_guest_isa_str()returns value equal to len thenassert_failed()shouldn't be called.
Quoted 8 lines
Quoted 3 lines
Maybe do you mean that it will never fire in release build then yes it should be changed to
BUG_ON()and the condition should be inverted:BUG_ON(build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) != len);No. Unlike in
BUG_ON(), you can't use expressions with side effects (i.e. also function calls, unless they're const/pure) inASSERT(). That's true for standard C'sassert()as well, i.e. not Xen specific at all. (We do, however, diverge fromassert()in another aspect.)
Got you.
Also it could be in release build that build_guest_isa_str() won't be called at all because of how ASSERT() is open-coded: if ( 0 && (p) ...
Then probably it would be better to do in the following way:
if ( build_guest_isa_str(d->arch.isa_str, len + 1, d->arch.isa) != len )
{
XVFREE(d->arch.isa_str);
return -EINVAL;
}return 0;
I am not sure that -EINVAL is the best one option but I don't see any better now.
~ Oleksii
Re: [PATCH v2 08/26] xen/riscv: implement make_arch_nodes()
In reply to Oleksii KurochkoQuoted 8 lines · Oleksii Kurochko
No RISC-V-specific nodes need to be created at the moment, so
make_arch_nodes()is implemented to simply return 0.It is placed in
dom0less-build.casmake_arch_nodes()is only used in the dom0less code path. In the future, it will be extended to create an emulated UART node.Signed-off-by Oleksii Kurochko <[email protected]>
Re: [PATCH v2 09/26] xen/riscv: introduce init interrupt controller operations
In reply to Oleksii KurochkoQuoted 20 lines · Oleksii Kurochko
Introduce
intc_hw_init_opsstructure to avoid risky mix of init function and non-init function.Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- New patch.--- xen/arch/riscv/aplic.c | 7 +++++-- xen/arch/riscv/include/asm/intc.h | 10 +++++++--- xen/arch/riscv/intc.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-)xen/arch/riscv/aplic.c +0
-0@@ -306,12 +306,15 @@ static const hw_irq_controller aplic_xen_irq_type = {static const struct intc_hw_operations aplic_ops = {
xen/arch/riscv/aplic.c static const hw_irq_controller aplic_xen_irq_type = { · line 316
Quoted 10 lines
xen/arch/riscv/aplic.c +3
-1.info = &aplic_info, - .init = aplic_init, .host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, }; +static const struct intc_hw_init_ops __initdata aplic_init_ops = { + .init = aplic_init, +};
const wants to pair with __initconst. Then:
However, I have another comment for consideration:
Quoted 5 lines
xen/arch/riscv/intc.c +0
-0@@ -12,9 +12,13 @@static const struct intc_hw_operations *__ro_after_init intc_hw_ops;
xen/arch/riscv/intc.c line 22
Quoted 10 lines
xen/arch/riscv/intc.c +5
-1-void __init register_intc_ops (const struct intc_hw_operations *ops) +static const struct intc_hw_init_ops *__initdata intc_hw_init_ops; + +void __init register_intc_ops (const struct intc_hw_operations *ops, +const struct intc_hw_init_ops *init_ops) { intc_hw_ops = ops; + intc_hw_init_ops = init_ops; }
Again following what we do e.g. in x86'es IOMMU code, instead of passing two pointers to the function, have struct intc_hw_init_ops have a const struct intc_hw_operations * member which then can be used to set intc_hw_ops here? Both will always come in pairs anyway.
Jan
Re: [PATCH v2 10/26] xen/riscv: implement make_intc_domU_node()
In reply to Oleksii Kurochkoxen/arch/riscv/include/asm/intc.h struct intc_hw_init_ops { · line 54
Quoted 11 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/intc.h +7
-0@@ -46,6 +45,15 @@ struct intc_hw_init_ops {int (*init)(void ); }; +struct vintc_init_ops { +/* Create interrupt controller node for domain */ +int (*make_domu_dt_node)(struct kernel_info *kinfo); +}; + +struct vintc { +struct vintc_init_ops *init_ops;
Pointer-to-const?
xen/arch/riscv/intc.c void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority) · line 82
Quoted 10 lines
xen/arch/riscv/intc.c +6
-0@@ -73,3 +74,13 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)intc_set_irq_type (desc, desc->arch.type);intc_set_irq_priority (desc, priority); } + +int __init make_intc_domU_node (struct kernel_info *kinfo) +{ +struct vintc *vintc = kinfo->bd.d->arch.vintc; + +if ( intc_hw_ops && vintc->init_ops && vintc->init_ops->make_domu_dt_node )
Why the intc_hw_ops check? The variable isn't otherwise used here.
As to the other two checks: Is it plausible for either of the two pointers to be NULL? If not, merely ASSERT() or even build upon ...
xen/arch/riscv/intc.c void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority) · line 83
Quoted 1 line
xen/arch/riscv/intc.c +1
-0+return vintc->init_ops->make_domu_dt_node (kinfo);
... NULL derefs to fault anyway?
Jan
Re: [PATCH v2 11/26] xen/riscv: introduce aia_init() and aia_usable()
In reply to Oleksii KurochkoQuoted 8 lines · Oleksii Kurochko
aia_init()is going to contain all the logic related to AIA initialization.At the moment, it only checks whether the SSAIA extension is available, and if so, sets
is_aia_usable(which indicates more than just the availability of the extension) to true; it also signifies that the necessary components (to be introduced in follow-up patches) have been initialized.Signed-off-by Oleksii Kurochko <[email protected]>
albeit again with a comment for consideration:
xen/arch/riscv/aia.c line 10
Quoted 13 lines
xen/arch/riscv/aia.c new file +10
-0@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <xen/errno.h> +#include <xen/init.h> +#include <xen/sections.h> +#include <xen/types.h> + +#include <asm/cpufeature.h> + +static bool __ro_after_init is_aia_usable;
While in an if() the is_ prefix may make sense, in most other uses (all of the ones below, imo) it is of questionable value. I'd suggest to drop it, but of course with the then leading underscore retained, to ...
xen/arch/riscv/aia.c line 15
Quoted 4 lines
xen/arch/riscv/aia.c new file +4
-0+bool aia_usable (void ) +{ +return is_aia_usable; +}
... not conflict with this function.
Jan
Re: [PATCH v2 12/26] xen/riscv: add basic VGEIN management for AIA guests
In reply to Oleksii KurochkoQuoted 23 lines · Oleksii Kurochko
xen/arch/riscv/aia.c +12
-0@@ -1,11 +1,33 @@/* SPDX-License-Identifier: GPL-2.0-only */ +#include <xen/bitmap.h> +#include <xen/cpu.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> +#include <xen/sched.h> +#include <xen/spinlock.h> #include <xen/types.h> +#include <xen/xvmalloc.h> +#include <asm/aia.h> #include <asm/cpufeature.h> +#include <asm/csr.h> +#include <asm/current.h> + +struct vgein_ctrl { +unsigned long bmp; +spinlock_t lock;
xen/arch/riscv/aia.c line 30
Quoted 10 lines
xen/arch/riscv/aia.c +10
-0+struct vcpu **owners; +/* The least-significant bits are implemented first, apart from bit 0 */ +unsigned int geilen; +}; + +/* +* Bitmap for each physical cpus to detect which VS (guest) +* interrupt file id was used. +*/ +static DEFINE_PER_CPU(struct vgein_ctrl, vgein);
Why "Bitmap" in the comment?
xen/arch/riscv/aia.c bool aia_usable(void) · line 47
Quoted 13 lines
xen/arch/riscv/aia.c +9
-0@@ -14,10 +36,132 @@ bool aia_usable(void)return is_aia_usable; } +static int vgein_init (unsigned int cpu) +{ +struct vgein_ctrl *vgein = &per_cpu (vgein, cpu); + +csr_write (CSR_HGEIE ,-1UL ); + vgein->geilen =flsl (csr_read (CSR_HGEIE ) >>1 ); +csr_write (CSR_HGEIE ,0 ); + +printk ("cpu%d.geilen=%u\n" , cpu, vgein->geilen);
%u please with unsigned int.
Quoted 14 lines
xen/arch/riscv/aia.c +14
-0+if ( !vgein->geilen ) +return -EOPNOTSUPP ; + + vgein->owners = xvzalloc_array(struct vcpu *, vgein->geilen); +if ( !vgein->owners ) +return -ENOMEM ; + +spin_lock_init (&vgein->lock); + +return 0 ; +} + +static int cf_check cpu_callback(struct notifier_block *nfb, unsigned long action, +void *hcpu)
xen/arch/riscv/aia.c bool aia_usable(void) · line 72
Quoted 10 lines
xen/arch/riscv/aia.c +10
-0+{ +unsigned int cpu = (unsigned long )hcpu; +int rc =0 ; + +switch ( action ) + { +case CPU_STARTING : + rc =vgein_init (cpu); +if ( rc ) +printk ("AIA: failed to init vgein for CPU%un" , cpu);
Looks like there's \ missing ahead of the trailing 'n'.
Quoted 13 lines
xen/arch/riscv/aia.c +11
-0+break ; + } + +return notifier_from_errno (rc); +} + +static struct notifier_block cpu_nfb = { + .notifier_call = cpu_callback, +}; +void __init aia_init (void ) { +int rc;
xen/arch/riscv/aia.c bool aia_usable(void) · line 95
Quoted 10 lines
xen/arch/riscv/aia.c +9
-0+if ( !riscv_isa_extension_available (NULL , RISCV_ISA_EXT_ssaia) ) + { +dprintk (XENLOG_WARNING ,"SSAIA isn't present in riscv,isa\n" ); +return ; + } + +if ( (rc =vgein_init (0 )) ) + { +dprintk (XENLOG_ERR ,"vgein_init() failed with rc(%d)\n" , rc);
Messages like this one, provided they're really needed, should be yet more terse imo: "vgein_init() failed: %d\n".
Quoted 34 lines
xen/arch/riscv/aia.c +31
-0return ; + } is_aia_usable = true; + +register_cpu_notifier (&cpu_nfb); +} + +unsigned int vgein_assign (struct vcpu *v) +{ +unsigned int vgein_id; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); +unsigned long *bmp = &vgein->bmp; +unsigned long flags; + +spin_lock_irqsave (&vgein->lock, flags); +/* +* The vgein_id shouldn't be zero, as it will indicate that no guest +* external interrupt source is selected for VS-level external interrupts +* according to RISC-V priviliged spec: +* Hypervisor Status Register (hstatus) in RISC-V priviliged spec: +* +* The VGEIN (Virtual Guest External Interrupt Number) field selects +* a guest external interrupt source for VS-level external interrupts. +* VGEIN is a WLRL field that must be able to hold values between zero +* and the maximum guest external interrupt number (known as GEILEN), +* inclusive. +* When VGEIN=0, no guest external interrupt source is selected for +* VS-level external interrupts. +* +* So start to search from bit number 1. +*/ + vgein_id =find_next_zero_bit (bmp, vgein->geilen +1 ,1 ); +
xen/arch/riscv/aia.c bool aia_usable(void) · line 139
Quoted 10 lines
xen/arch/riscv/aia.c +10
-0+if ( vgein_id > vgein->geilen ) + vgein_id =0 ; +else +__set_bit (vgein_id, bmp); + +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: %pv: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, v, vgein_id, v->processor, *bmp);
%d vs unsigned int again (and then yet again further down).
xen/arch/riscv/aia.c bool aia_usable(void) · line 142
Quoted 3 lines
xen/arch/riscv/aia.c +3
-0+#endif + +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ;
Is this needed when vgein_release() also does it?
Quoted 15 lines
xen/arch/riscv/aia.c +15
-0+vcpu_guest_cpu_user_regs (v)->hstatus |= +MASK_INSR (vgein_id,HSTATUS_VGEIN ); + +return vgein_id; +} + +void vgein_release (struct vcpu *v,unsigned int vgen_id) +{ +unsigned long flags; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); + +if ( !vgen_id ) +return ; + +spin_lock_irqsave (&vgein->lock, flags);
xen/arch/riscv/aia.c bool aia_usable(void) · line 167
Quoted 10 lines
xen/arch/riscv/aia.c +9
-0+__clear_bit (vgen_id, &vgein->bmp); +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, vgen_id, v->processor, vgein->bmp); +#endif + +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ; }
Overall: How is one to review these two functions, when it's entirely unclear where they're going to be called from? Among other aspects it doesn't become clear what the behavior is going to be when vgein_assign() doesn't find an available ID. I've therefore only commented on mechanical aspects I noticed.
Jan
Re: [PATCH v2 13/26] xen/riscv: introduce per-vCPU IMSIC state
In reply to Oleksii KurochkoQuoted 11 lines · Oleksii Kurochko
xen/arch/riscv/imsic.c +1
-0@@ -16,6 +16,7 @@#include <xen/errno.h> #include <xen/init.h> #include <xen/macros.h> +#include <xen/sched.h> #include <xen/smp.h> #include <xen/spinlock.h> #include <xen/xvmalloc.h>
xen/arch/riscv/imsic.c do { \ · line 65
Quoted 9 lines
xen/arch/riscv/imsic.c +6
-0@@ -56,6 +57,16 @@ do { \csr_clear (CSR_SIREG , v); \ }while (0 ) +unsigned int vcpu_guest_file_id(const struct vcpu *v) +{ +return ACCESS_ONCE (v->arch.vimsic_state->guest_file_id); +} + +void imsic_set_guest_file_id (const struct vcpu *v,unsigned int guest_file_id)
Some people will demand that "const" be omitted in cases like this one, as it only works ...
xen/arch/riscv/imsic.c do { \ · line 67
Quoted 2 lines
xen/arch/riscv/imsic.c +2
-0+{ +ACCESS_ONCE (v->arch.vimsic_state->guest_file_id) = guest_file_id;
... as long as vimsic_state is a pointer (and not a sub-structure).
Quoted 16 lines
xen/arch/riscv/imsic.c +12
-0@@ -312,6 +323,30 @@ static int imsic_parse_node(const struct dt_device_node *node,return 0 ; } +int vcpu_imsic_init (struct vcpu *v) +{ +struct vimsic_state *imsic_state; + +/* Allocate IMSIC context */ + imsic_state = xvzalloc(struct vimsic_state); +if ( !imsic_state ) +return -ENOMEM ; + + v->arch.vimsic_state = imsic_state; + +/* Setup IMSIC context */
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 347
Quoted 10 lines
xen/arch/riscv/imsic.c +10
-0+rwlock_init (&imsic_state->vsfile_lock); + + imsic_state->vsfile_pcpu =NR_CPUS ; + +return 0 ; +} + +void vcpu_imsic_deinit (const struct vcpu *v) +{ +xvfree (v->arch.vimsic_state);
Better XVFREE(), for the function to be idempotent.
Quoted 18 lines
xen/arch/riscv/include/asm/imsic.h +5
-0@@ -11,6 +11,7 @@#ifndef ASM_RISCV_IMSIC_H #define ASM_RISCV_IMSIC_H +#include <xen/rwlock.h> #include <xen/spinlock.h> #include <xen/stdbool.h> #include <xen/types.h> @@ -61,7 +62,24 @@ struct imsic_config {spinlock_t lock; }; +struct vimsic_state { +/* IMSIC VS-file */ +rwlock_t vsfile_lock; +/*
xen/arch/riscv/include/asm/imsic.h struct imsic_config { · line 78
Quoted 10 lines
xen/arch/riscv/include/asm/imsic.h +10
-0+* (guest_file_id == 0) -> s/w IMSIC SW-file +* (guest_file_id > 0) -> h/w IMSIC VS-file +*/ +unsigned int guest_file_id; +/* +* (vsfile_pcpu >= 0) => h/w IMSIC VS-file +* (vsfile_pcpu == NR_CPUS) => s/w IMSIC SW-file +*/ +unsigned int vsfile_pcpu; +};
In the comments, what does SW stand for? Not "software" I assume, as that's already expressed by s/w.
Jan
Re: [PATCH v2 09/26] xen/riscv: introduce init interrupt controller operations
In reply to Jan BeulichQuoted 28 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Introduce
intc_hw_init_opsstructure to avoid risky mix of init function and non-init function.Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- New patch.--- xen/arch/riscv/aplic.c | 7 +++++-- xen/arch/riscv/include/asm/intc.h | 10 +++++++--- xen/arch/riscv/intc.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-)xen/arch/riscv/aplic.c +0
-1@@ -306,12 +306,15 @@ static const hw_irq_controller aplic_xen_irq_type = {static const struct intc_hw_operations aplic_ops = { .info = &aplic_info, - .init = aplic_init, .host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, };
Quoted 7 lines
xen/arch/riscv/aplic.c static const hw_irq_controller aplic_xen_irq_type = { · line 316
Quoted 3 lines
xen/arch/riscv/aplic.c +3
-0+static const struct intc_hw_init_ops __initdata aplic_init_ops = { + .init = aplic_init, +};const wants to pair with
__initconst. Then:Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 8 lines
However, I have another comment for consideration:
Quoted 6 lines
xen/arch/riscv/intc.c +0
-0@@ -12,9 +12,13 @@static const struct intc_hw_operations *__ro_after_init intc_hw_ops;
Quoted 14 lines
xen/arch/riscv/intc.c line 22
Quoted 9 lines
xen/arch/riscv/intc.c +5
-1-void __init register_intc_ops (const struct intc_hw_operations *ops) +static const struct intc_hw_init_ops *__initdata intc_hw_init_ops; + +void __init register_intc_ops (const struct intc_hw_operations *ops, +const struct intc_hw_init_ops *init_ops) { intc_hw_ops = ops; + intc_hw_init_ops = init_ops; }Again following what we do e.g. in x86'es IOMMU code, instead of passing two pointers to the function, have struct
intc_hw_init_opshave a const structintc_hw_operations* member which then can be used to setintc_hw_opshere? Both will always come in pairs anyway.
It makes sense to me.
So I will do the following:
@@ -350,7 +351,7 @@ static int __init aplic_preinit(struct dt_device_node *node, const void *dat)
dt_irq_xlate = aplic_irq_xlate;
- register_intc_ops(&aplic_ops, &aplic_init_ops);
+ register_intc_ops(&aplic_init_ops);
/* Enable supervisor external interrupt */
csr_set(CSR_SIE, BIT(IRQ_S_EXT, UL));xen/arch/riscv/include/asm/intc.h +2 -2
@@ -42,14 +42,14 @@ struct intc_hw_operations { };struct intc_hw_init_ops { +const struct intc_hw_operations *ops;/* Initialize the intc and the boot CPU */ int (*init)(void ); };void intc_preinit (void ); -void register_intc_ops (const struct intc_hw_operations *ops, -const struct intc_hw_init_ops *init_ops); +void register_intc_ops (const struct intc_hw_init_ops *init_ops);void intc_init (void );
xen/arch/riscv/intc.c +2 -3
@@ -14,10 +14,9 @@ static const struct intc_hw_operations *__ro_after_init intc_hw_ops;static const struct intc_hw_init_ops *__initdata intc_hw_init_ops; -void __init register_intc_ops (const struct intc_hw_operations *ops, -const struct intc_hw_init_ops *init_ops) +void __init register_intc_ops (const struct intc_hw_init_ops *init_ops) { - intc_hw_ops = ops; + intc_hw_ops = init_ops->ops; intc_hw_init_ops = init_ops; }
Thanks.
~ Oleksii
Re: [PATCH v2 10/26] xen/riscv: implement make_intc_domU_node()
In reply to Jan BeulichQuoted 28 lines · Jan Beulich
xen/arch/riscv/include/asm/intc.h struct intc_hw_init_ops { · line 54
Quoted 11 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/intc.h +7
-0@@ -46,6 +45,15 @@ struct intc_hw_init_ops {int (*init)(void ); }; +struct vintc_init_ops { +/* Create interrupt controller node for domain */ +int (*make_domu_dt_node)(struct kernel_info *kinfo); +}; + +struct vintc { +struct vintc_init_ops *init_ops;Pointer-to-const?
xen/arch/riscv/intc.c void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority) · line 82
Quoted 10 lines
xen/arch/riscv/intc.c +6
-0@@ -73,3 +74,13 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)intc_set_irq_type (desc, desc->arch.type);intc_set_irq_priority (desc, priority); } + +int __init make_intc_domU_node (struct kernel_info *kinfo) +{ +struct vintc *vintc = kinfo->bd.d->arch.vintc; + +if ( intc_hw_ops && vintc->init_ops && vintc->init_ops->make_domu_dt_node )Why the
intc_hw_opscheck? The variable isn't otherwise used here.
Quoted 6 lines
As to the other two checks: Is it plausible for either of the two pointers to be NULL? If not, merely
ASSERT()or even build upon ...xen/arch/riscv/intc.c void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority) · line 83
Quoted 1 line
xen/arch/riscv/intc.c +1
-0+return vintc->init_ops->make_domu_dt_node (kinfo);... NULL derefs to fault anyway?
Specifically this pointers can't be NULL.
Generally it would be okay to just have have NULL derefs in runtime but there isn't guarantee that some register which will be useful to debug where NULL deref happens will be properly filled. I faced that several times so I prefer to have here ASSERT().
Thanks.
~ Oleksii
Re: [PATCH v2 11/26] xen/riscv: introduce aia_init() and aia_usable()
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko
aia_init()is going to contain all the logic related to AIA initialization.At the moment, it only checks whether the SSAIA extension is available, and if so, sets
is_aia_usable(which indicates more than just the availability of the extension) to true; it also signifies that the necessary components (to be introduced in follow-up patches) have been initialized.Signed-off-by Oleksii Kurochko <[email protected]>Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 20 lines
albeit again with a comment for consideration:
xen/arch/riscv/aia.c line 10
Quoted 13 lines
xen/arch/riscv/aia.c new file +10
-0@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <xen/errno.h> +#include <xen/init.h> +#include <xen/sections.h> +#include <xen/types.h> + +#include <asm/cpufeature.h> + +static bool __ro_after_init is_aia_usable;While in an
if()theis_prefix may make sense, in most other uses (all of the ones below, imo) it is of questionable value. I'd suggest to drop it, but of course with the then leading underscore retained, to ...
Quoted 7 lines
xen/arch/riscv/aia.c line 15
Quoted 4 lines
xen/arch/riscv/aia.c new file +4
-0+bool aia_usable (void ) +{ +return is_aia_usable; +}... not conflict with this function.
IIUC then I am okay with the following changes:
-static bool __ro_after_init is_aia_usable;
+static bool __ro_after_init _aia_usable;
bool aia_usable(void)
{
- return is_aia_usable;
+ return _aia_usable;
}
void __init aia_init(void)
@@ -19,5 +19,5 @@ void __init aia_init(void)
if ( !riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ssaia) )
return;
- is_aia_usable = true;
+ _aia_usable = true;
}
~ Oleksii
Re: [PATCH v2 12/26] xen/riscv: add basic VGEIN management for AIA guests
In reply to Jan BeulichQuoted 28 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
xen/arch/riscv/aia.c +16
-0@@ -1,11 +1,33 @@/* SPDX-License-Identifier: GPL-2.0-only */ +#include <xen/bitmap.h> +#include <xen/cpu.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> +#include <xen/sched.h> +#include <xen/spinlock.h> #include <xen/types.h> +#include <xen/xvmalloc.h> +#include <asm/aia.h> #include <asm/cpufeature.h> +#include <asm/csr.h> +#include <asm/current.h> + +struct vgein_ctrl { +unsigned long bmp; +spinlock_t lock; +struct vcpu **owners; +/* The least-significant bits are implemented first, apart from bit 0 */ +unsigned int geilen; +};
Quoted 8 lines
xen/arch/riscv/aia.c line 30
Quoted 6 lines
xen/arch/riscv/aia.c +6
-0+ +/* +* Bitmap for each physical cpus to detect which VS (guest) +* interrupt file id was used. +*/ +static DEFINE_PER_CPU(struct vgein_ctrl, vgein);Why "Bitmap" in the comment?
Hmm, good question. Looks like rudiment from initial implementation.
I will rephrase it to:
/*
* VGEIN control structure for each physical CPU to track which VS (guest)
* interrupt file IDs are in use.
*/
Quoted 40 lines
Quoted 27 lines
xen/arch/riscv/aia.c +27
-0+ +unsigned int vgein_assign (struct vcpu *v) +{ +unsigned int vgein_id; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); +unsigned long *bmp = &vgein->bmp; +unsigned long flags; + +spin_lock_irqsave (&vgein->lock, flags); +/* +* The vgein_id shouldn't be zero, as it will indicate that no guest +* external interrupt source is selected for VS-level external interrupts +* according to RISC-V priviliged spec: +* Hypervisor Status Register (hstatus) in RISC-V priviliged spec: +* +* The VGEIN (Virtual Guest External Interrupt Number) field selects +* a guest external interrupt source for VS-level external interrupts. +* VGEIN is a WLRL field that must be able to hold values between zero +* and the maximum guest external interrupt number (known as GEILEN), +* inclusive. +* When VGEIN=0, no guest external interrupt source is selected for +* VS-level external interrupts. +* +* So start to search from bit number 1. +*/ + vgein_id =find_next_zero_bit (bmp, vgein->geilen +1 ,1 ); +xen/arch/riscv/aia.c bool aia_usable(void) · line 139
Quoted 10 lines
xen/arch/riscv/aia.c +10
-0+if ( vgein_id > vgein->geilen ) + vgein_id =0 ; +else +__set_bit (vgein_id, bmp); + +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: %pv: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, v, vgein_id, v->processor, *bmp);%d vs unsigned int again (and then yet again further down).
Quoted 5 lines
xen/arch/riscv/aia.c bool aia_usable(void) · line 142
Quoted 3 lines
xen/arch/riscv/aia.c +3
-0+#endif + +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ;Is this needed when
vgein_release()also does it?
Considering how ->hstatus is initialized I agree that we don't need that here.
Quoted 24 lines
Quoted 24 lines
xen/arch/riscv/aia.c +23
-0+vcpu_guest_cpu_user_regs (v)->hstatus |= +MASK_INSR (vgein_id,HSTATUS_VGEIN ); + +return vgein_id; +} + +void vgein_release (struct vcpu *v,unsigned int vgen_id) +{ +unsigned long flags; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); + +if ( !vgen_id ) +return ; + +spin_lock_irqsave (&vgein->lock, flags); +__clear_bit (vgen_id, &vgein->bmp); +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: vgein_id(%u), xen_cpu%d_bmp=%#lx\n" , + __func__, vgen_id, v->processor, vgein->bmp); +#endif +
Quoted 8 lines
Quoted 2 lines
+ vcpu_guest_cpu_user_regs(v)->hstatus &= ~HSTATUS_VGEIN;
}Overall: How is one to review these two functions, when it's entirely unclear where they're going to be called from? Among other aspects it doesn't become clear what the behavior is going to be when
vgein_assign()doesn't find an available ID. I've therefore only commented on mechanical aspects I noticed.
It is used in "[PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support" from this patch series. I realize that was not obvious, sorry for the omission.
That said, I am planning to move all AIA-related logic out of vcpu_vaplic_init() and into continue_new_vcpu(), because some IMSIC-related operations require knowing which physical CPU the vCPU will be scheduled on, and moving the work there avoids redundant recalculation. As a result, vgein_assign() would no longer be called from vcpu_vaplic_init(), so I could drop this patch from the series entirely and reintroduce it when it is actually needed.
Alternatively, I can keep the patch and extend the commit message to explain the intended call site and how the return value will be handled.
Thanks.
~ Oleksii
Re: [PATCH v2 13/26] xen/riscv: introduce per-vCPU IMSIC state
In reply to Jan BeulichQuoted 25 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
xen/arch/riscv/imsic.c +1
-0@@ -16,6 +16,7 @@#include <xen/errno.h> #include <xen/init.h> #include <xen/macros.h> +#include <xen/sched.h> #include <xen/smp.h> #include <xen/spinlock.h> #include <xen/xvmalloc.h> xen/arch/riscv/imsic.c do { \ · line 65
Quoted 9 lines
xen/arch/riscv/imsic.c +6
-0@@ -56,6 +57,16 @@ do { \csr_clear (CSR_SIREG , v); \ }while (0 ) +unsigned int vcpu_guest_file_id(const struct vcpu *v) +{ +return ACCESS_ONCE (v->arch.vimsic_state->guest_file_id); +} + +void imsic_set_guest_file_id (const struct vcpu *v,unsigned int guest_file_id)Some people will demand that "const" be omitted in cases like this one, as it only works ...
Quoted 4 lines
xen/arch/riscv/imsic.c do { \ · line 67
Quoted 2 lines
xen/arch/riscv/imsic.c +2
-0+{ +ACCESS_ONCE (v->arch.vimsic_state->guest_file_id) = guest_file_id;... as long as
vimsic_stateis a pointer (and not a sub-structure).
Agree, it works only because of a pointer is used. Lets ommit const then here.
Quoted 21 lines
Quoted 21 lines
xen/arch/riscv/imsic.c +16
-0@@ -312,6 +323,30 @@ static int imsic_parse_node(const struct dt_device_node *node,return 0 ; } +int vcpu_imsic_init (struct vcpu *v) +{ +struct vimsic_state *imsic_state; + +/* Allocate IMSIC context */ + imsic_state = xvzalloc(struct vimsic_state); +if ( !imsic_state ) +return -ENOMEM ; + + v->arch.vimsic_state = imsic_state; + +/* Setup IMSIC context */ +rwlock_init (&imsic_state->vsfile_lock); + + imsic_state->vsfile_pcpu =NR_CPUS ; +
Quoted 8 lines
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 347
Quoted 6 lines
xen/arch/riscv/imsic.c +6
-0+return 0 ; +} + +void vcpu_imsic_deinit (const struct vcpu *v) +{ +xvfree (v->arch.vimsic_state);Better
XVFREE(), for the function to be idempotent.
Agree it would be better.
Quoted 24 lines
Quoted 24 lines
xen/arch/riscv/include/asm/imsic.h +10
-0@@ -11,6 +11,7 @@#ifndef ASM_RISCV_IMSIC_H #define ASM_RISCV_IMSIC_H +#include <xen/rwlock.h> #include <xen/spinlock.h> #include <xen/stdbool.h> #include <xen/types.h> @@ -61,7 +62,24 @@ struct imsic_config {spinlock_t lock; }; +struct vimsic_state { +/* IMSIC VS-file */ +rwlock_t vsfile_lock; +/* +* (guest_file_id == 0) -> s/w IMSIC SW-file +* (guest_file_id > 0) -> h/w IMSIC VS-file +*/ +unsigned int guest_file_id; +/*
Quoted 8 lines
xen/arch/riscv/include/asm/imsic.h struct imsic_config { · line 78
Quoted 5 lines
xen/arch/riscv/include/asm/imsic.h +5
-0+* (vsfile_pcpu >= 0) => h/w IMSIC VS-file +* (vsfile_pcpu == NR_CPUS) => s/w IMSIC SW-file +*/ +unsigned int vsfile_pcpu; +};In the comments, what does SW stand for? Not "software" I assume, as that's already expressed by s/w.
It is a typo and it should be VS-file.
Thanks.
~ Oleksii
Re: [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
In reply to Oleksii KurochkoQuoted 15 lines · Oleksii Kurochko
xen/arch/riscv/aplic.c +5
-0@@ -295,6 +295,11 @@ static void cf_check aplic_set_irq_type(struct irq_desc *desc,spin_unlock (&aplic.lock); } +static unsigned int cf_check aplic_irq_num(void) +{ +return aplic_info.num_irqs; +} +static const hw_irq_controller aplic_xen_irq_type = { .typename ="aplic" , .startup = aplic_irq_startup,
xen/arch/riscv/aplic.c static const struct intc_hw_operations aplic_ops = { · line 317
Quoted 4 lines
xen/arch/riscv/aplic.c +1
-0@@ -309,6 +314,7 @@ static const struct intc_hw_operations aplic_ops = { .host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, + .irq_nums = aplic_irq_num,
Hook handler names and respective field names would preferably match up. It's unclear why the field uses some kind of plural(?), while the function uses singular.
Quoted 15 lines
xen/arch/riscv/domain.c +1
-1@@ -11,6 +11,7 @@#include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> +#include <asm/intc.h> #include <asm/riscv_encoding.h> #include <asm/vtimer.h> @@ -155,14 +156,10 @@ int arch_vcpu_create(struct vcpu *v)if ( (rc =vcpu_vtimer_init (v)) ) goto fail; -/*
xen/arch/riscv/domain.c int arch_vcpu_create(struct vcpu *v) · line 161
Quoted 10 lines
xen/arch/riscv/domain.c +3
-7-* As interrupt controller (IC) is not yet implemented, -* return an error. -* -* TODO: Drop this once IC is implemented. -*/ - rc = -EOPNOTSUPP ; - goto fail; +ASSERT (v->domain->arch.vintc->ops->vcpu_init); + +if ( (rc = v->domain->arch.vintc->ops->vcpu_init (v)) )
I don't understand this model of the use of ASSERT(). As previously said (more than once) - you'll crash anyway if any of the involved pointers is NULL. If you really think an up-front check is better, then why would you check only the leaf of the pointer chain, and not also vintc and ops? (Once you do you'll then likely notice that there are more assertions than actual code.)
Quoted 13 lines
xen/arch/riscv/include/asm/vaplic.h new file +10
-0@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: MIT */ +/* +* xen/arch/riscv/vaplic.c +* +* Virtual RISC-V Advanced Platform-Level Interrupt Controller support +* +* Copyright (c) Microchip. +*/ + +#ifndef ASM__RISCV__VAPLIC_H
xen/arch/riscv/include/asm/vaplic.h line 20
Quoted 10 lines
xen/arch/riscv/include/asm/vaplic.h new file +10
-0+#define ASM__RISCV__VAPLIC_H + +#include <xen/kernel.h> +#include <xen/types.h> + +#include <asm/intc.h> + +struct domain ; + +#define to_vaplic (d) container_of(d->arch.vintc, struct vaplic, vintc)
d wants parenthesizing here.
xen/arch/riscv/intc.c void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority) · line 86
Quoted 12 lines
xen/arch/riscv/intc.c +6
-0@@ -78,6 +78,13 @@ void intc_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)intc_set_irq_priority (desc, priority); } +unsigned int intc_irq_nums (void ) +{ +ASSERT (intc_hw_ops && intc_hw_ops->irq_nums); + +return intc_hw_ops->irq_nums (); +}
You use this to set domains' properties. As indicated before, I view it as wrong to do so for any domain, besides perhaps Dom0 / hwdom. If you want to do so nevertheless, at the very least I'd expect something to be said about such a decision in the description.
Quoted 19 lines
xen/arch/riscv/vaplic.c new file +16
-0@@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: MIT */ +/* +* xen/arch/riscv/vaplic.c +* +* Virtual RISC-V Advanced Platform-Level Interrupt Controller support +* +* Copyright (c) Microchip. +* Copyright (c) Vates +*/ + +#include <xen/errno.h> +#include <xen/sched.h> +#include <xen/xvmalloc.h> + +#include <asm/aia.h> +#include <asm/imsic.h>
xen/arch/riscv/vaplic.c line 26
Quoted 10 lines
xen/arch/riscv/vaplic.c new file +10
-0+#include <asm/intc.h> +#include <asm/vaplic.h> + +#include "aplic-priv.h" + +#define VAPLIC_NUM_SOURCES 96 + +static int cf_check vcpu_vaplic_init(struct vcpu *v) +{ +int rc =0 ;
Nit: Pointless initializer.
Quoted 18 lines
xen/arch/riscv/vaplic.c new file +18
-0+unsigned int vgein_id; + + rc =vcpu_imsic_init (v); +if ( rc ) +return rc; + +if ( !(vgein_id =vgein_assign (v)) ) + { +printk ("Software interrupt files aren't supported\n" ); + rc = -EOPNOTSUPP ; + goto fail; + } + +imsic_set_guest_file_id (v, vgein_id); + +return rc; + + fail:
xen/arch/riscv/vaplic.c line 54
Quoted 10 lines
xen/arch/riscv/vaplic.c new file +10
-0+vcpu_imsic_deinit (v); + +return rc; +} + +static const struct vintc_ops vintc_ops = { + .vcpu_init = vcpu_vaplic_init, +}; + +int __init domain_vaplic_init (struct domain *d)
Why __init, and why is there no caller? Plus why is the vCPU-init a hook, but the domain init is not? Either you mean to allow for other ICs, or you you don't.
Quoted 14 lines
xen/arch/riscv/vaplic.c new file +14
-0+{ + struct vaplic *vaplic = xvzalloc(struct vaplic); + +if ( !vaplic ) +return -ENOMEM ; + + d->arch.vintc = &vaplic->vintc; + d->arch.vintc->ops = &vintc_ops; + + vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM ; + + d->arch.vintc->irq_nums =min (intc_irq_nums (), +VAPLIC_NUM_SOURCES +0U ); +
xen/arch/riscv/vaplic.c line 78
Quoted 10 lines
xen/arch/riscv/vaplic.c new file +10
-0+ +return 0 ; +} + +void __init domain_vaplic_deinit (struct domain *d) +{ +struct vaplic *vaplic =to_vaplic (d); + +xvfree (vaplic); +}
And d->arch.vintc turns into a dangling pointer. The way you arrange data types, you can't use XVFREE() here, but imo you really want to make sure the function is idempotent.
Jan
Re: [PATCH v2 15/26] xen/riscv: introduce (de)initialization helpers for vINTC
In reply to Oleksii KurochkoQuoted 11 lines · Oleksii Kurochko
xen/arch/riscv/intc.c +1
-0@@ -11,6 +11,7 @@#include <asm/aia.h> #include <asm/intc.h> +#include <asm/vaplic.h> static const struct intc_hw_operations *__ro_after_init intc_hw_ops;
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 102
Quoted 8 lines
xen/arch/riscv/intc.c +5
-0@@ -94,3 +95,38 @@ int __init make_intc_domU_node(struct kernel_info *kinfo)return -EOPNOTSUPP ; } + +int domain_vintc_init (struct domain *d) +{ +int ret = -EOPNOTSUPP ; +const enum intc_version ver = intc_hw_ops->info->hw_version;
Again - why would what the underlying hardware has control what all domains get?
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 111
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+switch ( ver ) + { +case INTC_APLIC : + ret =domain_vaplic_init (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);
If we take this path for whatever reason, ...
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+break ; + } + +return ret; +} + +void domain_vintc_deinit (struct domain *d) +{
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 129
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC : +domain_vaplic_deinit (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);
... we're also going to take this path (very quickly afterwards), just to get the same message twice without it being clear why it appears twice.
Jan
Re: [PATCH v2 16/26] xen/riscv: create APLIC DT node for guest domains
In reply to Oleksii KurochkoQuoted 11 lines · Oleksii Kurochko
xen/arch/riscv/imsic.c +1
-0@@ -14,6 +14,7 @@#include <xen/cpumask.h> #include <xen/device_tree.h> #include <xen/errno.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <xen/macros.h> #include <xen/sched.h>
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 531
Quoted 9 lines
xen/arch/riscv/imsic.c +6
-0@@ -522,3 +523,9 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } + +int __init vimsic_make_domu_dt_node (struct kernel_info *kinfo, +unsigned int *phandle) +{ +return -EOPNOTSUPP ; +}
This, I assume, is going to be filled properly by the next patch.
Quoted 24 lines
xen/arch/riscv/vaplic.c +5
-0@@ -9,6 +9,8 @@*/ #include <xen/errno.h> +#include <xen/fdt-kernel.h> +#include <xen/libfdt/libfdt.h> #include <xen/sched.h> #include <xen/xvmalloc.h> @@ -19,8 +21,11 @@#include "aplic-priv.h" +#define VAPLIC_COMPATIBLE "riscv,aplic"#define VAPLIC_NUM_SOURCES 96 +#define FDT_VAPLIC_INT_CELLS 2 + static int cf_check vcpu_vaplic_init(struct vcpu *v) {int rc =0 ;
xen/arch/riscv/vaplic.c static int cf_check vcpu_vaplic_init(struct vcpu *v) · line 55
Quoted 4 lines
xen/arch/riscv/vaplic.c +1
-0@@ -47,6 +52,73 @@ static int cf_check vcpu_vaplic_init(struct vcpu *v)return rc; } +static int __init cf_check vaplic_make_domu_dt_node(struct kernel_info *kinfo)
Again - why __init here and ...
Quoted 53 lines
xen/arch/riscv/vaplic.c +53
-0+{ +int res =0 ; +void *fdt = kinfo->fdt; +unsigned int msi_parent_phandle; +char vaplic_name[128 ]; +paddr_t aplic_addr =GUEST_APLIC_S_BASE ; +paddr_t aplic_size =APLIC_SIZE (kinfo->bd.d->max_vcpus); +const __be32 reg[] = { +cpu_to_be32 (aplic_addr >>32 ), +cpu_to_be32 (aplic_addr), +cpu_to_be32 (aplic_size >>32 ), +cpu_to_be32 (aplic_size), + }; +struct vintc *vintc = kinfo->bd.d->arch.vintc; + + res =snprintf (vaplic_name,sizeof (vaplic_name),"/soc/aplic@%x" , +GUEST_APLIC_S_BASE ); +if ( res >=sizeof (vaplic_name) ) + { +dprintk (XENLOG_DEBUG ,"vaplic name is truncated\n" ); +return -ENOBUFS ; + } + + res =vimsic_make_domu_dt_node (kinfo, &msi_parent_phandle); +if ( res ) +return res; + + res =fdt_begin_node (fdt, vaplic_name); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#interrupt-cells" ,FDT_VAPLIC_INT_CELLS ); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , reg,sizeof (reg)); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"riscv,num-sources" , vintc->irq_nums); +if ( res ) +return res; + + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,VAPLIC_COMPATIBLE ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"msi-parent" , msi_parent_phandle); +if ( res )
xen/arch/riscv/vaplic.c static int cf_check vcpu_vaplic_init(struct vcpu *v) · line 118
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+return res; + + res =fdt_property_cell (fdt,"phandle" , kinfo->phandle_intc); +if ( res ) +return res; + +return fdt_end_node (fdt); +} + +static const struct vintc_init_ops __initdata init_ops = {
... __initdata here? If you really want to have the option of moving domain creation stuff to .init.* when only dom0less is configured, then a proper abstraction is needed, along the lines of init_or_livepatch.
xen/arch/riscv/vaplic.c int __init domain_vaplic_init(struct domain *d) · line 138
Quoted 9 lines
xen/arch/riscv/vaplic.c +3
-1@@ -60,13 +132,14 @@ int __init domain_vaplic_init(struct domain *d) d->arch.vintc = &vaplic->vintc; d->arch.vintc->ops = &vintc_ops; + d->arch.vintc->init_ops = &init_ops; - vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM ; + vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM | +APLIC_DOMAINCFG_RO80 ;
This looks unrelated. I don't mind it being done here, but then it wants mentioning in the description. Or maybe I simply don't understand what this is about.
Jan
Re: [PATCH v2 17/26] xen/riscv: generate IMSIC DT node for guest domains
In reply to Oleksii KurochkoQuoted 45 lines · Oleksii Kurochko
Guests using the IMSIC interrupt controller require a corresponding Device Tree description.
Add support for generating an IMSIC node when building the guest DT. This allows guests to discover and use the IMSIC interrupt controller.
Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- s/imsic_make_reg_property/guest_imsic_make_reg_property.
- s/imsic_set_interrupt_extended_prop/guest_imsic_set_interrupt_extended_prop.
- Use initalizer for regs[] array inimsic_make_reg_property().
- Move buf[] insde thefor()loop.
- Correct check of returned phandle.
- Drop local variable len.
- /s/XVFREE/xvfree inimsic_set_interrupt_extended_prop().
- Drop initializer for local variable data.
- s/uint32_t/unsinged int for pos and cpu inimsic_set_interrupt_extended_prop().
- Dropnext_phandleas it is now in common code.
- Introducevcpu_imsic_deinit.
- Refactorvimsic_make_domu_dt_node()to avoid usage of host IMSIC dt node.--- xen/arch/riscv/imsic.c | 127 +++++++++++++++++++++- xen/arch/riscv/include/asm/guest-layout.h | 2 + 2 files changed, 128 insertions(+), 1 deletion(-)xen/arch/riscv/imsic.c +3
-0@@ -13,9 +13,12 @@#include <xen/const.h> #include <xen/cpumask.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/errno.h> +#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/macros.h> #include <xen/sched.h> #include <xen/smp.h>
xen/arch/riscv/imsic.c static struct imsic_config imsic_cfg = { · line 41
Quoted 4 lines
xen/arch/riscv/imsic.c +1
-0@@ -35,6 +38,11 @@ static struct imsic_config imsic_cfg = { .lock = SPIN_LOCK_UNLOCKED, }; +static unsigned int __ro_after_init guest_num_msis;
How come this is __ro_after_init, when it's ...
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 305
Quoted 8 lines
xen/arch/riscv/imsic.c +4
-0@@ -291,6 +299,11 @@ static int imsic_parse_node(const struct dt_device_node *node,return -ENOENT ; } +if (dt_property_read_u32 (node,"riscv,num-guest-ids" , &tmp) ) + guest_num_msis = tmp; +else + guest_num_msis = imsic_cfg.nr_ids;
... written by a non-__init function? Plus are you again inheriting a host property into guests without saying why?
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 540
Quoted 5 lines
xen/arch/riscv/imsic.c +1
-0@@ -524,8 +537,120 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } +static int __init guest_imsic_make_reg_property (struct domain *d,void *fdt)
Same question again as to __init throughout here.
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 542
Quoted 2 lines
xen/arch/riscv/imsic.c +2
-0+{ +paddr_t base_addr =GUEST_IMSIC_S_BASE ;
So you make a local variable for a constant, ...
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 547
Quoted 5 lines
xen/arch/riscv/imsic.c +5
-0+__be32 regs[4 ] = { +cpu_to_be32 (base_addr >>32 ), +cpu_to_be32 (base_addr), +cpu_to_be32 ((IMSIC_MMIO_PAGE_SZ * d->max_vcpus) >>32 ), +cpu_to_be32 (IMSIC_MMIO_PAGE_SZ * d->max_vcpus),
... but this non-constant expression is spelled out twice.
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 558
Quoted 6 lines
xen/arch/riscv/imsic.c +6
-0+static int __init guest_imsic_set_interrupt_extended_prop (struct domain *d, +void *fdt) +{ +unsigned int cpu, pos =0 ; +uint32_t phandle; +uint32_t *irq_ext;
Doesn't this want to be __be32, seeing ...
Quoted 11 lines
xen/arch/riscv/imsic.c +11
-0+int res; + + irq_ext =xvzalloc_array (uint32_t, d->max_vcpus *2 ); +if ( !irq_ext ) +return -ENOMEM ; + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ ) + { +char buf[64 ]; + +snprintf (buf,sizeof (buf),"/cpus/cpu@%u/interrupt-controller" , cpu);
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 579
Quoted 10 lines
xen/arch/riscv/imsic.c +10
-0+ phandle =fdt_get_phandle (fdt,fdt_path_offset (fdt, buf)); + +if ( !phandle ) + { + res = -ENODEV ; + goto out; + } + + irq_ext[pos++] =cpu_to_be32 (phandle); + irq_ext[pos++] =cpu_to_be32 (IRQ_S_EXT );
... this?
Also, just like "buf", "phandle" can be local to this loop's body.
xen/arch/riscv/include/asm/guest-layout.h line 10
Quoted 9 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0@@ -5,6 +5,8 @@#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_IMSIC_S_BASE 0x28000000 +#define GUEST_RAM_BANKS 2
Is this going to become an unannotated collection of (seemingly) random numbers?
Jan
Re: [PATCH v2 20/26] xen/riscv: add missing APLIC register offsets, masks to asm/aplic.h.
In reply to Oleksii Kurochkoxen/arch/riscv/include/asm/aplic.h line 18
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/aplic.h +1
-0@@ -15,6 +15,11 @@#include <asm/imsic.h> +#define APLIC_REG_OFFSET_MASK 0x3fff
This I can see this as wanting to live separately. Yet ...
xen/arch/riscv/include/asm/aplic.h line 21
Quoted 3 lines
xen/arch/riscv/include/asm/aplic.h +3
-0+#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_EIID_MASK 0x7ff
... what (set of) register(s) do these apply to? Perhaps ...
Quoted 23 lines
xen/arch/riscv/include/asm/aplic.h +19
-0@@ -26,6 +31,36 @@#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_LAST 0x0ffc + +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIP_LAST 0x1c7c +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIP_LAST 0x1d7c +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIE_LAST 0x1e7c +#define APLIC_SETIENUM 0x1edc +
xen/arch/riscv/include/asm/aplic.h line 62
Quoted 10 lines
xen/arch/riscv/include/asm/aplic.h +10
-0+#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIE_LAST 0x1f7c +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_LAST 0x3ffc
... these? And then is ...
xen/arch/riscv/include/asm/aplic.h line 64
Quoted 1 line
xen/arch/riscv/include/asm/aplic.h +0
-0#define APLIC_TARGET_HART_IDX_SHIFT 18
... this also covering some part of them? Can't they (a) live together and (b) have some kind of connection to what they apply to?
And then why is there again a mix of *_SHIFT and *_MASK?
Jan
Re: [PATCH v2 19/26] xen/riscv: implement IRQ routing for device passthrough
In reply to Oleksii KurochkoQuoted 12 lines · Oleksii Kurochko
xen/arch/riscv/device.c new file +9
-0@@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <xen/device_tree.h> +#include <xen/errno.h> +#include <xen/iocap.h> +#include <xen/rangeset.h> +#include <xen/sched.h> + +#include <asm/intc.h>
xen/arch/riscv/device.c line 19
Quoted 10 lines
xen/arch/riscv/device.c new file +10
-0+ +int map_irq_to_domain (struct domain *d,unsigned int irq, +bool need_mapping,const char *devname) +{ +int res; + + res =irq_permit_access (d, irq); +if ( res ) + { +printk (XENLOG_ERR "Unable to permit to %pd access to IRQ %u\n" , d, irq);
Nit: Drop the first "to"?
Quoted 31 lines
xen/arch/riscv/device.c new file +31
-0+return res; + } + +if ( need_mapping ) + { +/* +* Checking the return of vintc_reserve_virq is not +* necessary. It should not fail except when we try to map +* the IRQ twice. This can legitimately happen if the IRQ is shared. +*/ +vintc_reserve_virq (d, irq); + + res =route_irq_to_guest (d, irq, irq, devname); +if ( res <0 ) + { +printk (XENLOG_ERR "Unable to map IRQ%u to %pd\n" , irq, d); +return res; + } + } + +dt_dprintk (" - IRQ: %u\n" , irq); + +return 0 ; +} + +/* +* map_device_irqs_to_domain retrieves the interrupts configuration from +* a device tree node and maps those interrupts to the target domain. +* +* Returns: +* < 0 error
xen/arch/riscv/device.c line 60
Quoted 10 lines
xen/arch/riscv/device.c new file +10
-0+* 0 success +*/ +int map_device_irqs_to_domain (struct domain *d, +struct dt_device_node *dev, +bool need_mapping, +struct rangeset *irq_ranges) +{ +unsigned int i, nirq; +int res, irq; +struct dt_raw_irq rirq;
Move the latter three variables to the loop's scope and ...
xen/arch/riscv/device.c line 62
Quoted 1 line
xen/arch/riscv/device.c new file +1
-0+ nirq =dt_number_of_irq (dev);
... make this the variable's initializer?
Quoted 31 lines
xen/arch/riscv/device.c new file +31
-0+/* Give permission and map IRQs */ +for ( i =0 ; i < nirq; i++ ) + { + res =dt_device_get_raw_irq (dev, i, &rirq); +if ( res ) + { +printk (XENLOG_ERR "Unable to retrieve irq %u for %s\n" , + i,dt_node_full_name (dev)); +return res; + } + +/* +* Don't map IRQ that have no physical meaning +* ie: IRQ whose controller is not APLIC/IMSIC/PLIC. +*/ +if ( rirq.controller != dt_interrupt_controller ) + { +dt_dprintk ("irq %u not connected to primary controller." +"Connected to %s\n" , i, +dt_node_full_name (rirq.controller)); +continue ; + } + + irq =platform_get_irq (dev, i); +if ( irq <0 ) + { +printk ("Unable to get irq %u for %s\n" , i,dt_node_full_name (dev)); +return irq; + } + + res =map_irq_to_domain (d, irq, need_mapping,dt_node_name (dev));
xen/arch/riscv/device.c line 104
Quoted 10 lines
xen/arch/riscv/device.c new file +10
-0+if ( res ) +return res; + + +/* +* At the moment there is only one user of map_device_irqs_to_domain() +* for RISC-V which calls it irq_ranges == NULL. +*/ +if ( irq_ranges ) +return -EOPNOTSUPP ;
Why is this checked last, and inside the loop (when it's loop invariant)?
xen/arch/riscv/include/asm/intc.h enum intc_version { · line 16
Quoted 7 lines
xen/arch/riscv/include/asm/intc.h +1
-0@@ -13,8 +13,11 @@ enum intc_version { };struct cpu_user_regs ; +struct domain ;
I can spot why this is needed, but ...
xen/arch/riscv/include/asm/intc.h enum intc_version { · line 21
Quoted 5 lines
xen/arch/riscv/include/asm/intc.h +2
-0+struct dt_device_node ;struct irq_desc ;struct kernel_info ; +struct rangeset ;struct vcpu ;
... I'm at a loss to explain the need for these two additions.
xen/arch/riscv/include/asm/setup.h line 10
Quoted 9 lines
xen/arch/riscv/include/asm/setup.h +3
-0@@ -5,6 +5,10 @@#include <xen/types.h> +struct domain ; +struct dt_device_node ; +struct rangeset ;
Same here - why would they be needed when you make no other changes to this header?
Quoted 15 lines
xen/arch/riscv/intc.c +2
-0@@ -7,7 +7,9 @@#include <xen/init.h> #include <xen/irq.h> #include <xen/lib.h> +#include <xen/sched.h> #include <xen/spinlock.h> +#include <xen/xvmalloc.h> #include <asm/aia.h> #include <asm/intc.h> @@ -86,6 +88,22 @@ unsigned int intc_irq_nums(void)return intc_hw_ops->irq_nums (); }
xen/arch/riscv/intc.c unsigned int intc_irq_nums(void) · line 99
Quoted 10 lines
xen/arch/riscv/intc.c +9
-0+int intc_route_irq_to_guest (struct irq_desc *desc, +unsigned int priority) +{ +ASSERT (spin_is_locked (&desc->lock)); + +ASSERT (intc_hw_ops->guest_irq_type); + + desc->handler = intc_hw_ops->guest_irq_type; +set_bit (_IRQ_GUEST, &desc->status);
Is desc->status accessed anywhere without holding desc->lock? If not, __set_bit() or simply |= ?
Quoted 16 lines
xen/arch/riscv/intc.c +8
-0@@ -112,6 +130,14 @@ int domain_vintc_init(struct domain *d)break ; } +if ( !ret ) + { + d->arch.vintc->allocated_irqs = + xvzalloc_array(unsigned long, BITS_TO_LONGS(d->arch.vintc->irq_nums)); +if ( !d->arch.vintc->allocated_irqs ) + ret = -ENOMEM ; + } +return ret; }
xen/arch/riscv/intc.c void domain_vintc_deinit(struct domain *d) · line 160
Quoted 6 lines
xen/arch/riscv/intc.c +3
-0@@ -129,4 +155,14 @@ void domain_vintc_deinit(struct domain *d)printk ("vintc (ver:%d) isn't implemented\n" , ver);break ; } + +xvfree (d->arch.vintc->allocated_irqs); +}
XVFREE()
xen/arch/riscv/intc.c void domain_vintc_deinit(struct domain *d) · line 168
Quoted 7 lines
xen/arch/riscv/intc.c +6
-0+bool vintc_reserve_virq (const struct domain *d,unsigned int virq) +{ +if ( virq >= d->arch.vintc->irq_nums ) +return false; + +return !test_and_set_bit (virq, d->arch.vintc->allocated_irqs); }
As to function / field naming: You don't look to be allocating IRQs. So is there a reason the field name gives the impression of allocation? Simply s/allocated/used/ or some such?
Quoted 57 lines
xen/arch/riscv/intc.c +53
-0@@ -221,3 +239,160 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq)spin_unlock (&desc->lock);irq_exit (); } + +static inline struct irq_guest *irq_get_guest_info (struct irq_desc *desc) +{ +ASSERT (spin_is_locked (&desc->lock)); +ASSERT (test_bit (_IRQ_GUEST, &desc->status)); +ASSERT (desc->action !=NULL ); + +return desc->action->dev_id; +} + +static inline struct domain *irq_get_domain (struct irq_desc *desc) +{ +return irq_get_guest_info (desc)->d; +} + +void release_irq (unsigned int irq,const void *dev_id) +{ +struct irq_desc *desc; +unsigned long flags; +struct irqaction *action, **action_ptr; + + desc =irq_to_desc (irq); + +spin_lock_irqsave (&desc->lock,flags); + + action_ptr = &desc->action; +#ifdef CONFIG_IRQ_HAS_MULTIPLE_ACTION +for ( ;; ) + { + action = *action_ptr; +if ( !action ) + { +printk (XENLOG_WARNING "Trying to free already-free IRQ %u\n" , irq); +spin_unlock_irqrestore (&desc->lock, flags); +return ; + } + +if ( action->dev_id == dev_id ) +break ; + + action_ptr = &action->next; + } + +/* Found it - remove it from the action list */ + *action_ptr = action->next; +#else + action = *action_ptr; +#endif + +/* If this was the last action, shut down the IRQ */ +if ( !desc->action ) + { + desc->handler->shutdown (desc);
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 304
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+clear_bit (_IRQ_GUEST, &desc->status); + } + +spin_unlock_irqrestore (&desc->lock,flags); + +/* Wait to make sure it's not being used on another CPU */ +do {smp_mb (); }while (test_bit (_IRQ_INPROGRESS, &desc->status) ); + +if ( action->free_on_release ) +xvfree (action);
When !IRQ_HAS_MULTIPLE_ACTION desc->action becomes a dangling pointer here.
Quoted 5 lines
xen/arch/riscv/intc.c +5
-0+/* Route an IRQ to a specific guest */ +int route_irq_to_guest (struct domain *d,unsigned int virq, +unsigned int irq,const char *devname) +{ +struct irqaction *action;
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 319
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+struct irq_guest *info; +struct irq_desc *desc; +unsigned long flags; +int retval =0 ; + + desc =irq_to_desc (irq); + + action = xvmalloc(struct irqaction); +if ( !action ) +return -ENOMEM ;
This is freed by release_irq(), but ...
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 321
Quoted 2 lines
xen/arch/riscv/intc.c +2
-0+ info = xvmalloc(struct irq_guest); +if ( !info )
... where is the (non-error-path) freeing of this?
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 332
Quoted 11 lines
xen/arch/riscv/intc.c +11
-0+ { +xvfree (action); +return -ENOMEM ; + } + + info->d = d; + info->virq = virq; + + action->dev_id = info; + action->name = devname; + action->free_on_release =1 ;
true
Quoted 17 lines
xen/arch/riscv/intc.c +17
-0+spin_lock_irqsave (&desc->lock, flags); + +/* +* If the IRQ is already used by someone +* - If it's the same domain -> Xen doesn't need to update the IRQ desc. +* For safety check if we are not trying to assign the IRQ to a +* different vIRQ. +* - Otherwise -> For now, don't allow the IRQ to be shared between +* Xen and domains. +*/ +if ( desc->action !=NULL ) + { +if (test_bit (_IRQ_GUEST, &desc->status) ) + { +struct domain *ad =irq_get_domain (desc); + +if ( d != ad )
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 359
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+ { +printk (XENLOG_G_ERR "IRQ %u is already used by domain %u\n" , + irq, ad->domain_id); + retval = -EBUSY ; + } + else if ( irq_get_guest_info(desc)->virq != virq ) + { +printk (XENLOG_G_ERR +"d%u: IRQ %u is already assigned to vIRQ %u\n" , + d->domain_id, irq,irq_get_guest_info (desc)->virq);
Please can you get used to using %pd?
Quoted 12 lines
xen/arch/riscv/intc.c +12
-0+ retval = -EBUSY ; + } + } +else + { +printk (XENLOG_G_ERR "IRQ %u is already used by Xen\n" , irq); + retval = -EBUSY ; + } + goto out; + } + + retval =_setup_irq (desc,0 , action);
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 381
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+if ( retval ) + goto out; + + retval =intc_route_irq_to_guest (desc,IRQ_NO_PRIORITY ); + +spin_unlock_irqrestore (&desc->lock, flags); + +if ( retval ) + { +release_irq (desc->irq, info);
Is de-referencing desc legitimate / race free with desc->lock not held?
Jan
Re: [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
In reply to Jan BeulichQuoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/aplic.c +5
-0@@ -295,6 +295,11 @@ static void cf_check aplic_set_irq_type(struct irq_desc *desc,spin_unlock (&aplic.lock); } +static unsigned int cf_check aplic_irq_num(void) +{ +return aplic_info.num_irqs; +} +static const hw_irq_controller aplic_xen_irq_type = { .typename ="aplic" , .startup = aplic_irq_startup,@@ -309,6 +314,7 @@ static const struct intc_hw_operations aplic_ops = {
Quoted 8 lines
xen/arch/riscv/aplic.c static const struct intc_hw_operations aplic_ops = { · line 317
Quoted 4 lines
xen/arch/riscv/aplic.c +1
-0.host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, + .irq_nums = aplic_irq_num,Hook handler names and respective field names would preferably match up. It's unclear why the field uses some kind of plural(?), while the function uses singular.
I will do s/aplic_irq_num/aplic_irq_nums.
Quoted 25 lines
Quoted 25 lines
xen/arch/riscv/domain.c +3
-8@@ -11,6 +11,7 @@#include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> +#include <asm/intc.h> #include <asm/riscv_encoding.h> #include <asm/vtimer.h> @@ -155,14 +156,10 @@ int arch_vcpu_create(struct vcpu *v)if ( (rc =vcpu_vtimer_init (v)) ) goto fail; -/* -* As interrupt controller (IC) is not yet implemented, -* return an error. -* -* TODO: Drop this once IC is implemented. -*/ - rc = -EOPNOTSUPP ; - goto fail; +ASSERT (v->domain->arch.vintc->ops->vcpu_init); +
Quoted 8 lines
xen/arch/riscv/domain.c int arch_vcpu_create(struct vcpu *v) · line 161
Quoted 1 line
xen/arch/riscv/domain.c +1
-0+if ( (rc = v->domain->arch.vintc->ops->vcpu_init (v)) )I don't understand this model of the use of
ASSERT(). As previously said (more than once) - you'll crash anyway if any of the involved pointers is NULL. If you really think an up-front check is better, then why would you check only the leaf of the pointer chain, and not also vintc and ops? (Once you do you'll then likely notice that there are more assertions than actual code.)
Let me show an example. For the test I will comment ASSERT() here + make ->vcpu_init = NULL. Then the following dump will be occurred because of NULL pointer dereference:
(XEN) scause : 000000000000000c Unhandled exception[Instruction Page Fault] (XEN) htval : 0000000000000000 htinst : 0000000000000000 (XEN) hedeleg : 0000000000000000 hideleg : 0000000000000000 (XEN) hstatus : 0000000200000000 [ ] (XEN) hgatp : 0000000000000000 (XEN) hstateen0 : 0000000000000000 (XEN) stvec : ffffffffc00397f0 vstvec : 0000000000000000 (XEN) sepc : 0000000000000000 vsepc : 0000000000000000 (XEN) stval : 0000000000000000 vstval : 0000000000000000 (XEN) status : 0000000200000120 vsstatus : 0000000a00000000 (XEN) satp : 80000000000806c5 (XEN) vscause : 0000000000000000 [Instruction Address Misaligned] (XEN) ra : ffffffffc0038f92 sp : ffffffffc00a6580 (XEN) gp : 0000000000000000 tp : ffffffffc005e9c0 (XEN) t0 : ffffffffc04c9f78 t1 : 0000000052464e43 (XEN) t2 : 0000000000000000 s0 : ffffffffc00a65a0 (XEN) s1 : 000000323feaa000 a0 : 000000323feaa000 (XEN) a1 : 000000321a0010a8 a2 : 0000000000000000 (XEN) a3 : ffffffffc005c978 a4 : 0000000000000000 (XEN) a5 : 0000000000000000 a6 : 0000000000000000 (XEN) a7 : 000000321a0010a0 s2 : 0000000000000000 (XEN) s3 : 0000000000000000 s4 : 0000000000000000 (XEN) s5 : 0000000000000000 s6 : ffffffffc00a6638 (XEN) s7 : ffffffffc00a6638 s8 : ffffffffc00a6658 (XEN) s9 : ffffffffffffe000 s10 : 00000032105f4328
(XEN) s11 : 0000000000000000 t3 : 0000000000000001 (XEN) t4 : 0000000000000000 t5 : 0000000000080000 (XEN) t6 : 0000000000000001
So sepc = 0x0 => the CPU attempted to fetch and execute an instruction at address NULL.
stval = 0x0 => Faulting address = 0x0 (confirms null fetch).
During writing of that I reliased that I have also ra register which will tell where the bad call came from and then using addr2line/nm/objdump/whatever it still could be recoverable a place where NULL dereference happened in this case:
$ addr2line -e xen/xen-syms 0xffffffffc0038f92 /build/xen/arch/riscv/domain.c:218
So I agree now that such ASSERT should be dropped and shouldn't be used in such cases. I will drop it here and in the similar places where I added ASSERT for the reason as it is hard to identify a place where fault happened.
Quoted 11 lines
xen/arch/riscv/domain.c int arch_vcpu_create(struct vcpu *v)
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+unsigned int intc_irq_nums (void ) +{ +ASSERT (intc_hw_ops && intc_hw_ops->irq_nums); + +return intc_hw_ops->irq_nums (); +}You use this to set domains' properties. As indicated before, I view it as wrong to do so for any domain, besides perhaps Dom0 / hwdom. If you want to do so nevertheless, at the very least I'd expect something to be said about such a decision in the description.
If you could explain how this is expected to work for non-Dom0/hwdom domains, I would consider reworking it.
Basically, I don't understand how the following scenario is supposed to work. Let's say the host interrupt controller can manage 15 interrupts, while the guest interrupt controller supports only 7. If we want to pass through 8 devices to the guest, what should happen in that case? Should Xen simply report that the 8th device cannot be passed through because the guest supports only 7 IRQs?
Another concern is related to 1:1 IRQ mapping. Suppose I want to pass through a UART device whose IRQ number is typically greater than 10. In that case, it seems Xen would again have to report that the device cannot be passed through because its interrupt number exceeds the number of IRQs supported by the guest interrupt controller. This could be addressed by introducing a non-1:1 IRQ mapping between the host and guest, but the current dom0less codebase appears to assume a 1:1 IRQ mapping (unless I am mistaken).
Considering that virtual interrupt controllers use the maximum possible number of interrupts supported by the interrupt controller, the concerns mentioned above are unlikely to arise for a long time, if ever, unless support for features such as migration is introduced. Therefore, I think it would be reasonable to remove intc_irq_nums() and avoid using it to initialize virtual interrupts or domain properties.
Quoted 8 lines
xen/arch/riscv/domain.c
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+ +static const struct vintc_ops vintc_ops = { + .vcpu_init = vcpu_vaplic_init, +}; + +int __init domain_vaplic_init (struct domain *d)Why
__init, and why is there no caller?
The caller is in follow-up patch. I will add that to commit message.
Considering that domain_vintc_init() isn't __init from where domain_vaplic_init() is called then __init should be dropped here. I will do that.
Plus why is the vCPU-init a hook,
Quoted 2 lines
but the domain init is not? Either you mean to allow for other ICs, or you you don't.
IIUC your question domain_vaplic_init() ins't a hook because vaplic structure is allocated dynamically so vintc, vintc->ops and/or vintc->init_ops aren't initialized at the moment when vintc->{ops or init_ops}->domain_vaplic_init() is used in domain_vintc_init() (which is introduced in the follow up patch).
Quoted 21 lines
Quoted 21 lines
xen/arch/riscv/domain.c +20
-0+{ + struct vaplic *vaplic = xvzalloc(struct vaplic); + +if ( !vaplic ) +return -ENOMEM ; + + d->arch.vintc = &vaplic->vintc; + d->arch.vintc->ops = &vintc_ops; + + vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM ; + + d->arch.vintc->irq_nums =min (intc_irq_nums (), +VAPLIC_NUM_SOURCES +0U ); + + +return 0 ; +} + +void __init domain_vaplic_deinit (struct domain *d) +{
Quoted 8 lines
xen/arch/riscv/domain.c
Quoted 4 lines
xen/arch/riscv/domain.c +4
-0+struct vaplic *vaplic =to_vaplic (d); + +xvfree (vaplic); +}And
d->arch.vintcturns into a dangling pointer. The way you arrange data types, you can't useXVFREE()here, but imo you really want to make sure the function is idempotent.
I will do then:
void domain_vaplic_deinit(struct domain *d)
{
struct vaplic *vaplic;
if ( !d->arch.vintc )
return;
vaplic = to_vaplic(d);
d->arch.vintc = NULL;
xvfree(vaplic);
}Thanks.
~ Oleksii
Re: [PATCH v2 15/26] xen/riscv: introduce (de)initialization helpers for vINTC
In reply to Jan BeulichQuoted 13 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/intc.c +1
-0@@ -11,6 +11,7 @@#include <asm/aia.h> #include <asm/intc.h> +#include <asm/vaplic.h> static const struct intc_hw_operations *__ro_after_init intc_hw_ops;@@ -94,3 +95,38 @@ int __init make_intc_domU_node(struct kernel_info *kinfo)
Quoted 10 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 102
Quoted 7 lines
xen/arch/riscv/intc.c +5
-0return -EOPNOTSUPP ; } + +int domain_vintc_init (struct domain *d) +{ +int ret = -EOPNOTSUPP ; +const enum intc_version ver = intc_hw_ops->info->hw_version;Again - why would what the underlying hardware has control what all domains get?
If host uses AIA (APLIC/IMSIC) compatible controller then guest should use virtual AIA compatible controller, shouldn't it?
I don't think that it is a case when host uses PLIC interrupt controller but guests are going to use virtual APLIC.
Quoted 25 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 111
Quoted 9 lines
xen/arch/riscv/intc.c +8
-0+switch ( ver ) + { +case INTC_APLIC : + ret =domain_vaplic_init (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);If we take this path for whatever reason, ...
Quoted 13 lines
xen/arch/riscv/intc.c +13
-0+break ; + } + +return ret; +} + +void domain_vintc_deinit (struct domain *d) +{ +const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC :
Quoted 8 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 129
Quoted 5 lines
xen/arch/riscv/intc.c +5
-0+domain_vaplic_deinit (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);... we're also going to take this path (very quickly afterwards), just to get the same message twice without it being clear why it appears twice.
I missed to add printk("%s: ...", __func__, ...). I will update correspondingly if we will follow this way.
Thanks.
~ Oleksii
Re: [PATCH v2 16/26] xen/riscv: create APLIC DT node for guest domains
In reply to Jan BeulichQuoted 13 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/imsic.c +1
-0@@ -14,6 +14,7 @@#include <xen/cpumask.h> #include <xen/device_tree.h> #include <xen/errno.h> +#include <xen/fdt-kernel.h> #include <xen/init.h> #include <xen/macros.h> #include <xen/sched.h> @@ -522,3 +523,9 @@ int __init imsic_init(const struct dt_device_node *node)
Quoted 10 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 531
Quoted 8 lines
xen/arch/riscv/imsic.c +6
-0return rc; } + +int __init vimsic_make_domu_dt_node (struct kernel_info *kinfo, +unsigned int *phandle) +{ +return -EOPNOTSUPP ; +}This, I assume, is going to be filled properly by the next patch.
Yes as it was mentioned in the commit message. Actually here considering that this function isn't static I can just re-order patches and drop part of commit message in the current patch.
Quoted 92 lines
Quoted 25 lines
xen/arch/riscv/vaplic.c +5
-0@@ -9,6 +9,8 @@*/ #include <xen/errno.h> +#include <xen/fdt-kernel.h> +#include <xen/libfdt/libfdt.h> #include <xen/sched.h> #include <xen/xvmalloc.h> @@ -19,8 +21,11 @@#include "aplic-priv.h" +#define VAPLIC_COMPATIBLE "riscv,aplic"#define VAPLIC_NUM_SOURCES 96 +#define FDT_VAPLIC_INT_CELLS 2 + static int cf_check vcpu_vaplic_init(struct vcpu *v) {int rc =0 ;xen/arch/riscv/vaplic.c static int cf_check vcpu_vaplic_init(struct vcpu *v) · line 55
Quoted 4 lines
xen/arch/riscv/vaplic.c +1
-0@@ -47,6 +52,73 @@ static int cf_check vcpu_vaplic_init(struct vcpu *v)return rc; } +static int __init cf_check vaplic_make_domu_dt_node(struct kernel_info *kinfo)Again - why
__inithere and ...Quoted 60 lines
xen/arch/riscv/vaplic.c +60
-0+{ +int res =0 ; +void *fdt = kinfo->fdt; +unsigned int msi_parent_phandle; +char vaplic_name[128 ]; +paddr_t aplic_addr =GUEST_APLIC_S_BASE ; +paddr_t aplic_size =APLIC_SIZE (kinfo->bd.d->max_vcpus); +const __be32 reg[] = { +cpu_to_be32 (aplic_addr >>32 ), +cpu_to_be32 (aplic_addr), +cpu_to_be32 (aplic_size >>32 ), +cpu_to_be32 (aplic_size), + }; +struct vintc *vintc = kinfo->bd.d->arch.vintc; + + res =snprintf (vaplic_name,sizeof (vaplic_name),"/soc/aplic@%x" , +GUEST_APLIC_S_BASE ); +if ( res >=sizeof (vaplic_name) ) + { +dprintk (XENLOG_DEBUG ,"vaplic name is truncated\n" ); +return -ENOBUFS ; + } + + res =vimsic_make_domu_dt_node (kinfo, &msi_parent_phandle); +if ( res ) +return res; + + res =fdt_begin_node (fdt, vaplic_name); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"#interrupt-cells" ,FDT_VAPLIC_INT_CELLS ); +if ( res ) +return res; + + res =fdt_property (fdt,"reg" , reg,sizeof (reg)); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"riscv,num-sources" , vintc->irq_nums); +if ( res ) +return res; + + res =fdt_property (fdt,"interrupt-controller" ,NULL ,0 ); +if ( res ) +return res; + + res =fdt_property_string (fdt,"compatible" ,VAPLIC_COMPATIBLE ); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"msi-parent" , msi_parent_phandle); +if ( res ) +return res; + + res =fdt_property_cell (fdt,"phandle" , kinfo->phandle_intc); +if ( res ) +return res; + +return fdt_end_node (fdt);
Quoted 8 lines
xen/arch/riscv/vaplic.c static int cf_check vcpu_vaplic_init(struct vcpu *v) · line 118
Quoted 3 lines
xen/arch/riscv/vaplic.c +3
-0+} + +static const struct vintc_init_ops __initdata init_ops = {...
__initdatahere? If you really want to have the option of moving domain creation stuff to .init.* when only dom0less is configured, then a proper abstraction is needed, along the lines ofinit_or_livepatch.
It was marked as __init and __initdata, respectively, because I had the dom0less use case in mind, where all domains are created during boot. In that scenario, vaplic_make_domu_dt_node() would not be reused, so the associated memory could be freed afterwards.
In the case of Dom0 with the xl toolstack, I assume the toolstack will create the node for the guest domain. For Dom0, however, I am not sure whether we can reuse the current implementation as-is. As it is written now, I assume that properties such as aplic_addr, reg, and compatible should be reused from the host, right? If that is not a requirement for Dom0, then __init and __initdata could be removed (and possibly init_ops as well).
Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/vaplic.c +1
-0@@ -60,13 +132,14 @@ int __init domain_vaplic_init(struct domain *d) d->arch.vintc = &vaplic->vintc; d->arch.vintc->ops = &vintc_ops; + d->arch.vintc->init_ops = &init_ops;
Quoted 7 lines
xen/arch/riscv/vaplic.c int __init domain_vaplic_init(struct domain *d) · line 138
Quoted 3 lines
xen/arch/riscv/vaplic.c +2
-1- vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM ; + vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM | +APLIC_DOMAINCFG_RO80 ;This looks unrelated. I don't mind it being done here, but then it wants mentioning in the description. Or maybe I simply don't understand what this is about.
Right, it shouldn't be here. It would be better to move to [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support.
Thanks.
~ Oleksii
Re: [PATCH v2 17/26] xen/riscv: generate IMSIC DT node for guest domains
In reply to Jan BeulichQuoted 57 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Guests using the IMSIC interrupt controller require a corresponding Device Tree description.
Add support for generating an IMSIC node when building the guest DT. This allows guests to discover and use the IMSIC interrupt controller.
Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- s/imsic_make_reg_property/guest_imsic_make_reg_property.
- s/imsic_set_interrupt_extended_prop/guest_imsic_set_interrupt_extended_prop.
- Use initalizer for regs[] array inimsic_make_reg_property().
- Move buf[] insde thefor()loop.
- Correct check of returned phandle.
- Drop local variable len.
- /s/XVFREE/xvfree inimsic_set_interrupt_extended_prop().
- Drop initializer for local variable data.
- s/uint32_t/unsinged int for pos and cpu inimsic_set_interrupt_extended_prop().
- Dropnext_phandleas it is now in common code.
- Introducevcpu_imsic_deinit.
- Refactorvimsic_make_domu_dt_node()to avoid usage of host IMSIC dt node.--- xen/arch/riscv/imsic.c | 127 +++++++++++++++++++++- xen/arch/riscv/include/asm/guest-layout.h | 2 + 2 files changed, 128 insertions(+), 1 deletion(-)xen/arch/riscv/imsic.c +3
-0@@ -13,9 +13,12 @@#include <xen/const.h> #include <xen/cpumask.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/errno.h> +#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/macros.h> #include <xen/sched.h> #include <xen/smp.h> xen/arch/riscv/imsic.c static struct imsic_config imsic_cfg = { · line 41
Quoted 4 lines
xen/arch/riscv/imsic.c +1
-0@@ -35,6 +38,11 @@ static struct imsic_config imsic_cfg = { .lock = SPIN_LOCK_UNLOCKED, }; +static unsigned int __ro_after_init guest_num_msis;How come this is
__ro_after_init, when it's ...Quoted 4 lines
xen/arch/riscv/imsic.c +0
-0@@ -291,6 +299,11 @@ static int imsic_parse_node(const struct dt_device_node *node,return -ENOENT ; }
Quoted 6 lines
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 305
Quoted 4 lines
xen/arch/riscv/imsic.c +4
-0+if (dt_property_read_u32 (node,"riscv,num-guest-ids" , &tmp) ) + guest_num_msis = tmp; +else + guest_num_msis = imsic_cfg.nr_ids;... written by a non-__init function?
__ro_after_init should be dropped. I will use __read_mostly instead.
Plus are you again inheriting a host
Quoted 1 line
property into guests without saying why?
Based on the discussion for similar place around vAPLIC code in the previous patch I will add here or the comment why or introduce VIMISC_NR_IDS and use it instead.
Quoted 8 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 540
Quoted 6 lines
xen/arch/riscv/imsic.c +1
-0@@ -524,8 +537,120 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } +static int __init guest_imsic_make_reg_property (struct domain *d,void *fdt)Same question again as to
__initthroughout here.
From Arm code I understood that libxl creates the interrupt controller node in userspace before handing the FDT to Xen so this and related functions won't be called by userspace.
And for dom0's it is a separate *_make_hdwom_dt_node() (took it from Arm code base) which can expose the real hardware MMIO regions to dom0 (since dom0 may need direct hardware access - if it isn't strict requirement then the current one way to create node could be re-used).
So if for dom0/hwdom it should be created separate node then we will have similar separate function for IMSIC node here and then it looks fine to keep __init.
Quoted 6 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 542
Quoted 3 lines
xen/arch/riscv/imsic.c +2
-0+{ +paddr_t base_addr =GUEST_IMSIC_S_BASE ;So you make a local variable for a constant, ...
Quoted 7 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 547
Quoted 5 lines
xen/arch/riscv/imsic.c +5
-0+__be32 regs[4 ] = { +cpu_to_be32 (base_addr >>32 ), +cpu_to_be32 (base_addr), +cpu_to_be32 ((IMSIC_MMIO_PAGE_SZ * d->max_vcpus) >>32 ), +cpu_to_be32 (IMSIC_MMIO_PAGE_SZ * d->max_vcpus),... but this non-constant expression is spelled out twice.
I will use instead of base_addr GUEST_IMSIC_S_BASE directly and introduce local variable for:
paddr_t size = IMSIC_MMIO_PAGE_SZ * d->max_vcpus;
Quoted 25 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 558
Quoted 7 lines
xen/arch/riscv/imsic.c +6
-0+static int __init guest_imsic_set_interrupt_extended_prop (struct domain *d, +void *fdt) +{ +unsigned int cpu, pos =0 ; +uint32_t phandle; +uint32_t *irq_ext;Doesn't this want to be
__be32, seeing ...Quoted 15 lines
xen/arch/riscv/imsic.c +15
-0+int res; + + irq_ext =xvzalloc_array (uint32_t, d->max_vcpus *2 ); +if ( !irq_ext ) +return -ENOMEM ; + +for ( cpu =0 ; cpu < d->max_vcpus; cpu++ ) + { +char buf[64 ]; + +snprintf (buf,sizeof (buf),"/cpus/cpu@%u/interrupt-controller" , cpu); + phandle =fdt_get_phandle (fdt,fdt_path_offset (fdt, buf)); + +if ( !phandle ) + {
Quoted 8 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 579
Quoted 6 lines
xen/arch/riscv/imsic.c +6
-0+ res = -ENODEV ; + goto out; + } + + irq_ext[pos++] =cpu_to_be32 (phandle); + irq_ext[pos++] =cpu_to_be32 (IRQ_S_EXT );... this?
Agree, __be32 should be added for definition of irq_ext.
Quoted 7 lines
Also, just like "buf", "phandle" can be local to this loop's body.
Quoted 4 lines
xen/arch/riscv/include/asm/guest-layout.h +0
-0@@ -5,6 +5,8 @@
Quoted 8 lines
xen/arch/riscv/include/asm/guest-layout.h line 10
Quoted 5 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_IMSIC_S_BASE 0x28000000 +#define GUEST_RAM_BANKS 2Is this going to become an unannotated collection of (seemingly) random numbers?
Could you please clarify what kind of annotation you would expect to see here? It's just an address, which is typically used for IMSIC by QEMU, so I chose the same notation for the guest address as well.
Thanks.
~ Oleksii
Re: [PATCH v2 19/26] xen/riscv: implement IRQ routing for device passthrough
In reply to Jan BeulichQuoted 17 lines · Jan Beulich
Quoted 16 lines · Oleksii Kurochko
xen/arch/riscv/device.c new file +13
-0@@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <xen/device_tree.h> +#include <xen/errno.h> +#include <xen/iocap.h> +#include <xen/rangeset.h> +#include <xen/sched.h> + +#include <asm/intc.h> + +int map_irq_to_domain (struct domain *d,unsigned int irq, +bool need_mapping,const char *devname) +{
Quoted 8 lines
xen/arch/riscv/device.c line 19
Quoted 6 lines
xen/arch/riscv/device.c new file +6
-0+int res; + + res =irq_permit_access (d, irq); +if ( res ) + { +printk (XENLOG_ERR "Unable to permit to %pd access to IRQ %u\n" , d, irq);Nit: Drop the first "to"?
Sure, I will drop that.
Quoted 45 lines
Quoted 32 lines
xen/arch/riscv/device.c new file +31
-0+return res; + } + +if ( need_mapping ) + { +/* +* Checking the return of vintc_reserve_virq is not +* necessary. It should not fail except when we try to map +* the IRQ twice. This can legitimately happen if the IRQ is shared. +*/ +vintc_reserve_virq (d, irq); + + res =route_irq_to_guest (d, irq, irq, devname); +if ( res <0 ) + { +printk (XENLOG_ERR "Unable to map IRQ%u to %pd\n" , irq, d); +return res; + } + } + +dt_dprintk (" - IRQ: %u\n" , irq); + +return 0 ; +} + +/* +* map_device_irqs_to_domain retrieves the interrupts configuration from +* a device tree node and maps those interrupts to the target domain. +* +* Returns: +* < 0 error xen/arch/riscv/device.c line 60
Quoted 10 lines
xen/arch/riscv/device.c new file +10
-0+* 0 success +*/ +int map_device_irqs_to_domain (struct domain *d, +struct dt_device_node *dev, +bool need_mapping, +struct rangeset *irq_ranges) +{ +unsigned int i, nirq; +int res, irq; +struct dt_raw_irq rirq;Move the latter three variables to the loop's scope and ...
Quoted 3 lines
xen/arch/riscv/device.c line 62
Quoted 1 line
xen/arch/riscv/device.c new file +1
-0+ nirq =dt_number_of_irq (dev);... make this the variable's initializer?
It makes sense. I will do that.
Quoted 36 lines
Quoted 36 lines
xen/arch/riscv/device.c new file +35
-0+/* Give permission and map IRQs */ +for ( i =0 ; i < nirq; i++ ) + { + res =dt_device_get_raw_irq (dev, i, &rirq); +if ( res ) + { +printk (XENLOG_ERR "Unable to retrieve irq %u for %s\n" , + i,dt_node_full_name (dev)); +return res; + } + +/* +* Don't map IRQ that have no physical meaning +* ie: IRQ whose controller is not APLIC/IMSIC/PLIC. +*/ +if ( rirq.controller != dt_interrupt_controller ) + { +dt_dprintk ("irq %u not connected to primary controller." +"Connected to %s\n" , i, +dt_node_full_name (rirq.controller)); +continue ; + } + + irq =platform_get_irq (dev, i); +if ( irq <0 ) + { +printk ("Unable to get irq %u for %s\n" , i,dt_node_full_name (dev)); +return irq; + } + + res =map_irq_to_domain (d, irq, need_mapping,dt_node_name (dev)); +if ( res ) +return res; + +
Quoted 8 lines
xen/arch/riscv/device.c line 104
Quoted 6 lines
xen/arch/riscv/device.c new file +6
-0+/* +* At the moment there is only one user of map_device_irqs_to_domain() +* for RISC-V which calls it irq_ranges == NULL. +*/ +if ( irq_ranges ) +return -EOPNOTSUPP ;Why is this checked last, and inside the loop (when it's loop invariant)?
Just to show the place where irq_ranges will be handled. But currently I agree it would be better just move outside the loop.
Quoted 11 lines
xen/arch/riscv/include/asm/intc.h enum intc_version { · line 16
Quoted 8 lines
xen/arch/riscv/include/asm/intc.h +1
-0@@ -13,8 +13,11 @@ enum intc_version { };struct cpu_user_regs ; +struct domain ;I can spot why this is needed, but ...
Quoted 7 lines
xen/arch/riscv/include/asm/intc.h enum intc_version { · line 21
Quoted 5 lines
xen/arch/riscv/include/asm/intc.h +2
-0+struct dt_device_node ;struct irq_desc ;struct kernel_info ; +struct rangeset ;struct vcpu ;... I'm at a loss to explain the need for these two additions.
Rudements from previous version of this patch series. I will drop them. The same below ...
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/include/asm/setup.h +0
-0@@ -5,6 +5,10 @@
Quoted 8 lines
xen/arch/riscv/include/asm/setup.h line 10
Quoted 5 lines
xen/arch/riscv/include/asm/setup.h +3
-0#include <xen/types.h> +struct domain ; +struct dt_device_node ; +struct rangeset ;Same here - why would they be needed when you make no other changes to this header?
... here.
Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/intc.c +2
-0@@ -7,7 +7,9 @@#include <xen/init.h> #include <xen/irq.h> #include <xen/lib.h> +#include <xen/sched.h> #include <xen/spinlock.h> +#include <xen/xvmalloc.h> #include <asm/aia.h> #include <asm/intc.h> @@ -86,6 +88,22 @@ unsigned int intc_irq_nums(void)return intc_hw_ops->irq_nums (); }
Quoted 12 lines
xen/arch/riscv/intc.c unsigned int intc_irq_nums(void) · line 99
Quoted 9 lines
xen/arch/riscv/intc.c +9
-0+int intc_route_irq_to_guest (struct irq_desc *desc, +unsigned int priority) +{ +ASSERT (spin_is_locked (&desc->lock)); + +ASSERT (intc_hw_ops->guest_irq_type); + + desc->handler = intc_hw_ops->guest_irq_type; +set_bit (_IRQ_GUEST, &desc->status);Is
desc->statusaccessed anywhere without holdingdesc->lock? If not,__set_bit()or simply |= ?
In release_irq() it could be used without lock:
...
/* Wait to make sure it's not being used on another CPU */
do { smp_mb(); } while ( test_bit(_IRQ_INPROGRESS, &desc->status) );Quoted 26 lines
Quoted 17 lines
xen/arch/riscv/intc.c +8
-0@@ -112,6 +130,14 @@ int domain_vintc_init(struct domain *d)break ; } +if ( !ret ) + { + d->arch.vintc->allocated_irqs = + xvzalloc_array(unsigned long, BITS_TO_LONGS(d->arch.vintc->irq_nums)); +if ( !d->arch.vintc->allocated_irqs ) + ret = -ENOMEM ; + } +return ret; }xen/arch/riscv/intc.c void domain_vintc_deinit(struct domain *d) · line 160
Quoted 6 lines
xen/arch/riscv/intc.c +3
-0@@ -129,4 +155,14 @@ void domain_vintc_deinit(struct domain *d)printk ("vintc (ver:%d) isn't implemented\n" , ver);break ; } + +xvfree (d->arch.vintc->allocated_irqs); +}
XVFREE()
Quoted 11 lines
xen/arch/riscv/intc.c void domain_vintc_deinit(struct domain *d) · line 168
Quoted 7 lines
xen/arch/riscv/intc.c +6
-0+bool vintc_reserve_virq (const struct domain *d,unsigned int virq) +{ +if ( virq >= d->arch.vintc->irq_nums ) +return false; + +return !test_and_set_bit (virq, d->arch.vintc->allocated_irqs); }As to function / field naming: You don't look to be allocating IRQs. So is there a reason the field name gives the impression of allocation? Simply s/allocated/used/ or some such?
I am okay to rename to 'used' instead of 'allocated'.
Quoted 44 lines
Quoted 44 lines
xen/arch/riscv/intc.c +39
-0@@ -221,3 +239,160 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq)spin_unlock (&desc->lock);irq_exit (); } + +static inline struct irq_guest *irq_get_guest_info (struct irq_desc *desc) +{ +ASSERT (spin_is_locked (&desc->lock)); +ASSERT (test_bit (_IRQ_GUEST, &desc->status)); +ASSERT (desc->action !=NULL ); + +return desc->action->dev_id; +} + +static inline struct domain *irq_get_domain (struct irq_desc *desc) +{ +return irq_get_guest_info (desc)->d; +} + +void release_irq (unsigned int irq,const void *dev_id) +{ +struct irq_desc *desc; +unsigned long flags; +struct irqaction *action, **action_ptr; + + desc =irq_to_desc (irq); + +spin_lock_irqsave (&desc->lock,flags); + + action_ptr = &desc->action; +#ifdef CONFIG_IRQ_HAS_MULTIPLE_ACTION +for ( ;; ) + { + action = *action_ptr; +if ( !action ) + { +printk (XENLOG_WARNING "Trying to free already-free IRQ %u\n" , irq); +spin_unlock_irqrestore (&desc->lock, flags); +return ; + } + +if ( action->dev_id == dev_id ) +break ;
Quoted 8 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 288
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+ + action_ptr = &action->next; + } + +/* Found it - remove it from the action list */ + *action_ptr = action->next; +#else + action = *action_ptr;
It is needed to add *action_ptr = NULL here to deal with ...
Quoted 10 lines
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+#endif + +/* If this was the last action, shut down the IRQ */ +if ( !desc->action ) + { + desc->handler->shutdown (desc); +clear_bit (_IRQ_GUEST, &desc->status); + } + +spin_unlock_irqrestore (&desc->lock,flags);
Quoted 8 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 304
Quoted 6 lines
xen/arch/riscv/intc.c +6
-0+ +/* Wait to make sure it's not being used on another CPU */ +do {smp_mb (); }while (test_bit (_IRQ_INPROGRESS, &desc->status) ); + +if ( action->free_on_release ) +xvfree (action);When !IRQ_HAS_MULTIPLE_ACTION
desc->actionbecomes a dangling pointer here.
... it could be xvfree here as action is local variable.
Quoted 19 lines
Quoted 6 lines
xen/arch/riscv/intc.c +5
-0+/* Route an IRQ to a specific guest */ +int route_irq_to_guest (struct domain *d,unsigned int virq, +unsigned int irq,const char *devname) +{ +struct irqaction *action;xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 319
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+struct irq_guest *info; +struct irq_desc *desc; +unsigned long flags; +int retval =0 ; + + desc =irq_to_desc (irq); + + action = xvmalloc(struct irqaction); +if ( !action ) +return -ENOMEM ;This is freed by
release_irq(), but ...
Quoted 5 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 321
Quoted 2 lines
xen/arch/riscv/intc.c +2
-0+ info = xvmalloc(struct irq_guest); +if ( !info )... where is the (non-error-path) freeing of this?
Agree it should be freed.
I am thing about just to update release_irq():
if ( action->free_on_release )
{
xvfree(action->dev_id);
xvfree(action);
}But I think it is conceptually is incorrect as owner of ->dev_id in this case is guest so it would be better if guest will do that. So I think it would be better to intoduce now release_guest_irq():
int release_guest_irq(struct domain *d, unsigned int virq)
{
struct irq_desc *desc;
struct irq_guest *info;
unsigned long flags;
desc = irq_to_desc(virq);
spin_lock_irqsave(&desc->lock, flags);
if ( !test_bit(_IRQ_GUEST, &desc->status) )
goto unlock_err;
info = irq_get_guest_info(desc);
if ( d != info->d )
goto unlock_err;
spin_unlock_irqrestore(&desc->lock, flags);
release_irq(desc->irq, info);
xvfree(info);
return 0; unlock_err:
spin_unlock_irqrestore(&desc->lock, flags);
return -EINVAL;
}and then call it in domain_vintc_deinit() for all virqs of a domain.
Quoted 35 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 332
Quoted 11 lines
xen/arch/riscv/intc.c +11
-0+ { +xvfree (action); +return -ENOMEM ; + } + + info->d = d; + info->virq = virq; + + action->dev_id = info; + action->name = devname; + action->free_on_release =1 ;true
Quoted 21 lines
xen/arch/riscv/intc.c +21
-0+spin_lock_irqsave (&desc->lock, flags); + +/* +* If the IRQ is already used by someone +* - If it's the same domain -> Xen doesn't need to update the IRQ desc. +* For safety check if we are not trying to assign the IRQ to a +* different vIRQ. +* - Otherwise -> For now, don't allow the IRQ to be shared between +* Xen and domains. +*/ +if ( desc->action !=NULL ) + { +if (test_bit (_IRQ_GUEST, &desc->status) ) + { +struct domain *ad =irq_get_domain (desc); + +if ( d != ad ) + { +printk (XENLOG_G_ERR "IRQ %u is already used by domain %u\n" , + irq, ad->domain_id); + retval = -EBUSY ;
Quoted 8 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 359
Quoted 6 lines
xen/arch/riscv/intc.c +6
-0+ } +else if (irq_get_guest_info (desc)->virq != virq ) + { +printk (XENLOG_G_ERR +"d%u: IRQ %u is already assigned to vIRQ %u\n" , + d->domain_id, irq,irq_get_guest_info (desc)->virq);Please can you get used to using %pd?
I'll do my best. Thanks for consistently pointing that out. I appreciate it.
Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/intc.c +16
-0+ retval = -EBUSY ; + } + } +else + { +printk (XENLOG_G_ERR "IRQ %u is already used by Xen\n" , irq); + retval = -EBUSY ; + } + goto out; + } + + retval =_setup_irq (desc,0 , action); +if ( retval ) + goto out; + + retval =intc_route_irq_to_guest (desc,IRQ_NO_PRIORITY );
Quoted 8 lines
xen/arch/riscv/intc.c void do_IRQ(struct cpu_user_regs *regs, unsigned int irq) · line 381
Quoted 6 lines
xen/arch/riscv/intc.c +6
-0+ +spin_unlock_irqrestore (&desc->lock, flags); + +if ( retval ) + { +release_irq (desc->irq, info);Is de-referencing desc legitimate / race free with
desc->locknot held?
desc itself cannot be freed, it's a pointer into the statically allocated array irq_desc[NR_IRQS] (riscv/irq.c:29). The descriptor object lives for the lifetime of the system.
desc->irq is write-once, it's set during init_irq_data() at boot (riscv/irq.c:172) and never modified again. It's effectively an immutable field after initialization, so reading it without the lock held is safe.
Thanks.
~ Oleksii
Re: [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
In reply to Oleksii KurochkoQuoted 26 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/aplic.c +5
-0@@ -295,6 +295,11 @@ static void cf_check aplic_set_irq_type(struct irq_desc *desc,spin_unlock (&aplic.lock); } +static unsigned int cf_check aplic_irq_num(void) +{ +return aplic_info.num_irqs; +} +static const hw_irq_controller aplic_xen_irq_type = { .typename ="aplic" , .startup = aplic_irq_startup,@@ -309,6 +314,7 @@ static const struct intc_hw_operations aplic_ops = {Quoted 8 lines
xen/arch/riscv/aplic.c static const struct intc_hw_operations aplic_ops = { · line 317
Quoted 4 lines
xen/arch/riscv/aplic.c +1
-0.host_irq_type = &aplic_xen_irq_type, .handle_interrupt = aplic_handle_interrupt, .set_irq_type = aplic_set_irq_type, + .irq_nums = aplic_irq_num,Hook handler names and respective field names would preferably match up. It's unclear why the field uses some kind of plural(?), while the function uses singular.
Quoted 1 line
I will do s/aplic_irq_num/aplic_irq_nums.
Well. What "nums"? Isn't it "number of IRQs" that the hook returns? That would call for .nr_irqs and aplic_nr_irqs(), for example.
Quoted 15 lines
Quoted 11 lines
xen/arch/riscv/aplic.c static const struct intc_hw_operations aplic_ops = {
Quoted 6 lines
xen/arch/riscv/aplic.c +6
-0+unsigned int intc_irq_nums (void ) +{ +ASSERT (intc_hw_ops && intc_hw_ops->irq_nums); + +return intc_hw_ops->irq_nums (); +}You use this to set domains' properties. As indicated before, I view it as wrong to do so for any domain, besides perhaps Dom0 / hwdom. If you want to do so nevertheless, at the very least I'd expect something to be said about such a decision in the description.
If you could explain how this is expected to work for non-Dom0/hwdom domains, I would consider reworking it.
Quoted 6 lines
Basically, I don't understand how the following scenario is supposed to work. Let's say the host interrupt controller can manage 15 interrupts, while the guest interrupt controller supports only 7. If we want to pass through 8 devices to the guest, what should happen in that case? Should Xen simply report that the 8th device cannot be passed through because the guest supports only 7 IRQs?
If IRQs cannot be shared - yes.
Quoted 8 lines
Another concern is related to 1:1 IRQ mapping. Suppose I want to pass through a UART device whose IRQ number is typically greater than 10. In that case, it seems Xen would again have to report that the device cannot be passed through because its interrupt number exceeds the number of IRQs supported by the guest interrupt controller. This could be addressed by introducing a non-1:1 IRQ mapping between the host and guest, but the current dom0less codebase appears to assume a 1:1 IRQ mapping (unless I am mistaken).
Sounds like a (perhaps significant) shortcoming to me.
Quoted 6 lines
Considering that virtual interrupt controllers use the maximum possible number of interrupts supported by the interrupt controller, the concerns mentioned above are unlikely to arise for a long time, if ever, unless support for features such as migration is introduced. Therefore, I think it would be reasonable to remove
intc_irq_nums()and avoid using it to initialize virtual interrupts or domain properties.
Yes.
Jan
Re: [PATCH v2 15/26] xen/riscv: introduce (de)initialization helpers for vINTC
In reply to Oleksii KurochkoQuoted 25 lines · Oleksii Kurochko
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/intc.c +1
-0@@ -11,6 +11,7 @@#include <asm/aia.h> #include <asm/intc.h> +#include <asm/vaplic.h> static const struct intc_hw_operations *__ro_after_init intc_hw_ops;@@ -94,3 +95,38 @@ int __init make_intc_domU_node(struct kernel_info *kinfo)Quoted 10 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 102
Quoted 7 lines
xen/arch/riscv/intc.c +5
-0return -EOPNOTSUPP ; } + +int domain_vintc_init (struct domain *d) +{ +int ret = -EOPNOTSUPP ; +const enum intc_version ver = intc_hw_ops->info->hw_version;Again - why would what the underlying hardware has control what all domains get?
Quoted 5 lines
If host uses AIA (APLIC/IMSIC) compatible controller then guest should use virtual AIA compatible controller, shouldn't it?
I don't think that it is a case when host uses PLIC interrupt controller but guests are going to use virtual APLIC.
Well, this may be a present restriction, but once you want to support migration (and different IC models), I guess you won't get around such.
Quoted 33 lines
Quoted 24 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 111
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+switch ( ver ) + { +case INTC_APLIC : + ret =domain_vaplic_init (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);If we take this path for whatever reason, ...
Quoted 13 lines
xen/arch/riscv/intc.c +13
-0+break ; + } + +return ret; +} + +void domain_vintc_deinit (struct domain *d) +{ +const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC :Quoted 8 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 129
Quoted 5 lines
xen/arch/riscv/intc.c +5
-0+domain_vaplic_deinit (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);... we're also going to take this path (very quickly afterwards), just to get the same message twice without it being clear why it appears twice.
Quoted 2 lines
I missed to add
printk("%s: ...", __func__, ...). I will update correspondingly if we will follow this way.
That's not quite what I was after, though. I question the need for this latter message.
Jan
Re: [PATCH v2 17/26] xen/riscv: generate IMSIC DT node for guest domains
In reply to Oleksii KurochkoQuoted 81 lines · Oleksii Kurochko
Quoted 57 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Guests using the IMSIC interrupt controller require a corresponding Device Tree description.
Add support for generating an IMSIC node when building the guest DT. This allows guests to discover and use the IMSIC interrupt controller.
Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- s/imsic_make_reg_property/guest_imsic_make_reg_property.
- s/imsic_set_interrupt_extended_prop/guest_imsic_set_interrupt_extended_prop.
- Use initalizer for regs[] array inimsic_make_reg_property().
- Move buf[] insde thefor()loop.
- Correct check of returned phandle.
- Drop local variable len.
- /s/XVFREE/xvfree inimsic_set_interrupt_extended_prop().
- Drop initializer for local variable data.
- s/uint32_t/unsinged int for pos and cpu inimsic_set_interrupt_extended_prop().
- Dropnext_phandleas it is now in common code.
- Introducevcpu_imsic_deinit.
- Refactorvimsic_make_domu_dt_node()to avoid usage of host IMSIC dt node.--- xen/arch/riscv/imsic.c | 127 +++++++++++++++++++++- xen/arch/riscv/include/asm/guest-layout.h | 2 + 2 files changed, 128 insertions(+), 1 deletion(-)xen/arch/riscv/imsic.c +3
-0@@ -13,9 +13,12 @@#include <xen/const.h> #include <xen/cpumask.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/errno.h> +#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/macros.h> #include <xen/sched.h> #include <xen/smp.h> xen/arch/riscv/imsic.c static struct imsic_config imsic_cfg = { · line 41
Quoted 4 lines
xen/arch/riscv/imsic.c +1
-0@@ -35,6 +38,11 @@ static struct imsic_config imsic_cfg = { .lock = SPIN_LOCK_UNLOCKED, }; +static unsigned int __ro_after_init guest_num_msis;How come this is
__ro_after_init, when it's ...Quoted 4 lines
xen/arch/riscv/imsic.c +0
-0@@ -291,6 +299,11 @@ static int imsic_parse_node(const struct dt_device_node *node,return -ENOENT ; }Quoted 6 lines
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 305
Quoted 4 lines
xen/arch/riscv/imsic.c +4
-0+if (dt_property_read_u32 (node,"riscv,num-guest-ids" , &tmp) ) + guest_num_msis = tmp; +else + guest_num_msis = imsic_cfg.nr_ids;... written by a non-__init function?
__ro_after_initshould be dropped. I will use__read_mostlyinstead.Plus are you again inheriting a host
Quoted 1 line
property into guests without saying why?
Based on the discussion for similar place around vAPLIC code in the previous patch I will add here or the comment why or introduce
VIMISC_NR_IDSand use it instead.Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/imsic.c +0
-0@@ -524,8 +537,120 @@ int __init imsic_init(const struct dt_device_node *node)return rc; }
Quoted 7 lines
Quoted 3 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 540
Quoted 1 line
xen/arch/riscv/imsic.c +1
-0+static int __init guest_imsic_make_reg_property (struct domain *d,void *fdt)Same question again as to
__initthroughout here.From Arm code I understood that libxl creates the interrupt controller
node in userspace before handing the FDT to Xen so this and related functions won't be called by userspace.
Hmm, okay, on the v3 submission I'll have to keep that in mind, and call paths will need checking accordingly.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/riscv/include/asm/guest-layout.h +0
-0@@ -5,6 +5,8 @@Quoted 8 lines
xen/arch/riscv/include/asm/guest-layout.h line 10
Quoted 5 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_IMSIC_S_BASE 0x28000000 +#define GUEST_RAM_BANKS 2Is this going to become an unannotated collection of (seemingly) random numbers?
Quoted 3 lines
Could you please clarify what kind of annotation you would expect to see here? It's just an address, which is typically used for IMSIC by QEMU, so I chose the same notation for the guest address as well.
Well, how could I have known? The description says nothing, and there are also no comments. The numbers here look entirely arbitrary, and if someone wanted to change them, it wouldn't be clear at all what constraints need taking into consideration.
Jan
Re: [PATCH v2 19/26] xen/riscv: implement IRQ routing for device passthrough
In reply to Oleksii KurochkoQuoted 19 lines · Oleksii Kurochko
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/intc.c +0
-0@@ -86,6 +88,22 @@ unsigned int intc_irq_nums(void)return intc_hw_ops->irq_nums (); }Quoted 12 lines
xen/arch/riscv/intc.c unsigned int intc_irq_nums(void) · line 99
Quoted 9 lines
xen/arch/riscv/intc.c +9
-0+int intc_route_irq_to_guest (struct irq_desc *desc, +unsigned int priority) +{ +ASSERT (spin_is_locked (&desc->lock)); + +ASSERT (intc_hw_ops->guest_irq_type); + + desc->handler = intc_hw_ops->guest_irq_type; +set_bit (_IRQ_GUEST, &desc->status);Is
desc->statusaccessed anywhere without holdingdesc->lock? If not,__set_bit()or simply |= ?
Quoted 4 lines
In
release_irq()it could be used without lock:
.../* Wait to make sure it's not being used on another CPU */ do { smp_mb(); } while ( test_bit(_IRQ_INPROGRESS, &desc->status) );
That's fine with the update being non-atomic. I probably should have said "modified" instead of "accessed".
Jan
Re: [PATCH v2 20/26] xen/riscv: add missing APLIC register offsets, masks to asm/aplic.h.
In reply to Jan BeulichQuoted 5 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/aplic.h +0
-0@@ -15,6 +15,11 @@
Quoted 7 lines
xen/arch/riscv/include/asm/aplic.h line 18
Quoted 3 lines
xen/arch/riscv/include/asm/aplic.h +1
-0#include <asm/imsic.h> +#define APLIC_REG_OFFSET_MASK 0x3fffThis I can see this as wanting to live separately. Yet ...
Quoted 1 line
xen/arch/riscv/include/asm/aplic.h +1
-0+#define APLIC_TARGET_IPRIO_MASK 0xff
This could be dropped as I don't use this mask anymore.
Quoted 32 lines
xen/arch/riscv/include/asm/aplic.h line 21
Quoted 2 lines
xen/arch/riscv/include/asm/aplic.h +2
-0+#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_EIID_MASK 0x7ff... what (set of) register(s) do these apply to? Perhaps ...
Quoted 27 lines
xen/arch/riscv/include/asm/aplic.h +23
-0@@ -26,6 +31,36 @@#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_LAST 0x0ffc + +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIP_LAST 0x1c7c +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIP_LAST 0x1d7c +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIE_LAST 0x1e7c +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIE_LAST 0x1f7c +#define APLIC_CLRIENUM 0x1fdc +
Quoted 8 lines
xen/arch/riscv/include/asm/aplic.h line 62
Quoted 6 lines
xen/arch/riscv/include/asm/aplic.h +6
-0+#define APLIC_SETIPNUM_LE 0x2000 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_LAST 0x3ffc... these? And then is ...
Yes, it is applied to target register.
Quoted 5 lines
xen/arch/riscv/include/asm/aplic.h line 64
Quoted 2 lines
xen/arch/riscv/include/asm/aplic.h +0
-0#define APLIC_TARGET_HART_IDX_SHIFT 18... this also covering some part of them? Can't they (a) live together and (b) have some kind of connection to what they apply to?
Agreed, they could live together. The intent was simply to keep all MMIO definitions in sequence, but I'm okay with grouping macros related to a specific register together.
Quoted 2 lines
And then why is there again a mix of
*_SHIFTand*_MASK?
This APLIC_TARGET_GUEST_IDX_SHIFT - could be dropped I've already reworked that in different branch and use *_MASK instead so I will re-apply that changes in this patch. But APLIC_TARGET_HART_IDX_SHIFT is used in to not open-code 12 in calculation of shift value for group_index:
static void cf_check aplic_set_irq_affinity(struct irq_desc *desc, const cpumask_t *mask) {
...
value |= cpu << APLIC_TARGET_HART_IDX_SHIFT;
value |= group_index << (lhxw + APLIC_TARGET_HART_IDX_SHIFT);Thanks.
~ Oleksii
Re: [PATCH v2 20/26] xen/riscv: add missing APLIC register offsets, masks to asm/aplic.h.
Quoted 60 lines · Oleksii Kurochko
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/aplic.h +0
-0@@ -15,6 +15,11 @@Quoted 7 lines
xen/arch/riscv/include/asm/aplic.h line 18
Quoted 3 lines
xen/arch/riscv/include/asm/aplic.h +1
-0#include <asm/imsic.h> +#define APLIC_REG_OFFSET_MASK 0x3fffThis I can see this as wanting to live separately. Yet ...
Quoted 1 line
xen/arch/riscv/include/asm/aplic.h +1
-0+#define APLIC_TARGET_IPRIO_MASK 0xffThis could be dropped as I don't use this mask anymore.
Quoted 32 lines
xen/arch/riscv/include/asm/aplic.h line 21
Quoted 2 lines
xen/arch/riscv/include/asm/aplic.h +2
-0+#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_EIID_MASK 0x7ff... what (set of) register(s) do these apply to? Perhaps ...
Quoted 27 lines
xen/arch/riscv/include/asm/aplic.h +23
-0@@ -26,6 +31,36 @@#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_LAST 0x0ffc + +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIP_LAST 0x1c7c +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIP_LAST 0x1d7c +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIE_LAST 0x1e7c +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIE_LAST 0x1f7c +#define APLIC_CLRIENUM 0x1fdc +Quoted 8 lines
xen/arch/riscv/include/asm/aplic.h line 62
Quoted 6 lines
xen/arch/riscv/include/asm/aplic.h +6
-0+#define APLIC_SETIPNUM_LE 0x2000 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_LAST 0x3ffc... these? And then is ...
Yes, it is applied to target register.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
xen/arch/riscv/include/asm/aplic.h line 64
Quoted 1 line
xen/arch/riscv/include/asm/aplic.h +0
-0#define APLIC_TARGET_HART_IDX_SHIFT 18... this also covering some part of them? Can't they (a) live together and (b) have some kind of connection to what they apply to?
Agreed, they could live together. The intent was simply to keep all MMIO definitions in sequence,
That would also be fine, but then please add a comment to the register field definition groups.
Jan
Quoted 2 lines
but I'm okay with grouping macros related to a
specific register together.
Re: [PATCH v2 15/26] xen/riscv: introduce (de)initialization helpers for vINTC
In reply to Jan BeulichQuoted 30 lines · Jan Beulich
Quoted 30 lines
Quoted 30 lines
xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 111
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+switch ( ver ) + { +case INTC_APLIC : + ret =domain_vaplic_init (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);If we take this path for whatever reason, ...
Quoted 8 lines
xen/arch/riscv/intc.c +8
-0+break ; + } + +return ret; +} + +void domain_vintc_deinit (struct domain *d) +{xen/arch/riscv/intc.c int __init make_intc_domU_node(struct kernel_info *kinfo) · line 129
Quoted 10 lines
xen/arch/riscv/intc.c +10
-0+const enum intc_version ver = intc_hw_ops->info->hw_version; + +switch ( ver ) + { +case INTC_APLIC : +domain_vaplic_deinit (d); +break ; + +default : +printk ("vintc (ver:%d) isn't implemented\n" , ver);... we're also going to take this path (very quickly afterwards), just to get the same message twice without it being clear why it appears twice.
Quoted 4 lines
Quoted 2 lines
I missed to add
printk("%s: ...", __func__, ...). I will update correspondingly if we will follow this way.That's not quite what I was after, though. I question the need for this latter message.
I think I got you, and I agree that when someone decides to add support for a new IC to domain_vintc_init(), it is expected that domain_vintc_deinit() will be updated as well. So there is no real need to have the same or a very similar message in domain_vintc_deinit().
I will remove the message from domain_vintc_deinit().
Thanks.
~ Oleksii
Re: [PATCH v2 17/26] xen/riscv: generate IMSIC DT node for guest domains
In reply to Jan BeulichQuoted 86 lines · Jan Beulich
Quoted 85 lines · Oleksii Kurochko
Quoted 57 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Guests using the IMSIC interrupt controller require a corresponding Device Tree description.
Add support for generating an IMSIC node when building the guest DT. This allows guests to discover and use the IMSIC interrupt controller.
Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- s/imsic_make_reg_property/guest_imsic_make_reg_property.
- s/imsic_set_interrupt_extended_prop/guest_imsic_set_interrupt_extended_prop.
- Use initalizer for regs[] array inimsic_make_reg_property().
- Move buf[] insde thefor()loop.
- Correct check of returned phandle.
- Drop local variable len.
- /s/XVFREE/xvfree inimsic_set_interrupt_extended_prop().
- Drop initializer for local variable data.
- s/uint32_t/unsinged int for pos and cpu inimsic_set_interrupt_extended_prop().
- Dropnext_phandleas it is now in common code.
- Introducevcpu_imsic_deinit.
- Refactorvimsic_make_domu_dt_node()to avoid usage of host IMSIC dt node.--- xen/arch/riscv/imsic.c | 127 +++++++++++++++++++++- xen/arch/riscv/include/asm/guest-layout.h | 2 + 2 files changed, 128 insertions(+), 1 deletion(-)xen/arch/riscv/imsic.c +3
-0@@ -13,9 +13,12 @@#include <xen/const.h> #include <xen/cpumask.h> #include <xen/device_tree.h> +#include <xen/domain.h> #include <xen/errno.h> +#include <xen/fdt-domain-build.h> #include <xen/fdt-kernel.h> #include <xen/init.h> +#include <xen/libfdt/libfdt.h> #include <xen/macros.h> #include <xen/sched.h> #include <xen/smp.h> xen/arch/riscv/imsic.c static struct imsic_config imsic_cfg = { · line 41
Quoted 4 lines
xen/arch/riscv/imsic.c +1
-0@@ -35,6 +38,11 @@ static struct imsic_config imsic_cfg = { .lock = SPIN_LOCK_UNLOCKED, }; +static unsigned int __ro_after_init guest_num_msis;How come this is
__ro_after_init, when it's ...Quoted 4 lines
xen/arch/riscv/imsic.c +0
-0@@ -291,6 +299,11 @@ static int imsic_parse_node(const struct dt_device_node *node,return -ENOENT ; }Quoted 6 lines
xen/arch/riscv/imsic.c static int imsic_parse_node(const struct dt_device_node *node, · line 305
Quoted 4 lines
xen/arch/riscv/imsic.c +4
-0+if (dt_property_read_u32 (node,"riscv,num-guest-ids" , &tmp) ) + guest_num_msis = tmp; +else + guest_num_msis = imsic_cfg.nr_ids;... written by a non-__init function?
__ro_after_initshould be dropped. I will use__read_mostlyinstead.Plus are you again inheriting a host
Quoted 1 line
property into guests without saying why?
Based on the discussion for similar place around vAPLIC code in the previous patch I will add here or the comment why or introduce
VIMISC_NR_IDSand use it instead.Quoted 8 lines
xen/arch/riscv/imsic.c int __init imsic_init(const struct dt_device_node *node) · line 540
Quoted 6 lines
xen/arch/riscv/imsic.c +1
-0@@ -524,8 +537,120 @@ int __init imsic_init(const struct dt_device_node *node)return rc; } +static int __init guest_imsic_make_reg_property (struct domain *d,void *fdt)Same question again as to
__initthroughout here.
Quoted 7 lines
Quoted 3 lines
From Arm code I understood that libxl creates the interrupt controller
node in userspace before handing the FDT to Xen so this and related functions won't be called by userspace.Hmm, okay, on the v3 submission I'll have to keep that in mind, and call paths will need checking accordingly.
I will update the commit message with clarification why __init is used here and connected functions.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
xen/arch/riscv/include/asm/guest-layout.h line 10
Quoted 9 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0@@ -5,6 +5,8 @@#define GUEST_APLIC_S_BASE 0xd000000 +#define GUEST_IMSIC_S_BASE 0x28000000 +#define GUEST_RAM_BANKS 2Is this going to become an unannotated collection of (seemingly) random numbers?
Quoted 8 lines
Quoted 3 lines
Could you please clarify what kind of annotation you would expect to see here? It's just an address, which is typically used for IMSIC by QEMU, so I chose the same notation for the guest address as well.
Well, how could I have known? The description says nothing, and there are also no comments. The numbers here look entirely arbitrary, and if someone wanted to change them, it wouldn't be clear at all what constraints need taking into consideration.
I will add the comment or update the commit message with information why this numbers were choosen.
Thanks.
~ Oleksii
Re: [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
In reply to Oleksii KurochkoQuoted 13 lines · Oleksii Kurochko
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ +static const struct vintc_ops vintc_ops = { + .vcpu_init = vcpu_vaplic_init, +}; + +int __init domain_vaplic_init(struct domain *d)Why
__init, and why is there no caller?The caller is in follow-up patch. I will add that to commit message.
Considering that
domain_vintc_init()isn't__initfrom wheredomain_vaplic_init()is called then__initshould be dropped here. I will do that.
Quoted 7 lines
Plus why is the vCPU-init a hook,
Quoted 2 lines
but the domain init is not? Either you mean to allow for other ICs, or you you don't.
IIUC your question
domain_vaplic_init()ins't a hook because vaplic structure is allocated dynamically so vintc,vintc->opsand/or vintc-Quoted 1 line
init_opsaren't initialized at the moment when vintc->{ops orinit_ops}->domain_vaplic_init() is used in
domain_vintc_init()(which is introduced in the follow up patch).
As an alternative it could be that vintc->init_ops are moved as a variable to int.c and then domain_vaplic_(de)init will be just a hook. Something like:
$ git diff
xen/arch/riscv/aplic.c +2 -0
@@ -28,6 +28,7 @@#include <asm/imsic.h> #include <asm/intc.h> #include <asm/io.h> +#include <asm/vaplic.h> #include <asm/riscv_encoding.h> #define APLIC_DEFAULT_PRIORITY 1
@@ -394,6 +395,7 @@ static int __init aplic_preinit(struct dt_device_node *node, const void *dat) dt_irq_xlate = aplic_irq_xlate;register_intc_ops (&aplic_init_ops); +register_vintc_init_ops (&vaplic_vintc_init_ops);/* Enable supervisor external interrupt */ csr_set (CSR_SIE ,BIT (IRQ_S_EXT ,UL ));
xen/arch/riscv/include/asm/intc.h +3 -1
@@ -56,6 +56,8 @@ struct intc_hw_init_ops { };struct vintc_init_ops { +int (*init)(struct domain *d); +void (*deinit)(struct domain *d);/* Create interrupt controller node for domain */ int (*make_domu_dt_node)(struct kernel_info *kinfo); };
@@ -79,13 +81,13 @@ struct vintc_ops {struct vintc {unsigned int irq_nums;unsigned long *allocated_irqs; -const struct vintc_init_ops *init_ops;const struct vintc_ops *ops; };void intc_preinit (void );void register_intc_ops (const struct intc_hw_init_ops *init_ops); +void register_vintc_init_ops (const struct vintc_init_ops *ops);void intc_init (void );
xen/arch/riscv/include/asm/vaplic.h +1 -4
@@ -15,8 +15,6 @@#include <asm/intc.h> -struct domain ; -#define to_vaplic (d) container_of(d->arch.vintc, struct vaplic, vintc)struct vaplic_regs {
@@ -31,7 +29,6 @@ struct vaplic { paddr_t regs_size; }; -int domain_vaplic_init (struct domain *d); -void domain_vaplic_deinit (struct domain *d); +extern const struct vintc_init_ops vaplic_vintc_init_ops;#endif /* ASM__RISCV__VAPLIC_H */
xen/arch/riscv/intc.c +12 -29
78 lines not on this page · raw message
xen/arch/riscv/vaplic.c +8 -4
@@ -397,7 +397,12 @@ static int __init cf_check vaplic_make_domu_dt_node(struct kernel_info *kinfo)return fdt_end_node (fdt); } -static const struct vintc_init_ops __initdata init_ops = { +static int domain_vaplic_init (struct domain *d); +static void domain_vaplic_deinit (struct domain *d); + +const struct vintc_init_ops vaplic_vintc_init_ops = { + .init = domain_vaplic_init, + .deinit = domain_vaplic_deinit, .make_domu_dt_node = vaplic_make_domu_dt_node, };
@@ -408,7 +413,7 @@ static const struct vintc_ops vintc_ops = { .emulate_load = vaplic_emulate_load, }; -int __init domain_vaplic_init (struct domain *d) +static int domain_vaplic_init (struct domain *d) { struct vaplic *vaplic = xvzalloc(struct vaplic);
@@ -417,7 +422,6 @@ int __init domain_vaplic_init(struct domain *d) d->arch.vintc = &vaplic->vintc; d->arch.vintc->ops = &vintc_ops; - d->arch.vintc->init_ops = &init_ops; vaplic->regs.domaincfg =APLIC_DOMAINCFG_IE |APLIC_DOMAINCFG_DM |APLIC_DOMAINCFG_RO80 ;
@@ -428,7 +432,7 @@ int __init domain_vaplic_init(struct domain *d)return 0 ; } -void __init domain_vaplic_deinit (struct domain *d) +static void domain_vaplic_deinit (struct domain *d) {struct vaplic *vaplic =to_vaplic (d);
The downside is that we still need register_vintc_init_ops() which will called from real interrupt controller code.
Would it be better solution?
~ Oleksii
Re: [PATCH v2 14/26] xen/riscv: add very early virtual APLIC (vAPLIC) initialization support
Quoted 24 lines · Oleksii Kurochko
On 6/4/26 1:29 PM, Oleksii Kurochko wrote:
xen/arch/riscv/intc.c +2
-12@@ -105,29 +111,18 @@ int intc_route_irq_to_guest(struct irq_desc *desc,int __init make_intc_domU_node (struct kernel_info *kinfo) { -struct vintc *vintc = kinfo->bd.d->arch.vintc; - -ASSERT (vintc->init_ops && vintc->init_ops->make_domu_dt_node); -return vintc->init_ops->make_domu_dt_node (kinfo); +return vintc_init_ops->make_domu_dt_node (kinfo); }int domain_vintc_init (struct domain *d) { -int ret = -EOPNOTSUPP ; -const enum intc_version ver = intc_hw_ops->info->hw_version; - -switch ( ver ) - { -case INTC_APLIC : - ret =domain_vaplic_init (d); -break ; +int ret;
xen/arch/riscv/intc.c int intc_route_irq_to_guest(struct irq_desc *desc, · line 126
Quoted 10 lines
xen/arch/riscv/intc.c +1
-4-default : -printk ("vintc (ver:%d) isn't implemented\n" , ver); -break ; - } + ret = vintc_init_ops->init (d);if ( !ret ) { d->arch.vintc->allocated_irqs =
Thinking about it some more, in particular the longer term case of domains not inheriting HW properties may end up better without this change, i.e. keeping the switch() that you had (and a direct call).
Jan
Re: [PATCH v2 21/26] xen/riscv: implement virtual APLIC MMIO emulation
In reply to Oleksii KurochkoQuoted 62 lines · Oleksii Kurochko
Guests running under Xen program interrupt routing by writing to APLIC MMIO registers. Xen must intercept these accesses to enforce interrupt isolation between domains and to translate guest routing intent into the underlying physical MSI topology.
Writes are gated by the domain's authorised interrupt bitmap so that a guest cannot affect interrupts it does not own. TARGET register writes additionally require translation of the hart and IMSIC guest-file indices from virtual to physical, as the APLIC uses these fields directly to compute the MSI delivery address.
Delegation (
APLIC_SOURCECFG_D) is not yet supported.Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- Merge the following patches into one:
xen/riscv: add vaplic access check:
- Add check that address is properly aligned.
- Check vaplic range intead of APLIC one.
- Return bool fromvaplic_is_accessinstead of int.
xen/riscv: emulate guest writes to virtual APLIC MMIO
- DropCALC_REG_VALUE.
- Use unsigned int instead ofuin32_tfor offset.
- s/.../subtracting in the comment.
- start one line comments from the upper case.
- Check the value before being written to sourcecfg register.
- 'unsigned int' for loop index.
- Omit unneessary braces.
- s/vaplic_update_target/aplic_msi_target_gen.
- UseIMSIC_MMIO_PAGE_SHIFTinstead of 12 inaplic_msi_target_gen().
- Drop explicit usage of APLIC register in store function.
- Drop APLIC_REG_{GET,SET} macros and introudce APLIC specific funtcions.
- Ignore write toSOURCECFG_BASEwhen value is out-of-range.
- DropASSERT(!target_vcpu)inside handler of targer register setting, just avoid such writings + debug message.
-domain_crash()instead ofpanic()in the case of default case.
- DropASSERT()inAPLIC_SOURCE_CFG_BASEcase and usedomain_crash()instead.
xen/riscv: emulate guest reads from virtual APLIC MMIO:
- s/regval_to_irqn/regindx_to_irqn.
- pass toto_vaplic()a domain instead of vintc.
- add check that load access is aligned.
- instead ofpanic()just crash adomain().
- use 'unsigned int' for local variable offset.
- Return 0 in the caseAPLIC_CLRIE_BASE...APLIC_CLRIE_LAST reading to follow AIA spec.
- Drop explicit usage of physical APLIC registers.--- xen/arch/riscv/aplic.c | 25 +++ xen/arch/riscv/include/asm/aplic.h | 9 + xen/arch/riscv/include/asm/intc.h | 10 +- xen/arch/riscv/include/asm/vaplic.h | 3 + xen/arch/riscv/vaplic.c | 289 +++++++++++++++++++++++++++- 5 files changed, 333 insertions(+), 3 deletions(-)xen/arch/riscv/aplic.c +0
-0
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 48
Quoted 9 lines
xen/arch/riscv/aplic.c +6
-0@@ -40,6 +40,31 @@ static struct intc_info __ro_after_init aplic_info = { .hw_version = INTC_APLIC, }; +uint32_t aplic_hw_read_reg (unsigned int offset,uint32_t mask) +{ +unsigned long flags; +uint32_t val; + +ASSERT (offset < aplic.size);
Further assert suitable alignment of "offset"?
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 51
Quoted 2 lines
xen/arch/riscv/aplic.c +2
-0+spin_lock_irqsave (&aplic.lock, flags); + val =readl ((void __iomem *)((uintptr_t )aplic.regs + offset)) & mask;
Easier as
val = readl((volatile void __iomem *)aplic.regs + offset) & mask;
? (Note that like const, volatile also shouldn't be cast away.)
xen/arch/riscv/include/asm/aplic.h line 18
Quoted 7 lines
xen/arch/riscv/include/asm/aplic.h +1
-0@@ -15,6 +15,8 @@#include <asm/imsic.h> +#define APLIC_NUM_REGS 32
What's this? It isn't used afaics, and it also doesn't match up with ...
Quoted 19 lines
xen/arch/riscv/include/asm/aplic.h +4
-0@@ -24,6 +26,8 @@#define APLIC_DOMAINCFG_IE BIT(8, U)#define APLIC_DOMAINCFG_DM BIT(2, U) +#define APLIC_SOURCECFG_D BIT(10, U) +#define APLIC_SOURCECFG_SM_INACTIVE 0x0#define APLIC_SOURCECFG_SM_DETACH 0x1#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4@@ -71,6 +75,8 @@#define APLIC_SIZE (nr_cpus) (APLIC_MIN_SIZE + \ APLIC_SIZE_ALIGN(APLIC_IDC_SIZE * (nr_cpus))) +#define APLIC_SETCLR_OFFSET_MASK ((32 * sizeof(uint32_t)) - 1) +struct aplic_regs {uint32_t domaincfg;/* 0x0000 */ uint32_t sourcecfg[1023 ];/* 0x0004 */
xen/arch/riscv/include/asm/aplic.h struct aplic_regs { · line 121
Quoted 2 lines
xen/arch/riscv/include/asm/aplic.h +0
-0@@ -114,4 +120,7 @@ struct aplic_regs {uint32_t target[1023 ];/* 0x3008 */ };
... this struct holding all the (far more than 32) registers.
As to APLIC_SETCLR_OFFSET_MASK: Any reason it has the low 2 bits set? The literal 32 in there also looks rather arbitrary. It would be helpful if there was a connection to those 32-s in struct aplic_regs that it actually matches up with.
xen/arch/riscv/include/asm/vaplic.h struct vaplic_regs { · line 31
Quoted 9 lines
xen/arch/riscv/include/asm/vaplic.h +3
-0@@ -26,6 +26,9 @@ struct vaplic_regs {struct vaplic {struct vintc vintc;struct vaplic_regs regs; + +paddr_t regs_start; +paddr_t regs_size;
Can regs_size really go beyond 4G?
Quoted 7 lines
xen/arch/riscv/vaplic.c +1
-0@@ -26,6 +26,283 @@#define FDT_VAPLIC_INT_CELLS 2 +#define AUTH_IRQ_BIT (d, irqn) ( \
xen/arch/riscv/vaplic.c line 39
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+ ((irqn) <= (d)->arch.vintc->irq_nums) && \ + test_bit(irqn, (d)->arch.vintc->allocated_irqs) ) + +#define regindx_to_irqn (reg_val) ((reg_val) / sizeof(uint32_t)) + +static inline uint32_t generate_auth_mask (const struct domain *d, +unsigned int irqsn) +{ +if ( irqsn >=DIV_ROUND_UP (d->arch.vintc->irq_nums, +sizeof (uint32_t ) *BITS_PER_BYTE ) )
Why the rounding up? Isn't ->irqs_num the proper upper bound?
xen/arch/riscv/vaplic.c line 41
Quoted 2 lines
xen/arch/riscv/vaplic.c +2
-0+ { +dprintk (XENLOG_DEBUG ,"incorrect irqsn(%d) is passed\n" , irqsn);
Once again: %u please with an unsigned int argument.
xen/arch/riscv/vaplic.c line 46
Quoted 4 lines
xen/arch/riscv/vaplic.c +4
-0+return 0U ; + } + +return *((uint32_t *)d->arch.vintc->allocated_irqs + irqsn);
Such casts would better be avoided
xen/arch/riscv/vaplic.c line 49
Quoted 1 line
xen/arch/riscv/vaplic.c +1
-0+static int vaplic_emulate_load(const struct vcpu *vcpu,
v please for struct vcpu * variables (in the common case, of course there can be exceptions).
Quoted 11 lines
xen/arch/riscv/vaplic.c +11
-0+ const unsigned long addr, uint32_t *out) +{ + const struct domain *d = vcpu->domain; +const struct vaplic *vaplic =to_vaplic (d); +const unsigned int offset = addr &APLIC_REG_OFFSET_MASK ; +uint32_t auth_mask; +unsigned int i; + +switch ( offset ) + { +case APLIC_DOMAINCFG :
xen/arch/riscv/vaplic.c line 70
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+ *out = vaplic->regs.domaincfg; + +return 0 ; + +case APLIC_SETIPNUM : +case APLIC_SETIPNUM_LE : +case APLIC_CLRIPNUM : +case APLIC_SETIENUM : +case APLIC_CLRIENUM : + case APLIC_CLRIE_BASE ... APLIC_CLRIE_LAST:
For ranges like this APLIC_REG_OFFSET_MASK having the low two bits clear (or there being some other mechanism to ensure only properly aligned offsets are handled) would help. Mis-aligned accesses shouldn't be handled ...
xen/arch/riscv/vaplic.c line 77
Quoted 7 lines
xen/arch/riscv/vaplic.c +7
-0+/* +* Based on the RISC-V AIA spec a read of these registers +* always returns zero +*/ + *out =0 ; + +return 0 ;
... like this.
xen/arch/riscv/vaplic.c line 89
Quoted 11 lines
xen/arch/riscv/vaplic.c +11
-0+ case APLIC_SETIP_BASE ... APLIC_SETIP_LAST: + case APLIC_CLRIP_BASE ... APLIC_CLRIP_LAST: + case APLIC_SETIE_BASE ... APLIC_SETIE_LAST: + i =regindx_to_irqn (offset &APLIC_SETCLR_OFFSET_MASK ); + auth_mask =generate_auth_mask (d, i); + +break ; + + case APLIC_TARGET_BASE ... APLIC_TARGET_LAST: +/* +* As target registers start for 1:
s/for/from/ ?
xen/arch/riscv/vaplic.c line 96
Quoted 7 lines
xen/arch/riscv/vaplic.c +7
-0+* 0x3000 genmsi +* 0x3004 target[1] +* 0x3008 target[2] +* ... +* 0x3FFC target[1023] +* It is necessary to calculate an interrupt number by substracting +* of APLIC_GENMSI instead of APLIC_TARGET_BASE.
Stray "of"? Or did you mean "subtraction"? (Also check other similar comments.)
xen/arch/riscv/vaplic.c line 120
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+default : +gdprintk (XENLOG_WARNING ,"Unhandled APLIC read at offset %#x\n" , + offset); + +domain_crash (vcpu->domain); + +return -EINVAL ; + } + + *out =aplic_hw_read_reg (offset, auth_mask);
You blindly assume a 32-bit access here (and also in the write counterpart). How do you end up knowing?
xen/arch/riscv/vaplic.c line 148
Quoted 2 lines
xen/arch/riscv/vaplic.c +2
-0+static int cf_check vaplic_emulate_store(const struct vcpu *vcpu, +unsigned long addr,uint32_t value)
Why cf_check here but not for vaplic_emulate_load()?
Quoted 64 lines
xen/arch/riscv/vaplic.c +64
-0+{ +int rc = -EINVAL ; +const struct domain *d = vcpu->domain; +unsigned int offset = addr &APLIC_REG_OFFSET_MASK ; + +switch ( offset ) + { + case APLIC_SETIP_BASE ... APLIC_SETIP_LAST: + case APLIC_CLRIP_BASE ... APLIC_CLRIP_LAST: + case APLIC_SETIE_BASE ... APLIC_SETIE_LAST: + case APLIC_CLRIE_BASE ... APLIC_CLRIE_LAST: + { +unsigned int irqn =regindx_to_irqn (offset &APLIC_SETCLR_OFFSET_MASK ); + value &=generate_auth_mask (d, irqn); + +break ; + } + + case APLIC_SOURCECFG_BASE ... APLIC_SOURCECFG_LAST: +if ( value &APLIC_SOURCECFG_D ) + { + rc = -EOPNOTSUPP ; + +dprintk (XENLOG_ERR ,"APLIC_SOURCECFG_D isn't supported\n" ); + + goto fail; + } + +/* +* As sourcecfg register starts from 1: +* 0x0000 domaincfg +* 0x0004 sourcecfg[1] +* 0x0008 sourcecfg[2] +* ... +* 0x0FFC sourcecfg[1023] +* It is necessary to calculate an interrupt number by subtracting +* of APLIC_DOMAINCFG instead of APLIC_SOURCECFG_BASE. +*/ +if ( !AUTH_IRQ_BIT (d,regindx_to_irqn (offset -APLIC_DOMAINCFG )) ) +/* Interrupt not enabled, ignore it */ +return 0 ; + +if ( value >APLIC_SOURCECFG_SM_LEVEL_LOW ) + { +gdprintk (XENLOG_ERR , +"value(%u) is incorrect for sourcecfg register\n" , value); + +return 0 ; + } + +break ; + + case APLIC_TARGET_BASE ... APLIC_TARGET_LAST: + { +struct vcpu *target_vcpu =NULL ; + +/* +* Look at vaplic_emulate_load() for explanation why +* APLIC_GENMSI is subtracted. +*/ +if ( !AUTH_IRQ_BIT (d,regindx_to_irqn (offset -APLIC_GENMSI )) ) +/* Interrupt not enabled, ignore it */ +return 0 ; +
xen/arch/riscv/vaplic.c line 222
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+for (unsigned int i =0 ; i < vcpu->domain->max_vcpus; i++ ) + { +struct vcpu *v = vcpu->domain->vcpu[i]; + +if ( v->vcpu_id == (value >>APLIC_TARGET_HART_IDX_SHIFT ) ) + { + target_vcpu = v; +break ; + } + }
Why is a loop needed here? vCPU-s are numbered sequentially.
xen/arch/riscv/vaplic.c line 291
Quoted 8 lines
xen/arch/riscv/vaplic.c +8
-0+static bool cf_check vaplic_is_access(const struct vcpu *vcpu, +unsigned long addr) +{ +const struct vaplic *vaplic =to_vaplic (vcpu->domain); +paddr_t start = vaplic->regs_start; +paddr_t end = vaplic->regs_start + vaplic->regs_size; + +if ( addr &0x3 )
Nit: Does the 0x here add any value?
xen/arch/riscv/vaplic.c line 297
Quoted 6 lines
xen/arch/riscv/vaplic.c +6
-0+ { +dprintk (XENLOG_DEBUG , +"APLIC MMIO address should be properly aligned\n" ); + +return false; + }
Ah, okay - here is the alignment check. Mind me asking for a (documenting) assertion then in the actual read and write handlers?
xen/arch/riscv/vaplic.c line 299
Quoted 1 line
xen/arch/riscv/vaplic.c +1
-0+/* check if it is an APLIC access */
Nit: Style.
Jan
Re: [PATCH v2 23/26] xen/riscv: initialize RCU, scheduler, and system domains in start_xen()
In reply to Oleksii KurochkoQuoted 7 lines · Oleksii Kurochko
Wire up the missing early-boot initialization steps in
start_xen().The scheduler must be initialized prior to
do_initcalls()becausecpupool_create_pool()is called during initcalls; without it, BUG_ON(IS_ERR(pool)) is triggered insidecpupool_create_pool().Signed-off-by Oleksii Kurochko <[email protected]>
Re: [PATCH v2 25/26] xen/riscv: add initial dom0less infrastructure support
In reply to Oleksii KurochkoQuoted 23 lines · Oleksii Kurochko
Enable dom0less support for RISC-V by selecting
HAS_DOM0LESSand providing the minimal architecture hooks required by the common dom0less infrastructure.Add stub implementations for architecture-specific helpers used when building domains from the device tree. These currently perform no additional work but allow the generic dom0less code to build and run on RISC-V.
Introduce
max_init_domidas a runtime variable rather than a constant so that it can be updated during dom0less domain creation.Provide missing helpers and definitions required by the domain construction code, including domain bitness helpers and the
p2m_set_allocation()prototype.Additionally define the guest magic memory region in the public RISC-V interface.
As
HAS_DOM0LESSis selected for RISC-V now it could be a compilation issue ifCONFIG_STATIC_MEMORY=yasguest_physmap_add_pages()isn't yet provided.
Quoted 8 lines
Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- Move declaration ofp2m_set_allocation()top2m-common.h.
- Add__initdataformax_init_domidand drop initalizer for it.
- AddCONFIG_STATIC_MEMORY=nto CI's randconfig to avoid compilation error because ofguest_physmap_add_pages()isn't provided.
Yet another trap for people to fall into, and yet another item to clean up before the port is really ready to use. Imo there want to be HAS_STATIC_MEMORY, which RISC-V simply wouldn't select (for the time being).
xen/arch/riscv/dom0less-build.c int __init arch_parse_dom0less_node(struct dt_device_node *node, · line 110
Quoted 12 lines
xen/arch/riscv/dom0less-build.c +6
-0@@ -102,3 +102,9 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,return 0 ; } + +int __init arch_handle_passthrough_prop (struct kernel_info *kinfo, +struct dt_device_node *node) +{ +return 0 ; +}
No FIXME comment or anything alike? That is, nothing is going to be needed here even once pass-through is supported?
Quoted 12 lines
xen/arch/riscv/domain-build.c +6
-0@@ -158,9 +158,22 @@ int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo)return fdt_end_node (fdt); } +int __init construct_hwdom (struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +return -EOPNOTSUPP ; +} +
xen/arch/riscv/domain-build.c int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo) · line 176
Quoted 10 lines
xen/arch/riscv/domain-build.c +4
-0int __init make_timer_node (const struct kernel_info *kinfo) {/* There is no need for timer node for RISC-V. */ return 0 ; } + +int __init make_hypervisor_node (struct domain *d, +const struct kernel_info *kinfo, +int addrcells,int sizecells)
The last two parameters being of plain int type is, I suppose, dictated by DT code?
xen/arch/riscv/include/asm/guest-layout.h line 28
Quoted 8 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0@@ -24,4 +24,7 @@#define GUEST_RAM_BANK_BASES { GUEST_RAM0_BASE, GUEST_RAM1_BASE }#define GUEST_RAM_BANK_SIZES { GUEST_RAM0_SIZE, GUEST_RAM1_SIZE } +#define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000) +#define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000)
Why xen_mk_ullong()? That's needed in the public headers only, iirc.
Also these are again two seemingly arbitrary numbers.
Jan
Re: [PATCH v2 26/26] xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable callbacks
In reply to Oleksii KurochkoQuoted 5 lines · Oleksii Kurochko
desc->statusis only set once duringsetup_irq(), but interrupts can be enabled/disabled at runtime, so update it in the corresponding callbacks.
wmb()inaplic_irq_enable()ensuresdo_IRQ(), which can fire immediately after the interrupt is enabled, sees the updateddesc->status.
This doesn't look entirely correct. Aiui ...
Quoted 21 lines
No
rmb()is
needed on thedo_IRQ()side becausedesc->statusis read under a spinlock, which implies an acquire barrier.No barrier is needed in
aplic_irq_disable()because the hardware disables the interrupt before the status is updated, sodo_IRQ()cannot fire, andspin_unlock()makes the updated value visible.Fixes d4676a1398bc5 ("xen/riscv: implementation of aplic and imsic operations")Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- New patch.--- xen/arch/riscv/aplic.c | 5 +++++ xen/arch/riscv/irq.c | 3 --- 2 files changed, 5 insertions(+), 3 deletions(-)
xen/arch/riscv/aplic.c static void cf_check aplic_irq_enable(struct irq_desc *desc) · line 168
Quoted 8 lines
xen/arch/riscv/aplic.c +3
-0@@ -161,6 +161,9 @@ static void cf_check aplic_irq_enable(struct irq_desc *desc)spin_lock (&aplic.lock); + desc->status &= ~IRQ_DISABLED ; +wmb (); +/* Enable interrupt in IMSIC */ imsic_irq_enable (desc->irq);
... you want to order the ->status update ahead of the imsic_irq_enable() operation. Yet that's a CSR write. Do fences really cover that? If not, you may need to resort to mb(), to order the ->status write against the ->irq read, in lieu of being able to order against a CSR write.
Of course with imsic_irq_enable() itself acquiring a lock (which necessarily has a memory write), you could then further argue that wmb() is indeed sufficient, bot for a reason different from the one presently stated in the description. (I would also strongly suggest to annotate the wmb() with an explanatory comment.)
xen/arch/riscv/aplic.c static void cf_check aplic_irq_disable(struct irq_desc *desc) · line 198
Quoted 8 lines
xen/arch/riscv/aplic.c +2
-0@@ -189,6 +192,8 @@ static void cf_check aplic_irq_disable(struct irq_desc *desc)/* Disable interrupt in IMSIC */ imsic_irq_disable (desc->irq); + desc->status |=IRQ_DISABLED ; +spin_unlock (&aplic.lock); }
The ->status write becoming globally visible ahead of imsic_irq_disable() doing its work is not an issue? In the paragraph in the description you again appear to assume that the CSR write and the memory write of the spin-unlock are ordered wrt one another.
xen/arch/riscv/irq.c int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new) · line 148
Quoted 10 lines
xen/arch/riscv/irq.c +0
-3@@ -145,9 +145,6 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new) desc->handler->set_affinity (desc,cpumask_of (smp_processor_id ())); desc->handler->startup (desc); - -/* Enable irq */ - desc->status &= ~IRQ_DISABLED ; }
Arm and x86 are different in this regard, so it's hard to tell which way it ought to be for RISC-V. This removal surely wants a sentence or two in the description.
Jan
Re: [PATCH v2 21/26] xen/riscv: implement virtual APLIC MMIO emulation
In reply to Jan BeulichQuoted 66 lines · Jan Beulich
Quoted 65 lines · Oleksii Kurochko
Guests running under Xen program interrupt routing by writing to APLIC MMIO registers. Xen must intercept these accesses to enforce interrupt isolation between domains and to translate guest routing intent into the underlying physical MSI topology.
Writes are gated by the domain's authorised interrupt bitmap so that a guest cannot affect interrupts it does not own. TARGET register writes additionally require translation of the hart and IMSIC guest-file indices from virtual to physical, as the APLIC uses these fields directly to compute the MSI delivery address.
Delegation (
APLIC_SOURCECFG_D) is not yet supported.Co-developed-by Romain Caritey <[email protected]>Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- Merge the following patches into one:
xen/riscv: add vaplic access check:
- Add check that address is properly aligned.
- Check vaplic range intead of APLIC one.
- Return bool fromvaplic_is_accessinstead of int.
xen/riscv: emulate guest writes to virtual APLIC MMIO
- DropCALC_REG_VALUE.
- Use unsigned int instead ofuin32_tfor offset.
- s/.../subtracting in the comment.
- start one line comments from the upper case.
- Check the value before being written to sourcecfg register.
- 'unsigned int' for loop index.
- Omit unneessary braces.
- s/vaplic_update_target/aplic_msi_target_gen.
- UseIMSIC_MMIO_PAGE_SHIFTinstead of 12 inaplic_msi_target_gen().
- Drop explicit usage of APLIC register in store function.
- Drop APLIC_REG_{GET,SET} macros and introudce APLIC specific funtcions.
- Ignore write toSOURCECFG_BASEwhen value is out-of-range.
- DropASSERT(!target_vcpu)inside handler of targer register setting, just avoid such writings + debug message.
-domain_crash()instead ofpanic()in the case of default case.
- DropASSERT()inAPLIC_SOURCE_CFG_BASEcase and usedomain_crash()instead.
xen/riscv: emulate guest reads from virtual APLIC MMIO:
- s/regval_to_irqn/regindx_to_irqn.
- pass toto_vaplic()a domain instead of vintc.
- add check that load access is aligned.
- instead ofpanic()just crash adomain().
- use 'unsigned int' for local variable offset.
- Return 0 in the caseAPLIC_CLRIE_BASE...APLIC_CLRIE_LAST reading to follow AIA spec.
- Drop explicit usage of physical APLIC registers.--- xen/arch/riscv/aplic.c | 25 +++ xen/arch/riscv/include/asm/aplic.h | 9 + xen/arch/riscv/include/asm/intc.h | 10 +- xen/arch/riscv/include/asm/vaplic.h | 3 + xen/arch/riscv/vaplic.c | 289 +++++++++++++++++++++++++++- 5 files changed, 333 insertions(+), 3 deletions(-)xen/arch/riscv/aplic.c +0
-0@@ -40,6 +40,31 @@ static struct intc_info __ro_after_init aplic_info = { .hw_version = INTC_APLIC, };
Quoted 8 lines
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 48
Quoted 6 lines
xen/arch/riscv/aplic.c +6
-0+uint32_t aplic_hw_read_reg (unsigned int offset,uint32_t mask) +{ +unsigned long flags; +uint32_t val; + +ASSERT (offset < aplic.size);Further assert suitable alignment of "offset"?
Sure, I will update ASSERT(...) to:
ASSERT((offset < aplic.size) && IS_ALIGNED(offset, sizeof(uint32_t)));
Quoted 9 lines
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 51
Quoted 3 lines
xen/arch/riscv/aplic.c +2
-0+spin_lock_irqsave (&aplic.lock, flags); + val =readl ((void __iomem *)((uintptr_t )aplic.regs + offset)) & mask;Easier as
val = readl((volatile void __iomem *)aplic.regs + offset) & mask;? (Note that like const, volatile also shouldn't be cast away.)
Is arithmetic on void * pointers correct from the C standard's point of view?
It works with GCC (see https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html), but I can't find anything that guarantees the same behavior for other compilers.
I'm okay with the suggested change if it's acceptable for Xen to rely on GCC's void * pointer arithmetic extension.
Quoted 10 lines
xen/arch/riscv/include/asm/aplic.h line 18
Quoted 8 lines
xen/arch/riscv/include/asm/aplic.h +1
-0@@ -15,6 +15,8 @@#include <asm/imsic.h> +#define APLIC_NUM_REGS 32What's this? It isn't used afaics, and it also doesn't match up with ...
It is rudement and isn't used anymore. It should be just dropped.
Quoted 25 lines
Quoted 20 lines
xen/arch/riscv/include/asm/aplic.h +4
-0@@ -24,6 +26,8 @@#define APLIC_DOMAINCFG_IE BIT(8, U)#define APLIC_DOMAINCFG_DM BIT(2, U) +#define APLIC_SOURCECFG_D BIT(10, U) +#define APLIC_SOURCECFG_SM_INACTIVE 0x0#define APLIC_SOURCECFG_SM_DETACH 0x1#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4@@ -71,6 +75,8 @@#define APLIC_SIZE (nr_cpus) (APLIC_MIN_SIZE + \ APLIC_SIZE_ALIGN(APLIC_IDC_SIZE * (nr_cpus))) +#define APLIC_SETCLR_OFFSET_MASK ((32 * sizeof(uint32_t)) - 1) +struct aplic_regs {uint32_t domaincfg;/* 0x0000 */ uint32_t sourcecfg[1023 ];/* 0x0004 */ xen/arch/riscv/include/asm/aplic.h struct aplic_regs { · line 121
Quoted 2 lines
xen/arch/riscv/include/asm/aplic.h +0
-0@@ -114,4 +120,7 @@ struct aplic_regs {uint32_t target[1023 ];/* 0x3008 */ };... this struct holding all the (far more than 32) registers.
Quoted 1 line
As to
APLIC_SETCLR_OFFSET_MASK: Any reason it has the low 2 bits set?
Agree, with the current defintion of AIA spec. low 2 bits are always 0 so APLIC_SETCLR_OFFSET_MASK should be updated correspondingly ...
The
Quoted 3 lines
literal 32 in there also looks rather arbitrary. It would be helpful if there was a connection to those 32-s in struct
aplic_regsthat it actually matches up with.
t could be updated to make the relationship clearer:
#define APLIC_SETCLR_OFFSET_MASK (sizeof_field(struct aplic_regs, setip) - 1)
Using setip is fine here, as all SET* and CLR* register groups consist of 32 registers and therefore have identical sizes. Adding a brief comment above APLIC_SETCLR_OFFSET_MASK to explain this could improve readability:
/*
* Using setip is fine here, as all SET* and CLR* register groups
consist of 32
* registers and therefore have identical sizes.
*/
#define APLIC_SETCLR_OFFSET_MASK (sizeof_field(struct aplic_regs, setip) - 1)
Also, BUILD_BUG_ON() could be added to aplic_preinit() to verify what is mentioned in the comment if it makes sense.
...:
#define APLIC_SETCLR_OFFSET_MASK
(sizeof_field(struct aplic_regs, setip) - sizeof(uint32_t))
Quoted 12 lines
xen/arch/riscv/include/asm/vaplic.h struct vaplic_regs { · line 31
Quoted 10 lines
xen/arch/riscv/include/asm/vaplic.h +3
-0@@ -26,6 +26,9 @@ struct vaplic_regs {struct vaplic {struct vintc vintc;struct vaplic_regs regs; + +paddr_t regs_start; +paddr_t regs_size;Can
regs_sizereally go beyond 4G?
Good question and it depends on an amount of vCPUs:
#define APLIC_MIN_SIZE 0x4000
#define APLIC_SIZE_ALIGN(x) ROUNDUP(x, APLIC_MIN_SIZE)
#define APLIC_SIZE(nr_cpus) (APLIC_MIN_SIZE + \
APLIC_SIZE_ALIGN(APLIC_IDC_SIZE *
(nr_cpus)))
paddr_t aplic_size = APLIC_SIZE(d->max_vcpus);
With the current limitation of 128 vCPUs max (IIRC) it won't beyond 4G.
Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/vaplic.c +0
-0@@ -26,6 +26,283 @@#define FDT_VAPLIC_INT_CELLS 2
Quoted 13 lines
xen/arch/riscv/vaplic.c line 39
Quoted 11 lines
xen/arch/riscv/vaplic.c +11
-0+#define AUTH_IRQ_BIT (d, irqn) ( \ + ((irqn) <= (d)->arch.vintc->irq_nums) && \ + test_bit(irqn, (d)->arch.vintc->allocated_irqs) ) + +#define regindx_to_irqn (reg_val) ((reg_val) / sizeof(uint32_t)) + +static inline uint32_t generate_auth_mask (const struct domain *d, +unsigned int irqsn) +{ +if ( irqsn >=DIV_ROUND_UP (d->arch.vintc->irq_nums, +sizeof (uint32_t ) *BITS_PER_BYTE ) )Why the rounding up? Isn't ->irqs_num the proper upper bound?
Probably irqsn isn't a correct name here as it looks like "IRQ source number" (an IRQ number), but regindx_to_irqn(offset & MASK) actually computes offset / 4 - a word index into the used_irqs bitmap. Word 0 covers IRQs 0–31, word 1 covers IRQs 32–63, etc. Given that, the bounds check:
irqsn >= DIV_ROUND_UP(irq_nums, sizeof(uint32_t) * BITS_PER_BYTE)
is "word index >= number of 32-bit words in the bitmap" which is correct. The DIV_ROUND_UP converts the IRQ count into a word count to compare against the word index.
So the real issue is naming. There are two options to resolve it:
- rename to reflect reality; call it word_idx (not irqsn), and rename regindx_to_irqn to something like regoffset_to_word_idx. The DIV_ROUND_UP check then reads clearly.
- Change the API to take an actual IRQ number, pass irqsn * 32 (the first IRQ in the word) and check irqsn >= irq_nums directly, computing the word index inside generate_auth_mask. This aligns with how AUTH_IRQ_BIT works.
Which one option do you prefer?
Quoted 4 lines
xen/arch/riscv/vaplic.c line 41
Quoted 3 lines
xen/arch/riscv/vaplic.c +2
-0+ { +dprintk (XENLOG_DEBUG ,"incorrect irqsn(%d) is passed\n" , irqsn);
Quoted 8 lines
Once again: %u please with an unsigned int argument.
xen/arch/riscv/vaplic.c line 46
Quoted 4 lines
xen/arch/riscv/vaplic.c +4
-0+return 0U ; + } + +return *((uint32_t *)d->arch.vintc->allocated_irqs + irqsn);Such casts would better be avoided
It was just a convenient way to find necessary word.
Would it be better to have the following:
return (uint32_t)(d->arch.vintc->used_irqs[(irqsn * 32) /BITS_PER_LONG] >> ((irqsn * 32) % BITS_PER_LONG));
Quoted 41 lines
xen/arch/riscv/vaplic.c line 49
Quoted 2 lines
xen/arch/riscv/vaplic.c +1
-0+static int vaplic_emulate_load(const struct vcpu *vcpu,v please for struct vcpu * variables (in the common case, of course there can be exceptions).
Quoted 11 lines
xen/arch/riscv/vaplic.c +11
-0+ const unsigned long addr, uint32_t *out) +{ + const struct domain *d = vcpu->domain; +const struct vaplic *vaplic =to_vaplic (d); +const unsigned int offset = addr &APLIC_REG_OFFSET_MASK ; +uint32_t auth_mask; +unsigned int i; + +switch ( offset ) + { +case APLIC_DOMAINCFG :xen/arch/riscv/vaplic.c line 70
Quoted 10 lines
xen/arch/riscv/vaplic.c +10
-0+ *out = vaplic->regs.domaincfg; + +return 0 ; + +case APLIC_SETIPNUM : +case APLIC_SETIPNUM_LE : +case APLIC_CLRIPNUM : +case APLIC_SETIENUM : +case APLIC_CLRIENUM : + case APLIC_CLRIE_BASE ... APLIC_CLRIE_LAST:For ranges like this
APLIC_REG_OFFSET_MASKhaving the low two bits clear (or there being some other mechanism to ensure only properly aligned offsets are handled) would help. Mis-aligned accesses shouldn't be handled ...xen/arch/riscv/vaplic.c line 77
Quoted 7 lines
xen/arch/riscv/vaplic.c +7
-0+/* +* Based on the RISC-V AIA spec a read of these registers +* always returns zero +*/ + *out =0 ; + +return 0 ;
Quoted 1 line
... like this.
There is such check in vaplic_is_access() below. I will add a comment above vaplic_emulate_load() that offset is properly aligned because of the check in vaplic_is_access(). The similar comment I'll add for vaplic_emulate_store().
Quoted 14 lines
xen/arch/riscv/vaplic.c line 89
Quoted 12 lines
xen/arch/riscv/vaplic.c +11
-0+ case APLIC_SETIP_BASE ... APLIC_SETIP_LAST: + case APLIC_CLRIP_BASE ... APLIC_CLRIP_LAST: + case APLIC_SETIE_BASE ... APLIC_SETIE_LAST: + i =regindx_to_irqn (offset &APLIC_SETCLR_OFFSET_MASK ); + auth_mask =generate_auth_mask (d, i); + +break ; + + case APLIC_TARGET_BASE ... APLIC_TARGET_LAST: +/* +* As target registers start for 1: s/for/from/ ?
I'll apply that.
Quoted 10 lines
xen/arch/riscv/vaplic.c line 96
Quoted 8 lines
xen/arch/riscv/vaplic.c +7
-0+* 0x3000 genmsi +* 0x3004 target[1] +* 0x3008 target[2] +* ... +* 0x3FFC target[1023] +* It is necessary to calculate an interrupt number by substracting +* of APLIC_GENMSI instead of APLIC_TARGET_BASE. Stray "of"? Or did you mean "subtraction"? (Also check other similar comments.)
Agree, 'of' should be dropped here.
Quoted 14 lines
xen/arch/riscv/vaplic.c line 120
Quoted 11 lines
xen/arch/riscv/vaplic.c +10
-0+default : +gdprintk (XENLOG_WARNING ,"Unhandled APLIC read at offset %#x\n" , + offset); + +domain_crash (vcpu->domain); + +return -EINVAL ; + } + + *out =aplic_hw_read_reg (offset, auth_mask);You blindly assume a 32-bit access here (and also in the write counterpart). How do you end up knowing?
he APLIC spec requires all register accesses to be 32-bit wide.
Also, I have the following at the caller side (yes, it can't be understand from the current patch):
/* Fault address should be aligned to length of MMIO */
if ( fault_addr & (len - 1) )
return -EIO;
if ( vintc->ops->is_access(vcpu, fault_addr) )
{
/* PLIC/APLIC access are always on 32bit */
ASSERT( len == 4 );
rc = vintc->ops->emulate_store(vcpu, fault_addr, data32);
if ( rc < 0 )
return rc;
}Probably it would be better addi a size parameter to both callbacks:
int (*emulate_load)(const struct vcpu *vcpu, unsigned long addr,
unsigned int size, uint32_t *out);
int (*emulate_store)(const struct vcpu *vcpu, unsigned long addr,
unsigned int size, uint32_t in);Then vaplic_emulate_load can validate upfront:
if ( size != sizeof(uint32_t) )
{
gdprintk(XENLOG_WARNING, "APLIC: unsupported access size %u\n", size);
domain_crash(vcpu->domain);
return -EINVAL;
}or maybe it would be better instead of domain_crash() just ignore such loads:
if ( size != sizeof(uint32_t) )
{
gdprintk(XENLOG_WARNING,
"APLIC: unsupported %u-byte access at offset %#x, ignoring\n",
size, offset);
*out = 0; /* for loads */
return 0;
}I want to add size argument as I think that I want to introduce common MMIO handling instead of is_access() to avoid growing of if/else in the snippet above
Quoted 5 lines
xen/arch/riscv/vaplic.c line 148
Quoted 3 lines
xen/arch/riscv/vaplic.c +2
-0+static int cf_check vaplic_emulate_store(const struct vcpu *vcpu, +unsigned long addr,uint32_t value)Why
cf_checkhere but not forvaplic_emulate_load()?
It should be added for both.
Quoted 69 lines
Quoted 69 lines
xen/arch/riscv/vaplic.c +68
-0+{ +int rc = -EINVAL ; +const struct domain *d = vcpu->domain; +unsigned int offset = addr &APLIC_REG_OFFSET_MASK ; + +switch ( offset ) + { + case APLIC_SETIP_BASE ... APLIC_SETIP_LAST: + case APLIC_CLRIP_BASE ... APLIC_CLRIP_LAST: + case APLIC_SETIE_BASE ... APLIC_SETIE_LAST: + case APLIC_CLRIE_BASE ... APLIC_CLRIE_LAST: + { +unsigned int irqn =regindx_to_irqn (offset &APLIC_SETCLR_OFFSET_MASK ); + value &=generate_auth_mask (d, irqn); + +break ; + } + + case APLIC_SOURCECFG_BASE ... APLIC_SOURCECFG_LAST: +if ( value &APLIC_SOURCECFG_D ) + { + rc = -EOPNOTSUPP ; + +dprintk (XENLOG_ERR ,"APLIC_SOURCECFG_D isn't supported\n" ); + + goto fail; + } + +/* +* As sourcecfg register starts from 1: +* 0x0000 domaincfg +* 0x0004 sourcecfg[1] +* 0x0008 sourcecfg[2] +* ... +* 0x0FFC sourcecfg[1023] +* It is necessary to calculate an interrupt number by subtracting +* of APLIC_DOMAINCFG instead of APLIC_SOURCECFG_BASE. +*/ +if ( !AUTH_IRQ_BIT (d,regindx_to_irqn (offset -APLIC_DOMAINCFG )) ) +/* Interrupt not enabled, ignore it */ +return 0 ; + +if ( value >APLIC_SOURCECFG_SM_LEVEL_LOW ) + { +gdprintk (XENLOG_ERR , +"value(%u) is incorrect for sourcecfg register\n" , value); + +return 0 ; + } + +break ; + + case APLIC_TARGET_BASE ... APLIC_TARGET_LAST: + { +struct vcpu *target_vcpu =NULL ; + +/* +* Look at vaplic_emulate_load() for explanation why +* APLIC_GENMSI is subtracted. +*/ +if ( !AUTH_IRQ_BIT (d,regindx_to_irqn (offset -APLIC_GENMSI )) ) +/* Interrupt not enabled, ignore it */ +return 0 ; + +for (unsigned int i =0 ; i < vcpu->domain->max_vcpus; i++ ) + { +struct vcpu *v = vcpu->domain->vcpu[i]; +
Quoted 8 lines
xen/arch/riscv/vaplic.c line 222
Quoted 6 lines
xen/arch/riscv/vaplic.c +6
-0+if ( v->vcpu_id == (value >>APLIC_TARGET_HART_IDX_SHIFT ) ) + { + target_vcpu = v; +break ; + } + }Why is a loop needed here? vCPU-s are numbered sequentially.
Agree, it isn't needed. The following will be enough:
unsigned int hart_idx = value >> APLIC_TARGET_HART_IDX_SHIFT;
if ( hart_idx < vcpu->domain->max_vcpus )
target_vcpu = vcpu->domain->vcpu[hart_idx];Quoted 11 lines
xen/arch/riscv/vaplic.c line 291
Quoted 9 lines
xen/arch/riscv/vaplic.c +8
-0+static bool cf_check vaplic_is_access(const struct vcpu *vcpu, +unsigned long addr) +{ +const struct vaplic *vaplic =to_vaplic (vcpu->domain); +paddr_t start = vaplic->regs_start; +paddr_t end = vaplic->regs_start + vaplic->regs_size; + +if ( addr &0x3 )Nit: Does the 0x here add any value?
Agree, there is no any sense.
Quoted 10 lines
xen/arch/riscv/vaplic.c line 297
Quoted 7 lines
xen/arch/riscv/vaplic.c +6
-0+ { +dprintk (XENLOG_DEBUG , +"APLIC MMIO address should be properly aligned\n" ); + +return false; + }Ah, okay - here is the alignment check. Mind me asking for a (documenting) assertion then in the actual read and write handlers?
Sure, I will add the comment above or add a correspondent ASSERT().
Quoted 4 lines
xen/arch/riscv/vaplic.c line 299
Quoted 2 lines
xen/arch/riscv/vaplic.c +1
-0+/* check if it is an APLIC access */ Nit: Style.
I will update that.
Thanks.
~ Oleksii
Re: [PATCH v2 21/26] xen/riscv: implement virtual APLIC MMIO emulation
Quoted 14 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 51
Quoted 2 lines · Oleksii Kurochko
xen/arch/riscv/aplic.c +2
-0+spin_lock_irqsave (&aplic.lock, flags); + val =readl ((void __iomem *)((uintptr_t )aplic.regs + offset)) & mask;Easier as
val = readl((volatile void __iomem *)aplic.regs + offset) & mask;? (Note that like const, volatile also shouldn't be cast away.)
Is arithmetic on void * pointers correct from the C standard's point of view?
Quoted 6 lines
It works with GCC (see
https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html), but I can't find anything that guarantees the same behavior for other compilers.I'm okay with the suggested change if it's acceptable for Xen to rely on GCC's void * pointer arithmetic extension.
We use that all over the place. Even docs/misra/C-language-toolchain.rst mentions it as explicitly permitted.
Quoted 17 lines
Quoted 11 lines
xen/arch/riscv/include/asm/vaplic.h struct vaplic_regs { · line 31
Quoted 9 lines
xen/arch/riscv/include/asm/vaplic.h +3
-0@@ -26,6 +26,9 @@ struct vaplic_regs {struct vaplic {struct vintc vintc;struct vaplic_regs regs; + +paddr_t regs_start; +paddr_t regs_size;Can
regs_sizereally go beyond 4G?Good question and it depends on an amount of vCPUs:
#define
APLIC_MIN_SIZE0x4000
#defineAPLIC_SIZE_ALIGN(x)ROUNDUP(x, APLIC_MIN_SIZE)
Quoted 7 lines
#define
APLIC_SIZE(nr_cpus)(APLIC_MIN_SIZE+ \
APLIC_SIZE_ALIGN(APLIC_IDC_SIZE *
(nr_cpus)))
paddr_taplic_size=APLIC_SIZE(d->max_vcpus);With the current limitation of 128 vCPUs max (IIRC) it won't beyond 4G.
Tying to the overly low limit of 128 isn't very helpful, I guess. With APLIC_IDC_SIZE resolving to 32, the limit would be millions of vCPU-s aiui, so imo not a concern at all.
Quoted 35 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/vaplic.c +0
-0@@ -26,6 +26,283 @@#define FDT_VAPLIC_INT_CELLS 2Quoted 13 lines
xen/arch/riscv/vaplic.c line 39
Quoted 11 lines
xen/arch/riscv/vaplic.c +11
-0+#define AUTH_IRQ_BIT (d, irqn) ( \ + ((irqn) <= (d)->arch.vintc->irq_nums) && \ + test_bit(irqn, (d)->arch.vintc->allocated_irqs) ) + +#define regindx_to_irqn (reg_val) ((reg_val) / sizeof(uint32_t)) + +static inline uint32_t generate_auth_mask (const struct domain *d, +unsigned int irqsn) +{ +if ( irqsn >=DIV_ROUND_UP (d->arch.vintc->irq_nums, +sizeof (uint32_t ) *BITS_PER_BYTE ) )Why the rounding up? Isn't ->irqs_num the proper upper bound?
Probably irqsn isn't a correct name here as it looks like "IRQ source number" (an IRQ number), but
regindx_to_irqn(offset & MASK)actually computes offset / 4 - a word index into theused_irqsbitmap. Word 0 covers IRQs 0–31, word 1 covers IRQs 32–63, etc.
Given that, the bounds check:
irqsn >=DIV_ROUND_UP(irq_nums, sizeof(uint32_t) * BITS_PER_BYTE)
is "word index >= number of 32-bit words in the bitmap" which is correct. TheDIV_ROUND_UPconverts the IRQ count into a word count to compare against the word index.So the real issue is naming. There are two options to resolve it:
- rename to reflect reality; call itword_idx(not irqsn), and renameregindx_to_irqnto something likeregoffset_to_word_idx. TheDIV_ROUND_UPcheck then reads clearly.
Quoted 6 lines
- Change the API to take an actual IRQ number, pass irqsn * 32 (the first IRQ in the word) and check irqsn >=
irq_numsdirectly, computing the word index insidegenerate_auth_mask. This aligns with howAUTH_IRQ_BITworks.Which one option do you prefer?
I don't care very much as long as the result is self-consistent.
Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/vaplic.c +5
-0+default : +gdprintk (XENLOG_WARNING ,"Unhandled APLIC read at offset %#x\n" , + offset); + +domain_crash (vcpu->domain);Quoted 8 lines
xen/arch/riscv/vaplic.c line 120
Quoted 5 lines
xen/arch/riscv/vaplic.c +5
-0+ +return -EINVAL ; + } + + *out =aplic_hw_read_reg (offset, auth_mask);You blindly assume a 32-bit access here (and also in the write counterpart). How do you end up knowing?
he APLIC spec requires all register accesses to be 32-bit wide.
Also, I have the following at the caller side (yes, it can't be understand from the current patch):
Quoted 8 lines
/* Fault address should be aligned to length of MMIO */
if ( fault_addr & (len - 1) )
return -EIO;
if ( vintc->ops->is_access(vcpu, fault_addr) )
{
/* PLIC/APLIC access are always on 32bit */
ASSERT( len == 4 );"len" being guest controlled, how can you have such an assertion?
Quoted 11 lines
rc = vintc->ops->emulate_store(vcpu, fault_addr, data32); if ( rc < 0 ) return rc; }Probably it would be better addi a size parameter to both callbacks:
int (*emulate_load)(const struct vcpu *vcpu, unsigned long addr,
unsigned int size, uint32_t *out); int (*emulate_store)(const struct vcpu *vcpu, unsigned long addr, unsigned int size, uint32_t in);
Yes please.
Jan
Re: [PATCH v2 25/26] xen/riscv: add initial dom0less infrastructure support
In reply to Jan BeulichQuoted 33 lines · Jan Beulich
Quoted 23 lines · Oleksii Kurochko
Enable dom0less support for RISC-V by selecting
HAS_DOM0LESSand providing the minimal architecture hooks required by the common dom0less infrastructure.Add stub implementations for architecture-specific helpers used when building domains from the device tree. These currently perform no additional work but allow the generic dom0less code to build and run on RISC-V.
Introduce
max_init_domidas a runtime variable rather than a constant so that it can be updated during dom0less domain creation.Provide missing helpers and definitions required by the domain construction code, including domain bitness helpers and the
p2m_set_allocation()prototype.Additionally define the guest magic memory region in the public RISC-V interface.
As
HAS_DOM0LESSis selected for RISC-V now it could be a compilation issue ifCONFIG_STATIC_MEMORY=yasguest_physmap_add_pages()isn't yet provided.Quoted 8 lines
Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- Move declaration ofp2m_set_allocation()top2m-common.h.
- Add__initdataformax_init_domidand drop initalizer for it.
- AddCONFIG_STATIC_MEMORY=nto CI's randconfig to avoid compilation error because ofguest_physmap_add_pages()isn't provided.
Quoted 4 lines
Yet another trap for people to fall into, and yet another item to clean up before the port is really ready to use. Imo there want to be
HAS_STATIC_MEMORY, which RISC-V simply wouldn't select (for the time being).
Sounds good to me. I will do the following then in the separate patch:
xen/arch/arm/Kconfig +0 -0
index 683ab7d25a1e..d748404e82da 100644
xen/arch/arm/Kconfig +1 -0
@@ -22,6 +22,7 @@ config ARM select HAS_GRANT_CACHE_FLUSH if GRANT_TABLE select HAS_SHARED_INFO select HAS_STACK_PROTECTOR + select HAS_STATIC_MEMORY select HAS_UBSAN config ARCH_DEFCONFIG
xen/common/Kconfig +4 -1
@@ -161,6 +161,9 @@ config HAS_SCHED_GRANULARITY config HAS_SHARED_INFO bool +config HAS_STATIC_MEMORY + bool + config HAS_SOFT_RESET bool
@@ -196,7 +199,7 @@ config NUMA config STATIC_MEMORY bool "Static Allocation Support (UNSUPPORTED)" if UNSUPPORTED - depends on DOM0LESS_BOOT && HAS_DEVICE_TREE_DISCOVERY + depends on HAS_STATIC_MEMORY && DOM0LESS_BOOT && HAS_DEVICE_TREE_DISCOVERY help Static Allocation refers to system or sub-system(domains) for which memory areas are pre-defined by configuration using
physical
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/dom0less-build.c +0
-0@@ -102,3 +102,9 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,
Quoted 11 lines
xen/arch/riscv/dom0less-build.c int __init arch_parse_dom0less_node(struct dt_device_node *node, · line 110
Quoted 8 lines
xen/arch/riscv/dom0less-build.c +6
-0return 0 ; } + +int __init arch_handle_passthrough_prop (struct kernel_info *kinfo, +struct dt_device_node *node) +{ +return 0 ; +}No FIXME comment or anything alike? That is, nothing is going to be needed here even once pass-through is supported?
At the moment (even in downstream), RISC-V has nothing to do. I can just add the comment above return:
/* Nothing specific to do for now */
Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/domain-build.c +6
-0@@ -158,9 +158,22 @@ int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo)return fdt_end_node (fdt); } +int __init construct_hwdom (struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +return -EOPNOTSUPP ; +} +int __init make_timer_node (const struct kernel_info *kinfo) {/* There is no need for timer node for RISC-V. */
Quoted 9 lines
xen/arch/riscv/domain-build.c int __init make_cpus_node(const struct domain *d, struct kernel_info *kinfo) · line 176
Quoted 6 lines
xen/arch/riscv/domain-build.c +4
-0return 0 ; } + +int __init make_hypervisor_node (struct domain *d, +const struct kernel_info *kinfo, +int addrcells,int sizecells)The last two parameters being of plain int type is, I suppose, dictated by DT code?
Yes, it is dictated by DT code.
Quoted 11 lines
xen/arch/riscv/include/asm/guest-layout.h line 28
Quoted 9 lines
xen/arch/riscv/include/asm/guest-layout.h +2
-0@@ -24,4 +24,7 @@#define GUEST_RAM_BANK_BASES { GUEST_RAM0_BASE, GUEST_RAM1_BASE }#define GUEST_RAM_BANK_SIZES { GUEST_RAM0_SIZE, GUEST_RAM1_SIZE } +#define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000) +#define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000)Why
xen_mk_ullong()? That's needed in the public headers only, iirc.
I didn't know that it is only for public headers.
I can change that to _ULL.
Quoted 2 lines
Also these are again two seemingly arbitrary numbers.
It is pretty arbitrary, I just took what isn't used by QEMU machine for its address space. I double checked and it should be changed to something else as it falls into PCIE_ECAM range:
[VIRT_PCIE_ECAM] = { 0x30000000, 0x10000000 },
[VIRT_PCIE_MMIO] = { 0x40000000, 0x40000000 },
[VIRT_DRAM] = { 0x80000000, 0x0 },I will use 0x79000000 instead.
I will update the commit message that it GUEST_MAGIC_BASE and GUEST_MAGIC_SIZE are chosen arbitary and not to overlap with address space provided by QEMU for RISC-V machine.
Thanks.
~ Oleksii
Re: [PATCH v2 26/26] xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable callbacks
In reply to Jan BeulichQuoted 33 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
desc->statusis only set once duringsetup_irq(), but interrupts can be enabled/disabled at runtime, so update it in the corresponding callbacks.
wmb()inaplic_irq_enable()ensuresdo_IRQ(), which can fire immediately after the interrupt is enabled, sees the updateddesc->status.This doesn't look entirely correct. Aiui ...
Quoted 24 lines
No
rmb()is
needed on thedo_IRQ()side becausedesc->statusis read under a spinlock, which implies an acquire barrier.No barrier is needed in
aplic_irq_disable()because the hardware disables the interrupt before the status is updated, sodo_IRQ()cannot fire, andspin_unlock()makes the updated value visible.Fixes d4676a1398bc5 ("xen/riscv: implementation of aplic and imsic operations")Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- New patch.--- xen/arch/riscv/aplic.c | 5 +++++ xen/arch/riscv/irq.c | 3 --- 2 files changed, 5 insertions(+), 3 deletions(-)xen/arch/riscv/aplic.c +0
-0@@ -161,6 +161,9 @@ static void cf_check aplic_irq_enable(struct irq_desc *desc)spin_lock (&aplic.lock);
Quoted 8 lines
xen/arch/riscv/aplic.c static void cf_check aplic_irq_enable(struct irq_desc *desc) · line 168
Quoted 5 lines
xen/arch/riscv/aplic.c +3
-0+ desc->status &= ~IRQ_DISABLED ; +wmb (); +/* Enable interrupt in IMSIC */ imsic_irq_enable (desc->irq);... you want to order the ->status update ahead of the
imsic_irq_enable()operation. Yet that's a CSR write. Do fences really cover that?
Good point. The RISC-V ISA spec is clear that fence orders memory predecessor and successor operations. CSR accesses are architecturally a distinct category. fence w,w carries no formal guarantee that a preceding memory store to desc->status becomes visible before a subsequent CSR write.
Also, according to the spec:
Each RISC-V hart normally observes its own CSR accesses, including its implicit CSR accesses, as performed in program order. In particular, unless specified otherwise, a CSR access is performed after the execution of any prior instructions in program order whose behavior modifies or is modified by the CSR state and before the execution of any subsequent instructions in program order whose behavior modifies or is modified by the CSR state. Furthermore, an explicit CSR read returns the CSR state before the execution of the instruction, while an explicit CSR write suppresses and overrides any implicit writes or modifications to the same CSR by the same instruction.
and ...
If not,
Quoted 8 lines
you may need to resort to
mb(), to order the ->status write against the ->irq read, in lieu of being able to order against a CSR write.Of course with
imsic_irq_enable()itself acquiring a lock (which necessarily has a memory write), you could then further argue thatwmb()is indeed sufficient, bot for a reason different from the one presently stated in the description. (I would also strongly suggest to annotate thewmb()with an explanatory comment.)
...
After re-looking at imsic_irq_enable() it is calling imsic_local_eix_update() which targets the local hart's IMSIC interrupt file (note the function name), and IRQs are disabled at this point (the ASSERT(!local_irq_is_enabled())). So do_IRQ() can only fire on the same hart, after IRQs are re-enabled on return from aplic_irq_enable(). Same-hart program order already guarantees desc->status is committed (before of the mentioned above) before the CSR write retires, making the barrier redundant.
So barrier could be dropped here.
On the other side if some code will check on different CPU what is desc->status without a barrier it will see a stale value. But then in this case we have to use or atomic set_bit() here or spinlock(desc->lock) around an update of desc->status. Then the similar is needed for aplic_irq_disable().
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/aplic.c +0
-0@@ -189,6 +192,8 @@ static void cf_check aplic_irq_disable(struct irq_desc *desc)/* Disable interrupt in IMSIC */ imsic_irq_disable (desc->irq);
Quoted 9 lines
xen/arch/riscv/aplic.c static void cf_check aplic_irq_disable(struct irq_desc *desc) · line 198
Quoted 4 lines
xen/arch/riscv/aplic.c +2
-0+ desc->status |=IRQ_DISABLED ; +spin_unlock (&aplic.lock); }The ->status write becoming globally visible ahead of
imsic_irq_disable()doing its work is not an issue? In the paragraph in the description you again appear to assume that the CSR write and the memory write of the spin-unlock are ordered wrt one another.
The ordering is intentional: desc->status |= IRQ_DISABLED is placed after imsic_irq_disable() so that do_IRQ() can never observe IRQ_DISABLED while the interrupt is still enabled in hardware.
Regarding the fence/CSR ordering concern: imsic_irq_disable() writes to the local hart's IMSIC interrupt file, so do_IRQ() for this irq can only run on the same hart. Same-hart program order already guarantees that both the CSR write and the desc->status update are visible in the correct sequence before any subsequent interrupt handler runs.
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/irq.c +0
-0@@ -145,9 +145,6 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new) desc->handler->set_affinity (desc,cpumask_of (smp_processor_id ()));
Quoted 9 lines
xen/arch/riscv/irq.c int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new) · line 148
Quoted 5 lines
xen/arch/riscv/irq.c +0
-3desc->handler->startup (desc); - -/* Enable irq */ - desc->status &= ~IRQ_DISABLED ; }Arm and x86 are different in this regard, so it's hard to tell which way it ought to be for RISC-V. This removal surely wants a sentence or two in the description.
I will add the following to commit description:
The removal of desc->status &= ~IRQ_DISABLED from setup_irq() is safe for RISC-V because aplic_irq_startup() calls aplic_irq_enable() directly, which now clears the flag itself. The explicit clear in setup_irq() is therefore redundant.
Thanks.
~ Oleksii
Re: [PATCH v2 21/26] xen/riscv: implement virtual APLIC MMIO emulation
In reply to Jan BeulichQuoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
xen/arch/riscv/aplic.c static struct intc_info __ro_after_init aplic_info = { · line 51
Quoted 2 lines · Oleksii Kurochko
xen/arch/riscv/aplic.c +2
-0+spin_lock_irqsave (&aplic.lock, flags); + val =readl ((void __iomem *)((uintptr_t )aplic.regs + offset)) & mask;Easier as
val = readl((volatile void __iomem *)aplic.regs + offset) & mask;? (Note that like const, volatile also shouldn't be cast away.)
Is arithmetic on void * pointers correct from the C standard's point of view?
It works with GCC (see
https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html), but I can't find anything that guarantees the same behavior for other compilers.
Quoted 5 lines
Quoted 2 lines
I'm okay with the suggested change if it's acceptable for Xen to rely on GCC's void * pointer arithmetic extension.
We use that all over the place. Even
docs/misra/C-language-toolchain.rstmentions it as explicitly permitted.
Thanks for that. I will then just apply your suggestion.
Quoted 22 lines
Quoted 22 lines
Quoted 12 lines
xen/arch/riscv/include/asm/vaplic.h struct vaplic_regs { · line 31
Quoted 10 lines
xen/arch/riscv/include/asm/vaplic.h +3
-0@@ -26,6 +26,9 @@ struct vaplic_regs {struct vaplic {struct vintc vintc;struct vaplic_regs regs; + +paddr_t regs_start; +paddr_t regs_size;Can
regs_sizereally go beyond 4G?Good question and it depends on an amount of vCPUs:
#define
APLIC_MIN_SIZE0x4000
#defineAPLIC_SIZE_ALIGN(x)ROUNDUP(x, APLIC_MIN_SIZE)#define
APLIC_SIZE(nr_cpus)(APLIC_MIN_SIZE+ \
APLIC_SIZE_ALIGN(APLIC_IDC_SIZE *
(nr_cpus)))
Quoted 7 lines
Quoted 3 lines
paddr_taplic_size=APLIC_SIZE(d->max_vcpus);With the current limitation of 128 vCPUs max (IIRC) it won't beyond 4G.
Tying to the overly low limit of 128 isn't very helpful, I guess. With
APLIC_IDC_SIZEresolving to 32, the limit would be millions of vCPU-s aiui, so imo not a concern at all.
Then 'unsigned int' should be more then enough.
Quoted 23 lines
Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/include/asm/vaplic.h +5
-0+default : +gdprintk (XENLOG_WARNING ,"Unhandled APLIC read at offset %#x\n" , + offset); + +domain_crash (vcpu->domain);Quoted 8 lines
xen/arch/riscv/include/asm/vaplic.h
Quoted 5 lines
xen/arch/riscv/include/asm/vaplic.h +5
-0+ +return -EINVAL ; + } + + *out =aplic_hw_read_reg (offset, auth_mask);You blindly assume a 32-bit access here (and also in the write counterpart). How do you end up knowing?
he APLIC spec requires all register accesses to be 32-bit wide.
Also, I have the following at the caller side (yes, it can't be understand from the current patch):
/* Fault address should be aligned to length of MMIO */ if ( fault_addr & (len - 1) ) return -EIO;
Quoted 6 lines
Quoted 4 lines
if ( vintc->ops->is_access(vcpu, fault_addr) ) { /* PLIC/APLIC access are always on 32bit */ ASSERT( len == 4 );"len" being guest controlled, how can you have such an assertion?
Agree, ASSERT() isn't the best option here. Either rejecting of such length should happen or domain_crash() instead. The first one option I think is more preferrable.
~ Oleksii
Re: [PATCH v2 26/26] xen/riscv: manage IRQ_DISABLED flag in APLIC irq enable/disable callbacks
In reply to Oleksii KurochkoQuoted 42 lines · Oleksii Kurochko
Quoted 27 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
desc->statusis only set once duringsetup_irq(), but interrupts can be enabled/disabled at runtime, so update it in the corresponding callbacks.
wmb()inaplic_irq_enable()ensuresdo_IRQ(), which can fire immediately after the interrupt is enabled, sees the updateddesc->status.This doesn't look entirely correct. Aiui ...
Quoted 18 lines
No
rmb()is
needed on thedo_IRQ()side becausedesc->statusis read under a spinlock, which implies an acquire barrier.No barrier is needed in
aplic_irq_disable()because the hardware disables the interrupt before the status is updated, sodo_IRQ()cannot fire, andspin_unlock()makes the updated value visible.Fixes d4676a1398bc5 ("xen/riscv: implementation of aplic and imsic operations")Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v2:
- New patch.
---
xen/arch/riscv/aplic.c| 5 +++++
xen/arch/riscv/irq.c | 3 ---2 files changed, 5 insertions(+), 3 deletions(-)Quoted 13 lines
xen/arch/riscv/aplic.c static void cf_check aplic_irq_enable(struct · line 167
Quoted 10 lines
xen/arch/riscv/aplic.c +3
-0@@ -161,6 +161,9 @@ static void cf_check aplic_irq_enable(struct irq_desc *desc) spin_lock(&aplic.lock); + desc->status &= ~IRQ_DISABLED; + wmb(); + /* Enable interrupt in IMSIC */ imsic_irq_enable(desc->irq);... you want to order the ->status update ahead of the
imsic_irq_enable()operation. Yet that's a CSR write. Do fences really cover that?
Quoted 5 lines
Good point. The RISC-V ISA spec is clear that fence orders memory predecessor and successor operations. CSR accesses are architecturally a distinct category. fence w,w carries no formal guarantee that a preceding memory store to
desc->statusbecomes visible before a subsequent CSR write.Also, according to the spec:
Each RISC-V hart normally observes its own CSR accesses, including its implicit CSR accesses, as performed in program order. In particular, unless specified otherwise, a CSR access is performed after the execution of any prior instructions in program order whose behavior modifies or is modified by the CSR state and before the execution of any subsequent instructions in program order whose behavior modifies or is modified by the CSR state. Furthermore, an explicit CSR read returns the CSR state before the execution of the instruction, while an explicit CSR write suppresses and overrides any implicit writes or modifications to the same CSR by the same instruction.
After inspecting of RISC-V spec again I found that:
To enforce ordering in all other cases, software should execute a FENCE instruction between the relevant accesses. For the purposes of the FENCE instruction, CSR read accesses are classified as device input (I), and CSR write accesses are classified as device output (O).
So wmb() should be enough in the current case as wmb is RISCV_FENCE(ow, ow). I will add the following comment:
/*
* wmb() (fence ow,ow) orders the ->status memory write (w) before the
* CSR write inside imsic_irq_enable() (device output, o on RISC-V).
* arch_lock_release_barrier() uses fence rw,rw which does not cover
* device output (o), so wmb() is required to close that gap.
*/
wmb();
Quoted 16 lines
Quoted 15 lines
xen/arch/riscv/aplic.c static void cf_check aplic_irq_disable(struct · line 198
Quoted 10 lines
xen/arch/riscv/aplic.c +2
-0@@ -189,6 +192,8 @@ static void cf_check aplic_irq_disable(struct irq_desc *desc) /* Disable interrupt in IMSIC */ imsic_irq_disable(desc->irq); + desc->status |= IRQ_DISABLED; + spin_unlock(&aplic.lock); }The ->status write becoming globally visible ahead of
imsic_irq_disable()doing its work is not an issue? In the paragraph in the description you again appear to assume that the CSR write and the memory write of the spin-unlock are ordered wrt one another.
Quoted 3 lines
The ordering is intentional:
desc->status|=IRQ_DISABLEDis placed afterimsic_irq_disable()so thatdo_IRQ()can never observeIRQ_DISABLEDwhile the interrupt is still enabled in hardware.Regarding the fence/CSR ordering concern:
imsic_irq_disable()writes to the local hart's IMSIC interrupt file, sodo_IRQ()for this irq can only run on the same hart. Same-hart program order already guarantees that both the CSR write and thedesc->statusupdate are visible in the correct sequence before any subsequent interrupt handler runs.
And here I re-read your comment and IIUC that we want to guarantee thatdesc->status |= IRQ_DISABLED; isn't globally visible until imsic_irq_disable() is executed. Then we need the following here:
+ /*
+ * wmb() (fence ow,ow) ensures the CSR write (device output, o) inside + * imsic_irq_disable() is globally visible before ->status is marked + * IRQ_DISABLED. imsic_irq_disable()'s spin_unlock uses fence rw,rw + * which does not order device output (o) writes before subsequent + * memory writes (w), so an explicit wmb() is needed here. + */
+ wmb();
Would you be okay with such barriers or you still think it is needed mb()?
Also I think considering that I'll added the comments above wmb() I could drop what I mentioned in commit message regarding barriers().
~ Oleksii