Skip to contentSkip to conversation

Question about rtdm_mmap_to_user()

Xenomai-help Perrine Martignoni 6 participants 11 messages Apr 3, 2008 – Apr 4, 2008

lore
Share & tools
lore
#0 lore raw

Hello,

I try to allocate a buffer in kernel space (with kmalloc) and then map it into user-space (with rtdm_mmap_to_user).
After, I fill in the buffer in user space with the address returned by rtdm_mmap_to_user, and when I read it into the kernel space with the kernel address, I don't have the values put in user-space.

What I'm doing wrong ?
Have you a simple example of use of rtdm_mmap_to_user ?

Thanks in advance.
Best Regards,
Perrine

#1 lore raw

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

RT_HEAP heap_desc;
void *shared;

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

============================================

Hope this helps :)

#2 lore raw
Quoted 13 lines

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

Quoted 7 lines

RT_HEAP heap_desc;
void *shared;

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

I tried exactly what you wrote. And it doesn't work. I have error -11. It doesn't find the Heap I created in kernel space.
I don't understand why. I put exactly the same name.

Quoted 25 lines · Bosko Radivojevic

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

RT_HEAP heap_desc;
void *shared;

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

============================================

Hope this helps :)

#3 lore raw
Quoted 16 lines · Perrine Martignoni
Quoted 16 lines

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

RT_HEAP heap_desc;
void *shared;

Quoted 8 lines
Quoted 4 lines

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

I tried exactly what you wrote. And it doesn't work. I have error -11. It doesn't find the Heap I created in kernel space.
I don't understand why. I put exactly the same name.

Mixing up RTDM and native API usage, though technically feasible, isn't a good idea anyway. Better have a look at Hannes' example, and if that one or your own version still doesn't work, post more details (Xenomai version, target architecture, test code, etc.).

Jan

#4 lore raw
In reply to Perrine Martignoni
Quoted 2 lines · Perrine Martignoni
Quoted 2 lines

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared);

[..]

Quoted 3 lines

I tried exactly what you wrote. And it doesn't work. I have error -11. It doesn't find the Heap I created in kernel space.
I don't understand why. I put exactly the same name.

rt_heap_create() must be called from kernel module init function. Here is copy&paste from my code (with checking return values):

kernel_module:
===========

RT_HEAP heap_desc;
void *shared_mem;

int init_module (void)
{
int ret;

ret = rt_heap_create (&heap_desc, "MainSHM", 4*1024, H_SHARED);

  if (ret) {
    printk ("Error rt_heap_create: %d\n", ret);
  }
  ret = rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared_mem);
  if (ret) {
    printk ("Error rt_heap_alloc: %d\n", ret);

}
printk ("4kB heap memory created, name: MainSHM\n"); return 1;
}

void cleanup_module(void)
{
rt_heap_delete (&heap_desc);
}

#5 lore raw
Quoted 2 lines

Except that the example listed in the document in question takes care of defining the heap descriptor in global memory, not in the init_module()

stack.

It effectively works better with the RT_HEAP variable in global scope.

I thought that the RT_HEAP variable was working like handles or descriptors in some RTOS where the life cycle of the descriptor is not correlated to the Os Object life cycle, that is to say:

=> enter local scope {
handle myHandle =
CreateOsObject(x,y,z,&err); // or err = CreateOsObject(&myHandle,x,y,z);
=> exit local scope }

When you go out of the local scope, you can no more use the handle (and then reference the created OS object ) because it was a local variable but the object created is still alive and will not be modified because the handle was declared on the stack. So I thought that the RT_HEAP variable was just a descriptor of a Heap object managed by xenomai but not the heap object itself.

Best Regards,
Perrine

Quoted 41 lines · Bosko Radivojevic
Quoted 2 lines · Perrine Martignoni
Quoted 2 lines

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared);

[..]

Quoted 1 line

I tried exactly what you wrote. And it doesn't work. I have error -11.

It

Quoted 2 lines

doesn't find the Heap I created in kernel space. I don't understand why. I put exactly the same name.

rt_heap_create() must be called from kernel module init function. Here is copy&paste from my code (with checking return values):

kernel_module:
===========

RT_HEAP heap_desc;
void *shared_mem;

int init_module (void)
{
int ret;

ret = rt_heap_create (&heap_desc, "MainSHM", 4*1024, H_SHARED);

 if (ret) {
   printk ("Error rt_heap_create: %d\n", ret);
 }
 ret = rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared_mem);
 if (ret) {
   printk ("Error rt_heap_alloc: %d\n", ret);

}
printk ("4kB heap memory created, name: MainSHM\n"); return 1;
}

void cleanup_module(void)
{
rt_heap_delete (&heap_desc);
}

#6 lore raw
Quoted 16 lines · Perrine Martignoni
Quoted 2 lines

Except that the example listed in the document in question takes care of defining the heap descriptor in global memory, not in the init_module()

stack.

It effectively works better with the RT_HEAP variable in global scope.

I thought that the RT_HEAP variable was working like handles or descriptors in some RTOS where the life cycle of the descriptor is not correlated to the Os Object life cycle, that is to say:

=> enter local scope {
handle myHandle =
CreateOsObject(x,y,z,&err); // or err = CreateOsObject(&myHandle,x,y,z);
=> exit local scope }

Quoted 6 lines

When you go out of the local scope, you can no more use the handle (and then reference the created OS object ) because it was a local variable but the object created is still alive and will not be modified because the handle was declared on the stack. So I thought that the RT_HEAP variable was just a descriptor of a Heap object managed by xenomai but not the heap object itself.

The behaviour of the RT_HEAP descriptor is documented in rt_heap_create documentation.

Signature · 1 line
 Gilles
#7 lore raw
In reply to Bosko Radivojevic
Quoted 16 lines · Bosko Radivojevic

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

RT_HEAP heap_desc;
void *shared;

Quoted 8 lines

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

============================================

Hope this helps :)

This is a bad example. A good example would check the return values of the services called.

Signature · 1 line
 Gilles
#8 lore raw
Quoted 1 line

Mixing up RTDM and native API usage, though technically feasible, isn't a

good idea anyway. Better have a look at Hannes' example, >and if that one or your own version still doesn't work, post more details (Xenomai version, target architecture, test code, etc.).

I found an example in Native-API-Tour.pdf which do this. I try to follow it and it doesn't work.

I work on an MPC8347 with linux-2.6.23 and xenomai-2.4.0.

Here is my code :

*In kernel space* : I create a memory heap in init()
----------------------------------------------------------------------------------------------------------------------------------------------
#include <rtdm/rtdm_driver.h>

static struct rtdm_device t_MEMDevice = {
    struct_version:     RTDM_DEVICE_STRUCT_VER,
    device_flags:       RTDM_NAMED_DEVICE,
    context_size:       0,
    device_name:        DEV_FILE_MEMORY,

    open_rt:        NULL,
    open_nrt:       dmem_Open,

    ops:{
        close_rt:   NULL,
        close_nrt:  dmem_Close,
        ioctl_rt:   dmem_Ioctl,
        ioctl_nrt:  dmem_Ioctl,
        read_rt:    NULL,
        read_nrt:   NULL,
        write_rt:   NULL,
        write_rt:   NULL,
        recvmsg_rt: NULL,
        recvmsg_nrt:NULL,
        sendmsg_rt: NULL,
        sendmsg_nrt:NULL,
    },
    device_class:       RTDM_CLASS_EXPERIMENTAL,
    device_sub_class:   222,
    driver_name:        DRV_NAME,
    peripheral_name:    DEV_FILE_NAME,
    provider_name:      "-",
    proc_name:          t_MEMDevice.device_name,
};

static int dmem_Init(void)

{
        RT_HEAP Heap;
        int ret =0;
        void *p_HeapMem = NULL;

        if((ret = rt_heap_create(&Heap, "HeapTest", 2048, H_MAPPABLE |
H_SINGLE)))
        {
           rtdm_printk("Impossible de creer l'espace memoire Heap : ret =
%d\n", ret);
        }

        ret = rt_heap_alloc(&Heap, 0, TM_INFINITE, &p_HeapMem);

        rtdm_printk("**DRIVER p_HeapMem %x \n", p_HeapMem);

        if(ret != 0)
          rtdm_printk("RETOUR ALLOC ret %d\n",ret);


    if(rtdm_dev_register(&t_MEMDevice)!=0)
        rtdm_printk("Enregistrement impossible du device MEMORY\n");
    else
        rtdm_printk("Initialisation OK\n");

    return 0;
}

static void dmem_Exit(void)

{
    if(rtdm_dev_unregister(&t_MEMDevice,0))
        rtdm_printk("Desabonnement KO\n");
    else
        rtdm_printk("Desabonnement OK\n");
}

module_init(dmem_Init);
module_exit(dmem_Exit);
----------------------------------------------------------------------------------------------------------------------------------

I see in /proc/xenomai/registry/native/heaps the *HeapTest *I create. And I see that :

* type=kernel:size=3017801728:used=1

In User space :* I want to bind the heap
--------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/heap.h>

#define TASK_PRIO 50

static RT_TASK s_TaskTest;

static void taskTest(void *cookie)

{
    void *p_HeapMem = NULL;
    int i=0;
    int ret=0;
    RT_HEAP Heap;

    ret = rt_heap_bind(&Heap, "HeapTest", TM_INFINITE);
    printf("ret heap_bind %d\n", ret);

    rt_heap_alloc(&Heap, 0, TM_INFINITE, &p_HeapMem);

    printf("p_HeapMem %x \n", p_HeapMem);

}

int main(int argc, char * argv[])

{
    int ret = 0;

    mlockall(MCL_CURRENT | MCL_FUTURE);

      // creation de la tache temps reel TaskTest
    if((ret = rt_task_create(&s_TaskTest, "TaskTest", 0, TASK_PRIO,
T_JOINABLE))){
        fprintf(stderr,"failed to create TaskTest, code %d\n",ret);
        return ret;
    }
    // demarrage de la tache temps reel TaskTest
    if((ret = rt_task_start(&s_TaskTest, &taskTest, (void *)1))){
        fprintf(stderr,"failed to start TaskTest, code %d\n",ret);
        return ret;
    }

    rt_task_join(&s_TaskTest);

    return ret;
}

When I put NONBLOCK, I have the error -11 and when I put TM_INFINITE, it runs and doesn't stop.

Best Regards,
Perrine

Quoted 33 lines · Gilles Chanteperdrix
Quoted 16 lines · Bosko Radivojevic

Why don't you try rt_heap_* functions? Quick example:

Kernel space:

RT_HEAP heap_desc;
void *shared;

rt_heap_create (&heap_desc, "MySHM", 2048, H_SHARED); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [.. cleanup ..]
rt_heap_delete (&heap_desc);
User space:

RT_HEAP heap_desc;
void *shared;

Quoted 8 lines

rt_heap_bind (&heap_desc, "MySHM", TM_NONBLOCK); rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared); [..cleanup..]
rt_heap_unbind (&heap_desc);

============================================

Hope this helps :)

This is a bad example. A good example would check the return values of the services called.

--
Gilles

#9 lore raw
Quoted 8 lines · Perrine Martignoni
Quoted 1 line

Mixing up RTDM and native API usage, though technically feasible, isn't

a good idea anyway. Better have a look at Hannes' example, >and if that one or your own version still doesn't work, post more details (Xenomai version, target architecture, test code, etc.).

I found an example in Native-API-Tour.pdf which do this. I try to follow it and it doesn't work.

Except that the example listed in the document in question takes care of defining the heap descriptor in global memory, not in the init_module() stack.

Quoted 3 lines

static int dmem_Init(void)

{   
        RT_HEAP Heap;

^^^^ this won't survive long enough.

Quoted 9 lines
        int ret =0;
        void *p_HeapMem = NULL;

----------------------------------------------------------------------------------------------------------------------------------

I see in /proc/xenomai/registry/native/heaps the /HeapTest /I create. And I see that :

* type=kernel:size=3017801728:used=1

i.e. we see a 2.8+ _gigabyte_ heap from which 1 single byte would be in use..., albeit you defined a 2Kb heap initially, to be used as a single contiguous segment. Getting braindamage values like this when looking at the /proc interface should ring a bell about a possible corruption of some important kernel-based information, like a heap descriptor.

Signature · 1 line
Philippe.
#10 lore raw
In reply to Perrine Martignoni

http://www.captain.at/xenomai-real-time-driver-example.php :-)

Quoted 19 lines · Perrine Martignoni

Hello,

I try to allocate a buffer in kernel space (with kmalloc) and then map it into user-space (with rtdm_mmap_to_user).
After, I fill in the buffer in user space with the address returned by rtdm_mmap_to_user, and when I read it into the kernel space with the kernel address, I don't have the values put in user-space.

What I'm doing wrong ?
Have you a simple example of use of rtdm_mmap_to_user ?

Thanks in advance.
Best Regards,
Perrine

------------------------------------------------------------------------

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help