From patchwork Thu Jun 30 10:02:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901535 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEAE4C433EF for ; Thu, 30 Jun 2022 10:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234246AbiF3KDz (ORCPT ); Thu, 30 Jun 2022 06:03:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234703AbiF3KDy (ORCPT ); Thu, 30 Jun 2022 06:03:54 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7F4673B3C1 for ; Thu, 30 Jun 2022 03:03:51 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 63EC81063; Thu, 30 Jun 2022 03:03:51 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1B6A33F5A1; Thu, 30 Jun 2022 03:03:50 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 01/27] lib: Fix style for acpi.{c,h} Date: Thu, 30 Jun 2022 11:02:58 +0100 Message-Id: <20220630100324.3153655-2-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Signed-off-by: Nikos Nikoleris --- lib/acpi.h | 148 ++++++++++++++++++++++++++--------------------------- lib/acpi.c | 70 ++++++++++++------------- 2 files changed, 108 insertions(+), 110 deletions(-) diff --git a/lib/acpi.h b/lib/acpi.h index 1e89840..456e62d 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -3,7 +3,7 @@ #include "libcflat.h" -#define ACPI_SIGNATURE(c1, c2, c3, c4) \ +#define ACPI_SIGNATURE(c1, c2, c3, c4) \ ((c1) | ((c2) << 8) | ((c3) << 16) | ((c4) << 24)) #define RSDP_SIGNATURE ACPI_SIGNATURE('R','S','D','P') @@ -12,98 +12,98 @@ #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S') -#define ACPI_SIGNATURE_8BYTE(c1, c2, c3, c4, c5, c6, c7, c8) \ - ((uint64_t)(ACPI_SIGNATURE(c1, c2, c3, c4))) | \ +#define ACPI_SIGNATURE_8BYTE(c1, c2, c3, c4, c5, c6, c7, c8) \ + ((uint64_t)(ACPI_SIGNATURE(c1, c2, c3, c4))) | \ ((uint64_t)(ACPI_SIGNATURE(c5, c6, c7, c8)) << 32) #define RSDP_SIGNATURE_8BYTE (ACPI_SIGNATURE_8BYTE('R', 'S', 'D', ' ', 'P', 'T', 'R', ' ')) -struct rsdp_descriptor { /* Root System Descriptor Pointer */ - u64 signature; /* ACPI signature, contains "RSD PTR " */ - u8 checksum; /* To make sum of struct == 0 */ - u8 oem_id [6]; /* OEM identification */ - u8 revision; /* Must be 0 for 1.0, 2 for 2.0 */ - u32 rsdt_physical_address; /* 32-bit physical address of RSDT */ - u32 length; /* XSDT Length in bytes including hdr */ - u64 xsdt_physical_address; /* 64-bit physical address of XSDT */ - u8 extended_checksum; /* Checksum of entire table */ - u8 reserved [3]; /* Reserved field must be 0 */ +struct rsdp_descriptor { /* Root System Descriptor Pointer */ + u64 signature; /* ACPI signature, contains "RSD PTR " */ + u8 checksum; /* To make sum of struct == 0 */ + u8 oem_id [6]; /* OEM identification */ + u8 revision; /* Must be 0 for 1.0, 2 for 2.0 */ + u32 rsdt_physical_address; /* 32-bit physical address of RSDT */ + u32 length; /* XSDT Length in bytes including hdr */ + u64 xsdt_physical_address; /* 64-bit physical address of XSDT */ + u8 extended_checksum; /* Checksum of entire table */ + u8 reserved [3]; /* Reserved field must be 0 */ }; -#define ACPI_TABLE_HEADER_DEF /* ACPI common table header */ \ - u32 signature; /* ACPI signature (4 ASCII characters) */ \ - u32 length; /* Length of table, in bytes, including header */ \ - u8 revision; /* ACPI Specification minor version # */ \ - u8 checksum; /* To make sum of entire table == 0 */ \ - u8 oem_id [6]; /* OEM identification */ \ - u8 oem_table_id [8]; /* OEM table identification */ \ - u32 oem_revision; /* OEM revision number */ \ - u8 asl_compiler_id [4]; /* ASL compiler vendor ID */ \ - u32 asl_compiler_revision; /* ASL compiler revision number */ +#define ACPI_TABLE_HEADER_DEF /* ACPI common table header */ \ + u32 signature; /* ACPI signature (4 ASCII characters) */ \ + u32 length; /* Length of table, in bytes, including header */ \ + u8 revision; /* ACPI Specification minor version # */ \ + u8 checksum; /* To make sum of entire table == 0 */ \ + u8 oem_id [6]; /* OEM identification */ \ + u8 oem_table_id [8]; /* OEM table identification */ \ + u32 oem_revision; /* OEM revision number */ \ + u8 asl_compiler_id [4]; /* ASL compiler vendor ID */ \ + u32 asl_compiler_revision; /* ASL compiler revision number */ struct acpi_table { - ACPI_TABLE_HEADER_DEF - char data[0]; + ACPI_TABLE_HEADER_DEF + char data[0]; }; struct rsdt_descriptor_rev1 { - ACPI_TABLE_HEADER_DEF - u32 table_offset_entry[0]; + ACPI_TABLE_HEADER_DEF + u32 table_offset_entry[1]; }; struct fadt_descriptor_rev1 { - ACPI_TABLE_HEADER_DEF /* ACPI common table header */ - u32 firmware_ctrl; /* Physical address of FACS */ - u32 dsdt; /* Physical address of DSDT */ - u8 model; /* System Interrupt Model */ - u8 reserved1; /* Reserved */ - u16 sci_int; /* System vector of SCI interrupt */ - u32 smi_cmd; /* Port address of SMI command port */ - u8 acpi_enable; /* Value to write to smi_cmd to enable ACPI */ - u8 acpi_disable; /* Value to write to smi_cmd to disable ACPI */ - u8 S4bios_req; /* Value to write to SMI CMD to enter S4BIOS state */ - u8 reserved2; /* Reserved - must be zero */ - u32 pm1a_evt_blk; /* Port address of Power Mgt 1a acpi_event Reg Blk */ - u32 pm1b_evt_blk; /* Port address of Power Mgt 1b acpi_event Reg Blk */ - u32 pm1a_cnt_blk; /* Port address of Power Mgt 1a Control Reg Blk */ - u32 pm1b_cnt_blk; /* Port address of Power Mgt 1b Control Reg Blk */ - u32 pm2_cnt_blk; /* Port address of Power Mgt 2 Control Reg Blk */ - u32 pm_tmr_blk; /* Port address of Power Mgt Timer Ctrl Reg Blk */ - u32 gpe0_blk; /* Port addr of General Purpose acpi_event 0 Reg Blk */ - u32 gpe1_blk; /* Port addr of General Purpose acpi_event 1 Reg Blk */ - u8 pm1_evt_len; /* Byte length of ports at pm1_x_evt_blk */ - u8 pm1_cnt_len; /* Byte length of ports at pm1_x_cnt_blk */ - u8 pm2_cnt_len; /* Byte Length of ports at pm2_cnt_blk */ - u8 pm_tmr_len; /* Byte Length of ports at pm_tm_blk */ - u8 gpe0_blk_len; /* Byte Length of ports at gpe0_blk */ - u8 gpe1_blk_len; /* Byte Length of ports at gpe1_blk */ - u8 gpe1_base; /* Offset in gpe model where gpe1 events start */ - u8 reserved3; /* Reserved */ - u16 plvl2_lat; /* Worst case HW latency to enter/exit C2 state */ - u16 plvl3_lat; /* Worst case HW latency to enter/exit C3 state */ - u16 flush_size; /* Size of area read to flush caches */ - u16 flush_stride; /* Stride used in flushing caches */ - u8 duty_offset; /* Bit location of duty cycle field in p_cnt reg */ - u8 duty_width; /* Bit width of duty cycle field in p_cnt reg */ - u8 day_alrm; /* Index to day-of-month alarm in RTC CMOS RAM */ - u8 mon_alrm; /* Index to month-of-year alarm in RTC CMOS RAM */ - u8 century; /* Index to century in RTC CMOS RAM */ - u8 reserved4; /* Reserved */ - u8 reserved4a; /* Reserved */ - u8 reserved4b; /* Reserved */ + ACPI_TABLE_HEADER_DEF /* ACPI common table header */ + u32 firmware_ctrl; /* Physical address of FACS */ + u32 dsdt; /* Physical address of DSDT */ + u8 model; /* System Interrupt Model */ + u8 reserved1; /* Reserved */ + u16 sci_int; /* System vector of SCI interrupt */ + u32 smi_cmd; /* Port address of SMI command port */ + u8 acpi_enable; /* Value to write to smi_cmd to enable ACPI */ + u8 acpi_disable; /* Value to write to smi_cmd to disable ACPI */ + u8 S4bios_req; /* Value to write to SMI CMD to enter S4BIOS state */ + u8 reserved2; /* Reserved - must be zero */ + u32 pm1a_evt_blk; /* Port address of Power Mgt 1a acpi_event Reg Blk */ + u32 pm1b_evt_blk; /* Port address of Power Mgt 1b acpi_event Reg Blk */ + u32 pm1a_cnt_blk; /* Port address of Power Mgt 1a Control Reg Blk */ + u32 pm1b_cnt_blk; /* Port address of Power Mgt 1b Control Reg Blk */ + u32 pm2_cnt_blk; /* Port address of Power Mgt 2 Control Reg Blk */ + u32 pm_tmr_blk; /* Port address of Power Mgt Timer Ctrl Reg Blk */ + u32 gpe0_blk; /* Port addr of General Purpose acpi_event 0 Reg Blk */ + u32 gpe1_blk; /* Port addr of General Purpose acpi_event 1 Reg Blk */ + u8 pm1_evt_len; /* Byte length of ports at pm1_x_evt_blk */ + u8 pm1_cnt_len; /* Byte length of ports at pm1_x_cnt_blk */ + u8 pm2_cnt_len; /* Byte Length of ports at pm2_cnt_blk */ + u8 pm_tmr_len; /* Byte Length of ports at pm_tm_blk */ + u8 gpe0_blk_len; /* Byte Length of ports at gpe0_blk */ + u8 gpe1_blk_len; /* Byte Length of ports at gpe1_blk */ + u8 gpe1_base; /* Offset in gpe model where gpe1 events start */ + u8 reserved3; /* Reserved */ + u16 plvl2_lat; /* Worst case HW latency to enter/exit C2 state */ + u16 plvl3_lat; /* Worst case HW latency to enter/exit C3 state */ + u16 flush_size; /* Size of area read to flush caches */ + u16 flush_stride; /* Stride used in flushing caches */ + u8 duty_offset; /* Bit location of duty cycle field in p_cnt reg */ + u8 duty_width; /* Bit width of duty cycle field in p_cnt reg */ + u8 day_alrm; /* Index to day-of-month alarm in RTC CMOS RAM */ + u8 mon_alrm; /* Index to month-of-year alarm in RTC CMOS RAM */ + u8 century; /* Index to century in RTC CMOS RAM */ + u8 reserved4; /* Reserved */ + u8 reserved4a; /* Reserved */ + u8 reserved4b; /* Reserved */ }; struct facs_descriptor_rev1 { - u32 signature; /* ACPI Signature */ - u32 length; /* Length of structure, in bytes */ - u32 hardware_signature; /* Hardware configuration signature */ - u32 firmware_waking_vector; /* ACPI OS waking vector */ - u32 global_lock; /* Global Lock */ - u32 S4bios_f : 1; /* Indicates if S4BIOS support is present */ - u32 reserved1 : 31; /* Must be 0 */ - u8 reserved3 [40]; /* Reserved - must be zero */ + u32 signature; /* ACPI Signature */ + u32 length; /* Length of structure, in bytes */ + u32 hardware_signature; /* Hardware configuration signature */ + u32 firmware_waking_vector; /* ACPI OS waking vector */ + u32 global_lock; /* Global Lock */ + u32 S4bios_f : 1; /* Indicates if S4BIOS support is present */ + u32 reserved1 : 31; /* Must be 0 */ + u8 reserved3 [40]; /* Reserved - must be zero */ }; void set_efi_rsdp(struct rsdp_descriptor *rsdp); diff --git a/lib/acpi.c b/lib/acpi.c index de275ca..b7fd923 100644 --- a/lib/acpi.c +++ b/lib/acpi.c @@ -36,47 +36,45 @@ static struct rsdp_descriptor *get_rsdp(void) } #endif /* CONFIG_EFI */ -void* find_acpi_table_addr(u32 sig) +void *find_acpi_table_addr(u32 sig) { - struct rsdp_descriptor *rsdp; - struct rsdt_descriptor_rev1 *rsdt; - void *end; - int i; + struct rsdp_descriptor *rsdp; + struct rsdt_descriptor_rev1 *rsdt; + void *end; + int i; - /* FACS is special... */ - if (sig == FACS_SIGNATURE) { - struct fadt_descriptor_rev1 *fadt; - fadt = find_acpi_table_addr(FACP_SIGNATURE); - if (!fadt) { - return NULL; - } - return (void*)(ulong)fadt->firmware_ctrl; - } + /* FACS is special... */ + if (sig == FACS_SIGNATURE) { + struct fadt_descriptor_rev1 *fadt; + fadt = find_acpi_table_addr(FACP_SIGNATURE); + if (!fadt) + return NULL; - rsdp = get_rsdp(); - if (rsdp == NULL) { - printf("Can't find RSDP\n"); - return 0; - } + return (void *)(ulong)fadt->firmware_ctrl; + } - if (sig == RSDP_SIGNATURE) { - return rsdp; - } + rsdp = get_rsdp(); + if (rsdp == NULL) { + printf("Can't find RSDP\n"); + return NULL; + } - rsdt = (void*)(ulong)rsdp->rsdt_physical_address; - if (!rsdt || rsdt->signature != RSDT_SIGNATURE) - return 0; + if (sig == RSDP_SIGNATURE) + return rsdp; - if (sig == RSDT_SIGNATURE) { - return rsdt; - } + rsdt = (void *)(ulong)rsdp->rsdt_physical_address; + if (!rsdt || rsdt->signature != RSDT_SIGNATURE) + return NULL; + + if (sig == RSDT_SIGNATURE) + return rsdt; - end = (void*)rsdt + rsdt->length; - for (i=0; (void*)&rsdt->table_offset_entry[i] < end; i++) { - struct acpi_table *t = (void*)(ulong)rsdt->table_offset_entry[i]; - if (t && t->signature == sig) { - return t; - } - } - return NULL; + end = (void *)rsdt + rsdt->length; + for (i = 0; (void *)&rsdt->table_offset_entry[i] < end; i++) { + struct acpi_table *t = (void *)(ulong)rsdt->table_offset_entry[i]; + if (t && t->signature == sig) { + return t; + } + } + return NULL; } From patchwork Thu Jun 30 10:02:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901536 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13B11C43334 for ; Thu, 30 Jun 2022 10:03:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234703AbiF3KD6 (ORCPT ); Thu, 30 Jun 2022 06:03:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234701AbiF3KD4 (ORCPT ); Thu, 30 Jun 2022 06:03:56 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AD7D2443D2 for ; Thu, 30 Jun 2022 03:03:52 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 865941BF7; Thu, 30 Jun 2022 03:03:52 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 583623F5A1; Thu, 30 Jun 2022 03:03:51 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 02/27] x86: Avoid references to fields of ACPI tables Date: Thu, 30 Jun 2022 11:02:59 +0100 Message-Id: <20220630100324.3153655-3-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org ACPI table definitions in have to be packed. However, once we do that, direct references to members of the packed struct might result in unaligned pointers and gcc complains about them. This change modifies the code to avoid such references in preparation for making the APCI table definitions packed. Signed-off-by: Nikos Nikoleris --- x86/s3.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/x86/s3.c b/x86/s3.c index 378d37a..0bcbe18 100644 --- a/x86/s3.c +++ b/x86/s3.c @@ -2,15 +2,6 @@ #include "acpi.h" #include "asm/io.h" -static u32* find_resume_vector_addr(void) -{ - struct facs_descriptor_rev1 *facs = find_acpi_table_addr(FACS_SIGNATURE); - if (!facs) - return 0; - printf("FACS is at %p\n", facs); - return &facs->firmware_waking_vector; -} - #define RTC_SECONDS_ALARM 1 #define RTC_MINUTES_ALARM 3 #define RTC_HOURS_ALARM 5 @@ -40,12 +31,14 @@ extern char resume_start, resume_end; int main(int argc, char **argv) { struct fadt_descriptor_rev1 *fadt = find_acpi_table_addr(FACP_SIGNATURE); - volatile u32 *resume_vector_ptr = find_resume_vector_addr(); + struct facs_descriptor_rev1 *facs = find_acpi_table_addr(FACS_SIGNATURE); char *addr, *resume_vec = (void*)0x1000; - *resume_vector_ptr = (u32)(ulong)resume_vec; + assert(facs); + facs->firmware_waking_vector = (u32)(ulong)resume_vec; - printf("resume vector addr is %p\n", resume_vector_ptr); + printf("FACS is at %p\n", facs); + printf("resume vector addr is %p\n", &facs->firmware_waking_vector); for (addr = &resume_start; addr < &resume_end; addr++) *resume_vec++ = *addr; printf("copy resume code from %p\n", &resume_start); From patchwork Thu Jun 30 10:03:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29B77C433EF for ; Thu, 30 Jun 2022 10:04:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234705AbiF3KD7 (ORCPT ); Thu, 30 Jun 2022 06:03:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234710AbiF3KD4 (ORCPT ); Thu, 30 Jun 2022 06:03:56 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E3AA43EEF for ; Thu, 30 Jun 2022 03:03:53 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A8F8F1C01; Thu, 30 Jun 2022 03:03:53 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7A9BB3F5A1; Thu, 30 Jun 2022 03:03:52 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 03/27] lib: Ensure all struct definition for ACPI tables are packed Date: Thu, 30 Jun 2022 11:03:00 +0100 Message-Id: <20220630100324.3153655-4-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org All ACPI table definitions are provided with precise definitions of field sizes and offsets, make sure that no compiler optimization can interfere with the memory layout of the corresponding structs. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/acpi.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/acpi.h b/lib/acpi.h index 456e62d..b853a55 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -3,6 +3,12 @@ #include "libcflat.h" +/* + * All tables and structures must be byte-packed to match the ACPI + * specification, since the tables are provided by the system BIOS + */ +#pragma pack(1) + #define ACPI_SIGNATURE(c1, c2, c3, c4) \ ((c1) | ((c2) << 8) | ((c3) << 16) | ((c4) << 24)) @@ -106,6 +112,8 @@ struct facs_descriptor_rev1 u8 reserved3 [40]; /* Reserved - must be zero */ }; +#pragma pack(0) + void set_efi_rsdp(struct rsdp_descriptor *rsdp); void* find_acpi_table_addr(u32 sig); From patchwork Thu Jun 30 10:03:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901538 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53CDAC43334 for ; Thu, 30 Jun 2022 10:04:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234714AbiF3KEA (ORCPT ); Thu, 30 Jun 2022 06:04:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234307AbiF3KD5 (ORCPT ); Thu, 30 Jun 2022 06:03:57 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D2E532A27E for ; Thu, 30 Jun 2022 03:03:54 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CAF3E1042; Thu, 30 Jun 2022 03:03:54 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9D6AA3F5A1; Thu, 30 Jun 2022 03:03:53 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 04/27] lib: Add support for the XSDT ACPI table Date: Thu, 30 Jun 2022 11:03:01 +0100 Message-Id: <20220630100324.3153655-5-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org XSDT provides pointers to other ACPI tables much like RSDT. However, contrary to RSDT that provides 32-bit addresses, XSDT provides 64-bit pointers. ACPI requires that if XSDT is valid then it takes precedence over RSDT. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/acpi.h | 6 ++++++ lib/acpi.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/acpi.h b/lib/acpi.h index b853a55..c5f0aa5 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -14,6 +14,7 @@ #define RSDP_SIGNATURE ACPI_SIGNATURE('R','S','D','P') #define RSDT_SIGNATURE ACPI_SIGNATURE('R','S','D','T') +#define XSDT_SIGNATURE ACPI_SIGNATURE('X','S','D','T') #define FACP_SIGNATURE ACPI_SIGNATURE('F','A','C','P') #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S') @@ -57,6 +58,11 @@ struct rsdt_descriptor_rev1 { u32 table_offset_entry[1]; }; +struct acpi_table_xsdt { + ACPI_TABLE_HEADER_DEF + u64 table_offset_entry[1]; +}; + struct fadt_descriptor_rev1 { ACPI_TABLE_HEADER_DEF /* ACPI common table header */ diff --git a/lib/acpi.c b/lib/acpi.c index b7fd923..240a922 100644 --- a/lib/acpi.c +++ b/lib/acpi.c @@ -40,6 +40,7 @@ void *find_acpi_table_addr(u32 sig) { struct rsdp_descriptor *rsdp; struct rsdt_descriptor_rev1 *rsdt; + struct acpi_table_xsdt *xsdt = NULL; void *end; int i; @@ -64,17 +65,41 @@ void *find_acpi_table_addr(u32 sig) rsdt = (void *)(ulong)rsdp->rsdt_physical_address; if (!rsdt || rsdt->signature != RSDT_SIGNATURE) - return NULL; + rsdt = NULL; if (sig == RSDT_SIGNATURE) return rsdt; - end = (void *)rsdt + rsdt->length; - for (i = 0; (void *)&rsdt->table_offset_entry[i] < end; i++) { - struct acpi_table *t = (void *)(ulong)rsdt->table_offset_entry[i]; - if (t && t->signature == sig) { - return t; + /* + * When the system implements APCI 2.0 and above and XSDT is + * valid we have use XSDT to find other ACPI tables, + * otherwise, we use RSDT. + */ + if (rsdp->revision == 2) + xsdt = (void *)(ulong)rsdp->xsdt_physical_address; + if (!xsdt || xsdt->signature != XSDT_SIGNATURE) + xsdt = NULL; + + if (sig == XSDT_SIGNATURE) + return xsdt; + + if (xsdt) { + end = (void *)(ulong)xsdt + xsdt->length; + for (i = 0; (void *)&xsdt->table_offset_entry[i] < end; i++) { + struct acpi_table *t = (void *)xsdt->table_offset_entry[i]; + + if (t && t->signature == sig) + return t; + } + } else if (rsdt) { + end = (void *)rsdt + rsdt->length; + for (i = 0; (void *)&rsdt->table_offset_entry[i] < end; i++) { + struct acpi_table *t = (void *)(ulong)rsdt->table_offset_entry[i]; + + if (t && t->signature == sig) + return t; } } + return NULL; } From patchwork Thu Jun 30 10:03:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901539 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8A74CCA47B for ; Thu, 30 Jun 2022 10:04:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234701AbiF3KEB (ORCPT ); Thu, 30 Jun 2022 06:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234702AbiF3KD6 (ORCPT ); Thu, 30 Jun 2022 06:03:58 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E6A603B3C1 for ; Thu, 30 Jun 2022 03:03:55 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EDD361C0A; Thu, 30 Jun 2022 03:03:55 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BFBFA3F5A1; Thu, 30 Jun 2022 03:03:54 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 05/27] lib: Extend the definition of the ACPI table FADT Date: Thu, 30 Jun 2022 11:03:02 +0100 Message-Id: <20220630100324.3153655-6-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change add more fields in the APCI table FADT to allow for the discovery of the PSCI conduit in arm64 systems. The definition for FADT is similar to the one in include/acpi/actbl.h in Linux. Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller Reviewed-by: Andrew Jones --- lib/acpi.h | 33 +++++++++++++++++++++++++++++---- lib/acpi.c | 3 ++- x86/s3.c | 2 +- x86/vmexit.c | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/acpi.h b/lib/acpi.h index c5f0aa5..e49647b 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -63,7 +63,15 @@ struct acpi_table_xsdt { u64 table_offset_entry[1]; }; -struct fadt_descriptor_rev1 +struct acpi_generic_address { + u8 space_id; /* Address space where struct or register exists */ + u8 bit_width; /* Size in bits of given register */ + u8 bit_offset; /* Bit offset within the register */ + u8 access_width; /* Minimum Access size (ACPI 3.0) */ + u64 address; /* 64-bit address of struct or register */ +}; + +struct acpi_table_fadt { ACPI_TABLE_HEADER_DEF /* ACPI common table header */ u32 firmware_ctrl; /* Physical address of FACS */ @@ -101,9 +109,26 @@ struct fadt_descriptor_rev1 u8 day_alrm; /* Index to day-of-month alarm in RTC CMOS RAM */ u8 mon_alrm; /* Index to month-of-year alarm in RTC CMOS RAM */ u8 century; /* Index to century in RTC CMOS RAM */ - u8 reserved4; /* Reserved */ - u8 reserved4a; /* Reserved */ - u8 reserved4b; /* Reserved */ + u16 boot_flags; /* IA-PC Boot Architecture Flags (see below for individual flags) */ + u8 reserved; /* Reserved, must be zero */ + u32 flags; /* Miscellaneous flag bits (see below for individual flags) */ + struct acpi_generic_address reset_register; /* 64-bit address of the Reset register */ + u8 reset_value; /* Value to write to the reset_register port to reset the system */ + u16 arm_boot_flags; /* ARM-Specific Boot Flags (see below for individual flags) (ACPI 5.1) */ + u8 minor_revision; /* FADT Minor Revision (ACPI 5.1) */ + u64 Xfacs; /* 64-bit physical address of FACS */ + u64 Xdsdt; /* 64-bit physical address of DSDT */ + struct acpi_generic_address xpm1a_event_block; /* 64-bit Extended Power Mgt 1a Event Reg Blk address */ + struct acpi_generic_address xpm1b_event_block; /* 64-bit Extended Power Mgt 1b Event Reg Blk address */ + struct acpi_generic_address xpm1a_control_block; /* 64-bit Extended Power Mgt 1a Control Reg Blk address */ + struct acpi_generic_address xpm1b_control_block; /* 64-bit Extended Power Mgt 1b Control Reg Blk address */ + struct acpi_generic_address xpm2_control_block; /* 64-bit Extended Power Mgt 2 Control Reg Blk address */ + struct acpi_generic_address xpm_timer_block; /* 64-bit Extended Power Mgt Timer Ctrl Reg Blk address */ + struct acpi_generic_address xgpe0_block; /* 64-bit Extended General Purpose Event 0 Reg Blk address */ + struct acpi_generic_address xgpe1_block; /* 64-bit Extended General Purpose Event 1 Reg Blk address */ + struct acpi_generic_address sleep_control; /* 64-bit Sleep Control register (ACPI 5.0) */ + struct acpi_generic_address sleep_status; /* 64-bit Sleep Status register (ACPI 5.0) */ + u64 hypervisor_id; /* Hypervisor Vendor ID (ACPI 6.0) */ }; struct facs_descriptor_rev1 diff --git a/lib/acpi.c b/lib/acpi.c index 240a922..aeaccb8 100644 --- a/lib/acpi.c +++ b/lib/acpi.c @@ -46,7 +46,8 @@ void *find_acpi_table_addr(u32 sig) /* FACS is special... */ if (sig == FACS_SIGNATURE) { - struct fadt_descriptor_rev1 *fadt; + struct acpi_table_fadt *fadt; + fadt = find_acpi_table_addr(FACP_SIGNATURE); if (!fadt) return NULL; diff --git a/x86/s3.c b/x86/s3.c index 0bcbe18..b891a95 100644 --- a/x86/s3.c +++ b/x86/s3.c @@ -30,7 +30,7 @@ extern char resume_start, resume_end; int main(int argc, char **argv) { - struct fadt_descriptor_rev1 *fadt = find_acpi_table_addr(FACP_SIGNATURE); + struct acpi_table_fadt *fadt = find_acpi_table_addr(FACP_SIGNATURE); struct facs_descriptor_rev1 *facs = find_acpi_table_addr(FACS_SIGNATURE); char *addr, *resume_vec = (void*)0x1000; diff --git a/x86/vmexit.c b/x86/vmexit.c index 8015cc7..7e00bce 100644 --- a/x86/vmexit.c +++ b/x86/vmexit.c @@ -206,7 +206,7 @@ int pm_tmr_blk; static void inl_pmtimer(void) { if (!pm_tmr_blk) { - struct fadt_descriptor_rev1 *fadt; + struct acpi_table_fadt *fadt; fadt = find_acpi_table_addr(FACP_SIGNATURE); pm_tmr_blk = fadt->pm_tmr_blk; From patchwork Thu Jun 30 10:03:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901540 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2A6BCCA47E for ; Thu, 30 Jun 2022 10:04:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233219AbiF3KEB (ORCPT ); Thu, 30 Jun 2022 06:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234706AbiF3KD7 (ORCPT ); Thu, 30 Jun 2022 06:03:59 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 136BF4338E for ; Thu, 30 Jun 2022 03:03:57 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1DA4F1C00; Thu, 30 Jun 2022 03:03:57 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E227A3F5A1; Thu, 30 Jun 2022 03:03:55 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 06/27] devicetree: Check if fdt is NULL before returning that a DT is available Date: Thu, 30 Jun 2022 11:03:03 +0100 Message-Id: <20220630100324.3153655-7-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Up until now, for platfroms that support DT, KUT would unconditionally use DT to configure the system and the code made the assumption that the fdt will always be a valid pointer. In Arm systems that support both ACPI and DT, we plat to follow the same convension as the Linux kernel. First, we attempt to configure the system using the DT. If an FDT was not provided then we attempt to configure the system using ACPI. As a result, for Arm systems with support for ACPI the fdt pointer can be NULL. This change modifies dt_available() to check if the fdt is a valid pointer and return if we can use information from the DT. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/devicetree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/devicetree.c b/lib/devicetree.c index 78c1f6f..3ff9d16 100644 --- a/lib/devicetree.c +++ b/lib/devicetree.c @@ -16,7 +16,7 @@ const void *dt_fdt(void) bool dt_available(void) { - return fdt_check_header(fdt) == 0; + return fdt && fdt_check_header(fdt) == 0; } int dt_get_nr_cells(int fdtnode, u32 *nr_address_cells, u32 *nr_size_cells) From patchwork Thu Jun 30 10:03:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901541 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9F21C433EF for ; Thu, 30 Jun 2022 10:04:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234719AbiF3KEC (ORCPT ); Thu, 30 Jun 2022 06:04:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234711AbiF3KEA (ORCPT ); Thu, 30 Jun 2022 06:04:00 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1CD6D43ADF for ; Thu, 30 Jun 2022 03:03:58 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 40D2A1063; Thu, 30 Jun 2022 03:03:58 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1211C3F5A1; Thu, 30 Jun 2022 03:03:56 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 07/27] arm/arm64: Add support for setting up the PSCI conduit through ACPI Date: Thu, 30 Jun 2022 11:03:04 +0100 Message-Id: <20220630100324.3153655-8-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In systems with ACPI support and when a DT is not provided, we can use the FADT to discover whether PSCI calls need to use smc or hvc calls. This change implements this but retains the default behavior; we check if a valid DT is provided, if not, we try to setup the PSCI conduit using ACPI. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones Reviewed-by: Ricardo Koller --- arm/Makefile.common | 1 + lib/acpi.h | 5 +++++ lib/arm/psci.c | 25 ++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/arm/Makefile.common b/arm/Makefile.common index 38385e0..8e9b3bb 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -38,6 +38,7 @@ cflatobjs += lib/alloc_page.o cflatobjs += lib/vmalloc.o cflatobjs += lib/alloc.o cflatobjs += lib/devicetree.o +cflatobjs += lib/acpi.o cflatobjs += lib/pci.o cflatobjs += lib/pci-host-generic.o cflatobjs += lib/pci-testdev.o diff --git a/lib/acpi.h b/lib/acpi.h index e49647b..6c18f73 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -131,6 +131,11 @@ struct acpi_table_fadt u64 hypervisor_id; /* Hypervisor Vendor ID (ACPI 6.0) */ }; +/* Masks for FADT ARM Boot Architecture Flags (arm_boot_flags) ACPI 5.1 */ + +#define ACPI_FADT_PSCI_COMPLIANT (1) /* 00: [V5+] PSCI 0.2+ is implemented */ +#define ACPI_FADT_PSCI_USE_HVC (1<<1) /* 01: [V5+] HVC must be used instead of SMC as the PSCI conduit */ + struct facs_descriptor_rev1 { u32 signature; /* ACPI Signature */ diff --git a/lib/arm/psci.c b/lib/arm/psci.c index 9c031a1..afbc33d 100644 --- a/lib/arm/psci.c +++ b/lib/arm/psci.c @@ -6,6 +6,7 @@ * * This work is licensed under the terms of the GNU LGPL, version 2. */ +#include #include #include #include @@ -56,7 +57,7 @@ void psci_system_off(void) printf("CPU%d unable to do system off (error = %d)\n", smp_processor_id(), err); } -void psci_set_conduit(void) +static void psci_set_conduit_fdt(void) { const void *fdt = dt_fdt(); const struct fdt_property *method; @@ -75,3 +76,25 @@ void psci_set_conduit(void) else assert_msg(false, "Unknown PSCI conduit: %s", method->data); } + +static void psci_set_conduit_acpi(void) +{ + struct acpi_table_fadt *fadt = find_acpi_table_addr(FACP_SIGNATURE); + + assert_msg(fadt, "Unable to find ACPI FADT"); + assert_msg(fadt->arm_boot_flags & ACPI_FADT_PSCI_COMPLIANT, + "PSCI is not supported in this platform"); + + if (fadt->arm_boot_flags & ACPI_FADT_PSCI_USE_HVC) + psci_invoke = psci_invoke_hvc; + else + psci_invoke = psci_invoke_smc; +} + +void psci_set_conduit(void) +{ + if (dt_available()) + psci_set_conduit_fdt(); + else + psci_set_conduit_acpi(); +} From patchwork Thu Jun 30 10:03:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901542 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8169CC43334 for ; Thu, 30 Jun 2022 10:04:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234725AbiF3KEF (ORCPT ); Thu, 30 Jun 2022 06:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234710AbiF3KEB (ORCPT ); Thu, 30 Jun 2022 06:04:01 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 45E942B278 for ; Thu, 30 Jun 2022 03:03:59 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 69B661042; Thu, 30 Jun 2022 03:03:59 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3B51C3F5A1; Thu, 30 Jun 2022 03:03:58 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 08/27] arm/arm64: Add support for discovering the UART through ACPI Date: Thu, 30 Jun 2022 11:03:05 +0100 Message-Id: <20220630100324.3153655-9-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In systems with ACPI support and when a DT is not provided, we can use the SPCR to discover the serial port address range. This change implements this but retains the default behavior; we check if a valid DT is provided, if not, we try to discover the UART using ACPI. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones Reviewed-by: Ricardo Koller --- lib/acpi.h | 25 +++++++++++++++++++++++++ lib/arm/io.c | 23 ++++++++++++++++++----- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/acpi.h b/lib/acpi.h index 6c18f73..1cd99c5 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -17,6 +17,7 @@ #define XSDT_SIGNATURE ACPI_SIGNATURE('X','S','D','T') #define FACP_SIGNATURE ACPI_SIGNATURE('F','A','C','P') #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S') +#define SPCR_SIGNATURE ACPI_SIGNATURE('S','P','C','R') #define ACPI_SIGNATURE_8BYTE(c1, c2, c3, c4, c5, c6, c7, c8) \ @@ -148,6 +149,30 @@ struct facs_descriptor_rev1 u8 reserved3 [40]; /* Reserved - must be zero */ }; +struct spcr_descriptor { + ACPI_TABLE_HEADER_DEF /* ACPI common table header */ + u8 interface_type; /* 0=full 16550, 1=subset of 16550 */ + u8 reserved[3]; + struct acpi_generic_address serial_port; + u8 interrupt_type; + u8 pc_interrupt; + u32 interrupt; + u8 baud_rate; + u8 parity; + u8 stop_bits; + u8 flow_control; + u8 terminal_type; + u8 reserved1; + u16 pci_device_id; + u16 pci_vendor_id; + u8 pci_bus; + u8 pci_device; + u8 pci_function; + u32 pci_flags; + u8 pci_segment; + u32 reserved2; +}; + #pragma pack(0) void set_efi_rsdp(struct rsdp_descriptor *rsdp); diff --git a/lib/arm/io.c b/lib/arm/io.c index 343e108..a91f116 100644 --- a/lib/arm/io.c +++ b/lib/arm/io.c @@ -9,6 +9,7 @@ * This work is licensed under the terms of the GNU LGPL, version 2. */ #include +#include #include #include #include @@ -29,7 +30,7 @@ static struct spinlock uart_lock; #define UART_EARLY_BASE (u8 *)(unsigned long)CONFIG_UART_EARLY_BASE static volatile u8 *uart0_base = UART_EARLY_BASE; -static void uart0_init(void) +static void uart0_init_fdt(void) { /* * kvm-unit-tests uses the uart only for output. Both uart models have @@ -65,17 +66,29 @@ static void uart0_init(void) } uart0_base = ioremap(base.addr, base.size); +} + +static void uart0_init_acpi(void) +{ + struct spcr_descriptor *spcr = find_acpi_table_addr(SPCR_SIGNATURE); + + assert_msg(spcr, "Unable to find ACPI SPCR"); + uart0_base = ioremap(spcr->serial_port.address, spcr->serial_port.bit_width); +} + +void io_init(void) +{ + if (dt_available()) + uart0_init_fdt(); + else + uart0_init_acpi(); if (uart0_base != UART_EARLY_BASE) { printf("WARNING: early print support may not work. " "Found uart at %p, but early base is %p.\n", uart0_base, UART_EARLY_BASE); } -} -void io_init(void) -{ - uart0_init(); chr_testdev_init(); } From patchwork Thu Jun 30 10:03:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901543 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CF00CCA47E for ; Thu, 30 Jun 2022 10:04:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234730AbiF3KEF (ORCPT ); Thu, 30 Jun 2022 06:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234720AbiF3KEC (ORCPT ); Thu, 30 Jun 2022 06:04:02 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 79D494338E for ; Thu, 30 Jun 2022 03:04:00 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8C8EB1BF7; Thu, 30 Jun 2022 03:04:00 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5EC723F5A1; Thu, 30 Jun 2022 03:03:59 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 09/27] arm/arm64: Add support for timer initialization through ACPI Date: Thu, 30 Jun 2022 11:03:06 +0100 Message-Id: <20220630100324.3153655-10-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org For systems with ACPI support, we can discover timers through the ACPI GTDT table. This change implements the code to discover timers through the GTDT and adds ACPI support in timer_save_state. This change retains the default behavior; we check if a valid DT is provided, if not, we try to discover timers using ACPI. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- arm/Makefile.common | 1 + lib/arm/asm/timer.h | 2 ++ lib/acpi.h | 18 +++++++++++ lib/arm/setup.c | 39 ---------------------- lib/arm/timer.c | 79 +++++++++++++++++++++++++++++++++++++++++++++ arm/micro-bench.c | 4 +-- arm/timer.c | 10 +++--- 7 files changed, 107 insertions(+), 46 deletions(-) create mode 100644 lib/arm/timer.c diff --git a/arm/Makefile.common b/arm/Makefile.common index 8e9b3bb..5be42c0 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -53,6 +53,7 @@ cflatobjs += lib/arm/psci.o cflatobjs += lib/arm/smp.o cflatobjs += lib/arm/delay.o cflatobjs += lib/arm/gic.o lib/arm/gic-v2.o lib/arm/gic-v3.o +cflatobjs += lib/arm/timer.o OBJDIRS += lib/arm diff --git a/lib/arm/asm/timer.h b/lib/arm/asm/timer.h index f75cc67..aaf839f 100644 --- a/lib/arm/asm/timer.h +++ b/lib/arm/asm/timer.h @@ -27,5 +27,7 @@ extern struct timer_state __timer_state; #define TIMER_PTIMER_IRQ (__timer_state.ptimer.irq) #define TIMER_VTIMER_IRQ (__timer_state.vtimer.irq) +void timer_save_state(void); + #endif /* !__ASSEMBLY__ */ #endif /* _ASMARM_TIMER_H_ */ diff --git a/lib/acpi.h b/lib/acpi.h index 1cd99c5..9910c39 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -18,6 +18,7 @@ #define FACP_SIGNATURE ACPI_SIGNATURE('F','A','C','P') #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S') #define SPCR_SIGNATURE ACPI_SIGNATURE('S','P','C','R') +#define GTDT_SIGNATURE ACPI_SIGNATURE('G','T','D','T') #define ACPI_SIGNATURE_8BYTE(c1, c2, c3, c4, c5, c6, c7, c8) \ @@ -173,6 +174,23 @@ struct spcr_descriptor { u32 reserved2; }; +struct acpi_table_gtdt { + ACPI_TABLE_HEADER_DEF /* ACPI common table header */ + u64 counter_block_addresss; + u32 reserved; + u32 secure_el1_interrupt; + u32 secure_el1_flags; + u32 non_secure_el1_interrupt; + u32 non_secure_el1_flags; + u32 virtual_timer_interrupt; + u32 virtual_timer_flags; + u32 non_secure_el2_interrupt; + u32 non_secure_el2_flags; + u64 counter_read_block_address; + u32 platform_timer_count; + u32 platform_timer_offset; +}; + #pragma pack(0) void set_efi_rsdp(struct rsdp_descriptor *rsdp); diff --git a/lib/arm/setup.c b/lib/arm/setup.c index bcdf0d7..1572c64 100644 --- a/lib/arm/setup.c +++ b/lib/arm/setup.c @@ -35,8 +35,6 @@ extern unsigned long etext; -struct timer_state __timer_state; - char *initrd; u32 initrd_size; @@ -199,43 +197,6 @@ static void mem_init(phys_addr_t freemem_start) page_alloc_ops_enable(); } -static void timer_save_state(void) -{ - const struct fdt_property *prop; - const void *fdt = dt_fdt(); - int node, len; - u32 *data; - - node = fdt_node_offset_by_compatible(fdt, -1, "arm,armv8-timer"); - assert(node >= 0 || node == -FDT_ERR_NOTFOUND); - - if (node == -FDT_ERR_NOTFOUND) { - __timer_state.ptimer.irq = -1; - __timer_state.vtimer.irq = -1; - return; - } - - /* - * From Linux devicetree timer binding documentation - * - * interrupts : - * secure timer irq - * non-secure timer irq (ptimer) - * virtual timer irq (vtimer) - * hypervisor timer irq - */ - prop = fdt_get_property(fdt, node, "interrupts", &len); - assert(prop && len == (4 * 3 * sizeof(u32))); - - data = (u32 *)prop->data; - assert(fdt32_to_cpu(data[3]) == 1 /* PPI */); - __timer_state.ptimer.irq = fdt32_to_cpu(data[4]); - __timer_state.ptimer.irq_flags = fdt32_to_cpu(data[5]); - assert(fdt32_to_cpu(data[6]) == 1 /* PPI */); - __timer_state.vtimer.irq = fdt32_to_cpu(data[7]); - __timer_state.vtimer.irq_flags = fdt32_to_cpu(data[8]); -} - void setup(const void *fdt, phys_addr_t freemem_start) { void *freemem; diff --git a/lib/arm/timer.c b/lib/arm/timer.c new file mode 100644 index 0000000..507d9c8 --- /dev/null +++ b/lib/arm/timer.c @@ -0,0 +1,79 @@ +/* + * Initialize timers. + * + * Copyright (C) 2022, Arm Ltd., Nikos Nikoleris + * Copyright (C) 2014, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +#include +#include +#include +#include +#include +#include + +struct timer_state __timer_state; + +static void timer_save_state_fdt(void) +{ + const struct fdt_property *prop; + const void *fdt = dt_fdt(); + int node, len; + u32 *data; + + node = fdt_node_offset_by_compatible(fdt, -1, "arm,armv8-timer"); + assert(node >= 0 || node == -FDT_ERR_NOTFOUND); + + if (node == -FDT_ERR_NOTFOUND) { + __timer_state.ptimer.irq = -1; + __timer_state.vtimer.irq = -1; + return; + } + + /* + * From Linux devicetree timer binding documentation + * + * interrupts : + * secure timer irq + * non-secure timer irq (ptimer) + * virtual timer irq (vtimer) + * hypervisor timer irq + */ + prop = fdt_get_property(fdt, node, "interrupts", &len); + assert(prop && len == (4 * 3 * sizeof(u32))); + + data = (u32 *)prop->data; + assert(fdt32_to_cpu(data[3]) == 1 /* PPI */); + __timer_state.ptimer.irq = PPI(fdt32_to_cpu(data[4])); + __timer_state.ptimer.irq_flags = fdt32_to_cpu(data[5]); + assert(fdt32_to_cpu(data[6]) == 1 /* PPI */); + __timer_state.vtimer.irq = PPI(fdt32_to_cpu(data[7])); + __timer_state.vtimer.irq_flags = fdt32_to_cpu(data[8]); +} + +static void timer_save_state_acpi(void) +{ + struct acpi_table_gtdt *gtdt = find_acpi_table_addr(GTDT_SIGNATURE); + + if (!gtdt) { + printf("Cannot find ACPI GTDT"); + __timer_state.ptimer.irq = -1; + __timer_state.vtimer.irq = -1; + return; + } + + __timer_state.ptimer.irq = gtdt->non_secure_el1_interrupt; + __timer_state.ptimer.irq_flags = gtdt->non_secure_el1_flags; + + __timer_state.vtimer.irq = gtdt->virtual_timer_interrupt; + __timer_state.vtimer.irq_flags = gtdt->virtual_timer_flags; +} + +void timer_save_state(void) +{ + if (dt_available()) + timer_save_state_fdt(); + else + timer_save_state_acpi(); +} diff --git a/arm/micro-bench.c b/arm/micro-bench.c index 8436612..5be9b18 100644 --- a/arm/micro-bench.c +++ b/arm/micro-bench.c @@ -43,7 +43,7 @@ static void gic_irq_handler(struct pt_regs *regs) irq_received = true; gic_write_eoir(irqstat); - if (irqstat == PPI(TIMER_VTIMER_IRQ)) { + if (irqstat == TIMER_VTIMER_IRQ) { write_sysreg((ARCH_TIMER_CTL_IMASK | ARCH_TIMER_CTL_ENABLE), cntv_ctl_el0); isb(); @@ -229,7 +229,7 @@ static bool timer_prep(void) assert_msg(0, "Unreachable"); } - writel(1 << PPI(TIMER_VTIMER_IRQ), gic_isenabler); + writel(1 << TIMER_VTIMER_IRQ, gic_isenabler); write_sysreg(ARCH_TIMER_CTL_IMASK | ARCH_TIMER_CTL_ENABLE, cntv_ctl_el0); isb(); diff --git a/arm/timer.c b/arm/timer.c index c4e7b10..a4a8281 100644 --- a/arm/timer.c +++ b/arm/timer.c @@ -139,7 +139,7 @@ static struct timer_info ptimer_info = { static void set_timer_irq_enabled(struct timer_info *info, bool enabled) { - u32 val = 1 << PPI(info->irq); + u32 val = 1 << info->irq; if (enabled) writel(val, gic_isenabler); @@ -153,9 +153,9 @@ static void irq_handler(struct pt_regs *regs) u32 irqstat = gic_read_iar(); u32 irqnr = gic_iar_irqnr(irqstat); - if (irqnr == PPI(vtimer_info.irq)) { + if (irqnr == vtimer_info.irq) { info = &vtimer_info; - } else if (irqnr == PPI(ptimer_info.irq)) { + } else if (irqnr == ptimer_info.irq) { info = &ptimer_info; } else { if (irqnr != GICC_INT_SPURIOUS) @@ -185,9 +185,9 @@ static bool gic_timer_check_state(struct timer_info *info, /* Wait for up to 1s for the GIC to sample the interrupt. */ for (i = 0; i < 10; i++) { mdelay(100); - if (gic_irq_state(PPI(info->irq)) == expected_state) { + if (gic_irq_state(info->irq) == expected_state) { mdelay(100); - if (gic_irq_state(PPI(info->irq)) == expected_state) + if (gic_irq_state(info->irq) == expected_state) return true; } } From patchwork Thu Jun 30 10:03:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901544 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A4D5CCA47B for ; Thu, 30 Jun 2022 10:04:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234376AbiF3KEG (ORCPT ); Thu, 30 Jun 2022 06:04:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234721AbiF3KED (ORCPT ); Thu, 30 Jun 2022 06:04:03 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8D6E643AC1 for ; Thu, 30 Jun 2022 03:04:01 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B12971063; Thu, 30 Jun 2022 03:04:01 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 810CF3F5A1; Thu, 30 Jun 2022 03:04:00 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 10/27] arm/arm64: Add support for cpu initialization through ACPI Date: Thu, 30 Jun 2022 11:03:07 +0100 Message-Id: <20220630100324.3153655-11-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In systems with ACPI support and when a DT is not provided, we can use the MADTs to discover the number of CPUs and their corresponding MIDR. This change implements this but retains the default behavior; we check if a valid DT is provided, if not, we try to discover the cores in the system using ACPI. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/acpi.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/acpi.c | 21 +++++++++++++++++ lib/arm/setup.c | 25 +++++++++++++++++--- 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/lib/acpi.h b/lib/acpi.h index 9910c39..160bfbe 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -17,6 +17,7 @@ #define XSDT_SIGNATURE ACPI_SIGNATURE('X','S','D','T') #define FACP_SIGNATURE ACPI_SIGNATURE('F','A','C','P') #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S') +#define MADT_SIGNATURE ACPI_SIGNATURE('A','P','I','C') #define SPCR_SIGNATURE ACPI_SIGNATURE('S','P','C','R') #define GTDT_SIGNATURE ACPI_SIGNATURE('G','T','D','T') @@ -150,6 +151,67 @@ struct facs_descriptor_rev1 u8 reserved3 [40]; /* Reserved - must be zero */ }; +struct acpi_table_madt { + ACPI_TABLE_HEADER_DEF /* ACPI common table header */ + u32 address; /* Physical address of local APIC */ + u32 flags; +}; + +struct acpi_subtable_header { + u8 type; + u8 length; +}; + +typedef int (*acpi_table_handler)(struct acpi_subtable_header *header); + +/* 11: Generic interrupt - GICC (ACPI 5.0 + ACPI 6.0 + ACPI 6.3 changes) */ + +struct acpi_madt_generic_interrupt { + u8 type; + u8 length; + u16 reserved; /* reserved - must be zero */ + u32 cpu_interface_number; + u32 uid; + u32 flags; + u32 parking_version; + u32 performance_interrupt; + u64 parked_address; + u64 base_address; + u64 gicv_base_address; + u64 gich_base_address; + u32 vgic_interrupt; + u64 gicr_base_address; + u64 arm_mpidr; + u8 efficiency_class; + u8 reserved2[1]; + u16 spe_interrupt; /* ACPI 6.3 */ +}; + +/* Values for MADT subtable type in struct acpi_subtable_header */ + +enum acpi_madt_type { + ACPI_MADT_TYPE_LOCAL_APIC = 0, + ACPI_MADT_TYPE_IO_APIC = 1, + ACPI_MADT_TYPE_INTERRUPT_OVERRIDE = 2, + ACPI_MADT_TYPE_NMI_SOURCE = 3, + ACPI_MADT_TYPE_LOCAL_APIC_NMI = 4, + ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE = 5, + ACPI_MADT_TYPE_IO_SAPIC = 6, + ACPI_MADT_TYPE_LOCAL_SAPIC = 7, + ACPI_MADT_TYPE_INTERRUPT_SOURCE = 8, + ACPI_MADT_TYPE_LOCAL_X2APIC = 9, + ACPI_MADT_TYPE_LOCAL_X2APIC_NMI = 10, + ACPI_MADT_TYPE_GENERIC_INTERRUPT = 11, + ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR = 12, + ACPI_MADT_TYPE_GENERIC_MSI_FRAME = 13, + ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR = 14, + ACPI_MADT_TYPE_GENERIC_TRANSLATOR = 15, + ACPI_MADT_TYPE_RESERVED = 16 /* 16 and greater are reserved */ +}; + +/* MADT Local APIC flags */ +#define ACPI_MADT_ENABLED (1) /* 00: Processor is usable if set */ + struct spcr_descriptor { ACPI_TABLE_HEADER_DEF /* ACPI common table header */ u8 interface_type; /* 0=full 16550, 1=subset of 16550 */ @@ -195,5 +257,6 @@ struct acpi_table_gtdt { void set_efi_rsdp(struct rsdp_descriptor *rsdp); void* find_acpi_table_addr(u32 sig); +void acpi_table_parse_madt(enum acpi_madt_type mtype, acpi_table_handler handler); #endif diff --git a/lib/acpi.c b/lib/acpi.c index aeaccb8..b652d18 100644 --- a/lib/acpi.c +++ b/lib/acpi.c @@ -104,3 +104,24 @@ void *find_acpi_table_addr(u32 sig) return NULL; } + +void acpi_table_parse_madt(enum acpi_madt_type mtype, + acpi_table_handler handler) +{ + struct acpi_table_madt *madt; + struct acpi_subtable_header *header; + void *end; + + madt = find_acpi_table_addr(MADT_SIGNATURE); + assert(madt); + + header = (void *)(ulong)madt + sizeof(struct acpi_table_madt); + end = (void *)((ulong)madt + madt->length); + + while ((void *)header < end) { + if (header->type == mtype) + handler(header); + + header = (void *)(ulong)header + header->length; + } +} diff --git a/lib/arm/setup.c b/lib/arm/setup.c index 1572c64..3612991 100644 --- a/lib/arm/setup.c +++ b/lib/arm/setup.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -55,7 +56,7 @@ int mpidr_to_cpu(uint64_t mpidr) return -1; } -static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused) +static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused) { int cpu = nr_cpus++; @@ -65,13 +66,31 @@ static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused) set_cpu_present(cpu, true); } +static int cpu_set_acpi(struct acpi_subtable_header *header) +{ + int cpu = nr_cpus++; + struct acpi_madt_generic_interrupt *gicc = (void *)header; + + assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS); + + cpus[cpu] = gicc->arm_mpidr; + set_cpu_present(cpu, true); + + return 0; +} + static void cpu_init(void) { int ret; nr_cpus = 0; - ret = dt_for_each_cpu_node(cpu_set, NULL); - assert(ret == 0); + if (dt_available()) { + ret = dt_for_each_cpu_node(cpu_set_fdt, NULL); + assert(ret == 0); + } else { + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, cpu_set_acpi); + } + set_cpu_online(0, true); } From patchwork Thu Jun 30 10:03:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901545 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E209C433EF for ; Thu, 30 Jun 2022 10:04:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234723AbiF3KEI (ORCPT ); Thu, 30 Jun 2022 06:04:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234722AbiF3KEG (ORCPT ); Thu, 30 Jun 2022 06:04:06 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B94A02A732 for ; Thu, 30 Jun 2022 03:04:02 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DD3A41042; Thu, 30 Jun 2022 03:04:02 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A77E83F5A1; Thu, 30 Jun 2022 03:04:01 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 11/27] arm/arm64: Add support for gic initialization through ACPI Date: Thu, 30 Jun 2022 11:03:08 +0100 Message-Id: <20220630100324.3153655-12-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In systems with ACPI support and when a DT is not provided, we can use the MADTs to figure out if it implements a GICv2 or a GICv3 and discover the GIC parameters. This change implements this but retains the default behavior; we check if a valid DT is provided, if not, we try to discover the cores in the system using ACPI. Signed-off-by: Nikos Nikoleris --- lib/acpi.h | 44 +++++++++++++++++ lib/libcflat.h | 1 + lib/arm/gic.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 1 deletion(-) diff --git a/lib/acpi.h b/lib/acpi.h index 160bfbe..9f6fbe4 100644 --- a/lib/acpi.h +++ b/lib/acpi.h @@ -187,6 +187,50 @@ struct acpi_madt_generic_interrupt { u16 spe_interrupt; /* ACPI 6.3 */ }; +#define ACPI_MADT_ENABLED (1) /* 00: Processor is usable if set */ + +/* 12: Generic Distributor (ACPI 5.0 + ACPI 6.0 changes) */ + +struct acpi_madt_generic_distributor { + struct acpi_subtable_header header; + u16 reserved; /* reserved - must be zero */ + u32 gic_id; + u64 base_address; + u32 global_irq_base; + u8 version; + u8 reserved2[3]; /* reserved - must be zero */ +}; + +/* Values for Version field above */ + +enum acpi_madt_gic_version { + ACPI_MADT_GIC_VERSION_NONE = 0, + ACPI_MADT_GIC_VERSION_V1 = 1, + ACPI_MADT_GIC_VERSION_V2 = 2, + ACPI_MADT_GIC_VERSION_V3 = 3, + ACPI_MADT_GIC_VERSION_V4 = 4, + ACPI_MADT_GIC_VERSION_RESERVED = 5 /* 5 and greater are reserved */ +}; + +/* 14: Generic Redistributor (ACPI 5.1) */ + +struct acpi_madt_generic_redistributor { + struct acpi_subtable_header header; + u16 reserved; /* reserved - must be zero */ + u64 base_address; + u32 length; +}; + +/* 15: Generic Translator (ACPI 6.0) */ + +struct acpi_madt_generic_translator { + struct acpi_subtable_header header; + u16 reserved; /* reserved - must be zero */ + u32 translation_id; + u64 base_address; + u32 reserved2; +}; + /* Values for MADT subtable type in struct acpi_subtable_header */ enum acpi_madt_type { diff --git a/lib/libcflat.h b/lib/libcflat.h index c1fd31f..700f435 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -161,6 +161,7 @@ extern void setup_vm(void); #define SZ_8K (1 << 13) #define SZ_16K (1 << 14) #define SZ_64K (1 << 16) +#define SZ_128K (1 << 17) #define SZ_1M (1 << 20) #define SZ_2M (1 << 21) #define SZ_1G (1 << 30) diff --git a/lib/arm/gic.c b/lib/arm/gic.c index 1bfcfcf..c346d1f 100644 --- a/lib/arm/gic.c +++ b/lib/arm/gic.c @@ -3,6 +3,7 @@ * * This work is licensed under the terms of the GNU LGPL, version 2. */ +#include #include #include #include @@ -120,7 +121,7 @@ int gic_version(void) return 0; } -int gic_init(void) +static int gic_init_fdt(void) { if (gicv2_init()) { gic_common_ops = &gicv2_common_ops; @@ -133,6 +134,130 @@ int gic_init(void) return gic_version(); } +#define ACPI_GICV2_DIST_MEM_SIZE (SZ_4K) +#define ACPI_GIC_CPU_IF_MEM_SIZE (SZ_8K) +#define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K) +#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) + +static int gic_acpi_version(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_distributor *dist = (void *)header; + int version = dist->version; + + if (version == 2) + gic_common_ops = &gicv2_common_ops; + else if (version == 3) + gic_common_ops = &gicv3_common_ops; + + return version; +} + +static int gicv2_acpi_parse_madt_cpu(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_interrupt *gicc = (void *)header; + static phys_addr_t gicc_base_address; + + if (!(gicc->flags & ACPI_MADT_ENABLED)) + return 0; + + if (!gicc_base_address) { + gicc_base_address = gicc->base_address; + gicv2_data.cpu_base = ioremap(gicc_base_address, ACPI_GIC_CPU_IF_MEM_SIZE); + } + assert(gicc_base_address == gicc->base_address); + + return 0; +} + +static int gicv2_acpi_parse_madt_dist(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_distributor *dist = (void *)header; + + gicv2_data.dist_base = ioremap(dist->base_address, ACPI_GICV2_DIST_MEM_SIZE); + + return 0; +} + +static int gicv3_acpi_parse_madt_gicc(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_interrupt *gicc = (void *)header; + static phys_addr_t gicr_base_address; + + if (!(gicc->flags & ACPI_MADT_ENABLED)) + return 0; + + if (!gicr_base_address) { + gicr_base_address = gicc->gicr_base_address; + gicv3_data.redist_bases[0] = ioremap(gicr_base_address, SZ_64K * 2); + } + assert(gicr_base_address == gicc->gicr_base_address); + + return 0; +} + +static int gicv3_acpi_parse_madt_dist(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_distributor *dist = (void *)header; + + gicv3_data.dist_base = ioremap(dist->base_address, ACPI_GICV3_DIST_MEM_SIZE); + + return 0; +} + +static int gicv3_acpi_parse_madt_redist(struct acpi_subtable_header *header) +{ + static int i; + struct acpi_madt_generic_redistributor *redist = (void *)header; + + gicv3_data.redist_bases[i++] = ioremap(redist->base_address, redist->length); + + return 0; +} + +static int gicv3_acpi_parse_madt_its(struct acpi_subtable_header *header) +{ + struct acpi_madt_generic_translator *its_entry = (void *)header; + + its_data.base = ioremap(its_entry->base_address, ACPI_GICV3_ITS_MEM_SIZE - 1); + + return 0; +} + +static int gic_init_acpi(void) +{ + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, + gic_acpi_version); + if (gic_version() == 2) { + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, + gicv2_acpi_parse_madt_cpu); + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, + gicv2_acpi_parse_madt_dist); + } else if (gic_version() == 3) { + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, + gicv3_acpi_parse_madt_dist); + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR, + gicv3_acpi_parse_madt_redist); + if (!gicv3_data.redist_base) + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR, + gicv3_acpi_parse_madt_gicc); + acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, + gicv3_acpi_parse_madt_its); +#ifdef __aarch64__ + its_init(); +#endif + } + + return gic_version(); +} + +int gic_init(void) +{ + if (dt_available()) + return gic_init_fdt(); + else + return gic_init_acpi(); +} + void gic_enable_defaults(void) { if (!gic_common_ops) { From patchwork Thu Jun 30 10:03:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901546 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DFC9C43334 for ; Thu, 30 Jun 2022 10:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233724AbiF3KEI (ORCPT ); Thu, 30 Jun 2022 06:04:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234717AbiF3KEH (ORCPT ); Thu, 30 Jun 2022 06:04:07 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 08A1B4338E for ; Thu, 30 Jun 2022 03:04:03 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 15D2A1BF7; Thu, 30 Jun 2022 03:04:04 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1B0B3F5A1; Thu, 30 Jun 2022 03:04:02 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 12/27] lib/printf: Support for precision modifier in printf Date: Thu, 30 Jun 2022 11:03:09 +0100 Message-Id: <20220630100324.3153655-13-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This follows the typical format of: printf("%.Ns", *str); Where N might be a decimal digit string or '*'. This feature is used by a future change. See also: man 3 printf Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/printf.c | 95 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/lib/printf.c b/lib/printf.c index 383799e..d600199 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -6,6 +6,7 @@ */ #include "libcflat.h" +#include "ctype.h" #define BUFSZ 2000 @@ -19,6 +20,7 @@ typedef struct strprops { char pad; int npad; bool alternate; + int precision; } strprops_t; static void addchar(pstream_t *p, char c) @@ -43,7 +45,7 @@ static void print_str(pstream_t *p, const char *s, strprops_t props) } } - while (*s) + while (*s && props.precision--) addchar(p, *s++); if (npad < 0) { @@ -73,12 +75,13 @@ static void print_int(pstream_t *ps, long long n, int base, strprops_t props) n /= base; } + while (p == buf || (p - buf < props.precision)) + *p++ = '0'; + props.precision = -1; + if (s) *p++ = '-'; - if (p == buf) - *p++ = '0'; - for (i = 0; i < (p - buf) / 2; ++i) { char tmp; @@ -104,8 +107,13 @@ static void print_unsigned(pstream_t *ps, unsigned long long n, int base, } if (p == buf) + props.alternate = false; + + while (p == buf || (p - buf < props.precision)) *p++ = '0'; - else if (props.alternate && base == 16) { + props.precision = -1; + + if (props.alternate && base == 16) { if (props.pad == '0') { addchar(ps, '0'); addchar(ps, 'x'); @@ -147,9 +155,56 @@ static int fmtnum(const char **fmt) return num; } +/* + * Adapted from drivers/firmware/efi/libstub/vsprintf.c + */ +static int skip_atoi(const char **s) +{ + int i = 0; + + do { + i = i*10 + *((*s)++) - '0'; + } while (isdigit(**s)); + + return i; +} + +/* + * Adapted from drivers/firmware/efi/libstub/vsprintf.c + */ +static int get_int(const char **fmt, va_list *ap) +{ + if (isdigit(**fmt)) + return skip_atoi(fmt); + + if (**fmt == '*') { + ++(*fmt); + /* it's the next argument */ + return va_arg(*ap, int); + } + return 0; +} + int vsnprintf(char *buf, int size, const char *fmt, va_list va) { pstream_t s; + va_list args; + + /* + * We want to pass our input va_list to helper functions by reference, + * but there's an annoying edge case. If va_list was originally passed + * to us by value, we could just pass &ap down to the helpers. This is + * the case on, for example, X86_32. + * However, on X86_64 (and possibly others), va_list is actually a + * size-1 array containing a structure. Our function parameter ap has + * decayed from T[1] to T*, and &ap has type T** rather than T(*)[1], + * which is what will be expected by a function taking a va_list * + * parameter. + * One standard way to solve this mess is by creating a copy in a local + * variable of type va_list and then passing a pointer to that local + * copy instead, which is what we do here. + */ + va_copy(args, va); s.buffer = buf; s.remain = size - 1; @@ -160,6 +215,7 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va) strprops_t props; memset(&props, 0, sizeof(props)); props.pad = ' '; + props.precision = -1; if (f != '%') { addchar(&s, f); @@ -172,11 +228,15 @@ morefmt: addchar(&s, '%'); break; case 'c': - addchar(&s, va_arg(va, int)); + addchar(&s, va_arg(args, int)); break; case '\0': --fmt; break; + case '.': + props.pad = ' '; + props.precision = get_int(&fmt, &args); + goto morefmt; case '#': props.alternate = true; goto morefmt; @@ -204,54 +264,55 @@ morefmt: case 'd': switch (nlong) { case 0: - print_int(&s, va_arg(va, int), 10, props); + print_int(&s, va_arg(args, int), 10, props); break; case 1: - print_int(&s, va_arg(va, long), 10, props); + print_int(&s, va_arg(args, long), 10, props); break; default: - print_int(&s, va_arg(va, long long), 10, props); + print_int(&s, va_arg(args, long long), 10, props); break; } break; case 'u': switch (nlong) { case 0: - print_unsigned(&s, va_arg(va, unsigned), 10, props); + print_unsigned(&s, va_arg(args, unsigned int), 10, props); break; case 1: - print_unsigned(&s, va_arg(va, unsigned long), 10, props); + print_unsigned(&s, va_arg(args, unsigned long), 10, props); break; default: - print_unsigned(&s, va_arg(va, unsigned long long), 10, props); + print_unsigned(&s, va_arg(args, unsigned long long), 10, props); break; } break; case 'x': switch (nlong) { case 0: - print_unsigned(&s, va_arg(va, unsigned), 16, props); + print_unsigned(&s, va_arg(args, unsigned int), 16, props); break; case 1: - print_unsigned(&s, va_arg(va, unsigned long), 16, props); + print_unsigned(&s, va_arg(args, unsigned long), 16, props); break; default: - print_unsigned(&s, va_arg(va, unsigned long long), 16, props); + print_unsigned(&s, va_arg(args, unsigned long long), 16, props); break; } break; case 'p': props.alternate = true; - print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props); + print_unsigned(&s, (unsigned long)va_arg(args, void *), 16, props); break; case 's': - print_str(&s, va_arg(va, const char *), props); + print_str(&s, va_arg(args, const char *), props); break; default: addchar(&s, f); break; } } + va_end(args); *s.buffer = 0; return s.added; } From patchwork Thu Jun 30 10:03:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F726C43334 for ; Thu, 30 Jun 2022 10:04:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234732AbiF3KEL (ORCPT ); Thu, 30 Jun 2022 06:04:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234728AbiF3KEJ (ORCPT ); Thu, 30 Jun 2022 06:04:09 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 360FD2AE2F for ; Thu, 30 Jun 2022 03:04:05 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 38DAE1063; Thu, 30 Jun 2022 03:04:05 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0A5303F5A1; Thu, 30 Jun 2022 03:04:03 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 13/27] lib/printf: Add support for printing wide strings Date: Thu, 30 Jun 2022 11:03:10 +0100 Message-Id: <20220630100324.3153655-14-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change adds support for wide strings (u16*) to printf() variants. This feature is used by a future change. Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- lib/printf.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/lib/printf.c b/lib/printf.c index d600199..27041bb 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -58,6 +58,102 @@ static void print_str(pstream_t *p, const char *s, strprops_t props) } } +/* + * Adapted from drivers/firmware/efi/libstub/vsprintf.c + */ +static u32 utf16_to_utf32(const u16 **s16) +{ + u16 c0, c1; + + c0 = *(*s16)++; + /* not a surrogate */ + if ((c0 & 0xf800) != 0xd800) + return c0; + /* invalid: low surrogate instead of high */ + if (c0 & 0x0400) + return 0xfffd; + c1 = **s16; + /* invalid: missing low surrogate */ + if ((c1 & 0xfc00) != 0xdc00) + return 0xfffd; + /* valid surrogate pair */ + ++(*s16); + return (0x10000 - (0xd800 << 10) - 0xdc00) + (c0 << 10) + c1; +} + +/* + * Adapted from drivers/firmware/efi/libstub/vsprintf.c + */ +static size_t utf16s_utf8nlen(const u16 *s16, size_t maxlen) +{ + size_t len, clen; + + for (len = 0; len < maxlen && *s16; len += clen) { + u16 c0 = *s16++; + + /* First, get the length for a BMP character */ + clen = 1 + (c0 >= 0x80) + (c0 >= 0x800); + if (len + clen > maxlen) + break; + /* + * If this is a high surrogate, and we're already at maxlen, we + * can't include the character if it's a valid surrogate pair. + * Avoid accessing one extra word just to check if it's valid + * or not. + */ + if ((c0 & 0xfc00) == 0xd800) { + if (len + clen == maxlen) + break; + if ((*s16 & 0xfc00) == 0xdc00) { + ++s16; + ++clen; + } + } + } + + return len; +} + +/* + * Adapted from drivers/firmware/efi/libstub/vsprintf.c + */ +static void print_wstring(pstream_t *p, const u16 *s, strprops_t props) +{ + const u16 *ws = (const u16 *)s; + size_t pos = 0, size = p->remain + 1, len = utf16s_utf8nlen(ws, props.precision); + + while (len-- > 0) { + u32 c32 = utf16_to_utf32(&ws); + u8 *s8; + size_t clen; + + if (c32 < 0x80) { + addchar(p, c32); + continue; + } + + /* Number of trailing octets */ + clen = 1 + (c32 >= 0x800) + (c32 >= 0x10000); + + len -= clen; + s8 = (u8 *)(p->buffer - p->added + pos); + + /* Avoid writing partial character */ + addchar(p, '\0'); + pos += clen; + if (pos >= size) + continue; + + /* Set high bits of leading octet */ + *s8 = (0xf00 >> 1) >> clen; + /* Write trailing octets in reverse order */ + for (s8 += clen; clen; --clen, c32 >>= 6) + *s8-- = 0x80 | (c32 & 0x3f); + /* Set low bits of leading octet */ + *s8 |= c32; + } +} + static char digits[16] = "0123456789abcdef"; static void print_int(pstream_t *ps, long long n, int base, strprops_t props) @@ -305,7 +401,10 @@ morefmt: print_unsigned(&s, (unsigned long)va_arg(args, void *), 16, props); break; case 's': - print_str(&s, va_arg(args, const char *), props); + if (nlong) + print_wstring(&s, va_arg(args, const u16 *), props); + else + print_str(&s, va_arg(args, const char *), props); break; default: addchar(&s, f); From patchwork Thu Jun 30 10:03:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901548 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA0B3CCA47B for ; Thu, 30 Jun 2022 10:04:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234728AbiF3KEN (ORCPT ); Thu, 30 Jun 2022 06:04:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233995AbiF3KEM (ORCPT ); Thu, 30 Jun 2022 06:04:12 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B9FA3443DD for ; Thu, 30 Jun 2022 03:04:06 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5C3201C00; Thu, 30 Jun 2022 03:04:06 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2DA8C3F5A1; Thu, 30 Jun 2022 03:04:05 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 14/27] lib/efi: Add support for getting the cmdline Date: Thu, 30 Jun 2022 11:03:11 +0100 Message-Id: <20220630100324.3153655-15-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change adds support for discovering the command line arguments, as a string. Then, we parse this string to populate __argc and __argv for EFI tests. Signed-off-by: Nikos Nikoleris --- lib/linux/efi.h | 20 ++++++++++ lib/argv.h | 1 + lib/argv.c | 2 +- lib/efi.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) diff --git a/lib/linux/efi.h b/lib/linux/efi.h index 455625a..9a1cf87 100644 --- a/lib/linux/efi.h +++ b/lib/linux/efi.h @@ -60,6 +60,8 @@ typedef guid_t efi_guid_t; #define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) +#define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) + typedef struct { efi_guid_t guid; void *table; @@ -416,6 +418,24 @@ struct efi_boot_memmap { unsigned long *buff_size; }; +#define __aligned_u64 u64 __attribute__((aligned(8))) + +struct efi_loaded_image_64 { + u32 revision; + efi_handle_t parent_handle; + efi_system_table_t *system_table; + efi_handle_t device_handle; + void *file_path; + void *reserved; + u32 load_options_size; + void *load_options; + void *image_base; + __aligned_u64 image_size; + unsigned int image_code_type; + unsigned int image_data_type; + efi_status_t (__efiapi * unload)(efi_handle_t image_handle); +}; + #define efi_bs_call(func, ...) efi_system_table->boottime->func(__VA_ARGS__) #define efi_rs_call(func, ...) efi_system_table->runtime->func(__VA_ARGS__) diff --git a/lib/argv.h b/lib/argv.h index 1fd746d..0fa7772 100644 --- a/lib/argv.h +++ b/lib/argv.h @@ -9,6 +9,7 @@ #define _ARGV_H_ extern void __setup_args(void); +extern void setup_args(const char *args); extern void setup_args_progname(const char *args); extern void setup_env(char *env, int size); extern void add_setup_arg(const char *arg); diff --git a/lib/argv.c b/lib/argv.c index 9ffa673..fa5ff9a 100644 --- a/lib/argv.c +++ b/lib/argv.c @@ -41,7 +41,7 @@ void __setup_args(void) __argc = argv - __argv; } -static void setup_args(const char *args) +void setup_args(const char *args) { if (!args) return; diff --git a/lib/efi.c b/lib/efi.c index 64cc978..f524ec9 100644 --- a/lib/efi.c +++ b/lib/efi.c @@ -8,6 +8,9 @@ */ #include "efi.h" +#include +#include +#include #include #include @@ -96,6 +99,80 @@ static void efi_exit(efi_status_t code) efi_rs_call(reset_system, EFI_RESET_SHUTDOWN, code, 0, NULL); } +/* Adapted from drivers/firmware/efi/libstub/efi-stub.c */ +static char *efi_convert_cmdline(struct efi_loaded_image_64 *image, int *cmd_line_len) +{ + const u16 *s2; + unsigned long cmdline_addr = 0; + int options_chars = image->load_options_size; + const u16 *options = image->load_options; + int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */ + bool in_quote = false; + efi_status_t status; + const int COMMAND_LINE_SIZE = 2048; + + if (options) { + s2 = options; + while (options_bytes < COMMAND_LINE_SIZE && options_chars--) { + u16 c = *s2++; + + if (c < 0x80) { + if (c == L'\0' || c == L'\n') + break; + if (c == L'"') + in_quote = !in_quote; + else if (!in_quote && isspace((char)c)) + safe_options_bytes = options_bytes; + + options_bytes++; + continue; + } + + /* + * Get the number of UTF-8 bytes corresponding to a + * UTF-16 character. + * The first part handles everything in the BMP. + */ + options_bytes += 2 + (c >= 0x800); + /* + * Add one more byte for valid surrogate pairs. Invalid + * surrogates will be replaced with 0xfffd and take up + * only 3 bytes. + */ + if ((c & 0xfc00) == 0xd800) { + /* + * If the very last word is a high surrogate, + * we must ignore it since we can't access the + * low surrogate. + */ + if (!options_chars) { + options_bytes -= 3; + } else if ((*s2 & 0xfc00) == 0xdc00) { + options_bytes++; + options_chars--; + s2++; + } + } + } + if (options_bytes >= COMMAND_LINE_SIZE) { + options_bytes = safe_options_bytes; + printf("Command line is too long: truncated to %d bytes\n", + options_bytes); + } + } + + options_bytes++; /* NUL termination */ + + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes, (void **)&cmdline_addr); + if (status != EFI_SUCCESS) + return NULL; + + snprintf((char *)cmdline_addr, options_bytes, "%.*ls", options_bytes - 1, options); + + *cmd_line_len = options_bytes; + return (char *)cmdline_addr; +} + efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) { int ret; @@ -109,6 +186,31 @@ efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) unsigned long map_size = 0, desc_size = 0, key = 0, buff_size = 0; u32 desc_ver; + /* Helper variables needed to get the cmdline */ + struct efi_loaded_image_64 *image; + efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; + char *cmdline_ptr = NULL; + int cmdline_size = 0; + + /* + * Get a handle to the loaded image protocol. This is used to get + * information about the running image, such as size and the command + * line. + */ + status = efi_bs_call(handle_protocol, handle, &loaded_image_proto, (void *)&image); + if (status != EFI_SUCCESS) { + printf("Failed to get loaded image protocol\n"); + goto efi_main_error; + } + + cmdline_ptr = efi_convert_cmdline(image, &cmdline_size); + if (!cmdline_ptr) { + printf("getting command line via LOADED_IMAGE_PROTOCOL\n"); + status = EFI_OUT_OF_RESOURCES; + goto efi_main_error; + } + setup_args(cmdline_ptr); + /* Set up efi_bootinfo */ efi_bootinfo.mem_map.map = ↦ efi_bootinfo.mem_map.map_size = &map_size; From patchwork Thu Jun 30 10:03:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C0B0C433EF for ; Thu, 30 Jun 2022 10:04:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234729AbiF3KEN (ORCPT ); Thu, 30 Jun 2022 06:04:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234249AbiF3KEM (ORCPT ); Thu, 30 Jun 2022 06:04:12 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 847E943AEF for ; Thu, 30 Jun 2022 03:04:07 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8093B1042; Thu, 30 Jun 2022 03:04:07 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 513873F5A1; Thu, 30 Jun 2022 03:04:06 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: Andrew Jones , andrew.jones@linux.dev, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 15/27] arm/arm64: mmu_disable: Clean and invalidate before disabling Date: Thu, 30 Jun 2022 11:03:12 +0100 Message-Id: <20220630100324.3153655-16-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Andrew Jones The commit message of commit 410b3bf09e76 ("arm/arm64: Perform dcache clean + invalidate after turning MMU off") justifies cleaning and invalidating the dcache after disabling the MMU by saying it's nice not to rely on the current page tables and that it should still work (per the spec), as long as there's an identity map in the current tables. Doing the invalidation after also somewhat helped with reenabling the MMU without seeing stale data, but the real problem with reenabling was because the cache needs to be disabled with the MMU, but it wasn't. Since we have to trust/validate that the current page tables have an identity map anyway, then there's no harm in doing the clean and invalidate first (it feels a little better to do so, anyway, considering the cache maintenance instructions take virtual addresses). Then, also disable the cache with the MMU to avoid problems when reenabling. We invalidate the Icache and disable that too for good measure. And, a final TLB invalidation ensures we're crystal clean when we return from asm_mmu_disable(). Cc: Alexandru Elisei Signed-off-by: Andrew Jones Signed-off-by: Nikos Nikoleris --- arm/cstart.S | 28 +++++++++++++++++++++------- arm/cstart64.S | 21 ++++++++++++++++----- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/arm/cstart.S b/arm/cstart.S index 7036e67..dc324c5 100644 --- a/arm/cstart.S +++ b/arm/cstart.S @@ -179,6 +179,7 @@ halt: .globl asm_mmu_enable asm_mmu_enable: /* TLBIALL */ + mov r2, #0 mcr p15, 0, r2, c8, c7, 0 dsb nsh @@ -211,12 +212,7 @@ asm_mmu_enable: .globl asm_mmu_disable asm_mmu_disable: - /* SCTLR */ - mrc p15, 0, r0, c1, c0, 0 - bic r0, #CR_M - mcr p15, 0, r0, c1, c0, 0 - isb - + /* Clean + invalidate the entire memory */ ldr r0, =__phys_offset ldr r0, [r0] ldr r1, =__phys_end @@ -224,7 +220,25 @@ asm_mmu_disable: sub r1, r1, r0 dcache_by_line_op dccimvac, sy, r0, r1, r2, r3 - mov pc, lr + /* Invalidate Icache */ + mov r0, #0 + mcr p15, 0, r0, c7, c5, 0 + isb + + /* Disable cache, Icache and MMU */ + mrc p15, 0, r0, c1, c0, 0 + bic r0, #CR_C + bic r0, #CR_I + bic r0, #CR_M + mcr p15, 0, r0, c1, c0, 0 + isb + + /* Invalidate TLB */ + mov r0, #0 + mcr p15, 0, r0, c8, c7, 0 + dsb nsh + + mov pc, lr /* * Vectors diff --git a/arm/cstart64.S b/arm/cstart64.S index e4ab7d0..390feb9 100644 --- a/arm/cstart64.S +++ b/arm/cstart64.S @@ -246,11 +246,6 @@ asm_mmu_enable: .globl asm_mmu_disable asm_mmu_disable: - mrs x0, sctlr_el1 - bic x0, x0, SCTLR_EL1_M - msr sctlr_el1, x0 - isb - /* Clean + invalidate the entire memory */ adrp x0, __phys_offset ldr x0, [x0, :lo12:__phys_offset] @@ -259,6 +254,22 @@ asm_mmu_disable: sub x1, x1, x0 dcache_by_line_op civac, sy, x0, x1, x2, x3 + /* Invalidate Icache */ + ic iallu + isb + + /* Disable cache, Icache and MMU */ + mrs x0, sctlr_el1 + bic x0, x0, SCTLR_EL1_C + bic x0, x0, SCTLR_EL1_I + bic x0, x0, SCTLR_EL1_M + msr sctlr_el1, x0 + isb + + /* Invalidate TLB */ + tlbi vmalle1 + dsb nsh + ret /* From patchwork Thu Jun 30 10:03:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901550 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92AB4CCA47B for ; Thu, 30 Jun 2022 10:04:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233919AbiF3KER (ORCPT ); Thu, 30 Jun 2022 06:04:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232924AbiF3KEQ (ORCPT ); Thu, 30 Jun 2022 06:04:16 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 82002443E2 for ; Thu, 30 Jun 2022 03:04:08 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A62121BF7; Thu, 30 Jun 2022 03:04:08 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 755913F5A1; Thu, 30 Jun 2022 03:04:07 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: Andrew Jones , andrew.jones@linux.dev, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 16/27] arm/arm64: Rename etext to _etext Date: Thu, 30 Jun 2022 11:03:13 +0100 Message-Id: <20220630100324.3153655-17-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Andrew Jones Rename etext to the more popular _etext allowing different linker scripts to more easily be used. Signed-off-by: Andrew Jones Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- lib/arm/setup.c | 4 ++-- arm/flat.lds | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/arm/setup.c b/lib/arm/setup.c index 3612991..13513d0 100644 --- a/lib/arm/setup.c +++ b/lib/arm/setup.c @@ -34,7 +34,7 @@ #define NR_EXTRA_MEM_REGIONS 16 #define NR_INITIAL_MEM_REGIONS (MAX_DT_MEM_REGIONS + NR_EXTRA_MEM_REGIONS) -extern unsigned long etext; +extern unsigned long _etext; char *initrd; u32 initrd_size; @@ -140,7 +140,7 @@ unsigned int mem_region_get_flags(phys_addr_t paddr) static void mem_regions_add_assumed(void) { - phys_addr_t code_end = (phys_addr_t)(unsigned long)&etext; + phys_addr_t code_end = (phys_addr_t)(unsigned long)&_etext; struct mem_region *r; r = mem_region_find(code_end - 1); diff --git a/arm/flat.lds b/arm/flat.lds index 47fcb64..9016ac9 100644 --- a/arm/flat.lds +++ b/arm/flat.lds @@ -27,7 +27,7 @@ SECTIONS PROVIDE(_text = .); .text : { *(.init) *(.text) *(.text.*) } . = ALIGN(64K); - PROVIDE(etext = .); + PROVIDE(_etext = .); PROVIDE(reloc_start = .); .rela.dyn : { *(.rela.dyn) } From patchwork Thu Jun 30 10:03:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CBB1C43334 for ; Thu, 30 Jun 2022 10:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234736AbiF3KEU (ORCPT ); Thu, 30 Jun 2022 06:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234313AbiF3KES (ORCPT ); Thu, 30 Jun 2022 06:04:18 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DE3FC443D1 for ; Thu, 30 Jun 2022 03:04:09 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C9B411C01; Thu, 30 Jun 2022 03:04:09 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9AF783F5A1; Thu, 30 Jun 2022 03:04:08 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 17/27] lib: Avoid ms_abi for calls related to EFI on arm64 Date: Thu, 30 Jun 2022 11:03:14 +0100 Message-Id: <20220630100324.3153655-18-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org x86_64 requires that EFI calls use the ms_abi calling convention. For arm64 this is unnecessary. Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/linux/efi.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/linux/efi.h b/lib/linux/efi.h index 9a1cf87..53748dd 100644 --- a/lib/linux/efi.h +++ b/lib/linux/efi.h @@ -33,7 +33,11 @@ typedef u16 efi_char16_t; /* UNICODE character */ typedef u64 efi_physical_addr_t; typedef void *efi_handle_t; +#ifdef __x86_64__ #define __efiapi __attribute__((ms_abi)) +#else +#define __efiapi +#endif /* * The UEFI spec and EDK2 reference implementation both define EFI_GUID as From patchwork Thu Jun 30 10:03:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901552 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFF5BC433EF for ; Thu, 30 Jun 2022 10:04:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234739AbiF3KEX (ORCPT ); Thu, 30 Jun 2022 06:04:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232873AbiF3KET (ORCPT ); Thu, 30 Jun 2022 06:04:19 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C80F12B278 for ; Thu, 30 Jun 2022 03:04:10 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EC90B1C0A; Thu, 30 Jun 2022 03:04:10 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BDFA23F5A1; Thu, 30 Jun 2022 03:04:09 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: Andrew Jones , andrew.jones@linux.dev, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 18/27] arm64: Add a new type of memory type flag MR_F_RESERVED Date: Thu, 30 Jun 2022 11:03:15 +0100 Message-Id: <20220630100324.3153655-19-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Andrew Jones This will be used by future change to add PTE entries for special EFI memory regions. Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- lib/arm/asm/setup.h | 1 + lib/arm/mmu.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/arm/asm/setup.h b/lib/arm/asm/setup.h index f0e70b1..64cd379 100644 --- a/lib/arm/asm/setup.h +++ b/lib/arm/asm/setup.h @@ -15,6 +15,7 @@ extern int nr_cpus; #define MR_F_IO (1U << 0) #define MR_F_CODE (1U << 1) +#define MR_F_RESERVED (1U << 2) #define MR_F_UNKNOWN (1U << 31) struct mem_region { diff --git a/lib/arm/mmu.c b/lib/arm/mmu.c index e1a72fe..931be98 100644 --- a/lib/arm/mmu.c +++ b/lib/arm/mmu.c @@ -174,6 +174,10 @@ void *setup_mmu(phys_addr_t phys_end, void *unused) for (r = mem_regions; r->end; ++r) { if (r->flags & MR_F_IO) { continue; + } else if (r->flags & MR_F_RESERVED) { + /* Reserved pages need to be writable for whatever reserved them */ + mmu_set_range_ptes(mmu_idmap, r->start, r->start, r->end, + __pgprot(PTE_WBWA)); } else if (r->flags & MR_F_CODE) { /* armv8 requires code shared between EL1 and EL0 to be read-only */ mmu_set_range_ptes(mmu_idmap, r->start, r->start, r->end, From patchwork Thu Jun 30 10:03:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2243FC43334 for ; Thu, 30 Jun 2022 10:04:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234313AbiF3KEY (ORCPT ); Thu, 30 Jun 2022 06:04:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234737AbiF3KEW (ORCPT ); Thu, 30 Jun 2022 06:04:22 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4F7D03ED20 for ; Thu, 30 Jun 2022 03:04:12 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1CE8E1063; Thu, 30 Jun 2022 03:04:12 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E177A3F5A1; Thu, 30 Jun 2022 03:04:10 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 19/27] arm/arm64: Add a setup sequence for systems that boot through EFI Date: Thu, 30 Jun 2022 11:03:16 +0100 Message-Id: <20220630100324.3153655-20-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change implements an alternative setup sequence for the system when we are booting through EFI. The memory map is discovered through EFI boot services and devices through ACPI. This change is based on a change initially proposed by Andrew Jones Signed-off-by: Nikos Nikoleris --- lib/linux/efi.h | 1 + lib/arm/asm/setup.h | 2 + lib/arm/setup.c | 181 +++++++++++++++++++++++++++++++++++++++++++- arm/cstart.S | 1 + arm/cstart64.S | 1 + 5 files changed, 184 insertions(+), 2 deletions(-) diff --git a/lib/linux/efi.h b/lib/linux/efi.h index 53748dd..89f9a9e 100644 --- a/lib/linux/efi.h +++ b/lib/linux/efi.h @@ -63,6 +63,7 @@ typedef guid_t efi_guid_t; (c) & 0xff, ((c) >> 8) & 0xff, d } } #define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) +#define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81) #define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) diff --git a/lib/arm/asm/setup.h b/lib/arm/asm/setup.h index 64cd379..c4cd485 100644 --- a/lib/arm/asm/setup.h +++ b/lib/arm/asm/setup.h @@ -6,6 +6,7 @@ * This work is licensed under the terms of the GNU LGPL, version 2. */ #include +#include #include #include @@ -37,5 +38,6 @@ extern unsigned int mem_region_get_flags(phys_addr_t paddr); #define SMP_CACHE_BYTES L1_CACHE_BYTES void setup(const void *fdt, phys_addr_t freemem_start); +efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo); #endif /* _ASMARM_SETUP_H_ */ diff --git a/lib/arm/setup.c b/lib/arm/setup.c index 13513d0..30d04d0 100644 --- a/lib/arm/setup.c +++ b/lib/arm/setup.c @@ -34,7 +34,7 @@ #define NR_EXTRA_MEM_REGIONS 16 #define NR_INITIAL_MEM_REGIONS (MAX_DT_MEM_REGIONS + NR_EXTRA_MEM_REGIONS) -extern unsigned long _etext; +extern unsigned long _text, _etext, _data, _edata; char *initrd; u32 initrd_size; @@ -44,7 +44,10 @@ int nr_cpus; static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1]; struct mem_region *mem_regions = __initial_mem_regions; -phys_addr_t __phys_offset, __phys_end; +phys_addr_t __phys_offset = (phys_addr_t)-1, __phys_end = 0; + +extern void exceptions_init(void); +extern void asm_mmu_disable(void); int mpidr_to_cpu(uint64_t mpidr) { @@ -272,3 +275,177 @@ void setup(const void *fdt, phys_addr_t freemem_start) if (!(auxinfo.flags & AUXINFO_MMU_OFF)) setup_vm(); } + +#ifdef CONFIG_EFI + +#include + +static efi_status_t setup_rsdp(efi_bootinfo_t *efi_bootinfo) +{ + efi_status_t status; + struct rsdp_descriptor *rsdp; + + /* + * RSDP resides in an EFI_ACPI_RECLAIM_MEMORY region, which is not used + * by kvm-unit-tests arm64 memory allocator. So it is not necessary to + * copy the data structure to another memory region to prevent + * unintentional overwrite. + */ + status = efi_get_system_config_table(ACPI_20_TABLE_GUID, (void **)&rsdp); + if (status != EFI_SUCCESS) + return status; + + set_efi_rsdp(rsdp); + + return EFI_SUCCESS; +} + +static efi_status_t efi_mem_init(efi_bootinfo_t *efi_bootinfo) +{ + int i; + unsigned long free_mem_pages = 0; + unsigned long free_mem_start = 0; + struct efi_boot_memmap *map = &(efi_bootinfo->mem_map); + efi_memory_desc_t *buffer = *map->map; + efi_memory_desc_t *d = NULL; + phys_addr_t base, top; + struct mem_region *r; + uintptr_t text = (uintptr_t)&_text, etext = __ALIGN((uintptr_t)&_etext, 4096); + uintptr_t data = (uintptr_t)&_data, edata = __ALIGN((uintptr_t)&_edata, 4096); + + /* + * Record the largest free EFI_CONVENTIONAL_MEMORY region + * which will be used to set up the memory allocator, so that + * the memory allocator can work in the largest free + * continuous memory region. + */ + for (i = 0, r = &mem_regions[0]; i < *(map->map_size); i += *(map->desc_size), ++r) { + d = (efi_memory_desc_t *)(&((u8 *)buffer)[i]); + + r->start = d->phys_addr; + r->end = d->phys_addr + d->num_pages * EFI_PAGE_SIZE; + + switch (d->type) { + case EFI_RESERVED_TYPE: + case EFI_LOADER_DATA: + case EFI_BOOT_SERVICES_CODE: + case EFI_BOOT_SERVICES_DATA: + case EFI_RUNTIME_SERVICES_CODE: + case EFI_RUNTIME_SERVICES_DATA: + case EFI_UNUSABLE_MEMORY: + case EFI_ACPI_RECLAIM_MEMORY: + case EFI_ACPI_MEMORY_NVS: + case EFI_PAL_CODE: + r->flags = MR_F_RESERVED; + break; + case EFI_MEMORY_MAPPED_IO: + case EFI_MEMORY_MAPPED_IO_PORT_SPACE: + r->flags = MR_F_IO; + break; + case EFI_LOADER_CODE: + if (r->start <= text && r->end > text) { + /* This is the unit test region. Flag the code separately. */ + phys_addr_t tmp = r->end; + + assert(etext <= data); + assert(edata <= r->end); + r->flags = MR_F_CODE; + r->end = data; + ++r; + r->start = data; + r->end = tmp; + } else { + r->flags = MR_F_RESERVED; + } + break; + case EFI_CONVENTIONAL_MEMORY: + if (free_mem_pages < d->num_pages) { + free_mem_pages = d->num_pages; + free_mem_start = d->phys_addr; + } + break; + } + + if (!(r->flags & MR_F_IO)) { + if (r->start < __phys_offset) + __phys_offset = r->start; + if (r->end > __phys_end) + __phys_end = r->end; + } + } + __phys_end &= PHYS_MASK; + asm_mmu_disable(); + + if (free_mem_pages == 0) + return EFI_OUT_OF_RESOURCES; + + assert(sizeof(long) == 8 || free_mem_start < (3ul << 30)); + + phys_alloc_init(free_mem_start, free_mem_pages << EFI_PAGE_SHIFT); + phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES); + + phys_alloc_get_unused(&base, &top); + base = PAGE_ALIGN(base); + top = top & PAGE_MASK; + assert(sizeof(long) == 8 || !(base >> 32)); + if (sizeof(long) != 8 && (top >> 32) != 0) + top = ((uint64_t)1 << 32); + page_alloc_init_area(0, base >> PAGE_SHIFT, top >> PAGE_SHIFT); + page_alloc_ops_enable(); + + return EFI_SUCCESS; +} + +efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) +{ + efi_status_t status; + + struct thread_info *ti = current_thread_info(); + + memset(ti, 0, sizeof(*ti)); + + exceptions_init(); + + status = efi_mem_init(efi_bootinfo); + if (status != EFI_SUCCESS) { + printf("Failed to initialize memory: "); + switch (status) { + case EFI_OUT_OF_RESOURCES: + printf("No free memory region\n"); + break; + default: + printf("Unknown error\n"); + break; + } + return status; + } + + status = setup_rsdp(efi_bootinfo); + if (status != EFI_SUCCESS) { + printf("Cannot find RSDP in EFI system table\n"); + return status; + } + + psci_set_conduit(); + cpu_init(); + /* cpu_init must be called before thread_info_init */ + thread_info_init(current_thread_info(), 0); + /* mem_init must be called before io_init */ + io_init(); + + timer_save_state(); + if (initrd) { + /* environ is currently the only file in the initrd */ + char *env = malloc(initrd_size); + + memcpy(env, initrd, initrd_size); + setup_env(env, initrd_size); + } + + if (!(auxinfo.flags & AUXINFO_MMU_OFF)) + setup_vm(); + + return EFI_SUCCESS; +} + +#endif diff --git a/arm/cstart.S b/arm/cstart.S index dc324c5..66a55b9 100644 --- a/arm/cstart.S +++ b/arm/cstart.S @@ -256,6 +256,7 @@ asm_mmu_disable: * * Input r0 is the stack top, which is the exception stacks base */ +.globl exceptions_init exceptions_init: mrc p15, 0, r2, c1, c0, 0 @ read SCTLR bic r2, #CR_V @ SCTLR.V := 0 diff --git a/arm/cstart64.S b/arm/cstart64.S index 390feb9..55b41ea 100644 --- a/arm/cstart64.S +++ b/arm/cstart64.S @@ -276,6 +276,7 @@ asm_mmu_disable: * Vectors */ +.globl exceptions_init exceptions_init: adrp x4, vector_table add x4, x4, :lo12:vector_table From patchwork Thu Jun 30 10:03:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901554 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 770C8C43334 for ; Thu, 30 Jun 2022 10:04:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234756AbiF3KEZ (ORCPT ); Thu, 30 Jun 2022 06:04:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234741AbiF3KEX (ORCPT ); Thu, 30 Jun 2022 06:04:23 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3810C43AEF for ; Thu, 30 Jun 2022 03:04:13 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 402D51042; Thu, 30 Jun 2022 03:04:13 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 11EC33F5A1; Thu, 30 Jun 2022 03:04:11 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 20/27] arm64: Copy code from GNU-EFI Date: Thu, 30 Jun 2022 11:03:17 +0100 Message-Id: <20220630100324.3153655-21-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change adds unmodified dependencies that we need from GNU-EFI in order to build arm64 EFI apps. GNU-EFI sources from https://git.code.sf.net/p/gnu-efi/code v3.0.14 Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- arm/efi/elf_aarch64_efi.lds | 63 +++++++++++++++++ arm/efi/crt0-efi-aarch64.S | 130 ++++++++++++++++++++++++++++++++++++ arm/efi/reloc_aarch64.c | 97 +++++++++++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 arm/efi/elf_aarch64_efi.lds create mode 100644 arm/efi/crt0-efi-aarch64.S create mode 100644 arm/efi/reloc_aarch64.c diff --git a/arm/efi/elf_aarch64_efi.lds b/arm/efi/elf_aarch64_efi.lds new file mode 100644 index 0000000..836d982 --- /dev/null +++ b/arm/efi/elf_aarch64_efi.lds @@ -0,0 +1,63 @@ +OUTPUT_FORMAT("elf64-littleaarch64", "elf64-littleaarch64", "elf64-littleaarch64") +OUTPUT_ARCH(aarch64) +ENTRY(_start) +SECTIONS +{ + .text 0x0 : { + _text = .; + *(.text.head) + *(.text) + *(.text.*) + *(.gnu.linkonce.t.*) + *(.srodata) + *(.rodata*) + . = ALIGN(16); + } + _etext = .; + _text_size = . - _text; + .dynamic : { *(.dynamic) } + .data : ALIGN(4096) + { + _data = .; + *(.sdata) + *(.data) + *(.data1) + *(.data.*) + *(.got.plt) + *(.got) + + /* the EFI loader doesn't seem to like a .bss section, so we stick + it all into .data: */ + . = ALIGN(16); + _bss = .; + *(.sbss) + *(.scommon) + *(.dynbss) + *(.bss) + *(COMMON) + . = ALIGN(16); + _bss_end = .; + } + + .rela.dyn : { *(.rela.dyn) } + .rela.plt : { *(.rela.plt) } + .rela.got : { *(.rela.got) } + .rela.data : { *(.rela.data) *(.rela.data*) } + . = ALIGN(512); + _edata = .; + _data_size = . - _data; + + . = ALIGN(4096); + .dynsym : { *(.dynsym) } + . = ALIGN(4096); + .dynstr : { *(.dynstr) } + . = ALIGN(4096); + .note.gnu.build-id : { *(.note.gnu.build-id) } + /DISCARD/ : + { + *(.rel.reloc) + *(.eh_frame) + *(.note.GNU-stack) + } + .comment 0 : { *(.comment) } +} diff --git a/arm/efi/crt0-efi-aarch64.S b/arm/efi/crt0-efi-aarch64.S new file mode 100644 index 0000000..d50e78d --- /dev/null +++ b/arm/efi/crt0-efi-aarch64.S @@ -0,0 +1,130 @@ +/* + * crt0-efi-aarch64.S - PE/COFF header for AArch64 EFI applications + * + * Copright (C) 2014 Linaro Ltd. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice and this list of conditions, without modification. + * 2. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * Alternatively, this software may be distributed 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. + */ + + .section .text.head + + /* + * Magic "MZ" signature for PE/COFF + */ + .globl ImageBase +ImageBase: + .ascii "MZ" + .skip 58 // 'MZ' + pad + offset == 64 + .long pe_header - ImageBase // Offset to the PE header. +pe_header: + .ascii "PE" + .short 0 +coff_header: + .short 0xaa64 // AArch64 + .short 2 // nr_sections + .long 0 // TimeDateStamp + .long 0 // PointerToSymbolTable + .long 0 // NumberOfSymbols + .short section_table - optional_header // SizeOfOptionalHeader + .short 0x206 // Characteristics. + // IMAGE_FILE_DEBUG_STRIPPED | + // IMAGE_FILE_EXECUTABLE_IMAGE | + // IMAGE_FILE_LINE_NUMS_STRIPPED +optional_header: + .short 0x20b // PE32+ format + .byte 0x02 // MajorLinkerVersion + .byte 0x14 // MinorLinkerVersion + .long _data - _start // SizeOfCode + .long _data_size // SizeOfInitializedData + .long 0 // SizeOfUninitializedData + .long _start - ImageBase // AddressOfEntryPoint + .long _start - ImageBase // BaseOfCode + +extra_header_fields: + .quad 0 // ImageBase + .long 0x1000 // SectionAlignment + .long 0x200 // FileAlignment + .short 0 // MajorOperatingSystemVersion + .short 0 // MinorOperatingSystemVersion + .short 0 // MajorImageVersion + .short 0 // MinorImageVersion + .short 0 // MajorSubsystemVersion + .short 0 // MinorSubsystemVersion + .long 0 // Win32VersionValue + + .long _edata - ImageBase // SizeOfImage + + // Everything before the kernel image is considered part of the header + .long _start - ImageBase // SizeOfHeaders + .long 0 // CheckSum + .short EFI_SUBSYSTEM // Subsystem + .short 0 // DllCharacteristics + .quad 0 // SizeOfStackReserve + .quad 0 // SizeOfStackCommit + .quad 0 // SizeOfHeapReserve + .quad 0 // SizeOfHeapCommit + .long 0 // LoaderFlags + .long 0x6 // NumberOfRvaAndSizes + + .quad 0 // ExportTable + .quad 0 // ImportTable + .quad 0 // ResourceTable + .quad 0 // ExceptionTable + .quad 0 // CertificationTable + .quad 0 // BaseRelocationTable + + // Section table +section_table: + .ascii ".text\0\0\0" + .long _data - _start // VirtualSize + .long _start - ImageBase // VirtualAddress + .long _data - _start // SizeOfRawData + .long _start - ImageBase // PointerToRawData + + .long 0 // PointerToRelocations (0 for executables) + .long 0 // PointerToLineNumbers (0 for executables) + .short 0 // NumberOfRelocations (0 for executables) + .short 0 // NumberOfLineNumbers (0 for executables) + .long 0x60000020 // Characteristics (section flags) + + .ascii ".data\0\0\0" + .long _data_size // VirtualSize + .long _data - ImageBase // VirtualAddress + .long _data_size // SizeOfRawData + .long _data - ImageBase // PointerToRawData + + .long 0 // PointerToRelocations (0 for executables) + .long 0 // PointerToLineNumbers (0 for executables) + .short 0 // NumberOfRelocations (0 for executables) + .short 0 // NumberOfLineNumbers (0 for executables) + .long 0xc0000040 // Characteristics (section flags) + + .align 12 +_start: + stp x29, x30, [sp, #-32]! + mov x29, sp + + stp x0, x1, [sp, #16] + mov x2, x0 + mov x3, x1 + adr x0, ImageBase + adrp x1, _DYNAMIC + add x1, x1, #:lo12:_DYNAMIC + bl _relocate + cbnz x0, 0f + + ldp x0, x1, [sp, #16] + bl efi_main + +0: ldp x29, x30, [sp], #32 + ret diff --git a/arm/efi/reloc_aarch64.c b/arm/efi/reloc_aarch64.c new file mode 100644 index 0000000..0867279 --- /dev/null +++ b/arm/efi/reloc_aarch64.c @@ -0,0 +1,97 @@ +/* reloc_aarch64.c - position independent x86 ELF shared object relocator + Copyright (C) 2014 Linaro Ltd. + Copyright (C) 1999 Hewlett-Packard Co. + Contributed by David Mosberger . + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + * Neither the name of Hewlett-Packard Co. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +#include +#include + +#include + +EFI_STATUS _relocate (long ldbase, Elf64_Dyn *dyn, + EFI_HANDLE image EFI_UNUSED, + EFI_SYSTEM_TABLE *systab EFI_UNUSED) +{ + long relsz = 0, relent = 0; + Elf64_Rela *rel = 0; + unsigned long *addr; + int i; + + for (i = 0; dyn[i].d_tag != DT_NULL; ++i) { + switch (dyn[i].d_tag) { + case DT_RELA: + rel = (Elf64_Rela*) + ((unsigned long)dyn[i].d_un.d_ptr + + ldbase); + break; + + case DT_RELASZ: + relsz = dyn[i].d_un.d_val; + break; + + case DT_RELAENT: + relent = dyn[i].d_un.d_val; + break; + + default: + break; + } + } + + if (!rel && relent == 0) + return EFI_SUCCESS; + + if (!rel || relent == 0) + return EFI_LOAD_ERROR; + + while (relsz > 0) { + /* apply the relocs */ + switch (ELF64_R_TYPE (rel->r_info)) { + case R_AARCH64_NONE: + break; + + case R_AARCH64_RELATIVE: + addr = (unsigned long *) + (ldbase + rel->r_offset); + *addr = ldbase + rel->r_addend; + break; + + default: + break; + } + rel = (Elf64_Rela*) ((char *) rel + relent); + relsz -= relent; + } + return EFI_SUCCESS; +} From patchwork Thu Jun 30 10:03:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC97DC433EF for ; Thu, 30 Jun 2022 10:04:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234768AbiF3KE2 (ORCPT ); Thu, 30 Jun 2022 06:04:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234733AbiF3KEX (ORCPT ); Thu, 30 Jun 2022 06:04:23 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 71E8D43ADF for ; Thu, 30 Jun 2022 03:04:14 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 67D2C1C00; Thu, 30 Jun 2022 03:04:14 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3489A3F5A1; Thu, 30 Jun 2022 03:04:13 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 21/27] arm64: Change GNU-EFI imported file to use defined types Date: Thu, 30 Jun 2022 11:03:18 +0100 Message-Id: <20220630100324.3153655-22-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Signed-off-by: Nikos Nikoleris --- arm/efi/reloc_aarch64.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arm/efi/reloc_aarch64.c b/arm/efi/reloc_aarch64.c index 0867279..fa0cd6b 100644 --- a/arm/efi/reloc_aarch64.c +++ b/arm/efi/reloc_aarch64.c @@ -34,14 +34,11 @@ SUCH DAMAGE. */ -#include -#include - +#include "efi.h" #include -EFI_STATUS _relocate (long ldbase, Elf64_Dyn *dyn, - EFI_HANDLE image EFI_UNUSED, - EFI_SYSTEM_TABLE *systab EFI_UNUSED) +efi_status_t _relocate(long ldbase, Elf64_Dyn *dyn, efi_handle_t image, + efi_system_table_t *sys_tab) { long relsz = 0, relent = 0; Elf64_Rela *rel = 0; From patchwork Thu Jun 30 10:03:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901556 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84640C43334 for ; Thu, 30 Jun 2022 10:04:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234770AbiF3KEa (ORCPT ); Thu, 30 Jun 2022 06:04:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234747AbiF3KEY (ORCPT ); Thu, 30 Jun 2022 06:04:24 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 819962A732 for ; Thu, 30 Jun 2022 03:04:15 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8DCBA1C14; Thu, 30 Jun 2022 03:04:15 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5D0993F5A1; Thu, 30 Jun 2022 03:04:14 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 22/27] arm64: Use code from the gnu-efi when booting with EFI Date: Thu, 30 Jun 2022 11:03:19 +0100 Message-Id: <20220630100324.3153655-23-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org arm/efi/crt0-efi-aarch64.S defines the header and the handover sequence from EFI to a efi_main. This change includes the whole file in arm/cstart64.S when we compile with EFI support. In addition, we change the handover code in arm/efi/crt0-efi-aarch64.S to align the stack pointer. This alignment is necessary because we make assumptions about cpu0's stack alignment and most importantly we place its thread_info at the bottom of this stack. Signed-off-by: Nikos Nikoleris --- arm/cstart64.S | 6 ++++++ arm/efi/crt0-efi-aarch64.S | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/arm/cstart64.S b/arm/cstart64.S index 55b41ea..08cf02f 100644 --- a/arm/cstart64.S +++ b/arm/cstart64.S @@ -15,6 +15,10 @@ #include #include +#ifdef CONFIG_EFI +#include "efi/crt0-efi-aarch64.S" +#else + .macro zero_range, tmp1, tmp2 9998: cmp \tmp1, \tmp2 b.eq 9997f @@ -107,6 +111,8 @@ start: bl exit b halt +#endif + .text /* diff --git a/arm/efi/crt0-efi-aarch64.S b/arm/efi/crt0-efi-aarch64.S index d50e78d..03d29b0 100644 --- a/arm/efi/crt0-efi-aarch64.S +++ b/arm/efi/crt0-efi-aarch64.S @@ -111,10 +111,19 @@ section_table: .align 12 _start: - stp x29, x30, [sp, #-32]! + stp x29, x30, [sp, #-16]! + + /* Align sp; this is necessary due to way we store cpu0's thread_info */ mov x29, sp + and x29, x29, #THREAD_MASK + mov x30, sp + mov sp, x29 + str x30, [sp, #-16]! + + mov x29, sp + + stp x0, x1, [sp, #-16]! - stp x0, x1, [sp, #16] mov x2, x0 mov x3, x1 adr x0, ImageBase @@ -123,8 +132,12 @@ _start: bl _relocate cbnz x0, 0f - ldp x0, x1, [sp, #16] + ldp x0, x1, [sp], #16 bl efi_main -0: ldp x29, x30, [sp], #32 + /* Restore sp */ + ldr x30, [sp], #16 + mov sp, x30 + +0: ldp x29, x30, [sp], #16 ret From patchwork Thu Jun 30 10:03:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FCEAC43334 for ; Thu, 30 Jun 2022 10:04:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234759AbiF3KEc (ORCPT ); Thu, 30 Jun 2022 06:04:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234753AbiF3KEY (ORCPT ); Thu, 30 Jun 2022 06:04:24 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8C094443D9 for ; Thu, 30 Jun 2022 03:04:16 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B057A1BF7; Thu, 30 Jun 2022 03:04:16 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 81FFB3F5A1; Thu, 30 Jun 2022 03:04:15 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 23/27] lib: Avoid external dependency in libelf Date: Thu, 30 Jun 2022 11:03:20 +0100 Message-Id: <20220630100324.3153655-24-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org There is just a small number of definitions we need from uapi/linux/elf.h and asm/elf.h and the relocation code is self-contained. Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- lib/elf.h | 57 +++++++++++++++++++++++++++++++++++++++++ arm/efi/reloc_aarch64.c | 3 +-- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 lib/elf.h diff --git a/lib/elf.h b/lib/elf.h new file mode 100644 index 0000000..abd5cf4 --- /dev/null +++ b/lib/elf.h @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: LGPL-2.0-or-later */ +/* + * Relevant definitions from uapi/linux/elf.h and asm/elf.h + */ + +#ifndef _ELF_H_ +#define _ELF_H_ + +#include + +/* 64-bit ELF base types. */ +typedef u64 Elf64_Addr; +typedef u64 Elf64_Xword; +typedef s64 Elf64_Sxword; + +typedef struct { + Elf64_Sxword d_tag; /* entry tag value */ + union { + Elf64_Xword d_val; + Elf64_Addr d_ptr; + } d_un; +} Elf64_Dyn; + +typedef struct elf64_rel { + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ +} Elf64_Rel; + +typedef struct elf64_rela { + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ + Elf64_Sxword r_addend; /* Constant addend used to compute value */ +} Elf64_Rela; + +/* This is the info that is needed to parse the dynamic section of the file */ +#define DT_NULL 0 +#define DT_RELA 7 +#define DT_RELASZ 8 +#define DT_RELAENT 9 + +/* x86 relocation types. */ +#define R_X86_64_NONE 0 /* No reloc */ +#define R_X86_64_RELATIVE 8 /* Adjust by program base */ + + +/* + * AArch64 static relocation types. + */ + +/* Miscellaneous. */ +#define R_AARCH64_NONE 256 +#define R_AARCH64_RELATIVE 1027 + +/* The following are used with relocations */ +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) + +#endif /* _ELF_H_ */ diff --git a/arm/efi/reloc_aarch64.c b/arm/efi/reloc_aarch64.c index fa0cd6b..3f6d9a6 100644 --- a/arm/efi/reloc_aarch64.c +++ b/arm/efi/reloc_aarch64.c @@ -34,8 +34,7 @@ SUCH DAMAGE. */ -#include "efi.h" -#include +#include efi_status_t _relocate(long ldbase, Elf64_Dyn *dyn, efi_handle_t image, efi_system_table_t *sys_tab) From patchwork Thu Jun 30 10:03:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901558 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EDCBC433EF for ; Thu, 30 Jun 2022 10:04:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234766AbiF3KEf (ORCPT ); Thu, 30 Jun 2022 06:04:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234761AbiF3KE0 (ORCPT ); Thu, 30 Jun 2022 06:04:26 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E296A43EEB for ; Thu, 30 Jun 2022 03:04:17 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D66591C25; Thu, 30 Jun 2022 03:04:17 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A4E543F93E; Thu, 30 Jun 2022 03:04:16 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 24/27] x86: Move x86_64-specific EFI CFLAGS to x86_64 Makefile Date: Thu, 30 Jun 2022 11:03:21 +0100 Message-Id: <20220630100324.3153655-25-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Compiler flag -macculate-outgoing-args is only needed by the x86_64 ABI. Move it to the relevant Makefile. Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- Makefile | 4 ---- x86/Makefile.x86_64 | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 6ed5dea..307bc29 100644 --- a/Makefile +++ b/Makefile @@ -40,14 +40,10 @@ OBJDIRS += $(LIBFDT_objdir) # EFI App ifeq ($(CONFIG_EFI),y) -EFI_ARCH = x86_64 EFI_CFLAGS := -DCONFIG_EFI # The following CFLAGS and LDFLAGS come from: # - GNU-EFI/Makefile.defaults # - GNU-EFI/apps/Makefile -# Function calls must include the number of arguments passed to the functions -# More details: https://wiki.osdev.org/GNU-EFI -EFI_CFLAGS += -maccumulate-outgoing-args # GCC defines wchar to be 32 bits, but EFI expects 16 bits EFI_CFLAGS += -fshort-wchar # EFI applications use PIC as they are loaded to dynamic addresses, not a fixed diff --git a/x86/Makefile.x86_64 b/x86/Makefile.x86_64 index e19284a..c0ffac4 100644 --- a/x86/Makefile.x86_64 +++ b/x86/Makefile.x86_64 @@ -2,6 +2,10 @@ cstart.o = $(TEST_DIR)/cstart64.o bits = 64 ldarch = elf64-x86-64 ifeq ($(CONFIG_EFI),y) +# Function calls must include the number of arguments passed to the functions +# More details: https://wiki.osdev.org/GNU-EFI +CFLAGS += -maccumulate-outgoing-args + exe = efi bin = so FORMAT = efi-app-x86_64 From patchwork Thu Jun 30 10:03:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 964EBC43334 for ; Thu, 30 Jun 2022 10:04:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234794AbiF3KEg (ORCPT ); Thu, 30 Jun 2022 06:04:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234772AbiF3KEb (ORCPT ); Thu, 30 Jun 2022 06:04:31 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0B0C0443D1 for ; Thu, 30 Jun 2022 03:04:18 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 05D3E1C01; Thu, 30 Jun 2022 03:04:19 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CB2703F5A1; Thu, 30 Jun 2022 03:04:17 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 25/27] arm64: Add support for efi in Makefile Date: Thu, 30 Jun 2022 11:03:22 +0100 Message-Id: <20220630100324.3153655-26-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Users can now build kvm-unit-tests as efi apps by supplying an extra argument when invoking configure: $> ./configure --enable-efi This patch is based on an earlier version by Andrew Jones Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- configure | 15 ++++++++++++--- arm/Makefile.arm | 6 ++++++ arm/Makefile.arm64 | 18 ++++++++++++++---- arm/Makefile.common | 45 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/configure b/configure index 5b7daac..2ff9881 100755 --- a/configure +++ b/configure @@ -196,14 +196,19 @@ else fi fi -if [ "$efi" ] && [ "$arch" != "x86_64" ]; then +if [ "$efi" ] && [ "$arch" != "x86_64" ] && [ "$arch" != "arm64" ]; then echo "--[enable|disable]-efi is not supported for $arch" usage fi if [ -z "$page_size" ]; then - [ "$arch" = "arm64" ] && page_size="65536" - [ "$arch" = "arm" ] && page_size="4096" + if [ "$efi" = 'y' ] && [ "$arch" = "arm64" ]; then + page_size="4096" + elif [ "$arch" = "arm64" ]; then + page_size="65536" + elif [ "$arch" = "arm" ]; then + page_size="4096" + fi else if [ "$arch" != "arm64" ]; then echo "--page-size is not supported for $arch" @@ -218,6 +223,10 @@ else echo "arm64 doesn't support page size of $page_size" usage fi + if [ "$efi" = 'y' ] && [ "$page_size" != "4096" ]; then + echo "efi must use 4K pages" + exit 1 + fi fi [ -z "$processor" ] && processor="$arch" diff --git a/arm/Makefile.arm b/arm/Makefile.arm index 01fd4c7..2ce00f5 100644 --- a/arm/Makefile.arm +++ b/arm/Makefile.arm @@ -7,6 +7,10 @@ bits = 32 ldarch = elf32-littlearm machine = -marm -mfpu=vfp +ifeq ($(CONFIG_EFI),y) +$(error Cannot build arm32 tests as EFI apps) +endif + # stack.o relies on frame pointers. KEEP_FRAME_POINTER := y @@ -32,6 +36,8 @@ cflatobjs += lib/arm/stack.o cflatobjs += lib/ldiv32.o cflatobjs += lib/arm/ldivmod.o +exe = flat + # arm specific tests tests = diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64 index 42e18e7..fd3d681 100644 --- a/arm/Makefile.arm64 +++ b/arm/Makefile.arm64 @@ -27,11 +27,21 @@ cflatobjs += lib/arm64/gic-v3-its.o lib/arm64/gic-v3-its-cmd.o OBJDIRS += lib/arm64 +ifeq ($(CONFIG_EFI),y) +# avoid jump tables before all relocations have been processed +arm/efi/reloc_aarch64.o: CFLAGS += -fno-jump-tables +cflatobjs += arm/efi/reloc_aarch64.o + +exe = efi +else +exe = flat +endif + # arm64 specific tests -tests = $(TEST_DIR)/timer.flat -tests += $(TEST_DIR)/micro-bench.flat -tests += $(TEST_DIR)/cache.flat -tests += $(TEST_DIR)/debug.flat +tests = $(TEST_DIR)/timer.$(exe) +tests += $(TEST_DIR)/micro-bench.$(exe) +tests += $(TEST_DIR)/cache.$(exe) +tests += $(TEST_DIR)/debug.$(exe) include $(SRCDIR)/$(TEST_DIR)/Makefile.common diff --git a/arm/Makefile.common b/arm/Makefile.common index 5be42c0..a8007f4 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -4,14 +4,14 @@ # Authors: Andrew Jones # -tests-common = $(TEST_DIR)/selftest.flat -tests-common += $(TEST_DIR)/spinlock-test.flat -tests-common += $(TEST_DIR)/pci-test.flat -tests-common += $(TEST_DIR)/pmu.flat -tests-common += $(TEST_DIR)/gic.flat -tests-common += $(TEST_DIR)/psci.flat -tests-common += $(TEST_DIR)/sieve.flat -tests-common += $(TEST_DIR)/pl031.flat +tests-common = $(TEST_DIR)/selftest.$(exe) +tests-common += $(TEST_DIR)/spinlock-test.$(exe) +tests-common += $(TEST_DIR)/pci-test.$(exe) +tests-common += $(TEST_DIR)/pmu.$(exe) +tests-common += $(TEST_DIR)/gic.$(exe) +tests-common += $(TEST_DIR)/psci.$(exe) +tests-common += $(TEST_DIR)/sieve.$(exe) +tests-common += $(TEST_DIR)/pl031.$(exe) tests-all = $(tests-common) $(tests) all: directories $(tests-all) @@ -54,6 +54,9 @@ cflatobjs += lib/arm/smp.o cflatobjs += lib/arm/delay.o cflatobjs += lib/arm/gic.o lib/arm/gic-v2.o lib/arm/gic-v3.o cflatobjs += lib/arm/timer.o +ifeq ($(CONFIG_EFI),y) +cflatobjs += lib/efi.o +endif OBJDIRS += lib/arm @@ -61,6 +64,25 @@ libeabi = lib/arm/libeabi.a eabiobjs = lib/arm/eabi_compat.o FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libeabi) + +ifeq ($(CONFIG_EFI),y) +%.so: EFI_LDFLAGS += -defsym=EFI_SUBSYSTEM=0xa --no-undefined +%.so: %.o $(FLATLIBS) $(SRCDIR)/arm/efi/elf_aarch64_efi.lds $(cstart.o) + $(CC) $(CFLAGS) -c -o $(@:.so=.aux.o) $(SRCDIR)/lib/auxinfo.c \ + -DPROGNAME=\"$(@:.so=.efi)\" -DAUXFLAGS=$(AUXFLAGS) + $(LD) $(EFI_LDFLAGS) -o $@ -T $(SRCDIR)/arm/efi/elf_aarch64_efi.lds \ + $(filter %.o, $^) $(FLATLIBS) $(@:.so=.aux.o) \ + $(EFI_LIBS) + $(RM) $(@:.so=.aux.o) + +%.efi: %.so + $(call arch_elf_check, $^) + $(OBJCOPY) \ + -j .text -j .sdata -j .data -j .dynamic -j .dynsym \ + -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \ + -j .reloc \ + -O binary $^ $@ +else %.elf: LDFLAGS = -nostdlib $(arch_LDFLAGS) %.elf: %.o $(FLATLIBS) $(SRCDIR)/arm/flat.lds $(cstart.o) $(CC) $(CFLAGS) -c -o $(@:.elf=.aux.o) $(SRCDIR)/lib/auxinfo.c \ @@ -74,13 +96,14 @@ FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libeabi) $(call arch_elf_check, $^) $(OBJCOPY) -O binary $^ $@ @chmod a-x $@ +endif $(libeabi): $(eabiobjs) $(AR) rcs $@ $^ arm_clean: asm_offsets_clean - $(RM) $(TEST_DIR)/*.{o,flat,elf} $(libeabi) $(eabiobjs) \ - $(TEST_DIR)/.*.d lib/arm/.*.d + $(RM) $(TEST_DIR)/*.{o,flat,elf,so,efi} $(libeabi) $(eabiobjs) \ + $(TEST_DIR)/.*.d $(TEST_DIR)/efi/.*.d lib/arm/.*.d generated-files = $(asm-offsets) -$(tests-all:.flat=.o) $(cstart.o) $(cflatobjs): $(generated-files) +$(tests-all:.$(exe)=.o) $(cstart.o) $(cflatobjs): $(generated-files) From patchwork Thu Jun 30 10:03:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6573C43334 for ; Thu, 30 Jun 2022 10:04:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234778AbiF3KEi (ORCPT ); Thu, 30 Jun 2022 06:04:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234780AbiF3KEc (ORCPT ); Thu, 30 Jun 2022 06:04:32 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0D88144750 for ; Thu, 30 Jun 2022 03:04:20 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2C0261C0A; Thu, 30 Jun 2022 03:04:20 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EF1913F5A1; Thu, 30 Jun 2022 03:04:18 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: Alexandru Elisei , andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 26/27] lib: arm: Print test exit status Date: Thu, 30 Jun 2022 11:03:23 +0100 Message-Id: <20220630100324.3153655-27-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexandru Elisei The arm tests can be run under kvmtool, which doesn't emulate a chr-testdev device. Print the test exit status to make it possible for the runner scripts to pick it up when they have support for it. Signed-off-by: Alexandru Elisei Signed-off-by: Nikos Nikoleris Reviewed-by: Andrew Jones --- lib/arm/io.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/arm/io.c b/lib/arm/io.c index a91f116..337cf1b 100644 --- a/lib/arm/io.c +++ b/lib/arm/io.c @@ -138,6 +138,12 @@ extern void halt(int code); void exit(int code) { + /* + * Print the test return code in the format used by chr-testdev so the + * runner can pick it up if there is chr-testdev is not present. + */ + printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1); + chr_testdev_exit(code); psci_system_off(); halt(code); From patchwork Thu Jun 30 10:03:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12901561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 291D1C43334 for ; Thu, 30 Jun 2022 10:04:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234805AbiF3KEp (ORCPT ); Thu, 30 Jun 2022 06:04:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234757AbiF3KEd (ORCPT ); Thu, 30 Jun 2022 06:04:33 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 520734475C for ; Thu, 30 Jun 2022 03:04:21 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4E79A1C2B; Thu, 30 Jun 2022 03:04:21 -0700 (PDT) Received: from godel.lab.cambridge.arm.com (godel.lab.cambridge.arm.com [10.7.66.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2060E3F5A1; Thu, 30 Jun 2022 03:04:20 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: andrew.jones@linux.dev, drjones@redhat.com, pbonzini@redhat.com, jade.alglave@arm.com, alexandru.elisei@arm.com, ricarkol@google.com Subject: [kvm-unit-tests PATCH v3 27/27] arm64: Add an efi/run script Date: Thu, 30 Jun 2022 11:03:24 +0100 Message-Id: <20220630100324.3153655-28-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220630100324.3153655-1-nikos.nikoleris@arm.com> References: <20220630100324.3153655-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change adds a efi/run script inspired by the one in x86. This script will setup a folder with the test compiled as an EFI app and a startup.nsh script. The script launches QEMU providing an image with EDKII and the path to the folder with the test which is executed automatically. For example: $> ./arm/efi/run ./arm/selftest.efi setup smp=2 mem=256 Signed-off-by: Nikos Nikoleris Reviewed-by: Ricardo Koller --- scripts/runtime.bash | 14 +++++----- arm/efi/run | 61 ++++++++++++++++++++++++++++++++++++++++++++ arm/run | 14 +++++++--- arm/Makefile.common | 1 + arm/dummy.c | 4 +++ 5 files changed, 82 insertions(+), 12 deletions(-) create mode 100755 arm/efi/run create mode 100644 arm/dummy.c diff --git a/scripts/runtime.bash b/scripts/runtime.bash index 7d0180b..dc28f24 100644 --- a/scripts/runtime.bash +++ b/scripts/runtime.bash @@ -131,14 +131,12 @@ function run() fi last_line=$(premature_failure > >(tail -1)) && { - skip=true - if [ "${CONFIG_EFI}" == "y" ] && [[ "${last_line}" =~ "enabling apic" ]]; then - skip=false - fi - if [ ${skip} == true ]; then - print_result "SKIP" $testname "" "$last_line" - return 77 - fi + if [ "${CONFIG_EFI}" == "y" ] && [ "${ARCH}" = x86_64 ]; then + if ! [[ "${last_line}" =~ "enabling apic" ]]; then + print_result "SKIP" $testname "" "$last_line" + return 77 + fi + fi } cmdline=$(get_cmdline $kernel) diff --git a/arm/efi/run b/arm/efi/run new file mode 100755 index 0000000..dfff717 --- /dev/null +++ b/arm/efi/run @@ -0,0 +1,61 @@ +#!/bin/bash + +set -e + +if [ $# -eq 0 ]; then + echo "Usage $0 TEST_CASE [QEMU_ARGS]" + exit 2 +fi + +if [ ! -f config.mak ]; then + echo "run './configure --enable-efi && make' first. See ./configure -h" + exit 2 +fi +source config.mak +source scripts/arch-run.bash +source scripts/common.bash + +: "${EFI_SRC:=$(realpath "$(dirname "$0")/../")}" +: "${EFI_UEFI:=/usr/share/qemu-efi-aarch64/QEMU_EFI.fd}" +: "${EFI_TEST:=efi-tests}" +: "${EFI_CASE:=$(basename $1 .efi)}" + +if [ ! -f "$EFI_UEFI" ]; then + echo "UEFI firmware not found: $EFI_UEFI" + echo "Please install the UEFI firmware to this path" + echo "Or specify the correct path with the env variable EFI_UEFI" + exit 2 +fi + +# Remove the TEST_CASE from $@ +shift 1 + +# Fish out the arguments for the test, they should be the next string +# after the "-append" option +qemu_args=() +cmd_args=() +while (( "$#" )); do + if [ "$1" = "-append" ]; then + cmd_args=$2 + shift 2 + else + qemu_args+=("$1") + shift 1 + fi +done + +if [ "$EFI_CASE" = "_NO_FILE_4Uhere_" ]; then + EFI_CASE=dummy +fi + +: "${EFI_CASE_DIR:="$EFI_TEST/$EFI_CASE"}" +mkdir -p "$EFI_CASE_DIR" + +cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_TEST/$EFI_CASE/" +echo "@echo -off" > "$EFI_TEST/$EFI_CASE/startup.nsh" +echo "$EFI_CASE.efi" "${cmd_args[@]}" >> "$EFI_TEST/$EFI_CASE/startup.nsh" + +EFI_RUN=y $TEST_DIR/run \ + -bios "$EFI_UEFI" \ + -drive file.dir="$EFI_TEST/$EFI_CASE/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \ + "${qemu_args[@]}" diff --git a/arm/run b/arm/run index 1284891..62f845b 100755 --- a/arm/run +++ b/arm/run @@ -65,8 +65,10 @@ if $qemu $M -chardev testdev,id=id -initrd . 2>&1 \ exit 2 fi -chr_testdev='-device virtio-serial-device' -chr_testdev+=' -device virtconsole,chardev=ctd -chardev testdev,id=ctd' +if [ "$EFI_RUN" != "y" ]; then + chr_testdev='-device virtio-serial-device' + chr_testdev+=' -device virtconsole,chardev=ctd -chardev testdev,id=ctd' +fi pci_testdev= if $qemu $M -device '?' 2>&1 | grep pci-testdev > /dev/null; then @@ -75,7 +77,11 @@ fi A="-accel $ACCEL" command="$qemu -nodefaults $M $A -cpu $processor $chr_testdev $pci_testdev" -command+=" -display none -serial stdio -kernel" +command+=" -display none -serial stdio" command="$(migration_cmd) $(timeout_cmd) $command" -run_qemu $command "$@" +if [ "$EFI_RUN" = "y" ]; then + ENVIRON_DEFAULT=n run_qemu_status $command "$@" +else + run_qemu $command -kernel "$@" +fi diff --git a/arm/Makefile.common b/arm/Makefile.common index a8007f4..aabd335 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -12,6 +12,7 @@ tests-common += $(TEST_DIR)/gic.$(exe) tests-common += $(TEST_DIR)/psci.$(exe) tests-common += $(TEST_DIR)/sieve.$(exe) tests-common += $(TEST_DIR)/pl031.$(exe) +tests-common += $(TEST_DIR)/dummy.$(exe) tests-all = $(tests-common) $(tests) all: directories $(tests-all) diff --git a/arm/dummy.c b/arm/dummy.c new file mode 100644 index 0000000..5019e79 --- /dev/null +++ b/arm/dummy.c @@ -0,0 +1,4 @@ +int main(int argc, char **argv) +{ + return 0; +}