Note: This patch has been merged with upstream. Simply update your archlinux machine and npt should just work.

The new npt patch allows you to enable npt on ryzen cpus and get native performance in a virtual machine. Please be advised that this patch is not mainline and may cause other problems. As always, make backups.

For this procedure we will be using the Arch Build System so that pacman will manage all of the files and so we can rollback a kernel by looking into /var/cache/pacman/pkg/ and using pacman -U.

mkdir builds
cd builds

Make sure you have subversion installed and then:

svn checkout --depth=empty svn://svn.archlinux.org/packages
cd packages
svn update linux
cd linux/repos/core-x86_64/

Edit the PKGBUILD file to include the following near prepare(){

# add upstream patch
patch -p1 -i ../patch-${pkgver}

# ADD THE FOLLOWING LINE TO THE PKGBUILD
patch -p1 -i ../patch-ryzen

Next, we put the npt patch into the src folder.

cd src
vim patch-ryzen

and copy paste the following lines:

diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index af256b786a70..af09baa3d736 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -3626,6 +3626,13 @@  static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 	u32 ecx = msr->index;
 	u64 data = msr->data;
 	switch (ecx) {
+	case MSR_IA32_CR_PAT:
+		if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
+			return 1;
+		vcpu->arch.pat = data;
+		svm->vmcb->save.g_pat = data;
+		mark_dirty(svm->vmcb, VMCB_NPT);
+		break;
 	case MSR_IA32_TSC:
 		kvm_write_tsc(vcpu, msr);
 		break;

Finally, in order to build the package and install the new patched kernel:

cd ..
makepkg -si

Make sure `/etc/modprobe.d/kvm_amd.conf` has npt enabled:

options kvm_amd npt=1

and reboot

References:

https://wiki.archlinux.org/index.php/Creating_packages

https://wiki.archlinux.org/index.php/Arch_Build_System

https://level1techs.com/article/patch-npt-ryzen-better-performance