Friday, June 15, 2012

gaming on gentoo/linux via xen

xen - a hypervisor who is one of many hardware virtualization techniques which allows running concurrently guests on a host system. One of the special key features of xen is vga passthrough which allows a guest to have full access to a pci device.
When i bought my new PC i wanted to use that feature because i missed gaming on the PC and since i don't wanted to reboot every time i decided to setup a windows 7 guest with a passthroughed graphic card.
In this post i'll gonna explain my setup and show what i have done to get it work.

My problem was easy to describe. I wanted gaming. Wine was an opinion, but i don't wanted to "frikle" around to get games working and multiboot was a no-go. Even though boot times are already pretty fast i wanted to have linux running all the time. This brought me to xen...

linux and windows side by side

1) Setup

First, my hardware setup and how it should work.
I have a an 8core amd processor with 16GB ram and two graphic cards. 4cores, 8GB ram and the high end graphic card is solely for the virtualized windows. That's enough power to run most new games without problems. (aside of linux)
Both graphic cards are connected to my 30" monitor and with the input key on the monitor i can switch between windows and linux.
The linux graphic card has two more 22" screens on the left and right of the 30".

2) Installation

2.1 System:
First i made a typical gentoo installation. It's ~amd64 keyworded, means it's more or less bloody edge. I even have a few 9999 packages installed like mesa, libdrm, xorg-server, etc but that shouldn't have any influence on xen.

 CFLAGS="-O2 -pipe -fomit-frame-pointer -mcx16 -msahf -maes -mpclmul -mpopcnt -mabm -mlwp -mavx -march=native"  

With gcc-4.6 i'll add "-march=bdver1 -mtune=bdver1" to CFLAGS for enabling all possible features of the new bulldozer cpu.

Another change was to create a second runlevel especially for xen. I already bloged about it so you can read the small howto here: Link

2.2 Network:
I created a bridge so that xen can create tap devices for the virtual guest. A good howto for creating bridges can be found here: Link

2.3 Xen:
Really important is to have a multilib profile on gentoo, otherwise you couldn't set the "hvm" flag on xen-tools which is required for the amd-v processor feature. (i had to install gentoo a second time because of this)
The installation of xen was really smooth. I enabled following flags, where at least the "hvm" flag is really important.

xen flags:
 pp-emulation/xen-tools hvm qemu screen  

I also tried out xen from git because it has lots of vga passthrough patches, but since i couldn't start any virtual machine with the git version, i switched back to the latest version in portage. The git version of xen can be found in the virtualization overlay.

2.4 Kernel:
On the kernel side i compiled almost everything which has to do with xen directly into the kernel. I choose to use the latest git-sources so that i have all the latest features. I'll provide my configuration here for those who are interested: Download

2.5 Grub:
Grub also needed a new entry for xen which looks like this:
 title Gentoo Linux XEN  
 root (hd0,0)  
 kernel /boot/xen.gz dom0_mem=8192M,max:8192M iommu=1 xsave=1 dom0_max_vcpus=4 dom0_vcpus_pin                                                                          
 module /boot/gentoo-3.4.0-rc6 root=/dev/md3 softlevel=xen xen-pciback.permissive xen-pciback.hide=(01:00.0)(01:00.1)(00:12.0)(00:12.2) pci=resource_aligment=01:00.0,01:00.1,00:12.0,00:12.2  

Below the descriptions about this configuration. Dom0 is the host system, all systems which are virtualized are domU's

dom0_mem=8192M = the host system get's 8GB Ram...
,max:8192M = and can't have more than 8GB
iommu=1 = turn iommu on
xsave=1 = don't know exactly what it does, but it was need to prevent a bug with avx, see here: LInk
dom0_max_vcpus=4 = the host system get's 4 cpu's
dom0_vcpus_pin = it pin's the first 4 core's solely for the host
softlevel=xen = see here
xen-pciback.permissive xen-pciback.hide=(01:00.0)(01:00.1)(00:12.0)(00:12.2) pci=resource_aligment=01:00.0,01:00.1,00:12.0,00:12.2 = this is actually for pci(vga) passthrough. The bus number of you're graphic card and/or your usb bus from lspci can be found with lspci. It hide's the first pci device - the hd7970 (01:00.0 & 01:00.1) and one usb bus (00:12.0 & 00:12.2) which i also passthroughed to the guest. Note, you always have to hide everything on a single bus e.g. 01:00.0 & 01:00.1 and not just 01:00.0

3) Windows 7

3.1 Installation:
The installation of Windows 7 went really smooth. First i've created an 30GB empty image with dd:
 dd if=/dev/zero of=win7_64 bs=1M count=30000  

Then i've wrote a config file for xen:
Download this config: Download

The last line is important, because this one says which pci buses are gonna be passthroughed to the guest. I also enabled vnc, so that i can connect to the system with krdc which was important while installing windows.
"disk" is the option where you point to the image and also to the iso of the windows7 dvd. A better explanation of all options can be found here: Link and as PDF

It's important not to turn on gfx_passthru, because with xen-4.1.2 vga passthrough isn't supported at boot time on ati devices. First you have to install windows and than the driver for you're gpu. Afterwards windows want to restart the system to enable the new gpu - do that and you'll have you graphical output on your second gpu :)
As long as you don't want to try out xen-unstable that's the way to go, which means as for now you don't need the gfx_passthru option.

To start xen you just have to type following into a console (as root):
 xl create /path/to/the/win7-config.cfg  

Next you start your favorite vnc viewer and connect to localhost and install windows. Afterwards you'll have to download the windows ati driver and install them. But be sure to install just the drivers without the ati ccc. It's known that the ati ccc makes troubles with xen. Don't install it.
I think it's the best way is to extract them and let windows install the drivers over the device manager.

3.2 Audio:
I've choose to create a Windows 7 64Bit Version. Actually, when i started to though about xen my biggest worries where everywhere except audio. I actually though it's pretty easy to get audio to work, but actually it's almost impossible.
Why? Well, xen doesn't have any emulated audio cards which work with windows7 64bit. I really searched long for a solution and the final one is more or less a workaround.
I bought a new external USB audio card for about 20€ and connected it on the windows usb bus where windows immediately installed the driver.
The usb audio card has one output and one input. From the output of the usb audio card i go into the input of my onboard audio card on linux. Now i can easy turn on/turn off the sound in windows over linux. Crazy - isn't it? :)
And the cool thing is i can run music on linux while playing on windows at the same time since the virtualized windows is just one more input for linux.

This is the audio card which i bought:
USB Audio Card
Product page of the sound card: Link

3.3 Input:
Input was also a important topic. With pci passthrough input devices are also handled different. The problem is that the monitor output is not on the host system thus you also can't move with the mouse from the hostsystem like in qemu or virtualbox.
That's why, besides the graphic card, i also passthrough an complete usb bus. So i had 4 usb slot solely for windows. I already needed one usb slot for the external audio card. Now i needed two other for the mouse and keyboard.
Furthermore i don't wanted to have 2 mice and 2 keyboard for technically one system and so i searched for a switcher like a kvm switch but just for usb. I found this one. It works just for one device so i had to buy it two times.
Delock USB Switcher
Pretty smart isn't it :) It works, but it's not perfect. Here is the product page of this device: Link

3.4 Harddisks:
Besides the 30GB harddrive for the windows7 system i also created a 300GB harddrive just for data. There i store all my games. In case something goes wrong with windows i still have all my games one separate (virtual) partition.

4) Problems

So far everything works really flawless, but there are still some problems.

  • I wouldn't count the audio devices as problem, because it works really good but it's sad that xen still can't emulate any modern audio cards.
  • It was also a pain for me when i found out that xen supports passthrough first after you installed windows and installed the ati driver. I tried alot after i found that out. Anyway, this will come with xen-4.2.
  • Another problem right now is actually the usb switcher - and that one is also just a minor one.It works, but mostly just from windows to linux. Linux actually never want to release the keyboard/mouse and so i always have to unplug the devices first. It's annoying, but its ok :)
  • My biggest problem right now is that once you shut down the virtualized windows you cannot start windows again. I have to restart the whole linux system too, otherwise it won't work. A bit annoying but i hope that will be fixed with xen 4.2.
Lastly i made a short demonstration video showing UT3 with max details on my 30" screen. I know, it's not the newest game out there, but i think it still has a really good graphic and actually i don't have anything better. (newer yes, but i guess diablo 3 isn't such a pretty game :) )

Btw, sorry for the bad quality - it's my first video on youtube.
That's all for now. Next step will be xen unstable, kernel 3.5 with latest gcc xD


  1. Tricky config, but most impressive. Too bad there had to be so many compromises :-/
    What about upgrading issues of Xen, ATI drivers, kernel etc.
    After all, ~amd64 might introduce some bugs and instabilities. Have you been reanimating or fixing your setup after possible buggy upgrade yet?

  2. Well, this config is just a few weeks old thus i didn't had that much upgrades, but so far i didn't had any bigger problems. I'm using kernel-3.5-rc3 now with xen-4.2 (git/unstable) and still the same ati-drivers (in windows). No regressions so far. Even ~amd64 didn't introduce new bugs yet, but since that setup is just a few weeks old it might be to soon to tell how stable it is.

  3. Have been considering attempting this myself. Regarding the USB switching, if you haven't already looked into this angle, check out Pretty straightforward, host a server on one OS, client on the other, toggle between using keyboard command or you can configure it so that the Windows is 'Above' the Linux, so moving your mouse pointer to the top of the Linux screen automatically switches the pointer to the bottom of Windows etc. Maybe even create a dedicated virtual bridge for keyboard/mouse traffic, but I'm not sure if that would really be necessary.

  4. Hi all.

    I have a little different setup than Mike. I am running a 6core Phenom 1090t, with an 890fx chipset and 16gb ram, and a ATI 6870 with a seperate monitor connected to it. For input I have a small USB Hub connected to a passed thru PCI USB 3.0 card. This hub is holding USB Mouse + Keyboard, Wifi USB, USB Audio. So the XEN hardware is almost a second PC, but missing dedicated casing, ram, mainboard and cpu !

    My biggest problem with XEN wasn't the pci passthrough, but to get input and peripherlas working. First I used some VIA chipset USB 2.0 card, which wouldn't work at all. I then bought a second chaep USB 3.0 PCI-E card, which sadly was again a VIA chipset and wouldn't work. Then I found a NEC chip USB 3.0 card, and ever since then things are working fine.
    To get XEN working can be a tidous "walk to the shop, get some hardware, try and walk back" job....

    Also keep in mind that for the time beeing only some Nvidia cards would work (i think Optimus series, which is weak for gaming). Maybe this will change in the future, maybe never...

    For the gaming experience: Sadly my second monitor is only capable of 1650x1440 resolution, but I can play Crysis Warfare in everything turned to max at ~35fps. I also tested Amnesia and Metro 2033. Everything works great.

    If you are living in Linux like me and want to be able to play the latest games, I recommend to get a console like XBox360/PS3.
    The time, money and effort you have to put into setting up XEN can be a fun job, but I think with a console you have less trouble... Just my humble opinion.

    I just want to share my experience with you guys.

    1. I wonder if you modded the nvidia quadro drivers ini to include the new gtx 600 series card if it would work at possibly reduced performance. I would be interested in the results.

  5. @ dyssfunkshinal:
    Thanks for the hint. Never heard from before. Actually i didn't tried it out (yet) since i'm pretty satisfied with the switcher. Basically it always works from windows -> linux but never from linux -> windows. That's really not a big problem. Besides that i a bit concerned that with synergy the reaction of the mouse wouldn't be that snappy like when you have it directly connected.

    @ paines:
    Thanks for sharing your experience.
    Our setup's are really quite the same. I'm wondering that the usb port's can be so difficult. Seems that i had quite a luck with mine since i had no problems at all with them. To my understanding from the xen wiki it's only possible to passthrough full bus and not a single usb slot, thus i checked my lspci an saw that i had 4 buses with usb slots. One of them had 4 usb slots which was perfectly for my xen-vm (1-mouse, 2-keyboard, 3-usb-audio, 4-free one) Anyway, good to know that via makes troubles here :)
    For the nvidia cards i can't say anything. I always bought ati cards for my pc's and i guess i'll stay there :)
    After all you're right, it's really alot of time you spend on xen to get things working, however a console isn't what i want and since my brother has an ps3 anyway i just had to go there ;)

    1. I was skeptical at first, but I have used synergy for over a year now over Fast Ethernet with 2-4 separate physical machines and never noticed any issues with responsiveness. Whether or not my experience here will translate over to the virtual machines I don't know, but I'd be interested in your findings if/when you do try it out.

  6. Typo in the grub conf lines: pci=resource_aligment=... needs an 'n' in the word "alignment" eg: pci=resource_alignment=...

    also the xen wiki says to use semi-colons ";" (not comma ",") as the pci=resource_alignment= seperator:

    1. Thanks for the hint. I was surprised about my typo, because it meant pci=resource_alignment never take an effect. However i've correct it and found out it's actually a bad idea.
      First, i couldn't add the usb devices (00:12.0,00:12.2) to resource_alignment - the kernel wouldn't boot.
      Secondly, enabling it just the graphic card took the effect that i always had an gpu lookup when starting X and many AMD-Vi errors in "xl dmesg".

      I couldn't find any detailed information about "pci=resource_alignment", but for now i removed it from the boot parameter since prassthru works anyway...

  7. For your USB switcher you said you bought 2 so you could add both devices, did you try plugging a hub into the switcher? I already have a hub I could use but need to know if I should order 1 or 2 of these switchers.

    1. Unfortunately i don't have an usb hub to test it and i cannot say if it would work. :(

  8. Hi,
    May I ask what are the video cards you are using? You've mentioned the higher end ati for DomU windows, what about for Dom0 Linux? And what driver?
    I'm trying out xen myself and I'm looking for a card for Dom0 Linux with 3d accel. I was playing with a nvidia gtx460 and as expected it's mostly headache since it's not officially supported by the closed source driver.

    1. Sure,
      DomU (Windows7): XFX R7970 Radeon H7970 Black Edition
      Dom0: PowerColor Radeon HD6850
      For the HD6850 on linux i'm using the xf86-video-ati drivers. Those drivers working pretty well and they even have 3d accel.
      You can see my full system setup on a older blog post here:

    2. Thanks for your reply!
      Someone suggested that i shouldn't be doing 3d and stuff on dom0. Instead I could leave Dom0 as just a light weight xen control.
      Then I run linux and windows on DomUs with vga passthru.
      I do know that nvidia vga passthru requires some patches to xen. I guess I'll see how that goes.
      Or perhaps if I have enough with nvidia, I'll get some ati cards to try.
      Thanks again!

  9. Thanks for the youtube replies. Between your blog, and I've managed to get a working base Xen kernel installed. I've not started a domU yet, the biggest nightmare I've had so far was getting UEFI, Hardened Sources and Grub2 to work nicely together. (Dual-booting until I get it working) Have you had any luck with Windows 8 yet or just only Windows 7 x64?

    Also, any better solution to the audio drivers problem? I'm thinking... surely I should be able to pci-passthrough my onboard intel hd right... which would solve the audio emulation problem.

    1. Dom0 running in the background okay, Windows 8 x64 on my monitor with my HD6950 / Alienware TactX Keyboard + Mouse all passed through. A little bit laggy on the mouse movement, but this is all on a single SATA while installing Diablo 3 (for my performance test).

    2. Nice to hear your got it working. Regarding Audio: As you already wrote you can passthrough your onboard Audio Card. However, xen devs working toward using qemu upstream for xen-4.3 [1], which would mean newer Audio Devices for xen and thus supported audio hardware for actual windows guest. Unfortunately we still have to wait a few months.


  10. Sorry to revive this, but im interested in trying this out myself, I just can't find info related to my question....

    I've seen some videos where a Xen server (a netbook) seems to be hosting the HVMs, I might be wrong, but Vmware ESX seems to work that way so I´ve been reading a bit more about Xen.

    Do I need another computer to run a setup like yours?

    For what I've read, seems like some linux distros have a Xen kernel (or you can install one), so it seems to me than server and hypervisor reside in the same computer than dom0 and the domUs, Am I wrong?

    Any help will be appreciated.

    My hardware:
    Motherboard: Asus M5A99X EVO Rev1
    CPU: FX-8350
    Cooler: Noctua NH-D14
    RAM: Corsair 4x2 CMZ4GX3M1A1600C9
    Video card 1: Asus Radeon HD 7970 DirectCUII
    Video card 2: MSI Radeon HD 6950 TwinFrozr
    SSD: Liteon LCM-256M3S 6Gbps 256GB
    Storage: 2Tb Seagate barracuda & 3Tb barracuda
    Optical: Pioner 16x BD-RW
    Optional: 2 External HDD, 1 on Docking, 1 from Power eSata
    PSU: Corsair TX850w
    Chasis: ThermalTake Armor Revo Snow

    1. Hi,

      You're not wrong. Like my setup you only need *one* Computer. Both dom0 and the domUs reside on the same machine. HowTo install and configure xen is explained in my blog post(s) (which is still correct, just have a look at my further posts about xen to get the newest information).
      However, keep in mind i've wrote this howto for gentoo, i don't know if it applies to other distros, but i guess mostly yes. :)

      Regarding Xen-Kernel: It's not exactly a complete kernel which you install with the xen package on gentoo. You need to install the "xen vitrual maschine monitor" (app-emulation/xen) which you use with a typical kernel, which has however xen features enabled. (see blog post) Other distros might have a pre-configured "xen kernel".

      Looking at your Hardware it seems you should be able to do such setup like mine. Make sure IOMMU is enabled in the BIOS. ;)

  11. Very usefull, i almost have the same configuration with a debian host.

  12. This post is so informative and makes a very nice image on the topic in my mind. It is the first time I visit your blog, but I was extremely impressed. Keep posting as I am gonna come to read it everyday!
    Gaming monitor