summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/hle_ipc.cpp
blob: b7070af03c0267d36500c55b1d8b2dcfb966bf8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <boost/range/algorithm_ext/erase.hpp>
#include "common/assert.h"
#include "common/common_types.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/hle_ipc.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/process.h"
#include "core/hle/kernel/server_session.h"

namespace Kernel {

void SessionRequestHandler::ClientConnected(SharedPtr<ServerSession> server_session) {
    server_session->SetHleHandler(shared_from_this());
    connected_sessions.push_back(server_session);
}

void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_session) {
    server_session->SetHleHandler(nullptr);
    boost::range::remove_erase(connected_sessions, server_session);
}

HLERequestContext::HLERequestContext(SharedPtr<ServerSession> session)
    : session(std::move(session)) {
    cmd_buf[0] = 0;
}

HLERequestContext::~HLERequestContext() = default;

SharedPtr<Object> HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const {
    ASSERT(id_from_cmdbuf < request_handles.size());
    return request_handles[id_from_cmdbuf];
}

u32 HLERequestContext::AddOutgoingHandle(SharedPtr<Object> object) {
    request_handles.push_back(std::move(object));
    return static_cast<u32>(request_handles.size() - 1);
}

void HLERequestContext::ClearIncomingObjects() {
    request_handles.clear();
}

void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf) {
    IPC::RequestParser rp(src_cmdbuf);
    command_header = std::make_unique<IPC::CommandHeader>(rp.PopRaw<IPC::CommandHeader>());

    // If handle descriptor is present, add size of it
    if (command_header->enable_handle_descriptor) {
        handle_descriptor_header =
            std::make_unique<IPC::HandleDescriptorHeader>(rp.PopRaw<IPC::HandleDescriptorHeader>());
        if (handle_descriptor_header->send_current_pid) {
            rp.Skip(2, false);
        }
        rp.Skip(handle_descriptor_header->num_handles_to_copy, false);
        rp.Skip(handle_descriptor_header->num_handles_to_move, false);
    }

    // Padding to align to 16 bytes
    rp.AlignWithPadding();

    if (command_header->num_buf_x_descriptors) {
        UNIMPLEMENTED();
    }
    if (command_header->num_buf_a_descriptors) {
        UNIMPLEMENTED();
    }
    if (command_header->num_buf_b_descriptors) {
        UNIMPLEMENTED();
    }
    if (command_header->num_buf_w_descriptors) {
        UNIMPLEMENTED();
    }
    if (command_header->buf_c_descriptor_flags !=
        IPC::CommandHeader::BufferDescriptorCFlag::Disabled) {
        UNIMPLEMENTED();
    }

    data_payload_header =
        std::make_unique<IPC::DataPayloadHeader>(rp.PopRaw<IPC::DataPayloadHeader>());
    ASSERT(data_payload_header->magic == 0x49434653 || data_payload_header->magic == 0x4F434653);

    data_payload_offset = rp.GetCurrentOffset();
    command = rp.Pop<u32_le>();
}

ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf,
                                                                Process& src_process,
                                                                HandleTable& src_table) {
    ParseCommandBuffer(src_cmdbuf);
    size_t untranslated_size = data_payload_offset + command_header->data_size;
    std::copy_n(src_cmdbuf, untranslated_size, cmd_buf.begin());

    if (command_header->enable_handle_descriptor) {
        if (handle_descriptor_header->num_handles_to_copy ||
            handle_descriptor_header->num_handles_to_move) {
            UNIMPLEMENTED();
        }
    }
    return RESULT_SUCCESS;
}

ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
                                                           HandleTable& dst_table) {
    ParseCommandBuffer(&cmd_buf[0]);
    size_t untranslated_size = data_payload_offset + command_header->data_size;
    std::copy_n(cmd_buf.begin(), untranslated_size, dst_cmdbuf);

    if (command_header->enable_handle_descriptor) {
        size_t command_size = untranslated_size + handle_descriptor_header->num_handles_to_copy +
                              handle_descriptor_header->num_handles_to_move;
        ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH);

        size_t untranslated_index = untranslated_size;
        size_t handle_write_offset = 3;
        while (untranslated_index < command_size) {
            u32 descriptor = cmd_buf[untranslated_index];
            untranslated_index += 1;

            switch (IPC::GetDescriptorType(descriptor)) {
            case IPC::DescriptorType::CopyHandle:
            case IPC::DescriptorType::MoveHandle: {
                // HLE services don't use handles, so we treat both CopyHandle and MoveHandle
                // equally
                u32 num_handles = IPC::HandleNumberFromDesc(descriptor);
                for (u32 j = 0; j < num_handles; ++j) {
                    SharedPtr<Object> object = GetIncomingHandle(cmd_buf[untranslated_index]);
                    Handle handle = 0;
                    if (object != nullptr) {
                        // TODO(yuriks): Figure out the proper error handling for if this fails
                        handle = dst_table.Create(object).Unwrap();
                    }
                    dst_cmdbuf[handle_write_offset++] = handle;
                    untranslated_index++;
                }
                break;
            }
            default:
                UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor);
            }
        }
    }

    return RESULT_SUCCESS;
}

} // namespace Kernel