ghes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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_GHES_ERRORS_FW_CFG_FILE "etc/hardware_errors"
  28. #define ACPI_GHES_DATA_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 int acpi_ghes_record_mem_error(uint64_t error_block_address,
  162. uint64_t error_physical_addr)
  163. {
  164. GArray *block;
  165. /* Memory Error Section Type */
  166. const uint8_t uefi_cper_mem_sec[] =
  167. UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
  168. 0xED, 0x7C, 0x83, 0xB1);
  169. /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
  170. * Table 17-13 Generic Error Data Entry
  171. */
  172. QemuUUID fru_id = {};
  173. uint32_t data_length;
  174. block = g_array_new(false, true /* clear */, 1);
  175. /* This is the length if adding a new generic error data entry*/
  176. data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
  177. /*
  178. * It should not run out of the preallocated memory if adding a new generic
  179. * error data entry
  180. */
  181. assert((data_length + ACPI_GHES_GESB_SIZE) <=
  182. ACPI_GHES_MAX_RAW_DATA_LENGTH);
  183. /* Build the new generic error status block header */
  184. acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
  185. 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
  186. /* Build this new generic error data entry header */
  187. acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
  188. ACPI_CPER_SEV_RECOVERABLE, 0, 0,
  189. ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
  190. /* Build the memory section CPER for above new generic error data entry */
  191. acpi_ghes_build_append_mem_cper(block, error_physical_addr);
  192. /* Write the generic error data entry into guest memory */
  193. cpu_physical_memory_write(error_block_address, block->data, block->len);
  194. g_array_free(block, true);
  195. return 0;
  196. }
  197. /*
  198. * Build table for the hardware error fw_cfg blob.
  199. * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
  200. * See docs/specs/acpi_hest_ghes.rst for blobs format.
  201. */
  202. static void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
  203. {
  204. int i, error_status_block_offset;
  205. /* Build error_block_address */
  206. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  207. build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
  208. }
  209. /* Build read_ack_register */
  210. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  211. /*
  212. * Initialize the value of read_ack_register to 1, so GHES can be
  213. * writable after (re)boot.
  214. * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
  215. * (GHESv2 - Type 10)
  216. */
  217. build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
  218. }
  219. /* Generic Error Status Block offset in the hardware error fw_cfg blob */
  220. error_status_block_offset = hardware_errors->len;
  221. /* Reserve space for Error Status Data Block */
  222. acpi_data_push(hardware_errors,
  223. ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
  224. /* Tell guest firmware to place hardware_errors blob into RAM */
  225. bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
  226. hardware_errors, sizeof(uint64_t), false);
  227. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  228. /*
  229. * Tell firmware to patch error_block_address entries to point to
  230. * corresponding "Generic Error Status Block"
  231. */
  232. bios_linker_loader_add_pointer(linker,
  233. ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
  234. sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
  235. error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
  236. }
  237. /*
  238. * tell firmware to write hardware_errors GPA into
  239. * hardware_errors_addr fw_cfg, once the former has been initialized.
  240. */
  241. bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
  242. 0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
  243. }
  244. /* Build Generic Hardware Error Source version 2 (GHESv2) */
  245. static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
  246. {
  247. uint64_t address_offset;
  248. /*
  249. * Type:
  250. * Generic Hardware Error Source version 2(GHESv2 - Type 10)
  251. */
  252. build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
  253. /* Source Id */
  254. build_append_int_noprefix(table_data, source_id, 2);
  255. /* Related Source Id */
  256. build_append_int_noprefix(table_data, 0xffff, 2);
  257. /* Flags */
  258. build_append_int_noprefix(table_data, 0, 1);
  259. /* Enabled */
  260. build_append_int_noprefix(table_data, 1, 1);
  261. /* Number of Records To Pre-allocate */
  262. build_append_int_noprefix(table_data, 1, 4);
  263. /* Max Sections Per Record */
  264. build_append_int_noprefix(table_data, 1, 4);
  265. /* Max Raw Data Length */
  266. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  267. address_offset = table_data->len;
  268. /* Error Status Address */
  269. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  270. 4 /* QWord access */, 0);
  271. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  272. address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
  273. ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
  274. switch (source_id) {
  275. case ACPI_HEST_SRC_ID_SEA:
  276. /*
  277. * Notification Structure
  278. * Now only enable ARMv8 SEA notification type
  279. */
  280. build_ghes_hw_error_notification(table_data, ACPI_GHES_NOTIFY_SEA);
  281. break;
  282. default:
  283. error_report("Not support this error source");
  284. abort();
  285. }
  286. /* Error Status Block Length */
  287. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  288. /*
  289. * Read Ack Register
  290. * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
  291. * version 2 (GHESv2 - Type 10)
  292. */
  293. address_offset = table_data->len;
  294. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  295. 4 /* QWord access */, 0);
  296. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  297. address_offset + GAS_ADDR_OFFSET,
  298. sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
  299. (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
  300. /*
  301. * Read Ack Preserve field
  302. * We only provide the first bit in Read Ack Register to OSPM to write
  303. * while the other bits are preserved.
  304. */
  305. build_append_int_noprefix(table_data, ~0x1ULL, 8);
  306. /* Read Ack Write */
  307. build_append_int_noprefix(table_data, 0x1, 8);
  308. }
  309. /* Build Hardware Error Source Table */
  310. void acpi_build_hest(GArray *table_data, GArray *hardware_errors,
  311. BIOSLinker *linker,
  312. const char *oem_id, const char *oem_table_id)
  313. {
  314. AcpiTable table = { .sig = "HEST", .rev = 1,
  315. .oem_id = oem_id, .oem_table_id = oem_table_id };
  316. build_ghes_error_table(hardware_errors, linker);
  317. acpi_table_begin(&table, table_data);
  318. /* Error Source Count */
  319. build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
  320. build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
  321. acpi_table_end(linker, &table);
  322. }
  323. void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
  324. GArray *hardware_error)
  325. {
  326. /* Create a read-only fw_cfg file for GHES */
  327. fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
  328. hardware_error->len);
  329. /* Create a read-write fw_cfg file for Address */
  330. fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
  331. NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
  332. ags->present = true;
  333. }
  334. int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
  335. {
  336. uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
  337. uint64_t start_addr;
  338. bool ret = -1;
  339. AcpiGedState *acpi_ged_state;
  340. AcpiGhesState *ags;
  341. assert(source_id < ACPI_GHES_ERROR_SOURCE_COUNT);
  342. acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
  343. NULL));
  344. g_assert(acpi_ged_state);
  345. ags = &acpi_ged_state->ghes_state;
  346. start_addr = le64_to_cpu(ags->ghes_addr_le);
  347. if (!physical_address) {
  348. return -1;
  349. }
  350. if (source_id < ACPI_GHES_ERROR_SOURCE_COUNT) {
  351. start_addr += source_id * sizeof(uint64_t);
  352. }
  353. cpu_physical_memory_read(start_addr, &error_block_addr,
  354. sizeof(error_block_addr));
  355. error_block_addr = le64_to_cpu(error_block_addr);
  356. read_ack_register_addr = start_addr +
  357. ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
  358. cpu_physical_memory_read(read_ack_register_addr,
  359. &read_ack_register, sizeof(read_ack_register));
  360. /* zero means OSPM does not acknowledge the error */
  361. if (!read_ack_register) {
  362. error_report("OSPM does not acknowledge previous error,"
  363. " so can not record CPER for current error anymore");
  364. } else if (error_block_addr) {
  365. read_ack_register = cpu_to_le64(0);
  366. /*
  367. * Clear the Read Ack Register, OSPM will write it to 1 when
  368. * it acknowledges this error.
  369. */
  370. cpu_physical_memory_write(read_ack_register_addr,
  371. &read_ack_register, sizeof(uint64_t));
  372. ret = acpi_ghes_record_mem_error(error_block_addr,
  373. physical_address);
  374. } else {
  375. error_report("can not find Generic Error Status Block");
  376. }
  377. return ret;
  378. }
  379. bool acpi_ghes_present(void)
  380. {
  381. AcpiGedState *acpi_ged_state;
  382. AcpiGhesState *ags;
  383. acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
  384. NULL));
  385. if (!acpi_ged_state) {
  386. return false;
  387. }
  388. ags = &acpi_ged_state->ghes_state;
  389. return ags->present;
  390. }