Skip to content

Commit 48d6710

Browse files
committed
RISC-V: KVM: Implement ONE_REG interface for SBI FWFT state
The KVM user-space needs a way to save/restore the state of SBI FWFT features so implement SBI extension ONE_REG callbacks. Signed-off-by: Anup Patel <apatel@ventanamicro.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Link: https://lore.kernel.org/r/20250823155947.1354229-6-apatel@ventanamicro.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 85e7850 commit 48d6710

3 files changed

Lines changed: 200 additions & 13 deletions

File tree

arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct kvm_sbi_fwft_feature;
1616
struct kvm_sbi_fwft_config {
1717
const struct kvm_sbi_fwft_feature *feature;
1818
bool supported;
19+
bool enabled;
1920
unsigned long flags;
2021
};
2122

arch/riscv/include/uapi/asm/kvm.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ struct kvm_riscv_sbi_sta {
220220
unsigned long shmem_hi;
221221
};
222222

223+
struct kvm_riscv_sbi_fwft_feature {
224+
unsigned long enable;
225+
unsigned long flags;
226+
unsigned long value;
227+
};
228+
229+
/* SBI FWFT extension registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
230+
struct kvm_riscv_sbi_fwft {
231+
struct kvm_riscv_sbi_fwft_feature misaligned_deleg;
232+
struct kvm_riscv_sbi_fwft_feature pointer_masking;
233+
};
234+
223235
/* Possible states for kvm_riscv_timer */
224236
#define KVM_RISCV_TIMER_STATE_OFF 0
225237
#define KVM_RISCV_TIMER_STATE_ON 1
@@ -303,6 +315,9 @@ struct kvm_riscv_sbi_sta {
303315
#define KVM_REG_RISCV_SBI_STA (0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT)
304316
#define KVM_REG_RISCV_SBI_STA_REG(name) \
305317
(offsetof(struct kvm_riscv_sbi_sta, name) / sizeof(unsigned long))
318+
#define KVM_REG_RISCV_SBI_FWFT (0x1 << KVM_REG_RISCV_SUBTYPE_SHIFT)
319+
#define KVM_REG_RISCV_SBI_FWFT_REG(name) \
320+
(offsetof(struct kvm_riscv_sbi_fwft, name) / sizeof(unsigned long))
306321

307322
/* Device Control API: RISC-V AIA */
308323
#define KVM_DEV_RISCV_APLIC_ALIGN 0x1000

arch/riscv/kvm/vcpu_sbi_fwft.c

Lines changed: 184 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ struct kvm_sbi_fwft_feature {
2222
*/
2323
enum sbi_fwft_feature_t id;
2424

25+
/**
26+
* @first_reg_num: ONE_REG index of the first ONE_REG register
27+
*/
28+
unsigned long first_reg_num;
29+
2530
/**
2631
* @supported: Check if the feature is supported on the vcpu
2732
*
@@ -44,7 +49,8 @@ struct kvm_sbi_fwft_feature {
4449
*
4550
* This callback is mandatory
4651
*/
47-
long (*set)(struct kvm_vcpu *vcpu, struct kvm_sbi_fwft_config *conf, unsigned long value);
52+
long (*set)(struct kvm_vcpu *vcpu, struct kvm_sbi_fwft_config *conf,
53+
bool one_reg_access, unsigned long value);
4854

4955
/**
5056
* @get: Get the feature current value
@@ -53,7 +59,8 @@ struct kvm_sbi_fwft_feature {
5359
*
5460
* This callback is mandatory
5561
*/
56-
long (*get)(struct kvm_vcpu *vcpu, struct kvm_sbi_fwft_config *conf, unsigned long *value);
62+
long (*get)(struct kvm_vcpu *vcpu, struct kvm_sbi_fwft_config *conf,
63+
bool one_reg_access, unsigned long *value);
5764
};
5865

5966
static const enum sbi_fwft_feature_t kvm_fwft_defined_features[] = {
@@ -91,16 +98,18 @@ static void kvm_sbi_fwft_reset_misaligned_delegation(struct kvm_vcpu *vcpu)
9198

9299
static long kvm_sbi_fwft_set_misaligned_delegation(struct kvm_vcpu *vcpu,
93100
struct kvm_sbi_fwft_config *conf,
94-
unsigned long value)
101+
bool one_reg_access, unsigned long value)
95102
{
96103
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
97104

98105
if (value == 1) {
99106
cfg->hedeleg |= MIS_DELEG;
100-
csr_set(CSR_HEDELEG, MIS_DELEG);
107+
if (!one_reg_access)
108+
csr_set(CSR_HEDELEG, MIS_DELEG);
101109
} else if (value == 0) {
102110
cfg->hedeleg &= ~MIS_DELEG;
103-
csr_clear(CSR_HEDELEG, MIS_DELEG);
111+
if (!one_reg_access)
112+
csr_clear(CSR_HEDELEG, MIS_DELEG);
104113
} else {
105114
return SBI_ERR_INVALID_PARAM;
106115
}
@@ -110,10 +119,11 @@ static long kvm_sbi_fwft_set_misaligned_delegation(struct kvm_vcpu *vcpu,
110119

111120
static long kvm_sbi_fwft_get_misaligned_delegation(struct kvm_vcpu *vcpu,
112121
struct kvm_sbi_fwft_config *conf,
113-
unsigned long *value)
122+
bool one_reg_access, unsigned long *value)
114123
{
115-
*value = (csr_read(CSR_HEDELEG) & MIS_DELEG) == MIS_DELEG;
124+
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
116125

126+
*value = (cfg->hedeleg & MIS_DELEG) == MIS_DELEG;
117127
return SBI_SUCCESS;
118128
}
119129

@@ -145,7 +155,7 @@ static void kvm_sbi_fwft_reset_pointer_masking_pmlen(struct kvm_vcpu *vcpu)
145155

146156
static long kvm_sbi_fwft_set_pointer_masking_pmlen(struct kvm_vcpu *vcpu,
147157
struct kvm_sbi_fwft_config *conf,
148-
unsigned long value)
158+
bool one_reg_access, unsigned long value)
149159
{
150160
struct kvm_sbi_fwft *fwft = vcpu_to_fwft(vcpu);
151161
unsigned long pmm;
@@ -176,14 +186,15 @@ static long kvm_sbi_fwft_set_pointer_masking_pmlen(struct kvm_vcpu *vcpu,
176186
* update here so that VCPU see's pointer masking mode change
177187
* immediately.
178188
*/
179-
csr_write(CSR_HENVCFG, vcpu->arch.cfg.henvcfg);
189+
if (!one_reg_access)
190+
csr_write(CSR_HENVCFG, vcpu->arch.cfg.henvcfg);
180191

181192
return SBI_SUCCESS;
182193
}
183194

184195
static long kvm_sbi_fwft_get_pointer_masking_pmlen(struct kvm_vcpu *vcpu,
185196
struct kvm_sbi_fwft_config *conf,
186-
unsigned long *value)
197+
bool one_reg_access, unsigned long *value)
187198
{
188199
switch (vcpu->arch.cfg.henvcfg & ENVCFG_PMM) {
189200
case ENVCFG_PMM_PMLEN_0:
@@ -207,6 +218,8 @@ static long kvm_sbi_fwft_get_pointer_masking_pmlen(struct kvm_vcpu *vcpu,
207218
static const struct kvm_sbi_fwft_feature features[] = {
208219
{
209220
.id = SBI_FWFT_MISALIGNED_EXC_DELEG,
221+
.first_reg_num = offsetof(struct kvm_riscv_sbi_fwft, misaligned_deleg.enable) /
222+
sizeof(unsigned long),
210223
.supported = kvm_sbi_fwft_misaligned_delegation_supported,
211224
.reset = kvm_sbi_fwft_reset_misaligned_delegation,
212225
.set = kvm_sbi_fwft_set_misaligned_delegation,
@@ -215,6 +228,8 @@ static const struct kvm_sbi_fwft_feature features[] = {
215228
#ifndef CONFIG_32BIT
216229
{
217230
.id = SBI_FWFT_POINTER_MASKING_PMLEN,
231+
.first_reg_num = offsetof(struct kvm_riscv_sbi_fwft, pointer_masking.enable) /
232+
sizeof(unsigned long),
218233
.supported = kvm_sbi_fwft_pointer_masking_pmlen_supported,
219234
.reset = kvm_sbi_fwft_reset_pointer_masking_pmlen,
220235
.set = kvm_sbi_fwft_set_pointer_masking_pmlen,
@@ -223,6 +238,20 @@ static const struct kvm_sbi_fwft_feature features[] = {
223238
#endif
224239
};
225240

241+
static const struct kvm_sbi_fwft_feature *kvm_sbi_fwft_regnum_to_feature(unsigned long reg_num)
242+
{
243+
const struct kvm_sbi_fwft_feature *feature;
244+
int i;
245+
246+
for (i = 0; i < ARRAY_SIZE(features); i++) {
247+
feature = &features[i];
248+
if (feature->first_reg_num <= reg_num && reg_num < (feature->first_reg_num + 3))
249+
return feature;
250+
}
251+
252+
return NULL;
253+
}
254+
226255
static struct kvm_sbi_fwft_config *
227256
kvm_sbi_fwft_get_config(struct kvm_vcpu *vcpu, enum sbi_fwft_feature_t feature)
228257
{
@@ -250,7 +279,7 @@ static int kvm_fwft_get_feature(struct kvm_vcpu *vcpu, u32 feature,
250279
return SBI_ERR_DENIED;
251280
}
252281

253-
if (!tconf->supported)
282+
if (!tconf->supported || !tconf->enabled)
254283
return SBI_ERR_NOT_SUPPORTED;
255284

256285
*conf = tconf;
@@ -276,7 +305,7 @@ static int kvm_sbi_fwft_set(struct kvm_vcpu *vcpu, u32 feature,
276305

277306
conf->flags = flags;
278307

279-
return conf->feature->set(vcpu, conf, value);
308+
return conf->feature->set(vcpu, conf, false, value);
280309
}
281310

282311
static int kvm_sbi_fwft_get(struct kvm_vcpu *vcpu, unsigned long feature,
@@ -289,7 +318,7 @@ static int kvm_sbi_fwft_get(struct kvm_vcpu *vcpu, unsigned long feature,
289318
if (ret)
290319
return ret;
291320

292-
return conf->feature->get(vcpu, conf, value);
321+
return conf->feature->get(vcpu, conf, false, value);
293322
}
294323

295324
static int kvm_sbi_ext_fwft_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
@@ -336,6 +365,7 @@ static int kvm_sbi_ext_fwft_init(struct kvm_vcpu *vcpu)
336365
else
337366
conf->supported = true;
338367

368+
conf->enabled = conf->supported;
339369
conf->feature = feature;
340370
}
341371

@@ -363,11 +393,152 @@ static void kvm_sbi_ext_fwft_reset(struct kvm_vcpu *vcpu)
363393
}
364394
}
365395

396+
static unsigned long kvm_sbi_ext_fwft_get_reg_count(struct kvm_vcpu *vcpu)
397+
{
398+
unsigned long max_reg_count = sizeof(struct kvm_riscv_sbi_fwft) / sizeof(unsigned long);
399+
const struct kvm_sbi_fwft_feature *feature;
400+
struct kvm_sbi_fwft_config *conf;
401+
unsigned long reg, ret = 0;
402+
403+
for (reg = 0; reg < max_reg_count; reg++) {
404+
feature = kvm_sbi_fwft_regnum_to_feature(reg);
405+
if (!feature)
406+
continue;
407+
408+
conf = kvm_sbi_fwft_get_config(vcpu, feature->id);
409+
if (!conf || !conf->supported)
410+
continue;
411+
412+
ret++;
413+
}
414+
415+
return ret;
416+
}
417+
418+
static int kvm_sbi_ext_fwft_get_reg_id(struct kvm_vcpu *vcpu, int index, u64 *reg_id)
419+
{
420+
int reg, max_reg_count = sizeof(struct kvm_riscv_sbi_fwft) / sizeof(unsigned long);
421+
const struct kvm_sbi_fwft_feature *feature;
422+
struct kvm_sbi_fwft_config *conf;
423+
int idx = 0;
424+
425+
for (reg = 0; reg < max_reg_count; reg++) {
426+
feature = kvm_sbi_fwft_regnum_to_feature(reg);
427+
if (!feature)
428+
continue;
429+
430+
conf = kvm_sbi_fwft_get_config(vcpu, feature->id);
431+
if (!conf || !conf->supported)
432+
continue;
433+
434+
if (index == idx) {
435+
*reg_id = KVM_REG_RISCV |
436+
(IS_ENABLED(CONFIG_32BIT) ?
437+
KVM_REG_SIZE_U32 : KVM_REG_SIZE_U64) |
438+
KVM_REG_RISCV_SBI_STATE |
439+
KVM_REG_RISCV_SBI_FWFT | reg;
440+
return 0;
441+
}
442+
443+
idx++;
444+
}
445+
446+
return -ENOENT;
447+
}
448+
449+
static int kvm_sbi_ext_fwft_get_reg(struct kvm_vcpu *vcpu, unsigned long reg_num,
450+
unsigned long reg_size, void *reg_val)
451+
{
452+
const struct kvm_sbi_fwft_feature *feature;
453+
struct kvm_sbi_fwft_config *conf;
454+
unsigned long *value;
455+
int ret = 0;
456+
457+
if (reg_size != sizeof(unsigned long))
458+
return -EINVAL;
459+
value = reg_val;
460+
461+
feature = kvm_sbi_fwft_regnum_to_feature(reg_num);
462+
if (!feature)
463+
return -ENOENT;
464+
465+
conf = kvm_sbi_fwft_get_config(vcpu, feature->id);
466+
if (!conf || !conf->supported)
467+
return -ENOENT;
468+
469+
switch (reg_num - feature->first_reg_num) {
470+
case 0:
471+
*value = conf->enabled;
472+
break;
473+
case 1:
474+
*value = conf->flags;
475+
break;
476+
case 2:
477+
ret = conf->feature->get(vcpu, conf, true, value);
478+
break;
479+
default:
480+
return -ENOENT;
481+
}
482+
483+
return sbi_err_map_linux_errno(ret);
484+
}
485+
486+
static int kvm_sbi_ext_fwft_set_reg(struct kvm_vcpu *vcpu, unsigned long reg_num,
487+
unsigned long reg_size, const void *reg_val)
488+
{
489+
const struct kvm_sbi_fwft_feature *feature;
490+
struct kvm_sbi_fwft_config *conf;
491+
unsigned long value;
492+
int ret = 0;
493+
494+
if (reg_size != sizeof(unsigned long))
495+
return -EINVAL;
496+
value = *(const unsigned long *)reg_val;
497+
498+
feature = kvm_sbi_fwft_regnum_to_feature(reg_num);
499+
if (!feature)
500+
return -ENOENT;
501+
502+
conf = kvm_sbi_fwft_get_config(vcpu, feature->id);
503+
if (!conf || !conf->supported)
504+
return -ENOENT;
505+
506+
switch (reg_num - feature->first_reg_num) {
507+
case 0:
508+
switch (value) {
509+
case 0:
510+
conf->enabled = false;
511+
break;
512+
case 1:
513+
conf->enabled = true;
514+
break;
515+
default:
516+
return -EINVAL;
517+
}
518+
break;
519+
case 1:
520+
conf->flags = value & SBI_FWFT_SET_FLAG_LOCK;
521+
break;
522+
case 2:
523+
ret = conf->feature->set(vcpu, conf, true, value);
524+
break;
525+
default:
526+
return -ENOENT;
527+
}
528+
529+
return sbi_err_map_linux_errno(ret);
530+
}
531+
366532
const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_fwft = {
367533
.extid_start = SBI_EXT_FWFT,
368534
.extid_end = SBI_EXT_FWFT,
369535
.handler = kvm_sbi_ext_fwft_handler,
370536
.init = kvm_sbi_ext_fwft_init,
371537
.deinit = kvm_sbi_ext_fwft_deinit,
372538
.reset = kvm_sbi_ext_fwft_reset,
539+
.state_reg_subtype = KVM_REG_RISCV_SBI_FWFT,
540+
.get_state_reg_count = kvm_sbi_ext_fwft_get_reg_count,
541+
.get_state_reg_id = kvm_sbi_ext_fwft_get_reg_id,
542+
.get_state_reg = kvm_sbi_ext_fwft_get_reg,
543+
.set_state_reg = kvm_sbi_ext_fwft_set_reg,
373544
};

0 commit comments

Comments
 (0)