From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4BA5F3858D1E for ; Tue, 7 May 2024 14:50:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4BA5F3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4BA5F3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715093444; cv=none; b=tqaVdAqfyniD/RxUD52wtjAKBDP3bMzPx1m1klYhC9JadplhogNN+eY4Y8UbCdBoPyirIkuJPa7EFAXb8M0ZuyZ8qS3llzhiVhb29VJlANFDjU+JHRAFCLYJEJoCFpCJsnNf981E/xsPhmbHsXocckO+S7UH0rClgyGxWUdcgtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715093444; c=relaxed/simple; bh=qC3dH5fL4h/xmGo930ZAEJ5KtXhwAi7UP/UWZ0e1nmU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Cu0WHKk2fObKV83AD03L6gZ+93jsgKsJPn1NtugNcABemTQws3ontZWD1IbweHpMQRam/439YKKnh0DdwwDqPWCa+AtAIPCIQGyMcvQ/hGRXOFoWmLS9sr8kH+UbeMsNVOTHU/aLfFZnw0vuV2HI/yMIddhUK3gf0d8ZiVSGMGU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715093441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=oFLRn9z3V+VjjCSzgWRfOxCLiWh0FLrIOCbQ/vw+W+4=; b=ZzOR3IhScCQiKFCm3Gx6RWltJeLf5Hug8pvEZhHpMrrhMp2YbCoPzAwtVPGyEImchiNEKD 9TnhTsDf+5gCECMIHgmMGhjHe3THIkJwxHy7nJFG8/+LdyRZgZCIOq8K9pM+sr0Pt/sb70 V5Q22KD//zNe0OJAq9D8Z/AG8CvAzPs= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-Z3W_lh7TPIuYXenv5METSw-1; Tue, 07 May 2024 10:50:40 -0400 X-MC-Unique: Z3W_lh7TPIuYXenv5METSw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-34f1b148725so1046506f8f.2 for ; Tue, 07 May 2024 07:50:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715093439; x=1715698239; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oFLRn9z3V+VjjCSzgWRfOxCLiWh0FLrIOCbQ/vw+W+4=; b=JnfG4q7tJpSBhj+u9MYYtarmBpXzVJ60YBO7gSNBEFMWHdab4ZJgcClgOo6AZMxPoR f8FSALzkKu4ueaxlza21xRNyzvsQpnS5TaybKImCFwvRDR9BkVmVQvjhANQ/fqkqUpXd tpLuAJErmhqCAb5NQVmVYfImCVogcTDA/hTWUWWoe83gb6kWbsnvhWa+IZyuknIkaf+9 nMsjznX1pfdj0CphYnvAVGfAgVuGbQdLsPc/VeLkm4X/T4HPqjMymtC/9ZjG+fsd+XND oz3utpwFAU4SH2kj/vDD5Bso4kmmbhyp0cH6IocsXiRpZ1aSoV1e0cHxCwklNhidQ8xj oAZg== X-Forwarded-Encrypted: i=1; AJvYcCVBVGzlzEkDvZKxqtuXHTBXA7SQqZ7Ca2Tw2ekBk17Kq8oECapwO+IDdNY8rY7aLZ3mEoXYZ35M2GEt0TU27Dj1TWIXKVX84Qmv4A== X-Gm-Message-State: AOJu0YxGjpZEPzwi4Fc+n7U6bt/3rTivjj0VXB/dmEF7rU0lTDL9lO1l Ws3l199phMvZGZjaqhUf+5hbVVUJonv2iQZuZkva8mh5nrp6CvhYOVUt85cbyg+1iVhwvujPbEl njdN+2ubV5GqsY13/8EDqCc9dLzoSQnGvozjWjL86hOWSyr94Xnzuh0Q29RTNCzROl8E= X-Received: by 2002:adf:e506:0:b0:34d:7fbb:e93b with SMTP id ffacd0b85a97d-34fca054a08mr52014f8f.14.1715093438696; Tue, 07 May 2024 07:50:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEEdIrTT17nG/L4Xp+5OZ+A6cTfhJZ5mAkG2HYoL3Tg2+Ig0nVE9PYPMwhM2hlvt7fmbxZsxA== X-Received: by 2002:adf:e506:0:b0:34d:7fbb:e93b with SMTP id ffacd0b85a97d-34fca054a08mr51993f8f.14.1715093438097; Tue, 07 May 2024 07:50:38 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id o10-20020a5d684a000000b0034a7a95c8cfsm13186167wrw.9.2024.05.07.07.50.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:50:37 -0700 (PDT) From: Andrew Burgess To: "Willgerodt, Felix" , "gdb-patches@sourceware.org" Cc: John Baldwin Subject: RE: [PATCHv5 11/11] gdb/gdbserver: share x86/linux tdesc caching In-Reply-To: References: Date: Tue, 07 May 2024 15:50:36 +0100 Message-ID: <87pltxu14z.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: "Willgerodt, Felix" writes: >> -----Original Message----- >> From: Andrew Burgess >> Sent: Freitag, 26. April 2024 17:02 >> To: gdb-patches@sourceware.org >> Cc: Andrew Burgess ; Willgerodt, Felix >> ; John Baldwin >> Subject: [PATCHv5 11/11] gdb/gdbserver: share x86/linux tdesc caching >> >> This commit builds on the previous series of commits to share the >> target description caching code between GDB and gdbserver for >> x86/Linux targets. >> >> The objective of this commit is to move the four functions (2 each of) >> i386_linux_read_description and amd64_linux_read_description into the >> gdb/arch/ directory and combine them so we have just a single copy of >> each. Then GDB, gdbserver, and the in-process-agent (IPA) will link >> against these shared functions. >> >> It is worth reading the description of the previous commit(s) to see >> why this merging is not as simple as it seems, but in summary, >> gdbserver used to generate a different set of possible target >> descriptions than GDB. The previous commit(s) fixed this, so now it >> should be simpler to share the functions. >> >> One curiosity with this patch is the function >> x86_linux_post_init_tdesc. On the gdbserver side the two functions >> amd64_linux_read_description and i386_linux_read_description have some >> functionality that is not present on the GDB side, that is some >> additional configuration that is performed as each target description >> is created to setup the expedited registers. >> >> To support this I've added the function x86_linux_post_init_tdesc. >> This function is called from the two *_linux_read_description >> functions, but is implemented separately for GDB and gdbserver. >> >> An alternative approach that avoids adding x86_linux_post_init_tdesc >> would be to have x86_linux_tdesc_for_tid return a non-const target >> description, in x86_target::low_arch_setup we could then inspect the >> target description to figure out if it is 64-bit or not, and modify >> the target description as needed. In the end I figured that adding >> the x86_linux_post_init_tdesc function was good enough, so went with >> that solution. >> >> The contents of gdbserver/linux-x86-low.cc have moved to >> gdb/arch/x86-linux-tdesc-features.c, and gdbserver/linux-x86-tdesc.h >> has moved to gdb/arch/x86-linux-tdesc-features.h, this change leads to >> some updates in the #includes in the gdbserver/ directory. >> >> For testing I've done the following: >> >> - Built on x86-64 GNU/Linux for all targets, and just for the native >> target, >> >> - Build on i386 GNU/Linux for all targets, and just for the native >> target, >> >> - Build on a 64-bit, non-x86 GNU/Linux for all targets, just for the >> native target, and for targets x86_64-*-linux and i386-*-linux. >> >> This did flush out a bunch of build issues where I'd failed to add a >> required file in a configure.* file, but I think everything should now >> be good. >> --- >> gdb/Makefile.in | 5 + >> gdb/amd64-linux-tdep.c | 31 -- >> gdb/arch/amd64-linux-tdesc.c | 61 ++++ >> gdb/arch/i386-linux-tdesc.c | 51 +++ >> gdb/arch/x86-linux-tdesc-features.c | 247 +++++++++++++++ >> .../arch/x86-linux-tdesc-features.h | 50 +-- >> gdb/arch/x86-linux-tdesc.h | 37 +++ >> gdb/configure.nat | 6 +- >> gdb/configure.tgt | 11 +- >> gdb/i386-linux-tdep.c | 26 +- >> gdbserver/configure.srv | 9 + >> gdbserver/linux-amd64-ipa.cc | 2 +- >> gdbserver/linux-i386-ipa.cc | 2 +- >> gdbserver/linux-x86-low.cc | 2 +- >> gdbserver/linux-x86-tdesc.cc | 292 +----------------- >> 15 files changed, 469 insertions(+), 363 deletions(-) >> create mode 100644 gdb/arch/amd64-linux-tdesc.c >> create mode 100644 gdb/arch/i386-linux-tdesc.c >> create mode 100644 gdb/arch/x86-linux-tdesc-features.c >> rename gdbserver/linux-x86-tdesc.h => gdb/arch/x86-linux-tdesc-features.h >> (52%) >> create mode 100644 gdb/arch/x86-linux-tdesc.h >> >> diff --git a/gdb/Makefile.in b/gdb/Makefile.in >> index a24b2232daa..1e49ae396f4 100644 >> --- a/gdb/Makefile.in >> +++ b/gdb/Makefile.in >> @@ -748,6 +748,7 @@ ALL_64_TARGET_OBS = \ >> arch/aarch64-insn.o \ >> arch/aarch64-mte-linux.o \ >> arch/aarch64-scalable-linux.o \ >> + arch/amd64-linux-tdesc.o \ >> arch/amd64.o \ >> arch/riscv.o \ >> bpf-tdep.o \ >> @@ -788,9 +789,11 @@ ALL_TARGET_OBS = \ >> arch/arm.o \ >> arch/arm-get-next-pcs.o \ >> arch/arm-linux.o \ >> + arch/i386-linux-tdesc.o \ >> arch/i386.o \ >> arch/loongarch.o \ >> arch/ppc-linux-common.o \ >> + arch/x86-linux-tdesc-features.o \ >> arm-bsd-tdep.o \ >> arm-fbsd-tdep.o \ >> arm-linux-tdep.o \ >> @@ -1558,6 +1561,8 @@ HFILES_NO_SRCDIR = \ >> arch/ppc-linux-common.h \ >> arch/ppc-linux-tdesc.h \ >> arch/riscv.h \ >> + arch/x86-linux-tdesc-features.h \ >> + arch/x86-linux-tdesc.h \ >> cli/cli-cmds.h \ >> cli/cli-decode.h \ >> cli/cli-script.h \ >> diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c >> index bcb9868e79e..c707745cd9a 100644 >> --- a/gdb/amd64-linux-tdep.c >> +++ b/gdb/amd64-linux-tdep.c >> @@ -1577,37 +1577,6 @@ amd64_linux_record_signal (struct gdbarch *gdbarch, >> return 0; >> } >> >> -const target_desc * >> -amd64_linux_read_description (uint64_t xcr0_features_bit, bool is_x32) >> -{ >> - static target_desc *amd64_linux_tdescs \ >> - [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {}; >> - static target_desc *x32_linux_tdescs \ >> - [2/*AVX*/][2/*AVX512*/][2/*PKRU*/] = {}; >> - >> - target_desc **tdesc; >> - >> - if (is_x32) >> - { >> - tdesc = &x32_linux_tdescs[(xcr0_features_bit & X86_XSTATE_AVX) ? 1 : 0 ] >> - [(xcr0_features_bit & X86_XSTATE_AVX512) ? 1 : 0] >> - [(xcr0_features_bit & X86_XSTATE_PKRU) ? 1 : 0]; >> - } >> - else >> - { >> - tdesc = &amd64_linux_tdescs[(xcr0_features_bit & X86_XSTATE_AVX) ? 1 : 0] >> - [(xcr0_features_bit & X86_XSTATE_MPX) ? 1 : 0] >> - [(xcr0_features_bit & X86_XSTATE_AVX512) ? 1 : 0] >> - [(xcr0_features_bit & X86_XSTATE_PKRU) ? 1 : 0]; >> - } >> - >> - if (*tdesc == NULL) >> - *tdesc = amd64_create_target_description (xcr0_features_bit, is_x32, >> - true, true); >> - >> - return *tdesc; >> -} >> - >> /* Get Linux/x86 target description from core dump. */ >> >> static const struct target_desc * >> diff --git a/gdb/arch/amd64-linux-tdesc.c b/gdb/arch/amd64-linux-tdesc.c >> new file mode 100644 >> index 00000000000..63b5ddfcece >> --- /dev/null >> +++ b/gdb/arch/amd64-linux-tdesc.c >> @@ -0,0 +1,61 @@ >> +/* Target description related code for GNU/Linux x86-64. >> + >> + Copyright (C) 2024 Free Software Foundation, Inc. >> + >> + This file is part of GDB. >> + >> + This program is free software; you can redistribute it and/or modify >> + it under the terms of the GNU General Public License as published by >> + the Free Software Foundation; either version 3 of the License, or >> + (at your option) any later version. >> + >> + This program is distributed in the hope that it will be useful, >> + but WITHOUT ANY WARRANTY; without even the implied warranty of >> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + GNU General Public License for more details. >> + >> + You should have received a copy of the GNU General Public License >> + along with this program. If not, see . */ >> + >> +#include "arch/x86-linux-tdesc.h" >> +#include "arch/amd64-linux-tdesc.h" >> +#include "arch/amd64.h" >> +#include "arch/x86-linux-tdesc-features.h" >> + >> + >> +/* See arch/amd64-linux-tdesc.h. */ >> + >> +const struct target_desc * >> +amd64_linux_read_description (uint64_t xcr0, bool is_x32) >> +{ >> + /* The type used for the amd64 and x32 target description caches. */ >> + using tdesc_cache_type = std::unordered_map> target_desc_up>; >> + >> + /* Caches for the previously seen amd64 and x32 target descriptions, >> + indexed by the xcr0 value that created the target description. These >> + need to be static within this function to ensure they are initialised >> + before first use. */ >> + static tdesc_cache_type amd64_tdesc_cache, x32_tdesc_cache; >> + > > Sorry for this long comment, it isn't strictly related to your series, as you don't > change the status quo. But since you are changing the cache, I find it relevant. > > I wonder if caching of target descriptions based on xcr0 alone is really > good enough. It isn't always the only thing used for determining registers. > I realize you don't change that fact, though with the map we would need to > hash xcr0 with the other factors for the key. Arguably that could still be > viewed as better than the current way. > > Right now there is already the segments and "orig_rax" register for amd64. > Though I don't know if IPA really supports them and I am not super familiar with > their details. It seems like we just add them unconditionally or based on whether > or not we are on Linux. I am not really sure that means we don't care about them > for our cache (especially the Linux part), but it seems that is the current state. > > But in the near future there will be the shadow stack pointer register for CET, > which we can only read with a separate ptrace call. > See this patch, which we want to post soon: > https://github.com/intel/gdb/commit/afff428f38a1f03ac0127797deda524d81ec156e > > I must admit I never really fully understood why this caching is done. > Is it for a single GDB session connecting to different gdbserver sessions one > after the other? Or for inferior re-starts? > Or for runtime target description updates similar to SVE on aarch64? > (Which we will also add at some point for AMX on x86.) > Or a mix of all? You are 100% correct. And I agree with everything you've said. I also spotted this issue and had a choice to make. I could: (a) fix the caching mechanism first, which would require duplicating the effort in GDB and gdbserver, then (b) merge the GDB and gdbserver code. Or I could: (a) merge the GDB and gdbserver code, then (b) fix the caching mechanism. As you can see, I opted to merge the code first, but this was already 11 patches. Which is going to be at least 12 in v6. Realistically, if I tried to "fix" caching at the same time then either each patch is going to get much bigger (and break the one change per patch rule) or the series is going to start getting really long. I've stopped where I have to try and keep the number of patches down; I don't want to overload reviewers. Plus, if I write too much on top of earlier patches and get asked to rewrite things the rewrites become more work. My plan after this series is merged was to unify all of the x86 tdesc caching, not just for Linux, but for all x86 targets (e.g. FreeBSD). I was going to adopt an approach similar to the approach I used for risc-v, which I think works pretty well, that is have a single struct that can holds all the features that impact tdesc creation, give that struct a hash() function, and then use this as the key for caching created tdesc. I'll take a look and see if I can do something part way towards this solution as part of this patch which would address your immediate concerns. Thanks, Andrew