1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-29 19:22:13 +00:00
serenity/Kernel/Devices/KCOVInstance.cpp
Andreas Kling 79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00

52 lines
1.5 KiB
C++

/*
* Copyright (c) 2021, Patrick Meyer <git@the-space.agency>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <Kernel/Devices/KCOVInstance.h>
namespace Kernel {
KCOVInstance::KCOVInstance(ProcessID pid)
{
m_pid = pid;
}
ErrorOr<void> KCOVInstance::buffer_allocate(size_t buffer_size_in_entries)
{
if (buffer_size_in_entries < 2 || buffer_size_in_entries > KCOV_MAX_ENTRIES)
return EINVAL;
// first entry contains index of last PC
m_buffer_size_in_entries = buffer_size_in_entries - 1;
m_buffer_size_in_bytes = Memory::page_round_up(buffer_size_in_entries * KCOV_ENTRY_SIZE);
// one single vmobject is representing the buffer
// - we allocate one kernel region using that vmobject
// - when an mmap call comes in, we allocate another userspace region,
// backed by the same vmobject
m_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(m_buffer_size_in_bytes, AllocationStrategy::AllocateNow));
m_kernel_region = TRY(MM.allocate_kernel_region_with_vmobject(
*m_vmobject, m_buffer_size_in_bytes, String::formatted("kcov_{}", m_pid),
Memory::Region::Access::ReadWrite));
m_buffer = (u64*)m_kernel_region->vaddr().as_ptr();
return {};
}
void KCOVInstance::buffer_add_pc(u64 pc)
{
auto idx = (u64)m_buffer[0];
if (idx >= m_buffer_size_in_entries) {
// the buffer is already full
return;
}
m_buffer[idx + 1] = pc;
m_buffer[0] = idx + 1;
}
}