chore(runtime): fix logging of RMI and RSI commands

This patch fixes the following issues with logging:
- logging of RMI function name in rmi_log_on_exit()
INFO("SMC_RMI_%-25s", handler->fn_name);
now matches the maximum name length of RSI handler
function SMC_RSI_RDEV_GET_INTERFACE_REPORT of 33 chars
excluding the null terminator.
- removed logging of return status and results of RSI
calls which return to host, because these values are
valid only when after the call execution continues in
realm.

Signed-off-by: AlexeiFedorov <Alexei.Fedorov@arm.com>
Change-Id: Ie9d43fe6451d88648bc5994fcf396d2c8015733e
diff --git a/runtime/core/exit.c b/runtime/core/exit.c
index b52a363..33c9c30 100644
--- a/runtime/core/exit.c
+++ b/runtime/core/exit.c
@@ -386,7 +386,7 @@
 	struct rsi_result res = {UPDATE_REC_RETURN_TO_REALM, 0UL,
 				{{[0 ... SMC_RESULT_REGS-1] = 0UL}}};
 	unsigned int function_id = (unsigned int)rec->regs[0];
-	bool restore_simd_ctx = false;
+	bool rec_ret, restore_simd_ctx = false;
 	unsigned int i;
 
 	RSI_LOG_SET(rec->regs);
@@ -468,10 +468,12 @@
 		advance_pc();
 	}
 
-	/* Log RSI call */
-	RSI_LOG_EXIT(function_id, rec->regs);
+	rec_ret = (((unsigned int)res.action & FLAG_EXIT_TO_HOST) == 0U);
 
-	return (((unsigned int)res.action & FLAG_EXIT_TO_HOST) == 0U);
+	/* Log RSI call */
+	RSI_LOG_EXIT(function_id, rec->regs, rec_ret);
+
+	return rec_ret;
 }
 
 /*
diff --git a/runtime/core/handler.c b/runtime/core/handler.c
index fad1c6a..46894b2 100644
--- a/runtime/core/handler.c
+++ b/runtime/core/handler.c
@@ -221,8 +221,12 @@
 	    (handler->log_error && (rc.status != RMI_SUCCESS))) {
 		unsigned int num;
 
-		/* Print function name */
-		INFO("SMC_RMI_%-21s", handler->fn_name);
+		/*
+		 * Print function name.
+		 * RSI handler function SMC_RSI_RDEV_GET_INTERFACE_REPORT has
+		 * maximum name length of 33 chars excluding the null terminator.
+		 */
+		INFO("SMC_RMI_%-25s", handler->fn_name);
 
 		/* Print arguments */
 		num = (unsigned int)handler->type & 0xFFU;
diff --git a/runtime/include/rsi-logger.h b/runtime/include/rsi-logger.h
index c5a506b..5a30134 100644
--- a/runtime/include/rsi-logger.h
+++ b/runtime/include/rsi-logger.h
@@ -20,7 +20,7 @@
 #if (RSI_LOG_LEVEL > LOG_LEVEL_NONE) && (RSI_LOG_LEVEL <= LOG_LEVEL)
 
 void rsi_log_on_exit(unsigned int function_id, unsigned long args[],
-		     unsigned long regs[]);
+		     unsigned long regs[], bool ret_to_rec);
 
 /*
  * Store SMC RSI parameters. Takes an array of regs[] of size
@@ -33,13 +33,14 @@
 	}
 
 /*
- * Macro prints RSI call function name, parameters and result values
+ * Macro prints RSI call function name and its parameters, result values are
+ * printed when 'rec_to_ret' is set to true.
  */
-# define RSI_LOG_EXIT(id, res)	rsi_log_on_exit(id, rsi_log_args, res)
+# define RSI_LOG_EXIT(id, res, rec_to_ret)	rsi_log_on_exit(id, rsi_log_args, res, rec_to_ret)
 
 #else
 # define RSI_LOG_SET(regs)
-# define RSI_LOG_EXIT(id, res)
+# define RSI_LOG_EXIT(id, res, rec_to_ret)
 
 #endif /* (> LOG_LEVEL_NONE) && (<= LOG_LEVEL) */
 #endif /* RSI_LOGGER_H */
diff --git a/runtime/rsi/logger.c b/runtime/rsi/logger.c
index ebeddf1..fc620dc 100644
--- a/runtime/rsi/logger.c
+++ b/runtime/rsi/logger.c
@@ -11,8 +11,11 @@
 #include <string.h>
 #include <utils_def.h>
 
-/* RMI handler uses 29 chars for function name */
-#define MAX_NAME_LEN	29UL
+/*
+ * RSI handler uses 34 chars max for function name including the null
+ * terminator
+ */
+#define MAX_NAME_LEN	sizeof("SMC_RSI_RDEV_GET_INTERFACE_REPORT")
 
 /* Max 10 64-bit parameters separated by space */
 #define PARAMS_STR_LEN	(10UL * sizeof("0123456789ABCDEF"))
@@ -20,7 +23,7 @@
 #define MAX_STATUS_LEN	sizeof("RSI_ERROR_UNKNOWN")
 
 #define BUFFER_SIZE	(MAX_NAME_LEN + PARAMS_STR_LEN + \
-			sizeof(" > ") - 1UL + MAX_STATUS_LEN)
+			sizeof(" > ") - 3UL + MAX_STATUS_LEN)
 
 #define WHITESPACE_CHAR	0x20
 
@@ -96,10 +99,10 @@
 
 		num = logger->num_args;
 		if (logger->fn_name != NULL) {
-			cnt = snprintf(buf, MAX_NAME_LEN + 1UL,
+			cnt = snprintf(buf, MAX_NAME_LEN,
 				       "%s%s", "SMC_RSI", logger->fn_name);
 		} else {
-			cnt = snprintf(buf, MAX_NAME_LEN + 1UL,
+			cnt = snprintf(buf, MAX_NAME_LEN,
 				       "%s", "SMC_RSI_<unsupported>");
 		}
 		break;
@@ -108,22 +111,22 @@
 	case SMC32_PSCI_FID_MIN ... SMC32_PSCI_FID_MAX:
 		FALLTHROUGH;
 	case SMC64_PSCI_FID_MIN ... SMC64_PSCI_FID_MAX:
-		cnt = snprintf(buf, MAX_NAME_LEN + 1UL, "%s%08x", "PSCI_", id);
+		cnt = snprintf(buf, MAX_NAME_LEN, "%s%08x", "PSCI_", id);
 		break;
 
 	/* Other SMC calls */
 	default:
-		cnt = snprintf(buf, MAX_NAME_LEN + 1UL, "%s%08x", "SMC_", id);
+		cnt = snprintf(buf, MAX_NAME_LEN, "%s%08x", "SMC_", id);
 		break;
 	}
 
-	assert((cnt > 0) && ((unsigned int)cnt < (MAX_NAME_LEN + 1U)));
+	assert((cnt > 0) && ((unsigned int)cnt < MAX_NAME_LEN));
 
 	(void)memset((void *)((uintptr_t)buf + (unsigned int)cnt), WHITESPACE_CHAR,
 					MAX_NAME_LEN - (size_t)cnt);
 
-	buf = (char *)((uintptr_t)buf + MAX_NAME_LEN);
-	len -= MAX_NAME_LEN;
+	buf = (char *)((uintptr_t)buf + MAX_NAME_LEN - 1UL);
+	len -= (MAX_NAME_LEN - 1UL);
 
 	/* Arguments */
 	for (unsigned int i = 0U; i < num; i++) {
@@ -155,14 +158,23 @@
 
 /* cppcheck-suppress misra-c2012-8.4 */
 void rsi_log_on_exit(unsigned int function_id, unsigned long args[],
-		     unsigned long regs[])
+		     unsigned long regs[], bool ret_to_rec)
 {
 	char buffer[BUFFER_SIZE];
 	size_t len = print_entry(function_id, args, buffer, sizeof(buffer));
 	char *buf = (char *)((uintptr_t)buffer + sizeof(buffer) - len);
-	unsigned int num = 3U;	/* results in X1-X3 */
+	unsigned int num;
 	int cnt;
 
+	/*
+	 * Return status and results in regs[] are only valid if the RSI call
+	 * execution returns to REC.
+	 */
+	if (!ret_to_rec) {
+		rmm_log("%s\n", buffer);
+		return;
+	}
+
 	switch (function_id) {
 	case SMC_RSI_VERSION ... SMC_RSI_PLANE_REG_WRITE: {
 		const struct rsi_handler *logger =
@@ -176,6 +188,7 @@
 	default:
 		/* Print result code */
 		cnt = print_code(buf, len, regs[0]);
+		num = 3U;	/* results in X1-X3 */
 	}
 
 	assert((cnt > 0) && (cnt < (int)len));
diff --git a/runtime/tests/rsi_logger_tests.cpp b/runtime/tests/rsi_logger_tests.cpp
index fd4fd88..1ae38a7 100644
--- a/runtime/tests/rsi_logger_tests.cpp
+++ b/runtime/tests/rsi_logger_tests.cpp
@@ -40,7 +40,7 @@
 };
 
 #if (RSI_LOG_LEVEL > LOG_LEVEL_NONE) && (RSI_LOG_LEVEL <= LOG_LEVEL)
-static void rsi_log_test(unsigned int id, unsigned int status)
+static void rsi_log_test(unsigned int id, unsigned int status, bool ret_to_rec)
 {
 	unsigned long args[10];
 	unsigned long regs[5];
@@ -67,22 +67,26 @@
 		regs[i] = rand();
 	}
 
-	rsi_log_on_exit(id, args, regs);
+	rsi_log_on_exit(id, args, regs, ret_to_rec);
 }
 
 TEST(rsi_logger_tests, RSI_LOGGER_TC1)
 {
 	unsigned int status, id;
 
-	for (status = LOG_SUCCESS; status <= LOG_RANDOM; status++) {
-		for (id = SMC_RSI_VERSION; id <= SMC_RSI_HOST_CALL; id++) {
-			rsi_log_test(id, status);
-		}
-	}
+	for (unsigned int i = 0U; i < 2U; i++) {
+		bool ret_to_rec = ((i & 1U) != 0U);
 
-	rsi_log_test(SMC32_PSCI_FID_MIN, LOG_RANDOM);
-	rsi_log_test(SMC64_PSCI_FID_MAX, LOG_RANDOM);
-	rsi_log_test(SMC64_PSCI_FID_MAX + rand(), LOG_RANDOM);
+		for (status = LOG_SUCCESS; status <= LOG_RANDOM; status++) {
+			for (id = SMC_RSI_VERSION; id <= SMC_RSI_PLANE_REG_WRITE; id++) {
+				rsi_log_test(id, status, ret_to_rec);
+			}
+		}
+
+		rsi_log_test(SMC32_PSCI_FID_MIN, LOG_RANDOM, ret_to_rec);
+		rsi_log_test(SMC64_PSCI_FID_MAX, LOG_RANDOM, ret_to_rec);
+		rsi_log_test(SMC64_PSCI_FID_MAX + rand(), LOG_RANDOM, ret_to_rec);
+	}
 
 	TEST_EXIT;
 }