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