2
0

ghes.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Support for generating APEI tables and recording CPER for Guests
  3. *
  4. * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Author: Dongjiu Geng <gengdongjiu@huawei.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "hw/acpi/ghes.h"
  22. #include "hw/acpi/aml-build.h"
  23. #include "qemu/error-report.h"
  24. #include "hw/acpi/generic_event_device.h"
  25. #include "hw/nvram/fw_cfg.h"
  26. #include "qemu/uuid.h"
  27. #define ACPI_HW_ERROR_FW_CFG_FILE "etc/hardware_errors"
  28. #define ACPI_HW_ERROR_ADDR_FW_CFG_FILE "etc/hardware_errors_addr"
  29. /* The max size in bytes for one error block */
  30. #define ACPI_GHES_MAX_RAW_DATA_LENGTH (1 * KiB)
  31. /* Generic Hardware Error Source version 2 */
  32. #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2 10
  33. /* Address offset in Generic Address Structure(GAS) */
  34. #define GAS_ADDR_OFFSET 4
  35. /*
  36. * The total size of Generic Error Data Entry
  37. * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data,
  38. * Table 18-343 Generic Error Data Entry
  39. */
  40. #define ACPI_GHES_DATA_LENGTH 72
  41. /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */
  42. #define ACPI_GHES_MEM_CPER_LENGTH 80
  43. /* Masks for block_status flags */
  44. #define ACPI_GEBS_UNCORRECTABLE 1
  45. /*
  46. * Total size for Generic Error Status Block except Generic Error Data Entries
  47. * ACPI 6.2: 18.3.2.7.1 Generic Error Data,
  48. * Table 18-380 Generic Error Status Block
  49. */
  50. #define ACPI_GHES_GESB_SIZE 20
  51. /*
  52. * Values for error_severity field
  53. */
  54. enum AcpiGenericErrorSeverity {
  55. ACPI_CPER_SEV_RECOVERABLE = 0,
  56. ACPI_CPER_SEV_FATAL = 1,
  57. ACPI_CPER_SEV_CORRECTED = 2,
  58. ACPI_CPER_SEV_NONE = 3,
  59. };
  60. /*
  61. * Hardware Error Notification
  62. * ACPI 4.0: 17.3.2.7 Hardware Error Notification
  63. * Composes dummy Hardware Error Notification descriptor of specified type
  64. */
  65. static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
  66. {
  67. /* Type */
  68. build_append_int_noprefix(table, type, 1);
  69. /*
  70. * Length:
  71. * Total length of the structure in bytes
  72. */
  73. build_append_int_noprefix(table, 28, 1);
  74. /* Configuration Write Enable */
  75. build_append_int_noprefix(table, 0, 2);
  76. /* Poll Interval */
  77. build_append_int_noprefix(table, 0, 4);
  78. /* Vector */
  79. build_append_int_noprefix(table, 0, 4);
  80. /* Switch To Polling Threshold Value */
  81. build_append_int_noprefix(table, 0, 4);
  82. /* Switch To Polling Threshold Window */
  83. build_append_int_noprefix(table, 0, 4);
  84. /* Error Threshold Value */
  85. build_append_int_noprefix(table, 0, 4);
  86. /* Error Threshold Window */
  87. build_append_int_noprefix(table, 0, 4);
  88. }
  89. /*
  90. * Generic Error Data Entry
  91. * ACPI 6.1: 18.3.2.7.1 Generic Error Data
  92. */
  93. static void acpi_ghes_generic_error_data(GArray *table,
  94. const uint8_t *section_type, uint32_t error_severity,
  95. uint8_t validation_bits, uint8_t flags,
  96. uint32_t error_data_length, QemuUUID fru_id,
  97. uint64_t time_stamp)
  98. {
  99. const uint8_t fru_text[20] = {0};
  100. /* Section Type */
  101. g_array_append_vals(table, section_type, 16);
  102. /* Error Severity */
  103. build_append_int_noprefix(table, error_severity, 4);
  104. /* Revision */
  105. build_append_int_noprefix(table, 0x300, 2);
  106. /* Validation Bits */
  107. build_append_int_noprefix(table, validation_bits, 1);
  108. /* Flags */
  109. build_append_int_noprefix(table, flags, 1);
  110. /* Error Data Length */
  111. build_append_int_noprefix(table, error_data_length, 4);
  112. /* FRU Id */
  113. g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
  114. /* FRU Text */
  115. g_array_append_vals(table, fru_text, sizeof(fru_text));
  116. /* Timestamp */
  117. build_append_int_noprefix(table, time_stamp, 8);
  118. }
  119. /*
  120. * Generic Error Status Block
  121. * ACPI 6.1: 18.3.2.7.1 Generic Error Data
  122. */
  123. static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status,
  124. uint32_t raw_data_offset, uint32_t raw_data_length,
  125. uint32_t data_length, uint32_t error_severity)
  126. {
  127. /* Block Status */
  128. build_append_int_noprefix(table, block_status, 4);
  129. /* Raw Data Offset */
  130. build_append_int_noprefix(table, raw_data_offset, 4);
  131. /* Raw Data Length */
  132. build_append_int_noprefix(table, raw_data_length, 4);
  133. /* Data Length */
  134. build_append_int_noprefix(table, data_length, 4);
  135. /* Error Severity */
  136. build_append_int_noprefix(table, error_severity, 4);
  137. }
  138. /* UEFI 2.6: N.2.5 Memory Error Section */
  139. static void acpi_ghes_build_append_mem_cper(GArray *table,
  140. uint64_t error_physical_addr)
  141. {
  142. /*
  143. * Memory Error Record
  144. */
  145. /* Validation Bits */
  146. build_append_int_noprefix(table,
  147. (1ULL << 14) | /* Type Valid */
  148. (1ULL << 1) /* Physical Address Valid */,
  149. 8);
  150. /* Error Status */
  151. build_append_int_noprefix(table, 0, 8);
  152. /* Physical Address */
  153. build_append_int_noprefix(table, error_physical_addr, 8);
  154. /* Skip all the detailed information normally found in such a record */
  155. build_append_int_noprefix(table, 0, 48);
  156. /* Memory Error Type */
  157. build_append_int_noprefix(table, 0 /* Unknown error */, 1);
  158. /* Skip all the detailed information normally found in such a record */
  159. build_append_int_noprefix(table, 0, 7);
  160. }
  161. static void
  162. ghes_gen_err_data_uncorrectable_recoverable(GArray *block,
  163. const uint8_t *section_type,
  164. int data_length)
  165. {
  166. /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
  167. * Table 17-13 Generic Error Data Entry
  168. */
  169. QemuUUID fru_id = {};
  170. /* Build the new generic error status block header */
  171. acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
  172. 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
  173. /* Build this new generic error data entry header */
  174. acpi_ghes_generic_error_data(block, section_type,
  175. ACPI_CPER_SEV_RECOVERABLE, 0, 0,
  176. ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
  177. }
  178. /*
  179. * Build table for the hardware error fw_cfg blob.
  180. * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
  181. * See docs/specs/acpi_hest_ghes.rst for blobs format.
  182. */
  183. static void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
  184. {
  185. int i, error_status_block_offset;
  186. /* Build error_block_address */
  187. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  188. build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
  189. }
  190. /* Build read_ack_register */
  191. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  192. /*
  193. * Initialize the value of read_ack_register to 1, so GHES can be
  194. * writable after (re)boot.
  195. * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
  196. * (GHESv2 - Type 10)
  197. */
  198. build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
  199. }
  200. /* Generic Error Status Block offset in the hardware error fw_cfg blob */
  201. error_status_block_offset = hardware_errors->len;
  202. /* Reserve space for Error Status Data Block */
  203. acpi_data_push(hardware_errors,
  204. ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
  205. /* Tell guest firmware to place hardware_errors blob into RAM */
  206. bios_linker_loader_alloc(linker, ACPI_HW_ERROR_FW_CFG_FILE,
  207. hardware_errors, sizeof(uint64_t), false);
  208. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  209. /*
  210. * Tell firmware to patch error_block_address entries to point to
  211. * corresponding "Generic Error Status Block"
  212. */
  213. bios_linker_loader_add_pointer(linker,
  214. ACPI_HW_ERROR_FW_CFG_FILE,
  215. sizeof(uint64_t) * i,
  216. sizeof(uint64_t),
  217. ACPI_HW_ERROR_FW_CFG_FILE,
  218. error_status_block_offset +
  219. i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
  220. }
  221. /*
  222. * tell firmware to write hardware_errors GPA into
  223. * hardware_errors_addr fw_cfg, once the former has been initialized.
  224. */
  225. bios_linker_loader_write_pointer(linker, ACPI_HW_ERROR_ADDR_FW_CFG_FILE, 0,
  226. sizeof(uint64_t),
  227. ACPI_HW_ERROR_FW_CFG_FILE, 0);
  228. }
  229. /* Build Generic Hardware Error Source version 2 (GHESv2) */
  230. static void build_ghes_v2(GArray *table_data,
  231. BIOSLinker *linker,
  232. enum AcpiGhesNotifyType notify,
  233. uint16_t source_id)
  234. {
  235. uint64_t address_offset;
  236. /*
  237. * Type:
  238. * Generic Hardware Error Source version 2(GHESv2 - Type 10)
  239. */
  240. build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
  241. /* Source Id */
  242. build_append_int_noprefix(table_data, source_id, 2);
  243. /* Related Source Id */
  244. build_append_int_noprefix(table_data, 0xffff, 2);
  245. /* Flags */
  246. build_append_int_noprefix(table_data, 0, 1);
  247. /* Enabled */
  248. build_append_int_noprefix(table_data, 1, 1);
  249. /* Number of Records To Pre-allocate */
  250. build_append_int_noprefix(table_data, 1, 4);
  251. /* Max Sections Per Record */
  252. build_append_int_noprefix(table_data, 1, 4);
  253. /* Max Raw Data Length */
  254. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  255. address_offset = table_data->len;
  256. /* Error Status Address */
  257. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  258. 4 /* QWord access */, 0);
  259. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  260. address_offset + GAS_ADDR_OFFSET,
  261. sizeof(uint64_t),
  262. ACPI_HW_ERROR_FW_CFG_FILE,
  263. source_id * sizeof(uint64_t));
  264. /* Notification Structure */
  265. build_ghes_hw_error_notification(table_data, notify);
  266. /* Error Status Block Length */
  267. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  268. /*
  269. * Read Ack Register
  270. * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
  271. * version 2 (GHESv2 - Type 10)
  272. */
  273. address_offset = table_data->len;
  274. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  275. 4 /* QWord access */, 0);
  276. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  277. address_offset + GAS_ADDR_OFFSET,
  278. sizeof(uint64_t),
  279. ACPI_HW_ERROR_FW_CFG_FILE,
  280. (ACPI_GHES_ERROR_SOURCE_COUNT + source_id)
  281. * sizeof(uint64_t));
  282. /*
  283. * Read Ack Preserve field
  284. * We only provide the first bit in Read Ack Register to OSPM to write
  285. * while the other bits are preserved.
  286. */
  287. build_append_int_noprefix(table_data, ~0x1ULL, 8);
  288. /* Read Ack Write */
  289. build_append_int_noprefix(table_data, 0x1, 8);
  290. }
  291. /* Build Hardware Error Source Table */
  292. void acpi_build_hest(GArray *table_data, GArray *hardware_errors,
  293. BIOSLinker *linker,
  294. const char *oem_id, const char *oem_table_id)
  295. {
  296. AcpiTable table = { .sig = "HEST", .rev = 1,
  297. .oem_id = oem_id, .oem_table_id = oem_table_id };
  298. build_ghes_error_table(hardware_errors, linker);
  299. acpi_table_begin(&table, table_data);
  300. /* Error Source Count */
  301. build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
  302. build_ghes_v2(table_data, linker,
  303. ACPI_GHES_NOTIFY_SEA, ACPI_HEST_SRC_ID_SEA);
  304. acpi_table_end(linker, &table);
  305. }
  306. void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
  307. GArray *hardware_error)
  308. {
  309. /* Create a read-only fw_cfg file for GHES */
  310. fw_cfg_add_file(s, ACPI_HW_ERROR_FW_CFG_FILE, hardware_error->data,
  311. hardware_error->len);
  312. /* Create a read-write fw_cfg file for Address */
  313. fw_cfg_add_file_callback(s, ACPI_HW_ERROR_ADDR_FW_CFG_FILE, NULL, NULL,
  314. NULL, &(ags->hw_error_le), sizeof(ags->hw_error_le), false);
  315. ags->present = true;
  316. }
  317. static void get_hw_error_offsets(uint64_t ghes_addr,
  318. uint64_t *cper_addr,
  319. uint64_t *read_ack_register_addr)
  320. {
  321. if (!ghes_addr) {
  322. return;
  323. }
  324. /*
  325. * non-HEST version supports only one source, so no need to change
  326. * the start offset based on the source ID. Also, we can't validate
  327. * the source ID, as it is stored inside the HEST table.
  328. */
  329. cpu_physical_memory_read(ghes_addr, cper_addr,
  330. sizeof(*cper_addr));
  331. *cper_addr = le64_to_cpu(*cper_addr);
  332. /*
  333. * As the current version supports only one source, the ack offset is
  334. * just sizeof(uint64_t).
  335. */
  336. *read_ack_register_addr = ghes_addr + sizeof(uint64_t);
  337. }
  338. static void ghes_record_cper_errors(const void *cper, size_t len,
  339. uint16_t source_id, Error **errp)
  340. {
  341. uint64_t cper_addr = 0, read_ack_register_addr = 0, read_ack_register;
  342. AcpiGedState *acpi_ged_state;
  343. AcpiGhesState *ags;
  344. if (len > ACPI_GHES_MAX_RAW_DATA_LENGTH) {
  345. error_setg(errp, "GHES CPER record is too big: %zd", len);
  346. return;
  347. }
  348. acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
  349. NULL));
  350. if (!acpi_ged_state) {
  351. error_setg(errp, "Can't find ACPI_GED object");
  352. return;
  353. }
  354. ags = &acpi_ged_state->ghes_state;
  355. assert(ACPI_GHES_ERROR_SOURCE_COUNT == 1);
  356. get_hw_error_offsets(le64_to_cpu(ags->hw_error_le),
  357. &cper_addr, &read_ack_register_addr);
  358. if (!cper_addr) {
  359. error_setg(errp, "can not find Generic Error Status Block");
  360. return;
  361. }
  362. cpu_physical_memory_read(read_ack_register_addr,
  363. &read_ack_register, sizeof(read_ack_register));
  364. /* zero means OSPM does not acknowledge the error */
  365. if (!read_ack_register) {
  366. error_setg(errp,
  367. "OSPM does not acknowledge previous error,"
  368. " so can not record CPER for current error anymore");
  369. return;
  370. }
  371. read_ack_register = cpu_to_le64(0);
  372. /*
  373. * Clear the Read Ack Register, OSPM will write 1 to this register when
  374. * it acknowledges the error.
  375. */
  376. cpu_physical_memory_write(read_ack_register_addr,
  377. &read_ack_register, sizeof(uint64_t));
  378. /* Write the generic error data entry into guest memory */
  379. cpu_physical_memory_write(cper_addr, cper, len);
  380. }
  381. int acpi_ghes_memory_errors(uint16_t source_id, uint64_t physical_address)
  382. {
  383. /* Memory Error Section Type */
  384. const uint8_t guid[] =
  385. UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
  386. 0xED, 0x7C, 0x83, 0xB1);
  387. Error *errp = NULL;
  388. int data_length;
  389. GArray *block;
  390. block = g_array_new(false, true /* clear */, 1);
  391. data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
  392. /*
  393. * It should not run out of the preallocated memory if adding a new generic
  394. * error data entry
  395. */
  396. assert((data_length + ACPI_GHES_GESB_SIZE) <=
  397. ACPI_GHES_MAX_RAW_DATA_LENGTH);
  398. ghes_gen_err_data_uncorrectable_recoverable(block, guid, data_length);
  399. /* Build the memory section CPER for above new generic error data entry */
  400. acpi_ghes_build_append_mem_cper(block, physical_address);
  401. /* Report the error */
  402. ghes_record_cper_errors(block->data, block->len, source_id, &errp);
  403. g_array_free(block, true);
  404. if (errp) {
  405. error_report_err(errp);
  406. return -1;
  407. }
  408. return 0;
  409. }
  410. bool acpi_ghes_present(void)
  411. {
  412. AcpiGedState *acpi_ged_state;
  413. AcpiGhesState *ags;
  414. acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
  415. NULL));
  416. if (!acpi_ged_state) {
  417. return false;
  418. }
  419. ags = &acpi_ged_state->ghes_state;
  420. return ags->present;
  421. }