From patchwork Tue Mar 5 13:53:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 225048 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7650F2C031E for ; Wed, 6 Mar 2013 01:05:34 +1100 (EST) Received: from localhost ([::1]:53366 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCsUm-0003cD-KK for incoming@patchwork.ozlabs.org; Tue, 05 Mar 2013 09:05:32 -0500 Received: from eggs.gnu.org ([208.118.235.92]:52769) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCsUH-0003aY-0X for qemu-devel@nongnu.org; Tue, 05 Mar 2013 09:05:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UCsJj-0006n5-0Z for qemu-devel@nongnu.org; Tue, 05 Mar 2013 08:54:19 -0500 Received: from e23smtp01.au.ibm.com ([202.81.31.143]:36277) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCsJh-0006mN-Um for qemu-devel@nongnu.org; Tue, 05 Mar 2013 08:54:06 -0500 Received: from /spool/local by e23smtp01.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 5 Mar 2013 23:48:04 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp01.au.ibm.com (202.81.31.207) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 5 Mar 2013 23:47:53 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id 9017B2CE804A for ; Wed, 6 Mar 2013 00:53:51 +1100 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r25Drm7n2097424 for ; Wed, 6 Mar 2013 00:53:48 +1100 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r25Drpqr016913 for ; Wed, 6 Mar 2013 00:53:51 +1100 Received: from titi.austin.rr.com (sig-9-65-237-165.mts.ibm.com [9.65.237.165]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r25DrYFi016593; Wed, 6 Mar 2013 00:53:49 +1100 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Tue, 5 Mar 2013 07:53:29 -0600 Message-Id: <1362491612-19226-6-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1362491612-19226-1-git-send-email-aliguori@us.ibm.com> References: <1362491612-19226-1-git-send-email-aliguori@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13030513-1618-0000-0000-000003725657 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.143 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi Subject: [Qemu-devel] [RFC PATCH 5/8] fw_cfg: add qtest test harness X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org While it's limited to PC support right now, the abstraction is there to support other platforms. Signed-off-by: Anthony Liguori --- tests/Makefile | 7 +- tests/fw_cfg-test.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 tests/fw_cfg-test.c diff --git a/tests/Makefile b/tests/Makefile index 8eae1b4..7482297 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -67,6 +67,7 @@ check-qtest-i386-y += tests/hd-geo-test$(EXESUF) gcov-files-i386-y += hw/hd-geometry.c check-qtest-i386-y += tests/rtc-test$(EXESUF) check-qtest-i386-y += tests/i440fx-test$(EXESUF) +check-qtest-i386-y += tests/fw_cfg-test$(EXESUF) check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y)) @@ -129,12 +130,16 @@ tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $( tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a +libqos-obj-y = tests/libqos/pci.o +libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o + tests/rtc-test$(EXESUF): tests/rtc-test.o tests/m48t59-test$(EXESUF): tests/m48t59-test.o tests/fdc-test$(EXESUF): tests/fdc-test.o tests/hd-geo-test$(EXESUF): tests/hd-geo-test.o tests/tmp105-test$(EXESUF): tests/tmp105-test.o -tests/i440fx-test$(EXESUF): tests/i440fx-test.o tests/libqos/pci.o tests/libqos/pci-pc.o +tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y) +tests/fw_cfg-test$(EXESUF): tests/fw_cfg-test.o $(libqos-pc-obj-y) # QTest rules diff --git a/tests/fw_cfg-test.c b/tests/fw_cfg-test.c new file mode 100644 index 0000000..b784dc8 --- /dev/null +++ b/tests/fw_cfg-test.c @@ -0,0 +1,202 @@ +#define NO_QEMU_PROTOS + +#include "libqtest.h" +#include "hw/fw_cfg.h" + +#include +#include + +typedef struct QFWCFG QFWCFG; + +struct QFWCFG +{ + void (*select)(QFWCFG *fw_cfg, uint16_t key); + void (*read)(QFWCFG *fw_cfg, void *data, size_t len); +}; + +/* PC specific */ + +static void pc_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) +{ + outw(0x510, key); +} + +static void pc_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len) +{ + uint8_t *ptr = data; + int i; + + for (i = 0; i < len; i++) { + ptr[i] = inb(0x511); + } +} + +static QFWCFG *pc_fw_cfg_init(void) +{ + QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg)); + + fw_cfg->select = pc_fw_cfg_select; + fw_cfg->read = pc_fw_cfg_read; + + return fw_cfg; +} + +/* Generic code */ + +static void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key) +{ + fw_cfg->select(fw_cfg, key); +} + +static void qfw_cfg_read_data(QFWCFG *fw_cfg, void *data, size_t len) +{ + fw_cfg->read(fw_cfg, data, len); +} + +static void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void *data, size_t len) +{ + qfw_cfg_select(fw_cfg, key); + qfw_cfg_read_data(fw_cfg, data, len); +} + +static uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key) +{ + uint16_t value; + qfw_cfg_get(fw_cfg, key, &value, sizeof(value)); + return value; +} + +static uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key) +{ + uint32_t value; + qfw_cfg_get(fw_cfg, key, &value, sizeof(value)); + return value; +} + +static uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key) +{ + uint64_t value; + qfw_cfg_get(fw_cfg, key, &value, sizeof(value)); + return value; +} + +static uint64_t ram_size = 128 << 20; +static uint16_t nb_cpus = 1; +static uint16_t max_cpus = 1; +static uint64_t nb_nodes = 0; +static uint16_t boot_menu = 0; +static QFWCFG *fw_cfg = NULL; + +static void test_fw_cfg_signature(void) +{ + char buf[5]; + + qfw_cfg_get(fw_cfg, FW_CFG_SIGNATURE, buf, 4); + buf[4] = 0; + + g_assert_cmpstr(buf, ==, "QEMU"); +} + +static void test_fw_cfg_id(void) +{ + g_assert_cmpint(qfw_cfg_get_u32(fw_cfg, FW_CFG_ID), ==, 1); +} + +static void test_fw_cfg_uuid(void) +{ + uint8_t buf[16]; + static const uint8_t uuid[16] = { + 0x46, 0x00, 0xcb, 0x32, 0x38, 0xec, 0x4b, 0x2f, + 0x8a, 0xcb, 0x81, 0xc6, 0xea, 0x54, 0xf2, 0xd8, + }; + + qfw_cfg_get(fw_cfg, FW_CFG_UUID, buf, 16); + g_assert(memcmp(buf, uuid, sizeof(buf)) == 0); +} + +static void test_fw_cfg_ram_size(void) +{ + g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE), ==, ram_size); +} + +static void test_fw_cfg_nographic(void) +{ + g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NOGRAPHIC), ==, 0); +} + +static void test_fw_cfg_nb_cpus(void) +{ + g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NB_CPUS), ==, nb_cpus); +} + +static void test_fw_cfg_max_cpus(void) +{ + g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_MAX_CPUS), ==, max_cpus); +} + +static void test_fw_cfg_numa(void) +{ + uint64_t *cpu_mask; + uint64_t *node_mask; + + g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_NUMA), ==, nb_nodes); + + cpu_mask = g_malloc0(sizeof(uint64_t) * max_cpus); + node_mask = g_malloc0(sizeof(uint64_t) * nb_nodes); + + qfw_cfg_read_data(fw_cfg, cpu_mask, sizeof(uint64_t) * max_cpus); + qfw_cfg_read_data(fw_cfg, node_mask, sizeof(uint64_t) * nb_nodes); + + if (nb_nodes) { + g_assert(cpu_mask[0] & 0x01); + g_assert_cmpint(node_mask[0], ==, ram_size); + } + + g_free(node_mask); + g_free(cpu_mask); +} + +static void test_fw_cfg_boot_menu(void) +{ + g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_MENU), ==, boot_menu); +} + +int main(int argc, char **argv) +{ + QTestState *s = NULL; + char *cmdline; + int ret; + + g_test_init(&argc, &argv, NULL); + + fw_cfg = pc_fw_cfg_init(); + + g_test_add_func("/fw_cfg/signature", test_fw_cfg_signature); + g_test_add_func("/fw_cfg/id", test_fw_cfg_id); + g_test_add_func("/fw_cfg/uuid", test_fw_cfg_uuid); + g_test_add_func("/fw_cfg/ram_size", test_fw_cfg_ram_size); + g_test_add_func("/fw_cfg/nographic", test_fw_cfg_nographic); + g_test_add_func("/fw_cfg/nb_cpus", test_fw_cfg_nb_cpus); +#if 0 + g_test_add_func("/fw_cfg/machine_id", test_fw_cfg_machine_id); + g_test_add_func("/fw_cfg/kernel", test_fw_cfg_kernel); + g_test_add_func("/fw_cfg/initrd", test_fw_cfg_initrd); + g_test_add_func("/fw_cfg/boot_device", test_fw_cfg_boot_device); +#endif + g_test_add_func("/fw_cfg/max_cpus", test_fw_cfg_max_cpus); + g_test_add_func("/fw_cfg/numa", test_fw_cfg_numa); + g_test_add_func("/fw_cfg/boot_menu", test_fw_cfg_boot_menu); + + cmdline = g_strdup_printf("-display none " + "-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8 "); + qtest_start(cmdline); + g_free(cmdline); + + ret = g_test_run(); + + if (s) { + qtest_quit(s); + } + + return ret; +}