1 #include <unistd.h>
2
3 #include <analyzer/analyzer_main.hpp>
4 #include <attn/attn_common.hpp>
5 #include <attn/attn_dbus.hpp>
6 #include <attn/attn_dump.hpp>
7 #include <attn/attn_logging.hpp>
8 #include <attn/pel/pel_minimal.hpp>
9 #include <phosphor-logging/log.hpp>
10 #include <util/dbus.hpp>
11 #include <util/ffdc.hpp>
12 #include <util/trace.hpp>
13
14 namespace attn
15 {
16 /** @brief Tuple containing information about ffdc files */
17 using FFDCTuple =
18 std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
19
20 /**
21 * Create FFDCTuple objects corresponding to the specified FFDC files.
22 *
23 * The D-Bus method to create an error log requires a vector of tuples to
24 * pass in the FFDC file information.
25 *
26 * @param files - FFDC files
27 * @return vector of FFDCTuple objects
28 */
createFFDCTuples(const std::vector<util::FFDCFile> & files)29 std::vector<FFDCTuple> createFFDCTuples(
30 const std::vector<util::FFDCFile>& files)
31 {
32 std::vector<FFDCTuple> ffdcTuples{};
33 util::transformFFDC(files, ffdcTuples);
34
35 return ffdcTuples;
36 }
37
38 /**
39 * @brief Create an FFDCFile object containing raw data
40 *
41 * Throws an exception if an error occurs.
42 *
43 * @param i_buffer - raw data to add to ffdc faw data file
44 * @param i_size - size of the raw data
45 * @return FFDCFile object
46 */
createFFDCRawFile(void * i_buffer,size_t i_size)47 util::FFDCFile createFFDCRawFile(void* i_buffer, size_t i_size)
48 {
49 util::FFDCFile file{util::FFDCFormat::Custom};
50
51 // Write buffer to file and then reset file description file offset
52 int fd = file.getFileDescriptor();
53 size_t numBytes = write(fd, static_cast<char*>(i_buffer), i_size);
54 if (i_size != numBytes)
55 {
56 trace::err("%s only %u of %u bytes written", file.getPath().c_str(),
57 numBytes, i_size);
58 }
59
60 lseek(fd, 0, SEEK_SET);
61
62 return file;
63 }
64
65 /**
66 * Create FFDCFile objects containing debug data to store in the error log.
67 *
68 * If an error occurs, the error is written to the journal but an exception
69 * is not thrown.
70 *
71 * @param i_buffer - raw data (if creating raw dump ffdc entry in log)
72 * @return vector of FFDCFile objects
73 */
createFFDCFiles(char * i_buffer=nullptr,size_t i_size=0)74 std::vector<util::FFDCFile> createFFDCFiles(char* i_buffer = nullptr,
75 size_t i_size = 0)
76 {
77 std::vector<util::FFDCFile> files{};
78
79 // Create raw dump file
80 if ((nullptr != i_buffer) && (0 != i_size))
81 {
82 files.emplace_back(createFFDCRawFile(i_buffer, i_size));
83 }
84
85 // Create trace dump file
86 util::createFFDCTraceFiles(files);
87
88 // Add PRD scratch registers
89 addPrdScratchRegs(files);
90
91 return files;
92 }
93
94 /**
95 * Create a PEL from an existing PEL
96 *
97 * Create a new PEL based on the specified raw PEL and submit the new PEL
98 * to the backend logging code as a raw PEL. Note that additional data map
99 * here contains data to be committed to the PEL and it can also be used to
100 * create the PEL as it contains needed information.
101 *
102 * @param i_rawPel - buffer containing a raw PEL
103 * @param i_additional - additional data to be added to the new PEL
104 */
createPelCustom(std::vector<uint8_t> & i_rawPel,std::map<std::string,std::string> i_additional)105 void createPelCustom(std::vector<uint8_t>& i_rawPel,
106 std::map<std::string, std::string> i_additional)
107 {
108 // create PEL object from buffer
109 auto tiPel = std::make_unique<pel::PelMinimal>(i_rawPel);
110
111 // The additional data contains the TI info as well as the value for the
112 // subystem that provided the TI info. Get the subystem from additional
113 // data and then populate the primary SRC and SRC words for the custom PEL
114 // based on the subsystem's TI info.
115 std::map<std::string, std::string>::iterator it;
116 uint8_t subsystem;
117
118 it = i_additional.find("Subsystem");
119 if (it != i_additional.end())
120 {
121 subsystem = std::stoi(it->second);
122 tiPel->setSubsystem(subsystem);
123 }
124 else
125 {
126 // The entry with key "Subsystem" does not exist in the additional map.
127 // Log the error, create failure event, and return.
128 trace::err("Error the key Subsystem does not exist in the map.");
129 eventAttentionFail((int)AttnSection::attnLogging | ATTN_INVALID_KEY);
130 return;
131 }
132
133 // If recoverable attentions are active we will call the analyzer and
134 // then link the custom pel to analyzer pel.
135 it = i_additional.find("recoverables");
136 if (it != i_additional.end() && "true" == it->second)
137 {
138 DumpParameters dumpParameters;
139 auto plid = analyzer::analyzeHardware(
140 analyzer::AnalysisType::TERMINATE_IMMEDIATE, dumpParameters);
141 if (0 != plid)
142 {
143 // Link the PLID if an attention was found and a PEL was generated.
144 tiPel->setPlid(plid);
145 }
146 }
147
148 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
149 {
150 // populate hypervisor SRC words
151 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
152 (uint32_t)std::stoul(i_additional["0x10 SRC Word 12"], nullptr, 16),
153 (uint32_t)std::stoul(i_additional["0x14 SRC Word 13"], nullptr, 16),
154 (uint32_t)std::stoul(i_additional["0x18 SRC Word 14"], nullptr, 16),
155 (uint32_t)std::stoul(i_additional["0x1c SRC Word 15"], nullptr, 16),
156 (uint32_t)std::stoul(i_additional["0x20 SRC Word 16"], nullptr, 16),
157 (uint32_t)std::stoul(i_additional["0x24 SRC Word 17"], nullptr, 16),
158 (uint32_t)std::stoul(i_additional["0x28 SRC Word 18"], nullptr, 16),
159 (uint32_t)std::stoul(i_additional["0x2c SRC Word 19"], nullptr,
160 16)});
161
162 // Populate phyp primary SRC
163
164 // char array for raw pel src
165 std::array<char, pel::asciiStringSize> srcChars{'0'};
166 std::string srcString;
167
168 // src from TI info
169 it = i_additional.find("SrcAscii");
170 if (it != i_additional.end())
171 {
172 srcString = it->second;
173 }
174 else
175 {
176 // The entry with key "Subsystem" does not exist in the additional
177 // map. Log the error, create failure event, and return.
178 trace::err("Error the key SrcAscii does not exist in the map.");
179 eventAttentionFail(
180 (int)AttnSection::attnLogging | ATTN_INVALID_KEY);
181 return;
182 }
183
184 // copy from string to char array
185 srcString.copy(srcChars.data(),
186 std::min(srcString.size(), pel::asciiStringSize), 0);
187
188 tiPel->setAsciiString(srcChars); // pel object src is char array
189
190 // set symptom-id
191 auto symptomId = (srcString.substr(0, 8) + '_');
192
193 symptomId += (i_additional["0x10 SRC Word 12"]);
194 symptomId += (i_additional["0x14 SRC Word 13"] + '_');
195 symptomId += (i_additional["0x18 SRC Word 14"]);
196 symptomId += (i_additional["0x1c SRC Word 15"] + '_');
197 symptomId += (i_additional["0x20 SRC Word 16"]);
198 symptomId += (i_additional["0x24 SRC Word 17"] + '_');
199 symptomId += (i_additional["0x28 SRC Word 18"]);
200 symptomId += (i_additional["0x2c SRC Word 19"]);
201
202 // setSymptomId will take care of required null-terminate and padding
203 tiPel->setSymptomId(symptomId);
204 }
205 else
206 {
207 // Populate hostboot SRC words - note HB word 0 from the shared info
208 // data (additional data "0x10 HB Word") is reflected in the PEL as
209 // "reason code" so we zero it here. Also note that the first word
210 // in this group of words starts at word 0 and word 1 does not exits.
211 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
212 (uint32_t)0x00000000,
213 (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], nullptr, 16),
214 (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], nullptr, 16),
215 (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], nullptr, 16),
216 (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], nullptr, 16),
217 (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], nullptr, 16),
218 (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], nullptr, 16),
219 (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], nullptr, 16)});
220
221 // Populate hostboot primary SRC
222
223 // char array for raw pel src
224 std::array<char, pel::asciiStringSize> srcChars{'0'};
225 std::string srcString;
226
227 // src from TI info
228 it = i_additional.find("SrcAscii");
229 if (it != i_additional.end())
230 {
231 srcString = it->second;
232 }
233 else
234 {
235 // The entry with key "Subsystem" does not exist in the additional
236 // map. Log the error, create failure event, and return.
237 trace::err("Error the key SrcAscii does not exist in the map.");
238 eventAttentionFail(
239 (int)AttnSection::attnLogging | ATTN_INVALID_KEY);
240 return;
241 }
242
243 // copy from string to char array
244 srcString.copy(srcChars.data(),
245 std::min(srcString.size(), pel::asciiStringSize), 0);
246
247 tiPel->setAsciiString(srcChars); // pel object src is char array
248
249 // set symptom-id
250 auto symptomId = (srcString.substr(0, 8) + '_');
251
252 symptomId += (i_additional["0x10 HB Word 0"]); // note: word 1
253 symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist
254 symptomId += (i_additional["0x18 HB Word 3"]);
255 symptomId += (i_additional["0x1c HB Word 4"] + '_');
256 symptomId += (i_additional["0x20 HB Word 5"]);
257 symptomId += (i_additional["0x24 HB Word 6"] + '_');
258 symptomId += (i_additional["0x28 HB Word 7"]);
259 symptomId += (i_additional["0x2c HB Word 8"]);
260
261 // setSymptomId will take care of required null-terminate and padding
262 tiPel->setSymptomId(symptomId);
263 }
264
265 // set severity, event type and action flags
266 tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination));
267 tiPel->setType(static_cast<uint8_t>(pel::EventType::na));
268
269 auto actionFlags = pel::ActionFlags::service | pel::ActionFlags::report |
270 pel::ActionFlags::call;
271
272 it = i_additional.find("hidden");
273 if (it != i_additional.end() && "true" == it->second)
274 {
275 trace::inf("making HB TI PEL hidden");
276 actionFlags = actionFlags | pel::ActionFlags::hidden;
277 }
278
279 tiPel->setAction(static_cast<uint16_t>(actionFlags));
280
281 // The raw PEL that we used as the basis for this custom PEL contains some
282 // user data sections that do not need to be in this PEL. However we do
283 // want to include the raw TI information.
284 int ffdcCount = 0;
285 it = i_additional.find("FFDC count");
286 if (it != i_additional.end())
287 {
288 // remove all sections except 1 (raw Ti info)
289 ffdcCount = std::stoi(it->second) - 1;
290 }
291 tiPel->setSectionCount(tiPel->getSectionCount() - ffdcCount);
292
293 // Update the raw PEL with the new custom PEL data
294 tiPel->raw(i_rawPel);
295
296 // create PEL from raw data
297 createPelRaw(i_rawPel);
298 }
299
300 /**
301 * Log an event handled by the attention handler
302 *
303 * Basic (non TI) events will generate a standard message-registry based PEL
304 *
305 * TI events will create two PEL's. One PEL will be informational and will
306 * contain trace information relevent to attention handler. The second PEL
307 * will be specific to the TI type (including the primary SRC) and will be
308 * based off of the TI information provided to the attention handler through
309 * shared TI info data area.
310 *
311 * @param i_event - The event type
312 * @param i_additional - Additional PEL data
313 * @param i_ffdc - FFDC PEL data
314 * @param i_severity - Severity level
315 * @return Event log Id (0 if no event log generated)
316 */
event(EventType i_event,std::map<std::string,std::string> & i_additional,const std::vector<util::FFDCFile> & i_ffdc,std::string i_severity=levelPelError)317 uint32_t event(EventType i_event,
318 std::map<std::string, std::string>& i_additional,
319 const std::vector<util::FFDCFile>& i_ffdc,
320 std::string i_severity = levelPelError)
321 {
322 uint32_t pelId = 0; // assume no event log generated
323
324 bool eventValid = false; // assume no event created
325 bool tiEvent = false; // assume not a terminate event
326
327 // count user data sections so we can fixup custom PEL
328 i_additional["FFDC count"] = std::to_string(i_ffdc.size());
329
330 std::string eventName;
331
332 switch (i_event)
333 {
334 case EventType::Checkstop:
335 eventName = "org.open_power.HwDiags.Error.Checkstop";
336 eventValid = true;
337 break;
338 case EventType::Terminate:
339 eventName = "org.open_power.Attn.Error.Terminate";
340 eventValid = true;
341 tiEvent = true;
342 break;
343 case EventType::Vital:
344 eventName = "org.open_power.Attn.Error.Vital";
345 eventValid = true;
346 break;
347 case EventType::HwDiagsFail:
348 case EventType::AttentionFail:
349 eventName = "org.open_power.Attn.Error.Fail";
350 eventValid = true;
351 break;
352 default:
353 eventValid = false;
354 break;
355 }
356
357 if (true == eventValid)
358 {
359 // Create PEL with additional data and FFDC data. The newly created
360 // PEL's platform log-id will be returned.
361 pelId = util::dbus::createPel(eventName, i_severity, i_additional,
362 createFFDCTuples(i_ffdc));
363
364 // If this is a TI event we will create an additional PEL that is
365 // specific to the subsystem that generated the TI.
366 if ((0 != pelId) && (true == tiEvent))
367 {
368 // get file descriptor and size of information PEL
369 int pelFd = getPel(pelId);
370
371 // if PEL found, read into buffer
372 if (-1 != pelFd)
373 {
374 auto pelSize = lseek(pelFd, 0, SEEK_END);
375 lseek(pelFd, 0, SEEK_SET);
376
377 // read information PEL into buffer
378 std::vector<uint8_t> buffer(pelSize);
379 size_t numBytes = read(pelFd, buffer.data(), buffer.size());
380 if (buffer.size() != numBytes)
381 {
382 trace::err("Error reading event log: %u of %u bytes read",
383 numBytes, buffer.size());
384 }
385 else
386 {
387 // create PEL from buffer
388 createPelCustom(buffer, i_additional);
389 }
390
391 close(pelFd);
392 }
393
394 std::map<std::string, std::string>::iterator it;
395 uint8_t subsystem;
396
397 it = i_additional.find("Subsystem");
398 if (it != i_additional.end())
399 {
400 subsystem = std::stoi(it->second);
401 }
402 else
403 {
404 // The entry with key "Subsystem" does not exist in the
405 // additional map. Log the error, create failure event, and
406 // return.
407 trace::err(
408 "Error the key Subsystem does not exist in the map.");
409 eventAttentionFail(
410 (int)AttnSection::attnLogging | ATTN_INVALID_KEY);
411 return 0;
412 }
413
414 // If not hypervisor TI
415 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem)
416 {
417 // Request a dump and transition the host
418 if ("true" == i_additional["Dump"])
419 {
420 // will not return until dump is complete
421 requestDump(pelId, DumpParameters{0, DumpType::Hostboot});
422 }
423 }
424 }
425 }
426 return pelId;
427 }
428
429 /**
430 * Commit special attention TI event to log
431 *
432 * Create a event log with provided additional information and standard
433 * FFDC data plus TI FFDC data
434 *
435 * @param i_additional - Additional log data
436 * @param i_ti_InfoData - TI FFDC data
437 */
eventTerminate(std::map<std::string,std::string> i_additionalData,char * i_tiInfoData)438 void eventTerminate(std::map<std::string, std::string> i_additionalData,
439 char* i_tiInfoData)
440 {
441 uint32_t tiInfoSize = 0; // assume TI info was not available
442
443 if (nullptr != i_tiInfoData)
444 {
445 tiInfoSize = 56; // assume not hypervisor TI
446
447 std::map<std::string, std::string>::iterator it;
448 uint8_t subsystem;
449
450 it = i_additionalData.find("Subsystem");
451 if (it != i_additionalData.end())
452 {
453 subsystem = std::stoi(it->second);
454 }
455 else
456 {
457 // The entry with key "Subsystem" does not exist in the additional
458 // map. Log the error, create failure event, and return.
459 trace::err("Error the key Subsystem does not exist in the map.");
460 eventAttentionFail(
461 (int)AttnSection::attnLogging | ATTN_INVALID_KEY);
462 return;
463 }
464
465 // If hypervisor
466 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
467 {
468 tiInfoSize = 1024; // assume hypervisor max
469
470 // hypervisor may just want some of the data
471 if (0 == (*(i_tiInfoData + 0x09) & 0x01))
472 {
473 uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50);
474 uint32_t tiAdditional = be32toh(*additionalLength);
475 tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional));
476 }
477 }
478 }
479
480 trace::inf("TI info size = %u", tiInfoSize);
481
482 event(EventType::Terminate, i_additionalData,
483 createFFDCFiles(i_tiInfoData, tiInfoSize));
484 }
485
486 /** @brief Commit SBE vital event to log, returns event log ID */
eventVital(std::string severity)487 uint32_t eventVital(std::string severity)
488 {
489 // Additional data for log
490 std::map<std::string, std::string> additionalData;
491
492 // Create log event with additional data and FFDC data
493 return event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0),
494 severity);
495 }
496
497 /**
498 * Commit attention handler failure event to log
499 *
500 * Create an event log containing the specified error code.
501 *
502 * @param i_error - Error code
503 */
eventAttentionFail(int i_error)504 void eventAttentionFail(int i_error)
505 {
506 // Additional data for log
507 std::map<std::string, std::string> additionalData;
508 additionalData["ERROR_CODE"] = std::to_string(i_error);
509
510 // Create log event with additional data and FFDC data
511 event(EventType::AttentionFail, additionalData,
512 createFFDCFiles(nullptr, 0));
513 }
514
515 } // namespace attn
516