Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #define pr_fmt(fmt)	"efi: memattr: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/early_ioremap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static int __initdata tbl_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) unsigned long __ro_after_init efi_mem_attr_table = EFI_INVALID_TABLE_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Reserve the memory associated with the Memory Attributes configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * table, if it exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) int __init efi_memattr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	efi_memory_attributes_table_t *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	if (efi_mem_attr_table == EFI_INVALID_TABLE_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	tbl = early_memremap(efi_mem_attr_table, sizeof(*tbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (!tbl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		       efi_mem_attr_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (tbl->version > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		pr_warn("Unexpected EFI Memory Attributes table version %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			tbl->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	memblock_reserve(efi_mem_attr_table, tbl_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	set_bit(EFI_MEM_ATTR, &efi.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	early_memunmap(tbl, sizeof(*tbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Returns a copy @out of the UEFI memory descriptor @in if it is covered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * entirely by a UEFI memory map entry with matching attributes. The virtual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * address of @out is set according to the matching entry that was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u64 in_paddr = in->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	efi_memory_desc_t *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	*out = *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (in->type != EFI_RUNTIME_SERVICES_CODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	    in->type != EFI_RUNTIME_SERVICES_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		pr_warn("Entry type should be RuntimeServiceCode/Data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (PAGE_SIZE > EFI_PAGE_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	    (!PAGE_ALIGNED(in->phys_addr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	     !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		 * Since arm64 may execute with page sizes of up to 64 KB, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		 * UEFI spec mandates that RuntimeServices memory regions must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 * be 64 KB aligned. We need to validate this here since we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		 * not be able to tighten permissions on such regions without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		 * affecting adjacent regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		pr_warn("Entry address region misaligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	for_each_efi_memory_desc(md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		u64 md_paddr = md->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		if (!(md->attribute & EFI_MEMORY_RUNTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (md->virt_addr == 0 && md->phys_addr != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			/* no virtual mapping has been installed by the stub */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		 * This entry covers the start of @in, check whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 * it covers the end as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (md_paddr + md_size < in_paddr + in_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			pr_warn("Entry covers multiple EFI memory map regions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (md->type != in->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			pr_warn("Entry type deviates from EFI memory map region type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	pr_warn("No matching entry found in the EFI memory map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * To be called after the EFI page tables have been populated. If a memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * attributes table is available, its contents will be used to update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * mappings with tightened permissions as described by the table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * This requires the UEFI memory map to have already been populated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * virtual addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int __init efi_memattr_apply_permissions(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 					 efi_memattr_perm_setter fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	efi_memory_attributes_table_t *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (tbl_size <= sizeof(*tbl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * We need the EFI memory map to be setup so we can use it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 * lookup the virtual addresses of all entries in the  of EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * Memory Attributes table. If it isn't available, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 * function should not be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (!tbl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		       efi_mem_attr_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (efi_enabled(EFI_DBG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		pr_info("Processing EFI Memory Attributes table:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		efi_memory_desc_t md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		char buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				       &md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		size = md.num_pages << EFI_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (efi_enabled(EFI_DBG) || !valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			pr_info("%s 0x%012llx-0x%012llx %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				valid ? "" : "!", md.phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				md.phys_addr + size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 				efi_md_typeattr_format(buf, sizeof(buf), &md));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			ret = fn(mm, &md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				pr_err("Error updating mappings, skipping subsequent md's\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	memunmap(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }