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