HOW-TO patch Nvidia drivers in Windows 10 for VGA passthrough

Questions about virtualization software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
powerhouse
Level 6
Level 6
Posts: 1138
Joined: Thu May 03, 2012 3:54 am
Location: Israel
Contact:

HOW-TO patch Nvidia drivers in Windows 10 for VGA passthrough

Post by powerhouse »

Background

For the past 4 years I'm running Linux Mint as my main operating system, and Windows in a VM using VGA passthrough for bare-metal graphics performance. This much more elegant solution has entirely replaced the need for dual-booting. I have written two How-Tos, one for Xen VGA passthrough, the other for KVM VGA passthrough (see here for reference).

Nvidia graphics cards - specifically the consumer-type versions - have a mysterious bug that prevents them from running in a virtual machine. Nvidia has no plans to fix this bug, so if someone wants to run his/her graphics card in a virtual environment with VGA passthrough, he/she needs to buy the expensive professional versions (Quadro 2000 and above). For more on that, see https://wiki.archlinux.org/index.php/PC ... indows_VMs.

There is a workaround in KVM:

Code: Select all

-cpu host,kvm=off
kvm=off turns off the hypervisor extensions, which may have an impact on features and performance of the VM.

sk1080 over at the Arch Linux forum has written a nice little patch for the Nvidia driver to fix the bug Nvidia can't or doesn't want to fix :x .

Warning: Patching the Nvidia driver will most likely void your license to use the driver software. In other words, you are not allowed to modify the driver!

This means the entire how-to below was written to show how Nvidia could fix their drivers.

Is it necessary to patch the Nvidia driver?

No! Nvidia should fix their "bugs", not we. Performance-wise I haven't seen a difference between the patched Nvidia driver and using the kvm=off option. My complete qemu -cpu option line is now:

Code: Select all

-cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time,hv_vendor_id=whatever123
Here my benchmarks using above qemu option:
UserBenchmarks: Game 76%, Desk 75%, Work 66%
CPU: Intel Core i7-3930K - 85.5%
GPU: Nvidia GTX 970 - 78.3%
SSD: Red Hat VirtIO 118GB - 83.1%
HDD: Red Hat VirtIO 1.5TB - 72.1%
HDD: Red Hat VirtIO 1.5TB - 166.6%
RAM: QEMU 20GB - 98.3%
MBD: QEMU Standard PC (Q35 + ICH9, 2009)

How to patch the Nvidia driver under Windows 10

1. Backup your Windows VM !!!

2. Using KVM/qemu, boot your Windows 10 VM using the kvm=off option described above.

3. Inside Windows, download the Nvidia graphics driver for your graphics card from here http://www.nvidia.com/Download/index.aspx?lang=en-us.
The latest driver for my Nvidia GTX 970, as of this writing, is 372.54.

4. Run the Nvidia driver installer. It will unpack the files to C:/NVIDIA/DisplayDriver/{version}/Win10_64/International/Display.Driver, with the driver as of this writing it will be C:\NVIDIA\DisplayDriver\372.54\Win10_64\International\Display.Driver

Exit the Nvidia installer once it unpacked the files !!!

5. Download the Windows WDK from here: https://developer.microsoft.com/en-us/w ... driver-kit

On the webpage that opens, go to step 2, then click "Install Windows Driver Kit (WDK) 10". You do NOT need to install the other stuff!

Run the installer and install the Windows Driver Kit (WDK).

6. Download Python from https://www.python.org/downloads/. As of this writing the latest version is 3.5.2

Run the installer.

7. To enable testsigning:
  1. Right-click on the Windows menu icon and select "Command Prompt (Admin)"
  2. On the command prompt, enter

    Code: Select all

    Bcdedit.exe -set TESTSIGNING ON
    and press Enter
Later, if you wish to disable testsigning, use the following command:

Code: Select all

Bcdedit.exe -set TESTSIGNING OFF
Important: Please be aware of the security risks when you run testsigned drivers.

8. Reboot your Windows 10 VM.

9. Open https://github.com/sk1080/nvidia-kvm-patcher and download the two script files (a Powershell script and a Python script):
  • gencert.ps1
  • patcher.py

Both files must reside in the same location. (Note: I opened the file, copied the content into an empty Notepad file and saved it.)

10. Open the Windows File Explorer and go to the folder where you saved the two files.

Click "File", then select "Open command prompt" -> "Open command prompt as administrator".

11. Before running the script, open the patcher.py file in Notepad or with IDLE (right click on the file and select) and check that the driver version you are going to install is actually supported by this patch. See below for reference:
# TODO: Wildcard Search
PATCHES = {
"41FF978804000085C0": "31C0909090909085C0", # 361.91 - 368.39
"41FF97B804000085C0": "31C0909090909085C0", # 372.54
}
12. Copy and paste the following onto the command line:

Code: Select all

patcher.py C:\NVIDIA\DisplayDriver\372.54\Win10_64\International\Display.Driver
and hit Enter.

Note: Replace the version (here "372.54") in the path name with the Nvidia driver version you downloaded.

The patcher.py script will run for a few minutes, after which you see "Warnings: None". Hit Enter.

The Python script will now execute the Powershell script and testsign the driver.

13. Using the File Explorer, go to

Code: Select all

C:\NVIDIA\DisplayDriver\372.54\Win10_64\International
and run "setup.exe" to install the patched Nvidia driver.

14. Shutdown Windows and modify your qemu start script:

Code: Select all

-cpu host,hv_vapic,hv_time,hv_relaxed,hv_spinlocks=0x1fff \
Note: Remove the "kvm=off" option and instead add "hv_vapic,hv_time,hv_relaxed,hv_spinlocks=0x1fff"



References

viewtopic.php?f=231&t=212692

http://blog.wikichoon.com/2014/07/enabl ... h-kvm.html

https://wiki.archlinux.org/index.php/PC ... h_via_OVMF

https://github.com/sk1080/nvidia-kvm-patcher

https://msdn.microsoft.com/en-us/window ... ion-option


Credits

All credit should go to sk1080 who wrote the script and provided the instructions. Great work !!!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Subjects of interest: Linux, vfio passthrough virtualization, photography
See my blog on virtualization, including tutorials: https://www.heiko-sieger.info/category/ ... alization/
Locked

Return to “Virtual Machines”