kernel: make handle table per-process

This commit is contained in:
Weiyi Wang 2018-10-19 21:04:18 -04:00
parent fda2a5cf54
commit eb285c33fd
11 changed files with 169 additions and 119 deletions

View file

@ -26,14 +26,13 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
HLERequestContext context(std::move(session));
auto process = kernel.CreateProcess(kernel.CreateCodeSet("", 0));
HandleTable handle_table;
SECTION("works with empty cmdbuf") {
const u32_le input[]{
IPC::MakeHeader(0x1234, 0, 0),
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
REQUIRE(context.CommandBuffer()[0] == 0x12340000);
}
@ -46,7 +45,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
0xAABBCCDD,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
auto* output = context.CommandBuffer();
REQUIRE(output[1] == 0x12345678);
@ -56,34 +55,34 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
SECTION("translates move handles") {
auto a = MakeObject(kernel);
Handle a_handle = handle_table.Create(a).Unwrap();
Handle a_handle = process->handle_table.Create(a).Unwrap();
const u32_le input[]{
IPC::MakeHeader(0, 0, 2),
IPC::MoveHandleDesc(1),
a_handle,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
auto* output = context.CommandBuffer();
REQUIRE(context.GetIncomingHandle(output[2]) == a);
REQUIRE(handle_table.GetGeneric(a_handle) == nullptr);
REQUIRE(process->handle_table.GetGeneric(a_handle) == nullptr);
}
SECTION("translates copy handles") {
auto a = MakeObject(kernel);
Handle a_handle = handle_table.Create(a).Unwrap();
Handle a_handle = process->handle_table.Create(a).Unwrap();
const u32_le input[]{
IPC::MakeHeader(0, 0, 2),
IPC::CopyHandleDesc(1),
a_handle,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
auto* output = context.CommandBuffer();
REQUIRE(context.GetIncomingHandle(output[2]) == a);
REQUIRE(handle_table.GetGeneric(a_handle) == a);
REQUIRE(process->handle_table.GetGeneric(a_handle) == a);
}
SECTION("translates multi-handle descriptors") {
@ -91,12 +90,15 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
auto b = MakeObject(kernel);
auto c = MakeObject(kernel);
const u32_le input[]{
IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2),
handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(),
IPC::MoveHandleDesc(1), handle_table.Create(c).Unwrap(),
IPC::MakeHeader(0, 0, 5),
IPC::MoveHandleDesc(2),
process->handle_table.Create(a).Unwrap(),
process->handle_table.Create(b).Unwrap(),
IPC::MoveHandleDesc(1),
process->handle_table.Create(c).Unwrap(),
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
auto* output = context.CommandBuffer();
REQUIRE(context.GetIncomingHandle(output[2]) == a);
@ -111,7 +113,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
0,
};
auto result = context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
auto result = context.PopulateFromIncomingCommandBuffer(input, *process);
REQUIRE(result == RESULT_SUCCESS);
auto* output = context.CommandBuffer();
@ -125,7 +127,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
0x98989898,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
REQUIRE(context.CommandBuffer()[2] == process->process_id);
}
@ -145,7 +147,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
target_address,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
CHECK(context.GetStaticBuffer(0) == *buffer);
@ -166,7 +168,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
target_address,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
std::vector<u8> other_buffer(buffer->size());
context.GetMappedBuffer(0).Read(other_buffer.data(), 0, buffer->size());
@ -199,7 +201,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
0x12345678,
0xABCDEF00,
IPC::MoveHandleDesc(1),
handle_table.Create(a).Unwrap(),
process->handle_table.Create(a).Unwrap(),
IPC::CallingPidDesc(),
0,
IPC::StaticBufferDesc(buffer_static->size(), 0),
@ -208,7 +210,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
target_address_mapped,
};
context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input, *process);
auto* output = context.CommandBuffer();
CHECK(output[1] == 0x12345678);
@ -236,14 +238,13 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
HLERequestContext context(std::move(session));
auto process = kernel.CreateProcess(kernel.CreateCodeSet("", 0));
HandleTable handle_table;
auto* input = context.CommandBuffer();
u32_le output[IPC::COMMAND_BUFFER_LENGTH];
SECTION("works with empty cmdbuf") {
input[0] = IPC::MakeHeader(0x1234, 0, 0);
context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
context.WriteToOutgoingCommandBuffer(output, *process);
REQUIRE(output[0] == 0x12340000);
}
@ -254,7 +255,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
input[2] = 0x21122112;
input[3] = 0xAABBCCDD;
context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
context.WriteToOutgoingCommandBuffer(output, *process);
REQUIRE(output[1] == 0x12345678);
REQUIRE(output[2] == 0x21122112);
@ -270,10 +271,10 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
input[3] = IPC::CopyHandleDesc(1);
input[4] = context.AddOutgoingHandle(b);
context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
context.WriteToOutgoingCommandBuffer(output, *process);
REQUIRE(handle_table.GetGeneric(output[2]) == a);
REQUIRE(handle_table.GetGeneric(output[4]) == b);
REQUIRE(process->handle_table.GetGeneric(output[2]) == a);
REQUIRE(process->handle_table.GetGeneric(output[4]) == b);
}
SECTION("translates null handles") {
@ -281,7 +282,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
input[1] = IPC::MoveHandleDesc(1);
input[2] = context.AddOutgoingHandle(nullptr);
auto result = context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
auto result = context.WriteToOutgoingCommandBuffer(output, *process);
REQUIRE(result == RESULT_SUCCESS);
REQUIRE(output[2] == 0);
@ -298,11 +299,11 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
input[4] = IPC::CopyHandleDesc(1);
input[5] = context.AddOutgoingHandle(c);
context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
context.WriteToOutgoingCommandBuffer(output, *process);
REQUIRE(handle_table.GetGeneric(output[2]) == a);
REQUIRE(handle_table.GetGeneric(output[3]) == b);
REQUIRE(handle_table.GetGeneric(output[5]) == c);
REQUIRE(process->handle_table.GetGeneric(output[2]) == a);
REQUIRE(process->handle_table.GetGeneric(output[3]) == b);
REQUIRE(process->handle_table.GetGeneric(output[5]) == c);
}
SECTION("translates StaticBuffer descriptors") {
@ -329,7 +330,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
IPC::StaticBufferDesc(output_buffer->size(), 0);
output_cmdbuff[IPC::COMMAND_BUFFER_LENGTH + 1] = target_address;
context.WriteToOutgoingCommandBuffer(output_cmdbuff.data(), *process, handle_table);
context.WriteToOutgoingCommandBuffer(output_cmdbuff.data(), *process);
CHECK(*output_buffer == input_buffer);
REQUIRE(process->vm_manager.UnmapRange(target_address, output_buffer->size()) ==
@ -352,7 +353,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
target_address,
};
context.PopulateFromIncomingCommandBuffer(input_cmdbuff, *process, handle_table);
context.PopulateFromIncomingCommandBuffer(input_cmdbuff, *process);
context.GetMappedBuffer(0).Write(input_buffer.data(), 0, input_buffer.size());
@ -360,7 +361,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
input[1] = IPC::MappedBufferDesc(output_buffer->size(), IPC::W);
input[2] = 0;
context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
context.WriteToOutgoingCommandBuffer(output, *process);
CHECK(output[1] == IPC::MappedBufferDesc(output_buffer->size(), IPC::W));
CHECK(output[2] == target_address);