title: How do I directly access reserved memory with a kernel module?
body: <p>I'm trying to limit the OS (Ubuntu Server 15.04) to a certain memory usage and reserve the rest but write a kernel module to read/write to the reserved memory. I figured out how to limit the usage/reserve memory using the kernel parameters "mem=4G memmap=4G@0 memmap=4G$4G" (4GB for OS and 4GB reserved, split at 4GB point) but I don't know how DMA to reserved memory works with kernel modules. I was thinking just create a proc file but I'm not sure if you can create one outside of the OS's allocated memory.</p>
  
  <p>Any suggestions? Thanks!</p>
  
  <p><strong>Edit:</strong> This is for research so it doesn't need to be "nice"</p>
  
  <p><strong>Update:</strong>
  Maybe I don't need to write a kernel module. I just found this and I'm going to give it a shot:
  <a href="http://elinux.org/Memory_Management#Reserving_.28and_accessing.29_the_top_of_memory_on_startup" rel="nofollow noreferrer">http://elinux.org/Memory_Management#Reserving_.28and_accessing.29_the_top_of_memory_on_startup</a></p>
  
  <p><strong>Update:</strong>
  I tried the link above but I segfault whenever I try to write. Here's my code:</p>
  
  <pre><code>    #include &lt;fcntl.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;sys/mann.h&gt;
  
      #define RESERVED_MEMORY_SIZE 0x100000000
  
      int main() {
              int fd;
              char *reserved_memory;
  
              fd = open("/dev/mem", O_RDWR | O_SYNC);
              reserved_memory = (char *) mmap(0, RESERVED_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 4096);
              reserved_memory[0] = 'a';
              return 0;
      }
  </code></pre>
  
  <p>dmesg shows:</p>
  
  <pre><code>    a.out[1167]: segfault at ffffffffffffffff ip 00000000004005d7 sp 00007ffeffccbd80 error 7 in a.out[400000+1000]
  </code></pre>
  
  <p>For kicks I tried reserved_memory[1]:</p>
  
  <pre><code>    a.out[1180]: segfault at 0 ip 00000000004005db sp 00007ffc388d77b0 error 6 in a.out[400000+1000]
  </code></pre>
  
  <p>I'll look into the format of those messages so I can figure out what it's telling me.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I found this question by somebody with the same issue as me however the only solution appears to be a kernel rebuild. I'm going to try to avoid this so maybe my best option is a custom kernel module again.
  <a href="https://stackoverflow.com/questions/11891979/accessing-mmaped-dev-mem">accessing mmaped /dev/mem?</a></p>