1fcbc3db1SBrandon Kim #include "buffer.hpp" 2fcbc3db1SBrandon Kim 3fcbc3db1SBrandon Kim #include "pci_handler.hpp" 4fcbc3db1SBrandon Kim 5fcbc3db1SBrandon Kim #include <fmt/format.h> 6fcbc3db1SBrandon Kim 717ee1a93SBrandon Kim #include <boost/endian/arithmetic.hpp> 817ee1a93SBrandon Kim #include <boost/endian/conversion.hpp> 917ee1a93SBrandon Kim 1017ee1a93SBrandon Kim #include <algorithm> 11fcbc3db1SBrandon Kim #include <array> 12cf0b9752SBrandon Kim #include <cstddef> 13fcbc3db1SBrandon Kim #include <cstdint> 14fcbc3db1SBrandon Kim #include <memory> 1540ce08e1SBrandon Kim #include <numeric> 16fcbc3db1SBrandon Kim #include <span> 17fcbc3db1SBrandon Kim #include <vector> 18fcbc3db1SBrandon Kim 19fcbc3db1SBrandon Kim namespace bios_bmc_smm_error_logger 20fcbc3db1SBrandon Kim { 21fcbc3db1SBrandon Kim 22fcbc3db1SBrandon Kim BufferImpl::BufferImpl(std::unique_ptr<DataInterface> dataInterface) : 23fcbc3db1SBrandon Kim dataInterface(std::move(dataInterface)){}; 24fcbc3db1SBrandon Kim 25fcbc3db1SBrandon Kim void BufferImpl::initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize, 26fcbc3db1SBrandon Kim uint16_t ueRegionSize, 27fcbc3db1SBrandon Kim const std::array<uint32_t, 4>& magicNumber) 28fcbc3db1SBrandon Kim { 29fcbc3db1SBrandon Kim const size_t memoryRegionSize = dataInterface->getMemoryRegionSize(); 3026660e9bSBrandon Kim const size_t proposedBufferSize = 3126660e9bSBrandon Kim sizeof(struct CircularBufferHeader) + ueRegionSize + queueSize; 3226660e9bSBrandon Kim if (proposedBufferSize > memoryRegionSize) 3326660e9bSBrandon Kim { 3426660e9bSBrandon Kim throw std::runtime_error(fmt::format( 3526660e9bSBrandon Kim "[initialize] Proposed region size '{}' is bigger than the " 3626660e9bSBrandon Kim "BMC's allocated MMIO region of '{}'", 3726660e9bSBrandon Kim proposedBufferSize, memoryRegionSize)); 3826660e9bSBrandon Kim } 3926660e9bSBrandon Kim 4026660e9bSBrandon Kim // Initialize the whole buffer with 0x00 4126660e9bSBrandon Kim const std::vector<uint8_t> emptyVector(proposedBufferSize, 0); 42fcbc3db1SBrandon Kim size_t byteWritten = dataInterface->write(0, emptyVector); 4326660e9bSBrandon Kim if (byteWritten != proposedBufferSize) 44fcbc3db1SBrandon Kim { 45fcbc3db1SBrandon Kim throw std::runtime_error( 4626660e9bSBrandon Kim fmt::format("[initialize] Only erased '{}'", byteWritten)); 47fcbc3db1SBrandon Kim } 48fcbc3db1SBrandon Kim 49fcbc3db1SBrandon Kim // Create an initial buffer header and write to it 50fcbc3db1SBrandon Kim struct CircularBufferHeader initializationHeader = {}; 5117ee1a93SBrandon Kim initializationHeader.bmcInterfaceVersion = 5217ee1a93SBrandon Kim boost::endian::native_to_little(bmcInterfaceVersion); 5317ee1a93SBrandon Kim initializationHeader.queueSize = boost::endian::native_to_little(queueSize); 5417ee1a93SBrandon Kim initializationHeader.ueRegionSize = 5517ee1a93SBrandon Kim boost::endian::native_to_little(ueRegionSize); 5617ee1a93SBrandon Kim std::transform(magicNumber.begin(), magicNumber.end(), 5717ee1a93SBrandon Kim initializationHeader.magicNumber.begin(), 5817ee1a93SBrandon Kim [](uint32_t number) -> little_uint32_t { 5917ee1a93SBrandon Kim return boost::endian::native_to_little(number); 6017ee1a93SBrandon Kim }); 61fcbc3db1SBrandon Kim 62fcbc3db1SBrandon Kim uint8_t* initializationHeaderPtr = 63fcbc3db1SBrandon Kim reinterpret_cast<uint8_t*>(&initializationHeader); 64fcbc3db1SBrandon Kim size_t initializationHeaderSize = sizeof(initializationHeader); 65fcbc3db1SBrandon Kim byteWritten = dataInterface->write( 66fcbc3db1SBrandon Kim 0, std::span<const uint8_t>(initializationHeaderPtr, 67fcbc3db1SBrandon Kim initializationHeaderPtr + 68fcbc3db1SBrandon Kim initializationHeaderSize)); 69fcbc3db1SBrandon Kim if (byteWritten != initializationHeaderSize) 70fcbc3db1SBrandon Kim { 71fcbc3db1SBrandon Kim throw std::runtime_error(fmt::format( 7226660e9bSBrandon Kim "[initialize] Only wrote '{}' bytes of the header", byteWritten)); 73fcbc3db1SBrandon Kim } 7460cab57fSBrandon Kim cachedBufferHeader = initializationHeader; 75fcbc3db1SBrandon Kim } 76fcbc3db1SBrandon Kim 7717ee1a93SBrandon Kim void BufferImpl::readBufferHeader() 7817ee1a93SBrandon Kim { 7917ee1a93SBrandon Kim size_t headerSize = sizeof(struct CircularBufferHeader); 8017ee1a93SBrandon Kim std::vector<uint8_t> bytesRead = 814662b1bdSBrandon Kim dataInterface->read(/*offset=*/0, headerSize); 8217ee1a93SBrandon Kim 8317ee1a93SBrandon Kim if (bytesRead.size() != headerSize) 8417ee1a93SBrandon Kim { 8517ee1a93SBrandon Kim throw std::runtime_error( 8617ee1a93SBrandon Kim fmt::format("Buffer header read only read '{}', expected '{}'", 8717ee1a93SBrandon Kim bytesRead.size(), headerSize)); 8817ee1a93SBrandon Kim } 8917ee1a93SBrandon Kim 9017ee1a93SBrandon Kim cachedBufferHeader = 9160cab57fSBrandon Kim *reinterpret_cast<struct CircularBufferHeader*>(bytesRead.data()); 9217ee1a93SBrandon Kim }; 9317ee1a93SBrandon Kim 9417ee1a93SBrandon Kim struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const 9517ee1a93SBrandon Kim { 9617ee1a93SBrandon Kim return cachedBufferHeader; 9717ee1a93SBrandon Kim } 9817ee1a93SBrandon Kim 99cf0b9752SBrandon Kim void BufferImpl::updateReadPtr(const uint32_t newReadPtr) 100cf0b9752SBrandon Kim { 101cf0b9752SBrandon Kim constexpr uint8_t bmcReadPtrOffset = 102cf0b9752SBrandon Kim offsetof(struct CircularBufferHeader, bmcReadPtr); 103cf0b9752SBrandon Kim 104*271d2313SBrandon Kim little_uint24_t truncatedReadPtr = 105*271d2313SBrandon Kim boost::endian::native_to_little(newReadPtr & 0xffffff); 106cf0b9752SBrandon Kim uint8_t* truncatedReadPtrPtr = 107cf0b9752SBrandon Kim reinterpret_cast<uint8_t*>(&truncatedReadPtr); 108cf0b9752SBrandon Kim 109cf0b9752SBrandon Kim size_t writtenSize = dataInterface->write( 110cf0b9752SBrandon Kim bmcReadPtrOffset, std::span<const uint8_t>{ 111cf0b9752SBrandon Kim truncatedReadPtrPtr, 112*271d2313SBrandon Kim truncatedReadPtrPtr + sizeof(truncatedReadPtr)}); 113*271d2313SBrandon Kim if (writtenSize != sizeof(truncatedReadPtr)) 114cf0b9752SBrandon Kim { 115cf0b9752SBrandon Kim throw std::runtime_error(fmt::format( 116cf0b9752SBrandon Kim "[updateReadPtr] Wrote '{}' bytes, instead of expected '{}'", 117*271d2313SBrandon Kim writtenSize, sizeof(truncatedReadPtr))); 118cf0b9752SBrandon Kim } 119cf0b9752SBrandon Kim cachedBufferHeader.bmcReadPtr = truncatedReadPtr; 120cf0b9752SBrandon Kim } 121cf0b9752SBrandon Kim 122c49284b6SBrandon Kim void BufferImpl::updateBmcFlags(const uint32_t newBmcFlag) 123c49284b6SBrandon Kim { 124c49284b6SBrandon Kim constexpr uint8_t bmcFlagsPtrOffset = 125c49284b6SBrandon Kim offsetof(struct CircularBufferHeader, bmcFlags); 126c49284b6SBrandon Kim 127c49284b6SBrandon Kim little_uint32_t littleNewBmcFlag = 128c49284b6SBrandon Kim boost::endian::native_to_little(newBmcFlag); 129c49284b6SBrandon Kim uint8_t* littleNewBmcFlagPtr = 130c49284b6SBrandon Kim reinterpret_cast<uint8_t*>(&littleNewBmcFlag); 131c49284b6SBrandon Kim 132c49284b6SBrandon Kim size_t writtenSize = dataInterface->write( 133c49284b6SBrandon Kim bmcFlagsPtrOffset, std::span<const uint8_t>{ 134c49284b6SBrandon Kim littleNewBmcFlagPtr, 135c49284b6SBrandon Kim littleNewBmcFlagPtr + sizeof(little_uint32_t)}); 136c49284b6SBrandon Kim if (writtenSize != sizeof(little_uint32_t)) 137c49284b6SBrandon Kim { 138c49284b6SBrandon Kim throw std::runtime_error(fmt::format( 139c49284b6SBrandon Kim "[updateBmcFlags] Wrote '{}' bytes, instead of expected '{}'", 140c49284b6SBrandon Kim writtenSize, sizeof(little_uint32_t))); 141c49284b6SBrandon Kim } 142c49284b6SBrandon Kim cachedBufferHeader.bmcFlags = littleNewBmcFlag; 143c49284b6SBrandon Kim } 144c49284b6SBrandon Kim 1459836cfa6SBrandon Kim size_t BufferImpl::getQueueOffset() 1469836cfa6SBrandon Kim { 1479836cfa6SBrandon Kim return sizeof(struct CircularBufferHeader) + 1489836cfa6SBrandon Kim boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); 1499836cfa6SBrandon Kim } 1509836cfa6SBrandon Kim 15135d4335eSBrandon Kim std::vector<uint8_t> BufferImpl::wraparoundRead(const uint32_t relativeOffset, 15235d4335eSBrandon Kim const uint32_t length) 1539836cfa6SBrandon Kim { 15426660e9bSBrandon Kim const size_t queueSize = 15526660e9bSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.queueSize); 1569836cfa6SBrandon Kim 15735d4335eSBrandon Kim if (relativeOffset > queueSize) 15835d4335eSBrandon Kim { 15935d4335eSBrandon Kim throw std::runtime_error( 16035d4335eSBrandon Kim fmt::format("[wraparoundRead] relativeOffset '{}' was bigger " 16135d4335eSBrandon Kim "than queueSize '{}'", 16235d4335eSBrandon Kim relativeOffset, queueSize)); 16335d4335eSBrandon Kim } 16435d4335eSBrandon Kim if (length > queueSize) 1659836cfa6SBrandon Kim { 1669836cfa6SBrandon Kim throw std::runtime_error(fmt::format( 16735d4335eSBrandon Kim "[wraparoundRead] length '{}' was bigger than queueSize '{}'", 16835d4335eSBrandon Kim length, queueSize)); 1699836cfa6SBrandon Kim } 1709836cfa6SBrandon Kim 17135d4335eSBrandon Kim // Do a calculation to see if the read will wraparound 17235d4335eSBrandon Kim const size_t queueOffset = getQueueOffset(); 17335d4335eSBrandon Kim const size_t queueSizeToQueueEnd = queueSize - relativeOffset; 17435d4335eSBrandon Kim size_t numWraparoundBytesToRead = 0; 17535d4335eSBrandon Kim if (length > queueSizeToQueueEnd) 17635d4335eSBrandon Kim { 17735d4335eSBrandon Kim // This means we will wrap, count the bytes that are left to read 17835d4335eSBrandon Kim numWraparoundBytesToRead = length - queueSizeToQueueEnd; 17935d4335eSBrandon Kim } 18035d4335eSBrandon Kim const size_t numBytesToReadTillQueueEnd = length - numWraparoundBytesToRead; 18135d4335eSBrandon Kim 18235d4335eSBrandon Kim std::vector<uint8_t> bytesRead = dataInterface->read( 18335d4335eSBrandon Kim queueOffset + relativeOffset, numBytesToReadTillQueueEnd); 18435d4335eSBrandon Kim if (bytesRead.size() != numBytesToReadTillQueueEnd) 18535d4335eSBrandon Kim { 18635d4335eSBrandon Kim throw std::runtime_error( 18735d4335eSBrandon Kim fmt::format("[wraparoundRead] Read '{}' which was not " 18835d4335eSBrandon Kim "the requested length of '{}'", 18935d4335eSBrandon Kim bytesRead.size(), numBytesToReadTillQueueEnd)); 19035d4335eSBrandon Kim } 19135d4335eSBrandon Kim size_t updatedReadPtr = relativeOffset + numBytesToReadTillQueueEnd; 1924662b1bdSBrandon Kim if (updatedReadPtr == queueSize) 1934662b1bdSBrandon Kim { 1944662b1bdSBrandon Kim // If we read all the way up to the end of the queue, we need to 1954662b1bdSBrandon Kim // manually wrap the updateReadPtr around to 0 1964662b1bdSBrandon Kim updatedReadPtr = 0; 1974662b1bdSBrandon Kim } 1989836cfa6SBrandon Kim 1999836cfa6SBrandon Kim // If there are any more bytes to be read beyond the buffer, wrap around and 2009836cfa6SBrandon Kim // read from the beginning of the buffer (offset by the queueOffset) 20135d4335eSBrandon Kim if (numWraparoundBytesToRead > 0) 2029836cfa6SBrandon Kim { 2039836cfa6SBrandon Kim std::vector<uint8_t> wrappedBytesRead = 20435d4335eSBrandon Kim dataInterface->read(queueOffset, numWraparoundBytesToRead); 20535d4335eSBrandon Kim if (numWraparoundBytesToRead != wrappedBytesRead.size()) 2069836cfa6SBrandon Kim { 2079836cfa6SBrandon Kim throw std::runtime_error(fmt::format( 20835d4335eSBrandon Kim "[wraparoundRead] Buffer wrapped around but read '{}' which " 20935d4335eSBrandon Kim "was not the requested lenght of '{}'", 21035d4335eSBrandon Kim wrappedBytesRead.size(), numWraparoundBytesToRead)); 2119836cfa6SBrandon Kim } 2129836cfa6SBrandon Kim bytesRead.insert(bytesRead.end(), wrappedBytesRead.begin(), 2139836cfa6SBrandon Kim wrappedBytesRead.end()); 21435d4335eSBrandon Kim updatedReadPtr = numWraparoundBytesToRead; 2159836cfa6SBrandon Kim } 21635d4335eSBrandon Kim updateReadPtr(updatedReadPtr); 2179836cfa6SBrandon Kim 2189836cfa6SBrandon Kim return bytesRead; 2199836cfa6SBrandon Kim } 2209836cfa6SBrandon Kim 22135d4335eSBrandon Kim struct QueueEntryHeader BufferImpl::readEntryHeader(size_t relativeOffset) 2227bac2d69SBrandon Kim { 2237bac2d69SBrandon Kim size_t headerSize = sizeof(struct QueueEntryHeader); 2247bac2d69SBrandon Kim // wraparonudRead will throw if it did not read all the bytes, let it 2257bac2d69SBrandon Kim // propagate up the stack 22635d4335eSBrandon Kim std::vector<uint8_t> bytesRead = wraparoundRead(relativeOffset, headerSize); 2277bac2d69SBrandon Kim 2287bac2d69SBrandon Kim return *reinterpret_cast<struct QueueEntryHeader*>(bytesRead.data()); 2297bac2d69SBrandon Kim } 2307bac2d69SBrandon Kim 23135d4335eSBrandon Kim EntryPair BufferImpl::readEntry(size_t relativeOffset) 23240ce08e1SBrandon Kim { 23335d4335eSBrandon Kim struct QueueEntryHeader entryHeader = readEntryHeader(relativeOffset); 23435d4335eSBrandon Kim size_t entrySize = boost::endian::little_to_native(entryHeader.entrySize); 23540ce08e1SBrandon Kim 23640ce08e1SBrandon Kim // wraparonudRead may throw if entrySize was bigger than the buffer or if it 23726660e9bSBrandon Kim // was not able to read all the bytes, let it propagate up the stack 23835d4335eSBrandon Kim std::vector<uint8_t> entry = wraparoundRead( 23935d4335eSBrandon Kim relativeOffset + sizeof(struct QueueEntryHeader), entrySize); 24040ce08e1SBrandon Kim 24140ce08e1SBrandon Kim // Calculate the checksum 24240ce08e1SBrandon Kim uint8_t* entryHeaderPtr = reinterpret_cast<uint8_t*>(&entryHeader); 243f0e4adc9SBrandon Kim uint8_t checksum = 244f0e4adc9SBrandon Kim std::accumulate(entryHeaderPtr, 245f0e4adc9SBrandon Kim entryHeaderPtr + sizeof(struct QueueEntryHeader), 0, 246f0e4adc9SBrandon Kim std::bit_xor<void>()) ^ 247f0e4adc9SBrandon Kim std::accumulate(entry.begin(), entry.end(), 0, std::bit_xor<void>()); 248f0e4adc9SBrandon Kim 24940ce08e1SBrandon Kim if (checksum != 0) 25040ce08e1SBrandon Kim { 25140ce08e1SBrandon Kim throw std::runtime_error(fmt::format( 25240ce08e1SBrandon Kim "[readEntry] Checksum was '{}', expected '0'", checksum)); 25340ce08e1SBrandon Kim } 25440ce08e1SBrandon Kim 25540ce08e1SBrandon Kim return {entryHeader, entry}; 25640ce08e1SBrandon Kim } 25740ce08e1SBrandon Kim 2584662b1bdSBrandon Kim std::vector<EntryPair> BufferImpl::readErrorLogs() 2594662b1bdSBrandon Kim { 2604662b1bdSBrandon Kim // Reading the buffer header will update the cachedBufferHeader 2614662b1bdSBrandon Kim readBufferHeader(); 2624662b1bdSBrandon Kim 2634662b1bdSBrandon Kim const size_t queueSize = 2644662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.queueSize); 2654662b1bdSBrandon Kim size_t currentBiosWritePtr = 2664662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.biosWritePtr); 2674662b1bdSBrandon Kim if (currentBiosWritePtr > queueSize) 2684662b1bdSBrandon Kim { 2694662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 2704662b1bdSBrandon Kim "[readErrorLogs] currentBiosWritePtr was '{}' which was bigger " 2714662b1bdSBrandon Kim "than queueSize '{}'", 2724662b1bdSBrandon Kim currentBiosWritePtr, queueSize)); 2734662b1bdSBrandon Kim } 2744662b1bdSBrandon Kim size_t currentReadPtr = 2754662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); 2764662b1bdSBrandon Kim if (currentReadPtr > queueSize) 2774662b1bdSBrandon Kim { 2784662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 2794662b1bdSBrandon Kim "[readErrorLogs] currentReadPtr was '{}' which was bigger " 2804662b1bdSBrandon Kim "than queueSize '{}'", 2814662b1bdSBrandon Kim currentReadPtr, queueSize)); 2824662b1bdSBrandon Kim } 2834662b1bdSBrandon Kim 2844662b1bdSBrandon Kim size_t bytesToRead; 2854662b1bdSBrandon Kim if (currentBiosWritePtr == currentReadPtr) 2864662b1bdSBrandon Kim { 2874662b1bdSBrandon Kim // No new payload was detected, return an empty vector gracefully 2884662b1bdSBrandon Kim return {}; 2894662b1bdSBrandon Kim } 2904662b1bdSBrandon Kim 2914662b1bdSBrandon Kim if (currentBiosWritePtr > currentReadPtr) 2924662b1bdSBrandon Kim { 2934662b1bdSBrandon Kim // Simply subtract in this case 2944662b1bdSBrandon Kim bytesToRead = currentBiosWritePtr - currentReadPtr; 2954662b1bdSBrandon Kim } 2964662b1bdSBrandon Kim else 2974662b1bdSBrandon Kim { 2984662b1bdSBrandon Kim // Calculate the bytes to the "end" (QueueSize - ReadPtr) + 2994662b1bdSBrandon Kim // bytes to read from the "beginning" (0 + WritePtr) 3004662b1bdSBrandon Kim bytesToRead = (queueSize - currentReadPtr) + currentBiosWritePtr; 3014662b1bdSBrandon Kim } 3024662b1bdSBrandon Kim 3034662b1bdSBrandon Kim size_t byteRead = 0; 3044662b1bdSBrandon Kim std::vector<EntryPair> entryPairs; 3054662b1bdSBrandon Kim while (byteRead < bytesToRead) 3064662b1bdSBrandon Kim { 3074662b1bdSBrandon Kim EntryPair entryPair = readEntry(currentReadPtr); 3084662b1bdSBrandon Kim byteRead += sizeof(struct QueueEntryHeader) + entryPair.second.size(); 3094662b1bdSBrandon Kim entryPairs.push_back(entryPair); 3104662b1bdSBrandon Kim 3114662b1bdSBrandon Kim // Note: readEntry() will update cachedBufferHeader.bmcReadPtr 3124662b1bdSBrandon Kim currentReadPtr = 3134662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); 3144662b1bdSBrandon Kim } 3154662b1bdSBrandon Kim if (currentBiosWritePtr != currentReadPtr) 3164662b1bdSBrandon Kim { 3174662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 3184662b1bdSBrandon Kim "[readErrorLogs] biosWritePtr '{}' and bmcReaddPtr '{}' " 3194662b1bdSBrandon Kim "are not identical after reading through all the logs", 3204662b1bdSBrandon Kim currentBiosWritePtr, currentReadPtr)); 3214662b1bdSBrandon Kim } 3224662b1bdSBrandon Kim return entryPairs; 3234662b1bdSBrandon Kim } 3244662b1bdSBrandon Kim 325fcbc3db1SBrandon Kim } // namespace bios_bmc_smm_error_logger 326