gpoulios commented on code in PR #16734:
URL: https://github.com/apache/nuttx/pull/16734#discussion_r2217035282


##########
drivers/misc/optee_rpc.c:
##########
@@ -0,0 +1,366 @@
+/****************************************************************************
+ * drivers/misc/optee_rpc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <stdint.h>
+#include "optee.h"
+#include "optee_msg.h"
+#include "optee_rpc.h"
+
+#ifdef CONFIG_DEV_OPTEE_SUPPLICANT
+#  include "optee_supplicant.h"
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: optee_rpc_handle_cmd_get_time
+ *
+ * Description:
+ *   Return REE wall-clock time (seconds + nanoseconds) to secure world.
+ *
+ * Parameters:
+ *   arg - Pointer to the RPC message argument located in shared memory by
+ *         the secure world. The answer will be placed in the same argument.
+ *
+ * Returned Value:
+ *   The time is written to:
+ *     arg->params[0].u.value.a   containing seconds since epoch
+ *     arg->params[0].u.value.b   containing nanoseconds
+ *   Result code is written to arg->ret.
+ *
+ ****************************************************************************/
+
+static void optee_rpc_handle_cmd_get_time(FAR struct optee_msg_arg *arg)
+{
+  struct timespec ts;
+
+  /* OP-TEE parameter validation. */
+
+  if (arg->num_params != 1 ||
+      (arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK)
+      != OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
+    {
+      arg->ret = TEE_ERROR_BAD_PARAMETERS;
+      return;
+    }
+
+  if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
+    {
+      /* Should not happen unless the RTC driver is missing */
+
+      arg->ret = TEE_ERROR_GENERIC;
+      return;
+    }
+
+  arg->params[0].u.value.a = (uint32_t)ts.tv_sec;   /* Seconds since epoch. */
+  arg->params[0].u.value.b = (uint32_t)ts.tv_nsec;  /* Nanoseconds.         */
+
+  arg->ret = TEE_SUCCESS;
+  return;
+}
+
+/****************************************************************************
+ * Name: optee_rpc_cmd_suspend
+ *
+ * Description:
+ *   Request from OP-TEE to suspend the current nuttx process.
+ *
+ * Parameters:
+ *   arg - Pointer to the RPC message argument, located in a shared page, by
+ *         the secure world, containing the time in msec to sleep.
+ *
+ * Returned Value:
+ *   None.  Result codes are written into arg->ret.
+ *
+ ****************************************************************************/
+
+static void optee_rpc_cmd_suspend(FAR struct optee_msg_arg *arg)
+{
+  /* OP-TEE parameter validation. */
+
+  if (arg->num_params != 1 ||
+      (arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
+          OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
+    {
+      arg->ret = TEE_ERROR_BAD_PARAMETERS;
+      return;
+    }
+
+  uint32_t msec_to_wait = arg->params[0].u.value.a;
+
+  if (msec_to_wait)
+    {
+      int ret = nxsig_usleep((useconds_t)msec_to_wait * 1000);
+
+      if (ret < 0 && get_errno() != EINTR)
+        {
+            arg->ret = TEE_ERROR_GENERIC;
+            return;
+        }
+    }
+
+  arg->ret = TEE_SUCCESS;
+}
+
+/****************************************************************************
+ * Name: optee_rpc_cmd_shm_alloc
+ *
+ * Description:
+ *   Handle OP-TEE's RPC to allocate shared memory.
+ *
+ * Parameters:
+ *   priv - Pointer to the driver's optee_priv_data struct.
+ *   arg - Pointer to the RPC message argument, located in a shared page
+ *         by the secure world. A copy of this message might be sent to the
+ *         supplicant process that runs in userspace for further processing.
+ *   last_page_list - Passes by reference a pointer that will be updated with
+ *                    the virtual address of the page list.
+ *
+ * Returned Value:
+ *   None.  Result codes are written into arg->ret.
+ *   Information about the shared memory is passed through arg->params
+ *
+ ****************************************************************************/
+
+static void optee_rpc_cmd_shm_alloc(FAR struct optee_priv_data *priv,
+                                    FAR struct optee_msg_arg *arg,
+                                    FAR void **last_page_list)
+{
+  FAR struct optee_shm *shm;
+  size_t n;
+  size_t size;
+  int32_t ret = OK;
+
+  arg->ret_origin = TEE_ORIGIN_COMMS;
+
+  /* OP-TEE parameter validation. */
+
+  if (arg->num_params != 1 ||
+      arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
+    {
+      arg->ret = TEE_ERROR_BAD_PARAMETERS;
+      return;
+    }
+
+  for (n = 1; n < arg->num_params; n++)
+    {
+      if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE)
+        {
+          arg->ret = TEE_ERROR_BAD_PARAMETERS;
+          return;
+        }
+    }
+
+  size = arg->params[0].u.value.b;
+  switch (arg->params[0].u.value.a)
+    {
+      case OPTEE_MSG_RPC_SHM_TYPE_APPL:
+#ifdef CONFIG_DEV_OPTEE_SUPPLICANT
+        ret = optee_supplicant_cmd_alloc(priv, size, &shm);
+#else
+        arg->ret = TEE_ERROR_NOT_SUPPORTED;
+        return;
+#endif
+        break;
+      case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
+        ret = optee_shm_alloc(priv, NULL , size, TEE_SHM_ALLOC, &shm);
+        break;
+      default:
+        arg->ret = TEE_ERROR_BAD_PARAMETERS;
+        return;
+    }
+
+  if (ret == -ENOMEM)
+    {
+      arg->ret = TEE_ERROR_OUT_OF_MEMORY;
+      return;
+    }
+  else if (ret == -ECOMM)
+    {
+      arg->ret = TEE_ERROR_COMMUNICATION;
+      return;
+    }
+  else if (ret != OK)
+    {
+      arg->ret = TEE_ERROR_GENERIC;
+      return;
+    }
+
+  if (shm->flags | TEE_SHM_REGISTER)

Review Comment:
   Always true



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to