^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 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) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/mach/map.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) efi_memory_desc_t *md = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) pte_t pte = *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (md->attribute & EFI_MEMORY_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) pte = set_pte_bit(pte, __pgprot(L_PTE_RDONLY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (md->attribute & EFI_MEMORY_XP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) pte = set_pte_bit(pte, __pgprot(L_PTE_XN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) set_pte_ext(ptep, pte, PTE_EXT_NG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int __init efi_set_mapping_permissions(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long base, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) base = md->virt_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) size = md->num_pages << EFI_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * We can only use apply_to_page_range() if we can guarantee that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * entire region was mapped using pages. This should be the case if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * region does not cover any naturally aligned SECTION_SIZE sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (round_down(base + size, SECTION_SIZE) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) round_up(base, SECTION_SIZE) + SECTION_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return apply_to_page_range(mm, base, size, set_permissions, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct map_desc desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .virtual = md->virt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .pfn = __phys_to_pfn(md->phys_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .length = md->num_pages * EFI_PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Order is important here: memory regions may have all of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * bits below set (and usually do), so we check them in order of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * preference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (md->attribute & EFI_MEMORY_WB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) desc.type = MT_MEMORY_RWX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) else if (md->attribute & EFI_MEMORY_WT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) desc.type = MT_MEMORY_RWX_NONCACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) else if (md->attribute & EFI_MEMORY_WC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) desc.type = MT_DEVICE_WC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) desc.type = MT_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) create_mapping_late(mm, &desc, true);
^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 stricter permissions were specified, apply them now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (md->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return efi_set_mapping_permissions(mm, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }