blob: 895c65a2fabf5f3c59a91e0bcf35c146c12c28cd [file] [log] [blame]
Brandon Kim82ab8322023-08-17 00:50:18 +00001#include "config.h"
2
Brandon Kimfcbc3db2022-06-06 21:26:18 -07003#include "buffer.hpp"
4
5#include "pci_handler.hpp"
6
7#include <fmt/format.h>
8
Brandon Kim17ee1a92022-06-07 21:04:08 -07009#include <boost/endian/arithmetic.hpp>
10#include <boost/endian/conversion.hpp>
11
12#include <algorithm>
Brandon Kimfcbc3db2022-06-06 21:26:18 -070013#include <array>
Brandon Kimcf0b9752022-06-15 10:32:21 -070014#include <cstddef>
Brandon Kimfcbc3db2022-06-06 21:26:18 -070015#include <cstdint>
16#include <memory>
Brandon Kim40ce08e2022-06-15 16:58:44 -070017#include <numeric>
Brandon Kimfcbc3db2022-06-06 21:26:18 -070018#include <span>
19#include <vector>
20
21namespace bios_bmc_smm_error_logger
22{
23
24BufferImpl::BufferImpl(std::unique_ptr<DataInterface> dataInterface) :
25 dataInterface(std::move(dataInterface)){};
26
27void BufferImpl::initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize,
28 uint16_t ueRegionSize,
29 const std::array<uint32_t, 4>& magicNumber)
30{
Brandon Kimfcbc3db2022-06-06 21:26:18 -070031 const size_t memoryRegionSize = dataInterface->getMemoryRegionSize();
Brandon Kim3def3c82022-09-13 05:29:20 +000032 if (queueSize > memoryRegionSize)
Brandon Kim26660e92022-06-15 16:58:44 -070033 {
34 throw std::runtime_error(fmt::format(
Brandon Kim3def3c82022-09-13 05:29:20 +000035 "[initialize] Proposed queue size '{}' is bigger than the "
Brandon Kim26660e92022-06-15 16:58:44 -070036 "BMC's allocated MMIO region of '{}'",
Brandon Kim3def3c82022-09-13 05:29:20 +000037 queueSize, memoryRegionSize));
Brandon Kim26660e92022-06-15 16:58:44 -070038 }
39
40 // Initialize the whole buffer with 0x00
Brandon Kim3def3c82022-09-13 05:29:20 +000041 const std::vector<uint8_t> emptyVector(queueSize, 0);
Brandon Kimfcbc3db2022-06-06 21:26:18 -070042 size_t byteWritten = dataInterface->write(0, emptyVector);
Brandon Kim3def3c82022-09-13 05:29:20 +000043 if (byteWritten != queueSize)
Brandon Kimfcbc3db2022-06-06 21:26:18 -070044 {
45 throw std::runtime_error(
Brandon Kim26660e92022-06-15 16:58:44 -070046 fmt::format("[initialize] Only erased '{}'", byteWritten));
Brandon Kimfcbc3db2022-06-06 21:26:18 -070047 }
48
49 // Create an initial buffer header and write to it
50 struct CircularBufferHeader initializationHeader = {};
Brandon Kim17ee1a92022-06-07 21:04:08 -070051 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 {
59 return boost::endian::native_to_little(number);
60 });
Brandon Kimfcbc3db2022-06-06 21:26:18 -070061
62 uint8_t* initializationHeaderPtr =
63 reinterpret_cast<uint8_t*>(&initializationHeader);
64 size_t initializationHeaderSize = sizeof(initializationHeader);
65 byteWritten = dataInterface->write(
66 0, std::span<const uint8_t>(initializationHeaderPtr,
67 initializationHeaderPtr +
68 initializationHeaderSize));
69 if (byteWritten != initializationHeaderSize)
70 {
71 throw std::runtime_error(fmt::format(
Brandon Kim26660e92022-06-15 16:58:44 -070072 "[initialize] Only wrote '{}' bytes of the header", byteWritten));
Brandon Kimfcbc3db2022-06-06 21:26:18 -070073 }
Brandon Kim60cab572022-06-15 14:20:05 -070074 cachedBufferHeader = initializationHeader;
Brandon Kimfcbc3db2022-06-06 21:26:18 -070075}
76
Brandon Kim17ee1a92022-06-07 21:04:08 -070077void BufferImpl::readBufferHeader()
78{
79 size_t headerSize = sizeof(struct CircularBufferHeader);
Patrick Williamsb8125762023-05-10 07:51:26 -050080 std::vector<uint8_t> bytesRead = dataInterface->read(/*offset=*/0,
81 headerSize);
Brandon Kim17ee1a92022-06-07 21:04:08 -070082
83 if (bytesRead.size() != headerSize)
84 {
85 throw std::runtime_error(
86 fmt::format("Buffer header read only read '{}', expected '{}'",
87 bytesRead.size(), headerSize));
88 }
89
90 cachedBufferHeader =
Brandon Kim60cab572022-06-15 14:20:05 -070091 *reinterpret_cast<struct CircularBufferHeader*>(bytesRead.data());
Brandon Kim17ee1a92022-06-07 21:04:08 -070092};
93
94struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const
95{
96 return cachedBufferHeader;
97}
98
Brandon Kimcf0b9752022-06-15 10:32:21 -070099void BufferImpl::updateReadPtr(const uint32_t newReadPtr)
100{
Patrick Williamsb8125762023-05-10 07:51:26 -0500101 constexpr uint8_t bmcReadPtrOffset = offsetof(struct CircularBufferHeader,
102 bmcReadPtr);
Brandon Kimcf0b9752022-06-15 10:32:21 -0700103
Brandon Kim271d2312022-09-02 16:34:55 +0000104 little_uint24_t truncatedReadPtr =
105 boost::endian::native_to_little(newReadPtr & 0xffffff);
Brandon Kimcf0b9752022-06-15 10:32:21 -0700106 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 Kim271d2312022-09-02 16:34:55 +0000112 truncatedReadPtrPtr + sizeof(truncatedReadPtr)});
113 if (writtenSize != sizeof(truncatedReadPtr))
Brandon Kimcf0b9752022-06-15 10:32:21 -0700114 {
115 throw std::runtime_error(fmt::format(
116 "[updateReadPtr] Wrote '{}' bytes, instead of expected '{}'",
Brandon Kim271d2312022-09-02 16:34:55 +0000117 writtenSize, sizeof(truncatedReadPtr)));
Brandon Kimcf0b9752022-06-15 10:32:21 -0700118 }
119 cachedBufferHeader.bmcReadPtr = truncatedReadPtr;
120}
121
Brandon Kimc49284b2022-06-17 09:55:26 -0700122void BufferImpl::updateBmcFlags(const uint32_t newBmcFlag)
123{
Patrick Williamsb8125762023-05-10 07:51:26 -0500124 constexpr uint8_t bmcFlagsPtrOffset = offsetof(struct CircularBufferHeader,
125 bmcFlags);
Brandon Kimc49284b2022-06-17 09:55:26 -0700126
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 {
138 throw std::runtime_error(fmt::format(
139 "[updateBmcFlags] Wrote '{}' bytes, instead of expected '{}'",
140 writtenSize, sizeof(little_uint32_t)));
141 }
142 cachedBufferHeader.bmcFlags = littleNewBmcFlag;
143}
144
Brandon Kim35d43352022-06-16 11:13:36 -0700145std::vector<uint8_t> BufferImpl::wraparoundRead(const uint32_t relativeOffset,
146 const uint32_t length)
Brandon Kim9836cfa2022-06-15 11:21:11 -0700147{
Brandon Kim3def3c82022-09-13 05:29:20 +0000148 const size_t maxOffset = getMaxOffset();
Brandon Kim9836cfa2022-06-15 11:21:11 -0700149
Brandon Kim3def3c82022-09-13 05:29:20 +0000150 if (relativeOffset > maxOffset)
Brandon Kim35d43352022-06-16 11:13:36 -0700151 {
152 throw std::runtime_error(
153 fmt::format("[wraparoundRead] relativeOffset '{}' was bigger "
Brandon Kim3def3c82022-09-13 05:29:20 +0000154 "than maxOffset '{}'",
155 relativeOffset, maxOffset));
Brandon Kim35d43352022-06-16 11:13:36 -0700156 }
Brandon Kim3def3c82022-09-13 05:29:20 +0000157 if (length > maxOffset)
Brandon Kim9836cfa2022-06-15 11:21:11 -0700158 {
159 throw std::runtime_error(fmt::format(
Brandon Kim3def3c82022-09-13 05:29:20 +0000160 "[wraparoundRead] length '{}' was bigger than maxOffset '{}'",
161 length, maxOffset));
Brandon Kim9836cfa2022-06-15 11:21:11 -0700162 }
163
Brandon Kim35d43352022-06-16 11:13:36 -0700164 // Do a calculation to see if the read will wraparound
165 const size_t queueOffset = getQueueOffset();
Brandon Kim3def3c82022-09-13 05:29:20 +0000166 const size_t writableSpace = maxOffset - relativeOffset;
Brandon Kim35d43352022-06-16 11:13:36 -0700167 size_t numWraparoundBytesToRead = 0;
Brandon Kim3def3c82022-09-13 05:29:20 +0000168 if (length > writableSpace)
Brandon Kim35d43352022-06-16 11:13:36 -0700169 {
170 // This means we will wrap, count the bytes that are left to read
Brandon Kim3def3c82022-09-13 05:29:20 +0000171 numWraparoundBytesToRead = length - writableSpace;
Brandon Kim35d43352022-06-16 11:13:36 -0700172 }
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(
180 fmt::format("[wraparoundRead] Read '{}' which was not "
181 "the requested length of '{}'",
182 bytesRead.size(), numBytesToReadTillQueueEnd));
183 }
184 size_t updatedReadPtr = relativeOffset + numBytesToReadTillQueueEnd;
Brandon Kim3def3c82022-09-13 05:29:20 +0000185 if (updatedReadPtr == maxOffset)
Brandon Kim4662b1b2022-06-16 21:38:02 -0700186 {
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 Kim9836cfa2022-06-15 11:21:11 -0700191
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 Kim35d43352022-06-16 11:13:36 -0700194 if (numWraparoundBytesToRead > 0)
Brandon Kim9836cfa2022-06-15 11:21:11 -0700195 {
196 std::vector<uint8_t> wrappedBytesRead =
Brandon Kim35d43352022-06-16 11:13:36 -0700197 dataInterface->read(queueOffset, numWraparoundBytesToRead);
198 if (numWraparoundBytesToRead != wrappedBytesRead.size())
Brandon Kim9836cfa2022-06-15 11:21:11 -0700199 {
200 throw std::runtime_error(fmt::format(
Brandon Kim35d43352022-06-16 11:13:36 -0700201 "[wraparoundRead] Buffer wrapped around but read '{}' which "
202 "was not the requested lenght of '{}'",
203 wrappedBytesRead.size(), numWraparoundBytesToRead));
Brandon Kim9836cfa2022-06-15 11:21:11 -0700204 }
205 bytesRead.insert(bytesRead.end(), wrappedBytesRead.begin(),
206 wrappedBytesRead.end());
Brandon Kim35d43352022-06-16 11:13:36 -0700207 updatedReadPtr = numWraparoundBytesToRead;
Brandon Kim9836cfa2022-06-15 11:21:11 -0700208 }
Brandon Kim35d43352022-06-16 11:13:36 -0700209 updateReadPtr(updatedReadPtr);
Brandon Kim9836cfa2022-06-15 11:21:11 -0700210
211 return bytesRead;
212}
213
Brandon Kim613ba532022-09-12 20:55:21 +0000214struct QueueEntryHeader BufferImpl::readEntryHeader()
Brandon Kim7bac2d62022-06-07 21:37:51 -0700215{
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 Kim613ba532022-09-12 20:55:21 +0000219 std::vector<uint8_t> bytesRead = wraparoundRead(
220 boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
221 headerSize);
Brandon Kim7bac2d62022-06-07 21:37:51 -0700222
223 return *reinterpret_cast<struct QueueEntryHeader*>(bytesRead.data());
224}
225
Brandon Kim613ba532022-09-12 20:55:21 +0000226EntryPair BufferImpl::readEntry()
Brandon Kim40ce08e2022-06-15 16:58:44 -0700227{
Brandon Kim613ba532022-09-12 20:55:21 +0000228 struct QueueEntryHeader entryHeader = readEntryHeader();
Brandon Kim35d43352022-06-16 11:13:36 -0700229 size_t entrySize = boost::endian::little_to_native(entryHeader.entrySize);
Brandon Kim40ce08e2022-06-15 16:58:44 -0700230
231 // wraparonudRead may throw if entrySize was bigger than the buffer or if it
Brandon Kim26660e92022-06-15 16:58:44 -0700232 // was not able to read all the bytes, let it propagate up the stack
Brandon Kim35d43352022-06-16 11:13:36 -0700233 std::vector<uint8_t> entry = wraparoundRead(
Brandon Kim613ba532022-09-12 20:55:21 +0000234 boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
235 entrySize);
Brandon Kim40ce08e2022-06-15 16:58:44 -0700236
237 // Calculate the checksum
238 uint8_t* entryHeaderPtr = reinterpret_cast<uint8_t*>(&entryHeader);
Brandon Kimf0e4adc2022-06-16 23:14:25 -0700239 uint8_t checksum =
240 std::accumulate(entryHeaderPtr,
241 entryHeaderPtr + sizeof(struct QueueEntryHeader), 0,
242 std::bit_xor<void>()) ^
243 std::accumulate(entry.begin(), entry.end(), 0, std::bit_xor<void>());
244
Brandon Kim40ce08e2022-06-15 16:58:44 -0700245 if (checksum != 0)
246 {
247 throw std::runtime_error(fmt::format(
248 "[readEntry] Checksum was '{}', expected '0'", checksum));
249 }
250
251 return {entryHeader, entry};
252}
253
Brandon Kim4662b1b2022-06-16 21:38:02 -0700254std::vector<EntryPair> BufferImpl::readErrorLogs()
255{
256 // Reading the buffer header will update the cachedBufferHeader
257 readBufferHeader();
258
Brandon Kim3def3c82022-09-13 05:29:20 +0000259 const size_t maxOffset = getMaxOffset();
Brandon Kim4662b1b2022-06-16 21:38:02 -0700260 size_t currentBiosWritePtr =
261 boost::endian::little_to_native(cachedBufferHeader.biosWritePtr);
Brandon Kim3def3c82022-09-13 05:29:20 +0000262 if (currentBiosWritePtr > maxOffset)
Brandon Kim4662b1b2022-06-16 21:38:02 -0700263 {
264 throw std::runtime_error(fmt::format(
265 "[readErrorLogs] currentBiosWritePtr was '{}' which was bigger "
Brandon Kim3def3c82022-09-13 05:29:20 +0000266 "than maxOffset '{}'",
267 currentBiosWritePtr, maxOffset));
Brandon Kim4662b1b2022-06-16 21:38:02 -0700268 }
269 size_t currentReadPtr =
270 boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr);
Brandon Kim3def3c82022-09-13 05:29:20 +0000271 if (currentReadPtr > maxOffset)
Brandon Kim4662b1b2022-06-16 21:38:02 -0700272 {
273 throw std::runtime_error(fmt::format(
274 "[readErrorLogs] currentReadPtr was '{}' which was bigger "
Brandon Kim3def3c82022-09-13 05:29:20 +0000275 "than maxOffset '{}'",
276 currentReadPtr, maxOffset));
Brandon Kim4662b1b2022-06-16 21:38:02 -0700277 }
278
279 size_t bytesToRead;
280 if (currentBiosWritePtr == currentReadPtr)
281 {
282 // No new payload was detected, return an empty vector gracefully
283 return {};
284 }
285
286 if (currentBiosWritePtr > currentReadPtr)
287 {
288 // Simply subtract in this case
289 bytesToRead = currentBiosWritePtr - currentReadPtr;
290 }
291 else
292 {
Brandon Kim3def3c82022-09-13 05:29:20 +0000293 // Calculate the bytes to the "end" (maxOffset - ReadPtr) +
Brandon Kim4662b1b2022-06-16 21:38:02 -0700294 // bytes to read from the "beginning" (0 + WritePtr)
Brandon Kim3def3c82022-09-13 05:29:20 +0000295 bytesToRead = (maxOffset - currentReadPtr) + currentBiosWritePtr;
Brandon Kim4662b1b2022-06-16 21:38:02 -0700296 }
297
298 size_t byteRead = 0;
299 std::vector<EntryPair> entryPairs;
300 while (byteRead < bytesToRead)
301 {
Brandon Kim613ba532022-09-12 20:55:21 +0000302 EntryPair entryPair = readEntry();
Brandon Kim4662b1b2022-06-16 21:38:02 -0700303 byteRead += sizeof(struct QueueEntryHeader) + entryPair.second.size();
304 entryPairs.push_back(entryPair);
305
306 // Note: readEntry() will update cachedBufferHeader.bmcReadPtr
307 currentReadPtr =
308 boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr);
309 }
310 if (currentBiosWritePtr != currentReadPtr)
311 {
312 throw std::runtime_error(fmt::format(
313 "[readErrorLogs] biosWritePtr '{}' and bmcReaddPtr '{}' "
314 "are not identical after reading through all the logs",
315 currentBiosWritePtr, currentReadPtr));
316 }
317 return entryPairs;
318}
319
Brandon Kim3def3c82022-09-13 05:29:20 +0000320size_t BufferImpl::getMaxOffset()
321{
322 size_t queueSize =
323 boost::endian::little_to_native(cachedBufferHeader.queueSize);
324 size_t ueRegionSize =
325 boost::endian::little_to_native(cachedBufferHeader.ueRegionSize);
326
Brandon Kim82ab8322023-08-17 00:50:18 +0000327 if (queueSize != QUEUE_REGION_SIZE)
328 {
329 throw std::runtime_error(fmt::format(
330 "[{}] runtime queueSize '{}' did not match compile-time queueSize "
331 "'{}'. This indicates that the buffer was corrupted",
332 __FUNCTION__, queueSize, QUEUE_REGION_SIZE));
333 }
334 if (ueRegionSize != UE_REGION_SIZE)
335 {
336 throw std::runtime_error(fmt::format(
337 "[{}] runtime ueRegionSize '{}' did not match compile-time "
338 "ueRegionSize '{}'. This indicates that the buffer was corrupted",
339 __FUNCTION__, ueRegionSize, UE_REGION_SIZE));
340 }
341
Brandon Kim3def3c82022-09-13 05:29:20 +0000342 return queueSize - ueRegionSize - sizeof(struct CircularBufferHeader);
343}
344
Brandon Kim82ab8322023-08-17 00:50:18 +0000345size_t BufferImpl::getQueueOffset()
346{
347 size_t ueRegionSize =
348 boost::endian::little_to_native(cachedBufferHeader.ueRegionSize);
349
350 if (ueRegionSize != UE_REGION_SIZE)
351 {
352 throw std::runtime_error(fmt::format(
353 "[{}] runtime ueRegionSize '{}' did not match compile-time "
354 "ueRegionSize '{}'. This indicates that the buffer was corrupted",
355 __FUNCTION__, ueRegionSize, UE_REGION_SIZE));
356 }
357 return sizeof(struct CircularBufferHeader) + ueRegionSize;
358}
359
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700360} // namespace bios_bmc_smm_error_logger