Skip to content

Commit 0bfbc91

Browse files
committed
Merge tag 'riscv-for-linus-6.10-mw1' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux
Pull RISC-V updates from Palmer Dabbelt: - Add byte/half-word compare-and-exchange, emulated via LR/SC loops - Support for Rust - Support for Zihintpause in hwprobe - Add PR_RISCV_SET_ICACHE_FLUSH_CTX prctl() - Support lockless lockrefs * tag 'riscv-for-linus-6.10-mw1' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux: (42 commits) riscv: defconfig: Enable CONFIG_CLK_SOPHGO_CV1800 riscv: select ARCH_HAS_FAST_MULTIPLIER riscv: mm: still create swiotlb buffer for kmalloc() bouncing if required riscv: Annotate pgtable_l{4,5}_enabled with __ro_after_init riscv: Remove redundant CONFIG_64BIT from pgtable_l{4,5}_enabled riscv: mm: Always use an ASID to flush mm contexts riscv: mm: Preserve global TLB entries when switching contexts riscv: mm: Make asid_bits a local variable riscv: mm: Use a fixed layout for the MM context ID riscv: mm: Introduce cntx2asid/cntx2version helper macros riscv: Avoid TLB flush loops when affected by SiFive CIP-1200 riscv: Apply SiFive CIP-1200 workaround to single-ASID sfence.vma riscv: mm: Combine the SMP and UP TLB flush code riscv: Only send remote fences when some other CPU is online riscv: mm: Broadcast kernel TLB flushes only when needed riscv: Use IPIs for remote cache/TLB flushes by default riscv: Factor out page table TLB synchronization riscv: Flush the instruction cache during SMP bringup riscv: hwprobe: export Zihintpause ISA extension riscv: misaligned: remove CONFIG_RISCV_M_MODE specific code ...
2 parents 4f05e82 + 92cce91 commit 0bfbc91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+754
-667
lines changed

Documentation/arch/riscv/cmodx.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
==============================================================================
4+
Concurrent Modification and Execution of Instructions (CMODX) for RISC-V Linux
5+
==============================================================================
6+
7+
CMODX is a programming technique where a program executes instructions that were
8+
modified by the program itself. Instruction storage and the instruction cache
9+
(icache) are not guaranteed to be synchronized on RISC-V hardware. Therefore, the
10+
program must enforce its own synchronization with the unprivileged fence.i
11+
instruction.
12+
13+
However, the default Linux ABI prohibits the use of fence.i in userspace
14+
applications. At any point the scheduler may migrate a task onto a new hart. If
15+
migration occurs after the userspace synchronized the icache and instruction
16+
storage with fence.i, the icache on the new hart will no longer be clean. This
17+
is due to the behavior of fence.i only affecting the hart that it is called on.
18+
Thus, the hart that the task has been migrated to may not have synchronized
19+
instruction storage and icache.
20+
21+
There are two ways to solve this problem: use the riscv_flush_icache() syscall,
22+
or use the ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` prctl() and emit fence.i in
23+
userspace. The syscall performs a one-off icache flushing operation. The prctl
24+
changes the Linux ABI to allow userspace to emit icache flushing operations.
25+
26+
As an aside, "deferred" icache flushes can sometimes be triggered in the kernel.
27+
At the time of writing, this only occurs during the riscv_flush_icache() syscall
28+
and when the kernel uses copy_to_user_page(). These deferred flushes happen only
29+
when the memory map being used by a hart changes. If the prctl() context caused
30+
an icache flush, this deferred icache flush will be skipped as it is redundant.
31+
Therefore, there will be no additional flush when using the riscv_flush_icache()
32+
syscall inside of the prctl() context.
33+
34+
prctl() Interface
35+
---------------------
36+
37+
Call prctl() with ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` as the first argument. The
38+
remaining arguments will be delegated to the riscv_set_icache_flush_ctx
39+
function detailed below.
40+
41+
.. kernel-doc:: arch/riscv/mm/cacheflush.c
42+
:identifiers: riscv_set_icache_flush_ctx
43+
44+
Example usage:
45+
46+
The following files are meant to be compiled and linked with each other. The
47+
modify_instruction() function replaces an add with 0 with an add with one,
48+
causing the instruction sequence in get_value() to change from returning a zero
49+
to returning a one.
50+
51+
cmodx.c::
52+
53+
#include <stdio.h>
54+
#include <sys/prctl.h>
55+
56+
extern int get_value();
57+
extern void modify_instruction();
58+
59+
int main()
60+
{
61+
int value = get_value();
62+
printf("Value before cmodx: %d\n", value);
63+
64+
// Call prctl before first fence.i is called inside modify_instruction
65+
prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_ON, PR_RISCV_CTX_SW_FENCEI, PR_RISCV_SCOPE_PER_PROCESS);
66+
modify_instruction();
67+
// Call prctl after final fence.i is called in process
68+
prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_OFF, PR_RISCV_CTX_SW_FENCEI, PR_RISCV_SCOPE_PER_PROCESS);
69+
70+
value = get_value();
71+
printf("Value after cmodx: %d\n", value);
72+
return 0;
73+
}
74+
75+
cmodx.S::
76+
77+
.option norvc
78+
79+
.text
80+
.global modify_instruction
81+
modify_instruction:
82+
lw a0, new_insn
83+
lui a5,%hi(old_insn)
84+
sw a0,%lo(old_insn)(a5)
85+
fence.i
86+
ret
87+
88+
.section modifiable, "awx"
89+
.global get_value
90+
get_value:
91+
li a0, 0
92+
old_insn:
93+
addi a0, a0, 0
94+
ret
95+
96+
.data
97+
new_insn:
98+
addi a0, a0, 1

Documentation/arch/riscv/hwprobe.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ The following keys are defined:
188188
manual starting from commit 95cf1f9 ("Add changes requested by Ved
189189
during signoff")
190190

191+
* :c:macro:`RISCV_HWPROBE_EXT_ZIHINTPAUSE`: The Zihintpause extension is
192+
supported as defined in the RISC-V ISA manual starting from commit
193+
d8ab5c78c207 ("Zihintpause is ratified").
194+
191195
* :c:macro:`RISCV_HWPROBE_KEY_CPUPERF_0`: A bitmask that contains performance
192196
information about the selected set of processors.
193197

Documentation/arch/riscv/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RISC-V architecture
1313
patch-acceptance
1414
uabi
1515
vector
16+
cmodx
1617

1718
features
1819

Documentation/rust/arch-support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Architecture Level of support Constraints
1717
============= ================ ==============================================
1818
``arm64`` Maintained Little Endian only.
1919
``loongarch`` Maintained \-
20+
``riscv`` Maintained ``riscv64`` only.
2021
``um`` Maintained ``x86_64`` only.
2122
``x86`` Maintained ``x86_64`` only.
2223
============= ================ ==============================================

arch/riscv/Kconfig

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config RISCV
2323
select ARCH_HAS_DEBUG_VIRTUAL if MMU
2424
select ARCH_HAS_DEBUG_VM_PGTABLE
2525
select ARCH_HAS_DEBUG_WX
26+
select ARCH_HAS_FAST_MULTIPLIER
2627
select ARCH_HAS_FORTIFY_SOURCE
2728
select ARCH_HAS_GCOV_PROFILE_ALL
2829
select ARCH_HAS_GIGANTIC_PAGE
@@ -57,10 +58,11 @@ config RISCV
5758
select ARCH_SUPPORTS_PAGE_TABLE_CHECK if MMU
5859
select ARCH_SUPPORTS_PER_VMA_LOCK if MMU
5960
select ARCH_SUPPORTS_SHADOW_CALL_STACK if HAVE_SHADOW_CALL_STACK
61+
select ARCH_USE_CMPXCHG_LOCKREF if 64BIT
6062
select ARCH_USE_MEMTEST
6163
select ARCH_USE_QUEUED_RWLOCKS
6264
select ARCH_USES_CFI_TRAPS if CFI_CLANG
63-
select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH if SMP && MMU
65+
select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH if MMU
6466
select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
6567
select ARCH_WANT_FRAME_POINTERS
6668
select ARCH_WANT_GENERAL_HUGETLB if !RISCV_ISA_SVNAPOT
@@ -71,7 +73,7 @@ config RISCV
7173
select ARCH_WANTS_THP_SWAP if HAVE_ARCH_TRANSPARENT_HUGEPAGE
7274
select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
7375
select BUILDTIME_TABLE_SORT if MMU
74-
select CLINT_TIMER if !MMU
76+
select CLINT_TIMER if RISCV_M_MODE
7577
select CLONE_BACKWARDS
7678
select COMMON_CLK
7779
select CPU_PM if CPU_IDLE || HIBERNATION || SUSPEND
@@ -155,6 +157,7 @@ config RISCV
155157
select HAVE_REGS_AND_STACK_ACCESS_API
156158
select HAVE_RETHOOK if !XIP_KERNEL
157159
select HAVE_RSEQ
160+
select HAVE_RUST if 64BIT
158161
select HAVE_SAMPLE_FTRACE_DIRECT
159162
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
160163
select HAVE_STACKPROTECTOR
@@ -231,8 +234,12 @@ config ARCH_MMAP_RND_COMPAT_BITS_MAX
231234

232235
# set if we run in machine mode, cleared if we run in supervisor mode
233236
config RISCV_M_MODE
234-
bool
235-
default !MMU
237+
bool "Build a kernel that runs in machine mode"
238+
depends on !MMU
239+
default y
240+
help
241+
Select this option if you want to run the kernel in M-mode,
242+
without the assistance of any other firmware.
236243

237244
# set if we are running in S-mode and can use SBI calls
238245
config RISCV_SBI
@@ -249,8 +256,9 @@ config MMU
249256

250257
config PAGE_OFFSET
251258
hex
252-
default 0xC0000000 if 32BIT && MMU
253-
default 0x80000000 if !MMU
259+
default 0x80000000 if !MMU && RISCV_M_MODE
260+
default 0x80200000 if !MMU
261+
default 0xc0000000 if 32BIT
254262
default 0xff60000000000000 if 64BIT
255263

256264
config KASAN_SHADOW_OFFSET
@@ -598,7 +606,6 @@ config TOOLCHAIN_HAS_VECTOR_CRYPTO
598606
config RISCV_ISA_ZBB
599607
bool "Zbb extension support for bit manipulation instructions"
600608
depends on TOOLCHAIN_HAS_ZBB
601-
depends on MMU
602609
depends on RISCV_ALTERNATIVE
603610
default y
604611
help
@@ -630,7 +637,6 @@ config RISCV_ISA_ZICBOM
630637

631638
config RISCV_ISA_ZICBOZ
632639
bool "Zicboz extension support for faster zeroing of memory"
633-
depends on MMU
634640
depends on RISCV_ALTERNATIVE
635641
default y
636642
help

arch/riscv/Makefile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
3434
KBUILD_AFLAGS += -mabi=lp64
3535

3636
KBUILD_LDFLAGS += -melf64lriscv
37+
38+
KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
39+
-Cno-redzone
3740
else
3841
BITS := 32
3942
UTS_MACHINE := riscv32
@@ -68,6 +71,10 @@ riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
6871
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c
6972
riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v
7073

74+
ifneq ($(CONFIG_RISCV_ISA_C),y)
75+
KBUILD_RUSTFLAGS += -Ctarget-feature=-c
76+
endif
77+
7178
ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC
7279
KBUILD_CFLAGS += -Wa,-misa-spec=2.2
7380
KBUILD_AFLAGS += -Wa,-misa-spec=2.2
@@ -133,7 +140,15 @@ boot := arch/riscv/boot
133140
ifeq ($(CONFIG_XIP_KERNEL),y)
134141
KBUILD_IMAGE := $(boot)/xipImage
135142
else
143+
ifeq ($(CONFIG_RISCV_M_MODE)$(CONFIG_SOC_CANAAN_K210),yy)
144+
KBUILD_IMAGE := $(boot)/loader.bin
145+
else
146+
ifeq ($(CONFIG_EFI_ZBOOT),)
136147
KBUILD_IMAGE := $(boot)/Image.gz
148+
else
149+
KBUILD_IMAGE := $(boot)/vmlinuz.efi
150+
endif
151+
endif
137152
endif
138153

139154
libs-y += arch/riscv/lib/
@@ -153,17 +168,6 @@ endif
153168
vdso-install-y += arch/riscv/kernel/vdso/vdso.so.dbg
154169
vdso-install-$(CONFIG_COMPAT) += arch/riscv/kernel/compat_vdso/compat_vdso.so.dbg
155170

156-
ifneq ($(CONFIG_XIP_KERNEL),y)
157-
ifeq ($(CONFIG_RISCV_M_MODE)$(CONFIG_SOC_CANAAN_K210),yy)
158-
KBUILD_IMAGE := $(boot)/loader.bin
159-
else
160-
ifeq ($(CONFIG_EFI_ZBOOT),)
161-
KBUILD_IMAGE := $(boot)/Image.gz
162-
else
163-
KBUILD_IMAGE := $(boot)/vmlinuz.efi
164-
endif
165-
endif
166-
endif
167171
BOOT_TARGETS := Image Image.gz loader loader.bin xipImage vmlinuz.efi
168172

169173
all: $(notdir $(KBUILD_IMAGE))

arch/riscv/configs/defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ CONFIG_VIRTIO_BALLOON=y
234234
CONFIG_VIRTIO_INPUT=y
235235
CONFIG_VIRTIO_MMIO=y
236236
CONFIG_RENESAS_OSTM=y
237+
CONFIG_CLK_SOPHGO_CV1800=y
237238
CONFIG_SUN8I_DE2_CCU=m
238239
CONFIG_SUN50I_IOMMU=y
239240
CONFIG_RPMSG_CHAR=y

arch/riscv/errata/sifive/errata.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static bool errata_cip_1200_check_func(unsigned long arch_id, unsigned long imp
4242
return false;
4343
if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626)
4444
return false;
45+
46+
#ifdef CONFIG_MMU
47+
tlb_flush_all_threshold = 0;
48+
#endif
49+
4550
return true;
4651
}
4752

0 commit comments

Comments
 (0)