adding base command buffer submit and some buffer commands in phi
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#include <Buffer.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int PhiIsBufferCommand(const PhiCmdHeader* header)
|
||||
{
|
||||
switch((PhiCmdType)header->type)
|
||||
{
|
||||
case PHI_CMD_COPY_BUFFER:
|
||||
case PHI_CMD_FILL_BUFFER:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static PhiStatus CopyBuffer(PhiCommandReader* reader)
|
||||
{
|
||||
PhiCmdCopyBuffer command;
|
||||
PhiStatus status = PhiReadCommandData(reader, &command, sizeof(command));
|
||||
if(status != PHI_STATUS_OK)
|
||||
return status;
|
||||
|
||||
if(command.src_memory == 0 || command.dst_memory == 0)
|
||||
return PHI_STATUS_INVALID_HANDLE;
|
||||
|
||||
void* dst = (void*)((uintptr_t)command.dst_memory + (uintptr_t)command.dst_offset);
|
||||
const void* src = (const void*)((uintptr_t)command.src_memory + (uintptr_t)command.src_offset);
|
||||
|
||||
memcpy(dst, src, (size_t)command.size);
|
||||
|
||||
return PHI_STATUS_OK;
|
||||
}
|
||||
|
||||
static PhiStatus FillBuffer(PhiCommandReader* reader)
|
||||
{
|
||||
PhiCmdFillBuffer command;
|
||||
PhiStatus status = PhiReadCommandData(reader, &command, sizeof(command));
|
||||
if(status != PHI_STATUS_OK)
|
||||
return status;
|
||||
|
||||
if(command.memory == 0)
|
||||
return PHI_STATUS_INVALID_HANDLE;
|
||||
|
||||
uint32_t* dst = (uint32_t*)((uintptr_t)command.memory + (uintptr_t)command.offset);
|
||||
|
||||
for(; command.size >= 4; command.size -= 4, dst++)
|
||||
*dst = command.data;
|
||||
|
||||
return PHI_STATUS_OK;
|
||||
}
|
||||
|
||||
PhiStatus PhiExecuteBufferCommand(PhiCommandReader* reader, const PhiCmdHeader* header)
|
||||
{
|
||||
switch((PhiCmdType)header->type)
|
||||
{
|
||||
case PHI_CMD_COPY_BUFFER:
|
||||
return CopyBuffer(reader);
|
||||
case PHI_CMD_FILL_BUFFER:
|
||||
return FillBuffer(reader);
|
||||
|
||||
default:
|
||||
return PHI_STATUS_BAD_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef APE_PHI_BUFFER_H
|
||||
#define APE_PHI_BUFFER_H
|
||||
|
||||
#include <CommandBuffer.h>
|
||||
|
||||
int PhiIsBufferCommand(const PhiCmdHeader* header);
|
||||
PhiStatus PhiExecuteBufferCommand(PhiCommandReader* reader, const PhiCmdHeader* header);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <CommandBuffer.h>
|
||||
|
||||
#include <Buffer.h>
|
||||
|
||||
PhiStatus PhiReadCommandData(PhiCommandReader* reader, void* data, uint64_t size)
|
||||
{
|
||||
if(reader->remaining < size)
|
||||
return PHI_STATUS_BAD_MESSAGE;
|
||||
|
||||
if(ReadAll(reader->endpoint, data, (size_t)size) < 0)
|
||||
return PHI_STATUS_BAD_MESSAGE;
|
||||
|
||||
reader->remaining -= size;
|
||||
return PHI_STATUS_OK;
|
||||
}
|
||||
|
||||
int PhiDrainCommandReader(PhiCommandReader* reader)
|
||||
{
|
||||
if(reader->remaining == 0)
|
||||
return 0;
|
||||
|
||||
int result = DrainPayload(reader->endpoint, reader->remaining);
|
||||
reader->remaining = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
static PhiStatus ReadCommandHeader(PhiCommandReader* reader, PhiCmdHeader* command_header)
|
||||
{
|
||||
PhiStatus status = PhiReadCommandData(reader, command_header, sizeof(*command_header));
|
||||
if(status != PHI_STATUS_OK)
|
||||
return status;
|
||||
|
||||
if(command_header->magic != PHI_COMMAND_MAGIC)
|
||||
return PHI_STATUS_BAD_MESSAGE;
|
||||
|
||||
return PHI_STATUS_OK;
|
||||
}
|
||||
|
||||
static PhiStatus ExecuteCommand(PhiCommandReader* reader, const PhiCmdHeader* command_header)
|
||||
{
|
||||
if(PhiIsBufferCommand(command_header))
|
||||
return PhiExecuteBufferCommand(reader, command_header);
|
||||
|
||||
return PHI_STATUS_BAD_MESSAGE;
|
||||
}
|
||||
|
||||
int HandleWorkExecution(scif_epd_t endpoint, const PhiMessageHeader* header)
|
||||
{
|
||||
PhiWorkExecutionRequest request;
|
||||
PhiWorkExecutionReply reply = {
|
||||
.result = {
|
||||
.status = PHI_STATUS_OK,
|
||||
.reserved = 0,
|
||||
},
|
||||
};
|
||||
|
||||
if(header->payload_size < sizeof(request))
|
||||
{
|
||||
if(DrainPayload(endpoint, header->payload_size) < 0)
|
||||
return -1;
|
||||
reply.result.status = PHI_STATUS_BAD_MESSAGE;
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
if(ReadAll(endpoint, &request, sizeof(request)) < 0)
|
||||
return -1;
|
||||
|
||||
PhiCommandReader reader = {
|
||||
.endpoint = endpoint,
|
||||
.remaining = header->payload_size - sizeof(request),
|
||||
};
|
||||
|
||||
if(reader.remaining != request.command_buffer_size)
|
||||
{
|
||||
if(PhiDrainCommandReader(&reader) < 0)
|
||||
return -1;
|
||||
reply.result.status = PHI_STATUS_BAD_MESSAGE;
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
|
||||
for(uint64_t cmd_index = 0; cmd_index < request.cmd_count; ++cmd_index)
|
||||
{
|
||||
PhiCmdHeader cmd_header;
|
||||
reply.result.status = ReadCommandHeader(&reader, &cmd_header);
|
||||
if(reply.result.status != PHI_STATUS_OK)
|
||||
break;
|
||||
|
||||
reply.result.status = ExecuteCommand(&reader, &cmd_header);
|
||||
if(reply.result.status != PHI_STATUS_OK)
|
||||
break;
|
||||
}
|
||||
|
||||
if(reader.remaining > 0 && PhiDrainCommandReader(&reader) < 0)
|
||||
return -1;
|
||||
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef APE_PHI_COMMAND_BUFFER_H
|
||||
#define APE_PHI_COMMAND_BUFFER_H
|
||||
|
||||
#include <Daemon.h>
|
||||
|
||||
typedef struct PhiCommandReader
|
||||
{
|
||||
scif_epd_t endpoint;
|
||||
uint64_t remaining;
|
||||
} PhiCommandReader;
|
||||
|
||||
int HandleWorkExecution(scif_epd_t endpoint, const PhiMessageHeader* header);
|
||||
int PhiDrainCommandReader(PhiCommandReader* reader);
|
||||
PhiStatus PhiReadCommandData(PhiCommandReader* reader, void* data, uint64_t size);
|
||||
|
||||
#endif
|
||||
+12
-6
@@ -1,4 +1,5 @@
|
||||
#include <Daemon.h>
|
||||
#include <CommandBuffer.h>
|
||||
#include <Logger.h>
|
||||
#include <Memory.h>
|
||||
|
||||
@@ -83,24 +84,29 @@ int HandlePacket(scif_epd_t endpoint)
|
||||
continue;
|
||||
}
|
||||
|
||||
switch((PhiCommandType)header.type)
|
||||
switch((PhiPacketType)header.type)
|
||||
{
|
||||
case PHI_COMMAND_HELLO:
|
||||
case PHI_PACKET_HELLO:
|
||||
if(HandleHello(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_ALLOC_MEMORY:
|
||||
case PHI_PACKET_ALLOC_MEMORY:
|
||||
if(HandleAllocMemory(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_FREE_MEMORY:
|
||||
case PHI_PACKET_FREE_MEMORY:
|
||||
if(HandleFreeMemory(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_SHUTDOWN:
|
||||
case PHI_PACKET_WORK_EXECUTION:
|
||||
if(HandleWorkExecution(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_PACKET_SHUTDOWN:
|
||||
if(DrainPayload(endpoint, header.payload_size) < 0)
|
||||
return -1;
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_OK) < 0)
|
||||
@@ -110,7 +116,7 @@ int HandlePacket(scif_epd_t endpoint)
|
||||
default:
|
||||
if(DrainPayload(endpoint, header.payload_size) < 0)
|
||||
return -1;
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_UNSUPPORTED_COMMAND) < 0)
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_UNSUPPORTED_PACKET) < 0)
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user