2021-01-29 14:03:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-29 14:03:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Memory.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <Kernel/Arch/PC/BIOS.h>
|
2021-07-11 12:02:15 -07:00
|
|
|
#include <Kernel/Devices/MemoryDevice.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2021-01-29 14:03:25 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-18 11:37:26 +03:00
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<MemoryDevice> MemoryDevice::must_create()
|
|
|
|
{
|
2021-06-28 15:19:25 +02:00
|
|
|
return adopt_ref(*new MemoryDevice);
|
2021-06-18 11:37:26 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT MemoryDevice::MemoryDevice()
|
2021-01-29 14:03:25 +02:00
|
|
|
: CharacterDevice(1, 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT MemoryDevice::~MemoryDevice()
|
2021-01-29 14:03:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> MemoryDevice::read(FileDescription&, u64, UserOrKernelBuffer&, size_t)
|
2021-01-29 14:03:25 +02:00
|
|
|
{
|
|
|
|
TODO();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryDevice::did_seek(FileDescription&, off_t)
|
|
|
|
{
|
|
|
|
TODO();
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
KResultOr<Memory::Region*> MemoryDevice::mmap(Process& process, FileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
|
2021-01-29 14:03:25 +02:00
|
|
|
{
|
|
|
|
auto viewed_address = PhysicalAddress(offset);
|
|
|
|
|
|
|
|
dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes", viewed_address, range.size());
|
|
|
|
if (!MM.is_allowed_to_mmap_to_userspace(viewed_address, range)) {
|
|
|
|
dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes failed due to violation of access", viewed_address, range.size());
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:07:59 +00:00
|
|
|
auto maybe_vmobject = Memory::AnonymousVMObject::try_create_for_physical_range(viewed_address, range.size());
|
|
|
|
if (maybe_vmobject.is_error())
|
|
|
|
return maybe_vmobject.error();
|
|
|
|
|
2021-01-29 14:03:25 +02:00
|
|
|
dbgln("MemoryDevice: Mapped physical memory at {} for range of {} bytes", viewed_address, range.size());
|
2021-08-06 13:59:22 +02:00
|
|
|
return process.address_space().allocate_region_with_vmobject(
|
2021-01-29 14:03:25 +02:00
|
|
|
range,
|
2021-08-15 09:07:59 +00:00
|
|
|
maybe_vmobject.release_value(),
|
2021-01-29 14:03:25 +02:00
|
|
|
0,
|
|
|
|
"Mapped Physical Memory",
|
|
|
|
prot,
|
|
|
|
shared);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|