2020-04-09 15:12:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-09 15:12:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/StringView.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2020-04-09 15:12:58 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2020-04-09 15:12:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct TypedMapping {
|
|
|
|
const T* ptr() const { return reinterpret_cast<const T*>(region->vaddr().offset(offset).as_ptr()); }
|
|
|
|
T* ptr() { return reinterpret_cast<T*>(region->vaddr().offset(offset).as_ptr()); }
|
|
|
|
const T* operator->() const { return ptr(); }
|
2020-05-23 13:57:19 +00:00
|
|
|
T* operator->() { return ptr(); }
|
2020-04-09 15:12:58 +00:00
|
|
|
const T& operator*() const { return *ptr(); }
|
|
|
|
T& operator*() { return *ptr(); }
|
|
|
|
OwnPtr<Region> region;
|
|
|
|
size_t offset { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2022-01-13 16:20:22 +00:00
|
|
|
static ErrorOr<TypedMapping<T>> map_typed(PhysicalAddress paddr, size_t length, Region::Access access = Region::Access::Read)
|
2020-04-09 15:12:58 +00:00
|
|
|
{
|
|
|
|
TypedMapping<T> table;
|
2022-01-13 16:20:22 +00:00
|
|
|
auto mapping_length = TRY(page_round_up(paddr.offset_in_page() + length));
|
|
|
|
table.region = TRY(MM.allocate_kernel_region(paddr.page_base(), mapping_length, {}, access));
|
2020-04-09 15:12:58 +00:00
|
|
|
table.offset = paddr.offset_in_page();
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-01-13 16:20:22 +00:00
|
|
|
static ErrorOr<TypedMapping<T>> map_typed(PhysicalAddress paddr)
|
2020-04-09 15:12:58 +00:00
|
|
|
{
|
|
|
|
return map_typed<T>(paddr, sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-01-13 16:20:22 +00:00
|
|
|
static ErrorOr<TypedMapping<T>> map_typed_writable(PhysicalAddress paddr)
|
2020-04-09 15:12:58 +00:00
|
|
|
{
|
|
|
|
return map_typed<T>(paddr, sizeof(T), Region::Access::Read | Region::Access::Write);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|