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 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #include "qemu/osdep.h" 23 #include "qemu/units.h" 24 #include "hw/acpi/ghes.h" 25 #include "hw/acpi/aml-build.h" 26 #include "qemu/error-report.h" 27 #include "hw/acpi/generic_event_device.h" 28 #include "hw/nvram/fw_cfg.h" 29 #include "qemu/uuid.h" 30 31 #define ACPI_HW_ERROR_FW_CFG_FILE "etc/hardware_errors" 32 #define ACPI_HW_ERROR_ADDR_FW_CFG_FILE "etc/hardware_errors_addr" 33 #define ACPI_HEST_ADDR_FW_CFG_FILE "etc/acpi_table_hest_addr" 34 35 /* The max size in bytes for one error block */ 36 #define ACPI_GHES_MAX_RAW_DATA_LENGTH (1 * KiB) 37 38 /* Generic Hardware Error Source version 2 */ 39 #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2 10 40 41 /* Address offset in Generic Address Structure(GAS) */ 42 #define GAS_ADDR_OFFSET 4 43 44 /* 45 * ACPI spec 1.0b 46 * 5.2.3 System Description Table Header 47 */ 48 #define ACPI_DESC_HEADER_OFFSET 36 49 50 /* 51 * The total size of Generic Error Data Entry 52 * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data, 53 * Table 18-343 Generic Error Data Entry 54 */ 55 #define ACPI_GHES_DATA_LENGTH 72 56 57 /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */ 58 #define ACPI_GHES_MEM_CPER_LENGTH 80 59 60 /* Masks for block_status flags */ 61 #define ACPI_GEBS_UNCORRECTABLE 1 62 63 /* 64 * Total size for Generic Error Status Block except Generic Error Data Entries 65 * ACPI 6.2: 18.3.2.7.1 Generic Error Data, 66 * Table 18-380 Generic Error Status Block 67 */ 68 #define ACPI_GHES_GESB_SIZE 20 69 70 /* 71 * See the memory layout map at docs/specs/acpi_hest_ghes.rst. 72 */ 73 74 /* 75 * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source version 2 76 * Table 18-344 Generic Hardware Error Source version 2 (GHESv2) Structure 77 */ 78 #define HEST_GHES_V2_ENTRY_SIZE 92 79 80 /* 81 * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source version 2 82 * Table 18-344 Generic Hardware Error Source version 2 (GHESv2) Structure 83 * Read Ack Register 84 */ 85 #define GHES_READ_ACK_ADDR_OFF 64 86 87 /* 88 * ACPI 6.1: 18.3.2.7: Generic Hardware Error Source 89 * Table 18-341 Generic Hardware Error Source Structure 90 * Error Status Address 91 */ 92 #define GHES_ERR_STATUS_ADDR_OFF 20 93 94 /* 95 * Values for error_severity field 96 */ 97 enum AcpiGenericErrorSeverity { 98 ACPI_CPER_SEV_RECOVERABLE = 0, 99 ACPI_CPER_SEV_FATAL = 1, 100 ACPI_CPER_SEV_CORRECTED = 2, 101 ACPI_CPER_SEV_NONE = 3, 102 }; 103 104 /* 105 * Hardware Error Notification 106 * ACPI 4.0: 17.3.2.7 Hardware Error Notification 107 * Composes dummy Hardware Error Notification descriptor of specified type 108 */ 109 static void build_ghes_hw_error_notification(GArray *table, const uint8_t type) 110 { 111 /* Type */ 112 build_append_int_noprefix(table, type, 1); 113 /* 114 * Length: 115 * Total length of the structure in bytes 116 */ 117 build_append_int_noprefix(table, 28, 1); 118 /* Configuration Write Enable */ 119 build_append_int_noprefix(table, 0, 2); 120 /* Poll Interval */ 121 build_append_int_noprefix(table, 0, 4); 122 /* Vector */ 123 build_append_int_noprefix(table, 0, 4); 124 /* Switch To Polling Threshold Value */ 125 build_append_int_noprefix(table, 0, 4); 126 /* Switch To Polling Threshold Window */ 127 build_append_int_noprefix(table, 0, 4); 128 /* Error Threshold Value */ 129 build_append_int_noprefix(table, 0, 4); 130 /* Error Threshold Window */ 131 build_append_int_noprefix(table, 0, 4); 132 } 133 134 /* 135 * Generic Error Data Entry 136 * ACPI 6.1: 18.3.2.7.1 Generic Error Data 137 */ 138 static void acpi_ghes_generic_error_data(GArray *table, 139 const uint8_t *section_type, uint32_t error_severity, 140 uint8_t validation_bits, uint8_t flags, 141 uint32_t error_data_length, QemuUUID fru_id, 142 uint64_t time_stamp) 143 { 144 const uint8_t fru_text[20] = {0}; 145 146 /* Section Type */ 147 g_array_append_vals(table, section_type, 16); 148 149 /* Error Severity */ 150 build_append_int_noprefix(table, error_severity, 4); 151 /* Revision */ 152 build_append_int_noprefix(table, 0x300, 2); 153 /* Validation Bits */ 154 build_append_int_noprefix(table, validation_bits, 1); 155 /* Flags */ 156 build_append_int_noprefix(table, flags, 1); 157 /* Error Data Length */ 158 build_append_int_noprefix(table, error_data_length, 4); 159 160 /* FRU Id */ 161 g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data)); 162 163 /* FRU Text */ 164 g_array_append_vals(table, fru_text, sizeof(fru_text)); 165 166 /* Timestamp */ 167 build_append_int_noprefix(table, time_stamp, 8); 168 } 169 170 /* 171 * Generic Error Status Block 172 * ACPI 6.1: 18.3.2.7.1 Generic Error Data 173 */ 174 static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status, 175 uint32_t raw_data_offset, uint32_t raw_data_length, 176 uint32_t data_length, uint32_t error_severity) 177 { 178 /* Block Status */ 179 build_append_int_noprefix(table, block_status, 4); 180 /* Raw Data Offset */ 181 build_append_int_noprefix(table, raw_data_offset, 4); 182 /* Raw Data Length */ 183 build_append_int_noprefix(table, raw_data_length, 4); 184 /* Data Length */ 185 build_append_int_noprefix(table, data_length, 4); 186 /* Error Severity */ 187 build_append_int_noprefix(table, error_severity, 4); 188 } 189 190 /* UEFI 2.6: N.2.5 Memory Error Section */ 191 static void acpi_ghes_build_append_mem_cper(GArray *table, 192 uint64_t error_physical_addr) 193 { 194 /* 195 * Memory Error Record 196 */ 197 198 /* Validation Bits */ 199 build_append_int_noprefix(table, 200 (1ULL << 14) | /* Type Valid */ 201 (1ULL << 1) /* Physical Address Valid */, 202 8); 203 /* Error Status */ 204 build_append_int_noprefix(table, 0, 8); 205 /* Physical Address */ 206 build_append_int_noprefix(table, error_physical_addr, 8); 207 /* Skip all the detailed information normally found in such a record */ 208 build_append_int_noprefix(table, 0, 48); 209 /* Memory Error Type */ 210 build_append_int_noprefix(table, 0 /* Unknown error */, 1); 211 /* Skip all the detailed information normally found in such a record */ 212 build_append_int_noprefix(table, 0, 7); 213 } 214 215 static void 216 ghes_gen_err_data_uncorrectable_recoverable(GArray *block, 217 const uint8_t *section_type, 218 int data_length) 219 { 220 /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data, 221 * Table 17-13 Generic Error Data Entry 222 */ 223 QemuUUID fru_id = {}; 224 225 /* Build the new generic error status block header */ 226 acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE, 227 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE); 228 229 /* Build this new generic error data entry header */ 230 acpi_ghes_generic_error_data(block, section_type, 231 ACPI_CPER_SEV_RECOVERABLE, 0, 0, 232 ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0); 233 } 234 235 /* 236 * Build table for the hardware error fw_cfg blob. 237 * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs. 238 * See docs/specs/acpi_hest_ghes.rst for blobs format. 239 */ 240 static void build_ghes_error_table(AcpiGhesState *ags, GArray *hardware_errors, 241 BIOSLinker *linker, int num_sources) 242 { 243 int i, error_status_block_offset; 244 245 /* Build error_block_address */ 246 for (i = 0; i < num_sources; i++) { 247 build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t)); 248 } 249 250 /* Build read_ack_register */ 251 for (i = 0; i < num_sources; i++) { 252 /* 253 * Initialize the value of read_ack_register to 1, so GHES can be 254 * writable after (re)boot. 255 * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2 256 * (GHESv2 - Type 10) 257 */ 258 build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t)); 259 } 260 261 /* Generic Error Status Block offset in the hardware error fw_cfg blob */ 262 error_status_block_offset = hardware_errors->len; 263 264 /* Reserve space for Error Status Data Block */ 265 acpi_data_push(hardware_errors, 266 ACPI_GHES_MAX_RAW_DATA_LENGTH * num_sources); 267 268 /* Tell guest firmware to place hardware_errors blob into RAM */ 269 bios_linker_loader_alloc(linker, ACPI_HW_ERROR_FW_CFG_FILE, 270 hardware_errors, sizeof(uint64_t), false); 271 272 for (i = 0; i < num_sources; i++) { 273 /* 274 * Tell firmware to patch error_block_address entries to point to 275 * corresponding "Generic Error Status Block" 276 */ 277 bios_linker_loader_add_pointer(linker, 278 ACPI_HW_ERROR_FW_CFG_FILE, 279 sizeof(uint64_t) * i, 280 sizeof(uint64_t), 281 ACPI_HW_ERROR_FW_CFG_FILE, 282 error_status_block_offset + 283 i * ACPI_GHES_MAX_RAW_DATA_LENGTH); 284 } 285 286 if (!ags->use_hest_addr) { 287 /* 288 * Tell firmware to write hardware_errors GPA into 289 * hardware_errors_addr fw_cfg, once the former has been initialized. 290 */ 291 bios_linker_loader_write_pointer(linker, ACPI_HW_ERROR_ADDR_FW_CFG_FILE, 292 0, sizeof(uint64_t), 293 ACPI_HW_ERROR_FW_CFG_FILE, 0); 294 } 295 } 296 297 /* Build Generic Hardware Error Source version 2 (GHESv2) */ 298 static void build_ghes_v2_entry(GArray *table_data, 299 BIOSLinker *linker, 300 const AcpiNotificationSourceId *notif_src, 301 uint16_t index, int num_sources) 302 { 303 uint64_t address_offset; 304 const uint16_t notify = notif_src->notify; 305 const uint16_t source_id = notif_src->source_id; 306 307 /* 308 * Type: 309 * Generic Hardware Error Source version 2(GHESv2 - Type 10) 310 */ 311 build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2); 312 /* Source Id */ 313 build_append_int_noprefix(table_data, source_id, 2); 314 /* Related Source Id */ 315 build_append_int_noprefix(table_data, 0xffff, 2); 316 /* Flags */ 317 build_append_int_noprefix(table_data, 0, 1); 318 /* Enabled */ 319 build_append_int_noprefix(table_data, 1, 1); 320 321 /* Number of Records To Pre-allocate */ 322 build_append_int_noprefix(table_data, 1, 4); 323 /* Max Sections Per Record */ 324 build_append_int_noprefix(table_data, 1, 4); 325 /* Max Raw Data Length */ 326 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4); 327 328 address_offset = table_data->len; 329 /* Error Status Address */ 330 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0, 331 4 /* QWord access */, 0); 332 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, 333 address_offset + GAS_ADDR_OFFSET, 334 sizeof(uint64_t), 335 ACPI_HW_ERROR_FW_CFG_FILE, 336 index * sizeof(uint64_t)); 337 338 /* Notification Structure */ 339 build_ghes_hw_error_notification(table_data, notify); 340 341 /* Error Status Block Length */ 342 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4); 343 344 /* 345 * Read Ack Register 346 * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source 347 * version 2 (GHESv2 - Type 10) 348 */ 349 address_offset = table_data->len; 350 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0, 351 4 /* QWord access */, 0); 352 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, 353 address_offset + GAS_ADDR_OFFSET, 354 sizeof(uint64_t), 355 ACPI_HW_ERROR_FW_CFG_FILE, 356 (num_sources + index) * sizeof(uint64_t)); 357 358 /* 359 * Read Ack Preserve field 360 * We only provide the first bit in Read Ack Register to OSPM to write 361 * while the other bits are preserved. 362 */ 363 build_append_int_noprefix(table_data, ~0x1ULL, 8); 364 /* Read Ack Write */ 365 build_append_int_noprefix(table_data, 0x1, 8); 366 } 367 368 /* Build Hardware Error Source Table */ 369 void acpi_build_hest(AcpiGhesState *ags, GArray *table_data, 370 GArray *hardware_errors, 371 BIOSLinker *linker, 372 const AcpiNotificationSourceId *notif_source, 373 int num_sources, 374 const char *oem_id, const char *oem_table_id) 375 { 376 AcpiTable table = { .sig = "HEST", .rev = 1, 377 .oem_id = oem_id, .oem_table_id = oem_table_id }; 378 uint32_t hest_offset; 379 int i; 380 381 hest_offset = table_data->len; 382 383 build_ghes_error_table(ags, hardware_errors, linker, num_sources); 384 385 acpi_table_begin(&table, table_data); 386 387 /* Error Source Count */ 388 build_append_int_noprefix(table_data, num_sources, 4); 389 for (i = 0; i < num_sources; i++) { 390 build_ghes_v2_entry(table_data, linker, ¬if_source[i], i, num_sources); 391 } 392 393 acpi_table_end(linker, &table); 394 395 if (ags->use_hest_addr) { 396 /* 397 * Tell firmware to write into GPA the address of HEST via fw_cfg, 398 * once initialized. 399 */ 400 bios_linker_loader_write_pointer(linker, 401 ACPI_HEST_ADDR_FW_CFG_FILE, 0, 402 sizeof(uint64_t), 403 ACPI_BUILD_TABLE_FILE, hest_offset); 404 } 405 } 406 407 void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s, 408 GArray *hardware_error) 409 { 410 /* Create a read-only fw_cfg file for GHES */ 411 fw_cfg_add_file(s, ACPI_HW_ERROR_FW_CFG_FILE, hardware_error->data, 412 hardware_error->len); 413 414 if (ags->use_hest_addr) { 415 fw_cfg_add_file_callback(s, ACPI_HEST_ADDR_FW_CFG_FILE, NULL, NULL, 416 NULL, &(ags->hest_addr_le), sizeof(ags->hest_addr_le), false); 417 } else { 418 /* Create a read-write fw_cfg file for Address */ 419 fw_cfg_add_file_callback(s, ACPI_HW_ERROR_ADDR_FW_CFG_FILE, NULL, NULL, 420 NULL, &(ags->hw_error_le), sizeof(ags->hw_error_le), false); 421 } 422 } 423 424 static void get_hw_error_offsets(uint64_t ghes_addr, 425 uint64_t *cper_addr, 426 uint64_t *read_ack_register_addr) 427 { 428 /* 429 * non-HEST version supports only one source, so no need to change 430 * the start offset based on the source ID. Also, we can't validate 431 * the source ID, as it is stored inside the HEST table. 432 */ 433 434 cpu_physical_memory_read(ghes_addr, cper_addr, 435 sizeof(*cper_addr)); 436 437 *cper_addr = le64_to_cpu(*cper_addr); 438 439 /* 440 * As the current version supports only one source, the ack offset is 441 * just sizeof(uint64_t). 442 */ 443 *read_ack_register_addr = ghes_addr + sizeof(uint64_t); 444 } 445 446 static void get_ghes_source_offsets(uint16_t source_id, 447 uint64_t hest_addr, 448 uint64_t *cper_addr, 449 uint64_t *read_ack_start_addr, 450 Error **errp) 451 { 452 uint64_t hest_err_block_addr, hest_read_ack_addr; 453 uint64_t err_source_entry, error_block_addr; 454 uint32_t num_sources, i; 455 456 hest_addr += ACPI_DESC_HEADER_OFFSET; 457 458 cpu_physical_memory_read(hest_addr, &num_sources, 459 sizeof(num_sources)); 460 num_sources = le32_to_cpu(num_sources); 461 462 err_source_entry = hest_addr + sizeof(num_sources); 463 464 /* 465 * Currently, HEST Error source navigates only for GHESv2 tables 466 */ 467 for (i = 0; i < num_sources; i++) { 468 uint64_t addr = err_source_entry; 469 uint16_t type, src_id; 470 471 cpu_physical_memory_read(addr, &type, sizeof(type)); 472 type = le16_to_cpu(type); 473 474 /* For now, we only know the size of GHESv2 table */ 475 if (type != ACPI_GHES_SOURCE_GENERIC_ERROR_V2) { 476 error_setg(errp, "HEST: type %d not supported.", type); 477 return; 478 } 479 480 /* Compare CPER source ID at the GHESv2 structure */ 481 addr += sizeof(type); 482 cpu_physical_memory_read(addr, &src_id, sizeof(src_id)); 483 if (le16_to_cpu(src_id) == source_id) { 484 break; 485 } 486 487 err_source_entry += HEST_GHES_V2_ENTRY_SIZE; 488 } 489 if (i == num_sources) { 490 error_setg(errp, "HEST: Source %d not found.", source_id); 491 return; 492 } 493 494 /* Navigate through table address pointers */ 495 hest_err_block_addr = err_source_entry + GHES_ERR_STATUS_ADDR_OFF + 496 GAS_ADDR_OFFSET; 497 498 cpu_physical_memory_read(hest_err_block_addr, &error_block_addr, 499 sizeof(error_block_addr)); 500 error_block_addr = le64_to_cpu(error_block_addr); 501 502 cpu_physical_memory_read(error_block_addr, cper_addr, 503 sizeof(*cper_addr)); 504 *cper_addr = le64_to_cpu(*cper_addr); 505 506 hest_read_ack_addr = err_source_entry + GHES_READ_ACK_ADDR_OFF + 507 GAS_ADDR_OFFSET; 508 cpu_physical_memory_read(hest_read_ack_addr, read_ack_start_addr, 509 sizeof(*read_ack_start_addr)); 510 *read_ack_start_addr = le64_to_cpu(*read_ack_start_addr); 511 } 512 513 NotifierList acpi_generic_error_notifiers = 514 NOTIFIER_LIST_INITIALIZER(acpi_generic_error_notifiers); 515 516 void ghes_record_cper_errors(AcpiGhesState *ags, const void *cper, size_t len, 517 uint16_t source_id, Error **errp) 518 { 519 uint64_t cper_addr = 0, read_ack_register_addr = 0, read_ack_register; 520 521 if (len > ACPI_GHES_MAX_RAW_DATA_LENGTH) { 522 error_setg(errp, "GHES CPER record is too big: %zd", len); 523 return; 524 } 525 526 if (!ags->use_hest_addr) { 527 get_hw_error_offsets(le64_to_cpu(ags->hw_error_le), 528 &cper_addr, &read_ack_register_addr); 529 } else { 530 get_ghes_source_offsets(source_id, le64_to_cpu(ags->hest_addr_le), 531 &cper_addr, &read_ack_register_addr, errp); 532 } 533 534 cpu_physical_memory_read(read_ack_register_addr, 535 &read_ack_register, sizeof(read_ack_register)); 536 537 /* zero means OSPM does not acknowledge the error */ 538 if (!read_ack_register) { 539 error_setg(errp, 540 "OSPM does not acknowledge previous error," 541 " so can not record CPER for current error anymore"); 542 return; 543 } 544 545 read_ack_register = cpu_to_le64(0); 546 /* 547 * Clear the Read Ack Register, OSPM will write 1 to this register when 548 * it acknowledges the error. 549 */ 550 cpu_physical_memory_write(read_ack_register_addr, 551 &read_ack_register, sizeof(uint64_t)); 552 553 /* Write the generic error data entry into guest memory */ 554 cpu_physical_memory_write(cper_addr, cper, len); 555 556 notifier_list_notify(&acpi_generic_error_notifiers, &source_id); 557 } 558 559 int acpi_ghes_memory_errors(AcpiGhesState *ags, uint16_t source_id, 560 uint64_t physical_address) 561 { 562 /* Memory Error Section Type */ 563 const uint8_t guid[] = 564 UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \ 565 0xED, 0x7C, 0x83, 0xB1); 566 Error *errp = NULL; 567 int data_length; 568 GArray *block; 569 570 block = g_array_new(false, true /* clear */, 1); 571 572 data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH; 573 /* 574 * It should not run out of the preallocated memory if adding a new generic 575 * error data entry 576 */ 577 assert((data_length + ACPI_GHES_GESB_SIZE) <= 578 ACPI_GHES_MAX_RAW_DATA_LENGTH); 579 580 ghes_gen_err_data_uncorrectable_recoverable(block, guid, data_length); 581 582 /* Build the memory section CPER for above new generic error data entry */ 583 acpi_ghes_build_append_mem_cper(block, physical_address); 584 585 /* Report the error */ 586 ghes_record_cper_errors(ags, block->data, block->len, source_id, &errp); 587 588 g_array_free(block, true); 589 590 if (errp) { 591 error_report_err(errp); 592 return -1; 593 } 594 595 return 0; 596 } 597 598 AcpiGhesState *acpi_ghes_get_state(void) 599 { 600 AcpiGedState *acpi_ged_state; 601 AcpiGhesState *ags; 602 603 acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED, 604 NULL)); 605 606 if (!acpi_ged_state) { 607 return NULL; 608 } 609 ags = &acpi_ged_state->ghes_state; 610 611 if (!ags->hw_error_le && !ags->hest_addr_le) { 612 return NULL; 613 } 614 return ags; 615 } 616