From: Luis Machado <luis.machado@linaro.org>
To: gdb-patches@sourceware.org, Alan.Hayward@arm.com
Cc: omair.javaid@linaro.org, catalin.marinas@arm.com,
david.spickett@linaro.org, jose.marchesi@oracle.com
Subject: [PATCH 14/23] AArch64: Implement the memory tagging gdbarch hooks
Date: Wed, 15 Jul 2020 16:45:04 -0300 [thread overview]
Message-ID: <20200715194513.16641-15-luis.machado@linaro.org> (raw)
In-Reply-To: <20200715194513.16641-1-luis.machado@linaro.org>
This patch implements the memory tagging gdbarch hooks for AArch64, for
the MTE feature.
gdb/ChangeLog:
YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
* aarch64-linux-tdep.c: Include target.h, arch-utils.h, value.h.
(aarch64_linux_get_atag, aarch64_linux_tagged_address_p)
(aarch64_linux_memtag_mismatch_p, aarch64_linux_set_memtags)
(aarch64_linux_get_memtag, aarch64_linux_memtag_to_string): New
functions.
(aarch64_linux_init_abi): Initialize MTE-related gdbarch hooks.
* arch/aarch64-mte-linux.c (make_ltag_bits, make_ltag)
(aarch64_linux_set_ltag, aarch64_linux_get_ltag): New functions.
* arch/aarch64-mte-linux.h (MTE_LOGICAL_TAG_START_BIT)
(MTE_LOGICAL_MAX_VALUE): Define.
(make_ltag_bits, make_ltag, aarch64_linux_set_ltag)
(aarch64_linux_get_ltag): New prototype.
---
gdb/aarch64-linux-tdep.c | 212 +++++++++++++++++++++++++++++++++++
gdb/arch/aarch64-mte-linux.c | 36 ++++++
gdb/arch/aarch64-mte-linux.h | 19 ++++
3 files changed, 267 insertions(+)
diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index 53f9d9f6d2..abcd325951 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -30,6 +30,7 @@
#include "symtab.h"
#include "tramp-frame.h"
#include "trad-frame.h"
+#include "target.h"
#include "target/target.h"
#include "regcache.h"
@@ -46,6 +47,9 @@
#include "arch/aarch64-mte-linux.h"
+#include "arch-utils.h"
+#include "value.h"
+
/* Signal frame handling.
+------------+ ^
@@ -1437,6 +1441,189 @@ aarch64_linux_gcc_target_options (struct gdbarch *gdbarch)
return {};
}
+/* Helper to get the allocation tag from a 64-bit ADDRESS.
+
+ Return 0 for success and non-zero otherwise. */
+
+static int
+aarch64_linux_get_atag (CORE_ADDR address, CORE_ADDR *tag)
+{
+ gdb::byte_vector tags;
+
+ /* Attempt to fetch the allocation tag. */
+ if (target_fetch_memtags (address, 0, tags) != 0)
+ return 1;
+
+ /* Only one tag should've been returned. Make sure we got exactly that. */
+ gdb_assert (tags.size () == 1);
+
+ /* Although our tags are 4 bits in size, they are stored in a
+ byte. */
+ *tag = tags[0];
+
+ return 0;
+}
+
+/* Implement the tagged_address_p gdbarch method. */
+
+static bool
+aarch64_linux_tagged_address_p (struct gdbarch *gdbarch, struct value *address)
+{
+ gdb_assert (address != nullptr);
+
+ CORE_ADDR addr = value_as_address (address);
+
+ /* Remove the top byte for the memory range check. */
+ addr = address_significant (gdbarch, addr);
+
+ /* Check if the page that contains ADDRESS is mapped with PROT_MTE. */
+ if (!linux_address_in_memtag_page (addr))
+ return false;
+
+ /* We have a valid tag in the top byte of the 64-bit address. */
+ return true;
+}
+
+/* Implement the memtag_mismatch_p gdbarch method. */
+
+static bool
+aarch64_linux_memtag_mismatch_p (struct gdbarch *gdbarch,
+ struct value *address)
+{
+ gdb_assert (address != nullptr);
+
+ /* Make sure we are dealing with a tagged address to begin with. */
+ if (!aarch64_linux_tagged_address_p (gdbarch, address))
+ return false;
+
+ CORE_ADDR addr = value_as_address (address);
+
+ /* Fetch the allocation tag for ADDRESS. */
+ CORE_ADDR atag = 0;
+
+ if (aarch64_linux_get_atag (addr, &atag) != 0)
+ return false;
+
+ /* Fetch the logical tag for ADDRESS. */
+ gdb_byte ltag = aarch64_linux_get_ltag (addr);
+
+ /* Are the tags the same? */
+ if (ltag == atag)
+ return false;
+
+ return true;
+}
+
+/* Implement the set_memtags gdbarch method. */
+
+static int
+aarch64_linux_set_memtags (struct gdbarch *gdbarch, struct value *address,
+ size_t length, const gdb::byte_vector &tags,
+ enum memtag_type tag_type)
+{
+ if (tags.empty ())
+ return 0;
+
+ gdb_assert (address != nullptr);
+
+ CORE_ADDR addr = value_as_address (address);
+
+ /* Set the logical tag or the allocation tag. */
+ if (tag_type == tag_logical)
+ {
+ /* When setting logical tags, we don't care about the length, since
+ we are only setting a single logical tag. */
+ addr = aarch64_linux_set_ltag (addr, tags[0]);
+
+ /* Update the value's content with the tag. */
+ enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
+ gdb_byte *srcbuf = value_contents_raw (address);
+ store_unsigned_integer (srcbuf, sizeof (addr), byte_order, addr);
+ }
+ else
+ {
+ /* Make sure we are dealing with a tagged address to begin with. */
+ if (!aarch64_linux_tagged_address_p (gdbarch, address))
+ return 1;
+
+ /* With G being the number of tag granules and N the number of tags
+ passed in, we can have the following cases:
+
+ 1 - G == N: Store all the N tags to memory.
+
+ 2 - G < N : Warn about having more tags than granules, but write G
+ tags.
+
+ 3 - G > N : This is a "fill tags" operation. We should use the tags
+ as a pattern to fill the granules repeatedly until we have
+ written G tags to memory.
+ */
+
+ size_t g = get_tag_granules (addr, length, MTE_GRANULE_SIZE);
+ size_t n = tags.size ();
+
+ if (g < n)
+ {
+ warning (_("Got more tags than memory granules. Tags will be "
+ "truncated."));
+ }
+ else if (g > n)
+ warning (_("Using tag pattern to fill memory range."));
+
+ if (target_store_memtags (addr, length, tags) != 0)
+ return 1;
+ }
+ return 0;
+}
+
+/* Implement the get_memtag gdbarch method. */
+
+static struct value *
+aarch64_linux_get_memtag (struct gdbarch *gdbarch, struct value *address,
+ enum memtag_type tag_type)
+{
+ gdb_assert (address != nullptr);
+
+ CORE_ADDR addr = value_as_address (address);
+ CORE_ADDR tag = 0;
+
+ /* Get the logical tag or the allocation tag. */
+ if (tag_type == tag_logical)
+ tag = aarch64_linux_get_ltag (addr);
+ else
+ {
+ /* Make sure we are dealing with a tagged address to begin with. */
+ if (!aarch64_linux_tagged_address_p (gdbarch, address))
+ return nullptr;
+
+ if (aarch64_linux_get_atag (addr, &tag) != 0)
+ return nullptr;
+ }
+
+ /* Convert the tag to a value. */
+ return value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
+ tag);
+}
+
+/* Implement the memtag_to_string gdbarch method. */
+
+static std::string
+aarch64_linux_memtag_to_string (struct gdbarch *gdbarch,
+ struct value *address,
+ enum memtag_type tag_type)
+{
+ gdb_assert (address != nullptr);
+
+ struct value *v_tag = aarch64_linux_get_memtag (gdbarch, address, tag_type);
+
+ if (v_tag == nullptr && tag_allocation)
+ error (_("Error getting tag from target"));
+
+ CORE_ADDR tag = value_as_address (v_tag);
+
+ return string_printf ("0x%s", phex_nz (tag, sizeof (tag)));
+}
+
static void
aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
@@ -1494,6 +1681,31 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
data associated with the address. */
set_gdbarch_significant_addr_bit (gdbarch, 56);
+ /* MTE-specific settings and hooks. */
+ if (tdep->has_mte ())
+ {
+ /* Register a hook for checking if an address is tagged or not. */
+ set_gdbarch_tagged_address_p (gdbarch, aarch64_linux_tagged_address_p);
+
+ /* Register a hook for checking if there is a memory tag mismatch. */
+ set_gdbarch_memtag_mismatch_p (gdbarch,
+ aarch64_linux_memtag_mismatch_p);
+
+ /* Register a hook for setting the logical/allocation tags for
+ a range of addresses. */
+ set_gdbarch_set_memtags (gdbarch, aarch64_linux_set_memtags);
+
+ /* Register a hook for extracting the logical/allocation tag from an
+ address. */
+ set_gdbarch_get_memtag (gdbarch, aarch64_linux_get_memtag);
+
+ /* Set the allocation tag granule size to 16 bytes. */
+ set_gdbarch_memtag_granule_size (gdbarch, MTE_GRANULE_SIZE);
+
+ /* Register a hook for converting a memory tag to a string. */
+ set_gdbarch_memtag_to_string (gdbarch, aarch64_linux_memtag_to_string);
+ }
+
/* Initialize the aarch64_linux_record_tdep. */
/* These values are the size of the type that will be used in a system
call. They are obtained from Linux Kernel source. */
diff --git a/gdb/arch/aarch64-mte-linux.c b/gdb/arch/aarch64-mte-linux.c
index ee162aef19..5c02e5ef5b 100644
--- a/gdb/arch/aarch64-mte-linux.c
+++ b/gdb/arch/aarch64-mte-linux.c
@@ -32,3 +32,39 @@ get_tag_granules (CORE_ADDR addr, size_t len, size_t granule_size)
/* We always have at least 1 granule. */
return 1 + (e_addr - s_addr) / granule_size;
}
+
+/* See arch/aarch64-mte-linux.h */
+
+CORE_ADDR
+make_ltag_bits (CORE_ADDR value)
+{
+ return value & MTE_LOGICAL_MAX_VALUE;
+}
+
+/* See arch/aarch64-mte-linux.h */
+
+CORE_ADDR
+make_ltag (CORE_ADDR value)
+{
+ return make_ltag_bits (value) << MTE_LOGICAL_TAG_START_BIT;
+}
+
+/* See arch/aarch64-mte-linux.h */
+
+CORE_ADDR
+aarch64_linux_set_ltag (CORE_ADDR address, CORE_ADDR tag)
+{
+ /* Remove the existing tag. */
+ address &= ~make_ltag (MTE_LOGICAL_MAX_VALUE);
+
+ /* Return the new tagged address. */
+ return address | make_ltag (tag);
+}
+
+/* See arch/aarch64-mte-linux.h */
+
+CORE_ADDR
+aarch64_linux_get_ltag (CORE_ADDR address)
+{
+ return make_ltag_bits (address >> MTE_LOGICAL_TAG_START_BIT);
+}
diff --git a/gdb/arch/aarch64-mte-linux.h b/gdb/arch/aarch64-mte-linux.h
index e555f0af19..5c5783f28b 100644
--- a/gdb/arch/aarch64-mte-linux.h
+++ b/gdb/arch/aarch64-mte-linux.h
@@ -32,10 +32,29 @@
/* We have one tag per 16 bytes of memory. */
#define MTE_GRANULE_SIZE 16
+#define MTE_LOGICAL_TAG_START_BIT 56
+#define MTE_LOGICAL_MAX_VALUE 0xf
/* Return the number of tag granules in the memory range
[ADDR, ADDR + LEN) given GRANULE_SIZE. */
extern size_t get_tag_granules (CORE_ADDR addr, size_t len,
size_t granule_size);
+/* Return the 4-bit tag made from VALUE. */
+extern CORE_ADDR make_ltag_bits (CORE_ADDR value);
+
+/* Return the 4-bit tag that can be OR-ed to an address. */
+extern CORE_ADDR make_ltag (CORE_ADDR value);
+
+/* Helper to set the logical TAG for a 64-bit ADDRESS.
+
+ It is always possible to set the logical tag. */
+extern CORE_ADDR aarch64_linux_set_ltag (CORE_ADDR address,
+ CORE_ADDR tag);
+
+/* Helper to get the logical tag from a 64-bit ADDRESS.
+
+ It is always possible to get the logical tag. */
+extern CORE_ADDR aarch64_linux_get_ltag (CORE_ADDR address);
+
#endif /* ARCH_AARCH64_LINUX_H */
--
2.17.1
next prev parent reply other threads:[~2020-07-15 19:48 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-15 19:44 [PATCH 00/23] Memory Tagging Support + AArch64 Linux implementation Luis Machado
2020-07-15 19:44 ` [PATCH 01/23] New target methods for memory tagging support Luis Machado
2020-07-15 19:44 ` [PATCH 02/23] New gdbarch memory tagging hooks Luis Machado
2020-07-15 19:44 ` [PATCH 03/23] Add GDB-side remote target support for memory tagging Luis Machado
2020-07-15 19:44 ` [PATCH 04/23] Unit testing for GDB-side remote memory tagging handling Luis Machado
2020-07-15 19:44 ` [PATCH 05/23] GDBserver remote packet support for memory tagging Luis Machado
2020-07-15 19:44 ` [PATCH 06/23] Unit tests for gdbserver memory tagging remote packets Luis Machado
2020-07-15 19:44 ` [PATCH 07/23] Documentation for " Luis Machado
2020-07-17 5:54 ` Eli Zaretskii
2020-07-17 14:20 ` Luis Machado
2020-07-15 19:44 ` [PATCH 08/23] AArch64: Add MTE CPU feature check support Luis Machado
2020-07-15 19:44 ` [PATCH 09/23] AArch64: Add target description/feature for MTE registers Luis Machado
2020-07-15 19:45 ` [PATCH 10/23] AArch64: Add MTE register set support for GDB and gdbserver Luis Machado
2020-07-15 19:45 ` [PATCH 11/23] AArch64: Add MTE ptrace requests Luis Machado
2020-07-15 19:45 ` [PATCH 12/23] AArch64: Implement memory tagging target methods for AArch64 Luis Machado
2020-07-15 19:45 ` [PATCH 13/23] Refactor parsing of /proc/<pid>/smaps Luis Machado
2020-07-15 19:45 ` Luis Machado [this message]
2020-07-15 19:45 ` [PATCH 15/23] AArch64: Add unit testing for logical tag set/get operations Luis Machado
2020-07-15 19:45 ` [PATCH 16/23] AArch64: Report tag violation error information Luis Machado
2020-07-15 19:45 ` [PATCH 17/23] AArch64: Add gdbserver MTE support Luis Machado
2020-07-15 19:45 ` [PATCH 18/23] New mtag commands Luis Machado
2020-07-15 19:45 ` [PATCH 19/23] Documentation for the new " Luis Machado
2020-07-17 6:11 ` Eli Zaretskii
2020-07-17 14:20 ` Luis Machado
2020-07-17 14:29 ` Eli Zaretskii
2020-07-17 15:08 ` Luis Machado
2020-07-15 19:45 ` [PATCH 20/23] Extend "x" and "print" commands to support memory tagging Luis Machado
2020-07-15 19:45 ` [PATCH 21/23] Document new "x" and "print" memory tagging extensions Luis Machado
2020-07-17 6:16 ` Eli Zaretskii
2020-07-17 14:20 ` Luis Machado
2020-07-17 14:31 ` Eli Zaretskii
2020-07-15 19:45 ` [PATCH 22/23] Add NEWS entry Luis Machado
2020-07-17 6:18 ` Eli Zaretskii
2020-07-17 14:20 ` Luis Machado
2020-07-15 19:45 ` [PATCH 23/23] Add memory tagging testcases Luis Machado
2020-07-16 16:49 ` [PATCH 00/23] Memory Tagging Support + AArch64 Linux implementation Alan Hayward
2020-07-17 12:33 ` Luis Machado
2020-07-17 22:02 ` John Baldwin
2020-07-23 13:59 ` Luis Machado
2020-07-23 16:48 ` John Baldwin
2020-07-24 16:10 ` David Spickett
2020-07-23 14:59 ` Luis Machado
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200715194513.16641-15-luis.machado@linaro.org \
--to=luis.machado@linaro.org \
--cc=Alan.Hayward@arm.com \
--cc=catalin.marinas@arm.com \
--cc=david.spickett@linaro.org \
--cc=gdb-patches@sourceware.org \
--cc=jose.marchesi@oracle.com \
--cc=omair.javaid@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).