xref: /openbmc/linux/drivers/firmware/efi/memattr.c (revision cf1d2ffc)
14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
210f0d2f5SArd Biesheuvel /*
310f0d2f5SArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
410f0d2f5SArd Biesheuvel  */
510f0d2f5SArd Biesheuvel 
610f0d2f5SArd Biesheuvel #define pr_fmt(fmt)	"efi: memattr: " fmt
710f0d2f5SArd Biesheuvel 
810f0d2f5SArd Biesheuvel #include <linux/efi.h>
910f0d2f5SArd Biesheuvel #include <linux/init.h>
1010f0d2f5SArd Biesheuvel #include <linux/io.h>
1110f0d2f5SArd Biesheuvel #include <linux/memblock.h>
1210f0d2f5SArd Biesheuvel 
1310f0d2f5SArd Biesheuvel #include <asm/early_ioremap.h>
1410f0d2f5SArd Biesheuvel 
1510f0d2f5SArd Biesheuvel static int __initdata tbl_size;
16a17e809eSArd Biesheuvel unsigned long __ro_after_init efi_mem_attr_table = EFI_INVALID_TABLE_ADDR;
1710f0d2f5SArd Biesheuvel 
1810f0d2f5SArd Biesheuvel /*
1910f0d2f5SArd Biesheuvel  * Reserve the memory associated with the Memory Attributes configuration
2010f0d2f5SArd Biesheuvel  * table, if it exists.
2110f0d2f5SArd Biesheuvel  */
efi_memattr_init(void)2210f0d2f5SArd Biesheuvel int __init efi_memattr_init(void)
2310f0d2f5SArd Biesheuvel {
2410f0d2f5SArd Biesheuvel 	efi_memory_attributes_table_t *tbl;
2510f0d2f5SArd Biesheuvel 
26a17e809eSArd Biesheuvel 	if (efi_mem_attr_table == EFI_INVALID_TABLE_ADDR)
2710f0d2f5SArd Biesheuvel 		return 0;
2810f0d2f5SArd Biesheuvel 
29a17e809eSArd Biesheuvel 	tbl = early_memremap(efi_mem_attr_table, sizeof(*tbl));
3010f0d2f5SArd Biesheuvel 	if (!tbl) {
3110f0d2f5SArd Biesheuvel 		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
32a17e809eSArd Biesheuvel 		       efi_mem_attr_table);
3310f0d2f5SArd Biesheuvel 		return -ENOMEM;
3410f0d2f5SArd Biesheuvel 	}
3510f0d2f5SArd Biesheuvel 
3610f0d2f5SArd Biesheuvel 	if (tbl->version > 2) {
3710f0d2f5SArd Biesheuvel 		pr_warn("Unexpected EFI Memory Attributes table version %d\n",
3810f0d2f5SArd Biesheuvel 			tbl->version);
3910f0d2f5SArd Biesheuvel 		goto unmap;
4010f0d2f5SArd Biesheuvel 	}
4110f0d2f5SArd Biesheuvel 
4210f0d2f5SArd Biesheuvel 	tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
43a17e809eSArd Biesheuvel 	memblock_reserve(efi_mem_attr_table, tbl_size);
44a19ebf59SSai Praneeth 	set_bit(EFI_MEM_ATTR, &efi.flags);
4510f0d2f5SArd Biesheuvel 
4610f0d2f5SArd Biesheuvel unmap:
4710f0d2f5SArd Biesheuvel 	early_memunmap(tbl, sizeof(*tbl));
4810f0d2f5SArd Biesheuvel 	return 0;
4910f0d2f5SArd Biesheuvel }
5010f0d2f5SArd Biesheuvel 
5110f0d2f5SArd Biesheuvel /*
5210f0d2f5SArd Biesheuvel  * Returns a copy @out of the UEFI memory descriptor @in if it is covered
5310f0d2f5SArd Biesheuvel  * entirely by a UEFI memory map entry with matching attributes. The virtual
5410f0d2f5SArd Biesheuvel  * address of @out is set according to the matching entry that was found.
5510f0d2f5SArd Biesheuvel  */
entry_is_valid(const efi_memory_desc_t * in,efi_memory_desc_t * out)5610f0d2f5SArd Biesheuvel static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
5710f0d2f5SArd Biesheuvel {
5810f0d2f5SArd Biesheuvel 	u64 in_paddr = in->phys_addr;
5910f0d2f5SArd Biesheuvel 	u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
6010f0d2f5SArd Biesheuvel 	efi_memory_desc_t *md;
6110f0d2f5SArd Biesheuvel 
6210f0d2f5SArd Biesheuvel 	*out = *in;
6310f0d2f5SArd Biesheuvel 
6410f0d2f5SArd Biesheuvel 	if (in->type != EFI_RUNTIME_SERVICES_CODE &&
6510f0d2f5SArd Biesheuvel 	    in->type != EFI_RUNTIME_SERVICES_DATA) {
6610f0d2f5SArd Biesheuvel 		pr_warn("Entry type should be RuntimeServiceCode/Data\n");
6710f0d2f5SArd Biesheuvel 		return false;
6810f0d2f5SArd Biesheuvel 	}
6910f0d2f5SArd Biesheuvel 
7010f0d2f5SArd Biesheuvel 	if (PAGE_SIZE > EFI_PAGE_SIZE &&
7110f0d2f5SArd Biesheuvel 	    (!PAGE_ALIGNED(in->phys_addr) ||
7210f0d2f5SArd Biesheuvel 	     !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
7310f0d2f5SArd Biesheuvel 		/*
7410f0d2f5SArd Biesheuvel 		 * Since arm64 may execute with page sizes of up to 64 KB, the
7510f0d2f5SArd Biesheuvel 		 * UEFI spec mandates that RuntimeServices memory regions must
7610f0d2f5SArd Biesheuvel 		 * be 64 KB aligned. We need to validate this here since we will
7710f0d2f5SArd Biesheuvel 		 * not be able to tighten permissions on such regions without
7810f0d2f5SArd Biesheuvel 		 * affecting adjacent regions.
7910f0d2f5SArd Biesheuvel 		 */
8010f0d2f5SArd Biesheuvel 		pr_warn("Entry address region misaligned\n");
8110f0d2f5SArd Biesheuvel 		return false;
8210f0d2f5SArd Biesheuvel 	}
8310f0d2f5SArd Biesheuvel 
8410f0d2f5SArd Biesheuvel 	for_each_efi_memory_desc(md) {
8510f0d2f5SArd Biesheuvel 		u64 md_paddr = md->phys_addr;
8610f0d2f5SArd Biesheuvel 		u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
8710f0d2f5SArd Biesheuvel 
8810f0d2f5SArd Biesheuvel 		if (!(md->attribute & EFI_MEMORY_RUNTIME))
8910f0d2f5SArd Biesheuvel 			continue;
905de0fef0SArd Biesheuvel 		if (md->virt_addr == 0 && md->phys_addr != 0) {
9110f0d2f5SArd Biesheuvel 			/* no virtual mapping has been installed by the stub */
9210f0d2f5SArd Biesheuvel 			break;
9310f0d2f5SArd Biesheuvel 		}
9410f0d2f5SArd Biesheuvel 
9510f0d2f5SArd Biesheuvel 		if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
9610f0d2f5SArd Biesheuvel 			continue;
9710f0d2f5SArd Biesheuvel 
9810f0d2f5SArd Biesheuvel 		/*
9910f0d2f5SArd Biesheuvel 		 * This entry covers the start of @in, check whether
10010f0d2f5SArd Biesheuvel 		 * it covers the end as well.
10110f0d2f5SArd Biesheuvel 		 */
10210f0d2f5SArd Biesheuvel 		if (md_paddr + md_size < in_paddr + in_size) {
10310f0d2f5SArd Biesheuvel 			pr_warn("Entry covers multiple EFI memory map regions\n");
10410f0d2f5SArd Biesheuvel 			return false;
10510f0d2f5SArd Biesheuvel 		}
10610f0d2f5SArd Biesheuvel 
10710f0d2f5SArd Biesheuvel 		if (md->type != in->type) {
10810f0d2f5SArd Biesheuvel 			pr_warn("Entry type deviates from EFI memory map region type\n");
10910f0d2f5SArd Biesheuvel 			return false;
11010f0d2f5SArd Biesheuvel 		}
11110f0d2f5SArd Biesheuvel 
11210f0d2f5SArd Biesheuvel 		out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
11310f0d2f5SArd Biesheuvel 
11410f0d2f5SArd Biesheuvel 		return true;
11510f0d2f5SArd Biesheuvel 	}
11610f0d2f5SArd Biesheuvel 
11710f0d2f5SArd Biesheuvel 	pr_warn("No matching entry found in the EFI memory map\n");
11810f0d2f5SArd Biesheuvel 	return false;
11910f0d2f5SArd Biesheuvel }
12010f0d2f5SArd Biesheuvel 
12110f0d2f5SArd Biesheuvel /*
12210f0d2f5SArd Biesheuvel  * To be called after the EFI page tables have been populated. If a memory
12310f0d2f5SArd Biesheuvel  * attributes table is available, its contents will be used to update the
12410f0d2f5SArd Biesheuvel  * mappings with tightened permissions as described by the table.
12510f0d2f5SArd Biesheuvel  * This requires the UEFI memory map to have already been populated with
12610f0d2f5SArd Biesheuvel  * virtual addresses.
12710f0d2f5SArd Biesheuvel  */
efi_memattr_apply_permissions(struct mm_struct * mm,efi_memattr_perm_setter fn)12810f0d2f5SArd Biesheuvel int __init efi_memattr_apply_permissions(struct mm_struct *mm,
12910f0d2f5SArd Biesheuvel 					 efi_memattr_perm_setter fn)
13010f0d2f5SArd Biesheuvel {
13110f0d2f5SArd Biesheuvel 	efi_memory_attributes_table_t *tbl;
132*cf1d2ffcSArd Biesheuvel 	bool has_bti = false;
13310f0d2f5SArd Biesheuvel 	int i, ret;
13410f0d2f5SArd Biesheuvel 
13510f0d2f5SArd Biesheuvel 	if (tbl_size <= sizeof(*tbl))
13610f0d2f5SArd Biesheuvel 		return 0;
13710f0d2f5SArd Biesheuvel 
13810f0d2f5SArd Biesheuvel 	/*
13910f0d2f5SArd Biesheuvel 	 * We need the EFI memory map to be setup so we can use it to
14010f0d2f5SArd Biesheuvel 	 * lookup the virtual addresses of all entries in the  of EFI
14110f0d2f5SArd Biesheuvel 	 * Memory Attributes table. If it isn't available, this
14210f0d2f5SArd Biesheuvel 	 * function should not be called.
14310f0d2f5SArd Biesheuvel 	 */
14410f0d2f5SArd Biesheuvel 	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
14510f0d2f5SArd Biesheuvel 		return 0;
14610f0d2f5SArd Biesheuvel 
147a17e809eSArd Biesheuvel 	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
14810f0d2f5SArd Biesheuvel 	if (!tbl) {
14910f0d2f5SArd Biesheuvel 		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
150a17e809eSArd Biesheuvel 		       efi_mem_attr_table);
15110f0d2f5SArd Biesheuvel 		return -ENOMEM;
15210f0d2f5SArd Biesheuvel 	}
15310f0d2f5SArd Biesheuvel 
154*cf1d2ffcSArd Biesheuvel 	if (tbl->version > 1 &&
155*cf1d2ffcSArd Biesheuvel 	    (tbl->flags & EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD))
156*cf1d2ffcSArd Biesheuvel 		has_bti = true;
157*cf1d2ffcSArd Biesheuvel 
15810f0d2f5SArd Biesheuvel 	if (efi_enabled(EFI_DBG))
15910f0d2f5SArd Biesheuvel 		pr_info("Processing EFI Memory Attributes table:\n");
16010f0d2f5SArd Biesheuvel 
16110f0d2f5SArd Biesheuvel 	for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
16210f0d2f5SArd Biesheuvel 		efi_memory_desc_t md;
16310f0d2f5SArd Biesheuvel 		unsigned long size;
16410f0d2f5SArd Biesheuvel 		bool valid;
16510f0d2f5SArd Biesheuvel 		char buf[64];
16610f0d2f5SArd Biesheuvel 
16710f0d2f5SArd Biesheuvel 		valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
16810f0d2f5SArd Biesheuvel 				       &md);
16910f0d2f5SArd Biesheuvel 		size = md.num_pages << EFI_PAGE_SHIFT;
17010f0d2f5SArd Biesheuvel 		if (efi_enabled(EFI_DBG) || !valid)
17110f0d2f5SArd Biesheuvel 			pr_info("%s 0x%012llx-0x%012llx %s\n",
17210f0d2f5SArd Biesheuvel 				valid ? "" : "!", md.phys_addr,
17310f0d2f5SArd Biesheuvel 				md.phys_addr + size - 1,
17410f0d2f5SArd Biesheuvel 				efi_md_typeattr_format(buf, sizeof(buf), &md));
17510f0d2f5SArd Biesheuvel 
17618141e89SSai Praneeth 		if (valid) {
177*cf1d2ffcSArd Biesheuvel 			ret = fn(mm, &md, has_bti);
17818141e89SSai Praneeth 			if (ret)
17918141e89SSai Praneeth 				pr_err("Error updating mappings, skipping subsequent md's\n");
18018141e89SSai Praneeth 		}
18110f0d2f5SArd Biesheuvel 	}
18210f0d2f5SArd Biesheuvel 	memunmap(tbl);
18310f0d2f5SArd Biesheuvel 	return ret;
18410f0d2f5SArd Biesheuvel }
185