Skip to content

Commit

Permalink
renamed public structs, moved DMA function prototypes to wh_server_dma.h
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbrett committed Apr 11, 2024
1 parent 1fd84e3 commit 192fa01
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 90 deletions.
24 changes: 12 additions & 12 deletions src/wh_server_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* building a binary tree, etc.) */


static int _checkAddrAgainstAllowList(const whDmaAddrList allowList, void* addr,
static int _checkAddrAgainstAllowList(const whServerDmaAddrList allowList, void* addr,
size_t size)
{
uintptr_t startAddr = (uintptr_t)addr;
Expand All @@ -26,8 +26,8 @@ static int _checkAddrAgainstAllowList(const whDmaAddrList allowList, void* addr,
return WH_ERROR_ACCESS;
}

static int _checkMemOperAgainstAllowList(const whDmaAddrAllowList* allowList,
whDmaOper oper, void* addr,
static int _checkMemOperAgainstAllowList(const whServerDmaAddrAllowList* allowList,
whServerDmaOper oper, void* addr,
size_t size)
{
int rc = WH_ERROR_OK;
Expand All @@ -48,7 +48,7 @@ static int _checkMemOperAgainstAllowList(const whDmaAddrAllowList* allowList,
return rc;
}

int wh_Server_DmaRegisterCb32(whServerContext* server, whDmaClientMem32Cb cb)
int wh_Server_DmaRegisterCb32(whServerContext* server, whServerDmaClientMem32Cb cb)
{
if (NULL == server || NULL == cb) {
return WH_ERROR_BADARGS;
Expand All @@ -59,7 +59,7 @@ int wh_Server_DmaRegisterCb32(whServerContext* server, whDmaClientMem32Cb cb)
return WH_ERROR_OK;
}

int wh_Server_DmaRegisterCb64(whServerContext* server, whDmaClientMem64Cb cb)
int wh_Server_DmaRegisterCb64(whServerContext* server, whServerDmaClientMem64Cb cb)
{
if (NULL == server || NULL == cb) {
return WH_ERROR_BADARGS;
Expand All @@ -71,7 +71,7 @@ int wh_Server_DmaRegisterCb64(whServerContext* server, whDmaClientMem64Cb cb)
}

int wh_Server_DmaRegisterAllowList(whServerContext* server,
const whDmaAddrAllowList* allowlist)
const whServerDmaAddrAllowList* allowlist)
{
if (NULL == server || NULL == allowlist) {
return WH_ERROR_BADARGS;
Expand All @@ -86,7 +86,7 @@ int wh_Server_DmaRegisterAllowList(whServerContext* server,
int wh_Server_DmaProcessClientAddress32(whServerContext* server,
uint32_t clientAddr,
void** xformedCliAddr, uint32_t len,
whDmaOper oper, whDmaFlags flags)
whServerDmaOper oper, whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down Expand Up @@ -117,7 +117,7 @@ int wh_Server_DmaProcessClientAddress32(whServerContext* server,
int wh_Server_DmaProcessClientAddress64(whServerContext* server,
uint64_t clientAddr,
void** xformedCliAddr, uint64_t len,
whDmaOper oper, whDmaFlags flags)
whServerDmaOper oper, whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down Expand Up @@ -146,7 +146,7 @@ int wh_Server_DmaProcessClientAddress64(whServerContext* server,

int whServerDma_CopyFromClient32(struct whServerContext_t* server,
void* serverPtr, uint32_t clientAddr,
size_t len, whDmaFlags flags)
size_t len, whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down Expand Up @@ -187,7 +187,7 @@ int whServerDma_CopyFromClient32(struct whServerContext_t* server,

int whServerDma_CopyFromClient64(struct whServerContext_t* server,
void* serverPtr, uint64_t clientAddr,
size_t len, whDmaFlags flags)
size_t len, whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down Expand Up @@ -227,7 +227,7 @@ int whServerDma_CopyFromClient64(struct whServerContext_t* server,

int whServerDma_CopyToClient32(struct whServerContext_t* server,
uint32_t clientAddr, void* serverPtr, size_t len,
whDmaFlags flags)
whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down Expand Up @@ -268,7 +268,7 @@ int whServerDma_CopyToClient32(struct whServerContext_t* server,

int whServerDma_CopyToClient64(struct whServerContext_t* server,
uint64_t clientAddr, void* serverPtr, size_t len,
whDmaFlags flags)
whServerDmaFlags flags)
{
int rc = WH_ERROR_OK;

Expand Down
24 changes: 12 additions & 12 deletions src/wh_server_nvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata),
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjDma32;
}

resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjDma32;
}
Expand All @@ -289,14 +289,14 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata),
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjDma32;
}

resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
} else {
/* Request is malformed */
resp.rc = WH_ERROR_ABORTED;
Expand All @@ -322,7 +322,7 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespReadDma32;
}
Expand All @@ -337,7 +337,7 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress32(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_WRITE_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0});
} else {
/* Request is malformed */
resp.rc = WH_ERROR_ABORTED;
Expand All @@ -364,14 +364,14 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata),
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjectDma64;
}

resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjectDma64;
}
Expand All @@ -388,14 +388,14 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata),
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespAddObjectDma64;
}

resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
} else {
/* Request is malformed */
resp.rc = WH_ERROR_ABORTED;
Expand All @@ -421,7 +421,7 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0});
if (resp.rc != WH_ERROR_OK) {
goto transRespReadDma64;
}
Expand All @@ -436,7 +436,7 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
/* perform platform-specific host address processing */
resp.rc = wh_Server_DmaProcessClientAddress64(
server, req.data_hostaddr, &data, req.data_len,
WH_DMA_OPER_CLIENT_WRITE_POST, (whDmaFlags){0});
WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0});
} else {
/* Request is malformed */
resp.rc = WH_ERROR_ABORTED;
Expand Down
26 changes: 13 additions & 13 deletions test/wh_test_clientserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ static int _testCallbacks(whServerContext* server, whClientContext* client)

static int _customServerDmaCb(struct whServerContext_t* server,
void* clientAddr, void** serverPtr,
uint32_t len, whDmaOper oper,
whDmaFlags flags)
uint32_t len, whServerDmaOper oper,
whServerDmaFlags flags)
{
/* remapped "client" address, a.k.a. arbitary "server" buffer */
void *srvTmpBuf = (void*)((uintptr_t)clientAddr +
Expand Down Expand Up @@ -196,15 +196,15 @@ static int _customServerDmaCb(struct whServerContext_t* server,

static int _customServerDma32Cb(struct whServerContext_t* server,
uint32_t clientAddr, void** serverPtr,
uint32_t len, whDmaOper oper, whDmaFlags flags)
uint32_t len, whServerDmaOper oper, whServerDmaFlags flags)
{
return _customServerDmaCb(server, (void*)((uintptr_t)clientAddr), serverPtr,
len, oper, flags);
}

static int _customServerDma64Cb(struct whServerContext_t* server,
uint64_t clientAddr, void** serverPtr,
uint64_t len, whDmaOper oper, whDmaFlags flags)
uint64_t len, whServerDmaOper oper, whServerDmaFlags flags)
{
return _customServerDmaCb(server, (void*)((uintptr_t)clientAddr), serverPtr,
len, oper, flags);
Expand All @@ -215,7 +215,7 @@ static int _testDma(whServerContext* server, whClientContext* client)
int rc = 0;
TestMemory testMem = {0};

const whDmaAddrAllowList allowList = {
const whServerDmaAddrAllowList allowList = {
.readList =
{
{&testMem.srvBufAllow, sizeof(testMem.srvBufAllow)},
Expand Down Expand Up @@ -243,13 +243,13 @@ static int _testDma(whServerContext* server, whClientContext* client)
/* 64-bit host system */
WH_TEST_RETURN_ON_FAIL(whServerDma_CopyFromClient64(
server, testMem.srvBufAllow, (uint64_t)((uintptr_t)testMem.cliBuf),
sizeof(testMem.cliBuf), (whDmaFlags){0}));
sizeof(testMem.cliBuf), (whServerDmaFlags){0}));
}
else if (sizeof(void*) == sizeof(uint32_t)) {
/* 32-bit host system */
WH_TEST_RETURN_ON_FAIL(whServerDma_CopyFromClient32(
server, testMem.srvBufAllow, (uint32_t)((uintptr_t)testMem.cliBuf),
sizeof(testMem.cliBuf), (whDmaFlags){0}));
sizeof(testMem.cliBuf), (whServerDmaFlags){0}));
}

/* Ensure data was copied */
Expand All @@ -273,13 +273,13 @@ static int _testDma(whServerContext* server, whClientContext* client)
/* 64-bit host system */
WH_TEST_RETURN_ON_FAIL(whServerDma_CopyToClient64(
server, (uint64_t)((uintptr_t)testMem.cliBuf), testMem.srvBufAllow,
sizeof(testMem.srvBufAllow), (whDmaFlags){0}));
sizeof(testMem.srvBufAllow), (whServerDmaFlags){0}));
}
else if (sizeof(void*) == sizeof(uint32_t)) {
/* 32-bit host system */
WH_TEST_RETURN_ON_FAIL(whServerDma_CopyToClient32(
server, (uint32_t)((uintptr_t)testMem.cliBuf), testMem.srvBufAllow,
sizeof(testMem.srvBufAllow), (whDmaFlags){0}));
sizeof(testMem.srvBufAllow), (whServerDmaFlags){0}));
}

/* Ensure data was copied */
Expand All @@ -293,25 +293,25 @@ static int _testDma(whServerContext* server, whClientContext* client)
whServerDma_CopyFromClient64(
server, testMem.srvBufDeny,
(uint64_t)((uintptr_t)testMem.cliBuf),
sizeof(testMem.cliBuf), (whDmaFlags){0}));
sizeof(testMem.cliBuf), (whServerDmaFlags){0}));
WH_TEST_ASSERT_RETURN(WH_ERROR_ACCESS ==
whServerDma_CopyToClient64(
server, (uint64_t)((uintptr_t)testMem.cliBuf),
testMem.srvBufDeny,
sizeof(testMem.srvBufDeny), (whDmaFlags){0}));
sizeof(testMem.srvBufDeny), (whServerDmaFlags){0}));
}
else if (sizeof(void*) == sizeof(uint32_t)) {
/* 32-bit host system */
WH_TEST_ASSERT_RETURN(WH_ERROR_ACCESS ==
whServerDma_CopyFromClient32(
server, testMem.srvBufDeny,
(uint32_t)((uintptr_t)testMem.cliBuf),
sizeof(testMem.cliBuf), (whDmaFlags){0}));
sizeof(testMem.cliBuf), (whServerDmaFlags){0}));
WH_TEST_ASSERT_RETURN(WH_ERROR_ACCESS ==
whServerDma_CopyToClient32(
server, (uint32_t)((uintptr_t)testMem.cliBuf),
testMem.srvBufDeny,
sizeof(testMem.srvBufDeny), (whDmaFlags){0}));
sizeof(testMem.srvBufDeny), (whServerDmaFlags){0}));
}

return rc;
Expand Down
20 changes: 2 additions & 18 deletions wolfhsm/wh_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef struct whServerContext_t {
crypto_context* crypto;
CacheSlot cache[WOLFHSM_NUM_RAMKEYS];
whServerCustomCb customHandlerTable[WH_CUSTOM_CB_NUM_CALLBACKS];
whDmaContext dma;
whServerDmaContext dma;
} whServerContext;

typedef struct whServerConfig_t {
Expand All @@ -63,7 +63,7 @@ typedef struct whServerConfig_t {
#if defined WOLF_CRYPTO_CB /* TODO: should we be relying on wolfSSL defines? */
int devId;
#endif
whDmaConfig* dmaConfig;
whServerDmaConfig* dmaConfig;
} whServerConfig;

/* Initialize the comms and crypto cache components.
Expand All @@ -90,20 +90,4 @@ int wh_Server_HandleCustomCbRequest(whServerContext* server, uint16_t magic,
uint16_t req_size, const void* req_packet,
uint16_t* out_resp_size, void* resp_packet);

/* Registers custom client DMA callbacs to handle platform specific restrictions
* on accessing the client address space such as caching and address translation */
int wh_Server_DmaRegisterCb32(whServerContext* server, whDmaClientMem32Cb cb);
int wh_Server_DmaRegisterCb64(whServerContext* server, whDmaClientMem64Cb cb);
int wh_Server_DmaRegisterAllowList(whServerContext* server, const whDmaAddrAllowList* allowlist);

/* Helper functions to invoke user supplied client address DMA callbacks */
int wh_Server_DmaProcessClientAddress32(whServerContext* server,
uint32_t clientAddr, void** serverPtr,
uint32_t len, whDmaOper oper,
whDmaFlags flags);
int wh_Server_DmaProcessClientAddress64(whServerContext* server,
uint64_t clientAddr, void** serverPtr,
uint64_t len, whDmaOper oper,
whDmaFlags flags);

#endif /* WOLFHSM_WH_SERVER_H_ */
Loading

0 comments on commit 192fa01

Please sign in to comment.