On 27.05.20 14:32, [email protected] wrote: > From: Nikhil Devshatwar <[email protected]> > > ivshmem protocol does not describe a fixed size for the > rw, input and output regions. For each platform, the uio > driver will populate this information in the sysfw. > > Extract the size from sysfs maps entries and use it for > mapping different regions. > This will make the demo generic such that it will work on > all platforms with different sizes for ivshmem.
The original plan was to keep the demos simple, thus these hard-codings of the normally used page size. I'm fine with getpagesize but I'm not sure yet if it's worth the also read back the region size because we do not use that feature in the demos. In any case, please align all demo implementations then. Jan > > Signed-off-by: Nikhil Devshatwar <[email protected]> > --- > tools/ivshmem-demo.c | 50 ++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 41 insertions(+), 9 deletions(-) > > diff --git a/tools/ivshmem-demo.c b/tools/ivshmem-demo.c > index 8201ad15..163653e1 100644 > --- a/tools/ivshmem-demo.c > +++ b/tools/ivshmem-demo.c > @@ -17,6 +17,7 @@ > #include <signal.h> > #include <stdio.h> > #include <stdint.h> > +#include <stdlib.h> > #include <string.h> > #include <unistd.h> > #include <sys/mman.h> > @@ -44,6 +45,25 @@ static inline void mmio_write32(void *address, uint32_t > value) > *(volatile uint32_t *)address = value; > } > > +static int uio_read_mem_size(char *devpath, int idx) > +{ > + char sysfs_path[64]; > + char output[20] = ""; > + int fd, ret, size; > + > + snprintf(sysfs_path, sizeof(sysfs_path), > + "/sys/class/uio/%s/maps/map%d/size", > + basename(devpath), idx); > + fd = open(sysfs_path, O_RDONLY); > + if (fd < 0) > + return fd; > + ret = read(fd, output, sizeof(output)); > + if (ret < 0) > + return ret; > + sscanf(output, "0x%x", &size); > + return size; > +} > + > static void print_shmem(void) > { > printf("state[0] = %d\n", state[0]); > @@ -67,7 +87,9 @@ int main(int argc, char *argv[]) > sigset_t sigset; > char *path; > int has_msix; > - int ret; > + int ret, size, offset, pgsize; > + > + pgsize = getpagesize(); > > if (argc < 2) > path = strdup("/dev/uio0"); > @@ -82,29 +104,39 @@ int main(int argc, char *argv[]) > "/sys/class/uio/%s/device/msi_irqs", basename(path)); > has_msix = access(sysfs_path, R_OK) == 0; > > - regs = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, > - fds[0].fd, 0); > + offset = 0; > + size = uio_read_mem_size(path, 0); > + regs = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, > + fds[0].fd, offset); > if (regs == MAP_FAILED) > error(1, errno, "mmap(regs)"); > > id = mmio_read32(®s->id); > printf("ID = %d\n", id); > > - state = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fds[0].fd, 4096 * 1); > + offset += pgsize; > + size = uio_read_mem_size(path, 1); > + state = mmap(NULL, size, PROT_READ, MAP_SHARED, fds[0].fd, offset); > if (state == MAP_FAILED) > error(1, errno, "mmap(state)"); > > - rw = mmap(NULL, 4096 * 9, PROT_READ | PROT_WRITE, MAP_SHARED, > - fds[0].fd, 4096 * 2); > + offset += pgsize; > + size = uio_read_mem_size(path, 2); > + rw = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, > + fds[0].fd, offset); > if (rw == MAP_FAILED) > error(1, errno, "mmap(rw)"); > > - in = mmap(NULL, 4096 * 6, PROT_READ, MAP_SHARED, fds[0].fd, 4096 * 3); > + offset += pgsize; > + size = uio_read_mem_size(path, 3); > + in = mmap(NULL, size, PROT_READ, MAP_SHARED, fds[0].fd, offset); > if (in == MAP_FAILED) > error(1, errno, "mmap(in)"); > > - out = mmap(NULL, 4096 * 2, PROT_READ | PROT_WRITE, MAP_SHARED, > - fds[0].fd, 4096 * 4); > + offset += pgsize; > + size = uio_read_mem_size(path, 4); > + out = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, > + fds[0].fd, offset); > if (out == MAP_FAILED) > error(1, errno, "mmap(out)"); > > -- Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux -- You received this message because you are subscribed to the Google Groups "Jailhouse" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jailhouse-dev/4063dea0-f02c-db59-dda2-1212e49bc6e2%40siemens.com.
