Accessing Userspace Memory (Slideshow)

Address Spaces

User Address Space

  • MMU programmed (configured) per-process

  • Context switch includes re-programming MMU

  • Memory access violations

Kernel Address Space

  • Kernel can see all memory

  • Userspace addresses?

Copying To and From Userspace

#include <linux/uaccess.h>

unsigned long copy_to_user(void __user * to,
                           const void * from,
                           unsigned long n);
unsigned long copy_from_user(void * to,
                             const void __user * from,
                             unsigned long n);
  • Returns number of bytes not transferred ⟶ 0 is ok

  • Usually used directly in ioctl() implementation

    • ⟶ return -EFAULT when soemthing has not been transferred

    • ⟶ Calling process segfaults

Documentation

copy_from_user

copy_to_user