From patchwork Mon May 29 12:13:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Kurochko X-Patchwork-Id: 13258487 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 C410FC77B7E for ; Mon, 29 May 2023 12:13:48 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.540603.842450 (Exim 4.92) (envelope-from ) id 1q3bkd-0005DX-Sm; Mon, 29 May 2023 12:13:27 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 540603.842450; Mon, 29 May 2023 12:13:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3bkd-0005DQ-Pw; Mon, 29 May 2023 12:13:27 +0000 Received: by outflank-mailman (input) for mailman id 540603; Mon, 29 May 2023 12:13:27 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3bkd-0005DK-2F for xen-devel@lists.xenproject.org; Mon, 29 May 2023 12:13:27 +0000 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [2a00:1450:4864:20::234]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 36b8513a-fe1a-11ed-b231-6b7b168915f2; Mon, 29 May 2023 14:13:25 +0200 (CEST) Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2af7081c9ebso33984231fa.1 for ; Mon, 29 May 2023 05:13:26 -0700 (PDT) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id z14-20020a2e964e000000b002ac87c15fd4sm2427762ljh.95.2023.05.29.05.13.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 May 2023 05:13:24 -0700 (PDT) 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: 36b8513a-fe1a-11ed-b231-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685362405; x=1687954405; 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=wkQ4zHDkTr4O9GodmGbD3ibtCyQrQEbN6NGk6h/UX0A=; b=d58sQl2RjRtSnDVfLMcJjRKVv1upnmb9HuOWNe/xYJmPy3KUgpdZOWqOGyHyY1UiN1 c1qldE4Id8rQqM/qwM/Y6+tMhWjabRmrOBH/TzzsaYqTZ1FKuB6lBQpJmBCQL5tejuzz uxc6QNunuKRvGTS2RMV3KuXMQAfbkQR9sJpV+Nzly3CskFCeMDq1/ZH1dkEJAI3fUsr2 lvyo8oseMqvIsh0TVDdNBasMESmZzrJOwg4NLM1u8wx7wcwahPcQ35WLShm4+fqQmyDN 5OYTKGZI/CEwikX5y/LyhTX9AsqngumqHBM8ii6vekCz9IMHnKJtkE4afavoiJSrrUnn vfbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685362405; x=1687954405; 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=wkQ4zHDkTr4O9GodmGbD3ibtCyQrQEbN6NGk6h/UX0A=; b=N3LseP5VmksNXALGPd6BW08UaoPk1MgfGeJEfmE13gc3I0W9wZ870aN8ZQXJQkeIWH tHskXeggc1FCTQXelAXm0C71T1igC5Mk3Ld9ww4sMvoYEVUWgCNxy2QmxjK28bJGK3dm gtYN0QYDCqNxu59Af1lgFsyJKEiNZAFaqnUxgryGwGIt5GBhUpEyHpxiK8P9bCzfaf64 DzHLp9+LceyeiDXvtUEZ4++nT8tbPoP5lWNUivO9Rd+zgvYPQe4lbT5sWwMURVCqM4eR yIW5cC2LxoxcaQCS2rIdg4qm7jYSMfKfhEEa43QzEqZaYMOLs5VFAtcqiDPfoATEHqQy zvRg== X-Gm-Message-State: AC+VfDzIuMWbtG70ZGYW5UOrRCstG1M6+Zwo757PAXHQLujr4fXKBa80 HXISeD7qmUMKHLRa2caIcAlVGgFZnNg= X-Google-Smtp-Source: ACHHUZ4Ha9S4qBbRlL0G/+/O2i7GhUTN8dMSJOG9tXm+dMCi/JovQUoEALmoa6jvQUrT/H5v3za5aw== X-Received: by 2002:a2e:9417:0:b0:2ad:8a4b:6a9e with SMTP id i23-20020a2e9417000000b002ad8a4b6a9emr3593392ljh.26.1685362405214; Mon, 29 May 2023 05:13:25 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Julien Grall , Andrew Cooper , Stefano Stabellini , Oleksii Kurochko , Bob Eshleman , Alistair Francis , Connor Davis Subject: [PATCH v6 3/6] xen/riscv: introduce decode_cause() stuff Date: Mon, 29 May 2023 15:13:16 +0300 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 The patch introduces stuff needed to decode a reason of an exception. Signed-off-by: Oleksii Kurochko --- Changes in V6: - Remove usage of LINK_TO_LOAD() due to the MMU being enabled first. - Change early_printk() to printk() --- Changes in V5: - Remove from riscv/traps/c as nothing would require inclusion. - decode_reserved_interrupt_cause(), decode_interrupt_cause(), decode_cause, do_unexpected_trap() were made as static they are expected to be used only in traps.c - use LINK_TO_LOAD() for addresses which can be linker time relative. --- Changes in V4: - fix string in decode_reserved_interrupt_cause() --- Changes in V3: - Nothing changed --- Changes in V2: - Make decode_trap_cause() more optimization friendly. - Merge the pathc which introduces do_unexpected_trap() to the current one. --- xen/arch/riscv/traps.c | 84 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/traps.c b/xen/arch/riscv/traps.c index ccd3593f5a..ea1012e83e 100644 --- a/xen/arch/riscv/traps.c +++ b/xen/arch/riscv/traps.c @@ -4,10 +4,92 @@ * * RISC-V Trap handlers */ + +#include + +#include +#include #include #include -void do_trap(struct cpu_user_regs *cpu_regs) +static const char *decode_trap_cause(unsigned long cause) +{ + static const char *const trap_causes[] = { + [CAUSE_MISALIGNED_FETCH] = "Instruction Address Misaligned", + [CAUSE_FETCH_ACCESS] = "Instruction Access Fault", + [CAUSE_ILLEGAL_INSTRUCTION] = "Illegal Instruction", + [CAUSE_BREAKPOINT] = "Breakpoint", + [CAUSE_MISALIGNED_LOAD] = "Load Address Misaligned", + [CAUSE_LOAD_ACCESS] = "Load Access Fault", + [CAUSE_MISALIGNED_STORE] = "Store/AMO Address Misaligned", + [CAUSE_STORE_ACCESS] = "Store/AMO Access Fault", + [CAUSE_USER_ECALL] = "Environment Call from U-Mode", + [CAUSE_SUPERVISOR_ECALL] = "Environment Call from S-Mode", + [CAUSE_MACHINE_ECALL] = "Environment Call from M-Mode", + [CAUSE_FETCH_PAGE_FAULT] = "Instruction Page Fault", + [CAUSE_LOAD_PAGE_FAULT] = "Load Page Fault", + [CAUSE_STORE_PAGE_FAULT] = "Store/AMO Page Fault", + [CAUSE_FETCH_GUEST_PAGE_FAULT] = "Instruction Guest Page Fault", + [CAUSE_LOAD_GUEST_PAGE_FAULT] = "Load Guest Page Fault", + [CAUSE_VIRTUAL_INST_FAULT] = "Virtualized Instruction Fault", + [CAUSE_STORE_GUEST_PAGE_FAULT] = "Guest Store/AMO Page Fault", + }; + + if ( cause < ARRAY_SIZE(trap_causes) && trap_causes[cause] ) + return trap_causes[cause]; + return "UNKNOWN"; +} + +static const char *decode_reserved_interrupt_cause(unsigned long irq_cause) +{ + switch ( irq_cause ) + { + case IRQ_M_SOFT: + return "M-mode Software Interrupt"; + case IRQ_M_TIMER: + return "M-mode TIMER Interrupt"; + case IRQ_M_EXT: + return "M-mode External Interrupt"; + default: + return "UNKNOWN IRQ type"; + } +} + +static const char *decode_interrupt_cause(unsigned long cause) +{ + unsigned long irq_cause = cause & ~CAUSE_IRQ_FLAG; + + switch ( irq_cause ) + { + case IRQ_S_SOFT: + return "Supervisor Software Interrupt"; + case IRQ_S_TIMER: + return "Supervisor Timer Interrupt"; + case IRQ_S_EXT: + return "Supervisor External Interrupt"; + default: + return decode_reserved_interrupt_cause(irq_cause); + } +} + +static const char *decode_cause(unsigned long cause) +{ + if ( cause & CAUSE_IRQ_FLAG ) + return decode_interrupt_cause(cause); + + return decode_trap_cause(cause); +} + +static void do_unexpected_trap(const struct cpu_user_regs *regs) { + unsigned long cause = csr_read(CSR_SCAUSE); + + printk("Unhandled exception: %s\n", decode_cause(cause)); + die(); } + +void do_trap(struct cpu_user_regs *cpu_regs) +{ + do_unexpected_trap(cpu_regs); +}