Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kernel: Start implementing x86 SMAP support
Supervisor Mode Access Prevention (SMAP) is an x86 CPU feature that prevents the kernel from accessing userspace memory. With SMAP enabled, trying to read/write a userspace memory address while in the kernel will now generate a page fault. Since it's sometimes necessary to read/write userspace memory, there are two new instructions that quickly switch the protection on/off: STAC (disables protection) and CLAC (enables protection.) These are exposed in kernel code via the stac() and clac() helpers. There's also a SmapDisabler RAII object that can be used to ensure that you don't forget to re-enable protection before returning to userspace code. THis patch also adds copy_to_user(), copy_from_user() and memset_user() which are the "correct" way of doing things. These functions allow us to briefly disable protection for a specific purpose, and then turn it back on immediately after it's done. Going forward all kernel code should be moved to using these and all uses of SmapDisabler are to be considered FIXME's. Note that we're not realizing the full potential of this feature since I've used SmapDisabler quite liberally in this initial bring-up patch.
- Loading branch information
9eef39d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wild idea: what if, instead of explicit Linux-like
copy_to_user()
©_from_user()
there was a wrapper type that would let you do this naturally? It could actually be a variation of the sameSmapDisabler
type (or it could be named differently for clarity):Instead of
you'd write
*SmapDisabler(dest_ptr) = value;
or say
*UserMemory(dest_ptr) = value;
9eef39d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, something like a
Userspace<T*>
or similar might be an interesting option.9eef39d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moreover, it's not okay to first validate a read/write and then do it. The thread can get preempted in the middle and change the regions and/or the pointer, even without an explicit sleep.
So
Userspace<T*>
would have to:EFAULT
in the caller