xref: /openbmc/linux/arch/arm64/kernel/elfcore.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
16dd8b1a0SCatalin Marinas // SPDX-License-Identifier: GPL-2.0-only
26dd8b1a0SCatalin Marinas 
36dd8b1a0SCatalin Marinas #include <linux/coredump.h>
46dd8b1a0SCatalin Marinas #include <linux/elfcore.h>
56dd8b1a0SCatalin Marinas #include <linux/kernel.h>
66dd8b1a0SCatalin Marinas #include <linux/mm.h>
76dd8b1a0SCatalin Marinas 
86dd8b1a0SCatalin Marinas #include <asm/cpufeature.h>
96dd8b1a0SCatalin Marinas #include <asm/mte.h>
106dd8b1a0SCatalin Marinas 
11*4f4c549fSCatalin Marinas #define for_each_mte_vma(cprm, i, m)					\
126dd8b1a0SCatalin Marinas 	if (system_supports_mte())					\
13*4f4c549fSCatalin Marinas 		for (i = 0, m = cprm->vma_meta;				\
14*4f4c549fSCatalin Marinas 		     i < cprm->vma_count;				\
15*4f4c549fSCatalin Marinas 		     i++, m = cprm->vma_meta + i)			\
16*4f4c549fSCatalin Marinas 			if (m->flags & VM_MTE)
176dd8b1a0SCatalin Marinas 
mte_vma_tag_dump_size(struct core_vma_metadata * m)18*4f4c549fSCatalin Marinas static unsigned long mte_vma_tag_dump_size(struct core_vma_metadata *m)
196dd8b1a0SCatalin Marinas {
20*4f4c549fSCatalin Marinas 	return (m->dump_size >> PAGE_SHIFT) * MTE_PAGE_TAG_STORAGE;
216dd8b1a0SCatalin Marinas }
226dd8b1a0SCatalin Marinas 
236dd8b1a0SCatalin Marinas /* Derived from dump_user_range(); start/end must be page-aligned */
mte_dump_tag_range(struct coredump_params * cprm,unsigned long start,unsigned long len)246dd8b1a0SCatalin Marinas static int mte_dump_tag_range(struct coredump_params *cprm,
25*4f4c549fSCatalin Marinas 			      unsigned long start, unsigned long len)
266dd8b1a0SCatalin Marinas {
2716decce2SCatalin Marinas 	int ret = 1;
286dd8b1a0SCatalin Marinas 	unsigned long addr;
2916decce2SCatalin Marinas 	void *tags = NULL;
306dd8b1a0SCatalin Marinas 
31*4f4c549fSCatalin Marinas 	for (addr = start; addr < start + len; addr += PAGE_SIZE) {
326dd8b1a0SCatalin Marinas 		struct page *page = get_dump_page(addr);
336dd8b1a0SCatalin Marinas 
346dd8b1a0SCatalin Marinas 		/*
356dd8b1a0SCatalin Marinas 		 * get_dump_page() returns NULL when encountering an empty
366dd8b1a0SCatalin Marinas 		 * page table entry that would otherwise have been filled with
376dd8b1a0SCatalin Marinas 		 * the zero page. Skip the equivalent tag dump which would
386dd8b1a0SCatalin Marinas 		 * have been all zeros.
396dd8b1a0SCatalin Marinas 		 */
406dd8b1a0SCatalin Marinas 		if (!page) {
416dd8b1a0SCatalin Marinas 			dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
426dd8b1a0SCatalin Marinas 			continue;
436dd8b1a0SCatalin Marinas 		}
446dd8b1a0SCatalin Marinas 
456dd8b1a0SCatalin Marinas 		/*
466dd8b1a0SCatalin Marinas 		 * Pages mapped in user space as !pte_access_permitted() (e.g.
476dd8b1a0SCatalin Marinas 		 * PROT_EXEC only) may not have the PG_mte_tagged flag set.
486dd8b1a0SCatalin Marinas 		 */
49e059853dSCatalin Marinas 		if (!page_mte_tagged(page)) {
506dd8b1a0SCatalin Marinas 			put_page(page);
516dd8b1a0SCatalin Marinas 			dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
526dd8b1a0SCatalin Marinas 			continue;
536dd8b1a0SCatalin Marinas 		}
546dd8b1a0SCatalin Marinas 
5516decce2SCatalin Marinas 		if (!tags) {
5616decce2SCatalin Marinas 			tags = mte_allocate_tag_storage();
5716decce2SCatalin Marinas 			if (!tags) {
586dd8b1a0SCatalin Marinas 				put_page(page);
5916decce2SCatalin Marinas 				ret = 0;
6016decce2SCatalin Marinas 				break;
6116decce2SCatalin Marinas 			}
626dd8b1a0SCatalin Marinas 		}
636dd8b1a0SCatalin Marinas 
6416decce2SCatalin Marinas 		mte_save_page_tags(page_address(page), tags);
6516decce2SCatalin Marinas 		put_page(page);
6616decce2SCatalin Marinas 		if (!dump_emit(cprm, tags, MTE_PAGE_TAG_STORAGE)) {
6716decce2SCatalin Marinas 			ret = 0;
6816decce2SCatalin Marinas 			break;
6916decce2SCatalin Marinas 		}
7016decce2SCatalin Marinas 	}
7116decce2SCatalin Marinas 
7216decce2SCatalin Marinas 	if (tags)
7316decce2SCatalin Marinas 		mte_free_tag_storage(tags);
7416decce2SCatalin Marinas 
7516decce2SCatalin Marinas 	return ret;
766dd8b1a0SCatalin Marinas }
776dd8b1a0SCatalin Marinas 
elf_core_extra_phdrs(struct coredump_params * cprm)7819e183b5SCatalin Marinas Elf_Half elf_core_extra_phdrs(struct coredump_params *cprm)
796dd8b1a0SCatalin Marinas {
80*4f4c549fSCatalin Marinas 	int i;
81*4f4c549fSCatalin Marinas 	struct core_vma_metadata *m;
826dd8b1a0SCatalin Marinas 	int vma_count = 0;
836dd8b1a0SCatalin Marinas 
84*4f4c549fSCatalin Marinas 	for_each_mte_vma(cprm, i, m)
856dd8b1a0SCatalin Marinas 		vma_count++;
866dd8b1a0SCatalin Marinas 
876dd8b1a0SCatalin Marinas 	return vma_count;
886dd8b1a0SCatalin Marinas }
896dd8b1a0SCatalin Marinas 
elf_core_write_extra_phdrs(struct coredump_params * cprm,loff_t offset)906dd8b1a0SCatalin Marinas int elf_core_write_extra_phdrs(struct coredump_params *cprm, loff_t offset)
916dd8b1a0SCatalin Marinas {
92*4f4c549fSCatalin Marinas 	int i;
93*4f4c549fSCatalin Marinas 	struct core_vma_metadata *m;
946dd8b1a0SCatalin Marinas 
95*4f4c549fSCatalin Marinas 	for_each_mte_vma(cprm, i, m) {
966dd8b1a0SCatalin Marinas 		struct elf_phdr phdr;
976dd8b1a0SCatalin Marinas 
98c35fe2a6SCatalin Marinas 		phdr.p_type = PT_AARCH64_MEMTAG_MTE;
996dd8b1a0SCatalin Marinas 		phdr.p_offset = offset;
100*4f4c549fSCatalin Marinas 		phdr.p_vaddr = m->start;
1016dd8b1a0SCatalin Marinas 		phdr.p_paddr = 0;
102*4f4c549fSCatalin Marinas 		phdr.p_filesz = mte_vma_tag_dump_size(m);
103*4f4c549fSCatalin Marinas 		phdr.p_memsz = m->end - m->start;
1046dd8b1a0SCatalin Marinas 		offset += phdr.p_filesz;
1056dd8b1a0SCatalin Marinas 		phdr.p_flags = 0;
1066dd8b1a0SCatalin Marinas 		phdr.p_align = 0;
1076dd8b1a0SCatalin Marinas 
1086dd8b1a0SCatalin Marinas 		if (!dump_emit(cprm, &phdr, sizeof(phdr)))
1096dd8b1a0SCatalin Marinas 			return 0;
1106dd8b1a0SCatalin Marinas 	}
1116dd8b1a0SCatalin Marinas 
1126dd8b1a0SCatalin Marinas 	return 1;
1136dd8b1a0SCatalin Marinas }
1146dd8b1a0SCatalin Marinas 
elf_core_extra_data_size(struct coredump_params * cprm)11519e183b5SCatalin Marinas size_t elf_core_extra_data_size(struct coredump_params *cprm)
1166dd8b1a0SCatalin Marinas {
117*4f4c549fSCatalin Marinas 	int i;
118*4f4c549fSCatalin Marinas 	struct core_vma_metadata *m;
1196dd8b1a0SCatalin Marinas 	size_t data_size = 0;
1206dd8b1a0SCatalin Marinas 
121*4f4c549fSCatalin Marinas 	for_each_mte_vma(cprm, i, m)
122*4f4c549fSCatalin Marinas 		data_size += mte_vma_tag_dump_size(m);
1236dd8b1a0SCatalin Marinas 
1246dd8b1a0SCatalin Marinas 	return data_size;
1256dd8b1a0SCatalin Marinas }
1266dd8b1a0SCatalin Marinas 
elf_core_write_extra_data(struct coredump_params * cprm)1276dd8b1a0SCatalin Marinas int elf_core_write_extra_data(struct coredump_params *cprm)
1286dd8b1a0SCatalin Marinas {
129*4f4c549fSCatalin Marinas 	int i;
130*4f4c549fSCatalin Marinas 	struct core_vma_metadata *m;
1316dd8b1a0SCatalin Marinas 
132*4f4c549fSCatalin Marinas 	for_each_mte_vma(cprm, i, m) {
133*4f4c549fSCatalin Marinas 		if (!mte_dump_tag_range(cprm, m->start, m->dump_size))
1346dd8b1a0SCatalin Marinas 			return 0;
1356dd8b1a0SCatalin Marinas 	}
1366dd8b1a0SCatalin Marinas 
1376dd8b1a0SCatalin Marinas 	return 1;
1386dd8b1a0SCatalin Marinas }
139