From patchwork Thu Aug 20 17:42:39 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 31770 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by bilbo.ozlabs.org (Postfix) with ESMTPS id 6F378B7B3E for ; Fri, 21 Aug 2009 04:23:23 +1000 (EST) Received: from localhost ([127.0.0.1]:33021 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeCIB-00050O-Vt for incoming@patchwork.ozlabs.org; Thu, 20 Aug 2009 14:23:20 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MeBhb-0006WS-4k for qemu-devel@nongnu.org; Thu, 20 Aug 2009 13:45:31 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MeBhV-0006Q9-BR for qemu-devel@nongnu.org; Thu, 20 Aug 2009 13:45:30 -0400 Received: from [199.232.76.173] (port=39162 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeBhU-0006Pk-Jg for qemu-devel@nongnu.org; Thu, 20 Aug 2009 13:45:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63099) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MeBhU-00070P-1f for qemu-devel@nongnu.org; Thu, 20 Aug 2009 13:45:24 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n7KHjM2R022068 for ; Thu, 20 Aug 2009 13:45:22 -0400 Received: from localhost.localdomain (vpn2-8-130.ams2.redhat.com [10.36.8.130]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n7KHis4g012471; Thu, 20 Aug 2009 13:45:21 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Thu, 20 Aug 2009 19:42:39 +0200 Message-Id: <20ca5615c8cdc456296698133e3b0dbd5a1f4de7.1250788880.git.quintela@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [Qemu-devel] [PATCH 21/23] Port PCIDevice state to VMState X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This uses a variant of buffer, with extra checks. Also uses the new support for cheking that a read value is less or equal than a field. Signed-off-by: Juan Quintela --- hw/hw.h | 12 +++++++++++ hw/pci.c | 65 +++++++++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/hw/hw.h b/hw/hw.h index c574b47..c77b1be 100644 --- a/hw/hw.h +++ b/hw/hw.h @@ -401,6 +401,18 @@ extern const VMStateInfo vmstate_info_buffer; + type_check_array(uint8_t,typeof_field(_state, _field),sizeof(typeof_field(_state,_field))) \ } +extern const VMStateDescription vmstate_pci_device; + +#define VMSTATE_PCI_DEVICE(_field, _state) { \ + .name = (stringify(_field)), \ + .version_id = 2, \ + .size = sizeof(PCIDevice), \ + .vmsd = &vmstate_pci_device, \ + .flags = VMS_STRUCT, \ + .offset = offsetof(_state, _field) \ + + type_check(PCIDevice,typeof_field(_state, _field)) \ +} + /* _f : field name _f_n : num of elements field_name _n : num of elements diff --git a/hw/pci.c b/hw/pci.c index 7644dee..2266e23 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -140,39 +140,60 @@ int pci_bus_num(PCIBus *s) return s->bus_num; } -void pci_device_save(PCIDevice *s, QEMUFile *f) -{ - int i; - - qemu_put_be32(f, s->version_id); /* PCI device version */ - qemu_put_buffer(f, s->config, 256); - for (i = 0; i < 4; i++) - qemu_put_be32(f, s->irq_state[i]); -} - -int pci_device_load(PCIDevice *s, QEMUFile *f) +static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) { - uint8_t config[PCI_CONFIG_SPACE_SIZE]; - uint32_t version_id; + PCIDevice *s = container_of(pv, PCIDevice, config); + uint8_t config[size]; int i; - version_id = qemu_get_be32(f); - if (version_id > 2) - return -EINVAL; - qemu_get_buffer(f, config, sizeof config); - for (i = 0; i < sizeof config; ++i) + qemu_get_buffer(f, config, size); + for (i = 0; i < size; ++i) if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) return -EINVAL; - memcpy(s->config, config, sizeof config); + memcpy(s->config, config, size); pci_update_mappings(s); - if (version_id >= 2) - for (i = 0; i < 4; i ++) - s->irq_state[i] = qemu_get_be32(f); return 0; } +/* just put buffer */ +static void put_pci_config_device(QEMUFile *f, const void *pv, size_t size) +{ + const uint8_t *v = pv; + qemu_put_buffer(f, v, size); +} + +static VMStateInfo vmstate_info_pci_config = { + .name = "pci config", + .get = get_pci_config_device, + .put = put_pci_config_device, +}; + +const VMStateDescription vmstate_pci_device = { + .name = "PCIDevice", + .version_id = 2, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField []) { + VMSTATE_INT32_LE(version_id, PCIDevice), + VMSTATE_SINGLE(config, PCIDevice, 0, vmstate_info_pci_config, + typeof_field(PCIDevice,config)), + VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, 4, 2), + VMSTATE_END_OF_LIST() + } +}; + +void pci_device_save(PCIDevice *s, QEMUFile *f) +{ + vmstate_save_state(f, &vmstate_pci_device, s); +} + +int pci_device_load(PCIDevice *s, QEMUFile *f) +{ + return vmstate_load_state(f, &vmstate_pci_device, s, s->version_id); +} + static int pci_set_default_subsystem_id(PCIDevice *pci_dev) { uint16_t *id;