Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A50F1E909AC for ; Tue, 17 Feb 2026 14:23:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1235079.1538122 (Exim 4.92) (envelope-from ) id 1vsLzH-0003Mo-Ow; Tue, 17 Feb 2026 14:23:39 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1235079.1538122; Tue, 17 Feb 2026 14:23:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vsLzH-0003Mh-MK; Tue, 17 Feb 2026 14:23:39 +0000 Received: by outflank-mailman (input) for mailman id 1235079; Tue, 17 Feb 2026 14:23:38 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vsLzG-0003CN-ID for xen-devel@lists.xenproject.org; Tue, 17 Feb 2026 14:23:38 +0000 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [2a00:1450:4864:20::333]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3f644c50-0c0c-11f1-9ccf-f158ae23cfc8; Tue, 17 Feb 2026 15:23:36 +0100 (CET) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-482f454be5bso59038675e9.0 for ; Tue, 17 Feb 2026 06:23:36 -0800 (PST) Received: from [192.168.1.6] (user-109-243-67-101.play-internet.pl. [109.243.67.101]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48371998777sm402436275e9.1.2026.02.17.06.23.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Feb 2026 06:23:35 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3f644c50-0c0c-11f1-9ccf-f158ae23cfc8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771338216; x=1771943016; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=11JyZZS+qJmiFN3fNB+Iee1JkfEd9hcyAJUVgAJ0YHw=; b=RuHXJUPrXBRc/Gi7UBK91ySzqLmZ98Jy+7SZYHoJIWkBQjKha20NFHXHCTIqisSCbO JnRTc8TyHfDCz014x7PNnRyqqc3PmMeqcjptz4EnLjZpKCCfIrEpa+N3LqRWHP32vRd8 TtTsW5qln/Go82hiVsJ0oSLYFq+PqUp1zeW/oQADe7O5cFhBRxQK+ywHsIwP/KH5+BIs FNF10zIDfpWWioibEueiIffi11+adUj7X96W9foQmu+vIQj37LwXB7PBSDWTQjdyOiAW 4hgNMtgGMTxOSYACa6SPuO2iVvxjHPNSA9QgErwj5cop4JHKmThAlCy941nSJWrAQT+Y fGYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771338216; x=1771943016; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=11JyZZS+qJmiFN3fNB+Iee1JkfEd9hcyAJUVgAJ0YHw=; b=sVRYfQQhbrAf1o4uD1sFjF+fTC07m+xR7bjoJia3YlZeYgrFGbGL/kLQf2FvgIzDR0 EAmnpwB38O1LoFxc/J4C2cC2PaqdABrGUU3D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id 5b1f17b1804b1-48378da52a1mr193686835e9.16.1771338216091; Tue, 17 Feb 2026 06:23:36 -0800 (PST) Message-ID: <974f0b55-3ead-4892-92c0-707be1303834@gmail.com> Date: Tue, 17 Feb 2026 15:23:32 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 02/16] xen/riscv: detect and store supported hypervisor CSR bits at boot To: Jan Beulich Cc: Romain Caritey , Alistair Francis , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Julien Grall , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , xen-devel@lists.xenproject.org References: <06c3b8f88803039a3d900c8cb2f1fd9d05f44e3e.1770999383.git.oleksii.kurochko@gmail.com> Content-Language: en-US From: Oleksii Kurochko In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/17/26 12:37 PM, Jan Beulich wrote: > On 13.02.2026 17:28, 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. >> >> Note that csr_set() is used instead of csr_write() to write all ones to >> the mask, as the CSRRS instruction, according to the RISC-V specification, >> sets only those bits that are writable (note that the quote consider only >> non-read-only CSRs as writing to read-only CSRs according to the spec. >> will raise an exception): >> Any bit that is high in rs1 will cause the corresponding bit to be set >> in the CSR, if that CSR bit is writable. >> In contrast, the CSRRW instruction does not take CSR bit writability into >> account, which could lead to unintended side effects when writing all ones >> to a CSR. >> >> 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 >> --- >> Changes in V4: >> - Move csr_masks defintion to domain.c. Make it static as at the moment >> it is going to be used only in domain.c. > Except that ... > >> --- a/xen/arch/riscv/domain.c >> +++ b/xen/arch/riscv/domain.c >> @@ -2,9 +2,14 @@ >> >> #include >> #include >> +#include >> #include >> #include >> >> +#include >> + >> +struct csr_masks __ro_after_init csr_masks; > ... it's not static here and even has ... > >> --- a/xen/arch/riscv/include/asm/setup.h >> +++ b/xen/arch/riscv/include/asm/setup.h >> @@ -5,6 +5,15 @@ >> >> #include >> >> +struct csr_masks { >> + register_t hedeleg; >> + register_t henvcfg; >> + register_t hideleg; >> + register_t hstateen0; >> +}; >> + >> +extern struct csr_masks csr_masks; > ... a declaration here. If you want to keep it non-static, it (and the struct > decl) likely wants moving elsewhere. Whereas if you truly want it to be static, > the struct decl would want moving to domain.c as well. Wrong patch version. I made it static so csr_masks declaration and struct csr_masks were in domain.c. Also, init_csr_masks() was moved to domain.c too. I will update the patch version next time. > >> --- a/xen/arch/riscv/setup.c >> +++ b/xen/arch/riscv/setup.c >> @@ -70,6 +70,25 @@ static void * __init relocate_fdt(paddr_t dtb_paddr, size_t dtb_size) >> return fdt; >> } >> >> +void __init init_csr_masks(void) >> +{ >> + register_t old; > As indicated before, this would better be ... > >> +#define INIT_CSR_MASK(csr, field) do { \ >> + old = csr_read(CSR_##csr); \ >> + csr_set(CSR_##csr, ULONG_MAX); \ >> + csr_masks.field = csr_read(CSR_##csr); \ >> + csr_write(CSR_##csr, old); \ >> +} while (0) > ... local to the scope the macro introduces. IOW with both this and the > earlier remark, let's try to strive to have scope and exposure of variables > as narrow as possible (unless of course there are clear reasons not to). > > And btw, wouldn't you again better use csr_swap() here? It makes sense, I'll use csr_swap() instead of pair csr_read() and csr_write(). Thanks. ~ Oleksii