From: Jan Beulich <jbeulich@suse.com>
To: Oleksii Kurochko <oleksii.kurochko@gmail.com>
Cc: "Romain Caritey" <Romain.Caritey@microchip.com>,
"Alistair Francis" <alistair.francis@wdc.com>,
"Connor Davis" <connojdavis@gmail.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Anthony PERARD" <anthony.perard@vates.tech>,
"Michal Orzel" <michal.orzel@amd.com>,
"Julien Grall" <julien@xen.org>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Stefano Stabellini" <sstabellini@kernel.org>,
xen-devel@lists.xenproject.org
Subject: Re: [PATCH v7 01/14] xen/riscv: detect and store supported hypervisor CSR bits at boot
Date: Tue, 10 Mar 2026 09:11:44 +0100 [thread overview]
Message-ID: <2de7f459-00b3-4968-aaa2-9067cb0c4aa4@suse.com> (raw)
In-Reply-To: <3201951150104f17593e16c7ba00ada51ac1e10f.1772814110.git.oleksii.kurochko@gmail.com>
On 06.03.2026 17:33, Oleksii Kurochko wrote:
> Some hypervisor CSRs expose optional functionality and may not implement
> all architectural bits. Writing unsupported bits can either be ignored
> or raise an exception depending on the platform.
>
> Detect the set of writable bits for selected hypervisor CSRs at boot and
> store the resulting masks for later use. This allows safely programming
> these CSRs during vCPU context switching and avoids relying on hardcoded
> architectural assumptions.
>
> Use csr_read()&csr_write() instead of csr_swap()+all ones mask as some
> CSR registers have WPRI fields which should be preserved during write
> operation.
>
> Also, ro_one struct is introduced to cover the cases when a bit in CSR
> register (at the momemnt, it is only hstateen0) may be r/o-one to have
> hypervisor view of register seen by guest correct.
>
> Masks are calculated at the moment only for hedeleg, henvcfg, hideleg,
> hstateen0 registers as only them are going to be used in the follow up
> patch.
>
> If the Smstateen extension is not implemented, hstateen0 cannot be read
> because the register is considered non-existent. Instructions that attempt
> to access a CSR that is not implemented or not visible in the current mode
> are reserved and will raise an illegal-instruction exception.
>
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
I'll commit as-is, yet still a couple of remarks:
> ---
> Changes in V7:
> - Use csr_read_set() in INIT_CSR_MASK() instead of csr_read()+csr_write().
> - Add undef of INIT_CSR_MASK().
> - Move local variable old above INIT_CSR_MASK().
This contradicts ...
> - Introduce INIT_RO_ONE_MASK() to init csr_masks.ro_one.* fields.
> - Introduce defines for masks intead of constants.
> - Move old variable inside macros INIT_CSR_MASK() and INIT_RO_ONE_MASK().
... this. You may want to prune revlog entries when making incremental
changes within one revision.
> --- a/xen/arch/riscv/domain.c
> +++ b/xen/arch/riscv/domain.c
> @@ -2,9 +2,66 @@
>
> #include <xen/init.h>
> #include <xen/mm.h>
> +#include <xen/sections.h>
> #include <xen/sched.h>
> #include <xen/vmap.h>
>
> +#include <asm/cpufeature.h>
> +#include <asm/csr.h>
> +
> +struct csr_masks {
> + register_t hedeleg;
> + register_t henvcfg;
> + register_t hideleg;
> + register_t hstateen0;
> +
> + struct {
> + register_t hstateen0;
> + } ro_one;
> +};
> +
> +static struct csr_masks __ro_after_init csr_masks;
> +
> +#define HEDELEG_AVAIL_MASK ULONG_MAX
> +#define HIDELEG_AVAIL_MASK ULONG_MAX
> +#define HENVCFG_AVAIL_MASK _UL(0xE0000003000000FF)
> +#define HSTATEEN0_AVAIL_MASK _UL(0xDE00000000000007)
It's not quite clear to me what AVAIL in here is to signal. It's also not
quite clear to me why you would use _UL() in #define-s sitting in a C file
(and hence not possibly being used in assembly code; even for asm() I'd
expect constants to be properly passed in as C operands).
> +void __init init_csr_masks(void)
> +{
> + /*
> + * The mask specifies the bits that may be safely modified without
> + * causing side effects.
> + *
> + * For example, registers such as henvcfg or hstateen0 contain WPRI
> + * fields that must be preserved. Any write to the full register must
> + * therefore retain the original values of those fields.
> + */
> +#define INIT_CSR_MASK(csr, field, mask) do { \
> + register_t old = csr_read_set(CSR_##csr, mask); \
> + csr_masks.field = csr_swap(CSR_##csr, old); \
> + } while (0)
> +
> +#define INIT_RO_ONE_MASK(csr, field, mask) do { \
> + register_t old = csr_read_clear(CSR_HSTATEEN0, mask); \
> + csr_masks.ro_one.field = csr_swap(CSR_##csr, old) & mask; \
> + } while (0)
> +
> + INIT_CSR_MASK(HEDELEG, hedeleg, HEDELEG_AVAIL_MASK);
> + INIT_CSR_MASK(HIDELEG, hideleg, HIDELEG_AVAIL_MASK);
> +
> + INIT_CSR_MASK(HENVCFG, henvcfg, HENVCFG_AVAIL_MASK);
> +
> + if ( riscv_isa_extension_available(NULL, RISCV_ISA_EXT_smstateen) )
> + {
> + INIT_CSR_MASK(HSTATEEN0, hstateen0, HSTATEEN0_AVAIL_MASK);
> + INIT_RO_ONE_MASK(HSTATEEN0, hstateen0, HSTATEEN0_AVAIL_MASK);
> + }
The 3rd macro parameters are now redundant. At the example of INIT_CSR_MASK(),
you could now have
#define INIT_CSR_MASK(csr, field) do { \
register_t old = csr_read_set(CSR_ ## csr, csr ## _AVAIL_MASK); \
csr_masks.field = csr_swap(CSR_ ## csr, old); \
} while (0)
This would reduce the risk of incomplete editing after copy-and-paste, or
other typo-ing.
Note also that ## being a binary operator, ./CODING_STYLE wants us to put
blanks around it just like for non-pre-processor binary operators. I'll
try to remember to make that adjustment when committing.
Jan
next prev parent reply other threads:[~2026-03-10 8:11 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-06 16:33 [PATCH v7 00/14] xen/riscv: introduce vtimer related things Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 01/14] xen/riscv: detect and store supported hypervisor CSR bits at boot Oleksii Kurochko
2026-03-10 8:11 ` Jan Beulich [this message]
2026-03-10 8:17 ` Jan Beulich
2026-03-10 16:00 ` Oleksii Kurochko
2026-03-10 16:14 ` Jan Beulich
2026-03-06 16:33 ` [PATCH v7 02/14] xen/riscv: implement vcpu_csr_init() Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 03/14] xen/riscv: introduce tracking of pending vCPU interrupts, part 1 Oleksii Kurochko
2026-03-10 8:13 ` Jan Beulich
2026-03-06 16:33 ` [PATCH v7 04/14] xen/riscv: introduce tracking of pending vCPU interrupts, part 2 Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 05/14] xen/riscv: introduce basic vtimer infrastructure for guests Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 06/14] xen/riscv: introduce vcpu_kick() implementation Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 07/14] xen/riscv: add vtimer context switch helpers Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 08/14] xen/riscv: implement SBI legacy SET_TIMER support for guests Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 09/14] xen/riscv: introduce sbi_set_timer() Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 10/14] xen/riscv: implement reprogram_timer() via SBI Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 11/14] xen/riscv: handle hypervisor timer interrupts Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 12/14] xen/riscv: init tasklet subsystem Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 13/14] xen/riscv: implement sync_vcpu_execstate() Oleksii Kurochko
2026-03-06 16:33 ` [PATCH v7 14/14] xen/riscv: Disable SSTC extension and add trap-based CSR probing Oleksii Kurochko
2026-03-10 9:15 ` Jan Beulich
2026-03-11 9:54 ` Oleksii Kurochko
2026-03-11 10:54 ` Oleksii Kurochko
2026-03-11 10:58 ` Jan Beulich
2026-03-11 11:38 ` Oleksii Kurochko
2026-03-11 12:54 ` Jan Beulich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2de7f459-00b3-4968-aaa2-9067cb0c4aa4@suse.com \
--to=jbeulich@suse.com \
--cc=Romain.Caritey@microchip.com \
--cc=alistair.francis@wdc.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=connojdavis@gmail.com \
--cc=julien@xen.org \
--cc=michal.orzel@amd.com \
--cc=oleksii.kurochko@gmail.com \
--cc=roger.pau@citrix.com \
--cc=sstabellini@kernel.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.