SlideShare a Scribd company logo
QEMU AND DEVICE EMULATION
Yan Vugenfirer, yan@daynix.com
Daynix Computing LTD
Daynix Computing LTD
AGENDA
• Introduction to QEMU
• Development environment
• Examples of existing devices
• Adding a new device to
QEMU
Daynix Computing LTD
WHAT IS QEMU?
• Open source project (GPLv2.0+ license)
• Machine emulator
• Virtualizer
• Works together with KVM and Xen
Daynix Computing LTD
WHY SHOULDYOU CARE?
• Open source
• Easy to add additional devices
• Emulates different HW architectures
• Can be used for SW development long before
HW is ready
Daynix Computing LTD
GET IT NOW
• Project website: http://wiki.qemu.org/Main_Page
• Code repository: git clone git://git.qemu-
project.org/qemu.git
Daynix Computing LTD
PREPARING DEVELOPMENT
ENVIRONMENT
• Installing the packages on Ubuntu
• sudo apt-get update
• sudo apt-get install git
• sudo apt-get install build-essential
• sudo apt-get install pkg-config
• sudo apt-get install zlib zlib-dev
• sudo apt-get install zlib1g-dev zlib1g
• sudo apt-get install glib2.0
• sudo apt-get install libpixman-1-0
• sudo apt-get install libtool
• sudo apt-get install dh-autoreconf
• sudo apt-get install bridge-utils
Daynix Computing LTD
COMPILING QEMU
• git clone https://github.com/qemu/qemu.git
• cd qemu
• git submodule update --init pixman
• ./configure --disable-docs —target-list=x86_64-softmmu
• make -j 8
• Skip if development version only: make install
Daynix Computing LTD
NETWORK CONFIGURATION
Linux host
Virtual machine
QEMU process
NIC
Linux bridge
Physical NIC
Daynix Computing LTD
NETWORK CONFIGURATION
• On the host edit /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto eth0
#iface eth0 inet dhcp
iface eth0 inet manual
auto br0
iface br0 inet dhcp
bridge_ports eth0
Daynix Computing LTD
#!/bin/sh
switch=br0
/sbin/ifconfig $1 promisc 0.0.0.0
/sbin/brctl addif ${switch} $1
QEMU-IFUP SCRIPT
• Create qemu-ifup script with following content
Daynix Computing LTD
STORAGE
• Download Ubuntu server - http://
www.ubuntu.com/download/server
• CreateVM image
• qemu-img create -f qcow ubuntu.qcow 8G
Daynix Computing LTD
RUNNINGYOU FIRSTVIRTUAL
MACHINE
./qemu/x86_64-softmmu/qemu-system-x86_64 
-M pc -name Test_VM -smp 2 -m 512M 
-drive file=/home/qemu/images/ubuntu.qcow,if=ide 
-usbdevice tablet 
-boot order=cdn,once=c,menu=on 
-netdev tap,id=hostnet1,script=/home/qemu/dev/qemu-
ifup,ifname=testvm_nic 
-device e1000,netdev=hostnet1,mac=56:54:46:6b:64:22,bus=pci.
0,id=e1000_01 
-cdrom /ISO/ubuntu-14.04.1-server-amd64.iso 
-vnc :5
Daynix Computing LTD
CONNECTINGTOVM
• Use theVNC viewer of your choice
• ssh after you configured networking
Daynix Computing LTD
CODETREE
• cd qemu/HW
Daynix Computing LTD
QEMU DEVICE MODEL
• QDev device model abstraction
• Tree of devices connected by buses
• Represented by DeviceState and BusState
• Devices have common API
• Devices have properties
• Check include/hw/qdev-core.h for more info
Daynix Computing LTD
EXAMPLES OF EXISTING
DEVICES
• e1000
• qemu/hw/net/e1000.c
• virtio family -base code
• qemu/hw/virtio/virtio-pci.c, qemu/hw/virtio/virtio-rng.c
• virtio-net
• qemu/hw/net/virtio-net.c
• vmxnet3
• qemu/hw/net/vmxnet3.c
LET’S CODE!
Daynix Computing LTD
ADDING NEW DEVICE
• Basic source file for the device
• Enable the compilation of the device
• Command line options
• Step by step enhancement of the device
Daynix Computing LTD
ADDING NEW DEVICE
• In qemu/HW/net/ let’s create devix.c
• Add CONFIG_DEVIX_PCI option to default-configs/
pci.mak
• Add devix.o to hw/net/Makefile.objs
Daynix Computing LTD
COMPILATION
diff --git a/default-configs/pci.mak b/default-configs/
pci.mak
index 91b1e92..fcf2cf2 100644
--- a/default-configs/pci.mak
+++ b/default-configs/pci.mak
@@ -30,3 +30,4 @@ CONFIG_IPACK=y
CONFIG_WDT_IB6300ESB=y
CONFIG_PCI_TESTDEV=y
CONFIG_NVME_PCI=y
+CONFIG_DEVIX_PCI=y
Daynix Computing LTD
COMPILATION
diff --git a/hw/net/Makefile.objs b/hw/net/Makefile.objs
index ea93293..7800755 100644
--- a/hw/net/Makefile.objs
+++ b/hw/net/Makefile.objs
@@ -10,6 +10,7 @@ common-obj-$(CONFIG_E1000_PCI) += e1000.o
common-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o
common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet_tx_pkt.o
vmxnet_rx_pkt.o
common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet3.o
+common-obj-$(CONFIG_DEVIX_PCI) += devix.o
Daynix Computing LTD
REGISTERYOUR DEVICETYPE
static const TypeInfo devix_info = {
.name = TYPE_DEVIX,
.parent = TYPE_PCI_DEVICE,
.instance_size = sizeof(DEVIXState),
.class_init = devix_class_init,
.instance_init = devix_instance_init,
};
static void devix_register_types(void)
{
DVX_CBPRN("devix_register_types called...");
type_register_static(&devix_info);
}
type_init(devix_register_types)
Daynix Computing LTD
DEVICETYPE INITIALIZATION
static void devix_class_init(ObjectClass *class, void *data)
{
DeviceClass *dc = DEVICE_CLASS(class);
PCIDeviceClass *c = PCI_DEVICE_CLASS(class);
c->init = devix_pci_init;
c->exit = devix_pci_uninit;
c->vendor_id = PCI_VENDOR_ID_DAYNIX;
c->device_id = PCI_DEVICE_ID_DAYNIX_DEVIX;
c->revision = PCI_DEVICE_ID_DAYNIX_DEVIX_REVISION;
c->class_id = PCI_CLASS_NETWORK_ETHERNET;
c->subsystem_vendor_id = PCI_VENDOR_ID_DAYNIX;
c->subsystem_id = PCI_DEVICE_ID_DAYNIX_DEVIX;
c->config_write = devix_write_config;
c->config_read = devix_read_config;
dc->desc = "Daynix educational device v1";
dc->reset = devix_qdev_reset;
dc->vmsd = &vmstate_devix;
dc->props = devix_properties;
set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
}
Daynix Computing LTD
PCI CONFIGURATION SPACE
ACCESS CALLBACKS
static void uint32_t devix_read_config(PCIDevice *pci_dev,
uint32_t address, int len)
{
return pci_default_read_config(pci_dev, address, len);
}
static void
devix_write_config(PCIDevice *pci_dev, uint32_t address, uint32_t val,
int len)
{
pci_default_write_config(pci_dev, address, val, len);
}
• At this point we have PCI device
• No BARs or Interrupts… yet
Daynix Computing LTD
VM POINT OFVIEW
00:04.0 Ethernet controller: Device 9696:1234
(rev 01)
Subsystem: Device 9696:1234
Physical Slot: 4
Control: I/O+ Mem+ BusMaster- SpecCycle-
MemWINV- VGASnoop- ParErr- Stepping- SERR+
FastB2B- DisINTx-
Status: Cap- 66MHz- UDF- FastB2B- ParErr-
DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR-
<PERR- INTx-
• sudo lspci -vv
Daynix Computing LTD
• Register IO and memory space
• Configure interrupts
• Our own device initializations
PCI INITIALIZATION
Daynix Computing LTD
MEMORY REGIONS
typedef struct {
PCIDevice parent_obj;
MemoryRegion bar0;
MemoryRegion bar1;
} DEVIXState;
• Add placeholders for memory regions into device
state structure
Daynix Computing LTD
REGISTER MEMORY REGIONS
static int devix_pci_init(PCIDevice *pci_dev)
{
DeviceState *dev = DEVICE(pci_dev);
DEVIXState *s = DEVIX(pci_dev);
DVX_CBPRN("Starting init...");
memory_region_init_io(&s->bar0, OBJECT(s), &b0_ops, s,
"devix-b0", DEVIX_BAR0_SIZE);
pci_register_bar(pci_dev, DEVIX_BAR0_IDX,
PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
memory_region_init_io(&s->bar1, OBJECT(s), &b1_ops, s,
"devix-b1", DEVIX_BAR1_SIZE);
pci_register_bar(pci_dev, DEVIX_BAR1_IDX,
PCI_BASE_ADDRESS_SPACE_IO, &s->bar1);
devix_net_init(s);
return 0;
}
Daynix Computing LTD
ACCESS CALLBACKS
static const MemoryRegionOps b0_ops = {
.read = devix_io_bar0_read,
.write = devix_io_bar0_write,
.endianness = DEVICE_LITTLE_ENDIAN,
.impl = {
.min_access_size = 4,
.max_access_size = 4,
},
};
Daynix Computing LTD
ACCESS CALLBACKS - WRITE
static void
devix_io_bar0_write(void *opaque, hwaddr addr,
uint64_t val, unsigned size)
{
DEVIXState *s = opaque;
DVX_WRPRN("BAR0 write [%" PRIx64 "] = %"
PRIx64 ", size %d",
(uint64_t) addr, val, size);
}
• Size
• Address
• Value
Daynix Computing LTD
ACCESS CALLBACKS - READ
static uint64_t
devix_io_bar0_read(void *opaque, hwaddr addr,
unsigned size)
{
DEVIXState *s = opaque;
uint64_t ret = 0;
DVX_CBPRN("Read BAR0 [%" PRIx64 "], size
%d",(uint64_t) addr, size);
return ret; /* Returns value of the read
register */
}
• Size
• Address
• Return the
value
Daynix Computing LTD
VM POINT OFVIEW
00:04.0 Ethernet controller: Device 9696:1234
(rev 01)
Subsystem: Device 9696:1234
Physical Slot: 4
Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV-
VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx-
Status: Cap- 66MHz- UDF- FastB2B- ParErr-
DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR-
INTx-
Region 0: Memory at febd2000 (32-bit, non-
prefetchable) [size=4K]
Region 1: I/O ports at c000 [size=512]
• sudo lspci -vv
Daynix Computing LTD
LEGACY INTERRUPTS
• Add interrupt line in pci_init
/* Interrupt pin A */
pci_dev->config[PCI_INTERRUPT_PIN] = 0x01;
Daynix Computing LTD
LEGACY INTERRUPTS
• Call to void pci_irq_assert(PCIDevice *pci_dev)
when you want to raise interrupt
• Call void pci_irq_deassert(PCIDevice *pci_dev) to
de-assert interrupt from one of your registers
callback depending on clear interrupt logic
Daynix Computing LTD
VM POINT OFVIEW
00:04.0 Ethernet controller: Device 9696:1234
(rev 01)
Subsystem: Device 9696:1234
Physical Slot: 4
Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV-
VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx-
Status: Cap- 66MHz- UDF- FastB2B- ParErr-
DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR-
INTx-
Interrupt: pin A routed to IRQ 11
Region 0: Memory at febd2000 (32-bit, non-
prefetchable) [size=4K]
Region 1: I/O ports at c000 [size=512]
• sudo lspci -vv
Daynix Computing LTD
“DMA”
• Actually guest memory access
• Synchronous
• void cpu_physical_memory_read(hwaddr addr, void *buf, int len)
• void cpu_physical_memory_write(hwaddr addr, const void *buf, int len)
• iov_xxx functions
• Check include/exec/cpu-common.h for more access functions
DEMO
Daynix Computing LTD
WHAT’S NEXT?
• Add network back end
• Add MSI and MSI-x support
• Littlebig endian
considerations
Q&A
Daynix Computing LTD
LINKS
• Project website: http://wiki.qemu.org/Main_Page
• Code repository: git clone git://git.qemu-project.org/
qemu.git
• QEMU new device model - http://www.linux-kvm.org/
wiki/images/f/fe/2010-forum-armbru-qdev.pdf
• Daynix - www.daynix.com

More Related Content

PPTX
QEMU - Binary Translation
PPTX
Linux Network Stack
PDF
VMworld 2013: ESXi Native Networking Driver Model - Delivering on Simplicity ...
PPTX
Linux Initialization Process (1)
PDF
initramfsについて
PDF
How A Compiler Works: GNU Toolchain
PDF
Let's trace Linux Lernel with KGDB @ COSCUP 2021
PDF
Qemu JIT Code Generator and System Emulation
QEMU - Binary Translation
Linux Network Stack
VMworld 2013: ESXi Native Networking Driver Model - Delivering on Simplicity ...
Linux Initialization Process (1)
initramfsについて
How A Compiler Works: GNU Toolchain
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Qemu JIT Code Generator and System Emulation

What's hot (20)

PDF
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
PDF
BPF: Tracing and more
PDF
Kdump and the kernel crash dump analysis
PPT
U boot porting guide for SoC
PDF
U-Boot - An universal bootloader
PPTX
Linux Kernel Booting Process (1) - For NLKB
PDF
YOW2020 Linux Systems Performance
ODP
eBPF maps 101
PDF
Linux kernel debugging
PPTX
Memory model
PDF
from Source to Binary: How GNU Toolchain Works
PDF
Interpreter, Compiler, JIT from scratch
PDF
Qemu Introduction
PDF
LinuxCon 2015 Linux Kernel Networking Walkthrough
PDF
用十分鐘 向jserv學習作業系統設計
PDF
What Can Compilers Do for Us?
PDF
Linux Linux Traffic Control
PPTX
Static partitioning virtualization on RISC-V
PDF
I/O仮想化最前線〜ネットワークI/Oを中心に〜
PDF
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
BPF: Tracing and more
Kdump and the kernel crash dump analysis
U boot porting guide for SoC
U-Boot - An universal bootloader
Linux Kernel Booting Process (1) - For NLKB
YOW2020 Linux Systems Performance
eBPF maps 101
Linux kernel debugging
Memory model
from Source to Binary: How GNU Toolchain Works
Interpreter, Compiler, JIT from scratch
Qemu Introduction
LinuxCon 2015 Linux Kernel Networking Walkthrough
用十分鐘 向jserv學習作業系統設計
What Can Compilers Do for Us?
Linux Linux Traffic Control
Static partitioning virtualization on RISC-V
I/O仮想化最前線〜ネットワークI/Oを中心に〜
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
Ad

Similar to Qemu device prototyping (20)

PDF
Buiding a better Userspace - The current and future state of QEMU and KVM int...
PDF
XS Boston 2008 VT-D PCI
PDF
PDF
PDF
Project ACRN Device Model architecture introduction
PDF
Development platform virtualization using qemu
PDF
bhyve Device Emulation Introduction
ODP
Kvm and libvirt
PDF
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
PDF
XS Boston 2008 Self IO Emulation
ODP
Physical Computing with Linux
PPTX
Lect 1_Embedded Linux Embedded RTOS ppt
PDF
Status update-qemu-pcie
PPTX
Xen Project Update LinuxCon Brazil
PDF
Embedded Android : System Development - Part II (Linux device drivers)
PDF
Project ACRN Device Passthrough Introduction
PDF
2015.10.05 Updated > Network Device Development - Part 1: Switch
PPT
Basic Linux Internals
PDF
SR-IOV+KVM on Debian/Stable
PDF
Rmll Virtualization As Is Tool 20090707 V1.0
Buiding a better Userspace - The current and future state of QEMU and KVM int...
XS Boston 2008 VT-D PCI
Project ACRN Device Model architecture introduction
Development platform virtualization using qemu
bhyve Device Emulation Introduction
Kvm and libvirt
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
XS Boston 2008 Self IO Emulation
Physical Computing with Linux
Lect 1_Embedded Linux Embedded RTOS ppt
Status update-qemu-pcie
Xen Project Update LinuxCon Brazil
Embedded Android : System Development - Part II (Linux device drivers)
Project ACRN Device Passthrough Introduction
2015.10.05 Updated > Network Device Development - Part 1: Switch
Basic Linux Internals
SR-IOV+KVM on Debian/Stable
Rmll Virtualization As Is Tool 20090707 V1.0
Ad

More from Yan Vugenfirer (14)

PDF
HCK-CI: Enabling CI for Windows Guest Paravirtualized Drivers - Kostiantyn Ko...
PDF
Receive side scaling (RSS) with eBPF in QEMU and virtio-net
PDF
Implementing SR-IOv failover for Windows guests during live migration
PDF
Windows network teaming
PDF
Rebuild presentation - IoT Israel MeetUp
PDF
Rebuild presentation during Docker's Birthday party
PDF
Contributing to open source using Git
PDF
Introduction to Git
PDF
Microsoft Hardware Certification Kit (HCK) setup
PDF
UsbDk at a Glance 
PDF
Building “old” Windows drivers (XP, Vista, 2003 and 2008) with Visual Studio ...
PPTX
Advanced NDISTest options
PDF
QEMU Development and Testing Automation Using MS HCK - Anton Nayshtut and Yan...
PDF
Windows guest debugging presentation from KVM Forum 2012
HCK-CI: Enabling CI for Windows Guest Paravirtualized Drivers - Kostiantyn Ko...
Receive side scaling (RSS) with eBPF in QEMU and virtio-net
Implementing SR-IOv failover for Windows guests during live migration
Windows network teaming
Rebuild presentation - IoT Israel MeetUp
Rebuild presentation during Docker's Birthday party
Contributing to open source using Git
Introduction to Git
Microsoft Hardware Certification Kit (HCK) setup
UsbDk at a Glance 
Building “old” Windows drivers (XP, Vista, 2003 and 2008) with Visual Studio ...
Advanced NDISTest options
QEMU Development and Testing Automation Using MS HCK - Anton Nayshtut and Yan...
Windows guest debugging presentation from KVM Forum 2012

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Introduction to Artificial Intelligence
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
AI in Product Development-omnex systems
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ai tools demonstartion for schools and inter college
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
medical staffing services at VALiNTRY
PPT
Introduction Database Management System for Course Database
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Introduction to Artificial Intelligence
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Design an Analysis of Algorithms I-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
VVF-Customer-Presentation2025-Ver1.9.pptx
AI in Product Development-omnex systems
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
ISO 45001 Occupational Health and Safety Management System
ai tools demonstartion for schools and inter college
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
medical staffing services at VALiNTRY
Introduction Database Management System for Course Database
Odoo Companies in India – Driving Business Transformation.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Operating system designcfffgfgggggggvggggggggg

Qemu device prototyping

  • 1. QEMU AND DEVICE EMULATION Yan Vugenfirer, yan@daynix.com Daynix Computing LTD
  • 2. Daynix Computing LTD AGENDA • Introduction to QEMU • Development environment • Examples of existing devices • Adding a new device to QEMU
  • 3. Daynix Computing LTD WHAT IS QEMU? • Open source project (GPLv2.0+ license) • Machine emulator • Virtualizer • Works together with KVM and Xen
  • 4. Daynix Computing LTD WHY SHOULDYOU CARE? • Open source • Easy to add additional devices • Emulates different HW architectures • Can be used for SW development long before HW is ready
  • 5. Daynix Computing LTD GET IT NOW • Project website: http://wiki.qemu.org/Main_Page • Code repository: git clone git://git.qemu- project.org/qemu.git
  • 6. Daynix Computing LTD PREPARING DEVELOPMENT ENVIRONMENT • Installing the packages on Ubuntu • sudo apt-get update • sudo apt-get install git • sudo apt-get install build-essential • sudo apt-get install pkg-config • sudo apt-get install zlib zlib-dev • sudo apt-get install zlib1g-dev zlib1g • sudo apt-get install glib2.0 • sudo apt-get install libpixman-1-0 • sudo apt-get install libtool • sudo apt-get install dh-autoreconf • sudo apt-get install bridge-utils
  • 7. Daynix Computing LTD COMPILING QEMU • git clone https://github.com/qemu/qemu.git • cd qemu • git submodule update --init pixman • ./configure --disable-docs —target-list=x86_64-softmmu • make -j 8 • Skip if development version only: make install
  • 8. Daynix Computing LTD NETWORK CONFIGURATION Linux host Virtual machine QEMU process NIC Linux bridge Physical NIC
  • 9. Daynix Computing LTD NETWORK CONFIGURATION • On the host edit /etc/network/interfaces # The loopback network interface auto lo iface lo inet loopback # The primary network interface #auto eth0 #iface eth0 inet dhcp iface eth0 inet manual auto br0 iface br0 inet dhcp bridge_ports eth0
  • 10. Daynix Computing LTD #!/bin/sh switch=br0 /sbin/ifconfig $1 promisc 0.0.0.0 /sbin/brctl addif ${switch} $1 QEMU-IFUP SCRIPT • Create qemu-ifup script with following content
  • 11. Daynix Computing LTD STORAGE • Download Ubuntu server - http:// www.ubuntu.com/download/server • CreateVM image • qemu-img create -f qcow ubuntu.qcow 8G
  • 12. Daynix Computing LTD RUNNINGYOU FIRSTVIRTUAL MACHINE ./qemu/x86_64-softmmu/qemu-system-x86_64 -M pc -name Test_VM -smp 2 -m 512M -drive file=/home/qemu/images/ubuntu.qcow,if=ide -usbdevice tablet -boot order=cdn,once=c,menu=on -netdev tap,id=hostnet1,script=/home/qemu/dev/qemu- ifup,ifname=testvm_nic -device e1000,netdev=hostnet1,mac=56:54:46:6b:64:22,bus=pci. 0,id=e1000_01 -cdrom /ISO/ubuntu-14.04.1-server-amd64.iso -vnc :5
  • 13. Daynix Computing LTD CONNECTINGTOVM • Use theVNC viewer of your choice • ssh after you configured networking
  • 15. Daynix Computing LTD QEMU DEVICE MODEL • QDev device model abstraction • Tree of devices connected by buses • Represented by DeviceState and BusState • Devices have common API • Devices have properties • Check include/hw/qdev-core.h for more info
  • 16. Daynix Computing LTD EXAMPLES OF EXISTING DEVICES • e1000 • qemu/hw/net/e1000.c • virtio family -base code • qemu/hw/virtio/virtio-pci.c, qemu/hw/virtio/virtio-rng.c • virtio-net • qemu/hw/net/virtio-net.c • vmxnet3 • qemu/hw/net/vmxnet3.c
  • 18. Daynix Computing LTD ADDING NEW DEVICE • Basic source file for the device • Enable the compilation of the device • Command line options • Step by step enhancement of the device
  • 19. Daynix Computing LTD ADDING NEW DEVICE • In qemu/HW/net/ let’s create devix.c • Add CONFIG_DEVIX_PCI option to default-configs/ pci.mak • Add devix.o to hw/net/Makefile.objs
  • 20. Daynix Computing LTD COMPILATION diff --git a/default-configs/pci.mak b/default-configs/ pci.mak index 91b1e92..fcf2cf2 100644 --- a/default-configs/pci.mak +++ b/default-configs/pci.mak @@ -30,3 +30,4 @@ CONFIG_IPACK=y CONFIG_WDT_IB6300ESB=y CONFIG_PCI_TESTDEV=y CONFIG_NVME_PCI=y +CONFIG_DEVIX_PCI=y
  • 21. Daynix Computing LTD COMPILATION diff --git a/hw/net/Makefile.objs b/hw/net/Makefile.objs index ea93293..7800755 100644 --- a/hw/net/Makefile.objs +++ b/hw/net/Makefile.objs @@ -10,6 +10,7 @@ common-obj-$(CONFIG_E1000_PCI) += e1000.o common-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet_tx_pkt.o vmxnet_rx_pkt.o common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet3.o +common-obj-$(CONFIG_DEVIX_PCI) += devix.o
  • 22. Daynix Computing LTD REGISTERYOUR DEVICETYPE static const TypeInfo devix_info = { .name = TYPE_DEVIX, .parent = TYPE_PCI_DEVICE, .instance_size = sizeof(DEVIXState), .class_init = devix_class_init, .instance_init = devix_instance_init, }; static void devix_register_types(void) { DVX_CBPRN("devix_register_types called..."); type_register_static(&devix_info); } type_init(devix_register_types)
  • 23. Daynix Computing LTD DEVICETYPE INITIALIZATION static void devix_class_init(ObjectClass *class, void *data) { DeviceClass *dc = DEVICE_CLASS(class); PCIDeviceClass *c = PCI_DEVICE_CLASS(class); c->init = devix_pci_init; c->exit = devix_pci_uninit; c->vendor_id = PCI_VENDOR_ID_DAYNIX; c->device_id = PCI_DEVICE_ID_DAYNIX_DEVIX; c->revision = PCI_DEVICE_ID_DAYNIX_DEVIX_REVISION; c->class_id = PCI_CLASS_NETWORK_ETHERNET; c->subsystem_vendor_id = PCI_VENDOR_ID_DAYNIX; c->subsystem_id = PCI_DEVICE_ID_DAYNIX_DEVIX; c->config_write = devix_write_config; c->config_read = devix_read_config; dc->desc = "Daynix educational device v1"; dc->reset = devix_qdev_reset; dc->vmsd = &vmstate_devix; dc->props = devix_properties; set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); }
  • 24. Daynix Computing LTD PCI CONFIGURATION SPACE ACCESS CALLBACKS static void uint32_t devix_read_config(PCIDevice *pci_dev, uint32_t address, int len) { return pci_default_read_config(pci_dev, address, len); } static void devix_write_config(PCIDevice *pci_dev, uint32_t address, uint32_t val, int len) { pci_default_write_config(pci_dev, address, val, len); }
  • 25. • At this point we have PCI device • No BARs or Interrupts… yet
  • 26. Daynix Computing LTD VM POINT OFVIEW 00:04.0 Ethernet controller: Device 9696:1234 (rev 01) Subsystem: Device 9696:1234 Physical Slot: 4 Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- • sudo lspci -vv
  • 27. Daynix Computing LTD • Register IO and memory space • Configure interrupts • Our own device initializations PCI INITIALIZATION
  • 28. Daynix Computing LTD MEMORY REGIONS typedef struct { PCIDevice parent_obj; MemoryRegion bar0; MemoryRegion bar1; } DEVIXState; • Add placeholders for memory regions into device state structure
  • 29. Daynix Computing LTD REGISTER MEMORY REGIONS static int devix_pci_init(PCIDevice *pci_dev) { DeviceState *dev = DEVICE(pci_dev); DEVIXState *s = DEVIX(pci_dev); DVX_CBPRN("Starting init..."); memory_region_init_io(&s->bar0, OBJECT(s), &b0_ops, s, "devix-b0", DEVIX_BAR0_SIZE); pci_register_bar(pci_dev, DEVIX_BAR0_IDX, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0); memory_region_init_io(&s->bar1, OBJECT(s), &b1_ops, s, "devix-b1", DEVIX_BAR1_SIZE); pci_register_bar(pci_dev, DEVIX_BAR1_IDX, PCI_BASE_ADDRESS_SPACE_IO, &s->bar1); devix_net_init(s); return 0; }
  • 30. Daynix Computing LTD ACCESS CALLBACKS static const MemoryRegionOps b0_ops = { .read = devix_io_bar0_read, .write = devix_io_bar0_write, .endianness = DEVICE_LITTLE_ENDIAN, .impl = { .min_access_size = 4, .max_access_size = 4, }, };
  • 31. Daynix Computing LTD ACCESS CALLBACKS - WRITE static void devix_io_bar0_write(void *opaque, hwaddr addr, uint64_t val, unsigned size) { DEVIXState *s = opaque; DVX_WRPRN("BAR0 write [%" PRIx64 "] = %" PRIx64 ", size %d", (uint64_t) addr, val, size); } • Size • Address • Value
  • 32. Daynix Computing LTD ACCESS CALLBACKS - READ static uint64_t devix_io_bar0_read(void *opaque, hwaddr addr, unsigned size) { DEVIXState *s = opaque; uint64_t ret = 0; DVX_CBPRN("Read BAR0 [%" PRIx64 "], size %d",(uint64_t) addr, size); return ret; /* Returns value of the read register */ } • Size • Address • Return the value
  • 33. Daynix Computing LTD VM POINT OFVIEW 00:04.0 Ethernet controller: Device 9696:1234 (rev 01) Subsystem: Device 9696:1234 Physical Slot: 4 Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Region 0: Memory at febd2000 (32-bit, non- prefetchable) [size=4K] Region 1: I/O ports at c000 [size=512] • sudo lspci -vv
  • 34. Daynix Computing LTD LEGACY INTERRUPTS • Add interrupt line in pci_init /* Interrupt pin A */ pci_dev->config[PCI_INTERRUPT_PIN] = 0x01;
  • 35. Daynix Computing LTD LEGACY INTERRUPTS • Call to void pci_irq_assert(PCIDevice *pci_dev) when you want to raise interrupt • Call void pci_irq_deassert(PCIDevice *pci_dev) to de-assert interrupt from one of your registers callback depending on clear interrupt logic
  • 36. Daynix Computing LTD VM POINT OFVIEW 00:04.0 Ethernet controller: Device 9696:1234 (rev 01) Subsystem: Device 9696:1234 Physical Slot: 4 Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Interrupt: pin A routed to IRQ 11 Region 0: Memory at febd2000 (32-bit, non- prefetchable) [size=4K] Region 1: I/O ports at c000 [size=512] • sudo lspci -vv
  • 37. Daynix Computing LTD “DMA” • Actually guest memory access • Synchronous • void cpu_physical_memory_read(hwaddr addr, void *buf, int len) • void cpu_physical_memory_write(hwaddr addr, const void *buf, int len) • iov_xxx functions • Check include/exec/cpu-common.h for more access functions
  • 38. DEMO
  • 39. Daynix Computing LTD WHAT’S NEXT? • Add network back end • Add MSI and MSI-x support • Littlebig endian considerations
  • 40. Q&A
  • 41. Daynix Computing LTD LINKS • Project website: http://wiki.qemu.org/Main_Page • Code repository: git clone git://git.qemu-project.org/ qemu.git • QEMU new device model - http://www.linux-kvm.org/ wiki/images/f/fe/2010-forum-armbru-qdev.pdf • Daynix - www.daynix.com