qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU
@ 2019-05-14 11:18 Dongjiu Geng
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration Dongjiu Geng
                   ` (10 more replies)
  0 siblings, 11 replies; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

In the ARMv8 platform, the CPU error type are synchronous external
abort(SEA) and SError Interrupt (SEI). If exception happens to guest,
sometimes guest itself do the recovery is better, because host 
does not know guest's detailed information. For example, if a guest
user-space application happen exception, host does not which application
encounter errors.

For the ARMv8 SEA/SEI, KVM or host kernel delivers SIGBUS to notify user
space. After user space gets  the notification, it will record the CPER
to guest GHES buffer for guest and inject a exception or IRQ to guest.

In the current implement, if the SIGBUS is BUS_MCEERR_AR, we will
treat it as synchronous exception, and use ARMv8 SEA notification type
to notify guest after recording CPER for guest;

This series patches are based on Qemu 4.0, which have two parts:
1. Generate APEI/GHES table.
2. Handle the SIGBUS signal, record the CPER in runtime and fill into guest memory,
   then according to SIGBUS type to notify guest.

Whole solution was suggested by James(james.morse@arm.com); APEI part solution is suggested by
Laszlo(lersek@redhat.com). Shown some discussion in [1].


This series patches have already tested on ARM64 platform with RAS feature enabled:
Show the APEI part verification result in [2]
Show the BUS_MCEERR_AR SIGBUS handling verification result in [3]

---
change since v16:
1. check whether ACPI table is enabled when handling the memory error in the SIGBUS handler.

Change since v15:
1. Add a doc-comment in the proper format for 'include/exec/ram_addr.h'
2. Remove write_part_cpustate_to_list() because there is another bug fix patch
   has been merged "arm: Allow system registers for KVM guests to be changed by QEMU code"
3. Add some comments for kvm_inject_arm_sea() in 'target/arm/kvm64.c'
4. Compare the arm_current_el() return value to 0,1,2,3, not to PSTATE_MODE_* constants.
5. Change the RAS support wasn't introduced before 4.1 QEMU version.
6. Move the no_ras flag  patch to begin in this series

Change since v14:
1. Remove the BUS_MCEERR_AO handling logic because this asynchronous signal was masked by main thread 
2. Address some Igor Mammedov's comments(ACPI part)
   1) change the comments for the enum AcpiHestNotifyType definition and remove ditto in patch 1
   2) change some patch commit messages and separate "APEI GHES table generation" patch to more patches.
3. Address some peter's comments(arm64 Synchronous External Abort injection)
   1) change some code notes
   2) using arm_current_el() for current EL
   2) use the helper functions for those (syn_data_abort_*).

Change since v13:
1. Move the patches that set guest ESR and inject virtual SError out of this series
2. Clean and optimize the APEI part patches 
3. Update the commit messages and add some comments for the code

Change since v12:
1. Address Paolo's comments to move HWPoisonPage definition to accel/kvm/kvm-all.c
2. Only call kvm_cpu_synchronize_state() when get the BUS_MCEERR_AR signal
3. Only add and enable GPIO-Signal and ARMv8 SEA two hardware error sources
4. Address Michael's comments to not sync SPDX from Linux kernel header file 

Change since v11:
Address James's comments(james.morse@arm.com)
1. Check whether KVM has the capability to to set ESR instead of detecting host CPU RAS capability
2. For SIGBUS_MCEERR_AR SIGBUS, use Synchronous-External-Abort(SEA) notification type
   for SIGBUS_MCEERR_AO SIGBUS, use GPIO-Signal notification


Address Shannon's comments(for ACPI part):
1. Unify hest_ghes.c and hest_ghes.h license declaration
2. Remove unnecessary including "qmp-commands.h" in hest_ghes.c
3. Unconditionally add guest APEI table based on James's comments(james.morse@arm.com) 
4. Add a option to virt machine for migration compatibility. On new virt machine it's on
   by default while off for old ones, we enabled it since 2.12
5. Refer to the ACPI spec version which introduces Hardware Error Notification first time
6. Add ACPI_HEST_NOTIFY_RESERVED notification type

Address Igor's comments(for ACPI part):
1. Add doc patch first which will describe how it's supposed to work between QEMU/firmware/guest
   OS with expected flows.
2. Move APEI diagrams into doc/spec patch
3. Remove redundant g_malloc in ghes_record_cper()
4. Use build_append_int_noprefix() API to compose whole error status block and whole APEI table, 
   and try to get rid of most structures in patch 1, as they will be left unused after that
5. Reuse something like https://github.com/imammedo/qemu/commit/3d2fd6d13a3ea298d2ee814835495ce6241d085c
   to build GAS
6. Remove much offsetof() in the function
7. Build independent tables first and only then build dependent tables passing to it pointers
   to previously build table if necessary.
8. Redefine macro GHES_ACPI_HEST_NOTIFY_RESERVED to ACPI_HEST_ERROR_SOURCE_COUNT to avoid confusion


Address Peter Maydell's comments
1. linux-headers is done as a patch of their own created using scripts/update-linux-headers.sh run against a
   mainline kernel tree 
2. Tested whether this patchset builds OK on aarch32  
3. Abstract Hwpoison page adding code  out properly into a cpu-independent source file from target/i386/kvm.c,
   such as kvm-all.c
4. Add doc-comment formatted documentation comment for new globally-visible function prototype in a header

---
[1]:
https://lkml.org/lkml/2017/2/27/246
https://patchwork.kernel.org/patch/9633105/
https://patchwork.kernel.org/patch/9925227/

[2]:
Note: the UEFI(QEMU_EFI.fd) is needed if guest want to use ACPI table.

After guest boot up, dump the APEI table, then can see the initialized table
(1) # iasl -p ./HEST -d /sys/firmware/acpi/tables/HEST
(2) # cat HEST.dsl
    /*
     * Intel ACPI Component Architecture
     * AML/ASL+ Disassembler version 20170728 (64-bit version)
     * Copyright (c) 2000 - 2017 Intel Corporation
     *
     * Disassembly of /sys/firmware/acpi/tables/HEST, Mon Sep  5 07:59:17 2016
     *
     * ACPI Data Table [HEST]
     *
     * Format: [HexOffset DecimalOffset ByteLength]  FieldName : FieldValue
     */

    ..................................................................................
    [308h 0776   2]                Subtable Type : 000A [Generic Hardware Error Source V2]
    [30Ah 0778   2]                    Source Id : 0001
    [30Ch 0780   2]            Related Source Id : FFFF
    [30Eh 0782   1]                     Reserved : 00
    [30Fh 0783   1]                      Enabled : 01
    [310h 0784   4]       Records To Preallocate : 00000001
    [314h 0788   4]      Max Sections Per Record : 00000001
    [318h 0792   4]          Max Raw Data Length : 00001000

    [31Ch 0796  12]         Error Status Address : [Generic Address Structure]
    [31Ch 0796   1]                     Space ID : 00 [SystemMemory]
    [31Dh 0797   1]                    Bit Width : 40
    [31Eh 0798   1]                   Bit Offset : 00
    [31Fh 0799   1]         Encoded Access Width : 04 [QWord Access:64]
    [320h 0800   8]                      Address : 00000000785D0040

    [328h 0808  28]                       Notify : [Hardware Error Notification Structure]
    [328h 0808   1]                  Notify Type : 08 [SEA]
    [329h 0809   1]                Notify Length : 1C
    [32Ah 0810   2]   Configuration Write Enable : 0000
    [32Ch 0812   4]                 PollInterval : 00000000
    [330h 0816   4]                       Vector : 00000000
    [334h 0820   4]      Polling Threshold Value : 00000000
    [338h 0824   4]     Polling Threshold Window : 00000000
    [33Ch 0828   4]        Error Threshold Value : 00000000
    [340h 0832   4]       Error Threshold Window : 00000000

    [344h 0836   4]    Error Status Block Length : 00001000
    [348h 0840  12]            Read Ack Register : [Generic Address Structure]
    [348h 0840   1]                     Space ID : 00 [SystemMemory]
    [349h 0841   1]                    Bit Width : 40
    [34Ah 0842   1]                   Bit Offset : 00
    [34Bh 0843   1]         Encoded Access Width : 04 [QWord Access:64]
    [34Ch 0844   8]                      Address : 00000000785D0098

    [354h 0852   8]            Read Ack Preserve : 00000000FFFFFFFE
    [35Ch 0860   8]               Read Ack Write : 0000000000000001

    .....................................................................................

(3) After a synchronous external abort(SEA) happen, Qemu receive a SIGBUS and 
    filled the CPER into guest GHES memory.  For example, according to above table,
    the address that contains the physical address of a block of memory that holds
    the error status data for this abort is 0x00000000785D0040
(4) the address for SEA notification error source is 0x785d80b0
    (qemu) xp /1 0x00000000785D0040
    00000000785d0040: 0x785d80b0

(5) check the content of generic error status block and generic error data entry
    (qemu) xp /100x 0x785d80b0
    00000000785d80b0: 0x00000001 0x00000000 0x00000000 0x00000098
    00000000785d80c0: 0x00000000 0xa5bc1114 0x4ede6f64 0x833e63b8
    00000000785d80d0: 0xb1837ced 0x00000000 0x00000300 0x00000050
    00000000785d80e0: 0x00000000 0x00000000 0x00000000 0x00000000
    00000000785d80f0: 0x00000000 0x00000000 0x00000000 0x00000000
    00000000785d8100: 0x00000000 0x00000000 0x00000000 0x00004002
(6) check the OSPM's ACK value(for example SEA)
    /* Before OSPM acknowledges the error, check the ACK value */
    (qemu) xp /1 0x00000000785D0098
    00000000785d00f0: 0x00000000

    /* After OSPM acknowledges the error, check the ACK value, it change to 1 from 0 */
    (qemu) xp /1 0x00000000785D0098
    00000000785d00f0: 0x00000001

[3]: KVM deliver "BUS_MCEERR_AR" to Qemu, Qemu record the guest CPER and inject
    synchronous external abort to notify guest, then guest do the recovery.

[ 1552.516170] Synchronous External Abort: synchronous external abort (0x92000410) at 0x000000003751c6b4
[ 1553.074073] {1}[Hardware Error]: Hardware error from APEI Generic Hardware Error Source: 8
[ 1553.081654] {1}[Hardware Error]: event severity: recoverable
[ 1554.034191] {1}[Hardware Error]:  Error 0, type: recoverable
[ 1554.037934] {1}[Hardware Error]:   section_type: memory error
[ 1554.513261] {1}[Hardware Error]:   physical_address: 0x0000000040fa6000
[ 1554.513944] {1}[Hardware Error]:   error_type: 0, unknown
[ 1555.041451] Memory failure: 0x40fa6: Killing mca-recover:1296 due to hardware memory corruption
[ 1555.373116] Memory failure: 0x40fa6: recovery action for dirty LRU page: Recovered



Dongjiu Geng (10):
  hw/arm/virt: Add RAS platform version for migration
  ACPI: add some GHES structures and macros definition
  acpi: add build_append_ghes_notify() helper for Hardware Error
    Notification
  acpi: add build_append_ghes_generic_data() helper for Generic Error
    Data Entry
  acpi: add build_append_ghes_generic_status() helper for Generic Error
    Status Block
  docs: APEI GHES generation and CPER record description
  ACPI: Add APEI GHES table generation support
  KVM: Move related hwpoison page functions to accel/kvm/ folder
  target-arm: kvm64: inject synchronous External Abort
  target-arm: kvm64: handle SIGBUS signal from kernel or KVM

 accel/kvm/kvm-all.c             |  33 ++++
 default-configs/arm-softmmu.mak |   1 +
 docs/specs/acpi_hest_ghes.txt   |  97 +++++++++++
 hw/acpi/Kconfig                 |   4 +
 hw/acpi/Makefile.objs           |   1 +
 hw/acpi/acpi_ghes.c             | 348 ++++++++++++++++++++++++++++++++++++++++
 hw/acpi/aml-build.c             |  70 ++++++++
 hw/arm/virt-acpi-build.c        |  12 ++
 hw/arm/virt.c                   |   6 +
 include/exec/ram_addr.h         |  24 +++
 include/hw/acpi/acpi-defs.h     |  52 ++++++
 include/hw/acpi/acpi_ghes.h     |  83 ++++++++++
 include/hw/acpi/aml-build.h     |  21 +++
 include/hw/arm/virt.h           |   1 +
 include/sysemu/kvm.h            |   2 +-
 target/arm/internals.h          |   5 +-
 target/arm/kvm.c                |   3 +
 target/arm/kvm64.c              |  73 +++++++++
 target/arm/op_helper.c          |   2 +-
 target/i386/kvm.c               |  34 +---
 20 files changed, 835 insertions(+), 37 deletions(-)
 create mode 100644 docs/specs/acpi_hest_ghes.txt
 create mode 100644 hw/acpi/acpi_ghes.c
 create mode 100644 include/hw/acpi/acpi_ghes.h

-- 
1.8.3.1



^ permalink raw reply	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-20 12:04   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition Dongjiu Geng
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Support this feature since version 4.1, disable it by
default in the old version.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 hw/arm/virt.c         | 6 ++++++
 include/hw/arm/virt.h | 1 +
 2 files changed, 7 insertions(+)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 5331ab7..7bdd41b 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -2043,8 +2043,14 @@ DEFINE_VIRT_MACHINE_AS_LATEST(4, 1)
 
 static void virt_machine_4_0_options(MachineClass *mc)
 {
+    VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
+
     virt_machine_4_1_options(mc);
     compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
+    /* Disable memory recovery feature for 4.0 as RAS support was
+     * introduced with 4.1.
+     */
+    vmc->no_ras = true;
 }
 DEFINE_VIRT_MACHINE(4, 0)
 
diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h
index 4240709..7f1a033 100644
--- a/include/hw/arm/virt.h
+++ b/include/hw/arm/virt.h
@@ -104,6 +104,7 @@ typedef struct {
     bool disallow_affinity_adjustment;
     bool no_its;
     bool no_pmu;
+    bool no_ras;
     bool claim_edge_triggered_timers;
     bool smbios_old_sys_ver;
     bool no_highmem_ecam;
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-05-29  3:40   ` Michael S. Tsirkin
  2019-06-20 12:10   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification Dongjiu Geng
                   ` (8 subsequent siblings)
  10 siblings, 2 replies; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Add Generic Error Status Block structures and some macros
definitions, which is referred to the ACPI 4.0 or ACPI 6.2. The
HEST table generation and CPER record will use them.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 include/hw/acpi/acpi-defs.h | 52 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
index f9aa4bd..d1996fb 100644
--- a/include/hw/acpi/acpi-defs.h
+++ b/include/hw/acpi/acpi-defs.h
@@ -224,6 +224,25 @@ typedef struct AcpiMultipleApicTable AcpiMultipleApicTable;
 #define ACPI_APIC_RESERVED              16   /* 16 and greater are reserved */
 
 /*
+ * Values for Hardware Error Notification Type field
+ */
+enum AcpiHestNotifyType {
+    ACPI_HEST_NOTIFY_POLLED = 0,
+    ACPI_HEST_NOTIFY_EXTERNAL = 1,
+    ACPI_HEST_NOTIFY_LOCAL = 2,
+    ACPI_HEST_NOTIFY_SCI = 3,
+    ACPI_HEST_NOTIFY_NMI = 4,
+    ACPI_HEST_NOTIFY_CMCI = 5,  /* ACPI 5.0: 18.3.2.7, Table 18-290 */
+    ACPI_HEST_NOTIFY_MCE = 6,   /* ACPI 5.0: 18.3.2.7, Table 18-290 */
+    ACPI_HEST_NOTIFY_GPIO = 7,  /* ACPI 6.0: 18.3.2.7, Table 18-332 */
+    ACPI_HEST_NOTIFY_SEA = 8,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
+    ACPI_HEST_NOTIFY_SEI = 9,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
+    ACPI_HEST_NOTIFY_GSIV = 10, /* ACPI 6.1: 18.3.2.9, Table 18-345 */
+    ACPI_HEST_NOTIFY_SDEI = 11, /* ACPI 6.2: 18.3.2.9, Table 18-383 */
+    ACPI_HEST_NOTIFY_RESERVED = 12 /* 12 and greater are reserved */
+};
+
+/*
  * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
  */
 #define ACPI_SUB_HEADER_DEF   /* Common ACPI sub-structure header */\
@@ -400,6 +419,39 @@ struct AcpiSystemResourceAffinityTable {
 } QEMU_PACKED;
 typedef struct AcpiSystemResourceAffinityTable AcpiSystemResourceAffinityTable;
 
+/*
+ * Generic Error Status Block
+ */
+struct AcpiGenericErrorStatus {
+    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
+    uint32_t block_status;
+    uint32_t raw_data_offset;
+    uint32_t raw_data_length;
+    uint32_t data_length;
+    uint32_t error_severity;
+} QEMU_PACKED;
+typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;
+
+/*
+ * Masks for block_status flags above
+ */
+#define ACPI_GEBS_UNCORRECTABLE         1
+
+/*
+ * Values for error_severity field above
+ */
+enum AcpiGenericErrorSeverity {
+    ACPI_CPER_SEV_RECOVERABLE,
+    ACPI_CPER_SEV_FATAL,
+    ACPI_CPER_SEV_CORRECTED,
+    ACPI_CPER_SEV_NONE,
+};
+
+/*
+ * Generic Hardware Error Source version 2
+ */
+#define ACPI_HEST_SOURCE_GENERIC_ERROR_V2    10
+
 #define ACPI_SRAT_PROCESSOR_APIC     0
 #define ACPI_SRAT_MEMORY             1
 #define ACPI_SRAT_PROCESSOR_x2APIC   2
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration Dongjiu Geng
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-24 11:21   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry Dongjiu Geng
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

It will help to add Hardware Error Notification to ACPI tables
without using packed C structures and avoid endianness
issues as API doesn't need explicit conversion.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 hw/acpi/aml-build.c         | 22 ++++++++++++++++++++++
 include/hw/acpi/aml-build.h |  8 ++++++++
 2 files changed, 30 insertions(+)

diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index 555c24f..fb53f21 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -274,6 +274,28 @@ void build_append_gas(GArray *table, AmlAddressSpace as,
     build_append_int_noprefix(table, address, 8);
 }
 
+/* Hardware Error Notification
+ * ACPI 4.0: 17.3.2.7 Hardware Error Notification
+ */
+void build_append_ghes_notify(GArray *table, const uint8_t type,
+                              uint8_t length, uint16_t config_write_enable,
+                              uint32_t poll_interval, uint32_t vector,
+                              uint32_t polling_threshold_value,
+                              uint32_t polling_threshold_window,
+                              uint32_t error_threshold_value,
+                              uint32_t error_threshold_window)
+{
+        build_append_int_noprefix(table, type, 1); /* type */
+        build_append_int_noprefix(table, length, 1);
+        build_append_int_noprefix(table, config_write_enable, 2);
+        build_append_int_noprefix(table, poll_interval, 4);
+        build_append_int_noprefix(table, vector, 4);
+        build_append_int_noprefix(table, polling_threshold_value, 4);
+        build_append_int_noprefix(table, polling_threshold_window, 4);
+        build_append_int_noprefix(table, error_threshold_value, 4);
+        build_append_int_noprefix(table, error_threshold_window, 4);
+}
+
 /*
  * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
  * and return the offset to 0x00000000 for runtime patching.
diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
index 1a563ad..90c8ef8 100644
--- a/include/hw/acpi/aml-build.h
+++ b/include/hw/acpi/aml-build.h
@@ -411,6 +411,14 @@ build_append_gas_from_struct(GArray *table, const struct AcpiGenericAddress *s)
                      s->access_width, s->address);
 }
 
+void build_append_ghes_notify(GArray *table, const uint8_t type,
+                              uint8_t length, uint16_t config_write_enable,
+                              uint32_t poll_interval, uint32_t vector,
+                              uint32_t polling_threshold_value,
+                              uint32_t polling_threshold_window,
+                              uint32_t error_threshold_value,
+                              uint32_t error_threshold_window);
+
 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
                        uint64_t len, int node, MemoryAffinityFlags flags);
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (2 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-20 12:28   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block Dongjiu Geng
                   ` (6 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

It will help to add Generic Error Data Entry to ACPI tables
without using packed C structures and avoid endianness
issues as API doesn't need explicit conversion.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 hw/acpi/aml-build.c         | 32 ++++++++++++++++++++++++++++++++
 include/hw/acpi/aml-build.h |  6 ++++++
 2 files changed, 38 insertions(+)

diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index fb53f21..102a288 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -296,6 +296,38 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
         build_append_int_noprefix(table, error_threshold_window, 4);
 }
 
+/* Generic Error Data Entry
+ * ACPI 4.0: 17.3.2.6.1 Generic Error Data
+ */
+void build_append_ghes_generic_data(GArray *table, const char *section_type,
+                                    uint32_t error_severity, uint16_t revision,
+                                    uint8_t validation_bits, uint8_t flags,
+                                    uint32_t error_data_length, uint8_t *fru_id,
+                                    uint8_t *fru_text, uint64_t time_stamp)
+{
+    int i;
+
+    for (i = 0; i < 16; i++) {
+        build_append_int_noprefix(table, section_type[i], 1);
+    }
+
+    build_append_int_noprefix(table, error_severity, 4);
+    build_append_int_noprefix(table, revision, 2);
+    build_append_int_noprefix(table, validation_bits, 1);
+    build_append_int_noprefix(table, flags, 1);
+    build_append_int_noprefix(table, error_data_length, 4);
+
+    for (i = 0; i < 16; i++) {
+        build_append_int_noprefix(table, fru_id[i], 1);
+    }
+
+    for (i = 0; i < 20; i++) {
+        build_append_int_noprefix(table, fru_text[i], 1);
+    }
+
+    build_append_int_noprefix(table, time_stamp, 8);
+}
+
 /*
  * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
  * and return the offset to 0x00000000 for runtime patching.
diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
index 90c8ef8..a71db2f 100644
--- a/include/hw/acpi/aml-build.h
+++ b/include/hw/acpi/aml-build.h
@@ -419,6 +419,12 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
                               uint32_t error_threshold_value,
                               uint32_t error_threshold_window);
 
+void build_append_ghes_generic_data(GArray *table, const char *section_type,
+                                    uint32_t error_severity, uint16_t revision,
+                                    uint8_t validation_bits, uint8_t flags,
+                                    uint32_t error_data_length, uint8_t *fru_id,
+                                    uint8_t *fru_text, uint64_t time_stamp);
+
 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
                        uint64_t len, int node, MemoryAffinityFlags flags);
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (3 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-20 12:42   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description Dongjiu Geng
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

It will help to add Generic Error Status Block to ACPI tables
without using packed C structures and avoid endianness
issues as API doesn't need explicit conversion.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 hw/acpi/aml-build.c         | 14 ++++++++++++++
 include/hw/acpi/aml-build.h |  6 ++++++
 2 files changed, 20 insertions(+)

diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index 102a288..ce90970 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -296,6 +296,20 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
         build_append_int_noprefix(table, error_threshold_window, 4);
 }
 
+/* Generic Error Status Block
+ * ACPI 4.0: 17.3.2.6.1 Generic Error Data
+ */
+void build_append_ghes_generic_status(GArray *table, uint32_t block_status,
+                      uint32_t raw_data_offset, uint32_t raw_data_length,
+                      uint32_t data_length, uint32_t error_severity)
+{
+    build_append_int_noprefix(table, block_status, 4);
+    build_append_int_noprefix(table, raw_data_offset, 4);
+    build_append_int_noprefix(table, raw_data_length, 4);
+    build_append_int_noprefix(table, data_length, 4);
+    build_append_int_noprefix(table, error_severity, 4);
+}
+
 /* Generic Error Data Entry
  * ACPI 4.0: 17.3.2.6.1 Generic Error Data
  */
diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
index a71db2f..1ec7e1b 100644
--- a/include/hw/acpi/aml-build.h
+++ b/include/hw/acpi/aml-build.h
@@ -425,6 +425,12 @@ void build_append_ghes_generic_data(GArray *table, const char *section_type,
                                     uint32_t error_data_length, uint8_t *fru_id,
                                     uint8_t *fru_text, uint64_t time_stamp);
 
+void
+build_append_ghes_generic_status(GArray *table, uint32_t block_status,
+                                 uint32_t raw_data_offset,
+                                 uint32_t raw_data_length,
+                                 uint32_t data_length, uint32_t error_severity);
+
 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
                        uint64_t len, int node, MemoryAffinityFlags flags);
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (4 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-24 11:39   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Add APEI/GHES detailed design document

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 docs/specs/acpi_hest_ghes.txt | 97 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 docs/specs/acpi_hest_ghes.txt

diff --git a/docs/specs/acpi_hest_ghes.txt b/docs/specs/acpi_hest_ghes.txt
new file mode 100644
index 0000000..fbfc787
--- /dev/null
+++ b/docs/specs/acpi_hest_ghes.txt
@@ -0,0 +1,97 @@
+APEI tables generating and CPER record
+=============================
+
+Copyright (C) 2017 HuaWei Corporation.
+
+Design Details:
+-------------------
+
+       etc/acpi/tables                                 etc/hardware_errors
+    ====================                      ==========================================
++ +--------------------------+            +-----------------------+
+| | HEST                     |            |    address            |            +--------------+
+| +--------------------------+            |    registers          |            | Error Status |
+| | GHES1                    |            | +---------------------+            | Data Block 1 |
+| +--------------------------+ +--------->| |error_block_address1 |----------->| +------------+
+| | .................        | |          | +---------------------+            | |  CPER      |
+| | error_status_address-----+-+ +------->| |error_block_address2 |--------+   | |  CPER      |
+| | .................        |   |        | +---------------------+        |   | |  ....      |
+| | read_ack_register--------+-+ |        | |    ..............   |        |   | |  CPER      |
+| | read_ack_preserve        | | |        +-----------------------+        |   | +------------+
+| | read_ack_write           | | | +----->| |error_block_addressN |------+ |   | Error Status |
++ +--------------------------+ | | |      | +---------------------+      | |   | Data Block 2 |
+| | GHES2                    | +-+-+----->| |read_ack_register1   |      | +-->| +------------+
++ +--------------------------+   | |      | +---------------------+      |     | |  CPER      |
+| | .................        |   | | +--->| |read_ack_register2   |      |     | |  CPER      |
+| | error_status_address-----+---+ | |    | +---------------------+      |     | |  ....      |
+| | .................        |     | |    | |  .............      |      |     | |  CPER      |
+| | read_ack_register--------+-----+-+    | +---------------------+      |     +-+------------+
+| | read_ack_preserve        |     |   +->| |read_ack_registerN   |      |     | |..........  |
+| | read_ack_write           |     |   |  | +---------------------+      |     | +------------+
++ +--------------------------|     |   |                                 |     | Error Status |
+| | ...............          |     |   |                                 |     | Data Block N |
++ +--------------------------+     |   |                                 +---->| +------------+
+| | GHESN                    |     |   |                                       | |  CPER      |
++ +--------------------------+     |   |                                       | |  CPER      |
+| | .................        |     |   |                                       | |  ....      |
+| | error_status_address-----+-----+   |                                       | |  CPER      |
+| | .................        |         |                                       +-+------------+
+| | read_ack_register--------+---------+
+| | read_ack_preserve        |
+| | read_ack_write           |
++ +--------------------------+
+
+(1) QEMU generates the ACPI HEST table. This table goes in the current
+    "etc/acpi/tables" fw_cfg blob. Each error source has different
+    notification type.
+
+(2) A new fw_cfg blob called "etc/hardware_errors" is introduced. QEMU
+    also need to populate this blob. The "etc/hardwre_errors" fw_cfg blob
+    contains one address registers table and one Error Status Data Block
+    table, all of which are pre-allocated.
+
+(3) The address registers table contains N Error Block Address entries
+    and N Read Ack Address entries, the size for each entry is 8-byte.
+    The Error Status Data Block table contains N Error Status Data Block
+    entries, the size for each entry is 4096(0x1000) bytes. The total size
+    for "etc/hardware_errors" fw_cfg blob is (N * 8 * 2 + N * 4096) bytes.
+
+(4) QEMU generates the ACPI linker/loader script for the firmware
+
+(4a) The HEST table is part of "etc/acpi/tables", the firmware already
+    allocates the memory for it, because QEMU already generates an ALLOCATE
+    linker/loader command for it
+
+(4b) QEMU creates another ALLOCATE command for the "etc/hardware_errors"
+    blob. The firmware allocates memory for this blob and downloads it.
+
+(5) QEMU generates N ADD_POINTER commands, which patch address in the
+    "error_status_address" fields of the HEST table with a pointer to the
+    corresponding "address registers" in the downloaded "etc/hardware_errors"
+    blob.
+
+(6) QEMU generates N ADD_POINTER commands, which patch address in the
+    "read_ack_register" fields of the HEST table with a pointer to the
+    corresponding "address registers" in the downloaded "etc/hardware_errors" blob.
+
+(7) QEMU generates N ADD_POINTER commands for the firmware, which patch
+    address in the " error_block_address" fields with a pointer to the
+    respective "Error Status Data Block" in the downloaded "etc/hardware_errors"
+    blob.
+
+(8) QEMU Defines a third and write-only fw_cfg blob which is called
+    "etc/hardware_errors_addr". Through that blob, the firmware can send back
+    the guest-side allocation addresses to QEMU. The "etc/hardware_errors_addr"
+    blob contains a 8-byte entry. QEMU generates a single WRITE_POINTER commands
+    for the firmware, the firmware will write back the start address of
+    "etc/hardware_errors" blob to fw_cfg file "etc/hardware_errors_addr". Then
+    Qemu will know the Error Status Data Block for every error source. Each of
+    Error Status Data Block has fixed size which is 4096(0x1000).
+
+(9) When QEMU gets SIGBUS from the kernel, QEMU formats the CPER right into
+    guest memory, and then injects whatever interrupt (or assert whatever GPIO line)
+    as a notification which is necessary for notifying the guest.
+
+(10) This notification (in virtual hardware) will be handled by guest kernel,
+    guest APEI driver will read the CPER which is recorded by QEMU and do the
+    recovery.
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (5 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-05-29  3:37   ` Michael S. Tsirkin
                     ` (2 more replies)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder Dongjiu Geng
                   ` (3 subsequent siblings)
  10 siblings, 3 replies; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

This implements APEI GHES Table generation via fw_cfg blobs.
Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
source. Afterwards, we can extend the supported types if needed. For the
CPER section type, currently it is memory section because kernel
mainly wants userspace to handle the memory errors.

This patch follows the spec ACPI 6.2 to build the Hardware Error Source
table, for the detailed information, please refer to document:
docs/specs/acpi_hest_ghes.txt

Suggested-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 default-configs/arm-softmmu.mak |   1 +
 hw/acpi/Kconfig                 |   4 +
 hw/acpi/Makefile.objs           |   1 +
 hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
 hw/acpi/aml-build.c             |   2 +
 hw/arm/virt-acpi-build.c        |  12 +++
 include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
 include/hw/acpi/aml-build.h     |   1 +
 8 files changed, 271 insertions(+)
 create mode 100644 hw/acpi/acpi_ghes.c
 create mode 100644 include/hw/acpi/acpi_ghes.h

diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
index 613d19a..7b33ae9 100644
--- a/default-configs/arm-softmmu.mak
+++ b/default-configs/arm-softmmu.mak
@@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
 
 # for realview and versatilepb
 CONFIG_LSI_SCSI_PCI=y
+CONFIG_ACPI_APEI=y
diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
index eca3bee..5228a4b 100644
--- a/hw/acpi/Kconfig
+++ b/hw/acpi/Kconfig
@@ -23,6 +23,10 @@ config ACPI_NVDIMM
     bool
     depends on ACPI
 
+config ACPI_APEI
+    bool
+    depends on ACPI
+
 config ACPI_VMGENID
     bool
     default y
diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
index 2d46e37..5099ada 100644
--- a/hw/acpi/Makefile.objs
+++ b/hw/acpi/Makefile.objs
@@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
 common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
 common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
 common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
+common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
 common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
 
 common-obj-y += acpi_interface.o
diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
new file mode 100644
index 0000000..d03e797
--- /dev/null
+++ b/hw/acpi/acpi_ghes.c
@@ -0,0 +1,171 @@
+/* Support for generating APEI tables and record CPER for Guests
+ *
+ * Copyright (C) 2017 HuaWei Corporation.
+ *
+ * Author: Dongjiu Geng <gengdongjiu@huawei.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/acpi/acpi.h"
+#include "hw/acpi/aml-build.h"
+#include "hw/acpi/acpi_ghes.h"
+#include "hw/nvram/fw_cfg.h"
+#include "sysemu/sysemu.h"
+#include "qemu/error-report.h"
+
+/* Build table for the hardware error fw_cfg blob */
+void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
+{
+    int i;
+
+    /*
+     * | +--------------------------+
+     * | |    error_block_address   |
+     * | |      ..........          |
+     * | +--------------------------+
+     * | |    read_ack_register     |
+     * | |     ...........          |
+     * | +--------------------------+
+     * | |  Error Status Data Block |
+     * | |      ........            |
+     * | +--------------------------+
+     */
+
+    /* Build error_block_address */
+    build_append_int_noprefix((void *)hardware_errors, 0,
+                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);
+
+    /* Build read_ack_register */
+    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
+        /* Initialize the value of read_ack_register to 1, so GHES can be
+         * writeable in the first time
+         */
+        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
+
+     /* Build Error Status Data Block */
+    build_append_int_noprefix((void *)hardware_errors, 0,
+                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
+
+    /* Allocate guest memory for the hardware error fw_cfg blob */
+    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
+                            1, false);
+}
+
+/* Build Hardware Error Source Table */
+void build_apei_hest(GArray *table_data, GArray *hardware_errors,
+                                            BIOSLinker *linker)
+{
+    uint32_t i, error_status_block_offset, length = table_data->len;
+
+    /* Reserve Hardware Error Source Table header size */
+    acpi_data_push(table_data, sizeof(AcpiTableHeader));
+
+    /* Set the error source counts */
+    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
+
+    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
+        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
+         */
+        build_append_int_noprefix(table_data,
+            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
+        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
+        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
+        build_append_int_noprefix(table_data, 0, 1); /* flags */
+
+        build_append_int_noprefix(table_data, 1, 1); /* enabled */
+
+        /* Number of Records To Pre-allocate */
+        build_append_int_noprefix(table_data, 1, 4);
+        /* Max Sections Per Record */
+        build_append_int_noprefix(table_data, 1, 4);
+        /* Max Raw Data Length */
+        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
+
+        /* Build error status address*/
+        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
+        bios_linker_loader_add_pointer(linker,
+            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
+            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
+
+        /* Build Hardware Error Notification
+         * Now only enable GPIO-Signal and ARMv8 SEA notification types
+         */
+        if (i == 0) {
+            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
+                                     0, 0, 0, 0, 0, 0, 0);
+        } else if (i == 1) {
+            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
+                                     0, 0, 0, 0, 0, 0);
+        }
+
+        /* Error Status Block Length */
+        build_append_int_noprefix(table_data,
+            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
+
+        /* Build Read ACK register
+         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
+         * version 2 (GHESv2 - Type 10)
+         */
+        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
+        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
+            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
+            GHES_ERRORS_FW_CFG_FILE,
+            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
+
+        /* Build Read Ack Preserve and Read Ack Writer */
+        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
+        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
+    }
+
+    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
+    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
+                                ACPI_HEST_ERROR_SOURCE_COUNT;
+
+    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
+        /* Patch address of Error Status Data Block into
+         * the error_block_address of hardware_errors fw_cfg blob
+         */
+        bios_linker_loader_add_pointer(linker,
+            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
+            GHES_ERRORS_FW_CFG_FILE,
+            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
+
+    /* write address of hardware_errors fw_cfg blob into the
+     * hardware_errors_addr fw_cfg blob.
+     */
+    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
+        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
+
+    build_header(linker, table_data,
+        (void *)(table_data->data + length), "HEST",
+        table_data->len - length, 1, NULL, "GHES");
+}
+
+static GhesState ges;
+void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
+{
+
+    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
+    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
+
+    /* Create a read-only fw_cfg file for GHES */
+    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
+                    request_block_size);
+
+    /* Create a read-write fw_cfg file for Address */
+    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
+        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
+}
diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index ce90970..3f2b84f 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
     tables->table_data = g_array_new(false, true /* clear */, 1);
     tables->tcpalog = g_array_new(false, true /* clear */, 1);
     tables->vmgenid = g_array_new(false, true /* clear */, 1);
+    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
     tables->linker = bios_linker_loader_init();
 }
 
@@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
     g_array_free(tables->table_data, true);
     g_array_free(tables->tcpalog, mfre);
     g_array_free(tables->vmgenid, mfre);
+    g_array_free(tables->hardware_errors, mfre);
 }
 
 /*
diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
index bf9c0bc..54f4ba5 100644
--- a/hw/arm/virt-acpi-build.c
+++ b/hw/arm/virt-acpi-build.c
@@ -45,6 +45,7 @@
 #include "hw/arm/virt.h"
 #include "sysemu/numa.h"
 #include "kvm_arm.h"
+#include "hw/acpi/acpi_ghes.h"
 
 #define ARM_SPI_BASE 32
 #define ACPI_POWER_BUTTON_DEVICE "PWRB"
@@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
     acpi_add_table(table_offsets, tables_blob);
     build_spcr(tables_blob, tables->linker, vms);
 
+    if (!vmc->no_ras) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_hardware_error_table(tables->hardware_errors, tables->linker);
+        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
+    }
+
     if (nb_numa_nodes > 0) {
         acpi_add_table(table_offsets, tables_blob);
         build_srat(tables_blob, tables->linker, vms);
@@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
 
 void virt_acpi_setup(VirtMachineState *vms)
 {
+    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
     AcpiBuildTables tables;
     AcpiBuildState *build_state;
 
@@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
     fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
                     acpi_data_len(tables.tcpalog));
 
+    if (!vmc->no_ras) {
+        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
+    }
+
     build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
                                               ACPI_BUILD_RSDP_FILE, 0);
 
diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
new file mode 100644
index 0000000..38fd87c
--- /dev/null
+++ b/include/hw/acpi/acpi_ghes.h
@@ -0,0 +1,79 @@
+/* Support for generating APEI tables and record CPER for Guests
+ *
+ * Copyright (C) 2017 HuaWei Corporation.
+ *
+ * Author: Dongjiu Geng <gengdongjiu@huawei.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ACPI_GHES_H
+#define ACPI_GHES_H
+
+#include "hw/acpi/bios-linker-loader.h"
+
+#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
+#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
+
+/* The size of Address field in Generic Address Structure,
+ * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
+ */
+#define GHES_ADDRESS_SIZE           8
+
+#define GHES_DATA_LENGTH            72
+#define GHES_CPER_LENGTH            80
+
+#define ReadAckPreserve             0xfffffffe
+#define ReadAckWrite                0x1
+
+/* The max size in bytes for one error block */
+#define GHES_MAX_RAW_DATA_LENGTH        0x1000
+/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
+ */
+#define ACPI_HEST_ERROR_SOURCE_COUNT    2
+
+/*
+ * | +--------------------------+ 0
+ * | |        Header            |
+ * | +--------------------------+ 40---+-
+ * | | .................        |      |
+ * | | error_status_address-----+ 60   |
+ * | | .................        |      |
+ * | | read_ack_register--------+ 104  92
+ * | | read_ack_preserve        |      |
+ * | | read_ack_write           |      |
+ * + +--------------------------+ 132--+-
+ *
+ * From above GHES definition, the error status address offset is 60;
+ * the Read ack register offset is 104, the whole size of GHESv2 is 92
+ */
+
+/* The error status address offset in GHES */
+#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
+                    offsetof(struct AcpiGenericAddress, address) + n * 92)
+
+/* The read Ack register offset in GHES */
+#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
+                    offsetof(struct AcpiGenericAddress, address) + n * 92)
+
+typedef struct GhesState {
+    uint64_t ghes_addr_le;
+} GhesState;
+
+void build_apei_hest(GArray *table_data, GArray *hardware_error,
+                     BIOSLinker *linker);
+
+void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
+void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
+#endif
diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
index 1ec7e1b..78c0252 100644
--- a/include/hw/acpi/aml-build.h
+++ b/include/hw/acpi/aml-build.h
@@ -220,6 +220,7 @@ struct AcpiBuildTables {
     GArray *rsdp;
     GArray *tcpalog;
     GArray *vmgenid;
+    GArray *hardware_errors;
     BIOSLinker *linker;
 } AcpiBuildTables;
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (6 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-24 12:32   ` Igor Mammedov
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 09/10] target-arm: kvm64: inject synchronous External Abort Dongjiu Geng
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

kvm_hwpoison_page_add() and kvm_unpoison_all() will be used both
by X86 and ARM platforms, so move these functions to a common
accel/kvm/ folder to avoid duplicate code.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 accel/kvm/kvm-all.c     | 33 +++++++++++++++++++++++++++++++++
 include/exec/ram_addr.h | 24 ++++++++++++++++++++++++
 target/arm/kvm.c        |  3 +++
 target/i386/kvm.c       | 34 +---------------------------------
 4 files changed, 61 insertions(+), 33 deletions(-)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 524c4dd..b9f9f29 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -625,6 +625,39 @@ int kvm_vm_check_extension(KVMState *s, unsigned int extension)
     return ret;
 }
 
+typedef struct HWPoisonPage {
+    ram_addr_t ram_addr;
+    QLIST_ENTRY(HWPoisonPage) list;
+} HWPoisonPage;
+
+static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
+    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
+
+void kvm_unpoison_all(void *param)
+{
+    HWPoisonPage *page, *next_page;
+
+    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
+        QLIST_REMOVE(page, list);
+        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
+        g_free(page);
+    }
+}
+
+void kvm_hwpoison_page_add(ram_addr_t ram_addr)
+{
+    HWPoisonPage *page;
+
+    QLIST_FOREACH(page, &hwpoison_page_list, list) {
+        if (page->ram_addr == ram_addr) {
+            return;
+        }
+    }
+    page = g_new(HWPoisonPage, 1);
+    page->ram_addr = ram_addr;
+    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
+}
+
 static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
 {
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
index 139ad79..193b0a7 100644
--- a/include/exec/ram_addr.h
+++ b/include/exec/ram_addr.h
@@ -116,6 +116,30 @@ void qemu_ram_free(RAMBlock *block);
 
 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
 
+/**
+ * kvm_hwpoison_page_add:
+ *
+ * Parameters:
+ *  @ram_addr: the address in the RAM for the poisoned page
+ *
+ * Add a poisoned page to the list
+ *
+ * Return: None.
+ */
+void kvm_hwpoison_page_add(ram_addr_t ram_addr);
+
+/**
+ * kvm_unpoison_all:
+ *
+ * Parameters:
+ *  @param: some data may be passed to this function
+ *
+ * Free and remove all the poisoned pages in the list
+ *
+ * Return: None.
+ */
+void kvm_unpoison_all(void *param);
+
 #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
 #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
 
diff --git a/target/arm/kvm.c b/target/arm/kvm.c
index 5995634..6d3b25b 100644
--- a/target/arm/kvm.c
+++ b/target/arm/kvm.c
@@ -29,6 +29,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/log.h"
+#include "exec/ram_addr.h"
 
 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
     KVM_CAP_LAST_INFO
@@ -187,6 +188,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
 
     cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
 
+    qemu_register_reset(kvm_unpoison_all, NULL);
+
     return 0;
 }
 
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
index 3b29ce5..9bdb879 100644
--- a/target/i386/kvm.c
+++ b/target/i386/kvm.c
@@ -46,6 +46,7 @@
 #include "migration/blocker.h"
 #include "exec/memattrs.h"
 #include "trace.h"
+#include "exec/ram_addr.h"
 
 //#define DEBUG_KVM
 
@@ -467,39 +468,6 @@ uint32_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index)
 }
 
 
-typedef struct HWPoisonPage {
-    ram_addr_t ram_addr;
-    QLIST_ENTRY(HWPoisonPage) list;
-} HWPoisonPage;
-
-static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
-    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
-
-static void kvm_unpoison_all(void *param)
-{
-    HWPoisonPage *page, *next_page;
-
-    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
-        QLIST_REMOVE(page, list);
-        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
-        g_free(page);
-    }
-}
-
-static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
-{
-    HWPoisonPage *page;
-
-    QLIST_FOREACH(page, &hwpoison_page_list, list) {
-        if (page->ram_addr == ram_addr) {
-            return;
-        }
-    }
-    page = g_new(HWPoisonPage, 1);
-    page->ram_addr = ram_addr;
-    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
-}
-
 static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
                                      int *max_banks)
 {
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 09/10] target-arm: kvm64: inject synchronous External Abort
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (7 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM Dongjiu Geng
  2019-05-15  9:40 ` [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU gengdongjiu
  10 siblings, 0 replies; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Add synchronous external abort injection logic, setup
exception type and syndrome value. When switch to guest,
it will jump to the synchronous external abort vector
table entry.

The ESR_ELx.DFSC is set to synchronous external abort(0x10),
and ESR_ELx.FnV is set to not valid(0x1), which will tell
guest that FAR is not valid and hold an UNKNOWN value.
These value will be set to KVM register structures through
KVM_SET_ONE_REG IOCTL.

Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 target/arm/internals.h |  5 +++--
 target/arm/kvm64.c     | 34 ++++++++++++++++++++++++++++++++++
 target/arm/op_helper.c |  2 +-
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/target/arm/internals.h b/target/arm/internals.h
index 587a1dd..4d67a91 100644
--- a/target/arm/internals.h
+++ b/target/arm/internals.h
@@ -451,13 +451,14 @@ static inline uint32_t syn_insn_abort(int same_el, int ea, int s1ptw, int fsc)
         | ARM_EL_IL | (ea << 9) | (s1ptw << 7) | fsc;
 }
 
-static inline uint32_t syn_data_abort_no_iss(int same_el,
+static inline uint32_t syn_data_abort_no_iss(int same_el, int fnv,
                                              int ea, int cm, int s1ptw,
                                              int wnr, int fsc)
 {
     return (EC_DATAABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
            | ARM_EL_IL
-           | (ea << 9) | (cm << 8) | (s1ptw << 7) | (wnr << 6) | fsc;
+           | (fnv << 10) | (ea << 9) | (cm << 8) | (s1ptw << 7)
+           | (wnr << 6) | fsc;
 }
 
 static inline uint32_t syn_data_abort_with_iss(int same_el,
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
index e3ba149..c7bdc6a 100644
--- a/target/arm/kvm64.c
+++ b/target/arm/kvm64.c
@@ -697,6 +697,40 @@ int kvm_arm_cpreg_level(uint64_t regidx)
     return KVM_PUT_RUNTIME_STATE;
 }
 
+/* Inject synchronous external abort */
+static void kvm_inject_arm_sea(CPUState *c)
+{
+    ARMCPU *cpu = ARM_CPU(c);
+    CPUARMState *env = &cpu->env;
+    CPUClass *cc = CPU_GET_CLASS(c);
+    uint32_t esr;
+    bool same_el;
+
+    /**
+     * set the exception type to synchronous data abort
+     * and the target exception Level to EL1.
+     */
+    c->exception_index = EXCP_DATA_ABORT;
+    env->exception.target_el = 1;
+
+    /*
+     * Set the DFSC to synchronous external abort and set FnV to not valid,
+     * this will tell guest the FAR_ELx is UNKNOWN for this abort.
+     */
+
+    /* This exception comes from lower or current exception level. */
+    same_el = arm_current_el(env) == env->exception.target_el;
+    esr = syn_data_abort_no_iss(same_el, 1, 0, 0, 0, 0, 0x10);
+
+    env->exception.syndrome = esr;
+
+    /**
+     * The vcpu thread already hold BQL, so no need hold again when
+     * calling do_interrupt
+     */
+    cc->do_interrupt(c);
+}
+
 #define AARCH64_CORE_REG(x)   (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
                  KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
 
diff --git a/target/arm/op_helper.c b/target/arm/op_helper.c
index 8698b4d..d43134a 100644
--- a/target/arm/op_helper.c
+++ b/target/arm/op_helper.c
@@ -109,7 +109,7 @@ static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
      * ISV field.
      */
     if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
-        syn = syn_data_abort_no_iss(same_el,
+        syn = syn_data_abort_no_iss(same_el, 0,
                                     ea, 0, s1ptw, is_write, fsc);
     } else {
         /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (8 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 09/10] target-arm: kvm64: inject synchronous External Abort Dongjiu Geng
@ 2019-05-14 11:18 ` Dongjiu Geng
  2019-06-06 13:31   ` Jonathan Cameron
  2019-06-24 13:08   ` Igor Mammedov
  2019-05-15  9:40 ` [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU gengdongjiu
  10 siblings, 2 replies; 45+ messages in thread
From: Dongjiu Geng @ 2019-05-14 11:18 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, gengdongjiu, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Add SIGBUS signal handler. In this handler, it checks the SIGBUS type,
translates the host VA delivered by host to guest PA, then fill this PA
to guest APEI GHES memory, then notify guest according to the SIGBUS type.

If guest accesses the poisoned memory, it generates Synchronous External
Abort(SEA). Then host kernel gets an APEI notification and call memory_failure()
to unmapped the affected page for the guest's stage 2, finally return
to guest.

Guest continues to access PG_hwpoison page, it will trap to KVM as stage2 fault,
then a SIGBUS_MCEERR_AR synchronous signal is delivered to Qemu, Qemu record this
error address into guest APEI GHES memory and notify guest using
Synchronous-External-Abort(SEA).

Suggested-by: James Morse <james.morse@arm.com>
Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
---
 hw/acpi/acpi_ghes.c         | 177 ++++++++++++++++++++++++++++++++++++++++++++
 include/hw/acpi/acpi_ghes.h |   6 +-
 include/sysemu/kvm.h        |   2 +-
 target/arm/kvm64.c          |  39 ++++++++++
 4 files changed, 222 insertions(+), 2 deletions(-)

diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
index d03e797..06b7374 100644
--- a/hw/acpi/acpi_ghes.c
+++ b/hw/acpi/acpi_ghes.c
@@ -26,6 +26,101 @@
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
+/* UEFI 2.6: N.2.5 Memory Error Section */
+static void build_append_mem_cper(GArray *table, uint64_t error_physical_addr)
+{
+    /*
+     * Memory Error Record
+     */
+    build_append_int_noprefix(table,
+                 (1UL << 14) | /* Type Valid */
+                 (1UL << 1) /* Physical Address Valid */,
+                 8);
+    /* Memory error status information */
+    build_append_int_noprefix(table, 0, 8);
+    /* The physical address at which the memory error occurred */
+    build_append_int_noprefix(table, error_physical_addr, 8);
+    build_append_int_noprefix(table, 0, 48);
+    build_append_int_noprefix(table, 0 /* Unknown error */, 1);
+    build_append_int_noprefix(table, 0, 7);
+}
+
+static int ghes_record_mem_error(uint64_t error_block_address,
+                                    uint64_t error_physical_addr)
+{
+    GArray *block;
+    uint64_t current_block_length;
+    uint32_t data_length;
+    /* Memory section */
+    char mem_section_id_le[] = {0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE,
+                                0x4E, 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C,
+                                0x83, 0xB1};
+    uint8_t fru_id[16] = {0};
+    uint8_t fru_text[20] = {0};
+
+    /* Generic Error Status Block
+     * | +---------------------+
+     * | |     block_status    |
+     * | +---------------------+
+     * | |    raw_data_offset  |
+     * | +---------------------+
+     * | |    raw_data_length  |
+     * | +---------------------+
+     * | |     data_length     |
+     * | +---------------------+
+     * | |   error_severity    |
+     * | +---------------------+
+     */
+    block = g_array_new(false, true /* clear */, 1);
+
+    /* Get the length of the Generic Error Data Entries */
+    cpu_physical_memory_read(error_block_address +
+        offsetof(AcpiGenericErrorStatus, data_length), &data_length, 4);
+
+    /* The current whole length of the generic error status block */
+    current_block_length = sizeof(AcpiGenericErrorStatus) + le32_to_cpu(data_length);
+
+    /* This is the length if adding a new generic error data entry*/
+    data_length += GHES_DATA_LENGTH;
+    data_length += GHES_MEM_CPER_LENGTH;
+
+    /* Check whether it will run out of the preallocated memory if adding a new
+     * generic error data entry
+     */
+    if ((data_length + sizeof(AcpiGenericErrorStatus)) > GHES_MAX_RAW_DATA_LENGTH) {
+        error_report("Record CPER out of boundary!!!");
+        return GHES_CPER_FAIL;
+    }
+
+    /* Build the new generic error status block header */
+    build_append_ghes_generic_status(block, cpu_to_le32(ACPI_GEBS_UNCORRECTABLE), 0, 0,
+        cpu_to_le32(data_length), cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE));
+
+    /* Write back above generic error status block header to guest memory */
+    cpu_physical_memory_write(error_block_address, block->data,
+                              block->len);
+
+    /* Add a new generic error data entry */
+
+    data_length = block->len;
+    /* Build this new generic error data entry header */
+    build_append_ghes_generic_data(block, mem_section_id_le,
+                    cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE), cpu_to_le32(0x300), 0, 0,
+                    cpu_to_le32(80)/* the total size of Memory Error Record */, fru_id,
+                    fru_text, 0);
+
+    /* Build the memory section CPER for above new generic error data entry */
+    build_append_mem_cper(block, error_physical_addr);
+
+    /* Write back above this new generic error data entry to guest memory */
+    cpu_physical_memory_write(error_block_address + current_block_length,
+                    block->data + data_length, block->len - data_length);
+
+    g_array_free(block, true);
+
+    return GHES_CPER_OK;
+}
+
 /* Build table for the hardware error fw_cfg blob */
 void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
 {
@@ -169,3 +264,85 @@ void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
     fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
         &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
 }
+
+bool ghes_record_errors(uint32_t notify, uint64_t physical_address)
+{
+    uint64_t error_block_addr, read_ack_register_addr;
+    int read_ack_register = 0, loop = 0;
+    uint64_t start_addr = le32_to_cpu(ges.ghes_addr_le);
+    bool ret = GHES_CPER_FAIL;
+    const uint8_t error_source_id[] = { 0xff, 0xff, 0xff, 0xff,
+                                        0xff, 0xff, 0xff, 0, 1};
+
+    /*
+     * | +---------------------+ ges.ghes_addr_le
+     * | |error_block_address0 |
+     * | +---------------------+ --+--
+     * | |    .............    | GHES_ADDRESS_SIZE
+     * | +---------------------+ --+--
+     * | |error_block_addressN |
+     * | +---------------------+
+     * | | read_ack_register0  |
+     * | +---------------------+ --+--
+     * | |   .............     | GHES_ADDRESS_SIZE
+     * | +---------------------+ --+--
+     * | | read_ack_registerN  |
+     * | +---------------------+ --+--
+     * | |      CPER           |   |
+     * | |      ....           | GHES_MAX_RAW_DATA_LENGT
+     * | |      CPER           |   |
+     * | +---------------------+ --+--
+     * | |    ..........       |
+     * | +---------------------+
+     * | |      CPER           |
+     * | |      ....           |
+     * | |      CPER           |
+     * | +---------------------+
+     */
+    if (physical_address && notify < ACPI_HEST_NOTIFY_RESERVED) {
+        /* Find and check the source id for this new CPER */
+        if (error_source_id[notify] != 0xff) {
+            start_addr += error_source_id[notify] * GHES_ADDRESS_SIZE;
+        } else {
+            goto out;
+        }
+
+        cpu_physical_memory_read(start_addr, &error_block_addr,
+                                    GHES_ADDRESS_SIZE);
+
+        read_ack_register_addr = start_addr +
+                        ACPI_HEST_ERROR_SOURCE_COUNT * GHES_ADDRESS_SIZE;
+retry:
+        cpu_physical_memory_read(read_ack_register_addr,
+                                 &read_ack_register, GHES_ADDRESS_SIZE);
+
+        /* zero means OSPM does not acknowledge the error */
+        if (!read_ack_register) {
+            if (loop < 3) {
+                usleep(100 * 1000);
+                loop++;
+                goto retry;
+            } else {
+                error_report("OSPM does not acknowledge previous error,"
+                    " so can not record CPER for current error, forcibly acknowledge"
+                    " previous error to avoid blocking next time CPER record! Exit");
+                read_ack_register = 1;
+                cpu_physical_memory_write(read_ack_register_addr,
+                    &read_ack_register, GHES_ADDRESS_SIZE);
+            }
+        } else {
+            if (error_block_addr) {
+                read_ack_register = 0;
+                /* Clear the Read Ack Register, OSPM will write it to 1 when
+                 * acknowledge this error.
+                 */
+                cpu_physical_memory_write(read_ack_register_addr,
+                    &read_ack_register, GHES_ADDRESS_SIZE);
+                ret = ghes_record_mem_error(error_block_addr, physical_address);
+            }
+        }
+    }
+
+out:
+    return ret;
+}
diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
index 38fd87c..6b38097 100644
--- a/include/hw/acpi/acpi_ghes.h
+++ b/include/hw/acpi/acpi_ghes.h
@@ -32,11 +32,14 @@
 #define GHES_ADDRESS_SIZE           8
 
 #define GHES_DATA_LENGTH            72
-#define GHES_CPER_LENGTH            80
+#define GHES_MEM_CPER_LENGTH        80
 
 #define ReadAckPreserve             0xfffffffe
 #define ReadAckWrite                0x1
 
+#define GHES_CPER_OK                1
+#define GHES_CPER_FAIL              0
+
 /* The max size in bytes for one error block */
 #define GHES_MAX_RAW_DATA_LENGTH        0x1000
 /* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
@@ -76,4 +79,5 @@ void build_apei_hest(GArray *table_data, GArray *hardware_error,
 
 void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
 void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
+bool ghes_record_errors(uint32_t notify, uint64_t error_physical_addr);
 #endif
diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
index a6d1cd1..1d1a7a8 100644
--- a/include/sysemu/kvm.h
+++ b/include/sysemu/kvm.h
@@ -377,7 +377,7 @@ bool kvm_vcpu_id_is_valid(int vcpu_id);
 /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
 unsigned long kvm_arch_vcpu_id(CPUState *cpu);
 
-#ifdef TARGET_I386
+#if defined(TARGET_I386) || defined(TARGET_AARCH64)
 #define KVM_HAVE_MCE_INJECTION 1
 void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
 #endif
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
index c7bdc6a..d2eac28 100644
--- a/target/arm/kvm64.c
+++ b/target/arm/kvm64.c
@@ -27,6 +27,10 @@
 #include "kvm_arm.h"
 #include "internals.h"
 #include "hw/arm/arm.h"
+#include "exec/ram_addr.h"
+#include "hw/acpi/acpi-defs.h"
+#include "hw/acpi/acpi_ghes.h"
+#include "hw/acpi/acpi.h"
 
 static bool have_guest_debug;
 
@@ -1029,6 +1033,41 @@ int kvm_arch_get_registers(CPUState *cs)
     return ret;
 }
 
+void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
+{
+    ram_addr_t ram_addr;
+    hwaddr paddr;
+
+    assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
+
+    if (acpi_enabled && addr) {
+        ram_addr = qemu_ram_addr_from_host(addr);
+        if (ram_addr != RAM_ADDR_INVALID &&
+            kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
+            kvm_hwpoison_page_add(ram_addr);
+            /* Asynchronous signal will be masked by main thread, so
+             * only handle synchronous signal.
+             */
+            if (code == BUS_MCEERR_AR) {
+                kvm_cpu_synchronize_state(c);
+                if (GHES_CPER_FAIL != ghes_record_errors(ACPI_HEST_NOTIFY_SEA, paddr)) {
+                    kvm_inject_arm_sea(c);
+                } else {
+                    fprintf(stderr, "failed to record the error\n");
+                }
+            }
+            return;
+        }
+        fprintf(stderr, "Hardware memory error for memory used by "
+                "QEMU itself instead of guest system!\n");
+    }
+
+    if (code == BUS_MCEERR_AR) {
+        fprintf(stderr, "Hardware memory error!\n");
+        exit(1);
+    }
+}
+
 /* C6.6.29 BRK instruction */
 static const uint32_t brk_insn = 0xd4200000;
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU
  2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
                   ` (9 preceding siblings ...)
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM Dongjiu Geng
@ 2019-05-15  9:40 ` gengdongjiu
  10 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-05-15  9:40 UTC (permalink / raw)
  To: pbonzini, mst, imammedo, shannon.zhaosl, peter.maydell, lersek,
	james.morse, mtosatti, rth, ehabkost, zhengxiang9,
	jonathan.cameron, xuwei5, kvm, qemu-devel, qemu-arm, linuxarm

Hi All,
 for this series patch, we can use below simple method to test:

1). Apply below hard code change after applying this series patch:
diff --git a/cpus.c b/cpus.c
index e58e7ab..7149f54 100644
--- a/cpus.c
+++ b/cpus.c
@@ -1131,6 +1131,8 @@ static void sigbus_reraise(void)

 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
 {
+    siginfo->si_code = BUS_MCEERR_AO;
+
     if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
         sigbus_reraise();
     }
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
index d2eac28..6c9956e 100644
--- a/target/arm/kvm64.c
+++ b/target/arm/kvm64.c
@@ -1035,20 +1035,23 @@ int kvm_arch_get_registers(CPUState *cs)

 void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
 {
+#if 0
     ram_addr_t ram_addr;
-    hwaddr paddr;
-
+#endif
+    hwaddr paddr = 0x400a1000;
     assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);

+#if 0
     if (acpi_enabled && addr) {
         ram_addr = qemu_ram_addr_from_host(addr);
         if (ram_addr != RAM_ADDR_INVALID &&
             kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
             kvm_hwpoison_page_add(ram_addr);
+#endif
             /* Asynchronous signal will be masked by main thread, so
              * only handle synchronous signal.
              */
-            if (code == BUS_MCEERR_AR) {
+            if (code == BUS_MCEERR_AR || BUS_MCEERR_AO == code) {
                 kvm_cpu_synchronize_state(c);
                 if (GHES_CPER_FAIL != ghes_record_errors(ACPI_HEST_NOTIFY_SEA, paddr)) {
                     kvm_inject_arm_sea(c);
@@ -1057,11 +1060,13 @@ void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
                 }
             }
             return;
+#if 0
         }
         fprintf(stderr, "Hardware memory error for memory used by "
                 "QEMU itself instead of guest system!\n");
     }

+#endif
     if (code == BUS_MCEERR_AR) {
         fprintf(stderr, "Hardware memory error!\n");
         exit(1);

2) In Arm64 machine run below command to run a virtual machine with kvm disabled, and enable bios:
./qemu-system-aarch64 -cpu cortex-a57 -machine virt,gic-version=3  -smp 4 -nographic -kernel Image -bios QEMU_EFI.fd -append "rdinit=/init console=ttyAMA0 mem=512M root=/dev/ram0 earlycon=pl011,0x9000000 rw" -initrd guestfs_new.cpio.gz

3) Deliver SGIBUS signal to QEMU:
   kill -s SIGBUS 11522

4) then you can see QEMU record the error memory address to APEI table, and inject a SEA to guest, for example:

[   75.514270] {1}[Hardware Error]: Hardware error from APEI Generic Hardware Error Source: 1
[   75.531858] {1}[Hardware Error]: event severity: recoverable
[   75.535095] {1}[Hardware Error]:  Error 0, type: recoverable
[   75.539223] {1}[Hardware Error]:   section_type: memory error
[   75.543878] {1}[Hardware Error]:   physical_address: 0x00000000400a1000
[   75.548696] {1}[Hardware Error]:   error_type: 0, unknown
[   75.577156] Internal error: synchronous external abort: 96000410 [#1] PREEMPT SMP

On 2019/5/14 19:18, Dongjiu Geng wrote:
> In the ARMv8 platform, the CPU error type are synchronous external
> abort(SEA) and SError Interrupt (SEI). If exception happens to guest,
> sometimes guest itself do the recovery is better, because host 
> does not know guest's detailed information. For example, if a guest
> user-space application happen exception, host does not which application
> encounter errors.
> 
> For the ARMv8 SEA/SEI, KVM or host kernel delivers SIGBUS to notify user
> space. After user space gets  the notification, it will record the CPER
> to guest GHES buffer for guest and inject a exception or IRQ to guest.
> 
> In the current implement, if the SIGBUS is BUS_MCEERR_AR, we will
> treat it as synchronous exception, and use ARMv8 SEA notification type
> to notify guest after recording CPER for guest;
> 
> This series patches are based on Qemu 4.0, which have two parts:
> 1. Generate APEI/GHES table.
> 2. Handle the SIGBUS signal, record the CPER in runtime and fill into guest memory,
>    then according to SIGBUS type to notify guest.
> 
> Whole solution was suggested by James(james.morse@arm.com); APEI part solution is suggested by
> Laszlo(lersek@redhat.com). Shown some discussion in [1].
> 
> 
> This series patches have already tested on ARM64 platform with RAS feature enabled:
> Show the APEI part verification result in [2]
> Show the BUS_MCEERR_AR SIGBUS handling verification result in [3]
> 
> ---
> change since v16:
> 1. check whether ACPI table is enabled when handling the memory error in the SIGBUS handler.
> 
> Change since v15:
> 1. Add a doc-comment in the proper format for 'include/exec/ram_addr.h'
> 2. Remove write_part_cpustate_to_list() because there is another bug fix patch
>    has been merged "arm: Allow system registers for KVM guests to be changed by QEMU code"
> 3. Add some comments for kvm_inject_arm_sea() in 'target/arm/kvm64.c'
> 4. Compare the arm_current_el() return value to 0,1,2,3, not to PSTATE_MODE_* constants.
> 5. Change the RAS support wasn't introduced before 4.1 QEMU version.
> 6. Move the no_ras flag  patch to begin in this series
> 
> Change since v14:
> 1. Remove the BUS_MCEERR_AO handling logic because this asynchronous signal was masked by main thread 
> 2. Address some Igor Mammedov's comments(ACPI part)
>    1) change the comments for the enum AcpiHestNotifyType definition and remove ditto in patch 1
>    2) change some patch commit messages and separate "APEI GHES table generation" patch to more patches.
> 3. Address some peter's comments(arm64 Synchronous External Abort injection)
>    1) change some code notes
>    2) using arm_current_el() for current EL
>    2) use the helper functions for those (syn_data_abort_*).
> 
> Change since v13:
> 1. Move the patches that set guest ESR and inject virtual SError out of this series
> 2. Clean and optimize the APEI part patches 
> 3. Update the commit messages and add some comments for the code
> 
> Change since v12:
> 1. Address Paolo's comments to move HWPoisonPage definition to accel/kvm/kvm-all.c
> 2. Only call kvm_cpu_synchronize_state() when get the BUS_MCEERR_AR signal
> 3. Only add and enable GPIO-Signal and ARMv8 SEA two hardware error sources
> 4. Address Michael's comments to not sync SPDX from Linux kernel header file 
> 
> Change since v11:
> Address James's comments(james.morse@arm.com)
> 1. Check whether KVM has the capability to to set ESR instead of detecting host CPU RAS capability
> 2. For SIGBUS_MCEERR_AR SIGBUS, use Synchronous-External-Abort(SEA) notification type
>    for SIGBUS_MCEERR_AO SIGBUS, use GPIO-Signal notification
> 
> 
> Address Shannon's comments(for ACPI part):
> 1. Unify hest_ghes.c and hest_ghes.h license declaration
> 2. Remove unnecessary including "qmp-commands.h" in hest_ghes.c
> 3. Unconditionally add guest APEI table based on James's comments(james.morse@arm.com) 
> 4. Add a option to virt machine for migration compatibility. On new virt machine it's on
>    by default while off for old ones, we enabled it since 2.12
> 5. Refer to the ACPI spec version which introduces Hardware Error Notification first time
> 6. Add ACPI_HEST_NOTIFY_RESERVED notification type
> 
> Address Igor's comments(for ACPI part):
> 1. Add doc patch first which will describe how it's supposed to work between QEMU/firmware/guest
>    OS with expected flows.
> 2. Move APEI diagrams into doc/spec patch
> 3. Remove redundant g_malloc in ghes_record_cper()
> 4. Use build_append_int_noprefix() API to compose whole error status block and whole APEI table, 
>    and try to get rid of most structures in patch 1, as they will be left unused after that
> 5. Reuse something like https://github.com/imammedo/qemu/commit/3d2fd6d13a3ea298d2ee814835495ce6241d085c
>    to build GAS
> 6. Remove much offsetof() in the function
> 7. Build independent tables first and only then build dependent tables passing to it pointers
>    to previously build table if necessary.
> 8. Redefine macro GHES_ACPI_HEST_NOTIFY_RESERVED to ACPI_HEST_ERROR_SOURCE_COUNT to avoid confusion
> 
> 
> Address Peter Maydell's comments
> 1. linux-headers is done as a patch of their own created using scripts/update-linux-headers.sh run against a
>    mainline kernel tree 
> 2. Tested whether this patchset builds OK on aarch32  
> 3. Abstract Hwpoison page adding code  out properly into a cpu-independent source file from target/i386/kvm.c,
>    such as kvm-all.c
> 4. Add doc-comment formatted documentation comment for new globally-visible function prototype in a header
> 
> ---
> [1]:
> https://lkml.org/lkml/2017/2/27/246
> https://patchwork.kernel.org/patch/9633105/
> https://patchwork.kernel.org/patch/9925227/
> 
> [2]:
> Note: the UEFI(QEMU_EFI.fd) is needed if guest want to use ACPI table.
> 
> After guest boot up, dump the APEI table, then can see the initialized table
> (1) # iasl -p ./HEST -d /sys/firmware/acpi/tables/HEST
> (2) # cat HEST.dsl
>     /*
>      * Intel ACPI Component Architecture
>      * AML/ASL+ Disassembler version 20170728 (64-bit version)
>      * Copyright (c) 2000 - 2017 Intel Corporation
>      *
>      * Disassembly of /sys/firmware/acpi/tables/HEST, Mon Sep  5 07:59:17 2016
>      *
>      * ACPI Data Table [HEST]
>      *
>      * Format: [HexOffset DecimalOffset ByteLength]  FieldName : FieldValue
>      */
> 
>     ..................................................................................
>     [308h 0776   2]                Subtable Type : 000A [Generic Hardware Error Source V2]
>     [30Ah 0778   2]                    Source Id : 0001
>     [30Ch 0780   2]            Related Source Id : FFFF
>     [30Eh 0782   1]                     Reserved : 00
>     [30Fh 0783   1]                      Enabled : 01
>     [310h 0784   4]       Records To Preallocate : 00000001
>     [314h 0788   4]      Max Sections Per Record : 00000001
>     [318h 0792   4]          Max Raw Data Length : 00001000
> 
>     [31Ch 0796  12]         Error Status Address : [Generic Address Structure]
>     [31Ch 0796   1]                     Space ID : 00 [SystemMemory]
>     [31Dh 0797   1]                    Bit Width : 40
>     [31Eh 0798   1]                   Bit Offset : 00
>     [31Fh 0799   1]         Encoded Access Width : 04 [QWord Access:64]
>     [320h 0800   8]                      Address : 00000000785D0040
> 
>     [328h 0808  28]                       Notify : [Hardware Error Notification Structure]
>     [328h 0808   1]                  Notify Type : 08 [SEA]
>     [329h 0809   1]                Notify Length : 1C
>     [32Ah 0810   2]   Configuration Write Enable : 0000
>     [32Ch 0812   4]                 PollInterval : 00000000
>     [330h 0816   4]                       Vector : 00000000
>     [334h 0820   4]      Polling Threshold Value : 00000000
>     [338h 0824   4]     Polling Threshold Window : 00000000
>     [33Ch 0828   4]        Error Threshold Value : 00000000
>     [340h 0832   4]       Error Threshold Window : 00000000
> 
>     [344h 0836   4]    Error Status Block Length : 00001000
>     [348h 0840  12]            Read Ack Register : [Generic Address Structure]
>     [348h 0840   1]                     Space ID : 00 [SystemMemory]
>     [349h 0841   1]                    Bit Width : 40
>     [34Ah 0842   1]                   Bit Offset : 00
>     [34Bh 0843   1]         Encoded Access Width : 04 [QWord Access:64]
>     [34Ch 0844   8]                      Address : 00000000785D0098
> 
>     [354h 0852   8]            Read Ack Preserve : 00000000FFFFFFFE
>     [35Ch 0860   8]               Read Ack Write : 0000000000000001
> 
>     .....................................................................................
> 
> (3) After a synchronous external abort(SEA) happen, Qemu receive a SIGBUS and 
>     filled the CPER into guest GHES memory.  For example, according to above table,
>     the address that contains the physical address of a block of memory that holds
>     the error status data for this abort is 0x00000000785D0040
> (4) the address for SEA notification error source is 0x785d80b0
>     (qemu) xp /1 0x00000000785D0040
>     00000000785d0040: 0x785d80b0
> 
> (5) check the content of generic error status block and generic error data entry
>     (qemu) xp /100x 0x785d80b0
>     00000000785d80b0: 0x00000001 0x00000000 0x00000000 0x00000098
>     00000000785d80c0: 0x00000000 0xa5bc1114 0x4ede6f64 0x833e63b8
>     00000000785d80d0: 0xb1837ced 0x00000000 0x00000300 0x00000050
>     00000000785d80e0: 0x00000000 0x00000000 0x00000000 0x00000000
>     00000000785d80f0: 0x00000000 0x00000000 0x00000000 0x00000000
>     00000000785d8100: 0x00000000 0x00000000 0x00000000 0x00004002
> (6) check the OSPM's ACK value(for example SEA)
>     /* Before OSPM acknowledges the error, check the ACK value */
>     (qemu) xp /1 0x00000000785D0098
>     00000000785d00f0: 0x00000000
> 
>     /* After OSPM acknowledges the error, check the ACK value, it change to 1 from 0 */
>     (qemu) xp /1 0x00000000785D0098
>     00000000785d00f0: 0x00000001
> 
> [3]: KVM deliver "BUS_MCEERR_AR" to Qemu, Qemu record the guest CPER and inject
>     synchronous external abort to notify guest, then guest do the recovery.
> 
> [ 1552.516170] Synchronous External Abort: synchronous external abort (0x92000410) at 0x000000003751c6b4
> [ 1553.074073] {1}[Hardware Error]: Hardware error from APEI Generic Hardware Error Source: 8
> [ 1553.081654] {1}[Hardware Error]: event severity: recoverable
> [ 1554.034191] {1}[Hardware Error]:  Error 0, type: recoverable
> [ 1554.037934] {1}[Hardware Error]:   section_type: memory error
> [ 1554.513261] {1}[Hardware Error]:   physical_address: 0x0000000040fa6000
> [ 1554.513944] {1}[Hardware Error]:   error_type: 0, unknown
> [ 1555.041451] Memory failure: 0x40fa6: Killing mca-recover:1296 due to hardware memory corruption
> [ 1555.373116] Memory failure: 0x40fa6: recovery action for dirty LRU page: Recovered
> 
> 
> 
> Dongjiu Geng (10):
>   hw/arm/virt: Add RAS platform version for migration
>   ACPI: add some GHES structures and macros definition
>   acpi: add build_append_ghes_notify() helper for Hardware Error
>     Notification
>   acpi: add build_append_ghes_generic_data() helper for Generic Error
>     Data Entry
>   acpi: add build_append_ghes_generic_status() helper for Generic Error
>     Status Block
>   docs: APEI GHES generation and CPER record description
>   ACPI: Add APEI GHES table generation support
>   KVM: Move related hwpoison page functions to accel/kvm/ folder
>   target-arm: kvm64: inject synchronous External Abort
>   target-arm: kvm64: handle SIGBUS signal from kernel or KVM
> 
>  accel/kvm/kvm-all.c             |  33 ++++
>  default-configs/arm-softmmu.mak |   1 +
>  docs/specs/acpi_hest_ghes.txt   |  97 +++++++++++
>  hw/acpi/Kconfig                 |   4 +
>  hw/acpi/Makefile.objs           |   1 +
>  hw/acpi/acpi_ghes.c             | 348 ++++++++++++++++++++++++++++++++++++++++
>  hw/acpi/aml-build.c             |  70 ++++++++
>  hw/arm/virt-acpi-build.c        |  12 ++
>  hw/arm/virt.c                   |   6 +
>  include/exec/ram_addr.h         |  24 +++
>  include/hw/acpi/acpi-defs.h     |  52 ++++++
>  include/hw/acpi/acpi_ghes.h     |  83 ++++++++++
>  include/hw/acpi/aml-build.h     |  21 +++
>  include/hw/arm/virt.h           |   1 +
>  include/sysemu/kvm.h            |   2 +-
>  target/arm/internals.h          |   5 +-
>  target/arm/kvm.c                |   3 +
>  target/arm/kvm64.c              |  73 +++++++++
>  target/arm/op_helper.c          |   2 +-
>  target/i386/kvm.c               |  34 +---
>  20 files changed, 835 insertions(+), 37 deletions(-)
>  create mode 100644 docs/specs/acpi_hest_ghes.txt
>  create mode 100644 hw/acpi/acpi_ghes.c
>  create mode 100644 include/hw/acpi/acpi_ghes.h
> 



^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
@ 2019-05-29  3:37   ` Michael S. Tsirkin
  2019-05-30 14:47     ` gengdongjiu
  2019-06-06 13:43   ` Jonathan Cameron
  2019-06-24 12:27   ` Igor Mammedov
  2 siblings, 1 reply; 45+ messages in thread
From: Michael S. Tsirkin @ 2019-05-29  3:37 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mtosatti, qemu-devel, linuxarm,
	shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	jonathan.cameron, imammedo, pbonzini, xuwei5, lersek, rth

On Tue, May 14, 2019 at 04:18:20AM -0700, Dongjiu Geng wrote:
> This implements APEI GHES Table generation via fw_cfg blobs.
> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
> source. Afterwards, we can extend the supported types if needed. For the
> CPER section type, currently it is memory section because kernel
> mainly wants userspace to handle the memory errors.
> 
> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
> table, for the detailed information, please refer to document:
> docs/specs/acpi_hest_ghes.txt
> 
> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  default-configs/arm-softmmu.mak |   1 +
>  hw/acpi/Kconfig                 |   4 +
>  hw/acpi/Makefile.objs           |   1 +
>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
>  hw/acpi/aml-build.c             |   2 +
>  hw/arm/virt-acpi-build.c        |  12 +++
>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
>  include/hw/acpi/aml-build.h     |   1 +
>  8 files changed, 271 insertions(+)
>  create mode 100644 hw/acpi/acpi_ghes.c
>  create mode 100644 include/hw/acpi/acpi_ghes.h
> 
> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
> index 613d19a..7b33ae9 100644
> --- a/default-configs/arm-softmmu.mak
> +++ b/default-configs/arm-softmmu.mak
> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
>  
>  # for realview and versatilepb
>  CONFIG_LSI_SCSI_PCI=y
> +CONFIG_ACPI_APEI=y
> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
> index eca3bee..5228a4b 100644
> --- a/hw/acpi/Kconfig
> +++ b/hw/acpi/Kconfig
> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
>      bool
>      depends on ACPI
>  
> +config ACPI_APEI
> +    bool
> +    depends on ACPI
> +
>  config ACPI_VMGENID
>      bool
>      default y
> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
> index 2d46e37..5099ada 100644
> --- a/hw/acpi/Makefile.objs
> +++ b/hw/acpi/Makefile.objs
> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
>  
>  common-obj-y += acpi_interface.o
> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> new file mode 100644
> index 0000000..d03e797
> --- /dev/null
> +++ b/hw/acpi/acpi_ghes.c
> @@ -0,0 +1,171 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/acpi/acpi.h"
> +#include "hw/acpi/aml-build.h"
> +#include "hw/acpi/acpi_ghes.h"
> +#include "hw/nvram/fw_cfg.h"
> +#include "sysemu/sysemu.h"
> +#include "qemu/error-report.h"
> +
> +/* Build table for the hardware error fw_cfg blob */
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
> +{
> +    int i;
> +
> +    /*
> +     * | +--------------------------+
> +     * | |    error_block_address   |
> +     * | |      ..........          |
> +     * | +--------------------------+
> +     * | |    read_ack_register     |
> +     * | |     ...........          |
> +     * | +--------------------------+
> +     * | |  Error Status Data Block |
> +     * | |      ........            |
> +     * | +--------------------------+
> +     */
> +
> +    /* Build error_block_address */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);

Why are you casting everything to void *?

C type safety goes out the window ...

> +
> +    /* Build read_ack_register */
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> +        /* Initialize the value of read_ack_register to 1, so GHES can be
> +         * writeable in the first time
> +         */
> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
> +
> +     /* Build Error Status Data Block */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
> +
> +    /* Allocate guest memory for the hardware error fw_cfg blob */
> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
> +                            1, false);
> +}
> +
> +/* Build Hardware Error Source Table */
> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
> +                                            BIOSLinker *linker)
> +{
> +    uint32_t i, error_status_block_offset, length = table_data->len;
> +
> +    /* Reserve Hardware Error Source Table header size */
> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
> +
> +    /* Set the error source counts */
> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
> +         */
> +        build_append_int_noprefix(table_data,
> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
> +
> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
> +
> +        /* Number of Records To Pre-allocate */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Sections Per Record */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Raw Data Length */
> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
> +
> +        /* Build error status address*/
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker,
> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
> +
> +        /* Build Hardware Error Notification
> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
> +         */
> +        if (i == 0) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
> +                                     0, 0, 0, 0, 0, 0, 0);
> +        } else if (i == 1) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
> +                                     0, 0, 0, 0, 0, 0);
> +        }
> +
> +        /* Error Status Block Length */
> +        build_append_int_noprefix(table_data,
> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
> +
> +        /* Build Read ACK register
> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
> +         * version 2 (GHESv2 - Type 10)
> +         */
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
> +
> +        /* Build Read Ack Preserve and Read Ack Writer */
> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
> +    }
> +
> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> +        /* Patch address of Error Status Data Block into
> +         * the error_block_address of hardware_errors fw_cfg blob
> +         */
> +        bios_linker_loader_add_pointer(linker,
> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
> +
> +    /* write address of hardware_errors fw_cfg blob into the
> +     * hardware_errors_addr fw_cfg blob.
> +     */
> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
> +
> +    build_header(linker, table_data,
> +        (void *)(table_data->data + length), "HEST",
> +        table_data->len - length, 1, NULL, "GHES");
> +}
> +
> +static GhesState ges;
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
> +{
> +
> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
> +
> +    /* Create a read-only fw_cfg file for GHES */
> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
> +                    request_block_size);
> +
> +    /* Create a read-write fw_cfg file for Address */
> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
> +}
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index ce90970..3f2b84f 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
>      tables->table_data = g_array_new(false, true /* clear */, 1);
>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
>      tables->linker = bios_linker_loader_init();
>  }
>  
> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
>      g_array_free(tables->table_data, true);
>      g_array_free(tables->tcpalog, mfre);
>      g_array_free(tables->vmgenid, mfre);
> +    g_array_free(tables->hardware_errors, mfre);
>  }
>  
>  /*
> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
> index bf9c0bc..54f4ba5 100644
> --- a/hw/arm/virt-acpi-build.c
> +++ b/hw/arm/virt-acpi-build.c
> @@ -45,6 +45,7 @@
>  #include "hw/arm/virt.h"
>  #include "sysemu/numa.h"
>  #include "kvm_arm.h"
> +#include "hw/acpi/acpi_ghes.h"
>  
>  #define ARM_SPI_BASE 32
>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
>      acpi_add_table(table_offsets, tables_blob);
>      build_spcr(tables_blob, tables->linker, vms);
>  
> +    if (!vmc->no_ras) {
> +        acpi_add_table(table_offsets, tables_blob);
> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
> +    }
> +
>      if (nb_numa_nodes > 0) {
>          acpi_add_table(table_offsets, tables_blob);
>          build_srat(tables_blob, tables->linker, vms);
> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
>  
>  void virt_acpi_setup(VirtMachineState *vms)
>  {
> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
>      AcpiBuildTables tables;
>      AcpiBuildState *build_state;
>  
> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
>                      acpi_data_len(tables.tcpalog));
>  
> +    if (!vmc->no_ras) {
> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
> +    }
> +
>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
>                                                ACPI_BUILD_RSDP_FILE, 0);
>  
> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> new file mode 100644
> index 0000000..38fd87c
> --- /dev/null
> +++ b/include/hw/acpi/acpi_ghes.h
> @@ -0,0 +1,79 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef ACPI_GHES_H
> +#define ACPI_GHES_H
> +
> +#include "hw/acpi/bios-linker-loader.h"
> +
> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
> +
> +/* The size of Address field in Generic Address Structure,
> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
> + */
> +#define GHES_ADDRESS_SIZE           8
> +
> +#define GHES_DATA_LENGTH            72
> +#define GHES_CPER_LENGTH            80


Don't see above two used anywhere in this patch.
Pls move to patch that uses them.

> +
> +#define ReadAckPreserve             0xfffffffe
> +#define ReadAckWrite                0x1

Why are these macros not all upper case?
I suggest just open-coding at use point
and adding a comment:

        build_append_int_noprefix(table_data, cpu_to_le64(0xfffffffe), 8); /* ReadAckPreserve */
        build_append_int_noprefix(table_data, cpu_to_le64(0x1), 8); /* ReadAckWrite */

> +
> +/* The max size in bytes for one error block */
> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> + */
> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2


Please prefix everything with acpi_ghes_

Or move to acpi/ghes.h and then prefix with ghes_ 

> +
> +/*
> + * | +--------------------------+ 0
> + * | |        Header            |
> + * | +--------------------------+ 40---+-
> + * | | .................        |      |
> + * | | error_status_address-----+ 60   |
> + * | | .................        |      |
> + * | | read_ack_register--------+ 104  92
> + * | | read_ack_preserve        |      |
> + * | | read_ack_write           |      |
> + * + +--------------------------+ 132--+-
> + *
> + * From above GHES definition, the error status address offset is 60;
> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
> + */
> +
> +/* The error status address offset in GHES */
> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +/* The read Ack register offset in GHES */
> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +typedef struct GhesState {
> +    uint64_t ghes_addr_le;
> +} GhesState;
> +
> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
> +                     BIOSLinker *linker);
> +
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> +#endif
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index 1ec7e1b..78c0252 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
>      GArray *rsdp;
>      GArray *tcpalog;
>      GArray *vmgenid;
> +    GArray *hardware_errors;
>      BIOSLinker *linker;
>  } AcpiBuildTables;
>  
> -- 
> 1.8.3.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition Dongjiu Geng
@ 2019-05-29  3:40   ` Michael S. Tsirkin
  2019-05-30 14:58     ` gengdongjiu
  2019-06-20 12:10   ` Igor Mammedov
  1 sibling, 1 reply; 45+ messages in thread
From: Michael S. Tsirkin @ 2019-05-29  3:40 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mtosatti, qemu-devel, linuxarm,
	shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	jonathan.cameron, imammedo, pbonzini, xuwei5, lersek, rth

On Tue, May 14, 2019 at 04:18:15AM -0700, Dongjiu Geng wrote:
> Add Generic Error Status Block structures and some macros
> definitions, which is referred to the ACPI 4.0 or ACPI 6.2. The
> HEST table generation and CPER record will use them.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>

Are these all still used? I'd rather you moved stuff to
where it's used.



> ---
>  include/hw/acpi/acpi-defs.h | 52 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 52 insertions(+)
> 
> diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
> index f9aa4bd..d1996fb 100644
> --- a/include/hw/acpi/acpi-defs.h
> +++ b/include/hw/acpi/acpi-defs.h
> @@ -224,6 +224,25 @@ typedef struct AcpiMultipleApicTable AcpiMultipleApicTable;
>  #define ACPI_APIC_RESERVED              16   /* 16 and greater are reserved */
>  
>  /*
> + * Values for Hardware Error Notification Type field
> + */
> +enum AcpiHestNotifyType {
> +    ACPI_HEST_NOTIFY_POLLED = 0,
> +    ACPI_HEST_NOTIFY_EXTERNAL = 1,
> +    ACPI_HEST_NOTIFY_LOCAL = 2,
> +    ACPI_HEST_NOTIFY_SCI = 3,
> +    ACPI_HEST_NOTIFY_NMI = 4,
> +    ACPI_HEST_NOTIFY_CMCI = 5,  /* ACPI 5.0: 18.3.2.7, Table 18-290 */
> +    ACPI_HEST_NOTIFY_MCE = 6,   /* ACPI 5.0: 18.3.2.7, Table 18-290 */
> +    ACPI_HEST_NOTIFY_GPIO = 7,  /* ACPI 6.0: 18.3.2.7, Table 18-332 */
> +    ACPI_HEST_NOTIFY_SEA = 8,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_SEI = 9,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_GSIV = 10, /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_SDEI = 11, /* ACPI 6.2: 18.3.2.9, Table 18-383 */
> +    ACPI_HEST_NOTIFY_RESERVED = 12 /* 12 and greater are reserved */
> +};
> +

If there's a single user, the best thing to do
is just to open-code with a comment that matches
spec names. It's hard to find stuff like ACPI_HEST_NOTIFY_GSIV
in a spec.

> +/*
>   * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
>   */
>  #define ACPI_SUB_HEADER_DEF   /* Common ACPI sub-structure header */\
> @@ -400,6 +419,39 @@ struct AcpiSystemResourceAffinityTable {
>  } QEMU_PACKED;
>  typedef struct AcpiSystemResourceAffinityTable AcpiSystemResourceAffinityTable;
>  
> +/*
> + * Generic Error Status Block
> + */
> +struct AcpiGenericErrorStatus {
> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
> +    uint32_t block_status;
> +    uint32_t raw_data_offset;
> +    uint32_t raw_data_length;
> +    uint32_t data_length;
> +    uint32_t error_severity;
> +} QEMU_PACKED;
> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;
> +
> +/*
> + * Masks for block_status flags above
> + */
> +#define ACPI_GEBS_UNCORRECTABLE         1
> +
> +/*
> + * Values for error_severity field above
> + */
> +enum AcpiGenericErrorSeverity {
> +    ACPI_CPER_SEV_RECOVERABLE,
> +    ACPI_CPER_SEV_FATAL,
> +    ACPI_CPER_SEV_CORRECTED,
> +    ACPI_CPER_SEV_NONE,
> +};
> +
> +/*
> + * Generic Hardware Error Source version 2
> + */
> +#define ACPI_HEST_SOURCE_GENERIC_ERROR_V2    10
> +
>  #define ACPI_SRAT_PROCESSOR_APIC     0
>  #define ACPI_SRAT_MEMORY             1
>  #define ACPI_SRAT_PROCESSOR_x2APIC   2
> -- 
> 1.8.3.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-05-29  3:37   ` Michael S. Tsirkin
@ 2019-05-30 14:47     ` gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-05-30 14:47 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: peter.maydell, ehabkost, kvm, mtosatti, qemu-devel, linuxarm,
	shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	jonathan.cameron, imammedo, pbonzini, xuwei5, lersek, rth

Hi Michael,
   Thanks for the review.

On 2019/5/29 11:37, Michael S. Tsirkin wrote:
> On Tue, May 14, 2019 at 04:18:20AM -0700, Dongjiu Geng wrote:
>> This implements APEI GHES Table generation via fw_cfg blobs.
>> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
>> source. Afterwards, we can extend the supported types if needed. For the
>> CPER section type, currently it is memory section because kernel
>> mainly wants userspace to handle the memory errors.
>>
>> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
>> table, for the detailed information, please refer to document:
>> docs/specs/acpi_hest_ghes.txt
>>
>> Suggested-by: Laszlo Ersek <lersek@redhat.com>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  default-configs/arm-softmmu.mak |   1 +
>>  hw/acpi/Kconfig                 |   4 +
>>  hw/acpi/Makefile.objs           |   1 +
>>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
>>  hw/acpi/aml-build.c             |   2 +
>>  hw/arm/virt-acpi-build.c        |  12 +++
>>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
>>  include/hw/acpi/aml-build.h     |   1 +
>>  8 files changed, 271 insertions(+)
>>  create mode 100644 hw/acpi/acpi_ghes.c
>>  create mode 100644 include/hw/acpi/acpi_ghes.h
>>
>> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
>> index 613d19a..7b33ae9 100644
>> --- a/default-configs/arm-softmmu.mak
>> +++ b/default-configs/arm-softmmu.mak
>> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
>>  
>>  # for realview and versatilepb
>>  CONFIG_LSI_SCSI_PCI=y
>> +CONFIG_ACPI_APEI=y
>> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
>> index eca3bee..5228a4b 100644
>> --- a/hw/acpi/Kconfig
>> +++ b/hw/acpi/Kconfig
>> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
>>      bool
>>      depends on ACPI
>>  
>> +config ACPI_APEI
>> +    bool
>> +    depends on ACPI
>> +
>>  config ACPI_VMGENID
>>      bool
>>      default y
>> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
>> index 2d46e37..5099ada 100644
>> --- a/hw/acpi/Makefile.objs
>> +++ b/hw/acpi/Makefile.objs
>> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
>>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
>>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
>>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
>> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
>>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
>>  
>>  common-obj-y += acpi_interface.o
>> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
>> new file mode 100644
>> index 0000000..d03e797
>> --- /dev/null
>> +++ b/hw/acpi/acpi_ghes.c
>> @@ -0,0 +1,171 @@
>> +/* Support for generating APEI tables and record CPER for Guests
>> + *
>> + * Copyright (C) 2017 HuaWei Corporation.
>> + *
>> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation; either version 2 of the License, or
>> + * (at your option) any later version.
>> +
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> +
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "hw/acpi/acpi.h"
>> +#include "hw/acpi/aml-build.h"
>> +#include "hw/acpi/acpi_ghes.h"
>> +#include "hw/nvram/fw_cfg.h"
>> +#include "sysemu/sysemu.h"
>> +#include "qemu/error-report.h"
>> +
>> +/* Build table for the hardware error fw_cfg blob */
>> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
>> +{
>> +    int i;
>> +
>> +    /*
>> +     * | +--------------------------+
>> +     * | |    error_block_address   |
>> +     * | |      ..........          |
>> +     * | +--------------------------+
>> +     * | |    read_ack_register     |
>> +     * | |     ...........          |
>> +     * | +--------------------------+
>> +     * | |  Error Status Data Block |
>> +     * | |      ........            |
>> +     * | +--------------------------+
>> +     */
>> +
>> +    /* Build error_block_address */
>> +    build_append_int_noprefix((void *)hardware_errors, 0,
>> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);
> 
> Why are you casting everything to void *?
> 
> C type safety goes out the window ...
  I will remove the "void *" transform.

> 
>> +
>> +    /* Build read_ack_register */
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
>> +        /* Initialize the value of read_ack_register to 1, so GHES can be
>> +         * writeable in the first time
>> +         */
>> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
>> +
>> +     /* Build Error Status Data Block */
>> +    build_append_int_noprefix((void *)hardware_errors, 0,
>> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
>> +
>> +    /* Allocate guest memory for the hardware error fw_cfg blob */
>> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
>> +                            1, false);
>> +}
>> +
>> +/* Build Hardware Error Source Table */
>> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
>> +                                            BIOSLinker *linker)
>> +{
>> +    uint32_t i, error_status_block_offset, length = table_data->len;
>> +
>> +    /* Reserve Hardware Error Source Table header size */
>> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
>> +
>> +    /* Set the error source counts */
>> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
>> +
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
>> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
>> +         */
>> +        build_append_int_noprefix(table_data,
>> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
>> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
>> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
>> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
>> +
>> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
>> +
>> +        /* Number of Records To Pre-allocate */
>> +        build_append_int_noprefix(table_data, 1, 4);
>> +        /* Max Sections Per Record */
>> +        build_append_int_noprefix(table_data, 1, 4);
>> +        /* Max Raw Data Length */
>> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
>> +
>> +        /* Build error status address*/
>> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
>> +        bios_linker_loader_add_pointer(linker,
>> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
>> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
>> +
>> +        /* Build Hardware Error Notification
>> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
>> +         */
>> +        if (i == 0) {
>> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
>> +                                     0, 0, 0, 0, 0, 0, 0);
>> +        } else if (i == 1) {
>> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
>> +                                     0, 0, 0, 0, 0, 0);
>> +        }
>> +
>> +        /* Error Status Block Length */
>> +        build_append_int_noprefix(table_data,
>> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
>> +
>> +        /* Build Read ACK register
>> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
>> +         * version 2 (GHESv2 - Type 10)
>> +         */
>> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
>> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
>> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
>> +            GHES_ERRORS_FW_CFG_FILE,
>> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
>> +
>> +        /* Build Read Ack Preserve and Read Ack Writer */
>> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
>> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
>> +    }
>> +
>> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
>> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
>> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
>> +
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
>> +        /* Patch address of Error Status Data Block into
>> +         * the error_block_address of hardware_errors fw_cfg blob
>> +         */
>> +        bios_linker_loader_add_pointer(linker,
>> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
>> +            GHES_ERRORS_FW_CFG_FILE,
>> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
>> +
>> +    /* write address of hardware_errors fw_cfg blob into the
>> +     * hardware_errors_addr fw_cfg blob.
>> +     */
>> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
>> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
>> +
>> +    build_header(linker, table_data,
>> +        (void *)(table_data->data + length), "HEST",
>> +        table_data->len - length, 1, NULL, "GHES");
>> +}
>> +
>> +static GhesState ges;
>> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
>> +{
>> +
>> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
>> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
>> +
>> +    /* Create a read-only fw_cfg file for GHES */
>> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
>> +                    request_block_size);
>> +
>> +    /* Create a read-write fw_cfg file for Address */
>> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
>> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
>> +}
>> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>> index ce90970..3f2b84f 100644
>> --- a/hw/acpi/aml-build.c
>> +++ b/hw/acpi/aml-build.c
>> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
>>      tables->table_data = g_array_new(false, true /* clear */, 1);
>>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
>>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
>> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
>>      tables->linker = bios_linker_loader_init();
>>  }
>>  
>> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
>>      g_array_free(tables->table_data, true);
>>      g_array_free(tables->tcpalog, mfre);
>>      g_array_free(tables->vmgenid, mfre);
>> +    g_array_free(tables->hardware_errors, mfre);
>>  }
>>  
>>  /*
>> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
>> index bf9c0bc..54f4ba5 100644
>> --- a/hw/arm/virt-acpi-build.c
>> +++ b/hw/arm/virt-acpi-build.c
>> @@ -45,6 +45,7 @@
>>  #include "hw/arm/virt.h"
>>  #include "sysemu/numa.h"
>>  #include "kvm_arm.h"
>> +#include "hw/acpi/acpi_ghes.h"
>>  
>>  #define ARM_SPI_BASE 32
>>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
>> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
>>      acpi_add_table(table_offsets, tables_blob);
>>      build_spcr(tables_blob, tables->linker, vms);
>>  
>> +    if (!vmc->no_ras) {
>> +        acpi_add_table(table_offsets, tables_blob);
>> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
>> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
>> +    }
>> +
>>      if (nb_numa_nodes > 0) {
>>          acpi_add_table(table_offsets, tables_blob);
>>          build_srat(tables_blob, tables->linker, vms);
>> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
>>  
>>  void virt_acpi_setup(VirtMachineState *vms)
>>  {
>> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
>>      AcpiBuildTables tables;
>>      AcpiBuildState *build_state;
>>  
>> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
>>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
>>                      acpi_data_len(tables.tcpalog));
>>  
>> +    if (!vmc->no_ras) {
>> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
>> +    }
>> +
>>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
>>                                                ACPI_BUILD_RSDP_FILE, 0);
>>  
>> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
>> new file mode 100644
>> index 0000000..38fd87c
>> --- /dev/null
>> +++ b/include/hw/acpi/acpi_ghes.h
>> @@ -0,0 +1,79 @@
>> +/* Support for generating APEI tables and record CPER for Guests
>> + *
>> + * Copyright (C) 2017 HuaWei Corporation.
>> + *
>> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation; either version 2 of the License, or
>> + * (at your option) any later version.
>> +
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> +
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#ifndef ACPI_GHES_H
>> +#define ACPI_GHES_H
>> +
>> +#include "hw/acpi/bios-linker-loader.h"
>> +
>> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
>> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
>> +
>> +/* The size of Address field in Generic Address Structure,
>> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
>> + */
>> +#define GHES_ADDRESS_SIZE           8
>> +
>> +#define GHES_DATA_LENGTH            72
>> +#define GHES_CPER_LENGTH            80
> 
> 
> Don't see above two used anywhere in this patch.
> Pls move to patch that uses them.
yes, thanks for the reminder. I use above two variables in another patch, not in this patch,
I will move them to another patch.

> 
>> +
>> +#define ReadAckPreserve             0xfffffffe
>> +#define ReadAckWrite                0x1
> 
> Why are these macros not all upper case?
> I suggest just open-coding at use point
> and adding a comment:
> 
>         build_append_int_noprefix(table_data, cpu_to_le64(0xfffffffe), 8); /* ReadAckPreserve */
>         build_append_int_noprefix(table_data, cpu_to_le64(0x1), 8); /* ReadAckWrite */
Ok, I will change them, thanks.

> 
>> +
>> +/* The max size in bytes for one error block */
>> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
>> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
>> + */
>> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2>
> 
> Please prefix everything with acpi_ghes_
> 
> Or move to acpi/ghes.h and then prefix with ghes_ 
ok, I will do it.

> 
>> +
>> +/*
>> + * | +--------------------------+ 0
>> + * | |        Header            |
>> + * | +--------------------------+ 40---+-
>> + * | | .................        |      |
>> + * | | error_status_address-----+ 60   |
>> + * | | .................        |      |
>> + * | | read_ack_register--------+ 104  92
>> + * | | read_ack_preserve        |      |
>> + * | | read_ack_write           |      |
>> + * + +--------------------------+ 132--+-
>> + *
>> + * From above GHES definition, the error status address offset is 60;
>> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
>> + */
>> +
>> +/* The error status address offset in GHES */
>> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
>> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
>> +
>> +/* The read Ack register offset in GHES */
>> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
>> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
>> +
>> +typedef struct GhesState {
>> +    uint64_t ghes_addr_le;
>> +} GhesState;
>> +
>> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
>> +                     BIOSLinker *linker);
>> +
>> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
>> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
>> +#endif
>> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
>> index 1ec7e1b..78c0252 100644
>> --- a/include/hw/acpi/aml-build.h
>> +++ b/include/hw/acpi/aml-build.h
>> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
>>      GArray *rsdp;
>>      GArray *tcpalog;
>>      GArray *vmgenid;
>> +    GArray *hardware_errors;
>>      BIOSLinker *linker;
>>  } AcpiBuildTables;
>>  
>> -- 
>> 1.8.3.1
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-05-29  3:40   ` Michael S. Tsirkin
@ 2019-05-30 14:58     ` gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-05-30 14:58 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: peter.maydell, ehabkost, kvm, mtosatti, qemu-devel, linuxarm,
	shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	jonathan.cameron, imammedo, pbonzini, xuwei5, lersek, rth


On 2019/5/29 11:40, Michael S. Tsirkin wrote:
> On Tue, May 14, 2019 at 04:18:15AM -0700, Dongjiu Geng wrote:
>> Add Generic Error Status Block structures and some macros
>> definitions, which is referred to the ACPI 4.0 or ACPI 6.2. The
>> HEST table generation and CPER record will use them.
>>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> 
> Are these all still used? I'd rather you moved stuff to
> where it's used.
Ok, I will move them, thanks

> 
> 
> 
>> ---
>>  include/hw/acpi/acpi-defs.h | 52 +++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 52 insertions(+)
>>
>> diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
>> index f9aa4bd..d1996fb 100644
>> --- a/include/hw/acpi/acpi-defs.h
>> +++ b/include/hw/acpi/acpi-defs.h
>> @@ -224,6 +224,25 @@ typedef struct AcpiMultipleApicTable AcpiMultipleApicTable;
>>  #define ACPI_APIC_RESERVED              16   /* 16 and greater are reserved */
>>  
>>  /*
>> + * Values for Hardware Error Notification Type field
>> + */
>> +enum AcpiHestNotifyType {
>> +    ACPI_HEST_NOTIFY_POLLED = 0,
>> +    ACPI_HEST_NOTIFY_EXTERNAL = 1,
>> +    ACPI_HEST_NOTIFY_LOCAL = 2,
>> +    ACPI_HEST_NOTIFY_SCI = 3,
>> +    ACPI_HEST_NOTIFY_NMI = 4,
>> +    ACPI_HEST_NOTIFY_CMCI = 5,  /* ACPI 5.0: 18.3.2.7, Table 18-290 */
>> +    ACPI_HEST_NOTIFY_MCE = 6,   /* ACPI 5.0: 18.3.2.7, Table 18-290 */
>> +    ACPI_HEST_NOTIFY_GPIO = 7,  /* ACPI 6.0: 18.3.2.7, Table 18-332 */
>> +    ACPI_HEST_NOTIFY_SEA = 8,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
>> +    ACPI_HEST_NOTIFY_SEI = 9,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
>> +    ACPI_HEST_NOTIFY_GSIV = 10, /* ACPI 6.1: 18.3.2.9, Table 18-345 */
>> +    ACPI_HEST_NOTIFY_SDEI = 11, /* ACPI 6.2: 18.3.2.9, Table 18-383 */
>> +    ACPI_HEST_NOTIFY_RESERVED = 12 /* 12 and greater are reserved */
>> +};
>> +
> 
> If there's a single user, the best thing to do
> is just to open-code with a comment that matches
> spec names. It's hard to find stuff like ACPI_HEST_NOTIFY_GSIV
> in a spec.
ok, I will do it, thanks

> 
>> +/*
>>   * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
>>   */
>>  #define ACPI_SUB_HEADER_DEF   /* Common ACPI sub-structure header */\
>> @@ -400,6 +419,39 @@ struct AcpiSystemResourceAffinityTable {
>>  } QEMU_PACKED;
>>  typedef struct AcpiSystemResourceAffinityTable AcpiSystemResourceAffinityTable;
>>  
>> +/*
>> + * Generic Error Status Block
>> + */
>> +struct AcpiGenericErrorStatus {
>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
>> +    uint32_t block_status;
>> +    uint32_t raw_data_offset;
>> +    uint32_t raw_data_length;
>> +    uint32_t data_length;
>> +    uint32_t error_severity;
>> +} QEMU_PACKED;
>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;
>> +
>> +/*
>> + * Masks for block_status flags above
>> + */
>> +#define ACPI_GEBS_UNCORRECTABLE         1
>> +
>> +/*
>> + * Values for error_severity field above
>> + */
>> +enum AcpiGenericErrorSeverity {
>> +    ACPI_CPER_SEV_RECOVERABLE,
>> +    ACPI_CPER_SEV_FATAL,
>> +    ACPI_CPER_SEV_CORRECTED,
>> +    ACPI_CPER_SEV_NONE,
>> +};
>> +
>> +/*
>> + * Generic Hardware Error Source version 2
>> + */
>> +#define ACPI_HEST_SOURCE_GENERIC_ERROR_V2    10
>> +
>>  #define ACPI_SRAT_PROCESSOR_APIC     0
>>  #define ACPI_SRAT_MEMORY             1
>>  #define ACPI_SRAT_PROCESSOR_x2APIC   2
>> -- 
>> 1.8.3.1
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM Dongjiu Geng
@ 2019-06-06 13:31   ` Jonathan Cameron
  2019-06-24 13:08   ` Igor Mammedov
  1 sibling, 0 replies; 45+ messages in thread
From: Jonathan Cameron @ 2019-06-06 13:31 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, imammedo, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:23 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> Add SIGBUS signal handler. In this handler, it checks the SIGBUS type,
> translates the host VA delivered by host to guest PA, then fill this PA
> to guest APEI GHES memory, then notify guest according to the SIGBUS type.
> 
> If guest accesses the poisoned memory, it generates Synchronous External
> Abort(SEA). Then host kernel gets an APEI notification and call memory_failure()
> to unmapped the affected page for the guest's stage 2, finally return
> to guest.
> 
> Guest continues to access PG_hwpoison page, it will trap to KVM as stage2 fault,
> then a SIGBUS_MCEERR_AR synchronous signal is delivered to Qemu, Qemu record this
> error address into guest APEI GHES memory and notify guest using
> Synchronous-External-Abort(SEA).
> 
> Suggested-by: James Morse <james.morse@arm.com>
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
Hi Dongjiu,

Good to see this moving forwards again.

A few really minor things inline.

Thanks,

Jonathan

> ---
>  hw/acpi/acpi_ghes.c         | 177 ++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/acpi/acpi_ghes.h |   6 +-
>  include/sysemu/kvm.h        |   2 +-
>  target/arm/kvm64.c          |  39 ++++++++++
>  4 files changed, 222 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> index d03e797..06b7374 100644
> --- a/hw/acpi/acpi_ghes.c
> +++ b/hw/acpi/acpi_ghes.c
> @@ -26,6 +26,101 @@
>  #include "sysemu/sysemu.h"
>  #include "qemu/error-report.h"
>  
> +/* UEFI 2.6: N.2.5 Memory Error Section */
> +static void build_append_mem_cper(GArray *table, uint64_t error_physical_addr)
> +{
> +    /*
> +     * Memory Error Record
> +     */
> +    build_append_int_noprefix(table,
> +                 (1UL << 14) | /* Type Valid */
> +                 (1UL << 1) /* Physical Address Valid */,
> +                 8);
> +    /* Memory error status information */
> +    build_append_int_noprefix(table, 0, 8);
> +    /* The physical address at which the memory error occurred */
> +    build_append_int_noprefix(table, error_physical_addr, 8);
> +    build_append_int_noprefix(table, 0, 48);

This could do with a comment to say we are basically skipping all the
detailed information normally found in such a record.


> +    build_append_int_noprefix(table, 0 /* Unknown error */, 1);
> +    build_append_int_noprefix(table, 0, 7);
A similar comment for this last section would probably use useful as well.

> +}
> +
> +static int ghes_record_mem_error(uint64_t error_block_address,
> +                                    uint64_t error_physical_addr)
> +{
> +    GArray *block;
> +    uint64_t current_block_length;
> +    uint32_t data_length;
> +    /* Memory section */
The variable name is clear I think, so not sure this comment adds any information.

> +    char mem_section_id_le[] = {0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE,
> +                                0x4E, 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C,
> +                                0x83, 0xB1};
> +    uint8_t fru_id[16] = {0};
> +    uint8_t fru_text[20] = {0};
> +
> +    /* Generic Error Status Block
> +     * | +---------------------+
> +     * | |     block_status    |
> +     * | +---------------------+
> +     * | |    raw_data_offset  |
> +     * | +---------------------+
> +     * | |    raw_data_length  |
> +     * | +---------------------+
> +     * | |     data_length     |
> +     * | +---------------------+
> +     * | |   error_severity    |
> +     * | +---------------------+
> +     */
> +    block = g_array_new(false, true /* clear */, 1);
> +
> +    /* Get the length of the Generic Error Data Entries */
> +    cpu_physical_memory_read(error_block_address +
> +        offsetof(AcpiGenericErrorStatus, data_length), &data_length, 4);
> +
> +    /* The current whole length of the generic error status block */
> +    current_block_length = sizeof(AcpiGenericErrorStatus) + le32_to_cpu(data_length);
> +
> +    /* This is the length if adding a new generic error data entry*/
> +    data_length += GHES_DATA_LENGTH;
> +    data_length += GHES_MEM_CPER_LENGTH;
> +
> +    /* Check whether it will run out of the preallocated memory if adding a new
> +     * generic error data entry
> +     */
> +    if ((data_length + sizeof(AcpiGenericErrorStatus)) > GHES_MAX_RAW_DATA_LENGTH) {
> +        error_report("Record CPER out of boundary!!!");
> +        return GHES_CPER_FAIL;
> +    }
> +
> +    /* Build the new generic error status block header */
> +    build_append_ghes_generic_status(block, cpu_to_le32(ACPI_GEBS_UNCORRECTABLE), 0, 0,
> +        cpu_to_le32(data_length), cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE));
> +
> +    /* Write back above generic error status block header to guest memory */
> +    cpu_physical_memory_write(error_block_address, block->data,
> +                              block->len);
> +
> +    /* Add a new generic error data entry */
> +
> +    data_length = block->len;
> +    /* Build this new generic error data entry header */
> +    build_append_ghes_generic_data(block, mem_section_id_le,
> +                    cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE), cpu_to_le32(0x300), 0, 0,
> +                    cpu_to_le32(80)/* the total size of Memory Error Record */, fru_id,

Use the define for that 80?

> +                    fru_text, 0);
> +
> +    /* Build the memory section CPER for above new generic error data entry */
> +    build_append_mem_cper(block, error_physical_addr);
> +
> +    /* Write back above this new generic error data entry to guest memory */
> +    cpu_physical_memory_write(error_block_address + current_block_length,
> +                    block->data + data_length, block->len - data_length);
> +
> +    g_array_free(block, true);
> +
> +    return GHES_CPER_OK;
> +}
> +
>  /* Build table for the hardware error fw_cfg blob */
>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
>  {
> @@ -169,3 +264,85 @@ void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
>      fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
>          &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
>  }
> +
> +bool ghes_record_errors(uint32_t notify, uint64_t physical_address)
> +{
> +    uint64_t error_block_addr, read_ack_register_addr;
> +    int read_ack_register = 0, loop = 0;
> +    uint64_t start_addr = le32_to_cpu(ges.ghes_addr_le);
> +    bool ret = GHES_CPER_FAIL;
> +    const uint8_t error_source_id[] = { 0xff, 0xff, 0xff, 0xff,
> +                                        0xff, 0xff, 0xff, 0, 1};
> +
> +    /*
> +     * | +---------------------+ ges.ghes_addr_le
> +     * | |error_block_address0 |
> +     * | +---------------------+ --+--
> +     * | |    .............    | GHES_ADDRESS_SIZE
> +     * | +---------------------+ --+--
> +     * | |error_block_addressN |
> +     * | +---------------------+
> +     * | | read_ack_register0  |
> +     * | +---------------------+ --+--
> +     * | |   .............     | GHES_ADDRESS_SIZE
> +     * | +---------------------+ --+--
> +     * | | read_ack_registerN  |
> +     * | +---------------------+ --+--
> +     * | |      CPER           |   |
> +     * | |      ....           | GHES_MAX_RAW_DATA_LENGT
> +     * | |      CPER           |   |
> +     * | +---------------------+ --+--
> +     * | |    ..........       |
> +     * | +---------------------+
> +     * | |      CPER           |
> +     * | |      ....           |
> +     * | |      CPER           |
> +     * | +---------------------+
> +     */
> +    if (physical_address && notify < ACPI_HEST_NOTIFY_RESERVED) {
> +        /* Find and check the source id for this new CPER */
> +        if (error_source_id[notify] != 0xff) {
> +            start_addr += error_source_id[notify] * GHES_ADDRESS_SIZE;
> +        } else {
> +            goto out;
> +        }
> +
> +        cpu_physical_memory_read(start_addr, &error_block_addr,
> +                                    GHES_ADDRESS_SIZE);
> +
> +        read_ack_register_addr = start_addr +
> +                        ACPI_HEST_ERROR_SOURCE_COUNT * GHES_ADDRESS_SIZE;
> +retry:
> +        cpu_physical_memory_read(read_ack_register_addr,
> +                                 &read_ack_register, GHES_ADDRESS_SIZE);
> +
> +        /* zero means OSPM does not acknowledge the error */
> +        if (!read_ack_register) {
> +            if (loop < 3) {
> +                usleep(100 * 1000);
> +                loop++;
> +                goto retry;
> +            } else {
> +                error_report("OSPM does not acknowledge previous error,"
> +                    " so can not record CPER for current error, forcibly acknowledge"
> +                    " previous error to avoid blocking next time CPER record! Exit");
> +                read_ack_register = 1;
> +                cpu_physical_memory_write(read_ack_register_addr,
> +                    &read_ack_register, GHES_ADDRESS_SIZE);
> +            }
> +        } else {
> +            if (error_block_addr) {
> +                read_ack_register = 0;
> +                /* Clear the Read Ack Register, OSPM will write it to 1 when
> +                 * acknowledge this error.
> +                 */
> +                cpu_physical_memory_write(read_ack_register_addr,
> +                    &read_ack_register, GHES_ADDRESS_SIZE);
> +                ret = ghes_record_mem_error(error_block_addr, physical_address);
> +            }
> +        }
> +    }
> +
> +out:
> +    return ret;
> +}
> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> index 38fd87c..6b38097 100644
> --- a/include/hw/acpi/acpi_ghes.h
> +++ b/include/hw/acpi/acpi_ghes.h
> @@ -32,11 +32,14 @@
>  #define GHES_ADDRESS_SIZE           8
>  
>  #define GHES_DATA_LENGTH            72
> -#define GHES_CPER_LENGTH            80
> +#define GHES_MEM_CPER_LENGTH        80

This is a good change to make, but please roll it into patch 7 where
this was introduced rather than introducing it only to rename later.

Actually it would be even better to just not introduce it in patch 7
and bring it in for the first time in this patch.

>  
>  #define ReadAckPreserve             0xfffffffe
>  #define ReadAckWrite                0x1
>  
> +#define GHES_CPER_OK                1
> +#define GHES_CPER_FAIL              0
> +
>  /* The max size in bytes for one error block */
>  #define GHES_MAX_RAW_DATA_LENGTH        0x1000
>  /* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> @@ -76,4 +79,5 @@ void build_apei_hest(GArray *table_data, GArray *hardware_error,
>  
>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
>  void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> +bool ghes_record_errors(uint32_t notify, uint64_t error_physical_addr);
>  #endif
> diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
> index a6d1cd1..1d1a7a8 100644
> --- a/include/sysemu/kvm.h
> +++ b/include/sysemu/kvm.h
> @@ -377,7 +377,7 @@ bool kvm_vcpu_id_is_valid(int vcpu_id);
>  /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
>  unsigned long kvm_arch_vcpu_id(CPUState *cpu);
>  
> -#ifdef TARGET_I386
> +#if defined(TARGET_I386) || defined(TARGET_AARCH64)
>  #define KVM_HAVE_MCE_INJECTION 1
>  void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
>  #endif
> diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
> index c7bdc6a..d2eac28 100644
> --- a/target/arm/kvm64.c
> +++ b/target/arm/kvm64.c
> @@ -27,6 +27,10 @@
>  #include "kvm_arm.h"
>  #include "internals.h"
>  #include "hw/arm/arm.h"
> +#include "exec/ram_addr.h"
> +#include "hw/acpi/acpi-defs.h"
> +#include "hw/acpi/acpi_ghes.h"
> +#include "hw/acpi/acpi.h"
>  
>  static bool have_guest_debug;
>  
> @@ -1029,6 +1033,41 @@ int kvm_arch_get_registers(CPUState *cs)
>      return ret;
>  }
>  
> +void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
> +{
> +    ram_addr_t ram_addr;
> +    hwaddr paddr;
> +
> +    assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
> +
> +    if (acpi_enabled && addr) {
> +        ram_addr = qemu_ram_addr_from_host(addr);
> +        if (ram_addr != RAM_ADDR_INVALID &&
> +            kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
> +            kvm_hwpoison_page_add(ram_addr);
> +            /* Asynchronous signal will be masked by main thread, so
> +             * only handle synchronous signal.
> +             */
> +            if (code == BUS_MCEERR_AR) {
> +                kvm_cpu_synchronize_state(c);
> +                if (GHES_CPER_FAIL != ghes_record_errors(ACPI_HEST_NOTIFY_SEA, paddr)) {
> +                    kvm_inject_arm_sea(c);
> +                } else {
> +                    fprintf(stderr, "failed to record the error\n");
> +                }
> +            }
> +            return;
> +        }
> +        fprintf(stderr, "Hardware memory error for memory used by "
> +                "QEMU itself instead of guest system!\n");
> +    }
> +
> +    if (code == BUS_MCEERR_AR) {
> +        fprintf(stderr, "Hardware memory error!\n");
> +        exit(1);
> +    }
> +}
> +
>  /* C6.6.29 BRK instruction */
>  static const uint32_t brk_insn = 0xd4200000;
>  




^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
  2019-05-29  3:37   ` Michael S. Tsirkin
@ 2019-06-06 13:43   ` Jonathan Cameron
  2019-06-24 12:27   ` Igor Mammedov
  2 siblings, 0 replies; 45+ messages in thread
From: Jonathan Cameron @ 2019-06-06 13:43 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, imammedo, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:20 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> This implements APEI GHES Table generation via fw_cfg blobs.
> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
> source. Afterwards, we can extend the supported types if needed. For the
> CPER section type, currently it is memory section because kernel
> mainly wants userspace to handle the memory errors.
> 
> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
> table, for the detailed information, please refer to document:
> docs/specs/acpi_hest_ghes.txt
> 
> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>

Just one general question, why support the GPIO-Signal error source in this
patch set?  Unless I'm missing something, it's not actually used and would
require some additional infrastructure (instantiate the GEDD or similar).

Might be better to drop it for now and simplify this set a little bit?

Note I'm using the gpio path for CCIX PER error emulation so have it hooked up.
The code here is fine, just not used (I think).

Thanks,

Jonathan



> ---
>  default-configs/arm-softmmu.mak |   1 +
>  hw/acpi/Kconfig                 |   4 +
>  hw/acpi/Makefile.objs           |   1 +
>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
>  hw/acpi/aml-build.c             |   2 +
>  hw/arm/virt-acpi-build.c        |  12 +++
>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
>  include/hw/acpi/aml-build.h     |   1 +
>  8 files changed, 271 insertions(+)
>  create mode 100644 hw/acpi/acpi_ghes.c
>  create mode 100644 include/hw/acpi/acpi_ghes.h
> 
> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
> index 613d19a..7b33ae9 100644
> --- a/default-configs/arm-softmmu.mak
> +++ b/default-configs/arm-softmmu.mak
> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
>  
>  # for realview and versatilepb
>  CONFIG_LSI_SCSI_PCI=y
> +CONFIG_ACPI_APEI=y
> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
> index eca3bee..5228a4b 100644
> --- a/hw/acpi/Kconfig
> +++ b/hw/acpi/Kconfig
> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
>      bool
>      depends on ACPI
>  
> +config ACPI_APEI
> +    bool
> +    depends on ACPI
> +
>  config ACPI_VMGENID
>      bool
>      default y
> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
> index 2d46e37..5099ada 100644
> --- a/hw/acpi/Makefile.objs
> +++ b/hw/acpi/Makefile.objs
> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
>  
>  common-obj-y += acpi_interface.o
> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> new file mode 100644
> index 0000000..d03e797
> --- /dev/null
> +++ b/hw/acpi/acpi_ghes.c
> @@ -0,0 +1,171 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/acpi/acpi.h"
> +#include "hw/acpi/aml-build.h"
> +#include "hw/acpi/acpi_ghes.h"
> +#include "hw/nvram/fw_cfg.h"
> +#include "sysemu/sysemu.h"
> +#include "qemu/error-report.h"
> +
> +/* Build table for the hardware error fw_cfg blob */
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
> +{
> +    int i;
> +
> +    /*
> +     * | +--------------------------+
> +     * | |    error_block_address   |
> +     * | |      ..........          |
> +     * | +--------------------------+
> +     * | |    read_ack_register     |
> +     * | |     ...........          |
> +     * | +--------------------------+
> +     * | |  Error Status Data Block |
> +     * | |      ........            |
> +     * | +--------------------------+
> +     */
> +
> +    /* Build error_block_address */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);
> +
> +    /* Build read_ack_register */
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> +        /* Initialize the value of read_ack_register to 1, so GHES can be
> +         * writeable in the first time
> +         */
> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
> +
> +     /* Build Error Status Data Block */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
> +
> +    /* Allocate guest memory for the hardware error fw_cfg blob */
> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
> +                            1, false);
> +}
> +
> +/* Build Hardware Error Source Table */
> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
> +                                            BIOSLinker *linker)
> +{
> +    uint32_t i, error_status_block_offset, length = table_data->len;
> +
> +    /* Reserve Hardware Error Source Table header size */
> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
> +
> +    /* Set the error source counts */
> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
> +         */
> +        build_append_int_noprefix(table_data,
> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
> +
> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
> +
> +        /* Number of Records To Pre-allocate */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Sections Per Record */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Raw Data Length */
> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
> +
> +        /* Build error status address*/
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker,
> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
> +
> +        /* Build Hardware Error Notification
> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
> +         */
> +        if (i == 0) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
> +                                     0, 0, 0, 0, 0, 0, 0);
> +        } else if (i == 1) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
> +                                     0, 0, 0, 0, 0, 0);
> +        }
> +
> +        /* Error Status Block Length */
> +        build_append_int_noprefix(table_data,
> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
> +
> +        /* Build Read ACK register
> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
> +         * version 2 (GHESv2 - Type 10)
> +         */
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
> +
> +        /* Build Read Ack Preserve and Read Ack Writer */
> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
> +    }
> +
> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> +        /* Patch address of Error Status Data Block into
> +         * the error_block_address of hardware_errors fw_cfg blob
> +         */
> +        bios_linker_loader_add_pointer(linker,
> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
> +
> +    /* write address of hardware_errors fw_cfg blob into the
> +     * hardware_errors_addr fw_cfg blob.
> +     */
> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
> +
> +    build_header(linker, table_data,
> +        (void *)(table_data->data + length), "HEST",
> +        table_data->len - length, 1, NULL, "GHES");
> +}
> +
> +static GhesState ges;
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
> +{
> +
> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
> +
> +    /* Create a read-only fw_cfg file for GHES */
> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
> +                    request_block_size);
> +
> +    /* Create a read-write fw_cfg file for Address */
> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
> +}
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index ce90970..3f2b84f 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
>      tables->table_data = g_array_new(false, true /* clear */, 1);
>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
>      tables->linker = bios_linker_loader_init();
>  }
>  
> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
>      g_array_free(tables->table_data, true);
>      g_array_free(tables->tcpalog, mfre);
>      g_array_free(tables->vmgenid, mfre);
> +    g_array_free(tables->hardware_errors, mfre);
>  }
>  
>  /*
> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
> index bf9c0bc..54f4ba5 100644
> --- a/hw/arm/virt-acpi-build.c
> +++ b/hw/arm/virt-acpi-build.c
> @@ -45,6 +45,7 @@
>  #include "hw/arm/virt.h"
>  #include "sysemu/numa.h"
>  #include "kvm_arm.h"
> +#include "hw/acpi/acpi_ghes.h"
>  
>  #define ARM_SPI_BASE 32
>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
>      acpi_add_table(table_offsets, tables_blob);
>      build_spcr(tables_blob, tables->linker, vms);
>  
> +    if (!vmc->no_ras) {
> +        acpi_add_table(table_offsets, tables_blob);
> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
> +    }
> +
>      if (nb_numa_nodes > 0) {
>          acpi_add_table(table_offsets, tables_blob);
>          build_srat(tables_blob, tables->linker, vms);
> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
>  
>  void virt_acpi_setup(VirtMachineState *vms)
>  {
> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
>      AcpiBuildTables tables;
>      AcpiBuildState *build_state;
>  
> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
>                      acpi_data_len(tables.tcpalog));
>  
> +    if (!vmc->no_ras) {
> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
> +    }
> +
>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
>                                                ACPI_BUILD_RSDP_FILE, 0);
>  
> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> new file mode 100644
> index 0000000..38fd87c
> --- /dev/null
> +++ b/include/hw/acpi/acpi_ghes.h
> @@ -0,0 +1,79 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef ACPI_GHES_H
> +#define ACPI_GHES_H
> +
> +#include "hw/acpi/bios-linker-loader.h"
> +
> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
> +
> +/* The size of Address field in Generic Address Structure,
> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
> + */
> +#define GHES_ADDRESS_SIZE           8
> +
> +#define GHES_DATA_LENGTH            72
> +#define GHES_CPER_LENGTH            80
> +
> +#define ReadAckPreserve             0xfffffffe
> +#define ReadAckWrite                0x1
> +
> +/* The max size in bytes for one error block */
> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> + */
> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2

The GPIO case is really handy when I'm abusing this code for error injection
but I don't think you actually use it in this patch set.  Perhaps it
makes sense to drop that block for now?

> +
> +/*
> + * | +--------------------------+ 0
> + * | |        Header            |
> + * | +--------------------------+ 40---+-
> + * | | .................        |      |
> + * | | error_status_address-----+ 60   |
> + * | | .................        |      |
> + * | | read_ack_register--------+ 104  92
> + * | | read_ack_preserve        |      |
> + * | | read_ack_write           |      |
> + * + +--------------------------+ 132--+-
> + *
> + * From above GHES definition, the error status address offset is 60;
> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
> + */
> +
> +/* The error status address offset in GHES */
> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +/* The read Ack register offset in GHES */
> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +typedef struct GhesState {
> +    uint64_t ghes_addr_le;
> +} GhesState;
> +
> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
> +                     BIOSLinker *linker);
> +
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> +#endif
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index 1ec7e1b..78c0252 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
>      GArray *rsdp;
>      GArray *tcpalog;
>      GArray *vmgenid;
> +    GArray *hardware_errors;
>      BIOSLinker *linker;
>  } AcpiBuildTables;
>  




^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration Dongjiu Geng
@ 2019-06-20 12:04   ` Igor Mammedov
  2019-06-24 12:19     ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-20 12:04 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:14 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> Support this feature since version 4.1, disable it by
> default in the old version.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  hw/arm/virt.c         | 6 ++++++
>  include/hw/arm/virt.h | 1 +
>  2 files changed, 7 insertions(+)
> 
> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
> index 5331ab7..7bdd41b 100644
> --- a/hw/arm/virt.c
> +++ b/hw/arm/virt.c
> @@ -2043,8 +2043,14 @@ DEFINE_VIRT_MACHINE_AS_LATEST(4, 1)
>  
>  static void virt_machine_4_0_options(MachineClass *mc)
>  {
> +    VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
> +
>      virt_machine_4_1_options(mc);
>      compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
> +    /* Disable memory recovery feature for 4.0 as RAS support was
> +     * introduced with 4.1.
> +     */
> +    vmc->no_ras = true;

So it would mean that the feature is enabled unconditionally for
new machine types and consumes resources whether user needs it or not.

In light of the race for leaner QEMU and faster startup times,
it might be better to make RAS optional and make user explicitly
enable it using a machine option.


>  }
>  DEFINE_VIRT_MACHINE(4, 0)
>  
> diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h
> index 4240709..7f1a033 100644
> --- a/include/hw/arm/virt.h
> +++ b/include/hw/arm/virt.h
> @@ -104,6 +104,7 @@ typedef struct {
>      bool disallow_affinity_adjustment;
>      bool no_its;
>      bool no_pmu;
> +    bool no_ras;
>      bool claim_edge_triggered_timers;
>      bool smbios_old_sys_ver;
>      bool no_highmem_ecam;



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition Dongjiu Geng
  2019-05-29  3:40   ` Michael S. Tsirkin
@ 2019-06-20 12:10   ` Igor Mammedov
  2019-06-20 14:04     ` gengdongjiu
  1 sibling, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-20 12:10 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:15 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> Add Generic Error Status Block structures and some macros
> definitions, which is referred to the ACPI 4.0 or ACPI 6.2. The
> HEST table generation and CPER record will use them.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  include/hw/acpi/acpi-defs.h | 52 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 52 insertions(+)
> 
> diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
> index f9aa4bd..d1996fb 100644
> --- a/include/hw/acpi/acpi-defs.h
> +++ b/include/hw/acpi/acpi-defs.h
> @@ -224,6 +224,25 @@ typedef struct AcpiMultipleApicTable AcpiMultipleApicTable;
>  #define ACPI_APIC_RESERVED              16   /* 16 and greater are reserved */
>  
>  /*
> + * Values for Hardware Error Notification Type field
> + */
> +enum AcpiHestNotifyType {
> +    ACPI_HEST_NOTIFY_POLLED = 0,
> +    ACPI_HEST_NOTIFY_EXTERNAL = 1,
> +    ACPI_HEST_NOTIFY_LOCAL = 2,
> +    ACPI_HEST_NOTIFY_SCI = 3,
> +    ACPI_HEST_NOTIFY_NMI = 4,
> +    ACPI_HEST_NOTIFY_CMCI = 5,  /* ACPI 5.0: 18.3.2.7, Table 18-290 */
> +    ACPI_HEST_NOTIFY_MCE = 6,   /* ACPI 5.0: 18.3.2.7, Table 18-290 */
> +    ACPI_HEST_NOTIFY_GPIO = 7,  /* ACPI 6.0: 18.3.2.7, Table 18-332 */
> +    ACPI_HEST_NOTIFY_SEA = 8,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_SEI = 9,   /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_GSIV = 10, /* ACPI 6.1: 18.3.2.9, Table 18-345 */
> +    ACPI_HEST_NOTIFY_SDEI = 11, /* ACPI 6.2: 18.3.2.9, Table 18-383 */
> +    ACPI_HEST_NOTIFY_RESERVED = 12 /* 12 and greater are reserved */
> +};
> +
> +/*
>   * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
>   */
>  #define ACPI_SUB_HEADER_DEF   /* Common ACPI sub-structure header */\
> @@ -400,6 +419,39 @@ struct AcpiSystemResourceAffinityTable {
>  } QEMU_PACKED;
>  typedef struct AcpiSystemResourceAffinityTable AcpiSystemResourceAffinityTable;
>  
> +/*
> + * Generic Error Status Block
> + */
> +struct AcpiGenericErrorStatus {
> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
> +    uint32_t block_status;
> +    uint32_t raw_data_offset;
> +    uint32_t raw_data_length;
> +    uint32_t data_length;
> +    uint32_t error_severity;
> +} QEMU_PACKED;
> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;

there shouldn't be packed structures,
is it a leftover from previous version?

> +
> +/*
> + * Masks for block_status flags above
> + */
> +#define ACPI_GEBS_UNCORRECTABLE         1
> +
> +/*
> + * Values for error_severity field above
> + */
> +enum AcpiGenericErrorSeverity {
> +    ACPI_CPER_SEV_RECOVERABLE,
> +    ACPI_CPER_SEV_FATAL,
> +    ACPI_CPER_SEV_CORRECTED,
> +    ACPI_CPER_SEV_NONE,
> +};
> +
> +/*
> + * Generic Hardware Error Source version 2
> + */
> +#define ACPI_HEST_SOURCE_GENERIC_ERROR_V2    10
> +
>  #define ACPI_SRAT_PROCESSOR_APIC     0
>  #define ACPI_SRAT_MEMORY             1
>  #define ACPI_SRAT_PROCESSOR_x2APIC   2



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry Dongjiu Geng
@ 2019-06-20 12:28   ` Igor Mammedov
  2019-06-24 12:37     ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-20 12:28 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:17 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> It will help to add Generic Error Data Entry to ACPI tables
> without using packed C structures and avoid endianness
> issues as API doesn't need explicit conversion.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  hw/acpi/aml-build.c         | 32 ++++++++++++++++++++++++++++++++
>  include/hw/acpi/aml-build.h |  6 ++++++
>  2 files changed, 38 insertions(+)
> 
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index fb53f21..102a288 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -296,6 +296,38 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>          build_append_int_noprefix(table, error_threshold_window, 4);
>  }
>  
> +/* Generic Error Data Entry
> + * ACPI 4.0: 17.3.2.6.1 Generic Error Data
> + */
> +void build_append_ghes_generic_data(GArray *table, const char *section_type,
s/build_append_ghes_generic_data/build_append_ghes_generic_error_data/

> +                                    uint32_t error_severity, uint16_t revision,
> +                                    uint8_t validation_bits, uint8_t flags,
> +                                    uint32_t error_data_length, uint8_t *fru_id,
> +                                    uint8_t *fru_text, uint64_t time_stamp)
checkpatch probably will complain due to too long lines
you can use:
void build_append_ghe...
         uint32_t error_severity, uint16_t revision,
         ...

> +{
> +    int i;
> +
> +    for (i = 0; i < 16; i++) {
> +        build_append_int_noprefix(table, section_type[i], 1);
                                            ^^^
use QemuUUID instead, see vmgenid_build_acpi

> +    }
> +
> +    build_append_int_noprefix(table, error_severity, 4);
> +    build_append_int_noprefix(table, revision, 2);
> +    build_append_int_noprefix(table, validation_bits, 1);
> +    build_append_int_noprefix(table, flags, 1);
> +    build_append_int_noprefix(table, error_data_length, 4);
> +
> +    for (i = 0; i < 16; i++) {
> +        build_append_int_noprefix(table, fru_id[i], 1);
same as section_type

> +    }
> +
> +    for (i = 0; i < 20; i++) {
> +        build_append_int_noprefix(table, fru_text[i], 1);
> +    }
instead of loop use g_array_insert_vals()

> +
> +    build_append_int_noprefix(table, time_stamp, 8);
that's not part of 'Table 17-13'
where does it come from?

> +}
> +
>  /*
>   * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
>   * and return the offset to 0x00000000 for runtime patching.
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index 90c8ef8..a71db2f 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -419,6 +419,12 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>                                uint32_t error_threshold_value,
>                                uint32_t error_threshold_window);
>  
> +void build_append_ghes_generic_data(GArray *table, const char *section_type,
> +                                    uint32_t error_severity, uint16_t revision,
> +                                    uint8_t validation_bits, uint8_t flags,
> +                                    uint32_t error_data_length, uint8_t *fru_id,
> +                                    uint8_t *fru_text, uint64_t time_stamp);
> +
>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
>                         uint64_t len, int node, MemoryAffinityFlags flags);
>  



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block Dongjiu Geng
@ 2019-06-20 12:42   ` Igor Mammedov
  2019-06-25 12:11     ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-20 12:42 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:18 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> It will help to add Generic Error Status Block to ACPI tables
> without using packed C structures and avoid endianness
> issues as API doesn't need explicit conversion.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  hw/acpi/aml-build.c         | 14 ++++++++++++++
>  include/hw/acpi/aml-build.h |  6 ++++++
>  2 files changed, 20 insertions(+)
> 
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index 102a288..ce90970 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -296,6 +296,20 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>          build_append_int_noprefix(table, error_threshold_window, 4);
>  }
>  
> +/* Generic Error Status Block
> + * ACPI 4.0: 17.3.2.6.1 Generic Error Data
> + */
> +void build_append_ghes_generic_status(GArray *table, uint32_t block_status,
maybe ..._generic_error_status???

> +                      uint32_t raw_data_offset, uint32_t raw_data_length,
> +                      uint32_t data_length, uint32_t error_severity)
see CODING_STYLE, 1.1 Multiline Indent

> +{
when describing filds from spec try to add 'verbatim' copy of the name from spec
so it would be esy to grep for it. Like:
       /* Block Status */
> +    build_append_int_noprefix(table, block_status, 4);
       /* Raw Data Offset */

note applies all other places where you compose ACPI tables

> +    build_append_int_noprefix(table, raw_data_offset, 4);
> +    build_append_int_noprefix(table, raw_data_length, 4);
> +    build_append_int_noprefix(table, data_length, 4);
> +    build_append_int_noprefix(table, error_severity, 4);
> +}
> +
>  /* Generic Error Data Entry
>   * ACPI 4.0: 17.3.2.6.1 Generic Error Data
>   */
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index a71db2f..1ec7e1b 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -425,6 +425,12 @@ void build_append_ghes_generic_data(GArray *table, const char *section_type,
>                                      uint32_t error_data_length, uint8_t *fru_id,
>                                      uint8_t *fru_text, uint64_t time_stamp);
>  
> +void
> +build_append_ghes_generic_status(GArray *table, uint32_t block_status,
> +                                 uint32_t raw_data_offset,
> +                                 uint32_t raw_data_length,
> +                                 uint32_t data_length, uint32_t error_severity);
this and previous patch, it might be better to to move declaration
to its own header, for example to include/hw/acpi/acpi_ghes.h
that you are adding later in the series.
And maybe move helpers to hw/acpi/acpi_ghes.c
They are not really independent ACPI primitives that are shared
with other tables, aren't they?
.
> +
>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
>                         uint64_t len, int node, MemoryAffinityFlags flags);
>  



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-20 12:10   ` Igor Mammedov
@ 2019-06-20 14:04     ` gengdongjiu
  2019-06-20 15:09       ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-20 14:04 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

Hi Igor,
   Thanks for your review.

On 2019/6/20 20:10, Igor Mammedov wrote:
>> + */
>> +struct AcpiGenericErrorStatus {
>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
>> +    uint32_t block_status;
>> +    uint32_t raw_data_offset;
>> +    uint32_t raw_data_length;
>> +    uint32_t data_length;
>> +    uint32_t error_severity;
>> +} QEMU_PACKED;
>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;
> there shouldn't be packed structures,
> is it a leftover from previous version?

I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.

> 
>> +
>> +/*
>> + * Masks for block_status flags above
>> + */
>> +#define ACPI_GEBS_UNCORRECTABLE         1
>> +
>> +/*



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-20 14:04     ` gengdongjiu
@ 2019-06-20 15:09       ` Igor Mammedov
  2019-06-20 17:17         ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-20 15:09 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Thu, 20 Jun 2019 22:04:01 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> Hi Igor,
>    Thanks for your review.
> 
> On 2019/6/20 20:10, Igor Mammedov wrote:
> >> + */
> >> +struct AcpiGenericErrorStatus {
> >> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
> >> +    uint32_t block_status;
> >> +    uint32_t raw_data_offset;
> >> +    uint32_t raw_data_length;
> >> +    uint32_t data_length;
> >> +    uint32_t error_severity;
> >> +} QEMU_PACKED;
> >> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;  
> > there shouldn't be packed structures,
> > is it a leftover from previous version?  
> 
> I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.

Question is why it's  there and where it is used?

BTW:
series doesn't apply to master anymore.
Do you have a repo somewhere available for testing?

> 
> >   
> >> +
> >> +/*
> >> + * Masks for block_status flags above
> >> + */
> >> +#define ACPI_GEBS_UNCORRECTABLE         1
> >> +
> >> +/*  
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-20 15:09       ` Igor Mammedov
@ 2019-06-20 17:17         ` gengdongjiu
  2019-06-24 11:16           ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-20 17:17 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/20 23:09, Igor Mammedov wrote:
> On Thu, 20 Jun 2019 22:04:01 +0800
> gengdongjiu <gengdongjiu@huawei.com> wrote:
> 
>> Hi Igor,
>>    Thanks for your review.
>>
>> On 2019/6/20 20:10, Igor Mammedov wrote:
>>>> + */
>>>> +struct AcpiGenericErrorStatus {
>>>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
>>>> +    uint32_t block_status;
>>>> +    uint32_t raw_data_offset;
>>>> +    uint32_t raw_data_length;
>>>> +    uint32_t data_length;
>>>> +    uint32_t error_severity;
>>>> +} QEMU_PACKED;
>>>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;  
>>> there shouldn't be packed structures,
>>> is it a leftover from previous version?  
>>
>> I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.
> 
> Question is why it's  there and where it is used?
sorry, it is my carelessness. it should be packed structures.

I used this structures to get its actual total size and member offset in [PATCH v17 10/10].
If it is not packed structures, the total size and member offset may be not right.


> 
> BTW:
> series doesn't apply to master anymore.
> Do you have a repo somewhere available for testing?

Thanks, I appreciated that you can have a test.

I still do not upload repo, you can reset to below commit[1] in master and apply this series.

BTW:
If test series, you should make an guest memory hardware error, let guest access the error address, then it will happen RAS error.
I provide a software hard code method to test this series, you can refer to https://www.mail-archive.com/qemu-devel@nongnu.org/msg619771.html


[1]:
commit efb4f3b62c69383a7308d7b739a3193e7c0ccae8
Merge: 5f02262 e841257
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Fri May 10 14:49:36 2019 +0100



> 
>>
>>>   
>>>> +
>>>> +/*
>>>> + * Masks for block_status flags above
>>>> + */
>>>> +#define ACPI_GEBS_UNCORRECTABLE         1
>>>> +
>>>> +/*  
>>
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-20 17:17         ` gengdongjiu
@ 2019-06-24 11:16           ` Igor Mammedov
  2019-06-25  9:56             ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 11:16 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Fri, 21 Jun 2019 01:17:48 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/20 23:09, Igor Mammedov wrote:
> > On Thu, 20 Jun 2019 22:04:01 +0800
> > gengdongjiu <gengdongjiu@huawei.com> wrote:
> >   
> >> Hi Igor,
> >>    Thanks for your review.
> >>
> >> On 2019/6/20 20:10, Igor Mammedov wrote:  
> >>>> + */
> >>>> +struct AcpiGenericErrorStatus {
> >>>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
> >>>> +    uint32_t block_status;
> >>>> +    uint32_t raw_data_offset;
> >>>> +    uint32_t raw_data_length;
> >>>> +    uint32_t data_length;
> >>>> +    uint32_t error_severity;
> >>>> +} QEMU_PACKED;
> >>>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;    
> >>> there shouldn't be packed structures,
> >>> is it a leftover from previous version?    
> >>
> >> I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.  
> > 
> > Question is why it's  there and where it is used?  
> sorry, it is my carelessness. it should be packed structures.
> 
> I used this structures to get its actual total size and member offset in [PATCH v17 10/10].
> If it is not packed structures, the total size and member offset may be not right.
I'd suggest to drop these typedefs and use a macro with size for that purpose,
Also it might be good to make it local to the file that would use it.

> > 
> > BTW:
> > series doesn't apply to master anymore.
> > Do you have a repo somewhere available for testing?  
> 
> Thanks, I appreciated that you can have a test.
> 
> I still do not upload repo, you can reset to below commit[1] in master and apply this series.
> 
> BTW:
> If test series, you should make an guest memory hardware error, let guest access the error address, then it will happen RAS error.
> I provide a software hard code method to test this series, you can refer to https://www.mail-archive.com/qemu-devel@nongnu.org/msg619771.html
> 
> 
> [1]:
> commit efb4f3b62c69383a7308d7b739a3193e7c0ccae8
> Merge: 5f02262 e841257
> Author: Peter Maydell <peter.maydell@linaro.org>
> Date:   Fri May 10 14:49:36 2019 +0100
> 
> 
> 
> >   
> >>  
> >>>     
> >>>> +
> >>>> +/*
> >>>> + * Masks for block_status flags above
> >>>> + */
> >>>> +#define ACPI_GEBS_UNCORRECTABLE         1
> >>>> +
> >>>> +/*    
> >>  
> > 
> > .
> >   
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification Dongjiu Geng
@ 2019-06-24 11:21   ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 11:21 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:16 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> It will help to add Hardware Error Notification to ACPI tables
> without using packed C structures and avoid endianness
> issues as API doesn't need explicit conversion.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  hw/acpi/aml-build.c         | 22 ++++++++++++++++++++++
>  include/hw/acpi/aml-build.h |  8 ++++++++
>  2 files changed, 30 insertions(+)
> 
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index 555c24f..fb53f21 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -274,6 +274,28 @@ void build_append_gas(GArray *table, AmlAddressSpace as,
>      build_append_int_noprefix(table, address, 8);
>  }
>  
> +/* Hardware Error Notification
> + * ACPI 4.0: 17.3.2.7 Hardware Error Notification
> + */
> +void build_append_ghes_notify(GArray *table, const uint8_t type,
> +                              uint8_t length, uint16_t config_write_enable,
> +                              uint32_t poll_interval, uint32_t vector,
> +                              uint32_t polling_threshold_value,
> +                              uint32_t polling_threshold_window,
> +                              uint32_t error_threshold_value,
> +                              uint32_t error_threshold_window)
> +{
> +        build_append_int_noprefix(table, type, 1); /* type */
comment should be verbatim copy from spec, in this case /* Type */
also do the same for other fields below

other than that patch looks good to me

> +        build_append_int_noprefix(table, length, 1);
> +        build_append_int_noprefix(table, config_write_enable, 2);
> +        build_append_int_noprefix(table, poll_interval, 4);
> +        build_append_int_noprefix(table, vector, 4);
> +        build_append_int_noprefix(table, polling_threshold_value, 4);
> +        build_append_int_noprefix(table, polling_threshold_window, 4);
> +        build_append_int_noprefix(table, error_threshold_value, 4);
> +        build_append_int_noprefix(table, error_threshold_window, 4);
> +}
> +
>  /*
>   * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
>   * and return the offset to 0x00000000 for runtime patching.
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index 1a563ad..90c8ef8 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -411,6 +411,14 @@ build_append_gas_from_struct(GArray *table, const struct AcpiGenericAddress *s)
>                       s->access_width, s->address);
>  }
>  
> +void build_append_ghes_notify(GArray *table, const uint8_t type,
> +                              uint8_t length, uint16_t config_write_enable,
> +                              uint32_t poll_interval, uint32_t vector,
> +                              uint32_t polling_threshold_value,
> +                              uint32_t polling_threshold_window,
> +                              uint32_t error_threshold_value,
> +                              uint32_t error_threshold_window);
> +
>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
>                         uint64_t len, int node, MemoryAffinityFlags flags);
>  



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description Dongjiu Geng
@ 2019-06-24 11:39   ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 11:39 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:19 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> Add APEI/GHES detailed design document
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  docs/specs/acpi_hest_ghes.txt | 97 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 97 insertions(+)
>  create mode 100644 docs/specs/acpi_hest_ghes.txt
> 
> diff --git a/docs/specs/acpi_hest_ghes.txt b/docs/specs/acpi_hest_ghes.txt
> new file mode 100644
> index 0000000..fbfc787
> --- /dev/null
> +++ b/docs/specs/acpi_hest_ghes.txt
> @@ -0,0 +1,97 @@
> +APEI tables generating and CPER record
> +=============================
> +
> +Copyright (C) 2017 HuaWei Corporation.
> +
> +Design Details:
> +-------------------
> +
> +       etc/acpi/tables                                 etc/hardware_errors
> +    ====================                      ==========================================
> ++ +--------------------------+            +-----------------------+
> +| | HEST                     |            |    address            |            +--------------+
> +| +--------------------------+            |    registers          |            | Error Status |
> +| | GHES1                    |            | +---------------------+            | Data Block 1 |
> +| +--------------------------+ +--------->| |error_block_address1 |----------->| +------------+
> +| | .................        | |          | +---------------------+            | |  CPER      |
> +| | error_status_address-----+-+ +------->| |error_block_address2 |--------+   | |  CPER      |
> +| | .................        |   |        | +---------------------+        |   | |  ....      |
> +| | read_ack_register--------+-+ |        | |    ..............   |        |   | |  CPER      |
> +| | read_ack_preserve        | | |        +-----------------------+        |   | +------------+
> +| | read_ack_write           | | | +----->| |error_block_addressN |------+ |   | Error Status |
> ++ +--------------------------+ | | |      | +---------------------+      | |   | Data Block 2 |
> +| | GHES2                    | +-+-+----->| |read_ack_register1   |      | +-->| +------------+
> ++ +--------------------------+   | |      | +---------------------+      |     | |  CPER      |
> +| | .................        |   | | +--->| |read_ack_register2   |      |     | |  CPER      |
> +| | error_status_address-----+---+ | |    | +---------------------+      |     | |  ....      |
> +| | .................        |     | |    | |  .............      |      |     | |  CPER      |
> +| | read_ack_register--------+-----+-+    | +---------------------+      |     +-+------------+
> +| | read_ack_preserve        |     |   +->| |read_ack_registerN   |      |     | |..........  |
> +| | read_ack_write           |     |   |  | +---------------------+      |     | +------------+
> ++ +--------------------------|     |   |                                 |     | Error Status |
> +| | ...............          |     |   |                                 |     | Data Block N |
> ++ +--------------------------+     |   |                                 +---->| +------------+
> +| | GHESN                    |     |   |                                       | |  CPER      |
> ++ +--------------------------+     |   |                                       | |  CPER      |
> +| | .................        |     |   |                                       | |  ....      |
> +| | error_status_address-----+-----+   |                                       | |  CPER      |
> +| | .................        |         |                                       +-+------------+
> +| | read_ack_register--------+---------+
> +| | read_ack_preserve        |
> +| | read_ack_write           |
> ++ +--------------------------+
> +
> +(1) QEMU generates the ACPI HEST table. This table goes in the current
> +    "etc/acpi/tables" fw_cfg blob. Each error source has different
> +    notification type.
> +
> +(2) A new fw_cfg blob called "etc/hardware_errors" is introduced. QEMU
> +    also need to populate this blob. The "etc/hardwre_errors" fw_cfg blob
> +    contains one address registers table and one Error Status Data Block

s/one/a/
in both cases

> +    table, all of which are pre-allocated.

drop /, all of which are pre-allocated./

> +
> +(3) The address registers table contains N Error Block Address entries
> +    and N Read Ack Address entries, the size for each entry is 8-byte.
> +    The Error Status Data Block table contains N Error Status Data Block
> +    entries, the size for each entry is 4096(0x1000) bytes. The total size
> +    for "etc/hardware_errors" fw_cfg blob is (N * 8 * 2 + N * 4096) bytes.
where 'N' is specified?

> +
> +(4) QEMU generates the ACPI linker/loader script for the firmware
> +
> +(4a) The HEST table is part of "etc/acpi/tables", the firmware already
> +    allocates the memory for it, because QEMU already generates an ALLOCATE
> +    linker/loader command for it
> +
> +(4b) QEMU creates another ALLOCATE command for the "etc/hardware_errors"
> +    blob. The firmware allocates memory for this blob and downloads it.
may be merge both points, like:

    the firmware pre-allocates memory for "etc/acpi/tables", "etc/hardware_errors"
    and copies blobs content there.

> +
> +(5) QEMU generates N ADD_POINTER commands, which patch address in the
> +    "error_status_address" fields of the HEST table with a pointer to the
> +    corresponding "address registers" in the downloaded "etc/hardware_errors"
> +    blob.

s/the downloaded//
the same applies to to other similar occurrences below

> +
> +(6) QEMU generates N ADD_POINTER commands, which patch address in the
> +    "read_ack_register" fields of the HEST table with a pointer to the
> +    corresponding "address registers" in the downloaded "etc/hardware_errors" blob.
> +
> +(7) QEMU generates N ADD_POINTER commands for the firmware, which patch
> +    address in the " error_block_address" fields with a pointer to the
> +    respective "Error Status Data Block" in the downloaded "etc/hardware_errors"
> +    blob.
> +
> +(8) QEMU Defines a third and write-only fw_cfg blob which is called
> +    "etc/hardware_errors_addr". Through that blob, the firmware can send back
> +    the guest-side allocation addresses to QEMU. The "etc/hardware_errors_addr"
> +    blob contains a 8-byte entry. QEMU generates a single WRITE_POINTER commands
> +    for the firmware, the firmware will write back the start address of
> +    "etc/hardware_errors" blob to fw_cfg file "etc/hardware_errors_addr". 

> Then
> +    Qemu will know the Error Status Data Block for every error source. Each of
> +    Error Status Data Block has fixed size which is 4096(0x1000).

this probably is not necessary.

> +
> +(9) When QEMU gets SIGBUS from the kernel, QEMU formats the CPER right into
> +    guest memory, and then injects whatever interrupt (or assert whatever GPIO line)
> +    as a notification which is necessary for notifying the guest.
> +
> +(10) This notification (in virtual hardware) will be handled by guest kernel,
> +    guest APEI driver will read the CPER which is recorded by QEMU and do the
> +    recovery.



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration
  2019-06-20 12:04   ` Igor Mammedov
@ 2019-06-24 12:19     ` gengdongjiu
  2019-06-25 13:16       ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-24 12:19 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/20 20:04, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:14 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> Support this feature since version 4.1, disable it by
>> default in the old version.
>>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  hw/arm/virt.c         | 6 ++++++
>>  include/hw/arm/virt.h | 1 +
>>  2 files changed, 7 insertions(+)
>>
>> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
>> index 5331ab7..7bdd41b 100644
>> --- a/hw/arm/virt.c
>> +++ b/hw/arm/virt.c
>> @@ -2043,8 +2043,14 @@ DEFINE_VIRT_MACHINE_AS_LATEST(4, 1)
>>  
>>  static void virt_machine_4_0_options(MachineClass *mc)
>>  {
>> +    VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
>> +
>>      virt_machine_4_1_options(mc);
>>      compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
>> +    /* Disable memory recovery feature for 4.0 as RAS support was
>> +     * introduced with 4.1.
>> +     */
>> +    vmc->no_ras = true;
> 
> So it would mean that the feature is enabled unconditionally for
> new machine types and consumes resources whether user needs it or not.
> 
> In light of the race for leaner QEMU and faster startup times,
> it might be better to make RAS optional and make user explicitly
> enable it using a machine option.

I will add a machine option to make RAS optional, do you think we should enable or disable it by default? I think it is better if we enable it by default.

> 
> 
>>  }
>>  DEFINE_VIRT_MACHINE(4, 0)
>>  
>> diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h
>> index 4240709..7f1a033 100644
>> --- a/include/hw/arm/virt.h
>> +++ b/include/hw/arm/virt.h
>> @@ -104,6 +104,7 @@ typedef struct {
>>      bool disallow_affinity_adjustment;
>>      bool no_its;
>>      bool no_pmu;
>> +    bool no_ras;
>>      bool claim_edge_triggered_timers;
>>      bool smbios_old_sys_ver;
>>      bool no_highmem_ecam;
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
  2019-05-29  3:37   ` Michael S. Tsirkin
  2019-06-06 13:43   ` Jonathan Cameron
@ 2019-06-24 12:27   ` Igor Mammedov
  2019-06-25 13:48     ` gengdongjiu
  2 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 12:27 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:20 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> This implements APEI GHES Table generation via fw_cfg blobs.
> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
> source. Afterwards, we can extend the supported types if needed. For the
> CPER section type, currently it is memory section because kernel
> mainly wants userspace to handle the memory errors.
> 
> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
> table, for the detailed information, please refer to document:
> docs/specs/acpi_hest_ghes.txt
> 
> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  default-configs/arm-softmmu.mak |   1 +
>  hw/acpi/Kconfig                 |   4 +
>  hw/acpi/Makefile.objs           |   1 +
>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
>  hw/acpi/aml-build.c             |   2 +
>  hw/arm/virt-acpi-build.c        |  12 +++
>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
>  include/hw/acpi/aml-build.h     |   1 +
>  8 files changed, 271 insertions(+)
>  create mode 100644 hw/acpi/acpi_ghes.c
>  create mode 100644 include/hw/acpi/acpi_ghes.h
> 
> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
> index 613d19a..7b33ae9 100644
> --- a/default-configs/arm-softmmu.mak
> +++ b/default-configs/arm-softmmu.mak
> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
>  
>  # for realview and versatilepb
>  CONFIG_LSI_SCSI_PCI=y
> +CONFIG_ACPI_APEI=y
> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
> index eca3bee..5228a4b 100644
> --- a/hw/acpi/Kconfig
> +++ b/hw/acpi/Kconfig
> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
>      bool
>      depends on ACPI
>  
> +config ACPI_APEI
> +    bool
> +    depends on ACPI
> +
>  config ACPI_VMGENID
>      bool
>      default y
> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
> index 2d46e37..5099ada 100644
> --- a/hw/acpi/Makefile.objs
> +++ b/hw/acpi/Makefile.objs
> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
>  
>  common-obj-y += acpi_interface.o
> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> new file mode 100644
> index 0000000..d03e797
> --- /dev/null
> +++ b/hw/acpi/acpi_ghes.c
> @@ -0,0 +1,171 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "hw/acpi/acpi.h"
> +#include "hw/acpi/aml-build.h"
> +#include "hw/acpi/acpi_ghes.h"
> +#include "hw/nvram/fw_cfg.h"
> +#include "sysemu/sysemu.h"
> +#include "qemu/error-report.h"
> +
> +/* Build table for the hardware error fw_cfg blob */
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
> +{
> +    int i;
> +
> +    /*
> +     * | +--------------------------+
> +     * | |    error_block_address   |
> +     * | |      ..........          |
> +     * | +--------------------------+
> +     * | |    read_ack_register     |
> +     * | |     ...........          |
> +     * | +--------------------------+
> +     * | |  Error Status Data Block |
> +     * | |      ........            |
> +     * | +--------------------------+
> +     */
> +
> +    /* Build error_block_address */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);
read CODING_STYLE wrt indentation rules.

> +
> +    /* Build read_ack_register */
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> +        /* Initialize the value of read_ack_register to 1, so GHES can be
> +         * writeable in the first time
where does this come from? a pointer to spec?


> +         */
> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
> +
> +     /* Build Error Status Data Block */
> +    build_append_int_noprefix((void *)hardware_errors, 0,
> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
> +
> +    /* Allocate guest memory for the hardware error fw_cfg blob */
> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
> +                            1, false);
> +}
> +
> +/* Build Hardware Error Source Table */
> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
> +                                            BIOSLinker *linker)
> +{
> +    uint32_t i, error_status_block_offset, length = table_data->len;

s/length/hest_start/

> +
> +    /* Reserve Hardware Error Source Table header size */
> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
> +
> +    /* Set the error source counts */
s/.*/Error Source Count/
like it's in spec, the same for other field comments

> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)

> +         */
shouldn't it be on previous line?

> +        build_append_int_noprefix(table_data,
> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
you don't need cpu_to_le16(), build_append_int_noprefix() does it for you.

> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
> +
> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
> +
> +        /* Number of Records To Pre-allocate */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Sections Per Record */
> +        build_append_int_noprefix(table_data, 1, 4);
> +        /* Max Raw Data Length */
> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
> +
> +        /* Build error status address*/
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker,
> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
> +

> +        /* Build Hardware Error Notification
> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
> +         */
> +        if (i == 0) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
> +                                     0, 0, 0, 0, 0, 0, 0);
> +        } else if (i == 1) {
> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
> +                                     0, 0, 0, 0, 0, 0);
> +        }
well, if we increase  ACPI_HEST_ERROR_SOURCE_COUNT,
this will silently break and build invalid tables.

suggest to put g_assert_not_reached() here


> +
> +        /* Error Status Block Length */
> +        build_append_int_noprefix(table_data,
> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
s/cpu_to_le32//

> +
> +        /* Build Read ACK register
> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
> +         * version 2 (GHESv2 - Type 10)
> +         */
> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
> +
> +        /* Build Read Ack Preserve and Read Ack Writer */

> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
s/cpu_to_le64/

it's recurring mistake on many patches, pls fix it where it's made. I won't comment on it anymore

> +    }
> +

it looks like below part belongs to build_hardware_error_table()
> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
> +
> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
try to check patches with checkpatch before posting
and see CODING_STYLE 4. Block structure

> +        /* Patch address of Error Status Data Block into
> +         * the error_block_address of hardware_errors fw_cfg blob
> +         */
> +        bios_linker_loader_add_pointer(linker,
> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
> +            GHES_ERRORS_FW_CFG_FILE,
> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
> +



> +    /* write address of hardware_errors fw_cfg blob into the
> +     * hardware_errors_addr fw_cfg blob.
> +     */
> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
> +
> +    build_header(linker, table_data,
> +        (void *)(table_data->data + length), "HEST",
> +        table_data->len - length, 1, NULL, "GHES");
> +}
> +
> +static GhesState ges;
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
> +{
> +
> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
> +
> +    /* Create a read-only fw_cfg file for GHES */
> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
> +                    request_block_size);
> +
> +    /* Create a read-write fw_cfg file for Address */
> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
> +}
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index ce90970..3f2b84f 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
>      tables->table_data = g_array_new(false, true /* clear */, 1);
>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
>      tables->linker = bios_linker_loader_init();
>  }
>  
> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
>      g_array_free(tables->table_data, true);
>      g_array_free(tables->tcpalog, mfre);
>      g_array_free(tables->vmgenid, mfre);
> +    g_array_free(tables->hardware_errors, mfre);
>  }
>  
>  /*
> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
> index bf9c0bc..54f4ba5 100644
> --- a/hw/arm/virt-acpi-build.c
> +++ b/hw/arm/virt-acpi-build.c
> @@ -45,6 +45,7 @@
>  #include "hw/arm/virt.h"
>  #include "sysemu/numa.h"
>  #include "kvm_arm.h"
> +#include "hw/acpi/acpi_ghes.h"
>  
>  #define ARM_SPI_BASE 32
>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
>      acpi_add_table(table_offsets, tables_blob);
>      build_spcr(tables_blob, tables->linker, vms);
>  
> +    if (!vmc->no_ras) {
> +        acpi_add_table(table_offsets, tables_blob);
> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
> +    }
> +
>      if (nb_numa_nodes > 0) {
>          acpi_add_table(table_offsets, tables_blob);
>          build_srat(tables_blob, tables->linker, vms);
> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
>  
>  void virt_acpi_setup(VirtMachineState *vms)
>  {
> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
>      AcpiBuildTables tables;
>      AcpiBuildState *build_state;
>  
> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
>                      acpi_data_len(tables.tcpalog));
>  
> +    if (!vmc->no_ras) {
> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
> +    }
> +
>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
>                                                ACPI_BUILD_RSDP_FILE, 0);
>  
> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> new file mode 100644
> index 0000000..38fd87c
> --- /dev/null
> +++ b/include/hw/acpi/acpi_ghes.h
> @@ -0,0 +1,79 @@
> +/* Support for generating APEI tables and record CPER for Guests
> + *
> + * Copyright (C) 2017 HuaWei Corporation.
> + *
> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> +
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> +
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef ACPI_GHES_H
> +#define ACPI_GHES_H
> +
> +#include "hw/acpi/bios-linker-loader.h"
> +
> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
> +
> +/* The size of Address field in Generic Address Structure,
> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
> + */
> +#define GHES_ADDRESS_SIZE           8
> +

> +#define GHES_DATA_LENGTH            72
> +#define GHES_CPER_LENGTH            80
add the similar comment as above if values come from spec
or an explanation where values come from

> +
> +#define ReadAckPreserve             0xfffffffe
> +#define ReadAckWrite                0x1
ditto, and macro should be uppercased.


> +
> +/* The max size in bytes for one error block */
> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> + */
> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2
> +
> +/*
> + * | +--------------------------+ 0
> + * | |        Header            |
> + * | +--------------------------+ 40---+-
> + * | | .................        |      |
> + * | | error_status_address-----+ 60   |
> + * | | .................        |      |
> + * | | read_ack_register--------+ 104  92
> + * | | read_ack_preserve        |      |
> + * | | read_ack_write           |      |
> + * + +--------------------------+ 132--+-
> + *
> + * From above GHES definition, the error status address offset is 60;
> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
> + */
> +
> +/* The error status address offset in GHES */
> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +/* The read Ack register offset in GHES */
> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> +
> +typedef struct GhesState {
> +    uint64_t ghes_addr_le;
> +} GhesState;
> +
> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
> +                     BIOSLinker *linker);
> +
> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> +#endif
> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> index 1ec7e1b..78c0252 100644
> --- a/include/hw/acpi/aml-build.h
> +++ b/include/hw/acpi/aml-build.h
> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
>      GArray *rsdp;
>      GArray *tcpalog;
>      GArray *vmgenid;
> +    GArray *hardware_errors;
>      BIOSLinker *linker;
>  } AcpiBuildTables;
>  



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder Dongjiu Geng
@ 2019-06-24 12:32   ` Igor Mammedov
  2019-06-25 12:28     ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 12:32 UTC (permalink / raw)
  To: Dongjiu Geng, pbonzini
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, lersek, rth

On Tue, 14 May 2019 04:18:21 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> kvm_hwpoison_page_add() and kvm_unpoison_all() will be used both
> by X86 and ARM platforms, so move these functions to a common
> accel/kvm/ folder to avoid duplicate code.
> 
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  accel/kvm/kvm-all.c     | 33 +++++++++++++++++++++++++++++++++
>  include/exec/ram_addr.h | 24 ++++++++++++++++++++++++
>  target/arm/kvm.c        |  3 +++
>  target/i386/kvm.c       | 34 +---------------------------------
>  4 files changed, 61 insertions(+), 33 deletions(-)
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 524c4dd..b9f9f29 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -625,6 +625,39 @@ int kvm_vm_check_extension(KVMState *s, unsigned int extension)
>      return ret;
>  }
>  
> +typedef struct HWPoisonPage {
> +    ram_addr_t ram_addr;
> +    QLIST_ENTRY(HWPoisonPage) list;
> +} HWPoisonPage;
> +
> +static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
> +    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
> +
> +void kvm_unpoison_all(void *param)
> +{
> +    HWPoisonPage *page, *next_page;
> +
> +    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
> +        QLIST_REMOVE(page, list);
> +        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
> +        g_free(page);
> +    }
> +}
> +
> +void kvm_hwpoison_page_add(ram_addr_t ram_addr)
> +{
> +    HWPoisonPage *page;
> +
> +    QLIST_FOREACH(page, &hwpoison_page_list, list) {
> +        if (page->ram_addr == ram_addr) {
> +            return;
> +        }
> +    }
> +    page = g_new(HWPoisonPage, 1);
> +    page->ram_addr = ram_addr;
> +    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
> +}
> +
>  static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
>  {
>  #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
> diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
> index 139ad79..193b0a7 100644
> --- a/include/exec/ram_addr.h
> +++ b/include/exec/ram_addr.h

it's not file for KVM specific code,
maybe Paolo could suggest a bettor place ...


> @@ -116,6 +116,30 @@ void qemu_ram_free(RAMBlock *block);
>  
>  int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
>  
> +/**
> + * kvm_hwpoison_page_add:
> + *
> + * Parameters:
> + *  @ram_addr: the address in the RAM for the poisoned page
> + *
> + * Add a poisoned page to the list
> + *
> + * Return: None.
> + */
> +void kvm_hwpoison_page_add(ram_addr_t ram_addr);
> +
> +/**
> + * kvm_unpoison_all:
> + *
> + * Parameters:
> + *  @param: some data may be passed to this function
> + *
> + * Free and remove all the poisoned pages in the list
> + *
> + * Return: None.
> + */
> +void kvm_unpoison_all(void *param);
> +
>  #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
>  #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
>  
> diff --git a/target/arm/kvm.c b/target/arm/kvm.c
> index 5995634..6d3b25b 100644
> --- a/target/arm/kvm.c
> +++ b/target/arm/kvm.c
> @@ -29,6 +29,7 @@
>  #include "exec/address-spaces.h"
>  #include "hw/boards.h"
>  #include "qemu/log.h"
> +#include "exec/ram_addr.h"
>  
>  const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
>      KVM_CAP_LAST_INFO
> @@ -187,6 +188,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
>  
>      cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
>  
> +    qemu_register_reset(kvm_unpoison_all, NULL);
> +
>      return 0;
>  }
>  
> diff --git a/target/i386/kvm.c b/target/i386/kvm.c
> index 3b29ce5..9bdb879 100644
> --- a/target/i386/kvm.c
> +++ b/target/i386/kvm.c
> @@ -46,6 +46,7 @@
>  #include "migration/blocker.h"
>  #include "exec/memattrs.h"
>  #include "trace.h"
> +#include "exec/ram_addr.h"
>  
>  //#define DEBUG_KVM
>  
> @@ -467,39 +468,6 @@ uint32_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index)
>  }
>  
>  
> -typedef struct HWPoisonPage {
> -    ram_addr_t ram_addr;
> -    QLIST_ENTRY(HWPoisonPage) list;
> -} HWPoisonPage;
> -
> -static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
> -    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
> -
> -static void kvm_unpoison_all(void *param)
> -{
> -    HWPoisonPage *page, *next_page;
> -
> -    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
> -        QLIST_REMOVE(page, list);
> -        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
> -        g_free(page);
> -    }
> -}
> -
> -static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
> -{
> -    HWPoisonPage *page;
> -
> -    QLIST_FOREACH(page, &hwpoison_page_list, list) {
> -        if (page->ram_addr == ram_addr) {
> -            return;
> -        }
> -    }
> -    page = g_new(HWPoisonPage, 1);
> -    page->ram_addr = ram_addr;
> -    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
> -}
> -
>  static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
>                                       int *max_banks)
>  {



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry
  2019-06-20 12:28   ` Igor Mammedov
@ 2019-06-24 12:37     ` gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-06-24 12:37 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/20 20:28, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:17 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> It will help to add Generic Error Data Entry to ACPI tables
>> without using packed C structures and avoid endianness
>> issues as API doesn't need explicit conversion.
>>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  hw/acpi/aml-build.c         | 32 ++++++++++++++++++++++++++++++++
>>  include/hw/acpi/aml-build.h |  6 ++++++
>>  2 files changed, 38 insertions(+)
>>
>> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>> index fb53f21..102a288 100644
>> --- a/hw/acpi/aml-build.c
>> +++ b/hw/acpi/aml-build.c
>> @@ -296,6 +296,38 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>>          build_append_int_noprefix(table, error_threshold_window, 4);
>>  }
>>  
>> +/* Generic Error Data Entry
>> + * ACPI 4.0: 17.3.2.6.1 Generic Error Data
>> + */
>> +void build_append_ghes_generic_data(GArray *table, const char *section_type,
> s/build_append_ghes_generic_data/build_append_ghes_generic_error_data/
> 
>> +                                    uint32_t error_severity, uint16_t revision,
>> +                                    uint8_t validation_bits, uint8_t flags,
>> +                                    uint32_t error_data_length, uint8_t *fru_id,
>> +                                    uint8_t *fru_text, uint64_t time_stamp)
> checkpatch probably will complain due to too long lines
> you can use:
> void build_append_ghe...
>          uint32_t error_severity, uint16_t revision,
>          ...
> 
>> +{
>> +    int i;
>> +
>> +    for (i = 0; i < 16; i++) {
>> +        build_append_int_noprefix(table, section_type[i], 1);
>                                             ^^^
> use QemuUUID instead, see vmgenid_build_acpi
ok.

> 
>> +    }
>> +
>> +    build_append_int_noprefix(table, error_severity, 4);
>> +    build_append_int_noprefix(table, revision, 2);
>> +    build_append_int_noprefix(table, validation_bits, 1);
>> +    build_append_int_noprefix(table, flags, 1);
>> +    build_append_int_noprefix(table, error_data_length, 4);
>> +
>> +    for (i = 0; i < 16; i++) {
>> +        build_append_int_noprefix(table, fru_id[i], 1);
> same as section_type
ok.

> 
>> +    }
>> +
>> +    for (i = 0; i < 20; i++) {
>> +        build_append_int_noprefix(table, fru_text[i], 1);
>> +    }
> instead of loop use g_array_insert_vals()
ok

> 
>> +
>> +    build_append_int_noprefix(table, time_stamp, 8);
> that's not part of 'Table 17-13'
> where does it come from?


It comes from "ACPI 6.1: Table 18-343 Generic Error Data Entry", I will update the comments, thanks for the pointing out.

> 
>> +}
>> +
>>  /*
>>   * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
>>   * and return the offset to 0x00000000 for runtime patching.
>> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
>> index 90c8ef8..a71db2f 100644
>> --- a/include/hw/acpi/aml-build.h
>> +++ b/include/hw/acpi/aml-build.h
>> @@ -419,6 +419,12 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>>                                uint32_t error_threshold_value,
>>                                uint32_t error_threshold_window);
>>  
>> +void build_append_ghes_generic_data(GArray *table, const char *section_type,
>> +                                    uint32_t error_severity, uint16_t revision,
>> +                                    uint8_t validation_bits, uint8_t flags,
>> +                                    uint32_t error_data_length, uint8_t *fru_id,
>> +                                    uint8_t *fru_text, uint64_t time_stamp);
>> +
>>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
>>                         uint64_t len, int node, MemoryAffinityFlags flags);
>>  
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM
  2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM Dongjiu Geng
  2019-06-06 13:31   ` Jonathan Cameron
@ 2019-06-24 13:08   ` Igor Mammedov
  2019-06-25 12:24     ` gengdongjiu
  1 sibling, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-24 13:08 UTC (permalink / raw)
  To: Dongjiu Geng
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 14 May 2019 04:18:23 -0700
Dongjiu Geng <gengdongjiu@huawei.com> wrote:

> Add SIGBUS signal handler. In this handler, it checks the SIGBUS type,
> translates the host VA delivered by host to guest PA, then fill this PA
> to guest APEI GHES memory, then notify guest according to the SIGBUS type.
> 
> If guest accesses the poisoned memory, it generates Synchronous External
> Abort(SEA). Then host kernel gets an APEI notification and call memory_failure()
> to unmapped the affected page for the guest's stage 2, finally return
> to guest.
> 
> Guest continues to access PG_hwpoison page, it will trap to KVM as stage2 fault,
> then a SIGBUS_MCEERR_AR synchronous signal is delivered to Qemu, Qemu record this
> error address into guest APEI GHES memory and notify guest using
> Synchronous-External-Abort(SEA).
> 
> Suggested-by: James Morse <james.morse@arm.com>
> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> ---
>  hw/acpi/acpi_ghes.c         | 177 ++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/acpi/acpi_ghes.h |   6 +-
>  include/sysemu/kvm.h        |   2 +-
>  target/arm/kvm64.c          |  39 ++++++++++
>  4 files changed, 222 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> index d03e797..06b7374 100644
> --- a/hw/acpi/acpi_ghes.c
> +++ b/hw/acpi/acpi_ghes.c
> @@ -26,6 +26,101 @@
>  #include "sysemu/sysemu.h"
>  #include "qemu/error-report.h"
>  
> +/* UEFI 2.6: N.2.5 Memory Error Section */
> +static void build_append_mem_cper(GArray *table, uint64_t error_physical_addr)
> +{
> +    /*
> +     * Memory Error Record
> +     */
> +    build_append_int_noprefix(table,
> +                 (1UL << 14) | /* Type Valid */
> +                 (1UL << 1) /* Physical Address Valid */,
> +                 8);
bad indent

> +    /* Memory error status information */
> +    build_append_int_noprefix(table, 0, 8);
> +    /* The physical address at which the memory error occurred */
> +    build_append_int_noprefix(table, error_physical_addr, 8);
> +    build_append_int_noprefix(table, 0, 48);
> +    build_append_int_noprefix(table, 0 /* Unknown error */, 1);
> +    build_append_int_noprefix(table, 0, 7);
> +}
> +
> +static int ghes_record_mem_error(uint64_t error_block_address,
> +                                    uint64_t error_physical_addr)
bad indent


> +{
> +    GArray *block;
> +    uint64_t current_block_length;
> +    uint32_t data_length;
> +    /* Memory section */
> +    char mem_section_id_le[] = {0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE,
> +                                0x4E, 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C,
> +                                0x83, 0xB1};
> +    uint8_t fru_id[16] = {0};
> +    uint8_t fru_text[20] = {0};
> +
> +    /* Generic Error Status Block
> +     * | +---------------------+
> +     * | |     block_status    |
> +     * | +---------------------+
> +     * | |    raw_data_offset  |
> +     * | +---------------------+
> +     * | |    raw_data_length  |
> +     * | +---------------------+
> +     * | |     data_length     |
> +     * | +---------------------+
> +     * | |   error_severity    |
> +     * | +---------------------+
> +     */
> +    block = g_array_new(false, true /* clear */, 1);
> +
> +    /* Get the length of the Generic Error Data Entries */
> +    cpu_physical_memory_read(error_block_address +
> +        offsetof(AcpiGenericErrorStatus, data_length), &data_length, 4);
> +    /* The current whole length of the generic error status block */
> +    current_block_length = sizeof(AcpiGenericErrorStatus) + le32_to_cpu(data_length);
I might be missing something but why do you read length from guest?
Isn't it something provided by QEMU/host?

> +
> +    /* This is the length if adding a new generic error data entry*/
> +    data_length += GHES_DATA_LENGTH;
> +    data_length += GHES_MEM_CPER_LENGTH;
> +
> +    /* Check whether it will run out of the preallocated memory if adding a new
> +     * generic error data entry
> +     */
> +    if ((data_length + sizeof(AcpiGenericErrorStatus)) > GHES_MAX_RAW_DATA_LENGTH) {
> +        error_report("Record CPER out of boundary!!!");
> +        return GHES_CPER_FAIL;
> +    }
> +
> +    /* Build the new generic error status block header */
> +    build_append_ghes_generic_status(block, cpu_to_le32(ACPI_GEBS_UNCORRECTABLE), 0, 0,
> +        cpu_to_le32(data_length), cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE));
> +
> +    /* Write back above generic error status block header to guest memory */
> +    cpu_physical_memory_write(error_block_address, block->data,
> +                              block->len);
> +
> +    /* Add a new generic error data entry */
> +
> +    data_length = block->len;
> +    /* Build this new generic error data entry header */
> +    build_append_ghes_generic_data(block, mem_section_id_le,
> +                    cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE), cpu_to_le32(0x300), 0, 0,
> +                    cpu_to_le32(80)/* the total size of Memory Error Record */, fru_id,
> +                    fru_text, 0);
> +
> +    /* Build the memory section CPER for above new generic error data entry */
> +    build_append_mem_cper(block, error_physical_addr);
> +
> +    /* Write back above this new generic error data entry to guest memory */
> +    cpu_physical_memory_write(error_block_address + current_block_length,
> +                    block->data + data_length, block->len - data_length);
> +
> +    g_array_free(block, true);
> +
> +    return GHES_CPER_OK;
> +}
> +
>  /* Build table for the hardware error fw_cfg blob */
>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
>  {
> @@ -169,3 +264,85 @@ void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
>      fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
>          &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
>  }
> +
> +bool ghes_record_errors(uint32_t notify, uint64_t physical_address)
> +{
> +    uint64_t error_block_addr, read_ack_register_addr;
> +    int read_ack_register = 0, loop = 0;
> +    uint64_t start_addr = le32_to_cpu(ges.ghes_addr_le);
> +    bool ret = GHES_CPER_FAIL;
> +    const uint8_t error_source_id[] = { 0xff, 0xff, 0xff, 0xff,
> +                                        0xff, 0xff, 0xff, 0, 1};
> +
> +    /*
> +     * | +---------------------+ ges.ghes_addr_le
> +     * | |error_block_address0 |
> +     * | +---------------------+ --+--
> +     * | |    .............    | GHES_ADDRESS_SIZE
> +     * | +---------------------+ --+--
> +     * | |error_block_addressN |
> +     * | +---------------------+
> +     * | | read_ack_register0  |
> +     * | +---------------------+ --+--
> +     * | |   .............     | GHES_ADDRESS_SIZE
> +     * | +---------------------+ --+--
> +     * | | read_ack_registerN  |
> +     * | +---------------------+ --+--
> +     * | |      CPER           |   |
> +     * | |      ....           | GHES_MAX_RAW_DATA_LENGT
> +     * | |      CPER           |   |
> +     * | +---------------------+ --+--
> +     * | |    ..........       |
> +     * | +---------------------+
> +     * | |      CPER           |
> +     * | |      ....           |
> +     * | |      CPER           |
> +     * | +---------------------+
> +     */
> +    if (physical_address && notify < ACPI_HEST_NOTIFY_RESERVED) {
> +        /* Find and check the source id for this new CPER */
> +        if (error_source_id[notify] != 0xff) {
> +            start_addr += error_source_id[notify] * GHES_ADDRESS_SIZE;
> +        } else {
> +            goto out;
> +        }
> +
> +        cpu_physical_memory_read(start_addr, &error_block_addr,
> +                                    GHES_ADDRESS_SIZE);
> +
> +        read_ack_register_addr = start_addr +
> +                        ACPI_HEST_ERROR_SOURCE_COUNT * GHES_ADDRESS_SIZE;
> +retry:
> +        cpu_physical_memory_read(read_ack_register_addr,
> +                                 &read_ack_register, GHES_ADDRESS_SIZE);
> +
> +        /* zero means OSPM does not acknowledge the error */
> +        if (!read_ack_register) {
> +            if (loop < 3) {
> +                usleep(100 * 1000);
> +                loop++;
> +                goto retry;
> +            } else {
> +                error_report("OSPM does not acknowledge previous error,"
> +                    " so can not record CPER for current error, forcibly acknowledge"
> +                    " previous error to avoid blocking next time CPER record! Exit");
> +                read_ack_register = 1;
> +                cpu_physical_memory_write(read_ack_register_addr,
> +                    &read_ack_register, GHES_ADDRESS_SIZE);
> +            }
> +        } else {
> +            if (error_block_addr) {
> +                read_ack_register = 0;
> +                /* Clear the Read Ack Register, OSPM will write it to 1 when
> +                 * acknowledge this error.
> +                 */
> +                cpu_physical_memory_write(read_ack_register_addr,
> +                    &read_ack_register, GHES_ADDRESS_SIZE);
> +                ret = ghes_record_mem_error(error_block_addr, physical_address);
> +            }
> +        }
> +    }
> +
> +out:
> +    return ret;
> +}
> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> index 38fd87c..6b38097 100644
> --- a/include/hw/acpi/acpi_ghes.h
> +++ b/include/hw/acpi/acpi_ghes.h
> @@ -32,11 +32,14 @@
>  #define GHES_ADDRESS_SIZE           8
>  
>  #define GHES_DATA_LENGTH            72
> -#define GHES_CPER_LENGTH            80
> +#define GHES_MEM_CPER_LENGTH        80
>  
>  #define ReadAckPreserve             0xfffffffe
>  #define ReadAckWrite                0x1
>  
> +#define GHES_CPER_OK                1
> +#define GHES_CPER_FAIL              0
> +
>  /* The max size in bytes for one error block */
>  #define GHES_MAX_RAW_DATA_LENGTH        0x1000
>  /* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> @@ -76,4 +79,5 @@ void build_apei_hest(GArray *table_data, GArray *hardware_error,
>  
>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
>  void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> +bool ghes_record_errors(uint32_t notify, uint64_t error_physical_addr);
>  #endif
> diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
> index a6d1cd1..1d1a7a8 100644
> --- a/include/sysemu/kvm.h
> +++ b/include/sysemu/kvm.h
> @@ -377,7 +377,7 @@ bool kvm_vcpu_id_is_valid(int vcpu_id);
>  /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
>  unsigned long kvm_arch_vcpu_id(CPUState *cpu);
>  
> -#ifdef TARGET_I386
> +#if defined(TARGET_I386) || defined(TARGET_AARCH64)
>  #define KVM_HAVE_MCE_INJECTION 1
>  void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
>  #endif
> diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
> index c7bdc6a..d2eac28 100644
> --- a/target/arm/kvm64.c
> +++ b/target/arm/kvm64.c
> @@ -27,6 +27,10 @@
>  #include "kvm_arm.h"
>  #include "internals.h"
>  #include "hw/arm/arm.h"
> +#include "exec/ram_addr.h"
> +#include "hw/acpi/acpi-defs.h"
> +#include "hw/acpi/acpi_ghes.h"
> +#include "hw/acpi/acpi.h"
>  
>  static bool have_guest_debug;
>  
> @@ -1029,6 +1033,41 @@ int kvm_arch_get_registers(CPUState *cs)
>      return ret;
>  }
>  
> +void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
> +{
> +    ram_addr_t ram_addr;
> +    hwaddr paddr;
> +
> +    assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
> +
> +    if (acpi_enabled && addr) {
> +        ram_addr = qemu_ram_addr_from_host(addr);
> +        if (ram_addr != RAM_ADDR_INVALID &&
> +            kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
> +            kvm_hwpoison_page_add(ram_addr);
> +            /* Asynchronous signal will be masked by main thread, so
> +             * only handle synchronous signal.
> +             */
> +            if (code == BUS_MCEERR_AR) {
> +                kvm_cpu_synchronize_state(c);
> +                if (GHES_CPER_FAIL != ghes_record_errors(ACPI_HEST_NOTIFY_SEA, paddr)) {
> +                    kvm_inject_arm_sea(c);
> +                } else {
> +                    fprintf(stderr, "failed to record the error\n");
> +                }
> +            }
> +            return;
> +        }
> +        fprintf(stderr, "Hardware memory error for memory used by "
> +                "QEMU itself instead of guest system!\n");
> +    }
> +
> +    if (code == BUS_MCEERR_AR) {
> +        fprintf(stderr, "Hardware memory error!\n");
> +        exit(1);
> +    }
> +}
> +
>  /* C6.6.29 BRK instruction */
>  static const uint32_t brk_insn = 0xd4200000;
>  



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-24 11:16           ` Igor Mammedov
@ 2019-06-25  9:56             ` gengdongjiu
  2019-06-25 13:33               ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-25  9:56 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/24 19:16, Igor Mammedov wrote:
>>>> On 2019/6/20 20:10, Igor Mammedov wrote:  
>>>>>> + */
>>>>>> +struct AcpiGenericErrorStatus {
>>>>>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
>>>>>> +    uint32_t block_status;
>>>>>> +    uint32_t raw_data_offset;
>>>>>> +    uint32_t raw_data_length;
>>>>>> +    uint32_t data_length;
>>>>>> +    uint32_t error_severity;
>>>>>> +} QEMU_PACKED;
>>>>>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;    
>>>>> there shouldn't be packed structures,
>>>>> is it a leftover from previous version?    
>>>> I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.  
>>> Question is why it's  there and where it is used?  
>> sorry, it is my carelessness. it should be packed structures.
>>
>> I used this structures to get its actual total size and member offset in [PATCH v17 10/10].
>> If it is not packed structures, the total size and member offset may be not right.
> I'd suggest to drop these typedefs and use a macro with size for that purpose,
> Also it might be good to make it local to the file that would use it.
so you mean we also use macro for the  member offset  in the structures?  such as the offset of data_length,
may be there is many hardcode.

> 
>>> BTW:
>>> series doesn't apply to master anymore.



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block
  2019-06-20 12:42   ` Igor Mammedov
@ 2019-06-25 12:11     ` gengdongjiu
  2019-06-25 13:41       ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-25 12:11 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/20 20:42, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:18 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> It will help to add Generic Error Status Block to ACPI tables
>> without using packed C structures and avoid endianness
>> issues as API doesn't need explicit conversion.
>>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  hw/acpi/aml-build.c         | 14 ++++++++++++++
>>  include/hw/acpi/aml-build.h |  6 ++++++
>>  2 files changed, 20 insertions(+)
>>
>> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>> index 102a288..ce90970 100644
>> --- a/hw/acpi/aml-build.c
>> +++ b/hw/acpi/aml-build.c
>> @@ -296,6 +296,20 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
>>          build_append_int_noprefix(table, error_threshold_window, 4);
>>  }
>>  
>> +/* Generic Error Status Block
>> + * ACPI 4.0: 17.3.2.6.1 Generic Error Data
>> + */
>> +void build_append_ghes_generic_status(GArray *table, uint32_t block_status,
> maybe ..._generic_error_status???
good point, the build_append_ghes_generic_error_status() is better than build_append_ghes_generic_status()

> 
>> +                      uint32_t raw_data_offset, uint32_t raw_data_length,
>> +                      uint32_t data_length, uint32_t error_severity)
> see CODING_STYLE, 1.1 Multiline Indent
> 
>> +{
> when describing filds from spec try to add 'verbatim' copy of the name from spec
> so it would be esy to grep for it. Like:
>        /* Block Status */
>> +    build_append_int_noprefix(table, block_status, 4);
>        /* Raw Data Offset */
> 
> note applies all other places where you compose ACPI tables
ok

> 
>> +    build_append_int_noprefix(table, raw_data_offset, 4);
>> +    build_append_int_noprefix(table, raw_data_length, 4);
>> +    build_append_int_noprefix(table, data_length, 4);
>> +    build_append_int_noprefix(table, error_severity, 4);
>> +}
>> +
>>  /* Generic Error Data Entry
>>   * ACPI 4.0: 17.3.2.6.1 Generic Error Data
>>   */
>> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
>> index a71db2f..1ec7e1b 100644
>> --- a/include/hw/acpi/aml-build.h
>> +++ b/include/hw/acpi/aml-build.h
>> @@ -425,6 +425,12 @@ void build_append_ghes_generic_data(GArray *table, const char *section_type,
>>                                      uint32_t error_data_length, uint8_t *fru_id,
>>                                      uint8_t *fru_text, uint64_t time_stamp);
>>  
>> +void
>> +build_append_ghes_generic_status(GArray *table, uint32_t block_status,
>> +                                 uint32_t raw_data_offset,
>> +                                 uint32_t raw_data_length,
>> +                                 uint32_t data_length, uint32_t error_severity);
> this and previous patch, it might be better to to move declaration
> to its own header, for example to include/hw/acpi/acpi_ghes.h
> that you are adding later in the series.
> And maybe move helpers to hw/acpi/acpi_ghes.c
> They are not really independent ACPI primitives that are shared
> with other tables, aren't they?
Some ACPI primitives are shared with other table, such as Notification Structure.
we have 10 types of error sources, some error source will share the  Notification Structure primitives.
Now I only implement Generic Hardware Error Source version 2 (GHESv2 - Type 10)

> .
>> +
>>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
>>                         uint64_t len, int node, MemoryAffinityFlags flags);
>>  
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM
  2019-06-24 13:08   ` Igor Mammedov
@ 2019-06-25 12:24     ` gengdongjiu
  2019-06-25 13:32       ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-25 12:24 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On 2019/6/24 21:08, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:23 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> Add SIGBUS signal handler. In this handler, it checks the SIGBUS type,
>> translates the host VA delivered by host to guest PA, then fill this PA
>> to guest APEI GHES memory, then notify guest according to the SIGBUS type.
>>
>> If guest accesses the poisoned memory, it generates Synchronous External
>> Abort(SEA). Then host kernel gets an APEI notification and call memory_failure()
>> to unmapped the affected page for the guest's stage 2, finally return
>> to guest.
>>
>> Guest continues to access PG_hwpoison page, it will trap to KVM as stage2 fault,
>> then a SIGBUS_MCEERR_AR synchronous signal is delivered to Qemu, Qemu record this
>> error address into guest APEI GHES memory and notify guest using
>> Synchronous-External-Abort(SEA).
>>
>> Suggested-by: James Morse <james.morse@arm.com>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  hw/acpi/acpi_ghes.c         | 177 ++++++++++++++++++++++++++++++++++++++++++++
>>  include/hw/acpi/acpi_ghes.h |   6 +-
>>  include/sysemu/kvm.h        |   2 +-
>>  target/arm/kvm64.c          |  39 ++++++++++
>>  4 files changed, 222 insertions(+), 2 deletions(-)
>>
>> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
>> index d03e797..06b7374 100644
>> --- a/hw/acpi/acpi_ghes.c
>> +++ b/hw/acpi/acpi_ghes.c
>> @@ -26,6 +26,101 @@
>>  #include "sysemu/sysemu.h"
>>  #include "qemu/error-report.h"
>>  
>> +/* UEFI 2.6: N.2.5 Memory Error Section */
>> +static void build_append_mem_cper(GArray *table, uint64_t error_physical_addr)
>> +{
>> +    /*
>> +     * Memory Error Record
>> +     */
>> +    build_append_int_noprefix(table,
>> +                 (1UL << 14) | /* Type Valid */
>> +                 (1UL << 1) /* Physical Address Valid */,
>> +                 8);
> bad indent
I will update it

> 
>> +    /* Memory error status information */
>> +    build_append_int_noprefix(table, 0, 8);
>> +    /* The physical address at which the memory error occurred */
>> +    build_append_int_noprefix(table, error_physical_addr, 8);
>> +    build_append_int_noprefix(table, 0, 48);
>> +    build_append_int_noprefix(table, 0 /* Unknown error */, 1);
>> +    build_append_int_noprefix(table, 0, 7);
>> +}
>> +
>> +static int ghes_record_mem_error(uint64_t error_block_address,
>> +                                    uint64_t error_physical_addr)
> bad indent
I will update it

> 
> 
>> +{
>> +    GArray *block;
>> +    uint64_t current_block_length;
>> +    uint32_t data_length;
>> +    /* Memory section */
>> +    char mem_section_id_le[] = {0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE,
>> +                                0x4E, 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C,
>> +                                0x83, 0xB1};
>> +    uint8_t fru_id[16] = {0};
>> +    uint8_t fru_text[20] = {0};
>> +
>> +    /* Generic Error Status Block
>> +     * | +---------------------+
>> +     * | |     block_status    |
>> +     * | +---------------------+
>> +     * | |    raw_data_offset  |
>> +     * | +---------------------+
>> +     * | |    raw_data_length  |
>> +     * | +---------------------+
>> +     * | |     data_length     |
>> +     * | +---------------------+
>> +     * | |   error_severity    |
>> +     * | +---------------------+
>> +     */
>> +    block = g_array_new(false, true /* clear */, 1);
>> +
>> +    /* Get the length of the Generic Error Data Entries */
>> +    cpu_physical_memory_read(error_block_address +
>> +        offsetof(AcpiGenericErrorStatus, data_length), &data_length, 4);
>> +    /* The current whole length of the generic error status block */
>> +    current_block_length = sizeof(AcpiGenericErrorStatus) + le32_to_cpu(data_length);
> I might be missing something but why do you read length from guest?
> Isn't it something provided by QEMU/host?
The length of the Generic Error Data Entries is not fixed, as the CPER number increases, the length will increase.
there is already a member to record the length for the CPER in the table, this table is in the guest.
so it is better directly read the length from the table instead of providing by QEMU/host.


> 
>> +
>> +    /* This is the length if adding a new generic error data entry*/
>> +    data_length += GHES_DATA_LENGTH;
>> +    data_length += GHES_MEM_CPER_LENGTH;
>> +
>> +    /* Check whether it will run out of the preallocated memory if adding a new
>> +     * generic error data entry
>> +     */
>> +    if ((data_length + sizeof(AcpiGenericErrorStatus)) > GHES_MAX_RAW_DATA_LENGTH) {
>> +        error_report("Record CPER out of boundary!!!");
>> +        return GHES_CPER_FAIL;
>> +    }
>> +
>> +    /* Build the new generic error status block header */
>> +    build_append_ghes_generic_status(block, cpu_to_le32(ACPI_GEBS_UNCORRECTABLE), 0, 0,
>> +        cpu_to_le32(data_length), cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE));
>> +
>> +    /* Write back above generic error status block header to guest memory */
>> +    cpu_physical_memory_write(error_block_address, block->data,
>> +                              block->len);
>> +
>> +    /* Add a new generic error data entry */
>> +
>> +    data_length = block->len;
>> +    /* Build this new generic error data entry header */
>> +    build_append_ghes_generic_data(block, mem_section_id_le,
>> +                    cpu_to_le32(ACPI_CPER_SEV_RECOVERABLE), cpu_to_le32(0x300), 0, 0,
>> +                    cpu_to_le32(80)/* the total size of Memory Error Record */, fru_id,
>> +                    fru_text, 0);
>> +
>> +    /* Build the memory section CPER for above new generic error data entry */
>> +    build_append_mem_cper(block, error_physical_addr);
>> +
>> +    /* Write back above this new generic error data entry to guest memory */
>> +    cpu_physical_memory_write(error_block_address + current_block_length,
>> +                    block->data + data_length, block->len - data_length);
>> +
>> +    g_array_free(block, true);
>> +
>> +    return GHES_CPER_OK;
>> +}
>> +
>>  /* Build table for the hardware error fw_cfg blob */
>>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
>>  {
>> @@ -169,3 +264,85 @@ void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
>>      fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
>>          &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
>>  }
>> +
>> +bool ghes_record_errors(uint32_t notify, uint64_t physical_address)
>> +{
>> +    uint64_t error_block_addr, read_ack_register_addr;
>> +    int read_ack_register = 0, loop = 0;
>> +    uint64_t start_addr = le32_to_cpu(ges.ghes_addr_le);
>> +    bool ret = GHES_CPER_FAIL;
>> +    const uint8_t error_source_id[] = { 0xff, 0xff, 0xff, 0xff,
>> +                                        0xff, 0xff, 0xff, 0, 1};
>> +
>> +    /*
>> +     * | +---------------------+ ges.ghes_addr_le
>> +     * | |error_block_address0 |
>> +     * | +---------------------+ --+--
>> +     * | |    .............    | GHES_ADDRESS_SIZE
>> +     * | +---------------------+ --+--
>> +     * | |error_block_addressN |
>> +     * | +---------------------+
>> +     * | | read_ack_register0  |
>> +     * | +---------------------+ --+--
>> +     * | |   .............     | GHES_ADDRESS_SIZE
>> +     * | +---------------------+ --+--
>> +     * | | read_ack_registerN  |
>> +     * | +---------------------+ --+--
>> +     * | |      CPER           |   |
>> +     * | |      ....           | GHES_MAX_RAW_DATA_LENGT
>> +     * | |      CPER           |   |
>> +     * | +---------------------+ --+--
>> +     * | |    ..........       |
>> +     * | +---------------------+
>> +     * | |      CPER           |
>> +     * | |      ....           |
>> +     * | |      CPER           |
>> +     * | +---------------------+
>> +     */
>> +    if (physical_address && notify < ACPI_HEST_NOTIFY_RESERVED) {
>> +        /* Find and check the source id for this new CPER */
>> +        if (error_source_id[notify] != 0xff) {
>> +            start_addr += error_source_id[notify] * GHES_ADDRESS_SIZE;
>> +        } else {
>> +            goto out;
>> +        }
>> +
>> +        cpu_physical_memory_read(start_addr, &error_block_addr,
>> +                                    GHES_ADDRESS_SIZE);
>> +
>> +        read_ack_register_addr = start_addr +
>> +                        ACPI_HEST_ERROR_SOURCE_COUNT * GHES_ADDRESS_SIZE;
>> +retry:
>> +        cpu_physical_memory_read(read_ack_register_addr,
>> +                                 &read_ack_register, GHES_ADDRESS_SIZE);
>> +
>> +        /* zero means OSPM does not acknowledge the error */
>> +        if (!read_ack_register) {
>> +            if (loop < 3) {
>> +                usleep(100 * 1000);
>> +                loop++;
>> +                goto retry;
>> +            } else {
>> +                error_report("OSPM does not acknowledge previous error,"
>> +                    " so can not record CPER for current error, forcibly acknowledge"
>> +                    " previous error to avoid blocking next time CPER record! Exit");
>> +                read_ack_register = 1;
>> +                cpu_physical_memory_write(read_ack_register_addr,
>> +                    &read_ack_register, GHES_ADDRESS_SIZE);
>> +            }
>> +        } else {
>> +            if (error_block_addr) {
>> +                read_ack_register = 0;
>> +                /* Clear the Read Ack Register, OSPM will write it to 1 when
>> +                 * acknowledge this error.
>> +                 */
>> +                cpu_physical_memory_write(read_ack_register_addr,
>> +                    &read_ack_register, GHES_ADDRESS_SIZE);
>> +                ret = ghes_record_mem_error(error_block_addr, physical_address);
>> +            }
>> +        }
>> +    }
>> +
>> +out:
>> +    return ret;
>> +}
>> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
>> index 38fd87c..6b38097 100644
>> --- a/include/hw/acpi/acpi_ghes.h
>> +++ b/include/hw/acpi/acpi_ghes.h
>> @@ -32,11 +32,14 @@
>>  #define GHES_ADDRESS_SIZE           8
>>  
>>  #define GHES_DATA_LENGTH            72
>> -#define GHES_CPER_LENGTH            80
>> +#define GHES_MEM_CPER_LENGTH        80
>>  
>>  #define ReadAckPreserve             0xfffffffe
>>  #define ReadAckWrite                0x1
>>  
>> +#define GHES_CPER_OK                1
>> +#define GHES_CPER_FAIL              0
>> +
>>  /* The max size in bytes for one error block */
>>  #define GHES_MAX_RAW_DATA_LENGTH        0x1000
>>  /* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
>> @@ -76,4 +79,5 @@ void build_apei_hest(GArray *table_data, GArray *hardware_error,
>>  
>>  void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
>>  void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
>> +bool ghes_record_errors(uint32_t notify, uint64_t error_physical_addr);
>>  #endif
>> diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
>> index a6d1cd1..1d1a7a8 100644
>> --- a/include/sysemu/kvm.h
>> +++ b/include/sysemu/kvm.h
>> @@ -377,7 +377,7 @@ bool kvm_vcpu_id_is_valid(int vcpu_id);
>>  /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
>>  unsigned long kvm_arch_vcpu_id(CPUState *cpu);
>>  
>> -#ifdef TARGET_I386
>> +#if defined(TARGET_I386) || defined(TARGET_AARCH64)
>>  #define KVM_HAVE_MCE_INJECTION 1
>>  void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
>>  #endif
>> diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
>> index c7bdc6a..d2eac28 100644
>> --- a/target/arm/kvm64.c
>> +++ b/target/arm/kvm64.c
>> @@ -27,6 +27,10 @@
>>  #include "kvm_arm.h"
>>  #include "internals.h"
>>  #include "hw/arm/arm.h"
>> +#include "exec/ram_addr.h"
>> +#include "hw/acpi/acpi-defs.h"
>> +#include "hw/acpi/acpi_ghes.h"
>> +#include "hw/acpi/acpi.h"
>>  
>>  static bool have_guest_debug;
>>  
>> @@ -1029,6 +1033,41 @@ int kvm_arch_get_registers(CPUState *cs)
>>      return ret;
>>  }
>>  
>> +void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
>> +{
>> +    ram_addr_t ram_addr;
>> +    hwaddr paddr;
>> +
>> +    assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
>> +
>> +    if (acpi_enabled && addr) {
>> +        ram_addr = qemu_ram_addr_from_host(addr);
>> +        if (ram_addr != RAM_ADDR_INVALID &&
>> +            kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
>> +            kvm_hwpoison_page_add(ram_addr);
>> +            /* Asynchronous signal will be masked by main thread, so
>> +             * only handle synchronous signal.
>> +             */
>> +            if (code == BUS_MCEERR_AR) {
>> +                kvm_cpu_synchronize_state(c);
>> +                if (GHES_CPER_FAIL != ghes_record_errors(ACPI_HEST_NOTIFY_SEA, paddr)) {
>> +                    kvm_inject_arm_sea(c);
>> +                } else {
>> +                    fprintf(stderr, "failed to record the error\n");
>> +                }
>> +            }
>> +            return;
>> +        }
>> +        fprintf(stderr, "Hardware memory error for memory used by "
>> +                "QEMU itself instead of guest system!\n");
>> +    }
>> +
>> +    if (code == BUS_MCEERR_AR) {
>> +        fprintf(stderr, "Hardware memory error!\n");
>> +        exit(1);
>> +    }
>> +}
>> +
>>  /* C6.6.29 BRK instruction */
>>  static const uint32_t brk_insn = 0xd4200000;
>>  
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder
  2019-06-24 12:32   ` Igor Mammedov
@ 2019-06-25 12:28     ` gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-06-25 12:28 UTC (permalink / raw)
  To: Igor Mammedov, pbonzini
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, lersek, rth



On 2019/6/24 20:32, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:21 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> kvm_hwpoison_page_add() and kvm_unpoison_all() will be used both
>> by X86 and ARM platforms, so move these functions to a common
>> accel/kvm/ folder to avoid duplicate code.
>>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  accel/kvm/kvm-all.c     | 33 +++++++++++++++++++++++++++++++++
>>  include/exec/ram_addr.h | 24 ++++++++++++++++++++++++
>>  target/arm/kvm.c        |  3 +++
>>  target/i386/kvm.c       | 34 +---------------------------------
>>  4 files changed, 61 insertions(+), 33 deletions(-)
>>
>> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
>> index 524c4dd..b9f9f29 100644
>> --- a/accel/kvm/kvm-all.c
>> +++ b/accel/kvm/kvm-all.c
>> @@ -625,6 +625,39 @@ int kvm_vm_check_extension(KVMState *s, unsigned int extension)
>>      return ret;
>>  }
>>  
>> +typedef struct HWPoisonPage {
>> +    ram_addr_t ram_addr;
>> +    QLIST_ENTRY(HWPoisonPage) list;
>> +} HWPoisonPage;
>> +
>> +static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
>> +    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
>> +
>> +void kvm_unpoison_all(void *param)
>> +{
>> +    HWPoisonPage *page, *next_page;
>> +
>> +    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
>> +        QLIST_REMOVE(page, list);
>> +        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
>> +        g_free(page);
>> +    }
>> +}
>> +
>> +void kvm_hwpoison_page_add(ram_addr_t ram_addr)
>> +{
>> +    HWPoisonPage *page;
>> +
>> +    QLIST_FOREACH(page, &hwpoison_page_list, list) {
>> +        if (page->ram_addr == ram_addr) {
>> +            return;
>> +        }
>> +    }
>> +    page = g_new(HWPoisonPage, 1);
>> +    page->ram_addr = ram_addr;
>> +    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
>> +}
>> +
>>  static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
>>  {
>>  #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
>> diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
>> index 139ad79..193b0a7 100644
>> --- a/include/exec/ram_addr.h
>> +++ b/include/exec/ram_addr.h
> 
> it's not file for KVM specific code,
> maybe Paolo could suggest a bettor place ...
I remember some people suggests me to move the code to KVM before, may be he is Peter.
Paolo, can you give a better place?

> 
> 
>> @@ -116,6 +116,30 @@ void qemu_ram_free(RAMBlock *block);
>>  
>>  int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
>>  
>> +/**
>> + * kvm_hwpoison_page_add:
>> + *
>> + * Parameters:
>> + *  @ram_addr: the address in the RAM for the poisoned page
>> + *
>> + * Add a poisoned page to the list
>> + *
>> + * Return: None.
>> + */
>> +void kvm_hwpoison_page_add(ram_addr_t ram_addr);
>> +
>> +/**
>> + * kvm_unpoison_all:
>> + *
>> + * Parameters:
>> + *  @param: some data may be passed to this function
>> + *
>> + * Free and remove all the poisoned pages in the list
>> + *
>> + * Return: None.
>> + */
>> +void kvm_unpoison_all(void *param);
>> +
>>  #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
>>  #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
>>  
>> diff --git a/target/arm/kvm.c b/target/arm/kvm.c
>> index 5995634..6d3b25b 100644
>> --- a/target/arm/kvm.c
>> +++ b/target/arm/kvm.c
>> @@ -29,6 +29,7 @@
>>  #include "exec/address-spaces.h"
>>  #include "hw/boards.h"
>>  #include "qemu/log.h"
>> +#include "exec/ram_addr.h"
>>  
>>  const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
>>      KVM_CAP_LAST_INFO
>> @@ -187,6 +188,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
>>  
>>      cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
>>  
>> +    qemu_register_reset(kvm_unpoison_all, NULL);
>> +
>>      return 0;
>>  }
>>  
>> diff --git a/target/i386/kvm.c b/target/i386/kvm.c
>> index 3b29ce5..9bdb879 100644
>> --- a/target/i386/kvm.c
>> +++ b/target/i386/kvm.c
>> @@ -46,6 +46,7 @@
>>  #include "migration/blocker.h"
>>  #include "exec/memattrs.h"
>>  #include "trace.h"
>> +#include "exec/ram_addr.h"
>>  
>>  //#define DEBUG_KVM
>>  
>> @@ -467,39 +468,6 @@ uint32_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index)
>>  }
>>  
>>  
>> -typedef struct HWPoisonPage {
>> -    ram_addr_t ram_addr;
>> -    QLIST_ENTRY(HWPoisonPage) list;
>> -} HWPoisonPage;
>> -
>> -static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
>> -    QLIST_HEAD_INITIALIZER(hwpoison_page_list);
>> -
>> -static void kvm_unpoison_all(void *param)
>> -{
>> -    HWPoisonPage *page, *next_page;
>> -
>> -    QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
>> -        QLIST_REMOVE(page, list);
>> -        qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
>> -        g_free(page);
>> -    }
>> -}
>> -
>> -static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
>> -{
>> -    HWPoisonPage *page;
>> -
>> -    QLIST_FOREACH(page, &hwpoison_page_list, list) {
>> -        if (page->ram_addr == ram_addr) {
>> -            return;
>> -        }
>> -    }
>> -    page = g_new(HWPoisonPage, 1);
>> -    page->ram_addr = ram_addr;
>> -    QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
>> -}
>> -
>>  static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
>>                                       int *max_banks)
>>  {
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration
  2019-06-24 12:19     ` gengdongjiu
@ 2019-06-25 13:16       ` Igor Mammedov
  2019-06-25 13:29         ` gengdongjiu
  0 siblings, 1 reply; 45+ messages in thread
From: Igor Mammedov @ 2019-06-25 13:16 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Mon, 24 Jun 2019 20:19:12 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/20 20:04, Igor Mammedov wrote:
> > On Tue, 14 May 2019 04:18:14 -0700
> > Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> >   
> >> Support this feature since version 4.1, disable it by
> >> default in the old version.
> >>
> >> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> >> ---
> >>  hw/arm/virt.c         | 6 ++++++
> >>  include/hw/arm/virt.h | 1 +
> >>  2 files changed, 7 insertions(+)
> >>
> >> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
> >> index 5331ab7..7bdd41b 100644
> >> --- a/hw/arm/virt.c
> >> +++ b/hw/arm/virt.c
> >> @@ -2043,8 +2043,14 @@ DEFINE_VIRT_MACHINE_AS_LATEST(4, 1)
> >>  
> >>  static void virt_machine_4_0_options(MachineClass *mc)
> >>  {
> >> +    VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
> >> +
> >>      virt_machine_4_1_options(mc);
> >>      compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
> >> +    /* Disable memory recovery feature for 4.0 as RAS support was
> >> +     * introduced with 4.1.
> >> +     */
> >> +    vmc->no_ras = true;  
> > 
> > So it would mean that the feature is enabled unconditionally for
> > new machine types and consumes resources whether user needs it or not.
> > 
> > In light of the race for leaner QEMU and faster startup times,
> > it might be better to make RAS optional and make user explicitly
> > enable it using a machine option.  
> 
> I will add a machine option to make RAS optional, do you think we should enable or disable it by default? I think it is better if we enable it by default.
I'd start with disabled by default as it's easy to make it
enabled by default later and we can't do so other way around.

 
> > 
> >   
> >>  }
> >>  DEFINE_VIRT_MACHINE(4, 0)
> >>  
> >> diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h
> >> index 4240709..7f1a033 100644
> >> --- a/include/hw/arm/virt.h
> >> +++ b/include/hw/arm/virt.h
> >> @@ -104,6 +104,7 @@ typedef struct {
> >>      bool disallow_affinity_adjustment;
> >>      bool no_its;
> >>      bool no_pmu;
> >> +    bool no_ras;
> >>      bool claim_edge_triggered_timers;
> >>      bool smbios_old_sys_ver;
> >>      bool no_highmem_ecam;  
> > 
> > .
> >   
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration
  2019-06-25 13:16       ` Igor Mammedov
@ 2019-06-25 13:29         ` gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-06-25 13:29 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth



On 2019/6/25 21:16, Igor Mammedov wrote:
>>> In light of the race for leaner QEMU and faster startup times,
>>> it might be better to make RAS optional and make user explicitly
>>> enable it using a machine option.  
>> I will add a machine option to make RAS optional, do you think we should enable or disable it by default? I think it is better if we enable it by default.
> I'd start with disabled by default as it's easy to make it
> enabled by default later and we can't do so other way around.
ok

> 
>  
>>>   
>>>>  }
>>>>  DEFINE_VIRT_MACHINE(4, 0)
>>>>  
>>>> diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM
  2019-06-25 12:24     ` gengdongjiu
@ 2019-06-25 13:32       ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-25 13:32 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 25 Jun 2019 20:24:32 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/24 21:08, Igor Mammedov wrote:
> > On Tue, 14 May 2019 04:18:23 -0700
> > Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> >   
> >> Add SIGBUS signal handler. In this handler, it checks the SIGBUS type,
> >> translates the host VA delivered by host to guest PA, then fill this PA
> >> to guest APEI GHES memory, then notify guest according to the SIGBUS type.
> >>
> >> If guest accesses the poisoned memory, it generates Synchronous External
> >> Abort(SEA). Then host kernel gets an APEI notification and call memory_failure()
> >> to unmapped the affected page for the guest's stage 2, finally return
> >> to guest.
> >>
> >> Guest continues to access PG_hwpoison page, it will trap to KVM as stage2 fault,
> >> then a SIGBUS_MCEERR_AR synchronous signal is delivered to Qemu, Qemu record this
> >> error address into guest APEI GHES memory and notify guest using
> >> Synchronous-External-Abort(SEA).
> >>
> >> Suggested-by: James Morse <james.morse@arm.com>
> >> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> >> ---
> >>  hw/acpi/acpi_ghes.c         | 177 ++++++++++++++++++++++++++++++++++++++++++++
> >>  include/hw/acpi/acpi_ghes.h |   6 +-
> >>  include/sysemu/kvm.h        |   2 +-
> >>  target/arm/kvm64.c          |  39 ++++++++++
> >>  4 files changed, 222 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> >> index d03e797..06b7374 100644
> >> --- a/hw/acpi/acpi_ghes.c
> >> +++ b/hw/acpi/acpi_ghes.c
> >> @@ -26,6 +26,101 @@
> >>  #include "sysemu/sysemu.h"
> >>  #include "qemu/error-report.h"
> >>  
> >> +/* UEFI 2.6: N.2.5 Memory Error Section */
> >> +static void build_append_mem_cper(GArray *table, uint64_t error_physical_addr)
> >> +{
> >> +    /*
> >> +     * Memory Error Record
> >> +     */
> >> +    build_append_int_noprefix(table,
> >> +                 (1UL << 14) | /* Type Valid */
> >> +                 (1UL << 1) /* Physical Address Valid */,
> >> +                 8);  
> > bad indent  
> I will update it
> 
> >   
> >> +    /* Memory error status information */
> >> +    build_append_int_noprefix(table, 0, 8);
> >> +    /* The physical address at which the memory error occurred */
> >> +    build_append_int_noprefix(table, error_physical_addr, 8);
> >> +    build_append_int_noprefix(table, 0, 48);
> >> +    build_append_int_noprefix(table, 0 /* Unknown error */, 1);
> >> +    build_append_int_noprefix(table, 0, 7);
> >> +}
> >> +
> >> +static int ghes_record_mem_error(uint64_t error_block_address,
> >> +                                    uint64_t error_physical_addr)  
> > bad indent  
> I will update it
> 
> > 
> >   
> >> +{
> >> +    GArray *block;
> >> +    uint64_t current_block_length;
> >> +    uint32_t data_length;
> >> +    /* Memory section */
> >> +    char mem_section_id_le[] = {0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE,
> >> +                                0x4E, 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C,
> >> +                                0x83, 0xB1};
> >> +    uint8_t fru_id[16] = {0};
> >> +    uint8_t fru_text[20] = {0};
> >> +
> >> +    /* Generic Error Status Block
> >> +     * | +---------------------+
> >> +     * | |     block_status    |
> >> +     * | +---------------------+
> >> +     * | |    raw_data_offset  |
> >> +     * | +---------------------+
> >> +     * | |    raw_data_length  |
> >> +     * | +---------------------+
> >> +     * | |     data_length     |
> >> +     * | +---------------------+
> >> +     * | |   error_severity    |
> >> +     * | +---------------------+
> >> +     */
> >> +    block = g_array_new(false, true /* clear */, 1);
> >> +
> >> +    /* Get the length of the Generic Error Data Entries */
> >> +    cpu_physical_memory_read(error_block_address +
> >> +        offsetof(AcpiGenericErrorStatus, data_length), &data_length, 4);
> >> +    /* The current whole length of the generic error status block */
> >> +    current_block_length = sizeof(AcpiGenericErrorStatus) + le32_to_cpu(data_length);  
> > I might be missing something but why do you read length from guest?
> > Isn't it something provided by QEMU/host?  
> The length of the Generic Error Data Entries is not fixed, as the CPER number increases, the length will increase.
> there is already a member to record the length for the CPER in the table, this table is in the guest.
> so it is better directly read the length from the table instead of providing by QEMU/host.
If not careful using guest provided length for reading/writing buffers in QEMU opens road for exploits.

So if CPER is provided and managed by QEMU then it's better to calculate length
without relying on guest state. Or even rewrite whole status block without trying
to calculate delta.



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition
  2019-06-25  9:56             ` gengdongjiu
@ 2019-06-25 13:33               ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-25 13:33 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 25 Jun 2019 17:56:00 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/24 19:16, Igor Mammedov wrote:
> >>>> On 2019/6/20 20:10, Igor Mammedov wrote:    
> >>>>>> + */
> >>>>>> +struct AcpiGenericErrorStatus {
> >>>>>> +    /* It is a bitmask composed of ACPI_GEBS_xxx macros */
> >>>>>> +    uint32_t block_status;
> >>>>>> +    uint32_t raw_data_offset;
> >>>>>> +    uint32_t raw_data_length;
> >>>>>> +    uint32_t data_length;
> >>>>>> +    uint32_t error_severity;
> >>>>>> +} QEMU_PACKED;
> >>>>>> +typedef struct AcpiGenericErrorStatus AcpiGenericErrorStatus;      
> >>>>> there shouldn't be packed structures,
> >>>>> is it a leftover from previous version?      
> >>>> I remember some people suggest to add QEMU_PACKED before, anyway I will remove it in my next version patch.    
> >>> Question is why it's  there and where it is used?    
> >> sorry, it is my carelessness. it should be packed structures.
> >>
> >> I used this structures to get its actual total size and member offset in [PATCH v17 10/10].
> >> If it is not packed structures, the total size and member offset may be not right.  
> > I'd suggest to drop these typedefs and use a macro with size for that purpose,
> > Also it might be good to make it local to the file that would use it.  
> so you mean we also use macro for the  member offset  in the structures?  such as the offset of data_length,
yes, but I hope there won't be need for data_length offset at all.

> may be there is many hardcode.
> 
> >   
> >>> BTW:
> >>> series doesn't apply to master anymore.  
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block
  2019-06-25 12:11     ` gengdongjiu
@ 2019-06-25 13:41       ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-25 13:41 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 25 Jun 2019 20:11:11 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/20 20:42, Igor Mammedov wrote:
> > On Tue, 14 May 2019 04:18:18 -0700
> > Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> >   
> >> It will help to add Generic Error Status Block to ACPI tables
> >> without using packed C structures and avoid endianness
> >> issues as API doesn't need explicit conversion.
> >>
> >> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> >> ---
> >>  hw/acpi/aml-build.c         | 14 ++++++++++++++
> >>  include/hw/acpi/aml-build.h |  6 ++++++
> >>  2 files changed, 20 insertions(+)
> >>
> >> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> >> index 102a288..ce90970 100644
> >> --- a/hw/acpi/aml-build.c
> >> +++ b/hw/acpi/aml-build.c
> >> @@ -296,6 +296,20 @@ void build_append_ghes_notify(GArray *table, const uint8_t type,
> >>          build_append_int_noprefix(table, error_threshold_window, 4);
> >>  }
> >>  
> >> +/* Generic Error Status Block
> >> + * ACPI 4.0: 17.3.2.6.1 Generic Error Data
> >> + */
> >> +void build_append_ghes_generic_status(GArray *table, uint32_t block_status,  
> > maybe ..._generic_error_status???  
> good point, the build_append_ghes_generic_error_status() is better than build_append_ghes_generic_status()
> 
> >   
> >> +                      uint32_t raw_data_offset, uint32_t raw_data_length,
> >> +                      uint32_t data_length, uint32_t error_severity)  
> > see CODING_STYLE, 1.1 Multiline Indent
> >   
> >> +{  
> > when describing filds from spec try to add 'verbatim' copy of the name from spec
> > so it would be esy to grep for it. Like:
> >        /* Block Status */  
> >> +    build_append_int_noprefix(table, block_status, 4);  
> >        /* Raw Data Offset */
> > 
> > note applies all other places where you compose ACPI tables  
> ok
> 
> >   
> >> +    build_append_int_noprefix(table, raw_data_offset, 4);
> >> +    build_append_int_noprefix(table, raw_data_length, 4);
> >> +    build_append_int_noprefix(table, data_length, 4);
> >> +    build_append_int_noprefix(table, error_severity, 4);
> >> +}
> >> +
> >>  /* Generic Error Data Entry
> >>   * ACPI 4.0: 17.3.2.6.1 Generic Error Data
> >>   */
> >> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> >> index a71db2f..1ec7e1b 100644
> >> --- a/include/hw/acpi/aml-build.h
> >> +++ b/include/hw/acpi/aml-build.h
> >> @@ -425,6 +425,12 @@ void build_append_ghes_generic_data(GArray *table, const char *section_type,
> >>                                      uint32_t error_data_length, uint8_t *fru_id,
> >>                                      uint8_t *fru_text, uint64_t time_stamp);
> >>  
> >> +void
> >> +build_append_ghes_generic_status(GArray *table, uint32_t block_status,
> >> +                                 uint32_t raw_data_offset,
> >> +                                 uint32_t raw_data_length,
> >> +                                 uint32_t data_length, uint32_t error_severity);  
> > this and previous patch, it might be better to to move declaration
> > to its own header, for example to include/hw/acpi/acpi_ghes.h
> > that you are adding later in the series.
> > And maybe move helpers to hw/acpi/acpi_ghes.c
> > They are not really independent ACPI primitives that are shared
> > with other tables, aren't they?  
> Some ACPI primitives are shared with other table, such as Notification Structure.
> we have 10 types of error sources, some error source will share the  Notification Structure primitives.
If read spec right, "Notification Structure" is a sub-table of HEST mechanism so it
should be fine to put all this together in hw/acpi/acpi_ghes.c.


> Now I only implement Generic Hardware Error Source version 2 (GHESv2 - Type 10)
> 
> > .  
> >> +
> >>  void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
> >>                         uint64_t len, int node, MemoryAffinityFlags flags);
> >>    
> > 
> > .
> >   
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-06-24 12:27   ` Igor Mammedov
@ 2019-06-25 13:48     ` gengdongjiu
  2019-06-26 14:25       ` Igor Mammedov
  0 siblings, 1 reply; 45+ messages in thread
From: gengdongjiu @ 2019-06-25 13:48 UTC (permalink / raw)
  To: Igor Mammedov
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On 2019/6/24 20:27, Igor Mammedov wrote:
> On Tue, 14 May 2019 04:18:20 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
>> This implements APEI GHES Table generation via fw_cfg blobs.
>> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
>> source. Afterwards, we can extend the supported types if needed. For the
>> CPER section type, currently it is memory section because kernel
>> mainly wants userspace to handle the memory errors.
>>
>> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
>> table, for the detailed information, please refer to document:
>> docs/specs/acpi_hest_ghes.txt
>>
>> Suggested-by: Laszlo Ersek <lersek@redhat.com>
>> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
>> ---
>>  default-configs/arm-softmmu.mak |   1 +
>>  hw/acpi/Kconfig                 |   4 +
>>  hw/acpi/Makefile.objs           |   1 +
>>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
>>  hw/acpi/aml-build.c             |   2 +
>>  hw/arm/virt-acpi-build.c        |  12 +++
>>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
>>  include/hw/acpi/aml-build.h     |   1 +
>>  8 files changed, 271 insertions(+)
>>  create mode 100644 hw/acpi/acpi_ghes.c
>>  create mode 100644 include/hw/acpi/acpi_ghes.h
>>
>> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
>> index 613d19a..7b33ae9 100644
>> --- a/default-configs/arm-softmmu.mak
>> +++ b/default-configs/arm-softmmu.mak
>> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
>>  
>>  # for realview and versatilepb
>>  CONFIG_LSI_SCSI_PCI=y
>> +CONFIG_ACPI_APEI=y
>> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
>> index eca3bee..5228a4b 100644
>> --- a/hw/acpi/Kconfig
>> +++ b/hw/acpi/Kconfig
>> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
>>      bool
>>      depends on ACPI
>>  
>> +config ACPI_APEI
>> +    bool
>> +    depends on ACPI
>> +
>>  config ACPI_VMGENID
>>      bool
>>      default y
>> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
>> index 2d46e37..5099ada 100644
>> --- a/hw/acpi/Makefile.objs
>> +++ b/hw/acpi/Makefile.objs
>> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
>>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
>>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
>>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
>> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
>>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
>>  
>>  common-obj-y += acpi_interface.o
>> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
>> new file mode 100644
>> index 0000000..d03e797
>> --- /dev/null
>> +++ b/hw/acpi/acpi_ghes.c
>> @@ -0,0 +1,171 @@
>> +/* Support for generating APEI tables and record CPER for Guests
>> + *
>> + * Copyright (C) 2017 HuaWei Corporation.
>> + *
>> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation; either version 2 of the License, or
>> + * (at your option) any later version.
>> +
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> +
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "hw/acpi/acpi.h"
>> +#include "hw/acpi/aml-build.h"
>> +#include "hw/acpi/acpi_ghes.h"
>> +#include "hw/nvram/fw_cfg.h"
>> +#include "sysemu/sysemu.h"
>> +#include "qemu/error-report.h"
>> +
>> +/* Build table for the hardware error fw_cfg blob */
>> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
>> +{
>> +    int i;
>> +
>> +    /*
>> +     * | +--------------------------+
>> +     * | |    error_block_address   |
>> +     * | |      ..........          |
>> +     * | +--------------------------+
>> +     * | |    read_ack_register     |
>> +     * | |     ...........          |
>> +     * | +--------------------------+
>> +     * | |  Error Status Data Block |
>> +     * | |      ........            |
>> +     * | +--------------------------+
>> +     */
>> +
>> +    /* Build error_block_address */
>> +    build_append_int_noprefix((void *)hardware_errors, 0,
>> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);
> read CODING_STYLE wrt indentation rules.
 thanks for the reminder.

> 
>> +
>> +    /* Build read_ack_register */
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
>> +        /* Initialize the value of read_ack_register to 1, so GHES can be
>> +         * writeable in the first time
> where does this come from? a pointer to spec?

It is come from "ACPI 6.2, Table 18-382 Generic Hardware Error Source version 2 (GHESv2) Structure", I will add a comment.
QEMU init the read_ack_register to 1, guest will change it from 1 to 0 after acknowledges the error.

> 
> 
>> +         */
>> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
>> +
>> +     /* Build Error Status Data Block */
>> +    build_append_int_noprefix((void *)hardware_errors, 0,
>> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
>> +
>> +    /* Allocate guest memory for the hardware error fw_cfg blob */
>> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
>> +                            1, false);
>> +}
>> +
>> +/* Build Hardware Error Source Table */
>> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
>> +                                            BIOSLinker *linker)
>> +{
>> +    uint32_t i, error_status_block_offset, length = table_data->len;
> 
> s/length/hest_start/
thanks for the suggestion.

> 
>> +
>> +    /* Reserve Hardware Error Source Table header size */
>> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
>> +
>> +    /* Set the error source counts */
> s/.*/Error Source Count/
> like it's in spec, the same for other field comments
ok, thanks for the suggestion.

> 
>> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
>> +
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
>> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
> 
>> +         */
> shouldn't it be on previous line?
yes, I will change it.

> 
>> +        build_append_int_noprefix(table_data,
>> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
>> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
> you don't need cpu_to_le16(), build_append_int_noprefix() does it for you.
thanks for the reminder.

> 
>> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
>> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
>> +
>> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
>> +
>> +        /* Number of Records To Pre-allocate */
>> +        build_append_int_noprefix(table_data, 1, 4);
>> +        /* Max Sections Per Record */
>> +        build_append_int_noprefix(table_data, 1, 4);
>> +        /* Max Raw Data Length */
>> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
>> +
>> +        /* Build error status address*/
>> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
>> +        bios_linker_loader_add_pointer(linker,
>> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
>> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
>> +
> 
>> +        /* Build Hardware Error Notification
>> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
>> +         */
>> +        if (i == 0) {
>> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
>> +                                     0, 0, 0, 0, 0, 0, 0);
>> +        } else if (i == 1) {
>> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
>> +                                     0, 0, 0, 0, 0, 0);
>> +        }
> well, if we increase  ACPI_HEST_ERROR_SOURCE_COUNT,
> this will silently break and build invalid tables.
> 
> suggest to put g_assert_not_reached() here
ok, thanks.

> 
> 
>> +
>> +        /* Error Status Block Length */
>> +        build_append_int_noprefix(table_data,
>> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
> s/cpu_to_le32//
I will remove.

> 
>> +
>> +        /* Build Read ACK register
>> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
>> +         * version 2 (GHESv2 - Type 10)
>> +         */
>> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
>> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
>> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
>> +            GHES_ERRORS_FW_CFG_FILE,
>> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
>> +
>> +        /* Build Read Ack Preserve and Read Ack Writer */
> 
>> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
>> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
> s/cpu_to_le64/
I will remove.

> 
> it's recurring mistake on many patches, pls fix it where it's made. I won't comment on it anymore
got it.

> 
>> +    }
>> +
> 
> it looks like below part belongs to build_hardware_error_table()

Below part mainly setup "Error Status Address" and "Read Ack Register"in the "ACPI 6.2: Table 18-382 Generic Hardware Error Source version 2 (GHESv2) Structure"
Maybe it is better belong to build_apei_hest().

>> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
>> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
>> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
>> +
>> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> try to check patches with checkpatch before posting
> and see CODING_STYLE 4. Block structure
ok, thanks.

> 
>> +        /* Patch address of Error Status Data Block into
>> +         * the error_block_address of hardware_errors fw_cfg blob
>> +         */
>> +        bios_linker_loader_add_pointer(linker,
>> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
>> +            GHES_ERRORS_FW_CFG_FILE,
>> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
>> +
> 
> 
> 
>> +    /* write address of hardware_errors fw_cfg blob into the
>> +     * hardware_errors_addr fw_cfg blob.
>> +     */
>> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
>> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
>> +
>> +    build_header(linker, table_data,
>> +        (void *)(table_data->data + length), "HEST",
>> +        table_data->len - length, 1, NULL, "GHES");
>> +}
>> +
>> +static GhesState ges;
>> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
>> +{
>> +
>> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
>> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
>> +
>> +    /* Create a read-only fw_cfg file for GHES */
>> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
>> +                    request_block_size);
>> +
>> +    /* Create a read-write fw_cfg file for Address */
>> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
>> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
>> +}
>> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>> index ce90970..3f2b84f 100644
>> --- a/hw/acpi/aml-build.c
>> +++ b/hw/acpi/aml-build.c
>> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
>>      tables->table_data = g_array_new(false, true /* clear */, 1);
>>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
>>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
>> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
>>      tables->linker = bios_linker_loader_init();
>>  }
>>  
>> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
>>      g_array_free(tables->table_data, true);
>>      g_array_free(tables->tcpalog, mfre);
>>      g_array_free(tables->vmgenid, mfre);
>> +    g_array_free(tables->hardware_errors, mfre);
>>  }
>>  
>>  /*
>> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
>> index bf9c0bc..54f4ba5 100644
>> --- a/hw/arm/virt-acpi-build.c
>> +++ b/hw/arm/virt-acpi-build.c
>> @@ -45,6 +45,7 @@
>>  #include "hw/arm/virt.h"
>>  #include "sysemu/numa.h"
>>  #include "kvm_arm.h"
>> +#include "hw/acpi/acpi_ghes.h"
>>  
>>  #define ARM_SPI_BASE 32
>>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
>> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
>>      acpi_add_table(table_offsets, tables_blob);
>>      build_spcr(tables_blob, tables->linker, vms);
>>  
>> +    if (!vmc->no_ras) {
>> +        acpi_add_table(table_offsets, tables_blob);
>> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
>> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
>> +    }
>> +
>>      if (nb_numa_nodes > 0) {
>>          acpi_add_table(table_offsets, tables_blob);
>>          build_srat(tables_blob, tables->linker, vms);
>> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
>>  
>>  void virt_acpi_setup(VirtMachineState *vms)
>>  {
>> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
>>      AcpiBuildTables tables;
>>      AcpiBuildState *build_state;
>>  
>> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
>>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
>>                      acpi_data_len(tables.tcpalog));
>>  
>> +    if (!vmc->no_ras) {
>> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
>> +    }
>> +
>>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
>>                                                ACPI_BUILD_RSDP_FILE, 0);
>>  
>> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
>> new file mode 100644
>> index 0000000..38fd87c
>> --- /dev/null
>> +++ b/include/hw/acpi/acpi_ghes.h
>> @@ -0,0 +1,79 @@
>> +/* Support for generating APEI tables and record CPER for Guests
>> + *
>> + * Copyright (C) 2017 HuaWei Corporation.
>> + *
>> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation; either version 2 of the License, or
>> + * (at your option) any later version.
>> +
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> +
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#ifndef ACPI_GHES_H
>> +#define ACPI_GHES_H
>> +
>> +#include "hw/acpi/bios-linker-loader.h"
>> +
>> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
>> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
>> +
>> +/* The size of Address field in Generic Address Structure,
>> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
>> + */
>> +#define GHES_ADDRESS_SIZE           8
>> +
> 
>> +#define GHES_DATA_LENGTH            72
>> +#define GHES_CPER_LENGTH            80
> add the similar comment as above if values come from spec
> or an explanation where values come from
  got it.

> 
>> +
>> +#define ReadAckPreserve             0xfffffffe
>> +#define ReadAckWrite                0x1
> ditto, and macro should be uppercased.
I will move above macros and folow Michael's comments to  just open-coding at use point
and adding a comment:

        build_append_int_noprefix(table_data, cpu_to_le64(0xfffffffe), 8); /* ReadAckPreserve */
        build_append_int_noprefix(table_data, cpu_to_le64(0x1), 8); /* ReadAckWrite */
> 
> 
>> +
>> +/* The max size in bytes for one error block */
>> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
>> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
>> + */
>> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2
>> +
>> +/*
>> + * | +--------------------------+ 0
>> + * | |        Header            |
>> + * | +--------------------------+ 40---+-
>> + * | | .................        |      |
>> + * | | error_status_address-----+ 60   |
>> + * | | .................        |      |
>> + * | | read_ack_register--------+ 104  92
>> + * | | read_ack_preserve        |      |
>> + * | | read_ack_write           |      |
>> + * + +--------------------------+ 132--+-
>> + *
>> + * From above GHES definition, the error status address offset is 60;
>> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
>> + */
>> +
>> +/* The error status address offset in GHES */
>> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
>> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
>> +
>> +/* The read Ack register offset in GHES */
>> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
>> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
>> +
>> +typedef struct GhesState {
>> +    uint64_t ghes_addr_le;
>> +} GhesState;
>> +
>> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
>> +                     BIOSLinker *linker);
>> +
>> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
>> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
>> +#endif
>> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
>> index 1ec7e1b..78c0252 100644
>> --- a/include/hw/acpi/aml-build.h
>> +++ b/include/hw/acpi/aml-build.h
>> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
>>      GArray *rsdp;
>>      GArray *tcpalog;
>>      GArray *vmgenid;
>> +    GArray *hardware_errors;
>>      BIOSLinker *linker;
>>  } AcpiBuildTables;
>>  
> 
> .
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
  2019-06-25 13:48     ` gengdongjiu
@ 2019-06-26 14:25       ` Igor Mammedov
  0 siblings, 0 replies; 45+ messages in thread
From: Igor Mammedov @ 2019-06-26 14:25 UTC (permalink / raw)
  To: gengdongjiu
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	linuxarm, shannon.zhaosl, zhengxiang9, qemu-arm, james.morse,
	xuwei5, jonathan.cameron, pbonzini, lersek, rth

On Tue, 25 Jun 2019 21:48:07 +0800
gengdongjiu <gengdongjiu@huawei.com> wrote:

> On 2019/6/24 20:27, Igor Mammedov wrote:
> > On Tue, 14 May 2019 04:18:20 -0700
> > Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> >   
> >> This implements APEI GHES Table generation via fw_cfg blobs.
> >> Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2 error
> >> source. Afterwards, we can extend the supported types if needed. For the
> >> CPER section type, currently it is memory section because kernel
> >> mainly wants userspace to handle the memory errors.
> >>
> >> This patch follows the spec ACPI 6.2 to build the Hardware Error Source
> >> table, for the detailed information, please refer to document:
> >> docs/specs/acpi_hest_ghes.txt
> >>
> >> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> >> Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> >> ---
> >>  default-configs/arm-softmmu.mak |   1 +
> >>  hw/acpi/Kconfig                 |   4 +
> >>  hw/acpi/Makefile.objs           |   1 +
> >>  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
> >>  hw/acpi/aml-build.c             |   2 +
> >>  hw/arm/virt-acpi-build.c        |  12 +++
> >>  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
> >>  include/hw/acpi/aml-build.h     |   1 +
> >>  8 files changed, 271 insertions(+)
> >>  create mode 100644 hw/acpi/acpi_ghes.c
> >>  create mode 100644 include/hw/acpi/acpi_ghes.h
> >>
> >> diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
> >> index 613d19a..7b33ae9 100644
> >> --- a/default-configs/arm-softmmu.mak
> >> +++ b/default-configs/arm-softmmu.mak
> >> @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
> >>  
> >>  # for realview and versatilepb
> >>  CONFIG_LSI_SCSI_PCI=y
> >> +CONFIG_ACPI_APEI=y
> >> diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig
> >> index eca3bee..5228a4b 100644
> >> --- a/hw/acpi/Kconfig
> >> +++ b/hw/acpi/Kconfig
> >> @@ -23,6 +23,10 @@ config ACPI_NVDIMM
> >>      bool
> >>      depends on ACPI
> >>  
> >> +config ACPI_APEI
> >> +    bool
> >> +    depends on ACPI
> >> +
> >>  config ACPI_VMGENID
> >>      bool
> >>      default y
> >> diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
> >> index 2d46e37..5099ada 100644
> >> --- a/hw/acpi/Makefile.objs
> >> +++ b/hw/acpi/Makefile.objs
> >> @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) += memory_hotplug.o
> >>  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
> >>  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
> >>  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
> >> +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
> >>  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
> >>  
> >>  common-obj-y += acpi_interface.o
> >> diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c
> >> new file mode 100644
> >> index 0000000..d03e797
> >> --- /dev/null
> >> +++ b/hw/acpi/acpi_ghes.c
> >> @@ -0,0 +1,171 @@
> >> +/* Support for generating APEI tables and record CPER for Guests
> >> + *
> >> + * Copyright (C) 2017 HuaWei Corporation.
> >> + *
> >> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> >> + *
> >> + * This program is free software; you can redistribute it and/or modify
> >> + * it under the terms of the GNU General Public License as published by
> >> + * the Free Software Foundation; either version 2 of the License, or
> >> + * (at your option) any later version.
> >> +
> >> + * This program is distributed in the hope that it will be useful,
> >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >> + * GNU General Public License for more details.
> >> +
> >> + * You should have received a copy of the GNU General Public License along
> >> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> >> + */
> >> +
> >> +#include "qemu/osdep.h"
> >> +#include "hw/acpi/acpi.h"
> >> +#include "hw/acpi/aml-build.h"
> >> +#include "hw/acpi/acpi_ghes.h"
> >> +#include "hw/nvram/fw_cfg.h"
> >> +#include "sysemu/sysemu.h"
> >> +#include "qemu/error-report.h"
> >> +
> >> +/* Build table for the hardware error fw_cfg blob */
> >> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker)
> >> +{
> >> +    int i;
> >> +
> >> +    /*
> >> +     * | +--------------------------+
> >> +     * | |    error_block_address   |
> >> +     * | |      ..........          |
> >> +     * | +--------------------------+
> >> +     * | |    read_ack_register     |
> >> +     * | |     ...........          |
> >> +     * | +--------------------------+
> >> +     * | |  Error Status Data Block |
> >> +     * | |      ........            |
> >> +     * | +--------------------------+
> >> +     */
> >> +
> >> +    /* Build error_block_address */
> >> +    build_append_int_noprefix((void *)hardware_errors, 0,
> >> +                    GHES_ADDRESS_SIZE * ACPI_HEST_ERROR_SOURCE_COUNT);  
> > read CODING_STYLE wrt indentation rules.  
>  thanks for the reminder.
> 
> >   
> >> +
> >> +    /* Build read_ack_register */
> >> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> >> +        /* Initialize the value of read_ack_register to 1, so GHES can be
> >> +         * writeable in the first time  
> > where does this come from? a pointer to spec?  
> 
> It is come from "ACPI 6.2, Table 18-382 Generic Hardware Error Source version 2 (GHESv2) Structure", I will add a comment.
> QEMU init the read_ack_register to 1, guest will change it from 1 to 0 after acknowledges the error.
> 
> > 
> >   
> >> +         */
> >> +        build_append_int_noprefix((void *)hardware_errors, 1, GHES_ADDRESS_SIZE);
> >> +
> >> +     /* Build Error Status Data Block */
> >> +    build_append_int_noprefix((void *)hardware_errors, 0,
> >> +                    GHES_MAX_RAW_DATA_LENGTH * ACPI_HEST_ERROR_SOURCE_COUNT);
> >> +
> >> +    /* Allocate guest memory for the hardware error fw_cfg blob */
> >> +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
> >> +                            1, false);
> >> +}
> >> +
> >> +/* Build Hardware Error Source Table */
> >> +void build_apei_hest(GArray *table_data, GArray *hardware_errors,
> >> +                                            BIOSLinker *linker)
> >> +{
> >> +    uint32_t i, error_status_block_offset, length = table_data->len;  
> > 
> > s/length/hest_start/  
> thanks for the suggestion.
> 
> >   
> >> +
> >> +    /* Reserve Hardware Error Source Table header size */
> >> +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
> >> +
> >> +    /* Set the error source counts */  
> > s/.*/Error Source Count/
> > like it's in spec, the same for other field comments  
> ok, thanks for the suggestion.
> 
> >   
> >> +    build_append_int_noprefix(table_data, ACPI_HEST_ERROR_SOURCE_COUNT, 4);
> >> +
> >> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
> >> +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)  
> >   
> >> +         */  
> > shouldn't it be on previous line?  
> yes, I will change it.
> 
> >   
> >> +        build_append_int_noprefix(table_data,
> >> +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
> >> +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */  
> > you don't need cpu_to_le16(), build_append_int_noprefix() does it for you.  
> thanks for the reminder.
> 
> >   
> >> +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
> >> +        build_append_int_noprefix(table_data, 0, 1); /* flags */
> >> +
> >> +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
> >> +
> >> +        /* Number of Records To Pre-allocate */
> >> +        build_append_int_noprefix(table_data, 1, 4);
> >> +        /* Max Sections Per Record */
> >> +        build_append_int_noprefix(table_data, 1, 4);
> >> +        /* Max Raw Data Length */
> >> +        build_append_int_noprefix(table_data, GHES_MAX_RAW_DATA_LENGTH, 4);
> >> +
> >> +        /* Build error status address*/
> >> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> >> +        bios_linker_loader_add_pointer(linker,
> >> +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
> >> +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i * GHES_ADDRESS_SIZE);
> >> +  
> >   
> >> +        /* Build Hardware Error Notification
> >> +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
> >> +         */
> >> +        if (i == 0) {
> >> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
> >> +                                     0, 0, 0, 0, 0, 0, 0);
> >> +        } else if (i == 1) {
> >> +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
> >> +                                     0, 0, 0, 0, 0, 0);
> >> +        }  
> > well, if we increase  ACPI_HEST_ERROR_SOURCE_COUNT,
> > this will silently break and build invalid tables.
> > 
> > suggest to put g_assert_not_reached() here  
> ok, thanks.
> 
> > 
> >   
> >> +
> >> +        /* Error Status Block Length */
> >> +        build_append_int_noprefix(table_data,
> >> +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);  
> > s/cpu_to_le32//  
> I will remove.
> 
> >   
> >> +
> >> +        /* Build Read ACK register
> >> +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
> >> +         * version 2 (GHESv2 - Type 10)
> >> +         */
> >> +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> >> +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> >> +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
> >> +            GHES_ERRORS_FW_CFG_FILE,
> >> +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
> >> +
> >> +        /* Build Read Ack Preserve and Read Ack Writer */  
> >   
> >> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
> >> +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);  
> > s/cpu_to_le64/  
> I will remove.
> 
> > 
> > it's recurring mistake on many patches, pls fix it where it's made. I won't comment on it anymore  
> got it.
> 
> >   
> >> +    }
> >> +  
> > 
> > it looks like below part belongs to build_hardware_error_table()  
> 
> Below part mainly setup "Error Status Address" and "Read Ack Register"in the "ACPI 6.2: Table 18-382 Generic Hardware Error Source version 2 (GHESv2) Structure"
> Maybe it is better belong to build_apei_hest().

it deals with only GHES_ERRORS_FW_CFG_FILE which is populated in
build_hardware_error_table(). I don't really see anything there that belongs to
more generic build_apei_hest(), so it should go to build_hardware_error_table() if possible

> 
> >> +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
> >> +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
> >> +                                ACPI_HEST_ERROR_SOURCE_COUNT;
> >> +
> >> +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)  
> > try to check patches with checkpatch before posting
> > and see CODING_STYLE 4. Block structure  
> ok, thanks.
> 
> >   
> >> +        /* Patch address of Error Status Data Block into
> >> +         * the error_block_address of hardware_errors fw_cfg blob
> >> +         */
> >> +        bios_linker_loader_add_pointer(linker,
> >> +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
> >> +            GHES_ERRORS_FW_CFG_FILE,
> >> +            error_status_block_offset + i * GHES_MAX_RAW_DATA_LENGTH);
> >> +  
> > 
> > 
> >   
> >> +    /* write address of hardware_errors fw_cfg blob into the
> >> +     * hardware_errors_addr fw_cfg blob.
> >> +     */
> >> +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
> >> +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
> >> +
> >> +    build_header(linker, table_data,
> >> +        (void *)(table_data->data + length), "HEST",
> >> +        table_data->len - length, 1, NULL, "GHES");
> >> +}
> >> +
> >> +static GhesState ges;
> >> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error)
> >> +{
> >> +
> >> +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
> >> +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
> >> +
> >> +    /* Create a read-only fw_cfg file for GHES */
> >> +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
> >> +                    request_block_size);
> >> +
> >> +    /* Create a read-write fw_cfg file for Address */
> >> +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
> >> +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false);
> >> +}
> >> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> >> index ce90970..3f2b84f 100644
> >> --- a/hw/acpi/aml-build.c
> >> +++ b/hw/acpi/aml-build.c
> >> @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
> >>      tables->table_data = g_array_new(false, true /* clear */, 1);
> >>      tables->tcpalog = g_array_new(false, true /* clear */, 1);
> >>      tables->vmgenid = g_array_new(false, true /* clear */, 1);
> >> +    tables->hardware_errors = g_array_new(false, true /* clear */, 1);
> >>      tables->linker = bios_linker_loader_init();
> >>  }
> >>  
> >> @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
> >>      g_array_free(tables->table_data, true);
> >>      g_array_free(tables->tcpalog, mfre);
> >>      g_array_free(tables->vmgenid, mfre);
> >> +    g_array_free(tables->hardware_errors, mfre);
> >>  }
> >>  
> >>  /*
> >> diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
> >> index bf9c0bc..54f4ba5 100644
> >> --- a/hw/arm/virt-acpi-build.c
> >> +++ b/hw/arm/virt-acpi-build.c
> >> @@ -45,6 +45,7 @@
> >>  #include "hw/arm/virt.h"
> >>  #include "sysemu/numa.h"
> >>  #include "kvm_arm.h"
> >> +#include "hw/acpi/acpi_ghes.h"
> >>  
> >>  #define ARM_SPI_BASE 32
> >>  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
> >> @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
> >>      acpi_add_table(table_offsets, tables_blob);
> >>      build_spcr(tables_blob, tables->linker, vms);
> >>  
> >> +    if (!vmc->no_ras) {
> >> +        acpi_add_table(table_offsets, tables_blob);
> >> +        build_hardware_error_table(tables->hardware_errors, tables->linker);
> >> +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
> >> +    }
> >> +
> >>      if (nb_numa_nodes > 0) {
> >>          acpi_add_table(table_offsets, tables_blob);
> >>          build_srat(tables_blob, tables->linker, vms);
> >> @@ -901,6 +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
> >>  
> >>  void virt_acpi_setup(VirtMachineState *vms)
> >>  {
> >> +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
> >>      AcpiBuildTables tables;
> >>      AcpiBuildState *build_state;
> >>  
> >> @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
> >>      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
> >>                      acpi_data_len(tables.tcpalog));
> >>  
> >> +    if (!vmc->no_ras) {
> >> +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
> >> +    }
> >> +
> >>      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
> >>                                                ACPI_BUILD_RSDP_FILE, 0);
> >>  
> >> diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> >> new file mode 100644
> >> index 0000000..38fd87c
> >> --- /dev/null
> >> +++ b/include/hw/acpi/acpi_ghes.h
> >> @@ -0,0 +1,79 @@
> >> +/* Support for generating APEI tables and record CPER for Guests
> >> + *
> >> + * Copyright (C) 2017 HuaWei Corporation.
> >> + *
> >> + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> >> + *
> >> + * This program is free software; you can redistribute it and/or modify
> >> + * it under the terms of the GNU General Public License as published by
> >> + * the Free Software Foundation; either version 2 of the License, or
> >> + * (at your option) any later version.
> >> +
> >> + * This program is distributed in the hope that it will be useful,
> >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >> + * GNU General Public License for more details.
> >> +
> >> + * You should have received a copy of the GNU General Public License along
> >> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> >> + */
> >> +
> >> +#ifndef ACPI_GHES_H
> >> +#define ACPI_GHES_H
> >> +
> >> +#include "hw/acpi/bios-linker-loader.h"
> >> +
> >> +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
> >> +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
> >> +
> >> +/* The size of Address field in Generic Address Structure,
> >> + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
> >> + */
> >> +#define GHES_ADDRESS_SIZE           8
> >> +  
> >   
> >> +#define GHES_DATA_LENGTH            72
> >> +#define GHES_CPER_LENGTH            80  
> > add the similar comment as above if values come from spec
> > or an explanation where values come from  
>   got it.
> 
> >   
> >> +
> >> +#define ReadAckPreserve             0xfffffffe
> >> +#define ReadAckWrite                0x1  
> > ditto, and macro should be uppercased.  
> I will move above macros and folow Michael's comments to  just open-coding at use point
> and adding a comment:
> 
>         build_append_int_noprefix(table_data, cpu_to_le64(0xfffffffe), 8); /* ReadAckPreserve */
>         build_append_int_noprefix(table_data, cpu_to_le64(0x1), 8); /* ReadAckWrite */
> > 
> >   
> >> +
> >> +/* The max size in bytes for one error block */
> >> +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
> >> +/* Now only have GPIO-Signal and ARMv8 SEA notification types error sources
> >> + */
> >> +#define ACPI_HEST_ERROR_SOURCE_COUNT    2
> >> +
> >> +/*
> >> + * | +--------------------------+ 0
> >> + * | |        Header            |
> >> + * | +--------------------------+ 40---+-
> >> + * | | .................        |      |
> >> + * | | error_status_address-----+ 60   |
> >> + * | | .................        |      |
> >> + * | | read_ack_register--------+ 104  92
> >> + * | | read_ack_preserve        |      |
> >> + * | | read_ack_write           |      |
> >> + * + +--------------------------+ 132--+-
> >> + *
> >> + * From above GHES definition, the error status address offset is 60;
> >> + * the Read ack register offset is 104, the whole size of GHESv2 is 92
> >> + */
> >> +
> >> +/* The error status address offset in GHES */
> >> +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
> >> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> >> +
> >> +/* The read Ack register offset in GHES */
> >> +#define READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
> >> +                    offsetof(struct AcpiGenericAddress, address) + n * 92)
> >> +
> >> +typedef struct GhesState {
> >> +    uint64_t ghes_addr_le;
> >> +} GhesState;
> >> +
> >> +void build_apei_hest(GArray *table_data, GArray *hardware_error,
> >> +                     BIOSLinker *linker);
> >> +
> >> +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker *linker);
> >> +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_errors);
> >> +#endif
> >> diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> >> index 1ec7e1b..78c0252 100644
> >> --- a/include/hw/acpi/aml-build.h
> >> +++ b/include/hw/acpi/aml-build.h
> >> @@ -220,6 +220,7 @@ struct AcpiBuildTables {
> >>      GArray *rsdp;
> >>      GArray *tcpalog;
> >>      GArray *vmgenid;
> >> +    GArray *hardware_errors;
> >>      BIOSLinker *linker;
> >>  } AcpiBuildTables;
> >>    
> > 
> > .
> >   
> 
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support
@ 2019-06-08 19:14 gengdongjiu
  0 siblings, 0 replies; 45+ messages in thread
From: gengdongjiu @ 2019-06-08 19:14 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: peter.maydell, ehabkost, kvm, mst, mtosatti, qemu-devel,
	Linuxarm, shannon.zhaosl, zhengxiang (A),
	qemu-arm, james.morse, xuwei (O),
	imammedo, pbonzini, lersek, rth

> 
> On Tue, 14 May 2019 04:18:20 -0700
> Dongjiu Geng <gengdongjiu@huawei.com> wrote:
> 
> > This implements APEI GHES Table generation via fw_cfg blobs.
> > Now it only support GPIO-Signal and ARMv8 SEA two types of GHESv2
> > error source. Afterwards, we can extend the supported types if needed.
> > For the CPER section type, currently it is memory section because
> > kernel mainly wants userspace to handle the memory errors.
> >
> > This patch follows the spec ACPI 6.2 to build the Hardware Error
> > Source table, for the detailed information, please refer to document:
> > docs/specs/acpi_hest_ghes.txt
> >
> > Suggested-by: Laszlo Ersek <lersek@redhat.com>
> > Signed-off-by: Dongjiu Geng <gengdongjiu@huawei.com>
> 
> Just one general question, why support the GPIO-Signal error source in this patch set?  Unless I'm missing something, it's not actually
> used and would require some additional infrastructure (instantiate the GEDD or similar).

In my Initial patches, for the asynchronous error, I want to use the GPIO-Signal to notify guest that there is asynchronous error happen. But later I found QEMU will mask the SIGBUS with BUS_MCEERR_AO.
SIGBUS with BUS_MCEERR_AO means the error is asynchronous error. So GPIO-Signal notification is not used since I do not handle BUS_MCEERR_AO in the sigbus handler.

> 
> Might be better to drop it for now and simplify this set a little bit?

Ok, I can drop the GPIO-Signal support.

> 
> Note I'm using the gpio path for CCIX PER error emulation so have it hooked up.
> The code here is fine, just not used (I think).

By the way, you also use the SIGBUS with BUS_MCEERR_AO to report CCIX error to QEMU, I am afraid this signal is also masked by QEMU.

> 
> Thanks,
> 
> Jonathan
> 
> 
> 
> > ---
> >  default-configs/arm-softmmu.mak |   1 +
> >  hw/acpi/Kconfig                 |   4 +
> >  hw/acpi/Makefile.objs           |   1 +
> >  hw/acpi/acpi_ghes.c             | 171 ++++++++++++++++++++++++++++++++++++++++
> >  hw/acpi/aml-build.c             |   2 +
> >  hw/arm/virt-acpi-build.c        |  12 +++
> >  include/hw/acpi/acpi_ghes.h     |  79 +++++++++++++++++++
> >  include/hw/acpi/aml-build.h     |   1 +
> >  8 files changed, 271 insertions(+)
> >  create mode 100644 hw/acpi/acpi_ghes.c  create mode 100644
> > include/hw/acpi/acpi_ghes.h
> >
> > diff --git a/default-configs/arm-softmmu.mak
> > b/default-configs/arm-softmmu.mak index 613d19a..7b33ae9 100644
> > --- a/default-configs/arm-softmmu.mak
> > +++ b/default-configs/arm-softmmu.mak
> > @@ -160,3 +160,4 @@ CONFIG_MUSICPAL=y
> >
> >  # for realview and versatilepb
> >  CONFIG_LSI_SCSI_PCI=y
> > +CONFIG_ACPI_APEI=y
> > diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig index eca3bee..5228a4b
> > 100644
> > --- a/hw/acpi/Kconfig
> > +++ b/hw/acpi/Kconfig
> > @@ -23,6 +23,10 @@ config ACPI_NVDIMM
> >      bool
> >      depends on ACPI
> >
> > +config ACPI_APEI
> > +    bool
> > +    depends on ACPI
> > +
> >  config ACPI_VMGENID
> >      bool
> >      default y
> > diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs index
> > 2d46e37..5099ada 100644
> > --- a/hw/acpi/Makefile.objs
> > +++ b/hw/acpi/Makefile.objs
> > @@ -6,6 +6,7 @@ common-obj-$(CONFIG_ACPI_MEMORY_HOTPLUG) +=
> > memory_hotplug.o
> >  common-obj-$(CONFIG_ACPI_CPU_HOTPLUG) += cpu.o
> >  common-obj-$(CONFIG_ACPI_NVDIMM) += nvdimm.o
> >  common-obj-$(CONFIG_ACPI_VMGENID) += vmgenid.o
> > +common-obj-$(CONFIG_ACPI_APEI) += acpi_ghes.o
> >  common-obj-$(call lnot,$(CONFIG_ACPI_X86)) += acpi-stub.o
> >
> >  common-obj-y += acpi_interface.o
> > diff --git a/hw/acpi/acpi_ghes.c b/hw/acpi/acpi_ghes.c new file mode
> > 100644 index 0000000..d03e797
> > --- /dev/null
> > +++ b/hw/acpi/acpi_ghes.c
> > @@ -0,0 +1,171 @@
> > +/* Support for generating APEI tables and record CPER for Guests
> > + *
> > + * Copyright (C) 2017 HuaWei Corporation.
> > + *
> > + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> > + *
> > + * This program is free software; you can redistribute it and/or
> > +modify
> > + * it under the terms of the GNU General Public License as published
> > +by
> > + * the Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > +
> > + * This program is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + * GNU General Public License for more details.
> > +
> > + * You should have received a copy of the GNU General Public License
> > + along
> > + * with this program; if not, see <http://www.gnu.org/licenses/>.
> > + */
> > +
> > +#include "qemu/osdep.h"
> > +#include "hw/acpi/acpi.h"
> > +#include "hw/acpi/aml-build.h"
> > +#include "hw/acpi/acpi_ghes.h"
> > +#include "hw/nvram/fw_cfg.h"
> > +#include "sysemu/sysemu.h"
> > +#include "qemu/error-report.h"
> > +
> > +/* Build table for the hardware error fw_cfg blob */ void
> > +build_hardware_error_table(GArray *hardware_errors, BIOSLinker
> > +*linker) {
> > +    int i;
> > +
> > +    /*
> > +     * | +--------------------------+
> > +     * | |    error_block_address   |
> > +     * | |      ..........          |
> > +     * | +--------------------------+
> > +     * | |    read_ack_register     |
> > +     * | |     ...........          |
> > +     * | +--------------------------+
> > +     * | |  Error Status Data Block |
> > +     * | |      ........            |
> > +     * | +--------------------------+
> > +     */
> > +
> > +    /* Build error_block_address */
> > +    build_append_int_noprefix((void *)hardware_errors, 0,
> > +                    GHES_ADDRESS_SIZE *
> > + ACPI_HEST_ERROR_SOURCE_COUNT);
> > +
> > +    /* Build read_ack_register */
> > +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> > +        /* Initialize the value of read_ack_register to 1, so GHES can be
> > +         * writeable in the first time
> > +         */
> > +        build_append_int_noprefix((void *)hardware_errors, 1,
> > + GHES_ADDRESS_SIZE);
> > +
> > +     /* Build Error Status Data Block */
> > +    build_append_int_noprefix((void *)hardware_errors, 0,
> > +                    GHES_MAX_RAW_DATA_LENGTH *
> > + ACPI_HEST_ERROR_SOURCE_COUNT);
> > +
> > +    /* Allocate guest memory for the hardware error fw_cfg blob */
> > +    bios_linker_loader_alloc(linker, GHES_ERRORS_FW_CFG_FILE, hardware_errors,
> > +                            1, false); }
> > +
> > +/* Build Hardware Error Source Table */ void build_apei_hest(GArray
> > +*table_data, GArray *hardware_errors,
> > +                                            BIOSLinker *linker) {
> > +    uint32_t i, error_status_block_offset, length = table_data->len;
> > +
> > +    /* Reserve Hardware Error Source Table header size */
> > +    acpi_data_push(table_data, sizeof(AcpiTableHeader));
> > +
> > +    /* Set the error source counts */
> > +    build_append_int_noprefix(table_data,
> > + ACPI_HEST_ERROR_SOURCE_COUNT, 4);
> > +
> > +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++) {
> > +        /* Generic Hardware Error Source version 2(GHESv2 - Type 10)
> > +         */
> > +        build_append_int_noprefix(table_data,
> > +            ACPI_HEST_SOURCE_GENERIC_ERROR_V2, 2); /* type */
> > +        build_append_int_noprefix(table_data, cpu_to_le16(i), 2); /* source id */
> > +        build_append_int_noprefix(table_data, 0xffff, 2); /* related source id */
> > +        build_append_int_noprefix(table_data, 0, 1); /* flags */
> > +
> > +        build_append_int_noprefix(table_data, 1, 1); /* enabled */
> > +
> > +        /* Number of Records To Pre-allocate */
> > +        build_append_int_noprefix(table_data, 1, 4);
> > +        /* Max Sections Per Record */
> > +        build_append_int_noprefix(table_data, 1, 4);
> > +        /* Max Raw Data Length */
> > +        build_append_int_noprefix(table_data,
> > + GHES_MAX_RAW_DATA_LENGTH, 4);
> > +
> > +        /* Build error status address*/
> > +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> > +        bios_linker_loader_add_pointer(linker,
> > +            ACPI_BUILD_TABLE_FILE, ERROR_STATUS_ADDRESS_OFFSET(length, i),
> > +            GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, i *
> > + GHES_ADDRESS_SIZE);
> > +
> > +        /* Build Hardware Error Notification
> > +         * Now only enable GPIO-Signal and ARMv8 SEA notification types
> > +         */
> > +        if (i == 0) {
> > +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_GPIO, 28,
> > +                                     0, 0, 0, 0, 0, 0, 0);
> > +        } else if (i == 1) {
> > +            build_append_ghes_notify(table_data, ACPI_HEST_NOTIFY_SEA, 28, 0,
> > +                                     0, 0, 0, 0, 0, 0);
> > +        }
> > +
> > +        /* Error Status Block Length */
> > +        build_append_int_noprefix(table_data,
> > +            cpu_to_le32(GHES_MAX_RAW_DATA_LENGTH), 4);
> > +
> > +        /* Build Read ACK register
> > +         * ACPI 6.1/6.2: 18.3.2.8 Generic Hardware Error Source
> > +         * version 2 (GHESv2 - Type 10)
> > +         */
> > +        build_append_gas(table_data, AML_SYSTEM_MEMORY, 0x40, 0, 4 /* QWord access */, 0);
> > +        bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> > +            READ_ACK_REGISTER_ADDRESS_OFFSET(length, i), GHES_ADDRESS_SIZE,
> > +            GHES_ERRORS_FW_CFG_FILE,
> > +            (ACPI_HEST_ERROR_SOURCE_COUNT + i) * GHES_ADDRESS_SIZE);
> > +
> > +        /* Build Read Ack Preserve and Read Ack Writer */
> > +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckPreserve), 8);
> > +        build_append_int_noprefix(table_data, cpu_to_le64(ReadAckWrite), 8);
> > +    }
> > +
> > +    /* Generic Error Status Block offset in the hardware error fw_cfg blob */
> > +    error_status_block_offset = GHES_ADDRESS_SIZE * 2 *
> > +                                ACPI_HEST_ERROR_SOURCE_COUNT;
> > +
> > +    for (i = 0; i < ACPI_HEST_ERROR_SOURCE_COUNT; i++)
> > +        /* Patch address of Error Status Data Block into
> > +         * the error_block_address of hardware_errors fw_cfg blob
> > +         */
> > +        bios_linker_loader_add_pointer(linker,
> > +            GHES_ERRORS_FW_CFG_FILE, GHES_ADDRESS_SIZE * i, GHES_ADDRESS_SIZE,
> > +            GHES_ERRORS_FW_CFG_FILE,
> > +            error_status_block_offset + i *
> > + GHES_MAX_RAW_DATA_LENGTH);
> > +
> > +    /* write address of hardware_errors fw_cfg blob into the
> > +     * hardware_errors_addr fw_cfg blob.
> > +     */
> > +    bios_linker_loader_write_pointer(linker, GHES_DATA_ADDR_FW_CFG_FILE,
> > +        0, GHES_ADDRESS_SIZE, GHES_ERRORS_FW_CFG_FILE, 0);
> > +
> > +    build_header(linker, table_data,
> > +        (void *)(table_data->data + length), "HEST",
> > +        table_data->len - length, 1, NULL, "GHES"); }
> > +
> > +static GhesState ges;
> > +void ghes_add_fw_cfg(FWCfgState *s, GArray *hardware_error) {
> > +
> > +    size_t size = 2 * GHES_ADDRESS_SIZE + GHES_MAX_RAW_DATA_LENGTH;
> > +    size_t request_block_size = ACPI_HEST_ERROR_SOURCE_COUNT * size;
> > +
> > +    /* Create a read-only fw_cfg file for GHES */
> > +    fw_cfg_add_file(s, GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
> > +                    request_block_size);
> > +
> > +    /* Create a read-write fw_cfg file for Address */
> > +    fw_cfg_add_file_callback(s, GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
> > +        &ges.ghes_addr_le, sizeof(ges.ghes_addr_le), false); }
> > diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index
> > ce90970..3f2b84f 100644
> > --- a/hw/acpi/aml-build.c
> > +++ b/hw/acpi/aml-build.c
> > @@ -1645,6 +1645,7 @@ void acpi_build_tables_init(AcpiBuildTables *tables)
> >      tables->table_data = g_array_new(false, true /* clear */, 1);
> >      tables->tcpalog = g_array_new(false, true /* clear */, 1);
> >      tables->vmgenid = g_array_new(false, true /* clear */, 1);
> > +    tables->hardware_errors = g_array_new(false, true /* clear */,
> > + 1);
> >      tables->linker = bios_linker_loader_init();  }
> >
> > @@ -1655,6 +1656,7 @@ void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
> >      g_array_free(tables->table_data, true);
> >      g_array_free(tables->tcpalog, mfre);
> >      g_array_free(tables->vmgenid, mfre);
> > +    g_array_free(tables->hardware_errors, mfre);
> >  }
> >
> >  /*
> > diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c index
> > bf9c0bc..54f4ba5 100644
> > --- a/hw/arm/virt-acpi-build.c
> > +++ b/hw/arm/virt-acpi-build.c
> > @@ -45,6 +45,7 @@
> >  #include "hw/arm/virt.h"
> >  #include "sysemu/numa.h"
> >  #include "kvm_arm.h"
> > +#include "hw/acpi/acpi_ghes.h"
> >
> >  #define ARM_SPI_BASE 32
> >  #define ACPI_POWER_BUTTON_DEVICE "PWRB"
> > @@ -808,6 +809,12 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
> >      acpi_add_table(table_offsets, tables_blob);
> >      build_spcr(tables_blob, tables->linker, vms);
> >
> > +    if (!vmc->no_ras) {
> > +        acpi_add_table(table_offsets, tables_blob);
> > +        build_hardware_error_table(tables->hardware_errors, tables->linker);
> > +        build_apei_hest(tables_blob, tables->hardware_errors, tables->linker);
> > +    }
> > +
> >      if (nb_numa_nodes > 0) {
> >          acpi_add_table(table_offsets, tables_blob);
> >          build_srat(tables_blob, tables->linker, vms); @@ -901,6
> > +908,7 @@ static const VMStateDescription vmstate_virt_acpi_build = {
> >
> >  void virt_acpi_setup(VirtMachineState *vms)  {
> > +    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
> >      AcpiBuildTables tables;
> >      AcpiBuildState *build_state;
> >
> > @@ -932,6 +940,10 @@ void virt_acpi_setup(VirtMachineState *vms)
> >      fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
> >                      acpi_data_len(tables.tcpalog));
> >
> > +    if (!vmc->no_ras) {
> > +        ghes_add_fw_cfg(vms->fw_cfg, tables.hardware_errors);
> > +    }
> > +
> >      build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
> >                                                ACPI_BUILD_RSDP_FILE,
> > 0);
> >
> > diff --git a/include/hw/acpi/acpi_ghes.h b/include/hw/acpi/acpi_ghes.h
> > new file mode 100644 index 0000000..38fd87c
> > --- /dev/null
> > +++ b/include/hw/acpi/acpi_ghes.h
> > @@ -0,0 +1,79 @@
> > +/* Support for generating APEI tables and record CPER for Guests
> > + *
> > + * Copyright (C) 2017 HuaWei Corporation.
> > + *
> > + * Author: Dongjiu Geng <gengdongjiu@huawei.com>
> > + *
> > + * This program is free software; you can redistribute it and/or
> > +modify
> > + * it under the terms of the GNU General Public License as published
> > +by
> > + * the Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > +
> > + * This program is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + * GNU General Public License for more details.
> > +
> > + * You should have received a copy of the GNU General Public License
> > + along
> > + * with this program; if not, see <http://www.gnu.org/licenses/>.
> > + */
> > +
> > +#ifndef ACPI_GHES_H
> > +#define ACPI_GHES_H
> > +
> > +#include "hw/acpi/bios-linker-loader.h"
> > +
> > +#define GHES_ERRORS_FW_CFG_FILE         "etc/hardware_errors"
> > +#define GHES_DATA_ADDR_FW_CFG_FILE      "etc/hardware_errors_addr"
> > +
> > +/* The size of Address field in Generic Address Structure,
> > + * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure.
> > + */
> > +#define GHES_ADDRESS_SIZE           8
> > +
> > +#define GHES_DATA_LENGTH            72
> > +#define GHES_CPER_LENGTH            80
> > +
> > +#define ReadAckPreserve             0xfffffffe
> > +#define ReadAckWrite                0x1
> > +
> > +/* The max size in bytes for one error block */
> > +#define GHES_MAX_RAW_DATA_LENGTH        0x1000
> > +/* Now only have GPIO-Signal and ARMv8 SEA notification types error
> > +sources  */
> > +#define ACPI_HEST_ERROR_SOURCE_COUNT    2
> 
> The GPIO case is really handy when I'm abusing this code for error injection but I don't think you actually use it in this patch set.  Perhaps it
> makes sense to drop that block for now?
> 
> > +
> > +/*
> > + * | +--------------------------+ 0
> > + * | |        Header            |
> > + * | +--------------------------+ 40---+-
> > + * | | .................        |      |
> > + * | | error_status_address-----+ 60   |
> > + * | | .................        |      |
> > + * | | read_ack_register--------+ 104  92
> > + * | | read_ack_preserve        |      |
> > + * | | read_ack_write           |      |
> > + * + +--------------------------+ 132--+-
> > + *
> > + * From above GHES definition, the error status address offset is 60;
> > + * the Read ack register offset is 104, the whole size of GHESv2 is
> > +92  */
> > +
> > +/* The error status address offset in GHES */
> > +#define ERROR_STATUS_ADDRESS_OFFSET(start_addr, n)     (start_addr + 60 + \
> > +                    offsetof(struct AcpiGenericAddress, address) + n
> > +* 92)
> > +
> > +/* The read Ack register offset in GHES */ #define
> > +READ_ACK_REGISTER_ADDRESS_OFFSET(start_addr, n) (start_addr + 104 + \
> > +                    offsetof(struct AcpiGenericAddress, address) + n
> > +* 92)
> > +
> > +typedef struct GhesState {
> > +    uint64_t ghes_addr_le;
> > +} GhesState;
> > +
> > +void build_apei_hest(GArray *table_data, GArray *hardware_error,
> > +                     BIOSLinker *linker);
> > +
> > +void build_hardware_error_table(GArray *hardware_errors, BIOSLinker
> > +*linker); void ghes_add_fw_cfg(FWCfgState *s, GArray
> > +*hardware_errors); #endif
> > diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h
> > index 1ec7e1b..78c0252 100644
> > --- a/include/hw/acpi/aml-build.h
> > +++ b/include/hw/acpi/aml-build.h
> > @@ -220,6 +220,7 @@ struct AcpiBuildTables {
> >      GArray *rsdp;
> >      GArray *tcpalog;
> >      GArray *vmgenid;
> > +    GArray *hardware_errors;
> >      BIOSLinker *linker;
> >  } AcpiBuildTables;
> >
> 



^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2019-06-26 14:27 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-14 11:18 [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU Dongjiu Geng
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 01/10] hw/arm/virt: Add RAS platform version for migration Dongjiu Geng
2019-06-20 12:04   ` Igor Mammedov
2019-06-24 12:19     ` gengdongjiu
2019-06-25 13:16       ` Igor Mammedov
2019-06-25 13:29         ` gengdongjiu
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 02/10] ACPI: add some GHES structures and macros definition Dongjiu Geng
2019-05-29  3:40   ` Michael S. Tsirkin
2019-05-30 14:58     ` gengdongjiu
2019-06-20 12:10   ` Igor Mammedov
2019-06-20 14:04     ` gengdongjiu
2019-06-20 15:09       ` Igor Mammedov
2019-06-20 17:17         ` gengdongjiu
2019-06-24 11:16           ` Igor Mammedov
2019-06-25  9:56             ` gengdongjiu
2019-06-25 13:33               ` Igor Mammedov
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 03/10] acpi: add build_append_ghes_notify() helper for Hardware Error Notification Dongjiu Geng
2019-06-24 11:21   ` Igor Mammedov
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 04/10] acpi: add build_append_ghes_generic_data() helper for Generic Error Data Entry Dongjiu Geng
2019-06-20 12:28   ` Igor Mammedov
2019-06-24 12:37     ` gengdongjiu
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 05/10] acpi: add build_append_ghes_generic_status() helper for Generic Error Status Block Dongjiu Geng
2019-06-20 12:42   ` Igor Mammedov
2019-06-25 12:11     ` gengdongjiu
2019-06-25 13:41       ` Igor Mammedov
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 06/10] docs: APEI GHES generation and CPER record description Dongjiu Geng
2019-06-24 11:39   ` Igor Mammedov
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support Dongjiu Geng
2019-05-29  3:37   ` Michael S. Tsirkin
2019-05-30 14:47     ` gengdongjiu
2019-06-06 13:43   ` Jonathan Cameron
2019-06-24 12:27   ` Igor Mammedov
2019-06-25 13:48     ` gengdongjiu
2019-06-26 14:25       ` Igor Mammedov
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 08/10] KVM: Move related hwpoison page functions to accel/kvm/ folder Dongjiu Geng
2019-06-24 12:32   ` Igor Mammedov
2019-06-25 12:28     ` gengdongjiu
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 09/10] target-arm: kvm64: inject synchronous External Abort Dongjiu Geng
2019-05-14 11:18 ` [Qemu-devel] [PATCH v17 10/10] target-arm: kvm64: handle SIGBUS signal from kernel or KVM Dongjiu Geng
2019-06-06 13:31   ` Jonathan Cameron
2019-06-24 13:08   ` Igor Mammedov
2019-06-25 12:24     ` gengdongjiu
2019-06-25 13:32       ` Igor Mammedov
2019-05-15  9:40 ` [Qemu-devel] [PATCH v17 00/10] Add ARMv8 RAS virtualization support in QEMU gengdongjiu
2019-06-08 19:14 [Qemu-devel] [PATCH v17 07/10] ACPI: Add APEI GHES table generation support gengdongjiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).