RISC-V nommu support v4
Subsystems (8)
Hi all,
below is a series to support nommu mode on RISC-V. For now this series just works under qemu with the qemu-virt platform, but Damien has also been able to get kernel based on this tree with additional driver hacks to work on the Kendryte KD210, but that will take a while to cleanup an upstream.
A git tree is available here:
git://git.infradead.org/users/hch/riscv.git riscv-nommu.4
Gitweb:
http://git.infradead.org/users/hch/riscv.git/shortlog/refs/heads/riscv-nommu.4
I've also pushed out a builtroot branch that can build a RISC-V nommu root filesystem here:
git://git.infradead.org/users/hch/buildroot.git riscv-nommu.2
Gitweb:
http://git.infradead.org/users/hch/buildroot.git/shortlog/refs/heads/riscv-nommu.2
Changes since v3:
- improve a few commit message
- cleanup riscv_cpuid_to_hartid_mask
- cleanup the timer handling
- cleanup the IPI handling a little more
- renamed CONFIG_M_MODE to CONFIG_RISCV_M_MODE
- split out CONFIG_RISCV_SBI to make some of the ifdefs more obbious
- use IS_ENABLED wherever possible instead of if ifdefs to make the code more readable
Changes since v2:
- rebased to 5.3-rc
- remove the EFI image header for nommu builds
- set ARCH_SLAB_MINALIGN to ensure stack alignment in the flat binary loader
- minor comment improvement
- use #defines for more CSRs
Changes since v1:
- fixes so that a kernel with this series still work on builds with an IOMMU
- small clint cleanups
- the binfmt_flat base and buildroot now don't put arguments on the stack
PATCH 1/20 irqchip/sifive-plic: set max threshold for ignored handlers
When running in M-mode, the S-mode plic handlers are still listed in the device tree. Ignore them by setting the maximum threshold.
--- drivers/irqchip/irq-sifive-plic.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/irqchip/irq-sifive-plic.c +10 -2
@@ -244,6 +244,7 @@ static int __init plic_init(struct device_node *node,struct plic_handler *handler;irq_hw_number_t hwirq;int cpu, hartid; +u32 threshold =0 ;if (of_irq_parse_one (node, i, &parent)) {pr_err ("failed to parse parent for context %d.\n" , i);
@@ -266,10 +267,16 @@ static int __init plic_init(struct device_node *node,continue ; } +/* +* When running in M-mode we need to ignore the S-mode handler. +* Here we assume it always comes later, but that might be a +* little fragile. +*/ handler =per_cpu_ptr (&plic_handlers, cpu);if (handler->present) {pr_warn ("handler already present for context %d.\n" , i); -continue ; + threshold =0xffffffff ; + goto done; } handler->present = true;
@@ -279,8 +286,9 @@ static int __init plic_init(struct device_node *node, handler->enable_base = plic_regs +ENABLE_BASE + i *ENABLE_PER_HART ; +done:/* priority must be > threshold to trigger an interrupt */ -writel (0 , handler->hart_base +CONTEXT_THRESHOLD ); +writel (threshold, handler->hart_base +CONTEXT_THRESHOLD );for (hwirq =1 ; hwirq <= nr_irqs; hwirq++)plic_toggle (handler, hwirq,0 ); nr_handlers++;
Signature · 1 line
2.20.1
PATCH 2/20 riscv: refactor the IPI code
In reply to Christoph HellwigThis prepares for adding native non-SBI IPI code.
--- arch/riscv/kernel/smp.c | 55 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 24 deletions(-)
arch/riscv/kernel/smp.c +31 -24
@@ -78,13 +78,38 @@ static void ipi_stop(void)wait_for_interrupt (); } +static void send_ipi_mask (const struct cpumask *mask,enum ipi_message_type op) +{ +int cpuid, hartid; +struct cpumask hartid_mask; + +cpumask_clear (&hartid_mask); +mb (); +for_each_cpu (cpuid, mask) { +set_bit (op, &ipi_data[cpuid].bits); + hartid =cpuid_to_hartid_map (cpuid); +cpumask_set_cpu (hartid, &hartid_mask); + } +mb (); +sbi_send_ipi (cpumask_bits (&hartid_mask)); +} + +static void send_ipi_single (int cpu,enum ipi_message_type op) +{ +send_ipi_mask (cpumask_of (cpu), op); +} + +static inline void clear_ipi (void ) +{ +csr_clear (CSR_SIP ,SIE_SSIE ); +} +void riscv_software_interrupt (void ) {unsigned long *pending_ipis = &ipi_data[smp_processor_id ()].bits;unsigned long *stats = ipi_data[smp_processor_id ()].stats; -/* Clear pending IPI */ -csr_clear (CSR_SIP ,SIE_SSIE ); +clear_ipi ();while (true) {unsigned long ops;
@@ -118,23 +143,6 @@ void riscv_software_interrupt(void) } } -static void -send_ipi_message (const struct cpumask *to_whom,enum ipi_message_type operation) -{ -int cpuid, hartid; -struct cpumask hartid_mask; - -cpumask_clear (&hartid_mask); -mb (); -for_each_cpu (cpuid, to_whom) { -set_bit (operation, &ipi_data[cpuid].bits); - hartid =cpuid_to_hartid_map (cpuid); -cpumask_set_cpu (hartid, &hartid_mask); - } -mb (); -sbi_send_ipi (cpumask_bits (&hartid_mask)); -} -static const char *const ipi_names[] = { [IPI_RESCHEDULE ] ="Rescheduling interrupts" , [IPI_CALL_FUNC ] ="Function call interrupts" ,
@@ -156,12 +164,12 @@ void show_ipi_stats(struct seq_file *p, int prec)void arch_send_call_function_ipi_mask (struct cpumask *mask) { -send_ipi_message (mask,IPI_CALL_FUNC ); +send_ipi_mask (mask,IPI_CALL_FUNC ); }void arch_send_call_function_single_ipi (int cpu) { -send_ipi_message (cpumask_of (cpu),IPI_CALL_FUNC ); +send_ipi_single (cpu,IPI_CALL_FUNC ); }void smp_send_stop (void )
@@ -176,7 +184,7 @@ void smp_send_stop(void)if (system_state <=SYSTEM_RUNNING )pr_crit ("SMP: stopping secondary CPUs\n" ); -send_ipi_message (&mask,IPI_CPU_STOP ); +send_ipi_mask (&mask,IPI_CPU_STOP ); }/* Wait up to one second for other CPUs to stop */
@@ -191,6 +199,5 @@ void smp_send_stop(void)void smp_send_reschedule (int cpu) { -send_ipi_message (cpumask_of (cpu),IPI_RESCHEDULE ); +send_ipi_single (cpu,IPI_RESCHEDULE ); } -
Signature · 7 lines
2.20.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 3/20 riscv: cleanup send_ipi_mask
In reply to Christoph HellwigUse the special barriers for atomic bitops to make the intention a little more clear, and use riscv_cpuid_to_hartid_mask instead of open coding it.
--- arch/riscv/kernel/smp.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
arch/riscv/kernel/smp.c +7 -9
@@ -80,17 +80,15 @@ static void ipi_stop(void)static void send_ipi_mask (const struct cpumask *mask,enum ipi_message_type op) { -int cpuid, hartid;struct cpumask hartid_mask; +int cpu; -cpumask_clear (&hartid_mask); -mb (); -for_each_cpu (cpuid, mask) { -set_bit (op, &ipi_data[cpuid].bits); - hartid =cpuid_to_hartid_map (cpuid); -cpumask_set_cpu (hartid, &hartid_mask); - } -mb (); +smp_mb__before_atomic (); +for_each_cpu (cpu, mask) +set_bit (op, &ipi_data[cpu].bits); +smp_mb__after_atomic (); + +riscv_cpuid_to_hartid_mask (mask, &hartid_mask);sbi_send_ipi (cpumask_bits (&hartid_mask)); }
Signature · 1 line
2.20.1
PATCH 4/20 riscv: optimize send_ipi_single
In reply to Christoph HellwigDon't go through send_ipi_mask, but just set the op bit an then pass a simple generate hartid mask directly to sbi_send_ipi.
--- arch/riscv/kernel/smp.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/smp.c +7 -1
@@ -94,7 +94,13 @@ static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)static void send_ipi_single (int cpu,enum ipi_message_type op) { -send_ipi_mask (cpumask_of (cpu), op); +int hartid =cpuid_to_hartid_map (cpu); + +smp_mb__before_atomic (); +set_bit (op, &ipi_data[cpu].bits); +smp_mb__after_atomic (); + +sbi_send_ipi (cpumask_bits (cpumask_of (hartid))); }static inline void clear_ipi (void )
Signature · 1 line
2.20.1
PATCH 5/20 riscv: cleanup riscv_cpuid_to_hartid_mask
In reply to Christoph HellwigMove the initial clearing of the mask from the callers to riscv_cpuid_to_hartid_mask, and remove the unused !CONFIG_SMP stub.
--- arch/riscv/include/asm/smp.h | 6 ------ arch/riscv/include/asm/tlbflush.h | 1 - arch/riscv/kernel/smp.c | 1 + arch/riscv/mm/cacheflush.c | 1 - 4 files changed, 1 insertion(+), 8 deletions(-)
arch/riscv/include/asm/smp.h +0 -6
@@ -61,11 +61,5 @@ static inline unsigned long cpuid_to_hartid_map(int cpu)return boot_cpu_hartid; } -static inline void riscv_cpuid_to_hartid_mask (const struct cpumask *in, -struct cpumask *out) -{ -cpumask_set_cpu (cpuid_to_hartid_map (0 ), out); -} -#endif /* CONFIG_SMP */ #endif /* _ASM_RISCV_SMP_H */
arch/riscv/include/asm/tlbflush.h +0 -1
@@ -47,7 +47,6 @@ static inline void remote_sfence_vma(struct cpumask *cmask, unsigned long start, {struct cpumask hmask; -cpumask_clear (&hmask);riscv_cpuid_to_hartid_mask (cmask, &hmask);sbi_remote_sfence_vma (hmask.bits, start, size); }
arch/riscv/kernel/smp.c +1 -0
@@ -56,6 +56,7 @@ 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); }
arch/riscv/mm/cacheflush.c +0 -1
@@ -47,7 +47,6 @@ void flush_icache_mm(struct mm_struct *mm, bool local)cpumask_andnot (&others,mm_cpumask (mm),cpumask_of (cpu)); local |=cpumask_empty (&others);if (mm != current->active_mm || !local) { -cpumask_clear (&hmask);riscv_cpuid_to_hartid_mask (&others, &hmask);sbi_remote_fence_i (hmask.bits); }else {
Signature · 1 line
2.20.1
PATCH 6/20 riscv: don't use the rdtime(h) pseudo-instructions
In reply to Christoph HellwigIf we just use the CSRs that these map to directly the code is simpler and doesn't require extra inline assembly code. Also fix up the top-level comment in timer-riscv.c to not talk about the cycle count or mention details of the clocksource interface, of which this file is just a consumer.
--- arch/riscv/include/asm/timex.h | 44 +++++++++++++++---------------- drivers/clocksource/timer-riscv.c | 17 +++--------- 2 files changed, 25 insertions(+), 36 deletions(-)
arch/riscv/include/asm/timex.h +21 -23
@@ -6,43 +6,41 @@#ifndef _ASM_RISCV_TIMEX_H#define _ASM_RISCV_TIMEX_H -#include <asm/param.h> +#include <asm/csr.h> typedef unsigned long cycles_t ; -static inline cycles_t get_cycles_inline (void ) +static inline cycles_t get_cycles (void ) { -cycles_t n; - - __asm__ __volatile__ ( -"rdtime %0" - :"=r" (n)); -return n; +return csr_read (CSR_TIME ); } -#define get_cycles get_cycles_inline +#define get_cycles get_cycles#ifdef CONFIG_64BIT -static inline uint64_t get_cycles64 (void ) +static inline u64 get_cycles64 (void ) +{ +return get_cycles (); +} +#else /* CONFIG_64BIT */ +static inline u32 get_cycles_hi (void ) { -return get_cycles (); +return csr_read (CSR_TIMEH ); } -#else -static inline uint64_t get_cycles64 (void ) + +static inline u64 get_cycles64 (void ) { -u32 lo, hi, tmp; - __asm__ __volatile__ ( -"1:\n" -"rdtimeh %0\n" -"rdtime %1\n" -"rdtimeh %2\n" -"bne %0, %2, 1b" - :"=&r" (hi),"=&r" (lo),"=&r" (tmp)); +u32 hi, lo; + +do { + hi =get_cycles_hi (); + lo =get_cycles (); + }while (hi !=get_cycles_hi ()); +return ((u64 )hi <<32 ) | lo; } -#endif +#endif /* CONFIG_64BIT */ #define ARCH_HAS_READ_CURRENT_TIMER -static inline int read_current_timer (unsigned long *timer_val) { *timer_val =get_cycles ();
drivers/clocksource/timer-riscv.c +4 -13
@@ -2,6 +2,10 @@/* * Copyright (C) 2012 Regents of the University of California * Copyright (C) 2017 SiFive +* +* All RISC-V systems have a timer attached to every hart. These timers can be +* read from the "time" and "timeh" CSRs, and can use the SBI to setup +* events. */ #include <linux/clocksource.h> #include <linux/clockchips.h>
@@ -12,19 +16,6 @@#include <asm/smp.h> #include <asm/sbi.h> -/* -* All RISC-V systems have a timer attached to every hart. These timers can be -* read by the 'rdcycle' pseudo instruction, and can use the SBI to setup -* events. In order to abstract the architecture-specific timer reading and -* setting functions away from the clock event insertion code, we provide -* function pointers to the clockevent subsystem that perform two basic -* operations: rdtime() reads the timer on the current CPU, and -* next_event(delta) sets the next timer event to 'delta' cycles in the future. -* As the timers are inherently a per-cpu resource, these callbacks perform -* operations on the current hart. There is guaranteed to be exactly one timer -* per hart on all RISC-V systems. -*/ -static int riscv_clock_next_event (unsigned long delta,struct clock_event_device *ce) {
Signature · 1 line
2.20.1
PATCH 7/20 riscv: move the TLB flush logic out of line
In reply to Christoph HellwigThe TLB flush logic is going to become more complex. Start moving it out of line.
--- arch/riscv/include/asm/tlbflush.h | 37 ++++++------------------------- arch/riscv/mm/Makefile | 3 +++ arch/riscv/mm/tlbflush.c | 35 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 arch/riscv/mm/tlbflush.c
arch/riscv/include/asm/tlbflush.h +7 -30
@@ -25,8 +25,13 @@ static inline void local_flush_tlb_page(unsigned long addr) __asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" ); } -#ifndef CONFIG_SMP - +#ifdef CONFIG_SMP +void flush_tlb_all (void ); +void flush_tlb_mm (struct mm_struct *mm); +void flush_tlb_page (struct vm_area_struct *vma,unsigned long addr); +void flush_tlb_range (struct vm_area_struct *vma,unsigned long start, +unsigned long end); +#else /* CONFIG_SMP */ #define flush_tlb_all () local_flush_tlb_all()#define flush_tlb_page (vma, addr) local_flush_tlb_page(addr)
@@ -37,34 +42,6 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, }#define flush_tlb_mm (mm) flush_tlb_all() - -#else /* CONFIG_SMP */ - -#include <asm/sbi.h> - -static inline void remote_sfence_vma (struct cpumask *cmask,unsigned long start, -unsigned long size) -{ -struct cpumask hmask; - -riscv_cpuid_to_hartid_mask (cmask, &hmask); -sbi_remote_sfence_vma (hmask.bits, start, size); -} - -#define flush_tlb_all () sbi_remote_sfence_vma(NULL, 0, -1) - -#define flush_tlb_range (vma, start, end) \ - remote_sfence_vma(mm_cpumask((vma)->vm_mm), start, (end) - (start)) - -static inline void flush_tlb_page (struct vm_area_struct *vma, -unsigned long addr) -{ -flush_tlb_range (vma, addr, addr +PAGE_SIZE ); -} - -#define flush_tlb_mm (mm) \ - remote_sfence_vma(mm_cpumask(mm), 0, -1) -#endif /* CONFIG_SMP */ /* Flush a range of kernel pages */
arch/riscv/mm/Makefile +3 -0
@@ -13,4 +13,7 @@ obj-y += cacheflush.o obj-y += context.o obj-y += sifive_l2_cache.o +ifeq ($(CONFIG_MMU),y) +obj-$(CONFIG_SMP) += tlbflush.o +endif obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
arch/riscv/mm/tlbflush.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/mm.h> +#include <linux/smp.h> +#include <asm/sbi.h> + +void flush_tlb_all (void ) +{ +sbi_remote_sfence_vma (NULL ,0 ,-1 ); +} + +static void __sbi_tlb_flush_range (struct cpumask *cmask,unsigned long start, +unsigned long size) +{ +struct cpumask hmask; + +riscv_cpuid_to_hartid_mask (cmask, &hmask); +sbi_remote_sfence_vma (hmask.bits, start, size); +} + +void flush_tlb_mm (struct mm_struct *mm) +{ +__sbi_tlb_flush_range (mm_cpumask (mm),0 ,-1 ); +} + +void flush_tlb_page (struct vm_area_struct *vma,unsigned long addr) +{ +__sbi_tlb_flush_range (mm_cpumask (vma->vm_mm), addr,PAGE_SIZE ); +} + +void flush_tlb_range (struct vm_area_struct *vma,unsigned long start, +unsigned long end) +{ +__sbi_tlb_flush_range (mm_cpumask (vma->vm_mm), start, end - start); +}
Signature · 1 line
2.20.1
PATCH 8/20 riscv: abstract out CSR names for supervisor vs machine mode
In reply to Christoph HellwigMany of the privileged CSRs exist in a supervisor and machine version that are used very similarly. Provide a new X-naming layer so that we don't have to ifdef everywhere for M-mode Linux support.
Contains contributions from Damien Le Moal <[email protected]>.
--- arch/riscv/Kconfig | 4 ++ arch/riscv/include/asm/asm.h | 6 +++ arch/riscv/include/asm/csr.h | 58 ++++++++++++++++++++++++++-- arch/riscv/include/asm/irqflags.h | 12 +++--- arch/riscv/include/asm/processor.h | 2 +- arch/riscv/include/asm/ptrace.h | 16 ++++---- arch/riscv/include/asm/switch_to.h | 10 ++--- arch/riscv/kernel/asm-offsets.c | 8 ++-- arch/riscv/kernel/entry.S | 62 ++++++++++++++++-------------- arch/riscv/kernel/fpu.S | 8 ++-- arch/riscv/kernel/head.S | 12 +++--- arch/riscv/kernel/irq.c | 4 +- arch/riscv/kernel/process.c | 15 ++++---- arch/riscv/kernel/signal.c | 21 +++++----- arch/riscv/kernel/traps.c | 16 ++++---- arch/riscv/lib/uaccess.S | 12 +++--- arch/riscv/mm/extable.c | 4 +- arch/riscv/mm/fault.c | 6 +-- drivers/clocksource/timer-riscv.c | 8 ++-- drivers/irqchip/irq-sifive-plic.c | 4 +- 20 files changed, 178 insertions(+), 110 deletions(-)
arch/riscv/Kconfig +4 -0
@@ -57,6 +57,10 @@ config RISCV select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select SPARSEMEM_STATIC if 32BIT +# set if we run in machine mode, cleared if we run in supervisor mode +config RISCV_M_MODE + bool + config MMU def_bool y
arch/riscv/include/asm/asm.h +6 -0
@@ -65,4 +65,10 @@#error "Unexpected __SIZEOF_SHORT__"#endif +#ifdef CONFIG_RISCV_M_MODE +# define Xret mret +#else +# define Xret sret +#endif +#endif /* _ASM_RISCV_ASM_H */
arch/riscv/include/asm/csr.h +55 -3
88 lines not on this page · raw message
arch/riscv/include/asm/irqflags.h +6 -6
@@ -13,31 +13,31 @@/* read interrupt enabled status */ static inline unsigned long arch_local_save_flags (void ) { -return csr_read (CSR_SSTATUS ); +return csr_read (CSR_XSTATUS ); }/* unconditionally enable interrupts */ static inline void arch_local_irq_enable (void ) { -csr_set (CSR_SSTATUS ,SR_SIE ); +csr_set (CSR_XSTATUS ,SR_XIE ); }/* unconditionally disable interrupts */ static inline void arch_local_irq_disable (void ) { -csr_clear (CSR_SSTATUS ,SR_SIE ); +csr_clear (CSR_XSTATUS ,SR_XIE ); }/* get status and disable interrupts */ static inline unsigned long arch_local_irq_save (void ) { -return csr_read_clear (CSR_SSTATUS ,SR_SIE ); +return csr_read_clear (CSR_XSTATUS ,SR_XIE ); }/* test flags */ static inline int arch_irqs_disabled_flags (unsigned long flags) { -return !(flags &SR_SIE ); +return !(flags &SR_XIE ); }/* test hardware interrupt enable bit */
@@ -49,7 +49,7 @@ static inline int arch_irqs_disabled(void)/* set interrupt enabled status */ static inline void arch_local_irq_restore (unsigned long flags) { -csr_set (CSR_SSTATUS , flags &SR_SIE ); +csr_set (CSR_XSTATUS , flags &SR_XIE ); }#endif /* _ASM_RISCV_IRQFLAGS_H */
arch/riscv/include/asm/processor.h +1 -1
@@ -42,7 +42,7 @@ struct thread_struct { ((struct pt_regs *)(task_stack_page (tsk) +THREAD_SIZE \ - ALIGN(sizeof(struct pt_regs), STACK_ALIGN))) -#define KSTK_EIP (tsk) (task_pt_regs(tsk)->sepc) +#define KSTK_EIP (tsk) (task_pt_regs(tsk)->xepc)#define KSTK_ESP (tsk) (task_pt_regs(tsk)->sp)
arch/riscv/include/asm/ptrace.h +8 -8
@@ -12,7 +12,7 @@ #ifndef __ASSEMBLY__ struct pt_regs { - unsigned long sepc; + unsigned long xepc; unsigned long ra; unsigned long sp; unsigned long gp;
@@ -44,10 +44,10 @@ struct pt_regs {unsigned long t4;unsigned long t5;unsigned long t6; -/* Supervisor CSRs */ -unsigned long sstatus; -unsigned long sbadaddr; -unsigned long scause; +/* Supervisor/Machine CSRs */ +unsigned long xstatus; +unsigned long xbadaddr; +unsigned long xcause;/* a0 value before the syscall */ unsigned long orig_a0; };
@@ -58,18 +58,18 @@ struct pt_regs {#define REG_FMT "%08lx"#endif -#define user_mode (regs) (((regs)->sstatus & SR_SPP) == 0) +#define user_mode (regs) (((regs)->xstatus & SR_XPP) == 0)/* Helpers for working with the instruction pointer */ static inline unsigned long instruction_pointer (struct pt_regs *regs) { -return regs->sepc; +return regs->xepc; }static inline void instruction_pointer_set (struct pt_regs *regs,unsigned long val) { - regs->sepc = val; + regs->xepc = val; }#define profile_pc (regs) instruction_pointer(regs)
arch/riscv/include/asm/switch_to.h +5 -5
@@ -16,19 +16,19 @@ extern void __fstate_restore(struct task_struct *restore_from);static inline void __fstate_clean (struct pt_regs *regs) { - regs->sstatus = (regs->sstatus & ~SR_FS ) |SR_FS_CLEAN ; + regs->xstatus = (regs->xstatus & ~SR_FS ) |SR_FS_CLEAN ; }static inline void fstate_off (struct task_struct *task,struct pt_regs *regs) { - regs->sstatus = (regs->sstatus & ~SR_FS ) |SR_FS_OFF ; + regs->xstatus = (regs->xstatus & ~SR_FS ) |SR_FS_OFF ; }static inline void fstate_save (struct task_struct *task,struct pt_regs *regs) { -if ((regs->sstatus &SR_FS ) ==SR_FS_DIRTY ) { +if ((regs->xstatus &SR_FS ) ==SR_FS_DIRTY ) {__fstate_save (task);__fstate_clean (regs); }
@@ -37,7 +37,7 @@ static inline void fstate_save(struct task_struct *task,static inline void fstate_restore (struct task_struct *task,struct pt_regs *regs) { -if ((regs->sstatus &SR_FS ) !=SR_FS_OFF ) { +if ((regs->xstatus &SR_FS ) !=SR_FS_OFF ) {__fstate_restore (task);__fstate_clean (regs); }
@@ -49,7 +49,7 @@ static inline void __switch_to_aux(struct task_struct *prev,struct pt_regs *regs; regs =task_pt_regs (prev); -if (unlikely (regs->sstatus &SR_SD )) +if (unlikely (regs->xstatus &SR_SD ))fstate_save (prev, regs);fstate_restore (next,task_pt_regs (next)); }
arch/riscv/kernel/asm-offsets.c +4 -4
@@ -71,7 +71,7 @@ void asm_offsets(void)OFFSET (TASK_THREAD_FCSR , task_struct, thread.fstate.fcsr);DEFINE (PT_SIZE ,sizeof (struct pt_regs )); -OFFSET (PT_SEPC , pt_regs, sepc); +OFFSET (PT_XEPC , pt_regs, xepc);OFFSET (PT_RA , pt_regs, ra);OFFSET (PT_FP , pt_regs, s0);OFFSET (PT_S0 , pt_regs, s0);
@@ -105,9 +105,9 @@ void asm_offsets(void)OFFSET (PT_T6 , pt_regs, t6);OFFSET (PT_GP , pt_regs, gp);OFFSET (PT_ORIG_A0 , pt_regs, orig_a0); -OFFSET (PT_SSTATUS , pt_regs, sstatus); -OFFSET (PT_SBADADDR , pt_regs, sbadaddr); -OFFSET (PT_SCAUSE , pt_regs, scause); +OFFSET (PT_XSTATUS , pt_regs, xstatus); +OFFSET (PT_XBADADDR , pt_regs, xbadaddr); +OFFSET (PT_XCAUSE , pt_regs, xcause);/* * THREAD_{F,X}* might be larger than a S-type offset can handle, but
arch/riscv/kernel/entry.S +34 -28
128 lines not on this page · raw message
arch/riscv/kernel/fpu.S +4 -4
@@ -23,7 +23,7 @@ ENTRY(__fstate_save) li a2, TASK_THREAD_F0 add a0, a0, a2 li t1, SR_FS - csrs CSR_SSTATUS, t1 + csrs CSR_XSTATUS, t1 frcsr t0 fsd f0, TASK_THREAD_F0_F0(a0) fsd f1, TASK_THREAD_F1_F0(a0)
@@ -58,7 +58,7 @@ ENTRY(__fstate_save) fsd f30, TASK_THREAD_F30_F0(a0) fsd f31, TASK_THREAD_F31_F0(a0) sw t0, TASK_THREAD_FCSR_F0(a0) - csrc CSR_SSTATUS, t1 + csrc CSR_XSTATUS, t1 ret ENDPROC(__fstate_save)
@@ -67,7 +67,7 @@ ENTRY(__fstate_restore) add a0, a0, a2 li t1, SR_FS lw t0, TASK_THREAD_FCSR_F0(a0) - csrs CSR_SSTATUS, t1 + csrs CSR_XSTATUS, t1 fld f0, TASK_THREAD_F0_F0(a0) fld f1, TASK_THREAD_F1_F0(a0) fld f2, TASK_THREAD_F2_F0(a0)
@@ -101,6 +101,6 @@ ENTRY(__fstate_restore) fld f30, TASK_THREAD_F30_F0(a0) fld f31, TASK_THREAD_F31_F0(a0) fscsr t0 - csrc CSR_SSTATUS, t1 + csrc CSR_XSTATUS, t1 ret ENDPROC(__fstate_restore)
arch/riscv/kernel/head.S +6 -6
@@ -47,8 +47,8 @@ ENTRY(_start) .global _start_kernel _start_kernel: /* Mask all interrupts */ - csrw CSR_SIE, zero - csrw CSR_SIP, zero + csrw CSR_XIE, zero + csrw CSR_XIP, zero /* Load the global pointer */ .option push
@@ -61,7 +61,7 @@ _start_kernel: * floating point in kernel space */ li t0, SR_FS - csrc CSR_SSTATUS, t0 + csrc CSR_XSTATUS, t0 /* Pick one hart to run the main boot sequence */ la a3, hart_lottery
@@ -111,7 +111,7 @@ relocate: /* Point stvec to virtual address of intruction after satp write */ la a2, 1f add a2, a2, a1 - csrw CSR_STVEC, a2 + csrw CSR_XTVEC, a2 /* Compute satp for kernel page tables, but don't load it yet */ srl a2, a0, PAGE_SHIFT
@@ -133,7 +133,7 @@ relocate: 1: /* Set trap vector to spin forever to help debug */ la a0, .Lsecondary_park - csrw CSR_STVEC, a0 + csrw CSR_XTVEC, a0 /* Reload the global pointer */ .option push
@@ -159,7 +159,7 @@ relocate: /* Set trap vector to spin forever to help debug */ la a3, .Lsecondary_park - csrw CSR_STVEC, a3 + csrw CSR_XTVEC, a3 slli a3, a0, LGREG la a1, __cpu_up_stack_pointer
arch/riscv/kernel/irq.c +2 -2
@@ -29,7 +29,7 @@ asmlinkage void __irq_entry do_IRQ(struct pt_regs *regs)struct pt_regs *old_regs =set_irq_regs (regs);irq_enter (); -switch (regs->scause & ~SCAUSE_IRQ_FLAG ) { +switch (regs->xcause & ~XCAUSE_IRQ_FLAG ) {case INTERRUPT_CAUSE_TIMER :riscv_timer_interrupt ();break ;
@@ -46,7 +46,7 @@ asmlinkage void __irq_entry do_IRQ(struct pt_regs *regs)handle_arch_irq (regs);break ;default : -pr_alert ("unexpected interrupt cause 0x%lx" , regs->scause); +pr_alert ("unexpected interrupt cause 0x%lx" , regs->xcause);BUG (); }irq_exit ();
arch/riscv/kernel/process.c +8 -7
@@ -34,7 +34,7 @@ void show_regs(struct pt_regs *regs)show_regs_print_info (KERN_DEFAULT );pr_cont ("sepc: " REG_FMT " ra : " REG_FMT " sp : " REG_FMT "\n" , - regs->sepc, regs->ra, regs->sp); + regs->xepc, regs->ra, regs->sp);pr_cont (" gp : " REG_FMT " tp : " REG_FMT " t0 : " REG_FMT "\n" , regs->gp, regs->tp, regs->t0); pr_cont(" t1 : " REG_FMT " t2 : " REG_FMT " s0 : " REG_FMT "\n",
@@ -56,23 +56,23 @@ void show_regs(struct pt_regs *regs)pr_cont (" t5 : " REG_FMT " t6 : " REG_FMT "\n" , regs->t5, regs->t6); -pr_cont ("sstatus: " REG_FMT " sbadaddr: " REG_FMT " scause: " REG_FMT "\n" , - regs->sstatus, regs->sbadaddr, regs->scause); +pr_cont ("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n" , + regs->xstatus, regs->xbadaddr, regs->xcause); }void start_thread (struct pt_regs *regs,unsigned long pc,unsigned long sp) { - regs->sstatus =SR_SPIE ; + regs->xstatus =SR_SPIE ;if (has_fpu) { - regs->sstatus |=SR_FS_INITIAL ; + regs->xstatus |=SR_FS_INITIAL ;/* * Restore the initial value to the FP register * before starting the user program. */ fstate_restore (current, regs); } - regs->sepc = pc; + regs->xepc = pc; regs->sp = sp;set_fs (USER_DS ); }
@@ -108,7 +108,8 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,const registerunsigned long gp __asm__ ("gp" );memset (childregs,0 ,sizeof (struct pt_regs )); childregs->gp = gp; - childregs->sstatus =SR_SPP |SR_SPIE ;/* Supervisor, irqs on */ +/* Supervisor/Machine, irqs on: */ + childregs->xstatus =SR_XPP |SR_XPIE ; p->thread.ra = (unsigned long )ret_from_kernel_thread; p->thread.s[0 ] = usp;/* fn */
arch/riscv/kernel/signal.c +10 -11
@@ -124,7 +124,7 @@ SYSCALL_DEFINE0(rt_sigreturn)pr_info_ratelimited ("%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n" , task->comm,task_pid_nr (task), __func__, - frame, (void *)regs->sepc, (void *)regs->sp); + frame, (void *)regs->xepc, (void *)regs->sp); }force_sig (SIGSEGV );return 0 ;
@@ -199,7 +199,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,* We always pass siginfo and mcontext, regardless of SA_SIGINFO, * since some things rely on this (e.g. glibc's debug/segfault.c). */ - regs->sepc = (unsigned long )ksig->ka.sa.sa_handler; + regs->xepc = (unsigned long )ksig->ka.sa.sa_handler; regs->sp = (unsigned long )frame; regs->a0 = ksig->sig;/* a0: signal number */ regs->a1 = (unsigned long )(&frame->info);/* a1: siginfo pointer */
@@ -208,7 +208,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,#if DEBUG_SIG pr_info ("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n" , current->comm,task_pid_nr (current), ksig->sig, - (void *)regs->sepc, (void *)regs->ra, frame); + (void *)regs->xepc, (void *)regs->ra, frame);#endif return 0 ;
@@ -220,10 +220,9 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)int ret;/* Are we from a system call? */ -if (regs->scause ==EXC_SYSCALL ) { +if (regs->xcause ==EXC_SYSCALL ) {/* Avoid additional syscall restarting via ret_from_exception */ - regs->scause =-1UL ; - + regs->xcause =-1UL ;/* If so, check system call restarting.. */ switch (regs->a0) {case -ERESTART_RESTARTBLOCK :
@@ -239,7 +238,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* fallthrough */ case -ERESTARTNOINTR : regs->a0 = regs->orig_a0; - regs->sepc -=0x4 ; + regs->xepc -=0x4 ;break ; } }
@@ -261,9 +260,9 @@ static void do_signal(struct pt_regs *regs) }/* Did we come from a system call? */ -if (regs->scause ==EXC_SYSCALL ) { +if (regs->xcause ==EXC_SYSCALL ) {/* Avoid additional syscall restarting via ret_from_exception */ - regs->scause =-1UL ; + regs->xcause =-1UL ;/* Restart the system call - no handlers present */ switch (regs->a0) {
@@ -271,12 +270,12 @@ static void do_signal(struct pt_regs *regs)case -ERESTARTSYS :case -ERESTARTNOINTR : regs->a0 = regs->orig_a0; - regs->sepc -=0x4 ; + regs->xepc -=0x4 ;break ;case -ERESTART_RESTARTBLOCK : regs->a0 = regs->orig_a0; regs->a7 = __NR_restart_syscall; - regs->sepc -=0x4 ; + regs->xepc -=0x4 ;break ; } }
arch/riscv/kernel/traps.c +8 -8
@@ -40,7 +40,7 @@ void die(struct pt_regs *regs, const char *str)print_modules ();show_regs (regs); - ret =notify_die (DIE_OOPS , str, regs,0 , regs->scause,SIGSEGV ); + ret =notify_die (DIE_OOPS , str, regs,0 , regs->xcause,SIGSEGV );bust_spinlocks (0 );add_taint (TAINT_DIE ,LOCKDEP_NOW_UNRELIABLE );
@@ -85,7 +85,7 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code,#define DO_ERROR_INFO (name, signo, code, str) \ asmlinkage void name(struct pt_regs *regs) \ { \ - do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \ + do_trap_error(regs, signo, code, regs->xepc, "Oops - " str); \ } DO_ERROR_INFO(do_trap_unknown,
@@ -128,12 +128,12 @@ asmlinkage void do_trap_break(struct pt_regs *regs)if (!user_mode (regs)) {enum bug_trap_type type; - type =report_bug (regs->sepc, regs); + type =report_bug (regs->xepc, regs);switch (type) {case BUG_TRAP_TYPE_NONE :break ;case BUG_TRAP_TYPE_WARN : - regs->sepc +=get_break_insn_length (regs->sepc); + regs->xepc +=get_break_insn_length (regs->xepc);break ;case BUG_TRAP_TYPE_BUG :die (regs,"Kernel BUG" );
@@ -141,7 +141,7 @@ asmlinkage void do_trap_break(struct pt_regs *regs) }#endif /* CONFIG_GENERIC_BUG */ -force_sig_fault (SIGTRAP ,TRAP_BRKPT , (void __user *)(regs->sepc)); +force_sig_fault (SIGTRAP ,TRAP_BRKPT , (void __user *)(regs->xepc)); }#ifdef CONFIG_GENERIC_BUG
@@ -166,9 +166,9 @@ void __init trap_init(void)* Set sup0 scratch register to 0, indicating to exception vector * that we are presently executing in the kernel */ -csr_write (CSR_SSCRATCH ,0 ); +csr_write (CSR_XSCRATCH ,0 );/* Set the exception vector address */ -csr_write (CSR_STVEC , &handle_exception); +csr_write (CSR_XTVEC , &handle_exception);/* Enable all interrupts */ -csr_write (CSR_SIE ,-1 ); +csr_write (CSR_XIE ,-1 ); }
arch/riscv/lib/uaccess.S +6 -6
@@ -18,7 +18,7 @@ ENTRY(__asm_copy_from_user) /* Enable access to user memory */ li t6, SR_SUM - csrs CSR_SSTATUS, t6 + csrs CSR_XSTATUS, t6 add a3, a1, a2 /* Use word-oriented copy only if low-order bits match */
@@ -47,7 +47,7 @@ ENTRY(__asm_copy_from_user) 3: /* Disable access to user memory */ - csrc CSR_SSTATUS, t6 + csrc CSR_XSTATUS, t6 li a0, 0 ret 4: /* Edge case: unalignment */
@@ -72,7 +72,7 @@ ENTRY(__clear_user) /* Enable access to user memory */ li t6, SR_SUM - csrs CSR_SSTATUS, t6 + csrs CSR_XSTATUS, t6 add a3, a0, a1 addi t0, a0, SZREG-1
@@ -94,7 +94,7 @@ ENTRY(__clear_user) 3: /* Disable access to user memory */ - csrc CSR_SSTATUS, t6 + csrc CSR_XSTATUS, t6 li a0, 0 ret 4: /* Edge case: unalignment */
@@ -114,11 +114,11 @@ ENDPROC(__clear_user) /* Fixup code for __copy_user(10) and __clear_user(11) */ 10: /* Disable access to user memory */ - csrs CSR_SSTATUS, t6 + csrs CSR_XSTATUS, t6 mv a0, a2 ret 11: - csrs CSR_SSTATUS, t6 + csrs CSR_XSTATUS, t6 mv a0, a1 ret .previous
arch/riscv/mm/extable.c +2 -2
@@ -15,9 +15,9 @@ int fixup_exception(struct pt_regs *regs) {const struct exception_table_entry *fixup; - fixup =search_exception_tables (regs->sepc); + fixup =search_exception_tables (regs->xepc);if (fixup) { - regs->sepc = fixup->fixup; + regs->xepc = fixup->fixup;return 1 ; }return 0 ;
arch/riscv/mm/fault.c +3 -3
@@ -32,8 +32,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs)int code =SEGV_MAPERR ;vm_fault_t fault; - cause = regs->scause; - addr = regs->sbadaddr; + cause = regs->xcause; + addr = regs->xbadaddr; tsk = current; mm = tsk->mm;
@@ -51,7 +51,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs) goto vmalloc_fault;/* Enable interrupts if they were enabled in the parent context. */ -if (likely (regs->sstatus &SR_SPIE )) +if (likely (regs->xstatus &SR_XPIE ))local_irq_enable ();/*
drivers/clocksource/timer-riscv.c +4 -4
@@ -19,7 +19,7 @@static int riscv_clock_next_event (unsigned long delta,struct clock_event_device *ce) { -csr_set (sie,SIE_STIE ); +csr_set (CSR_XIE ,XIE_XTIE );sbi_set_timer (get_cycles64 () + delta);return 0 ; }
@@ -61,13 +61,13 @@ static int riscv_timer_starting_cpu(unsigned int cpu) ce->cpumask =cpumask_of (cpu);clockevents_config_and_register (ce, riscv_timebase,100 ,0x7fffffff ); -csr_set (sie,SIE_STIE ); +csr_set (CSR_XIE ,XIE_XTIE );return 0 ; }static int riscv_timer_dying_cpu (unsigned int cpu) { -csr_clear (sie,SIE_STIE ); +csr_clear (CSR_XIE ,XIE_XTIE );return 0 ; }
@@ -76,7 +76,7 @@ void riscv_timer_interrupt(void) {struct clock_event_device *evdev =this_cpu_ptr (&riscv_clock_event); -csr_clear (sie,SIE_STIE ); +csr_clear (CSR_XIE ,XIE_XTIE ); evdev->event_handler (evdev); }
drivers/irqchip/irq-sifive-plic.c +2 -2
@@ -179,7 +179,7 @@ static void plic_handle_irq(struct pt_regs *regs)WARN_ON_ONCE (!handler->present); -csr_clear (sie,SIE_SEIE ); +csr_clear (CSR_XIE ,XIE_XEIE );while ((hwirq =readl (claim))) {int irq =irq_find_mapping (plic_irqdomain, hwirq);
@@ -190,7 +190,7 @@ static void plic_handle_irq(struct pt_regs *regs)generic_handle_irq (irq);writel (hwirq, claim); } -csr_set (sie,SIE_SEIE ); +csr_set (CSR_XIE ,XIE_XEIE ); }/*
Signature · 1 line
2.20.1
Re: [PATCH 08/20] riscv: abstract out CSR names for supervisor vs machine mode
Quoted 8 lines · Christoph Hellwig
Many of the privileged CSRs exist in a supervisor and machine version that are used very similarly. Provide a new X-naming layer so that we don't have to ifdef everywhere for M-mode Linux support.
Contains contributions from Damien Le Moal <[email protected]>.
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Atish Patra <[email protected]>
[ ... ]
Quoted 9 lines
arch/riscv/kernel/process.c +0
-1@@ -56,23 +56,23 @@ void show_regs(struct pt_regs *regs)pr_cont (" t5 : " REG_FMT " t6 : " REG_FMT "\n" , regs->t5, regs->t6); - pr_cont("sstatus: " REG_FMT " sbadaddr: " REG_FMT " scause: " REG_FMT "\n",
arch/riscv/kernel/process.c void show_regs(struct pt_regs *regs) · line 66
Quoted 10 lines
arch/riscv/kernel/process.c +3
-2- regs->sstatus, regs->sbadaddr, regs->scause); +pr_cont ("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n" , + regs->xstatus, regs->xbadaddr, regs->xcause); }void start_thread (struct pt_regs *regs,unsigned long pc,unsigned long sp) { - regs->sstatus =SR_SPIE ; + regs->xstatus =SR_SPIE ;
Looks like this should be "regs->xstatus = SR_PIE;"
Will update it here. Let me know if you don't agree -
- Paul
Re: [PATCH 08/20] riscv: abstract out CSR names for supervisor vs machine mode
Quoted 9 lines · Paul Walmsley
arch/riscv/kernel/process.c void show_regs(struct pt_regs *regs) · line 66
Quoted 5 lines
void start_thread(struct
pt_regs*regs, unsigned long pc,arch/riscv/kernel/process.c +1
-1unsigned long sp) { - regs->sstatus =SR_SPIE ; + regs->xstatus =SR_SPIE ;Looks like this should be "
regs->xstatus=SR_PIE;"Will update it here. Let me know if you don't agree -
there is no SR_PIE, do you mean SR_XPІE? Good catch in that case, but please let me resend the whole thing. I have some minor updates, and I'd also like to do the rebase myself.
PATCH 9/20 riscv: don't allow selecting SBI based drivers for M-mode
In reply to Christoph HellwigFrom: Damien Le Moal <[email protected]>
When running in M-mode we can't use SBI based drivers. Add a new CONFIG_RISCV_SBI that drivers that do SBI calls can depend on instead.
--- arch/riscv/Kconfig | 6 ++++++ drivers/tty/hvc/Kconfig | 2 +- drivers/tty/serial/Kconfig | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-)
arch/riscv/Kconfig +6 -0
@@ -61,6 +61,12 @@ config RISCV config RISCV_M_MODE bool +# set if we are running in S-mode and can use SBI calls +config RISCV_SBI + bool + depends on !RISCV_M_MODE + default y + config MMU def_bool y
drivers/tty/hvc/Kconfig +1 -1
@@ -89,7 +89,7 @@ config HVC_DCC config HVC_RISCV_SBI bool "RISC-V SBI console support" - depends on RISCV + depends on RISCV_SBI select HVC_DRIVER help This enables support for console output via RISC-V SBI calls, which
drivers/tty/serial/Kconfig +1 -1
@@ -88,7 +88,7 @@ config SERIAL_EARLYCON_ARM_SEMIHOST config SERIAL_EARLYCON_RISCV_SBI bool "Early console using RISC-V SBI" - depends on RISCV + depends on RISCV_SBI select SERIAL_CORE select SERIAL_CORE_CONSOLE select SERIAL_EARLYCON
Signature · 1 line
2.20.1
PATCH 10/20 riscv: poison SBI calls for M-mode
In reply to Christoph HellwigThere is no SBI when we run in M-mode, so fail the compile for any code trying to use SBI calls.
--- arch/riscv/include/asm/sbi.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/riscv/include/asm/sbi.h +3 -2
@@ -8,6 +8,7 @@#include <linux/types.h> +#ifdef CONFIG_RISCV_SBI #define SBI_SET_TIMER 0#define SBI_CONSOLE_PUTCHAR 1#define SBI_CONSOLE_GETCHAR 2
@@ -93,5 +94,5 @@ static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask, {SBI_CALL_4 (SBI_REMOTE_SFENCE_VMA_ASID , hart_mask, start, size, asid); } - -#endif +#endif /* CONFIG_RISCV_SBI */ +#endif /* _ASM_RISCV_SBI_H */
Signature · 1 line
2.20.1
PATCH 11/20 riscv: cleanup the default power off implementation
In reply to Christoph HellwigMove the sbi poweroff to a separate function and file that is only compiled if CONFIG_SBI is set. Provide a new default fallback power off that just sits in a wfi loop to save some power.
--- arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/reset.c | 5 ++--- arch/riscv/kernel/sbi.c | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/kernel/sbi.c
arch/riscv/kernel/Makefile +1 -0
@@ -39,5 +39,6 @@ obj-$(CONFIG_FUNCTION_TRACER) += mcount.o ftrace.o obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o obj-$(CONFIG_PERF_EVENTS) += perf_event.o +obj-$(CONFIG_RISCV_SBI) += sbi.o clean:
arch/riscv/kernel/reset.c +2 -3
@@ -4,12 +4,11 @@*/ #include <linux/reboot.h> -#include <asm/sbi.h> static void default_power_off (void ) { -sbi_shutdown (); -while (1 ); +while (1 ) +wait_for_interrupt (); }void (*pm_power_off)(void ) = default_power_off;
arch/riscv/kernel/sbi.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/init.h> +#include <linux/pm.h> +#include <asm/sbi.h> + +static void sbi_power_off (void ) +{ +sbi_shutdown (); +} + +static int __init sbi_init (void ) +{ + pm_power_off = sbi_power_off; +return 0 ; +} +early_initcall (sbi_init);
Signature · 1 line
2.20.1
PATCH 12/20 riscv: implement remote sfence.i using IPIs
In reply to Christoph HellwigThe RISC-V ISA only supports flushing the instruction cache for the local CPU core. Currently we always offload the remote TLB flushing to the SBI, which then issues an IPI under the hoods. But with M-mode
we do not have an SBI so we have to do it ourselves. IPI to the
other nodes using the existing kernel helpers instead if we have native clint support and thus can IPI directly from the kernel.
--- arch/riscv/include/asm/sbi.h | 3 +++ arch/riscv/mm/cacheflush.c | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-)
arch/riscv/include/asm/sbi.h +3 -0
@@ -94,5 +94,8 @@ static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask, {SBI_CALL_4 (SBI_REMOTE_SFENCE_VMA_ASID , hart_mask, start, size, asid); } +#else /* CONFIG_RISCV_SBI */ +/* stub to for code is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */ +void sbi_remote_fence_i (const unsigned long *hart_mask);#endif /* CONFIG_RISCV_SBI */ #endif /* _ASM_RISCV_SBI_H */
arch/riscv/mm/cacheflush.c +18 -6
@@ -10,9 +10,17 @@#include <asm/sbi.h> +static void ipi_remote_fence_i (void *info) +{ +return local_flush_icache_all (); +} +void flush_icache_all (void ) { -sbi_remote_fence_i (NULL ); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +sbi_remote_fence_i (NULL ); +else +on_each_cpu (ipi_remote_fence_i,NULL ,1 ); }/*
@@ -28,7 +36,7 @@ void flush_icache_all(void)void flush_icache_mm (struct mm_struct *mm,bool local) {unsigned int cpu; -cpumask_t others, hmask, *mask; +cpumask_t others, *mask;preempt_disable ();
@@ -46,10 +54,7 @@ void flush_icache_mm(struct mm_struct *mm, bool local)*/ cpumask_andnot (&others,mm_cpumask (mm),cpumask_of (cpu)); local |=cpumask_empty (&others); -if (mm != current->active_mm || !local) { -riscv_cpuid_to_hartid_mask (&others, &hmask); -sbi_remote_fence_i (hmask.bits); - }else { +if (mm == current->active_mm && local) {/* * It's assumed that at least one strongly ordered operation is * performed on this hart between setting a hart's cpumask bit
@@ -59,6 +64,13 @@ void flush_icache_mm(struct mm_struct *mm, bool local)* with flush_icache_deferred(). */ smp_mb (); + }else if (IS_ENABLED (CONFIG_RISCV_SBI )) { +cpumask_t hartid_mask; + +riscv_cpuid_to_hartid_mask (&others, &hartid_mask); +sbi_remote_fence_i (cpumask_bits (&hartid_mask)); + }else { +on_each_cpu_mask (&others, ipi_remote_fence_i,NULL ,1 ); }preempt_enable ();
Signature · 7 lines
2.20.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 13/20 riscv: add support for MMIO access to the timer registers
In reply to Christoph HellwigWhen running in M-mode we can't use the SBI to set the timer, and don't have access to the time CSR as that usually is emulated by M-mode. Instead provide code that directly accesses the MMIO for the timer.
--- arch/riscv/include/asm/sbi.h | 3 ++- arch/riscv/include/asm/timex.h | 19 +++++++++++++++++-- drivers/clocksource/timer-riscv.c | 21 +++++++++++++++++---- 3 files changed, 36 insertions(+), 7 deletions(-)
arch/riscv/include/asm/sbi.h +2 -1
@@ -95,7 +95,8 @@ static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,SBI_CALL_4 (SBI_REMOTE_SFENCE_VMA_ASID , hart_mask, start, size, asid); }#else /* CONFIG_RISCV_SBI */ -/* stub to for code is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */ +/* stubs to for code is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */ +void sbi_set_timer (uint64_t stime_value);void sbi_remote_fence_i (const unsigned long *hart_mask);#endif /* CONFIG_RISCV_SBI */ #endif /* _ASM_RISCV_SBI_H */
arch/riscv/include/asm/timex.h +17 -2
@@ -7,12 +7,25 @@#define _ASM_RISCV_TIMEX_H#include <asm/csr.h> +#include <asm/io.h> typedef unsigned long cycles_t ; +extern u64 __iomem *riscv_time_val; +extern u64 __iomem *riscv_time_cmp; + +#ifdef CONFIG_64BIT +#define mmio_get_cycles () readq_relaxed(riscv_time_val) +#else +#define mmio_get_cycles () readl_relaxed(riscv_time_val) +#define mmio_get_cycles_hi () readl_relaxed(((u32 *)riscv_time_val) + 1) +#endif +static inline cycles_t get_cycles (void ) { -return csr_read (CSR_TIME ); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +return csr_read (CSR_TIME ); +return mmio_get_cycles (); }#define get_cycles get_cycles
@@ -24,7 +37,9 @@ static inline u64 get_cycles64(void)#else /* CONFIG_64BIT */ static inline u32 get_cycles_hi (void ) { -return csr_read (CSR_TIMEH ); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +return csr_read (CSR_TIMEH ); +return mmio_get_cycles_hi (); }static inline u64 get_cycles64 (void )
drivers/clocksource/timer-riscv.c +17 -4
@@ -3,9 +3,9 @@* Copyright (C) 2012 Regents of the University of California * Copyright (C) 2017 SiFive * -* All RISC-V systems have a timer attached to every hart. These timers can be -* read from the "time" and "timeh" CSRs, and can use the SBI to setup -* events. +* All RISC-V systems have a timer attached to every hart. These timers can +* either be read from the "time" and "timeh" CSRs, and can use the SBI to +* setup events, or directly accessed using MMIO registers. */ #include <linux/clocksource.h> #include <linux/clockchips.h>
@@ -13,14 +13,27 @@#include <linux/delay.h> #include <linux/irq.h> #include <linux/sched_clock.h> +#include <linux/io-64-nonatomic-lo-hi.h> #include <asm/smp.h> #include <asm/sbi.h> +u64 __iomem *riscv_time_cmp; +u64 __iomem *riscv_time_val; + +static inline void mmio_set_timer (u64 val) +{ +writeq_relaxed (val, + riscv_time_cmp +cpuid_to_hartid_map (smp_processor_id ())); +} +static int riscv_clock_next_event (unsigned long delta,struct clock_event_device *ce) {csr_set (CSR_XIE ,XIE_XTIE ); -sbi_set_timer (get_cycles64 () + delta); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +sbi_set_timer (get_cycles64 () + delta); +else +mmio_set_timer (get_cycles64 () + delta);return 0 ; }
Signature · 1 line
2.20.1
PATCH 14/20 riscv: provide native clint access for M-mode
In reply to Christoph HellwigRISC-V has the concept of a cpu level interrupt controller. The interface for it is split between a standardized part that is exposed as bits in the mstatus/sstatus register and the mie/mip/sie/sip CRS. But the bit to actually trigger IPIs is not standardized and just mentioned as implementable using MMIO.
Add support for IPIs using MMIO using the SiFive clint layout (which is also shared by Ariane, Kendrye and the Qemu virt platform). Additional the MMIO block also support the time value and timer compare registers, so they are also set up using the same OF node. Support for other layouts should also be relatively easy to add in the future.
--- arch/riscv/include/asm/clint.h | 39 ++++++++++++++++++++++++++++++ arch/riscv/include/asm/sbi.h | 2 ++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/clint.c | 44 ++++++++++++++++++++++++++++++++++ arch/riscv/kernel/setup.c | 2 ++ arch/riscv/kernel/smp.c | 16 ++++++++++--- arch/riscv/kernel/smpboot.c | 4 ++++ 7 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/include/asm/clint.h create mode 100644 arch/riscv/kernel/clint.c
arch/riscv/include/asm/clint.h new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef _ASM_CLINT_H +#define _ASM_CLINT_H 1 + +#include <linux/io.h> +#include <linux/smp.h> + +#ifdef CONFIG_RISCV_M_MODE +extern u32 __iomem *clint_ipi_base; + +void clint_init_boot_cpu (void ); + +static inline void clint_send_ipi_single (unsigned long hartid) +{ +writel (1 , clint_ipi_base + hartid); +} + +static inline void clint_send_ipi_mask (const struct cpumask *hartid_mask) +{ +int hartid; + +for_each_cpu (hartid, hartid_mask) +clint_send_ipi_single (hartid); +} + +static inline void clint_clear_ipi (unsigned long hartid) +{ +writel (0 , clint_ipi_base + hartid); +} +#else /* CONFIG_RISCV_M_MODE */ +#define clint_init_boot_cpu () do { } while (0) + +/* stubs to for code is only reachable under IS_ENABLED(CONFIG_RISCV_M_MODE): */ +void clint_send_ipi_single (unsigned long hartid); +void clint_send_ipi_mask (const struct cpumask *hartid_mask); +void clint_clear_ipi (unsigned long hartid); +#endif /* CONFIG_RISCV_M_MODE */ + +#endif /* _ASM_CLINT_H */
arch/riscv/include/asm/sbi.h +2 -0
@@ -97,6 +97,8 @@ static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,#else /* CONFIG_RISCV_SBI */ /* stubs to for code is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */ void sbi_set_timer (uint64_t stime_value); +void sbi_clear_ipi (void ); +void sbi_send_ipi (const unsigned long *hart_mask);void sbi_remote_fence_i (const unsigned long *hart_mask);#endif /* CONFIG_RISCV_SBI */ #endif /* _ASM_RISCV_SBI_H */
arch/riscv/kernel/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-y += vdso.o obj-y += cacheinfo.o obj-y += vdso/ +obj-$(CONFIG_RISCV_M_MODE) += clint.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o
arch/riscv/kernel/clint.c new file +44 -0
44 lines not on this page · raw message
arch/riscv/kernel/setup.c +2 -0
@@ -17,6 +17,7 @@#include <linux/sched/task.h> #include <linux/swiotlb.h> +#include <asm/clint.h> #include <asm/setup.h> #include <asm/sections.h> #include <asm/pgtable.h>
@@ -65,6 +66,7 @@ void __init setup_arch(char **cmdline_p)setup_bootmem ();paging_init ();unflatten_device_tree (); +clint_init_boot_cpu ();#ifdef CONFIG_SWIOTLB swiotlb_init (1 );
arch/riscv/kernel/smp.c +13 -3
@@ -14,6 +14,7 @@#include <linux/seq_file.h> #include <linux/delay.h> +#include <asm/clint.h> #include <asm/sbi.h> #include <asm/tlbflush.h> #include <asm/cacheflush.h>
@@ -90,7 +91,10 @@ static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)smp_mb__after_atomic ();riscv_cpuid_to_hartid_mask (mask, &hartid_mask); -sbi_send_ipi (cpumask_bits (&hartid_mask)); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +sbi_send_ipi (cpumask_bits (&hartid_mask)); +else +clint_send_ipi_mask (&hartid_mask); }static void send_ipi_single (int cpu,enum ipi_message_type op)
@@ -101,12 +105,18 @@ static void send_ipi_single(int cpu, enum ipi_message_type op)set_bit (op, &ipi_data[cpu].bits);smp_mb__after_atomic (); -sbi_send_ipi (cpumask_bits (cpumask_of (hartid))); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +sbi_send_ipi (cpumask_bits (cpumask_of (hartid))); +else +clint_send_ipi_single (hartid); }static inline void clear_ipi (void ) { -csr_clear (CSR_SIP ,SIE_SSIE ); +if (IS_ENABLED (CONFIG_RISCV_SBI )) +csr_clear (CSR_SIP ,SIE_SSIE ); +else +clint_clear_ipi (cpuid_to_hartid_map (smp_processor_id ())); }void riscv_software_interrupt (void )
arch/riscv/kernel/smpboot.c +4 -0
@@ -24,6 +24,7 @@#include <linux/of.h> #include <linux/sched/task_stack.h> #include <linux/sched/mm.h> +#include <asm/clint.h> #include <asm/irq.h> #include <asm/mmu_context.h> #include <asm/tlbflush.h>
@@ -134,6 +135,9 @@ asmlinkage void __init smp_callin(void) {struct mm_struct *mm = &init_mm; +if (!IS_ENABLED (CONFIG_RISCV_SBI )) +clint_clear_ipi (cpuid_to_hartid_map (smp_processor_id ())); +/* All kernel threads share the same mm context. */ mmgrab (mm); current->active_mm = mm;
Signature · 1 line
2.20.1
PATCH 15/20 riscv: read the hart ID from mhartid on boot
In reply to Christoph HellwigFrom: Damien Le Moal <[email protected]>
When in M-Mode, we can use the mhartid CSR to get the ID of the running HART. Doing so, direct M-Mode boot without firmware is possible.
--- arch/riscv/include/asm/csr.h | 1 + arch/riscv/kernel/head.S | 8 ++++++++ 2 files changed, 9 insertions(+)
arch/riscv/include/asm/csr.h +1 -0
@@ -81,6 +81,7 @@#define SIE_SEIE (_AC(0x1, UL) << IRQ_S_EXT)/* symbolic CSR names: */ +#define CSR_MHARTID 0xf14#define CSR_MSTATUS 0x300#define CSR_MIE 0x304#define CSR_MTVEC 0x305
arch/riscv/kernel/head.S +8 -0
@@ -50,6 +50,14 @@ _start_kernel: csrw CSR_XIE, zero csrw CSR_XIP, zero +#ifdef CONFIG_RISCV_M_MODE + /* + * The hartid in a0 is expected later on, and we have no firmware + * to hand it to us. + */ + csrr a0, CSR_MHARTID +#endif + /* Load the global pointer */ .option push .option norelax
Signature · 1 line
2.20.1
PATCH 16/20 riscv: use the correct interrupt levels for M-mode
In reply to Christoph HellwigThe numerical levels for External/Timer/Software interrupts differ between S-mode and M-mode.
--- arch/riscv/kernel/irq.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/irq.c +9 -3
@@ -14,9 +14,15 @@/* * Possible interrupt causes: */ -#define INTERRUPT_CAUSE_SOFTWARE IRQ_S_SOFT -#define INTERRUPT_CAUSE_TIMER IRQ_S_TIMER -#define INTERRUPT_CAUSE_EXTERNAL IRQ_S_EXT +#ifdef CONFIG_RISCV_M_MODE +# define INTERRUPT_CAUSE_SOFTWARE IRQ_M_SOFT +# define INTERRUPT_CAUSE_TIMER IRQ_M_TIMER +# define INTERRUPT_CAUSE_EXTERNAL IRQ_M_EXT +#else +# define INTERRUPT_CAUSE_SOFTWARE IRQ_S_SOFT +# define INTERRUPT_CAUSE_TIMER IRQ_S_TIMER +# define INTERRUPT_CAUSE_EXTERNAL IRQ_S_EXT +#endif /* CONFIG_RISCV_M_MODE */ int arch_show_interrupts (struct seq_file *p,int prec) {
Signature · 1 line
2.20.1
PATCH 17/20 riscv: clear the instruction cache and all registers when booting
In reply to Christoph HellwigWhen we get booted we want a clear slate without any leaks from previous supervisors or the firmware. Flush the instruction cache and then clear all registers to known good values. This is really important for the upcoming nommu support that runs on M-mode, but can't really harm when running in S-mode either. Vaguely based on the concepts from opensbi.
--- arch/riscv/include/asm/csr.h | 1 + arch/riscv/kernel/head.S | 88 +++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-)
arch/riscv/include/asm/csr.h +1 -0
@@ -83,6 +83,7 @@/* symbolic CSR names: */ #define CSR_MHARTID 0xf14#define CSR_MSTATUS 0x300 +#define CSR_MISA 0x301#define CSR_MIE 0x304#define CSR_MTVEC 0x305#define CSR_MSCRATCH 0x340
arch/riscv/kernel/head.S +87 -1
111 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 18/20 riscv: add nommu support
In reply to Christoph HellwigThe kernel runs in M-mode without using page tables, and thus can't run bare metal without help from additional firmware.
Most of the patch is just stubbing out code not needed without page tables, but there is an interesting detail in the signals implementation:
- The normal RISC-V syscall ABI only implements rt_sigreturn as VDSO entry point, but the ELF VDSO is not supported for nommu Linux. We instead copy the code to call the syscall onto the stack.
In addition to enabling the nommu code a new defconfig for a small kernel image that can run in nommu mode on qemu is also provided, to run a kernel in qemu you can use the following command line:
qemu-system-riscv64 -smp 2 -m 64 -machine virt -nographic \ -kernel arch/riscv/boot/loader \ -drive file=rootfs.ext2,format=raw,id=hd0 \ -device virtio-blk-device,drive=hd0
Contains contributions from Damien Le Moal <[email protected]>.
--- arch/riscv/Kconfig | 24 +++++--- arch/riscv/configs/nommu_virt_defconfig | 78 +++++++++++++++++++++++++ arch/riscv/include/asm/cache.h | 8 +++ arch/riscv/include/asm/elf.h | 4 +- arch/riscv/include/asm/fixmap.h | 2 + arch/riscv/include/asm/futex.h | 6 ++ arch/riscv/include/asm/io.h | 4 ++ arch/riscv/include/asm/mmu.h | 3 + arch/riscv/include/asm/page.h | 10 +++- arch/riscv/include/asm/pgalloc.h | 2 + arch/riscv/include/asm/pgtable.h | 60 +++++++++++-------- arch/riscv/include/asm/tlbflush.h | 12 +++- arch/riscv/include/asm/uaccess.h | 4 ++ arch/riscv/kernel/Makefile | 3 +- arch/riscv/kernel/entry.S | 11 ++++ arch/riscv/kernel/head.S | 6 ++ arch/riscv/kernel/signal.c | 17 +++++- arch/riscv/lib/Makefile | 11 ++-- arch/riscv/mm/Makefile | 3 +- arch/riscv/mm/cacheflush.c | 2 + arch/riscv/mm/context.c | 2 + arch/riscv/mm/init.c | 13 ++++- 22 files changed, 233 insertions(+), 52 deletions(-) create mode 100644 arch/riscv/configs/nommu_virt_defconfig
arch/riscv/Kconfig +16 -8
@@ -26,13 +26,13 @@ config RISCV select GENERIC_IRQ_SHOW select GENERIC_PCI_IOMAP select GENERIC_SCHED_CLOCK - select GENERIC_STRNCPY_FROM_USER - select GENERIC_STRNLEN_USER + select GENERIC_STRNCPY_FROM_USER if MMU + select GENERIC_STRNLEN_USER if MMU select GENERIC_SMP_IDLE_THREAD select GENERIC_ATOMIC64 if !64BIT select HAVE_ARCH_AUDITSYSCALL select HAVE_MEMBLOCK_NODE_MAP - select HAVE_DMA_CONTIGUOUS + select HAVE_DMA_CONTIGUOUS if MMU select HAVE_FUTEX_CMPXCHG if FUTEX select HAVE_PERF_EVENTS select HAVE_SYSCALL_TRACEPOINTS
@@ -47,6 +47,7 @@ config RISCV select PCI_DOMAINS_GENERIC if PCI select PCI_MSI if PCI select RISCV_TIMER + select UACCESS_MEMCPY if !MMU select GENERIC_IRQ_MULTI_HANDLER select GENERIC_ARCH_TOPOLOGY if SMP select ARCH_HAS_PTE_SPECIAL
@@ -60,6 +61,7 @@ config RISCV # set if we run in machine mode, cleared if we run in supervisor mode config RISCV_M_MODE bool + default !MMU # set if we are running in S-mode and can use SBI calls config RISCV_SBI
@@ -68,7 +70,11 @@ config RISCV_SBI default y config MMU - def_bool y + bool "MMU-based Paged Memory Management Support" + default y + help + Select if you want MMU-based virtualised addressing space + support by paged memory management. If unsure, say 'Y'. config ZONE_DMA32 bool
@@ -87,6 +93,7 @@ config PA_BITS config PAGE_OFFSET hex default 0xC0000000 if 32BIT && MAXPHYSMEM_2GB + default 0x80000000 if 64BIT && !MMU default 0xffffffff80000000 if 64BIT && MAXPHYSMEM_2GB default 0xffffffe000000000 if 64BIT && MAXPHYSMEM_128GB
@@ -130,7 +137,7 @@ config GENERIC_HWEIGHT def_bool y config FIX_EARLYCON_MEM - def_bool y + def_bool CONFIG_MMU config PGTABLE_LEVELS int
@@ -155,6 +162,7 @@ config ARCH_RV32I select GENERIC_LIB_ASHRDI3 select GENERIC_LIB_LSHRDI3 select GENERIC_LIB_UCMPDI2 + select MMU config ARCH_RV64I bool "RV64I"
@@ -163,9 +171,9 @@ config ARCH_RV64I select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FTRACE_MCOUNT_RECORD - select HAVE_DYNAMIC_FTRACE - select HAVE_DYNAMIC_FTRACE_WITH_REGS - select SWIOTLB + select HAVE_DYNAMIC_FTRACE if MMU + select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRACE + select SWIOTLB if MMU endchoice
arch/riscv/configs/nommu_virt_defconfig new file +78 -0
78 lines not on this page · raw message
arch/riscv/include/asm/cache.h +8 -0
@@ -11,4 +11,12 @@#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT) +/* +* RISC-V requires the stack pointer to be 16-byte aligned, so ensure that +* the flat loader aligns it accordingly. +*/ +#ifndef CONFIG_MMU +#define ARCH_SLAB_MINALIGN 16 +#endif +#endif /* _ASM_RISCV_CACHE_H */
arch/riscv/include/asm/elf.h +2 -2
@@ -56,16 +56,16 @@ extern unsigned long elf_hwcap;*/ #define ELF_PLATFORM (NULL) +#ifdef CONFIG_MMU #define ARCH_DLINFO \ do { \ NEW_AUX_ENT(AT_SYSINFO_EHDR, \ (elf_addr_t)current->mm->context.vdso); \ } while (0) - -#define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm ;extern int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp); +#endif /* CONFIG_MMU */ #endif /* _ASM_RISCV_ELF_H */
arch/riscv/include/asm/fixmap.h +2 -0
@@ -11,6 +11,7 @@#include <asm/page.h> #include <asm/pgtable.h> +#ifdef CONFIG_MMU /* * Here we define all the compile-time 'special' virtual addresses. * The point is to have a constant address at compile time, but to
@@ -42,4 +43,5 @@ extern void __set_fixmap(enum fixed_addresses idx,#include <asm-generic/fixmap.h> +#endif /* CONFIG_MMU */ #endif /* _ASM_RISCV_FIXMAP_H */
arch/riscv/include/asm/futex.h +6 -0
@@ -12,6 +12,12 @@#include <linux/errno.h> #include <asm/asm.h> +/* We don't even really need the extable code, but for now keep it simple */ +#ifndef CONFIG_MMU +#define __enable_user_access () do { } while (0) +#define __disable_user_access () do { } while (0) +#endif +#define __futex_atomic_op (insn, ret, oldval, uaddr, oparg) \ { \ uintptr_t tmp; \
arch/riscv/include/asm/io.h +4 -0
@@ -14,6 +14,7 @@#include <linux/types.h> #include <asm/mmiowb.h> +#ifdef CONFIG_MMU extern void __iomem *ioremap (phys_addr_t offset,unsigned long size);/*
@@ -26,6 +27,9 @@ extern void __iomem *ioremap(phys_addr_t offset, unsigned long size);#define ioremap_wt (addr, size) ioremap((addr), (size))extern void iounmap (volatile void __iomem *addr); +#else +#define pgprot_noncached (x) (x) +#endif /* CONFIG_MMU */ /* Generic IO read/write. These perform native-endian accesses. */ #define __raw_writeb __raw_writeb
arch/riscv/include/asm/mmu.h +3 -0
@@ -10,6 +10,9 @@ #ifndef __ASSEMBLY__ typedef struct { +#ifndef CONFIG_MMU + unsigned long end_brk; +#endif void *vdso; #ifdef CONFIG_SMP /* A local icache flush is needed before user execution can resume. */
arch/riscv/include/asm/page.h +7 -3
@@ -88,8 +88,14 @@ typedef struct page *pgtable_t;#define PTE_FMT "%08lx"#endif +#ifdef CONFIG_MMU extern unsigned long va_pa_offset;extern unsigned long pfn_base; +#define ARCH_PFN_OFFSET (pfn_base) +#else +#define va_pa_offset 0 +#define ARCH_PFN_OFFSET (PAGE_OFFSET >> PAGE_SHIFT) +#endif /* CONFIG_MMU */ extern unsigned long max_low_pfn;extern unsigned long min_low_pfn;
@@ -112,11 +118,9 @@ extern unsigned long min_low_pfn;#ifdef CONFIG_FLATMEM #define pfn_valid (pfn) \ - (((pfn) >= pfn_base) && (((pfn)-pfn_base) < max_mapnr)) + (((pfn) >= ARCH_PFN_OFFSET) && (((pfn) - ARCH_PFN_OFFSET) < max_mapnr))#endif -#define ARCH_PFN_OFFSET (pfn_base) -#endif /* __ASSEMBLY__ */ #define virt_addr_valid (vaddr) (pfn_valid(virt_to_pfn(vaddr)))
arch/riscv/include/asm/pgalloc.h +2 -0
@@ -10,6 +10,7 @@#include <linux/mm.h> #include <asm/tlb.h> +#ifdef CONFIG_MMU #include <asm-generic/pgalloc.h> /* for pte_{alloc,free}_one */ static inline void pmd_populate_kernel(struct mm_struct *mm,
@@ -81,6 +82,7 @@ do { \pgtable_page_dtor (pte); \tlb_remove_page ((tlb), pte); \ } while (0) +#endif /* CONFIG_MMU */ static inline void check_pgt_cache (void ) {
arch/riscv/include/asm/pgtable.h +36 -24
90 lines not on this page · raw message
arch/riscv/include/asm/tlbflush.h +9 -3
@@ -10,6 +10,7 @@#include <linux/mm_types.h> #include <asm/smp.h> +#ifdef CONFIG_MMU /* * Flush entire local TLB. 'sfence.vma' implicitly fences with the instruction * cache as well, so a 'fence.i' is not necessary.
@@ -24,14 +25,19 @@ static inline void local_flush_tlb_page(unsigned long addr) { __asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" ); } +#else /* CONFIG_MMU */ +#define local_flush_tlb_all () do { } while (0) +#define local_flush_tlb_page (addr) do { } while (0) +#endif /* CONFIG_MMU */ -#ifdef CONFIG_SMP +#if defined(CONFIG_SMP ) && defined(CONFIG_MMU )void flush_tlb_all (void );void flush_tlb_mm (struct mm_struct *mm);void flush_tlb_page (struct vm_area_struct *vma,unsigned long addr);void flush_tlb_range (struct vm_area_struct *vma,unsigned long start,unsigned long end); -#else /* CONFIG_SMP */ +#else /* CONFIG_SMP && CONFIG_MMU */ +#define flush_tlb_all () local_flush_tlb_all()#define flush_tlb_page (vma, addr) local_flush_tlb_page(addr)
@@ -42,7 +48,7 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, }#define flush_tlb_mm (mm) flush_tlb_all() -#endif /* CONFIG_SMP */ +#endif /* !CONFIG_SMP || !CONFIG_MMU */ /* Flush a range of kernel pages */ static inline void flush_tlb_kernel_range(unsigned long start,
arch/riscv/include/asm/uaccess.h +4 -0
@@ -11,6 +11,7 @@/* * User space memory access functions */ +#ifdef CONFIG_MMU #include <linux/errno.h> #include <linux/compiler.h> #include <linux/thread_info.h>
@@ -475,4 +476,7 @@ unsigned long __must_check clear_user(void __user *to, unsigned long n) __ret; \ }) +#else /* CONFIG_MMU */ +#include <asm-generic/uaccess.h> +#endif /* CONFIG_MMU */ #endif /* _ASM_RISCV_UACCESS_H */
arch/riscv/kernel/Makefile +1 -2
@@ -25,9 +25,8 @@ obj-y += time.o obj-y += traps.o obj-y += riscv_ksyms.o obj-y += stacktrace.o -obj-y += vdso.o obj-y += cacheinfo.o -obj-y += vdso/ +obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += clint.o obj-$(CONFIG_FPU) += fpu.o
arch/riscv/kernel/entry.S +11 -0
@@ -370,6 +370,10 @@ ENTRY(__switch_to) ret ENDPROC(__switch_to) +#ifndef CONFIG_MMU +#define do_page_fault do_trap_unknown +#endif + .section ".rodata" /* Exception vector table */ ENTRY(excp_vect_table)
@@ -391,3 +395,10 @@ ENTRY(excp_vect_table) RISCV_PTR do_page_fault /* store page fault */ excp_vect_table_end: END(excp_vect_table) + +#ifndef CONFIG_MMU +ENTRY(__user_rt_sigreturn) + li a7, __NR_rt_sigreturn + scall +END(__user_rt_sigreturn) +#endif
arch/riscv/kernel/head.S +6 -0
@@ -104,8 +104,10 @@ clear_bss_done: la sp, init_thread_union + THREAD_SIZE mv a0, s1 call setup_vm +#ifdef CONFIG_MMU la a0, early_pg_dir call relocate +#endif /* CONFIG_MMU */ /* Restore C environment */ la tp, init_task
@@ -116,6 +118,7 @@ clear_bss_done: call parse_dtb tail start_kernel +#ifdef CONFIG_MMU relocate: /* Relocate return address */ li a1, PAGE_OFFSET
@@ -166,6 +169,7 @@ relocate: sfence.vma ret +#endif /* CONFIG_MMU */ .Lsecondary_start: #ifdef CONFIG_SMP
@@ -194,9 +198,11 @@ relocate: beqz tp, .Lwait_for_cpu_up fence +#ifdef CONFIG_MMU /* Enable virtual memory and relocate to virtual address */ la a0, swapper_pg_dir call relocate +#endif tail smp_callin #endif
arch/riscv/kernel/signal.c +16 -1
@@ -17,11 +17,16 @@#include <asm/switch_to.h> #include <asm/csr.h> +extern u32 __user_rt_sigreturn[2 ]; +#define DEBUG_SIG 0struct rt_sigframe {struct siginfo info;struct ucontext uc; +#ifndef CONFIG_MMU +u32 sigreturn_code[2 ]; +#endif };#ifdef CONFIG_FPU
@@ -166,7 +171,6 @@ static inline void __user *get_sigframe(struct ksignal *ksig,return (void __user *)sp; } -static int setup_rt_frame (struct ksignal *ksig,sigset_t *set,struct pt_regs *regs) {
@@ -189,8 +193,19 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,return -EFAULT ;/* Set up to return from userspace. */ +#ifdef CONFIG_MMU regs->ra = (unsigned long )VDSO_SYMBOL ( current->mm->context.vdso, rt_sigreturn); +#else +/* +* For the nommu case we don't have a VDSO. Instead we push two +* instructions to call the rt_sigreturn syscall onto the user stack. +*/ +if (copy_to_user (&frame->sigreturn_code, __user_rt_sigreturn, +sizeof (frame->sigreturn_code))) +return -EFAULT ; + regs->ra = (unsigned long )&frame->sigreturn_code;; +#endif /* CONFIG_MMU */ /* * Set up registers for signal handler.
arch/riscv/lib/Makefile +5 -6
@@ -1,7 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -lib-y += delay.o -lib-y += memcpy.o -lib-y += memset.o -lib-y += uaccess.o - -lib-$(CONFIG_64BIT) += tishift.o +lib-y += delay.o +lib-y += memcpy.o +lib-y += memset.o +lib-$(CONFIG_MMU) += uaccess.o +lib-$(CONFIG_64BIT) += tishift.o
arch/riscv/mm/Makefile +1 -2
@@ -6,9 +6,8 @@ CFLAGS_REMOVE_init.o = -pg endif obj-y += init.o -obj-y += fault.o obj-y += extable.o -obj-y += ioremap.o +obj-$(CONFIG_MMU) += fault.o ioremap.o obj-y += cacheflush.o obj-y += context.o obj-y += sifive_l2_cache.o
arch/riscv/mm/cacheflush.c +2 -0
@@ -78,6 +78,7 @@ void flush_icache_mm(struct mm_struct *mm, bool local)#endif /* CONFIG_SMP */ +#ifdef CONFIG_MMU void flush_icache_pte (pte_t pte) {struct page *page =pte_page (pte);
@@ -85,3 +86,4 @@ void flush_icache_pte(pte_t pte)if (!test_and_set_bit (PG_dcache_clean, &page->flags)) flush_icache_all(); } +#endif /* CONFIG_MMU */
arch/riscv/mm/context.c +2 -0
@@ -57,8 +57,10 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next,cpumask_clear_cpu (cpu,mm_cpumask (prev));cpumask_set_cpu (cpu,mm_cpumask (next)); +#ifdef CONFIG_MMU csr_write (CSR_SATP ,virt_to_pfn (next->pgd) |SATP_MODE );local_flush_tlb_all (); +#endif flush_icache_deferred (next); }
arch/riscv/mm/init.c +12 -1
@@ -23,6 +23,7 @@ unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]EXPORT_SYMBOL (empty_zero_page);extern char _start[]; +void *dtb_early_va;static void __init zone_sizes_init (void ) {
@@ -132,12 +133,12 @@ void __init setup_bootmem(void) } } +#ifdef CONFIG_MMU unsigned long va_pa_offset;EXPORT_SYMBOL (va_pa_offset);unsigned long pfn_base;EXPORT_SYMBOL (pfn_base); -void *dtb_early_va; pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
@@ -438,6 +439,16 @@ static void __init setup_vm_final(void)csr_write (CSR_SATP ,PFN_DOWN (__pa (swapper_pg_dir)) |SATP_MODE );local_flush_tlb_all (); } +#else +asmlinkage void __init setup_vm(uintptr_t dtb_pa) +{ + dtb_early_va = (void *)dtb_pa; +} + +static inline void setup_vm_final (void ) +{ +} +#endif /* CONFIG_MMU */ void __init paging_init (void ) {
Signature · 1 line
2.20.1
PATCH 19/20 riscv: provide a flat image loader
In reply to Christoph HellwigThis allows just loading the kernel at a pre-set address without qemu going bonkers trying to map the ELF file.
--- arch/riscv/Makefile | 13 +++++++++---- arch/riscv/boot/Makefile | 7 ++++++- arch/riscv/boot/loader.S | 8 ++++++++ arch/riscv/boot/loader.lds | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 arch/riscv/boot/loader.S create mode 100644 arch/riscv/boot/loader.lds
arch/riscv/Makefile +9 -4
@@ -80,13 +80,18 @@ PHONY += vdso_install vdso_install: $(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso $@ -all: Image.gz +ifeq ($(CONFIG_RISCV_M_MODE),y) +KBUILD_IMAGE := $(boot)/loader +else +KBUILD_IMAGE := $(boot)/Image.gz +endif +BOOT_TARGETS := Image Image.gz loader -Image: vmlinux - $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@ +all: $(notdir $(KBUILD_IMAGE)) -Image.%: Image +$(BOOT_TARGETS): vmlinux $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@ + @$(kecho) ' Kernel: $(boot)/$@ is ready' zinstall install: $(Q)$(MAKE) $(build)=$(boot) $@
arch/riscv/boot/Makefile +6 -1
@@ -16,7 +16,7 @@ OBJCOPYFLAGS_Image :=-O binary -R .note -R .note.gnu.build-id -R .comment -S -targets := Image +targets := Image loader $(obj)/Image: vmlinux FORCE $(call if_changed,objcopy)
@@ -24,6 +24,11 @@ $(obj)/Image: vmlinux FORCE $(obj)/Image.gz: $(obj)/Image FORCE $(call if_changed,gzip) +loader.o: $(src)/loader.S $(obj)/Image + +$(obj)/loader: $(obj)/loader.o $(obj)/Image FORCE + $(Q)$(LD) -T $(src)/loader.lds -o $@ $(obj)/loader.o + install: $(CONFIG_SHELL) $(srctree)/$(src)/install.sh $(KERNELRELEASE) \ $(obj)/Image System.map "$(INSTALL_PATH)"
arch/riscv/boot/loader.S new file +8 -0
@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + + .align 4 + .section .payload, "ax", %progbits + .globl _start +_start: + .incbin "arch/riscv/boot/Image" +
arch/riscv/boot/loader.lds new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS +{ + . = 0x80000000; + + .payload : { + *(.payload) + . = ALIGN(8); + } +}
Signature · 1 line
2.20.1
PATCH 20/20 riscv: disable the EFI PECOFF header for M-mode
In reply to Christoph HellwigNo point in bloating the kernel image with a bootloader header if we run bare metal.
--- arch/riscv/kernel/head.S | 2 ++ 1 file changed, 2 insertions(+)
arch/riscv/kernel/head.S +2 -0
@@ -16,6 +16,7 @@ __INIT ENTRY(_start) +#ifndef CONFIG_RISCV_M_MODE /* * Image header expected by Linux boot-loaders. The image header data * structure is described in asm/image.h.
@@ -47,6 +48,7 @@ ENTRY(_start) .global _start_kernel _start_kernel: +#endif /* CONFIG_RISCV_M_MODE */ /* Mask all interrupts */ csrw CSR_XIE, zero csrw CSR_XIP, zero
Signature · 1 line
2.20.1