| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Extensible Firmware Interface |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Based on Extensible Firmware Interface Specification version 2.4 |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * 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 | 8 | */ |
|---|
| 13 | 9 | |
|---|
| 14 | 10 | #include <linux/efi.h> |
|---|
| 15 | 11 | #include <linux/init.h> |
|---|
| 16 | 12 | |
|---|
| 17 | 13 | #include <asm/efi.h> |
|---|
| 14 | + |
|---|
| 15 | +static bool region_is_misaligned(const efi_memory_desc_t *md) |
|---|
| 16 | +{ |
|---|
| 17 | + if (PAGE_SIZE == EFI_PAGE_SIZE) |
|---|
| 18 | + return false; |
|---|
| 19 | + return !PAGE_ALIGNED(md->phys_addr) || |
|---|
| 20 | + !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT); |
|---|
| 21 | +} |
|---|
| 18 | 22 | |
|---|
| 19 | 23 | /* |
|---|
| 20 | 24 | * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be |
|---|
| .. | .. |
|---|
| 29 | 33 | if (type == EFI_MEMORY_MAPPED_IO) |
|---|
| 30 | 34 | return PROT_DEVICE_nGnRE; |
|---|
| 31 | 35 | |
|---|
| 32 | | - if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr), |
|---|
| 33 | | - "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?")) |
|---|
| 36 | + if (region_is_misaligned(md)) { |
|---|
| 37 | + static bool __initdata code_is_misaligned; |
|---|
| 38 | + |
|---|
| 34 | 39 | /* |
|---|
| 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. |
|---|
| 40 | + * Regions that are not aligned to the OS page size cannot be |
|---|
| 41 | + * mapped with strict permissions, as those might interfere |
|---|
| 42 | + * with the permissions that are needed by the adjacent |
|---|
| 43 | + * region's mapping. However, if we haven't encountered any |
|---|
| 44 | + * misaligned runtime code regions so far, we can safely use |
|---|
| 45 | + * non-executable permissions for non-code regions. |
|---|
| 38 | 46 | */ |
|---|
| 39 | | - return pgprot_val(PAGE_KERNEL_EXEC); |
|---|
| 47 | + code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE); |
|---|
| 48 | + |
|---|
| 49 | + return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC) |
|---|
| 50 | + : pgprot_val(PAGE_KERNEL); |
|---|
| 51 | + } |
|---|
| 40 | 52 | |
|---|
| 41 | 53 | /* R-- */ |
|---|
| 42 | 54 | if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) == |
|---|
| .. | .. |
|---|
| 58 | 70 | } |
|---|
| 59 | 71 | |
|---|
| 60 | 72 | /* we will fill this structure from the stub, so don't put it in .bss */ |
|---|
| 61 | | -struct screen_info screen_info __section(.data); |
|---|
| 73 | +struct screen_info screen_info __section(".data"); |
|---|
| 62 | 74 | |
|---|
| 63 | 75 | int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) |
|---|
| 64 | 76 | { |
|---|
| .. | .. |
|---|
| 66 | 78 | bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE || |
|---|
| 67 | 79 | md->type == EFI_RUNTIME_SERVICES_DATA); |
|---|
| 68 | 80 | |
|---|
| 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 | | - */ |
|---|
| 81 | + /* |
|---|
| 82 | + * If this region is not aligned to the page size used by the OS, the |
|---|
| 83 | + * mapping will be rounded outwards, and may end up sharing a page |
|---|
| 84 | + * frame with an adjacent runtime memory region. Given that the page |
|---|
| 85 | + * table descriptor covering the shared page will be rewritten when the |
|---|
| 86 | + * adjacent region gets mapped, we must avoid block mappings here so we |
|---|
| 87 | + * don't have to worry about splitting them when that happens. |
|---|
| 88 | + */ |
|---|
| 89 | + if (region_is_misaligned(md)) |
|---|
| 80 | 90 | page_mappings_only = true; |
|---|
| 81 | | - } |
|---|
| 82 | 91 | |
|---|
| 83 | 92 | create_pgd_mapping(mm, md->phys_addr, md->virt_addr, |
|---|
| 84 | 93 | md->num_pages << EFI_PAGE_SHIFT, |
|---|
| .. | .. |
|---|
| 86 | 95 | return 0; |
|---|
| 87 | 96 | } |
|---|
| 88 | 97 | |
|---|
| 89 | | -static int __init set_permissions(pte_t *ptep, pgtable_t token, |
|---|
| 90 | | - unsigned long addr, void *data) |
|---|
| 98 | +static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data) |
|---|
| 91 | 99 | { |
|---|
| 92 | 100 | efi_memory_desc_t *md = data; |
|---|
| 93 | 101 | pte_t pte = READ_ONCE(*ptep); |
|---|
| .. | .. |
|---|
| 105 | 113 | { |
|---|
| 106 | 114 | BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE && |
|---|
| 107 | 115 | md->type != EFI_RUNTIME_SERVICES_DATA); |
|---|
| 116 | + |
|---|
| 117 | + if (region_is_misaligned(md)) |
|---|
| 118 | + return 0; |
|---|
| 108 | 119 | |
|---|
| 109 | 120 | /* |
|---|
| 110 | 121 | * Calling apply_to_page_range() is only safe on regions that are |
|---|
| .. | .. |
|---|
| 132 | 143 | pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f); |
|---|
| 133 | 144 | return s; |
|---|
| 134 | 145 | } |
|---|
| 146 | + |
|---|
| 147 | +DEFINE_RAW_SPINLOCK(efi_rt_lock); |
|---|
| 148 | + |
|---|
| 149 | +asmlinkage u64 *efi_rt_stack_top __ro_after_init; |
|---|
| 150 | + |
|---|
| 151 | +/* EFI requires 8 KiB of stack space for runtime services */ |
|---|
| 152 | +static_assert(THREAD_SIZE >= SZ_8K); |
|---|
| 153 | + |
|---|
| 154 | +static int __init arm64_efi_rt_init(void) |
|---|
| 155 | +{ |
|---|
| 156 | + void *p; |
|---|
| 157 | + |
|---|
| 158 | + if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
|---|
| 159 | + return 0; |
|---|
| 160 | + |
|---|
| 161 | + p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL, |
|---|
| 162 | + NUMA_NO_NODE, &&l); |
|---|
| 163 | +l: if (!p) { |
|---|
| 164 | + pr_warn("Failed to allocate EFI runtime stack\n"); |
|---|
| 165 | + clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); |
|---|
| 166 | + return -ENOMEM; |
|---|
| 167 | + } |
|---|
| 168 | + |
|---|
| 169 | + efi_rt_stack_top = p + THREAD_SIZE; |
|---|
| 170 | + return 0; |
|---|
| 171 | +} |
|---|
| 172 | +core_initcall(arm64_efi_rt_init); |
|---|