Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 3 | #include "buffer.hpp" |
| 4 | |
| 5 | #include "pci_handler.hpp" |
| 6 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 7 | #include <boost/endian/arithmetic.hpp> |
| 8 | #include <boost/endian/conversion.hpp> |
Brandon Kim | aec9986 | 2025-06-08 22:41:40 +0000 | [diff] [blame] | 9 | #include <stdplus/print.hpp> |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 12 | #include <array> |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 13 | #include <cstddef> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 14 | #include <cstdint> |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 15 | #include <format> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 16 | #include <memory> |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 17 | #include <numeric> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 18 | #include <span> |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace bios_bmc_smm_error_logger |
| 22 | { |
| 23 | |
| 24 | BufferImpl::BufferImpl(std::unique_ptr<DataInterface> dataInterface) : |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 25 | dataInterface(std::move(dataInterface)) {}; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 26 | |
| 27 | void BufferImpl::initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize, |
| 28 | uint16_t ueRegionSize, |
| 29 | const std::array<uint32_t, 4>& magicNumber) |
| 30 | { |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 31 | const size_t memoryRegionSize = dataInterface->getMemoryRegionSize(); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 32 | if (queueSize > memoryRegionSize) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 33 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 34 | throw std::runtime_error(std::format( |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 35 | "[initialize] Proposed queue size '{}' is bigger than the " |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 36 | "BMC's allocated MMIO region of '{}'", |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 37 | queueSize, memoryRegionSize)); |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Initialize the whole buffer with 0x00 |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 41 | const std::vector<uint8_t> emptyVector(queueSize, 0); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 42 | size_t byteWritten = dataInterface->write(0, emptyVector); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 43 | if (byteWritten != queueSize) |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 44 | { |
| 45 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 46 | std::format("[initialize] Only erased '{}'", byteWritten)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Create an initial buffer header and write to it |
| 50 | struct CircularBufferHeader initializationHeader = {}; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 51 | initializationHeader.bmcInterfaceVersion = |
| 52 | boost::endian::native_to_little(bmcInterfaceVersion); |
| 53 | initializationHeader.queueSize = boost::endian::native_to_little(queueSize); |
| 54 | initializationHeader.ueRegionSize = |
| 55 | boost::endian::native_to_little(ueRegionSize); |
| 56 | std::transform(magicNumber.begin(), magicNumber.end(), |
| 57 | initializationHeader.magicNumber.begin(), |
| 58 | [](uint32_t number) -> little_uint32_t { |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 59 | return boost::endian::native_to_little(number); |
| 60 | }); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 61 | |
| 62 | uint8_t* initializationHeaderPtr = |
| 63 | reinterpret_cast<uint8_t*>(&initializationHeader); |
| 64 | size_t initializationHeaderSize = sizeof(initializationHeader); |
| 65 | byteWritten = dataInterface->write( |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 66 | 0, std::span<const uint8_t>( |
| 67 | initializationHeaderPtr, |
| 68 | initializationHeaderPtr + initializationHeaderSize)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 69 | if (byteWritten != initializationHeaderSize) |
| 70 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 71 | throw std::runtime_error(std::format( |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 72 | "[initialize] Only wrote '{}' bytes of the header", byteWritten)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 73 | } |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 74 | cachedBufferHeader = initializationHeader; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 77 | void BufferImpl::readBufferHeader() |
| 78 | { |
| 79 | size_t headerSize = sizeof(struct CircularBufferHeader); |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 80 | std::vector<uint8_t> bytesRead = |
| 81 | dataInterface->read(/*offset=*/0, headerSize); |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 82 | |
| 83 | if (bytesRead.size() != headerSize) |
| 84 | { |
| 85 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 86 | std::format("Buffer header read only read '{}', expected '{}'", |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 87 | bytesRead.size(), headerSize)); |
| 88 | } |
| 89 | |
| 90 | cachedBufferHeader = |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 91 | *reinterpret_cast<struct CircularBufferHeader*>(bytesRead.data()); |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const |
| 95 | { |
| 96 | return cachedBufferHeader; |
| 97 | } |
| 98 | |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 99 | void BufferImpl::updateReadPtr(const uint32_t newReadPtr) |
| 100 | { |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 101 | constexpr uint8_t bmcReadPtrOffset = |
| 102 | offsetof(struct CircularBufferHeader, bmcReadPtr); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 103 | |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 104 | little_uint24_t truncatedReadPtr = |
| 105 | boost::endian::native_to_little(newReadPtr & 0xffffff); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 106 | uint8_t* truncatedReadPtrPtr = |
| 107 | reinterpret_cast<uint8_t*>(&truncatedReadPtr); |
| 108 | |
| 109 | size_t writtenSize = dataInterface->write( |
| 110 | bmcReadPtrOffset, std::span<const uint8_t>{ |
| 111 | truncatedReadPtrPtr, |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 112 | truncatedReadPtrPtr + sizeof(truncatedReadPtr)}); |
| 113 | if (writtenSize != sizeof(truncatedReadPtr)) |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 114 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 115 | throw std::runtime_error(std::format( |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 116 | "[updateReadPtr] Wrote '{}' bytes, instead of expected '{}'", |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 117 | writtenSize, sizeof(truncatedReadPtr))); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 118 | } |
| 119 | cachedBufferHeader.bmcReadPtr = truncatedReadPtr; |
| 120 | } |
| 121 | |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 122 | void BufferImpl::updateBmcFlags(const uint32_t newBmcFlag) |
| 123 | { |
Patrick Williams | 1a64356 | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 124 | constexpr uint8_t bmcFlagsPtrOffset = |
| 125 | offsetof(struct CircularBufferHeader, bmcFlags); |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 126 | |
| 127 | little_uint32_t littleNewBmcFlag = |
| 128 | boost::endian::native_to_little(newBmcFlag); |
| 129 | uint8_t* littleNewBmcFlagPtr = |
| 130 | reinterpret_cast<uint8_t*>(&littleNewBmcFlag); |
| 131 | |
| 132 | size_t writtenSize = dataInterface->write( |
| 133 | bmcFlagsPtrOffset, std::span<const uint8_t>{ |
| 134 | littleNewBmcFlagPtr, |
| 135 | littleNewBmcFlagPtr + sizeof(little_uint32_t)}); |
| 136 | if (writtenSize != sizeof(little_uint32_t)) |
| 137 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 138 | throw std::runtime_error(std::format( |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 139 | "[updateBmcFlags] Wrote '{}' bytes, instead of expected '{}'", |
| 140 | writtenSize, sizeof(little_uint32_t))); |
| 141 | } |
| 142 | cachedBufferHeader.bmcFlags = littleNewBmcFlag; |
| 143 | } |
| 144 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 145 | std::vector<uint8_t> BufferImpl::wraparoundRead(const uint32_t relativeOffset, |
| 146 | const uint32_t length) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 147 | { |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 148 | const size_t maxOffset = getMaxOffset(); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 149 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 150 | if (relativeOffset > maxOffset) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 151 | { |
| 152 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 153 | std::format("[wraparoundRead] relativeOffset '{}' was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 154 | "than maxOffset '{}'", |
| 155 | relativeOffset, maxOffset)); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 156 | } |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 157 | if (length > maxOffset) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 158 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 159 | throw std::runtime_error(std::format( |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 160 | "[wraparoundRead] length '{}' was bigger than maxOffset '{}'", |
| 161 | length, maxOffset)); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 164 | // Do a calculation to see if the read will wraparound |
| 165 | const size_t queueOffset = getQueueOffset(); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 166 | const size_t writableSpace = maxOffset - relativeOffset; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 167 | size_t numWraparoundBytesToRead = 0; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 168 | if (length > writableSpace) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 169 | { |
| 170 | // This means we will wrap, count the bytes that are left to read |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 171 | numWraparoundBytesToRead = length - writableSpace; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 172 | } |
| 173 | const size_t numBytesToReadTillQueueEnd = length - numWraparoundBytesToRead; |
| 174 | |
| 175 | std::vector<uint8_t> bytesRead = dataInterface->read( |
| 176 | queueOffset + relativeOffset, numBytesToReadTillQueueEnd); |
| 177 | if (bytesRead.size() != numBytesToReadTillQueueEnd) |
| 178 | { |
| 179 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 180 | std::format("[wraparoundRead] Read '{}' which was not " |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 181 | "the requested length of '{}'", |
| 182 | bytesRead.size(), numBytesToReadTillQueueEnd)); |
| 183 | } |
| 184 | size_t updatedReadPtr = relativeOffset + numBytesToReadTillQueueEnd; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 185 | if (updatedReadPtr == maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 186 | { |
| 187 | // If we read all the way up to the end of the queue, we need to |
| 188 | // manually wrap the updateReadPtr around to 0 |
| 189 | updatedReadPtr = 0; |
| 190 | } |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 191 | |
| 192 | // If there are any more bytes to be read beyond the buffer, wrap around and |
| 193 | // read from the beginning of the buffer (offset by the queueOffset) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 194 | if (numWraparoundBytesToRead > 0) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 195 | { |
| 196 | std::vector<uint8_t> wrappedBytesRead = |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 197 | dataInterface->read(queueOffset, numWraparoundBytesToRead); |
| 198 | if (numWraparoundBytesToRead != wrappedBytesRead.size()) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 199 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 200 | throw std::runtime_error(std::format( |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 201 | "[wraparoundRead] Buffer wrapped around but read '{}' which " |
| 202 | "was not the requested lenght of '{}'", |
| 203 | wrappedBytesRead.size(), numWraparoundBytesToRead)); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 204 | } |
| 205 | bytesRead.insert(bytesRead.end(), wrappedBytesRead.begin(), |
| 206 | wrappedBytesRead.end()); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 207 | updatedReadPtr = numWraparoundBytesToRead; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 208 | } |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 209 | updateReadPtr(updatedReadPtr); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 210 | |
| 211 | return bytesRead; |
| 212 | } |
| 213 | |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 214 | struct QueueEntryHeader BufferImpl::readEntryHeader() |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 215 | { |
| 216 | size_t headerSize = sizeof(struct QueueEntryHeader); |
| 217 | // wraparonudRead will throw if it did not read all the bytes, let it |
| 218 | // propagate up the stack |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 219 | std::vector<uint8_t> bytesRead = wraparoundRead( |
| 220 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 221 | headerSize); |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 222 | |
| 223 | return *reinterpret_cast<struct QueueEntryHeader*>(bytesRead.data()); |
| 224 | } |
| 225 | |
Brandon Kim | aec9986 | 2025-06-08 22:41:40 +0000 | [diff] [blame] | 226 | std::vector<uint8_t> BufferImpl::readUeLogFromReservedRegion() |
| 227 | { |
| 228 | // Ensure cachedBufferHeader is up-to-date |
| 229 | readBufferHeader(); |
| 230 | |
| 231 | uint16_t currentUeRegionSize = |
| 232 | boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); |
| 233 | if (currentUeRegionSize == 0) |
| 234 | { |
| 235 | stdplus::print(stderr, |
| 236 | "[readUeLogFromReservedRegion] UE Region size is 0\n"); |
| 237 | return {}; |
| 238 | } |
| 239 | |
| 240 | uint32_t biosSideFlags = |
| 241 | boost::endian::little_to_native(cachedBufferHeader.biosFlags); |
| 242 | uint32_t bmcSideFlags = |
| 243 | boost::endian::little_to_native(cachedBufferHeader.bmcFlags); |
| 244 | |
| 245 | // (BIOS_switch ^ BMC_switch) & BIT0 == BIT0 -> unread log |
| 246 | // This means if the ueSwitch bit differs, there's an unread log. |
| 247 | if (!((biosSideFlags ^ bmcSideFlags) & |
| 248 | static_cast<uint32_t>(BufferFlags::ueSwitch))) |
| 249 | { |
| 250 | return {}; |
| 251 | } |
| 252 | // UE log should be present and unread by BMC, read from end of header |
| 253 | // (0x30) to the size of the UE region specified in the header. |
| 254 | size_t ueRegionOffset = sizeof(struct CircularBufferHeader); |
| 255 | std::vector<uint8_t> ueLogData = |
| 256 | dataInterface->read(ueRegionOffset, currentUeRegionSize); |
| 257 | |
| 258 | if (ueLogData.size() == currentUeRegionSize) |
| 259 | { |
| 260 | return ueLogData; |
| 261 | } |
| 262 | stdplus::print(stderr, |
| 263 | "[readUeLogFromReservedRegion] Failed to read " |
| 264 | "full UE log. Expected {}, got {}\n", |
| 265 | currentUeRegionSize, ueLogData.size()); |
| 266 | // Throwing an exception allows main loop to handle re-init. |
| 267 | throw std::runtime_error( |
| 268 | std::format("Failed to read full UE log. Expected {}, got {}", |
| 269 | currentUeRegionSize, ueLogData.size())); |
| 270 | } |
| 271 | |
Brandon Kim | d49db6f | 2025-06-09 00:03:59 +0000 | [diff] [blame^] | 272 | bool BufferImpl::checkForOverflowAndAcknowledge() |
| 273 | { |
| 274 | // Ensure cachedBufferHeader is up-to-date |
| 275 | readBufferHeader(); |
| 276 | |
| 277 | uint32_t biosSideFlags = |
| 278 | boost::endian::little_to_native(cachedBufferHeader.biosFlags); |
| 279 | uint32_t bmcSideFlags = |
| 280 | boost::endian::little_to_native(cachedBufferHeader.bmcFlags); |
| 281 | |
| 282 | // Design: (BIOS_switch ^ BMC_switch) & BIT1 == BIT1 -> unlogged overflow |
| 283 | // This means if the overflow bit differs, there's an |
| 284 | // unacknowledged overflow. |
| 285 | if ((biosSideFlags ^ bmcSideFlags) & |
| 286 | static_cast<uint32_t>(BufferFlags::overflow)) |
| 287 | { |
| 288 | // Overflow incident has occurred and BMC has not acknowledged it. |
| 289 | // Toggle BMC's view of the overflow flag to acknowledge. |
| 290 | uint32_t newBmcFlags = |
| 291 | bmcSideFlags ^ static_cast<uint32_t>(BufferFlags::overflow); |
| 292 | updateBmcFlags(newBmcFlags); |
| 293 | |
| 294 | // Overflow was detected and acknowledged |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | // No new overflow incident or already acknowledged |
| 299 | return false; |
| 300 | } |
| 301 | |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 302 | EntryPair BufferImpl::readEntry() |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 303 | { |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 304 | struct QueueEntryHeader entryHeader = readEntryHeader(); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 305 | size_t entrySize = boost::endian::little_to_native(entryHeader.entrySize); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 306 | |
| 307 | // wraparonudRead may throw if entrySize was bigger than the buffer or if it |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 308 | // was not able to read all the bytes, let it propagate up the stack |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 309 | std::vector<uint8_t> entry = wraparoundRead( |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 310 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 311 | entrySize); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 312 | |
| 313 | // Calculate the checksum |
| 314 | uint8_t* entryHeaderPtr = reinterpret_cast<uint8_t*>(&entryHeader); |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 315 | uint8_t checksum = |
| 316 | std::accumulate(entryHeaderPtr, |
| 317 | entryHeaderPtr + sizeof(struct QueueEntryHeader), 0, |
| 318 | std::bit_xor<void>()) ^ |
| 319 | std::accumulate(entry.begin(), entry.end(), 0, std::bit_xor<void>()); |
| 320 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 321 | if (checksum != 0) |
| 322 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 323 | throw std::runtime_error(std::format( |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 324 | "[readEntry] Checksum was '{}', expected '0'", checksum)); |
| 325 | } |
| 326 | |
| 327 | return {entryHeader, entry}; |
| 328 | } |
| 329 | |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 330 | std::vector<EntryPair> BufferImpl::readErrorLogs() |
| 331 | { |
| 332 | // Reading the buffer header will update the cachedBufferHeader |
| 333 | readBufferHeader(); |
| 334 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 335 | const size_t maxOffset = getMaxOffset(); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 336 | size_t currentBiosWritePtr = |
| 337 | boost::endian::little_to_native(cachedBufferHeader.biosWritePtr); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 338 | if (currentBiosWritePtr > maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 339 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 340 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 341 | "[readErrorLogs] currentBiosWritePtr was '{}' which was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 342 | "than maxOffset '{}'", |
| 343 | currentBiosWritePtr, maxOffset)); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 344 | } |
| 345 | size_t currentReadPtr = |
| 346 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 347 | if (currentReadPtr > maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 348 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 349 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 350 | "[readErrorLogs] currentReadPtr was '{}' which was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 351 | "than maxOffset '{}'", |
| 352 | currentReadPtr, maxOffset)); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | size_t bytesToRead; |
| 356 | if (currentBiosWritePtr == currentReadPtr) |
| 357 | { |
| 358 | // No new payload was detected, return an empty vector gracefully |
| 359 | return {}; |
| 360 | } |
| 361 | |
| 362 | if (currentBiosWritePtr > currentReadPtr) |
| 363 | { |
| 364 | // Simply subtract in this case |
| 365 | bytesToRead = currentBiosWritePtr - currentReadPtr; |
| 366 | } |
| 367 | else |
| 368 | { |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 369 | // Calculate the bytes to the "end" (maxOffset - ReadPtr) + |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 370 | // bytes to read from the "beginning" (0 + WritePtr) |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 371 | bytesToRead = (maxOffset - currentReadPtr) + currentBiosWritePtr; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | size_t byteRead = 0; |
| 375 | std::vector<EntryPair> entryPairs; |
| 376 | while (byteRead < bytesToRead) |
| 377 | { |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 378 | EntryPair entryPair = readEntry(); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 379 | byteRead += sizeof(struct QueueEntryHeader) + entryPair.second.size(); |
| 380 | entryPairs.push_back(entryPair); |
| 381 | |
| 382 | // Note: readEntry() will update cachedBufferHeader.bmcReadPtr |
| 383 | currentReadPtr = |
| 384 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); |
| 385 | } |
| 386 | if (currentBiosWritePtr != currentReadPtr) |
| 387 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 388 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 389 | "[readErrorLogs] biosWritePtr '{}' and bmcReaddPtr '{}' " |
| 390 | "are not identical after reading through all the logs", |
| 391 | currentBiosWritePtr, currentReadPtr)); |
| 392 | } |
| 393 | return entryPairs; |
| 394 | } |
| 395 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 396 | size_t BufferImpl::getMaxOffset() |
| 397 | { |
| 398 | size_t queueSize = |
| 399 | boost::endian::little_to_native(cachedBufferHeader.queueSize); |
| 400 | size_t ueRegionSize = |
| 401 | boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); |
| 402 | |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 403 | if (queueSize != QUEUE_REGION_SIZE) |
| 404 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 405 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 406 | "[{}] runtime queueSize '{}' did not match compile-time queueSize " |
| 407 | "'{}'. This indicates that the buffer was corrupted", |
| 408 | __FUNCTION__, queueSize, QUEUE_REGION_SIZE)); |
| 409 | } |
| 410 | if (ueRegionSize != UE_REGION_SIZE) |
| 411 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 412 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 413 | "[{}] runtime ueRegionSize '{}' did not match compile-time " |
| 414 | "ueRegionSize '{}'. This indicates that the buffer was corrupted", |
| 415 | __FUNCTION__, ueRegionSize, UE_REGION_SIZE)); |
| 416 | } |
| 417 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 418 | return queueSize - ueRegionSize - sizeof(struct CircularBufferHeader); |
| 419 | } |
| 420 | |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 421 | size_t BufferImpl::getQueueOffset() |
| 422 | { |
| 423 | size_t ueRegionSize = |
| 424 | boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); |
| 425 | |
| 426 | if (ueRegionSize != UE_REGION_SIZE) |
| 427 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame] | 428 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 429 | "[{}] runtime ueRegionSize '{}' did not match compile-time " |
| 430 | "ueRegionSize '{}'. This indicates that the buffer was corrupted", |
| 431 | __FUNCTION__, ueRegionSize, UE_REGION_SIZE)); |
| 432 | } |
| 433 | return sizeof(struct CircularBufferHeader) + ueRegionSize; |
| 434 | } |
| 435 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 436 | } // namespace bios_bmc_smm_error_logger |