forked from Mirrors/opensbi
		
	lib: sbi: fwft: add support for SBI_FWFT_POINTER_MASKING_PMLEN
Add support for controlling the pointer masking mode on harts which support the Smnpm extension. This extension can only exist on harts where XLEN >= 64 bits. This implementation selects the mode with the smallest PMLEN that satisfies the caller's requested lower bound. Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
		
				
					committed by
					
						
						Anup Patel
					
				
			
			
				
	
			
			
			
						parent
						
							ebfaf1974e
						
					
				
				
					commit
					1cb234b1c9
				
			@@ -59,6 +59,7 @@ 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,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static bool fwft_is_defined_feature(enum sbi_fwft_feature_t feature)
 | 
			
		||||
@@ -145,6 +146,64 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)
 | 
			
		||||
	return SBI_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if __riscv_xlen > 32
 | 
			
		||||
static int fwft_pmlen_supported(struct fwft_config *conf)
 | 
			
		||||
{
 | 
			
		||||
	if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
 | 
			
		||||
				    SBI_HART_EXT_SMNPM))
 | 
			
		||||
		return SBI_ENOTSUPP;
 | 
			
		||||
 | 
			
		||||
	return SBI_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool fwft_try_to_set_pmm(unsigned long pmm)
 | 
			
		||||
{
 | 
			
		||||
	csr_set(CSR_MENVCFG, pmm);
 | 
			
		||||
	return (csr_read(CSR_MENVCFG) & ENVCFG_PMM) == pmm;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fwft_set_pmlen(struct fwft_config *conf, unsigned long value)
 | 
			
		||||
{
 | 
			
		||||
	unsigned long prev;
 | 
			
		||||
 | 
			
		||||
	if (value > 16)
 | 
			
		||||
		return SBI_EINVAL;
 | 
			
		||||
 | 
			
		||||
	prev = csr_read_clear(CSR_MENVCFG, ENVCFG_PMM);
 | 
			
		||||
	if (value == 0)
 | 
			
		||||
		return SBI_OK;
 | 
			
		||||
	if (value <= 7) {
 | 
			
		||||
		if (fwft_try_to_set_pmm(ENVCFG_PMM_PMLEN_7))
 | 
			
		||||
			return SBI_OK;
 | 
			
		||||
		csr_clear(CSR_MENVCFG, ENVCFG_PMM);
 | 
			
		||||
	}
 | 
			
		||||
	if (fwft_try_to_set_pmm(ENVCFG_PMM_PMLEN_16))
 | 
			
		||||
		return SBI_OK;
 | 
			
		||||
	csr_write(CSR_MENVCFG, prev);
 | 
			
		||||
 | 
			
		||||
	return SBI_EINVAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fwft_get_pmlen(struct fwft_config *conf, unsigned long *value)
 | 
			
		||||
{
 | 
			
		||||
	switch (csr_read(CSR_MENVCFG) & ENVCFG_PMM) {
 | 
			
		||||
	case ENVCFG_PMM_PMLEN_0:
 | 
			
		||||
		*value = 0;
 | 
			
		||||
		break;
 | 
			
		||||
	case ENVCFG_PMM_PMLEN_7:
 | 
			
		||||
		*value = 7;
 | 
			
		||||
		break;
 | 
			
		||||
	case ENVCFG_PMM_PMLEN_16:
 | 
			
		||||
		*value = 16;
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		return SBI_EFAIL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return SBI_OK;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
@@ -236,6 +295,14 @@ static const struct fwft_feature features[] =
 | 
			
		||||
		.set = fwft_set_adue,
 | 
			
		||||
		.get = fwft_get_adue,
 | 
			
		||||
	},
 | 
			
		||||
#if __riscv_xlen > 32
 | 
			
		||||
	{
 | 
			
		||||
		.id = SBI_FWFT_POINTER_MASKING_PMLEN,
 | 
			
		||||
		.supported = fwft_pmlen_supported,
 | 
			
		||||
		.set = fwft_set_pmlen,
 | 
			
		||||
		.get = fwft_get_pmlen,
 | 
			
		||||
	},
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot)
 | 
			
		||||
 
 | 
			
		||||
@@ -680,6 +680,7 @@ 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(smnpm, SBI_HART_EXT_SMNPM),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
_Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user