Skip to contentSkip to conversation

Need some help in understanding qemu infrastructure and execution flow

Qemu-devel Ronex Dicapriyo 2 participants 5 messages Dec 22, 2014

lore
Share & tools
lore
#0 lore raw

Hello,
I am interested in learning the qemu emulation technology, But I don't know where to start. I have some knowledge/experience in device modeling.
While walking through the qemu.org, I couldn't found proper documentation or guide to start working with QEMU.
So, I am seeking some helps from the user's or developer's of QEMU. My aim is to start step by step learning with :
1) the basic execution flow of QEMU simulation engine2) Notion of time, events in QEMU3) Multithreading or multi-processing4) ARM cortex-A series CPU implementation in QEMU
5) Emulate or boot a linux on any ARM based platform
6) Debugging in QEMU

Kindly direct me on proper path, and help me in learning QEMU. Kindly suggest any links and books which can be used for comprehension.

While checking some code, I found the use of VMSTATE_UINTx kind of macros, Can anyone please suggest what is the purpose of VMSTATE_xxx_xxx in QEMU ? Regards,
Ronex

#1 lore raw

Hello,

I am interested in learning the qemu emulation technology, But I don't know where to start. I have some knowledge/experience in device modeling.
While walking through the qemu.org, I couldn't found proper documentation or guide to start working with QEMU.
So, I am seeking some helps from the user's or developer's of QEMU. My aim is to start step by step learning with :
1) the basic execution flow of QEMU simulation engine2) Notion of time, events in QEMU3) Multithreading or multi-processing4) ARM cortex-A series CPU implementation in QEMU
5) Emulate or boot a linux on any ARM based platform
6) Debugging in QEMU

Kindly direct me on proper path, and help me in learning QEMU. Kindly suggest any links and books which can be used for comprehension.

While checking some hardware device source code, I found the use of VMSTATE_UINTx kind of macros, Can anyone please suggest what is the purpose of VMSTATE_xxx_xxx in QEMU ?

 Regards,
Ronex

#2 lore raw
Quoted 10 lines · Ronex Dicapriyo

I am interested in learning the qemu emulation technology, But I don't know where to start. I have some knowledge/experience in device modeling.

While walking through the qemu.org, I couldn't found proper documentation or guide to start working with QEMU.
So, I am seeking some helps from the user's or developer's of QEMU. My aim is to start step by step learning with :

1) the basic execution flow of QEMU simulation engine
2) Notion of time, events in QEMU

We don't have quite the same kind of view of the world that a more strictly timed simulation would. Mostly QEMU aims for "execute correct code as fast as possible". We don't have any kind of cycle accuracy, for instance. Generally we just execute code, and events such as timer interrupts happen at the intervals that the host clock says they should (ie a 50Hz interrupt will tick every 1/50th of a second).

Quoted 1 line

3) Multithreading or multi-processing

Currently we implement SMP guest configurations by simply doing a round-robin execution of each CPU on a single host thread. (There is work planned to change this so we can make use of multiple host threads.)

Quoted 1 line

4) ARM cortex-A series CPU implementation in QEMU

The front-end lives in target-arm/. The usual layout for a front-end is that the decoder (reads guest-cpu instructions and writes QEMU TCG intermediate representation ops) is in translate*.c, and helper functions that get called at runtime (either directly from translated code or from the QEMU core) are in *helper*.c.

Quoted 1 line

5) Emulate or boot a linux on any ARM based platform

There are a pile of tutorials out on the web for this. I'd use the "virt" board if I were you, it's relatively simple and supports virtio.

Quoted 1 line

6) Debugging in QEMU

Debugging of QEMU, or of the guest?

Quoted 2 lines

Kindly direct me on proper path, and help me in learning QEMU. Kindly suggest any links and books which can be used for comprehension.

In general we don't have much documentation of QEMU's internals. The source code is always your best resource for figuring out exactly what we do.

Quoted 3 lines

While checking some hardware device source code, I found the use of VMSTATE_UINTx kind of macros, Can anyone please suggest what is the purpose of VMSTATE_xxx_xxx in QEMU ?

They describe the state of the device so we can serialise it for live migration and VM snapshot save/restore. This one actually is documented -- see docs/migration.txt.

thanks
-- PMM

#3 lore raw

 Thanks for the explanations, I have some more doubts on the same, Kindly clarify.

Quoted 2 lines · Peter Maydell

1) the basic execution flow of QEMU simulation engine
2) Notion of time, events in QEMU

We don't have quite the same kind of view of the world that a more strictly timed simulation would. Mostly QEMU aims for "execute correct code as fast as possible". We don't have any kind of cycle accuracy, for instance. Generally we just execute code, and events such as timer interrupts happen at the intervals that the host clock says they should (ie a 50Hz interrupt will tick every 1/50th of a second).

[Ronex] Ok, As I can understand, QEMU is functional accurate, rather than cycle for fast simulation. But there must be some kind of reference clock, like say for example any timer device which raises an interrupt after couple of cycles (i.e after each 50ns it increments it timer counter register), How it can be implemented in QEMU, I could not understand what you are referring by host clock, are you pointing at the real-time clock of the host system where any qemu binary or executable runs (it sounds quite wierd to me ) ?

Quoted 1 line

3) Multithreading or multi-processing

Currently we implement SMP guest configurations by simply doing a round-robin execution of each CPU on a single host thread. (There is work planned to change this so we can make use of multiple host threads.)

Suppose I am directly accessing some X address of memory, which falls into the memory of device A, Now, if transaction doesn't involves CPU (may be using DMA), then how it calls the read/write methods associated with any device (for updating a device's register and carry out some specific functionality) ? Can you please suggest any memory management unit which is responsible for this ?

Quoted 1 line

6) Debugging in QEMU

Debugging of QEMU, or of the guest?

How to debug any device code of QEMU, as well as some application running on Guest ?

Quoted 2 lines

Kindly direct me on proper path, and help me in learning QEMU. Kindly suggest any links and books which can be used for comprehension.

In general we don't have much documentation of QEMU's
internals. The source code is always your best resource
for figuring out exactly what we do.
I understand this, Thanks for all the suggestions, I was looking some for documentation to avoid any wrong interpretations.

Quoted 3 lines

While checking some hardware device source code, I found the use of VMSTATE_UINTx kind of macros, Can anyone please suggest what is the purpose of VMSTATE_xxx_xxx in QEMU ?

They describe the state of the device so we can serialise it for live migration and VM snapshot save/restore. This one actually is documented -- see docs/migration.txt.

thanks
-- PMM

#4 lore raw
Quoted 7 lines · Ronex Dicapriyo

[Ronex] Ok, As I can understand, QEMU is functional accurate, rather than cycle for fast simulation. But there must be some kind of reference clock, like say for example any timer device which raises an interrupt after couple of cycles (i.e after each 50ns it increments it timer counter register), How it can be implemented in QEMU, I could not understand what you are referring by host clock, are you pointing at the real-time clock of the host system where any qemu binary or executable runs (it sounds quite wierd to me ) ?

See include/qemu/timer.h for the descriptions of our various clocks. Typically a timer will use QEMU_CLOCK_VIRTUAL, which boils down to a call to get_clock(), which is just a wrapper around clock_gettime(CLOCK_MONOTONIC, &ts). That is, if you want a timer interrupt in 500ms time, you get one in 500ms (host) time. In the meantime we'll run the emulated CPU as fast as we can. (This all assumes you're using QEMU's default mode; there is also a '-icount' command line option which attempts to link together the clock with a count of guest instructions executed in a way I don't entirely understand, but which has the effect of making it look a bit more like we execute a certain number of instructions per second.)

The reason for this is that we're just trying to get a vaguely plausible environment for a (typically Linux) guest to run. It wants its timers to occur at the frequency it sets them (so if you say "sleep 5" it really does sleep for 5 seconds), wants as good performance as possible, and doesn't really have serious timing dependencies between different devices.

Incidentally we would never implement a timer counter register as "after 50ns increment the value" -- you emulate this by simply working out what the correct value is at the point when the guest would read it (ie "last known value + (time since that last known value) / 50 ns"). Anything else is horrifically inefficient.

I suggest you have a look at the source for a timer device (they're all in hw/timer).

Quoted 6 lines

Suppose I am directly accessing some X address of memory, which falls into the memory of device A, Now, if transaction doesn't involves CPU (may be using DMA), then how it calls the read/write methods associated with any device (for updating a device's register and carry out some specific functionality) ? Can you please suggest any memory management unit which is responsible for this ?

The MMU isn't relevant here, typically. DMA devices simply use the APIs we have in QEMU for accessing guest physical memory (which in turn end up decoding into RAM accesses or other device accesses, etc). Try looking at the implementation of a DMA controller in hw/dma/ or of some other device you know that does DMA. Note that usually if the guest triggers a DMA operation we'll just instantaneously do it rather than trying to emulate the way the real hardware does this in the background over a period of time.

If you get a DMA device A to write to another device B in such a way that device B tries to DMA back to A then you'll probably deadlock QEMU. You'll often deadlock real hardware if you try that, too, so don't do that.

You almost certainly don't actually need to know the details of how we figure out that an access to physical address X is really a read from offset Y in device D -- that code just works and you're better off focusing on the details of what the APIs for implementing devices are.

Quoted 6 lines
Quoted 1 line

6) Debugging in QEMU

Debugging of QEMU, or of the guest?

How to debug any device code of QEMU, as well as some application running on Guest ?

Big topic. Short summary: we have some debug log options (-d) for tracing some of QEMU's operations, and there is of course gdb and putting printfs in your device code. For debugging the guest we also have a gdb stub implementation. There are no user-friendly facilities here but there is enough to let you get the job done if you know what you're doing.

-- PMM