From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id sKLmKo6vKmCIMwAAWB0awg (envelope-from ) for ; Mon, 15 Feb 2021 12:29:50 -0500 Received: by simark.ca (Postfix, from userid 112) id 1F9871EF82; Mon, 15 Feb 2021 12:29:49 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,MAILING_LIST_MULTI, RDNS_NONE,T_DKIM_INVALID,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C9F3E1E939 for ; Mon, 15 Feb 2021 12:29:46 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13C47396EC66; Mon, 15 Feb 2021 17:29:36 +0000 (GMT) Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 22CCD396EC56 for ; Mon, 15 Feb 2021 17:29:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 22CCD396EC56 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wr1-x430.google.com with SMTP id u14so9903957wri.3 for ; Mon, 15 Feb 2021 09:29:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qG7+97F2/JTBMBU179B71bTr3VtePCdeida/ZLweW5g=; b=HURrR0gjwEsP4VxQEHx72WhbXBTyOcW9+AgLMA8Joa6zpBEN0aQRJ137Dtsp2x7zif yP3PDwPeiEEjVCKpYfEHuWP5Mbw7Bj2L2oDEGnBoE+US8XYyCIqWkXi2dXZDFndfmXyN 0ZpHA2DHYgkXTOPNEJy1jXVtJ82W8mnpkXAhiXMF+2mxO3gmeENpvGfoWnKPrenhjXTw M7/TEVNxkjz6KBILKJpNSr/dtLlIjPGrKUPilcjxc30td2/3PNmRxNvBit1WDKrIhKoi LvNQNC8sVyVjQ7/7Rvl8F44y6Wow6a3cw6EXxWRQLNAqt5XjolDiIR4jU+wkVT5JrYhV nPSw== 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=qG7+97F2/JTBMBU179B71bTr3VtePCdeida/ZLweW5g=; b=dMFer0do2Tzf3lR74HX+YhbaDNl9ufNQTs/9BpRFIAf9vObrv9IAmstKR6hkwK1C8f 8UN+BggWNg+QDl7ofrbgKiJETXn/Gv5ImFjd9F4/ZDnRtQEAYCIOatAv4qBFzWrPPPBA l5dyddtor7/p7C0anTqaBb3Cz+Uj71vaS0zAIVDDnz1mZfsZ1OlmJLdGevAFtgCgNYII BZ1CoDd12rkeMiogSb7/MkfmqNNBu+9OPTNmidxbPwJa0wVxJDrfMTWKvpp6ajvh6FYb q4Q7L9qrGzoDyKqgwPycY2HG+NR/nnU1I6IOJY4o51KNbgn8jbwDk5NVPHMUcQyV8v2x Dzhg== X-Gm-Message-State: AOAM532OqlEXuvj5zn5D6CHtIFM4esPTRmvxNyXGEClK2ymQfdToSe+T I/G+xX8QacjMl7JfKURnrhSPeMe2OTWp/Q== X-Google-Smtp-Source: ABdhPJzQq8uT5R3gjuNz4bWWo+5puakVozcyDTLXKXMIaVP8m3njY3I2HeReSk7GpJ1i0p+QqYCsVw== X-Received: by 2002:a5d:50d2:: with SMTP id f18mr20406386wrt.338.1613410171022; Mon, 15 Feb 2021 09:29:31 -0800 (PST) Received: from localhost (host86-186-80-154.range86-186.btcentralplus.com. [86.186.80.154]) by smtp.gmail.com with ESMTPSA id v5sm15886wmh.2.2021.02.15.09.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Feb 2021 09:29:30 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org, binutils@sourceware.org Subject: [PATCHv3 8/9] gdb/riscv: write CSRs into baremetal core dumps Date: Mon, 15 Feb 2021 17:29:11 +0000 Message-Id: <7c6ff5095556761626d1a8b9b6ed95825f6d32ac.1613410057.git.andrew.burgess@embecosm.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Use the current target description to include CSRs into the RISC-V baremetal core dumps. Every CSR declared in the current target description will be included in the core dump. It will be critical for users that they have the same target description in use when loading the core file as was in use when writing the core file. This should be fine if the user allows the target description to be written into the core file. In more detail, this commit adds a NT_RISCV_CSR note type. The contents of this section is a series of either 4-byte (on RV32 targets), or 8-byte (on RV64 targets) values. Every CSR that is mentioned in the current target description is written out in the order the registers appear in the target description. As a consequence it is critical that the exact same target description, including the same register order, is in use when the CSRs are loaded from the core file. gdb/ChangeLog: * riscv-none-tdep.c: Add 'user-regs.h' and 'target-description.h' includes. (riscv_csrset): New static global. (riscv_update_csrmap): New function. (riscv_iterate_over_regset_sections): Process CSRs. --- gdb/ChangeLog | 9 +++++++ gdb/riscv-none-tdep.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/gdb/riscv-none-tdep.c b/gdb/riscv-none-tdep.c index f1ac592bfac..3247346ee94 100644 --- a/gdb/riscv-none-tdep.c +++ b/gdb/riscv-none-tdep.c @@ -23,6 +23,8 @@ #include "riscv-tdep.h" #include "elf-bfd.h" #include "regset.h" +#include "user-regs.h" +#include "target-descriptions.h" #ifdef HAVE_ELF #include "elf-none-tdep.h" @@ -65,6 +67,42 @@ static const struct regset riscv_fregset = riscv_fregmap, riscv_supply_regset, regcache_collect_regset }; +/* Define the CSR regset, this is not constant as the regmap field is + updated dynamically based on the current target description. */ + +static struct regset riscv_csrset = +{ + nullptr, regcache_supply_regset, regcache_collect_regset +}; + +/* Update the regmap field of RISCV_CSRSET based on the CSRs available in + the current target description. */ + +static void +riscv_update_csrmap (struct gdbarch *gdbarch, + const struct tdesc_feature *feature_csr) +{ + int i = 0; + + /* Release any previously defined map. */ + delete[] ((struct regcache_map_entry *) riscv_csrset.regmap); + + /* Now create a register map for every csr found in the target + description. */ + struct regcache_map_entry *riscv_csrmap + = new struct regcache_map_entry[feature_csr->registers.size() + 1]; + for (auto &csr : feature_csr->registers) + { + int regnum = user_reg_map_name_to_regnum (gdbarch, csr->name.c_str(), + csr->name.length()); + riscv_csrmap[i++] = {1, regnum, 0}; + } + + /* Mark the end of the array. */ + riscv_csrmap[i] = {0}; + riscv_csrset.regmap = riscv_csrmap; +} + /* Implement the "iterate_over_regset_sections" gdbarch method. */ static void @@ -84,6 +122,28 @@ riscv_iterate_over_regset_sections (struct gdbarch *gdbarch, + register_size (gdbarch, RISCV_CSR_FCSR_REGNUM)); cb (".reg2", sz, sz, &riscv_fregset, NULL, cb_data); } + + /* Read or write the CSRs. The set of CSRs is defined by the current + target description. The user is responsible for ensuring that the + same target description is in use when reading the core file as was + in use when writing the core file. */ + const struct target_desc *tdesc = gdbarch_target_desc (gdbarch); + + /* Do not dump/load any CSRs if there is no target description or the target + description does not contain any CSRs. */ + if (tdesc != nullptr) + { + const struct tdesc_feature *feature_csr + = tdesc_find_feature (tdesc, riscv_feature_name_csr); + if (feature_csr != nullptr && feature_csr->registers.size () > 0) + { + riscv_update_csrmap (gdbarch, feature_csr); + cb (".reg-riscv-csr", + (feature_csr->registers.size() * riscv_isa_xlen (gdbarch)), + (feature_csr->registers.size() * riscv_isa_xlen (gdbarch)), + &riscv_csrset, NULL, cb_data); + } + } } /* Initialize RISC-V bare-metal ABI info. */ -- 2.25.4