3 # Jailhouse, a Linux-based partitioning hypervisor
5 # Copyright (c) Siemens AG, 2014
6 # Copyright (c) Valentine Sinitsyn, 2014
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
11 # This script should help to create a basic jailhouse configuration file.
12 # It needs to be executed on the target machine, where it will gather
13 # information about the system. For more advanced scenarios you will have
14 # to change the generated C-code.
16 from __future__ import print_function
23 from mako.template import Template
28 template_default_dir = datadir + "/jailhouse"
30 template_default_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
34 # pretend to be part of the jailhouse tool
35 sys.argv[0] = sys.argv[0].replace('-', ' ')
37 parser = argparse.ArgumentParser()
38 parser.add_argument('-g', '--generate-collector',
39 help='generate a script to collect input files on '
42 parser.add_argument('-r', '--root',
43 help='gather information in ROOT/, the default is "/" '
44 'which means creating a config for localhost',
48 parser.add_argument('-t', '--template-dir',
49 help='the directory where the templates are located,'
50 'the default is "' + template_default_dir + '"',
51 default=template_default_dir,
55 memargs = [['--mem-inmates', '2M', 'inmate'],
56 ['--mem-hv', '64M', 'hypervisor']]
59 parser.add_argument(entry[0],
60 help='the amount of ' + entry[2] +
61 ' memory, default is "' + entry[1] +
62 '", format "xxx[K|M|G]"',
67 parser.add_argument('file', metavar='FILE',
68 help='name of file to write out',
71 options = parser.parse_args()
73 inputs = {'files': set(), 'files_opt': set(), 'files_intel': set()}
76 inputs['files'].add('/proc/iomem')
77 inputs['files'].add('/proc/cpuinfo')
78 inputs['files'].add('/proc/cmdline')
79 inputs['files'].add('/proc/ioports')
80 inputs['files'].add('/sys/bus/pci/devices/*/config')
81 inputs['files'].add('/sys/bus/pci/devices/*/class')
82 inputs['files'].add('/sys/devices/system/cpu/cpu*/uevent')
83 inputs['files'].add('/sys/firmware/acpi/tables/MCFG')
84 inputs['files'].add('/sys/firmware/acpi/tables/FACP')
86 inputs['files_opt'].add('/sys/class/dmi/id/product_name')
87 inputs['files_opt'].add('/sys/class/dmi/id/sys_vendor')
88 inputs['files_opt'].add('/sys/devices/jailhouse/enabled')
89 ## platform specific files
90 inputs['files_intel'].add('/sys/firmware/acpi/tables/DMAR')
93 def kmg_multiply(value, kmg):
94 if (kmg == 'K' or kmg == 'k'):
96 if (kmg == 'M' or kmg == 'm'):
97 return 1024**2 * value
98 if (kmg == 'G' or kmg == 'g'):
99 return 1024**3 * value
103 def kmg_multiply_str(str):
104 m = re.match(r'([0-9a-fA-FxX]+)([KMG]?)', str)
106 return kmg_multiply(int(m.group(1)), m.group(2))
107 raise RuntimeError('kmg_multiply_str can not parse input "' + str + '"')
110 def check_input_listed(name, optional=False):
111 set = inputs['files_opt']
112 if optional is False:
113 set = inputs['files']
115 if cpuvendor == 'GenuineIntel':
116 set = set.union(inputs['files_intel'])
119 if fnmatch.fnmatch(name, file):
121 raise RuntimeError('"' + name + '" is not a listed input file')
124 def input_open(name, mode='r', optional=False):
125 check_input_listed(name, optional)
127 f = open(options.root + name, mode)
128 except Exception as e:
130 return open("/dev/null", mode)
135 def input_readline(name, optional=False):
136 f = input_open(name, optional=optional)
142 def input_listdir(dir, wildcards):
144 check_input_listed(os.path.join(dir, w))
145 dirs = os.listdir(options.root + dir)
151 def __init__(self, id, start, len, flags, content, msix_address):
156 self.content = content
157 self.msix_address = msix_address
160 def __eq__(self, other):
161 return self.id == other.id and self.start == other.start and \
162 self.len == other.len and self.flags == other.flags
165 RW = 'JAILHOUSE_PCICAPS_WRITE'
168 def parse_pcicaps(dir):
170 f = input_open(os.path.join(dir, 'config'), 'rb')
172 (status,) = struct.unpack('<H', f.read(2))
173 # capability list supported?
174 if (status & (1 << 4)) == 0:
177 # walk capability list
179 (next,) = struct.unpack('B', f.read(1))
184 (id, next) = struct.unpack('<BB', f.read(2))
185 if id == 0x01: # Power Management
186 # this cap can be handed out completely
188 flags = PCICapability.RW
189 elif id == 0x05: # MSI
190 # access will be moderated by hypervisor
192 (msgctl,) = struct.unpack('<H', f.read(2))
193 if (msgctl & (1 << 7)) != 0: # 64-bit support
195 if (msgctl & (1 << 8)) != 0: # per-vector masking support
197 flags = PCICapability.RW
198 elif id == 0x11: # MSI-X
199 # access will be moderated by hypervisor
201 (table,) = struct.unpack('<xxI', f.read(6))
202 f.seek(0x10 + (table & 7) * 4)
203 (bar,) = struct.unpack('<I', f.read(4))
205 raise RuntimeError('Invalid MSI-X BAR found')
207 bar |= struct.unpack('<I', f.read(4))[0] << 32
208 msix_address = (bar & 0xfffffffffffffff0) + table & 0xfffffff8
209 flags = PCICapability.RW
211 # unknown/unhandled cap, mark its existence
213 flags = PCICapability.RD
215 content = f.read(len - 2)
216 caps.append(PCICapability(id, cap, len, flags, content,
222 def __init__(self, type, domain, bus, dev, fn, caps):
231 self.num_caps = len(caps)
232 self.num_msi_vectors = 0
234 self.num_msix_vectors = 0
235 self.msix_region_size = 0
236 self.msix_address = 0
238 if c.id in (0x05, 0x11):
239 msg_ctrl = struct.unpack('<H', c.content[:2])[0]
240 if c.id == 0x05: # MSI
241 self.num_msi_vectors = 1 << ((msg_ctrl >> 1) & 0x7)
242 self.msi_64bits = (msg_ctrl >> 7) & 1
244 vectors = (msg_ctrl & 0x7ff) + 1
245 self.num_msix_vectors = vectors
246 self.msix_region_size = (vectors * 16 + 0xfff) & 0xf000
247 self.msix_address = c.msix_address
250 return 'PCIDevice: %02x:%02x.%x' % (self.bus, self.dev, self.fn)
253 return self.bus << 8 | self.dev << 3 | self.fn
256 def parse_pcidevice_sysfsdir(basedir, dir):
257 dpath = os.path.join(basedir, dir)
258 dclass = input_readline(os.path.join(dpath, 'class'))
259 if re.match(r'0x0604..', dclass):
260 type = 'JAILHOUSE_PCI_TYPE_BRIDGE'
262 type = 'JAILHOUSE_PCI_TYPE_DEVICE'
264 domain = int(a[0], 16)
267 caps = PCICapability.parse_pcicaps(dpath)
268 return PCIDevice(type, domain, bus, int(df[0], 16), int(df[1], 16),
273 def __init__(self, start, stop, typestr, comments=None):
276 self.typestr = typestr
280 self.comments = comments
283 return 'MemRegion: %08x-%08x : %s' % \
284 (self.start, self.stop, self.typestr)
287 # round up to full PAGE_SIZE
288 return int((self.stop - self.start + 0xfff) / 0x1000) * 0x1000
290 def flagstr(self, p=''):
292 self.typestr == 'System RAM' or
293 self.typestr == 'RAM buffer' or
294 self.typestr == 'ACPI DMAR RMRR'
296 s = 'JAILHOUSE_MEM_READ | JAILHOUSE_MEM_WRITE |\n'
297 s += p + '\t\tJAILHOUSE_MEM_EXECUTE | JAILHOUSE_MEM_DMA'
299 return 'JAILHOUSE_MEM_READ | JAILHOUSE_MEM_WRITE'
302 class IOMemRegionTree:
303 def __init__(self, region, level, linenum):
305 self.linenum = linenum
313 s = (' ' * (self.level - 1)) + str(self.region) + ' line %d' \
315 if self.parent and self.parent.region:
316 s += ' --> ' + self.parent.region.typestr + ' line %d' \
317 % (self.parent.linenum)
319 for c in self.children:
324 def parse_iomem_line(line):
325 a = line.split(':', 1)
326 level = int(a[0].count(' ') / 2) + 1
327 region = a[0].split('-', 1)
329 return level, MemRegion(int(region[0], 16), int(region[1], 16), a[1])
332 def parse_iomem_file():
333 root = IOMemRegionTree(None, 0, -1)
334 f = input_open('/proc/iomem')
339 (level, r) = IOMemRegionTree.parse_iomem_line(line)
340 t = IOMemRegionTree(r, level, linenum)
341 if (t.level > lastlevel):
343 if (t.level == lastlevel):
344 t.parent = lastnode.parent
345 if (t.level < lastlevel):
347 while(t.level < p.level):
351 t.parent.children.append(t)
359 # recurse down the tree
361 def parse_iomem_tree(tree):
364 for tree in tree.children:
368 # System RAM on first level will be added without digging deeper
369 if (tree.level == 1 and s == 'System RAM'):
373 # blacklisted on all levels
375 (s.find('PCI MMCONFIG') >= 0) or
376 (s.find('APIC') >= 0) or # covers both APIC and IOAPIC
377 (s.find('dmar') >= 0)
381 # generally blacklisted, unless we find an HPET right behind it
383 if (s == 'reserved'):
384 for subtree in tree.children:
386 if (r2.typestr.find('HPET') >= 0):
390 # if the tree continues recurse further down ...
391 if (len(tree.children) > 0):
392 regions.extend(IOMemRegionTree.parse_iomem_tree(tree))
395 # add all remaining leaves
401 def parse_iomem(pcidevices):
402 regions = IOMemRegionTree.parse_iomem_tree(
403 IOMemRegionTree.parse_iomem_file())
405 # filter the list for MSI-X pages
409 if d.msix_address >= r.start and d.msix_address <= r.stop:
410 if d.msix_address > r.start:
411 head_r = MemRegion(r.start, d.msix_address - 1,
412 r.typestr, r.comments)
414 if d.msix_address + d.msix_region_size < r.stop:
415 tail_r = MemRegion(d.msix_address + d.msix_region_size,
416 r.stop, r.typestr, r.comments)
423 # newer Linux kernels will report the first page as reserved
424 # it is needed for CPU init so include it anyways
425 if (ret[0].typestr == 'System RAM' and ret[0].start == 0x1000):
431 def parse_pcidevices():
434 basedir = '/sys/bus/pci/devices'
435 list = input_listdir(basedir, ['*/class', '*/config'])
437 d = PCIDevice.parse_pcidevice_sysfsdir(basedir, dir)
441 # look for duplicate capability patterns
443 if d2.caps == d.caps:
444 # reused existing capability list, but record all users
445 d2.caps[0].comments.append(str(d))
446 d.caps_start = d2.caps_start
450 d.caps[0].comments.append(str(d))
451 d.caps_start = len(caps)
454 return (devices, caps)
457 def parse_kernel_cmdline():
458 line = input_readline('/proc/cmdline')
459 m = re.match(r'.*memmap=([0-9a-fA-FxX]+)([KMG]?)\$'
460 '([0-9a-fA-FxX]+)([KMG]?).*',
463 size = kmg_multiply(int(m.group(1), 0), m.group(2))
464 start = kmg_multiply(int(m.group(3), 0), m.group(4))
469 def alloc_mem(regions, size):
470 mem = [0x3b000000, size]
473 r.typestr == 'System RAM' and
474 r.start <= mem[0] and
475 r.stop + 1 >= mem[0] + mem[1]
478 head_r = MemRegion(r.start, mem[0] - 1, r.typestr, r.comments)
479 regions.insert(regions.index(r), head_r)
480 if r.stop + 1 > mem[0] + mem[1]:
481 tail_r = MemRegion(mem[0] + mem[1], r.stop, r.typestr,
483 regions.insert(regions.index(r), tail_r)
486 for r in reversed(regions):
487 if (r.typestr == 'System RAM' and r.size() >= mem[1]):
491 raise RuntimeError('failed to allocate memory')
495 list = input_listdir('/sys/devices/system/cpu', ['cpu*/uevent'])
498 if re.match(r'cpu[0-9]+', f):
503 def parse_dmar_devscope(f):
504 (scope_type, scope_len, bus, dev, fn) = \
505 struct.unpack('<BBxxxBBB', f.read(8))
507 raise RuntimeError('Unsupported DMAR Device Scope Structure')
508 return (scope_type, scope_len, bus, dev, fn)
511 # parsing of DMAR ACPI Table
512 # see Intel VT-d Spec chapter 8
513 def parse_dmar(pcidevices):
514 f = input_open('/sys/firmware/acpi/tables/DMAR', 'rb')
515 signature = f.read(4)
516 if signature != b'DMAR':
517 raise RuntimeError('DMAR: incorrect input file format %s' % signature)
518 (length,) = struct.unpack('<I', f.read(4))
527 (struct_type, struct_len) = struct.unpack('<HH', f.read(4))
531 # DMA Remapping Hardware Unit Definition
533 (flags, segment, base) = struct.unpack('<BxHQ', f.read(12))
535 raise RuntimeError('We do not support multiple PCI segments')
537 raise RuntimeError('Too many DMAR units. '
538 'Raise JAILHOUSE_MAX_DMAR_UNITS.')
543 d.iommu = len(units) - 1
544 offset += 16 - offset
545 while offset < struct_len:
546 (scope_type, scope_len, bus, dev, fn) =\
547 parse_dmar_devscope(f)
550 if d.bus == bus and d.dev == dev and d.fn == fn:
551 d.iommu = len(units) - 1
552 elif scope_type == 2:
553 raise RuntimeError('Unsupported DMAR Device Scope type')
554 elif scope_type == 3:
556 raise RuntimeError('We do not support more '
558 # encode the DMAR unit number into the device ID
559 ioapic_id = ((len(units) - 1) << 16) | \
560 (bus << 8) | (dev << 3) | fn
563 # Reserved Memory Region Reporting Structure
565 f.seek(8 - offset, os.SEEK_CUR)
567 (base, limit) = struct.unpack('<QQ', f.read(16))
571 while offset < struct_len:
572 (scope_type, scope_len, bus, dev, fn) =\
573 parse_dmar_devscope(f)
575 comments.append('PCI device: %02x:%02x.%x' %
578 comments.append('DMAR parser could not decode device path')
581 reg = MemRegion(base, limit, 'ACPI DMAR RMRR', comments)
584 f.seek(struct_len - offset, os.SEEK_CUR)
586 return units, ioapic_id, regions
591 f = input_open('/proc/ioports')
593 if line.endswith('ACPI PM_TMR\n'):
594 pm_timer_base = int(line.split('-')[0], 16)
602 with input_open('/sys/firmware/acpi/tables/FACP', 'rb') as f:
603 signature = f.read(4)
604 if signature != b'FACP':
605 raise RuntimeError('FACP: incorrect input file format %s' %
608 (flags,) = struct.unpack('>I', f.read(4))
609 tmr_val_ext = (flags & 256) >> 8
614 def __init__(self, base, end_bus):
616 self.end_bus = end_bus
620 f = input_open('/sys/firmware/acpi/tables/MCFG', 'rb')
621 signature = f.read(4)
622 if signature != b'MCFG':
623 raise RuntimeError('MCFG: incorrect input file format %s' %
625 (length,) = struct.unpack('<I', f.read(4))
627 raise RuntimeError('Multiple MMCONFIG regions found! '
628 'This is not supported')
630 (base, segment, start_bus, end_bus) = \
631 struct.unpack('<QHBB', f.read(12))
632 if segment != 0 or start_bus != 0:
633 raise RuntimeError('Invalid MCFG structure found')
634 return MMConfig(base, end_bus)
637 def get_cpu_vendor():
639 if cpuvendor is not None:
641 with input_open('/proc/cpuinfo', 'r') as f:
645 key, value = line.split(':')
646 if key.strip() == 'vendor_id':
647 cpuvendor = value.strip()
651 if options.generate_collector:
652 f = open(options.file, 'w')
653 filelist = ' '.join(inputs['files'])
654 filelist_opt = ' '.join(inputs['files_opt'])
655 filelist_intel = ' '.join(inputs['files_intel'])
657 tmpl = Template(filename=os.path.join(options.template_dir,
658 'jailhouse-config-collect.tmpl'))
659 f.write(tmpl.render(filelist=filelist, filelist_opt=filelist_opt,
660 filelist_intel=filelist_intel))
664 if ((options.root is '/') and (os.geteuid() is not 0)):
665 print('ERROR: You have to be root to work on "/"!', file=sys.stderr)
668 jh_enabled = input_readline('/sys/devices/jailhouse/enabled', True).rstrip()
669 if jh_enabled == '1':
670 print('ERROR: Jailhouse was enabled when collecting input files! '
671 'Disable jailhouse and try again.',
675 (pcidevices, pcicaps) = parse_pcidevices()
677 product = [input_readline('/sys/class/dmi/id/sys_vendor',
679 input_readline('/sys/class/dmi/id/product_name',
683 inmatemem = kmg_multiply_str(options.mem_inmates)
684 hvmem = [0, kmg_multiply_str(options.mem_hv)]
686 regions = parse_iomem(pcidevices)
687 ourmem = parse_kernel_cmdline()
688 total = hvmem[1] + inmatemem
690 mmconfig = MMConfig.parse()
692 if get_cpu_vendor() == 'GenuineIntel':
693 (dmar_units, ioapic_id, rmrr_regs) = parse_dmar(pcidevices)
695 (dmar_units, ioapic_id, rmrr_regs) = [], 0, []
699 if get_cpu_vendor() == 'AuthenticAMD':
700 d.iommu = 0 # temporary workaround
702 raise RuntimeError('PCI device %02x:%02x.%x outside the scope of an '
703 'IOMMU' % (d.bus, d.dev, d.fn))
705 # kernel does not have memmap region, pick one
707 ourmem = alloc_mem(regions, total)
708 elif (total > ourmem[1]):
709 raise RuntimeError('Your memmap reservation is too small you need >="' +
714 inmatereg = MemRegion(ourmem[0] + hvmem[1],
715 ourmem[0] + hvmem[1] + inmatemem - 1,
716 'JAILHOUSE Inmate Memory')
717 regions.append(inmatereg)
719 cpucount = count_cpus()
721 pm_timer_base = parse_ioports()
723 pm_timer_val_ext = parse_facp()
725 f = open(options.file, 'w')
726 tmpl = Template(filename=os.path.join(options.template_dir,
727 'root-cell-config.c.tmpl'))
728 f.write(tmpl.render(regions=regions,
730 argstr=' '.join(sys.argv),
733 pcidevices=pcidevices,
737 pm_timer_base=pm_timer_base,
738 pm_timer_val_ext=pm_timer_val_ext,
740 dmar_units=dmar_units))