From patchwork Mon Jul 19 16:03:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 12386185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5222C07E95 for ; Mon, 19 Jul 2021 16:11:07 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8AB54613AE for ; Mon, 19 Jul 2021 16:11:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8AB54613AE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=zVGvorsM5XwGgFXuet78AcrID+jWhfzEMmjv4tpn/I0=; b=LTh4hByjzLzzkctmqI6Rz/sLLU XSovw8JzgsB68XSstfLCCKGOQnxqAzfgebA7GCFg9bS1wWeTiFCfe9DaUAO2VOqze4oVydxcOv089 9GopFl2uumC6iU0QOPRViGbwcjBlYFRNA3qDZwv1hv6s51L+/2aXxzQiYQ9fzbSxwhl3yWpyaXgKp RZX+Y5vnjuHhqUYaleg+AQf8NMr/di+yfnMfuCh2IYU9mjYDjH8naSlZKXLho7hHGHYiOJdnR8xtz RHym9LrsbPABZI/9BPx/G1XmjeV5o4/9HRq1RjtZcJh/qaanZUsV7/3k2PpvMRVNz+2JN4fPpyKVw I8G2j8KA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5VpY-00AN5a-KJ; Mon, 19 Jul 2021 16:09:22 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5VkO-00AKg7-RF for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 16:04:02 +0000 Received: by mail-wr1-x44a.google.com with SMTP id a4-20020adffb840000b02901304c660e75so8865246wrr.19 for ; Mon, 19 Jul 2021 09:03:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=GDqskojL7dyXpCREyAWDqO7MHA/WU9T+kxKaEf44dB0=; b=rZVHJk2Z0+nWKmBUX0mm3cqH9vYGulzcqpX9aqYyTTQT1WF91Ke37EurEKLmcF2DCL keDGWcvL5Eov7/qcjCIs3tIX0eIw8v47J+bkm4uBrX21ondXLKbqPPfETvkh3JmN2HF3 lVQvFfjfEGWwWt7c6h7aJLYmb3uDiLRE8iD5Lu8vDIP0tbA4RgerBAn2zoxcd7EWdCIv EwgRukwaJ1agHHeJR5GyAzTxZG9ktl2QwQ7WvZqtSwsT0XTiFq6A1FQTVpebID91Ae9Z sQahfVdSSjsOIgNUF51cL7t1EEcrRajBDHGZl5vxsF1EMfXHD5Sn6PPayoPHy/NhT5zZ nEyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=GDqskojL7dyXpCREyAWDqO7MHA/WU9T+kxKaEf44dB0=; b=OE5wIp9YkrYVjuUo5ZxK6FnZ6OtRsrGMdBbLTkqWB+M6VOJrLEA0UReybaswjPAIZn mvIZJAVtomyvMzlXwv1qghlpFbqGvbA0Fr/DopNDL75IjkEPl/ylUylsDAIP5s4o/SK2 PH2qs/9xKF9ES9wqAw8HXopYBcN/i/w7nJG19dm9Y+CPMmxL83fxOGJ+9Z4JavqLGQTE WIoZo3TC3q6Z3jWQToTYr9FWNaNfemHS4QQRMM+pytMnYOkxTzTqoeJc56/yOePPiMFa bsWv+LZWEXXWCIswWSIHlADJjTLKbRX8R1wmm00tqMJXR8ZQRK2tL8Ne5qb3QQ8Lx/3z owrw== X-Gm-Message-State: AOAM532FXmpHXEfdRG6AjcpL9tEaYrIBjuwCkhiqRfuJzL8o/J3aNTUe 15EYSy5Vnx0tWQp4sEk975OlN/f8hw== X-Google-Smtp-Source: ABdhPJwp/L7WERVfHFTFWu/xpgqq2mIayL4vupqwygWgiPvc5ZG2Z0+cqTWmTKkJweVf7nAnOAngrv1AQg== X-Received: from tabba.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:482]) (user=tabba job=sendgmr) by 2002:a1c:35c2:: with SMTP id c185mr19332555wma.73.1626710638515; Mon, 19 Jul 2021 09:03:58 -0700 (PDT) Date: Mon, 19 Jul 2021 17:03:36 +0100 In-Reply-To: <20210719160346.609914-1-tabba@google.com> Message-Id: <20210719160346.609914-6-tabba@google.com> Mime-Version: 1.0 References: <20210719160346.609914-1-tabba@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH v3 05/15] KVM: arm64: Refactor sys_regs.h,c for nVHE reuse From: Fuad Tabba To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, christoffer.dall@arm.com, pbonzini@redhat.com, drjones@redhat.com, qperret@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, tabba@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210719_090400_980357_019F75C2 X-CRM114-Status: GOOD ( 17.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Refactor sys_regs.h and sys_regs.c to make it easier to reuse common code. It will be used in nVHE in a later patch. Note that the refactored code uses __inline_bsearch for find_reg instead of bsearch to avoid copying the bsearch code for nVHE. No functional change intended. Signed-off-by: Fuad Tabba Reviewed-by: Andrew Jones Acked-by: Will Deacon Op0, (u32)(x)->Op1, \ - (u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2) - static bool read_from_write_only(struct kvm_vcpu *vcpu, struct sys_reg_params *params, const struct sys_reg_desc *r) @@ -1026,8 +1022,6 @@ static bool access_arch_timer(struct kvm_vcpu *vcpu, return true; } -#define FEATURE(x) (GENMASK_ULL(x##_SHIFT + 3, x##_SHIFT)) - /* Read a sanitised cpufeature ID register by sys_reg_desc */ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r, bool raz) @@ -2106,23 +2100,6 @@ static int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n, return 0; } -static int match_sys_reg(const void *key, const void *elt) -{ - const unsigned long pval = (unsigned long)key; - const struct sys_reg_desc *r = elt; - - return pval - reg_to_encoding(r); -} - -static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params, - const struct sys_reg_desc table[], - unsigned int num) -{ - unsigned long pval = reg_to_encoding(params); - - return bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg); -} - int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu) { kvm_inject_undefined(vcpu); @@ -2365,13 +2342,8 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu) trace_kvm_handle_sys_reg(esr); - params.Op0 = (esr >> 20) & 3; - params.Op1 = (esr >> 14) & 0x7; - params.CRn = (esr >> 10) & 0xf; - params.CRm = (esr >> 1) & 0xf; - params.Op2 = (esr >> 17) & 0x7; + params = esr_sys64_to_params(esr); params.regval = vcpu_get_reg(vcpu, Rt); - params.is_write = !(esr & 1); ret = emulate_sys_reg(vcpu, ¶ms); diff --git a/arch/arm64/kvm/sys_regs.h b/arch/arm64/kvm/sys_regs.h index 9d0621417c2a..cc0cc95a0280 100644 --- a/arch/arm64/kvm/sys_regs.h +++ b/arch/arm64/kvm/sys_regs.h @@ -11,6 +11,12 @@ #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__ #define __ARM64_KVM_SYS_REGS_LOCAL_H__ +#include + +#define reg_to_encoding(x) \ + sys_reg((u32)(x)->Op0, (u32)(x)->Op1, \ + (u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2) + struct sys_reg_params { u8 Op0; u8 Op1; @@ -21,6 +27,14 @@ struct sys_reg_params { bool is_write; }; +#define esr_sys64_to_params(esr) \ + ((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3, \ + .Op1 = ((esr) >> 14) & 0x7, \ + .CRn = ((esr) >> 10) & 0xf, \ + .CRm = ((esr) >> 1) & 0xf, \ + .Op2 = ((esr) >> 17) & 0x7, \ + .is_write = !((esr) & 1) }) + struct sys_reg_desc { /* Sysreg string for debug */ const char *name; @@ -152,6 +166,23 @@ static inline int cmp_sys_reg(const struct sys_reg_desc *i1, return i1->Op2 - i2->Op2; } +static inline int match_sys_reg(const void *key, const void *elt) +{ + const unsigned long pval = (unsigned long)key; + const struct sys_reg_desc *r = elt; + + return pval - reg_to_encoding(r); +} + +static inline const struct sys_reg_desc * +find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[], + unsigned int num) +{ + unsigned long pval = reg_to_encoding(params); + + return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg); +} + const struct sys_reg_desc *find_reg_by_id(u64 id, struct sys_reg_params *params, const struct sys_reg_desc table[],