add byteordered types and accessor functions to qemu.
PATCH 1/4 add byteordered types and accessor functions to qemu.
--- bswap.h | 29 +++++++++++ hw/usb-net.c | 158 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 109 insertions(+), 78 deletions(-)
bswap.h +29 -0
@@ -206,4 +206,33 @@ static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)#undef le_bswaps#undef be_bswaps +/* byteordered types and accessors */ + +typedef struct {uint16_t le; }le16 __attribute__((__aligned__ (2 ))); +typedef struct {uint32_t le; }le32 ; +typedef struct {uint64_t le; }le64 ; +typedef struct {uint16_t be; }be16 __attribute__((__aligned__ (2 ))); +typedef struct {uint32_t be; }be32 ; +typedef struct {uint64_t be; }be64 ; + +static inline uint16_t read_le16 (le16 le) {return le16_to_cpu (le.le); } +static inline uint32_t read_le32 (le32 le) {return le32_to_cpu (le.le); } +static inline uint64_t read_le64 (le64 le) {return le64_to_cpu (le.le); } +static inline uint16_t read_be16 (be16 be) {return be16_to_cpu (be.be); } +static inline uint32_t read_be32 (be32 be) {return be32_to_cpu (be.be); } +static inline uint64_t read_be64 (be64 be) {return be64_to_cpu (be.be); } + +static inline le16 write_le16 (uint16_t cpu) \ + {le16 ret = { .le =cpu_to_le16 (cpu) };return ret; } +static inline le32 write_le32 (uint32_t cpu) \ + {le32 ret = { .le =cpu_to_le32 (cpu) };return ret; } +static inline le64 write_le64 (uint64_t cpu) \ + {le64 ret = { .le =cpu_to_le64 (cpu) };return ret; } +static inline be16 write_be16 (uint16_t cpu) \ + {be16 ret = { .be =cpu_to_be16 (cpu) };return ret; } +static inline be32 write_be32 (uint32_t cpu) \ + {be32 ret = { .be =cpu_to_be32 (cpu) };return ret; } +static inline be64 write_be64 (uint64_t cpu) \ + {be64 ret = { .be =cpu_to_be64 (cpu) };return ret; } +#endif /* BSWAP_H */
hw/usb-net.c +80 -78
376 lines not on this page · raw message
Signature · 1 line
1.5.5.1
PATCH 2/4 pci: add config space struct (from qemu-xen).
--- hw/pci.h | 26 ++++++++++++++++++++++++++ 1 files changed, 26 insertions(+), 0 deletions(-)
hw/pci.h +26 -0
@@ -6,6 +6,32 @@/* PCI bus */ +struct pci_config_header { +le16 vendor_id; +le16 device_id; +le16 command; +le16 status; +uint8_t revision; +uint8_t api; +uint8_t subclass; +uint8_t class; +uint8_t cache_line_size;/* Units of 32 bit words */ +uint8_t latency_timer;/* In units of bus cycles */ +uint8_t header_type;/* Should be 0 */ +uint8_t bist;/* Built in self test */ +le32 base_address_regs[6 ]; +le32 reserved1; +le16 sub_vendor_id; +le16 sub_device_id; +le32 rom_addr; +le32 reserved3; +le32 reserved4; +uint8_t interrupt_line; +uint8_t interrupt_pin; +uint8_t min_gnt; +uint8_t max_lat; +}; +extern target_phys_addr_t pci_mem_base; typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
Signature · 1 line
1.5.5.1
PATCH 3/4 pci: add default pci subsystem id for all devices.
In reply to Gerd HoffmannThis sets a default PCI subsystem ID for all emulated PCI devices. PCI specs require this, so do it. The defaults are global variables so they can easily be changed (before device creation) as Xen probably wants to use the XenSource vendor ID instead of the qemu default.
The defaults are pre-filled by pci_register_device(). Individual drivers can overwrite it of course when setting up the config space for the emulated device.
TODO: get an official vendor ID assigned, or borrow one (maybe
Qumranet which already sponsors the virtio IDs ???).
--- hw/pci.c | 11 +++++++++++ hw/pci.h | 2 ++ 2 files changed, 13 insertions(+), 0 deletions(-)
hw/pci.c +11 -0
@@ -50,6 +50,8 @@ static void pci_update_mappings(PCIDevice *d);static void pci_set_irq (void *opaque,int irq_num,int level);target_phys_addr_t pci_mem_base; +uint16_t pci_default_sub_vendor_id =0xfffa ;/* FIXME: get one assigned */ +uint16_t pci_default_sub_device_id =0x0001 ;static int pci_irq_index;static PCIBus *first_bus;
@@ -145,6 +147,14 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)return 0 ; } +static int pci_set_default_subsystem_id (PCIDevice *pci_dev) +{ +struct pci_config_header *conf = (void *)pci_dev->config; + + conf->sub_vendor_id =write_le16 (pci_default_sub_vendor_id); + conf->sub_device_id =write_le16 (pci_default_sub_device_id); +} +/* -1 for devfn means auto assign */ PCIDevice *pci_register_device(PCIBus *bus, const char *name, int instance_size, int devfn,
@@ -171,6 +181,7 @@ PCIDevice *pci_register_device(PCIBus *bus, const char *name, pci_dev->devfn = devfn;pstrcpy (pci_dev->name,sizeof (pci_dev->name), name);memset (pci_dev->irq_state,0 ,sizeof (pci_dev->irq_state)); +pci_set_default_subsystem_id (pci_dev);if (!config_read) config_read = pci_default_read_config;
hw/pci.h +2 -0
@@ -33,6 +33,8 @@ struct pci_config_header { };extern target_phys_addr_t pci_mem_base; +extern uint16_t pci_default_sub_vendor_id; +extern uint16_t pci_default_sub_device_id;typedef void PCIConfigWriteFunc (PCIDevice *pci_dev,uint32_t address,uint32_t data,int len);
Signature · 1 line
1.5.5.1
PATCH 4/4 pci: use pci_config_header in pci.c
In reply to Gerd HoffmannThis makes use of the new pci_config_header struct in pci.c, squashing a bunch of casts and hard-coded magic numbers.
--- hw/pci.c | 64 +++++++++++++++++++++++++++++++------------------------------- 1 files changed, 32 insertions(+), 32 deletions(-)
hw/pci.c +32 -32
@@ -199,8 +199,9 @@ void pci_register_io_region(PCIDevice *pci_dev, int region_num, uint32_t size, int type, PCIMapIORegionFunc *map_func) { + struct pci_config_header *conf = (void*)pci_dev->config;PCIIORegion *r; -uint32_t addr; +le32 *addr;if ((unsigned int )region_num >=PCI_NUM_REGIONS )return ;
@@ -210,11 +211,11 @@ void pci_register_io_region(PCIDevice *pci_dev, int region_num, r->type = type; r->map_func = map_func;if (region_num ==PCI_ROM_SLOT ) { - addr =0x30 ; + addr = &conf->rom_addr; }else { - addr =0x10 + region_num *4 ; + addr = conf->base_address_regs + region_num; } - *(uint32_t *)(pci_dev->config + addr) =cpu_to_le32 (type); + *addr =write_le32 (type); }static target_phys_addr_t pci_to_cpu_addr (target_phys_addr_t addr)
@@ -224,23 +225,24 @@ static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)static void pci_update_mappings (PCIDevice *d) { +struct pci_config_header *conf = (void *)d->config;PCIIORegion *r;int cmd, i; -uint32_t last_addr, new_addr, config_ofs; +uint32_t last_addr, new_addr; +le32 *config_addr; - cmd =le16_to_cpu (*(uint16_t *)(d->config +PCI_COMMAND )); + cmd =read_le16 (conf->command);for (i =0 ; i <PCI_NUM_REGIONS ; i++) { r = &d->io_regions[i];if (i ==PCI_ROM_SLOT ) { - config_ofs =0x30 ; + config_addr = &conf->rom_addr; }else { - config_ofs =0x10 + i *4 ; + config_addr = conf->base_address_regs + i; }if (r->size !=0 ) {if (r->type &PCI_ADDRESS_SPACE_IO ) {if (cmd &PCI_COMMAND_IO ) { - new_addr =le32_to_cpu (*(uint32_t *)(d->config + - config_ofs)); + new_addr =read_le32 (*config_addr); new_addr = new_addr & ~(r->size -1 ); last_addr = new_addr + r->size -1 ;/* NOTE: we have only 64K ioports on PC */
@@ -253,8 +255,7 @@ static void pci_update_mappings(PCIDevice *d) } }else {if (cmd &PCI_COMMAND_MEMORY ) { - new_addr =le32_to_cpu (*(uint32_t *)(d->config + - config_ofs)); + new_addr =read_le32 (*config_addr);/* the ROM slot has a specific enable bit */ if (i ==PCI_ROM_SLOT && !(new_addr &1 )) goto no_mem_map;
@@ -568,13 +569,14 @@ static pci_class_desc pci_class_descriptions[] =static void pci_info_device (PCIDevice *d) { +struct pci_config_header *conf = (void *)d->config;int i, class;PCIIORegion *r;pci_class_desc *desc;term_printf (" Bus %2d, device %3d, function %d:\n" , d->bus->bus_num, d->devfn >>3 , d->devfn &7 ); - class =le16_to_cpu (*((uint16_t *)(d->config +PCI_CLASS_DEVICE ))); + class = conf->class <<8 | conf->subclass;term_printf (" " ); desc = pci_class_descriptions;while (desc->desc && class != desc->class)
@@ -585,11 +587,11 @@ static void pci_info_device(PCIDevice *d)term_printf ("Class %04x" , class); }term_printf (": PCI device %04x:%04x\n" , -le16_to_cpu (*((uint16_t *)(d->config +PCI_VENDOR_ID ))), -le16_to_cpu (*((uint16_t *)(d->config +PCI_DEVICE_ID )))); +read_le16 (conf->vendor_id), +read_le16 (conf->device_id)); -if (d->config[PCI_INTERRUPT_PIN ] !=0 ) { -term_printf (" IRQ %d.\n" , d->config[PCI_INTERRUPT_LINE ]); +if (conf->interrupt_line !=0 ) { +term_printf (" IRQ %d.\n" , conf->interrupt_line); }if (class ==0x0604 ) {term_printf (" BUS %d.\n" , d->config[0x19 ]);
@@ -686,25 +688,23 @@ static void pci_bridge_write_config(PCIDevice *d,PCIBus *pci_bridge_init (PCIBus *bus,int devfn,uint32_t id,pci_map_irq_fn map_irq,const char *name) { +struct pci_config_header *conf;PCIBridge *s; s = (PCIBridge *)pci_register_device (bus, name,sizeof (PCIBridge), devfn,NULL , pci_bridge_write_config); - s->dev.config[0x00 ] = id >>16 ; - s->dev.config[0x01 ] = id >>24 ; - s->dev.config[0x02 ] = id;// device_id - s->dev.config[0x03 ] = id >>8 ; - s->dev.config[0x04 ] =0x06 ;// command = bus master, pci mem - s->dev.config[0x05 ] =0x00 ; - s->dev.config[0x06 ] =0xa0 ;// status = fast back-to-back, 66MHz, no error - s->dev.config[0x07 ] =0x00 ;// status = fast devsel - s->dev.config[0x08 ] =0x00 ;// revision - s->dev.config[0x09 ] =0x00 ;// programming i/f - s->dev.config[0x0A ] =0x04 ;// class_sub = PCI to PCI bridge - s->dev.config[0x0B ] =0x06 ;// class_base = PCI_bridge - s->dev.config[0x0D ] =0x10 ;// latency_timer - s->dev.config[0x0E ] =0x81 ;// header_type - s->dev.config[0x1E ] =0xa0 ;// secondary status + conf = (void *)s->dev.config; + conf->vendor_id =write_le16 (id >>16 ); + conf->device_id =write_le16 (id &0xffff ); + conf->command =write_le16 (0x0006 );// bus master, pci mem + conf->status =write_le16 (0x00a0 );// fast back-to-back, 66MHz, no error, fast devsel + conf->revision =0x00 ; + conf->api =0x00 ; + conf->subclass =0x04 ;// PCI to PCI bridge + conf->class =0x06 ;// PCI_bridge + conf->latency_timer =0x10 ; + conf->header_type =0x81 ; + s->dev.config[0x1E ] =0xa0 ;// secondary status s->bus =pci_register_secondary_bus (&s->dev, map_irq);return s->bus; }
Signature · 1 line
1.5.5.1
In message: <[email protected]>
Gerd Hoffmann <[email protected]> writes:
: +typedef struct { uint16_t le; } le16 __attribute__((__aligned__(2))); : +typedef struct { uint32_t le; } le32;
: +typedef struct { uint64_t le; } le64;
: +typedef struct { uint16_t be; } be16 __attribute__((__aligned__(2))); : +typedef struct { uint32_t be; } be32;
: +typedef struct { uint64_t be; } be64;
This is likely still wrong.
There's no packing to ensure that these structures are the right size. There's no asserts to make sure that these structures really are the right size.
They are using raw GCCisms, which is both verbose and non-portable. This may not be a concern in qemu.
#define packed(x) __attribute__((__aligned__(x))) __attribute__((__packed__)) typedef struct { uint16_t le; } le16 packed(2);
typedef struct { uint32_t le; } le32 packed(4);
typedef struct { uint64_t le; } le64 packed(8);
typedef struct { uint16_t be; } be16 packed(2);
typedef struct { uint32_t be; } be32 packed(4);
typedef struct { uint64_t be; } be64 packed(8);
#undef packed
However, something just feels wrong with this approach. I'm sure there's other subtleties that will bite you with it...
Warner