Skip to content

Commit d4b0021

Browse files
zifeihanRealFYang
authored andcommitted
8291893: riscv: remove fence.i used in user space
8291947: riscv: fail to build after JDK-8290840 8310656: RISC-V: __builtin___clear_cache can fail silently. Reviewed-by: fyang, luhenry Backport-of: 5a539e8da7dbea1eaa10d799f75199ea359f7a22
1 parent f71648c commit d4b0021

12 files changed

+141
-61
lines changed

src/hotspot/cpu/riscv/assembler_riscv.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,6 @@ void Assembler::movptr(Register Rd, address addr) {
311311
addi(Rd, Rd, offset);
312312
}
313313

314-
void Assembler::ifence() {
315-
fence_i();
316-
if (UseConservativeFence) {
317-
fence(ir, ir);
318-
}
319-
}
320-
321314
#define INSN(NAME, NEG_INSN) \
322315
void Assembler::NAME(Register Rs, Register Rt, const address &dest) { \
323316
NEG_INSN(Rt, Rs, dest); \

src/hotspot/cpu/riscv/assembler_riscv.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ class Assembler : public AbstractAssembler {
318318
void movptr(Register Rd, address addr);
319319
void movptr_with_offset(Register Rd, address addr, int32_t &offset);
320320
void movptr(Register Rd, uintptr_t imm64);
321-
void ifence();
322321
void j(const address &dest, Register temp = t0);
323322
void j(const Address &adr, Register temp = t0);
324323
void j(Label &l, Register temp = t0);
@@ -897,7 +896,6 @@ class Assembler : public AbstractAssembler {
897896
emit(insn); \
898897
}
899898

900-
INSN(fence_i, 0b0001111, 0b001, 0b000000000000);
901899
INSN(ecall, 0b1110011, 0b000, 0b000000000000);
902900
INSN(_ebreak, 0b1110011, 0b000, 0b000000000001);
903901

src/hotspot/cpu/riscv/compiledIC_riscv.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark)
6969
#undef __
7070

7171
int CompiledStaticCall::to_interp_stub_size() {
72-
// fence_i + fence* + (lui, addi, slli, addi, slli, addi) + (lui, addi, slli, addi, slli) + jalr
73-
return NativeFenceI::instruction_size() + 12 * NativeInstruction::instruction_size;
72+
// (lui, addi, slli, addi, slli, addi) + (lui, addi, slli, addi, slli) + jalr
73+
return 12 * NativeInstruction::instruction_size;
7474
}
7575

7676
int CompiledStaticCall::to_trampoline_stub_size() {
@@ -97,8 +97,7 @@ void CompiledDirectStaticCall::set_to_interpreted(const methodHandle& callee, ad
9797
}
9898

9999
// Creation also verifies the object.
100-
NativeMovConstReg* method_holder
101-
= nativeMovConstReg_at(stub + NativeFenceI::instruction_size());
100+
NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
102101
#ifndef PRODUCT
103102
NativeGeneralJump* jump = nativeGeneralJump_at(method_holder->next_instruction_address());
104103

@@ -123,8 +122,7 @@ void CompiledDirectStaticCall::set_stub_to_clean(static_stub_Relocation* static_
123122
address stub = static_stub->addr();
124123
assert(stub != NULL, "stub not found");
125124
// Creation also verifies the object.
126-
NativeMovConstReg* method_holder
127-
= nativeMovConstReg_at(stub + NativeFenceI::instruction_size());
125+
NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
128126
method_holder->set_data(0);
129127
}
130128

@@ -141,8 +139,7 @@ void CompiledDirectStaticCall::verify() {
141139
address stub = find_stub(false /* is_aot */);
142140
assert(stub != NULL, "no stub found for static call");
143141
// Creation also verifies the object.
144-
NativeMovConstReg* method_holder
145-
= nativeMovConstReg_at(stub + NativeFenceI::instruction_size());
142+
NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
146143
NativeJump* jump = nativeJump_at(method_holder->next_instruction_address());
147144

148145
// Verify state.

src/hotspot/cpu/riscv/globals_riscv.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ define_pd_global(intx, InlineSmallCode, 1000);
9999
product(bool, TraceTraps, false, "Trace all traps the signal handler") \
100100
/* For now we're going to be safe and add the I/O bits to userspace fences. */ \
101101
product(bool, UseConservativeFence, true, \
102-
"Extend i for r and o for w in the pred/succ flags of fence;" \
103-
"Extend fence.i to fence.i + fence.") \
102+
"Extend i for r and o for w in the pred/succ flags of fence") \
104103
product(bool, AvoidUnalignedAccesses, true, \
105104
"Avoid generating unaligned memory accesses") \
106105
experimental(bool, UseRVV, false, "Use RVV instructions") \

src/hotspot/cpu/riscv/icache_riscv.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
4+
* Copyright (c) 2023, Rivos Inc. All rights reserved.
45
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56
*
67
* This code is free software; you can redistribute it and/or modify it
@@ -25,16 +26,32 @@
2526

2627
#include "precompiled.hpp"
2728
#include "asm/macroAssembler.hpp"
29+
#include "riscv_flush_icache.hpp"
30+
#include "runtime/java.hpp"
2831
#include "runtime/icache.hpp"
2932

3033
#define __ _masm->
3134

3235
static int icache_flush(address addr, int lines, int magic) {
33-
os::icache_flush((long int) addr, (long int) (addr + (lines << ICache::log2_line_size)));
36+
// To make a store to instruction memory visible to all RISC-V harts,
37+
// the writing hart has to execute a data FENCE before requesting that
38+
// all remote RISC-V harts execute a FENCE.I.
39+
40+
// We need to make sure stores happens before the I/D cache synchronization.
41+
__asm__ volatile("fence rw, rw" : : : "memory");
42+
43+
RiscvFlushIcache::flush((uintptr_t)addr, ((uintptr_t)lines) << ICache::log2_line_size);
44+
3445
return magic;
3546
}
3647

3748
void ICacheStubGenerator::generate_icache_flush(ICache::flush_icache_stub_t* flush_icache_stub) {
49+
// Only riscv_flush_icache is supported as I-cache synchronization.
50+
// We must make sure the VM can execute such without error.
51+
if (!RiscvFlushIcache::test()) {
52+
vm_exit_during_initialization("Unable to synchronize I-cache");
53+
}
54+
3855
address start = (address)icache_flush;
3956
*flush_icache_stub = (ICache::flush_icache_stub_t)start;
4057

src/hotspot/cpu/riscv/macroAssembler_riscv.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ void MacroAssembler::emit_static_call_stub() {
574574
// CompiledDirectStaticCall::set_to_interpreted knows the
575575
// exact layout of this stub.
576576

577-
ifence();
578577
mov_metadata(xmethod, (Metadata*)NULL);
579578

580579
// Jump to the entry point of the i2c stub.
@@ -4137,10 +4136,6 @@ void MacroAssembler::cmp_l2i(Register dst, Register src1, Register src2, Registe
41374136
bind(done);
41384137
}
41394138

4140-
void MacroAssembler::safepoint_ifence() {
4141-
ifence();
4142-
}
4143-
41444139
#ifdef COMPILER2
41454140
// short string
41464141
// StringUTF16.indexOfChar

src/hotspot/cpu/riscv/macroAssembler_riscv.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ class MacroAssembler: public Assembler {
7171
atomic_incw(tmp1, tmp2);
7272
}
7373

74-
// Place a fence.i after code may have been modified due to a safepoint.
75-
void safepoint_ifence();
76-
7774
// Alignment
7875
void align(int modulus, int extra_offset = 0);
7976

src/hotspot/cpu/riscv/nativeInst_riscv.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
// - - NativeIllegalInstruction
4242
// - - NativeCallTrampolineStub
4343
// - - NativeMembar
44-
// - - NativeFenceI
4544

4645
// The base class for different kinds of native instruction abstractions.
4746
// Provides the primitive operations to manipulate code relative to this.
@@ -553,12 +552,4 @@ inline NativeMembar *NativeMembar_at(address addr) {
553552
return (NativeMembar*)addr;
554553
}
555554

556-
class NativeFenceI : public NativeInstruction {
557-
public:
558-
static inline int instruction_size() {
559-
// 2 for fence.i + fence
560-
return (UseConservativeFence ? 2 : 1) * NativeInstruction::instruction_size;
561-
}
562-
};
563-
564555
#endif // CPU_RISCV_NATIVEINST_RISCV_HPP

src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,6 @@ static void patch_callers_callsite(MacroAssembler *masm) {
331331
__ la_patchable(t0, RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)), offset);
332332
__ jalr(x1, t0, offset);
333333

334-
// Explicit fence.i required because fixup_callers_callsite may change the code
335-
// stream.
336-
__ safepoint_ifence();
337-
338334
__ pop_CPU_state();
339335
// restore sp
340336
__ leave();

src/hotspot/os_cpu/linux_riscv/os_linux_riscv.hpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,4 @@
3737
*(jlong *) dst = *(const jlong *) src;
3838
}
3939

40-
// SYSCALL_RISCV_FLUSH_ICACHE is used to flush instruction cache. The "fence.i" instruction
41-
// only work on the current hart, so kernel provides the icache flush syscall to flush icache
42-
// on each hart. You can pass a flag to determine a global or local icache flush.
43-
static void icache_flush(long int start, long int end)
44-
{
45-
const int SYSCALL_RISCV_FLUSH_ICACHE = 259;
46-
register long int __a7 asm ("a7") = SYSCALL_RISCV_FLUSH_ICACHE;
47-
register long int __a0 asm ("a0") = start;
48-
register long int __a1 asm ("a1") = end;
49-
// the flush can be applied to either all threads or only the current.
50-
// 0 means a global icache flush, and the icache flush will be applied
51-
// to other harts concurrently executing.
52-
register long int __a2 asm ("a2") = 0;
53-
__asm__ volatile ("ecall\n\t"
54-
: "+r" (__a0)
55-
: "r" (__a0), "r" (__a1), "r" (__a2), "r" (__a7)
56-
: "memory");
57-
}
58-
5940
#endif // OS_CPU_LINUX_RISCV_VM_OS_LINUX_RISCV_HPP

0 commit comments

Comments
 (0)