From patchwork Sat May 4 00:05:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 794553 Delivered-To: patch@linaro.org Received: by 2002:adf:a153:0:b0:34d:5089:5a9e with SMTP id r19csp985704wrr; Fri, 3 May 2024 17:05:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXy/8HmcAdtP1WUWdacP9YC8pQmLBUHB+6Eno9s++9zyGvWRAGiIMWgOKGRBgREEACVkdOyXLk3U3aEB7wpJb+s X-Google-Smtp-Source: AGHT+IGs0djTL6NHGXCLjzQ0SqKlcv49fNubvzeMCa59mYZ4yxmxkC2WV0dNXtAHO8TUfv7iQGTA X-Received: by 2002:a81:ac5c:0:b0:617:c383:42ac with SMTP id z28-20020a81ac5c000000b00617c38342acmr3993332ywj.51.1714781155353; Fri, 03 May 2024 17:05:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714781155; cv=pass; d=google.com; s=arc-20160816; b=bMOTfgzQnEcyJEeDz1yur9iTr/WyTNwkFnfTZMDMZlrlpH2SjyctY3/CxWcsFZSi8k VSg5XEBbu0bU8rkiW7XpFyrgIBX5SBkIMWkMhZee1j01v8jIAxDYNxANM7gVhswqN1wS ZygiH35ZW40xrH223T5QzvptoXGov/dlieRtdd/rVuyGb6nbbU3FPSS1TaNypZzZ4y7C rLd5suQH+9KdrHmoIlzVD3/IVxkF3sguRj0rYX4DFRPbh3ogv1ltEcDoBG+e8LKr9ek2 3Q+sfHY0lj5j10P6CQjmHyFbMlUxMFr3ksaWIuekFvrfWhQuuXlsHqmyiFA4sPJlpViK 1xYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=NTEil/YFBBxjvbmv1jlJQCuCE5xsTpEO3w74pD4l13Y=; fh=72kqq0iQhigvR9Vv/oqX5ebs3Yyyw7XhzWxOOEPdupI=; b=Y/bZuqmPHHgFWBY85IohaV087b2Vju2MkfpGL/wqQU/xzBGfhhI5OLjbeEJJ8p+iDm CpjCoxj+HVR1U8RVYt0/soMUOwDh/B+lp0cOoy+6CmsTyjmKv7cQnHgGI9o7tJ2G0wZs SVzJYTTniSfK8Ir+vns4Z1sShovv34R8tvcyl6K/gFqyQNNif6XegyRoOFl3U3UUaeGc RTH1EbQVdLDhGdsxB5cVfpex39NGz3PN4CTsrLciEdM+wBswyXQnpCYWtctlTh8gthml ClRiqJ0Kwfi8NSCVFFvfq43KY6c0jXt1R0wS3siYzOnN9SyN3ojtVxrkLH4RnnL/tA7V DiNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MocRfM1B; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z7-20020a05622a028700b0043acc860004si4460538qtw.304.2024.05.03.17.05.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 17:05:55 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MocRfM1B; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F00323844044 for ; Sat, 4 May 2024 00:05:54 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 1624F3845BE1 for ; Sat, 4 May 2024 00:05:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1624F3845BE1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1624F3845BE1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714781132; cv=none; b=kgvj+eOmuXwcEyYPKAc4MrF2lxsrPW1Hg86tM6YVUE80WwUrLKQYmw1AFUoqFJrlEYSuVtlaxLPFxHl1aZWg0J8+UyTurnWl8ib26C16NZc88flBQWsKgI8Hhw3hvjpxA+H1xBZA35LCMTjkjGt7cN04telGU+tH+WF7XCaBbrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714781132; c=relaxed/simple; bh=oPUnkaWAJb/Z/PgqT4REWtPLXXoB3Nj48clRJ+r6XwU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z6LS9iX7L0ZryRP0eymgmOe00w1Gs0XIGph34HAF4T1768hzJVwlyroj8EesyJlbd/2+izCStU6E5FjpJnlw6STD2IBLi17Nrh/Sgl8QCbiG7dBUgGjBHoPFiBJWxnzXDAW4ujGkspuUCNDhrmbBKqdqyr117yg4zdqtz2wrik0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6f453d2c5a1so265586b3a.2 for ; Fri, 03 May 2024 17:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714781129; x=1715385929; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=NTEil/YFBBxjvbmv1jlJQCuCE5xsTpEO3w74pD4l13Y=; b=MocRfM1BDSPP2QN7dWDUqLAVCoziPlwKPoaaxrrnOC1+Ry//rAhmmara1FuYwgZh4c Z1PVkypOedCz2na/CC35Jtn7VmrT4PaZ5IRztNterovepx+Awm3S2PykCRYD1wSQzqLY 1peCu0em5+MFKprbrxZl0QdIqWb/HjG4utstS6Oevry0AhVO+AxnxxLd0v7DQ+nKgr31 IU/qdydXNB9JCTseuD+DYBR/KVnQKTQ/xkf/luF/pdlekb580K4H8IzHa8glbd6KkPsv dC+WCK8tNPf3Y2sbHpLYFXx+lTiG6NQlARYO+3O6AdItMZx6PoJECD7pPuzZ/PAxpJyw TsEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714781129; x=1715385929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NTEil/YFBBxjvbmv1jlJQCuCE5xsTpEO3w74pD4l13Y=; b=HYOkghnDdmx2/TGjaEYMUYGnIwDiSMw4mmwEiKb1mnhe/pf6t7qK6r0DaFa7QEuBUw A3ByL6aC+vRfrcVLYNEceyyvqK6F15l9f4NXpIhuzGshr3FqM+k0H9khXQVw4CE3NZfC fQatoZkU4RkSFwUhcjX6Hj1/2A9HrbcOCdT5hRuH9q3AJ87G5ErsFvxUM5b7pZ4+YXrR MkFfKuMhgS3VcTcH2J6d2eB8KRzqCEf+1YVtuJp/Os0J31Se9BHDmh4egGtWf0l3F+bn RBRU8VmEMFNihtGJUnRm5otURn9OCRbOFGIrFH+DLg6WmWc4aMt8hQwiTSU0QpVmssXJ +Idg== X-Gm-Message-State: AOJu0YwV8rnRg+KM+RCjlPYNNXXK1rk2rdB2AIZggzwM1N4QA/RQ0PQS w5ZZYVH+jWIysqrcGwyAHGkBT78yxLC/sI0Yub931mM1xx1Wdp0QXHJu9FnbnvQ+jkx7CsuME6B H X-Received: by 2002:a05:6a21:3a84:b0:1af:8468:5f79 with SMTP id zv4-20020a056a213a8400b001af84685f79mr4367763pzb.16.1714781128815; Fri, 03 May 2024 17:05:28 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:50e3:94e0:79b0:c9f9]) by smtp.gmail.com with ESMTPSA id 200-20020a6301d1000000b006109431806dsm3663899pgb.92.2024.05.03.17.05.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 17:05:28 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Subject: [PATCH 1/5] gdb/aarch64: Implement software single stepping for MOPS instructions Date: Fri, 3 May 2024 21:05:17 -0300 Message-ID: <20240504000521.314531-2-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240504000521.314531-1-thiago.bauermann@linaro.org> References: <20240504000521.314531-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patch=linaro.org@sourceware.org The AArch64 MOPS (Memory Operation) instructions provide a standardised instruction sequence to perform a memset, memcpy or memmove. A sequence is always composed of three instructions: a prologue instruction, a main instruction and an epilogue instruction. As an illustration, here are the implementations of these memory operations in glibc 2.39: (gdb) disassemble/r Dump of assembler code for function __memset_mops: => 0x0000fffff7e8d780 <+0>: d503201f nop 0x0000fffff7e8d784 <+4>: aa0003e3 mov x3, x0 0x0000fffff7e8d788 <+8>: 19c10443 setp [x3]!, x2!, x1 0x0000fffff7e8d78c <+12>: 19c14443 setm [x3]!, x2!, x1 0x0000fffff7e8d790 <+16>: 19c18443 sete [x3]!, x2!, x1 0x0000fffff7e8d794 <+20>: d65f03c0 ret End of assembler dump. (gdb) disassemble/r Dump of assembler code for function __memcpy_mops: => 0x0000fffff7e8c580 <+0>: d503201f nop 0x0000fffff7e8c584 <+4>: aa0003e3 mov x3, x0 0x0000fffff7e8c588 <+8>: 19010443 cpyfp [x3]!, [x1]!, x2! 0x0000fffff7e8c58c <+12>: 19410443 cpyfm [x3]!, [x1]!, x2! 0x0000fffff7e8c590 <+16>: 19810443 cpyfe [x3]!, [x1]!, x2! 0x0000fffff7e8c594 <+20>: d65f03c0 ret End of assembler dump. (gdb) disassemble/r Dump of assembler code for function __memmove_mops: => 0x0000fffff7e8d180 <+0>: d503201f nop 0x0000fffff7e8d184 <+4>: aa0003e3 mov x3, x0 0x0000fffff7e8d188 <+8>: 1d010443 cpyp [x3]!, [x1]!, x2! 0x0000fffff7e8d18c <+12>: 1d410443 cpym [x3]!, [x1]!, x2! 0x0000fffff7e8d190 <+16>: 1d810443 cpye [x3]!, [x1]!, x2! 0x0000fffff7e8d194 <+20>: d65f03c0 ret End of assembler dump. The Arm Architecture Reference Manual says that "the prologue, main, and epilogue instructions are expected to be run in succession and to appear consecutively in memory". Therefore GDB needs to treat them as an atomic instruction sequence, and also can't do displaced stepping with them. This patch implements support for executing the sequence atomically, and also disables displaced step on them. PR tdep/31666 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31666 --- gdb/aarch64-tdep.c | 107 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c index 8d0553f3d7cd..e920cea49066 100644 --- a/gdb/aarch64-tdep.c +++ b/gdb/aarch64-tdep.c @@ -3444,6 +3444,104 @@ value_of_aarch64_user_reg (const frame_info_ptr &frame, const void *baton) return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame)); } +/* Single step through MOPS instruction sequences on AArch64. */ + +static std::vector +aarch64_software_single_step_mops (struct regcache *regcache, CORE_ADDR loc, + uint32_t insn) +{ + const int insn_size = 4; + struct gdbarch *gdbarch = regcache->arch (); + enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch); + uint8_t o0 = bit (insn, 21); + uint8_t op1 = bits (insn, 22, 23); + uint8_t op2 = bits (insn, 12, 15); + + /* Look for the prologue instruction that begins the sequence. */ + + /* CPYFP* */ + if (!((o0 == 0 && op1 == 0) + /* SETP* */ + || (o0 == 0 && op1 == 3 && op2 < 4) + /* CPYP* */ + || (o0 == 1 && op1 == 0) + /* SETGP* */ + || (o0 == 1 && op1 == 3 && op2 < 4))) + /* Prologue instruction not found. */ + return {}; + + /* Now look for the main instruction in the middle of the sequence. */ + + loc += insn_size; + ULONGEST insn_from_memory; + if (!safe_read_memory_unsigned_integer (loc, insn_size, + byte_order_for_code, + &insn_from_memory)) + { + /* Assume we don't have a MOPS sequence, as we couldn't read the + instruction in this location. */ + return {}; + } + + insn = insn_from_memory; + aarch64_inst inst; + if (aarch64_decode_insn (insn, &inst, 1, nullptr) != 0) + return {}; + if (!AARCH64_CPU_HAS_FEATURE (*inst.opcode->avariant, MOPS)) + return {}; + + o0 = bit (insn, 21); + op1 = bits (insn, 22, 23); + op2 = bits (insn, 12, 15); + + /* CPYFM* */ + if (!((o0 == 0 && op1 == 1) + /* SETM* */ + || (o0 == 0 && op1 == 3 && op2 >= 4 && op2 < 8) + /* CPYM* */ + || (o0 == 1 && op1 == 1) + /* SETGM* */ + || (o0 == 1 && op1 == 3 && op2 >= 4 && op2 < 8))) + /* Main instruction not found. */ + return {}; + + /* Now look for the epilogue instruction that ends the sequence. */ + + loc += insn_size; + if (!safe_read_memory_unsigned_integer (loc, insn_size, + byte_order_for_code, + &insn_from_memory)) + { + /* Assume we don't have a MOPS sequence, as we couldn't read the + instruction in this location. */ + return {}; + } + + insn = insn_from_memory; + if (aarch64_decode_insn (insn, &inst, 1, nullptr) != 0) + return {}; + if (!AARCH64_CPU_HAS_FEATURE (*inst.opcode->avariant, MOPS)) + return {}; + + o0 = bit (insn, 21); + op1 = bits (insn, 22, 23); + op2 = bits (insn, 12, 15); + + /* CPYFE* */ + if (!((o0 == 0 && op1 == 2) + /* SETE* (op2 >= 12 is unallocated space) */ + || (o0 == 0 && op1 == 3 && op2 >= 8 && op2 < 12) + /* CPYE* */ + || (o0 == 1 && op1 == 2) + /* SETGE* (op2 >= 12 is unallocated space) */ + || (o0 == 1 && op1 == 3 && op2 >= 8 && op2 < 12))) + /* Epilogue instruction not found. */ + return {}; + + /* Insert breakpoint after the end of the atomic sequence. */ + return { loc + insn_size }; +} + /* Implement the "software_single_step" gdbarch method, needed to single step through atomic sequences on AArch64. */ @@ -3479,6 +3577,9 @@ aarch64_software_single_step (struct regcache *regcache) if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0) return {}; + if (AARCH64_CPU_HAS_FEATURE (*inst.opcode->avariant, MOPS)) + return aarch64_software_single_step_mops (regcache, loc, insn); + /* Look for a Load Exclusive instruction which begins the sequence. */ if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0) return {}; @@ -3808,8 +3909,10 @@ aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch, if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0) return NULL; - /* Look for a Load Exclusive instruction which begins the sequence. */ - if (inst.opcode->iclass == ldstexcl && bit (insn, 22)) + /* Look for a Load Exclusive instruction which begins the sequence, + or for a MOPS instruction. */ + if ((inst.opcode->iclass == ldstexcl && bit (insn, 22)) + || AARCH64_CPU_HAS_FEATURE (*inst.opcode->avariant, MOPS)) { /* We can't displaced step atomic sequences. */ return NULL;