From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 3CEE3389042B for ; Tue, 16 Jun 2020 17:15:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3CEE3389042B 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-wm1-x32f.google.com with SMTP id f185so3888449wmf.3 for ; Tue, 16 Jun 2020 10:15:02 -0700 (PDT) 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=0t4tKPGnDws+c066T8wqsQkTQVB/siA2YrZbXxsZHLk=; b=H6cKVr1pL14vOJ23yaGHg5JSMyOzp+Y3HIN/vmxe2oqLkatJfo6IUgCiiGSntI34p/ Xm8PjGU+bc/THpdnJjlYkRbQMj0EuE+up1wRXkZqHFi07G+dzzsTOTa0FBAe0UdF4ELc c+cTlzyVxDgPHuKcOGtKDOhWCh+/aMbSrxSxmjSzWUdrYz8diI1Bc3GBoXLJm4ET1H+8 2SJy4ScB25/mkKGCs9RObajQVEpwPXNQ1jXP2COGCv3LGkwZC8k8c/VsAQe8D6hfn2E1 QdgZdCQWnD96l0g/DqLJGTrfvap2Gqt2WaFzdn2hCQBOtMQ80ZmdJvdBTbgzReF6J5kz W5Og== 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=0t4tKPGnDws+c066T8wqsQkTQVB/siA2YrZbXxsZHLk=; b=D2xzasnsg5INohMRFQbZFEZ1nTqhroGqkDNfXUOSgkWPv763MXGgLHJYIIDYkamJLh 90UGC4gZj3vl7FYhJlrJhsYEaB9hnoKART2jeDf7CiURATES3Wc/Ueoz2T2b2jaSomGa 5xTh0CNvXq5ZOhqp7gYlYw+AZhcqHKjJ+TSV19JaJqTR0ckGQf8x2eIA+j/jDaHCxbfM 7CGYqRDzEddhNZS2200nMLUQtdbSHYZ1F+b25ITMV4uM45YePs0EE2Ondr2/DXpaSD1Z KU70E10zrxtIqrVIff0dz80f8WPsmXk/pFtSRKSnO/CmBl0hdeZ9x87ZtVwV+OKnIVtR eGsg== X-Gm-Message-State: AOAM532N3alscYXri7Hmz65eT+FL3e9Q3EG2zDoESJQ9Ms7K+cU7/tY+ bnmrOp/P+euN7HbeaHiB89jMSO8+pJw= X-Google-Smtp-Source: ABdhPJyg4XYkXhBQzoUqzKvr/KvsuXSrAaUR/hMcEe4+WKaooy2jCQ5jeao+NDKex0sAotF9971Viw== X-Received: by 2002:a1c:5411:: with SMTP id i17mr4428482wmb.137.1592327700892; Tue, 16 Jun 2020 10:15:00 -0700 (PDT) Received: from localhost (host86-128-12-16.range86-128.btcentralplus.com. [86.128.12.16]) by smtp.gmail.com with ESMTPSA id 89sm3103711wrg.56.2020.06.16.10.15.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 10:15:00 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Nelson Chu , Jim Wilson , Tom Tromey , palmer@dabbelt.com, Andrew Burgess Subject: [PATCH 6/8] gdb: Extend target description processing of unknown registers Date: Tue, 16 Jun 2020 18:14:45 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: , X-List-Received-Date: Tue, 16 Jun 2020 17:15:04 -0000 This commit adds a new step to the processing of a target description done in tdesc_use_registers, this new step is about how unknown registers are processed. Currently an architecture looks through the target description and calls tdesc_numbered_register for each register is was expecting (or hoping) to find. This builds up a map from GDB's register numbers to the tdesc_reg object. Later the architecture calls tdesc_use_registers. In tdesc_use_registers we build a hash with keys being all the tdesc_reg object pointers, from this hash we remove all of the tdesc_reg objects that were assigned register numbers using tdesc_numbered_register. Finally we walk through all of the tdesc_reg objects, and if it was not already assigned a number we assign that register the next available number. The problem with this is that the architecture has no visibility of which unknown registers exist, and which tdesc_feature the register came from, in some cases this might be important. For example, on RISC-V GDB overrides the use of tdesc_register_reggroup_p, with riscv_register_reggroup_p to modify some of the register group choices. In this function GDB wants to treat all registers from a particular feature in a certain way. This is fine for registers that GDB knows might be in that feature, but for unknown registers the RISC-V parts of GDB have no easy way to figure out which unknown registers exist, and what numbers they were assigned. We could figure this information out by probing the register structures after calling tdesc_use_registers, but this would be horrible, much better to have tdesc_use_registers tell the architecture about unknown registers. This is what this commit does. A new phase of tdesc_use_registers, just before the unknown registers are assigned a number, we loop over each tdesc_reg object, if it has not been assigned a number then we figure out what number would be assigned and then call back into the architecture passing the tdesc_feature, register name, and the proposed register number. The architecture is free to return the proposed register number, or it can return a different number (which has a result identical to having called tdesc_numbered_register). Alternatively the architecture can return -1 to indicate the register should be numbered later. After calling the callback for every tdesc_reg object any registers still don't have a number assigned (because the architecture returned -1), then a new register number is assigned, which might be different from the proposed number that was suggested earlier. This commit adds the general target-description parts of this mechanism. No targets are currently using this code. The RISC-V target will make use of this in the next commit. There should be no user visible changes after this commit. gdb/ChangeLog: * target-descriptions.c (tdesc_use_registers): Add new parameter a callback, use the callback (when not null) to help number unknown registers. * target-descriptions.h (tdesc_unknown_register_ftype): New typedef. (tdesc_use_registers): Add extra parameter to declaration. --- gdb/ChangeLog | 8 ++++++++ gdb/target-descriptions.c | 31 ++++++++++++++++++++++++++++++- gdb/target-descriptions.h | 27 ++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c index 20a3a640f4f..9a661c8487b 100644 --- a/gdb/target-descriptions.c +++ b/gdb/target-descriptions.c @@ -1056,7 +1056,8 @@ set_tdesc_pseudo_register_reggroup_p void tdesc_use_registers (struct gdbarch *gdbarch, const struct target_desc *target_desc, - struct tdesc_arch_data *early_data) + struct tdesc_arch_data *early_data, + tdesc_unknown_register_ftype unk_reg_cb) { int num_regs = gdbarch_num_regs (gdbarch); struct tdesc_arch_data *data; @@ -1105,6 +1106,34 @@ tdesc_use_registers (struct gdbarch *gdbarch, while (data->arch_regs.size () < num_regs) data->arch_regs.emplace_back (nullptr, nullptr); + /* First we give the target a chance to number previously unknown + registers. This allows targets to record the numbers assigned based + on which feature the register was from. */ + if (unk_reg_cb != NULL) + { + for (const tdesc_feature_up &feature : target_desc->features) + for (const tdesc_reg_up ® : feature->registers) + if (htab_find (reg_hash, reg.get ()) != NULL) + { + int regno = unk_reg_cb (gdbarch, feature.get (), + reg->name.c_str (), num_regs); + gdb_assert (regno == -1 || regno >= num_regs); + if (regno != -1) + { + while (regno >= data->arch_regs.size ()) + data->arch_regs.emplace_back (nullptr, nullptr); + data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL); + num_regs = regno + 1; + htab_remove_elt (reg_hash, reg.get ()); + } + } + } + + /* Ensure the array was sized correctly above. */ + gdb_assert (data->arch_regs.size () == num_regs); + + /* Now in a final pass we assign register numbers to any remaining + unnumbered registers. */ for (const tdesc_feature_up &feature : target_desc->features) for (const tdesc_reg_up ® : feature->registers) if (htab_find (reg_hash, reg.get ()) != NULL) diff --git a/gdb/target-descriptions.h b/gdb/target-descriptions.h index 96d283fb379..6d842bf07ed 100644 --- a/gdb/target-descriptions.h +++ b/gdb/target-descriptions.h @@ -80,6 +80,30 @@ void set_tdesc_pseudo_register_reggroup_p (struct gdbarch *gdbarch, gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p); +/* Pointer to a function that should be called for each unknown register in + a target description, used by TDESC_USE_REGISTERS. + + GDBARCH is the architecture the target description is for, FEATURE is + the feature the unknown register is in, and REG_NAME is the name of the + register from the target description. The POSSIBLE_REGNUM is a proposed + (GDB internal) number for this register. + + The callback function can return, (-1) to indicate that the register + should not be assigned POSSIBLE_REGNUM now (though it might be later), + GDB will number the register automatically later on. Return + POSSIBLE_REGNUM (or greater) to have this register assigned that number. + Returning a value less that POSSIBLE_REGNUM is also acceptable, but take + care not to clash with a register number that has already been + assigned. + + The callback will always be called on the registers in the order they + appear in the target description. This means all unknown registers + within a single feature will be called one after another. */ + +typedef int (*tdesc_unknown_register_ftype) + (struct gdbarch *gdbarch, tdesc_feature *feature, + const char *reg_name, int possible_regnum); + /* Update GDBARCH to use the TARGET_DESC for registers. TARGET_DESC may be GDBARCH's target description or (if GDBARCH does not have one which describes registers) another target description @@ -95,7 +119,8 @@ void set_tdesc_pseudo_register_reggroup_p void tdesc_use_registers (struct gdbarch *gdbarch, const struct target_desc *target_desc, - struct tdesc_arch_data *early_data); + struct tdesc_arch_data *early_data, + tdesc_unknown_register_ftype unk_reg_cb = NULL); /* Allocate initial data for validation of a target description during gdbarch initialization. */ -- 2.25.4