sim/mem: don't let siwtch out when operated the host mem

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2022-12-21 18:41:52 +08:00 committed by Xiang Xiao
parent c08cc01c9d
commit fedad91b0d

View File

@ -51,6 +51,9 @@ static atomic_int g_uordblks;
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
extern uint64_t up_irq_save(void);
extern void up_irq_restore(uint64_t flags);
/**************************************************************************** /****************************************************************************
* Name: host_allocheap * Name: host_allocheap
* *
@ -61,6 +64,7 @@ static atomic_int g_uordblks;
void *host_allocheap(size_t sz) void *host_allocheap(size_t sz)
{ {
uint64_t flags = up_irq_save();
void *p; void *p;
#if defined(CONFIG_HOST_MACOS) && defined(CONFIG_HOST_ARM64) #if defined(CONFIG_HOST_MACOS) && defined(CONFIG_HOST_ARM64)
@ -73,6 +77,8 @@ void *host_allocheap(size_t sz)
MAP_ANON | MAP_PRIVATE, -1, 0); MAP_ANON | MAP_PRIVATE, -1, 0);
#endif #endif
up_irq_restore(flags);
if (p == MAP_FAILED) if (p == MAP_FAILED)
{ {
return NULL; return NULL;
@ -83,6 +89,7 @@ void *host_allocheap(size_t sz)
void *host_allocshmem(const char *name, size_t size, int master) void *host_allocshmem(const char *name, size_t size, int master)
{ {
uint64_t flags = up_irq_save();
void *mem; void *mem;
int oflag; int oflag;
int ret; int ret;
@ -97,6 +104,7 @@ void *host_allocshmem(const char *name, size_t size, int master)
fd = shm_open(name, oflag, S_IRUSR | S_IWUSR); fd = shm_open(name, oflag, S_IRUSR | S_IWUSR);
if (fd < 0) if (fd < 0)
{ {
up_irq_restore(flags);
return NULL; return NULL;
} }
@ -110,12 +118,14 @@ void *host_allocshmem(const char *name, size_t size, int master)
ret = ftruncate(fd, size); ret = ftruncate(fd, size);
if (ret < 0) if (ret < 0)
{ {
up_irq_restore(flags);
close(fd); close(fd);
return NULL; return NULL;
} }
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); /* Don't need keep fd any more once the memory get mapped */ close(fd); /* Don't need keep fd any more once the memory get mapped */
up_irq_restore(flags);
if (mem == MAP_FAILED) if (mem == MAP_FAILED)
{ {
return NULL; return NULL;
@ -126,7 +136,9 @@ void *host_allocshmem(const char *name, size_t size, int master)
void host_freeshmem(void *mem) void host_freeshmem(void *mem)
{ {
uint64_t flags = up_irq_save();
munmap(mem, 0); munmap(mem, 0);
up_irq_restore(flags);
} }
size_t host_mallocsize(void *mem) size_t host_mallocsize(void *mem)
@ -140,12 +152,14 @@ size_t host_mallocsize(void *mem)
void *host_memalign(size_t alignment, size_t size) void *host_memalign(size_t alignment, size_t size)
{ {
uint64_t flags = up_irq_save();
void *p; void *p;
int error; int error;
error = posix_memalign(&p, alignment, size); error = posix_memalign(&p, alignment, size);
if (error != 0) if (error != 0)
{ {
up_irq_restore(flags);
return NULL; return NULL;
} }
@ -153,26 +167,32 @@ void *host_memalign(size_t alignment, size_t size)
g_aordblks += 1; g_aordblks += 1;
g_uordblks += size; g_uordblks += size;
up_irq_restore(flags);
return p; return p;
} }
void host_free(void *mem) void host_free(void *mem)
{ {
size_t size; size_t size;
uint64_t flags;
if (mem == NULL) if (mem == NULL)
{ {
return; return;
} }
flags = up_irq_save();
size = host_mallocsize(mem); size = host_mallocsize(mem);
g_aordblks -= 1; g_aordblks -= 1;
g_uordblks -= size; g_uordblks -= size;
free(mem); free(mem);
up_irq_restore(flags);
} }
void *host_realloc(void *oldmem, size_t size) void *host_realloc(void *oldmem, size_t size)
{ {
uint64_t flags;
size_t oldsize; size_t oldsize;
void *mem; void *mem;
@ -186,10 +206,13 @@ void *host_realloc(void *oldmem, size_t size)
return host_memalign(sizeof(void *), size); return host_memalign(sizeof(void *), size);
} }
flags = up_irq_save();
oldsize = host_mallocsize(oldmem); oldsize = host_mallocsize(oldmem);
mem = realloc(oldmem, size); mem = realloc(oldmem, size);
if (mem == NULL) if (mem == NULL)
{ {
up_irq_restore(flags);
return NULL; return NULL;
} }
@ -197,6 +220,8 @@ void *host_realloc(void *oldmem, size_t size)
g_uordblks -= oldsize; g_uordblks -= oldsize;
g_uordblks += size; g_uordblks += size;
up_irq_restore(flags);
return mem; return mem;
} }