fix for memory management with mmap function calls

This commit is contained in:
JacobBarthelmeh 2021-03-25 01:32:37 +07:00
parent 13d81f1fb9
commit e074513d38
4 changed files with 77 additions and 57 deletions

View File

@ -94,6 +94,9 @@ int main(int argc, char* argv[])
if (virtual_base == (uintptr_t)MAP_FAILED) {
perror("mmap_device_io failed for base address ");
}
else {
munmap_device_io(virtual_base, 0x00010000);
}
wolfCrypt_Init();

View File

@ -335,13 +335,10 @@ static Error caamCreatePartition(unsigned int page, unsigned int par,
}
/* return a mapped address to the partition on success, returns 0 on fail */
CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int* phys,
unsigned int flag)
/* return a partitions physical address on success, returns 0 on fail */
CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int flag)
{
int err;
CAAM_ADDRESS vaddr;
unsigned int local;
(void)flag; /* flag is for future changes to flag passed when creating */
@ -353,13 +350,7 @@ CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int* phys,
return 0;
}
/* map secure partition to virtual address */
local = (CAAM_PAGE + (part << 12));
vaddr = CAAM_ADR_TO_VIRTUAL(local, partSz);
if (phys != NULL) {
*phys = local;
}
return vaddr;
return (CAAM_ADDRESS)(CAAM_PAGE + (part << 12));
}
@ -839,11 +830,19 @@ int caamAesCmac(DESCSTRUCT* desc, int sz, unsigned int args[4])
}
#endif
CAAM_ADR_UNMAP(vaddr[0], desc->buf[1].data, desc->buf[1].dataSz, 1);
CAAM_ADR_UNMAP(vaddr[1], desc->buf[0].data, desc->buf[0].dataSz + macSz, 0);
for (vidx = 2, i = 2; i < sz; i = i + 1) { /* unmap the input buffers */
CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[i].data, desc->buf[i].dataSz, 0);
vidx = 0;
CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[1].data, desc->buf[1].dataSz, 1);
CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[0].data, desc->buf[0].dataSz + macSz, 0);
if (sz == 2) {
CAAM_ADR_UNMAP(vaddr[vidx], 0, 0, 0);
}
else {
for (i = 2; i < sz; i = i + 1) { /* unmap the input buffers */
CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[i].data,
desc->buf[i].dataSz, 0);
}
}
return err;
}
@ -888,8 +887,7 @@ int caamECDSAMake(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
/* map secure partition to virtual address */
phys = (CAAM_PAGE + (part << 12));
buf[0].TheAddress = CAAM_ADR_TO_VIRTUAL(phys,
buf[0].Length + buf[1].Length + BLACK_KEY_MAC_SZ);
buf[0].TheAddress = phys;
desc->desc[desc->idx++] = phys;
/* public x,y out */
@ -979,8 +977,7 @@ int caamECDSAVerify(DESCSTRUCT* desc, CAAM_BUFFER* buf, int sz,
vidx = vidx + 1;
}
else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data,
desc->buf[i].dataSz);
desc->desc[desc->idx++] = desc->buf[i].data;
}
i = i + 1;
@ -1089,8 +1086,7 @@ int caamECDSASign(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++;
}
else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data,
desc->buf[i].dataSz);
desc->desc[desc->idx++] = desc->buf[i].data;
}
i++;
@ -1162,9 +1158,9 @@ int caamECDSA_ECDH(DESCSTRUCT* desc, int sz, unsigned int args[4])
void* vaddr[sz];
if (args != NULL) {
isBlackKey = args[0];
isBlackKey = args[0];
peerBlackKey = args[1];
pdECDSEL = args[2];
pdECDSEL = args[2];
}
if (pdECDSEL == 0) {
@ -1193,8 +1189,7 @@ int caamECDSA_ECDH(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++;
}
else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data,
desc->buf[i].dataSz);
desc->desc[desc->idx++] = desc->buf[i].data;
}
i++;
@ -1216,8 +1211,7 @@ int caamECDSA_ECDH(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++;
}
else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data,
desc->buf[i].dataSz);
desc->desc[desc->idx++] = desc->buf[i].data;
}
i++;
@ -1507,9 +1501,10 @@ int InitCAAM(void)
}
int caamJobRingFree()
int CleanupCAAM()
{
CAAM_FREE_MUTEX(&caam.ring.jr_lock);
CAAM_UNSET_BASEADDR();
caamFreeAllPart();
return 0;
}

View File

@ -85,6 +85,11 @@ int CAAM_SET_BASEADDR()
}
/* cleans up having set the base address */
void CAAM_UNSET_BASEADDR()
{
munmap_device_io(virtual_base, 0x00010000);
}
/* convert a virtual address to a physical address
* returns the physical address on success
@ -105,7 +110,7 @@ CAAM_ADDRESS CAAM_ADR_TO_PHYSICAL(void* in, int inSz)
if (ret != 0) {
WOLFSSL_MSG("posix offset failed");
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
perror("");
perror("posix offset failed : ");
#endif
}
msync(in, inSz, MS_INVALIDATE);
@ -146,7 +151,7 @@ void* CAAM_ADR_MAP(unsigned int in, int inSz, unsigned char copy)
if (vaddr == MAP_FAILED) {
WOLFSSL_MSG("Failed to map memory");
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
perror("");
perror("Failed to map memory : ");
#endif
}
else {
@ -229,10 +234,8 @@ int CAAM_ADR_SYNC(void* vaddr, int sz)
*/
static int sanityCheckPartitionAddress(CAAM_ADDRESS partAddr, int partSz)
{
unsigned int phys;
phys = CAAM_ADR_TO_PHYSICAL((void*)partAddr, partSz);
if (phys < CAAM_PAGE || (phys + partSz) > CAAM_PAGE*7) {
if (partAddr < CAAM_PAGE || partAddr > CAAM_PAGE * MAX_PART ||
partSz > 4096) {
WOLFSSL_MSG("error in physical address range");
return -1;
}
@ -924,7 +927,11 @@ static int doGET_PART(resmgr_context_t *ctp, io_devctl_t *msg,
partNumber = args[0];
partSz = args[1];
partAddr = caamGetPartition(partNumber, partSz, NULL, 0);
partAddr = caamGetPartition(partNumber, partSz, 0);
if (partAddr == 0) {
return EBADMSG;
}
SETIOV(&out_iov, &partAddr, sizeof(CAAM_ADDRESS));
resmgr_msgwritev(ctp, &out_iov, 1, sizeof(msg->o));
@ -943,6 +950,7 @@ static int doWRITE_PART(resmgr_context_t *ctp, io_devctl_t *msg,
{
int partSz, ret;
CAAM_ADDRESS partAddr;
CAAM_ADDRESS vaddr;
unsigned char *buf;
iov_t in_iov;
@ -968,7 +976,14 @@ static int doWRITE_PART(resmgr_context_t *ctp, io_devctl_t *msg,
return EBADMSG;
}
CAAM_ADR_UNMAP(buf, partAddr, partSz, 1);
vaddr = CAAM_ADR_TO_VIRTUAL(partAddr, partSz);
if (vaddr == 0) {
CAAM_ADR_UNMAP(buf, 0, partSz, 0);
return ECANCELED;
}
CAAM_ADR_UNMAP(buf, vaddr, partSz, 1);
CAAM_ADR_UNMAP((void*)vaddr, 0, partSz, 0);
return EOK;
}
@ -981,6 +996,7 @@ static int doREAD_PART(resmgr_context_t *ctp, io_devctl_t *msg,
{
int partSz;
CAAM_ADDRESS partAddr;
CAAM_ADDRESS vaddr;
unsigned char *buf;
iov_t out_iov;
@ -1002,10 +1018,18 @@ static int doREAD_PART(resmgr_context_t *ctp, io_devctl_t *msg,
if (buf == NULL) {
return ECANCELED;
}
memcpy(buf, (unsigned int*)partAddr, partSz);
vaddr = CAAM_ADR_TO_VIRTUAL(partAddr, partSz);
if (vaddr == 0) {
CAAM_ADR_UNMAP(buf, 0, partSz, 0);
return ECANCELED;
}
memcpy(buf, (unsigned char*)vaddr, partSz);
SETIOV(&out_iov, buf, partSz);
resmgr_msgwritev(ctp, &out_iov, 1, sizeof(msg->o));
CAAM_ADR_UNMAP(buf, 0, partSz, 0);
CAAM_ADR_UNMAP(buf, 0, partSz, 0);
CAAM_ADR_UNMAP((void*)vaddr, 0, partSz, 0);
return EOK;
}
@ -1249,14 +1273,14 @@ int main(int argc, char *argv[])
while (1) {
ctp = dispatch_block(ctp);
if (ctp == NULL) {
caamJobRingFree();
CleanupCAAM();
exit (1);
}
dispatch_handler (ctp);
}
pthread_mutex_destroy(&sm_mutex);
caamJobRingFree();
CleanupCAAM();
return 0;
}

View File

@ -271,6 +271,7 @@
unsigned int CAAM_READ(unsigned int reg);
void CAAM_WRITE(unsigned int reg, unsigned int in);
int CAAM_SET_BASEADDR(void);
void CAAM_UNSET_BASEADDR(void);
unsigned int CAAM_ADR_TO_PHYSICAL(void* in, int inSz);
void* CAAM_ADR_MAP(unsigned int in, int inSz, unsigned char copy);
void CAAM_ADR_UNMAP(void* vaddr, unsigned int out, int outSz,
@ -278,20 +279,18 @@ void CAAM_ADR_UNMAP(void* vaddr, unsigned int out, int outSz,
int CAAM_ADR_SYNC(void* vaddr, int sz);
CAAM_ADDRESS CAAM_ADR_TO_VIRTUAL(CAAM_ADDRESS in, int length);
#ifndef WOLFSSL_CAAM_BUFFER
#define WOLFSSL_CAAM_BUFFER
typedef struct CAAM_BUFFER {
int BufferType;
CAAM_ADDRESS TheAddress;
int Length;
} CAAM_BUFFER;
#endif
unsigned int caamReadRegister(unsigned int reg);
void caamWriteRegister(unsigned int reg, unsigned int in);
int SynchronousSendRequest(int type, unsigned int args[4], CAAM_BUFFER *buf, int sz);
int caamJobRingFree(void);
#ifndef WOLFSSL_CAAM_BUFFER
#define WOLFSSL_CAAM_BUFFER
typedef struct CAAM_BUFFER {
int BufferType;
CAAM_ADDRESS TheAddress;
int Length;
} CAAM_BUFFER;
#endif
unsigned int caamReadRegister(unsigned int reg);
void caamWriteRegister(unsigned int reg, unsigned int in);
int SynchronousSendRequest(int type, unsigned int args[4], CAAM_BUFFER *buf, int sz);
int CleanupCAAM(void);
/* Driver API that can be called by caam_<env>.c port layers */
@ -309,8 +308,7 @@ int caamECDSAMake(DESCSTRUCT *desc, CAAM_BUFFER *buf, unsigned int args[4]);
int caamAesCmac(DESCSTRUCT *desc, int sz, unsigned int args[4]);
int caamBlob(DESCSTRUCT *desc);
CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int* phys,
unsigned int flag);
CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int flag);
int caamFreePart(unsigned int part);
int caamFindUnusuedPartition(void);