blob: 558af5290c90580314612633edec315fd3bf5445 [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 Shelley021dab32020-12-08 20:28:40 -060030
31 // For the callout section, the value of '0xCA' is required per the
32 // phosphor-logging openpower-pel extention spec.
33 FFDC_CALLOUTS = 0xCA,
34};
35
36enum FfdcVersion_t : uint8_t
37{
38 FFDC_VERSION1 = 0x01,
39};
40
41//------------------------------------------------------------------------------
42
Zane Shelley021dab32020-12-08 20:28:40 -060043void __getSrc(const libhei::Signature& i_signature, uint32_t& o_word6,
44 uint32_t& o_word7, uint32_t& o_word8)
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060045{
46 // [ 0:15] chip model
47 // [16:23] reserved space in chip ID
48 // [24:31] chip EC level
Zane Shelley021dab32020-12-08 20:28:40 -060049 o_word6 = i_signature.getChip().getType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060050
51 // [ 0:15] chip position
Zane Shelleyff068a12021-06-17 17:45:40 -050052 // [16:23] node position
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060053 // [24:31] signature attention type
Zane Shelleyff068a12021-06-17 17:45:40 -050054 auto chipPos = util::pdbg::getChipPos(i_signature.getChip());
55 uint8_t nodePos = 0; // TODO: multi-node support
56 auto attn = i_signature.getAttnType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060057
Zane Shelleyff068a12021-06-17 17:45:40 -050058 o_word7 = (chipPos & 0xffff) << 16 | (nodePos & 0xff) << 8 | (attn & 0xff);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060059
60 // [ 0:15] signature ID
61 // [16:23] signature instance
62 // [24:31] signature bit position
Zane Shelley021dab32020-12-08 20:28:40 -060063 o_word8 = i_signature.toUint32();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060064
65 // Word 9 is currently unused
Zane Shelley021dab32020-12-08 20:28:40 -060066}
67
68//------------------------------------------------------------------------------
69
70void __setSrc(const libhei::Signature& i_rootCause,
71 std::map<std::string, std::string>& io_logData)
72{
73 uint32_t word6 = 0, word7 = 0, word8 = 0;
74 __getSrc(i_rootCause, word6, word7, word8);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060075
76 io_logData["SRC6"] = std::to_string(word6);
77 io_logData["SRC7"] = std::to_string(word7);
78 io_logData["SRC8"] = std::to_string(word8);
79}
80
81//------------------------------------------------------------------------------
82
Zane Shelley4ed4be52021-02-15 17:53:40 -060083void __addCalloutList(const ServiceData& i_servData,
84 std::vector<util::FFDCFile>& io_userDataFiles)
85{
Zane Shelley4ed4be52021-02-15 17:53:40 -060086 // Create a new entry for the user data section containing the callout list.
87 io_userDataFiles.emplace_back(util::FFDCFormat::JSON, FFDC_CALLOUTS,
88 FFDC_VERSION1);
89
90 // Use a file stream to write the JSON to file.
91 std::ofstream o{io_userDataFiles.back().getPath()};
Zane Shelleyc85716c2021-08-17 10:54:06 -050092 o << i_servData.getCalloutList();
Zane Shelley4ed4be52021-02-15 17:53:40 -060093}
94
95//------------------------------------------------------------------------------
96
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060097void __captureSignatureList(const libhei::IsolationData& i_isoData,
98 std::vector<util::FFDCFile>& io_userDataFiles)
99{
Zane Shelley021dab32020-12-08 20:28:40 -0600100 // Create a new entry for this user data section regardless if there are any
101 // signatures in the list.
102 io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_SIGNATURES,
103 FFDC_VERSION1);
104
Zane Shelleyb1106b52021-01-29 13:44:42 -0600105 // Create a streamer for easy writing to the FFDC file.
106 auto path = io_userDataFiles.back().getPath();
107 util::BinFileWriter stream{path};
108
109 // The first 4 bytes in the FFDC contains the number of signatures in the
110 // list. Then, the list of signatures will follow.
111
Zane Shelley021dab32020-12-08 20:28:40 -0600112 auto list = i_isoData.getSignatureList();
113
Zane Shelleyb1106b52021-01-29 13:44:42 -0600114 uint32_t numSigs = list.size();
115 stream << numSigs;
Zane Shelley021dab32020-12-08 20:28:40 -0600116
Zane Shelley021dab32020-12-08 20:28:40 -0600117 for (const auto& sig : list)
118 {
Zane Shelleyb1106b52021-01-29 13:44:42 -0600119 // Each signature will use the same format as the SRC (12 bytes each).
120 uint32_t word6 = 0, word7 = 0, word8 = 0;
Zane Shelley021dab32020-12-08 20:28:40 -0600121 __getSrc(sig, word6, word7, word8);
Zane Shelleyb1106b52021-01-29 13:44:42 -0600122 stream << word6 << word7 << word8;
Zane Shelley021dab32020-12-08 20:28:40 -0600123 }
124
Zane Shelleyb1106b52021-01-29 13:44:42 -0600125 // If the stream failed for any reason, remove the FFDC file.
126 if (!stream.good())
Zane Shelley021dab32020-12-08 20:28:40 -0600127 {
Zane Shelleyb1106b52021-01-29 13:44:42 -0600128 trace::err("Unable to write signature list FFDC file: %s",
129 path.string().c_str());
130 io_userDataFiles.pop_back();
Zane Shelley021dab32020-12-08 20:28:40 -0600131 }
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600132}
133
134//------------------------------------------------------------------------------
135
Zane Shelley8f60a622021-02-01 14:41:30 -0600136void __captureRegisterDump(const libhei::IsolationData& i_isoData,
137 std::vector<util::FFDCFile>& io_userDataFiles)
138{
139 // Create a new entry for this user data section regardless if there are any
140 // registers in the dump.
141 io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_REGISTER_DUMP,
142 FFDC_VERSION1);
143
144 // Create a streamer for easy writing to the FFDC file.
145 auto path = io_userDataFiles.back().getPath();
146 util::BinFileWriter stream{path};
147
148 // The first 4 bytes in the FFDC contains the number of chips with register
149 // data. Then the data for each chip will follow.
150
151 auto dump = i_isoData.getRegisterDump();
152
153 uint32_t numChips = dump.size();
154 stream << numChips;
155
156 for (const auto& entry : dump)
157 {
158 auto chip = entry.first;
159 auto regList = entry.second;
160
161 // Each chip will have the following information:
162 // 4 byte chip model/EC
163 // 2 byte chip position
Zane Shelleyff068a12021-06-17 17:45:40 -0500164 // 1 byte node position
Zane Shelley8f60a622021-02-01 14:41:30 -0600165 // 4 byte number of registers
166 // Then the data for each register will follow.
167
168 uint32_t chipType = chip.getType();
169 uint16_t chipPos = util::pdbg::getChipPos(chip);
Zane Shelleyff068a12021-06-17 17:45:40 -0500170 uint8_t nodePos = 0; // TODO: multi-node support
Zane Shelley8f60a622021-02-01 14:41:30 -0600171 uint32_t numRegs = regList.size();
Zane Shelleyff068a12021-06-17 17:45:40 -0500172 stream << chipType << chipPos << nodePos << numRegs;
Zane Shelley8f60a622021-02-01 14:41:30 -0600173
174 for (const auto& reg : regList)
175 {
176 // Each register will have the following information:
177 // 3 byte register ID
178 // 1 byte register instance
179 // 1 byte data size
180 // * byte data buffer (* depends on value of data size)
181
182 libhei::RegisterId_t regId = reg.regId; // 3 byte
183 libhei::Instance_t regInst = reg.regInst; // 1 byte
184
185 auto tmp = libhei::BitString::getMinBytes(reg.data->getBitLen());
186 if (255 < tmp)
187 {
188 trace::inf("Register data execeeded 255 and was truncated: "
189 "regId=0x%06x regInst=%u",
190 regId, regInst);
191 tmp = 255;
192 }
193 uint8_t dataSize = tmp;
194
195 stream << regId << regInst << dataSize;
196
197 stream.write(reg.data->getBufAddr(), dataSize);
198 }
199 }
200
201 // If the stream failed for any reason, remove the FFDC file.
202 if (!stream.good())
203 {
204 trace::err("Unable to write register dump FFDC file: %s",
205 path.string().c_str());
206 io_userDataFiles.pop_back();
207 }
208}
209
210//------------------------------------------------------------------------------
211
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600212std::string __getMessageRegistry(bool i_isCheckstop)
213{
214 // For now, there are only two choices:
215 return i_isCheckstop ? "org.open_power.HwDiags.Error.Checkstop"
216 : "org.open_power.HwDiags.Error.Predictive";
217}
218
219//------------------------------------------------------------------------------
220
221std::string __getMessageSeverity(bool i_isCheckstop)
222{
223 // We could specify the PEL severity in the message registry entry. However,
224 // that would require multiple copies of each entry for each possible
225 // severity. As a workaround, we will not explicitly state the PEL severity
226 // in the message registry. Instead, the message severity will be converted
227 // into a PEL severity via the openpower-pels extention of phosphor-logging.
228
229 // Initially, we'll use a severity that will generate a predictive PEL. This
230 // is intended for Terminate Immediate (TI) errors and will require service.
231 LogSvr::Entry::Level severity = LogSvr::Entry::Level::Warning;
232
233 // If the reason for analysis was due to a system checsktop, the severity
234 // will be upgraded to a unrecoverable PEL.
235 if (i_isCheckstop)
236 severity = LogSvr::Entry::Level::Error;
237
238 // Convert the message severity to a string.
239 return LogSvr::Entry::convertLevelToString(severity);
240}
241
242//------------------------------------------------------------------------------
243
Ben Tyner7029e522021-08-09 19:18:24 -0500244std::tuple<uint32_t, uint32_t> createPel(const libhei::IsolationData& i_isoData,
245 const ServiceData& i_servData)
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600246{
247 // The message registry will require additional log data to fill in keywords
248 // and additional log data.
249 std::map<std::string, std::string> logData;
250
251 // Keep track of the temporary files associated with the user data FFDC.
252 // WARNING: Once the objects stored in this vector go out of scope, the
253 // temporary files will be deleted. So they must remain in scope
254 // until the PEL is submitted.
255 std::vector<util::FFDCFile> userDataFiles;
256
257 // In several cases, it is important to know if the reason for analysis was
258 // due to a system checsktop.
Zane Shelleyca496192021-08-09 12:05:52 -0500259 bool isCheckstop = i_isoData.queryCheckstop();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600260
261 // Set words 6-9 of the SRC.
Zane Shelley8af9e462021-03-11 10:44:28 -0600262 __setSrc(i_servData.getRootCause(), logData);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600263
Zane Shelley4ed4be52021-02-15 17:53:40 -0600264 // Add the list of callouts to the PEL.
265 __addCalloutList(i_servData, userDataFiles);
266
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600267 // Capture the complete signature list.
268 __captureSignatureList(i_isoData, userDataFiles);
269
Zane Shelley8f60a622021-02-01 14:41:30 -0600270 // Capture the complete signature list.
271 __captureRegisterDump(i_isoData, userDataFiles);
272
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600273 // Now, that all of the user data files have been created, transform the
274 // data into the proper format for the PEL.
275 std::vector<util::FFDCTuple> userData;
276 util::transformFFDC(userDataFiles, userData);
277
Ben Tyner7029e522021-08-09 19:18:24 -0500278 // Response will be a tuple containing bmc-log-id, pel-log-id
279 std::tuple<uint32_t, uint32_t> response = {0, 0};
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600280
Ben Tyner7029e522021-08-09 19:18:24 -0500281 try
282 {
283 // We want to use the logging interface that returns the event log
284 // id's of the newly created logs (org.open_power.Logging.PEL) so
285 // find the service that implements this interface.
286 constexpr auto interface = "org.open_power.Logging.PEL";
287 constexpr auto path = "/xyz/openbmc_project/logging";
288 std::string service;
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600289
Ben Tyner7029e522021-08-09 19:18:24 -0500290 if (0 == util::dbus::findService(interface, path, service))
291 {
292 // Use function that returns log id's
293 constexpr auto function = "CreatePELWithFFDCFiles";
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600294
Ben Tyner7029e522021-08-09 19:18:24 -0500295 // Get access to logging interface and method for creating log.
296 auto bus = sdbusplus::bus::new_default_system();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600297
Ben Tyner7029e522021-08-09 19:18:24 -0500298 // Using direct create method (for additional data).
299 auto method =
300 bus.new_method_call(service.c_str(), path, interface, function);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600301
Ben Tyner7029e522021-08-09 19:18:24 -0500302 // The "Create" method requires manually adding the process ID.
303 logData["_PID"] = std::to_string(getpid());
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600304
Ben Tyner7029e522021-08-09 19:18:24 -0500305 // Get the message registry entry for this failure.
306 auto message = __getMessageRegistry(isCheckstop);
307
308 // Get the message severity for this failure.
309 auto severity = __getMessageSeverity(isCheckstop);
310
311 // Add the message, with additional log and user data.
312 method.append(message, severity, logData, userData);
313
314 // Log the event.
315 auto reply = bus.call(method);
316
317 // Parse reply for response
318 reply.read(response);
319 }
320 }
321 catch (const sdbusplus::exception::SdBusError& e)
322 {
323 trace::err("Exception while creating event log entry");
324 std::string exceptionString = std::string(e.what());
325 trace::err(exceptionString.c_str());
326 }
327
328 // return tuple of {bmc-log-id, pel-log-id} or {0, 0} on error
329 return response;
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600330}
331
332} // namespace analyzer