Massive refactor to accommodate RISC-V targets
- Refactored entire codebase (excluding the Insight component) to accommodate multiple target architectures (no longer specific to AVR) - Deleted 'generate SVD' GDB monitor command - I will eventually move this functionality to the Bloom website - Added unit size property to address spaces - Many other changes which I couldn't be bothered to describe here
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
|
||||
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
|
||||
|
||||
#include "src/Services/StringService.hpp"
|
||||
#include "src/Logger/Logger.hpp"
|
||||
#include "src/Exceptions/Exception.hpp"
|
||||
|
||||
@@ -16,124 +17,116 @@ namespace DebugServer::Gdb::AvrGdb::CommandPackets
|
||||
using namespace Exceptions;
|
||||
|
||||
WriteMemory::WriteMemory(const RawPacket& rawPacket, const TargetDescriptor& gdbTargetDescriptor)
|
||||
: CommandPacket(rawPacket)
|
||||
{
|
||||
if (this->data.size() < 4) {
|
||||
throw Exception("Invalid packet length");
|
||||
}
|
||||
: WriteMemory(rawPacket, gdbTargetDescriptor, WriteMemory::extractPacketData(rawPacket))
|
||||
{}
|
||||
|
||||
const auto packetString = QString::fromLocal8Bit(
|
||||
reinterpret_cast<const char*>(this->data.data() + 1),
|
||||
static_cast<int>(this->data.size() - 1)
|
||||
);
|
||||
|
||||
/*
|
||||
* The write memory ('M') packet consists of three segments, an address, a length and a buffer.
|
||||
* The address and length are separated by a comma character, and the buffer proceeds a colon character.
|
||||
*/
|
||||
const auto packetSegments = packetString.split(",");
|
||||
if (packetSegments.size() != 2) {
|
||||
throw Exception(
|
||||
"Unexpected number of segments in packet data: " + std::to_string(packetSegments.size())
|
||||
);
|
||||
}
|
||||
|
||||
bool conversionStatus = false;
|
||||
const auto gdbStartAddress = packetSegments.at(0).toUInt(&conversionStatus, 16);
|
||||
|
||||
if (!conversionStatus) {
|
||||
throw Exception("Failed to parse start address from write memory packet data");
|
||||
}
|
||||
|
||||
this->memoryType = gdbTargetDescriptor.getMemoryTypeFromGdbAddress(gdbStartAddress);
|
||||
this->startAddress = gdbStartAddress & ~(gdbTargetDescriptor.getMemoryOffset(this->memoryType));
|
||||
|
||||
const auto lengthAndBufferSegments = packetSegments.at(1).split(":");
|
||||
if (lengthAndBufferSegments.size() != 2) {
|
||||
throw Exception(
|
||||
"Unexpected number of segments in packet data: "
|
||||
+ std::to_string(lengthAndBufferSegments.size())
|
||||
);
|
||||
}
|
||||
|
||||
const auto bufferSize = lengthAndBufferSegments.at(0).toUInt(&conversionStatus, 16);
|
||||
if (!conversionStatus) {
|
||||
throw Exception("Failed to parse write length from write memory packet data");
|
||||
}
|
||||
|
||||
this->buffer = Packet::hexToData(lengthAndBufferSegments.at(1).toStdString());
|
||||
|
||||
if (this->buffer.size() != bufferSize) {
|
||||
throw Exception("Buffer size does not match length value given in write memory packet");
|
||||
}
|
||||
}
|
||||
|
||||
void WriteMemory::handle(Gdb::DebugSession& debugSession, TargetControllerService& targetControllerService) {
|
||||
void WriteMemory::handle(
|
||||
Gdb::DebugSession& debugSession,
|
||||
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
||||
const Targets::TargetDescriptor& targetDescriptor,
|
||||
TargetControllerService& targetControllerService
|
||||
) {
|
||||
Logger::info("Handling WriteMemory packet");
|
||||
|
||||
try {
|
||||
const auto& memoryDescriptorsByType = debugSession.gdbTargetDescriptor.targetDescriptor.memoryDescriptorsByType;
|
||||
const auto memoryDescriptorIt = memoryDescriptorsByType.find(this->memoryType);
|
||||
|
||||
if (memoryDescriptorIt == memoryDescriptorsByType.end()) {
|
||||
throw Exception("Target does not support the requested memory type.");
|
||||
}
|
||||
|
||||
if (this->memoryType == Targets::TargetMemoryType::FLASH) {
|
||||
/*
|
||||
* This shouldn't happen - GDB should send the FlashWrite (vFlashWrite) packet to write to the target's
|
||||
* program memory.
|
||||
*
|
||||
* A number of actions have to be taken before we can write to the target's program memory - this is
|
||||
* all covered in the FlashWrite and FlashDone command classes. I don't want to cover it again in here,
|
||||
* so just respond with an error and request that this issue be reported.
|
||||
*/
|
||||
throw Exception(
|
||||
"GDB attempted to write to program memory via an \"M\" packet - this is not supported. Please "
|
||||
"report this issue to Bloom developers with the full debug log."
|
||||
);
|
||||
}
|
||||
|
||||
if (this->buffer.size() == 0) {
|
||||
debugSession.connection.writePacket(OkResponsePacket());
|
||||
debugSession.connection.writePacket(OkResponsePacket{});
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& memoryDescriptor = memoryDescriptorIt->second;
|
||||
|
||||
if (this->memoryType == Targets::TargetMemoryType::EEPROM) {
|
||||
// GDB sends EEPROM addresses in relative form - we convert them to absolute form, here.
|
||||
this->startAddress = memoryDescriptor.addressRange.startAddress + this->startAddress;
|
||||
}
|
||||
|
||||
/*
|
||||
* In AVR targets, RAM is mapped to many registers and peripherals - we don't want to block GDB from
|
||||
* accessing them.
|
||||
*/
|
||||
const auto memoryStartAddress = (this->memoryType == Targets::TargetMemoryType::RAM)
|
||||
? 0x00
|
||||
: memoryDescriptor.addressRange.startAddress;
|
||||
|
||||
if (
|
||||
this->startAddress < memoryStartAddress
|
||||
|| (this->startAddress + (this->buffer.size() - 1)) > memoryDescriptor.addressRange.endAddress
|
||||
) {
|
||||
throw Exception(
|
||||
"GDB requested access to memory which is outside the target's memory range"
|
||||
);
|
||||
}
|
||||
|
||||
targetControllerService.writeMemory(
|
||||
this->memoryType,
|
||||
const auto addressRange = Targets::TargetMemoryAddressRange{
|
||||
this->startAddress,
|
||||
this->buffer
|
||||
this->startAddress + static_cast<Targets::TargetMemorySize>(this->buffer.size()) - 1
|
||||
};
|
||||
|
||||
const auto memorySegmentDescriptors = this->addressSpaceDescriptor.getIntersectingMemorySegmentDescriptors(
|
||||
addressRange
|
||||
);
|
||||
|
||||
debugSession.connection.writePacket(OkResponsePacket());
|
||||
auto accessibleBytes = Targets::TargetMemorySize{0};
|
||||
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
||||
if (!memorySegmentDescriptor->debugModeAccess.writeable) {
|
||||
throw Exception{
|
||||
"Attempted to access restricted memory segment (" + memorySegmentDescriptor->key
|
||||
+ ") - segment not writeable in debug mode"
|
||||
};
|
||||
}
|
||||
|
||||
accessibleBytes += memorySegmentDescriptor->addressRange.intersectingSize(addressRange);
|
||||
}
|
||||
|
||||
if (accessibleBytes < this->bytes) {
|
||||
throw Exception{"GDB requested access to memory which does not reside within any memory segment"};
|
||||
}
|
||||
|
||||
{
|
||||
const auto atomicSession = targetControllerService.makeAtomicSession();
|
||||
|
||||
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
||||
const auto segmentStartAddress = std::max(
|
||||
this->startAddress,
|
||||
memorySegmentDescriptor->addressRange.startAddress
|
||||
);
|
||||
|
||||
const auto bufferOffsetIt = buffer.begin() + (segmentStartAddress - this->startAddress);
|
||||
targetControllerService.writeMemory(
|
||||
this->addressSpaceDescriptor,
|
||||
*memorySegmentDescriptor,
|
||||
segmentStartAddress,
|
||||
Targets::TargetMemoryBuffer{
|
||||
bufferOffsetIt,
|
||||
bufferOffsetIt + memorySegmentDescriptor->addressRange.intersectingSize(addressRange)
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
debugSession.connection.writePacket(OkResponsePacket{});
|
||||
|
||||
} catch (const Exception& exception) {
|
||||
Logger::error("Failed to write memory to target - " + exception.getMessage());
|
||||
debugSession.connection.writePacket(ErrorResponsePacket());
|
||||
debugSession.connection.writePacket(ErrorResponsePacket{});
|
||||
}
|
||||
}
|
||||
|
||||
WriteMemory::PacketData WriteMemory::extractPacketData(const RawPacket& rawPacket) {
|
||||
using Services::StringService;
|
||||
|
||||
if (rawPacket.size() < 8) {
|
||||
throw Exception{"Invalid packet length"};
|
||||
}
|
||||
|
||||
const auto command = std::string{rawPacket.begin() + 2, rawPacket.end() - 3};
|
||||
|
||||
const auto commaDelimiterPos = command.find_first_of(',');
|
||||
const auto colonDelimiterPos = command.find_first_of(':');
|
||||
if (commaDelimiterPos == std::string::npos || colonDelimiterPos == std::string::npos) {
|
||||
throw Exception{"Invalid packet"};
|
||||
}
|
||||
|
||||
return {
|
||||
StringService::toUint32(command.substr(0, commaDelimiterPos), 16),
|
||||
StringService::toUint32(
|
||||
command.substr(commaDelimiterPos + 1, colonDelimiterPos - (commaDelimiterPos + 1)),
|
||||
16
|
||||
),
|
||||
StringService::dataFromHex(command.substr(colonDelimiterPos + 1))
|
||||
};
|
||||
}
|
||||
|
||||
WriteMemory::WriteMemory(
|
||||
const RawPacket& rawPacket,
|
||||
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
||||
PacketData&& packetData
|
||||
)
|
||||
: CommandPacket(rawPacket)
|
||||
, addressSpaceDescriptor(gdbTargetDescriptor.addressSpaceDescriptorFromGdbAddress(packetData.gdbStartAddress))
|
||||
, startAddress(gdbTargetDescriptor.translateGdbAddress(packetData.gdbStartAddress))
|
||||
, bytes(packetData.bytes)
|
||||
, buffer(std::move(packetData.buffer))
|
||||
{
|
||||
if (this->buffer.size() != this->bytes) {
|
||||
throw Exception{"Buffer size does not match length value given in write memory packet"};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user