Commit 7867e402 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'riscv-for-linus-5.17-mw1' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux

Pull more RISC-V updates from Palmer Dabbelt:

 - Support for sv48 paging

 - Hart ID mappings are now sparse, which enables more CPUs to come up
   on systems with sparse hart IDs

 - A handful of cleanups and fixes

* tag 'riscv-for-linus-5.17-mw1' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux: (27 commits)
  RISC-V: nommu_virt: Drop unused SLAB_MERGE_DEFAULT
  RISC-V: Remove redundant err variable
  riscv: dts: sifive unmatched: Add gpio poweroff
  riscv: canaan: remove useless select of non-existing config SYSCON
  RISC-V: Do not use cpumask data structure for hartid bitmap
  RISC-V: Move spinwait booting method to its own config
  RISC-V: Move the entire hart selection via lottery to SMP
  RISC-V: Use __cpu_up_stack/task_pointer only for spinwait method
  RISC-V: Do not print the SBI version during HSM extension boot print
  RISC-V: Avoid using per cpu array for ordered booting
  riscv: default to CONFIG_RISCV_SBI_V01=n
  riscv: fix boolconv.cocci warnings
  riscv: Explicit comment about user virtual address space size
  riscv: Use pgtable_l4_enabled to output mmu_type in cpuinfo
  riscv: Implement sv48 support
  asm-generic: Prepare for riscv use of pud_alloc_one and pud_free
  riscv: Allow to dynamically define VA_BITS
  riscv: Introduce functions to switch pt_ops
  riscv: Split early kasan mapping to prepare sv48 introduction
  riscv: Move KASAN mapping next to the kernel mapping
  ...
parents b21bae9a c59cd507
...@@ -47,12 +47,12 @@ RISC-V Linux Kernel SV39 ...@@ -47,12 +47,12 @@ RISC-V Linux Kernel SV39
| Kernel-space virtual memory, shared between all processes: | Kernel-space virtual memory, shared between all processes:
____________________________________________________________|___________________________________________________________ ____________________________________________________________|___________________________________________________________
| | | | | | | |
ffffffc000000000 | -256 GB | ffffffc7ffffffff | 32 GB | kasan ffffffc6fee00000 | -228 GB | ffffffc6feffffff | 2 MB | fixmap
ffffffcefee00000 | -196 GB | ffffffcefeffffff | 2 MB | fixmap ffffffc6ff000000 | -228 GB | ffffffc6ffffffff | 16 MB | PCI io
ffffffceff000000 | -196 GB | ffffffceffffffff | 16 MB | PCI io ffffffc700000000 | -228 GB | ffffffc7ffffffff | 4 GB | vmemmap
ffffffcf00000000 | -196 GB | ffffffcfffffffff | 4 GB | vmemmap ffffffc800000000 | -224 GB | ffffffd7ffffffff | 64 GB | vmalloc/ioremap space
ffffffd000000000 | -192 GB | ffffffdfffffffff | 64 GB | vmalloc/ioremap space ffffffd800000000 | -160 GB | fffffff6ffffffff | 124 GB | direct mapping of all physical memory
ffffffe000000000 | -128 GB | ffffffff7fffffff | 124 GB | direct mapping of all physical memory fffffff700000000 | -36 GB | fffffffeffffffff | 32 GB | kasan
__________________|____________|__________________|_________|____________________________________________________________ __________________|____________|__________________|_________|____________________________________________________________
| |
| |
......
...@@ -147,27 +147,16 @@ config MMU ...@@ -147,27 +147,16 @@ config MMU
Select if you want MMU-based virtualised addressing space Select if you want MMU-based virtualised addressing space
support by paged memory management. If unsure, say 'Y'. support by paged memory management. If unsure, say 'Y'.
config VA_BITS
int
default 32 if 32BIT
default 39 if 64BIT
config PA_BITS
int
default 34 if 32BIT
default 56 if 64BIT
config PAGE_OFFSET config PAGE_OFFSET
hex hex
default 0xC0000000 if 32BIT && MAXPHYSMEM_1GB default 0xC0000000 if 32BIT
default 0x80000000 if 64BIT && !MMU default 0x80000000 if 64BIT && !MMU
default 0xffffffff80000000 if 64BIT && MAXPHYSMEM_2GB default 0xffffaf8000000000 if 64BIT
default 0xffffffe000000000 if 64BIT && MAXPHYSMEM_128GB
config KASAN_SHADOW_OFFSET config KASAN_SHADOW_OFFSET
hex hex
depends on KASAN_GENERIC depends on KASAN_GENERIC
default 0xdfffffc800000000 if 64BIT default 0xdfffffff00000000 if 64BIT
default 0xffffffff if 32BIT default 0xffffffff if 32BIT
config ARCH_FLATMEM_ENABLE config ARCH_FLATMEM_ENABLE
...@@ -213,7 +202,7 @@ config FIX_EARLYCON_MEM ...@@ -213,7 +202,7 @@ config FIX_EARLYCON_MEM
config PGTABLE_LEVELS config PGTABLE_LEVELS
int int
default 3 if 64BIT default 4 if 64BIT
default 2 default 2
config LOCKDEP_SUPPORT config LOCKDEP_SUPPORT
...@@ -271,24 +260,6 @@ config MODULE_SECTIONS ...@@ -271,24 +260,6 @@ config MODULE_SECTIONS
bool bool
select HAVE_MOD_ARCH_SPECIFIC select HAVE_MOD_ARCH_SPECIFIC
choice
prompt "Maximum Physical Memory"
default MAXPHYSMEM_1GB if 32BIT
default MAXPHYSMEM_2GB if 64BIT && CMODEL_MEDLOW
default MAXPHYSMEM_128GB if 64BIT && CMODEL_MEDANY
config MAXPHYSMEM_1GB
depends on 32BIT
bool "1GiB"
config MAXPHYSMEM_2GB
depends on 64BIT
bool "2GiB"
config MAXPHYSMEM_128GB
depends on 64BIT && CMODEL_MEDANY
bool "128GiB"
endchoice
config SMP config SMP
bool "Symmetric Multi-Processing" bool "Symmetric Multi-Processing"
help help
...@@ -392,12 +363,25 @@ source "kernel/Kconfig.hz" ...@@ -392,12 +363,25 @@ source "kernel/Kconfig.hz"
config RISCV_SBI_V01 config RISCV_SBI_V01
bool "SBI v0.1 support" bool "SBI v0.1 support"
default y
depends on RISCV_SBI depends on RISCV_SBI
help help
This config allows kernel to use SBI v0.1 APIs. This will be This config allows kernel to use SBI v0.1 APIs. This will be
deprecated in future once legacy M-mode software are no longer in use. deprecated in future once legacy M-mode software are no longer in use.
config RISCV_BOOT_SPINWAIT
bool "Spinwait booting method"
depends on SMP
default y
help
This enables support for booting Linux via spinwait method. In the
spinwait method, all cores randomly jump to Linux. One of the cores
gets chosen via lottery and all other keep spinning on a percpu
variable. This method cannot support CPU hotplug and sparse hartid
scheme. It should be only enabled for M-mode Linux or platforms relying
on older firmware without SBI HSM extension. All other platforms should
rely on ordered booting via SBI HSM extension which gets chosen
dynamically at runtime if the firmware supports it.
config KEXEC config KEXEC
bool "Kexec system call" bool "Kexec system call"
select KEXEC_CORE select KEXEC_CORE
......
...@@ -39,6 +39,11 @@ rtcclk: rtcclk { ...@@ -39,6 +39,11 @@ rtcclk: rtcclk {
clock-frequency = <RTCCLK_FREQ>; clock-frequency = <RTCCLK_FREQ>;
clock-output-names = "rtcclk"; clock-output-names = "rtcclk";
}; };
gpio-poweroff {
compatible = "gpio-poweroff";
gpios = <&gpio 2 GPIO_ACTIVE_LOW>;
};
}; };
&uart0 { &uart0 {
......
...@@ -29,7 +29,6 @@ CONFIG_EMBEDDED=y ...@@ -29,7 +29,6 @@ CONFIG_EMBEDDED=y
CONFIG_SLOB=y CONFIG_SLOB=y
# CONFIG_MMU is not set # CONFIG_MMU is not set
CONFIG_SOC_CANAAN=y CONFIG_SOC_CANAAN=y
CONFIG_MAXPHYSMEM_2GB=y
CONFIG_SMP=y CONFIG_SMP=y
CONFIG_NR_CPUS=2 CONFIG_NR_CPUS=2
CONFIG_CMDLINE="earlycon console=ttySIF0" CONFIG_CMDLINE="earlycon console=ttySIF0"
......
...@@ -21,7 +21,6 @@ CONFIG_EMBEDDED=y ...@@ -21,7 +21,6 @@ CONFIG_EMBEDDED=y
CONFIG_SLOB=y CONFIG_SLOB=y
# CONFIG_MMU is not set # CONFIG_MMU is not set
CONFIG_SOC_CANAAN=y CONFIG_SOC_CANAAN=y
CONFIG_MAXPHYSMEM_2GB=y
CONFIG_SMP=y CONFIG_SMP=y
CONFIG_NR_CPUS=2 CONFIG_NR_CPUS=2
CONFIG_CMDLINE="earlycon console=ttySIF0 rootdelay=2 root=/dev/mmcblk0p1 ro" CONFIG_CMDLINE="earlycon console=ttySIF0 rootdelay=2 root=/dev/mmcblk0p1 ro"
......
...@@ -24,10 +24,8 @@ CONFIG_EXPERT=y ...@@ -24,10 +24,8 @@ CONFIG_EXPERT=y
# CONFIG_VM_EVENT_COUNTERS is not set # CONFIG_VM_EVENT_COUNTERS is not set
# CONFIG_COMPAT_BRK is not set # CONFIG_COMPAT_BRK is not set
CONFIG_SLOB=y CONFIG_SLOB=y
# CONFIG_SLAB_MERGE_DEFAULT is not set
# CONFIG_MMU is not set # CONFIG_MMU is not set
CONFIG_SOC_VIRT=y CONFIG_SOC_VIRT=y
CONFIG_MAXPHYSMEM_2GB=y
CONFIG_SMP=y CONFIG_SMP=y
CONFIG_CMDLINE="root=/dev/vda rw earlycon=uart8250,mmio,0x10000000,115200n8 console=ttyS0" CONFIG_CMDLINE="root=/dev/vda rw earlycon=uart8250,mmio,0x10000000,115200n8 console=ttyS0"
CONFIG_CMDLINE_FORCE=y CONFIG_CMDLINE_FORCE=y
......
...@@ -40,7 +40,5 @@ struct cpu_operations { ...@@ -40,7 +40,5 @@ struct cpu_operations {
extern const struct cpu_operations *cpu_ops[NR_CPUS]; extern const struct cpu_operations *cpu_ops[NR_CPUS];
void __init cpu_set_ops(int cpu); void __init cpu_set_ops(int cpu);
void cpu_update_secondary_bootdata(unsigned int cpuid,
struct task_struct *tidle);
#endif /* ifndef __ASM_CPU_OPS_H */ #endif /* ifndef __ASM_CPU_OPS_H */
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (c) 2021 by Rivos Inc.
*/
#ifndef __ASM_CPU_OPS_SBI_H
#define __ASM_CPU_OPS_SBI_H
#ifndef __ASSEMBLY__
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/threads.h>
/**
* struct sbi_hart_boot_data - Hart specific boot used during booting and
* cpu hotplug.
* @task_ptr: A pointer to the hart specific tp
* @stack_ptr: A pointer to the hart specific sp
*/
struct sbi_hart_boot_data {
void *task_ptr;
void *stack_ptr;
};
#endif
#endif /* ifndef __ASM_CPU_OPS_SBI_H */
...@@ -40,14 +40,13 @@ ...@@ -40,14 +40,13 @@
#ifndef CONFIG_64BIT #ifndef CONFIG_64BIT
#define SATP_PPN _AC(0x003FFFFF, UL) #define SATP_PPN _AC(0x003FFFFF, UL)
#define SATP_MODE_32 _AC(0x80000000, UL) #define SATP_MODE_32 _AC(0x80000000, UL)
#define SATP_MODE SATP_MODE_32
#define SATP_ASID_BITS 9 #define SATP_ASID_BITS 9
#define SATP_ASID_SHIFT 22 #define SATP_ASID_SHIFT 22
#define SATP_ASID_MASK _AC(0x1FF, UL) #define SATP_ASID_MASK _AC(0x1FF, UL)
#else #else
#define SATP_PPN _AC(0x00000FFFFFFFFFFF, UL) #define SATP_PPN _AC(0x00000FFFFFFFFFFF, UL)
#define SATP_MODE_39 _AC(0x8000000000000000, UL) #define SATP_MODE_39 _AC(0x8000000000000000, UL)
#define SATP_MODE SATP_MODE_39 #define SATP_MODE_48 _AC(0x9000000000000000, UL)
#define SATP_ASID_BITS 16 #define SATP_ASID_BITS 16
#define SATP_ASID_SHIFT 44 #define SATP_ASID_SHIFT 44
#define SATP_ASID_MASK _AC(0xFFFF, UL) #define SATP_ASID_MASK _AC(0xFFFF, UL)
......
...@@ -24,6 +24,7 @@ enum fixed_addresses { ...@@ -24,6 +24,7 @@ enum fixed_addresses {
FIX_HOLE, FIX_HOLE,
FIX_PTE, FIX_PTE,
FIX_PMD, FIX_PMD,
FIX_PUD,
FIX_TEXT_POKE1, FIX_TEXT_POKE1,
FIX_TEXT_POKE0, FIX_TEXT_POKE0,
FIX_EARLYCON_MEM_BASE, FIX_EARLYCON_MEM_BASE,
......
...@@ -27,13 +27,18 @@ ...@@ -27,13 +27,18 @@
*/ */
#define KASAN_SHADOW_SCALE_SHIFT 3 #define KASAN_SHADOW_SCALE_SHIFT 3
#define KASAN_SHADOW_SIZE (UL(1) << ((CONFIG_VA_BITS - 1) - KASAN_SHADOW_SCALE_SHIFT)) #define KASAN_SHADOW_SIZE (UL(1) << ((VA_BITS - 1) - KASAN_SHADOW_SCALE_SHIFT))
#define KASAN_SHADOW_START KERN_VIRT_START /*
#define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE) * Depending on the size of the virtual address space, the region may not be
* aligned on PGDIR_SIZE, so force its alignment to ease its population.
*/
#define KASAN_SHADOW_START ((KASAN_SHADOW_END - KASAN_SHADOW_SIZE) & PGDIR_MASK)
#define KASAN_SHADOW_END MODULES_LOWEST_VADDR
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
void kasan_init(void); void kasan_init(void);
asmlinkage void kasan_early_init(void); asmlinkage void kasan_early_init(void);
void kasan_swapper_init(void);
#endif #endif
#endif #endif
......
...@@ -31,9 +31,20 @@ ...@@ -31,9 +31,20 @@
* When not using MMU this corresponds to the first free page in * When not using MMU this corresponds to the first free page in
* physical memory (aligned on a page boundary). * physical memory (aligned on a page boundary).
*/ */
#ifdef CONFIG_64BIT
#ifdef CONFIG_MMU
#define PAGE_OFFSET kernel_map.page_offset
#else
#define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
#endif
#define KERN_VIRT_SIZE (-PAGE_OFFSET) /*
* By default, CONFIG_PAGE_OFFSET value corresponds to SV48 address space so
* define the PAGE_OFFSET value for SV39.
*/
#define PAGE_OFFSET_L3 _AC(0xffffffd800000000, UL)
#else
#define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
#endif /* CONFIG_64BIT */
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
...@@ -86,6 +97,7 @@ extern unsigned long riscv_pfn_base; ...@@ -86,6 +97,7 @@ extern unsigned long riscv_pfn_base;
#endif /* CONFIG_MMU */ #endif /* CONFIG_MMU */
struct kernel_mapping { struct kernel_mapping {
unsigned long page_offset;
unsigned long virt_addr; unsigned long virt_addr;
uintptr_t phys_addr; uintptr_t phys_addr;
uintptr_t size; uintptr_t size;
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include <asm/tlb.h> #include <asm/tlb.h>
#ifdef CONFIG_MMU #ifdef CONFIG_MMU
#define __HAVE_ARCH_PUD_ALLOC_ONE
#define __HAVE_ARCH_PUD_FREE
#include <asm-generic/pgalloc.h> #include <asm-generic/pgalloc.h>
static inline void pmd_populate_kernel(struct mm_struct *mm, static inline void pmd_populate_kernel(struct mm_struct *mm,
...@@ -36,6 +38,44 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) ...@@ -36,6 +38,44 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
} }
static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
{
if (pgtable_l4_enabled) {
unsigned long pfn = virt_to_pfn(pud);
set_p4d(p4d, __p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
}
}
static inline void p4d_populate_safe(struct mm_struct *mm, p4d_t *p4d,
pud_t *pud)
{
if (pgtable_l4_enabled) {
unsigned long pfn = virt_to_pfn(pud);
set_p4d_safe(p4d,
__p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
}
}
#define pud_alloc_one pud_alloc_one
static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
{
if (pgtable_l4_enabled)
return __pud_alloc_one(mm, addr);
return NULL;
}
#define pud_free pud_free
static inline void pud_free(struct mm_struct *mm, pud_t *pud)
{
if (pgtable_l4_enabled)
__pud_free(mm, pud);
}
#define __pud_free_tlb(tlb, pud, addr) pud_free((tlb)->mm, pud)
#endif /* __PAGETABLE_PMD_FOLDED */ #endif /* __PAGETABLE_PMD_FOLDED */
static inline pgd_t *pgd_alloc(struct mm_struct *mm) static inline pgd_t *pgd_alloc(struct mm_struct *mm)
......
...@@ -8,16 +8,36 @@ ...@@ -8,16 +8,36 @@
#include <linux/const.h> #include <linux/const.h>
#define PGDIR_SHIFT 30 extern bool pgtable_l4_enabled;
#define PGDIR_SHIFT_L3 30
#define PGDIR_SHIFT_L4 39
#define PGDIR_SIZE_L3 (_AC(1, UL) << PGDIR_SHIFT_L3)
#define PGDIR_SHIFT (pgtable_l4_enabled ? PGDIR_SHIFT_L4 : PGDIR_SHIFT_L3)
/* Size of region mapped by a page global directory */ /* Size of region mapped by a page global directory */
#define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT) #define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
#define PGDIR_MASK (~(PGDIR_SIZE - 1)) #define PGDIR_MASK (~(PGDIR_SIZE - 1))
/* pud is folded into pgd in case of 3-level page table */
#define PUD_SHIFT 30
#define PUD_SIZE (_AC(1, UL) << PUD_SHIFT)
#define PUD_MASK (~(PUD_SIZE - 1))
#define PMD_SHIFT 21 #define PMD_SHIFT 21
/* Size of region mapped by a page middle directory */ /* Size of region mapped by a page middle directory */
#define PMD_SIZE (_AC(1, UL) << PMD_SHIFT) #define PMD_SIZE (_AC(1, UL) << PMD_SHIFT)
#define PMD_MASK (~(PMD_SIZE - 1)) #define PMD_MASK (~(PMD_SIZE - 1))
/* Page Upper Directory entry */
typedef struct {
unsigned long pud;
} pud_t;
#define pud_val(x) ((x).pud)
#define __pud(x) ((pud_t) { (x) })
#define PTRS_PER_PUD (PAGE_SIZE / sizeof(pud_t))
/* Page Middle Directory entry */ /* Page Middle Directory entry */
typedef struct { typedef struct {
unsigned long pmd; unsigned long pmd;
...@@ -59,6 +79,16 @@ static inline void pud_clear(pud_t *pudp) ...@@ -59,6 +79,16 @@ static inline void pud_clear(pud_t *pudp)
set_pud(pudp, __pud(0)); set_pud(pudp, __pud(0));
} }
static inline pud_t pfn_pud(unsigned long pfn, pgprot_t prot)
{
return __pud((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot));
}
static inline unsigned long _pud_pfn(pud_t pud)
{
return pud_val(pud) >> _PAGE_PFN_SHIFT;
}
static inline pmd_t *pud_pgtable(pud_t pud) static inline pmd_t *pud_pgtable(pud_t pud)
{ {
return (pmd_t *)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT); return (pmd_t *)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT);
...@@ -69,6 +99,17 @@ static inline struct page *pud_page(pud_t pud) ...@@ -69,6 +99,17 @@ static inline struct page *pud_page(pud_t pud)
return pfn_to_page(pud_val(pud) >> _PAGE_PFN_SHIFT); return pfn_to_page(pud_val(pud) >> _PAGE_PFN_SHIFT);
} }
#define mm_pud_folded mm_pud_folded
static inline bool mm_pud_folded(struct mm_struct *mm)
{
if (pgtable_l4_enabled)
return false;
return true;
}
#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot) static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot)
{ {
return __pmd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); return __pmd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot));
...@@ -84,4 +125,69 @@ static inline unsigned long _pmd_pfn(pmd_t pmd) ...@@ -84,4 +125,69 @@ static inline unsigned long _pmd_pfn(pmd_t pmd)
#define pmd_ERROR(e) \ #define pmd_ERROR(e) \
pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e)) pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
#define pud_ERROR(e) \
pr_err("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
static inline void set_p4d(p4d_t *p4dp, p4d_t p4d)
{
if (pgtable_l4_enabled)
*p4dp = p4d;
else
set_pud((pud_t *)p4dp, (pud_t){ p4d_val(p4d) });
}
static inline int p4d_none(p4d_t p4d)
{
if (pgtable_l4_enabled)
return (p4d_val(p4d) == 0);
return 0;
}
static inline int p4d_present(p4d_t p4d)
{
if (pgtable_l4_enabled)
return (p4d_val(p4d) & _PAGE_PRESENT);
return 1;
}
static inline int p4d_bad(p4d_t p4d)
{
if (pgtable_l4_enabled)
return !p4d_present(p4d);
return 0;
}
static inline void p4d_clear(p4d_t *p4d)
{
if (pgtable_l4_enabled)
set_p4d(p4d, __p4d(0));
}
static inline pud_t *p4d_pgtable(p4d_t p4d)
{
if (pgtable_l4_enabled)
return (pud_t *)pfn_to_virt(p4d_val(p4d) >> _PAGE_PFN_SHIFT);
return (pud_t *)pud_pgtable((pud_t) { p4d_val(p4d) });
}
static inline struct page *p4d_page(p4d_t p4d)
{
return pfn_to_page(p4d_val(p4d) >> _PAGE_PFN_SHIFT);
}
#define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
#define pud_offset pud_offset
static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
{
if (pgtable_l4_enabled)
return p4d_pgtable(*p4d) + pud_index(address);
return (pud_t *)p4d;
}
#endif /* _ASM_RISCV_PGTABLE_64_H */ #endif /* _ASM_RISCV_PGTABLE_64_H */
...@@ -24,6 +24,17 @@ ...@@ -24,6 +24,17 @@
#define KERNEL_LINK_ADDR PAGE_OFFSET #define KERNEL_LINK_ADDR PAGE_OFFSET
#endif #endif
/* Number of entries in the page global directory */
#define PTRS_PER_PGD (PAGE_SIZE / sizeof(pgd_t))
/* Number of entries in the page table */
#define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t))
/*
* Half of the kernel address space (half of the entries of the page global
* directory) is for the direct mapping.
*/
#define KERN_VIRT_SIZE ((PTRS_PER_PGD / 2 * PGDIR_SIZE) / 2)
#define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1) #define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1)
#define VMALLOC_END PAGE_OFFSET #define VMALLOC_END PAGE_OFFSET
#define VMALLOC_START (PAGE_OFFSET - VMALLOC_SIZE) #define VMALLOC_START (PAGE_OFFSET - VMALLOC_SIZE)
...@@ -39,6 +50,8 @@ ...@@ -39,6 +50,8 @@
/* Modules always live before the kernel */ /* Modules always live before the kernel */
#ifdef CONFIG_64BIT #ifdef CONFIG_64BIT
/* This is used to define the end of the KASAN shadow region */
#define MODULES_LOWEST_VADDR (KERNEL_LINK_ADDR - SZ_2G)
#define MODULES_VADDR (PFN_ALIGN((unsigned long)&_end) - SZ_2G) #define MODULES_VADDR (PFN_ALIGN((unsigned long)&_end) - SZ_2G)
#define MODULES_END (PFN_ALIGN((unsigned long)&_start)) #define MODULES_END (PFN_ALIGN((unsigned long)&_start))
#endif #endif
...@@ -48,8 +61,14 @@ ...@@ -48,8 +61,14 @@
* struct pages to map half the virtual address space. Then * struct pages to map half the virtual address space. Then
* position vmemmap directly below the VMALLOC region. * position vmemmap directly below the VMALLOC region.
*/ */
#ifdef CONFIG_64BIT
#define VA_BITS (pgtable_l4_enabled ? 48 : 39)
#else
#define VA_BITS 32
#endif
#define VMEMMAP_SHIFT \ #define VMEMMAP_SHIFT \
(CONFIG_VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT) (VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT)
#define VMEMMAP_SIZE BIT(VMEMMAP_SHIFT) #define VMEMMAP_SIZE BIT(VMEMMAP_SHIFT)
#define VMEMMAP_END VMALLOC_START #define VMEMMAP_END VMALLOC_START
#define VMEMMAP_START (VMALLOC_START - VMEMMAP_SIZE) #define VMEMMAP_START (VMALLOC_START - VMEMMAP_SIZE)
...@@ -83,8 +102,7 @@ ...@@ -83,8 +102,7 @@
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
/* Page Upper Directory not used in RISC-V */ #include <asm-generic/pgtable-nop4d.h>
#include <asm-generic/pgtable-nopud.h>
#include <asm/page.h> #include <asm/page.h>
#include <asm/tlbflush.h> #include <asm/tlbflush.h>
#include <linux/mm_types.h> #include <linux/mm_types.h>
...@@ -107,12 +125,20 @@ ...@@ -107,12 +125,20 @@
#define XIP_FIXUP(addr) (addr) #define XIP_FIXUP(addr) (addr)
#endif /* CONFIG_XIP_KERNEL */ #endif /* CONFIG_XIP_KERNEL */
#ifdef CONFIG_MMU struct pt_alloc_ops {
/* Number of entries in the page global directory */ pte_t *(*get_pte_virt)(phys_addr_t pa);
#define PTRS_PER_PGD (PAGE_SIZE / sizeof(pgd_t)) phys_addr_t (*alloc_pte)(uintptr_t va);
/* Number of entries in the page table */ #ifndef __PAGETABLE_PMD_FOLDED
#define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t)) pmd_t *(*get_pmd_virt)(phys_addr_t pa);
phys_addr_t (*alloc_pmd)(uintptr_t va);
pud_t *(*get_pud_virt)(phys_addr_t pa);
phys_addr_t (*alloc_pud)(uintptr_t va);
#endif
};
extern struct pt_alloc_ops pt_ops __initdata;
#ifdef CONFIG_MMU
/* Number of PGD entries that a user-mode program can use */ /* Number of PGD entries that a user-mode program can use */
#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE) #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
...@@ -659,7 +685,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, ...@@ -659,7 +685,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
* and give the kernel the other (upper) half. * and give the kernel the other (upper) half.
*/ */
#ifdef CONFIG_64BIT #ifdef CONFIG_64BIT
#define KERN_VIRT_START (-(BIT(CONFIG_VA_BITS)) + TASK_SIZE) #define KERN_VIRT_START (-(BIT(VA_BITS)) + TASK_SIZE)
#else #else
#define KERN_VIRT_START FIXADDR_START #define KERN_VIRT_START FIXADDR_START
#endif #endif
...@@ -667,11 +693,22 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, ...@@ -667,11 +693,22 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
/* /*
* Task size is 0x4000000000 for RV64 or 0x9fc00000 for RV32. * Task size is 0x4000000000 for RV64 or 0x9fc00000 for RV32.
* Note that PGDIR_SIZE must evenly divide TASK_SIZE. * Note that PGDIR_SIZE must evenly divide TASK_SIZE.
* Task size is:
* - 0x9fc00000 (~2.5GB) for RV32.
* - 0x4000000000 ( 256GB) for RV64 using SV39 mmu
* - 0x800000000000 ( 128TB) for RV64 using SV48 mmu
*
* Note that PGDIR_SIZE must evenly divide TASK_SIZE since "RISC-V
* Instruction Set Manual Volume II: Privileged Architecture" states that
* "load and store effective addresses, which are 64bits, must have bits
* 63–48 all equal to bit 47, or else a page-fault exception will occur."
*/ */
#ifdef CONFIG_64BIT #ifdef CONFIG_64BIT
#define TASK_SIZE (PGDIR_SIZE * PTRS_PER_PGD / 2) #define TASK_SIZE (PGDIR_SIZE * PTRS_PER_PGD / 2)
#define TASK_SIZE_MIN (PGDIR_SIZE_L3 * PTRS_PER_PGD / 2)
#else #else
#define TASK_SIZE FIXADDR_START #define TASK_SIZE FIXADDR_START
#define TASK_SIZE_MIN TASK_SIZE
#endif #endif
#else /* CONFIG_MMU */ #else /* CONFIG_MMU */
...@@ -697,6 +734,8 @@ extern uintptr_t _dtb_early_pa; ...@@ -697,6 +734,8 @@ extern uintptr_t _dtb_early_pa;
#define dtb_early_va _dtb_early_va #define dtb_early_va _dtb_early_va
#define dtb_early_pa _dtb_early_pa #define dtb_early_pa _dtb_early_pa
#endif /* CONFIG_XIP_KERNEL */ #endif /* CONFIG_XIP_KERNEL */
extern u64 satp_mode;
extern bool pgtable_l4_enabled;
void paging_init(void); void paging_init(void);
void misc_mem_init(void); void misc_mem_init(void);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define _ASM_RISCV_SBI_H #define _ASM_RISCV_SBI_H
#include <linux/types.h> #include <linux/types.h>
#include <linux/cpumask.h>
#ifdef CONFIG_RISCV_SBI #ifdef CONFIG_RISCV_SBI
enum sbi_ext_id { enum sbi_ext_id {
...@@ -128,27 +129,27 @@ long sbi_get_mimpid(void); ...@@ -128,27 +129,27 @@ long sbi_get_mimpid(void);
void sbi_set_timer(uint64_t stime_value); void sbi_set_timer(uint64_t stime_value);
void sbi_shutdown(void); void sbi_shutdown(void);
void sbi_clear_ipi(void); void sbi_clear_ipi(void);
int sbi_send_ipi(const unsigned long *hart_mask); int sbi_send_ipi(const struct cpumask *cpu_mask);
int sbi_remote_fence_i(const unsigned long *hart_mask); int sbi_remote_fence_i(const struct cpumask *cpu_mask);
int sbi_remote_sfence_vma(const unsigned long *hart_mask, int sbi_remote_sfence_vma(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size); unsigned long size);
int sbi_remote_sfence_vma_asid(const unsigned long *hart_mask, int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size, unsigned long size,
unsigned long asid); unsigned long asid);
int sbi_remote_hfence_gvma(const unsigned long *hart_mask, int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size); unsigned long size);
int sbi_remote_hfence_gvma_vmid(const unsigned long *hart_mask, int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size, unsigned long size,
unsigned long vmid); unsigned long vmid);
int sbi_remote_hfence_vvma(const unsigned long *hart_mask, int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size); unsigned long size);
int sbi_remote_hfence_vvma_asid(const unsigned long *hart_mask, int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
unsigned long start, unsigned long start,
unsigned long size, unsigned long size,
unsigned long asid); unsigned long asid);
...@@ -183,7 +184,7 @@ static inline unsigned long sbi_mk_version(unsigned long major, ...@@ -183,7 +184,7 @@ static inline unsigned long sbi_mk_version(unsigned long major,
int sbi_err_map_linux_errno(int err); int sbi_err_map_linux_errno(int err);
#else /* CONFIG_RISCV_SBI */ #else /* CONFIG_RISCV_SBI */
static inline int sbi_remote_fence_i(const unsigned long *hart_mask) { return -1; } static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
static inline void sbi_init(void) {} static inline void sbi_init(void) {}
#endif /* CONFIG_RISCV_SBI */ #endif /* CONFIG_RISCV_SBI */
#endif /* _ASM_RISCV_SBI_H */ #endif /* _ASM_RISCV_SBI_H */
...@@ -92,8 +92,6 @@ static inline void riscv_clear_ipi(void) ...@@ -92,8 +92,6 @@ static inline void riscv_clear_ipi(void)
#endif /* CONFIG_SMP */ #endif /* CONFIG_SMP */
void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out);
#if defined(CONFIG_HOTPLUG_CPU) && (CONFIG_SMP) #if defined(CONFIG_HOTPLUG_CPU) && (CONFIG_SMP)
bool cpu_has_hotplug(unsigned int cpu); bool cpu_has_hotplug(unsigned int cpu);
#else #else
......
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
#define _ASM_RISCV_SPARSEMEM_H #define _ASM_RISCV_SPARSEMEM_H
#ifdef CONFIG_SPARSEMEM #ifdef CONFIG_SPARSEMEM
#define MAX_PHYSMEM_BITS CONFIG_PA_BITS #ifdef CONFIG_64BIT
#define MAX_PHYSMEM_BITS 56
#else
#define MAX_PHYSMEM_BITS 34
#endif /* CONFIG_64BIT */
#define SECTION_SIZE_BITS 27 #define SECTION_SIZE_BITS 27
#endif /* CONFIG_SPARSEMEM */ #endif /* CONFIG_SPARSEMEM */
......
...@@ -43,7 +43,8 @@ obj-$(CONFIG_FPU) += fpu.o ...@@ -43,7 +43,8 @@ obj-$(CONFIG_FPU) += fpu.o
obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smpboot.o
obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += smp.o
obj-$(CONFIG_SMP) += cpu_ops.o obj-$(CONFIG_SMP) += cpu_ops.o
obj-$(CONFIG_SMP) += cpu_ops_spinwait.o
obj-$(CONFIG_RISCV_BOOT_SPINWAIT) += cpu_ops_spinwait.o
obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_MODULES) += module.o
obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <asm/kvm_host.h> #include <asm/kvm_host.h>
#include <asm/thread_info.h> #include <asm/thread_info.h>
#include <asm/ptrace.h> #include <asm/ptrace.h>
#include <asm/cpu_ops_sbi.h>
void asm_offsets(void); void asm_offsets(void);
...@@ -468,4 +469,6 @@ void asm_offsets(void) ...@@ -468,4 +469,6 @@ void asm_offsets(void)
DEFINE(PT_SIZE_ON_STACK, ALIGN(sizeof(struct pt_regs), STACK_ALIGN)); DEFINE(PT_SIZE_ON_STACK, ALIGN(sizeof(struct pt_regs), STACK_ALIGN));
OFFSET(KERNEL_MAP_VIRT_ADDR, kernel_mapping, virt_addr); OFFSET(KERNEL_MAP_VIRT_ADDR, kernel_mapping, virt_addr);
OFFSET(SBI_HART_BOOT_TASK_PTR_OFFSET, sbi_hart_boot_data, task_ptr);
OFFSET(SBI_HART_BOOT_STACK_PTR_OFFSET, sbi_hart_boot_data, stack_ptr);
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/of.h> #include <linux/of.h>
#include <asm/smp.h> #include <asm/smp.h>
#include <asm/pgtable.h>
/* /*
* Returns the hart ID of the given device tree node, or -ENODEV if the node * Returns the hart ID of the given device tree node, or -ENODEV if the node
...@@ -71,18 +72,19 @@ static void print_isa(struct seq_file *f, const char *isa) ...@@ -71,18 +72,19 @@ static void print_isa(struct seq_file *f, const char *isa)
seq_puts(f, "\n"); seq_puts(f, "\n");
} }
static void print_mmu(struct seq_file *f, const char *mmu_type) static void print_mmu(struct seq_file *f)
{ {
char sv_type[16];
#if defined(CONFIG_32BIT) #if defined(CONFIG_32BIT)
if (strcmp(mmu_type, "riscv,sv32") != 0) strncpy(sv_type, "sv32", 5);
return;
#elif defined(CONFIG_64BIT) #elif defined(CONFIG_64BIT)
if (strcmp(mmu_type, "riscv,sv39") != 0 && if (pgtable_l4_enabled)
strcmp(mmu_type, "riscv,sv48") != 0) strncpy(sv_type, "sv48", 5);
return; else
strncpy(sv_type, "sv39", 5);
#endif #endif
seq_printf(f, "mmu\t\t: %s\n", sv_type);
seq_printf(f, "mmu\t\t: %s\n", mmu_type+6);
} }
static void *c_start(struct seq_file *m, loff_t *pos) static void *c_start(struct seq_file *m, loff_t *pos)
...@@ -107,14 +109,13 @@ static int c_show(struct seq_file *m, void *v) ...@@ -107,14 +109,13 @@ static int c_show(struct seq_file *m, void *v)
{ {
unsigned long cpu_id = (unsigned long)v - 1; unsigned long cpu_id = (unsigned long)v - 1;
struct device_node *node = of_get_cpu_node(cpu_id, NULL); struct device_node *node = of_get_cpu_node(cpu_id, NULL);
const char *compat, *isa, *mmu; const char *compat, *isa;
seq_printf(m, "processor\t: %lu\n", cpu_id); seq_printf(m, "processor\t: %lu\n", cpu_id);
seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id)); seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
if (!of_property_read_string(node, "riscv,isa", &isa)) if (!of_property_read_string(node, "riscv,isa", &isa))
print_isa(m, isa); print_isa(m, isa);
if (!of_property_read_string(node, "mmu-type", &mmu)) print_mmu(m);
print_mmu(m, mmu);
if (!of_property_read_string(node, "compatible", &compat) if (!of_property_read_string(node, "compatible", &compat)
&& strcmp(compat, "riscv")) && strcmp(compat, "riscv"))
seq_printf(m, "uarch\t\t: %s\n", compat); seq_printf(m, "uarch\t\t: %s\n", compat);
......
...@@ -8,37 +8,29 @@ ...@@ -8,37 +8,29 @@
#include <linux/of.h> #include <linux/of.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <asm/cpu_ops.h> #include <asm/cpu_ops.h>
#include <asm/sbi.h> #include <asm/sbi.h>
#include <asm/smp.h> #include <asm/smp.h>
const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init; const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
void *__cpu_up_stack_pointer[NR_CPUS] __section(".data");
void *__cpu_up_task_pointer[NR_CPUS] __section(".data");
extern const struct cpu_operations cpu_ops_sbi; extern const struct cpu_operations cpu_ops_sbi;
#ifdef CONFIG_RISCV_BOOT_SPINWAIT
extern const struct cpu_operations cpu_ops_spinwait; extern const struct cpu_operations cpu_ops_spinwait;
#else
void cpu_update_secondary_bootdata(unsigned int cpuid, const struct cpu_operations cpu_ops_spinwait = {
struct task_struct *tidle) .name = "",
{ .cpu_prepare = NULL,
int hartid = cpuid_to_hartid_map(cpuid); .cpu_start = NULL,
};
/* Make sure tidle is updated */ #endif
smp_mb();
WRITE_ONCE(__cpu_up_stack_pointer[hartid],
task_stack_page(tidle) + THREAD_SIZE);
WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
}
void __init cpu_set_ops(int cpuid) void __init cpu_set_ops(int cpuid)
{ {
#if IS_ENABLED(CONFIG_RISCV_SBI) #if IS_ENABLED(CONFIG_RISCV_SBI)
if (sbi_probe_extension(SBI_EXT_HSM) > 0) { if (sbi_probe_extension(SBI_EXT_HSM) > 0) {
if (!cpuid) if (!cpuid)
pr_info("SBI v0.2 HSM extension detected\n"); pr_info("SBI HSM extension detected\n");
cpu_ops[cpuid] = &cpu_ops_sbi; cpu_ops[cpuid] = &cpu_ops_sbi;
} else } else
#endif #endif
......
...@@ -7,13 +7,22 @@ ...@@ -7,13 +7,22 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/sched/task_stack.h>
#include <asm/cpu_ops.h> #include <asm/cpu_ops.h>
#include <asm/cpu_ops_sbi.h>
#include <asm/sbi.h> #include <asm/sbi.h>
#include <asm/smp.h> #include <asm/smp.h>
extern char secondary_start_sbi[]; extern char secondary_start_sbi[];
const struct cpu_operations cpu_ops_sbi; const struct cpu_operations cpu_ops_sbi;
/*
* Ordered booting via HSM brings one cpu at a time. However, cpu hotplug can
* be invoked from multiple threads in parallel. Define a per cpu data
* to handle that.
*/
DEFINE_PER_CPU(struct sbi_hart_boot_data, boot_data);
static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr, static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,
unsigned long priv) unsigned long priv)
{ {
...@@ -55,14 +64,19 @@ static int sbi_hsm_hart_get_status(unsigned long hartid) ...@@ -55,14 +64,19 @@ static int sbi_hsm_hart_get_status(unsigned long hartid)
static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle) static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle)
{ {
int rc;
unsigned long boot_addr = __pa_symbol(secondary_start_sbi); unsigned long boot_addr = __pa_symbol(secondary_start_sbi);
int hartid = cpuid_to_hartid_map(cpuid); int hartid = cpuid_to_hartid_map(cpuid);
unsigned long hsm_data;
cpu_update_secondary_bootdata(cpuid, tidle); struct sbi_hart_boot_data *bdata = &per_cpu(boot_data, cpuid);
rc = sbi_hsm_hart_start(hartid, boot_addr, 0);
/* Make sure tidle is updated */
return rc; smp_mb();
bdata->task_ptr = tidle;
bdata->stack_ptr = task_stack_page(tidle) + THREAD_SIZE;
/* Make sure boot data is updated */
smp_mb();
hsm_data = __pa(bdata);
return sbi_hsm_hart_start(hartid, boot_addr, hsm_data);
} }
static int sbi_cpu_prepare(unsigned int cpuid) static int sbi_cpu_prepare(unsigned int cpuid)
......
...@@ -6,11 +6,36 @@ ...@@ -6,11 +6,36 @@
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/sched/task_stack.h>
#include <asm/cpu_ops.h> #include <asm/cpu_ops.h>
#include <asm/sbi.h> #include <asm/sbi.h>
#include <asm/smp.h> #include <asm/smp.h>
const struct cpu_operations cpu_ops_spinwait; const struct cpu_operations cpu_ops_spinwait;
void *__cpu_spinwait_stack_pointer[NR_CPUS] __section(".data");
void *__cpu_spinwait_task_pointer[NR_CPUS] __section(".data");
static void cpu_update_secondary_bootdata(unsigned int cpuid,
struct task_struct *tidle)
{
int hartid = cpuid_to_hartid_map(cpuid);
/*
* The hartid must be less than NR_CPUS to avoid out-of-bound access
* errors for __cpu_spinwait_stack/task_pointer. That is not always possible
* for platforms with discontiguous hartid numbering scheme. That's why
* spinwait booting is not the recommended approach for any platforms
* booting Linux in S-mode and can be disabled in the future.
*/
if (hartid == INVALID_HARTID || hartid >= NR_CPUS)
return;
/* Make sure tidle is updated */
smp_mb();
WRITE_ONCE(__cpu_spinwait_stack_pointer[hartid],
task_stack_page(tidle) + THREAD_SIZE);
WRITE_ONCE(__cpu_spinwait_task_pointer[hartid], tidle);
}
static int spinwait_cpu_prepare(unsigned int cpuid) static int spinwait_cpu_prepare(unsigned int cpuid)
{ {
...@@ -28,7 +53,7 @@ static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle) ...@@ -28,7 +53,7 @@ static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle)
* selects the first cpu to boot the kernel and causes the remainder * selects the first cpu to boot the kernel and causes the remainder
* of the cpus to spin in a loop waiting for their stack pointer to be * of the cpus to spin in a loop waiting for their stack pointer to be
* setup by that main cpu. Writing to bootdata * setup by that main cpu. Writing to bootdata
* (i.e __cpu_up_stack_pointer) signals to the spinning cpus that they * (i.e __cpu_spinwait_stack_pointer) signals to the spinning cpus that they
* can continue the boot process. * can continue the boot process.
*/ */
cpu_update_secondary_bootdata(cpuid, tidle); cpu_update_secondary_bootdata(cpuid, tidle);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <asm/page.h> #include <asm/page.h>
#include <asm/pgtable.h> #include <asm/pgtable.h>
#include <asm/csr.h> #include <asm/csr.h>
#include <asm/cpu_ops_sbi.h>
#include <asm/hwcap.h> #include <asm/hwcap.h>
#include <asm/image.h> #include <asm/image.h>
#include "efi-header.S" #include "efi-header.S"
...@@ -105,7 +106,8 @@ relocate: ...@@ -105,7 +106,8 @@ relocate:
/* Compute satp for kernel page tables, but don't load it yet */ /* Compute satp for kernel page tables, but don't load it yet */
srl a2, a0, PAGE_SHIFT srl a2, a0, PAGE_SHIFT
li a1, SATP_MODE la a1, satp_mode
REG_L a1, 0(a1)
or a2, a2, a1 or a2, a2, a1
/* /*
...@@ -167,15 +169,15 @@ secondary_start_sbi: ...@@ -167,15 +169,15 @@ secondary_start_sbi:
la a3, .Lsecondary_park la a3, .Lsecondary_park
csrw CSR_TVEC, a3 csrw CSR_TVEC, a3
slli a3, a0, LGREG /* a0 contains the hartid & a1 contains boot data */
la a4, __cpu_up_stack_pointer li a2, SBI_HART_BOOT_TASK_PTR_OFFSET
XIP_FIXUP_OFFSET a4 XIP_FIXUP_OFFSET a2
la a5, __cpu_up_task_pointer add a2, a2, a1
XIP_FIXUP_OFFSET a5 REG_L tp, (a2)
add a4, a3, a4 li a3, SBI_HART_BOOT_STACK_PTR_OFFSET
add a5, a3, a5 XIP_FIXUP_OFFSET a3
REG_L sp, (a4) add a3, a3, a1
REG_L tp, (a5) REG_L sp, (a3)
.Lsecondary_start_common: .Lsecondary_start_common:
...@@ -257,13 +259,13 @@ pmp_done: ...@@ -257,13 +259,13 @@ pmp_done:
li t0, SR_FS li t0, SR_FS
csrc CSR_STATUS, t0 csrc CSR_STATUS, t0
#ifdef CONFIG_SMP #ifdef CONFIG_RISCV_BOOT_SPINWAIT
li t0, CONFIG_NR_CPUS li t0, CONFIG_NR_CPUS
blt a0, t0, .Lgood_cores blt a0, t0, .Lgood_cores
tail .Lsecondary_park tail .Lsecondary_park
.Lgood_cores: .Lgood_cores:
#endif
/* The lottery system is only required for spinwait booting method */
#ifndef CONFIG_XIP_KERNEL #ifndef CONFIG_XIP_KERNEL
/* Pick one hart to run the main boot sequence */ /* Pick one hart to run the main boot sequence */
la a3, hart_lottery la a3, hart_lottery
...@@ -282,6 +284,10 @@ pmp_done: ...@@ -282,6 +284,10 @@ pmp_done:
/* first time here if hart_lottery in RAM is not set */ /* first time here if hart_lottery in RAM is not set */
beq t0, t1, .Lsecondary_start beq t0, t1, .Lsecondary_start
#endif /* CONFIG_XIP */
#endif /* CONFIG_RISCV_BOOT_SPINWAIT */
#ifdef CONFIG_XIP_KERNEL
la sp, _end + THREAD_SIZE la sp, _end + THREAD_SIZE
XIP_FIXUP_OFFSET sp XIP_FIXUP_OFFSET sp
mv s0, a0 mv s0, a0
...@@ -338,16 +344,16 @@ clear_bss_done: ...@@ -338,16 +344,16 @@ clear_bss_done:
call soc_early_init call soc_early_init
tail start_kernel tail start_kernel
#if CONFIG_RISCV_BOOT_SPINWAIT
.Lsecondary_start: .Lsecondary_start:
#ifdef CONFIG_SMP
/* Set trap vector to spin forever to help debug */ /* Set trap vector to spin forever to help debug */
la a3, .Lsecondary_park la a3, .Lsecondary_park
csrw CSR_TVEC, a3 csrw CSR_TVEC, a3
slli a3, a0, LGREG slli a3, a0, LGREG
la a1, __cpu_up_stack_pointer la a1, __cpu_spinwait_stack_pointer
XIP_FIXUP_OFFSET a1 XIP_FIXUP_OFFSET a1
la a2, __cpu_up_task_pointer la a2, __cpu_spinwait_task_pointer
XIP_FIXUP_OFFSET a2 XIP_FIXUP_OFFSET a2
add a1, a3, a1 add a1, a3, a1
add a2, a3, a2 add a2, a3, a2
...@@ -365,7 +371,7 @@ clear_bss_done: ...@@ -365,7 +371,7 @@ clear_bss_done:
fence fence
tail .Lsecondary_start_common tail .Lsecondary_start_common
#endif #endif /* CONFIG_RISCV_BOOT_SPINWAIT */
END(_start_kernel) END(_start_kernel)
......
...@@ -16,7 +16,9 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa); ...@@ -16,7 +16,9 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa);
asmlinkage void __init __copy_data(void); asmlinkage void __init __copy_data(void);
#endif #endif
extern void *__cpu_up_stack_pointer[]; #ifdef CONFIG_RISCV_BOOT_SPINWAIT
extern void *__cpu_up_task_pointer[]; extern void *__cpu_spinwait_stack_pointer[];
extern void *__cpu_spinwait_task_pointer[];
#endif
#endif /* __ASM_HEAD_H */ #endif /* __ASM_HEAD_H */
...@@ -42,12 +42,10 @@ static int riscv_gpr_set(struct task_struct *target, ...@@ -42,12 +42,10 @@ static int riscv_gpr_set(struct task_struct *target,
unsigned int pos, unsigned int count, unsigned int pos, unsigned int count,
const void *kbuf, const void __user *ubuf) const void *kbuf, const void __user *ubuf)
{ {
int ret;
struct pt_regs *regs; struct pt_regs *regs;
regs = task_pt_regs(target); regs = task_pt_regs(target);
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1); return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
return ret;
} }
#ifdef CONFIG_FPU #ifdef CONFIG_FPU
......
This diff is collapsed.
...@@ -59,16 +59,6 @@ atomic_t hart_lottery __section(".sdata") ...@@ -59,16 +59,6 @@ atomic_t hart_lottery __section(".sdata")
unsigned long boot_cpu_hartid; unsigned long boot_cpu_hartid;
static DEFINE_PER_CPU(struct cpu, cpu_devices); static DEFINE_PER_CPU(struct cpu, cpu_devices);
void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
{
int cpu;
cpumask_clear(out);
for_each_cpu(cpu, in)
cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
}
EXPORT_SYMBOL_GPL(riscv_cpuid_to_hartid_mask);
/* /*
* Place kernel memory regions on the resource tree so that * Place kernel memory regions on the resource tree so that
* kexec-tools can retrieve them from /proc/iomem. While there * kexec-tools can retrieve them from /proc/iomem. While there
......
...@@ -96,7 +96,7 @@ void __init setup_smp(void) ...@@ -96,7 +96,7 @@ void __init setup_smp(void)
if (cpuid >= NR_CPUS) { if (cpuid >= NR_CPUS) {
pr_warn("Invalid cpuid [%d] for hartid [%d]\n", pr_warn("Invalid cpuid [%d] for hartid [%d]\n",
cpuid, hart); cpuid, hart);
break; continue;
} }
cpuid_to_hartid_map(cpuid) = hart; cpuid_to_hartid_map(cpuid) = hart;
......
...@@ -114,7 +114,6 @@ static bool stage2_get_leaf_entry(struct kvm *kvm, gpa_t addr, ...@@ -114,7 +114,6 @@ static bool stage2_get_leaf_entry(struct kvm *kvm, gpa_t addr,
static void stage2_remote_tlb_flush(struct kvm *kvm, u32 level, gpa_t addr) static void stage2_remote_tlb_flush(struct kvm *kvm, u32 level, gpa_t addr)
{ {
struct cpumask hmask;
unsigned long size = PAGE_SIZE; unsigned long size = PAGE_SIZE;
struct kvm_vmid *vmid = &kvm->arch.vmid; struct kvm_vmid *vmid = &kvm->arch.vmid;
...@@ -127,8 +126,7 @@ static void stage2_remote_tlb_flush(struct kvm *kvm, u32 level, gpa_t addr) ...@@ -127,8 +126,7 @@ static void stage2_remote_tlb_flush(struct kvm *kvm, u32 level, gpa_t addr)
* where the Guest/VM is running. * where the Guest/VM is running.
*/ */
preempt_disable(); preempt_disable();
riscv_cpuid_to_hartid_mask(cpu_online_mask, &hmask); sbi_remote_hfence_gvma_vmid(cpu_online_mask, addr, size,
sbi_remote_hfence_gvma_vmid(cpumask_bits(&hmask), addr, size,
READ_ONCE(vmid->vmid)); READ_ONCE(vmid->vmid));
preempt_enable(); preempt_enable();
} }
......
...@@ -82,7 +82,7 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run ...@@ -82,7 +82,7 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run
{ {
int ret = 0; int ret = 0;
unsigned long i; unsigned long i;
struct cpumask cm, hm; struct cpumask cm;
struct kvm_vcpu *tmp; struct kvm_vcpu *tmp;
struct kvm_cpu_context *cp = &vcpu->arch.guest_context; struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
unsigned long hmask = cp->a0; unsigned long hmask = cp->a0;
...@@ -90,7 +90,6 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run ...@@ -90,7 +90,6 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run
unsigned long funcid = cp->a6; unsigned long funcid = cp->a6;
cpumask_clear(&cm); cpumask_clear(&cm);
cpumask_clear(&hm);
kvm_for_each_vcpu(i, tmp, vcpu->kvm) { kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
if (hbase != -1UL) { if (hbase != -1UL) {
if (tmp->vcpu_id < hbase) if (tmp->vcpu_id < hbase)
...@@ -103,17 +102,15 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run ...@@ -103,17 +102,15 @@ static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run
cpumask_set_cpu(tmp->cpu, &cm); cpumask_set_cpu(tmp->cpu, &cm);
} }
riscv_cpuid_to_hartid_mask(&cm, &hm);
switch (funcid) { switch (funcid) {
case SBI_EXT_RFENCE_REMOTE_FENCE_I: case SBI_EXT_RFENCE_REMOTE_FENCE_I:
ret = sbi_remote_fence_i(cpumask_bits(&hm)); ret = sbi_remote_fence_i(&cm);
break; break;
case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA: case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
ret = sbi_remote_hfence_vvma(cpumask_bits(&hm), cp->a2, cp->a3); ret = sbi_remote_hfence_vvma(&cm, cp->a2, cp->a3);
break; break;
case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID: case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
ret = sbi_remote_hfence_vvma_asid(cpumask_bits(&hm), cp->a2, ret = sbi_remote_hfence_vvma_asid(&cm, cp->a2,
cp->a3, cp->a4); cp->a3, cp->a4);
break; break;
case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA: case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA:
......
...@@ -38,7 +38,7 @@ static int kvm_sbi_ext_v01_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, ...@@ -38,7 +38,7 @@ static int kvm_sbi_ext_v01_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
int i, ret = 0; int i, ret = 0;
u64 next_cycle; u64 next_cycle;
struct kvm_vcpu *rvcpu; struct kvm_vcpu *rvcpu;
struct cpumask cm, hm; struct cpumask cm;
struct kvm *kvm = vcpu->kvm; struct kvm *kvm = vcpu->kvm;
struct kvm_cpu_context *cp = &vcpu->arch.guest_context; struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
...@@ -101,15 +101,12 @@ static int kvm_sbi_ext_v01_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, ...@@ -101,15 +101,12 @@ static int kvm_sbi_ext_v01_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
continue; continue;
cpumask_set_cpu(rvcpu->cpu, &cm); cpumask_set_cpu(rvcpu->cpu, &cm);
} }
riscv_cpuid_to_hartid_mask(&cm, &hm);
if (cp->a7 == SBI_EXT_0_1_REMOTE_FENCE_I) if (cp->a7 == SBI_EXT_0_1_REMOTE_FENCE_I)
ret = sbi_remote_fence_i(cpumask_bits(&hm)); ret = sbi_remote_fence_i(&cm);
else if (cp->a7 == SBI_EXT_0_1_REMOTE_SFENCE_VMA) else if (cp->a7 == SBI_EXT_0_1_REMOTE_SFENCE_VMA)
ret = sbi_remote_hfence_vvma(cpumask_bits(&hm), ret = sbi_remote_hfence_vvma(&cm, cp->a1, cp->a2);
cp->a1, cp->a2);
else else
ret = sbi_remote_hfence_vvma_asid(cpumask_bits(&hm), ret = sbi_remote_hfence_vvma_asid(&cm, cp->a1, cp->a2, cp->a3);
cp->a1, cp->a2, cp->a3);
break; break;
default: default:
ret = -EINVAL; ret = -EINVAL;
......
...@@ -67,7 +67,6 @@ void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu) ...@@ -67,7 +67,6 @@ void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu)
{ {
unsigned long i; unsigned long i;
struct kvm_vcpu *v; struct kvm_vcpu *v;
struct cpumask hmask;
struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid; struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid;
if (!kvm_riscv_stage2_vmid_ver_changed(vmid)) if (!kvm_riscv_stage2_vmid_ver_changed(vmid))
...@@ -102,8 +101,7 @@ void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu) ...@@ -102,8 +101,7 @@ void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu)
* running, we force VM exits on all host CPUs using IPI and * running, we force VM exits on all host CPUs using IPI and
* flush all Guest TLBs. * flush all Guest TLBs.
*/ */
riscv_cpuid_to_hartid_mask(cpu_online_mask, &hmask); sbi_remote_hfence_gvma(cpu_online_mask, 0, 0);
sbi_remote_hfence_gvma(cpumask_bits(&hmask), 0, 0);
} }
vmid->vmid = vmid_next; vmid->vmid = vmid_next;
......
...@@ -67,10 +67,7 @@ void flush_icache_mm(struct mm_struct *mm, bool local) ...@@ -67,10 +67,7 @@ void flush_icache_mm(struct mm_struct *mm, bool local)
*/ */
smp_mb(); smp_mb();
} else if (IS_ENABLED(CONFIG_RISCV_SBI)) { } else if (IS_ENABLED(CONFIG_RISCV_SBI)) {
cpumask_t hartid_mask; sbi_remote_fence_i(&others);
riscv_cpuid_to_hartid_mask(&others, &hartid_mask);
sbi_remote_fence_i(cpumask_bits(&hartid_mask));
} else { } else {
on_each_cpu_mask(&others, ipi_remote_fence_i, NULL, 1); on_each_cpu_mask(&others, ipi_remote_fence_i, NULL, 1);
} }
......
...@@ -192,7 +192,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu) ...@@ -192,7 +192,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
switch_mm_fast: switch_mm_fast:
csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | csr_write(CSR_SATP, virt_to_pfn(mm->pgd) |
((cntx & asid_mask) << SATP_ASID_SHIFT) | ((cntx & asid_mask) << SATP_ASID_SHIFT) |
SATP_MODE); satp_mode);
if (need_flush_tlb) if (need_flush_tlb)
local_flush_tlb_all(); local_flush_tlb_all();
...@@ -201,7 +201,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu) ...@@ -201,7 +201,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
static void set_mm_noasid(struct mm_struct *mm) static void set_mm_noasid(struct mm_struct *mm)
{ {
/* Switch the page table and blindly nuke entire local TLB */ /* Switch the page table and blindly nuke entire local TLB */
csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | SATP_MODE); csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | satp_mode);
local_flush_tlb_all(); local_flush_tlb_all();
} }
......
This diff is collapsed.
...@@ -11,45 +11,27 @@ ...@@ -11,45 +11,27 @@
#include <asm/fixmap.h> #include <asm/fixmap.h>
#include <asm/pgalloc.h> #include <asm/pgalloc.h>
extern pgd_t early_pg_dir[PTRS_PER_PGD]; /*
asmlinkage void __init kasan_early_init(void) * Kasan shadow region must lie at a fixed address across sv39, sv48 and sv57
{ * which is right before the kernel.
uintptr_t i; *
pgd_t *pgd = early_pg_dir + pgd_index(KASAN_SHADOW_START); * For sv39, the region is aligned on PGDIR_SIZE so we only need to populate
* the page global directory with kasan_early_shadow_pmd.
BUILD_BUG_ON(KASAN_SHADOW_OFFSET != *
KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); * For sv48 and sv57, the region is not aligned on PGDIR_SIZE so the mapping
* must be divided as follows:
for (i = 0; i < PTRS_PER_PTE; ++i) * - the first PGD entry, although incomplete, is populated with
set_pte(kasan_early_shadow_pte + i, * kasan_early_shadow_pud/p4d
mk_pte(virt_to_page(kasan_early_shadow_page), * - the PGD entries in the middle are populated with kasan_early_shadow_pud/p4d
PAGE_KERNEL)); * - the last PGD entry is shared with the kernel mapping so populated at the
* lower levels pud/p4d
for (i = 0; i < PTRS_PER_PMD; ++i) *
set_pmd(kasan_early_shadow_pmd + i, * In addition, when shallow populating a kasan region (for example vmalloc),
pfn_pmd(PFN_DOWN * this region may also not be aligned on PGDIR size, so we must go down to the
(__pa((uintptr_t) kasan_early_shadow_pte)), * pud level too.
__pgprot(_PAGE_TABLE))); */
for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
i += PGDIR_SIZE, ++pgd)
set_pgd(pgd,
pfn_pgd(PFN_DOWN
(__pa(((uintptr_t) kasan_early_shadow_pmd))),
__pgprot(_PAGE_TABLE)));
/* init for swapper_pg_dir */
pgd = pgd_offset_k(KASAN_SHADOW_START);
for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
i += PGDIR_SIZE, ++pgd)
set_pgd(pgd,
pfn_pgd(PFN_DOWN
(__pa(((uintptr_t) kasan_early_shadow_pmd))),
__pgprot(_PAGE_TABLE)));
local_flush_tlb_all(); extern pgd_t early_pg_dir[PTRS_PER_PGD];
}
static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned long end) static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned long end)
{ {
...@@ -73,15 +55,19 @@ static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned ...@@ -73,15 +55,19 @@ static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned
set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(base_pte)), PAGE_TABLE)); set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(base_pte)), PAGE_TABLE));
} }
static void __init kasan_populate_pmd(pgd_t *pgd, unsigned long vaddr, unsigned long end) static void __init kasan_populate_pmd(pud_t *pud, unsigned long vaddr, unsigned long end)
{ {
phys_addr_t phys_addr; phys_addr_t phys_addr;
pmd_t *pmdp, *base_pmd; pmd_t *pmdp, *base_pmd;
unsigned long next; unsigned long next;
base_pmd = (pmd_t *)pgd_page_vaddr(*pgd); if (pud_none(*pud)) {
base_pmd = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
} else {
base_pmd = (pmd_t *)pud_pgtable(*pud);
if (base_pmd == lm_alias(kasan_early_shadow_pmd)) if (base_pmd == lm_alias(kasan_early_shadow_pmd))
base_pmd = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE); base_pmd = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
}
pmdp = base_pmd + pmd_index(vaddr); pmdp = base_pmd + pmd_index(vaddr);
...@@ -105,59 +91,207 @@ static void __init kasan_populate_pmd(pgd_t *pgd, unsigned long vaddr, unsigned ...@@ -105,59 +91,207 @@ static void __init kasan_populate_pmd(pgd_t *pgd, unsigned long vaddr, unsigned
* it entirely, memblock could allocate a page at a physical address * it entirely, memblock could allocate a page at a physical address
* where KASAN is not populated yet and then we'd get a page fault. * where KASAN is not populated yet and then we'd get a page fault.
*/ */
set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(base_pmd)), PAGE_TABLE)); set_pud(pud, pfn_pud(PFN_DOWN(__pa(base_pmd)), PAGE_TABLE));
} }
static void __init kasan_populate_pgd(unsigned long vaddr, unsigned long end) static void __init kasan_populate_pud(pgd_t *pgd,
unsigned long vaddr, unsigned long end,
bool early)
{
phys_addr_t phys_addr;
pud_t *pudp, *base_pud;
unsigned long next;
if (early) {
/*
* We can't use pgd_page_vaddr here as it would return a linear
* mapping address but it is not mapped yet, but when populating
* early_pg_dir, we need the physical address and when populating
* swapper_pg_dir, we need the kernel virtual address so use
* pt_ops facility.
*/
base_pud = pt_ops.get_pud_virt(pfn_to_phys(_pgd_pfn(*pgd)));
} else {
base_pud = (pud_t *)pgd_page_vaddr(*pgd);
if (base_pud == lm_alias(kasan_early_shadow_pud))
base_pud = memblock_alloc(PTRS_PER_PUD * sizeof(pud_t), PAGE_SIZE);
}
pudp = base_pud + pud_index(vaddr);
do {
next = pud_addr_end(vaddr, end);
if (pud_none(*pudp) && IS_ALIGNED(vaddr, PUD_SIZE) && (next - vaddr) >= PUD_SIZE) {
if (early) {
phys_addr = __pa(((uintptr_t)kasan_early_shadow_pmd));
set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_TABLE));
continue;
} else {
phys_addr = memblock_phys_alloc(PUD_SIZE, PUD_SIZE);
if (phys_addr) {
set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_KERNEL));
continue;
}
}
}
kasan_populate_pmd(pudp, vaddr, next);
} while (pudp++, vaddr = next, vaddr != end);
/*
* Wait for the whole PGD to be populated before setting the PGD in
* the page table, otherwise, if we did set the PGD before populating
* it entirely, memblock could allocate a page at a physical address
* where KASAN is not populated yet and then we'd get a page fault.
*/
if (!early)
set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(base_pud)), PAGE_TABLE));
}
#define kasan_early_shadow_pgd_next (pgtable_l4_enabled ? \
(uintptr_t)kasan_early_shadow_pud : \
(uintptr_t)kasan_early_shadow_pmd)
#define kasan_populate_pgd_next(pgdp, vaddr, next, early) \
(pgtable_l4_enabled ? \
kasan_populate_pud(pgdp, vaddr, next, early) : \
kasan_populate_pmd((pud_t *)pgdp, vaddr, next))
static void __init kasan_populate_pgd(pgd_t *pgdp,
unsigned long vaddr, unsigned long end,
bool early)
{ {
phys_addr_t phys_addr; phys_addr_t phys_addr;
pgd_t *pgdp = pgd_offset_k(vaddr);
unsigned long next; unsigned long next;
do { do {
next = pgd_addr_end(vaddr, end); next = pgd_addr_end(vaddr, end);
if (IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE) {
if (early) {
phys_addr = __pa((uintptr_t)kasan_early_shadow_pgd_next);
set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_TABLE));
continue;
} else if (pgd_page_vaddr(*pgdp) ==
(unsigned long)lm_alias(kasan_early_shadow_pgd_next)) {
/* /*
* pgdp can't be none since kasan_early_init initialized all KASAN * pgdp can't be none since kasan_early_init
* shadow region with kasan_early_shadow_pmd: if this is stillthe case, * initialized all KASAN shadow region with
* that means we can try to allocate a hugepage as a replacement. * kasan_early_shadow_pud: if this is still the
* case, that means we can try to allocate a
* hugepage as a replacement.
*/ */
if (pgd_page_vaddr(*pgdp) == (unsigned long)lm_alias(kasan_early_shadow_pmd) &&
IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE) {
phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE); phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE);
if (phys_addr) { if (phys_addr) {
set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL)); set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL));
continue; continue;
} }
} }
}
kasan_populate_pmd(pgdp, vaddr, next); kasan_populate_pgd_next(pgdp, vaddr, next, early);
} while (pgdp++, vaddr = next, vaddr != end); } while (pgdp++, vaddr = next, vaddr != end);
} }
asmlinkage void __init kasan_early_init(void)
{
uintptr_t i;
BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
for (i = 0; i < PTRS_PER_PTE; ++i)
set_pte(kasan_early_shadow_pte + i,
mk_pte(virt_to_page(kasan_early_shadow_page),
PAGE_KERNEL));
for (i = 0; i < PTRS_PER_PMD; ++i)
set_pmd(kasan_early_shadow_pmd + i,
pfn_pmd(PFN_DOWN
(__pa((uintptr_t)kasan_early_shadow_pte)),
PAGE_TABLE));
if (pgtable_l4_enabled) {
for (i = 0; i < PTRS_PER_PUD; ++i)
set_pud(kasan_early_shadow_pud + i,
pfn_pud(PFN_DOWN
(__pa(((uintptr_t)kasan_early_shadow_pmd))),
PAGE_TABLE));
}
kasan_populate_pgd(early_pg_dir + pgd_index(KASAN_SHADOW_START),
KASAN_SHADOW_START, KASAN_SHADOW_END, true);
local_flush_tlb_all();
}
void __init kasan_swapper_init(void)
{
kasan_populate_pgd(pgd_offset_k(KASAN_SHADOW_START),
KASAN_SHADOW_START, KASAN_SHADOW_END, true);
local_flush_tlb_all();
}
static void __init kasan_populate(void *start, void *end) static void __init kasan_populate(void *start, void *end)
{ {
unsigned long vaddr = (unsigned long)start & PAGE_MASK; unsigned long vaddr = (unsigned long)start & PAGE_MASK;
unsigned long vend = PAGE_ALIGN((unsigned long)end); unsigned long vend = PAGE_ALIGN((unsigned long)end);
kasan_populate_pgd(vaddr, vend); kasan_populate_pgd(pgd_offset_k(vaddr), vaddr, vend, false);
local_flush_tlb_all(); local_flush_tlb_all();
memset(start, KASAN_SHADOW_INIT, end - start); memset(start, KASAN_SHADOW_INIT, end - start);
} }
static void __init kasan_shallow_populate_pud(pgd_t *pgdp,
unsigned long vaddr, unsigned long end,
bool kasan_populate)
{
unsigned long next;
pud_t *pudp, *base_pud;
pmd_t *base_pmd;
bool is_kasan_pmd;
base_pud = (pud_t *)pgd_page_vaddr(*pgdp);
pudp = base_pud + pud_index(vaddr);
if (kasan_populate)
memcpy(base_pud, (void *)kasan_early_shadow_pgd_next,
sizeof(pud_t) * PTRS_PER_PUD);
do {
next = pud_addr_end(vaddr, end);
is_kasan_pmd = (pud_pgtable(*pudp) == lm_alias(kasan_early_shadow_pmd));
if (is_kasan_pmd) {
base_pmd = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
set_pud(pudp, pfn_pud(PFN_DOWN(__pa(base_pmd)), PAGE_TABLE));
}
} while (pudp++, vaddr = next, vaddr != end);
}
static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end) static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end)
{ {
unsigned long next; unsigned long next;
void *p; void *p;
pgd_t *pgd_k = pgd_offset_k(vaddr); pgd_t *pgd_k = pgd_offset_k(vaddr);
bool is_kasan_pgd_next;
do { do {
next = pgd_addr_end(vaddr, end); next = pgd_addr_end(vaddr, end);
if (pgd_page_vaddr(*pgd_k) == (unsigned long)lm_alias(kasan_early_shadow_pmd)) { is_kasan_pgd_next = (pgd_page_vaddr(*pgd_k) ==
(unsigned long)lm_alias(kasan_early_shadow_pgd_next));
if (is_kasan_pgd_next) {
p = memblock_alloc(PAGE_SIZE, PAGE_SIZE); p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE)); set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
} }
if (IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE)
continue;
kasan_shallow_populate_pud(pgd_k, vaddr, next, is_kasan_pgd_next);
} while (pgd_k++, vaddr = next, vaddr != end); } while (pgd_k++, vaddr = next, vaddr != end);
} }
......
...@@ -32,7 +32,6 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start, ...@@ -32,7 +32,6 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
unsigned long size, unsigned long stride) unsigned long size, unsigned long stride)
{ {
struct cpumask *cmask = mm_cpumask(mm); struct cpumask *cmask = mm_cpumask(mm);
struct cpumask hmask;
unsigned int cpuid; unsigned int cpuid;
bool broadcast; bool broadcast;
...@@ -46,9 +45,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start, ...@@ -46,9 +45,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
unsigned long asid = atomic_long_read(&mm->context.id); unsigned long asid = atomic_long_read(&mm->context.id);
if (broadcast) { if (broadcast) {
riscv_cpuid_to_hartid_mask(cmask, &hmask); sbi_remote_sfence_vma_asid(cmask, start, size, asid);
sbi_remote_sfence_vma_asid(cpumask_bits(&hmask),
start, size, asid);
} else if (size <= stride) { } else if (size <= stride) {
local_flush_tlb_page_asid(start, asid); local_flush_tlb_page_asid(start, asid);
} else { } else {
...@@ -56,9 +53,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start, ...@@ -56,9 +53,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
} }
} else { } else {
if (broadcast) { if (broadcast) {
riscv_cpuid_to_hartid_mask(cmask, &hmask); sbi_remote_sfence_vma(cmask, start, size);
sbi_remote_sfence_vma(cpumask_bits(&hmask),
start, size);
} else if (size <= stride) { } else if (size <= stride) {
local_flush_tlb_page(start); local_flush_tlb_page(start);
} else { } else {
......
...@@ -497,7 +497,7 @@ static int add_exception_handler(const struct bpf_insn *insn, ...@@ -497,7 +497,7 @@ static int add_exception_handler(const struct bpf_insn *insn,
offset = pc - (long)&ex->insn; offset = pc - (long)&ex->insn;
if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN)) if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
return -ERANGE; return -ERANGE;
ex->insn = pc; ex->insn = offset;
/* /*
* Since the extable follows the program, the fixup offset is always * Since the extable follows the program, the fixup offset is always
......
...@@ -40,6 +40,8 @@ ...@@ -40,6 +40,8 @@
#ifdef CONFIG_ARM64 #ifdef CONFIG_ARM64
# define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64 # define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64
#elif defined(CONFIG_RISCV)
# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_MIN
#else #else
# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
#endif #endif
......
...@@ -5,7 +5,6 @@ config SOC_K210_SYSCTL ...@@ -5,7 +5,6 @@ config SOC_K210_SYSCTL
depends on RISCV && SOC_CANAAN && OF depends on RISCV && SOC_CANAAN && OF
default SOC_CANAAN default SOC_CANAAN
select PM select PM
select SYSCON
select MFD_SYSCON select MFD_SYSCON
help help
Canaan Kendryte K210 SoC system controller driver. Canaan Kendryte K210 SoC system controller driver.
...@@ -147,6 +147,15 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) ...@@ -147,6 +147,15 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
#if CONFIG_PGTABLE_LEVELS > 3 #if CONFIG_PGTABLE_LEVELS > 3
static inline pud_t *__pud_alloc_one(struct mm_struct *mm, unsigned long addr)
{
gfp_t gfp = GFP_PGTABLE_USER;
if (mm == &init_mm)
gfp = GFP_PGTABLE_KERNEL;
return (pud_t *)get_zeroed_page(gfp);
}
#ifndef __HAVE_ARCH_PUD_ALLOC_ONE #ifndef __HAVE_ARCH_PUD_ALLOC_ONE
/** /**
* pud_alloc_one - allocate a page for PUD-level page table * pud_alloc_one - allocate a page for PUD-level page table
...@@ -159,20 +168,23 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) ...@@ -159,20 +168,23 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
*/ */
static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr) static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
{ {
gfp_t gfp = GFP_PGTABLE_USER; return __pud_alloc_one(mm, addr);
if (mm == &init_mm)
gfp = GFP_PGTABLE_KERNEL;
return (pud_t *)get_zeroed_page(gfp);
} }
#endif #endif
static inline void pud_free(struct mm_struct *mm, pud_t *pud) static inline void __pud_free(struct mm_struct *mm, pud_t *pud)
{ {
BUG_ON((unsigned long)pud & (PAGE_SIZE-1)); BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
free_page((unsigned long)pud); free_page((unsigned long)pud);
} }
#ifndef __HAVE_ARCH_PUD_FREE
static inline void pud_free(struct mm_struct *mm, pud_t *pud)
{
__pud_free(mm, pud);
}
#endif
#endif /* CONFIG_PGTABLE_LEVELS > 3 */ #endif /* CONFIG_PGTABLE_LEVELS > 3 */
#ifndef __HAVE_ARCH_PGD_FREE #ifndef __HAVE_ARCH_PGD_FREE
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment