title: Copying data in the kernel
body: <p>I want to copy a string into a local buffer using copystr() but have been having problems doing so. The string I want to copy is uap->path from the VFS function sys_chdir in /usr/src/sys/kern/vfs_syscalls.c. Here is one of the things I've tried:</p>
  
  <pre><code>#define USR_MAX_LEN 22
  char mypath[USR_MAX_LEN + 1];
  size_t len = USR_MAX_LEN;
  size_t done;
  
  copystr(uap-&gt;path, mypath, len, &amp;done);
  </code></pre>
  
  <p>Unfortunately when I do this I end up getting a kernel panic and a subsequent crash. It always crashes in copystr():</p>
  
  <pre><code>(kgdb) list *0xffffffff80c8c860
  0xffffffff80c8c860 is at /usr/src/sys/amd64/amd64/support.S:606.
  601             cld
  602     1:
  603             decq    %rdx
  604             jz      4f
  605             lodsb
  606             stosb
  607             orb     %al,%al
  608             jnz     1b
  609
  610             /* Success -- 0 byte reached */
  Current language:  auto; currently minimal
  (kgdb) bt
  #0  doadump (textdump=&lt;value optimized out&gt;) at pcpu.h:219
  #1  0xffffffff808af530 in kern_reboot (howto=260) at /usr/src/sys/kern/kern_shutdown.c:447
  #2  0xffffffff808af8f4 in panic (fmt=&lt;value optimized out&gt;) at /usr/src/sys/kern/kern_shutdown.c:754
  #3  0xffffffff80c8e6d2 in trap_fatal (frame=&lt;value optimized out&gt;, eva=&lt;value optimized out&gt;)
  at /usr/src/sys/amd64/amd64/trap.c:882
  #4  0xffffffff80c8e9a9 in trap_pfault (frame=0xfffffe004e8ed9f0, usermode=0) at /usr/src/sys/amd64/amd64/trap.c:699
  #5  0xffffffff80c8e136 in trap (frame=0xfffffe004e8ed9f0) at /usr/src/sys/amd64/amd64/trap.c:463
  #6  0xffffffff80c753d2 in calltrap () at /usr/src/sys/amd64/amd64/exception.S:232
  #7  0xffffffff80c8c860 in copystr () at /usr/src/sys/amd64/amd64/support.S:605
  #8  0xffffffff80950e64 in sys_chdir (td=0xfffff80002782000, uap=0xfffffe004e8edb80) at /usr/src/sys/kern/vfs_syscalls.c:838
  #9  0xffffffff80c8efc7 in amd64_syscall (td=0xfffff80002782000, traced=0) at subr_syscall.c:134
  #10 0xffffffff80c756bb in Xfast_syscall () at /usr/src/sys/amd64/amd64/exception.S:391
  #11 0x0000000800d1fc0a in ?? ()
  Previous frame inner to this frame (corrupt stack?)
  (kgdb) 
  </code></pre>
  
  <p>What am I doing wrong here? As far as I can tell my data types satisfy the ones from the man page for copystr so I don't think it's that. Any help is appreciated. Thanks.</p>