2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* This file contains the routines for handling the MMU on those
|
|
|
|
* PowerPC implementations where the MMU substantially follows the
|
|
|
|
* architecture specification. This includes the 6xx, 7xx, 7xxx,
|
2014-07-10 10:29:24 +08:00
|
|
|
* and 8260 implementations but excludes the 8xx and 4xx.
|
2005-09-26 14:04:21 +08:00
|
|
|
* -- paulus
|
|
|
|
*
|
|
|
|
* Derived from arch/ppc/mm/init.c:
|
|
|
|
* Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
|
|
|
|
*
|
|
|
|
* Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
|
|
|
|
* and Cort Dougan (PReP) (cort@cs.nmt.edu)
|
|
|
|
* Copyright (C) 1996 Paul Mackerras
|
|
|
|
*
|
|
|
|
* Derived from "arch/i386/mm/init.c"
|
|
|
|
* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/highmem.h>
|
2010-07-12 12:36:09 +08:00
|
|
|
#include <linux/memblock.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
#include <asm/prom.h>
|
|
|
|
#include <asm/mmu.h>
|
|
|
|
#include <asm/machdep.h>
|
2018-11-10 01:33:24 +08:00
|
|
|
#include <asm/code-patching.h>
|
2019-02-22 03:08:49 +08:00
|
|
|
#include <asm/sections.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2019-03-29 17:59:59 +08:00
|
|
|
#include <mm/mmu_decl.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2020-10-01 23:35:38 +08:00
|
|
|
u8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0};
|
|
|
|
|
2020-10-22 14:29:32 +08:00
|
|
|
static struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash;
|
|
|
|
static unsigned long __initdata Hash_size, Hash_mask;
|
|
|
|
static unsigned int __initdata hash_mb, hash_mb2;
|
|
|
|
unsigned long __initdata _SDR1;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2008-06-14 07:41:43 +08:00
|
|
|
struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2020-10-22 14:29:29 +08:00
|
|
|
static struct batrange { /* stores address ranges mapped by BATs */
|
2005-09-26 14:04:21 +08:00
|
|
|
unsigned long start;
|
|
|
|
unsigned long limit;
|
2008-06-14 07:41:42 +08:00
|
|
|
phys_addr_t phys;
|
2006-06-18 06:52:44 +08:00
|
|
|
} bat_addrs[8];
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2020-10-22 14:29:35 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
unsigned long mmu_hash_lock;
|
|
|
|
#endif
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Return PA for this VA if it is mapped by a BAT, or 0
|
|
|
|
*/
|
2016-02-10 00:07:58 +08:00
|
|
|
phys_addr_t v_block_mapped(unsigned long va)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
|
|
|
int b;
|
2018-11-17 01:27:42 +08:00
|
|
|
for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
|
2005-09-26 14:04:21 +08:00
|
|
|
if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
|
|
|
|
return bat_addrs[b].phys + (va - bat_addrs[b].start);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return VA for a given PA or 0 if not mapped
|
|
|
|
*/
|
2016-02-10 00:07:58 +08:00
|
|
|
unsigned long p_block_mapped(phys_addr_t pa)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
|
|
|
int b;
|
2018-11-17 01:27:42 +08:00
|
|
|
for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
|
2005-09-26 14:04:21 +08:00
|
|
|
if (pa >= bat_addrs[b].phys
|
|
|
|
&& pa < (bat_addrs[b].limit-bat_addrs[b].start)
|
|
|
|
+bat_addrs[b].phys)
|
|
|
|
return bat_addrs[b].start+(pa-bat_addrs[b].phys);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-10 23:29:25 +08:00
|
|
|
int __init find_free_bat(void)
|
2019-02-22 03:08:39 +08:00
|
|
|
{
|
|
|
|
int b;
|
2020-09-29 14:48:37 +08:00
|
|
|
int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
|
2019-02-22 03:08:39 +08:00
|
|
|
|
2020-09-29 14:48:37 +08:00
|
|
|
for (b = 0; b < n; b++) {
|
|
|
|
struct ppc_bat *bat = BATS[b];
|
2019-02-22 03:08:39 +08:00
|
|
|
|
2020-09-29 14:48:37 +08:00
|
|
|
if (!(bat[1].batu & 3))
|
|
|
|
return b;
|
2019-02-22 03:08:39 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:11:59 +08:00
|
|
|
/*
|
|
|
|
* This function calculates the size of the larger block usable to map the
|
|
|
|
* beginning of an area based on the start address and size of that area:
|
2020-09-29 14:48:36 +08:00
|
|
|
* - max block size is 256 on 6xx.
|
2019-05-01 00:11:59 +08:00
|
|
|
* - base address must be aligned to the block size. So the maximum block size
|
|
|
|
* is identified by the lowest bit set to 1 in the base address (for instance
|
|
|
|
* if base is 0x16000000, max size is 0x02000000).
|
|
|
|
* - block size has to be a power of two. This is calculated by finding the
|
|
|
|
* highest bit set to 1.
|
|
|
|
*/
|
2022-01-10 23:29:25 +08:00
|
|
|
unsigned int bat_block_size(unsigned long base, unsigned long top)
|
2019-02-22 03:08:39 +08:00
|
|
|
{
|
2020-09-29 14:48:36 +08:00
|
|
|
unsigned int max_size = SZ_256M;
|
2019-05-01 00:11:59 +08:00
|
|
|
unsigned int base_shift = (ffs(base) - 1) & 31;
|
2019-02-22 03:08:39 +08:00
|
|
|
unsigned int block_shift = (fls(top - base) - 1) & 31;
|
|
|
|
|
|
|
|
return min3(max_size, 1U << base_shift, 1U << block_shift);
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:08:48 +08:00
|
|
|
/*
|
|
|
|
* Set up one of the IBAT (block address translation) register pairs.
|
|
|
|
* The parameters are not checked; in particular size must be a power
|
|
|
|
* of 2 between 128k and 256M.
|
|
|
|
*/
|
|
|
|
static void setibat(int index, unsigned long virt, phys_addr_t phys,
|
|
|
|
unsigned int size, pgprot_t prot)
|
|
|
|
{
|
|
|
|
unsigned int bl = (size >> 17) - 1;
|
|
|
|
int wimgxpp;
|
|
|
|
struct ppc_bat *bat = BATS[index];
|
|
|
|
unsigned long flags = pgprot_val(prot);
|
|
|
|
|
|
|
|
if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
|
|
|
|
flags &= ~_PAGE_COHERENT;
|
|
|
|
|
|
|
|
wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
|
|
|
|
bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
|
|
|
|
bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
|
|
|
|
if (flags & _PAGE_USER)
|
|
|
|
bat[0].batu |= 1; /* Vp = 1 */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clearibat(int index)
|
|
|
|
{
|
|
|
|
struct ppc_bat *bat = BATS[index];
|
|
|
|
|
|
|
|
bat[0].batu = 0;
|
|
|
|
bat[0].batl = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:08:49 +08:00
|
|
|
static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
2019-02-22 03:08:39 +08:00
|
|
|
int idx;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2019-02-22 03:08:39 +08:00
|
|
|
while ((idx = find_free_bat()) != -1 && base != top) {
|
2022-01-10 23:29:25 +08:00
|
|
|
unsigned int size = bat_block_size(base, top);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2019-02-22 03:08:39 +08:00
|
|
|
if (size < 128 << 10)
|
2005-09-26 14:04:21 +08:00
|
|
|
break;
|
2019-02-22 03:08:39 +08:00
|
|
|
setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
|
|
|
|
base += size;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:08:39 +08:00
|
|
|
return base;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:08:49 +08:00
|
|
|
unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
|
|
|
|
{
|
2019-05-01 00:11:59 +08:00
|
|
|
unsigned long done;
|
2019-02-22 03:08:49 +08:00
|
|
|
unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
|
|
|
|
|
2020-11-25 15:10:46 +08:00
|
|
|
|
2021-03-04 22:35:09 +08:00
|
|
|
if (debug_pagealloc_enabled_or_kfence() || __map_without_bats) {
|
2020-11-25 15:10:46 +08:00
|
|
|
pr_debug_once("Read-Write memory mapped without BATs\n");
|
2020-05-19 13:49:28 +08:00
|
|
|
if (base >= border)
|
|
|
|
return base;
|
|
|
|
if (top >= border)
|
|
|
|
top = border;
|
|
|
|
}
|
2019-02-22 03:08:49 +08:00
|
|
|
|
|
|
|
if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
|
|
|
|
return __mmu_mapin_ram(base, top);
|
|
|
|
|
|
|
|
done = __mmu_mapin_ram(base, border);
|
2019-05-01 00:11:59 +08:00
|
|
|
if (done != border)
|
2019-02-22 03:08:49 +08:00
|
|
|
return done;
|
|
|
|
|
2019-05-01 00:11:59 +08:00
|
|
|
return __mmu_mapin_ram(border, top);
|
2019-02-22 03:08:49 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:15:22 +08:00
|
|
|
static bool is_module_segment(unsigned long addr)
|
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_MODULES))
|
|
|
|
return false;
|
2020-08-05 23:27:28 +08:00
|
|
|
if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M))
|
|
|
|
return false;
|
2020-08-21 15:15:25 +08:00
|
|
|
if (addr > ALIGN(MODULES_END, SZ_256M) - 1)
|
2020-08-05 23:27:28 +08:00
|
|
|
return false;
|
2020-06-29 19:15:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:08:49 +08:00
|
|
|
void mmu_mark_initmem_nx(void)
|
|
|
|
{
|
|
|
|
int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
|
|
|
|
int i;
|
|
|
|
unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
|
2021-11-26 20:40:35 +08:00
|
|
|
unsigned long top = ALIGN((unsigned long)_etext - PAGE_OFFSET, SZ_128K);
|
2020-05-19 13:48:56 +08:00
|
|
|
unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
|
2019-02-22 03:08:49 +08:00
|
|
|
unsigned long size;
|
|
|
|
|
2021-11-26 20:40:35 +08:00
|
|
|
for (i = 0; i < nb - 1 && base < top;) {
|
2022-01-10 23:29:25 +08:00
|
|
|
size = bat_block_size(base, top);
|
2019-02-22 03:08:49 +08:00
|
|
|
setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
|
|
|
|
base += size;
|
|
|
|
}
|
|
|
|
if (base < top) {
|
2022-01-10 23:29:25 +08:00
|
|
|
size = bat_block_size(base, top);
|
2019-02-22 03:08:49 +08:00
|
|
|
if ((top - base) > size) {
|
|
|
|
size <<= 1;
|
2020-05-19 13:48:56 +08:00
|
|
|
if (strict_kernel_rwx_enabled() && base + size > border)
|
|
|
|
pr_warn("Some RW data is getting mapped X. "
|
|
|
|
"Adjust CONFIG_DATA_SHIFT to avoid that.\n");
|
2019-02-22 03:08:49 +08:00
|
|
|
}
|
|
|
|
setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
|
|
|
|
base += size;
|
|
|
|
}
|
|
|
|
for (; i < nb; i++)
|
|
|
|
clearibat(i);
|
|
|
|
|
|
|
|
update_bats();
|
|
|
|
|
|
|
|
for (i = TASK_SIZE >> 28; i < 16; i++) {
|
|
|
|
/* Do not set NX on VM space for modules */
|
2020-06-29 19:15:22 +08:00
|
|
|
if (is_module_segment(i << 28))
|
|
|
|
continue;
|
|
|
|
|
2021-02-06 19:47:27 +08:00
|
|
|
mtsr(mfsr(i << 28) | 0x10000000, i << 28);
|
2019-02-22 03:08:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_mark_rodata_ro(void)
|
|
|
|
{
|
|
|
|
int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nb; i++) {
|
|
|
|
struct ppc_bat *bat = BATS[i];
|
|
|
|
|
|
|
|
if (bat_addrs[i].start < (unsigned long)__init_begin)
|
|
|
|
bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_bats();
|
|
|
|
}
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Set up one of the I/D BAT (block address translation) register pairs.
|
|
|
|
* The parameters are not checked; in particular size must be a power
|
|
|
|
* of 2 between 128k and 256M.
|
2019-02-22 03:08:43 +08:00
|
|
|
* On 603+, only set IBAT when _PAGE_EXEC is set
|
2005-09-26 14:04:21 +08:00
|
|
|
*/
|
2008-06-14 07:41:42 +08:00
|
|
|
void __init setbat(int index, unsigned long virt, phys_addr_t phys,
|
2015-03-25 17:11:55 +08:00
|
|
|
unsigned int size, pgprot_t prot)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
|
|
|
unsigned int bl;
|
|
|
|
int wimgxpp;
|
2019-09-17 04:25:39 +08:00
|
|
|
struct ppc_bat *bat;
|
2015-03-25 17:11:55 +08:00
|
|
|
unsigned long flags = pgprot_val(prot);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2019-09-17 04:25:39 +08:00
|
|
|
if (index == -1)
|
|
|
|
index = find_free_bat();
|
|
|
|
if (index == -1) {
|
|
|
|
pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
|
|
|
|
(unsigned long long)phys);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bat = BATS[index];
|
|
|
|
|
2009-01-23 14:51:28 +08:00
|
|
|
if ((flags & _PAGE_NO_CACHE) ||
|
|
|
|
(cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
|
|
|
|
flags &= ~_PAGE_COHERENT;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
bl = (size >> 17) - 1;
|
2020-09-29 14:48:37 +08:00
|
|
|
/* Do DBAT first */
|
|
|
|
wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
|
|
|
|
| _PAGE_COHERENT | _PAGE_GUARDED);
|
|
|
|
wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
|
|
|
|
bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
|
|
|
|
bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
|
|
|
|
if (flags & _PAGE_USER)
|
|
|
|
bat[1].batu |= 1; /* Vp = 1 */
|
|
|
|
if (flags & _PAGE_GUARDED) {
|
|
|
|
/* G bit must be zero in IBATs */
|
|
|
|
flags &= ~_PAGE_EXEC;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
2020-09-29 14:48:37 +08:00
|
|
|
if (flags & _PAGE_EXEC)
|
|
|
|
bat[0] = bat[1];
|
|
|
|
else
|
|
|
|
bat[0].batu = bat[0].batl = 0;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
bat_addrs[index].start = virt;
|
|
|
|
bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
|
|
|
|
bat_addrs[index].phys = phys;
|
|
|
|
}
|
|
|
|
|
2005-11-07 08:06:55 +08:00
|
|
|
/*
|
|
|
|
* Preload a translation in the hash table
|
|
|
|
*/
|
2020-11-25 15:10:47 +08:00
|
|
|
static void hash_preload(struct mm_struct *mm, unsigned long ea)
|
2005-11-07 08:06:55 +08:00
|
|
|
{
|
|
|
|
pmd_t *pmd;
|
|
|
|
|
2020-10-22 14:29:30 +08:00
|
|
|
if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
|
2005-11-07 08:06:55 +08:00
|
|
|
return;
|
2020-06-09 12:33:05 +08:00
|
|
|
pmd = pmd_off(mm, ea);
|
2005-11-07 08:06:55 +08:00
|
|
|
if (!pmd_none(*pmd))
|
2006-06-11 12:15:17 +08:00
|
|
|
add_hash_page(mm->context.id, ea, pmd_val(*pmd));
|
2005-11-07 08:06:55 +08:00
|
|
|
}
|
|
|
|
|
2019-08-16 13:41:42 +08:00
|
|
|
/*
|
|
|
|
* This is called at the end of handling a user page fault, when the
|
|
|
|
* fault has been handled by updating a PTE in the linux page tables.
|
|
|
|
* We use it to preload an HPTE into the hash table corresponding to
|
|
|
|
* the updated linux PTE.
|
|
|
|
*
|
|
|
|
* This must always be called with the pte lock held.
|
|
|
|
*/
|
|
|
|
void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
|
|
|
|
pte_t *ptep)
|
|
|
|
{
|
2019-08-16 13:41:44 +08:00
|
|
|
if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
|
|
|
|
return;
|
2019-08-16 13:41:42 +08:00
|
|
|
/*
|
|
|
|
* We don't need to worry about _PAGE_PRESENT here because we are
|
|
|
|
* called with either mm->page_table_lock held or ptl lock held
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
|
|
|
|
if (!pte_young(*ptep) || address >= TASK_SIZE)
|
|
|
|
return;
|
|
|
|
|
2019-08-16 13:41:43 +08:00
|
|
|
/* We have to test for regs NULL since init will get here first thing at boot */
|
|
|
|
if (!current->thread.regs)
|
|
|
|
return;
|
2019-08-16 13:41:42 +08:00
|
|
|
|
2019-08-16 13:41:43 +08:00
|
|
|
/* We also avoid filling the hash if not coming from a fault */
|
|
|
|
if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
|
2019-08-16 13:41:42 +08:00
|
|
|
return;
|
|
|
|
|
2019-08-16 13:41:43 +08:00
|
|
|
hash_preload(vma->vm_mm, address);
|
2019-08-16 13:41:42 +08:00
|
|
|
}
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Initialize the hash table and patch the instructions in hashtable.S.
|
|
|
|
*/
|
|
|
|
void __init MMU_init_hw(void)
|
|
|
|
{
|
|
|
|
unsigned int n_hpteg, lg_n_hpteg;
|
|
|
|
|
2018-11-10 01:33:22 +08:00
|
|
|
if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
|
2005-09-26 14:04:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
|
|
|
|
|
|
|
|
#define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
|
|
|
|
#define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
|
|
|
|
#define MIN_N_HPTEG 1024 /* min 64kB hash table */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow 1 HPTE (1/8 HPTEG) for each page of memory.
|
|
|
|
* This is less than the recommended amount, but then
|
|
|
|
* Linux ain't AIX.
|
|
|
|
*/
|
|
|
|
n_hpteg = total_memory / (PAGE_SIZE * 8);
|
|
|
|
if (n_hpteg < MIN_N_HPTEG)
|
|
|
|
n_hpteg = MIN_N_HPTEG;
|
|
|
|
lg_n_hpteg = __ilog2(n_hpteg);
|
|
|
|
if (n_hpteg & (n_hpteg - 1)) {
|
|
|
|
++lg_n_hpteg; /* round up if not power of 2 */
|
|
|
|
n_hpteg = 1 << lg_n_hpteg;
|
|
|
|
}
|
|
|
|
Hash_size = n_hpteg << LG_HPTEG_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find some memory for the hash table.
|
|
|
|
*/
|
|
|
|
if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
|
2019-03-08 08:31:06 +08:00
|
|
|
Hash = memblock_alloc(Hash_size, Hash_size);
|
2019-03-12 14:30:31 +08:00
|
|
|
if (!Hash)
|
|
|
|
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
|
|
|
|
__func__, Hash_size, Hash_size);
|
2005-09-26 14:04:21 +08:00
|
|
|
_SDR1 = __pa(Hash) | SDR1_LOW_BITS;
|
|
|
|
|
2019-04-27 00:36:37 +08:00
|
|
|
pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
|
|
|
|
(unsigned long long)(total_memory >> 20), Hash_size >> 10);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
Hash_mask = n_hpteg - 1;
|
2019-04-27 00:23:35 +08:00
|
|
|
hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
|
2005-09-26 14:04:21 +08:00
|
|
|
if (lg_n_hpteg > 16)
|
2019-04-27 00:23:35 +08:00
|
|
|
hash_mb2 = 16 - LG_HPTEG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init MMU_init_hw_patch(void)
|
|
|
|
{
|
|
|
|
unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
|
powerpc/32s: Fix DSI and ISI exceptions for CONFIG_VMAP_STACK
hash_page() needs to read page tables from kernel memory. When entire
kernel memory is mapped by BATs, which is normally the case when
CONFIG_STRICT_KERNEL_RWX is not set, it works even if the page hosting
the page table is not referenced in the MMU hash table.
However, if the page where the page table resides is not covered by
a BAT, a DSI fault can be encountered from hash_page(), and it loops
forever. This can happen when CONFIG_STRICT_KERNEL_RWX is selected
and the alignment of the different regions is too small to allow
covering the entire memory with BATs. This also happens when
CONFIG_DEBUG_PAGEALLOC is selected or when booting with 'nobats'
flag.
Also, if the page containing the kernel stack is not present in the
MMU hash table, registers cannot be saved and a recursive DSI fault
is encountered.
To allow hash_page() to properly do its job at all time and load the
MMU hash table whenever needed, it must run with data MMU disabled.
This means it must be called before re-enabling data MMU. To allow
this, registers clobbered by hash_page() and create_hpte() have to
be saved in the thread struct together with SRR0, SSR1, DAR and DSISR.
It is also necessary to ensure that DSI prolog doesn't overwrite
regs saved by prolog of the current running exception. That means:
- DSI can only use SPRN_SPRG_SCRATCH0
- Exceptions must free SPRN_SPRG_SCRATCH0 before writing to the stack.
This also fixes the Oops reported by Erhard when create_hpte() is
called by add_hash_page().
Due to prolog size increase, a few more exceptions had to get split
in two parts.
Fixes: cd08f109e262 ("powerpc/32s: Enable CONFIG_VMAP_STACK")
Reported-by: Erhard F. <erhard_f@mailbox.org>
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Tested-by: Erhard F. <erhard_f@mailbox.org>
Tested-by: Larry Finger <Larry.Finger@lwfinger.net>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=206501
Link: https://lore.kernel.org/r/64a4aa44686e9fd4b01333401367029771d9b231.1581761633.git.christophe.leroy@c-s.fr
2020-02-15 18:14:25 +08:00
|
|
|
unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2020-10-01 23:35:38 +08:00
|
|
|
if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
|
|
|
|
return;
|
|
|
|
|
2019-04-27 00:23:35 +08:00
|
|
|
if (ppc_md.progress)
|
|
|
|
ppc_md.progress("hash:patch", 0x345);
|
|
|
|
if (ppc_md.progress)
|
|
|
|
ppc_md.progress("hash:done", 0x205);
|
|
|
|
|
|
|
|
/* WARNING: Make sure nothing can trigger a KASAN check past this point */
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Patch up the instructions in hashtable.S:create_hpte
|
|
|
|
*/
|
2019-12-21 16:32:38 +08:00
|
|
|
modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
|
2019-04-27 00:23:35 +08:00
|
|
|
modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
|
|
|
|
modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
|
2018-11-10 01:33:24 +08:00
|
|
|
modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
|
|
|
|
modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Patch up the instructions in hashtable.S:flush_hash_page
|
|
|
|
*/
|
powerpc/32s: Fix DSI and ISI exceptions for CONFIG_VMAP_STACK
hash_page() needs to read page tables from kernel memory. When entire
kernel memory is mapped by BATs, which is normally the case when
CONFIG_STRICT_KERNEL_RWX is not set, it works even if the page hosting
the page table is not referenced in the MMU hash table.
However, if the page where the page table resides is not covered by
a BAT, a DSI fault can be encountered from hash_page(), and it loops
forever. This can happen when CONFIG_STRICT_KERNEL_RWX is selected
and the alignment of the different regions is too small to allow
covering the entire memory with BATs. This also happens when
CONFIG_DEBUG_PAGEALLOC is selected or when booting with 'nobats'
flag.
Also, if the page containing the kernel stack is not present in the
MMU hash table, registers cannot be saved and a recursive DSI fault
is encountered.
To allow hash_page() to properly do its job at all time and load the
MMU hash table whenever needed, it must run with data MMU disabled.
This means it must be called before re-enabling data MMU. To allow
this, registers clobbered by hash_page() and create_hpte() have to
be saved in the thread struct together with SRR0, SSR1, DAR and DSISR.
It is also necessary to ensure that DSI prolog doesn't overwrite
regs saved by prolog of the current running exception. That means:
- DSI can only use SPRN_SPRG_SCRATCH0
- Exceptions must free SPRN_SPRG_SCRATCH0 before writing to the stack.
This also fixes the Oops reported by Erhard when create_hpte() is
called by add_hash_page().
Due to prolog size increase, a few more exceptions had to get split
in two parts.
Fixes: cd08f109e262 ("powerpc/32s: Enable CONFIG_VMAP_STACK")
Reported-by: Erhard F. <erhard_f@mailbox.org>
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Tested-by: Erhard F. <erhard_f@mailbox.org>
Tested-by: Larry Finger <Larry.Finger@lwfinger.net>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=206501
Link: https://lore.kernel.org/r/64a4aa44686e9fd4b01333401367029771d9b231.1581761633.git.christophe.leroy@c-s.fr
2020-02-15 18:14:25 +08:00
|
|
|
modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
|
2019-04-27 00:23:35 +08:00
|
|
|
modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
|
|
|
|
modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
|
2018-11-10 01:33:24 +08:00
|
|
|
modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
2010-07-07 06:39:02 +08:00
|
|
|
|
|
|
|
void setup_initial_memory_limit(phys_addr_t first_memblock_base,
|
|
|
|
phys_addr_t first_memblock_size)
|
|
|
|
{
|
|
|
|
/* We don't currently support the first MEMBLOCK not mapping 0
|
|
|
|
* physical on those processors
|
|
|
|
*/
|
|
|
|
BUG_ON(first_memblock_base != 0);
|
|
|
|
|
2020-09-29 14:48:36 +08:00
|
|
|
memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M));
|
2010-07-07 06:39:02 +08:00
|
|
|
}
|
2019-03-11 16:30:35 +08:00
|
|
|
|
2019-04-27 00:36:39 +08:00
|
|
|
void __init print_system_hash_info(void)
|
|
|
|
{
|
|
|
|
pr_info("Hash_size = 0x%lx\n", Hash_size);
|
|
|
|
if (Hash_mask)
|
|
|
|
pr_info("Hash_mask = 0x%lx\n", Hash_mask);
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:29:41 +08:00
|
|
|
void __init early_init_mmu(void)
|
|
|
|
{
|
|
|
|
}
|