2020-07-13 06:21:41 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-13 06:21:41 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "SimpleRegion.h"
|
2020-07-27 08:40:36 -03:00
|
|
|
#include <string.h>
|
2020-07-13 06:21:41 -03:00
|
|
|
|
|
|
|
|
namespace UserspaceEmulator {
|
|
|
|
|
|
|
|
|
|
SimpleRegion::SimpleRegion(u32 base, u32 size)
|
|
|
|
|
: Region(base, size)
|
|
|
|
|
{
|
|
|
|
|
m_data = (u8*)calloc(1, size);
|
2020-07-27 08:40:36 -03:00
|
|
|
m_shadow_data = (u8*)malloc(size);
|
|
|
|
|
memset(m_shadow_data, 1, size);
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleRegion::~SimpleRegion()
|
|
|
|
|
{
|
2020-07-20 21:29:59 -03:00
|
|
|
free(m_shadow_data);
|
2020-07-13 06:21:41 -03:00
|
|
|
free(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
ValueWithShadow<u8> SimpleRegion::read8(FlatPtr offset)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
ValueWithShadow<u16> SimpleRegion::read16(u32 offset)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 1 < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
ValueWithShadow<u32> SimpleRegion::read32(u32 offset)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 3 < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-23 15:45:43 -03:00
|
|
|
ValueWithShadow<u64> SimpleRegion::read64(u32 offset)
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 7 < size());
|
2020-09-23 15:45:43 -03:00
|
|
|
return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 18:29:32 -03:00
|
|
|
ValueWithShadow<u128> SimpleRegion::read128(u32 offset)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(offset + 15 < size());
|
|
|
|
|
return { *reinterpret_cast<const u128*>(m_data + offset), *reinterpret_cast<const u128*>(m_shadow_data + offset) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ValueWithShadow<u256> SimpleRegion::read256(u32 offset)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(offset + 31 < size());
|
|
|
|
|
return { *reinterpret_cast<const u256*>(m_data + offset), *reinterpret_cast<const u256*>(m_shadow_data + offset) };
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
void SimpleRegion::write8(u32 offset, ValueWithShadow<u8> value)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
*reinterpret_cast<u8*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
void SimpleRegion::write16(u32 offset, ValueWithShadow<u16> value)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 1 < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
*reinterpret_cast<u16*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:29:59 -03:00
|
|
|
void SimpleRegion::write32(u32 offset, ValueWithShadow<u32> value)
|
2020-07-13 06:21:41 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 3 < size());
|
2020-07-20 21:29:59 -03:00
|
|
|
*reinterpret_cast<u32*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-23 15:45:43 -03:00
|
|
|
void SimpleRegion::write64(u32 offset, ValueWithShadow<u64> value)
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(offset + 7 < size());
|
2020-09-23 15:45:43 -03:00
|
|
|
*reinterpret_cast<u64*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
|
|
|
|
|
}
|
2021-04-10 18:29:32 -03:00
|
|
|
void SimpleRegion::write128(u32 offset, ValueWithShadow<u128> value)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(offset + 15 < size());
|
|
|
|
|
*reinterpret_cast<u128*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u128*>(m_shadow_data + offset) = value.shadow();
|
|
|
|
|
}
|
|
|
|
|
void SimpleRegion::write256(u32 offset, ValueWithShadow<u256> value)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(offset + 31 < size());
|
|
|
|
|
*reinterpret_cast<u256*>(m_data + offset) = value.value();
|
|
|
|
|
*reinterpret_cast<u256*>(m_shadow_data + offset) = value.shadow();
|
|
|
|
|
}
|
2020-09-23 15:45:43 -03:00
|
|
|
|
2020-07-13 15:14:14 -03:00
|
|
|
u8* SimpleRegion::cacheable_ptr(u32 offset)
|
|
|
|
|
{
|
|
|
|
|
return m_data + offset;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 06:21:41 -03:00
|
|
|
}
|