Skip to content

Commit 447ae00

Browse files
zhengxiaolinXRealFYang
authored andcommittedMay 30, 2022
8287418: riscv: Fix correctness issue of MacroAssembler::movptr
Reviewed-by: fjiang, yadongwang, fyang
1 parent a27ba1a commit 447ae00

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed
 

‎src/hotspot/cpu/riscv/assembler_riscv.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,23 @@ void Assembler::movptr_with_offset(Register Rd, address addr, int32_t &offset) {
282282
}
283283
#endif
284284
assert(is_unsigned_imm_in_range(imm64, 47, 0) || (imm64 == (uintptr_t)-1),
285-
"48-bit overflow in address constant");
286-
// Load upper 32 bits
287-
int32_t imm = imm64 >> 16;
285+
"bit 47 overflows in address constant");
286+
// Load upper 31 bits
287+
int32_t imm = imm64 >> 17;
288288
int64_t upper = imm, lower = imm;
289289
lower = (lower << 52) >> 52;
290290
upper -= lower;
291291
upper = (int32_t)upper;
292292
lui(Rd, upper);
293293
addi(Rd, Rd, lower);
294294

295-
// Load the rest 16 bits.
295+
// Load the rest 17 bits.
296296
slli(Rd, Rd, 11);
297-
addi(Rd, Rd, (imm64 >> 5) & 0x7ff);
298-
slli(Rd, Rd, 5);
297+
addi(Rd, Rd, (imm64 >> 6) & 0x7ff);
298+
slli(Rd, Rd, 6);
299299

300300
// This offset will be used by following jalr/ld.
301-
offset = imm64 & 0x1f;
301+
offset = imm64 & 0x3f;
302302
}
303303

304304
void Assembler::movptr(Register Rd, uintptr_t imm64) {

‎src/hotspot/cpu/riscv/gc/shared/barrierSetNMethod_riscv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static const struct CheckInsn barrierInsn[] = {
7373
{ 0x000fffff, 0x00028293, "addi t0, t0, imm1 "},
7474
{ 0xffffffff, 0x00b29293, "slli t0, t0, 11 "},
7575
{ 0x000fffff, 0x00028293, "addi t0, t0, imm2 "},
76-
{ 0xffffffff, 0x00529293, "slli t0, t0, 5 "},
76+
{ 0xffffffff, 0x00629293, "slli t0, t0, 6 "},
7777
{ 0x000fffff, 0x000280e7, "jalr ra, imm3(t0) "},
7878
{ 0x00000fff, 0x0000006f, "j skip "}
7979
/* guard: */

‎src/hotspot/cpu/riscv/macroAssembler_riscv.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,12 @@ static int patch_offset_in_pc_relative(address branch, int64_t offset) {
11811181

11821182
static int patch_addr_in_movptr(address branch, address target) {
11831183
const int MOVPTR_INSTRUCTIONS_NUM = 6; // lui + addi + slli + addi + slli + addi/jalr/load
1184-
int32_t lower = ((intptr_t)target << 36) >> 36;
1185-
int64_t upper = ((intptr_t)target - lower) >> 28;
1186-
Assembler::patch(branch + 0, 31, 12, upper & 0xfffff); // Lui. target[47:28] + target[27] ==> branch[31:12]
1187-
Assembler::patch(branch + 4, 31, 20, (lower >> 16) & 0xfff); // Addi. target[27:16] ==> branch[31:20]
1188-
Assembler::patch(branch + 12, 31, 20, (lower >> 5) & 0x7ff); // Addi. target[15: 5] ==> branch[31:20]
1189-
Assembler::patch(branch + 20, 31, 20, lower & 0x1f); // Addi/Jalr/Load. target[ 4: 0] ==> branch[31:20]
1184+
int32_t lower = ((intptr_t)target << 35) >> 35;
1185+
int64_t upper = ((intptr_t)target - lower) >> 29;
1186+
Assembler::patch(branch + 0, 31, 12, upper & 0xfffff); // Lui. target[48:29] + target[28] ==> branch[31:12]
1187+
Assembler::patch(branch + 4, 31, 20, (lower >> 17) & 0xfff); // Addi. target[28:17] ==> branch[31:20]
1188+
Assembler::patch(branch + 12, 31, 20, (lower >> 6) & 0x7ff); // Addi. target[16: 6] ==> branch[31:20]
1189+
Assembler::patch(branch + 20, 31, 20, lower & 0x3f); // Addi/Jalr/Load. target[ 5: 0] ==> branch[31:20]
11901190
return MOVPTR_INSTRUCTIONS_NUM * NativeInstruction::instruction_size;
11911191
}
11921192

@@ -1258,9 +1258,9 @@ static long get_offset_of_pc_relative(address insn_addr) {
12581258

12591259
static address get_target_of_movptr(address insn_addr) {
12601260
assert_cond(insn_addr != NULL);
1261-
intptr_t target_address = (((int64_t)Assembler::sextract(((unsigned*)insn_addr)[0], 31, 12)) & 0xfffff) << 28; // Lui.
1262-
target_address += ((int64_t)Assembler::sextract(((unsigned*)insn_addr)[1], 31, 20)) << 16; // Addi.
1263-
target_address += ((int64_t)Assembler::sextract(((unsigned*)insn_addr)[3], 31, 20)) << 5; // Addi.
1261+
intptr_t target_address = (((int64_t)Assembler::sextract(((unsigned*)insn_addr)[0], 31, 12)) & 0xfffff) << 29; // Lui.
1262+
target_address += ((int64_t)Assembler::sextract(((unsigned*)insn_addr)[1], 31, 20)) << 17; // Addi.
1263+
target_address += ((int64_t)Assembler::sextract(((unsigned*)insn_addr)[3], 31, 20)) << 6; // Addi.
12641264
target_address += ((int64_t)Assembler::sextract(((unsigned*)insn_addr)[5], 31, 20)); // Addi/Jalr/Load.
12651265
return (address) target_address;
12661266
}

‎src/hotspot/cpu/riscv/macroAssembler_riscv.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,8 @@ class MacroAssembler: public Assembler {
815815

816816
// Return true if an address is within the 48-bit RISCV64 address space.
817817
bool is_valid_riscv64_address(address addr) {
818-
return ((uintptr_t)addr >> 48) == 0;
818+
// sv48: must have bits 63–48 all equal to bit 47
819+
return ((uintptr_t)addr >> 47) == 0;
819820
}
820821

821822
void ld_constant(Register dest, const Address &const_addr) {

‎src/hotspot/cpu/riscv/nativeInst_riscv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bool NativeInstruction::is_movptr_at(address instr) {
8989
is_addi_at(instr + instruction_size) && // Addi
9090
is_slli_shift_at(instr + instruction_size * 2, 11) && // Slli Rd, Rs, 11
9191
is_addi_at(instr + instruction_size * 3) && // Addi
92-
is_slli_shift_at(instr + instruction_size * 4, 5) && // Slli Rd, Rs, 5
92+
is_slli_shift_at(instr + instruction_size * 4, 6) && // Slli Rd, Rs, 6
9393
(is_addi_at(instr + instruction_size * 5) ||
9494
is_jalr_at(instr + instruction_size * 5) ||
9595
is_load_at(instr + instruction_size * 5)) && // Addi/Jalr/Load

0 commit comments

Comments
 (0)
Please sign in to comment.