Skip to content

Commit b98666f

Browse files
nadime15charmitro
andcommitted
Add support for Zvkg extension
Add following instructions: vghsh.vv vgmul.vv Co-authored-by: Charalampos Mitrodimas <[email protected]>
1 parent 0806ed5 commit b98666f

7 files changed

+127
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Supported RISC-V ISA features
119119
- Zvbb extension for vector basic bit-manipulation, v1.0
120120
- Zvbc extension for vector carryless multiplication, v1.0
121121
- Zvkb extension for vector cryptography bit-manipulation, v1.0
122+
- Zvkg extension for vector GCM/GMAC, v1.0
122123
- Zvknha and Zvknhb extensions for vector cryptography NIST Suite: Vector SHA-2 Secure Hash, v1.0
123124
- Machine, Supervisor, and User modes
124125
- Smcntrpmf extension for cycle and instret privilege mode filtering, v1.0

config/default.json

+3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@
174174
"Zvbc" : {
175175
"supported" : true
176176
},
177+
"Zvkg" : {
178+
"supported" : true
179+
},
177180
"Zvknha" : {
178181
"supported" : true
179182
},

model/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ foreach (xlen IN ITEMS 32 64)
8888
"riscv_insts_zicboz.sail"
8989
"riscv_insts_zvbb.sail"
9090
"riscv_insts_zvbc.sail"
91+
"riscv_insts_zvkg.sail"
9192
"riscv_insts_zvknhab.sail"
9293
# Zimop and Zcmop should be at the end so they can be overridden by earlier extensions
9394
"riscv_insts_zimop.sail"

model/arithmetic.sail

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
/* SPDX-License-Identifier: BSD-2-Clause */
77
/*=======================================================================================*/
88

9+
/* Reverse bits in each byte */
10+
val brev8 : forall 'm, 'm >= 0 & mod('m, 8) == 0. (bits('m)) -> bits('m)
11+
function brev8(input) = {
12+
var output : bits('m) = zeros();
13+
foreach (i from 0 to ('m - 8) by 8)
14+
output[i+7..i] = reverse_bits(input[i+7..i]);
15+
output
16+
}
17+
918
/* Carry-less multiply. */
1019
val carryless_mul : forall 'n, 'n > 0. (bits('n), bits('n)) -> bits(2 * 'n)
1120
function carryless_mul(a, b) = {

model/riscv_extensions.sail

+3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ function clause hartSupports(Ext_Zvkb) = config extensions.Zvkb.supported
179179
// Vector Carryless Multiplication
180180
enum clause extension = Ext_Zvbc
181181
function clause hartSupports(Ext_Zvbc) = config extensions.Zvbc.supported
182+
// Vector GCM/GMAC
183+
enum clause extension = Ext_Zvkg
184+
function clause hartSupports(Ext_Zvkg) = config extensions.Zvkg.supported
182185
// NIST Suite: Vector SHA-2 Secure Hash
183186
enum clause extension = Ext_Zvknha
184187
function clause hartSupports(Ext_Zvknha) = config extensions.Zvknha.supported

model/riscv_insts_zvkg.sail

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*=======================================================================================*/
2+
/* This Sail RISC-V architecture model, comprising all files and */
3+
/* directories except where otherwise noted is subject the BSD */
4+
/* two-clause license in the LICENSE file. */
5+
/* */
6+
/* SPDX-License-Identifier: BSD-2-Clause */
7+
/*=======================================================================================*/
8+
9+
function clause currentlyEnabled(Ext_Zvkg) = hartSupports(Ext_Zvkg) & currentlyEnabled(Ext_V)
10+
11+
union clause ast = VGHSH_VV : (vregidx, vregidx, vregidx)
12+
13+
mapping clause encdec = VGHSH_VV(vs2, vs1, vd)
14+
<-> 0b1011001 @ encdec_vreg(vs2) @ encdec_vreg(vs1) @ 0b010 @ encdec_vreg(vd) @ 0b1110111
15+
when currentlyEnabled(Ext_Zvkg) & get_sew() == 32 & zvk_check_encdec(128, 4)
16+
17+
mapping clause assembly = VGHSH_VV(vs2, vs1, vd)
18+
<-> "vghsh.vv" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2) ^ sep() ^ vreg_name(vs1)
19+
20+
function clause execute (VGHSH_VV(vs2, vs1, vd)) = {
21+
let SEW = get_sew();
22+
let LMUL_pow = get_lmul_pow();
23+
let num_elem = get_num_elem(LMUL_pow, SEW);
24+
25+
assert(SEW == 32);
26+
27+
let vs2_val = read_vreg(num_elem, SEW, LMUL_pow, vs2);
28+
let vs1_val = read_vreg(num_elem, SEW, LMUL_pow, vs1);
29+
let vd_val = read_vreg(num_elem, SEW, LMUL_pow, vd);
30+
31+
let eg_len = (unsigned(vl) / 4);
32+
let eg_start = (unsigned(vstart) / 4);
33+
34+
foreach (i from eg_start to (eg_len - 1)) {
35+
assert(i * 4 + 3 < num_elem);
36+
37+
let Y : bits(128) = get_velem_quad(vd_val, i);
38+
let X : bits(128) = get_velem_quad(vs1_val, i);
39+
var H : bits(128) = brev8(get_velem_quad(vs2_val, i));
40+
41+
var Z : bits(128) = zeros();
42+
let S : bits(128) = brev8(Y ^ X);
43+
44+
foreach (b from 0 to 127) {
45+
if S[b] == bitone then Z = Z ^ H;
46+
47+
let reduce = H[127] == bitone;
48+
H = H << 1;
49+
if reduce then H = H[127..8] @ (H[7..0] ^ 0x87);
50+
};
51+
52+
write_velem_quad(vd, SEW, brev8(Z), i);
53+
};
54+
55+
set_vstart(zeros());
56+
RETIRE_SUCCESS
57+
}
58+
59+
union clause ast = VGMUL_VV : (vregidx, vregidx)
60+
61+
mapping clause encdec = VGMUL_VV(vs2, vd)
62+
<-> 0b1010001 @ encdec_vreg(vs2) @ 0b10001 @ 0b010 @ encdec_vreg(vd) @ 0b1110111
63+
when currentlyEnabled(Ext_Zvkg) & get_sew() == 32 & zvk_check_encdec(128, 4)
64+
65+
mapping clause assembly = VGMUL_VV(vs2, vd)
66+
<-> "vgmul.vv" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2)
67+
68+
function clause execute (VGMUL_VV(vs2, vd)) = {
69+
let SEW = get_sew();
70+
let LMUL_pow = get_lmul_pow();
71+
let num_elem = get_num_elem(LMUL_pow, SEW);
72+
73+
assert(SEW == 32);
74+
75+
let vs2_val = read_vreg(num_elem, SEW, LMUL_pow, vs2);
76+
let vd_val = read_vreg(num_elem, SEW, LMUL_pow, vd);
77+
78+
let eg_len = (unsigned(vl) / 4);
79+
let eg_start = (unsigned(vstart) / 4);
80+
81+
foreach (i from eg_start to (eg_len - 1)) {
82+
assert(i * 4 + 3 < num_elem);
83+
84+
let Y : bits(128) = brev8(get_velem_quad(vd_val, i));
85+
var H : bits(128) = brev8(get_velem_quad(vs2_val, i));
86+
var Z : bits(128) = zeros();
87+
88+
foreach (b from 0 to 127) {
89+
if Y[b] == bitone then Z = Z ^ H;
90+
91+
let reduce = H[127] == bitone;
92+
H = H << 1;
93+
if reduce then H = H[127..8] @ (H[7..0] ^ 0x87);
94+
};
95+
96+
write_velem_quad(vd, SEW, brev8(Z), i);
97+
};
98+
99+
set_vstart(zeros());
100+
RETIRE_SUCCESS
101+
}

model/riscv_types_zvk.sail

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*=======================================================================================*/
2+
/* This Sail RISC-V architecture model, comprising all files and */
3+
/* directories except where otherwise noted is subject the BSD */
4+
/* two-clause license in the LICENSE file. */
5+
/* */
6+
/* SPDX-License-Identifier: BSD-2-Clause */
7+
/*=======================================================================================*/
8+
9+
function zvk_check_encdec(EGW: int, EGS: int) -> bool = (unsigned(vl) % EGS == 0) & (unsigned(vstart) % EGS == 0) & (2 ^ get_lmul_pow() * VLEN) >= EGW

0 commit comments

Comments
 (0)