Skip to content

Commit 8907952

Browse files
xry111palmer-dabbelt
authored andcommitted
RISC-V: vDSO: Wire up getrandom() vDSO implementation
Hook up the generic vDSO implementation to the generic vDSO getrandom implementation by providing the required __arch_chacha20_blocks_nostack and getrandom_syscall implementations. Also wire up the selftests. The benchmark result: vdso: 25000000 times in 2.466341333 seconds libc: 25000000 times in 41.447720005 seconds syscall: 25000000 times in 41.043926672 seconds vdso: 25000000 x 256 times in 162.286219353 seconds libc: 25000000 x 256 times in 2953.855018685 seconds syscall: 25000000 x 256 times in 2796.268546000 seconds Signed-off-by: Xi Ruoyao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 0af2f6b commit 8907952

File tree

7 files changed

+300
-0
lines changed

7 files changed

+300
-0
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ config RISCV
218218
select THREAD_INFO_IN_TASK
219219
select TRACE_IRQFLAGS_SUPPORT
220220
select UACCESS_MEMCPY if !MMU
221+
select VDSO_GETRANDOM if HAVE_GENERIC_VDSO
221222
select USER_STACKTRACE_SUPPORT
222223
select ZONE_DMA32 if 64BIT
223224

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2025 Xi Ruoyao <[email protected]>. All Rights Reserved.
4+
*/
5+
#ifndef __ASM_VDSO_GETRANDOM_H
6+
#define __ASM_VDSO_GETRANDOM_H
7+
8+
#ifndef __ASSEMBLY__
9+
10+
#include <asm/unistd.h>
11+
12+
static __always_inline ssize_t getrandom_syscall(void *_buffer, size_t _len, unsigned int _flags)
13+
{
14+
register long ret asm("a0");
15+
register long nr asm("a7") = __NR_getrandom;
16+
register void *buffer asm("a0") = _buffer;
17+
register size_t len asm("a1") = _len;
18+
register unsigned int flags asm("a2") = _flags;
19+
20+
asm volatile ("ecall\n"
21+
: "+r" (ret)
22+
: "r" (nr), "r" (buffer), "r" (len), "r" (flags)
23+
: "memory");
24+
25+
return ret;
26+
}
27+
28+
#endif /* !__ASSEMBLY__ */
29+
30+
#endif /* __ASM_VDSO_GETRANDOM_H */

arch/riscv/kernel/vdso/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ vdso-syms += flush_icache
1313
vdso-syms += hwprobe
1414
vdso-syms += sys_hwprobe
1515

16+
ifdef CONFIG_VDSO_GETRANDOM
17+
vdso-syms += getrandom
18+
endif
19+
1620
# Files to link into the vdso
1721
obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
1822

23+
ifdef CONFIG_VDSO_GETRANDOM
24+
obj-vdso += vgetrandom-chacha.o
25+
endif
26+
1927
ccflags-y := -fno-stack-protector
2028
ccflags-y += -DDISABLE_BRANCH_PROFILING
2129
ccflags-y += -fno-builtin
@@ -24,6 +32,10 @@ ifneq ($(c-gettimeofday-y),)
2432
CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)
2533
endif
2634

35+
ifneq ($(c-getrandom-y),)
36+
CFLAGS_getrandom.o += -fPIC -include $(c-getrandom-y)
37+
endif
38+
2739
CFLAGS_hwprobe.o += -fPIC
2840

2941
# Build rules

arch/riscv/kernel/vdso/getrandom.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2025 Xi Ruoyao <[email protected]>. All Rights Reserved.
4+
*/
5+
#include <linux/types.h>
6+
7+
ssize_t __vdso_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
8+
{
9+
return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
10+
}

arch/riscv/kernel/vdso/vdso.lds.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ VERSION
8080
#ifndef COMPAT_VDSO
8181
__vdso_riscv_hwprobe;
8282
#endif
83+
__vdso_getrandom;
8384
local: *;
8485
};
8586
}
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2025 Xi Ruoyao <[email protected]>. All Rights Reserved.
4+
*
5+
* Based on arch/loongarch/vdso/vgetrandom-chacha.S.
6+
*/
7+
8+
#include <asm/asm.h>
9+
#include <linux/linkage.h>
10+
11+
.text
12+
13+
.macro ROTRI rd rs imm
14+
slliw t0, \rs, 32 - \imm
15+
srliw \rd, \rs, \imm
16+
or \rd, \rd, t0
17+
.endm
18+
19+
.macro OP_4REG op d0 d1 d2 d3 s0 s1 s2 s3
20+
\op \d0, \d0, \s0
21+
\op \d1, \d1, \s1
22+
\op \d2, \d2, \s2
23+
\op \d3, \d3, \s3
24+
.endm
25+
26+
/*
27+
* a0: output bytes
28+
* a1: 32-byte key input
29+
* a2: 8-byte counter input/output
30+
* a3: number of 64-byte blocks to write to output
31+
*/
32+
SYM_FUNC_START(__arch_chacha20_blocks_nostack)
33+
34+
#define output a0
35+
#define key a1
36+
#define counter a2
37+
#define nblocks a3
38+
#define i a4
39+
#define state0 s0
40+
#define state1 s1
41+
#define state2 s2
42+
#define state3 s3
43+
#define state4 s4
44+
#define state5 s5
45+
#define state6 s6
46+
#define state7 s7
47+
#define state8 s8
48+
#define state9 s9
49+
#define state10 s10
50+
#define state11 s11
51+
#define state12 a5
52+
#define state13 a6
53+
#define state14 a7
54+
#define state15 t1
55+
#define cnt t2
56+
#define copy0 t3
57+
#define copy1 t4
58+
#define copy2 t5
59+
#define copy3 t6
60+
61+
/* Packs to be used with OP_4REG */
62+
#define line0 state0, state1, state2, state3
63+
#define line1 state4, state5, state6, state7
64+
#define line2 state8, state9, state10, state11
65+
#define line3 state12, state13, state14, state15
66+
67+
#define line1_perm state5, state6, state7, state4
68+
#define line2_perm state10, state11, state8, state9
69+
#define line3_perm state15, state12, state13, state14
70+
71+
#define copy copy0, copy1, copy2, copy3
72+
73+
#define _16 16, 16, 16, 16
74+
#define _20 20, 20, 20, 20
75+
#define _24 24, 24, 24, 24
76+
#define _25 25, 25, 25, 25
77+
78+
addi sp, sp, -12*SZREG
79+
REG_S s0, (sp)
80+
REG_S s1, SZREG(sp)
81+
REG_S s2, 2*SZREG(sp)
82+
REG_S s3, 3*SZREG(sp)
83+
REG_S s4, 4*SZREG(sp)
84+
REG_S s5, 5*SZREG(sp)
85+
REG_S s6, 6*SZREG(sp)
86+
REG_S s7, 7*SZREG(sp)
87+
REG_S s8, 8*SZREG(sp)
88+
REG_S s9, 9*SZREG(sp)
89+
REG_S s10, 10*SZREG(sp)
90+
REG_S s11, 11*SZREG(sp)
91+
92+
ld cnt, (counter)
93+
94+
li copy0, 0x61707865
95+
li copy1, 0x3320646e
96+
li copy2, 0x79622d32
97+
li copy3, 0x6b206574
98+
99+
.Lblock:
100+
/* state[0,1,2,3] = "expand 32-byte k" */
101+
mv state0, copy0
102+
mv state1, copy1
103+
mv state2, copy2
104+
mv state3, copy3
105+
106+
/* state[4,5,..,11] = key */
107+
lw state4, (key)
108+
lw state5, 4(key)
109+
lw state6, 8(key)
110+
lw state7, 12(key)
111+
lw state8, 16(key)
112+
lw state9, 20(key)
113+
lw state10, 24(key)
114+
lw state11, 28(key)
115+
116+
/* state[12,13] = counter */
117+
mv state12, cnt
118+
srli state13, cnt, 32
119+
120+
/* state[14,15] = 0 */
121+
mv state14, zero
122+
mv state15, zero
123+
124+
li i, 10
125+
.Lpermute:
126+
/* odd round */
127+
OP_4REG addw line0, line1
128+
OP_4REG xor line3, line0
129+
OP_4REG ROTRI line3, _16
130+
131+
OP_4REG addw line2, line3
132+
OP_4REG xor line1, line2
133+
OP_4REG ROTRI line1, _20
134+
135+
OP_4REG addw line0, line1
136+
OP_4REG xor line3, line0
137+
OP_4REG ROTRI line3, _24
138+
139+
OP_4REG addw line2, line3
140+
OP_4REG xor line1, line2
141+
OP_4REG ROTRI line1, _25
142+
143+
/* even round */
144+
OP_4REG addw line0, line1_perm
145+
OP_4REG xor line3_perm, line0
146+
OP_4REG ROTRI line3_perm, _16
147+
148+
OP_4REG addw line2_perm, line3_perm
149+
OP_4REG xor line1_perm, line2_perm
150+
OP_4REG ROTRI line1_perm, _20
151+
152+
OP_4REG addw line0, line1_perm
153+
OP_4REG xor line3_perm, line0
154+
OP_4REG ROTRI line3_perm, _24
155+
156+
OP_4REG addw line2_perm, line3_perm
157+
OP_4REG xor line1_perm, line2_perm
158+
OP_4REG ROTRI line1_perm, _25
159+
160+
addi i, i, -1
161+
bnez i, .Lpermute
162+
163+
/* output[0,1,2,3] = copy[0,1,2,3] + state[0,1,2,3] */
164+
OP_4REG addw line0, copy
165+
sw state0, (output)
166+
sw state1, 4(output)
167+
sw state2, 8(output)
168+
sw state3, 12(output)
169+
170+
/* from now on state[0,1,2,3] are scratch registers */
171+
172+
/* state[0,1,2,3] = lo(key) */
173+
lw state0, (key)
174+
lw state1, 4(key)
175+
lw state2, 8(key)
176+
lw state3, 12(key)
177+
178+
/* output[4,5,6,7] = state[0,1,2,3] + state[4,5,6,7] */
179+
OP_4REG addw line1, line0
180+
sw state4, 16(output)
181+
sw state5, 20(output)
182+
sw state6, 24(output)
183+
sw state7, 28(output)
184+
185+
/* state[0,1,2,3] = hi(key) */
186+
lw state0, 16(key)
187+
lw state1, 20(key)
188+
lw state2, 24(key)
189+
lw state3, 28(key)
190+
191+
/* output[8,9,10,11] = tmp[0,1,2,3] + state[8,9,10,11] */
192+
OP_4REG addw line2, line0
193+
sw state8, 32(output)
194+
sw state9, 36(output)
195+
sw state10, 40(output)
196+
sw state11, 44(output)
197+
198+
/* output[12,13,14,15] = state[12,13,14,15] + [cnt_lo, cnt_hi, 0, 0] */
199+
addw state12, state12, cnt
200+
srli state0, cnt, 32
201+
addw state13, state13, state0
202+
sw state12, 48(output)
203+
sw state13, 52(output)
204+
sw state14, 56(output)
205+
sw state15, 60(output)
206+
207+
/* ++counter */
208+
addi cnt, cnt, 1
209+
210+
/* output += 64 */
211+
addi output, output, 64
212+
/* --nblocks */
213+
addi nblocks, nblocks, -1
214+
bnez nblocks, .Lblock
215+
216+
/* counter = [cnt_lo, cnt_hi] */
217+
sd cnt, (counter)
218+
219+
/* Zero out the potentially sensitive regs, in case nothing uses these
220+
* again. As at now copy[0,1,2,3] just contains "expand 32-byte k" and
221+
* state[0,...,11] are s0-s11 those we'll restore in the epilogue, we
222+
* only need to zero state[12,...,15].
223+
*/
224+
mv state12, zero
225+
mv state13, zero
226+
mv state14, zero
227+
mv state15, zero
228+
229+
REG_L s0, (sp)
230+
REG_L s1, SZREG(sp)
231+
REG_L s2, 2*SZREG(sp)
232+
REG_L s3, 3*SZREG(sp)
233+
REG_L s4, 4*SZREG(sp)
234+
REG_L s5, 5*SZREG(sp)
235+
REG_L s6, 6*SZREG(sp)
236+
REG_L s7, 7*SZREG(sp)
237+
REG_L s8, 8*SZREG(sp)
238+
REG_L s9, 9*SZREG(sp)
239+
REG_L s10, 10*SZREG(sp)
240+
REG_L s11, 11*SZREG(sp)
241+
addi sp, sp, 12*SZREG
242+
243+
ret
244+
SYM_FUNC_END(__arch_chacha20_blocks_nostack)

tools/testing/selftests/vDSO/vgetrandom-chacha.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "../../../../arch/loongarch/vdso/vgetrandom-chacha.S"
1212
#elif defined(__powerpc__) || defined(__powerpc64__)
1313
#include "../../../../arch/powerpc/kernel/vdso/vgetrandom-chacha.S"
14+
#elif defined(__riscv) && __riscv_xlen == 64
15+
#include "../../../../arch/riscv/kernel/vdso/vgetrandom-chacha.S"
1416
#elif defined(__s390x__)
1517
#include "../../../../arch/s390/kernel/vdso64/vgetrandom-chacha.S"
1618
#elif defined(__x86_64__)

0 commit comments

Comments
 (0)