[RFC PATCH 13/23] lib: sbi: mpxy: handle temporary shmem map and unmap errors

Yu-Chien Peter Lin peter.lin at sifive.com
Mon Nov 30 02:11:37 PST 2026


The MPXY calls access the S-mode shared memory between
sbi_hart_protection_temp_map_range() and
sbi_hart_protection_temp_unmap_range() but ignore both return values.
With Smepmp the temporary mapping is verified and can fail, and M-mode
must not touch the shared memory without a valid mapping.

Return SBI_ERR_FAILED without accessing the shared memory when the
mapping fails, and report a failed unmap instead of returning success.
When a call already fails for another reason, keep that error code.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 lib/sbi/sbi_mpxy.c | 61 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 45 insertions(+), 16 deletions(-)

diff --git a/lib/sbi/sbi_mpxy.c b/lib/sbi/sbi_mpxy.c
index a09a09dc..1f50569e 100644
--- a/lib/sbi/sbi_mpxy.c
+++ b/lib/sbi/sbi_mpxy.c
@@ -401,10 +401,14 @@ int sbi_mpxy_set_shmem(unsigned long shmem_phys_lo,
 	if (flags == SBI_EXT_MPXY_SHMEM_FLAG_OVERWRITE_RETURN) {
 		ret_buf = (unsigned long *)(ulong)SHMEM_PHYS_ADDR(shmem_phys_hi,
 								  shmem_phys_lo);
-		sbi_hart_protection_temp_map_range((unsigned long)ret_buf, mpxy_shmem_size);
+		if (sbi_hart_protection_temp_map_range((unsigned long)ret_buf,
+						       mpxy_shmem_size))
+			return SBI_ERR_FAILED;
 		ret_buf[0] = cpu_to_lle(ms->shmem.shmem_addr_lo);
 		ret_buf[1] = cpu_to_lle(ms->shmem.shmem_addr_hi);
-		sbi_hart_protection_temp_unmap_range((unsigned long)ret_buf, mpxy_shmem_size);
+		if (sbi_hart_protection_temp_unmap_range((unsigned long)ret_buf,
+							 mpxy_shmem_size))
+			return SBI_ERR_FAILED;
 	}
 
 	/** Setup the new shared memory */
@@ -436,7 +440,9 @@ int sbi_mpxy_get_channel_ids(u32 start_index)
 		return SBI_ERR_INVALID_PARAM;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	/** number of channel ids which can be stored in shmem adjusting
 	 * for remaining and returned fields */
@@ -466,7 +472,9 @@ int sbi_mpxy_get_channel_ids(u32 start_index)
 	shmem_base[0] = cpu_to_le32(remaining);
 	shmem_base[1] = cpu_to_le32(returned);
 
-	sbi_hart_protection_temp_unmap_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	if (sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+						 mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	return SBI_SUCCESS;
 }
@@ -475,7 +483,7 @@ int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 {
 	struct mpxy_state *ms = sbi_domain_mpxy_state_thishart_ptr();
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
-	int ret = SBI_SUCCESS;
+	int ret = SBI_SUCCESS, rc;
 	void *shmem_base;
 	u32 *attr_ptr;
 	u64 end_id;
@@ -499,7 +507,9 @@ int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 	shmem_base = hart_shmem_base(ms);
 	end_id = (u64)base_attr_id + attr_count - 1;
 
-	sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	/* Standard attributes range check */
 	if (mpxy_is_std_attr(base_attr_id)) {
@@ -538,7 +548,10 @@ int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 					       base_attr_id, attr_count);
 	}
 out:
-	sbi_hart_protection_temp_unmap_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	rc = sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+						  mpxy_shmem_size);
+	if (rc && !ret)
+		ret = SBI_ERR_FAILED;
 	return ret;
 }
 
@@ -629,7 +642,7 @@ int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 	u32 *mem_ptr, attr_id, attr_val;
 	struct sbi_mpxy_channel *channel;
-	int ret, mem_idx;
+	int ret, rc, mem_idx;
 	void *shmem_base;
 	u64 end_id;
 
@@ -652,7 +665,9 @@ int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 	shmem_base = hart_shmem_base(ms);
 	end_id = (u64)base_attr_id + attr_count - 1;
 
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	mem_ptr = (u32 *)shmem_base;
 
@@ -709,7 +724,10 @@ int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 					       base_attr_id, attr_count);
 	}
 out:
-	sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base, mpxy_shmem_size);
+	rc = sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+						  mpxy_shmem_size);
+	if (rc && !ret)
+		ret = SBI_ERR_FAILED;
 	return ret;
 }
 
@@ -722,7 +740,7 @@ int sbi_mpxy_send_message(u32 channel_id, u8 msg_id,
 	void *shmem_base, *resp_buf;
 	u32 resp_bufsize;
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
-	int ret;
+	int ret, rc;
 
 	if (!mpxy_shmem_enabled(ms))
 		return SBI_ERR_NO_SHMEM;
@@ -742,7 +760,9 @@ int sbi_mpxy_send_message(u32 channel_id, u8 msg_id,
 		return SBI_ERR_INVALID_PARAM;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	if (resp_data_len) {
 		resp_buf = shmem_base;
@@ -759,7 +779,10 @@ int sbi_mpxy_send_message(u32 channel_id, u8 msg_id,
 							     msg_data_len);
 	}
 
-	sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base, mpxy_shmem_size);
+	rc = sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+						  mpxy_shmem_size);
+	if (rc && !ret)
+		ret = SBI_ERR_FAILED;
 
 	if (ret == SBI_ERR_TIMEOUT || ret == SBI_ERR_IO)
 		return ret;
@@ -780,7 +803,7 @@ int sbi_mpxy_get_notification_events(u32 channel_id, unsigned long *events_len)
 	struct sbi_mpxy_channel *channel;
 	void *eventsbuf, *shmem_base;
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
-	int ret;
+	int ret, rc;
 
 	if (!mpxy_shmem_enabled(ms))
 		return SBI_ERR_NO_SHMEM;
@@ -790,16 +813,22 @@ int sbi_mpxy_get_notification_events(u32 channel_id, unsigned long *events_len)
 		return SBI_ERR_NOT_SUPPORTED;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 	eventsbuf = shmem_base;
 	ret = channel->get_notification_events(channel, eventsbuf,
 					       mpxy_shmem_size,
 					       events_len);
-	sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base, mpxy_shmem_size);
+	rc = sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+						  mpxy_shmem_size);
 
 	if (ret)
 		return ret;
 
+	if (rc)
+		return SBI_ERR_FAILED;
+
 	if (*events_len > (mpxy_shmem_size - 16))
 		return SBI_ERR_FAILED;
 
-- 
2.43.7




More information about the opensbi mailing list