From patchwork Tue Jul 28 08:26:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 11688647 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA3CE6C1 for ; Tue, 28 Jul 2020 08:27:09 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (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 5FAE32075D for ; Tue, 28 Jul 2020 08:27:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="hUfR4vPb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5FAE32075D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:39856 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k0Kx2-00053F-7q for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jul 2020 04:27:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49848) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0KwN-0003iF-UZ for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:28 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:46334) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k0KwM-0000ow-Bk for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:27 -0400 Received: by mail-pg1-x541.google.com with SMTP id l12so380930pgt.13 for ; Tue, 28 Jul 2020 01:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=W9alUim2NScoKewf8vVRphcseP9RDIaK8xQN1I3tCFA=; b=hUfR4vPbpjqCar2pjDFiPWYhwoLyZhrX23o8xuAdQdTf8S3EWLeEuaJB/0IVzwUSvx 5lSe7nVURS0GYNjGcvMbPkUnddvpzFBjhtAnxRgsMItH7f+AwOruCq/1Id81Fy8ORDkX xx0GlvvWqjhTGF9qrJ0MqdFpsAnashyeu9j6UxbvTUvb9sCiIhNDHisrpU13dE/fKu8U k48EmpiI8wesnKRjbxTj51iTGm/LQMRANGiK5TLtA6M1qWRSfc/LaY6oeRkwQOAc7a5Z zQK5j9PJKE4IdBATKXwxObDOcyIbM60O8i24EAK5gbCJHUYdl5fw0JsTalhCOct1m8sx HYSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=W9alUim2NScoKewf8vVRphcseP9RDIaK8xQN1I3tCFA=; b=DNu1QZeDcSman7a9wtoDGsYwDJAgca6GtyLBNijyU2D4U2ATAkyWVHcrYpSvYdO/Mu n6wVMoHxoB9jOVrDVdL8P4z9siGVLJGnEiWnAl2yCPDpwkvdYhSgOOwHM3pXHCOtIMZ4 5o3GxGO3RJg90/k3INyXcI5IDvCKu1sVFF21YAqpxFKph6/tNNe7XRtTKyZUCv5XnlnF pza20kJaea7oqvg8/sE+Y7IXMfTObcf63Mey7VIFrq3kKM+LPwUcmI2SvhYBCLBvfeFf mUH/ZcS4FzGY5yF0duC7NXewL14FxRlLsw5T5GOerdW13BAuI7tyNZGGPZG06VyMNizx ZgEQ== X-Gm-Message-State: AOAM533DfdeH+w7mJdgFBradlkISN0DBozLEWS70GLxVQu57FdI5oaJU omBinIW7FdouNQGo08lwQsR3Mw== X-Google-Smtp-Source: ABdhPJy07vtYKaWdDiulAvAS6OM0iYZ8F2eefMKq3oDREVTOtAS7BznEtL6odRJe2CFOFdZ7k4AhJw== X-Received: by 2002:a62:ce88:: with SMTP id y130mr24024841pfg.37.1595924785077; Tue, 28 Jul 2020 01:26:25 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id h2sm17599747pfk.93.2020.07.28.01.26.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 01:26:24 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, Alistair.Francis@wdc.com, bmeng.cn@gmail.com, sagark@eecs.berkeley.edu, kbastian@mail.uni-paderborn.de, qemu-riscv@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 1/4] target/riscv: Fix the range of pmpcfg of CSR funcion table Date: Tue, 28 Jul 2020 16:26:14 +0800 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::541; envelope-from=zong.li@sifive.com; helo=mail-pg1-x541.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bin Meng , Alistair Francis , Zong Li Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The range of Physical Memory Protection should be from CSR_PMPCFG0 to CSR_PMPCFG3, not to CSR_PMPADDR9. Signed-off-by: Zong Li Reviewed-by: Alistair Francis Reviewed-by: Bin Meng --- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index ac01c835e1..6a96a01b1c 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -1353,7 +1353,7 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTINST] = { hmode, read_mtinst, write_mtinst }, /* Physical Memory Protection */ - [CSR_PMPCFG0 ... CSR_PMPADDR9] = { pmp, read_pmpcfg, write_pmpcfg }, + [CSR_PMPCFG0 ... CSR_PMPCFG3] = { pmp, read_pmpcfg, write_pmpcfg }, [CSR_PMPADDR0 ... CSR_PMPADDR15] = { pmp, read_pmpaddr, write_pmpaddr }, /* Performance Counters */ From patchwork Tue Jul 28 08:26:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 11688653 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9E5AA6C1 for ; Tue, 28 Jul 2020 08:28:35 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (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 74D962075D for ; Tue, 28 Jul 2020 08:28:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="dlXP83ux" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 74D962075D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:45588 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k0KyQ-0007OQ-OF for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jul 2020 04:28:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0KwQ-0003nM-Rl for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:30 -0400 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]:53315) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k0KwO-0000pQ-Iy for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:30 -0400 Received: by mail-pj1-x1044.google.com with SMTP id a9so11115940pjd.3 for ; Tue, 28 Jul 2020 01:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/Id9a5vUvad08zDtt5SVSxT+Ins5Rla6YBPDd7YbMPI=; b=dlXP83uxY7DEtkclZ1PCWYVObqy64Rt4JDiOI2yEN4I3yjsbNGkjJBuyKNspJ74DS3 pIGD/oIcu5ltnX5zQZlVA0lnACL1qS7JOMlx3lfbdY42IgRclDDOfvBD4DlzVKfDcguo +5yTg8mEXhYtVi0JhEbytHXMAwVvI7t5N8oRDBGuPHy1N6FzXt5GR+CeJjILbwtDY4Cn SMGoXFSx4luxEJK01KEgcqFC1Q+X0ecd1fJCmoB6DwlJ+O7xwhTWUBpI5PmgFCXq2G2S noeTIVz4dcdZWwRp1HrUhcXff8ZVTNWft//3Da4w7CQEe2NRbuUJ7kKPKXo8JSmVWXzo 4T1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/Id9a5vUvad08zDtt5SVSxT+Ins5Rla6YBPDd7YbMPI=; b=FDFTS7mBjAQVG2KpjKFvY2jcxpz0qDWFwmlRSzDCbmr/TI31uZqAANH98nK4K3RglZ teAVBzG4vvntM2W13EQuxudLja+vcCB/GuGTrDCXUw5kZfZM+fx24j74TcrGNrVqG2i+ LmsdqUBw/Qyrb3WfS9/TeJl53uRmsjYW5v1OMr3F/D06w09tKrZlc7Awfi8/qAgo88HC irkUP7MKS4i7RV6FUmQBctlTP40F4Yyhch+4XrPETnBPMWs1cPK61PV6eVtMiNWoQcsx HjvHpROM6JhErDQTntV+/kTHgWJ8VD4cgCxHsf7+vwZeA96gKy6T+xfogSVJZpfcnoEE ugGA== X-Gm-Message-State: AOAM531GqAeQ2UKLwwA6P/BgkyjCy62Hh79VZnoVFQFwf5YLJyyvGZw6 +fBXOZZl35tm30az9vQNxG5f7w== X-Google-Smtp-Source: ABdhPJzVKLyk540Lx9/iqpLpZCLhoVFn0qO/YE2NBK/VHbNutIx8Q7a3x7KUFbdIU7QZV3GOxBPhVQ== X-Received: by 2002:a17:90b:8d7:: with SMTP id ds23mr3405209pjb.148.1595924787293; Tue, 28 Jul 2020 01:26:27 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id h2sm17599747pfk.93.2020.07.28.01.26.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 01:26:26 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, Alistair.Francis@wdc.com, bmeng.cn@gmail.com, sagark@eecs.berkeley.edu, kbastian@mail.uni-paderborn.de, qemu-riscv@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 2/4] target/riscv/pmp.c: Fix the index offset on RV64 Date: Tue, 28 Jul 2020 16:26:15 +0800 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1044; envelope-from=zong.li@sifive.com; helo=mail-pj1-x1044.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bin Meng , Alistair Francis , Zong Li Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" On RV64, the reg_index is 2 (pmpcfg2 CSR) after the seventh pmp entry, it is not 1 (pmpcfg1 CSR) like RV32. In the original implementation, the second parameter of pmp_write_cfg is "reg_index * sizeof(target_ulong)", and we get the the result which is started from 16 if reg_index is 2, but we expect that it should be started from 8. Separate the implementation for RV32 and RV64 respectively. Signed-off-by: Zong Li Reviewed-by: Bin Meng Reviewed-by: Alistair Francis --- target/riscv/pmp.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 2a2b9f5363..aeba796484 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -318,6 +318,10 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, return; } +#if defined(TARGET_RISCV64) + reg_index >>= 1; +#endif + for (i = 0; i < sizeof(target_ulong); i++) { cfg_val = (val >> 8 * i) & 0xff; pmp_write_cfg(env, (reg_index * sizeof(target_ulong)) + i, @@ -335,11 +339,16 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index) target_ulong cfg_val = 0; target_ulong val = 0; + trace_pmpcfg_csr_read(env->mhartid, reg_index, cfg_val); + +#if defined(TARGET_RISCV64) + reg_index >>= 1; +#endif + for (i = 0; i < sizeof(target_ulong); i++) { val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i); cfg_val |= (val << (i * 8)); } - trace_pmpcfg_csr_read(env->mhartid, reg_index, cfg_val); return cfg_val; } From patchwork Tue Jul 28 08:26:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 11688649 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 31D6814DD for ; Tue, 28 Jul 2020 08:27:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (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 08C6F20838 for ; Tue, 28 Jul 2020 08:27:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="gSGX97x+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 08C6F20838 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:40310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k0Kx7-0005Ej-Au for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jul 2020 04:27:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49906) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0KwS-0003pq-5X for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:32 -0400 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:33106) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k0KwQ-0000ph-IK for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:31 -0400 Received: by mail-pl1-x642.google.com with SMTP id t1so348306plq.0 for ; Tue, 28 Jul 2020 01:26:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PMPZNC54WFXLzT2HiWdjfPndP8TkMz19Jn6EwJG2AFc=; b=gSGX97x+US9ilo8NDO3o9WZrYxXIJBLGGLMkAAULkK0jMmzqf7CAgTxLxm9DJbge1P QBSnKlY3R5N6NzbacQskOT3hRLuI0zE53t4hKWXWo7ZX6V1f2Wc5789ol+msOw+WYX+I V+cZSfvy1pccsBLhNZYFyXUfIDnOoI0y3wNIXg9QqJdxFojnUMsZD1mOIsfJt4USUpT5 CMwp0NmjlX0yKWl83bzUXbRTn6HtxFUAl42333s63X7yEGMuSQBMui/fKkNNhADovPjI q7ZA38wZxI3R4LnxlwU6x/+vALD3PapPYq/KC83Kd28e6Jjbr4Vk2EZZMGzgFrgNCDE8 6BdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PMPZNC54WFXLzT2HiWdjfPndP8TkMz19Jn6EwJG2AFc=; b=e8pjgVtiksYS1Y7zqdEaZukmdsP3NmWqBEJolWPT8zIc6OBvfAyR0LoW0qz32IhKLu rEzKOOZ84rINvlKRP+OCavCnJgdU5YoyCP3tZ6thsjgyrfCAMCSb1HNsDR0aFjIXO+E7 +RmBlL/P5P11VKbYTDt0qto8xAGRZ9prDYZVQzMK36nF/40wpBa87tBEyAlFbOswv9df 5D2jTJjOeK9EmZnV+zj8PEIJUUbr2RDyXabL08iAmJL7D37IB3/ueUxwa+uqfjpGNkPx wn3LVEzXWdFna1du9klrsAlHPug74q6XFWoSGepM0T7QTe8QhEeA7BzSwl+EDcIFzG1c GGGQ== X-Gm-Message-State: AOAM532HYe3oXFWAB0CzVocXTkPRz+h7rkvhmRW8PfW1rcXBwuhp+NR+ kGggeAxDIt5AbQxg21oXnxCssg== X-Google-Smtp-Source: ABdhPJze6gyBY7ObJJbFhPylgZ8Z6i79vFvUiKZVgggDq+RZInxVraUNYrreGND5o0A17nc7SB4AGw== X-Received: by 2002:a17:90a:d42:: with SMTP id 2mr3328011pju.15.1595924789289; Tue, 28 Jul 2020 01:26:29 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id h2sm17599747pfk.93.2020.07.28.01.26.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 01:26:28 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, Alistair.Francis@wdc.com, bmeng.cn@gmail.com, sagark@eecs.berkeley.edu, kbastian@mail.uni-paderborn.de, qemu-riscv@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 3/4] target/riscv: Fix the translation of physical address Date: Tue, 28 Jul 2020 16:26:16 +0800 Message-Id: <370a983d0f9e8a9a927b9bb8af5e7bc84b1bf9b1.1595924470.git.zong.li@sifive.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::642; envelope-from=zong.li@sifive.com; helo=mail-pl1-x642.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zong Li Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The real physical address should add the 12 bits page offset. It also causes the PMP wrong checking due to the minimum granularity of PMP is 4 byte, but we always get the physical address which is 4KB alignment, that means, we always use the start address of the page to check PMP for all addresses which in the same page. Signed-off-by: Zong Li Reviewed-by: Alistair Francis --- target/riscv/cpu_helper.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 75d2ae3434..2f337e418c 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -543,7 +543,8 @@ restart: /* for superpage mappings, make a fake leaf PTE for the TLB's benefit. */ target_ulong vpn = addr >> PGSHIFT; - *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; + *physical = ((ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT) | + (addr & ~TARGET_PAGE_MASK); /* set permissions on the TLB entry */ if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) { @@ -630,7 +631,7 @@ hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) } } - return phys_addr; + return phys_addr & TARGET_PAGE_MASK; } void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, From patchwork Tue Jul 28 08:26:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 11688655 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AB6AB6C1 for ; Tue, 28 Jul 2020 08:29:41 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (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 81EEA2075D for ; Tue, 28 Jul 2020 08:29:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="gHmUNT7Z" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 81EEA2075D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:48688 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k0KzU-0000GZ-Qd for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jul 2020 04:29:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49928) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0KwU-0003vV-LK for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:34 -0400 Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:38925) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k0KwS-0000q4-MP for qemu-devel@nongnu.org; Tue, 28 Jul 2020 04:26:34 -0400 Received: by mail-pj1-x1042.google.com with SMTP id f9so4955279pju.4 for ; Tue, 28 Jul 2020 01:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UC0InaM2Av4NdGRB30AcNgGF6NmB2i/F1RQN192G0E0=; b=gHmUNT7Z0hYepK/a/IHgsaHkPfHTaZxEsXHq32AMZzjwvJ2IOrH0ATFeylOFqHKfXf t9oZqyOR8XMUsKY7DlXMMQZdyZNBWChkncRgeqy+ND71AgYtthjW6vGtt7307l29xoRR 4c/ih7hQW+DNqJzkIA31xpO1re6Ukrocb0Zp5o2Bu8MSiclpfuh3U2gTQyWI8i62Kobk HRqK6VP7uchy14bjAPiQZ/W75IJejkPZJEoIJ/fwi9R6T6Lv564N4qmePGihG/5qatVv Q9PLEustxY945RbmoIi5XShbPU6JMpJR48m0joisdfKJPbIRZhHIia27XiZFSFOQp4vD NmPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UC0InaM2Av4NdGRB30AcNgGF6NmB2i/F1RQN192G0E0=; b=bYA5D+IF5rvkUiwZVr0sNEh6d1zle5xxhrhj9hCKsxblXC/KMQFYxOHA8+r9D0r6YC YMxGlm/g4iQPQWN+Wtb2gXqxLebeI+GygWF975hO5kEZSoCBIHVgijydYAGk0LmHVI25 cXprxg/Sc1B1LqLXBh+RIvsp3/M/S6yRm/XRKbEghwCl96TeN2GcfKrEri4tyA4+v5Ph erzMxToNsKCyXBIyYr9b6Q+2QgdCZ4KDPWnykkX10kdq3ngSj8B2NdRPwnhQYOde4dj2 a+Ro93fPIXBLu+yO5/WxI1RWYZpV4YBw6NOFwE58i5Ke6U5f/V9fdVDohMYoiZyn7PGy Jxwg== X-Gm-Message-State: AOAM531VzgneNsN8MKXxQ5YA3dHR9xWvXfoSqmK5W1TKbChtxh+bZJ11 IOIJWJxJr222Gm9RsMGcEUks1A== X-Google-Smtp-Source: ABdhPJw7Wf/PUWOtDu/gHK+DK+I1oe0f4LPcvGnR5zBPgQ9DldM3nWupgVC7g+6FUAXx5nwd4LxPPw== X-Received: by 2002:a17:90a:ea83:: with SMTP id h3mr3524636pjz.176.1595924791246; Tue, 28 Jul 2020 01:26:31 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id h2sm17599747pfk.93.2020.07.28.01.26.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 01:26:30 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, Alistair.Francis@wdc.com, bmeng.cn@gmail.com, sagark@eecs.berkeley.edu, kbastian@mail.uni-paderborn.de, qemu-riscv@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 4/4] target/riscv: Change the TLB page size depends on PMP entries. Date: Tue, 28 Jul 2020 16:26:17 +0800 Message-Id: <6b0bf48662ef26ab4c15381a08e78a74ebd7ca79.1595924470.git.zong.li@sifive.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1042; envelope-from=zong.li@sifive.com; helo=mail-pj1-x1042.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zong Li Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The minimum granularity of PMP is 4 bytes, it is small than 4KB page size, therefore, the pmp checking would be ignored if its range doesn't start from the alignment of one page. This patch detects the pmp entries and sets the small page size to TLB if there is a PMP entry which cover the page size. Signed-off-by: Zong Li Reviewed-by: Alistair Francis --- target/riscv/cpu_helper.c | 10 ++++++-- target/riscv/pmp.c | 52 +++++++++++++++++++++++++++++++++++++++ target/riscv/pmp.h | 2 ++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 2f337e418c..fd1d373b6f 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -693,6 +693,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, bool first_stage_error = true; int ret = TRANSLATE_FAIL; int mode = mmu_idx; + target_ulong tlb_size = 0; env->guest_phys_fault_addr = 0; @@ -784,8 +785,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, } if (ret == TRANSLATE_SUCCESS) { - tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, - prot, mmu_idx, TARGET_PAGE_SIZE); + if (pmp_is_range_in_tlb(env, pa & TARGET_PAGE_MASK, &tlb_size)) { + tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1), + prot, mmu_idx, tlb_size); + } else { + tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, + prot, mmu_idx, TARGET_PAGE_SIZE); + } return true; } else if (probe) { return false; diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index aeba796484..adadf6e9ba 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -393,3 +393,55 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index) return val; } + +/* + * Calculate the TLB size if the start address or the end address of + * PMP entry is presented in thie TLB page. + */ +static target_ulong pmp_get_tlb_size(CPURISCVState *env, int pmp_index, + target_ulong tlb_sa, target_ulong tlb_ea) +{ + target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; + target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; + + if (pmp_sa >= tlb_sa && pmp_ea <= tlb_ea) { + return pmp_ea - pmp_sa + 1; + } + + if (pmp_sa >= tlb_sa && pmp_sa <= tlb_ea && pmp_ea >= tlb_ea) { + return tlb_ea - pmp_sa + 1; + } + + if (pmp_ea <= tlb_ea && pmp_ea >= tlb_sa && pmp_sa <= tlb_sa) { + return pmp_ea - tlb_sa + 1; + } + + return 0; +} + +/* + * Check is there a PMP entry whcih range covers this page. If so, + * try to find the minimum granularity for the TLB size. + */ +bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa, + target_ulong *tlb_size) +{ + int i; + target_ulong val; + target_ulong tlb_ea = (tlb_sa + TARGET_PAGE_SIZE - 1); + + for (i = 0; i < MAX_RISCV_PMPS; i++) { + val = pmp_get_tlb_size(env, i, tlb_sa, tlb_ea); + if (val) { + if (*tlb_size == 0 || *tlb_size > val) { + *tlb_size = val; + } + } + } + + if (*tlb_size != 0) { + return true; + } + + return false; +} diff --git a/target/riscv/pmp.h b/target/riscv/pmp.h index 8e19793132..c70f2ea4c4 100644 --- a/target/riscv/pmp.h +++ b/target/riscv/pmp.h @@ -60,5 +60,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index); bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, target_ulong size, pmp_priv_t priv, target_ulong mode); +bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa, + target_ulong *tlb_size); #endif