Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't copy raw pointers, since a pointer into kernel space is meaningless to userspace (and will segfault if dereferenced).</p> <p>The typical way of doing something like this is to ask the userspace code to allocate the memory and pass in a pointer to that memory into a system call. If the program doesn't pass in a large enough buffer, then fail with an error (e.g. <code>EFAULT</code>). If there's no way for the program to know in advance a priori how much memory it will need, then typically you'd return the amount of data needed when passed a <code>NULL</code> pointer.</p> <p>Example usage from userspace:</p> <pre><code>// Fixed-size data typedef struct { uint32_t someProperty; uint32_t numOfFruits; } ObjectCapabilities; // First query the number of fruits we need ObjectCapabilities caps; int r = sys_get_fruit(&amp;caps, NULL, 0); if (r != 0) { /* Handle error */ } // Now allocate memory and query the fruit uint32_t *arrayOfFruits = malloc(caps.numOfFruits * sizeof(uint32_t)); r = sys_get_fruit(&amp;caps, arrayOfFruits, caps.numOfFruits); if (r != 0) { /* Handle error */ } </code></pre> <p>And here's how the corresponding code would look in kernel space on the other side of the system call:</p> <pre><code>int sys_get_fruit(ObjectCapabilities __user *userCaps, uint32_t __user *userFruit, uint32_t numFruits) { ObjectCapabilities caps; caps.someProperty = 1024; caps.numOfFruits = 3; // Copy out fixed-size data int r = copy_to_user(userCaps, &amp;caps, sizeof(caps)); if (r != 0) return r; uint32_t localArray[] = { FRUIT_TYPE_APPLE, FRUIT_TYPE_ORANGE, FRUIT_TYPE_BANANA }; // Attempt to copy variable-sized data. Check the size first. if (numFruits * sizeof(uint32_t) &lt; sizeof(localArray)) return -EFAULT; return copy_to_user(userFruit, localArray, sizeof(localArray)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload