Skip to contentSkip to conversation

Release of COREMU, a scalable and portable full-system emulator

Qemu-devel Chen Yufei 9 participants 20 messages Jul 17, 2010 – Jul 25, 2010

lore
Share & tools
lore

Chen Yufei

author
#0 lore raw

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

The COREMU Team, Parallel Processing Institute, Fudan University

http://ppi.fudan.edu.cn/system_research_group

#1 lore raw
Quoted 13 lines · Chen Yufei

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

Quoted 5 lines

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Nice work. Do you plan to submit the improvements back to upstream QEMU?

author
#2 lore raw
Quoted 14 lines · Blue Swirl
Quoted 13 lines · Chen Yufei

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

Quoted 7 lines
Quoted 5 lines

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

#3 lore raw

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

author
#4 lore raw
Quoted 35 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

--
Best regards,
Chen Yufei

#5 lore raw
Quoted 39 lines · Chen Yufei
Quoted 37 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Quoted 7 lines
Quoted 2 lines

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

For upstream QEMU, the goal should be to integrate your modifications and enhancements into the existing architecture in a mostly seamless way. The library approach may help maintaining your changes out of tree, but it likely cannot contribute any benefit to an in-tree extension of QEMU for parallel TCG VCPUs.

Quoted 5 lines

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

Quoted 8 lines

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Upstream integration requires such iterative steps as well - in form of ideally small, focused patches that finally convert QEMU into a parallel emulator.

Also note that upstream already supports threaded VCPUs - in KVM mode. You obviously have resolved the major blocking points to apply this on TCG mode as well. But I don't see yet why we may need a new VCPU threading infrastructure for this. Rather only small tuning of what KVM already uses should suffice - if that's required at all.

To give it a start, you could identify some more trivial changes in your patches, split them out and rebase them over latest qemu.git, then post them as a patch series for inclusion (see the mailing list for various examples). Make sure to describe the reason for your changes as clear as possible, specifically if they are not (yet) obvious in the absence of COREMU features in upstream QEMU.

Be prepared that merging your code can be a lengthy process with quite a few discussions about why and how things are done, likely also with requests to change your current solution in some aspects. However, the result should be an optimal solution for the overall goal, parallel VCPU emulation - and no longer any need to maintain your private set of patches against quickly evolving QEMU.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#6 lore raw
In reply to Chen Yufei
Quoted 58 lines · Chen Yufei
Quoted 35 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

Quoted 8 lines

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock? Have you thought about making hardware emulation concurrent?

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

Stefan

#7 lore raw
Quoted 62 lines · Stefan Hajnoczi
Quoted 61 lines · Chen Yufei
Quoted 34 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Quoted 7 lines
Quoted 3 lines

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock?

BTW, this kind of lock is called qemu_global_mutex in QEMU, thus it is a sleepy lock here which is likely better for the code paths protected by it in upstream. Are they shorter in COREMU?

Quoted 5 lines

Have you thought about making hardware emulation concurrent?

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

While we are all looking forward to see more scalable hardware models :), I think it is a topic that can be addressed widely independent of parallelizing TCG VCPUs. The latter can benefit from the former, for sure, but it first of all has to solve its own issues.

Note that --enable-io-thread provides truly parallel KVM VCPUs also in upstream these days. Just for TCG, we need that sightly suboptimal CPU scheduling inside single-threaded tcg_cpu_exec (was renamed to cpu_exec_all today).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#8 lore raw

2010/7/22 Jan Kiszka <[email protected]>:

Quoted 77 lines
Quoted 62 lines · Stefan Hajnoczi
Quoted 61 lines · Chen Yufei
Quoted 34 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Quoted 7 lines
Quoted 3 lines

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock?

BTW, this kind of lock is called qemu_global_mutex in QEMU, thus it is a sleepy lock here which is likely better for the code paths protected by it in upstream. Are they shorter in COREMU?

Quoted 2 lines

Have you thought about making hardware emulation concurrent?

Quoted 8 lines
Quoted 3 lines

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

While we are all looking forward to see more scalable hardware models :), I think it is a topic that can be addressed widely independent of parallelizing TCG VCPUs. The latter can benefit from the former, for sure, but it first of all has to solve its own issues.

Right, but it's worth discussing with people who have worked on parallel vcpus from a different angle.

Quoted 11 lines

Note that --enable-io-thread provides truly parallel KVM VCPUs also in upstream these days. Just for TCG, we need that sightly suboptimal CPU scheduling inside single-threaded tcg_cpu_exec (was renamed to cpu_exec_all today).

Jan

--
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

#9 lore raw
In reply to Jan Kiszka
Quoted 63 lines · 在 2010年7月22日 下午9:00,Jan Kiszka <[email protected]> 写道:
Quoted 62 lines · Stefan Hajnoczi
Quoted 61 lines · Chen Yufei
Quoted 34 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock?

The global lock for hardware access is only for ARM target in our implementation. It is mainly because that we are not quite familiar with ARM. 4 ARM cores (Cortex A9 limitation) could be emulated in such way.
For x86_64 target, we have already made hardware emulation concurrently accessed. We can emulate 255 cores on a quad-core machine.

Quoted 10 lines

BTW, this kind of lock is called qemu_global_mutex in QEMU, thus it is a sleepy lock here which is likely better for the code paths protected by it in upstream. Are they shorter in COREMU?

Quoted 5 lines

Have you thought about making hardware emulation concurrent?

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

For example, when emulating IDE with bus master DMA,
1. Two VCPUs will not send disk w/r requests at the same time.
2. New DMA request will not be sent until the previous one has completed. These two points guarantee the emulated IDE with DMA can be concurrently accessed by either VCPU thread or hw thread with no additional locks.

The only work we need to do is to fix some misbehaving emulated device in current Qemu.
For example, in the function ide_write_dma_cb of Qemu

if (s->nsector == 0) {
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
/* In parallel emulation, OS may receive interrupt here before the DMA
state is updated */
    eot:
        bm->status &= ~BM_STATUS_DMAING;
        bm->status |= BM_STATUS_INT;
        bm->dma_cb = NULL;
        bm->unit = -1;
        bm->aiocb = NULL;
        return;
    }

The DMA state is changed after the IRQ has been sent. This is correct in sequantial emulation. But in parallel emulation, OS may find the DMA is busy even after an end of request interrupt is received. The correct solution should be:

if (s->nsector == 0) {
        s->status = READY_STAT | SEEK_STAT;
/* For coremu dma state need to be changed before irq is sent */
        bm->status &= ~BM_STATUS_DMAING;
        bm->status |= BM_STATUS_INT;
        bm->dma_cb = NULL;
        bm->unit = -1;
        bm->aiocb = NULL;
        ide_set_irq(s->bus);
        return;
       eot:
       ...
}

The DMA state need to be changed before the IRQ has been sent as what real hardware does.

Our evaluation shows that the implementation based on this hypothethis could correctly handle concurrent device accesses.
We also use a per VCPU lock-free queue to hold interrupts information for each VCPU.

For your convience, here is the url for our project
http://sourceforge.net/p/coremu/
We will do our best to merge our code to upstream. :-)

Quoted 18 lines

While we are all looking forward to see more scalable hardware models :), I think it is a topic that can be addressed widely independent of parallelizing TCG VCPUs. The latter can benefit from the former, for sure, but it first of all has to solve its own issues.

Note that --enable-io-thread provides truly parallel KVM VCPUs also in upstream these days. Just for TCG, we need that sightly suboptimal CPU scheduling inside single-threaded tcg_cpu_exec (was renamed to cpu_exec_all today).

Jan

--
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

Signature · 6 lines
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang
#10 lore raw

2010/7/22 wang Tiger <[email protected]>:

Quoted 91 lines · 在 2010年7月22日 下午9:00,Jan Kiszka <[email protected]> 写道:
Quoted 63 lines
Quoted 62 lines · Stefan Hajnoczi
Quoted 61 lines · Chen Yufei
Quoted 34 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock?

The global lock for hardware access is only for ARM target in our implementation. It is mainly because that we are not quite familiar with ARM. 4 ARM cores (Cortex A9 limitation) could be emulated in such way.
For x86_64 target, we have already made hardware emulation concurrently accessed. We can emulate 255 cores on a quad-core machine.

Quoted 10 lines

BTW, this kind of lock is called qemu_global_mutex in QEMU, thus it is a sleepy lock here which is likely better for the code paths protected by it in upstream. Are they shorter in COREMU?

Quoted 5 lines

Have you thought about making hardware emulation concurrent?

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

If you have one hardware thread that handles all device emulation and vcpu threads do no hardware emulation, then all hardware emulation is serialized anyway. Does this describe COREMU's model?

Quoted 85 lines

For example, when emulating IDE with bus master DMA,
1. Two VCPUs will not send disk w/r requests at the same time.
2. New DMA request will not be sent until the previous one has completed. These two points guarantee the emulated IDE with DMA can be concurrently accessed by either VCPU thread or hw thread with no additional locks.

The only work we need to do is to fix some misbehaving emulated device in current Qemu.
For example, in the function ide_write_dma_cb of Qemu

if (s->nsector == 0) {
       s->status = READY_STAT | SEEK_STAT;
       ide_set_irq(s->bus);
/* In parallel emulation, OS may receive interrupt here before the DMA
state is updated */
   eot:
       bm->status &= ~BM_STATUS_DMAING;
       bm->status |= BM_STATUS_INT;
       bm->dma_cb = NULL;
       bm->unit = -1;
       bm->aiocb = NULL;
       return;

}

The DMA state is changed after the IRQ has been sent. This is correct in sequantial emulation. But in parallel emulation, OS may find the DMA is busy even after an end of request interrupt is received. The correct solution should be:

if (s->nsector == 0) {
       s->status = READY_STAT | SEEK_STAT;
/* For coremu dma state need to be changed before irq is sent */
       bm->status &= ~BM_STATUS_DMAING;
       bm->status |= BM_STATUS_INT;
       bm->dma_cb = NULL;
       bm->unit = -1;
       bm->aiocb = NULL;
       ide_set_irq(s->bus);
       return;
      eot:
      ...
}

The DMA state need to be changed before the IRQ has been sent as what real hardware does.

Our evaluation shows that the implementation based on this hypothethis could correctly handle concurrent device accesses.
We also use a per VCPU lock-free queue to hold interrupts information for each VCPU.

For your convience, here is the url for our project
http://sourceforge.net/p/coremu/
We will do our best to merge our code to upstream. :-)

Quoted 11 lines

While we are all looking forward to see more scalable hardware models :), I think it is a topic that can be addressed widely independent of parallelizing TCG VCPUs. The latter can benefit from the former, for sure, but it first of all has to solve its own issues.

Note that --enable-io-thread provides truly parallel KVM VCPUs also in upstream these days. Just for TCG, we need that sightly suboptimal CPU scheduling inside single-threaded tcg_cpu_exec (was renamed to cpu_exec_all today).

Quoted 7 lines

Jan

--
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

--
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang

#11 lore raw
Quoted 99 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 91 lines · 在 2010年7月22日 下午9:00,Jan Kiszka <[email protected]> 写道:
Quoted 63 lines
Quoted 62 lines · Stefan Hajnoczi
Quoted 61 lines · Chen Yufei
Quoted 34 lines · Stefan Weil

Am 21.07.2010 09:03, schrieb Chen Yufei:

Quoted 24 lines · Blue Swirl
Quoted 23 lines · Chen Yufei
Quoted 20 lines

We are pleased to announce COREMU, which is a "multicore-on-multicore" full-system emulator built on Qemu. (Simply speaking, we made Qemu parallel.)

The project web page is located at:
http://ppi.fudan.edu.cn/coremu

You can also download the source code, images for playing on sourceforge
http://sf.net/p/coremu

COREMU is composed of
1. a parallel emulation library
2. a set of patches to qemu
(We worked on the master branch, commit 54d7cf136f040713095cbc064f62d753bff6f9d2)

It currently supports full-system emulation of x64 and ARM MPcore platforms.

By leveraging the underlying multicore resources, it can emulate up to 255 cores running commodity operating systems (even on a 4-core machine).

Enjoy,

Quoted 9 lines
Quoted 2 lines

Nice work. Do you plan to submit the improvements back to upstream QEMU?

It would be great if we can submit our code to QEMU, but we do not know the process. Would you please give us some instructions?

--
Best regards,
Chen Yufei

Quoted 5 lines

Some hints can be found here:
http://wiki.qemu.org/Contribute/StartHere

Kind regards,
Stefan Weil

The patch is in the attachment, produced with command git diff 54d7cf136f040713095cbc064f62d753bff6f9d2

In order to separate what need to be done to make QEMU parallel, we created a separate library, and the patched QEMU need to be compiled and linked with that library. To submit our enhancement to QEMU, maybe we need to incorporate this library into QEMU. I don't know what would be the best solution.

Our approach to make QEMU parallel can be found at http://ppi.fudan.edu.cn/coremu

I will give a short summary here:

1. Each emulated core thread runs a separate binary translator engine and has private code cache. We marked some variables in TCG as thread local. We also modified the TB invalidation mechanism.

2. Each core has a queue holding pending interrupts. The COREMU library provides this queue, and interrupt notification is done by sending realtime signals to the emulated core thread.

3. Atomic instruction emulation has to be modified for parallel emulation. We use lightweight memory transaction which requires only compare-and-swap instruction to emulate atomic instruction.

4. Some code in the original QEMU may cause data race bug after we make it parallel. We fixed these problems.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

--
Best regards,
Chen Yufei

Looking at the patch it seems there is a global lock for hardware access via coremu_spin_lock(&cm_hw_lock). How many cores have you tried running and do you have lock contention data for cm_hw_lock?

The global lock for hardware access is only for ARM target in our implementation. It is mainly because that we are not quite familiar with ARM. 4 ARM cores (Cortex A9 limitation) could be emulated in such way.
For x86_64 target, we have already made hardware emulation concurrently accessed. We can emulate 255 cores on a quad-core machine.

Quoted 10 lines

BTW, this kind of lock is called qemu_global_mutex in QEMU, thus it is a sleepy lock here which is likely better for the code paths protected by it in upstream. Are they shorter in COREMU?

Quoted 5 lines

Have you thought about making hardware emulation concurrent?

These are issues that qemu-kvm faces today since it executes vcpu threads in parallel. Both qemu-kvm and the COREMU patches could benefit from a solution for concurrent hardware access.

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

Quoted 4 lines

If you have one hardware thread that handles all device emulation and vcpu threads do no hardware emulation, then all hardware emulation is serialized anyway. Does this describe COREMU's model?

In our previous implementation, VCPU threads do no hardware emulation. When VCPU w/r an ioport, it put the ioport address and value infomation into a lock free queue. Hardware thread polls the queue to serve the request. When hardware issues an interrupt, hardware thread also put the irq information into a per VCPU lockfree queue. VCPU sets its LAPIC and serve the interrupt request.

For performance reason, we abandon this approach. VCPU is allowed to modify hardware state.
Hardware code is only to be slightly modified. The misbehavior of the guest OS can be easily detected.

Quoted 87 lines
Quoted 86 lines

For example, when emulating IDE with bus master DMA,
1. Two VCPUs will not send disk w/r requests at the same time.
2. New DMA request will not be sent until the previous one has completed. These two points guarantee the emulated IDE with DMA can be concurrently accessed by either VCPU thread or hw thread with no additional locks.

The only work we need to do is to fix some misbehaving emulated device in current Qemu.
For example, in the function ide_write_dma_cb of Qemu

if (s->nsector == 0) {
       s->status = READY_STAT | SEEK_STAT;
       ide_set_irq(s->bus);
/* In parallel emulation, OS may receive interrupt here before the DMA
state is updated */
   eot:
       bm->status &= ~BM_STATUS_DMAING;
       bm->status |= BM_STATUS_INT;
       bm->dma_cb = NULL;
       bm->unit = -1;
       bm->aiocb = NULL;
       return;

}

The DMA state is changed after the IRQ has been sent. This is correct in sequantial emulation. But in parallel emulation, OS may find the DMA is busy even after an end of request interrupt is received. The correct solution should be:

if (s->nsector == 0) {
       s->status = READY_STAT | SEEK_STAT;
/* For coremu dma state need to be changed before irq is sent */
       bm->status &= ~BM_STATUS_DMAING;
       bm->status |= BM_STATUS_INT;
       bm->dma_cb = NULL;
       bm->unit = -1;
       bm->aiocb = NULL;
       ide_set_irq(s->bus);
       return;
      eot:
      ...
}

The DMA state need to be changed before the IRQ has been sent as what real hardware does.

Our evaluation shows that the implementation based on this hypothethis could correctly handle concurrent device accesses.
We also use a per VCPU lock-free queue to hold interrupts information for each VCPU.

For your convience, here is the url for our project
http://sourceforge.net/p/coremu/
We will do our best to merge our code to upstream. :-)

Quoted 11 lines

While we are all looking forward to see more scalable hardware models :), I think it is a topic that can be addressed widely independent of parallelizing TCG VCPUs. The latter can benefit from the former, for sure, but it first of all has to solve its own issues.

Note that --enable-io-thread provides truly parallel KVM VCPUs also in upstream these days. Just for TCG, we need that sightly suboptimal CPU scheduling inside single-threaded tcg_cpu_exec (was renamed to cpu_exec_all today).

Quoted 7 lines

Jan

--
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

--
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang

Signature · 6 lines
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang
#12 lore raw
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

Jan

#13 lore raw
Quoted 27 lines · Jan Kiszka
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 4 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

But isn't that independent from making TCG atomic capable and parallel? At that point a TCG vCPU would have the exact same issues and interfaces as a KVM vCPU, right? And then we can tackle the concurrent device access issues together.

Alex

#14 lore raw

2010/7/23 Alexander Graf <[email protected]>:

Quoted 30 lines · Jan Kiszka
Quoted 28 lines
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 6 lines
Quoted 4 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

But isn't that independent from making TCG atomic capable and parallel? At that point a TCG vCPU would have the exact same issues and interfaces as a KVM vCPU, right? And then we can tackle the concurrent device access issues together.

An issue that might affect COREMU today is core QEMU subsystems that are not thread-safe and used from hardware emulation, for example:

cpu_physical_memory_read/write() to RAM will use qemu_get_ram_ptr(). This function moves the found RAMBlock to the head of the global RAM blocks list in a non-atomic way. Therefore, two unrelated hardware devices executing cpu_physical_memory_*() simultaneously face a race condition. I have seen this happen when playing with parallel hardware emulation.

Tiger: If you are only locking the hardware thread for ARM target, your hardware emulation is not safe for other targets. Have I missed something in the COREMU patch that defends against this problem?

Stefan

#15 lore raw
Quoted 37 lines · Stefan Hajnoczi

2010/7/23 Alexander Graf <[email protected]>:

Quoted 27 lines · Jan Kiszka
Quoted 26 lines
Quoted 13 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 5 lines
Quoted 4 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

But isn't that independent from making TCG atomic capable and parallel? At that point a TCG vCPU would have the exact same issues and interfaces as a KVM vCPU, right? And then we can tackle the concurrent device access issues together.

An issue that might affect COREMU today is core QEMU subsystems that are not thread-safe and used from hardware emulation, for example:

Quoted 6 lines

cpu_physical_memory_read/write() to RAM will use qemu_get_ram_ptr(). This function moves the found RAMBlock to the head of the global RAM blocks list in a non-atomic way. Therefore, two unrelated hardware devices executing cpu_physical_memory_*() simultaneously face a race condition. I have seen this happen when playing with parallel hardware emulation.

Those issues need to be identified and, in a first step, worked around by holding dedicated locks or just the global mutex. Maybe the above conflict can also directly be resolved by creating per-VCPU lookup lists (likely more efficient than tapping on other VCPU shoes by constantly reordering a global list). Likely a good example for a self-contained preparatory patch.

However, getting concurrency right is tricky enough. We should really be careful with turning to much upside down in a rush. Even if TCG may have some deeper hooks into the device model or thread-unsafe core parts than KVM, parallelizing it can and should remain a separate topic. And we also have to keep an eye on performance if a bit less than 255 VCPUs shall be emulated.

Jan

#16 lore raw
In reply to Stefan Hajnoczi
Quoted 48 lines · 在 2010年7月23日 下午5:13,Stefan Hajnoczi <[email protected]> 写道:

2010/7/23 Alexander Graf <[email protected]>:

Quoted 30 lines · Jan Kiszka
Quoted 28 lines
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 6 lines
Quoted 4 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

But isn't that independent from making TCG atomic capable and parallel? At that point a TCG vCPU would have the exact same issues and interfaces as a KVM vCPU, right? And then we can tackle the concurrent device access issues together.

An issue that might affect COREMU today is core QEMU subsystems that are not thread-safe and used from hardware emulation, for example:

cpu_physical_memory_read/write() to RAM will use qemu_get_ram_ptr(). This function moves the found RAMBlock to the head of the global RAM blocks list in a non-atomic way. Therefore, two unrelated hardware devices executing cpu_physical_memory_*() simultaneously face a race condition. I have seen this happen when playing with parallel hardware emulation.

Quoted 6 lines

Tiger: If you are only locking the hardware thread for ARM target, your hardware emulation is not safe for other targets. Have I missed something in the COREMU patch that defends against this problem?

Stefan

In fact, we solve this problem through a really simple method. In our prototype, we removed this piece of code like this:
void *qemu_get_ram_ptr(ram_addr_t addr)

{
    ......

    /* Move this entry to to start of the list.  */
#ifndef CONFIG_COREMU
    /* Different core can access this function at the same time.
     * For coremu, disable this optimization to avoid data race.
     * XXX or use spin lock here if performance impact is big. */
    if (prev) {
        prev->next = block->next;
        block->next = *prevp;
        *prevp = block;
    }
#endif
    return block->host + (addr - block->offset);
}

CONFIG_COREMU is defined when TCG parallel mode is configured. And the list is more likely to be read only without hotplug device, so we don't use a lock to protect it.
Reimplement this list with a lock free list is also reasonable, but seems unnecessary. :-)

Signature · 6 lines
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang
#17 lore raw

2010/7/23 wang Tiger <[email protected]>:

Quoted 75 lines · 在 2010年7月23日 下午5:13,Stefan Hajnoczi <[email protected]> 写道:
Quoted 48 lines

2010/7/23 Alexander Graf <[email protected]>:

Quoted 30 lines · Jan Kiszka
Quoted 28 lines
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 6 lines
Quoted 4 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

But isn't that independent from making TCG atomic capable and parallel? At that point a TCG vCPU would have the exact same issues and interfaces as a KVM vCPU, right? And then we can tackle the concurrent device access issues together.

An issue that might affect COREMU today is core QEMU subsystems that are not thread-safe and used from hardware emulation, for example:

cpu_physical_memory_read/write() to RAM will use qemu_get_ram_ptr(). This function moves the found RAMBlock to the head of the global RAM blocks list in a non-atomic way. Therefore, two unrelated hardware devices executing cpu_physical_memory_*() simultaneously face a race condition. I have seen this happen when playing with parallel hardware emulation.

Quoted 6 lines

Tiger: If you are only locking the hardware thread for ARM target, your hardware emulation is not safe for other targets. Have I missed something in the COREMU patch that defends against this problem?

Stefan

In fact, we solve this problem through a really simple method. In our prototype, we removed this piece of code like this:
void *qemu_get_ram_ptr(ram_addr_t addr)

{
   ......

   /* Move this entry to to start of the list.  */
#ifndef CONFIG_COREMU
   /* Different core can access this function at the same time.
    * For coremu, disable this optimization to avoid data race.
    * XXX or use spin lock here if performance impact is big. */
   if (prev) {
       prev->next = block->next;
       block->next = *prevp;
       *prevp = block;
   }
#endif
   return block->host + (addr - block->offset);
}
Quoted 5 lines

CONFIG_COREMU is defined when TCG parallel mode is configured. And the list is more likely to be read only without hotplug device, so we don't use a lock to protect it.
Reimplement this list with a lock free list is also reasonable, but seems unnecessary. :-)

Ah, good :).

Stefan

Quoted 8 lines

--
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang

#18 lore raw
Quoted 20 lines · Stefan Hajnoczi
Quoted 20 lines

In fact, we solve this problem through a really simple method. In our prototype, we removed this piece of code like this:
void *qemu_get_ram_ptr(ram_addr_t addr)

{
    ......

    /* Move this entry to to start of the list.  */
#ifndef CONFIG_COREMU
    /* Different core can access this function at the same time.
     * For coremu, disable this optimization to avoid data race.
     * XXX or use spin lock here if performance impact is big. */
    if (prev) {
        prev->next = block->next;
        block->next = *prevp;
        *prevp = block;
    }
#endif
    return block->host + (addr - block->offset);
}
Quoted 7 lines
Quoted 5 lines

CONFIG_COREMU is defined when TCG parallel mode is configured. And the list is more likely to be read only without hotplug device, so we don't use a lock to protect it.
Reimplement this list with a lock free list is also reasonable, but seems unnecessary. :-)

Ah, good :).

For this one in particular, you could just use circular lists (without a "head" node, unlike the Linux kernel's list data type, as there's always a RAM entry) and start iteration at "prev".

Paolo

#19 lore raw
In reply to Jan Kiszka
Quoted 27 lines · 在 2010年7月23日 下午3:53,Jan Kiszka <[email protected]> 写道:
Quoted 14 lines · wang Tiger
Quoted 7 lines · 在 2010年7月22日 下午11:47,Stefan Hajnoczi <[email protected]> 写道:

2010/7/22 wang Tiger <[email protected]>:

Quoted 6 lines

In our implementation for x86_64 target, all devices except LAPIC are emulated in a seperate thread. VCPUs are emulated in other threads (one thread per VCPU).
By observing some device drivers in linux, we have a hypothethis that drivers in OS have already ensured correct synchronization on concurrent hardware accesses.

Quoted 5 lines

This hypothesis is too optimistic. If hardware emulation code assumes it is only executed in a single-threaded fashion, but guests can execute it in parallel, then this opens up the possibility of race conditions that malicious guests can exploit. There needs to be
isolation: a guest should not be able to cause QEMU to crash.

Quoted 5 lines

In our prototype, we assume the guest behaves correctly. If hardware emulation code can ensure atomic access(behave like real hardware), VCPUS can access device freely. We actually refine some hardward emulation code (eg. BMDMA, IOAPIC ) to ensure the atomicity of hardware access.

This approach is surely helpful for a prototype to explore the limits. But it's not applicable to production systems. It would create a huge source of potential subtle regressions for other guest OSes, specifically those that you cannot analyze regarding synchronized hardware access. We must play safe.

Quoted 8 lines

That's why we currently have the global mutex. Its conversion can only happen step-wise, e.g. by establishing an infrastructure to declare the need of device models for that Big Lock. Then you can start converting individual models to private locks or even smart lock-less patterns.

Jan

I agree with you on this point. The approach we used is really helpful for a research prototype. But it needs a lot of work to make it applicable to production systems.
Its my pleasure if we can tackle this issue togethor.

Signature · 6 lines
Zhaoguo Wang, Parallel Processing Institute, Fudan University

Address: Room 320, Software Building, 825 Zhangheng Road, Shanghai, China

[email protected]
http://ppi.fudan.edu.cn/zhaoguo_wang