Changeset
accel/stubs/Makefile.objs |    9 +-
accel/stubs/whpx-stub.c   |   48 ++
configure                 |   48 +-
cpus.c                    |   66 ++-
include/sysemu/hw_accel.h |   13 +
include/sysemu/whpx.h     |   40 ++
qemu-options.hx           |    8 +-
target/i386/Makefile.objs |    1 +
target/i386/helper.c      |    2 +-
target/i386/whpx-all.c    | 1395 +++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 1619 insertions(+), 11 deletions(-)
create mode 100644 accel/stubs/whpx-stub.c
create mode 100644 include/sysemu/whpx.h
create mode 100644 target/i386/whpx-all.c
Git apply log
Switched to a new branch '1515788548-3570-1-git-send-email-juterry@microsoft.com'
Applying: Add the Windows Hypervisor Platform accelerator.
Applying: Add the WHPX vcpu API
Applying: Introduce the WHPX impl
Applying: Add the WHPX acceleration enlightenments
To https://github.com/patchew-project/qemu
 * [new tag]               patchew/1515788548-3570-1-git-send-email-juterry@microsoft.com -> patchew/1515788548-3570-1-git-send-email-juterry@microsoft.com
Test passed: docker

loading

Test failed: checkpatch

loading

Test passed: s390x

loading

Test passed: ppc

loading

[Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Justin Terry (VM) via Qemu-devel, 5 days ago
Hello QEMU Community!

Over the past few months the Hyper-V team at Microsoft has been working hard on
a new user-mode API for our hypervisor that will be released as part of the
upcoming SDK. This new API adds user-mode capabilities to create and manage
partitions at the hypervisor level, configure memory mappings for the
partition, and create and control execution of virtual processors.

With this new API we are now able to bring our hypervisor to the QEMU
community! The following patches implement the Windows Hypervisor Platform
accelerator (WHPX) for QEMU on Windows 10 hosts.

When compiling QEMU for x86_64 passing the --enable-whpx flag will compile the
accelerator for use. At runtime using the '-accel whpx' should see a
significant performance improvement over emulation, much like when using 'hax'
on Windows.

Over the next few days the pre-release version of the documentation for this new
API will be visible at https://docs.microsoft.com/en-us/virtualization under
the 'Windows Hypervisor Platform' section. Here you will see the requirements,
API, and examples.

Thank you for your feedback. 

Justin Terry (VM) (4):
  Add the Windows Hypervisor Platform accelerator.
  Add the WHPX vcpu API
  Introduce the WHPX impl
  Add the WHPX acceleration enlightenments

 accel/stubs/Makefile.objs |    9 +-
 accel/stubs/whpx-stub.c   |   48 ++
 configure                 |   48 +-
 cpus.c                    |   66 ++-
 include/sysemu/hw_accel.h |   13 +
 include/sysemu/whpx.h     |   40 ++
 qemu-options.hx           |    8 +-
 target/i386/Makefile.objs |    1 +
 target/i386/helper.c      |    2 +-
 target/i386/whpx-all.c    | 1395 +++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 1619 insertions(+), 11 deletions(-)
 create mode 100644 accel/stubs/whpx-stub.c
 create mode 100644 include/sysemu/whpx.h
 create mode 100644 target/i386/whpx-all.c

-- 
2.7.4


Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Paolo Bonzini, 5 days ago
> Hello QEMU Community!
> 
> Over the past few months the Hyper-V team at Microsoft has been working hard on
> a new user-mode API for our hypervisor that will be released as part of the
> upcoming SDK. This new API adds user-mode capabilities to create and manage
> partitions at the hypervisor level, configure memory mappings for the
> partition, and create and control execution of virtual processors.
> 
> With this new API we are now able to bring our hypervisor to the QEMU
> community! The following patches implement the Windows Hypervisor Platform
> accelerator (WHPX) for QEMU on Windows 10 hosts.
> 
> When compiling QEMU for x86_64 passing the --enable-whpx flag will compile
> the accelerator for use. At runtime using the '-accel whpx' should see a
> significant performance improvement over emulation, much like when using
> 'hax' on Windows.
> 
> Over the next few days the pre-release version of the documentation for this
> new API will be visible at https://docs.microsoft.com/en-us/virtualization under
> the 'Windows Hypervisor Platform' section. Here you will see the
> requirements, API, and examples.

That's great!  Accelerator galore. :)

From a quick skim they patches seem to be generally pretty good!  I have a few quick
question on a couple features that seem to be missing:

- an important missing feature is support for CPU models (e.g. "-cpu Haswell").  Code
  for that was added recently for Hypervisor.framework and you should be able to copy
  some of it and then more for your CPUID intercept.  However, the question I have is:
  what interfaces does WHPX support to query the hypervisor's CPUID capabilities?

- is dirty memory support available?  The log_sync callback seems a bit incomplete :)
  If it's not, VGA output probably will not work.

- I might have missed this because the relevant code is quite repetitive - what about
  get/set of XSAVE areas?  If either this or dirty memory is not present, it's probably
  best to block migration (grep for migration_blockers).

Also, what versions of Windows will support WHPX?

Thanks,

Paolo
> Thank you for your feedback.
> 
> Justin Terry (VM) (4):
>   Add the Windows Hypervisor Platform accelerator.
>   Add the WHPX vcpu API
>   Introduce the WHPX impl
>   Add the WHPX acceleration enlightenments
> 
>  accel/stubs/Makefile.objs |    9 +-
>  accel/stubs/whpx-stub.c   |   48 ++
>  configure                 |   48 +-
>  cpus.c                    |   66 ++-
>  include/sysemu/hw_accel.h |   13 +
>  include/sysemu/whpx.h     |   40 ++
>  qemu-options.hx           |    8 +-
>  target/i386/Makefile.objs |    1 +
>  target/i386/helper.c      |    2 +-
>  target/i386/whpx-all.c    | 1395
>  +++++++++++++++++++++++++++++++++++++++++++++
>  10 files changed, 1619 insertions(+), 11 deletions(-)
>  create mode 100644 accel/stubs/whpx-stub.c
>  create mode 100644 include/sysemu/whpx.h
>  create mode 100644 target/i386/whpx-all.c
> 
> --
> 2.7.4
> 
> 

Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Justin Terry (VM) via Qemu-devel, 20 hours ago
Hey Paolo,

Thanks for all the feedback. After talking with various members of the team around these scenarios I think its best we add the migration blockers for all of your comments unfortunately. The answers to your question are below:

1. For CPUID. The API has support for an "EXTENDED_VM_EXITS" of type WHvRunVpExitReasonX64Cpuid after a call to WHvRunVirtualProcessor. This will one day allow for the CPUID instruction to be resolved however it is not currently implemented in the platform. So for right now we will have to block on CPUID.

2. For dirty memory tracking. We currently don’t have support in the platform or API for this. I have added that to the feature backlog and will update QEMU as that gets implemented in the platform. However, VGA does indeed work with the current impl of log_sync.

3. Also, for XSAVE/RSTOR we again don’t have platform support for the required registers. I have added this to the feature platform backlog to gain access for that functionality as well.

4. This will be supported on Windows 10 hosts (I don’t have the insider build number yet) and it will require an Intel processor with VT-x and Unrestricted Guest support. Will update the qemu-devel list once we have an insider build that is public that has the platform bits.

So, if your ok with it I will block on CPUID, Memory Tracking, and XSAVE/RSTOR functionality and send out the updated PR.

-Justin

> -----Original Message-----
> From: Paolo Bonzini [mailto:pbonzini@redhat.com]
> Sent: Friday, January 12, 2018 12:48 PM
> To: Justin Terry (VM) <juterry@microsoft.com>
> Cc: qemu-devel@nongnu.org qemu-devel <qemu-devel@nongnu.org>
> Subject: Re: [PATCH 0/4] Implements the Windows Hypervisor Platform
> accelerator
> 
> 
> > Hello QEMU Community!
> >
> > Over the past few months the Hyper-V team at Microsoft has been
> > working hard on a new user-mode API for our hypervisor that will be
> > released as part of the upcoming SDK. This new API adds user-mode
> > capabilities to create and manage partitions at the hypervisor level,
> > configure memory mappings for the partition, and create and control
> execution of virtual processors.
> >
> > With this new API we are now able to bring our hypervisor to the QEMU
> > community! The following patches implement the Windows Hypervisor
> > Platform accelerator (WHPX) for QEMU on Windows 10 hosts.
> >
> > When compiling QEMU for x86_64 passing the --enable-whpx flag will
> > compile the accelerator for use. At runtime using the '-accel whpx'
> > should see a significant performance improvement over emulation, much
> > like when using 'hax' on Windows.
> >
> > Over the next few days the pre-release version of the documentation
> > for this new API will be visible at
> >
> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.
> > microsoft.com%2Fen-
> us%2Fvirtualization&data=02%7C01%7Cjuterry%40microsoft.com%7Ce979c5
> 0ab5544ab2dec708d559fdc86f%7C72f988bf86f141af91ab2d7cd011db47%7C1
> %7C0%7C636513868884987772&sdata=E%2BE5DX%2FJqsK2%2BZJVWzQ3qFy
> Kg0YLKrg6EtzLkvnJqbU%3D&reserved=0 under the 'Windows Hypervisor
> Platform' section. Here you will see the requirements, API, and examples.
> 
> That's great!  Accelerator galore. :)
> 
> From a quick skim they patches seem to be generally pretty good!  I have a
> few quick question on a couple features that seem to be missing:
> 
> - an important missing feature is support for CPU models (e.g. "-cpu
> Haswell").  Code
>   for that was added recently for Hypervisor.framework and you should be
> able to copy
>   some of it and then more for your CPUID intercept.  However, the question
> I have is:
>   what interfaces does WHPX support to query the hypervisor's CPUID
> capabilities?
> 
> - is dirty memory support available?  The log_sync callback seems a bit
> incomplete :)
>   If it's not, VGA output probably will not work.
> 
> - I might have missed this because the relevant code is quite repetitive -
> what about
>   get/set of XSAVE areas?  If either this or dirty memory is not present, it's
> probably
>   best to block migration (grep for migration_blockers).
> 
> Also, what versions of Windows will support WHPX?
> 
> Thanks,
> 
> Paolo
> > Thank you for your feedback.
> >
> > Justin Terry (VM) (4):
> >   Add the Windows Hypervisor Platform accelerator.
> >   Add the WHPX vcpu API
> >   Introduce the WHPX impl
> >   Add the WHPX acceleration enlightenments
> >
> >  accel/stubs/Makefile.objs |    9 +-
> >  accel/stubs/whpx-stub.c   |   48 ++
> >  configure                 |   48 +-
> >  cpus.c                    |   66 ++-
> >  include/sysemu/hw_accel.h |   13 +
> >  include/sysemu/whpx.h     |   40 ++
> >  qemu-options.hx           |    8 +-
> >  target/i386/Makefile.objs |    1 +
> >  target/i386/helper.c      |    2 +-
> >  target/i386/whpx-all.c    | 1395
> >  +++++++++++++++++++++++++++++++++++++++++++++
> >  10 files changed, 1619 insertions(+), 11 deletions(-)  create mode
> > 100644 accel/stubs/whpx-stub.c  create mode 100644
> > include/sysemu/whpx.h  create mode 100644 target/i386/whpx-all.c
> >
> > --
> > 2.7.4
> >
> >
Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Stefan Weil, 12 hours ago
Am 18.01.2018 um 00:41 schrieb Justin Terry (VM) via Qemu-devel:
> Hey Paolo,
[...]
> 4. This will be supported on Windows 10 hosts (I don’t have the insider build number yet) and it will require an Intel processor with VT-x and Unrestricted Guest support. Will update the qemu-devel list once we have an insider build that is public that has the platform bits.

Is it restricted to professional and enterprise versions of Windows
(that would be bad), or will it run on any Windows 10 host?

Stefan


Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Paolo Bonzini, 11 hours ago
On 18/01/2018 00:41, Justin Terry (VM) wrote:
> 
> Thanks for all the feedback. After talking with various members of
> the team around these scenarios I think its best we add the migration
> blockers for all of your comments unfortunately. The answers to your
> question are below:
> 
> 1. For CPUID. The API has support for an "EXTENDED_VM_EXITS" of type
> WHvRunVpExitReasonX64Cpuid after a call to WHvRunVirtualProcessor.
> This will one day allow for the CPUID instruction to be resolved
> however it is not currently implemented in the platform. So for right
> now we will have to block on CPUID.
> 
> 2. For dirty memory tracking. We currently don’t have support in the
> platform or API for this. I have added that to the feature backlog
> and will update QEMU as that gets implemented in the platform.
> However, VGA does indeed work with the current impl of log_sync.
> 
> 3. Also, for XSAVE/RSTOR we again don’t have platform support for the
> required registers. I have added this to the feature platform backlog
> to gain access for that functionality as well.
> 
> 4. This will be supported on Windows 10 hosts (I don’t have the
> insider build number yet) and it will require an Intel processor with
> VT-x and Unrestricted Guest support. Will update the qemu-devel list
> once we have an insider build that is public that has the platform
> bits.
> 
> So, if your ok with it I will block on CPUID, Memory Tracking, and
> XSAVE/RSTOR functionality and send out the updated PR.

Yes, thanks.  Of course just one migration blocker is enough.  Add a
comment like the above 1-3 bullet points above it.

Thanks,

Paolo

Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Stefan Weil, 5 days ago
Am 12.01.2018 um 21:22 schrieb Justin Terry (VM) via Qemu-devel:
> Hello QEMU Community!
> 
> Over the past few months the Hyper-V team at Microsoft has been working hard on
> a new user-mode API for our hypervisor that will be released as part of the
> upcoming SDK. This new API adds user-mode capabilities to create and manage
> partitions at the hypervisor level, configure memory mappings for the
> partition, and create and control execution of virtual processors.
> 
> With this new API we are now able to bring our hypervisor to the QEMU
> community! The following patches implement the Windows Hypervisor Platform
> accelerator (WHPX) for QEMU on Windows 10 hosts.
> 
> When compiling QEMU for x86_64 passing the --enable-whpx flag will compile the
> accelerator for use. At runtime using the '-accel whpx' should see a
> significant performance improvement over emulation, much like when using 'hax'
> on Windows.
> 
> Over the next few days the pre-release version of the documentation for this new
> API will be visible at https://docs.microsoft.com/en-us/virtualization under
> the 'Windows Hypervisor Platform' section. Here you will see the requirements,
> API, and examples.
> 
> Thank you for your feedback. 
> 
> Justin Terry (VM) (4):
>   Add the Windows Hypervisor Platform accelerator.
>   Add the WHPX vcpu API
>   Introduce the WHPX impl
>   Add the WHPX acceleration enlightenments
> 
>  accel/stubs/Makefile.objs |    9 +-
>  accel/stubs/whpx-stub.c   |   48 ++
>  configure                 |   48 +-
>  cpus.c                    |   66 ++-
>  include/sysemu/hw_accel.h |   13 +
>  include/sysemu/whpx.h     |   40 ++
>  qemu-options.hx           |    8 +-
>  target/i386/Makefile.objs |    1 +
>  target/i386/helper.c      |    2 +-
>  target/i386/whpx-all.c    | 1395 +++++++++++++++++++++++++++++++++++++++++++++
>  10 files changed, 1619 insertions(+), 11 deletions(-)
>  create mode 100644 accel/stubs/whpx-stub.c
>  create mode 100644 include/sysemu/whpx.h
>  create mode 100644 target/i386/whpx-all.c


Thank you for this contribution – I'll test it.

The patches 2 and 3 fail the test with scripts/checkpatch.pl. Please fix
that in the next iteration (maybe wait a little until there was more
feedback).

Regards,
Stefan

Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Stefan Weil, 5 days ago
Am 12.01.2018 um 22:57 schrieb Stefan Weil:
>> Over the next few days the pre-release version of the documentation for this new
>> API will be visible at https://docs.microsoft.com/en-us/virtualization under
>> the 'Windows Hypervisor Platform' section. Here you will see the requirements,
>> API, and examples.

Please inform the qemu-devel list when the required files for building
are available.

For now, building fails:

$ ../configure --enable-whpx

ERROR: User requested feature winhvplatform
       configure was not able to find it.
       winhvemulation is not installed

Enhancing the build instructions for QEMU would also be a good idea then.

Thanks,
Stefan

Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Justin Terry (VM) via Qemu-devel, 2 days ago
Hey Stefan,

Again thanks for the feedback. The short answer is that it will be first available on an Windows 10 Insider Preview release: https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewSDK. I just don’t know which one exactly. When this is released I will for sure update the community that it is now available for building and testing.

When you say "enhancing the build instructions for QEMU" do you mean having an entry on the wiki describing cross compiling on Windows? I would be happy to add to that wiki page describing what we do now. Could you give me a pointer on where you keep the wiki code so I can add the descriptions there?

Thanks,
Justin

> -----Original Message-----
> From: Stefan Weil [mailto:sw@weilnetz.de]
> Sent: Friday, January 12, 2018 2:06 PM
> To: Justin Terry (VM) <juterry@microsoft.com>; qemu-devel@nongnu.org
> Cc: pbonzini@redhat.com; crosthwaite.peter@gmail.com;
> ehabkost@redhat.com; rth@twiddle.net
> Subject: Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor
> Platform accelerator
> 
> Am 12.01.2018 um 22:57 schrieb Stefan Weil:
> >> Over the next few days the pre-release version of the documentation
> >> for this new API will be visible at
> >>
> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs
> >> .microsoft.com%2Fen-
> us%2Fvirtualization&data=02%7C01%7Cjuterry%40microsoft.com%7C736413
> 5f827e468fa10108d55a08a667%7C72f988bf86f141af91ab2d7cd011db47%7C1
> %7C1%7C636513915865544044&sdata=CGgf2qVBFUAi4UCfBEmTvrRb8BD7QG
> kKytkZyhRCoh4%3D&reserved=0 under the 'Windows Hypervisor Platform'
> section. Here you will see the requirements, API, and examples.
> 
> Please inform the qemu-devel list when the required files for building are
> available.
> 
> For now, building fails:
> 
> $ ../configure --enable-whpx
> 
> ERROR: User requested feature winhvplatform
>        configure was not able to find it.
>        winhvemulation is not installed
> 
> Enhancing the build instructions for QEMU would also be a good idea then.
> 
> Thanks,
> Stefan
Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by Justin Terry (VM) via Qemu-devel, 2 days ago
Hey Stefan,

Thanks for the feedback. I really apologize I forgot to run the checkpath.pl script before submission. I have fixed up all the issues and will send them in patch v2 once I fixup the other feedback.

Thanks,
Justin

-----Original Message-----
From: Stefan Weil [mailto:sw@weilnetz.de] 
Sent: Friday, January 12, 2018 1:57 PM
To: Justin Terry (VM) <juterry@microsoft.com>; qemu-devel@nongnu.org
Cc: pbonzini@redhat.com; rth@twiddle.net; ehabkost@redhat.com; crosthwaite.peter@gmail.com
Subject: Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator

Am 12.01.2018 um 21:22 schrieb Justin Terry (VM) via Qemu-devel:
> Hello QEMU Community!
> 
> Over the past few months the Hyper-V team at Microsoft has been 
> working hard on a new user-mode API for our hypervisor that will be 
> released as part of the upcoming SDK. This new API adds user-mode 
> capabilities to create and manage partitions at the hypervisor level, 
> configure memory mappings for the partition, and create and control execution of virtual processors.
> 
> With this new API we are now able to bring our hypervisor to the QEMU 
> community! The following patches implement the Windows Hypervisor 
> Platform accelerator (WHPX) for QEMU on Windows 10 hosts.
> 
> When compiling QEMU for x86_64 passing the --enable-whpx flag will 
> compile the accelerator for use. At runtime using the '-accel whpx' 
> should see a significant performance improvement over emulation, much like when using 'hax'
> on Windows.
> 
> Over the next few days the pre-release version of the documentation 
> for this new API will be visible at 
> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.
> microsoft.com%2Fen-us%2Fvirtualization&data=02%7C01%7Cjuterry%40microsoft.com%7Caf4261019804491eb49608d55a076ea4%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C1%7C636513910334166849&sdata=up%2FGCX06qE4RgerXWLJhxLsrspvzrvTZnY53nP4504c%3D&reserved=0 under the 'Windows Hypervisor Platform' section. Here you will see the requirements, API, and examples.
> 
> Thank you for your feedback. 
> 
> Justin Terry (VM) (4):
>   Add the Windows Hypervisor Platform accelerator.
>   Add the WHPX vcpu API
>   Introduce the WHPX impl
>   Add the WHPX acceleration enlightenments
> 
>  accel/stubs/Makefile.objs |    9 +-
>  accel/stubs/whpx-stub.c   |   48 ++
>  configure                 |   48 +-
>  cpus.c                    |   66 ++-
>  include/sysemu/hw_accel.h |   13 +
>  include/sysemu/whpx.h     |   40 ++
>  qemu-options.hx           |    8 +-
>  target/i386/Makefile.objs |    1 +
>  target/i386/helper.c      |    2 +-
>  target/i386/whpx-all.c    | 1395 +++++++++++++++++++++++++++++++++++++++++++++
>  10 files changed, 1619 insertions(+), 11 deletions(-)  create mode 
> 100644 accel/stubs/whpx-stub.c  create mode 100644 
> include/sysemu/whpx.h  create mode 100644 target/i386/whpx-all.c


Thank you for this contribution – I'll test it.

The patches 2 and 3 fail the test with scripts/checkpatch.pl. Please fix that in the next iteration (maybe wait a little until there was more feedback).

Regards,
Stefan
Re: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator
Posted by no-reply@patchew.org, 5 days ago
Hi,

This series seems to have some coding style problems. See output below for
more information:

Type: series
Message-id: 1515788548-3570-1-git-send-email-juterry@microsoft.com
Subject: [Qemu-devel] [PATCH 0/4] Implements the Windows Hypervisor Platform accelerator

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)"
for c in $commits; do
    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))
done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
c9a68bcd96 Add the WHPX acceleration enlightenments
03377875bf Introduce the WHPX impl
4fb340d015 Add the WHPX vcpu API
a4e1eebb6a Add the Windows Hypervisor Platform accelerator.

=== OUTPUT BEGIN ===
Checking PATCH 1/4: Add the Windows Hypervisor Platform accelerator....
Checking PATCH 2/4: Add the WHPX vcpu API...
ERROR: adding a line without newline at end of file
#126: FILE: include/sysemu/whpx.h:40:
+#endif /* QEMU_WHPX_H */

total: 1 errors, 0 warnings, 97 lines checked

Your patch has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.

Checking PATCH 3/4: Introduce the WHPX impl...
ERROR: do not use C99 // comments
#71: FILE: target/i386/whpx-all.c:38:
+    // X64 General purpose registers

ERROR: do not use C99 // comments
#91: FILE: target/i386/whpx-all.c:58:
+    // X64 Segment registers

ERROR: do not use C99 // comments
#101: FILE: target/i386/whpx-all.c:68:
+    // X64 Table registers

ERROR: do not use C99 // comments
#105: FILE: target/i386/whpx-all.c:72:
+    // X64 Control Registers

ERROR: do not use C99 // comments
#112: FILE: target/i386/whpx-all.c:79:
+    // X64 Debug Registers

ERROR: do not use C99 // comments
#113: FILE: target/i386/whpx-all.c:80:
+//    WHvX64RegisterDr0,

ERROR: do not use C99 // comments
#114: FILE: target/i386/whpx-all.c:81:
+//    WHvX64RegisterDr1,

ERROR: do not use C99 // comments
#115: FILE: target/i386/whpx-all.c:82:
+//    WHvX64RegisterDr2,

ERROR: do not use C99 // comments
#116: FILE: target/i386/whpx-all.c:83:
+//    WHvX64RegisterDr3,

ERROR: do not use C99 // comments
#117: FILE: target/i386/whpx-all.c:84:
+//    WHvX64RegisterDr6,

ERROR: do not use C99 // comments
#118: FILE: target/i386/whpx-all.c:85:
+//    WHvX64RegisterDr7,

ERROR: do not use C99 // comments
#120: FILE: target/i386/whpx-all.c:87:
+    // X64 Floating Point and Vector Registers

ERROR: do not use C99 // comments
#148: FILE: target/i386/whpx-all.c:115:
+    // X64 MSRs

ERROR: do not use C99 // comments
#155: FILE: target/i386/whpx-all.c:122:
+//    WHvX64RegisterPat,

ERROR: do not use C99 // comments
#166: FILE: target/i386/whpx-all.c:133:
+    // Interrupt / Event Registers

ERROR: do not use C99 // comments
#167: FILE: target/i386/whpx-all.c:134:
+//    WHvRegisterPendingInterruption,

ERROR: do not use C99 // comments
#168: FILE: target/i386/whpx-all.c:135:
+//    WHvRegisterInterruptState,

ERROR: do not use C99 // comments
#169: FILE: target/i386/whpx-all.c:136:
+//    WHvRegisterPendingEvent0,

ERROR: do not use C99 // comments
#170: FILE: target/i386/whpx-all.c:137:
+//    WHvRegisterPendingEvent1

ERROR: do not use C99 // comments
#171: FILE: target/i386/whpx-all.c:138:
+//    WHvX64RegisterDeliverabilityNotifications,

ERROR: "foo* bar" should be "foo *bar"
#199: FILE: target/i386/whpx-all.c:166:
+static struct whpx_vcpu* get_whpx_vcpu(CPUState* cpu)

ERROR: "(foo*)" should be "(foo *)"
#201: FILE: target/i386/whpx-all.c:168:
+    return (struct whpx_vcpu*)cpu->hax_vcpu;

WARNING: line over 80 characters
#204: FILE: target/i386/whpx-all.c:171:
+static WHV_X64_SEGMENT_REGISTER whpx_seg_q2h(const SegmentCache *qs, int v86, int r86)

ERROR: do not use C99 // comments
#232: FILE: target/i386/whpx-all.c:199:
+            //hs.Base &= 0xfffff;

ERROR: "foo* bar" should be "foo *bar"
#273: FILE: target/i386/whpx-all.c:240:
+static void whpx_set_registers(CPUState* cpu)

ERROR: "foo* bar" should be "foo *bar"
#276: FILE: target/i386/whpx-all.c:243:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "(foo*)" should be "(foo *)"
#277: FILE: target/i386/whpx-all.c:244:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

ERROR: do not use C99 // comments
#356: FILE: target/i386/whpx-all.c:323:
+        //vcxt.values[idx].Fp.AsUINT128.High64 = env->fpregs[i].mmx.MMX_Q(1);

WARNING: line over 80 characters
#422: FILE: target/i386/whpx-all.c:389:
+        error_report("WHPX: Failed to set virtual processor context, hr=%08lx", hr);

ERROR: "foo* bar" should be "foo *bar"
#429: FILE: target/i386/whpx-all.c:396:
+static void whpx_get_registers(CPUState* cpu)

ERROR: "foo* bar" should be "foo *bar"
#432: FILE: target/i386/whpx-all.c:399:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "(foo*)" should be "(foo *)"
#433: FILE: target/i386/whpx-all.c:400:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

WARNING: line over 80 characters
#448: FILE: target/i386/whpx-all.c:415:
+        error_report("WHPX: Failed to get virtual processor context, hr=%08lx", hr);

ERROR: do not use C99 // comments
#511: FILE: target/i386/whpx-all.c:478:
+        //env->fpregs[i].mmx.MMX_Q(1) = vcxt.values[idx].Fp.AsUINT128.High64;

WARNING: line over 80 characters
#575: FILE: target/i386/whpx-all.c:542:
+                                                WHV_EMULATOR_IO_ACCESS_INFO* IoAccess)

ERROR: "foo* bar" should be "foo *bar"
#575: FILE: target/i386/whpx-all.c:542:
+                                                WHV_EMULATOR_IO_ACCESS_INFO* IoAccess)

ERROR: line over 90 characters
#579: FILE: target/i386/whpx-all.c:546:
+                     (uint8_t*)&IoAccess->Data, IoAccess->AccessSize, IoAccess->Direction);

ERROR: "(foo*)" should be "(foo *)"
#579: FILE: target/i386/whpx-all.c:546:
+                     (uint8_t*)&IoAccess->Data, IoAccess->AccessSize, IoAccess->Direction);

WARNING: line over 80 characters
#584: FILE: target/i386/whpx-all.c:551:
+                                               WHV_EMULATOR_MEMORY_ACCESS_INFO* ma)

ERROR: "foo* bar" should be "foo *bar"
#584: FILE: target/i386/whpx-all.c:551:
+                                               WHV_EMULATOR_MEMORY_ACCESS_INFO* ma)

WARNING: line over 80 characters
#586: FILE: target/i386/whpx-all.c:553:
+    cpu_physical_memory_rw(ma->GpaAddress, ma->Data, ma->AccessSize, ma->Direction);

WARNING: line over 80 characters
#591: FILE: target/i386/whpx-all.c:558:
+                                                const WHV_REGISTER_NAME* RegisterNames,

ERROR: "foo* bar" should be "foo *bar"
#591: FILE: target/i386/whpx-all.c:558:
+                                                const WHV_REGISTER_NAME* RegisterNames,

WARNING: line over 80 characters
#593: FILE: target/i386/whpx-all.c:560:
+                                                WHV_REGISTER_VALUE* RegisterValues)

ERROR: "foo* bar" should be "foo *bar"
#593: FILE: target/i386/whpx-all.c:560:
+                                                WHV_REGISTER_VALUE* RegisterValues)

ERROR: "(foo*)" should be "(foo *)"
#597: FILE: target/i386/whpx-all.c:564:
+    CPUState* cpu = (CPUState*)ctx;

WARNING: line over 80 characters
#603: FILE: target/i386/whpx-all.c:570:
+        error_report("WHPX: Failed to get virtual processor registers, hr=%08lx", hr);

WARNING: line over 80 characters
#611: FILE: target/i386/whpx-all.c:578:
+                                                const WHV_REGISTER_NAME* RegisterNames,

ERROR: "foo* bar" should be "foo *bar"
#611: FILE: target/i386/whpx-all.c:578:
+                                                const WHV_REGISTER_NAME* RegisterNames,

WARNING: line over 80 characters
#613: FILE: target/i386/whpx-all.c:580:
+                                                const WHV_REGISTER_VALUE* RegisterValues)

ERROR: "foo* bar" should be "foo *bar"
#613: FILE: target/i386/whpx-all.c:580:
+                                                const WHV_REGISTER_VALUE* RegisterValues)

ERROR: "(foo*)" should be "(foo *)"
#617: FILE: target/i386/whpx-all.c:584:
+    CPUState* cpu = (CPUState*)ctx;

WARNING: line over 80 characters
#623: FILE: target/i386/whpx-all.c:590:
+        error_report("WHPX: Failed to set virtual processor registers, hr=%08lx", hr);

WARNING: line over 80 characters
#631: FILE: target/i386/whpx-all.c:598:
+                                                   WHV_GUEST_VIRTUAL_ADDRESS Gva,

WARNING: line over 80 characters
#632: FILE: target/i386/whpx-all.c:599:
+                                                   WHV_TRANSLATE_GVA_FLAGS TranslateFlags,

ERROR: line over 90 characters
#633: FILE: target/i386/whpx-all.c:600:
+                                                   WHV_TRANSLATE_GVA_RESULT_CODE* TranslationResult,

ERROR: "foo* bar" should be "foo *bar"
#633: FILE: target/i386/whpx-all.c:600:
+                                                   WHV_TRANSLATE_GVA_RESULT_CODE* TranslationResult,

WARNING: line over 80 characters
#634: FILE: target/i386/whpx-all.c:601:
+                                                   WHV_GUEST_PHYSICAL_ADDRESS* Gpa)

ERROR: "foo* bar" should be "foo *bar"
#634: FILE: target/i386/whpx-all.c:601:
+                                                   WHV_GUEST_PHYSICAL_ADDRESS* Gpa)

ERROR: "(foo*)" should be "(foo *)"
#638: FILE: target/i386/whpx-all.c:605:
+    CPUState* cpu = (CPUState*)ctx;

ERROR: "foo* bar" should be "foo *bar"
#663: FILE: target/i386/whpx-all.c:630:
+                                            WHV_VP_EXIT_CONTEXT* vp_ctx,

ERROR: "foo* bar" should be "foo *bar"
#664: FILE: target/i386/whpx-all.c:631:
+                                            uint8_t* bytes, uint8_t* count,

ERROR: "foo* bar" should be "foo *bar"
#714: FILE: target/i386/whpx-all.c:681:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

WARNING: line over 80 characters
#738: FILE: target/i386/whpx-all.c:705:
+static int whpx_handle_portio(CPUState *cpu, WHV_X64_IO_PORT_ACCESS_CONTEXT* ctx)

ERROR: "foo* bar" should be "foo *bar"
#741: FILE: target/i386/whpx-all.c:708:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "(foo*)" should be "(foo *)"
#767: FILE: target/i386/whpx-all.c:734:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

ERROR: "foo* bar" should be "foo *bar"
#787: FILE: target/i386/whpx-all.c:754:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "(foo*)" should be "(foo *)"
#788: FILE: target/i386/whpx-all.c:755:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

WARNING: line over 80 characters
#862: FILE: target/i386/whpx-all.c:829:
+        reg_values[reg_count].DeliverabilityNotifications.InterruptNotification = 1;

ERROR: line over 90 characters
#864: FILE: target/i386/whpx-all.c:831:
+    if (vcpu->window_registered != reg_values[reg_count].DeliverabilityNotifications.InterruptNotification) {

ERROR: line over 90 characters
#865: FILE: target/i386/whpx-all.c:832:
+        vcpu->window_registered = reg_values[reg_count].DeliverabilityNotifications.InterruptNotification;

WARNING: line over 80 characters
#876: FILE: target/i386/whpx-all.c:843:
+            error_report("WHPX: Failed o set interrupt state registers, hr=%08lx", hr);

ERROR: "foo* bar" should be "foo *bar"
#888: FILE: target/i386/whpx-all.c:855:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "(foo*)" should be "(foo *)"
#889: FILE: target/i386/whpx-all.c:856:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

WARNING: line over 80 characters
#902: FILE: target/i386/whpx-all.c:869:
+        error_report("WHPX: Failed to get interrupt state regusters, hr=%08lx", hr);

ERROR: "(foo*)" should be "(foo *)"
#930: FILE: target/i386/whpx-all.c:897:
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);

ERROR: "foo* bar" should be "foo *bar"
#932: FILE: target/i386/whpx-all.c:899:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "foo* bar" should be "foo *bar"
#976: FILE: target/i386/whpx-all.c:943:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

WARNING: line over 80 characters
#1007: FILE: target/i386/whpx-all.c:974:
+                WHvCancelRunVirtualProcessor(whpx->partition, cpu->cpu_index, 0);

WARNING: line over 80 characters
#1014: FILE: target/i386/whpx-all.c:981:
+            error_report("WHPX: Failed to exec a virtual processor, hr=%08lx", hr);

WARNING: line over 80 characters
#1051: FILE: target/i386/whpx-all.c:1018:
+            error_report("WHPX: Unexpected VP exit code %d", vcpu->exit_ctx.ExitReason);

WARNING: line over 80 characters
#1083: FILE: target/i386/whpx-all.c:1050:
+static void do_whpx_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)

WARNING: line over 80 characters
#1089: FILE: target/i386/whpx-all.c:1056:
+static void do_whpx_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg)

ERROR: "foo* bar" should be "foo *bar"
#1128: FILE: target/i386/whpx-all.c:1095:
+    struct whpx_vcpu* vcpu;

ERROR: line over 90 characters
#1140: FILE: target/i386/whpx-all.c:1107:
+        error_report("WHPX: Failed to setup instruction completion support, hr=%08lx", hr);

WARNING: line over 80 characters
#1147: FILE: target/i386/whpx-all.c:1114:
+        error_report("WHPX: Failed to create a virtual processor, hr=%08lx", hr);

ERROR: "(foo*)" should be "(foo *)"
#1156: FILE: target/i386/whpx-all.c:1123:
+    cpu->hax_vcpu = (struct hax_vcpu_state*)vcpu;

ERROR: "foo* bar" should be "foo *bar"
#1187: FILE: target/i386/whpx-all.c:1154:
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);

ERROR: "foo* bar" should be "foo *bar"
#1205: FILE: target/i386/whpx-all.c:1172:
+static void whpx_update_mapping(hwaddr start_pa, ram_addr_t size, void* host_va,

ERROR: "foo* bar" should be "foo *bar"
#1206: FILE: target/i386/whpx-all.c:1173:
+                               int add, int rom, const char* name)

ERROR: "(foo*)" should be "(foo *)"
#1240: FILE: target/i386/whpx-all.c:1207:
+                     (void*)start_pa, (void*)size, host_va, hr);

ERROR: "(foo*)" should be "(foo *)"
#1271: FILE: target/i386/whpx-all.c:1238:
+    whpx_update_mapping(start_pa, size, (void*)host_va, add,

ERROR: line over 90 characters
#1349: FILE: target/i386/whpx-all.c:1316:
+    hr = WHvGetCapability(WHvCapabilityCodeHypervisorPresent, &whpx_cap, sizeof(whpx_cap));

total: 68 errors, 25 warnings, 1399 lines checked

Your patch has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.

Checking PATCH 4/4: Add the WHPX acceleration enlightenments...
=== OUTPUT END ===

Test command exited with code: 1


---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to patchew-devel@freelists.org
[Qemu-devel] [PATCH 1/4] Add the Windows Hypervisor Platform accelerator.
Posted by Justin Terry (VM) via Qemu-devel, 5 days ago
Introduces the configure support for the new Windows Hypervisor Platform that
allows for hypervisor acceleration from usermode components on the Windows
platform.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
---
 configure       | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 qemu-options.hx |  8 ++++----
 2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index 89bd662..8d5a065 100755
--- a/configure
+++ b/configure
@@ -222,6 +222,17 @@ supported_hvf_target() {
     return 1
 }
 
+supported_whpx_target() {
+    test "$whpx" = "yes" || return 1
+    glob "$1" "*-softmmu" || return 1
+    case "${1%-softmmu}" in
+        i386|x86_64)
+            return 0
+        ;;
+    esac
+    return 1
+}
+
 supported_target() {
     case "$1" in
         *-softmmu)
@@ -248,6 +259,7 @@ supported_target() {
     supported_xen_target "$1" && return 0
     supported_hax_target "$1" && return 0
     supported_hvf_target "$1" && return 0
+    supported_whpx_target "$1" && return 0
     print_error "TCG disabled, but hardware accelerator not available for '$target'"
     return 1
 }
@@ -338,6 +350,7 @@ vhost_user=""
 kvm="no"
 hax="no"
 hvf="no"
+whpx="no"
 rdma=""
 gprof="no"
 debug_tcg="no"
@@ -1054,6 +1067,10 @@ for opt do
   ;;
   --enable-hvf) hvf="yes"
   ;;
+  --disable-whpx) whpx="no"
+  ;;
+  --enable-whpx) whpx="yes"
+  ;;
   --disable-tcg-interpreter) tcg_interpreter="no"
   ;;
   --enable-tcg-interpreter) tcg_interpreter="yes"
@@ -1548,6 +1565,7 @@ disabled with --disable-FEATURE, default is enabled if available:
   kvm             KVM acceleration support
   hax             HAX acceleration support
   hvf             Hypervisor.framework acceleration support
+  whpx            Windows Hypervisor Platform acceleration support
   rdma            RDMA-based migration support
   vde             support for vde network
   netmap          support for netmap network
@@ -2446,6 +2464,30 @@ if test "$xen_pv_domain_build" = "yes" &&
 fi
 
 ##########################################
+# Windows Hypervisor Platform accelerator (WHPX) check
+if test "$whpx" != "no" ; then
+    cat > $TMPC << EOF
+#include <windows.h>
+#include <winhvplatform.h>
+#include <winhvemulation.h>
+int main(void) {
+    WHV_CAPABILITY whpx_cap;
+    WHvGetCapability(WHvCapabilityCodeFeatures, &whpx_cap, sizeof(whpx_cap));
+    return 0;
+}
+EOF
+    if compile_prog "" "-lwinhvplatform -lwinhvemulation" ; then
+        libs_softmmu="$libs_softmmu -lwinhvplatform -lwinhvemulation"
+        whpx="yes"
+    else
+        if test "$whpx" = "yes"; then
+            feature_not_found "winhvplatform" "winhvemulation is not installed"
+        fi
+        whpx="no"
+    fi
+fi
+
+##########################################
 # Sparse probe
 if test "$sparse" != "no" ; then
   if has cgcc; then
@@ -5565,6 +5607,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "HAX support       $hax"
 echo "HVF support       $hvf"
+echo "WHPX support      $whpx"
 echo "TCG support       $tcg"
 if test "$tcg" = "yes" ; then
     echo "TCG debug enabled $debug_tcg"
@@ -5719,7 +5762,7 @@ if test "$mingw32" = "yes" ; then
     echo "CONFIG_QGA_NTDDDISK=y" >> $config_host_mak
   fi
   if test "$guest_agent_msi" = "yes"; then
-    echo "QEMU_GA_MSI_ENABLED=yes" >> $config_host_mak  
+    echo "QEMU_GA_MSI_ENABLED=yes" >> $config_host_mak
     echo "QEMU_GA_MSI_MINGW_DLL_PATH=${QEMU_GA_MSI_MINGW_DLL_PATH}" >> $config_host_mak
     echo "QEMU_GA_MSI_WITH_VSS=${QEMU_GA_MSI_WITH_VSS}" >> $config_host_mak
     echo "QEMU_GA_MSI_ARCH=${QEMU_GA_MSI_ARCH}" >> $config_host_mak
@@ -6641,6 +6684,9 @@ fi
 if supported_hvf_target $target; then
     echo "CONFIG_HVF=y" >> $config_target_mak
 fi
+if supported_whpx_target $target; then
+    echo "CONFIG_WHPX=y" >> $config_target_mak
+fi
 if test "$target_bigendian" = "yes" ; then
   echo "TARGET_WORDS_BIGENDIAN=y" >> $config_target_mak
 fi
diff --git a/qemu-options.hx b/qemu-options.hx
index 678181c..8c8f132 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -31,7 +31,7 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "-machine [type=]name[,prop[=value][,...]]\n"
     "                selects emulated machine ('-machine help' for list)\n"
     "                property accel=accel1[:accel2[:...]] selects accelerator\n"
-    "                supported accelerators are kvm, xen, hax, hvf or tcg (default: tcg)\n"
+    "                supported accelerators are kvm, xen, hax, hvf, whpx or tcg (default: tcg)\n"
     "                kernel_irqchip=on|off|split controls accelerated irqchip support (default=off)\n"
     "                vmport=on|off|auto controls emulation of vmport (default: auto)\n"
     "                kvm_shadow_mem=size of KVM shadow MMU in bytes\n"
@@ -66,7 +66,7 @@ Supported machine properties are:
 @table @option
 @item accel=@var{accels1}[:@var{accels2}[:...]]
 This is used to enable an accelerator. Depending on the target architecture,
-kvm, xen, hax, hvf or tcg can be available. By default, tcg is used. If there is
+kvm, xen, hax, hvf, whpx or tcg can be available. By default, tcg is used. If there is
 more than one accelerator specified, the next one is used if the previous one
 fails to initialize.
 @item kernel_irqchip=on|off
@@ -126,13 +126,13 @@ ETEXI
 
 DEF("accel", HAS_ARG, QEMU_OPTION_accel,
     "-accel [accel=]accelerator[,thread=single|multi]\n"
-    "                select accelerator (kvm, xen, hax, hvf or tcg; use 'help' for a list)\n"
+    "                select accelerator (kvm, xen, hax, hvf, whpx or tcg; use 'help' for a list)\n"
     "                thread=single|multi (enable multi-threaded TCG)", QEMU_ARCH_ALL)
 STEXI
 @item -accel @var{name}[,prop=@var{value}[,...]]
 @findex -accel
 This is used to enable an accelerator. Depending on the target architecture,
-kvm, xen, hax, hvf or tcg can be available. By default, tcg is used. If there is
+kvm, xen, hax, hvf, whpx or tcg can be available. By default, tcg is used. If there is
 more than one accelerator specified, the next one is used if the previous one
 fails to initialize.
 @table @option
-- 
2.7.4


[Qemu-devel] [PATCH 2/4] Add the WHPX vcpu API
Posted by Justin Terry (VM) via Qemu-devel, 5 days ago
Adds support for the Windows Hypervisor Platform accelerator (WHPX) stubs and
introduces the whpx.h sysemu API for managing the vcpu scheduling and
management.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
---
 accel/stubs/Makefile.objs |  9 +++++----
 accel/stubs/whpx-stub.c   | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/whpx.h     | 40 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 accel/stubs/whpx-stub.c
 create mode 100644 include/sysemu/whpx.h

diff --git a/accel/stubs/Makefile.objs b/accel/stubs/Makefile.objs
index 779343b..3894caf 100644
--- a/accel/stubs/Makefile.objs
+++ b/accel/stubs/Makefile.objs
@@ -1,4 +1,5 @@
-obj-$(call lnot,$(CONFIG_HAX)) += hax-stub.o
-obj-$(call lnot,$(CONFIG_HVF)) += hvf-stub.o
-obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
-obj-$(call lnot,$(CONFIG_TCG)) += tcg-stub.o
+obj-$(call lnot,$(CONFIG_HAX))  += hax-stub.o
+obj-$(call lnot,$(CONFIG_HVF))  += hvf-stub.o
+obj-$(call lnot,$(CONFIG_WHPX)) += whpx-stub.o
+obj-$(call lnot,$(CONFIG_KVM))  += kvm-stub.o
+obj-$(call lnot,$(CONFIG_TCG))  += tcg-stub.o
diff --git a/accel/stubs/whpx-stub.c b/accel/stubs/whpx-stub.c
new file mode 100644
index 0000000..c1d2451
--- /dev/null
+++ b/accel/stubs/whpx-stub.c
@@ -0,0 +1,48 @@
+/*
+ * QEMU Windows Hypervisor Platform accelerator (WHPX) stub
+ *
+ * Copyright Microsoft Corp. 2017
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "cpu.h"
+#include "sysemu/whpx.h"
+
+int whpx_init_vcpu(CPUState *cpu)
+{
+    return -1;
+}
+
+int whpx_vcpu_exec(CPUState *cpu)
+{
+    return -1;
+}
+
+void whpx_destroy_vcpu(CPUState *cpu)
+{
+}
+
+void whpx_vcpu_kick(CPUState *cpu)
+{
+}
+
+void whpx_cpu_synchronize_state(CPUState *cpu)
+{
+}
+
+void whpx_cpu_synchronize_post_reset(CPUState *cpu)
+{
+}
+
+void whpx_cpu_synchronize_post_init(CPUState *cpu)
+{
+}
+
+void whpx_cpu_synchronize_pre_loadvm(CPUState *cpu)
+{
+}
diff --git a/include/sysemu/whpx.h b/include/sysemu/whpx.h
new file mode 100644
index 0000000..49e6663
--- /dev/null
+++ b/include/sysemu/whpx.h
@@ -0,0 +1,40 @@
+/*
+ * QEMU Windows Hypervisor Platform accelerator (WHPX) support
+ *
+ * Copyright Microsoft, Corp. 2017
+ *
+ * Authors:
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_WHPX_H
+#define QEMU_WHPX_H
+
+#include "config-host.h"
+#include "qemu-common.h"
+
+int whpx_init_vcpu(CPUState *cpu);
+int whpx_vcpu_exec(CPUState *cpu);
+void whpx_destroy_vcpu(CPUState *cpu);
+void whpx_vcpu_kick(CPUState *cpu);
+
+
+void whpx_cpu_synchronize_state(CPUState *cpu);
+void whpx_cpu_synchronize_post_reset(CPUState *cpu);
+void whpx_cpu_synchronize_post_init(CPUState *cpu);
+void whpx_cpu_synchronize_pre_loadvm(CPUState *cpu);
+
+#ifdef CONFIG_WHPX
+
+int whpx_enabled(void);
+
+#else /* CONFIG_WHPX */
+
+#define whpx_enabled() (0)
+
+#endif /* CONFIG_WHPX */
+
+#endif /* QEMU_WHPX_H */
\ No newline at end of file
-- 
2.7.4


[Qemu-devel] [PATCH 3/4] Introduce the WHPX impl
Posted by Justin Terry (VM) via Qemu-devel, 5 days ago
Implements the Windows Hypervisor Platform accelerator (WHPX) target. Which
acts as a hypervisor accelerator for QEMU on the Windows platform. This enables
QEMU much greater speed over the emulated x86_64 path's that are taken on
Windows today.

1. Adds support for vPartition management.
2. Adds support for vCPU management.
3. Adds support for MMIO/PortIO.
4. Registers the WHPX ACCEL_CLASS.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
---
 target/i386/Makefile.objs |    1 +
 target/i386/whpx-all.c    | 1395 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1396 insertions(+)
 create mode 100644 target/i386/whpx-all.c

diff --git a/target/i386/Makefile.objs b/target/i386/Makefile.objs
index 44103a6..f5c6ef2 100644
--- a/target/i386/Makefile.objs
+++ b/target/i386/Makefile.objs
@@ -14,3 +14,4 @@ ifdef CONFIG_DARWIN
 obj-$(CONFIG_HAX) += hax-all.o hax-mem.o hax-darwin.o
 obj-$(CONFIG_HVF) += hvf/
 endif
+obj-$(CONFIG_WHPX) += whpx-all.o
diff --git a/target/i386/whpx-all.c b/target/i386/whpx-all.c
new file mode 100644
index 0000000..9829303
--- /dev/null
+++ b/target/i386/whpx-all.c
@@ -0,0 +1,1395 @@
+/*
+ * QEMU Windows Hypervisor Platform accelerator (WHPX)
+ *
+ * Copyright Microsoft Corp. 2017
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "cpu.h"
+#include "exec/address-spaces.h"
+#include "exec/exec-all.h"
+#include "exec/ioport.h"
+#include "qemu-common.h"
+#include "strings.h"
+#include "sysemu/accel.h"
+#include "sysemu/whpx.h"
+#include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
+#include "qemu/main-loop.h"
+#include "hw/boards.h"
+#include "qemu/error-report.h"
+#include "qemu/queue.h"
+
+#include <winhvplatform.h>
+#include <winhvemulation.h>
+
+struct whpx_state {
+    uint64_t mem_quota;
+    WHV_PARTITION_HANDLE partition;
+    uint32_t exit_ctx_size;
+};
+
+static const WHV_REGISTER_NAME whpx_register_names[] = {
+
+    // X64 General purpose registers
+    WHvX64RegisterRax,
+    WHvX64RegisterRcx,
+    WHvX64RegisterRdx,
+    WHvX64RegisterRbx,
+    WHvX64RegisterRsp,
+    WHvX64RegisterRbp,
+    WHvX64RegisterRsi,
+    WHvX64RegisterRdi,
+    WHvX64RegisterR8,
+    WHvX64RegisterR9,
+    WHvX64RegisterR10,
+    WHvX64RegisterR11,
+    WHvX64RegisterR12,
+    WHvX64RegisterR13,
+    WHvX64RegisterR14,
+    WHvX64RegisterR15,
+    WHvX64RegisterRip,
+    WHvX64RegisterRflags,
+
+    // X64 Segment registers
+    WHvX64RegisterEs,
+    WHvX64RegisterCs,
+    WHvX64RegisterSs,
+    WHvX64RegisterDs,
+    WHvX64RegisterFs,
+    WHvX64RegisterGs,
+    WHvX64RegisterLdtr,
+    WHvX64RegisterTr,
+
+    // X64 Table registers
+    WHvX64RegisterIdtr,
+    WHvX64RegisterGdtr,
+
+    // X64 Control Registers
+    WHvX64RegisterCr0,
+    WHvX64RegisterCr2,
+    WHvX64RegisterCr3,
+    WHvX64RegisterCr4,
+    WHvX64RegisterCr8,
+
+    // X64 Debug Registers
+//    WHvX64RegisterDr0,
+//    WHvX64RegisterDr1,
+//    WHvX64RegisterDr2,
+//    WHvX64RegisterDr3,
+//    WHvX64RegisterDr6,
+//    WHvX64RegisterDr7,
+
+    // X64 Floating Point and Vector Registers
+    WHvX64RegisterXmm0,
+    WHvX64RegisterXmm1,
+    WHvX64RegisterXmm2,
+    WHvX64RegisterXmm3,
+    WHvX64RegisterXmm4,
+    WHvX64RegisterXmm5,
+    WHvX64RegisterXmm6,
+    WHvX64RegisterXmm7,
+    WHvX64RegisterXmm8,
+    WHvX64RegisterXmm9,
+    WHvX64RegisterXmm10,
+    WHvX64RegisterXmm11,
+    WHvX64RegisterXmm12,
+    WHvX64RegisterXmm13,
+    WHvX64RegisterXmm14,
+    WHvX64RegisterXmm15,
+    WHvX64RegisterFpMmx0,
+    WHvX64RegisterFpMmx1,
+    WHvX64RegisterFpMmx2,
+    WHvX64RegisterFpMmx3,
+    WHvX64RegisterFpMmx4,
+    WHvX64RegisterFpMmx5,
+    WHvX64RegisterFpMmx6,
+    WHvX64RegisterFpMmx7,
+    WHvX64RegisterFpControlStatus,
+    WHvX64RegisterXmmControlStatus,
+
+    // X64 MSRs
+    WHvX64RegisterTsc,
+    WHvX64RegisterEfer,
+#ifdef TARGET_X86_64
+    WHvX64RegisterKernelGsBase,
+#endif
+    WHvX64RegisterApicBase,
+//    WHvX64RegisterPat,
+    WHvX64RegisterSysenterCs,
+    WHvX64RegisterSysenterEip,
+    WHvX64RegisterSysenterEsp,
+    WHvX64RegisterStar,
+#ifdef TARGET_X86_64
+    WHvX64RegisterLstar,
+    WHvX64RegisterCstar,
+    WHvX64RegisterSfmask,
+#endif
+
+    // Interrupt / Event Registers
+//    WHvRegisterPendingInterruption,
+//    WHvRegisterInterruptState,
+//    WHvRegisterPendingEvent0,
+//    WHvRegisterPendingEvent1
+//    WHvX64RegisterDeliverabilityNotifications,
+};
+
+struct whpx_register_set {
+    WHV_REGISTER_VALUE values[RTL_NUMBER_OF(whpx_register_names)];
+};
+
+struct whpx_vcpu {
+    WHV_EMULATOR_HANDLE emulator;
+    bool window_registered;
+    bool interruptable;
+    uint64_t tpr;
+    uint64_t apic_base;
+    WHV_X64_PENDING_INTERRUPTION_REGISTER interrupt_in_flight;
+
+    /* Must be the last field as it may have a tail */
+    WHV_RUN_VP_EXIT_CONTEXT exit_ctx;
+};
+
+static bool whpx_allowed;
+
+struct whpx_state whpx_global;
+
+
+/*
+ * VP support
+ */
+
+static struct whpx_vcpu* get_whpx_vcpu(CPUState* cpu)
+{
+    return (struct whpx_vcpu*)cpu->hax_vcpu;
+}
+
+static WHV_X64_SEGMENT_REGISTER whpx_seg_q2h(const SegmentCache *qs, int v86, int r86)
+{
+    WHV_X64_SEGMENT_REGISTER hs;
+    unsigned flags = qs->flags;
+
+    hs.Base = qs->base;
+    hs.Limit = qs->limit;
+    hs.Selector = qs->selector;
+
+    if (v86) {
+        hs.Attributes = 0;
+        hs.SegmentType = 3;
+        hs.Present = 1;
+        hs.DescriptorPrivilegeLevel = 3;
+        hs.NonSystemSegment = 1;
+
+    } else {
+        hs.SegmentType = (flags >> DESC_TYPE_SHIFT) & 15;
+        hs.NonSystemSegment = (flags & DESC_S_MASK) != 0;
+        hs.DescriptorPrivilegeLevel = (flags & DESC_DPL_MASK) != 0;
+        hs.Present = (flags & DESC_P_MASK) != 0;
+        hs.Reserved = 0;
+        hs.Available = (flags & DESC_AVL_MASK) != 0;
+        hs.Long = (flags >> DESC_L_SHIFT) & 1;
+        hs.Default = (flags >> DESC_B_SHIFT) & 1;
+        hs.Granularity = (flags & DESC_G_MASK) != 0;
+
+        if (r86) {
+            //hs.Base &= 0xfffff;
+        }
+    }
+
+    return hs;
+}
+
+static SegmentCache whpx_seg_h2q(const WHV_X64_SEGMENT_REGISTER *hs)
+{
+    SegmentCache qs;
+
+    qs.base = hs->Base;
+    qs.limit = hs->Limit;
+    qs.selector = hs->Selector;
+
+    qs.flags = hs->SegmentType << DESC_TYPE_SHIFT;
+    if (hs->NonSystemSegment) {
+        qs.flags |= DESC_S_MASK;
+    }
+    if (hs->DescriptorPrivilegeLevel) {
+        qs.flags |= DESC_DPL_MASK;
+    }
+    if (hs->Present) {
+        qs.flags |= DESC_P_MASK;
+    }
+    if (hs->Available) {
+        qs.flags |= DESC_AVL_MASK;
+    }
+    if (hs->Long) {
+        qs.flags |= DESC_L_MASK;
+    }
+    if (hs->Default) {
+        qs.flags |= DESC_B_MASK;
+    }
+    if (hs->Granularity) {
+        qs.flags |= DESC_G_MASK;
+    }
+
+    return qs;
+}
+
+static void whpx_set_registers(CPUState* cpu)
+{
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    struct whpx_register_set vcxt = {0};
+    HRESULT hr;
+    int idx = 0;
+    int i;
+    int v86, r86;
+
+    assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
+
+    v86 = (env->eflags & VM_MASK);
+    r86 = !(env->cr[0] & CR0_PE_MASK);
+
+    vcpu->tpr = cpu_get_apic_tpr(x86_cpu->apic_state);
+    vcpu->apic_base = cpu_get_apic_base(x86_cpu->apic_state);
+
+    /* Indexes for first 16 registers match between HV and QEMU definitions */
+    for (idx = 0; idx < CPU_NB_REGS64; idx += 1) {
+        vcxt.values[idx].Reg64 = env->regs[idx];
+    }
+
+    /* Same goes for RIP and RFLAGS */
+    assert(whpx_register_names[idx] == WHvX64RegisterRip);
+    vcxt.values[idx++].Reg64 = env->eip;
+
+    assert(whpx_register_names[idx] == WHvX64RegisterRflags);
+    vcxt.values[idx++].Reg64 = env->eflags;
+
+    /* Translate 6+4 segment registers. HV and QEMU order matches  */
+    assert(idx == WHvX64RegisterEs);
+    for (i = 0; i < 6; i += 1, idx += 1) {
+        vcxt.values[idx].Segment = whpx_seg_q2h(&env->segs[i], v86, r86);
+    }
+
+    assert(idx == WHvX64RegisterLdtr);
+    vcxt.values[idx++].Segment = whpx_seg_q2h(&env->ldt, 0, 0);
+
+    assert(idx == WHvX64RegisterTr);
+    vcxt.values[idx++].Segment = whpx_seg_q2h(&env->tr, 0, 0);
+
+    assert(idx == WHvX64RegisterIdtr);
+    vcxt.values[idx].Segment.Base = env->idt.base;
+    vcxt.values[idx].Segment.Limit = env->idt.limit;
+    vcxt.values[idx].Segment.Selector = 0;
+    vcxt.values[idx].Segment.Attributes = 0;
+    idx += 1;
+
+    assert(idx == WHvX64RegisterGdtr);
+    vcxt.values[idx].Segment.Base = env->gdt.base;
+    vcxt.values[idx].Segment.Limit = env->gdt.limit;
+    vcxt.values[idx].Segment.Selector = 0;
+    vcxt.values[idx].Segment.Attributes = 0;
+    idx += 1;
+
+    /* CR0, 2, 3, 4, 8 */
+    assert(whpx_register_names[idx] == WHvX64RegisterCr0);
+    vcxt.values[idx++].Reg64 = env->cr[0];
+    assert(whpx_register_names[idx] == WHvX64RegisterCr2);
+    vcxt.values[idx++].Reg64 = env->cr[2];
+    assert(whpx_register_names[idx] == WHvX64RegisterCr3);
+    vcxt.values[idx++].Reg64 = env->cr[3];
+    assert(whpx_register_names[idx] == WHvX64RegisterCr4);
+    vcxt.values[idx++].Reg64 = env->cr[4];
+    assert(whpx_register_names[idx] == WHvX64RegisterCr8);
+    vcxt.values[idx++].Reg64 = vcpu->tpr;
+
+    /* 8 Debug Registers - Skipped */
+
+    /* 16 XMM registers */
+    assert(whpx_register_names[idx] == WHvX64RegisterXmm0);
+    for (i = 0; i < 16; i += 1, idx += 1) {
+        vcxt.values[idx].Reg128.Low64 = env->xmm_regs[i].ZMM_Q(0);
+        vcxt.values[idx].Reg128.High64 = env->xmm_regs[i].ZMM_Q(1);
+    }
+
+    /* 8 FP registers */
+    assert(whpx_register_names[idx] == WHvX64RegisterFpMmx0);
+    for (i = 0; i < 8; i += 1, idx += 1) {
+        vcxt.values[idx].Fp.AsUINT128.Low64 = env->fpregs[i].mmx.MMX_Q(0);
+        //vcxt.values[idx].Fp.AsUINT128.High64 = env->fpregs[i].mmx.MMX_Q(1);
+    }
+
+    /* FP control status register */
+    assert(whpx_register_names[idx] == WHvX64RegisterFpControlStatus);
+    vcxt.values[idx].FpControlStatus.FpControl = env->fpuc;
+    vcxt.values[idx].FpControlStatus.FpStatus =
+        (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
+    vcxt.values[idx].FpControlStatus.FpTag = 0;
+    for (i = 0; i < 8; ++i) {
+        vcxt.values[idx].FpControlStatus.FpTag |= (!env->fptags[i]) << i;
+    }
+    vcxt.values[idx].FpControlStatus.Reserved = 0;
+    vcxt.values[idx].FpControlStatus.LastFpOp = env->fpop;
+    vcxt.values[idx].FpControlStatus.LastFpRip = env->fpip;
+    idx += 1;
+
+    /* XMM control status register */
+    assert(whpx_register_names[idx] == WHvX64RegisterXmmControlStatus);
+    vcxt.values[idx].XmmControlStatus.LastFpRdp = 0;
+    vcxt.values[idx].XmmControlStatus.XmmStatusControl = env->mxcsr;
+    vcxt.values[idx].XmmControlStatus.XmmStatusControlMask = 0x0000ffff;
+    idx += 1;
+
+    /* MSRs */
+    assert(whpx_register_names[idx] == WHvX64RegisterTsc);
+    vcxt.values[idx++].Reg64 = env->tsc;
+    assert(whpx_register_names[idx] == WHvX64RegisterEfer);
+    vcxt.values[idx++].Reg64 = env->efer;
+#ifdef TARGET_X86_64
+    assert(whpx_register_names[idx] == WHvX64RegisterKernelGsBase);
+    vcxt.values[idx++].Reg64 = env->kernelgsbase;
+#endif
+
+    assert(whpx_register_names[idx] == WHvX64RegisterApicBase);
+    vcxt.values[idx++].Reg64 = vcpu->apic_base;
+
+    /* WHvX64RegisterPat - Skipped */
+
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterCs);
+    vcxt.values[idx++].Reg64 = env->sysenter_cs;
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterEip);
+    vcxt.values[idx++].Reg64 = env->sysenter_eip;
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterEsp);
+    vcxt.values[idx++].Reg64 = env->sysenter_esp;
+    assert(whpx_register_names[idx] == WHvX64RegisterStar);
+    vcxt.values[idx++].Reg64 = env->star;
+#ifdef TARGET_X86_64
+    assert(whpx_register_names[idx] == WHvX64RegisterLstar);
+    vcxt.values[idx++].Reg64 = env->lstar;
+    assert(whpx_register_names[idx] == WHvX64RegisterCstar);
+    vcxt.values[idx++].Reg64 = env->cstar;
+    assert(whpx_register_names[idx] == WHvX64RegisterSfmask);
+    vcxt.values[idx++].Reg64 = env->fmask;
+#endif
+
+    /* Interrupt / Event Registers - Skipped */
+
+    assert(idx == RTL_NUMBER_OF(whpx_register_names));
+
+    hr = WHvSetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                         whpx_register_names,
+                                         RTL_NUMBER_OF(whpx_register_names),
+                                         &vcxt.values[0]);
+
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to set virtual processor context, hr=%08lx", hr);
+        __debugbreak();
+    }
+
+    return;
+}
+
+static void whpx_get_registers(CPUState* cpu)
+{
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    struct whpx_register_set vcxt;
+    uint64_t tpr, apic_base;
+    HRESULT hr;
+    int idx = 0;
+    int i;
+
+    assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
+
+    hr = WHvGetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                         whpx_register_names,
+                                         RTL_NUMBER_OF(whpx_register_names),
+                                         &vcxt.values[0]);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to get virtual processor context, hr=%08lx", hr);
+        __debugbreak();
+    }
+
+    /* Indexes for first 16 registers match between HV and QEMU definitions */
+    for (idx = 0; idx < CPU_NB_REGS64; idx += 1) {
+        env->regs[idx] = vcxt.values[idx].Reg64;
+    }
+
+    /* Same goes for RIP and RFLAGS */
+    assert(whpx_register_names[idx] == WHvX64RegisterRip);
+    env->eip = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterRflags);
+    env->eflags = vcxt.values[idx++].Reg64;
+
+    /* Translate 6+4 segment registers. HV and QEMU order matches  */
+    assert(idx == WHvX64RegisterEs);
+    for (i = 0; i < 6; i += 1, idx += 1) {
+        env->segs[i] = whpx_seg_h2q(&vcxt.values[idx].Segment);
+    }
+
+    assert(idx == WHvX64RegisterLdtr);
+    env->ldt = whpx_seg_h2q(&vcxt.values[idx++].Segment);
+    assert(idx == WHvX64RegisterTr);
+    env->tr = whpx_seg_h2q(&vcxt.values[idx++].Segment);
+    assert(idx == WHvX64RegisterIdtr);
+    env->idt.base = vcxt.values[idx].Segment.Base;
+    env->idt.limit = vcxt.values[idx].Segment.Limit;
+    idx += 1;
+    assert(idx == WHvX64RegisterGdtr);
+    env->gdt.base = vcxt.values[idx].Segment.Base;
+    env->gdt.limit = vcxt.values[idx].Segment.Limit;
+    idx += 1;
+
+    /* CR0, 2, 3, 4, 8 */
+    assert(whpx_register_names[idx] == WHvX64RegisterCr0);
+    env->cr[0] = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterCr2);
+    env->cr[2] = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterCr3);
+    env->cr[3] = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterCr4);
+    env->cr[4] = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterCr8);
+    tpr = vcxt.values[idx++].Reg64;
+    if (tpr != vcpu->tpr) {
+        vcpu->tpr = tpr;
+        cpu_set_apic_tpr(x86_cpu->apic_state, tpr);
+    }
+
+    /* 8 Debug Registers - Skipped */
+
+    /* 16 XMM registers */
+    assert(whpx_register_names[idx] == WHvX64RegisterXmm0);
+    for (i = 0; i < 16; i += 1, idx += 1) {
+        env->xmm_regs[i].ZMM_Q(0) = vcxt.values[idx].Reg128.Low64;
+        env->xmm_regs[i].ZMM_Q(1) = vcxt.values[idx].Reg128.High64;
+    }
+
+    /* 8 FP registers */
+    assert(whpx_register_names[idx] == WHvX64RegisterFpMmx0);
+    for (i = 0; i < 8; i += 1, idx += 1) {
+        env->fpregs[i].mmx.MMX_Q(0) = vcxt.values[idx].Fp.AsUINT128.Low64;
+        //env->fpregs[i].mmx.MMX_Q(1) = vcxt.values[idx].Fp.AsUINT128.High64;
+    }
+
+    /* FP control status register */
+    assert(whpx_register_names[idx] == WHvX64RegisterFpControlStatus);
+    env->fpuc = vcxt.values[idx].FpControlStatus.FpControl;
+    env->fpstt = (vcxt.values[idx].FpControlStatus.FpStatus >> 11) & 0x7;
+    env->fpus = vcxt.values[idx].FpControlStatus.FpStatus & ~0x3800;
+    for (i = 0; i < 8; ++i) {
+        env->fptags[i] = !((vcxt.values[idx].FpControlStatus.FpTag >> i) & 1);
+    }
+    env->fpop = vcxt.values[idx].FpControlStatus.LastFpOp;
+    env->fpip = vcxt.values[idx].FpControlStatus.LastFpRip;
+    idx += 1;
+
+    /* XMM control status register */
+    assert(whpx_register_names[idx] == WHvX64RegisterXmmControlStatus);
+    env->mxcsr = vcxt.values[idx].XmmControlStatus.XmmStatusControl;
+    idx += 1;
+
+    /* MSRs */
+    assert(whpx_register_names[idx] == WHvX64RegisterTsc);
+    env->tsc = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterEfer);
+    env->efer = vcxt.values[idx++].Reg64;
+#ifdef TARGET_X86_64
+    assert(whpx_register_names[idx] == WHvX64RegisterKernelGsBase);
+    env->kernelgsbase = vcxt.values[idx++].Reg64;
+#endif
+
+    assert(whpx_register_names[idx] == WHvX64RegisterApicBase);
+    apic_base = vcxt.values[idx++].Reg64;
+    if (apic_base != vcpu->apic_base) {
+        vcpu->apic_base = apic_base;
+        cpu_set_apic_base(x86_cpu->apic_state, vcpu->apic_base);
+    }
+
+    /* WHvX64RegisterPat - Skipped */
+
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterCs);
+    env->sysenter_cs = vcxt.values[idx++].Reg64;;
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterEip);
+    env->sysenter_eip = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterSysenterEsp);
+    env->sysenter_esp = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterStar);
+    env->star = vcxt.values[idx++].Reg64;
+#ifdef TARGET_X86_64
+    assert(whpx_register_names[idx] == WHvX64RegisterLstar);
+    env->lstar = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterCstar);
+    env->cstar = vcxt.values[idx++].Reg64;
+    assert(whpx_register_names[idx] == WHvX64RegisterSfmask);
+    env->fmask = vcxt.values[idx++].Reg64;
+#endif
+
+    /* Interrupt / Event Registers - Skipped */
+
+    assert(idx == RTL_NUMBER_OF(whpx_register_names));
+
+    return;
+}
+
+static HRESULT CALLBACK whpx_emu_ioport_callback(void* ctx,
+                                                WHV_EMULATOR_IO_ACCESS_INFO* IoAccess)
+{
+    MemTxAttrs attrs = { 0 };
+    address_space_rw(&address_space_io, IoAccess->Port, attrs,
+                     (uint8_t*)&IoAccess->Data, IoAccess->AccessSize, IoAccess->Direction);
+    return S_OK;
+}
+
+static HRESULT CALLBACK whpx_emu_memio_callback(void* ctx,
+                                               WHV_EMULATOR_MEMORY_ACCESS_INFO* ma)
+{
+    cpu_physical_memory_rw(ma->GpaAddress, ma->Data, ma->AccessSize, ma->Direction);
+    return S_OK;
+}
+
+static HRESULT CALLBACK whpx_emu_getreg_callback(void* ctx,
+                                                const WHV_REGISTER_NAME* RegisterNames,
+                                                UINT32 RegisterCount,
+                                                WHV_REGISTER_VALUE* RegisterValues)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    CPUState* cpu = (CPUState*)ctx;
+
+    hr = WHvGetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                         RegisterNames, RegisterCount,
+                                         RegisterValues);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to get virtual processor registers, hr=%08lx", hr);
+        __debugbreak();
+    }
+
+    return hr;
+}
+
+static HRESULT CALLBACK whpx_emu_setreg_callback(void* ctx,
+                                                const WHV_REGISTER_NAME* RegisterNames,
+                                                UINT32 RegisterCount,
+                                                const WHV_REGISTER_VALUE* RegisterValues)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    CPUState* cpu = (CPUState*)ctx;
+
+    hr = WHvSetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                         RegisterNames, RegisterCount,
+                                         RegisterValues);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to set virtual processor registers, hr=%08lx", hr);
+        __debugbreak();
+    }
+
+    return hr;
+}
+
+static HRESULT CALLBACK whpx_emu_translate_callback(void* ctx,
+                                                   WHV_GUEST_VIRTUAL_ADDRESS Gva,
+                                                   WHV_TRANSLATE_GVA_FLAGS TranslateFlags,
+                                                   WHV_TRANSLATE_GVA_RESULT_CODE* TranslationResult,
+                                                   WHV_GUEST_PHYSICAL_ADDRESS* Gpa)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    CPUState* cpu = (CPUState*)ctx;
+    WHV_TRANSLATE_GVA_RESULT res;
+
+    hr = WHvTranslateGva(whpx->partition, cpu->cpu_index,
+                         Gva, TranslateFlags, &res, Gpa);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to translate GVA, hr=%08lx", hr);
+        __debugbreak();
+    } else {
+        *TranslationResult = res.ResultCode;
+    }
+
+    return hr;
+}
+
+static const WHV_EMULATOR_CALLBACKS whpx_emu_callbacks = {
+    .WHvEmulatorIoPortCallback = whpx_emu_ioport_callback,
+    .WHvEmulatorMemoryCallback = whpx_emu_memio_callback,
+    .WHvEmulatorGetVirtualProcessorRegisters = whpx_emu_getreg_callback,
+    .WHvEmulatorSetVirtualProcessorRegisters = whpx_emu_setreg_callback,
+    .WHvEmulatorTranslateGvaPage = whpx_emu_translate_callback,
+};
+
+
+static void whpx_populate_instruction_stream(uint32_t cpu_index,
+                                            WHV_VP_EXIT_CONTEXT* vp_ctx,
+                                            uint8_t* bytes, uint8_t* count,
+                                            size_t buffer_size)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    ram_addr_t ip;
+    size_t copy_size;
+    hwaddr hw_ip;
+    WHV_TRANSLATE_GVA_RESULT res;
+
+    ip = vp_ctx->Rip + *count;
+    if (!vp_ctx->ExecutionState.EferLma || !vp_ctx->Cs.Long) {
+        ip += vp_ctx->Cs.Base;
+    }
+
+    buffer_size -= *count;
+    bytes += *count;
+
+    for (;;) {
+        copy_size = min(buffer_size, (TARGET_PAGE_SIZE -
+                                      (ip & TARGET_PAGE_MASK)));
+        if (!copy_size) {
+            return;
+        }
+
+        hr = WHvTranslateGva(whpx->partition, cpu_index, ip,
+                             WHvTranslateGvaFlagValidateExecute, &res,
+                             &hw_ip);
+        if (FAILED(hr)) {
+            error_report("WHPX: Failed to translate GVA, hr=%08lx", hr);
+            __debugbreak();
+            return;
+        } else if (WHvTranslateGvaResultSuccess != res.ResultCode) {
+            error_report("WHPX: Failed to translate GVA");
+            __debugbreak();
+            return;
+        }
+
+        cpu_physical_memory_rw(hw_ip, bytes, copy_size, 0);
+
+        *count += copy_size;
+        bytes += copy_size;
+        buffer_size -= copy_size;
+        ip += copy_size;
+    }
+}
+
+static int whpx_handle_mmio(CPUState *cpu, WHV_MEMORY_ACCESS_CONTEXT* ctx)
+{
+    HRESULT hr;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    WHV_EMULATOR_STATUS emu_status;
+
+    whpx_populate_instruction_stream(cpu->cpu_index, &ctx->VpContext,
+                                    ctx->InstructionBytes,
+                                    &ctx->InstructionByteCount,
+                                    sizeof(ctx->InstructionBytes));
+
+    hr = WHvEmulatorTryMmioEmulation(vcpu->emulator, cpu, ctx, &emu_status);
+    if (FAILED(hr)) {
+        __debugbreak();
+        error_report("WHPX: Failed to parse MMIO access, hr=%08lx", hr);
+        return -1;
+    }
+
+    if (!emu_status.EmulationSuccessful) {
+        __debugbreak();
+        error_report("WHPX: Failed to emulate MMIO access");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int whpx_handle_portio(CPUState *cpu, WHV_X64_IO_PORT_ACCESS_CONTEXT* ctx)
+{
+    HRESULT hr;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    WHV_EMULATOR_STATUS emu_status;
+
+    whpx_populate_instruction_stream(cpu->cpu_index, &ctx->VpContext,
+                                    ctx->InstructionBytes,
+                                    &ctx->InstructionByteCount,
+                                    sizeof(ctx->InstructionBytes));
+
+    hr = WHvEmulatorTryIoEmulation(vcpu->emulator, cpu, ctx, &emu_status);
+    if (FAILED(hr)) {
+        __debugbreak();
+        error_report("WHPX: Failed to parse PortIO access, hr=%08lx", hr);
+        return -1;
+    }
+
+    if (!emu_status.EmulationSuccessful) {
+        __debugbreak();
+        error_report("WHPX: Failed to emulate PortMMIO access");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int whpx_handle_halt(CPUState *cpu)
+{
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    int ret = 0;
+
+    qemu_mutex_lock_iothread();
+    if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
+          (env->eflags & IF_MASK)) &&
+        !(cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
+        cpu->exception_index = EXCP_HLT;
+        cpu->halted = true;
+        ret = 1;
+    }
+    qemu_mutex_unlock_iothread();
+
+    return ret;
+}
+
+static void whpx_vcpu_pre_run(CPUState *cpu)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    int irq;
+    WHV_X64_PENDING_INTERRUPTION_REGISTER new_int = {0};
+    UINT32 reg_count = 0;
+    WHV_REGISTER_VALUE reg_values[3] = {0};
+    WHV_REGISTER_NAME reg_names[3];
+
+    qemu_mutex_lock_iothread();
+
+    /* Inject NMI */
+    if (!vcpu->interrupt_in_flight.InterruptionPending &&
+        cpu->interrupt_request & (CPU_INTERRUPT_NMI | CPU_INTERRUPT_SMI)) {
+        if (cpu->interrupt_request & CPU_INTERRUPT_NMI) {
+            cpu->interrupt_request &= ~CPU_INTERRUPT_NMI;
+            vcpu->interruptable = false;
+            new_int.InterruptionType = WHvX64PendingNmi;
+            new_int.InterruptionPending = 1;
+            new_int.InterruptionVector = 2;
+        }
+        if (cpu->interrupt_request & CPU_INTERRUPT_SMI) {
+            qemu_mutex_lock_iothread();
+            cpu->interrupt_request &= ~CPU_INTERRUPT_SMI;
+            __debugbreak();
+            qemu_mutex_unlock_iothread();
+        }
+    }
+
+    /*
+     * Force the VCPU out of its inner loop to process any INIT requests or
+     * commit pending TPR access.
+     */
+    if (cpu->interrupt_request & (CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR)) {
+        if ((cpu->interrupt_request & CPU_INTERRUPT_INIT) &&
+            !(env->hflags & HF_SMM_MASK)) {
+            cpu->exit_request = 1;
+        }
+        if (cpu->interrupt_request & CPU_INTERRUPT_TPR) {
+            cpu->exit_request = 1;
+        }
+    }
+
+    /* Get pending hard interruption or replay one that was overwritten */
+    if (!vcpu->interrupt_in_flight.InterruptionPending &&
+        vcpu->interruptable && (env->eflags & IF_MASK)) {
+        assert(!new_int.InterruptionPending);
+        if (cpu->interrupt_request & CPU_INTERRUPT_HARD) {
+            irq = cpu_get_pic_interrupt(env);
+            if (irq >= 0) {
+                new_int.InterruptionType = WHvX64PendingInterrupt;
+                new_int.InterruptionPending = 1;
+                new_int.InterruptionVector = irq;
+            }
+        }
+    }
+
+    /* Setup interrupt state if new one was prepared */
+    if (new_int.InterruptionPending) {
+        reg_values[reg_count].PendingInterruption = new_int;
+        reg_names[reg_count] = WHvRegisterPendingInterruption;
+        reg_count += 1;
+    }
+
+    /* Sync the TPR to the CR8 if was modified during the intercept */
+    reg_values[reg_count].Reg64 = cpu_get_apic_tpr(x86_cpu->apic_state);
+    if (reg_values[reg_count].Reg64 != vcpu->tpr) {
+        vcpu->tpr = reg_values[reg_count].Reg64;
+        cpu->exit_request = 1;
+        reg_names[reg_count] = WHvX64RegisterCr8;
+        reg_count += 1;
+    }
+
+    /* Update the state of the interrupt delivery notification */
+    if (cpu->interrupt_request & CPU_INTERRUPT_HARD) {
+        reg_values[reg_count].DeliverabilityNotifications.InterruptNotification = 1;
+    }
+    if (vcpu->window_registered != reg_values[reg_count].DeliverabilityNotifications.InterruptNotification) {
+        vcpu->window_registered = reg_values[reg_count].DeliverabilityNotifications.InterruptNotification;
+        reg_names[reg_count] = WHvX64RegisterDeliverabilityNotifications;
+        reg_count += 1;
+    }
+
+    qemu_mutex_unlock_iothread();
+
+    if (reg_count) {
+        hr = WHvSetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                             reg_names, reg_count, reg_values);
+        if (FAILED(hr)) {
+            error_report("WHPX: Failed o set interrupt state registers, hr=%08lx", hr);
+            __debugbreak();
+        }
+    }
+
+    return;
+}
+
+static void whpx_vcpu_post_run(CPUState *cpu)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    WHV_REGISTER_VALUE reg_values[4];
+    const WHV_REGISTER_NAME reg_names[4] = {
+        WHvX64RegisterRflags,
+        WHvX64RegisterCr8,
+        WHvRegisterPendingInterruption,
+        WHvRegisterInterruptState,
+    };
+
+    hr = WHvGetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
+                                         reg_names, 4, reg_values);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to get interrupt state regusters, hr=%08lx", hr);
+        __debugbreak();
+        vcpu->interruptable = false;
+        return;
+    }
+
+    assert(reg_names[0] == WHvX64RegisterRflags);
+    env->eflags = reg_values[0].Reg64;
+
+    assert(reg_names[1] == WHvX64RegisterCr8);
+    if (vcpu->tpr != reg_values[1].Reg64) {
+        vcpu->tpr = reg_values[1].Reg64;
+        qemu_mutex_lock_iothread();
+        cpu_set_apic_tpr(x86_cpu->apic_state, vcpu->tpr);
+        qemu_mutex_unlock_iothread();
+    }
+
+    assert(reg_names[2] == WHvRegisterPendingInterruption);
+    vcpu->interrupt_in_flight = reg_values[2].PendingInterruption;
+
+    assert(reg_names[3] == WHvRegisterInterruptState);
+    vcpu->interruptable = !reg_values[3].InterruptState.InterruptShadow;
+
+    return;
+}
+
+static void whpx_vcpu_process_async_events(CPUState *cpu)
+{
+    struct CPUX86State* env = (CPUArchState*)(cpu->env_ptr);
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+
+    if ((cpu->interrupt_request & CPU_INTERRUPT_INIT) &&
+        !(env->hflags & HF_SMM_MASK)) {
+
+        do_cpu_init(x86_cpu);
+        cpu->vcpu_dirty = true;
+        vcpu->interruptable = true;
+    }
+
+    if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
+        cpu->interrupt_request &= ~CPU_INTERRUPT_POLL;
+        apic_poll_irq(x86_cpu->apic_state);
+    }
+
+    if (((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
+         (env->eflags & IF_MASK)) ||
+        (cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
+        cpu->halted = false;
+    }
+
+    if (cpu->interrupt_request & CPU_INTERRUPT_SIPI) {
+        if (!cpu->vcpu_dirty) {
+            whpx_get_registers(cpu);
+        }
+        do_cpu_sipi(x86_cpu);
+    }
+
+    if (cpu->interrupt_request & CPU_INTERRUPT_TPR) {
+        cpu->interrupt_request &= ~CPU_INTERRUPT_TPR;
+        if (!cpu->vcpu_dirty) {
+            whpx_get_registers(cpu);
+        }
+        apic_handle_tpr_access_report(x86_cpu->apic_state, env->eip,
+                                      env->tpr_access_type);
+    }
+
+    return;
+}
+
+static int whpx_vcpu_run(CPUState *cpu)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+    int ret;
+
+    whpx_vcpu_process_async_events(cpu);
+    if (cpu->halted) {
+        cpu->exception_index = EXCP_HLT;
+        atomic_set(&cpu->exit_request, false);
+        return 0;
+    }
+
+    qemu_mutex_unlock_iothread();
+    cpu_exec_start(cpu);
+
+    do {
+        if (cpu->vcpu_dirty) {
+            whpx_set_registers(cpu);
+            cpu->vcpu_dirty = false;
+        }
+
+        whpx_vcpu_pre_run(cpu);
+
+        if (atomic_read(&cpu->exit_request)) {
+            whpx_vcpu_kick(cpu);
+        }
+
+        for (;;) {
+            hr = WHvRunVirtualProcessor(whpx->partition, cpu->cpu_index,
+                                        &vcpu->exit_ctx, whpx->exit_ctx_size);
+
+            if (SUCCEEDED(hr) && (vcpu->exit_ctx.ExitReason ==
+                                  WHvRunVpExitReasonAlerted)) {
+                WHvCancelRunVirtualProcessor(whpx->partition, cpu->cpu_index, 0);
+            } else {
+                break;
+            }
+        }
+
+        if (FAILED(hr)) {
+            error_report("WHPX: Failed to exec a virtual processor, hr=%08lx", hr);
+            ret = -1;
+            break;
+        }
+
+        whpx_vcpu_post_run(cpu);
+
+        switch (vcpu->exit_ctx.ExitReason) {
+        case WHvRunVpExitReasonMemoryAccess:
+            ret = whpx_handle_mmio(cpu, &vcpu->exit_ctx.MemoryAccess);
+            break;
+
+        case WHvRunVpExitReasonX64IoPortAccess:
+            ret = whpx_handle_portio(cpu, &vcpu->exit_ctx.IoPortAccess);
+            break;
+
+        case WHvRunVpExitReasonX64InterruptWindow:
+            break;
+
+        case WHvRunVpExitReasonX64Halt:
+            ret = whpx_handle_halt(cpu);
+            break;
+
+        case WHvRunVpExitReasonCanceled:
+            cpu->exception_index = EXCP_INTERRUPT;
+            ret = 1;
+            break;
+
+        case WHvRunVpExitReasonNone:
+        case WHvRunVpExitReasonUnrecoverableException:
+        case WHvRunVpExitReasonInvalidVpRegisterValue:
+        case WHvRunVpExitReasonUnsupportedFeature:
+        case WHvRunVpExitReasonX64MsrAccess:
+        case WHvRunVpExitReasonX64Cpuid:
+        case WHvRunVpExitReasonException:
+        case WHvRunVpExitReasonAlerted:
+        default:
+            error_report("WHPX: Unexpected VP exit code %d", vcpu->exit_ctx.ExitReason);
+            whpx_get_registers(cpu);
+            qemu_mutex_lock_iothread();
+            qemu_system_guest_panicked(cpu_get_crash_info(cpu));
+            qemu_mutex_unlock_iothread();
+            break;
+        }
+
+    } while (!ret);
+
+    cpu_exec_end(cpu);
+    qemu_mutex_lock_iothread();
+    current_cpu = cpu;
+
+    atomic_set(&cpu->exit_request, false);
+
+    return ret < 0;
+}
+
+static void do_whpx_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
+{
+    whpx_get_registers(cpu);
+    cpu->vcpu_dirty = true;
+}
+
+static void do_whpx_cpu_synchronize_post_reset(CPUState *cpu,
+                                              run_on_cpu_data arg)
+{
+    whpx_set_registers(cpu);
+    cpu->vcpu_dirty = false;
+}
+
+static void do_whpx_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
+{
+    whpx_set_registers(cpu);
+    cpu->vcpu_dirty = false;
+}
+
+static void do_whpx_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg)
+{
+    cpu->vcpu_dirty = true;
+}
+
+/*
+ * CPU support.
+ */
+
+void whpx_cpu_synchronize_state(CPUState *cpu)
+{
+    if (!cpu->vcpu_dirty) {
+        run_on_cpu(cpu, do_whpx_cpu_synchronize_state, RUN_ON_CPU_NULL);
+    }
+}
+
+void whpx_cpu_synchronize_post_reset(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_whpx_cpu_synchronize_post_reset, RUN_ON_CPU_NULL);
+}
+
+void whpx_cpu_synchronize_post_init(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_whpx_cpu_synchronize_post_init, RUN_ON_CPU_NULL);
+}
+
+void whpx_cpu_synchronize_pre_loadvm(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_whpx_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL);
+}
+
+/*
+ * Vcpu support.
+ */
+
+int whpx_init_vcpu(CPUState *cpu)
+{
+    HRESULT hr;
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu;
+
+    vcpu = g_malloc0(FIELD_OFFSET(struct whpx_vcpu, exit_ctx) +
+                     whpx->exit_ctx_size);
+
+    if (!vcpu) {
+        error_report("WHPX: Failed to allocte VCPU context.");
+        return -ENOMEM;
+    }
+
+    hr = WHvEmulatorCreateEmulator(whpx_emu_callbacks, &vcpu->emulator);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to setup instruction completion support, hr=%08lx", hr);
+        g_free(cpu->hax_vcpu);
+        return -EINVAL;
+    }
+
+    hr = WHvCreateVirtualProcessor(whpx->partition, cpu->cpu_index, 0);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to create a virtual processor, hr=%08lx", hr);
+        WHvEmulatorDestroyEmulator(vcpu->emulator);
+        g_free(cpu->hax_vcpu);
+        return -EINVAL;
+    }
+
+    vcpu->interruptable = true;
+
+    cpu->vcpu_dirty = true;
+    cpu->hax_vcpu = (struct hax_vcpu_state*)vcpu;
+
+    return 0;
+}
+
+int whpx_vcpu_exec(CPUState *cpu)
+{
+    int ret;
+    int fatal;
+
+    for (;;) {
+        if (cpu->exception_index >= EXCP_INTERRUPT) {
+            ret = cpu->exception_index;
+            cpu->exception_index = -1;
+            break;
+        }
+
+        fatal = whpx_vcpu_run(cpu);
+
+        if (fatal) {
+            error_report("WHPX: Failed to exec a virtual processor");
+            abort();
+        }
+    }
+
+    return ret;
+}
+
+void whpx_destroy_vcpu(CPUState *cpu)
+{
+    struct whpx_state *whpx = &whpx_global;
+    struct whpx_vcpu* vcpu = get_whpx_vcpu(cpu);
+
+    WHvDeleteVirtualProcessor(whpx->partition, cpu->cpu_index);
+    WHvEmulatorDestroyEmulator(vcpu->emulator);
+    g_free(cpu->hax_vcpu);
+    return;
+}
+
+void whpx_vcpu_kick(CPUState *cpu)
+{
+    struct whpx_state *whpx = &whpx_global;
+    WHvCancelRunVirtualProcessor(whpx->partition, cpu->cpu_index, 0);
+}
+
+/*
+ * Memory support.
+ */
+
+static void whpx_update_mapping(hwaddr start_pa, ram_addr_t size, void* host_va,
+                               int add, int rom, const char* name)
+{
+    struct whpx_state *whpx = &whpx_global;
+    HRESULT hr;
+
+    /*
+    if (add) {
+        printf("WHPX: ADD PA:%p Size:%p, Host:%p, %s, '%s'\n",
+               (void*)start_pa, (void*)size, host_va,
+               (rom ? "ROM" : "RAM"), name);
+    } else {
+        printf("WHPX: DEL PA:%p Size:%p, Host:%p,      '%s'\n",
+               (void*)start_pa, (void*)size, host_va, name);
+    }
+    */
+
+    if (add) {
+        hr = WHvMapGpaRange(whpx->partition,
+                            host_va,
+                            start_pa,
+                            size,
+                            (WHvMapGpaRangeFlagRead |
+                             WHvMapGpaRangeFlagExecute |
+                             (rom ? 0 : WHvMapGpaRangeFlagWrite)));
+    } else {
+        hr = WHvUnmapGpaRange(whpx->partition,
+                              start_pa,
+                              size);
+    }
+
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to %s GPA range '%s' PA:%p, Size:%p bytes,"
+                     " Host:%p, hr=%08lx",
+                     (add ? "MAP" : "UNMAP"), name,
+                     (void*)start_pa, (void*)size, host_va, hr);
+    }
+}
+
+static void whpx_process_section(MemoryRegionSection *section, int add)
+{
+    MemoryRegion *mr = section->mr;
+    hwaddr start_pa = section->offset_within_address_space;
+    ram_addr_t size = int128_get64(section->size);
+    unsigned int delta;
+    uint64_t host_va;
+
+    if (!memory_region_is_ram(mr)) {
+        return;
+    }
+
+    delta = qemu_real_host_page_size - (start_pa & ~qemu_real_host_page_mask);
+    delta &= ~qemu_real_host_page_mask;
+    if (delta > size) {
+        return;
+    }
+    start_pa += delta;
+    size -= delta;
+    size &= qemu_real_host_page_mask;
+    if (!size || (start_pa & ~qemu_real_host_page_mask)) {
+        return;
+    }
+
+    host_va = (uintptr_t)memory_region_get_ram_ptr(mr)
+            + section->offset_within_region + delta;
+
+    whpx_update_mapping(start_pa, size, (void*)host_va, add,
+                       memory_region_is_rom(mr), mr->name);
+}
+
+static void whpx_region_add(MemoryListener *listener,
+                           MemoryRegionSection *section)
+{
+    memory_region_ref(section->mr);
+    whpx_process_section(section, 1);
+}
+
+static void whpx_region_del(MemoryListener *listener,
+                           MemoryRegionSection *section)
+{
+    whpx_process_section(section, 0);
+    memory_region_unref(section->mr);
+}
+
+static void whpx_transaction_begin(MemoryListener *listener)
+{
+}
+
+static void whpx_transaction_commit(MemoryListener *listener)
+{
+}
+
+static void whpx_log_sync(MemoryListener *listener,
+                         MemoryRegionSection *section)
+{
+    MemoryRegion *mr = section->mr;
+
+    if (!memory_region_is_ram(mr)) {
+        return;
+    }
+
+    memory_region_set_dirty(mr, 0, int128_get64(section->size));
+}
+
+static MemoryListener whpx_memory_listener = {
+    .begin = whpx_transaction_begin,
+    .commit = whpx_transaction_commit,
+    .region_add = whpx_region_add,
+    .region_del = whpx_region_del,
+    .log_sync = whpx_log_sync,
+    .priority = 10,
+};
+
+static void whpx_memory_init(void)
+{
+    memory_listener_register(&whpx_memory_listener, &address_space_memory);
+}
+
+static void whpx_handle_interrupt(CPUState *cpu, int mask)
+{
+    cpu->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(cpu)) {
+        qemu_cpu_kick(cpu);
+    }
+}
+
+/*
+ * Partition support
+ */
+
+static int whpx_accel_init(MachineState *ms)
+{
+    struct whpx_state *whpx;
+    int ret;
+    HRESULT hr;
+    WHV_CAPABILITY whpx_cap;
+    WHV_PARTITION_PROPERTY prop;
+
+    whpx = &whpx_global;
+
+    memset(whpx, 0, sizeof(struct whpx_state));
+    whpx->mem_quota = ms->ram_size;
+
+    hr = WHvGetCapability(WHvCapabilityCodeHypervisorPresent, &whpx_cap, sizeof(whpx_cap));
+    if (FAILED(hr) || !whpx_cap.HypervisorPresent) {
+        error_report("WHPX: No accelerator found, hr=%08lx", hr);
+        ret = -ENOSPC;
+        goto error;
+    }
+
+    hr = WHvCreatePartition(&whpx->partition);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to create partition, hr=%08lx", hr);
+        ret = -EINVAL;
+        goto error;
+    }
+
+    memset(&prop, 0, sizeof(WHV_PARTITION_PROPERTY));
+    prop.PropertyCode = WHvPartitionPropertyCodeProcessorCount;
+    prop.ProcessorCount = smp_cpus;
+    hr = WHvSetPartitionProperty(whpx->partition,
+                                 &prop,
+                                 sizeof(WHV_PARTITION_PROPERTY));
+
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to set partition core count to %d, hr=%08lx",
+                     smp_cores, hr);
+        ret = -EINVAL;
+        goto error;
+    }
+
+    hr = WHvSetupPartition(whpx->partition);
+    if (FAILED(hr)) {
+        error_report("WHPX: Failed to setup partition, hr=%08lx", hr);
+        ret = -EINVAL;
+        goto error;
+    }
+
+    whpx->exit_ctx_size = WHvGetRunExitContextSize();
+    assert(whpx->exit_ctx_size);
+
+    whpx_memory_init();
+
+    cpu_interrupt_handler = whpx_handle_interrupt;
+
+    printf("Windows Hypervisor Platform accelerator is operational\n");
+    return 0;
+
+  error:
+
+    if (NULL != whpx->partition) {
+        WHvDeletePartition(whpx->partition);
+        whpx->partition = NULL;
+    }
+
+    return ret;
+}
+
+int whpx_enabled(void)
+{
+    return whpx_allowed;
+}
+
+static void whpx_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "WHPX";
+    ac->init_machine = whpx_accel_init;
+    ac->allowed = &whpx_allowed;
+}
+
+static const TypeInfo whpx_accel_type = {
+    .name = ACCEL_CLASS_NAME("whpx"),
+    .parent = TYPE_ACCEL,
+    .class_init = whpx_accel_class_init,
+};
+
+static void whpx_type_init(void)
+{
+    type_register_static(&whpx_accel_type);
+}
+
+type_init(whpx_type_init);
-- 
2.7.4


[Qemu-devel] [PATCH 4/4] Add the WHPX acceleration enlightenments
Posted by Justin Terry (VM) via Qemu-devel, 5 days ago
Implements the WHPX accelerator cpu enlightenments to actually use the whpx-all
accelerator on Windows platforms.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
---
 cpus.c                    | 66 ++++++++++++++++++++++++++++++++++++++++++++++-
 include/sysemu/hw_accel.h | 13 ++++++++++
 target/i386/helper.c      |  2 +-
 3 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/cpus.c b/cpus.c
index e8139de..48df958 100644
--- a/cpus.c
+++ b/cpus.c
@@ -38,6 +38,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/hax.h"
 #include "sysemu/hvf.h"
+#include "sysemu/whpx.h"
 #include "qmp-commands.h"
 #include "exec/exec-all.h"
 
@@ -1507,6 +1508,46 @@ static void *qemu_hvf_cpu_thread_fn(void *arg)
     return NULL;
 }
 
+static void *qemu_whpx_cpu_thread_fn(void *arg)
+{
+    CPUState *cpu = arg;
+    int r;
+
+    qemu_mutex_lock_iothread();
+    qemu_thread_get_self(cpu->thread);
+    cpu->thread_id = qemu_get_thread_id();
+    current_cpu = cpu;
+
+    r = whpx_init_vcpu(cpu);
+    if (r < 0) {
+        fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
+        exit(1);
+    }
+
+    /* signal CPU creation */
+    cpu->created = true;
+    qemu_cond_signal(&qemu_cpu_cond);
+
+    do {
+        if (cpu_can_run(cpu)) {
+            r = whpx_vcpu_exec(cpu);
+            if (r == EXCP_DEBUG) {
+                cpu_handle_guest_debug(cpu);
+            }
+        }
+        while (cpu_thread_is_idle(cpu)) {
+            qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
+        }
+        qemu_wait_io_event_common(cpu);
+    } while (!cpu->unplug || cpu_can_run(cpu));
+
+    whpx_destroy_vcpu(cpu);
+    cpu->created = false;
+    qemu_cond_signal(&qemu_cpu_cond);
+    qemu_mutex_unlock_iothread();
+    return NULL;
+}
+
 #ifdef _WIN32
 static void CALLBACK dummy_apc_func(ULONG_PTR unused)
 {
@@ -1598,7 +1639,9 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
 #else /* _WIN32 */
     if (!qemu_cpu_is_self(cpu)) {
-        if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
+        if (whpx_enabled()) {
+            whpx_vcpu_kick(cpu);
+        } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
             fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
                     __func__, GetLastError());
             exit(1);
@@ -1845,6 +1888,25 @@ static void qemu_hvf_start_vcpu(CPUState *cpu)
     }
 }
 
+static void qemu_whpx_start_vcpu(CPUState *cpu)
+{
+    char thread_name[VCPU_THREAD_NAME_SIZE];
+
+    cpu->thread = g_malloc0(sizeof(QemuThread));
+    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
+    qemu_cond_init(cpu->halt_cond);
+    snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
+             cpu->cpu_index);
+    qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
+                       cpu, QEMU_THREAD_JOINABLE);
+#ifdef _WIN32
+    cpu->hThread = qemu_thread_get_handle(cpu->thread);
+#endif
+    while (!cpu->created) {
+        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
+    }
+}
+
 static void qemu_dummy_start_vcpu(CPUState *cpu)
 {
     char thread_name[VCPU_THREAD_NAME_SIZE];
@@ -1883,6 +1945,8 @@ void qemu_init_vcpu(CPUState *cpu)
         qemu_hvf_start_vcpu(cpu);
     } else if (tcg_enabled()) {
         qemu_tcg_init_vcpu(cpu);
+    } else if (whpx_enabled()) {
+        qemu_whpx_start_vcpu(cpu);
     } else {
         qemu_dummy_start_vcpu(cpu);
     }
diff --git a/include/sysemu/hw_accel.h b/include/sysemu/hw_accel.h
index 469ffda..d2ddfb5 100644
--- a/include/sysemu/hw_accel.h
+++ b/include/sysemu/hw_accel.h
@@ -14,6 +14,7 @@
 #include "qom/cpu.h"
 #include "sysemu/hax.h"
 #include "sysemu/kvm.h"
+#include "sysemu/whpx.h"
 
 static inline void cpu_synchronize_state(CPUState *cpu)
 {
@@ -23,6 +24,9 @@ static inline void cpu_synchronize_state(CPUState *cpu)
     if (hax_enabled()) {
         hax_cpu_synchronize_state(cpu);
     }
+    if (whpx_enabled()) {
+        whpx_cpu_synchronize_state(cpu);
+    }
 }
 
 static inline void cpu_synchronize_post_reset(CPUState *cpu)
@@ -33,6 +37,9 @@ static inline void cpu_synchronize_post_reset(CPUState *cpu)
     if (hax_enabled()) {
         hax_cpu_synchronize_post_reset(cpu);
     }
+    if (whpx_enabled()) {
+        whpx_cpu_synchronize_post_reset(cpu);
+    }
 }
 
 static inline void cpu_synchronize_post_init(CPUState *cpu)
@@ -43,6 +50,9 @@ static inline void cpu_synchronize_post_init(CPUState *cpu)
     if (hax_enabled()) {
         hax_cpu_synchronize_post_init(cpu);
     }
+    if (whpx_enabled()) {
+        whpx_cpu_synchronize_post_init(cpu);
+    }
 }
 
 static inline void cpu_synchronize_pre_loadvm(CPUState *cpu)
@@ -53,6 +63,9 @@ static inline void cpu_synchronize_pre_loadvm(CPUState *cpu)
     if (hax_enabled()) {
         hax_cpu_synchronize_pre_loadvm(cpu);
     }
+    if (whpx_enabled()) {
+        whpx_cpu_synchronize_pre_loadvm(cpu);
+    }
 }
 
 #endif /* QEMU_HW_ACCEL_H */
diff --git a/target/i386/helper.c b/target/i386/helper.c
index f63eb3d..9fba146 100644
--- a/target/i386/helper.c
+++ b/target/i386/helper.c
@@ -986,7 +986,7 @@ void cpu_report_tpr_access(CPUX86State *env, TPRAccess access)
     X86CPU *cpu = x86_env_get_cpu(env);
     CPUState *cs = CPU(cpu);
 
-    if (kvm_enabled()) {
+    if (kvm_enabled() || whpx_enabled()) {
         env->tpr_access_type = access;
 
         cpu_interrupt(cs, CPU_INTERRUPT_TPR);
-- 
2.7.4