From: Josh Poimboeuf <jpoim...@kernel.org>

In preparation for unwinding user space stacks with sframe, add basic
sframe compile infrastructure and support for reading the .sframe
section header.

sframe_add_section() reads the header and unconditionally returns an
error, so it's not very useful yet.  A subsequent patch will improve
that.

Link: 
https://lore.kernel.org/all/f27e8463783febfa0dabb0432a3dd6be8ad98412.1737511963.git.jpoim...@kernel.org/

Signed-off-by: Josh Poimboeuf <jpoim...@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rost...@goodmis.org>
---
 MAINTAINERS            |   1 +
 arch/Kconfig           |   3 +
 include/linux/sframe.h |  40 ++++++++++++
 kernel/unwind/Makefile |   3 +-
 kernel/unwind/sframe.c | 136 +++++++++++++++++++++++++++++++++++++++++
 kernel/unwind/sframe.h |  71 +++++++++++++++++++++
 6 files changed, 253 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/sframe.h
 create mode 100644 kernel/unwind/sframe.c
 create mode 100644 kernel/unwind/sframe.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 370d780fd5f8..a6a94d2d03b0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25932,6 +25932,7 @@ USERSPACE STACK UNWINDING
 M:     Josh Poimboeuf <jpoim...@kernel.org>
 M:     Steven Rostedt <rost...@goodmis.org>
 S:     Maintained
+F:     include/linux/sframe.h
 F:     include/linux/unwind*.h
 F:     kernel/unwind/
 
diff --git a/arch/Kconfig b/arch/Kconfig
index 2c41d3072910..c54d35e2f860 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -446,6 +446,9 @@ config HAVE_UNWIND_USER_COMPAT_FP
        bool
        depends on HAVE_UNWIND_USER_FP
 
+config HAVE_UNWIND_USER_SFRAME
+       bool
+
 config HAVE_PERF_REGS
        bool
        help
diff --git a/include/linux/sframe.h b/include/linux/sframe.h
new file mode 100644
index 000000000000..0584f661f698
--- /dev/null
+++ b/include/linux/sframe.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_SFRAME_H
+#define _LINUX_SFRAME_H
+
+#include <linux/mm_types.h>
+#include <linux/unwind_user_types.h>
+
+#ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
+
+struct sframe_section {
+       unsigned long   sframe_start;
+       unsigned long   sframe_end;
+       unsigned long   text_start;
+       unsigned long   text_end;
+
+       unsigned long   fdes_start;
+       unsigned long   fres_start;
+       unsigned long   fres_end;
+       unsigned int    num_fdes;
+
+       signed char     ra_off;
+       signed char     fp_off;
+};
+
+extern int sframe_add_section(unsigned long sframe_start, unsigned long 
sframe_end,
+                             unsigned long text_start, unsigned long text_end);
+extern int sframe_remove_section(unsigned long sframe_addr);
+
+#else /* !CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+static inline int sframe_add_section(unsigned long sframe_start, unsigned long 
sframe_end,
+                                    unsigned long text_start, unsigned long 
text_end)
+{
+       return -ENOSYS;
+}
+static inline int sframe_remove_section(unsigned long sframe_addr) { return 
-ENOSYS; }
+
+#endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+#endif /* _LINUX_SFRAME_H */
diff --git a/kernel/unwind/Makefile b/kernel/unwind/Makefile
index eae37bea54fd..146038165865 100644
--- a/kernel/unwind/Makefile
+++ b/kernel/unwind/Makefile
@@ -1 +1,2 @@
- obj-$(CONFIG_UNWIND_USER)     += user.o deferred.o
+ obj-$(CONFIG_UNWIND_USER)             += user.o deferred.o
+ obj-$(CONFIG_HAVE_UNWIND_USER_SFRAME) += sframe.o
diff --git a/kernel/unwind/sframe.c b/kernel/unwind/sframe.c
new file mode 100644
index 000000000000..20287f795b36
--- /dev/null
+++ b/kernel/unwind/sframe.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Userspace sframe access functions
+ */
+
+#define pr_fmt(fmt)    "sframe: " fmt
+
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/srcu.h>
+#include <linux/uaccess.h>
+#include <linux/mm.h>
+#include <linux/string_helpers.h>
+#include <linux/sframe.h>
+#include <linux/unwind_user_types.h>
+
+#include "sframe.h"
+
+#define dbg(fmt, ...)                                                  \
+       pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__)
+
+static void free_section(struct sframe_section *sec)
+{
+       kfree(sec);
+}
+
+static int sframe_read_header(struct sframe_section *sec)
+{
+       unsigned long header_end, fdes_start, fdes_end, fres_start, fres_end;
+       struct sframe_header shdr;
+       unsigned int num_fdes;
+
+       if (copy_from_user(&shdr, (void __user *)sec->sframe_start, 
sizeof(shdr))) {
+               dbg("header usercopy failed\n");
+               return -EFAULT;
+       }
+
+       if (shdr.preamble.magic != SFRAME_MAGIC ||
+           shdr.preamble.version != SFRAME_VERSION_2 ||
+           !(shdr.preamble.flags & SFRAME_F_FDE_SORTED) ||
+           shdr.auxhdr_len) {
+               dbg("bad/unsupported sframe header\n");
+               return -EINVAL;
+       }
+
+       if (!shdr.num_fdes || !shdr.num_fres) {
+               dbg("no fde/fre entries\n");
+               return -EINVAL;
+       }
+
+       header_end = sec->sframe_start + SFRAME_HEADER_SIZE(shdr);
+       if (header_end >= sec->sframe_end) {
+               dbg("header doesn't fit in section\n");
+               return -EINVAL;
+       }
+
+       num_fdes   = shdr.num_fdes;
+       fdes_start = header_end + shdr.fdes_off;
+       fdes_end   = fdes_start + (num_fdes * sizeof(struct sframe_fde));
+
+       fres_start = header_end + shdr.fres_off;
+       fres_end   = fres_start + shdr.fre_len;
+
+       if (fres_start < fdes_end || fres_end > sec->sframe_end) {
+               dbg("inconsistent fde/fre offsets\n");
+               return -EINVAL;
+       }
+
+       sec->num_fdes           = num_fdes;
+       sec->fdes_start         = fdes_start;
+       sec->fres_start         = fres_start;
+       sec->fres_end           = fres_end;
+
+       sec->ra_off             = shdr.cfa_fixed_ra_offset;
+       sec->fp_off             = shdr.cfa_fixed_fp_offset;
+
+       return 0;
+}
+
+int sframe_add_section(unsigned long sframe_start, unsigned long sframe_end,
+                      unsigned long text_start, unsigned long text_end)
+{
+       struct maple_tree *sframe_mt = &current->mm->sframe_mt;
+       struct vm_area_struct *sframe_vma, *text_vma;
+       struct mm_struct *mm = current->mm;
+       struct sframe_section *sec;
+       int ret;
+
+       if (!sframe_start || !sframe_end || !text_start || !text_end) {
+               dbg("zero-length sframe/text address\n");
+               return -EINVAL;
+       }
+
+       scoped_guard(mmap_read_lock, mm) {
+               sframe_vma = vma_lookup(mm, sframe_start);
+               if (!sframe_vma || sframe_end > sframe_vma->vm_end) {
+                       dbg("bad sframe address (0x%lx - 0x%lx)\n",
+                           sframe_start, sframe_end);
+                       return -EINVAL;
+               }
+
+               text_vma = vma_lookup(mm, text_start);
+               if (!text_vma ||
+                   !(text_vma->vm_flags & VM_EXEC) ||
+                   text_end > text_vma->vm_end) {
+                       dbg("bad text address (0x%lx - 0x%lx)\n",
+                           text_start, text_end);
+                       return -EINVAL;
+               }
+       }
+
+       sec = kzalloc(sizeof(*sec), GFP_KERNEL);
+       if (!sec)
+               return -ENOMEM;
+
+       sec->sframe_start       = sframe_start;
+       sec->sframe_end         = sframe_end;
+       sec->text_start         = text_start;
+       sec->text_end           = text_end;
+
+       ret = sframe_read_header(sec);
+       if (ret)
+               goto err_free;
+
+       /* TODO nowhere to store it yet - just free it and return an error */
+       ret = -ENOSYS;
+
+err_free:
+       free_section(sec);
+       return ret;
+}
+
+int sframe_remove_section(unsigned long sframe_start)
+{
+       return -ENOSYS;
+}
diff --git a/kernel/unwind/sframe.h b/kernel/unwind/sframe.h
new file mode 100644
index 000000000000..e9bfccfaf5b4
--- /dev/null
+++ b/kernel/unwind/sframe.h
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * From https://www.sourceware.org/binutils/docs/sframe-spec.html
+ */
+#ifndef _SFRAME_H
+#define _SFRAME_H
+
+#include <linux/types.h>
+
+#define SFRAME_VERSION_1                       1
+#define SFRAME_VERSION_2                       2
+#define SFRAME_MAGIC                           0xdee2
+
+#define SFRAME_F_FDE_SORTED                    0x1
+#define SFRAME_F_FRAME_POINTER                 0x2
+
+#define SFRAME_ABI_AARCH64_ENDIAN_BIG          1
+#define SFRAME_ABI_AARCH64_ENDIAN_LITTLE       2
+#define SFRAME_ABI_AMD64_ENDIAN_LITTLE         3
+
+#define SFRAME_FDE_TYPE_PCINC                  0
+#define SFRAME_FDE_TYPE_PCMASK                 1
+
+struct sframe_preamble {
+       u16     magic;
+       u8      version;
+       u8      flags;
+} __packed;
+
+struct sframe_header {
+       struct sframe_preamble preamble;
+       u8      abi_arch;
+       s8      cfa_fixed_fp_offset;
+       s8      cfa_fixed_ra_offset;
+       u8      auxhdr_len;
+       u32     num_fdes;
+       u32     num_fres;
+       u32     fre_len;
+       u32     fdes_off;
+       u32     fres_off;
+} __packed;
+
+#define SFRAME_HEADER_SIZE(header) \
+       ((sizeof(struct sframe_header) + header.auxhdr_len))
+
+#define SFRAME_AARCH64_PAUTH_KEY_A             0
+#define SFRAME_AARCH64_PAUTH_KEY_B             1
+
+struct sframe_fde {
+       s32     start_addr;
+       u32     func_size;
+       u32     fres_off;
+       u32     fres_num;
+       u8      info;
+       u8      rep_size;
+       u16 padding;
+} __packed;
+
+#define SFRAME_FUNC_FRE_TYPE(data)             (data & 0xf)
+#define SFRAME_FUNC_FDE_TYPE(data)             ((data >> 4) & 0x1)
+#define SFRAME_FUNC_PAUTH_KEY(data)            ((data >> 5) & 0x1)
+
+#define SFRAME_BASE_REG_FP                     0
+#define SFRAME_BASE_REG_SP                     1
+
+#define SFRAME_FRE_CFA_BASE_REG_ID(data)       (data & 0x1)
+#define SFRAME_FRE_OFFSET_COUNT(data)          ((data >> 1) & 0xf)
+#define SFRAME_FRE_OFFSET_SIZE(data)           ((data >> 5) & 0x3)
+#define SFRAME_FRE_MANGLED_RA_P(data)          ((data >> 7) & 0x1)
+
+#endif /* _SFRAME_H */
-- 
2.47.2



Reply via email to