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) { if (virtual_base == (uintptr_t)MAP_FAILED) {
perror("mmap_device_io failed for base address "); perror("mmap_device_io failed for base address ");
} }
else {
munmap_device_io(virtual_base, 0x00010000);
}
wolfCrypt_Init(); 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 */ /* return a partitions physical address on success, returns 0 on fail */
CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int* phys, CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int flag)
unsigned int flag)
{ {
int err; int err;
CAAM_ADDRESS vaddr;
unsigned int local;
(void)flag; /* flag is for future changes to flag passed when creating */ (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; return 0;
} }
/* map secure partition to virtual address */ return (CAAM_ADDRESS)(CAAM_PAGE + (part << 12));
local = (CAAM_PAGE + (part << 12));
vaddr = CAAM_ADR_TO_VIRTUAL(local, partSz);
if (phys != NULL) {
*phys = local;
}
return vaddr;
} }
@ -839,11 +830,19 @@ int caamAesCmac(DESCSTRUCT* desc, int sz, unsigned int args[4])
} }
#endif #endif
CAAM_ADR_UNMAP(vaddr[0], desc->buf[1].data, desc->buf[1].dataSz, 1); vidx = 0;
CAAM_ADR_UNMAP(vaddr[1], desc->buf[0].data, desc->buf[0].dataSz + macSz, 0); CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[1].data, desc->buf[1].dataSz, 1);
for (vidx = 2, i = 2; i < sz; i = i + 1) { /* unmap the input buffers */ CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[0].data, desc->buf[0].dataSz + macSz, 0);
CAAM_ADR_UNMAP(vaddr[vidx++], desc->buf[i].data, desc->buf[i].dataSz, 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; return err;
} }
@ -888,8 +887,7 @@ int caamECDSAMake(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
/* map secure partition to virtual address */ /* map secure partition to virtual address */
phys = (CAAM_PAGE + (part << 12)); phys = (CAAM_PAGE + (part << 12));
buf[0].TheAddress = CAAM_ADR_TO_VIRTUAL(phys, buf[0].TheAddress = phys;
buf[0].Length + buf[1].Length + BLACK_KEY_MAC_SZ);
desc->desc[desc->idx++] = phys; desc->desc[desc->idx++] = phys;
/* public x,y out */ /* public x,y out */
@ -979,8 +977,7 @@ int caamECDSAVerify(DESCSTRUCT* desc, CAAM_BUFFER* buf, int sz,
vidx = vidx + 1; vidx = vidx + 1;
} }
else { else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data, desc->desc[desc->idx++] = desc->buf[i].data;
desc->buf[i].dataSz);
} }
i = i + 1; i = i + 1;
@ -1089,8 +1086,7 @@ int caamECDSASign(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++; vidx++;
} }
else { else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data, desc->desc[desc->idx++] = desc->buf[i].data;
desc->buf[i].dataSz);
} }
i++; i++;
@ -1193,8 +1189,7 @@ int caamECDSA_ECDH(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++; vidx++;
} }
else { else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data, desc->desc[desc->idx++] = desc->buf[i].data;
desc->buf[i].dataSz);
} }
i++; i++;
@ -1216,8 +1211,7 @@ int caamECDSA_ECDH(DESCSTRUCT* desc, int sz, unsigned int args[4])
vidx++; vidx++;
} }
else { else {
desc->desc[desc->idx++] = CAAM_ADR_TO_PHYSICAL((void*)desc->buf[i].data, desc->desc[desc->idx++] = desc->buf[i].data;
desc->buf[i].dataSz);
} }
i++; i++;
@ -1507,9 +1501,10 @@ int InitCAAM(void)
} }
int caamJobRingFree() int CleanupCAAM()
{ {
CAAM_FREE_MUTEX(&caam.ring.jr_lock); CAAM_FREE_MUTEX(&caam.ring.jr_lock);
CAAM_UNSET_BASEADDR();
caamFreeAllPart(); caamFreeAllPart();
return 0; 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 /* convert a virtual address to a physical address
* returns the physical address on success * returns the physical address on success
@ -105,7 +110,7 @@ CAAM_ADDRESS CAAM_ADR_TO_PHYSICAL(void* in, int inSz)
if (ret != 0) { if (ret != 0) {
WOLFSSL_MSG("posix offset failed"); WOLFSSL_MSG("posix offset failed");
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT) #if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
perror(""); perror("posix offset failed : ");
#endif #endif
} }
msync(in, inSz, MS_INVALIDATE); 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) { if (vaddr == MAP_FAILED) {
WOLFSSL_MSG("Failed to map memory"); WOLFSSL_MSG("Failed to map memory");
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT) #if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
perror(""); perror("Failed to map memory : ");
#endif #endif
} }
else { else {
@ -229,10 +234,8 @@ int CAAM_ADR_SYNC(void* vaddr, int sz)
*/ */
static int sanityCheckPartitionAddress(CAAM_ADDRESS partAddr, int partSz) static int sanityCheckPartitionAddress(CAAM_ADDRESS partAddr, int partSz)
{ {
unsigned int phys; if (partAddr < CAAM_PAGE || partAddr > CAAM_PAGE * MAX_PART ||
partSz > 4096) {
phys = CAAM_ADR_TO_PHYSICAL((void*)partAddr, partSz);
if (phys < CAAM_PAGE || (phys + partSz) > CAAM_PAGE*7) {
WOLFSSL_MSG("error in physical address range"); WOLFSSL_MSG("error in physical address range");
return -1; return -1;
} }
@ -924,7 +927,11 @@ static int doGET_PART(resmgr_context_t *ctp, io_devctl_t *msg,
partNumber = args[0]; partNumber = args[0];
partSz = args[1]; 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)); SETIOV(&out_iov, &partAddr, sizeof(CAAM_ADDRESS));
resmgr_msgwritev(ctp, &out_iov, 1, sizeof(msg->o)); 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; int partSz, ret;
CAAM_ADDRESS partAddr; CAAM_ADDRESS partAddr;
CAAM_ADDRESS vaddr;
unsigned char *buf; unsigned char *buf;
iov_t in_iov; iov_t in_iov;
@ -968,7 +976,14 @@ static int doWRITE_PART(resmgr_context_t *ctp, io_devctl_t *msg,
return EBADMSG; 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; return EOK;
} }
@ -981,6 +996,7 @@ static int doREAD_PART(resmgr_context_t *ctp, io_devctl_t *msg,
{ {
int partSz; int partSz;
CAAM_ADDRESS partAddr; CAAM_ADDRESS partAddr;
CAAM_ADDRESS vaddr;
unsigned char *buf; unsigned char *buf;
iov_t out_iov; iov_t out_iov;
@ -1002,10 +1018,18 @@ static int doREAD_PART(resmgr_context_t *ctp, io_devctl_t *msg,
if (buf == NULL) { if (buf == NULL) {
return ECANCELED; 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); SETIOV(&out_iov, buf, partSz);
resmgr_msgwritev(ctp, &out_iov, 1, sizeof(msg->o)); 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; return EOK;
} }
@ -1249,14 +1273,14 @@ int main(int argc, char *argv[])
while (1) { while (1) {
ctp = dispatch_block(ctp); ctp = dispatch_block(ctp);
if (ctp == NULL) { if (ctp == NULL) {
caamJobRingFree(); CleanupCAAM();
exit (1); exit (1);
} }
dispatch_handler (ctp); dispatch_handler (ctp);
} }
pthread_mutex_destroy(&sm_mutex); pthread_mutex_destroy(&sm_mutex);
caamJobRingFree(); CleanupCAAM();
return 0; return 0;
} }

View File

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