title: Making a thread from scratch on Linux
body: <p>I wonder if it's possible to create a new thread from scratch. (Unlike fork() which gives me a copy of the memorymap and registerstate, etc.) Starting with an empty page-table, into which I can map code and data. I'd also need a way to place the instruction pointer.</p>
  
  <p><strong>Motivation</strong>: In hardware emulators I often find stuff like</p>
  
  <pre><code>uint32_t read32(uint32_t addr) {
      if (RAM1_BEGIN &lt;= addr &amp;&amp; addr &lt; RAM1_END)
          return *(uint32_t*)(ram1 + (addr-RAM1_BEGIN));
      else if (RAM2_BEGIN &lt;= addr &amp;&amp; addr &lt; RAM2_END)
          return *(uint32_t*)(ram2 + (addr-RAM2_BEGIN));
      ...
  }
  </code></pre>
  
  <p>I thought it would be a waste of time to emulate an MMU when the host already has one.</p>
  
  <p>I could just use mmap() to get free memory at the spots the hardware has, but I can't be 100% sure that it will be free. (Loader places binary and libraries arbitrarily.) That's why I concluded that I need a new empty thread (and address space).</p>