blob: 2106b36ea2a7aad0cd87a494e3b31113b9b48f6f [file] [log] [blame]
Zane Shelleyd3b9bac2020-11-17 21:59:12 -06001#include <unistd.h>
2
Zane Shelley4ed4be52021-02-15 17:53:40 -06003#include <analyzer/service_data.hpp>
Zane Shelley8f60a622021-02-01 14:41:30 -06004#include <analyzer/util.hpp>
Zane Shelleyd3b9bac2020-11-17 21:59:12 -06005#include <hei_main.hpp>
6#include <phosphor-logging/elog.hpp>
7#include <sdbusplus/bus.hpp>
Zane Shelleyb1106b52021-01-29 13:44:42 -06008#include <util/bin_stream.hpp>
Ben Tyner7029e522021-08-09 19:18:24 -05009#include <util/dbus.hpp>
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060010#include <util/ffdc_file.hpp>
11#include <util/pdbg.hpp>
12#include <util/trace.hpp>
13#include <xyz/openbmc_project/Logging/Create/server.hpp>
14#include <xyz/openbmc_project/Logging/Entry/server.hpp>
15
Zane Shelley021dab32020-12-08 20:28:40 -060016#include <fstream>
17#include <memory>
18
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060019namespace LogSvr = sdbusplus::xyz::openbmc_project::Logging::server;
20
21namespace analyzer
22{
23
Zane Shelley021dab32020-12-08 20:28:40 -060024//------------------------------------------------------------------------------
25
26enum FfdcSubType_t : uint8_t
27{
Zane Shelley8f60a622021-02-01 14:41:30 -060028 FFDC_SIGNATURES = 0x01,
29 FFDC_REGISTER_DUMP = 0x02,
Zane Shelley5f6e3de2021-02-23 13:57:37 -060030 FFDC_GUARD = 0x03,
Zane Shelley021dab32020-12-08 20:28:40 -060031
32 // For the callout section, the value of '0xCA' is required per the
33 // phosphor-logging openpower-pel extention spec.
34 FFDC_CALLOUTS = 0xCA,
35};
36
37enum FfdcVersion_t : uint8_t
38{
39 FFDC_VERSION1 = 0x01,
40};
41
42//------------------------------------------------------------------------------
43
Zane Shelley021dab32020-12-08 20:28:40 -060044void __getSrc(const libhei::Signature& i_signature, uint32_t& o_word6,
45 uint32_t& o_word7, uint32_t& o_word8)
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060046{
47 // [ 0:15] chip model
48 // [16:23] reserved space in chip ID
49 // [24:31] chip EC level
Zane Shelley021dab32020-12-08 20:28:40 -060050 o_word6 = i_signature.getChip().getType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060051
52 // [ 0:15] chip position
Zane Shelleyff068a12021-06-17 17:45:40 -050053 // [16:23] node position
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060054 // [24:31] signature attention type
Zane Shelleyff068a12021-06-17 17:45:40 -050055 auto chipPos = util::pdbg::getChipPos(i_signature.getChip());
56 uint8_t nodePos = 0; // TODO: multi-node support
57 auto attn = i_signature.getAttnType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060058
Zane Shelleyff068a12021-06-17 17:45:40 -050059 o_word7 = (chipPos & 0xffff) << 16 | (nodePos & 0xff) << 8 | (attn & 0xff);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060060
61 // [ 0:15] signature ID
62 // [16:23] signature instance
63 // [24:31] signature bit position
Zane Shelley021dab32020-12-08 20:28:40 -060064 o_word8 = i_signature.toUint32();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060065
66 // Word 9 is currently unused
Zane Shelley021dab32020-12-08 20:28:40 -060067}
68
69//------------------------------------------------------------------------------
70
71void __setSrc(const libhei::Signature& i_rootCause,
72 std::map<std::string, std::string>& io_logData)
73{
74 uint32_t word6 = 0, word7 = 0, word8 = 0;
75 __getSrc(i_rootCause, word6, word7, word8);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060076
77 io_logData["SRC6"] = std::to_string(word6);
78 io_logData["SRC7"] = std::to_string(word7);
79 io_logData["SRC8"] = std::to_string(word8);
80}
81
82//------------------------------------------------------------------------------
83
Zane Shelley4ed4be52021-02-15 17:53:40 -060084void __addCalloutList(const ServiceData& i_servData,
85 std::vector<util::FFDCFile>& io_userDataFiles)
86{
Zane Shelley4ed4be52021-02-15 17:53:40 -060087 // Create a new entry for the user data section containing the callout list.
88 io_userDataFiles.emplace_back(util::FFDCFormat::JSON, FFDC_CALLOUTS,
89 FFDC_VERSION1);
90
91 // Use a file stream to write the JSON to file.
92 std::ofstream o{io_userDataFiles.back().getPath()};
Zane Shelleyc85716c2021-08-17 10:54:06 -050093 o << i_servData.getCalloutList();
Zane Shelley4ed4be52021-02-15 17:53:40 -060094}
95
96//------------------------------------------------------------------------------
97
Zane Shelley5f6e3de2021-02-23 13:57:37 -060098void __addGuardList(const ServiceData& i_servData,
99 std::vector<util::FFDCFile>& io_userDataFiles)
100{
101 // Get the JSON output for the guard list.
102 nlohmann::json json;
103 i_servData.getGuardList(json);
104
105 // Create a new entry for the user data section containing the guard list.
106 io_userDataFiles.emplace_back(util::FFDCFormat::JSON, FFDC_GUARD,
107 FFDC_VERSION1);
108
109 // Use a file stream to write the JSON to file.
110 std::ofstream o{io_userDataFiles.back().getPath()};
111 o << json;
112}
113
114//------------------------------------------------------------------------------
115
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600116void __captureSignatureList(const libhei::IsolationData& i_isoData,
117 std::vector<util::FFDCFile>& io_userDataFiles)
118{
Zane Shelley021dab32020-12-08 20:28:40 -0600119 // Create a new entry for this user data section regardless if there are any
120 // signatures in the list.
121 io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_SIGNATURES,
122 FFDC_VERSION1);
123
Zane Shelleyb1106b52021-01-29 13:44:42 -0600124 // Create a streamer for easy writing to the FFDC file.
125 auto path = io_userDataFiles.back().getPath();
126 util::BinFileWriter stream{path};
127
128 // The first 4 bytes in the FFDC contains the number of signatures in the
129 // list. Then, the list of signatures will follow.
130
Zane Shelley021dab32020-12-08 20:28:40 -0600131 auto list = i_isoData.getSignatureList();
132
Zane Shelleyb1106b52021-01-29 13:44:42 -0600133 uint32_t numSigs = list.size();
134 stream << numSigs;
Zane Shelley021dab32020-12-08 20:28:40 -0600135
Zane Shelley021dab32020-12-08 20:28:40 -0600136 for (const auto& sig : list)
137 {
Zane Shelleyb1106b52021-01-29 13:44:42 -0600138 // Each signature will use the same format as the SRC (12 bytes each).
139 uint32_t word6 = 0, word7 = 0, word8 = 0;
Zane Shelley021dab32020-12-08 20:28:40 -0600140 __getSrc(sig, word6, word7, word8);
Zane Shelleyb1106b52021-01-29 13:44:42 -0600141 stream << word6 << word7 << word8;
Zane Shelley021dab32020-12-08 20:28:40 -0600142 }
143
Zane Shelleyb1106b52021-01-29 13:44:42 -0600144 // If the stream failed for any reason, remove the FFDC file.
145 if (!stream.good())
Zane Shelley021dab32020-12-08 20:28:40 -0600146 {
Zane Shelleyb1106b52021-01-29 13:44:42 -0600147 trace::err("Unable to write signature list FFDC file: %s",
148 path.string().c_str());
149 io_userDataFiles.pop_back();
Zane Shelley021dab32020-12-08 20:28:40 -0600150 }
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600151}
152
153//------------------------------------------------------------------------------
154
Zane Shelley8f60a622021-02-01 14:41:30 -0600155void __captureRegisterDump(const libhei::IsolationData& i_isoData,
156 std::vector<util::FFDCFile>& io_userDataFiles)
157{
158 // Create a new entry for this user data section regardless if there are any
159 // registers in the dump.
160 io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_REGISTER_DUMP,
161 FFDC_VERSION1);
162
163 // Create a streamer for easy writing to the FFDC file.
164 auto path = io_userDataFiles.back().getPath();
165 util::BinFileWriter stream{path};
166
167 // The first 4 bytes in the FFDC contains the number of chips with register
168 // data. Then the data for each chip will follow.
169
170 auto dump = i_isoData.getRegisterDump();
171
172 uint32_t numChips = dump.size();
173 stream << numChips;
174
175 for (const auto& entry : dump)
176 {
177 auto chip = entry.first;
178 auto regList = entry.second;
179
180 // Each chip will have the following information:
181 // 4 byte chip model/EC
182 // 2 byte chip position
Zane Shelleyff068a12021-06-17 17:45:40 -0500183 // 1 byte node position
Zane Shelley8f60a622021-02-01 14:41:30 -0600184 // 4 byte number of registers
185 // Then the data for each register will follow.
186
187 uint32_t chipType = chip.getType();
188 uint16_t chipPos = util::pdbg::getChipPos(chip);
Zane Shelleyff068a12021-06-17 17:45:40 -0500189 uint8_t nodePos = 0; // TODO: multi-node support
Zane Shelley8f60a622021-02-01 14:41:30 -0600190 uint32_t numRegs = regList.size();
Zane Shelleyff068a12021-06-17 17:45:40 -0500191 stream << chipType << chipPos << nodePos << numRegs;
Zane Shelley8f60a622021-02-01 14:41:30 -0600192
193 for (const auto& reg : regList)
194 {
195 // Each register will have the following information:
196 // 3 byte register ID
197 // 1 byte register instance
198 // 1 byte data size
199 // * byte data buffer (* depends on value of data size)
200
201 libhei::RegisterId_t regId = reg.regId; // 3 byte
202 libhei::Instance_t regInst = reg.regInst; // 1 byte
203
204 auto tmp = libhei::BitString::getMinBytes(reg.data->getBitLen());
205 if (255 < tmp)
206 {
207 trace::inf("Register data execeeded 255 and was truncated: "
208 "regId=0x%06x regInst=%u",
209 regId, regInst);
210 tmp = 255;
211 }
212 uint8_t dataSize = tmp;
213
214 stream << regId << regInst << dataSize;
215
216 stream.write(reg.data->getBufAddr(), dataSize);
217 }
218 }
219
220 // If the stream failed for any reason, remove the FFDC file.
221 if (!stream.good())
222 {
223 trace::err("Unable to write register dump FFDC file: %s",
224 path.string().c_str());
225 io_userDataFiles.pop_back();
226 }
227}
228
229//------------------------------------------------------------------------------
230
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600231std::string __getMessageRegistry(bool i_isCheckstop)
232{
233 // For now, there are only two choices:
234 return i_isCheckstop ? "org.open_power.HwDiags.Error.Checkstop"
235 : "org.open_power.HwDiags.Error.Predictive";
236}
237
238//------------------------------------------------------------------------------
239
240std::string __getMessageSeverity(bool i_isCheckstop)
241{
242 // We could specify the PEL severity in the message registry entry. However,
243 // that would require multiple copies of each entry for each possible
244 // severity. As a workaround, we will not explicitly state the PEL severity
245 // in the message registry. Instead, the message severity will be converted
246 // into a PEL severity via the openpower-pels extention of phosphor-logging.
247
248 // Initially, we'll use a severity that will generate a predictive PEL. This
249 // is intended for Terminate Immediate (TI) errors and will require service.
250 LogSvr::Entry::Level severity = LogSvr::Entry::Level::Warning;
251
252 // If the reason for analysis was due to a system checsktop, the severity
253 // will be upgraded to a unrecoverable PEL.
254 if (i_isCheckstop)
255 severity = LogSvr::Entry::Level::Error;
256
257 // Convert the message severity to a string.
258 return LogSvr::Entry::convertLevelToString(severity);
259}
260
261//------------------------------------------------------------------------------
262
Ben Tyner7029e522021-08-09 19:18:24 -0500263std::tuple<uint32_t, uint32_t> createPel(const libhei::IsolationData& i_isoData,
264 const ServiceData& i_servData)
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600265{
266 // The message registry will require additional log data to fill in keywords
267 // and additional log data.
268 std::map<std::string, std::string> logData;
269
270 // Keep track of the temporary files associated with the user data FFDC.
271 // WARNING: Once the objects stored in this vector go out of scope, the
272 // temporary files will be deleted. So they must remain in scope
273 // until the PEL is submitted.
274 std::vector<util::FFDCFile> userDataFiles;
275
276 // In several cases, it is important to know if the reason for analysis was
277 // due to a system checsktop.
Zane Shelleyca496192021-08-09 12:05:52 -0500278 bool isCheckstop = i_isoData.queryCheckstop();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600279
280 // Set words 6-9 of the SRC.
Zane Shelley8af9e462021-03-11 10:44:28 -0600281 __setSrc(i_servData.getRootCause(), logData);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600282
Zane Shelley4ed4be52021-02-15 17:53:40 -0600283 // Add the list of callouts to the PEL.
284 __addCalloutList(i_servData, userDataFiles);
285
Zane Shelley5f6e3de2021-02-23 13:57:37 -0600286 // Add the list of guard requests to the PEL.
287 __addGuardList(i_servData, userDataFiles);
288
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600289 // Capture the complete signature list.
290 __captureSignatureList(i_isoData, userDataFiles);
291
Zane Shelley8f60a622021-02-01 14:41:30 -0600292 // Capture the complete signature list.
293 __captureRegisterDump(i_isoData, userDataFiles);
294
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600295 // Now, that all of the user data files have been created, transform the
296 // data into the proper format for the PEL.
297 std::vector<util::FFDCTuple> userData;
298 util::transformFFDC(userDataFiles, userData);
299
Ben Tyner7029e522021-08-09 19:18:24 -0500300 // Response will be a tuple containing bmc-log-id, pel-log-id
301 std::tuple<uint32_t, uint32_t> response = {0, 0};
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600302
Ben Tyner7029e522021-08-09 19:18:24 -0500303 try
304 {
305 // We want to use the logging interface that returns the event log
306 // id's of the newly created logs (org.open_power.Logging.PEL) so
307 // find the service that implements this interface.
308 constexpr auto interface = "org.open_power.Logging.PEL";
309 constexpr auto path = "/xyz/openbmc_project/logging";
310 std::string service;
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600311
Ben Tyner7029e522021-08-09 19:18:24 -0500312 if (0 == util::dbus::findService(interface, path, service))
313 {
314 // Use function that returns log id's
315 constexpr auto function = "CreatePELWithFFDCFiles";
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600316
Ben Tyner7029e522021-08-09 19:18:24 -0500317 // Get access to logging interface and method for creating log.
318 auto bus = sdbusplus::bus::new_default_system();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600319
Ben Tyner7029e522021-08-09 19:18:24 -0500320 // Using direct create method (for additional data).
321 auto method =
322 bus.new_method_call(service.c_str(), path, interface, function);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600323
Ben Tyner7029e522021-08-09 19:18:24 -0500324 // The "Create" method requires manually adding the process ID.
325 logData["_PID"] = std::to_string(getpid());
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600326
Ben Tyner7029e522021-08-09 19:18:24 -0500327 // Get the message registry entry for this failure.
328 auto message = __getMessageRegistry(isCheckstop);
329
330 // Get the message severity for this failure.
331 auto severity = __getMessageSeverity(isCheckstop);
332
333 // Add the message, with additional log and user data.
334 method.append(message, severity, logData, userData);
335
336 // Log the event.
337 auto reply = bus.call(method);
338
339 // Parse reply for response
340 reply.read(response);
341 }
342 }
343 catch (const sdbusplus::exception::SdBusError& e)
344 {
345 trace::err("Exception while creating event log entry");
346 std::string exceptionString = std::string(e.what());
347 trace::err(exceptionString.c_str());
348 }
349
350 // return tuple of {bmc-log-id, pel-log-id} or {0, 0} on error
351 return response;
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600352}
353
354} // namespace analyzer