From patchwork Sun Jul 28 16:50:18 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: 1965733 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=EK/2f0BE; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=c9J/ceAz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WX6tp3VMSz20Mr for ; Mon, 29 Jul 2024 02:50:40 +1000 (AEST) 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:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fWoDH6BiZYLffOMrM5kuhHA6iThHdAA6o8jmrnMRy7E=; b=EK/2f0BEjfMZz+ 914shxjCIvLkKQXU+6ehj+Coo3vry6OwLeCWZISRyFgW1pqAy+vcRuNplYqKbEu20xyz72L4/63aQ 6vrayHd0/44pX1KIQj5m3tq0gJuaoeKuqqfqP9od1aWb4SI+o75rp0clrbTAE414WGB6SkT1b9tsM nNjD18f+j6e9qS8F0af0/kLPnHAXND7pclBy1VV2fsxkp7oRpKSJSHbChp9KlNdW1rft9pqcxe16V 71621NRTWa86QvN6LHpJhAdlqbhHvOGAyshUDFTgpmJA9/X3e2JmWnUwQAFqg4Bj6+awEysNirClS qjtPTCMeYmT+l4MrfjwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sY76R-00000008u0s-3nZT; Sun, 28 Jul 2024 16:50:35 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sY76P-00000008u01-0EGU for kvm-riscv@lists.infradead.org; Sun, 28 Jul 2024 16:50:34 +0000 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2cf11b91813so1496253a91.1 for ; Sun, 28 Jul 2024 09:50:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722185432; x=1722790232; darn=lists.infradead.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=joTVeUit/9PHPnwE5WAgfkXToAh6iydIyxEEP0jrhcY=; b=c9J/ceAzGyVycVG/BSM/a/EEHnxgaN0oIxPRhRTZ5qHDbgFR6/tZ380mYzCKsvj7c+ CPVMxCTYKqkBpfqewp4Z9Hegn6TgEXIeSxXz9APaUU6bYkpIMSNekcGaPK2sD69yTGD0 k939rIkAGpSuFnPpKE56d6qkj/Xv1rg2q11CM/sgLBsHF36Qi2NF+Ji7/tnLbXZO3Il8 6NPWFiStBMgn3oJVs5LIE8xxrgJs+bImdqgWGsfzC+lfzE7DO3+MEHxZAJXzoM2dzQVn 8IymAkpv+S2sixBw+QG0qcBBZJ4M28gwd/PTgs06l7WPl3yVd4PMIYhJu420r7qFxFPg ecwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722185432; x=1722790232; 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=joTVeUit/9PHPnwE5WAgfkXToAh6iydIyxEEP0jrhcY=; b=b3/pzVffZVZYly2z8wz/rY02Awwx0/MiDXmJ2UBcXwuQAOi+3uoLW5VjPGrtYd9DTd CTn5KqmJYvC3dqnrar1z/NYCg8hbiAE2IHr7Pn2uVoJDxDhvaG+ngK9tth+gLjiCa2C2 GzTenfH1p5TubeAVPFzfZA1tzhETHOLiiIHSVCewnYfVnAQuvb/gfOkSiClwiTIM9Zv3 9/EyYdQJQsEoO8JR7yoJJURSUI4Ty6bijPKU8dIcpdt8HoHZ27SofX/tGb7oYRVR47q8 min6FLfI58P+OfIDlleBaoK2qvE+Tchdf/vxHxKyBnIQo/uRtOOgafWgKWk0qRFmzwcL zdpg== X-Forwarded-Encrypted: i=1; AJvYcCXcljFwIyg+xtpUxSot+JxmJ0OVFYOcu04wEq9Oj8nLRy6sUX+5BoADmuZcPHMVQugxiY/VFnXqrOyJm2kSigCZQhR4+F0AxUbf2uAwog== X-Gm-Message-State: AOJu0YwWBCqbjToLNzMVXRbplmaqDF2IcdcRBsTQng9S5foAXp8+jGC0 Ypk6+uhmGokS9R89wQgjRFyD7NvxcngiECSyhspXjzDrobF9kswI X-Google-Smtp-Source: AGHT+IEJDBZ/ILl91arsRWdVOrvUi1IkAk95+X2hAc9tsD/3+c8iLAyb4IqEO5U5DnAaK7CwZ12yjw== X-Received: by 2002:a17:90a:1787:b0:2c9:75c6:32dc with SMTP id 98e67ed59e1d1-2cf7e09a8b8mr3830127a91.1.1722185431675; Sun, 28 Jul 2024 09:50:31 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c7b0besm6969413a91.14.2024.07.28.09.50.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jul 2024 09:50:31 -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 v5 1/5] riscv: Extend exception handling support for interrupts Date: Mon, 29 Jul 2024 00:50:18 +0800 Message-ID: <20240728165022.30075-2-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240728165022.30075-1-jamestiotio@gmail.com> References: <20240728165022.30075-1-jamestiotio@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240728_095033_121610_64DE897A X-CRM114-Status: GOOD ( 13.09 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Andrew Jones 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. Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:102a listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [jamestiotio(at)gmail.com] X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Andrew Jones 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..d6909d93 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 SR_SIE _AC(0x00000002, UL) + /* 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..6451adb5 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, SR_SIE); +} + +static inline void local_irq_disable(void) +{ + csr_clear(CSR_SSTATUS, SR_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();