aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorswapnili <swapnil.ingle@nutanix.com>2021-01-08 15:30:50 +0100
committerGitHub <noreply@github.com>2021-01-08 15:30:50 +0100
commitd211f2cf4920e7f082bb5e707c672cecb6f52a6c (patch)
treeb9fb13e4fe3e6cec4d44095109d666c431a9878a /lib
parent6ec31642f6253f5c19187c1ffb396d5921138b67 (diff)
downloadlibvfio-user-d211f2cf4920e7f082bb5e707c672cecb6f52a6c.zip
libvfio-user-d211f2cf4920e7f082bb5e707c672cecb6f52a6c.tar.gz
libvfio-user-d211f2cf4920e7f082bb5e707c672cecb6f52a6c.tar.bz2
Use prot flags sent by client to map dma regions (#227)
* Use prot flags sent by client to map dma regions Signed-off-by: Swapnil Ingle <swapnil.ingle@nutanix.com> Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/dma.c15
-rw-r--r--lib/dma.h5
-rw-r--r--lib/libvfio-user.c7
3 files changed, 19 insertions, 8 deletions
diff --git a/lib/dma.c b/lib/dma.c
index 69b3fe5..59d85eb 100644
--- a/lib/dma.c
+++ b/lib/dma.c
@@ -215,7 +215,7 @@ dma_controller_destroy(dma_controller_t *dma)
int
dma_controller_add_region(dma_controller_t *dma,
dma_addr_t dma_addr, size_t size,
- int fd, off_t offset)
+ int fd, off_t offset, uint32_t prot)
{
int idx;
dma_memory_region_t *region;
@@ -243,7 +243,13 @@ dma_controller_add_region(dma_controller_t *dma,
*/
vfu_log(dma->vfu_ctx, LOG_ERR,
"bad fd=%d for new DMA region %#lx-%#lx, existing fd=%d\n",
- fd, offset, offset + size, region->fd);
+ fd, offset, offset + size - 1, region->fd);
+ goto err;
+ }
+ if (region->prot != prot) {
+ vfu_log(dma->vfu_ctx, LOG_ERR, "bad prot=%#x "
+ "for new DMA region %#lx-%#lx, existing prot=%#x\n",
+ prot, offset, offset + size - 1, region->prot);
goto err;
}
return idx;
@@ -284,12 +290,13 @@ dma_controller_add_region(dma_controller_t *dma,
region->size = size;
region->page_size = page_size;
region->offset = offset;
+ region->prot = prot;
region->fd = fd;
region->refcnt = 0;
if (fd != -1) {
- region->virt_addr = dma_map_region(region, PROT_READ | PROT_WRITE,
- 0, region->size);
+ region->virt_addr = dma_map_region(region, region->prot, 0,
+ region->size);
if (region->virt_addr == MAP_FAILED) {
vfu_log(dma->vfu_ctx, LOG_ERR,
"failed to memory map DMA region %#lx-%#lx: %s\n",
diff --git a/lib/dma.h b/lib/dma.h
index 7bab3da..d8ff38c 100644
--- a/lib/dma.h
+++ b/lib/dma.h
@@ -83,6 +83,9 @@ struct vfu_ctx;
typedef struct {
dma_addr_t dma_addr; // DMA address of this region
+ uint32_t prot; // memory protection of the mapping
+ // defined in sys/mman.h
+
size_t size; // Size of this region
int fd; // File descriptor to mmap
int page_size; // Page size of this fd
@@ -116,7 +119,7 @@ dma_controller_destroy(dma_controller_t *dma);
int
dma_controller_add_region(dma_controller_t *dma,
dma_addr_t dma_addr, size_t size,
- int fd, off_t offset);
+ int fd, off_t offset, uint32_t prot);
int
dma_controller_remove_region(dma_controller_t *dma,
diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c
index c0503ff..2b95b43 100644
--- a/lib/libvfio-user.c
+++ b/lib/libvfio-user.c
@@ -519,7 +519,8 @@ handle_dma_map_or_unmap(vfu_ctx_t *vfu_ctx, uint32_t size, bool map,
dma_regions[i].addr,
dma_regions[i].size,
fd,
- dma_regions[i].offset);
+ dma_regions[i].offset,
+ dma_regions[i].prot);
if (ret < 0) {
if (fd != -1) {
close(fd);
@@ -534,10 +535,10 @@ handle_dma_map_or_unmap(vfu_ctx_t *vfu_ctx, uint32_t size, bool map,
}
ret = 0;
vfu_log(vfu_ctx, LOG_DEBUG,
- "added DMA region %#lx-%#lx offset=%#lx fd=%d",
+ "added DMA region %#lx-%#lx offset=%#lx fd=%d prot=%#x",
dma_regions[i].addr,
dma_regions[i].addr + dma_regions[i].size - 1,
- dma_regions[i].offset, fd);
+ dma_regions[i].offset, fd, dma_regions[i].prot);
} else {
ret = dma_controller_remove_region(vfu_ctx->dma,
dma_regions[i].addr,