Skip to content

Commit

Permalink
Extract device creation
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 6, 2024
1 parent 4c0c1bf commit 8cf451f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
5 changes: 0 additions & 5 deletions src/windows-emulator/handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,6 @@ class handle_store
constexpr auto KNOWN_DLLS_DIRECTORY = make_pseudo_handle(0x1337, handle_types::directory);
constexpr auto KNOWN_DLLS_SYMLINK = make_pseudo_handle(0x1337, handle_types::symlink);
constexpr auto SHARED_SECTION = make_pseudo_handle(0x1337, handle_types::section);
constexpr auto CONSOLE_SERVER = make_pseudo_handle(0x1338, handle_types::section);
constexpr auto CM_API = make_pseudo_handle(0x1338, handle_types::file);
constexpr auto KSEC_DD = make_pseudo_handle(0x1339, handle_types::file);
constexpr auto AFD_ENDPOINT = make_pseudo_handle(0x133A, handle_types::file);
constexpr auto CNG = make_pseudo_handle(0x133B, handle_types::file);

constexpr auto CONSOLE_HANDLE = make_pseudo_handle(0x1, handle_types::file);
constexpr auto STDOUT_HANDLE = make_pseudo_handle(0x2, handle_types::file);
Expand Down
29 changes: 29 additions & 0 deletions src/windows-emulator/io_device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "io_device.hpp"

namespace
{
struct dummy_device : stateless_device
{
void read() override
{
}

void write() override
{
}
};
}

std::unique_ptr<io_device> create_device(const std::wstring_view device)
{
if (device == L"CNG"
|| device == L"KsecDD"
|| device == L"DeviceApi\\CMApi"
|| device == L"ConDrv\\Server"
|| device == L"Afd\\Endpoint")
{
return std::make_unique<dummy_device>();
}

throw std::runtime_error("Unsupported device: " + std::string(device.begin(), device.end()));
}
22 changes: 17 additions & 5 deletions src/windows-emulator/io_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ struct io_device
io_device() = default;
virtual ~io_device() = default;

io_device(io_device&&) = default;
io_device& operator=(io_device&&) = default;

io_device(const io_device&) = delete;
io_device& operator=(const io_device&) = delete;

// TODO
virtual void read() = 0;
virtual void write() = 0;
Expand All @@ -16,12 +22,18 @@ struct io_device
virtual void deserialize(utils::buffer_deserializer& buffer) = 0;
};

// TODO
inline std::unique_ptr<io_device> create_device(const std::wstring_view device)
struct stateless_device : io_device
{
(void)device;
return {};
}
void serialize(utils::buffer_serializer&) const override
{
}

void deserialize(utils::buffer_deserializer&) override
{
}
};

std::unique_ptr<io_device> create_device(const std::wstring_view device);

class io_device_container : public io_device
{
Expand Down
40 changes: 11 additions & 29 deletions src/windows-emulator/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ namespace
return STATUS_SUCCESS;
}

if (value.type == handle_types::device && c.proc.devices.erase(handle))
{
return STATUS_SUCCESS;
}

if (value.type == handle_types::semaphore && c.proc.semaphores.erase(handle))
{
return STATUS_SUCCESS;
Expand Down Expand Up @@ -2040,41 +2045,18 @@ namespace
c.win_emu.logger.print(color::dark_gray, "--> Opening file: %S\n", filename.c_str());
});

if (filename == L"\\Device\\ConDrv\\Server")
{
file_handle.write(CONSOLE_SERVER.bits);
return STATUS_SUCCESS;
}

if (filename == L"\\Device\\DeviceApi\\CMApi")
constexpr std::wstring_view device_prefix = L"\\Device\\";
if (filename.starts_with(device_prefix))
{
file_handle.write(CM_API.bits);
return STATUS_SUCCESS;
}
auto device_name = filename.substr(device_prefix.size());
io_device_container container{std::move(device_name)};

if (filename == L"\\Device\\KsecDD")
{
file_handle.write(KSEC_DD.bits);
return STATUS_SUCCESS;
}
const auto handle = c.proc.devices.store(std::move(container));
file_handle.write(handle.bits);

if (filename == L"\\Device\\CNG")
{
file_handle.write(CNG.bits);
return STATUS_SUCCESS;
}

if (filename.starts_with(L"\\Device\\Afd\\Endpoint"))
{
file_handle.write(AFD_ENDPOINT.bits);
return STATUS_SUCCESS;
}

if (filename.starts_with(L"\\Device\\"))
{
return STATUS_NOT_SUPPORTED;
}

handle root_handle{};
root_handle.bits = reinterpret_cast<uint64_t>(attributes.RootDirectory);
if (root_handle.value.is_pseudo && (filename == L"\\Reference" || filename == L"\\Connect"))
Expand Down

0 comments on commit 8cf451f

Please sign in to comment.