Module: xenomai-2.5
Branch: master
Commit: bcd7078ddc7ac3ece81bf45279912691932b7205
URL:    
http://git.xenomai.org/?p=xenomai-2.5.git;a=commit;h=bcd7078ddc7ac3ece81bf45279912691932b7205

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sun Jun 13 17:27:06 2010 +0200

skins: sanitize heap binding

---

 include/nucleus/heap.h      |    5 +++
 ksrc/nucleus/shadow.c       |   16 +++------
 src/skins/common/sem_heap.c |   76 +++++++++++++++++++++++--------------------
 src/skins/native/heap.c     |   38 ++++-----------------
 src/skins/native/queue.c    |   38 ++++-----------------
 src/skins/psos+/rn.c        |   36 +++-----------------
 src/skins/rtai/shm.c        |   61 +++-------------------------------
 src/skins/vrtx/heap.c       |   36 +++-----------------
 src/skins/vrtx/pt.c         |   36 +++-----------------
 9 files changed, 90 insertions(+), 252 deletions(-)

diff --git a/include/nucleus/heap.h b/include/nucleus/heap.h
index fecdb79..f4ebe11 100644
--- a/include/nucleus/heap.h
+++ b/include/nucleus/heap.h
@@ -279,4 +279,9 @@ int xnheap_check_block(xnheap_t *heap,
 
 #define XNHEAP_DEV_NAME  "/dev/rtheap"
 
+struct xnheap_desc {
+       unsigned long handle;
+       unsigned int size;
+};
+
 #endif /* !_XENO_NUCLEUS_HEAP_H */
diff --git a/ksrc/nucleus/shadow.c b/ksrc/nucleus/shadow.c
index 1a32527..cc313cf 100644
--- a/ksrc/nucleus/shadow.c
+++ b/ksrc/nucleus/shadow.c
@@ -702,8 +702,7 @@ void __init xnheap_init_vdso(void)
 {
        nkvdso = (struct xnvdso *)
                xnheap_alloc(&__xnsys_global_ppd.sem_heap, sizeof(*nkvdso));
-
-       if (!nkvdso)
+       if (nkvdso == NULL)
                xnpod_fatal("Xenomai: cannot allocate memory for xnvdso!\n");
 
        nkvdso->features = XNVDSO_FEATURES;
@@ -1891,20 +1890,15 @@ static int xnshadow_sys_trace(struct pt_regs *regs)
        return err;
 }
 
-struct heap_info {
-       xnheap_t *addr;
-       unsigned size;
-};
-
 static int xnshadow_sys_sem_heap(struct pt_regs *regs)
 {
-       struct heap_info hinfo, __user *us_hinfo;
+       struct xnheap_desc hinfo, __user *us_hinfo;
        unsigned global;
 
        global = __xn_reg_arg2(regs);
-       us_hinfo = (struct heap_info __user *) __xn_reg_arg1(regs);
-       hinfo.addr = &xnsys_ppd_get(global)->sem_heap;
-       hinfo.size = xnheap_extentsize(hinfo.addr);
+       us_hinfo = (struct xnheap_desc __user *) __xn_reg_arg1(regs);
+       hinfo.handle = (unsigned long)&xnsys_ppd_get(global)->sem_heap;
+       hinfo.size = xnheap_extentsize(&xnsys_ppd_get(global)->sem_heap);
 
        return __xn_safe_copy_to_user(us_hinfo, &hinfo, sizeof(*us_hinfo));
 }
diff --git a/src/skins/common/sem_heap.c b/src/skins/common/sem_heap.c
index bf7031d..acb655b 100644
--- a/src/skins/common/sem_heap.c
+++ b/src/skins/common/sem_heap.c
@@ -11,62 +11,68 @@
 #include <sys/mman.h>
 
 #include <nucleus/vdso.h>
+#include <nucleus/heap.h>
 #include <asm/xenomai/syscall.h>
 #include <asm-generic/bits/current.h>
-
 #include "sem_heap.h"
 
 unsigned long xeno_sem_heap[2] = { 0, 0 };
+
 struct xnvdso *nkvdso;
 
-static void *map_sem_heap(unsigned shared)
+void *xeno_map_heap(unsigned long handle, unsigned int size)
 {
-       struct heap_info {
-               void *addr;
-               unsigned size;
-       } hinfo;
-       int fd, err;
+       int fd, ret;
+       void *addr;
 
-       fd = open("/dev/rtheap", O_RDWR, 0);
+       fd = open(XNHEAP_DEV_NAME, O_RDWR, 0);
        if (fd < 0) {
-               fprintf(stderr, "Xenomai: open: %m\n");
+               perror("Xenomai: open");
                return MAP_FAILED;
        }
 
-       err = XENOMAI_SYSCALL2(__xn_sys_sem_heap, &hinfo, shared);
-       if (err < 0) {
-               fprintf(stderr, "Xenomai: sys_sem_heap: %m\n");
+       ret = ioctl(fd, 0, handle);
+       if (ret) {
+               perror("Xenomai: ioctl");
                return MAP_FAILED;
        }
 
-       err = ioctl(fd, 0, hinfo.addr);
-       if (err < 0) {
-               fprintf(stderr, "Xenomai: ioctl: %m\n");
-               return MAP_FAILED;
-       }
+       addr = mmap(NULL, size, PROT_READ|PROT_WRITE,
+                   MAP_SHARED, fd, 0L);
 
-       hinfo.addr = mmap(NULL, hinfo.size,
-                         PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        close(fd);
 
-       return hinfo.addr;
+       return addr;
 }
 
-static void unmap_sem_heap(unsigned long heap_addr, unsigned shared)
+static void *map_sem_heap(unsigned int shared)
 {
-       struct heap_info {
-               void *addr;
-               unsigned size;
-       } hinfo;
-       int err;
+       struct xnheap_desc hinfo;
+       int ret;
 
-       err = XENOMAI_SYSCALL2(__xn_sys_sem_heap, &hinfo, shared);
-       if (err < 0) {
-               fprintf(stderr, "Xenomai: sys_sem_heap: %m\n");
+       ret = XENOMAI_SYSCALL2(__xn_sys_sem_heap, &hinfo, shared);
+       if (ret < 0) {
+               errno = -ret;
+               perror("Xenomai: sys_sem_heap");
+               return MAP_FAILED;
+       }
+
+       return xeno_map_heap(hinfo.handle, hinfo.size);
+}
+
+static void unmap_sem_heap(unsigned long heap_addr, unsigned int shared)
+{
+       struct xnheap_desc hinfo;
+       int ret;
+
+       ret = XENOMAI_SYSCALL2(__xn_sys_sem_heap, &hinfo, shared);
+       if (ret < 0) {
+               errno = -ret;
+               perror("Xenomai: unmap sem_heap");
                return;
        }
 
-       munmap((void *) heap_addr, hinfo.size);
+       munmap((void *)heap_addr, hinfo.size);
 }
 
 static void remap_on_fork(void)
@@ -75,7 +81,7 @@ static void remap_on_fork(void)
 
        xeno_sem_heap[0] = (unsigned long) map_sem_heap(0);
        if (xeno_sem_heap[0] == (unsigned long) MAP_FAILED) {
-               perror("Xenomai: mmap(local sem heap)");
+               perror("Xenomai: mmap local sem heap");
                exit(EXIT_FAILURE);
        }
 }
@@ -87,8 +93,8 @@ static void xeno_init_vdso(void)
 
        err = XENOMAI_SYSCALL2(__xn_sys_info, 0, &sysinfo);
        if (err < 0) {
-               fprintf(stderr, "Xenomai: sys_info failed: %s\n",
-                       strerror(-err));
+               errno = -err;
+               perror("Xenomai: sys_info failed");
                exit(EXIT_FAILURE);
        }
 
@@ -101,14 +107,14 @@ static void xeno_init_sem_heaps_inner(void)
 {
        xeno_sem_heap[0] = (unsigned long) map_sem_heap(0);
        if (xeno_sem_heap[0] == (unsigned long) MAP_FAILED) {
-               perror("Xenomai: mmap(local sem heap)");
+               perror("Xenomai: mmap local sem heap");
                exit(EXIT_FAILURE);
        }
        pthread_atfork(NULL, NULL, remap_on_fork);
 
        xeno_sem_heap[1] = (unsigned long) map_sem_heap(1);
        if (xeno_sem_heap[1] == (unsigned long) MAP_FAILED) {
-               perror("Xenomai: mmap(global sem heap)");
+               perror("Xenomai: mmap global sem heap");
                exit(EXIT_FAILURE);
        }
 
diff --git a/src/skins/native/heap.c b/src/skins/native/heap.c
index 0dfd8be..2ed929d 100644
--- a/src/skins/native/heap.c
+++ b/src/skins/native/heap.c
@@ -28,39 +28,17 @@
 
 extern int __native_muxid;
 
-static int __map_heap_memory(RT_HEAP *heap, RT_HEAP_PLACEHOLDER * php)
-{
-       int err, heapfd;
-
-       /* Open the heap device to share the heap memory with the
-          in-kernel skin and bound clients. */
-       heapfd = __real_open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return -ENOENT;
-
-       /* Bind this file instance to the shared heap. */
-       err = __real_ioctl(heapfd, 0, php->opaque2);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the heap memory into our address space. */
-       php->mapbase = (caddr_t) __real_mmap(NULL,
-                                            php->mapsize,
-                                            PROT_READ | PROT_WRITE,
-                                            MAP_SHARED, heapfd, 0L);
-       if (php->mapbase != MAP_FAILED)
-               /* Copy back a complete placeholder only if all is ok. */
-               *heap = *php;
-       else
-               err = -errno;
+void *xeno_map_heap(unsigned long handle, unsigned int size);
 
-      close_and_exit:
+static int __map_heap_memory(RT_HEAP *heap, RT_HEAP_PLACEHOLDER *php)
+{
+       php->mapbase = xeno_map_heap((unsigned long)php->opaque2, php->mapsize);
+       if (php->mapbase == MAP_FAILED)
+               return -errno;
 
-       __real_close(heapfd);
+       *heap = *php;
 
-       return err;
+       return 0;
 }
 
 int rt_heap_create(RT_HEAP *heap, const char *name, size_t heapsize, int mode)
diff --git a/src/skins/native/queue.c b/src/skins/native/queue.c
index 277dd68..abf6ab4 100644
--- a/src/skins/native/queue.c
+++ b/src/skins/native/queue.c
@@ -28,39 +28,17 @@
 
 extern int __native_muxid;
 
-static int __map_queue_memory(RT_QUEUE *q, RT_QUEUE_PLACEHOLDER * php)
-{
-       int err, heapfd;
-
-       /* Open the heap device to share the message pool memory with the
-          in-kernel skin and bound clients. */
-       heapfd = __real_open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return -ENOENT;
-
-       /* Bind this file instance to the shared heap. */
-       err = __real_ioctl(heapfd, 0, php->opaque2);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the heap memory into our address space. */
-       php->mapbase = (caddr_t) __real_mmap(NULL,
-                                            php->mapsize,
-                                            PROT_READ | PROT_WRITE,
-                                            MAP_SHARED, heapfd, 0L);
-       if (php->mapbase != MAP_FAILED)
-               /* Copy back a complete placeholder only if all is ok. */
-               *q = *php;
-       else
-               err = -errno;
+void *xeno_map_heap(unsigned long handle, unsigned int size);
 
-      close_and_exit:
+static int __map_queue_memory(RT_QUEUE *q, RT_QUEUE_PLACEHOLDER *php)
+{
+  php->mapbase = xeno_map_heap((unsigned long)php->opaque2, php->mapsize);
+       if (php->mapbase == MAP_FAILED)
+               return -errno;
 
-       __real_close(heapfd);
+       *q = *php;
 
-       return err;
+       return 0;
 }
 
 int rt_queue_create(RT_QUEUE *q,
diff --git a/src/skins/psos+/rn.c b/src/skins/psos+/rn.c
index a383b80..35736ad 100644
--- a/src/skins/psos+/rn.c
+++ b/src/skins/psos+/rn.c
@@ -34,42 +34,18 @@ struct rninfo {
        u_long mapsize;
 };
 
+void *xeno_map_heap(unsigned long handle, unsigned int size);
+
 static int __map_heap_memory(const struct rninfo *rnip)
 {
-       int err = 0, rnfd;
        caddr_t mapbase;
 
-       /* Open the heap device to share the region memory with the
-          in-kernel skin. */
-       rnfd = open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (rnfd < 0)
-               return -ENOENT;
-
-       /* Bind this file instance to the shared heap. */
-       err = ioctl(rnfd, 0, rnip->rncb);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the region memory into our address space. */
-       mapbase = (caddr_t) mmap(NULL,
-                                rnip->mapsize,
-                                PROT_READ | PROT_WRITE,
-                                MAP_SHARED, rnfd, 0L);
-
+       mapbase = xeno_map_heap((unsigned long)rnip->rncb, rnip->mapsize);
        if (mapbase == MAP_FAILED)
-               err = -ENOMEM;
-       else
-               err =
-                   XENOMAI_SKINCALL2(__psos_muxid, __psos_rn_bind, rnip->rnid,
-                                     mapbase);
-
-      close_and_exit:
-
-       close(rnfd);
+               return -errno;
 
-       return err;
+       return XENOMAI_SKINCALL2(__psos_muxid, __psos_rn_bind,
+                                rnip->rnid, mapbase);
 }
 
 u_long rn_create(const char name[4],
diff --git a/src/skins/rtai/shm.c b/src/skins/rtai/shm.c
index ecc4719..407ace4 100644
--- a/src/skins/rtai/shm.c
+++ b/src/skins/rtai/shm.c
@@ -28,70 +28,19 @@
 
 extern int __rtai_muxid;
 
+void *xeno_map_heap(unsigned long handle, unsigned int size);
+
 static void *__map_shm_heap_memory(unsigned long opaque, int mapsize)
 {
-       int err, heapfd;
-       void *mapbase = 0;
-
-       /* Open the heap device to share the heap memory with the
-          in-kernel skin and bound clients. */
-       heapfd = open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return 0;
+       void *mapbase;
 
-       /* Bind this file instance to the shared heap. */
-       err = ioctl(heapfd, 0, opaque);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the heap memory into our address space. */
-       mapbase = (caddr_t) mmap(NULL,
-                                mapsize,
-                                PROT_READ | PROT_WRITE,
-                                MAP_SHARED, heapfd, 0L);
+       mapbase = xeno_map_heap(opaque, mapsize);
        if (mapbase == MAP_FAILED)
-               mapbase = 0;
-
-      close_and_exit:
-
-       close(heapfd);
+               return NULL;
 
        return mapbase;
 }
 
-#if 0
-
-static int __unmap_shm_heap_memory(unsigned long opaque, void *mapbase,
-                                  int mapsize)
-{
-       int err, heapfd;
-
-       /* Open the heap device to share the heap memory with the
-          in-kernel skin and bound clients. */
-       heapfd = open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return 0;
-
-       /* Bind this file instance to the shared heap. */
-       err = ioctl(heapfd, 0, opaque);
-
-       if (err)
-               goto close_and_exit;
-
-       err = munmap(mapbase, mapsize);
-
-      close_and_exit:
-
-       close(heapfd);
-
-       return err;
-}
-
-#endif
-
 static void *_compat_shm_alloc(unsigned long name, int size, int suprt,
                               int isheap)
 {
diff --git a/src/skins/vrtx/heap.c b/src/skins/vrtx/heap.c
index fa3be16..ceaa45a 100644
--- a/src/skins/vrtx/heap.c
+++ b/src/skins/vrtx/heap.c
@@ -27,42 +27,18 @@
 
 extern int __vrtx_muxid;
 
+void *xeno_map_heap(unsigned long handle, unsigned int size);
+
 static int __map_heap_memory(const vrtx_hdesc_t *hdesc)
 {
-       int err = 0, heapfd;
        caddr_t mapbase;
 
-       /* Open the heap device to share the heap memory with the
-          in-kernel skin. */
-       heapfd = open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return -ENOENT;
-
-       /* Bind this file instance to the shared heap. */
-       err = ioctl(heapfd, 0, hdesc->hcb);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the heap memory into our address space. */
-       mapbase = (caddr_t) mmap(NULL,
-                                hdesc->hsize,
-                                PROT_READ | PROT_WRITE,
-                                MAP_SHARED, heapfd, 0L);
-
+       mapbase = xeno_map_heap((unsigned long)hdesc->hcb, hdesc->hsize);
        if (mapbase == MAP_FAILED)
-               err = -ENOMEM;
-       else
-               err =
-                   XENOMAI_SKINCALL2(__vrtx_muxid, __vrtx_hbind, hdesc->hid,
-                                     mapbase);
-
-      close_and_exit:
-
-       close(heapfd);
+               return -errno;
 
-       return err;
+       return XENOMAI_SKINCALL2(__vrtx_muxid, __vrtx_hbind,
+                                hdesc->hid, mapbase);
 }
 
 int sc_hcreate(char *heapaddr,
diff --git a/src/skins/vrtx/pt.c b/src/skins/vrtx/pt.c
index 6fa0b90..1e729a9 100644
--- a/src/skins/vrtx/pt.c
+++ b/src/skins/vrtx/pt.c
@@ -27,42 +27,18 @@
 
 extern int __vrtx_muxid;
 
+void *xeno_map_heap(unsigned long handle, unsigned int size);
+
 static int __map_pt_memory(const vrtx_pdesc_t *pdesc)
 {
-       int err = 0, heapfd;
        caddr_t mapbase;
 
-       /* Open the heap device to share the partition memory with the
-          in-kernel skin. */
-       heapfd = open(XNHEAP_DEV_NAME, O_RDWR);
-
-       if (heapfd < 0)
-               return -ENOENT;
-
-       /* Bind this file instance to the shared heap. */
-       err = ioctl(heapfd, 0, pdesc->ptcb);
-
-       if (err)
-               goto close_and_exit;
-
-       /* Map the heap memory into our address space. */
-       mapbase = (caddr_t) mmap(NULL,
-                                pdesc->ptsize,
-                                PROT_READ | PROT_WRITE,
-                                MAP_SHARED, heapfd, 0L);
-
+       mapbase = xeno_map_heap((unsigned long)pdesc->ptcb, pdesc->ptsize);
        if (mapbase == MAP_FAILED)
-               err = -ENOMEM;
-       else
-               err =
-                   XENOMAI_SKINCALL2(__vrtx_muxid, __vrtx_pbind, pdesc->pid,
-                                     mapbase);
-
-      close_and_exit:
-
-       close(heapfd);
+               return -errno;
 
-       return err;
+       return XENOMAI_SKINCALL2(__vrtx_muxid, __vrtx_pbind,
+                                pdesc->pid, mapbase);
 }
 
 int sc_pcreate(int pid, char *paddr,


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to