xref: /openbmc/linux/arch/arm64/kernel/efi.c (revision ddc141e5)
1 /*
2  * Extensible Firmware Interface
3  *
4  * Based on Extensible Firmware Interface Specification version 2.4
5  *
6  * Copyright (C) 2013, 2014 Linaro Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13 
14 #include <linux/efi.h>
15 #include <linux/init.h>
16 
17 #include <asm/efi.h>
18 
19 /*
20  * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
21  * executable, everything else can be mapped with the XN bits
22  * set. Also take the new (optional) RO/XP bits into account.
23  */
24 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
25 {
26 	u64 attr = md->attribute;
27 	u32 type = md->type;
28 
29 	if (type == EFI_MEMORY_MAPPED_IO)
30 		return PROT_DEVICE_nGnRE;
31 
32 	if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
33 		      "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
34 		/*
35 		 * If the region is not aligned to the page size of the OS, we
36 		 * can not use strict permissions, since that would also affect
37 		 * the mapping attributes of the adjacent regions.
38 		 */
39 		return pgprot_val(PAGE_KERNEL_EXEC);
40 
41 	/* R-- */
42 	if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
43 	    (EFI_MEMORY_XP | EFI_MEMORY_RO))
44 		return pgprot_val(PAGE_KERNEL_RO);
45 
46 	/* R-X */
47 	if (attr & EFI_MEMORY_RO)
48 		return pgprot_val(PAGE_KERNEL_ROX);
49 
50 	/* RW- */
51 	if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
52 	     EFI_MEMORY_XP) ||
53 	    type != EFI_RUNTIME_SERVICES_CODE)
54 		return pgprot_val(PAGE_KERNEL);
55 
56 	/* RWX */
57 	return pgprot_val(PAGE_KERNEL_EXEC);
58 }
59 
60 /* we will fill this structure from the stub, so don't put it in .bss */
61 struct screen_info screen_info __section(.data);
62 
63 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
64 {
65 	pteval_t prot_val = create_mapping_protection(md);
66 	bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
67 				   md->type == EFI_RUNTIME_SERVICES_DATA);
68 
69 	if (!PAGE_ALIGNED(md->phys_addr) ||
70 	    !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
71 		/*
72 		 * If the end address of this region is not aligned to page
73 		 * size, the mapping is rounded up, and may end up sharing a
74 		 * page frame with the next UEFI memory region. If we create
75 		 * a block entry now, we may need to split it again when mapping
76 		 * the next region, and support for that is going to be removed
77 		 * from the MMU routines. So avoid block mappings altogether in
78 		 * that case.
79 		 */
80 		page_mappings_only = true;
81 	}
82 
83 	create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
84 			   md->num_pages << EFI_PAGE_SHIFT,
85 			   __pgprot(prot_val | PTE_NG), page_mappings_only);
86 	return 0;
87 }
88 
89 static int __init set_permissions(pte_t *ptep, pgtable_t token,
90 				  unsigned long addr, void *data)
91 {
92 	efi_memory_desc_t *md = data;
93 	pte_t pte = READ_ONCE(*ptep);
94 
95 	if (md->attribute & EFI_MEMORY_RO)
96 		pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
97 	if (md->attribute & EFI_MEMORY_XP)
98 		pte = set_pte_bit(pte, __pgprot(PTE_PXN));
99 	set_pte(ptep, pte);
100 	return 0;
101 }
102 
103 int __init efi_set_mapping_permissions(struct mm_struct *mm,
104 				       efi_memory_desc_t *md)
105 {
106 	BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
107 	       md->type != EFI_RUNTIME_SERVICES_DATA);
108 
109 	/*
110 	 * Calling apply_to_page_range() is only safe on regions that are
111 	 * guaranteed to be mapped down to pages. Since we are only called
112 	 * for regions that have been mapped using efi_create_mapping() above
113 	 * (and this is checked by the generic Memory Attributes table parsing
114 	 * routines), there is no need to check that again here.
115 	 */
116 	return apply_to_page_range(mm, md->virt_addr,
117 				   md->num_pages << EFI_PAGE_SHIFT,
118 				   set_permissions, md);
119 }
120 
121 /*
122  * UpdateCapsule() depends on the system being shutdown via
123  * ResetSystem().
124  */
125 bool efi_poweroff_required(void)
126 {
127 	return efi_enabled(EFI_RUNTIME_SERVICES);
128 }
129