blob: 78c8cf01db5f902bc2727f65ac6aaa0c92f8d9f6 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002/*
Christophe Leroyf381d572019-08-20 14:07:17 +00003 * This file contains pgtable related functions for 64-bit machines.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10004 *
5 * Derived from arch/ppc64/mm/init.c
6 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 *
8 * Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
9 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
10 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +100011 *
12 * Derived from "arch/i386/mm/init.c"
13 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
14 *
15 * Dave Engebretsen <engebret@us.ibm.com>
16 * Rework for PPC64 port.
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017 */
18
Paul Mackerras14cf11a2005-09-26 16:04:21 +100019#include <linux/signal.h>
20#include <linux/sched.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040024#include <linux/export.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025#include <linux/types.h>
26#include <linux/mman.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/stddef.h>
30#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Aneesh Kumar K.V06743522014-11-05 21:57:39 +053032#include <linux/hugetlb.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100033
Paul Mackerras14cf11a2005-09-26 16:04:21 +100034#include <asm/page.h>
35#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100036#include <asm/mmu_context.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037#include <asm/mmu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038#include <asm/smp.h>
39#include <asm/machdep.h>
40#include <asm/tlb.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100041#include <asm/processor.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042#include <asm/cputable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100043#include <asm/sections.h>
Stephen Rothwell5e203d62006-09-25 13:36:31 +100044#include <asm/firmware.h>
Anton Blanchard68cf0d62014-09-17 22:15:35 +100045#include <asm/dma.h>
David Gibson800fc3e2005-11-16 15:43:48 +110046
Christophe Leroy9d9f2cc2019-03-29 09:59:59 +000047#include <mm/mmu_decl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
Aneesh Kumar K.V78f1dbd2012-09-10 02:52:57 +000049
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +100050#ifdef CONFIG_PPC_BOOK3S_64
51/*
52 * partition table and process table for ISA 3.0
53 */
54struct prtb_entry *process_tb;
55struct patb_entry *partition_tb;
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +100056/*
57 * page table size
58 */
59unsigned long __pte_index_size;
60EXPORT_SYMBOL(__pte_index_size);
61unsigned long __pmd_index_size;
62EXPORT_SYMBOL(__pmd_index_size);
63unsigned long __pud_index_size;
64EXPORT_SYMBOL(__pud_index_size);
65unsigned long __pgd_index_size;
66EXPORT_SYMBOL(__pgd_index_size);
Aneesh Kumar K.Vfae22112018-02-11 20:30:06 +053067unsigned long __pud_cache_index;
68EXPORT_SYMBOL(__pud_cache_index);
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +100069unsigned long __pte_table_size;
70EXPORT_SYMBOL(__pte_table_size);
71unsigned long __pmd_table_size;
72EXPORT_SYMBOL(__pmd_table_size);
73unsigned long __pud_table_size;
74EXPORT_SYMBOL(__pud_table_size);
75unsigned long __pgd_table_size;
76EXPORT_SYMBOL(__pgd_table_size);
Aneesh Kumar K.Va2f41eb2016-04-29 23:26:19 +100077unsigned long __pmd_val_bits;
78EXPORT_SYMBOL(__pmd_val_bits);
79unsigned long __pud_val_bits;
80EXPORT_SYMBOL(__pud_val_bits);
81unsigned long __pgd_val_bits;
82EXPORT_SYMBOL(__pgd_val_bits);
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +100083unsigned long __kernel_virt_start;
84EXPORT_SYMBOL(__kernel_virt_start);
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +100085unsigned long __vmalloc_start;
86EXPORT_SYMBOL(__vmalloc_start);
87unsigned long __vmalloc_end;
88EXPORT_SYMBOL(__vmalloc_end);
Michael Ellerman63ee9b22017-08-01 20:29:22 +100089unsigned long __kernel_io_start;
90EXPORT_SYMBOL(__kernel_io_start);
Aneesh Kumar K.Va35a3c62019-04-17 18:29:13 +053091unsigned long __kernel_io_end;
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +100092struct page *vmemmap;
93EXPORT_SYMBOL(vmemmap);
Aneesh Kumar K.V5ed7ecd2016-04-29 23:26:23 +100094unsigned long __pte_frag_nr;
95EXPORT_SYMBOL(__pte_frag_nr);
96unsigned long __pte_frag_size_shift;
97EXPORT_SYMBOL(__pte_frag_size_shift);
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +100098#endif
Benjamin Herrenschmidta2450672009-07-23 23:15:16 +000099
Aneesh Kumar K.V06743522014-11-05 21:57:39 +0530100#ifndef __PAGETABLE_PUD_FOLDED
101/* 4 level page table */
Mike Rapoport2fb47062020-06-04 16:46:44 -0700102struct page *p4d_page(p4d_t p4d)
Aneesh Kumar K.V06743522014-11-05 21:57:39 +0530103{
Mike Rapoport2fb47062020-06-04 16:46:44 -0700104 if (p4d_is_leaf(p4d)) {
105 VM_WARN_ON(!p4d_huge(p4d));
106 return pte_page(p4d_pte(p4d));
Aneesh Kumar K.Vd6eaced2019-05-14 11:33:00 +0530107 }
Aneesh Kumar K.Vdc4875f2021-07-07 18:09:56 -0700108 return virt_to_page(p4d_pgtable(p4d));
Aneesh Kumar K.V06743522014-11-05 21:57:39 +0530109}
110#endif
111
112struct page *pud_page(pud_t pud)
113{
Aneesh Kumar K.Vd6eaced2019-05-14 11:33:00 +0530114 if (pud_is_leaf(pud)) {
115 VM_WARN_ON(!pud_huge(pud));
Aneesh Kumar K.V06743522014-11-05 21:57:39 +0530116 return pte_page(pud_pte(pud));
Aneesh Kumar K.Vd6eaced2019-05-14 11:33:00 +0530117 }
Aneesh Kumar K.V9cf6fa22021-07-07 18:09:53 -0700118 return virt_to_page(pud_pgtable(pud));
Aneesh Kumar K.V06743522014-11-05 21:57:39 +0530119}
120
Aneesh Kumar K.V074c2ea2013-06-20 14:30:15 +0530121/*
122 * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags
123 * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address.
124 */
125struct page *pmd_page(pmd_t pmd)
126{
Aneesh Kumar K.Vd6eaced2019-05-14 11:33:00 +0530127 if (pmd_is_leaf(pmd)) {
Aneesh Kumar K.V1ecf2cd2019-05-14 11:33:01 +0530128 VM_WARN_ON(!(pmd_large(pmd) || pmd_huge(pmd)));
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530129 return pte_page(pmd_pte(pmd));
Aneesh Kumar K.Vd6eaced2019-05-14 11:33:00 +0530130 }
Aneesh Kumar K.V074c2ea2013-06-20 14:30:15 +0530131 return virt_to_page(pmd_page_vaddr(pmd));
132}
133
Balbir Singhcd65d692017-06-29 03:04:08 +1000134#ifdef CONFIG_STRICT_KERNEL_RWX
135void mark_rodata_ro(void)
136{
137 if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) {
138 pr_warn("Warning: Unable to mark rodata read only on this CPU.\n");
139 return;
140 }
141
Balbir Singh7614ff32017-06-29 03:04:09 +1000142 if (radix_enabled())
143 radix__mark_rodata_ro();
144 else
Balbir Singhcd65d692017-06-29 03:04:08 +1000145 hash__mark_rodata_ro();
Russell Currey453d87f2019-05-02 17:39:47 +1000146
147 // mark_initmem_nx() should have already run by now
148 ptdump_check_wx();
Balbir Singhcd65d692017-06-29 03:04:08 +1000149}
Michael Ellerman029d9252017-07-14 16:51:23 +1000150
151void mark_initmem_nx(void)
152{
153 if (radix_enabled())
154 radix__mark_initmem_nx();
155 else
156 hash__mark_initmem_nx();
157}
Balbir Singhcd65d692017-06-29 03:04:08 +1000158#endif