From patchwork Tue Jun 18 17:30:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13702791 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0D1813EFEE for ; Tue, 18 Jun 2024 17:32:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731940; cv=none; b=KRhRCmvAIjQjDjppRMrQtqrtAJeN5FiOGrdOhTGaTTtzYDkmwRtx+a/y7Oe0XoNFssBbBgN0O3r7gOAKW8Fd+rsXq7TKWd6Pkoau4Zslcs7sr/uYkSePRO4nmHermQVHlz/7i2slrv1PNzknhcaZTG206Ob6RDSgEH+xihOfYWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731940; c=relaxed/simple; bh=MeWzciPrDjbJv1TM3RG75iFaQAzDIO7TS+r94PXSuTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u7WxMjNHFRz29nXGdJnOq0dlFAEHpPvM41qPX2m1jv2bckjeH3Ze5O/uGL1jJKJuSe61140uuT17BZuXuSbhTm5Nef7MWIRPZ8CENVFFkN6+YozhToRnutNAqzqAFajStC615aREZqsFqDa6JQhr4/t+BYrMBbmOuPDDW8dZAuw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OoelQ6o5; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OoelQ6o5" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7042882e741so4706230b3a.2 for ; Tue, 18 Jun 2024 10:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718731938; x=1719336738; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ypkPbGYy/TpBDV9TrPXgJvZPqdy0Vxo2bZrG/RprffY=; b=OoelQ6o5SG8mtl2ziM2Z6DD4xIJxSDrYjKkaT0QSdYJKaYXq0WDhuafVeXwhr0Y715 vHsIskrHBPB6eq33y36s76t3gGDZ2/JzgPJ7NL88h49Trb3X0M2rDBzumEElv7hB7DOm RlPyT2lKNesG/bJEK0AWC/Q4ofR1Kz1iqrDq0pnU+oKdFTYgdBx+lKB6tu9vF4KrI8Qa 3UeopBkh6rUlMyMEsTLAfK/YXs0MNgOZ5wXL8fp3cvRFivlxwuRjAhKPrAEUHveFX9Cf r7e1lLyAks6laCts1x+SWsOhpOSVfV0QnPPdqYyINk4JXDrw1PDPsZMMvkI1CVu6yYNX jG7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718731938; x=1719336738; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ypkPbGYy/TpBDV9TrPXgJvZPqdy0Vxo2bZrG/RprffY=; b=POaSVQFDBI0dFrUN0du9LlTR8VvXSXPhFVpV7994Zdri+d0Zf13MBoVHAsh5XOOkc3 T/4IP4HX5/Htb6Ke/lsH+HZhdNWwc18rE/AFigaEP2TsHX3ri3vm6ab8bgf19hvXq70p T+S2h1fvvBkrYkL5+P0J27y3DLk6EPVIZ5RDo/fDadF40GAXS6ALpVUbqXUPflaoqiJa 3FDOxpeX7nBXKmH3SbmF4u9kKNBHGH+Qm+mdRuprvu5X61AgiW+aatFcTv6eHVaebN/A pmUmLxqFc2X38gPuPEX/cyAopWxjFK0o7h1mRKdPyoZ6jy+L+9iE2SjyrjU6Eu4l4sZC d3uw== X-Gm-Message-State: AOJu0YwNEkM8i7ZXI7pabtj0JI3ozqqn6iWdzcyf3K+ViJVigmMi4Tcx YD/h5CBnzybQTE8SO0TryIrHdzzTfDOP+cOmJcG9x6e0s5m6cwblHApPmVdM X-Google-Smtp-Source: AGHT+IEmDKZRwJWnZ4x8OKOuCT3HIiVjDep+N0dF149o+YWdExxKftpxAeLEZ49bfDbdiA9ETiWUJQ== X-Received: by 2002:a17:90b:3104:b0:2c2:dd1d:ce6a with SMTP id 98e67ed59e1d1-2c7b5dca8ccmr365401a91.45.1718731937685; Tue, 18 Jun 2024 10:32:17 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ee5a5sm13529305a91.17.2024.06.18.10.32.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 10:32:17 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH 1/4] riscv: Extend exception handling support for interrupts Date: Wed, 19 Jun 2024 01:30:50 +0800 Message-ID: <20240618173053.364776-2-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240618173053.364776-1-jamestiotio@gmail.com> References: <20240618173053.364776-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add install_irq_handler() to enable tests to install interrupt handlers. Also add local_irq_enable() and local_irq_disable() to respectively enable and disable IRQs via the sstatus.SIE bit. Signed-off-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 2 ++ lib/riscv/asm/processor.h | 13 +++++++++++++ lib/riscv/processor.c | 27 +++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index 52608512..d5879d2a 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -11,6 +11,8 @@ #define CSR_STVAL 0x143 #define CSR_SATP 0x180 +#define SSTATUS_SIE (_AC(1, UL) << 1) + /* Exception cause high bit - is an interrupt if set */ #define CAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1)) diff --git a/lib/riscv/asm/processor.h b/lib/riscv/asm/processor.h index 32c499d0..767b1caa 100644 --- a/lib/riscv/asm/processor.h +++ b/lib/riscv/asm/processor.h @@ -5,6 +5,7 @@ #include #define EXCEPTION_CAUSE_MAX 16 +#define INTERRUPT_CAUSE_MAX 16 typedef void (*exception_fn)(struct pt_regs *); @@ -13,6 +14,7 @@ struct thread_info { unsigned long hartid; unsigned long isa[1]; exception_fn exception_handlers[EXCEPTION_CAUSE_MAX]; + exception_fn interrupt_handlers[INTERRUPT_CAUSE_MAX]; }; static inline struct thread_info *current_thread_info(void) @@ -20,7 +22,18 @@ static inline struct thread_info *current_thread_info(void) return (struct thread_info *)csr_read(CSR_SSCRATCH); } +static inline void local_irq_enable(void) +{ + csr_set(CSR_SSTATUS, SSTATUS_SIE); +} + +static inline void local_irq_disable(void) +{ + csr_clear(CSR_SSTATUS, SSTATUS_SIE); +} + void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *)); +void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *)); void do_handle_exception(struct pt_regs *regs); void thread_info_init(void); diff --git a/lib/riscv/processor.c b/lib/riscv/processor.c index ece7cbff..0dffadc7 100644 --- a/lib/riscv/processor.c +++ b/lib/riscv/processor.c @@ -36,10 +36,21 @@ void do_handle_exception(struct pt_regs *regs) { struct thread_info *info = current_thread_info(); - assert(regs->cause < EXCEPTION_CAUSE_MAX); - if (info->exception_handlers[regs->cause]) { - info->exception_handlers[regs->cause](regs); - return; + if (regs->cause & CAUSE_IRQ_FLAG) { + unsigned long irq_cause = regs->cause & ~CAUSE_IRQ_FLAG; + + assert(irq_cause < INTERRUPT_CAUSE_MAX); + if (info->interrupt_handlers[irq_cause]) { + info->interrupt_handlers[irq_cause](regs); + return; + } + } else { + assert(regs->cause < EXCEPTION_CAUSE_MAX); + + if (info->exception_handlers[regs->cause]) { + info->exception_handlers[regs->cause](regs); + return; + } } show_regs(regs); @@ -47,6 +58,14 @@ void do_handle_exception(struct pt_regs *regs) abort(); } +void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *)) +{ + struct thread_info *info = current_thread_info(); + + assert(cause < INTERRUPT_CAUSE_MAX); + info->interrupt_handlers[cause] = handler; +} + void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *)) { struct thread_info *info = current_thread_info(); From patchwork Tue Jun 18 17:30:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13702792 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C94C613F00A for ; Tue, 18 Jun 2024 17:32:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731942; cv=none; b=QN3ku7a6SFOxN7S94CRc2O11frimXlmsebPiUbgNs5GlItxrsXh0EyJvxv96zEyLHUQ0cK4p1fDlumrVwSWpTcjK/q2Nxlz+hQmn4BhHUIlHlADhqYDFehC6U8kpicyb5YFeG2aR+oggHu6djrdWOheQ6bZlfKMd9kHu0Ls0DPQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731942; c=relaxed/simple; bh=GnWNyaD0dn1dpEgub594iNEV5lYBdNv90ZFO+R0wiLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K89DH93Nwcexlj7YtZSaTWD6kg9TmvPaSLdMR1P0YhduUgWKMnW0jfMoj5ETa2yN3r5NJhUxga/knLZscQfUuNcRyCsa2UCQXsmkzog00Xdp7snYAaB3ugHlDR1UYTxmg+NWV4PlV+1WmsELPfLHAem/oOby3YmCqjf/BsfkyBs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G4wQC3IJ; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G4wQC3IJ" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7062c11d0d1so7983b3a.1 for ; Tue, 18 Jun 2024 10:32:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718731940; x=1719336740; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Blulmc7pIET82cKP+ePX3OT26FWkLZf8vE5qV2TfTZA=; b=G4wQC3IJtvwbX3J+E5AzKMSNbl9ZCq0zjtNkRr20/OGSuv0rUlA/CbBqp/kxgrYnaz PlBYfQw0prj9hv/29mrnq0rvBrzallXxx0EcyeJ/QnPmZz4Ei6kEky5rH/6B58DQUA+8 N58CPqO6bhnqublJidAsRxUmKmtWgCrA4izJHArcJlBhE7q7oVkUQgZiruh3n2WvKbwp zdcd0npL5e31Q2odbDR9rvKyEFMjRIGKH9J0ezExdq/yH5FYyhlFXntp2GIc3YTCAUGK uRCOuFhsARDtUx7iF+/zVtc65Gs5cjbSH9YoYfeGrA098pWP0bLuaThXWkWOME3e4Bus 6CCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718731940; x=1719336740; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Blulmc7pIET82cKP+ePX3OT26FWkLZf8vE5qV2TfTZA=; b=TeTR7ubBICt9sfwnvXRtQo8JPoHnv88mrQ8qNAKz11O8cZTOTzVPYkEn0rl7OH1Aay Hx/fedrZJeS6DnDuwl0SzQ59IYxCyd2dHH0OluP2/gA56CVI1L/I3kSkpC7fDQwyZc8e OFfBmrq7OEUOUTC9R6Q0KZyY/HmhbutiJB9VpnbF84L8kHn8IdwfXjnsGQzEeFDFte+l 3pse3kqyWJMKzoQMw0dBw9snUQcpSMlX/A+26aWRfO88LDq+1TIvL1hyaaqL7NIGnvwh DHH/wVmZ8AduAHgA90rNX6FdEHNDIMeZRtP1cDbhC0FJIKpYfTeDspUq9U46LLd/aeU3 2meA== X-Gm-Message-State: AOJu0YynEcrqnL0uNxiigsFwFmfbA/Mnn9wvPISQK0hvrw1EmlauLhIp X+cU8zG2oGSXDFg0cM+f587Vm914Wx9Uqjaj28tvr5Qlf32dlE1Eahs9aiZR X-Google-Smtp-Source: AGHT+IGx8S9jQ3ds3aVy2ZQbfAouACrLu1UZ1btjCp4nsn/uJImlHjZZR82m8v5QLbJBPdxDfrYm8g== X-Received: by 2002:a17:90b:f92:b0:2bd:ef6b:c33b with SMTP id 98e67ed59e1d1-2c7b4a682a0mr513360a91.0.1718731939782; Tue, 18 Jun 2024 10:32:19 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ee5a5sm13529305a91.17.2024.06.18.10.32.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 10:32:19 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH 2/4] riscv: Update exception cause list Date: Wed, 19 Jun 2024 01:30:51 +0800 Message-ID: <20240618173053.364776-3-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240618173053.364776-1-jamestiotio@gmail.com> References: <20240618173053.364776-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Update the list of exception and interrupt causes to follow the latest RISC-V privileged ISA specification (version 20240411). Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 15 +++++++++------ lib/riscv/asm/processor.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index d5879d2a..c1777744 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -26,15 +26,18 @@ #define EXC_STORE_MISALIGNED 6 #define EXC_STORE_ACCESS 7 #define EXC_SYSCALL 8 -#define EXC_HYPERVISOR_SYSCALL 9 -#define EXC_SUPERVISOR_SYSCALL 10 +#define EXC_SUPERVISOR_SYSCALL 9 #define EXC_INST_PAGE_FAULT 12 #define EXC_LOAD_PAGE_FAULT 13 #define EXC_STORE_PAGE_FAULT 15 -#define EXC_INST_GUEST_PAGE_FAULT 20 -#define EXC_LOAD_GUEST_PAGE_FAULT 21 -#define EXC_VIRTUAL_INST_FAULT 22 -#define EXC_STORE_GUEST_PAGE_FAULT 23 +#define EXC_SOFTWARE_CHECK 18 +#define EXC_HARDWARE_ERROR 19 + +/* Interrupt causes */ +#define IRQ_SUPERVISOR_SOFTWARE 1 +#define IRQ_SUPERVISOR_TIMER 5 +#define IRQ_SUPERVISOR_EXTERNAL 9 +#define IRQ_COUNTER_OVERFLOW 13 #ifndef __ASSEMBLY__ diff --git a/lib/riscv/asm/processor.h b/lib/riscv/asm/processor.h index 767b1caa..5942ed2e 100644 --- a/lib/riscv/asm/processor.h +++ b/lib/riscv/asm/processor.h @@ -4,7 +4,7 @@ #include #include -#define EXCEPTION_CAUSE_MAX 16 +#define EXCEPTION_CAUSE_MAX 64 #define INTERRUPT_CAUSE_MAX 16 typedef void (*exception_fn)(struct pt_regs *); From patchwork Tue Jun 18 17:30:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13702793 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11E6C146A88 for ; Tue, 18 Jun 2024 17:32:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731946; cv=none; b=J5OBGQwMKviid67cqRo4m97fMv2EO3XKydixy04QWl8vn1iEZmlg4uRCe7dFc2c4IPPAUnQ2WcZP5g8VIljIEVSV8PNf1/26QrbIGwvVJHptk+qXLOSmy1rsWbSEviIwnQGC3Ndr2+lMgcCofNtBeofOq2Yu1s5y1ubPAYBoXtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731946; c=relaxed/simple; bh=nmIMTenljNjLUNMw2DuUzDYXcii4qGa5ncqWWGnRWJY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YawaDCrYwrMH1CahGns3GGu+9cfR74D2o19R29ycOyzGB4zX6SSoq8buOf6Ynbj+FHfLbWvVv/chDiMHdQXFR1eBpb2WY8Z5xufMmeXSo37pLlT8rsYdcLGVW9tJiAG0RIWfTexUBA3Y0Bd+YWmec+hIyObhjpH66LI/D9TgtSQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JrhIN+rE; arc=none smtp.client-ip=209.85.216.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JrhIN+rE" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2c4c7eb425fso4621538a91.1 for ; Tue, 18 Jun 2024 10:32:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718731943; x=1719336743; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PWxGifUK2SvPEf1BO6CmMLPV00mCLKHwlYt6Zz7pgPc=; b=JrhIN+rEaPf2RjH5kusyDLGLwSoS8pj6BieSnk8o1DH5vNo//a0cuQ4/Y/oF9tDQrV PUETY9my0HS5okc8BuPaziTdbF/0Gi+B3qS+kPF7BSM+CVY02iChNi+S2W78qGlHY+h+ Jw93yOxlXhqym8nG1En5fsC/NddoEP3Q3E5TsFL0Ojb6oLOzhiFGDnUUacrBwhFelJtR 3KA9qUlRvQ7ltTmSOAHXxwAbUQoC3EXFlAL9F1Lbv2iG2sWlywKH5hv6qpPe1Ue5YmO6 saK24C9zPj/YUWAPcWcdnlO6SKGiEMtWDgfF0yKu3G51p7h6mx3kyx87xOxp4Tky/9Ox Bpjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718731943; x=1719336743; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PWxGifUK2SvPEf1BO6CmMLPV00mCLKHwlYt6Zz7pgPc=; b=IJ81dD3wOGlJmStfRrwHGuUwekzUhA+hzM4BbJqo80LOK8i9n/2vXjfNBZWCLJr8pc d8ruibWPxzLn3rethpWuoGkpm9oETIIufTap7Co8xH2G47hnjKrUWoKGXOmbL0eEOMww gmLTaYQiT29OxQdIgK8d4OTHRueLfazp5VvktKxxN/jI8/szSH4ENmQEu2cbjLMisWgj LbBH2MANopp085Y/RzFJkdK916Kgektv0GoRulbDlJYesS1eMQIbwYoa+GdyCRHllmnj gwbmGOgo6jftQAicgo43eyBAZ92Z34dMQ8j048OHkfIbyzgWU5oQX4qRSH67mdRqbpOn lIvA== X-Gm-Message-State: AOJu0YyZu4W/hruMJdPoQe5gRtx+LqOuad/v6WFld4yWudmRPVtQ2h8T ru6MZztCTSl60856W18qHuD7PoLbSzaLoaIMADI2xKuUlqrm/iTwb2tmN0bR X-Google-Smtp-Source: AGHT+IG0z7MBG/kZ+UiDHnDdP5GEeh4qij7yknA/KoUMASQ8r7/Z5Ko3g4gmX+10IevrwdjVn4XP2w== X-Received: by 2002:a17:90a:c908:b0:2c4:b12b:fcc with SMTP id 98e67ed59e1d1-2c7b5cc800fmr359986a91.27.1718731941941; Tue, 18 Jun 2024 10:32:21 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ee5a5sm13529305a91.17.2024.06.18.10.32.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 10:32:21 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH 3/4] riscv: Add methods to toggle interrupt enable bits Date: Wed, 19 Jun 2024 01:30:52 +0800 Message-ID: <20240618173053.364776-4-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240618173053.364776-1-jamestiotio@gmail.com> References: <20240618173053.364776-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add some helper methods to toggle the interrupt enable bits in the SIE register. Signed-off-by: James Raphael Tiovalen --- riscv/Makefile | 1 + lib/riscv/asm/csr.h | 7 +++++++ lib/riscv/asm/interrupt.h | 12 ++++++++++++ lib/riscv/interrupt.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 lib/riscv/asm/interrupt.h create mode 100644 lib/riscv/interrupt.c diff --git a/riscv/Makefile b/riscv/Makefile index 919a3ebb..108d4481 100644 --- a/riscv/Makefile +++ b/riscv/Makefile @@ -30,6 +30,7 @@ cflatobjs += lib/memregions.o cflatobjs += lib/on-cpus.o cflatobjs += lib/vmalloc.o cflatobjs += lib/riscv/bitops.o +cflatobjs += lib/riscv/interrupt.o cflatobjs += lib/riscv/io.o cflatobjs += lib/riscv/isa.o cflatobjs += lib/riscv/mmu.o diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index c1777744..da58b0ce 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -4,15 +4,22 @@ #include #define CSR_SSTATUS 0x100 +#define CSR_SIE 0x104 #define CSR_STVEC 0x105 #define CSR_SSCRATCH 0x140 #define CSR_SEPC 0x141 #define CSR_SCAUSE 0x142 #define CSR_STVAL 0x143 +#define CSR_SIP 0x144 #define CSR_SATP 0x180 #define SSTATUS_SIE (_AC(1, UL) << 1) +#define SIE_SSIE (_AC(1, UL) << 1) +#define SIE_STIE (_AC(1, UL) << 5) +#define SIE_SEIE (_AC(1, UL) << 9) +#define SIE_LCOFIE (_AC(1, UL) << 13) + /* Exception cause high bit - is an interrupt if set */ #define CAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1)) diff --git a/lib/riscv/asm/interrupt.h b/lib/riscv/asm/interrupt.h new file mode 100644 index 00000000..b760afbb --- /dev/null +++ b/lib/riscv/asm/interrupt.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_INTERRUPT_H_ +#define _ASMRISCV_INTERRUPT_H_ + +#include + +void toggle_software_interrupt(bool enable); +void toggle_timer_interrupt(bool enable); +void toggle_external_interrupt(bool enable); +void toggle_local_cof_interrupt(bool enable); + +#endif /* _ASMRISCV_INTERRUPT_H_ */ diff --git a/lib/riscv/interrupt.c b/lib/riscv/interrupt.c new file mode 100644 index 00000000..bc0e16f1 --- /dev/null +++ b/lib/riscv/interrupt.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024, James Raphael Tiovalen + */ +#include +#include +#include + +void toggle_software_interrupt(bool enable) +{ + if (enable) + csr_set(CSR_SIE, SIE_SSIE); + else + csr_clear(CSR_SIE, SIE_SSIE); +} + +void toggle_timer_interrupt(bool enable) +{ + if (enable) + csr_set(CSR_SIE, SIE_STIE); + else + csr_clear(CSR_SIE, SIE_STIE); +} + +void toggle_external_interrupt(bool enable) +{ + if (enable) + csr_set(CSR_SIE, SIE_SEIE); + else + csr_clear(CSR_SIE, SIE_SEIE); +} + +void toggle_local_cof_interrupt(bool enable) +{ + if (enable) + csr_set(CSR_SIE, SIE_LCOFIE); + else + csr_clear(CSR_SIE, SIE_LCOFIE); +} From patchwork Tue Jun 18 17:30:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13702794 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAD80146D4D for ; Tue, 18 Jun 2024 17:32:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731948; cv=none; b=AG6hVA8+6cTLUvEP1XQ7v86BjBKFI9l/S0ciLjKspjEYG7usFfBOCdti86hICQlIrytlyt58Ca2Id/jy48vty+WyD4e6PJCkX5kuF/30THRe/ZpTIl8AIDAnGIn8KX7I3d2GZB3MMQhJChUK4L1VAqib0ee4pHHm6dm+uaPXDxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718731948; c=relaxed/simple; bh=Hu+FYb6PXyf1xaV7Tf3xHtffCUNny7P9Dsa272vQ6fg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ql7bBo4RX439lmKrHR0LYnwheRMgMch5KAVz17Y6JG+39qZovXGofaEYQEr2/WmTO6a9ZjApVL2NUKytTZJidBCe3SD3uqF0JqQhQnx9+9uVwaGfKi87YvnTc/JhYvcrIs9HpNTOvU5S4A7MlLu98IkOrb1PeZ1F9wM4w2rZsrM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=W8gOOmbY; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W8gOOmbY" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-6e4e6230f42so42386a12.0 for ; Tue, 18 Jun 2024 10:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718731946; x=1719336746; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uJ0IRqtJQTxYM5sbu/JBOLam6TAEXhPiQOr+HxYnzUE=; b=W8gOOmbYfc3jUFde3EAEvht92PUXFEEgQ1SWrKC4bDJNM7Zemhy32Jb07M+q12gcA3 RFfwzgTcMoNk/NLqgCF1+Js730G8zPKgAEZdloM4yGZvP6WIkpw4eEWSyVDBR719e43F cs0tU4UmypqwNzSi4ypvb/Rgd8CJpVFUOpLcybx7b8Bt2hDRmMKITsGbF3LMKtogMuML xXc8cm3ZLcVIRhFdGaWJgZH6wsdjjpufQHsIbFZmuwcLqKzuntBnLfsFyWmE7HXKBl5T mC67PMy7xAmAvfvAncHmxPmsjzIVD/B/KHUtMbu4PrINNBGZPWpU4HLLn0YlLhMHA60n wkig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718731946; x=1719336746; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uJ0IRqtJQTxYM5sbu/JBOLam6TAEXhPiQOr+HxYnzUE=; b=a5E7P7fXxlAKZd0FDRFN5GCT0lawEkqQqQ3WLcpj0MXYyfLMns+3j6mYjydRhEbb5S FroO+sQsnhNtfZcdyAN8JewF8J2wBXkbW4rga9JTMVQZGZQdCtrsGHldXewphvq7xCZs 3xB2lxW0doe7y3zDbLGt040hfo72r6bf7oJonbm2F+DaXqI1HkqYKkYI7wUdPqICv3tl b1w7AGwewHO/j8a9aYHA+JZvczWILI39PayV9++DSpjyEDIzQ+7By/oDz8FGCVkJs7Uv Y9z1NxOitM3aYOOgEY28/OFy3A11kA8cyHioPZ/3lvnr+Cz+16o2slRwBmdNxQA9g9Qf Jdfw== X-Gm-Message-State: AOJu0YzE7tw2tnQ//hjEn1r/gskx0KYhKfdh0Z7xRfCi1aU57GOHlwoo nsCop0/En8JindPmGWHO8M7QfQXdyJWVEW+vGmZIgag1zpjcp18nhXwM6S4r X-Google-Smtp-Source: AGHT+IHkLL4UhbVsE2pTLofbKhxmfl27h0FIlAgw5Vx/t7KgFu3OHOmecQE0PalhWlfXeyW89WK2Kw== X-Received: by 2002:a17:90a:6d24:b0:2c2:12d:fa01 with SMTP id 98e67ed59e1d1-2c6c951fa1bmr4819776a91.11.1718731945445; Tue, 18 Jun 2024 10:32:25 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ee5a5sm13529305a91.17.2024.06.18.10.32.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 10:32:25 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH 4/4] riscv: sbi: Add test for timer extension Date: Wed, 19 Jun 2024 01:30:53 +0800 Message-ID: <20240618173053.364776-5-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240618173053.364776-1-jamestiotio@gmail.com> References: <20240618173053.364776-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a test for the set_timer function of the time extension. The test checks that: - The time extension is available - The time counter monotonically increases - The installed timer interrupt handler is called - The timer interrupt is received within a reasonable time frame The timer interrupt delay can be set using the TIMER_DELAY environment variable. If the variable is not set, the default delay value is 1000000. The time interval used to validate the timer interrupt is between the specified delay and double the delay. Because of this, the test might fail if the delay is too short. Hence, we set the default delay value as the minimum value. This test has been verified on RV32 and RV64 with OpenSBI using QEMU. Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 6 ++++ lib/riscv/asm/sbi.h | 5 +++ riscv/sbi.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index da58b0ce..c4435650 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -12,6 +12,7 @@ #define CSR_STVAL 0x143 #define CSR_SIP 0x144 #define CSR_SATP 0x180 +#define CSR_TIME 0xc01 #define SSTATUS_SIE (_AC(1, UL) << 1) @@ -108,5 +109,10 @@ : "memory"); \ }) +#define wfi() \ +({ \ + __asm__ __volatile__("wfi" ::: "memory"); \ +}) + #endif /* !__ASSEMBLY__ */ #endif /* _ASMRISCV_CSR_H_ */ diff --git a/lib/riscv/asm/sbi.h b/lib/riscv/asm/sbi.h index d82a384d..eb4c77ef 100644 --- a/lib/riscv/asm/sbi.h +++ b/lib/riscv/asm/sbi.h @@ -18,6 +18,7 @@ enum sbi_ext_id { SBI_EXT_BASE = 0x10, SBI_EXT_HSM = 0x48534d, SBI_EXT_SRST = 0x53525354, + SBI_EXT_TIME = 0x54494D45, }; enum sbi_ext_base_fid { @@ -37,6 +38,10 @@ enum sbi_ext_hsm_fid { SBI_EXT_HSM_HART_SUSPEND, }; +enum sbi_ext_time_fid { + SBI_EXT_TIME_SET_TIMER = 0, +}; + struct sbiret { long error; long value; diff --git a/riscv/sbi.c b/riscv/sbi.c index 762e9711..6ad1dff6 100644 --- a/riscv/sbi.c +++ b/riscv/sbi.c @@ -6,8 +6,13 @@ */ #include #include +#include +#include +#include #include +static bool timer_work; + static void help(void) { puts("Test SBI\n"); @@ -19,6 +24,18 @@ static struct sbiret __base_sbi_ecall(int fid, unsigned long arg0) return sbi_ecall(SBI_EXT_BASE, fid, arg0, 0, 0, 0, 0, 0); } +static struct sbiret __time_sbi_ecall(int fid, unsigned long arg0) +{ + return sbi_ecall(SBI_EXT_TIME, fid, arg0, 0, 0, 0, 0, 0); +} + +static void timer_interrupt_handler(struct pt_regs *regs) +{ + timer_work = true; + toggle_timer_interrupt(false); + local_irq_disable(); +} + static bool env_or_skip(const char *env) { if (!getenv(env)) { @@ -112,6 +129,75 @@ static void check_base(void) report_prefix_pop(); } +static void check_time(void) +{ + struct sbiret ret; + unsigned long begin, end, duration; + const unsigned long default_delay = 1000000; + unsigned long delay = getenv("TIMER_DELAY") + ? MAX(strtol(getenv("TIMER_DELAY"), NULL, 0), default_delay) + : default_delay; + + report_prefix_push("time"); + + ret = __base_sbi_ecall(SBI_EXT_BASE_PROBE_EXT, SBI_EXT_TIME); + + if (ret.error) { + report_fail("probing for time extension failed"); + report_prefix_pop(); + return; + } + + if (!ret.value) { + report_skip("time extension not available"); + report_prefix_pop(); + return; + } + + begin = csr_read(CSR_TIME); + end = csr_read(CSR_TIME); + if (begin >= end) { + report_fail("time counter has decreased"); + report_prefix_pop(); + return; + } + + report_prefix_push("set_timer"); + + install_irq_handler(IRQ_SUPERVISOR_TIMER, timer_interrupt_handler); + local_irq_enable(); + + begin = csr_read(CSR_TIME); + ret = __time_sbi_ecall(SBI_EXT_TIME_SET_TIMER, csr_read(CSR_TIME) + delay); + + if (ret.error) { + report_fail("setting timer failed"); + install_irq_handler(IRQ_SUPERVISOR_TIMER, NULL); + report_prefix_pop(); + report_prefix_pop(); + return; + } + + toggle_timer_interrupt(true); + + while ((!timer_work) && (csr_read(CSR_TIME) <= (begin + delay))) + wfi(); + + end = csr_read(CSR_TIME); + report(timer_work, "timer interrupt received"); + + install_irq_handler(IRQ_SUPERVISOR_TIMER, NULL); + __time_sbi_ecall(SBI_EXT_TIME_SET_TIMER, -1); + + duration = end - begin; + if (timer_work) + report((duration >= delay) && (duration <= (delay + delay)), "timer delay honored"); + + report_prefix_pop(); + + report_prefix_pop(); +} + int main(int argc, char **argv) { @@ -122,6 +208,7 @@ int main(int argc, char **argv) report_prefix_push("sbi"); check_base(); + check_time(); return report_summary(); }