1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/coredump.h>
4 #include <linux/elfcore.h>
5 #include <linux/kernel.h>
8 #include <asm/cpufeature.h>
12 #define VMA_ITERATOR(name, mm, addr) \
13 struct mm_struct *name = mm
14 #define for_each_vma(vmi, vma) \
15 for (vma = vmi->mmap; vma; vma = vma->vm_next)
18 #define for_each_mte_vma(vmi, vma) \
19 if (system_supports_mte()) \
20 for_each_vma(vmi, vma) \
21 if (vma->vm_flags & VM_MTE)
23 static unsigned long mte_vma_tag_dump_size(struct vm_area_struct *vma)
25 if (vma->vm_flags & VM_DONTDUMP)
28 return vma_pages(vma) * MTE_PAGE_TAG_STORAGE;
31 /* Derived from dump_user_range(); start/end must be page-aligned */
32 static int mte_dump_tag_range(struct coredump_params *cprm,
33 unsigned long start, unsigned long end)
37 for (addr = start; addr < end; addr += PAGE_SIZE) {
38 char tags[MTE_PAGE_TAG_STORAGE];
39 struct page *page = get_dump_page(addr);
42 * get_dump_page() returns NULL when encountering an empty
43 * page table entry that would otherwise have been filled with
44 * the zero page. Skip the equivalent tag dump which would
45 * have been all zeros.
48 dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
53 * Pages mapped in user space as !pte_access_permitted() (e.g.
54 * PROT_EXEC only) may not have the PG_mte_tagged flag set.
56 if (!test_bit(PG_mte_tagged, &page->flags)) {
58 dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
62 mte_save_page_tags(page_address(page), tags);
64 if (!dump_emit(cprm, tags, MTE_PAGE_TAG_STORAGE))
71 Elf_Half elf_core_extra_phdrs(void)
73 struct vm_area_struct *vma;
75 VMA_ITERATOR(vmi, current->mm, 0);
77 for_each_mte_vma(vmi, vma)
83 int elf_core_write_extra_phdrs(struct coredump_params *cprm, loff_t offset)
85 struct vm_area_struct *vma;
86 VMA_ITERATOR(vmi, current->mm, 0);
88 for_each_mte_vma(vmi, vma) {
91 phdr.p_type = PT_ARM_MEMTAG_MTE;
92 phdr.p_offset = offset;
93 phdr.p_vaddr = vma->vm_start;
95 phdr.p_filesz = mte_vma_tag_dump_size(vma);
96 phdr.p_memsz = vma->vm_end - vma->vm_start;
97 offset += phdr.p_filesz;
101 if (!dump_emit(cprm, &phdr, sizeof(phdr)))
108 size_t elf_core_extra_data_size(void)
110 struct vm_area_struct *vma;
111 size_t data_size = 0;
112 VMA_ITERATOR(vmi, current->mm, 0);
114 for_each_mte_vma(vmi, vma)
115 data_size += mte_vma_tag_dump_size(vma);
120 int elf_core_write_extra_data(struct coredump_params *cprm)
122 struct vm_area_struct *vma;
123 VMA_ITERATOR(vmi, current->mm, 0);
125 for_each_mte_vma(vmi, vma) {
126 if (vma->vm_flags & VM_DONTDUMP)
129 if (!mte_dump_tag_range(cprm, vma->vm_start, vma->vm_end))