Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Extensible Firmware Interface
0004  *
0005  * Based on Extensible Firmware Interface Specification version 2.4
0006  *
0007  * Copyright (C) 2013, 2014 Linaro Ltd.
0008  */
0009 
0010 #include <linux/efi.h>
0011 #include <linux/init.h>
0012 
0013 #include <asm/efi.h>
0014 
0015 /*
0016  * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
0017  * executable, everything else can be mapped with the XN bits
0018  * set. Also take the new (optional) RO/XP bits into account.
0019  */
0020 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
0021 {
0022     u64 attr = md->attribute;
0023     u32 type = md->type;
0024 
0025     if (type == EFI_MEMORY_MAPPED_IO)
0026         return PROT_DEVICE_nGnRE;
0027 
0028     if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
0029               "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
0030         /*
0031          * If the region is not aligned to the page size of the OS, we
0032          * can not use strict permissions, since that would also affect
0033          * the mapping attributes of the adjacent regions.
0034          */
0035         return pgprot_val(PAGE_KERNEL_EXEC);
0036 
0037     /* R-- */
0038     if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
0039         (EFI_MEMORY_XP | EFI_MEMORY_RO))
0040         return pgprot_val(PAGE_KERNEL_RO);
0041 
0042     /* R-X */
0043     if (attr & EFI_MEMORY_RO)
0044         return pgprot_val(PAGE_KERNEL_ROX);
0045 
0046     /* RW- */
0047     if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
0048          EFI_MEMORY_XP) ||
0049         type != EFI_RUNTIME_SERVICES_CODE)
0050         return pgprot_val(PAGE_KERNEL);
0051 
0052     /* RWX */
0053     return pgprot_val(PAGE_KERNEL_EXEC);
0054 }
0055 
0056 /* we will fill this structure from the stub, so don't put it in .bss */
0057 struct screen_info screen_info __section(".data");
0058 EXPORT_SYMBOL(screen_info);
0059 
0060 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
0061 {
0062     pteval_t prot_val = create_mapping_protection(md);
0063     bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
0064                    md->type == EFI_RUNTIME_SERVICES_DATA);
0065 
0066     if (!PAGE_ALIGNED(md->phys_addr) ||
0067         !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
0068         /*
0069          * If the end address of this region is not aligned to page
0070          * size, the mapping is rounded up, and may end up sharing a
0071          * page frame with the next UEFI memory region. If we create
0072          * a block entry now, we may need to split it again when mapping
0073          * the next region, and support for that is going to be removed
0074          * from the MMU routines. So avoid block mappings altogether in
0075          * that case.
0076          */
0077         page_mappings_only = true;
0078     }
0079 
0080     create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
0081                md->num_pages << EFI_PAGE_SHIFT,
0082                __pgprot(prot_val | PTE_NG), page_mappings_only);
0083     return 0;
0084 }
0085 
0086 static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
0087 {
0088     efi_memory_desc_t *md = data;
0089     pte_t pte = READ_ONCE(*ptep);
0090 
0091     if (md->attribute & EFI_MEMORY_RO)
0092         pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
0093     if (md->attribute & EFI_MEMORY_XP)
0094         pte = set_pte_bit(pte, __pgprot(PTE_PXN));
0095     set_pte(ptep, pte);
0096     return 0;
0097 }
0098 
0099 int __init efi_set_mapping_permissions(struct mm_struct *mm,
0100                        efi_memory_desc_t *md)
0101 {
0102     BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
0103            md->type != EFI_RUNTIME_SERVICES_DATA);
0104 
0105     /*
0106      * Calling apply_to_page_range() is only safe on regions that are
0107      * guaranteed to be mapped down to pages. Since we are only called
0108      * for regions that have been mapped using efi_create_mapping() above
0109      * (and this is checked by the generic Memory Attributes table parsing
0110      * routines), there is no need to check that again here.
0111      */
0112     return apply_to_page_range(mm, md->virt_addr,
0113                    md->num_pages << EFI_PAGE_SHIFT,
0114                    set_permissions, md);
0115 }
0116 
0117 /*
0118  * UpdateCapsule() depends on the system being shutdown via
0119  * ResetSystem().
0120  */
0121 bool efi_poweroff_required(void)
0122 {
0123     return efi_enabled(EFI_RUNTIME_SERVICES);
0124 }
0125 
0126 asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
0127 {
0128     pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
0129     return s;
0130 }