Skip to content

Commit

Permalink
Prepare I/O control support
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 6, 2024
1 parent 300c80c commit aa2c561
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
7 changes: 2 additions & 5 deletions src/windows-emulator/io_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ namespace
{
struct dummy_device : stateless_device
{
void read() override
{
}

void write() override
NTSTATUS io_control(const io_device_context&) override
{
return STATUS_SUCCESS;
}
};
}
Expand Down
38 changes: 27 additions & 11 deletions src/windows-emulator/io_device.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
#pragma once

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

#include "emulator_utils.hpp"
#include "handles.hpp"

class windows_emulator;
struct process_context;

struct io_device_context
{
windows_emulator& win_emu;
x64_emulator& emu;
process_context& proc;

handle event;
emulator_pointer /*PIO_APC_ROUTINE*/ apc_routine;
emulator_pointer apc_context;
emulator_object<IO_STATUS_BLOCK> io_status_block;
ULONG io_control_code;
emulator_pointer input_buffer;
ULONG input_buffer_length;
emulator_pointer output_buffer;
ULONG output_buffer_length;
};

struct io_device
{
io_device() = default;
Expand All @@ -14,9 +38,7 @@ struct io_device
io_device(const io_device&) = delete;
io_device& operator=(const io_device&) = delete;

// TODO
virtual void read() = 0;
virtual void write() = 0;
virtual NTSTATUS io_control(const io_device_context& context) = 0;

virtual void serialize(utils::buffer_serializer& buffer) const = 0;
virtual void deserialize(utils::buffer_deserializer& buffer) = 0;
Expand Down Expand Up @@ -46,16 +68,10 @@ class io_device_container : public io_device
this->setup();
}

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

void write() override
NTSTATUS io_control(const io_device_context& context) override
{
this->assert_validity();
this->device_->write();
return this->device_->io_control(context);
}

void serialize(utils::buffer_serializer& buffer) const override
Expand Down
2 changes: 1 addition & 1 deletion src/windows-emulator/syscall_dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class syscall_dispatcher
private:
std::map<uint64_t, syscall_handler_entry> handlers_{};

void add_handlers(std::map<std::string, syscall_handler>& handler_mapping);
static void add_handlers(std::map<std::string, syscall_handler>& handler_mapping);
void add_handlers();
};
43 changes: 31 additions & 12 deletions src/windows-emulator/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1649,18 +1649,37 @@ namespace
return STATUS_SUCCESS;
}

NTSTATUS handle_NtDeviceIoControlFile(const syscall_context&, const handle /*file_handle*/,
const handle /*event*/,
const emulator_pointer /*PIO_APC_ROUTINE*/ /*apc_routine*/,
const emulator_pointer /*apc_context*/,
const emulator_object<IO_STATUS_BLOCK> /*io_status_block*/,
const ULONG /*io_control_code*/,
const emulator_pointer /*input_buffer*/,
const ULONG /*input_buffer_length*/, const emulator_pointer /*output_buffer*/,
const ULONG /*output_buffer_length*/)
{
//puts("NtDeviceIoControlFile not supported");
return STATUS_SUCCESS;
NTSTATUS handle_NtDeviceIoControlFile(const syscall_context& c, const handle file_handle,
const handle event,
const emulator_pointer /*PIO_APC_ROUTINE*/ apc_routine,
const emulator_pointer apc_context,
const emulator_object<IO_STATUS_BLOCK> io_status_block,
const ULONG io_control_code,
const emulator_pointer input_buffer,
const ULONG input_buffer_length, const emulator_pointer output_buffer,
const ULONG output_buffer_length)
{
auto* device = c.proc.devices.get(file_handle);
if (!device)
{
return STATUS_INVALID_HANDLE;
}

const io_device_context context{
.win_emu = c.win_emu,
.emu = c.emu,
.proc = c.proc,
.apc_routine = apc_routine,
.apc_context = apc_context,
.io_status_block = io_status_block,
.io_control_code = io_control_code,
.input_buffer = input_buffer,
.input_buffer_length = input_buffer_length,
.output_buffer = output_buffer,
.output_buffer_length = output_buffer_length,
};

return device->io_control(context);
}

NTSTATUS handle_NtQueryWnfStateData()
Expand Down

0 comments on commit aa2c561

Please sign in to comment.