2021-02-25 10:42:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-02-26 10:16:03 +00:00
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2021-02-25 10:42:49 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-25 10:42:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Vector.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2021-08-06 11:54:48 +00:00
|
|
|
#include <Kernel/Memory/VirtualRange.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <LibC/limits.h>
|
2021-02-25 10:42:49 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2021-02-25 10:42:49 +00:00
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
Vector<VirtualRange, 2> VirtualRange::carve(VirtualRange const& taken) const
|
2021-02-25 10:42:49 +00:00
|
|
|
{
|
|
|
|
VERIFY((taken.size() % PAGE_SIZE) == 0);
|
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
Vector<VirtualRange, 2> parts;
|
2021-02-25 10:42:49 +00:00
|
|
|
if (taken == *this)
|
|
|
|
return {};
|
|
|
|
if (taken.base() > base())
|
|
|
|
parts.append({ base(), taken.base().get() - base().get() });
|
|
|
|
if (taken.end() < end())
|
|
|
|
parts.append({ taken.end(), end().get() - taken.end().get() });
|
|
|
|
return parts;
|
|
|
|
}
|
2022-04-02 18:01:29 +00:00
|
|
|
|
|
|
|
bool VirtualRange::intersects(VirtualRange const& other) const
|
|
|
|
{
|
|
|
|
auto a = *this;
|
|
|
|
auto b = other;
|
|
|
|
|
|
|
|
if (a.base() > b.base())
|
|
|
|
swap(a, b);
|
|
|
|
|
|
|
|
return a.base() < b.end() && b.base() < a.end();
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
VirtualRange VirtualRange::intersect(VirtualRange const& other) const
|
2021-02-26 10:16:03 +00:00
|
|
|
{
|
|
|
|
if (*this == other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto new_base = max(base(), other.base());
|
|
|
|
auto new_end = min(end(), other.end());
|
|
|
|
VERIFY(new_base < new_end);
|
2021-08-06 11:54:48 +00:00
|
|
|
return VirtualRange(new_base, (new_end - new_base).get());
|
2021-02-26 10:16:03 +00:00
|
|
|
}
|
2021-02-25 10:42:49 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<VirtualRange> VirtualRange::expand_to_page_boundaries(FlatPtr address, size_t size)
|
2021-05-28 09:03:21 +00:00
|
|
|
{
|
|
|
|
if ((address + size) < address)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
auto base = VirtualAddress { address }.page_base();
|
2021-12-24 14:22:11 +00:00
|
|
|
auto end = TRY(page_round_up(address + size));
|
2021-08-06 11:54:48 +00:00
|
|
|
return VirtualRange { base, end - base.get() };
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 10:42:49 +00:00
|
|
|
}
|