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