title: Get starting address of a memory page in Linux
body: <p>In my code, I need to keep track of some pages which are being modified. Initially I give only read access to them, but on the signal handler I give them both read and write access (I am not going to explain what is the purpose for all that and there is actually more code in the signal handler, which I've not shown). </p>
  
  <p>Now my question is how to get the starting address of a page from a memory address which resides in that page. So basically I need to implement the <strong>get_page_start_addr</strong> shown here in the code. Up till now, I was only doing it for a single page, whose starting address my program knew. But now, since I need to do it for multiple pages, I need to get the starting address of the page from any arbitrary memory address, because mprotect only accepts starting page address as valid argument.</p>
  
  <pre><code>static void memory_change_handler(int sig, siginfo_t *si, void *unused)
  {
    long addr = (long)si-&gt;si_addr;
    long page_start_addr = get_page_start_addr( addr );
  
    if (mprotect((void*)page_start_addr, pagesize, PROT_READ | PROT_WRITE) == -1)
    {
      print_error();
      handle_error("mprotect");
    }
  }
  </code></pre>