Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <hei_main.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <util/ffdc_file.hpp> |
| 7 | #include <util/pdbg.hpp> |
| 8 | #include <util/trace.hpp> |
| 9 | #include <xyz/openbmc_project/Logging/Create/server.hpp> |
| 10 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 11 | |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 12 | #include <fstream> |
| 13 | #include <memory> |
| 14 | |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 15 | namespace LogSvr = sdbusplus::xyz::openbmc_project::Logging::server; |
| 16 | |
| 17 | namespace analyzer |
| 18 | { |
| 19 | |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 20 | //------------------------------------------------------------------------------ |
| 21 | |
| 22 | enum FfdcSubType_t : uint8_t |
| 23 | { |
| 24 | FFDC_SIGNATURES = 0x01, |
| 25 | FFDC_CAPTURE_DATA = 0x02, |
| 26 | |
| 27 | // For the callout section, the value of '0xCA' is required per the |
| 28 | // phosphor-logging openpower-pel extention spec. |
| 29 | FFDC_CALLOUTS = 0xCA, |
| 30 | }; |
| 31 | |
| 32 | enum FfdcVersion_t : uint8_t |
| 33 | { |
| 34 | FFDC_VERSION1 = 0x01, |
| 35 | }; |
| 36 | |
| 37 | //------------------------------------------------------------------------------ |
| 38 | |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 39 | bool __isCheckstop(const libhei::IsolationData& i_isoData) |
| 40 | { |
| 41 | // Look for any signature with a system checkstop attention. |
| 42 | auto list = i_isoData.getSignatureList(); |
| 43 | auto itr = std::find_if(list.begin(), list.end(), [](const auto& s) { |
| 44 | return libhei::ATTN_TYPE_CHECKSTOP == s.getAttnType(); |
| 45 | }); |
| 46 | |
| 47 | return list.end() != itr; |
| 48 | } |
| 49 | |
| 50 | //------------------------------------------------------------------------------ |
| 51 | |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 52 | void __getSrc(const libhei::Signature& i_signature, uint32_t& o_word6, |
| 53 | uint32_t& o_word7, uint32_t& o_word8) |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 54 | { |
| 55 | // [ 0:15] chip model |
| 56 | // [16:23] reserved space in chip ID |
| 57 | // [24:31] chip EC level |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 58 | o_word6 = i_signature.getChip().getType(); |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 59 | |
| 60 | // [ 0:15] chip position |
| 61 | // [16:23] unused |
| 62 | // [24:31] signature attention type |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 63 | auto pos = util::pdbg::getChipPos(i_signature.getChip()); |
| 64 | auto attn = i_signature.getAttnType(); |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 65 | |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 66 | o_word7 = (pos & 0xffff) << 16 | (attn & 0xff); |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 67 | |
| 68 | // [ 0:15] signature ID |
| 69 | // [16:23] signature instance |
| 70 | // [24:31] signature bit position |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 71 | o_word8 = i_signature.toUint32(); |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 72 | |
| 73 | // Word 9 is currently unused |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | //------------------------------------------------------------------------------ |
| 77 | |
| 78 | void __setSrc(const libhei::Signature& i_rootCause, |
| 79 | std::map<std::string, std::string>& io_logData) |
| 80 | { |
| 81 | uint32_t word6 = 0, word7 = 0, word8 = 0; |
| 82 | __getSrc(i_rootCause, word6, word7, word8); |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 83 | |
| 84 | io_logData["SRC6"] = std::to_string(word6); |
| 85 | io_logData["SRC7"] = std::to_string(word7); |
| 86 | io_logData["SRC8"] = std::to_string(word8); |
| 87 | } |
| 88 | |
| 89 | //------------------------------------------------------------------------------ |
| 90 | |
| 91 | void __captureSignatureList(const libhei::IsolationData& i_isoData, |
| 92 | std::vector<util::FFDCFile>& io_userDataFiles) |
| 93 | { |
Zane Shelley | 021dab3 | 2020-12-08 20:28:40 -0600 | [diff] [blame] | 94 | // Create a new entry for this user data section regardless if there are any |
| 95 | // signatures in the list. |
| 96 | io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_SIGNATURES, |
| 97 | FFDC_VERSION1); |
| 98 | |
| 99 | auto list = i_isoData.getSignatureList(); |
| 100 | |
| 101 | // The first entry in the buffer will be the number of signatures in the |
| 102 | // list. |
| 103 | uint32_t numSigs = list.size(); |
| 104 | size_t sz_numSigs = sizeof(numSigs); |
| 105 | |
| 106 | // Each signature in the buffer use the same format as the SRC. |
| 107 | uint32_t word6 = 0, word7 = 0, word8 = 0; |
| 108 | size_t sz_word = sizeof(uint32_t); |
| 109 | |
| 110 | // Allocate the buffer. |
| 111 | size_t sz_buffer = sz_numSigs + (numSigs * (3 * sz_word)); |
| 112 | std::unique_ptr<char[]> buffer{new char[sz_buffer]}; |
| 113 | |
| 114 | // Insert the number of signatures. |
| 115 | numSigs = htobe32(numSigs); |
| 116 | memcpy(&buffer[0], &numSigs, sz_numSigs); |
| 117 | |
| 118 | // Insert each signature. |
| 119 | size_t idx = sz_numSigs; |
| 120 | for (const auto& sig : list) |
| 121 | { |
| 122 | __getSrc(sig, word6, word7, word8); |
| 123 | |
| 124 | word6 = htobe32(word6); |
| 125 | word7 = htobe32(word7); |
| 126 | word8 = htobe32(word8); |
| 127 | |
| 128 | // clang-format off |
| 129 | memcpy(&buffer[idx], &word6, sz_word); idx += sz_word; |
| 130 | memcpy(&buffer[idx], &word7, sz_word); idx += sz_word; |
| 131 | memcpy(&buffer[idx], &word8, sz_word); idx += sz_word; |
| 132 | // clang-format on |
| 133 | } |
| 134 | |
| 135 | // Open the file for writing. |
| 136 | auto path = io_userDataFiles.back().getPath(); |
| 137 | std::ofstream file{path, std::ios::binary}; |
| 138 | if (!file.good()) |
| 139 | { |
| 140 | trace::err("Unable to open file: %s", path.string().c_str()); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // Write the buffer to file. |
| 145 | file.write(buffer.get(), sz_buffer); |
| 146 | if (!file.good()) |
| 147 | { |
| 148 | trace::err("Unable to write file: %s", path.string().c_str()); |
| 149 | } |
| 150 | } |
Zane Shelley | d3b9bac | 2020-11-17 21:59:12 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | //------------------------------------------------------------------------------ |
| 154 | |
| 155 | std::string __getMessageRegistry(bool i_isCheckstop) |
| 156 | { |
| 157 | // For now, there are only two choices: |
| 158 | return i_isCheckstop ? "org.open_power.HwDiags.Error.Checkstop" |
| 159 | : "org.open_power.HwDiags.Error.Predictive"; |
| 160 | } |
| 161 | |
| 162 | //------------------------------------------------------------------------------ |
| 163 | |
| 164 | std::string __getMessageSeverity(bool i_isCheckstop) |
| 165 | { |
| 166 | // We could specify the PEL severity in the message registry entry. However, |
| 167 | // that would require multiple copies of each entry for each possible |
| 168 | // severity. As a workaround, we will not explicitly state the PEL severity |
| 169 | // in the message registry. Instead, the message severity will be converted |
| 170 | // into a PEL severity via the openpower-pels extention of phosphor-logging. |
| 171 | |
| 172 | // Initially, we'll use a severity that will generate a predictive PEL. This |
| 173 | // is intended for Terminate Immediate (TI) errors and will require service. |
| 174 | LogSvr::Entry::Level severity = LogSvr::Entry::Level::Warning; |
| 175 | |
| 176 | // If the reason for analysis was due to a system checsktop, the severity |
| 177 | // will be upgraded to a unrecoverable PEL. |
| 178 | if (i_isCheckstop) |
| 179 | severity = LogSvr::Entry::Level::Error; |
| 180 | |
| 181 | // Convert the message severity to a string. |
| 182 | return LogSvr::Entry::convertLevelToString(severity); |
| 183 | } |
| 184 | |
| 185 | //------------------------------------------------------------------------------ |
| 186 | |
| 187 | void createPel(const libhei::Signature& i_rootCause, |
| 188 | const libhei::IsolationData& i_isoData) |
| 189 | { |
| 190 | // The message registry will require additional log data to fill in keywords |
| 191 | // and additional log data. |
| 192 | std::map<std::string, std::string> logData; |
| 193 | |
| 194 | // Keep track of the temporary files associated with the user data FFDC. |
| 195 | // WARNING: Once the objects stored in this vector go out of scope, the |
| 196 | // temporary files will be deleted. So they must remain in scope |
| 197 | // until the PEL is submitted. |
| 198 | std::vector<util::FFDCFile> userDataFiles; |
| 199 | |
| 200 | // In several cases, it is important to know if the reason for analysis was |
| 201 | // due to a system checsktop. |
| 202 | bool isCheckstop = __isCheckstop(i_isoData); |
| 203 | |
| 204 | // Set words 6-9 of the SRC. |
| 205 | __setSrc(i_rootCause, logData); |
| 206 | |
| 207 | // Capture the complete signature list. |
| 208 | __captureSignatureList(i_isoData, userDataFiles); |
| 209 | |
| 210 | // Now, that all of the user data files have been created, transform the |
| 211 | // data into the proper format for the PEL. |
| 212 | std::vector<util::FFDCTuple> userData; |
| 213 | util::transformFFDC(userDataFiles, userData); |
| 214 | |
| 215 | // Get access to logging interface and method for creating log. |
| 216 | auto bus = sdbusplus::bus::new_default_system(); |
| 217 | |
| 218 | // Using direct create method (for additional data). |
| 219 | auto method = bus.new_method_call( |
| 220 | "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", |
| 221 | "xyz.openbmc_project.Logging.Create", "CreateWithFFDCFiles"); |
| 222 | |
| 223 | // The "Create" method requires manually adding the process ID. |
| 224 | logData["_PID"] = std::to_string(getpid()); |
| 225 | |
| 226 | // Get the message registry entry for this failure. |
| 227 | auto message = __getMessageRegistry(isCheckstop); |
| 228 | |
| 229 | // Get the message severity for this failure. |
| 230 | auto severity = __getMessageSeverity(isCheckstop); |
| 231 | |
| 232 | // Add the message, with additional log and user data. |
| 233 | method.append(message, severity, logData, userData); |
| 234 | |
| 235 | // Log the event. |
| 236 | bus.call_noreply(method); |
| 237 | } |
| 238 | |
| 239 | } // namespace analyzer |