Use helpers defined in pyjailhouse/sysfs_parsers.py to gather the information
needed to determine if a machine can run jailhouse from sysfs.  Make checking
sysfs the default action when running jailhouse-hardware-check (instead of
checking a compiled configuration file).

Minor changes to pyjailhouse import warning.

Signed-off-by: Chris Goldsworthy <[email protected]>
---
 Documentation/pyjailhouse.md   |  4 +--
 tools/jailhouse-cell-linux     |  4 +--
 tools/jailhouse-hardware-check | 73 ++++++++++++++++++++++++++++--------------
 3 files changed, 53 insertions(+), 28 deletions(-)

diff --git a/Documentation/pyjailhouse.md b/Documentation/pyjailhouse.md
index 14fcf7d..5ee8466 100644
--- a/Documentation/pyjailhouse.md
+++ b/Documentation/pyjailhouse.md
@@ -55,6 +55,6 @@ For the example, the following wouldn't work inside of boz.py:
 `
 sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + ""
 from pyjailhouse import
-# Imports from directory containing this must be done above
-# import statement, see python documentation on sys.path[0]
+# Imports from directory containing this script must be done above the
+# line setting sys.path[0], see python documentation on sys.path[0]
 `
\ No newline at end of file
diff --git a/tools/jailhouse-cell-linux b/tools/jailhouse-cell-linux
index c3cee12..b6c0c75 100755
--- a/tools/jailhouse-cell-linux
+++ b/tools/jailhouse-cell-linux
@@ -19,8 +19,8 @@ import sys
 
 sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/.."
 from pyjailhouse.cell import JailhouseCell
-# Imports from directory containing this must be done above
-# import statement, see python documentation on sys.path[0]
+# Imports from directory containing this script must be done above the
+# line setting sys.path[0], see python documentation on sys.path[0]
 
 libexecdir = None
 
diff --git a/tools/jailhouse-hardware-check b/tools/jailhouse-hardware-check
index ae4879d..978a1e4 100755
--- a/tools/jailhouse-hardware-check
+++ b/tools/jailhouse-hardware-check
@@ -16,6 +16,12 @@ import mmap
 import os
 import struct
 import sys
+import argparse
+
+sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/.."
+import pyjailhouse.sysfs_parsers as sp
+# Imports from directory containing this script must be done above the
+# line setting sys.path[0], see python documentation on sys.path[0]
 
 # just a dummy to make python2 happy
 if sys.version_info[0] < 3:
@@ -24,6 +30,7 @@ if sys.version_info[0] < 3:
 
 check_passed = True
 ran_all = True
+iommu = None
 
 def check_feature(msg, ok, optional=False):
     if not (ok or optional):
@@ -155,7 +162,7 @@ class Sysconfig:
                          Sysconfig.PCIISVIRT_SIZE + Sysconfig.PCIDOMAIN_SIZE +
                          Sysconfig.X86_PADDING)
 
-        keys = 'base size amd_bdf amd_base_cap amd_features'
+        keys = 'base_addr mmio_size amd_bdf amd_base_cap amd_features'
         IOMMU = collections.namedtuple('IOMMU', keys)
 
         iommus = []
@@ -166,23 +173,34 @@ class Sysconfig:
         return iommus
 
 
-def usage(exit_code):
-    prog = os.path.basename(sys.argv[0]).replace('-', ' ')
-    print('usage: %s SYSCONFIG' % prog)
-    sys.exit(exit_code)
-
-
-if len(sys.argv) != 2:
-    usage(1)
-if sys.argv[1] in ("--help", "-h"):
-    usage(0)
+parser = argparse.ArgumentParser(description="Determine if this machine is "
+                                 "capable of running Jailhouse. By default, "
+                                 "this script pulls information from sysfs "
+                                 "to determine this. You can also provide a "
+                                 "root cell configuration, whose information "
+                                 "will be used instead of what comes from "
+                                 "sysfs.",
+                                 usage="jailhouse hardware check "
+                                 "[--root <dir> | --sysconfig <sysconfig>]")
+parser.add_argument("--sysconfig",
+                    help="sysconfig file to check")
+parser.add_argument("--root",
+                    help="root directory from which sysfs files should be "
+                         "read",
+                    default="/")
+arg = parser.parse_args()
 
 if os.uname()[4] not in ('x86_64', 'i686'):
     print('Unsupported architecture', file=sys.stderr)
     sys.exit(1)
 
-config = Sysconfig(sys.argv[1])
-iommu = config.parse_iommus()
+if arg.sysconfig is not None:
+    config = Sysconfig(arg.sysconfig)
+    iommu = config.parse_iommus()
+else:
+    sp.root_dir = arg.root
+    ioapics = sp.parse_madt()
+    pci_devices, _, _ = sp.parse_pcidevices()
 
 (cpu_vendor, cpu_features, cpu_count) = parse_cpuinfo()
 
@@ -198,6 +216,10 @@ check_feature('Number of CPUs > 1', cpu_count > 1)
 check_feature('Long mode', 'lm' in cpu_features)
 
 if cpu_vendor == 'GenuineIntel':
+    if iommu is None:
+        _, dmar_regions = sp.parse_iomem(pci_devices)
+        iommu, _ = sp.parse_dmar(pci_devices, ioapics, dmar_regions)
+
     check_feature('x2APIC', 'x2apic' in cpu_features, True)
     print()
     check_feature('VT-x (VMX)', 'vmx' in cpu_features)
@@ -247,14 +269,14 @@ if cpu_vendor == 'GenuineIntel':
     check_feature('  Activity state HLT',
                   msr.read(MSR.IA32_VMX_MISC) & (1 << 6))
 
-    for n in range(8):
-        if iommu[n].base == 0 and n > 0:
+    for n in range(len(iommu)):
+        if iommu[n].base_addr == 0 and n > 0:
             break
         print()
-        check_feature('VT-d (IOMMU #%d)' % n, iommu[n].base)
-        if iommu[n].base == 0:
+        check_feature('VT-d (IOMMU #%d)' % n, iommu[n].base_addr)
+        if iommu[n].base_addr == 0:
             break
-        mmio = MMIO(iommu[n].base, iommu[n].size)
+        mmio = MMIO(iommu[n].base_addr, iommu[n].mmio_size)
         cap = mmio.read64(0x08)
         if cap is None:
             continue
@@ -269,6 +291,9 @@ if cpu_vendor == 'GenuineIntel':
                       'x2apic' not in cpu_features)
 
 elif cpu_vendor == 'AuthenticAMD':
+    if iommu is None:
+        iommu, _ = sp.parse_ivrs(pci_devices, ioapics)
+
     print()
     check_feature('AMD-V (SVM)', 'svm' in cpu_features)
     check_feature('  NPT', 'npt' in cpu_features)
@@ -276,12 +301,12 @@ elif cpu_vendor == 'AuthenticAMD':
     check_feature('  AVIC', 'avic' in cpu_features, True)
     check_feature('  Flush by ASID', 'flushbyasid' in cpu_features, True)
 
-    for n in range(8):
-        if iommu[n].base == 0 and n > 0:
+    for n in range(len(iommu)):
+        if iommu[n].base_addr == 0 and n > 0:
             break
         print()
-        check_feature('AMD-Vi (IOMMU #%d)' % n, iommu[n].base)
-        if iommu[n].base == 0:
+        check_feature('AMD-Vi (IOMMU #%d)' % n, iommu[n].base_addr)
+        if iommu[n].base_addr == 0:
             break
 
         bdf = iommu[n].amd_bdf
@@ -294,9 +319,9 @@ elif cpu_vendor == 'AuthenticAMD':
         check_feature('  Extended feature register', caps & (1 << 27))
         check_feature('  Valid base register',
                       (base & (1 << 0)) == 0 or
-                      base == (iommu[n].base | (1 << 0)))
+                      base == (iommu[n].base_addr | (1 << 0)))
 
-        mmio = MMIO(iommu[n].base, iommu[n].size)
+        mmio = MMIO(iommu[n].base_addr, iommu[n].mmio_size)
         efr = mmio.read64(0x30)
         if efr is None:
             continue
-- 
2.7.4

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to