summaryrefslogtreecommitdiff
path: root/Kernel/StdLib.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2020-01-05 18:00:15 +0100
committerAndreas Kling <awesomekling@gmail.com>2020-01-05 18:14:51 +0100
commit9eef39d68a99c5e29099ae4eb4a56934b35eecde (patch)
tree992cd16e74009907740a54df812f1f791a92b385 /Kernel/StdLib.cpp
parent04b734501a6be77501b21f787607963f253ebc32 (diff)
downloadserenity-9eef39d68a99c5e29099ae4eb4a56934b35eecde.zip
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.
Diffstat (limited to 'Kernel/StdLib.cpp')
-rw-r--r--Kernel/StdLib.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp
index 6c4bcf8a63..1221ad3064 100644
--- a/Kernel/StdLib.cpp
+++ b/Kernel/StdLib.cpp
@@ -1,9 +1,23 @@
#include <AK/Assertions.h>
#include <AK/Types.h>
+#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Heap/kmalloc.h>
+#include <Kernel/StdLib.h>
extern "C" {
+void* copy_to_user(void* dest_ptr, const void* src_ptr, size_t n)
+{
+ SmapDisabler disabler;
+ auto* ptr = memcpy(dest_ptr, src_ptr, n);
+ return ptr;
+}
+
+void* copy_from_user(void* dest_ptr, const void* src_ptr, size_t n)
+{
+ return copy_to_user(dest_ptr, src_ptr, n);
+}
+
void* memcpy(void* dest_ptr, const void* src_ptr, size_t n)
{
size_t dest = (size_t)dest_ptr;
@@ -57,6 +71,13 @@ char* strncpy(char* dest, const char* src, size_t n)
return dest;
}
+void* memset_user(void* dest_ptr, int c, size_t n)
+{
+ SmapDisabler disabler;
+ auto* ptr = memset(dest_ptr, c, n);
+ return ptr;
+}
+
void* memset(void* dest_ptr, int c, size_t n)
{
size_t dest = (size_t)dest_ptr;