Skip to contentSkip to conversation

Enable Ssctr Ext using mstateen and add FWFT support for CTR.

PATCH Superseded Rajnesh Kanwal 2 participants 5 messages Feb 5, 2025 – Feb 8, 2025 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Enable Ssctr Ext using mstateen and add FWFT support for CTR.

The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage.

These two patches add support to allow S-mode to access Ssctr. Also this adds support for SBI_FWFT_CONTROL_TRANSFER_RECORD bit in FWFT ext.

In case of M-mode firmware (Opensbi etc) we keep Ssctr always enabled and return SBI_ERR_DENIED on sbi_fwft_set() call to notify that Ssctr is always enabled by default.

A hypervisor on the other hand can keep Ssctr disabled by default to avoid saving-restoring CTR state on vcpu switch. A guest vcpu has to request SBI implementation to enable CTR extension when kernel uses it.

I have proposed SBI_FWFT_CONTROL_TRANSFER_RECORD bit in FWFT extension in tech-prs group [0]. I am deliberately sending this series a bit early to gather feedback on the patches.

I have created a wiki to run CTR demo on kvm host and guest to demonstrate lazy save/restore use-case working with FWFT bit. Wiki can be found here [1].

[0]: https://lists.riscv.org/g/tech-prs/message/1333
[1]: https://github.com/rajnesh-kanwal/linux/wiki/Running-CTR-basic-demo-on-KVM-QEMU-RISCV-Virt-machine

---
Changelog:
v1: Incorporated feedback from Anup and Atish.
- Added FWFT bit to enable/disable Ssctr Ext to allow lazy context
save restore.
- https://patchwork.ozlabs.org/project/opensbi/patch/20240529150210.125200-1-rkanwal at rivosinc.com/

---
Rajnesh Kanwal (2):
lib: sbi: Enable Control Transfer Records (CTR) Ext using xstateen.
lib: sbi: fwft: Add FWFT support for Control transfer records ext

 include/sbi/riscv_encoding.h      | 13 +++++++++++++
 include/sbi/sbi_ecall_interface.h |  4 +++-
 include/sbi/sbi_hart.h            |  4 ++++
 lib/sbi/sbi_fwft.c                | 30 ++++++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c                | 19 +++++++++++++++++++
 5 files changed, 69 insertions(+), 1 deletion(-)

---
base-commit: a2807646a85878cb631cbc2b012cf888fc4427d7
change-id: 20250205-ctr_upstream_v2-7ef621789677

Best regards,

Signature · 2 lines
Best regards
Rajnesh Kanwal
#1 lore raw

PATCH v2 1/2 lib: sbi: Enable Control Transfer Records (CTR) Ext using xstateen.

The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage.

This extension is similar to Arch LBR in x86 and BRBE in ARM. The Extension has been stable and the latest release can be found here
https://github.com/riscv/riscv-control-transfer-records/release

---
 include/sbi/riscv_encoding.h | 13 +++++++++++++
 include/sbi/sbi_hart.h       |  4 ++++
 lib/sbi/sbi_hart.c           | 19 +++++++++++++++++++
 3 files changed, 36 insertions(+)
include/sbi/riscv_encoding.h +13 -0
index 050674a243fc7d341e0bbe73a2ab73ec103f1e1b..1608cf1759b4b38b12fad4cd59f8fd6779ce1ba4 100644
@@ -358,6 +358,17 @@
 #define CSR_SSTATEEN2			0x10E
 #define CSR_SSTATEEN3			0x10F
 
+/* Machine-Level Control transfer records CSRs */
+#define CSR_MCTRCTL                     0x34e
+
+/* Supervisor-Level Control transfer records CSRs */
+#define CSR_SCTRCTL                     0x14e
+#define CSR_SCTRSTATUS                  0x14f
+#define CSR_SCTRDEPTH                   0x15f
+
+/* VS-Level Control transfer records CSRs */
+#define CSR_VSCTRCTL                    0x24e
+
 /* ===== Hypervisor-level CSRs ===== */
 
 /* Hypervisor Trap Setup (H-extension) */
@@ -774,6 +785,8 @@
 #define SMSTATEEN0_CS			(_ULL(1) << SMSTATEEN0_CS_SHIFT)
 #define SMSTATEEN0_FCSR_SHIFT		1
 #define SMSTATEEN0_FCSR			(_ULL(1) << SMSTATEEN0_FCSR_SHIFT)
+#define SMSTATEEN0_CTR_SHIFT		54
+#define SMSTATEEN0_CTR			(_ULL(1) << SMSTATEEN0_CTR_SHIFT)
 #define SMSTATEEN0_CONTEXT_SHIFT	57
 #define SMSTATEEN0_CONTEXT		(_ULL(1) << SMSTATEEN0_CONTEXT_SHIFT)
 #define SMSTATEEN0_IMSIC_SHIFT		58
include/sbi/sbi_hart.h +4 -0
index 81ec061d36a93e35686136940984647e9f1d8d94..ef710d9aa1a591af486d0761cc752f19ff15ee70 100644
@@ -67,6 +67,10 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SVADE,
 	/** Hart has Svadu extension */
 	SBI_HART_EXT_SVADU,
+	/** HART has CTR M-mode CSRs */
+	SBI_HART_EXT_SMCTR,
+	/** HART has CTR S-mode CSRs */
+	SBI_HART_EXT_SSCTR,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +19 -0
index c3667019b9a195ecc9fa902471c1ff830a150458..09792332d369d30a15aa30f73f6dbc044be1b67c 100644
@@ -105,6 +105,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		else
 			mstateen_val &= ~(SMSTATEEN0_SVSLCT);
 
+		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCTR))
+			mstateen_val |= SMSTATEEN0_CTR;
+		else
+			mstateen_val &= ~SMSTATEEN0_CTR;
+
 		csr_write(CSR_MSTATEEN0, mstateen_val);
 #if __riscv_xlen == 32
 		csr_write(CSR_MSTATEEN0H, mstateen_val >> 32);
@@ -680,6 +685,8 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
 	__SBI_HART_EXT_DATA(svade, SBI_HART_EXT_SVADE),
 	__SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
+	__SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
+	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
@@ -933,6 +940,18 @@ __pmp_skip:
 	/* Save trap based detection of Zicntr */
 	has_zicntr = sbi_hart_has_extension(scratch, SBI_HART_EXT_ZICNTR);
 
+	/* Detect if hart has Smctr CSRs */
+	csr_read_allowed(CSR_MCTRCTL, (unsigned long)&trap);
+	if (!trap.cause)
+		__sbi_hart_update_extension(hfeatures, SBI_HART_EXT_SMCTR,
+					    true);
+
+	/* Detect if hart has Ssctr CSRs */
+	csr_read_allowed(CSR_SCTRCTL, (unsigned long)&trap);
+	if (!trap.cause)
+		__sbi_hart_update_extension(hfeatures, SBI_HART_EXT_SSCTR,
+					    true);
+
 	/* Let platform populate extensions */
 	rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(),
 					  hfeatures);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 1/2 lib: sbi: Enable Control Transfer Records (CTR) Ext using xstateen.

Hi Rajnesh,

Quoted 66 lines · Rajnesh Kanwal

The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage.

This extension is similar to Arch LBR in x86 and BRBE in ARM. The Extension has been stable and the latest release can be found here
https://github.com/riscv/riscv-control-transfer-records/release

---
 include/sbi/riscv_encoding.h | 13 +++++++++++++
 include/sbi/sbi_hart.h       |  4 ++++
 lib/sbi/sbi_hart.c           | 19 +++++++++++++++++++
 3 files changed, 36 insertions(+)
include/sbi/riscv_encoding.h +13 -0
index 050674a243fc7d341e0bbe73a2ab73ec103f1e1b..1608cf1759b4b38b12fad4cd59f8fd6779ce1ba4 100644
@@ -358,6 +358,17 @@
 #define CSR_SSTATEEN2			0x10E
 #define CSR_SSTATEEN3			0x10F
 
+/* Machine-Level Control transfer records CSRs */
+#define CSR_MCTRCTL                     0x34e
+
+/* Supervisor-Level Control transfer records CSRs */
+#define CSR_SCTRCTL                     0x14e
+#define CSR_SCTRSTATUS                  0x14f
+#define CSR_SCTRDEPTH                   0x15f
+
+/* VS-Level Control transfer records CSRs */
+#define CSR_VSCTRCTL                    0x24e
+
 /* ===== Hypervisor-level CSRs ===== */
 
 /* Hypervisor Trap Setup (H-extension) */
@@ -774,6 +785,8 @@
 #define SMSTATEEN0_CS			(_ULL(1) << SMSTATEEN0_CS_SHIFT)
 #define SMSTATEEN0_FCSR_SHIFT		1
 #define SMSTATEEN0_FCSR			(_ULL(1) << SMSTATEEN0_FCSR_SHIFT)
+#define SMSTATEEN0_CTR_SHIFT		54
+#define SMSTATEEN0_CTR			(_ULL(1) << SMSTATEEN0_CTR_SHIFT)
 #define SMSTATEEN0_CONTEXT_SHIFT	57
 #define SMSTATEEN0_CONTEXT		(_ULL(1) << SMSTATEEN0_CONTEXT_SHIFT)
 #define SMSTATEEN0_IMSIC_SHIFT		58
include/sbi/sbi_hart.h +4 -0
index 81ec061d36a93e35686136940984647e9f1d8d94..ef710d9aa1a591af486d0761cc752f19ff15ee70 100644
@@ -67,6 +67,10 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SVADE,
 	/** Hart has Svadu extension */
 	SBI_HART_EXT_SVADU,
+	/** HART has CTR M-mode CSRs */
+	SBI_HART_EXT_SMCTR,
+	/** HART has CTR S-mode CSRs */
+	SBI_HART_EXT_SSCTR,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +0 -0
index c3667019b9a195ecc9fa902471c1ff830a150458..09792332d369d30a15aa30f73f6dbc044be1b67c 100644

lib/sbi/sbi_hart.c static void mstatus_init(struct sbi_scratch *scratch) · line 111

Quoted 7 lines
lib/sbi/sbi_hart.c +4 -0
@@ -105,6 +105,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		else
 			mstateen_val &= ~(SMSTATEEN0_SVSLCT);
 
+		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCTR))
+			mstateen_val |= SMSTATEEN0_CTR;
+		else
+			mstateen_val &= ~SMSTATEEN0_CTR;

As I mentioned on the tech-prs thread, since there is a FWFT feature controlling this bit, I would recommend disabling it by default, so the feature has consistent behavior wherever possible.

Regards,
Samuel

Quoted 33 lines
lib/sbi/sbi_hart.c +15 -0
+
 		csr_write(CSR_MSTATEEN0, mstateen_val);
 #if __riscv_xlen == 32
 		csr_write(CSR_MSTATEEN0H, mstateen_val >> 32);
@@ -680,6 +685,8 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
 	__SBI_HART_EXT_DATA(svade, SBI_HART_EXT_SVADE),
 	__SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
+	__SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
+	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
@@ -933,6 +940,18 @@ __pmp_skip:
 	/* Save trap based detection of Zicntr */
 	has_zicntr = sbi_hart_has_extension(scratch, SBI_HART_EXT_ZICNTR);
 
+	/* Detect if hart has Smctr CSRs */
+	csr_read_allowed(CSR_MCTRCTL, (unsigned long)&trap);
+	if (!trap.cause)
+		__sbi_hart_update_extension(hfeatures, SBI_HART_EXT_SMCTR,
+					    true);
+
+	/* Detect if hart has Ssctr CSRs */
+	csr_read_allowed(CSR_SCTRCTL, (unsigned long)&trap);
+	if (!trap.cause)
+		__sbi_hart_update_extension(hfeatures, SBI_HART_EXT_SSCTR,
+					    true);
+
 	/* Let platform populate extensions */
 	rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(),
 					  hfeatures);
#3 lore raw

PATCH v2 2/2 lib: sbi: fwft: Add FWFT support for Control transfer records ext

In reply to Rajnesh Kanwal

Allow supervisor software to request that Ssctr be enabled for S-mode. We keep Ssctr always enabled and return SBI_ERR_DENIED on sbi_fwft_set() call to notify calling software that Ssctr is always enabled by default.

---
 include/sbi/sbi_ecall_interface.h |  4 +++-
 lib/sbi/sbi_fwft.c                | 30 ++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +3 -1
index e9a8167760a9d1e7a06d7142b4e02047b5201319..4db5284ff3d6500666e7c87653b8e4943663815a 100644
@@ -128,7 +128,9 @@ enum sbi_fwft_feature_t {
 	SBI_FWFT_SHADOW_STACK			= 0x2,
 	SBI_FWFT_DOUBLE_TRAP			= 0x3,
 	SBI_FWFT_PTE_AD_HW_UPDATING		= 0x4,
-	SBI_FWFT_LOCAL_RESERVED_START		= 0x5,
+	SBI_FWFT_POINTER_MASKING_PMLEN          = 0x5,
+	SBI_FWFT_CONTROL_TRANSFER_RECORD        = 0x6,
+	SBI_FWFT_LOCAL_RESERVED_START		= 0x7,
 	SBI_FWFT_LOCAL_RESERVED_END		= 0x3fffffff,
 	SBI_FWFT_LOCAL_PLATFORM_START		= 0x40000000,
 	SBI_FWFT_LOCAL_PLATFORM_END		= 0x7fffffff,
lib/sbi/sbi_fwft.c +30 -0
index f1ae91592fbc880f2e59586bed79cad12b23e994..c12566d891f17f653f3a7524bedfc289519628b6 100644
@@ -59,6 +59,8 @@ static const unsigned long fwft_defined_features[] = {
 	SBI_FWFT_SHADOW_STACK,
 	SBI_FWFT_DOUBLE_TRAP,
 	SBI_FWFT_PTE_AD_HW_UPDATING,
+	SBI_FWFT_POINTER_MASKING_PMLEN,
+	SBI_FWFT_CONTROL_TRANSFER_RECORD,
 };
 
 static bool fwft_is_defined_feature(enum sbi_fwft_feature_t feature)
@@ -145,6 +147,28 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+static int fwft_ctr_supported(struct fwft_config *conf)
+{
+	if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+				    SBI_HART_EXT_SSCTR))
+		return SBI_ENOTSUPP;
+
+	return SBI_OK;
+}
+
+static int fwft_set_ctr(struct fwft_config *conf, unsigned long value)
+{
+	/* CTR is enabled by default and can not be disabled. */
+	return SBI_EDENIED;
+}
+
+static int fwft_get_ctr(struct fwft_config *conf, unsigned long *value)
+{
+	*value = (csr_read(CSR_MSTATEEN0) & SMSTATEEN0_CTR) != 0;
+
+	return SBI_OK;
+}
+
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
 	int i;
@@ -236,6 +260,12 @@ static const struct fwft_feature features[] =
 		.set = fwft_set_adue,
 		.get = fwft_get_adue,
 	},
+	{
+		.id = SBI_FWFT_CONTROL_TRANSFER_RECORD,
+		.supported = fwft_ctr_supported,
+		.set = fwft_set_ctr,
+		.get = fwft_get_ctr,
+	},
 };
 
 int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot)
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 2/2 lib: sbi: fwft: Add FWFT support for Control transfer records ext

Hi Rajnesh,

Quoted 16 lines · Rajnesh Kanwal

Allow supervisor software to request that Ssctr be enabled for S-mode. We keep Ssctr always enabled and return SBI_ERR_DENIED on sbi_fwft_set() call to notify calling software that Ssctr is always enabled by default.

---
 include/sbi/sbi_ecall_interface.h |  4 +++-
 lib/sbi/sbi_fwft.c                | 30 ++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +0 -0
index e9a8167760a9d1e7a06d7142b4e02047b5201319..4db5284ff3d6500666e7c87653b8e4943663815a 100644

include/sbi/sbi_ecall_interface.h enum sbi_fwft_feature_t { · line 133

Quoted 7 lines
include/sbi/sbi_ecall_interface.h +3 -1
@@ -128,7 +128,9 @@ enum sbi_fwft_feature_t {
 	SBI_FWFT_SHADOW_STACK			= 0x2,
 	SBI_FWFT_DOUBLE_TRAP			= 0x3,
 	SBI_FWFT_PTE_AD_HW_UPDATING		= 0x4,
-	SBI_FWFT_LOCAL_RESERVED_START		= 0x5,
+	SBI_FWFT_POINTER_MASKING_PMLEN          = 0x5,
+	SBI_FWFT_CONTROL_TRANSFER_RECORD        = 0x6,
+	SBI_FWFT_LOCAL_RESERVED_START		= 0x7,

You will need to rebase your patch, as a SBI_FWFT_POINTER_MASKING_PMLEN implementation has been merged in the meantime.

Quoted 28 lines
include/sbi/sbi_ecall_interface.h +0 -0
 	SBI_FWFT_LOCAL_RESERVED_END		= 0x3fffffff,
 	SBI_FWFT_LOCAL_PLATFORM_START		= 0x40000000,
 	SBI_FWFT_LOCAL_PLATFORM_END		= 0x7fffffff,
lib/sbi/sbi_fwft.c +10 -0
index f1ae91592fbc880f2e59586bed79cad12b23e994..c12566d891f17f653f3a7524bedfc289519628b6 100644
@@ -59,6 +59,8 @@ static const unsigned long fwft_defined_features[] = {
 	SBI_FWFT_SHADOW_STACK,
 	SBI_FWFT_DOUBLE_TRAP,
 	SBI_FWFT_PTE_AD_HW_UPDATING,
+	SBI_FWFT_POINTER_MASKING_PMLEN,
+	SBI_FWFT_CONTROL_TRANSFER_RECORD,
 };
 
 static bool fwft_is_defined_feature(enum sbi_fwft_feature_t feature)
@@ -145,6 +147,28 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+static int fwft_ctr_supported(struct fwft_config *conf)
+{
+	if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+				    SBI_HART_EXT_SSCTR))
+		return SBI_ENOTSUPP;
+
+	return SBI_OK;
+}

lib/sbi/sbi_fwft.c static int fwft_get_adue(struct fwft_config *conf, unsigned long *value) · line 167

Quoted 10 lines
lib/sbi/sbi_fwft.c +10 -0
+
+static int fwft_set_ctr(struct fwft_config *conf, unsigned long value)
+{
+	/* CTR is enabled by default and can not be disabled. */
+	return SBI_EDENIED;
+}
+
+static int fwft_get_ctr(struct fwft_config *conf, unsigned long *value)
+{
+	*value = (csr_read(CSR_MSTATEEN0) & SMSTATEEN0_CTR) != 0;

Since Smstateen is not required for Ssctr, you need to handle the case where Smstateen is not implemented. In this case, it really would be impossible to disable the SBI_FWFT_CONTROL_TRANSFER_RECORD feature.

Regards,
Samuel

Quoted 21 lines
lib/sbi/sbi_fwft.c +10 -0
+
+	return SBI_OK;
+}
+
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
 	int i;
@@ -236,6 +260,12 @@ static const struct fwft_feature features[] =
 		.set = fwft_set_adue,
 		.get = fwft_get_adue,
 	},
+	{
+		.id = SBI_FWFT_CONTROL_TRANSFER_RECORD,
+		.supported = fwft_ctr_supported,
+		.set = fwft_set_ctr,
+		.get = fwft_get_ctr,
+	},
 };
 
 int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot)