SC.D

RISC-V SC.D 指令详解

指令手册R-type

在 RV64 上按预留状态条件存储 64 位 doubleword;成功 rd=0,失败 rd 为非零且不写内存

指令语法

sc.d rd, rs2, (rs1)
操作数说明
目标寄存器 rd:存放运算结果的通用寄存器。
源寄存器 rs1:第一个操作数寄存器。
源寄存器 rs2:第二个操作数寄存器。
AZalrsc原子操作

指令行为说明

SC.D 将 rs2 的 64 位 doubleword 作为候选写入值。只有当当前 hart 的有效预留包含 rs1 指向的目标字节时,SC.D 才把该值写入内存并令 rd=0;失败时不写内存,并向 rd 写入非零失败码。无论成功或失败,SC.D 都会使当前预留失效。

SC.D 原子指令解析与执行动画

展示 A-extension doubleword 原子指令的字段解析、aq/rl 排序位、内存读取和 RMW 状态更新。

rd
rs2
rs1
sc.d
,
,(
)
执行环境
31..27
26
25
24..20
19..15
14..12
11..7
6..0
00011
funct5
0
aq
1
rl
01011
rs2
01010
rs1
011
funct3
00110
rd
0101111
opcode
原子数据路径
instruction
0x1AB5332F
opcode
0101111 -> AMO
funct3
011 -> doubleword width
funct5
00011 -> SC.D
aq / rl
0/1 -> release
rd / rs2 / rs1
t1(x6) / a1(x11)=0x0000000000000009 / a0(x10)=0x00001000
memory read
mem[0x00001000] -> old doubleword 0x0000000000000005
condition
SC succeeds -> store 0x0000000000000009
rd / memory / reservation
t1(x6) = 0x00000000; mem[0x00001000] = 0x0000000000000009; reservation invalidated
当前步骤

概念步骤:拿到 32-bit 原子指令编码

A 扩展 doubleword 原子指令使用 32-bit 编码;动画先展示字段切分。

encoding: 0x1AB5332F
syntax : sc.d t1(x6), a1(x11), (a0(x10))
result : t1(x6) = 0x00000000; mem[0x00001000] = 0x0000000000000009; reservation invalidated
64-bit doubleword 结果视图
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
write doubleword
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
write
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

该动画展示 ISA 可见的原子读改写、比较并交换、LR/SC 预留状态和内存排序位,不表示任何特定 CPU 的缓存一致性实现、流水线或时序。

快速理解与检索要点

SC.D 是 RV64 LR/SC 序列的条件写入点。成功时写入 rs2 的 64 位值并让 rd=0;失败时内存保持旧值,rd 为非零。

funct5=00011、funct3=011、opcode=0101111 识别 SC.D;rd 是状态结果寄存器。
rs2 提供 64 位候选存储数据;rd 接收 0 或非零成功/失败状态。
SC.D 失败时不会写内存;成功和失败都会使当前 hart 的预留失效。
官方语义只保证失败码非零;可移植软件不应依赖具体失败码。

常见使用场景

原子与同步

结合 «sc.d.rl t0, a1, (a0) # store 64-bit a1 to *a0 if the reservation is valid; t0=0 on success» 等实际代码理解该场景。

RV64 专用操作

结合 «sc.d.rl t0, a1, (a0) # store 64-bit a1 to *a0 if the reservation is valid; t0=0 on success» 等实际代码理解该场景。

使用前检查清单

语法检查
  • 确认当前指令格式为 R-type。
  • 确认操作数排列顺序与示例一致。
语义检查
  • 确认目标寄存器用途和调用约定兼容。
  • 确认该指令不是伪指令展开后的底层形式。

容易混淆 / 常见误区

SC.D 可能失败,软件必须检查 rd
失败码只保证非零,不要依赖具体数值
地址必须 8 字节自然对齐
仅 RV64 可用
SC.D 成功或失败都会使预留失效

常见问题

SC.D 成功时 rd 保存写入的数据吗?

不。rd 是状态结果寄存器:成功写 0,失败写非零;被写入内存的数据来自 rs2。

SC.D 失败时内存会改变吗?

不会。SC.D 失败时不执行内存写入,只写 rd 的非零失败码,并使预留失效。