aboutsummaryrefslogtreecommitdiffstats
path: root/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c
diff options
context:
space:
mode:
Diffstat (limited to 'demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c')
-rw-r--r--demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c248
1 files changed, 23 insertions, 225 deletions
diff --git a/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c b/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c
index 00dcd2626..9c800a3c5 100644
--- a/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c
+++ b/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/proxies/tssockstub.c
@@ -23,6 +23,7 @@
#include "ch.h"
#include "chobjfifos.h"
#include "chtssi.h"
+#include "tsproxystubs.h"
#include "tssockstub.h"
#include <string.h>
#include <ctype.h>
@@ -31,13 +32,6 @@
/* Module local definitions. */
/*===========================================================================*/
-#define METHOD_MAX_PARAMS 6
-#define STUB_MAX_OPS 32
-
-#define OP_PRMDIR_NONE 0
-#define OP_PRMDIR_IN 1
-#define OP_PRMDIR_OUT 2
-
/*===========================================================================*/
/* Module exported variables. */
/*===========================================================================*/
@@ -45,231 +39,35 @@
/*===========================================================================*/
/* Module local types. */
/*===========================================================================*/
-typedef struct stub_op stub_op_t;
-typedef enum {FREE=0, CALLING, PENDING} op_state_t;
-
-typedef struct stub_param {
- uint32_t dir;
- uint32_t val;
- uint32_t size;
-} stub_parm_t;
-
-typedef struct stub_op {
- uint32_t op_code; /* e.g. connect, recv, sendv, close, etc.*/
- op_state_t op_state; /* calling, pending, free.*/
- stub_parm_t op_p[METHOD_MAX_PARAMS];
- thread_reference_t op_wthdp; /* TS internal client thread (the caller).*/
-} stub_op_t;
/*===========================================================================*/
/* Module local variables. */
/*===========================================================================*/
-
-static objects_fifo_t ops_fifo;
-static msg_t ops_msgs[STUB_MAX_OPS];
-static struct stub_op ops[STUB_MAX_OPS] = {0};
-static bool tsSkelIsReady = false;
+static stub_ctx_t stub_ctx;
/*===========================================================================*/
/* Module local functions. */
/*===========================================================================*/
-static bool isOpValid(stub_op_t *op)
-{
- if ((op < &ops[0]) || (op >= &ops[STUB_MAX_OPS]))
- return FALSE;
- if (((char *)op - (char *)&ops[0]) % sizeof ops[0])
- return FALSE;
- return TRUE;
-}
-
-/**
- * @brief Implement an a call to a NSEC function.
- * @details It activates the channel between the stubs service and
- * the skels daemon running in the nsec world.
- * To do it, it uses an event to signal the skels
- * daemon that a new op request is ready to be executed.
- * Behind the scenes, the skels daemon will then gets the op, calling
- * the stubs service via smc. The daemon executes it and then calls
- * the stubs service again to post the result and to wake up the
- * calling thread of this function.
- *
- * @param[in] op the 'remote' method description.
- *
- * @return the return value of 'remote' method.
- */
-static uint32_t callRemote(stub_op_t *op) {
- uint32_t r;
-
- chSysLock();
- chFifoSendObjectI(&ops_fifo, op);
- chEvtBroadcastFlagsI(&tsEventSource, EVT_F_SOCK_NEW_OP);
- chThdSuspendS(&op->op_wthdp);
- chSysUnlock();
- r = op->op_code;
- chFifoReturnObject(&ops_fifo, op);
- return r;
-}
-
-static stub_op_t *getNewOp(void) {
- stub_op_t *op = chFifoTakeObjectTimeout(&ops_fifo, TIME_INFINITE);
- memset(op, 0, sizeof *op);
- op->op_state = CALLING;
- return op;
-}
-
/*===========================================================================*/
/* Module exported functions. */
/*===========================================================================*/
/**
- * @brief The stubs service.
- * @details And this is where the magic happens.
+ * @brief The sockets stub service.
*/
-THD_WORKING_AREA(waTsStubsService, 1024);
-THD_FUNCTION(TsStubsService, tsstate) {
- ts_state_t *svcp = tsstate;
- skel_req_t *skrp;
- stub_op_t *op;
- msg_t r;
- int i;
-
- chFifoObjectInit(&ops_fifo, sizeof (stub_op_t), STUB_MAX_OPS,
- sizeof (uint8_t), ops, ops_msgs);
- for (;/* ever */;) {
-
- /* Wait a service request.*/
- (void)tssiWaitRequest(svcp);
- skrp = (skel_req_t *)TS_GET_DATA(svcp);
- r = SMC_SVC_OK;
-
- /* Process the request.*/
- if (TS_GET_DATALEN(svcp) != sizeof (skel_req_t)) {
- TS_SET_STATUS(svcp, SMC_SVC_INVALID);
- continue;
- }
-
- switch (skrp->req) {
- case SKEL_REQ_READY:
- tsSkelIsReady = true;
- break;
-
- case SKEL_REQ_GETOP:
-
- /* The nsec skeleton calls us to get a new op ready to be executed.*/
- if (chFifoReceiveObjectTimeout(&ops_fifo, (void **)&op, TIME_IMMEDIATE) ==
- MSG_TIMEOUT) {
-
- /* no op ready to be executed.*/
- r = SMC_SVC_NHND;
- break;
- }
- skrp->stub_op = (uint32_t)op;
- skrp->stub_op_code = op->op_code;
-
- /* Pass all the 'by value' arguments from stub to skel.*/
- for (i = 0; i < METHOD_MAX_PARAMS; ++i) {
- if (op->op_p[i].dir == OP_PRMDIR_NONE)
- skrp->stub_op_p[i] = op->op_p[i].val;
- }
- op->op_state = PENDING;
- break;
-
- case SKEL_REQ_CPYPRMS:
-
- /* The nsec skel calls us to get a copy of the 'in' parameters of
- the specified op.
- An 'in' parameter is an indirect argument, that is an argument
- the value of which is a pointer to a memory buffer, that
- must be copied in a non secure memory buffer.
- It represents data to be consumed by the callee.*/
- op = (stub_op_t *)skrp->stub_op;
- if (!isOpValid(op) || op->op_state != PENDING ||
- op->op_code != skrp->stub_op_code) {
- r = SMC_SVC_INVALID;
- break;
- }
-
- /* Copy all 'in' parameters.
- For each parameter check that the destination memory area
- is in the non secure memory arena.*/
- for (i = 0; i < METHOD_MAX_PARAMS; ++i) {
- if ((op->op_p[i].dir & OP_PRMDIR_IN) == 0)
- continue;
- if (!tsIsAddrSpaceValid((void *)skrp->stub_op_p[i], op->op_p[i].size)) {
- r = SMC_SVC_INVALID;
- break;
- }
- memcpy((void *)skrp->stub_op_p[i], (void *)op->op_p[i].val,
- op->op_p[i].size);
- }
- break;
-
- case SKEL_REQ_PUTRES:
-
- /* The nsec skel calls us to put a copy of the 'out' parameters of
- the specified op.
- An 'out' parameter is an indirect argument, that is an argument
- the value of which is a pointer to a memory buffer, that
- must be copied in a secure memory buffer.
- It represents data produced by the callee.*/
- op = (stub_op_t *)skrp->stub_op;
- if (!isOpValid(op) || op->op_state != PENDING ||
- op->op_code != skrp->stub_op_code) {
- r = SMC_SVC_INVALID;
- break;
- }
-
- /* Copy all 'out' parameters.
- For each parameter check that the source memory area
- is in the non secure memory arena, and that the size returned
- fits in the caller buffer size.*/
- for (i = 0; i < METHOD_MAX_PARAMS; ++i) {
- if ((op->op_p[i].dir & OP_PRMDIR_OUT) == 0)
- continue;
- if (!tsIsAddrSpaceValid((void *)skrp->stub_op_p[i], skrp->stub_op_p_sz[i])
- || (skrp->stub_op_p_sz[i] > op->op_p[i].size)) {
- r = SMC_SVC_INVALID;
- break;
- }
- memcpy((void *)op->op_p[i].val, (void *)skrp->stub_op_p[i],
- skrp->stub_op_p_sz[i]);
- }
- if (r != SMC_SVC_OK)
- break;
-
- /* Set the return value of the 'remote' callee method,
- and wake up the caller.*/
- op->op_code = skrp->stub_op_result;
- chThdResume(&op->op_wthdp, MSG_OK);
- break;
-
- default:
- r = SMC_SVC_INVALID;
- break;
- }
-
- /* Set the response.*/
- TS_SET_STATUS(svcp, r);
- }
-}
-/**
- * @brief Is the skeletons daemon ready to operate?
- * @details It is used at the startup to synchronize the
- * stub service with the skeleton daemon.
- */
-void tsWaitStubSkelReady(void) {
- while (!tsSkelIsReady) {
- chThdSleepMilliseconds(100);
- }
+THD_WORKING_AREA(waTsSocksStubsService, 1024);
+THD_FUNCTION(TsSocksStubsService, tsstatep) {
+ stub_ctx.event_flag = EVT_F_SOCK_NEW_OP;
+ TsStubService((ts_state_t *)tsstatep, &stub_ctx);
}
/**
* @brief The sockets API.
*/
int socket(int domain, int type, int protocol) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_SOCKET;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_SOCKET;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)domain;
op->op_p[1].dir = OP_PRMDIR_NONE;
@@ -280,8 +78,8 @@ int socket(int domain, int type, int protocol) {
}
int connect(int s, const struct sockaddr *name, socklen_t namelen) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_CONNECT;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_CONNECT;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
op->op_p[1].dir = OP_PRMDIR_IN;
@@ -293,16 +91,16 @@ int connect(int s, const struct sockaddr *name, socklen_t namelen) {
}
int close(int s) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_CLOSE;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_CLOSE;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
return (int)callRemote(op);
}
int recv(int s, void *mem, size_t len, int flags) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_RECV;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_RECV;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
op->op_p[1].dir = OP_PRMDIR_OUT;
@@ -316,8 +114,8 @@ int recv(int s, void *mem, size_t len, int flags) {
}
int send(int s, const void *dataptr, size_t size, int flags) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_SEND;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_SEND;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
op->op_p[1].dir = OP_PRMDIR_IN;
@@ -332,8 +130,8 @@ int send(int s, const void *dataptr, size_t size, int flags) {
int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
struct timeval *timeout) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_SELECT;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_SELECT;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)maxfdp1;
op->op_p[1].dir = OP_PRMDIR_IN|OP_PRMDIR_OUT;
@@ -352,8 +150,8 @@ int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
}
int bind(int s, const struct sockaddr *name, socklen_t namelen) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_BIND;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_BIND;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
op->op_p[1].dir = OP_PRMDIR_IN;
@@ -365,8 +163,8 @@ int bind(int s, const struct sockaddr *name, socklen_t namelen) {
}
int listen(int s, int backlog) {
- stub_op_t *op = getNewOp();
- op->op_code = STUB_OP_LISTEN;
+ stub_op_t *op = getNewOp(&stub_ctx);
+ op->op_code = SOCK_OP_LISTEN;
op->op_p[0].dir = OP_PRMDIR_NONE;
op->op_p[0].val = (uint32_t)s;
op->op_p[1].dir = OP_PRMDIR_NONE;