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(); 30*3def3c8eSBrandon Kim if (queueSize > memoryRegionSize) 3126660e9bSBrandon Kim { 3226660e9bSBrandon Kim throw std::runtime_error(fmt::format( 33*3def3c8eSBrandon Kim "[initialize] Proposed queue size '{}' is bigger than the " 3426660e9bSBrandon Kim "BMC's allocated MMIO region of '{}'", 35*3def3c8eSBrandon Kim queueSize, memoryRegionSize)); 3626660e9bSBrandon Kim } 3726660e9bSBrandon Kim 3826660e9bSBrandon Kim // Initialize the whole buffer with 0x00 39*3def3c8eSBrandon Kim const std::vector<uint8_t> emptyVector(queueSize, 0); 40fcbc3db1SBrandon Kim size_t byteWritten = dataInterface->write(0, emptyVector); 41*3def3c8eSBrandon Kim if (byteWritten != queueSize) 42fcbc3db1SBrandon Kim { 43fcbc3db1SBrandon Kim throw std::runtime_error( 4426660e9bSBrandon Kim fmt::format("[initialize] Only erased '{}'", byteWritten)); 45fcbc3db1SBrandon Kim } 46fcbc3db1SBrandon Kim 47fcbc3db1SBrandon Kim // Create an initial buffer header and write to it 48fcbc3db1SBrandon Kim struct CircularBufferHeader initializationHeader = {}; 4917ee1a93SBrandon Kim initializationHeader.bmcInterfaceVersion = 5017ee1a93SBrandon Kim boost::endian::native_to_little(bmcInterfaceVersion); 5117ee1a93SBrandon Kim initializationHeader.queueSize = boost::endian::native_to_little(queueSize); 5217ee1a93SBrandon Kim initializationHeader.ueRegionSize = 5317ee1a93SBrandon Kim boost::endian::native_to_little(ueRegionSize); 5417ee1a93SBrandon Kim std::transform(magicNumber.begin(), magicNumber.end(), 5517ee1a93SBrandon Kim initializationHeader.magicNumber.begin(), 5617ee1a93SBrandon Kim [](uint32_t number) -> little_uint32_t { 5717ee1a93SBrandon Kim return boost::endian::native_to_little(number); 5817ee1a93SBrandon Kim }); 59fcbc3db1SBrandon Kim 60fcbc3db1SBrandon Kim uint8_t* initializationHeaderPtr = 61fcbc3db1SBrandon Kim reinterpret_cast<uint8_t*>(&initializationHeader); 62fcbc3db1SBrandon Kim size_t initializationHeaderSize = sizeof(initializationHeader); 63fcbc3db1SBrandon Kim byteWritten = dataInterface->write( 64fcbc3db1SBrandon Kim 0, std::span<const uint8_t>(initializationHeaderPtr, 65fcbc3db1SBrandon Kim initializationHeaderPtr + 66fcbc3db1SBrandon Kim initializationHeaderSize)); 67fcbc3db1SBrandon Kim if (byteWritten != initializationHeaderSize) 68fcbc3db1SBrandon Kim { 69fcbc3db1SBrandon Kim throw std::runtime_error(fmt::format( 7026660e9bSBrandon Kim "[initialize] Only wrote '{}' bytes of the header", byteWritten)); 71fcbc3db1SBrandon Kim } 7260cab57fSBrandon Kim cachedBufferHeader = initializationHeader; 73fcbc3db1SBrandon Kim } 74fcbc3db1SBrandon Kim 7517ee1a93SBrandon Kim void BufferImpl::readBufferHeader() 7617ee1a93SBrandon Kim { 7717ee1a93SBrandon Kim size_t headerSize = sizeof(struct CircularBufferHeader); 7817ee1a93SBrandon Kim std::vector<uint8_t> bytesRead = 794662b1bdSBrandon Kim dataInterface->read(/*offset=*/0, headerSize); 8017ee1a93SBrandon Kim 8117ee1a93SBrandon Kim if (bytesRead.size() != headerSize) 8217ee1a93SBrandon Kim { 8317ee1a93SBrandon Kim throw std::runtime_error( 8417ee1a93SBrandon Kim fmt::format("Buffer header read only read '{}', expected '{}'", 8517ee1a93SBrandon Kim bytesRead.size(), headerSize)); 8617ee1a93SBrandon Kim } 8717ee1a93SBrandon Kim 8817ee1a93SBrandon Kim cachedBufferHeader = 8960cab57fSBrandon Kim *reinterpret_cast<struct CircularBufferHeader*>(bytesRead.data()); 9017ee1a93SBrandon Kim }; 9117ee1a93SBrandon Kim 9217ee1a93SBrandon Kim struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const 9317ee1a93SBrandon Kim { 9417ee1a93SBrandon Kim return cachedBufferHeader; 9517ee1a93SBrandon Kim } 9617ee1a93SBrandon Kim 97cf0b9752SBrandon Kim void BufferImpl::updateReadPtr(const uint32_t newReadPtr) 98cf0b9752SBrandon Kim { 99cf0b9752SBrandon Kim constexpr uint8_t bmcReadPtrOffset = 100cf0b9752SBrandon Kim offsetof(struct CircularBufferHeader, bmcReadPtr); 101cf0b9752SBrandon Kim 102271d2313SBrandon Kim little_uint24_t truncatedReadPtr = 103271d2313SBrandon Kim boost::endian::native_to_little(newReadPtr & 0xffffff); 104cf0b9752SBrandon Kim uint8_t* truncatedReadPtrPtr = 105cf0b9752SBrandon Kim reinterpret_cast<uint8_t*>(&truncatedReadPtr); 106cf0b9752SBrandon Kim 107cf0b9752SBrandon Kim size_t writtenSize = dataInterface->write( 108cf0b9752SBrandon Kim bmcReadPtrOffset, std::span<const uint8_t>{ 109cf0b9752SBrandon Kim truncatedReadPtrPtr, 110271d2313SBrandon Kim truncatedReadPtrPtr + sizeof(truncatedReadPtr)}); 111271d2313SBrandon Kim if (writtenSize != sizeof(truncatedReadPtr)) 112cf0b9752SBrandon Kim { 113cf0b9752SBrandon Kim throw std::runtime_error(fmt::format( 114cf0b9752SBrandon Kim "[updateReadPtr] Wrote '{}' bytes, instead of expected '{}'", 115271d2313SBrandon Kim writtenSize, sizeof(truncatedReadPtr))); 116cf0b9752SBrandon Kim } 117cf0b9752SBrandon Kim cachedBufferHeader.bmcReadPtr = truncatedReadPtr; 118cf0b9752SBrandon Kim } 119cf0b9752SBrandon Kim 120c49284b6SBrandon Kim void BufferImpl::updateBmcFlags(const uint32_t newBmcFlag) 121c49284b6SBrandon Kim { 122c49284b6SBrandon Kim constexpr uint8_t bmcFlagsPtrOffset = 123c49284b6SBrandon Kim offsetof(struct CircularBufferHeader, bmcFlags); 124c49284b6SBrandon Kim 125c49284b6SBrandon Kim little_uint32_t littleNewBmcFlag = 126c49284b6SBrandon Kim boost::endian::native_to_little(newBmcFlag); 127c49284b6SBrandon Kim uint8_t* littleNewBmcFlagPtr = 128c49284b6SBrandon Kim reinterpret_cast<uint8_t*>(&littleNewBmcFlag); 129c49284b6SBrandon Kim 130c49284b6SBrandon Kim size_t writtenSize = dataInterface->write( 131c49284b6SBrandon Kim bmcFlagsPtrOffset, std::span<const uint8_t>{ 132c49284b6SBrandon Kim littleNewBmcFlagPtr, 133c49284b6SBrandon Kim littleNewBmcFlagPtr + sizeof(little_uint32_t)}); 134c49284b6SBrandon Kim if (writtenSize != sizeof(little_uint32_t)) 135c49284b6SBrandon Kim { 136c49284b6SBrandon Kim throw std::runtime_error(fmt::format( 137c49284b6SBrandon Kim "[updateBmcFlags] Wrote '{}' bytes, instead of expected '{}'", 138c49284b6SBrandon Kim writtenSize, sizeof(little_uint32_t))); 139c49284b6SBrandon Kim } 140c49284b6SBrandon Kim cachedBufferHeader.bmcFlags = littleNewBmcFlag; 141c49284b6SBrandon Kim } 142c49284b6SBrandon Kim 1439836cfa6SBrandon Kim size_t BufferImpl::getQueueOffset() 1449836cfa6SBrandon Kim { 1459836cfa6SBrandon Kim return sizeof(struct CircularBufferHeader) + 1469836cfa6SBrandon Kim boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); 1479836cfa6SBrandon Kim } 1489836cfa6SBrandon Kim 14935d4335eSBrandon Kim std::vector<uint8_t> BufferImpl::wraparoundRead(const uint32_t relativeOffset, 15035d4335eSBrandon Kim const uint32_t length) 1519836cfa6SBrandon Kim { 152*3def3c8eSBrandon Kim const size_t maxOffset = getMaxOffset(); 1539836cfa6SBrandon Kim 154*3def3c8eSBrandon Kim if (relativeOffset > maxOffset) 15535d4335eSBrandon Kim { 15635d4335eSBrandon Kim throw std::runtime_error( 15735d4335eSBrandon Kim fmt::format("[wraparoundRead] relativeOffset '{}' was bigger " 158*3def3c8eSBrandon Kim "than maxOffset '{}'", 159*3def3c8eSBrandon Kim relativeOffset, maxOffset)); 16035d4335eSBrandon Kim } 161*3def3c8eSBrandon Kim if (length > maxOffset) 1629836cfa6SBrandon Kim { 1639836cfa6SBrandon Kim throw std::runtime_error(fmt::format( 164*3def3c8eSBrandon Kim "[wraparoundRead] length '{}' was bigger than maxOffset '{}'", 165*3def3c8eSBrandon Kim length, maxOffset)); 1669836cfa6SBrandon Kim } 1679836cfa6SBrandon Kim 16835d4335eSBrandon Kim // Do a calculation to see if the read will wraparound 16935d4335eSBrandon Kim const size_t queueOffset = getQueueOffset(); 170*3def3c8eSBrandon Kim const size_t writableSpace = maxOffset - relativeOffset; 17135d4335eSBrandon Kim size_t numWraparoundBytesToRead = 0; 172*3def3c8eSBrandon Kim if (length > writableSpace) 17335d4335eSBrandon Kim { 17435d4335eSBrandon Kim // This means we will wrap, count the bytes that are left to read 175*3def3c8eSBrandon Kim numWraparoundBytesToRead = length - writableSpace; 17635d4335eSBrandon Kim } 17735d4335eSBrandon Kim const size_t numBytesToReadTillQueueEnd = length - numWraparoundBytesToRead; 17835d4335eSBrandon Kim 17935d4335eSBrandon Kim std::vector<uint8_t> bytesRead = dataInterface->read( 18035d4335eSBrandon Kim queueOffset + relativeOffset, numBytesToReadTillQueueEnd); 18135d4335eSBrandon Kim if (bytesRead.size() != numBytesToReadTillQueueEnd) 18235d4335eSBrandon Kim { 18335d4335eSBrandon Kim throw std::runtime_error( 18435d4335eSBrandon Kim fmt::format("[wraparoundRead] Read '{}' which was not " 18535d4335eSBrandon Kim "the requested length of '{}'", 18635d4335eSBrandon Kim bytesRead.size(), numBytesToReadTillQueueEnd)); 18735d4335eSBrandon Kim } 18835d4335eSBrandon Kim size_t updatedReadPtr = relativeOffset + numBytesToReadTillQueueEnd; 189*3def3c8eSBrandon Kim if (updatedReadPtr == maxOffset) 1904662b1bdSBrandon Kim { 1914662b1bdSBrandon Kim // If we read all the way up to the end of the queue, we need to 1924662b1bdSBrandon Kim // manually wrap the updateReadPtr around to 0 1934662b1bdSBrandon Kim updatedReadPtr = 0; 1944662b1bdSBrandon Kim } 1959836cfa6SBrandon Kim 1969836cfa6SBrandon Kim // If there are any more bytes to be read beyond the buffer, wrap around and 1979836cfa6SBrandon Kim // read from the beginning of the buffer (offset by the queueOffset) 19835d4335eSBrandon Kim if (numWraparoundBytesToRead > 0) 1999836cfa6SBrandon Kim { 2009836cfa6SBrandon Kim std::vector<uint8_t> wrappedBytesRead = 20135d4335eSBrandon Kim dataInterface->read(queueOffset, numWraparoundBytesToRead); 20235d4335eSBrandon Kim if (numWraparoundBytesToRead != wrappedBytesRead.size()) 2039836cfa6SBrandon Kim { 2049836cfa6SBrandon Kim throw std::runtime_error(fmt::format( 20535d4335eSBrandon Kim "[wraparoundRead] Buffer wrapped around but read '{}' which " 20635d4335eSBrandon Kim "was not the requested lenght of '{}'", 20735d4335eSBrandon Kim wrappedBytesRead.size(), numWraparoundBytesToRead)); 2089836cfa6SBrandon Kim } 2099836cfa6SBrandon Kim bytesRead.insert(bytesRead.end(), wrappedBytesRead.begin(), 2109836cfa6SBrandon Kim wrappedBytesRead.end()); 21135d4335eSBrandon Kim updatedReadPtr = numWraparoundBytesToRead; 2129836cfa6SBrandon Kim } 21335d4335eSBrandon Kim updateReadPtr(updatedReadPtr); 2149836cfa6SBrandon Kim 2159836cfa6SBrandon Kim return bytesRead; 2169836cfa6SBrandon Kim } 2179836cfa6SBrandon Kim 218613ba537SBrandon Kim struct QueueEntryHeader BufferImpl::readEntryHeader() 2197bac2d69SBrandon Kim { 2207bac2d69SBrandon Kim size_t headerSize = sizeof(struct QueueEntryHeader); 2217bac2d69SBrandon Kim // wraparonudRead will throw if it did not read all the bytes, let it 2227bac2d69SBrandon Kim // propagate up the stack 223613ba537SBrandon Kim std::vector<uint8_t> bytesRead = wraparoundRead( 224613ba537SBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), 225613ba537SBrandon Kim headerSize); 2267bac2d69SBrandon Kim 2277bac2d69SBrandon Kim return *reinterpret_cast<struct QueueEntryHeader*>(bytesRead.data()); 2287bac2d69SBrandon Kim } 2297bac2d69SBrandon Kim 230613ba537SBrandon Kim EntryPair BufferImpl::readEntry() 23140ce08e1SBrandon Kim { 232613ba537SBrandon Kim struct QueueEntryHeader entryHeader = readEntryHeader(); 23335d4335eSBrandon Kim size_t entrySize = boost::endian::little_to_native(entryHeader.entrySize); 23440ce08e1SBrandon Kim 23540ce08e1SBrandon Kim // wraparonudRead may throw if entrySize was bigger than the buffer or if it 23626660e9bSBrandon Kim // was not able to read all the bytes, let it propagate up the stack 23735d4335eSBrandon Kim std::vector<uint8_t> entry = wraparoundRead( 238613ba537SBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), 239613ba537SBrandon Kim 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 263*3def3c8eSBrandon Kim const size_t maxOffset = getMaxOffset(); 2644662b1bdSBrandon Kim size_t currentBiosWritePtr = 2654662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.biosWritePtr); 266*3def3c8eSBrandon Kim if (currentBiosWritePtr > maxOffset) 2674662b1bdSBrandon Kim { 2684662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 2694662b1bdSBrandon Kim "[readErrorLogs] currentBiosWritePtr was '{}' which was bigger " 270*3def3c8eSBrandon Kim "than maxOffset '{}'", 271*3def3c8eSBrandon Kim currentBiosWritePtr, maxOffset)); 2724662b1bdSBrandon Kim } 2734662b1bdSBrandon Kim size_t currentReadPtr = 2744662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); 275*3def3c8eSBrandon Kim if (currentReadPtr > maxOffset) 2764662b1bdSBrandon Kim { 2774662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 2784662b1bdSBrandon Kim "[readErrorLogs] currentReadPtr was '{}' which was bigger " 279*3def3c8eSBrandon Kim "than maxOffset '{}'", 280*3def3c8eSBrandon Kim currentReadPtr, maxOffset)); 2814662b1bdSBrandon Kim } 2824662b1bdSBrandon Kim 2834662b1bdSBrandon Kim size_t bytesToRead; 2844662b1bdSBrandon Kim if (currentBiosWritePtr == currentReadPtr) 2854662b1bdSBrandon Kim { 2864662b1bdSBrandon Kim // No new payload was detected, return an empty vector gracefully 2874662b1bdSBrandon Kim return {}; 2884662b1bdSBrandon Kim } 2894662b1bdSBrandon Kim 2904662b1bdSBrandon Kim if (currentBiosWritePtr > currentReadPtr) 2914662b1bdSBrandon Kim { 2924662b1bdSBrandon Kim // Simply subtract in this case 2934662b1bdSBrandon Kim bytesToRead = currentBiosWritePtr - currentReadPtr; 2944662b1bdSBrandon Kim } 2954662b1bdSBrandon Kim else 2964662b1bdSBrandon Kim { 297*3def3c8eSBrandon Kim // Calculate the bytes to the "end" (maxOffset - ReadPtr) + 2984662b1bdSBrandon Kim // bytes to read from the "beginning" (0 + WritePtr) 299*3def3c8eSBrandon Kim bytesToRead = (maxOffset - currentReadPtr) + currentBiosWritePtr; 3004662b1bdSBrandon Kim } 3014662b1bdSBrandon Kim 3024662b1bdSBrandon Kim size_t byteRead = 0; 3034662b1bdSBrandon Kim std::vector<EntryPair> entryPairs; 3044662b1bdSBrandon Kim while (byteRead < bytesToRead) 3054662b1bdSBrandon Kim { 306613ba537SBrandon Kim EntryPair entryPair = readEntry(); 3074662b1bdSBrandon Kim byteRead += sizeof(struct QueueEntryHeader) + entryPair.second.size(); 3084662b1bdSBrandon Kim entryPairs.push_back(entryPair); 3094662b1bdSBrandon Kim 3104662b1bdSBrandon Kim // Note: readEntry() will update cachedBufferHeader.bmcReadPtr 3114662b1bdSBrandon Kim currentReadPtr = 3124662b1bdSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); 3134662b1bdSBrandon Kim } 3144662b1bdSBrandon Kim if (currentBiosWritePtr != currentReadPtr) 3154662b1bdSBrandon Kim { 3164662b1bdSBrandon Kim throw std::runtime_error(fmt::format( 3174662b1bdSBrandon Kim "[readErrorLogs] biosWritePtr '{}' and bmcReaddPtr '{}' " 3184662b1bdSBrandon Kim "are not identical after reading through all the logs", 3194662b1bdSBrandon Kim currentBiosWritePtr, currentReadPtr)); 3204662b1bdSBrandon Kim } 3214662b1bdSBrandon Kim return entryPairs; 3224662b1bdSBrandon Kim } 3234662b1bdSBrandon Kim 324*3def3c8eSBrandon Kim size_t BufferImpl::getMaxOffset() 325*3def3c8eSBrandon Kim { 326*3def3c8eSBrandon Kim size_t queueSize = 327*3def3c8eSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.queueSize); 328*3def3c8eSBrandon Kim size_t ueRegionSize = 329*3def3c8eSBrandon Kim boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); 330*3def3c8eSBrandon Kim 331*3def3c8eSBrandon Kim return queueSize - ueRegionSize - sizeof(struct CircularBufferHeader); 332*3def3c8eSBrandon Kim } 333*3def3c8eSBrandon Kim 334fcbc3db1SBrandon Kim } // namespace bios_bmc_smm_error_logger 335