Skip to content

Commit

Permalink
Prepare I/O device support
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 5, 2024
1 parent 67b204b commit 4c0c1bf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/windows-emulator/handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct handle_types
{
invalid = 0,
file,
device,
event,
section,
symlink,
Expand Down
80 changes: 80 additions & 0 deletions src/windows-emulator/io_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <memory>
#include <serialization.hpp>

struct io_device
{
io_device() = default;
virtual ~io_device() = default;

// TODO
virtual void read() = 0;
virtual void write() = 0;

virtual void serialize(utils::buffer_serializer& buffer) const = 0;
virtual void deserialize(utils::buffer_deserializer& buffer) = 0;
};

// TODO
inline std::unique_ptr<io_device> create_device(const std::wstring_view device)
{
(void)device;
return {};
}

class io_device_container : public io_device
{
public:
io_device_container() = default;

io_device_container(std::wstring device)
: device_name_(std::move(device))
{
this->setup();
}

void read() override
{
this->assert_validity();
this->device_->read();
}

void write() override
{
this->assert_validity();
this->device_->write();
}

void serialize(utils::buffer_serializer& buffer) const override
{
this->assert_validity();

buffer.write_string(this->device_name_);
this->device_->serialize(buffer);
}

void deserialize(utils::buffer_deserializer& buffer) override
{
buffer.read_string(this->device_name_);
this->setup();
this->device_->deserialize(buffer);
}

private:
std::wstring device_name_{};
std::unique_ptr<io_device> device_{};

void setup()
{
this->device_ = create_device(this->device_name_);
}

void assert_validity() const
{
if (!this->device_)
{
throw std::runtime_error("Device not created!");
}
}
};
4 changes: 4 additions & 0 deletions src/windows-emulator/process_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <x64_emulator.hpp>
#include <serialization_helper.hpp>

#include "io_device.hpp"

#define PEB_SEGMENT_SIZE (20 << 20) // 20 MB
#define GS_SEGMENT_SIZE (1 << 20) // 1 MB
Expand Down Expand Up @@ -392,6 +393,7 @@ struct process_context

handle_store<handle_types::event, event> events{};
handle_store<handle_types::file, file> files{};
handle_store<handle_types::device, io_device_container> devices{};
handle_store<handle_types::semaphore, semaphore> semaphores{};
handle_store<handle_types::port, port> ports{};
handle_store<handle_types::registry, registry_key, 2> registry_keys{};
Expand Down Expand Up @@ -428,6 +430,7 @@ struct process_context
buffer.write(this->shared_section_size);
buffer.write(this->events);
buffer.write(this->files);
buffer.write(this->devices);
buffer.write(this->semaphores);
buffer.write(this->ports);
buffer.write(this->registry_keys);
Expand Down Expand Up @@ -469,6 +472,7 @@ struct process_context
buffer.read(this->shared_section_size);
buffer.read(this->events);
buffer.read(this->files);
buffer.read(this->devices);
buffer.read(this->semaphores);
buffer.read(this->ports);
buffer.read(this->registry_keys);
Expand Down

0 comments on commit 4c0c1bf

Please sign in to comment.