From patchwork Tue Aug 1 13:20:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aktemur, Tankut Baris" X-Patchwork-Id: 73426 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 665EA385842D for ; Tue, 1 Aug 2023 13:21:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 665EA385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1690896092; bh=UMjuraoSKF7lQd0dWEjPNW+fki0MOBxbrDpCtDxbfQg=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ijWn9fWZmoLdM0ZmOghulsY/zYOgxTW8L1BXm4NFO8UpuYN0O2hassAKjBAHoYrkf EOIgDIkTeUd9ughFx5l3ComFOmKE7UKkKDaXGFKn2ieBJKXJAqo2YTKrSCkNWmnx62 zvXlcv17QgAbhcMQrW/5UjBZ+p3K6WSbVoY+2bQ4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mgamail.intel.com (unknown [192.55.52.115]) by sourceware.org (Postfix) with ESMTPS id AFA1A3858D28 for ; Tue, 1 Aug 2023 13:21:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFA1A3858D28 X-IronPort-AV: E=McAfee;i="6600,9927,10789"; a="369289051" X-IronPort-AV: E=Sophos;i="6.01,247,1684825200"; d="scan'208";a="369289051" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 06:21:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10789"; a="705831664" X-IronPort-AV: E=Sophos;i="6.01,247,1684825200"; d="scan'208";a="705831664" Received: from gkldtt-dev-004.igk.intel.com (HELO localhost) ([10.123.221.202]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 06:21:00 -0700 To: gdb-patches@sourceware.org Cc: pedro@palves.net Subject: [PATCH v2] gdbserver: select a thread, if necessary, to access memory (was: [PATCH] gdbserver: try selecting a thread first to access memory) Date: Tue, 1 Aug 2023 15:20:46 +0200 Message-Id: <20230801132046.3465441-1-tankut.baris.aktemur@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <24bc1dc1-b3fa-29e0-fc93-c202d9580a9f@palves.net> References: <24bc1dc1-b3fa-29e0-fc93-c202d9580a9f@palves.net> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Tankut Baris Aktemur via Gdb-patches From: "Aktemur, Tankut Baris" Reply-To: Tankut Baris Aktemur Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Since commit commit 7f8acedeebe295fc8cc1d11ed971cbfc1942618c Author: Pedro Alves Date: Tue Apr 5 13:57:11 2022 +0100 gdbserver: track current process as well as current thread gdbserver switches to a process, rather than a thread, before processing a memory access request coming from the GDB side. This switch sets current_thread to null. Some memory accesses on certain targets, however, may require having a thread context. Therefore, switch to the selected thread, if the target would require a thread context. --- gdbserver/server.cc | 16 ++++++++++++++-- gdbserver/target.cc | 6 ++++++ gdbserver/target.h | 7 +++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gdbserver/server.cc b/gdbserver/server.cc index c57270175b4..44642a64215 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -1072,7 +1072,13 @@ gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) /* (assume no half-trace half-real blocks for now) */ } - if (set_desired_process ()) + /* Some memory accesses may require having a thread context. + Attempt switching to the selected thread, if necessary. + Otherwise use the process as the context. */ + bool need_thread = the_target->memaddr_is_thread_specific (memaddr); + + if ((need_thread && set_desired_thread ()) + || (!need_thread && set_desired_process ())) res = read_inferior_memory (memaddr, myaddr, len); else res = 1; @@ -1093,7 +1099,13 @@ gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) { int ret; - if (set_desired_process ()) + /* Some memory accesses may require having a thread context. + Attempt switching to the selected thread, if necessary. + Otherwise use the process as the context. */ + bool need_thread = the_target->memaddr_is_thread_specific (memaddr); + + if ((need_thread && set_desired_thread ()) + || (!need_thread && set_desired_process ())) ret = target_write_memory (memaddr, myaddr, len); else ret = EIO; diff --git a/gdbserver/target.cc b/gdbserver/target.cc index f8e592d20c3..85efe6d88c6 100644 --- a/gdbserver/target.cc +++ b/gdbserver/target.cc @@ -832,3 +832,9 @@ process_stratum_target::get_ipa_tdesc_idx () { return 0; } + +bool +process_stratum_target::memaddr_is_thread_specific (CORE_ADDR memaddr) +{ + return false; +} diff --git a/gdbserver/target.h b/gdbserver/target.h index d993e361b76..bde2fb2eb4c 100644 --- a/gdbserver/target.h +++ b/gdbserver/target.h @@ -508,6 +508,13 @@ class process_stratum_target Returns true if successful and false otherwise. */ virtual bool store_memtags (CORE_ADDR address, size_t len, const gdb::byte_vector &tags, int type); + + /* Some memory addresses may require having a thread context. E.g.: + an address whose upper bits encode a thread-specific address + space. Let the target tell if MEMADDR is such an address, so + that the server can attempt switching the context before + accessing the memory. */ + virtual bool memaddr_is_thread_specific (CORE_ADDR memaddr); }; extern process_stratum_target *the_target;