Adds two checks for CPU specification:
 - overlap check detects two inmates using the same CPU(s)
 - boundary check detects CPU usage outside of what system
   config provides

Signed-off-by: Andrej Utz <[email protected]>
---
 pyjailhouse/config_parser.py | 14 ++++++++++++--
 tools/jailhouse-config-check | 28 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/pyjailhouse/config_parser.py b/pyjailhouse/config_parser.py
index 2b47d9b6..2a0e6ec9 100644
--- a/pyjailhouse/config_parser.py
+++ b/pyjailhouse/config_parser.py
@@ -192,17 +192,19 @@ class PIORegion(CStruct):
 
 class CellConfig(CStruct):
     # slots with a '_' prefix in name are private
-    __slots__ = 'name', '_flags', '_cpu_sets', \
+    __slots__ = 'name', '_flags', 'cpu_set', \
                 'memory_regions', 'cache_regions', 'irqchips', 'pio_regions', \
                 '_pci_devices', '_pci_caps', '_stream_ids', \
                 'vpci_irq_base', 'cpu_reset_address',
     _BIN_FIELD_NUM = len(__slots__)
     _BIN_FMT = struct.Struct('=32s4xIIIIIIIIIIQ8x32x')
     _BIN_FMT_HDR = struct.Struct('=6sH')
+    _BIN_FMT_CPU = struct.Struct('=Q')
     _BIN_SIGNATURE = b'JHCELL'
 
     def __init__(self):
         self.name = ""
+        self.cpu_set = set()
         self.memory_regions = []
         self.irqchips = []
         self.pio_regions = []
@@ -213,7 +215,15 @@ class CellConfig(CStruct):
     def parse(cls, stream):
         self = cls.parse_class(cls, stream)
         self.name = self.name.decode().strip('\0')
-        stream.seek(self._cpu_sets, io.SEEK_CUR) # skip CPU set
+
+        cpu_fmt = cls._BIN_FMT_CPU
+        cpu_set_num = int(self.cpu_set / cpu_fmt.size)
+        self.cpu_set = set()
+        for set_idx in range(cpu_set_num):
+            cpu_bits = cpu_fmt.unpack_from(stream.read(cpu_fmt.size))
+            for bit in range(cpu_fmt.size * 8):
+                if cpu_bits[0] & (1 << bit) > 0:
+                    self.cpu_set.add(bit)
 
         self.memory_regions = \
             cls.parse_array(MemRegion, self.memory_regions, stream)
diff --git a/tools/jailhouse-config-check b/tools/jailhouse-config-check
index 380f4a77..33d9110f 100755
--- a/tools/jailhouse-config-check
+++ b/tools/jailhouse-config-check
@@ -66,6 +66,7 @@ for cfg in args.cellcfgs:
 
 ret=0
 
+# Memory checks
 print("Overlapping memory regions inside cell:", end='')
 found=False
 for cell in cells:
@@ -100,4 +101,31 @@ for cell in cells:
             ret=1
 print("\n" if found else " None")
 
+# CPU checks
+print("Overlapping CPUs between cells:", end='')
+found = False
+for cell in non_root_cells:
+    cell_idx = cells.index(cell)
+    for cell2 in cells[cell_idx + 1:]:
+        overlap = cell.cpu_set & cell2.cpu_set
+        if overlap:
+            print("\n\nIn cell '%s' and '%s' following CPUs overlap: %s" %
+                  (cell.name, cell2.name, str(overlap)), end='')
+            found = True
+            ret = 1
+print("\n" if found else " None")
+
+print("CPUs not in root cell CPU set:", end='')
+found = False
+for cell in non_root_cells:
+    diff = cell.cpu_set - root_cell.cpu_set
+    if diff:
+        print("\n\nIn cell '%s': %s" % (cell.name, str(diff)), end='')
+        found = True
+        ret = 1
+if found:
+    print("\nNote: root cell CPU set: %s\n" % str(root_cell.cpu_set))
+else:
+    print(" None")
+
 exit(ret)
-- 
2.28.0

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jailhouse-dev/20200825145032.115837-8-andrej.utz%40st.oth-regensburg.de.

Reply via email to