mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:38:11 +00:00
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.
This commit is contained in:
parent
04b734501a
commit
9eef39d68a
11 changed files with 245 additions and 60 deletions
|
@ -48,6 +48,7 @@ FileDescription::~FileDescription()
|
|||
|
||||
KResult FileDescription::fstat(stat& buffer)
|
||||
{
|
||||
SmapDisabler disabler;
|
||||
if (is_fifo()) {
|
||||
memset(&buffer, 0, sizeof(buffer));
|
||||
buffer.st_mode = 001000;
|
||||
|
@ -102,6 +103,7 @@ off_t FileDescription::seek(off_t offset, int whence)
|
|||
|
||||
ssize_t FileDescription::read(u8* buffer, ssize_t count)
|
||||
{
|
||||
SmapDisabler disabler;
|
||||
int nread = m_file->read(*this, buffer, count);
|
||||
if (nread > 0 && m_file->is_seekable())
|
||||
m_current_offset += nread;
|
||||
|
@ -110,6 +112,7 @@ ssize_t FileDescription::read(u8* buffer, ssize_t count)
|
|||
|
||||
ssize_t FileDescription::write(const u8* data, ssize_t size)
|
||||
{
|
||||
SmapDisabler disabler;
|
||||
int nwritten = m_file->write(*this, data, size);
|
||||
if (nwritten > 0 && m_file->is_seekable())
|
||||
m_current_offset += nwritten;
|
||||
|
@ -160,7 +163,7 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
|
|||
if (size < temp_buffer.size())
|
||||
return -1;
|
||||
|
||||
memcpy(buffer, temp_buffer.data(), temp_buffer.size());
|
||||
copy_to_user(buffer, temp_buffer.data(), temp_buffer.size());
|
||||
return stream.offset();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue