Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  PowerPC version
0004  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
0005  *
0006  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
0007  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
0008  *    Copyright (C) 1996 Paul Mackerras
0009  *
0010  *  Derived from "arch/i386/mm/init.c"
0011  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
0012  *
0013  *  Dave Engebretsen <engebret@us.ibm.com>
0014  *      Rework for PPC64 port.
0015  */
0016 
0017 #undef DEBUG
0018 
0019 #include <linux/string.h>
0020 #include <linux/pgtable.h>
0021 #include <asm/pgalloc.h>
0022 #include <asm/kup.h>
0023 #include <asm/smp.h>
0024 
0025 phys_addr_t memstart_addr __ro_after_init = (phys_addr_t)~0ull;
0026 EXPORT_SYMBOL_GPL(memstart_addr);
0027 phys_addr_t kernstart_addr __ro_after_init;
0028 EXPORT_SYMBOL_GPL(kernstart_addr);
0029 unsigned long kernstart_virt_addr __ro_after_init = KERNELBASE;
0030 EXPORT_SYMBOL_GPL(kernstart_virt_addr);
0031 
0032 bool disable_kuep = !IS_ENABLED(CONFIG_PPC_KUEP);
0033 bool disable_kuap = !IS_ENABLED(CONFIG_PPC_KUAP);
0034 
0035 static int __init parse_nosmep(char *p)
0036 {
0037     if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
0038         return 0;
0039 
0040     disable_kuep = true;
0041     pr_warn("Disabling Kernel Userspace Execution Prevention\n");
0042     return 0;
0043 }
0044 early_param("nosmep", parse_nosmep);
0045 
0046 static int __init parse_nosmap(char *p)
0047 {
0048     disable_kuap = true;
0049     pr_warn("Disabling Kernel Userspace Access Protection\n");
0050     return 0;
0051 }
0052 early_param("nosmap", parse_nosmap);
0053 
0054 void __weak setup_kuep(bool disabled)
0055 {
0056     if (!IS_ENABLED(CONFIG_PPC_KUEP) || disabled)
0057         return;
0058 
0059     if (smp_processor_id() != boot_cpuid)
0060         return;
0061 
0062     pr_info("Activating Kernel Userspace Execution Prevention\n");
0063 }
0064 
0065 void setup_kup(void)
0066 {
0067     setup_kuap(disable_kuap);
0068     setup_kuep(disable_kuep);
0069 }
0070 
0071 #define CTOR(shift) static void ctor_##shift(void *addr) \
0072 {                           \
0073     memset(addr, 0, sizeof(void *) << (shift)); \
0074 }
0075 
0076 CTOR(0); CTOR(1); CTOR(2); CTOR(3); CTOR(4); CTOR(5); CTOR(6); CTOR(7);
0077 CTOR(8); CTOR(9); CTOR(10); CTOR(11); CTOR(12); CTOR(13); CTOR(14); CTOR(15);
0078 
0079 static inline void (*ctor(int shift))(void *)
0080 {
0081     BUILD_BUG_ON(MAX_PGTABLE_INDEX_SIZE != 15);
0082 
0083     switch (shift) {
0084     case 0: return ctor_0;
0085     case 1: return ctor_1;
0086     case 2: return ctor_2;
0087     case 3: return ctor_3;
0088     case 4: return ctor_4;
0089     case 5: return ctor_5;
0090     case 6: return ctor_6;
0091     case 7: return ctor_7;
0092     case 8: return ctor_8;
0093     case 9: return ctor_9;
0094     case 10: return ctor_10;
0095     case 11: return ctor_11;
0096     case 12: return ctor_12;
0097     case 13: return ctor_13;
0098     case 14: return ctor_14;
0099     case 15: return ctor_15;
0100     }
0101     return NULL;
0102 }
0103 
0104 struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE + 1];
0105 EXPORT_SYMBOL_GPL(pgtable_cache);   /* used by kvm_hv module */
0106 
0107 /*
0108  * Create a kmem_cache() for pagetables.  This is not used for PTE
0109  * pages - they're linked to struct page, come from the normal free
0110  * pages pool and have a different entry size (see real_pte_t) to
0111  * everything else.  Caches created by this function are used for all
0112  * the higher level pagetables, and for hugepage pagetables.
0113  */
0114 void pgtable_cache_add(unsigned int shift)
0115 {
0116     char *name;
0117     unsigned long table_size = sizeof(void *) << shift;
0118     unsigned long align = table_size;
0119 
0120     /* When batching pgtable pointers for RCU freeing, we store
0121      * the index size in the low bits.  Table alignment must be
0122      * big enough to fit it.
0123      *
0124      * Likewise, hugeapge pagetable pointers contain a (different)
0125      * shift value in the low bits.  All tables must be aligned so
0126      * as to leave enough 0 bits in the address to contain it. */
0127     unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1,
0128                      HUGEPD_SHIFT_MASK + 1);
0129     struct kmem_cache *new;
0130 
0131     /* It would be nice if this was a BUILD_BUG_ON(), but at the
0132      * moment, gcc doesn't seem to recognize is_power_of_2 as a
0133      * constant expression, so so much for that. */
0134     BUG_ON(!is_power_of_2(minalign));
0135     BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
0136 
0137     if (PGT_CACHE(shift))
0138         return; /* Already have a cache of this size */
0139 
0140     align = max_t(unsigned long, align, minalign);
0141     name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
0142     new = kmem_cache_create(name, table_size, align, 0, ctor(shift));
0143     if (!new)
0144         panic("Could not allocate pgtable cache for order %d", shift);
0145 
0146     kfree(name);
0147     pgtable_cache[shift] = new;
0148 
0149     pr_debug("Allocated pgtable cache for order %d\n", shift);
0150 }
0151 EXPORT_SYMBOL_GPL(pgtable_cache_add);   /* used by kvm_hv module */
0152 
0153 void pgtable_cache_init(void)
0154 {
0155     pgtable_cache_add(PGD_INDEX_SIZE);
0156 
0157     if (PMD_CACHE_INDEX)
0158         pgtable_cache_add(PMD_CACHE_INDEX);
0159     /*
0160      * In all current configs, when the PUD index exists it's the
0161      * same size as either the pgd or pmd index except with THP enabled
0162      * on book3s 64
0163      */
0164     if (PUD_CACHE_INDEX)
0165         pgtable_cache_add(PUD_CACHE_INDEX);
0166 }