common.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. #!/usr/bin/env python3
  2. import argparse
  3. import base64
  4. import collections
  5. import copy
  6. import datetime
  7. import distutils.file_util
  8. import glob
  9. import imp
  10. import json
  11. import multiprocessing
  12. import os
  13. import re
  14. import shlex
  15. import shutil
  16. import signal
  17. import stat
  18. import subprocess
  19. import sys
  20. import time
  21. import urllib
  22. import urllib.request
  23. this_module = sys.modules[__name__]
  24. root_dir = os.path.dirname(os.path.abspath(__file__))
  25. data_dir = os.path.join(root_dir, 'data')
  26. p9_dir = os.path.join(data_dir, '9p')
  27. gem5_non_default_src_root_dir = os.path.join(data_dir, 'gem5')
  28. out_dir = os.path.join(root_dir, 'out')
  29. bench_boot = os.path.join(out_dir, 'bench-boot.txt')
  30. packages_dir = os.path.join(root_dir, 'packages')
  31. lkmc_package_src_dir = os.path.join(this_module.packages_dir, 'lkmc')
  32. kernel_modules_subdir = 'kernel_modules'
  33. kernel_modules_src_dir = os.path.join(this_module.root_dir, this_module.kernel_modules_subdir)
  34. userland_subdir = 'userland'
  35. userland_src_dir = os.path.join(this_module.root_dir, this_module.userland_subdir)
  36. userland_build_ext = '.out'
  37. include_subdir = 'include'
  38. include_src_dir = os.path.join(this_module.root_dir, this_module.include_subdir)
  39. submodules_dir = os.path.join(root_dir, 'submodules')
  40. buildroot_src_dir = os.path.join(submodules_dir, 'buildroot')
  41. crosstool_ng_src_dir = os.path.join(submodules_dir, 'crosstool-ng')
  42. linux_src_dir = os.path.join(submodules_dir, 'linux')
  43. linux_config_dir = os.path.join(this_module.root_dir, 'linux_config')
  44. rootfs_overlay_dir = os.path.join(this_module.root_dir, 'rootfs_overlay')
  45. extract_vmlinux = os.path.join(linux_src_dir, 'scripts', 'extract-vmlinux')
  46. qemu_src_dir = os.path.join(submodules_dir, 'qemu')
  47. parsec_benchmark_src_dir = os.path.join(submodules_dir, 'parsec-benchmark')
  48. ccache_dir = os.path.join('/usr', 'lib', 'ccache')
  49. default_build_id = 'default'
  50. arch_short_to_long_dict = collections.OrderedDict([
  51. ('x', 'x86_64'),
  52. ('a', 'arm'),
  53. ('A', 'aarch64'),
  54. ])
  55. all_archs = [arch_short_to_long_dict[k] for k in arch_short_to_long_dict]
  56. arch_choices = []
  57. for key in this_module.arch_short_to_long_dict:
  58. arch_choices.append(key)
  59. arch_choices.append(this_module.arch_short_to_long_dict[key])
  60. default_arch = 'x86_64'
  61. gem5_cpt_prefix = '^cpt\.'
  62. sha = subprocess.check_output(['git', '-C', root_dir, 'log', '-1', '--format=%H']).decode().rstrip()
  63. release_dir = os.path.join(this_module.out_dir, 'release')
  64. release_zip_file = os.path.join(this_module.release_dir, 'lkmc-{}.zip'.format(this_module.sha))
  65. github_repo_id = 'cirosantilli/linux-kernel-module-cheat'
  66. asm_ext = '.S'
  67. c_ext = '.c'
  68. header_ext = '.h'
  69. kernel_module_ext = '.ko'
  70. obj_ext = '.o'
  71. config_file = os.path.join(data_dir, 'config')
  72. command_prefix = '+ '
  73. if os.path.exists(config_file):
  74. config = imp.load_source('config', config_file)
  75. configs = {x:getattr(config, x) for x in dir(config) if not x.startswith('__')}
  76. class Component:
  77. def __init__(self):
  78. pass
  79. def build(self):
  80. '''
  81. Parse CLI, and to the build based on it.
  82. The actual build work is done by do_build in implementing classes.
  83. '''
  84. parser = this_module.get_argparse(
  85. argparse_args=self.get_argparse_args(),
  86. default_args=self.get_default_args(),
  87. )
  88. self.add_parser_arguments(parser)
  89. parser.add_argument(
  90. '--clean',
  91. help='Clean the build instead of building.',
  92. action='store_true',
  93. )
  94. parser.add_argument(
  95. '-j', '--nproc',
  96. help='Number of processors to use for the build. Default: use all cores.',
  97. type=int,
  98. default=multiprocessing.cpu_count(),
  99. )
  100. args = this_module.setup(parser)
  101. if not this_module.dry_run:
  102. start_time = time.time()
  103. if args.clean:
  104. self.clean(args)
  105. else:
  106. self.do_build(args)
  107. if not this_module.dry_run:
  108. end_time = time.time()
  109. this_module.print_time(end_time - start_time)
  110. def add_parser_arguments(self, parser):
  111. pass
  112. def clean(self, args):
  113. build_dir = self.get_build_dir(args)
  114. if build_dir is not None:
  115. this_module.rmrf(build_dir)
  116. def do_build(self, args):
  117. '''
  118. Do the actual main build work.
  119. '''
  120. raise NotImplementedError()
  121. def get_argparse_args(self):
  122. '''
  123. Extra arguments for argparse.ArgumentParser.
  124. '''
  125. return {}
  126. def get_build_dir(self, args):
  127. '''
  128. Build directory, gets cleaned by --clean if not None.
  129. '''
  130. return None
  131. def get_default_args(self):
  132. '''
  133. Default values for command line arguments.
  134. '''
  135. return {}
  136. def add_dry_run_argument(parser):
  137. parser.add_argument('--dry-run', default=False, action='store_true', help='''\
  138. Print the commands that would be run, but don't run them.
  139. We aim display every command that modifies the filesystem state, and generate
  140. Bash equivalents even for actions taken directly in Python without shelling out.
  141. mkdir are generally omitted since those are obvious.
  142. ''')
  143. def base64_encode(string):
  144. return base64.b64encode(string.encode()).decode()
  145. def write_string_to_file(path, string, mode='w'):
  146. global this_module
  147. if mode == 'a':
  148. redirect = '>>'
  149. else:
  150. redirect = '>'
  151. print_cmd("cat << 'EOF' {} {}\n{}\nEOF".format(redirect, path, string))
  152. if not this_module.dry_run:
  153. with open(path, 'a') as f:
  154. f.write(string)
  155. def copy_dir_if_update_non_recursive(srcdir, destdir, filter_ext=None):
  156. # TODO print rsync equivalent.
  157. os.makedirs(destdir, exist_ok=True)
  158. for basename in os.listdir(srcdir):
  159. src = os.path.join(srcdir, basename)
  160. if os.path.isfile(src):
  161. noext, ext = os.path.splitext(basename)
  162. if filter_ext is not None and ext == filter_ext:
  163. distutils.file_util.copy_file(
  164. src,
  165. os.path.join(destdir, basename),
  166. update=1,
  167. )
  168. def gem_list_checkpoint_dirs():
  169. '''
  170. List checkpoint directory, oldest first.
  171. '''
  172. global this_module
  173. prefix_re = re.compile(this_module.gem5_cpt_prefix)
  174. files = list(filter(lambda x: os.path.isdir(os.path.join(this_module.m5out_dir, x)) and prefix_re.search(x), os.listdir(this_module.m5out_dir)))
  175. files.sort(key=lambda x: os.path.getmtime(os.path.join(this_module.m5out_dir, x)))
  176. return files
  177. def get_argparse(default_args=None, argparse_args=None):
  178. '''
  179. Return an argument parser with common arguments set.
  180. :type default_args: Dict[str,str]
  181. :type argparse_args: Dict
  182. '''
  183. global this_module
  184. if default_args is None:
  185. default_args = {}
  186. if argparse_args is None:
  187. argparse_args = {}
  188. parser = argparse.ArgumentParser(
  189. formatter_class=argparse.RawTextHelpFormatter,
  190. **argparse_args
  191. )
  192. this_module.add_dry_run_argument(parser)
  193. parser.add_argument(
  194. '-a', '--arch', choices=this_module.arch_choices, default=this_module.default_arch,
  195. help='CPU architecture. Default: %(default)s'
  196. )
  197. parser.add_argument(
  198. '-b', '--baremetal',
  199. help='''\
  200. Use the given baremetal executable instead of the Linux kernel.
  201. If the path is absolute, it is used as is.
  202. If the path is relative, we assume that it points to a source code
  203. inside baremetal/ and then try to use corresponding executable.
  204. '''
  205. )
  206. parser.add_argument(
  207. '--buildroot-build-id',
  208. default=default_build_id,
  209. help='Buildroot build ID. Allows you to keep multiple separate gem5 builds. Default: %(default)s'
  210. )
  211. parser.add_argument(
  212. '--crosstool-ng-build-id', default=default_build_id,
  213. help='Crosstool-NG build ID. Allows you to keep multiple separate crosstool-NG builds. Default: %(default)s'
  214. )
  215. parser.add_argument(
  216. '--docker', default=False, action='store_true',
  217. help='''\
  218. Use the docker download Ubuntu root filesystem instead of the default Buildroot one.
  219. '''
  220. )
  221. parser.add_argument(
  222. '-g', '--gem5', default=False, action='store_true',
  223. help='Use gem5 instead of QEMU'
  224. )
  225. parser.add_argument(
  226. '--gem5-src',
  227. help='''\
  228. Use the given directory as the gem5 source tree. Ignore `--gem5-worktree`.
  229. '''
  230. )
  231. parser.add_argument(
  232. '-L', '--linux-build-id', default=default_build_id,
  233. help='Linux build ID. Allows you to keep multiple separate Linux builds. Default: %(default)s'
  234. )
  235. parser.add_argument(
  236. '--machine',
  237. help='''Machine type.
  238. QEMU default: virt
  239. gem5 default: VExpress_GEM5_V1
  240. See the documentation for other values known to work.
  241. '''
  242. )
  243. parser.add_argument(
  244. '-M', '--gem5-build-id',
  245. help='''\
  246. gem5 build ID. Allows you to keep multiple separate gem5 builds. Default: {}
  247. '''.format(default_build_id)
  248. )
  249. parser.add_argument(
  250. '-N', '--gem5-worktree',
  251. help='''\
  252. Create and use a git worktree of the gem5 submodule.
  253. See: https://github.com/cirosantilli/linux-kernel-module-cheat#gem5-worktree
  254. '''
  255. )
  256. parser.add_argument(
  257. '--gem5-build-dir',
  258. help='''\
  259. Use the given directory as the gem5 build directory.
  260. '''
  261. )
  262. parser.add_argument(
  263. '--gem5-source-dir',
  264. help='''\
  265. Use the given directory as the gem5 source tree. Ignore `--gem5-worktree`.
  266. '''
  267. )
  268. parser.add_argument(
  269. '-n', '--run-id', default='0',
  270. help='''\
  271. ID for run outputs such as gem5's m5out. Allows you to do multiple runs,
  272. and then inspect separate outputs later in different output directories.
  273. Default: %(default)s
  274. '''
  275. )
  276. parser.add_argument(
  277. '-P', '--prebuilt', default=False, action='store_true',
  278. help='''\
  279. Use prebuilt packaged host utilities as much as possible instead
  280. of the ones we built ourselves. Saves build time, but decreases
  281. the likelihood of incompatibilities.
  282. '''
  283. )
  284. parser.add_argument(
  285. '--port-offset', type=int,
  286. help='''\
  287. Increase the ports to be used such as for GDB by an offset to run multiple
  288. instances in parallel.
  289. Default: the run ID (-n) if that is an integer, otherwise 0.
  290. '''
  291. )
  292. parser.add_argument(
  293. '-Q', '--qemu-build-id', default=default_build_id,
  294. help='QEMU build ID. Allows you to keep multiple separate QEMU builds. Default: %(default)s'
  295. )
  296. parser.add_argument(
  297. '--gem5-build-type', default='opt',
  298. help='gem5 build type, most often used for "debug" builds. Default: %(default)s'
  299. )
  300. parser.add_argument(
  301. '--userland-build-id', default=None
  302. )
  303. parser.add_argument(
  304. '-v', '--verbose', default=False, action='store_true',
  305. help='Show full compilation commands when they are not shown by default.'
  306. )
  307. if hasattr(this_module, 'configs'):
  308. defaults = this_module.configs.copy()
  309. else:
  310. defaults = {}
  311. defaults.update(default_args)
  312. # A bit ugly as it actually changes the defaults shown on --help, but we can't do any better
  313. # because it is impossible to check if arguments were given or not...
  314. # - https://stackoverflow.com/questions/30487767/check-if-argparse-optional-argument-is-set-or-not
  315. # - https://stackoverflow.com/questions/3609852/which-is-the-best-way-to-allow-configuration-options-be-overridden-at-the-comman
  316. parser.set_defaults(**defaults)
  317. return parser
  318. def get_elf_entry(elf_file_path):
  319. global this_module
  320. readelf_header = subprocess.check_output([
  321. this_module.get_toolchain_tool('readelf'),
  322. '-h',
  323. elf_file_path
  324. ])
  325. for line in readelf_header.decode().split('\n'):
  326. split = line.split()
  327. if line.startswith(' Entry point address:'):
  328. addr = line.split()[-1]
  329. break
  330. return int(addr, 0)
  331. def get_stats(stat_re=None, stats_file=None):
  332. global this_module
  333. if stat_re is None:
  334. stat_re = '^system.cpu[0-9]*.numCycles$'
  335. if stats_file is None:
  336. stats_file = this_module.stats_file
  337. stat_re = re.compile(stat_re)
  338. ret = []
  339. with open(stats_file, 'r') as statfile:
  340. for line in statfile:
  341. if line[0] != '-':
  342. cols = line.split()
  343. if len(cols) > 1 and stat_re.search(cols[0]):
  344. ret.append(cols[1])
  345. return ret
  346. def get_toolchain_prefix(tool, allowed_toolchains=None):
  347. buildroot_full_prefix = os.path.join(this_module.host_bin_dir, this_module.buildroot_toolchain_prefix)
  348. buildroot_exists = os.path.exists('{}-{}'.format(buildroot_full_prefix, tool))
  349. crosstool_ng_full_prefix = os.path.join(this_module.crosstool_ng_bin_dir, this_module.crosstool_ng_toolchain_prefix)
  350. crosstool_ng_exists = os.path.exists('{}-{}'.format(crosstool_ng_full_prefix, tool))
  351. host_tool = '{}-{}'.format(this_module.ubuntu_toolchain_prefix, tool)
  352. host_path = shutil.which(host_tool)
  353. if host_path is not None:
  354. host_exists = True
  355. host_full_prefix = host_path[:-(len(tool)+1)]
  356. else:
  357. host_exists = False
  358. host_full_prefix = None
  359. known_toolchains = {
  360. 'crosstool-ng': (crosstool_ng_exists, crosstool_ng_full_prefix),
  361. 'buildroot': (buildroot_exists, buildroot_full_prefix),
  362. 'host': (host_exists, host_full_prefix),
  363. }
  364. if allowed_toolchains is None:
  365. if this_module.baremetal is None:
  366. allowed_toolchains = ['buildroot', 'crosstool-ng', 'host']
  367. else:
  368. allowed_toolchains = ['crosstool-ng', 'buildroot', 'host']
  369. tried = []
  370. for toolchain in allowed_toolchains:
  371. exists, prefix = known_toolchains[toolchain]
  372. tried.append('{}-{}'.format(prefix, tool))
  373. if exists:
  374. return prefix
  375. raise Exception('Tool not found. Tried:\n' + '\n'.join(tried))
  376. def get_toolchain_tool(tool, allowed_toolchains=None):
  377. return '{}-{}'.format(this_module.get_toolchain_prefix(tool, allowed_toolchains), tool)
  378. def github_make_request(
  379. authenticate=False,
  380. data=None,
  381. extra_headers=None,
  382. path='',
  383. subdomain='api',
  384. url_params=None,
  385. **extra_request_args
  386. ):
  387. global this_module
  388. if extra_headers is None:
  389. extra_headers = {}
  390. headers = {'Accept': 'application/vnd.github.v3+json'}
  391. headers.update(extra_headers)
  392. if authenticate:
  393. headers['Authorization'] = 'token ' + os.environ['LKMC_GITHUB_TOKEN']
  394. if url_params is not None:
  395. path += '?' + urllib.parse.urlencode(url_params)
  396. request = urllib.request.Request(
  397. 'https://' + subdomain + '.github.com/repos/' + github_repo_id + path,
  398. headers=headers,
  399. data=data,
  400. **extra_request_args
  401. )
  402. response_body = urllib.request.urlopen(request).read().decode()
  403. if response_body:
  404. _json = json.loads(response_body)
  405. else:
  406. _json = {}
  407. return _json
  408. def log_error(msg):
  409. print('error: {}'.format(msg), file=sys.stderr)
  410. def make_build_dirs():
  411. global this_module
  412. os.makedirs(this_module.build_dir, exist_ok=True)
  413. os.makedirs(this_module.gem5_build_dir, exist_ok=True)
  414. os.makedirs(this_module.out_rootfs_overlay_dir, exist_ok=True)
  415. def make_run_dirs():
  416. '''
  417. Make directories required for the run.
  418. The user could nuke those anytime between runs to try and clean things up.
  419. '''
  420. global this_module
  421. os.makedirs(this_module.gem5_run_dir, exist_ok=True)
  422. os.makedirs(this_module.p9_dir, exist_ok=True)
  423. os.makedirs(this_module.qemu_run_dir, exist_ok=True)
  424. def cmd_to_string(cmd, cwd=None, extra_env=None, extra_paths=None):
  425. '''
  426. Format a command given as a list of strings so that it can
  427. be viewed nicely and executed by bash directly and print it to stdout.
  428. '''
  429. newline_separator = ' \\\n'
  430. out = []
  431. if extra_env is None:
  432. extra_env = {}
  433. if cwd is not None:
  434. out.append('cd {} &&{}'.format(shlex.quote(cwd), newline_separator))
  435. if extra_paths is not None:
  436. out.append('PATH="{}:${{PATH}}"'.format(':'.join(extra_paths)) + newline_separator)
  437. for key in extra_env:
  438. out.append('{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key])) + newline_separator)
  439. for arg in cmd:
  440. out.append(shlex.quote(arg) + newline_separator)
  441. return ' '.join(out) + ';'
  442. def print_cmd(cmd, cwd=None, cmd_file=None, extra_env=None, extra_paths=None):
  443. '''
  444. Print cmd_to_string to stdout.
  445. Optionally save the command to cmd_file file, and add extra_env
  446. environment variables to the command generated.
  447. '''
  448. global dry_run
  449. if type(cmd) is str:
  450. cmd_string = cmd
  451. else:
  452. cmd_string = cmd_to_string(cmd, cwd=cwd, extra_env=extra_env, extra_paths=extra_paths)
  453. print(this_module.command_prefix + cmd_string)
  454. if cmd_file is not None:
  455. with open(cmd_file, 'w') as f:
  456. f.write('#!/usr/bin/env bash\n')
  457. f.write(cmd_string)
  458. st = os.stat(cmd_file)
  459. os.chmod(cmd_file, st.st_mode | stat.S_IXUSR)
  460. def print_time(ellapsed_seconds):
  461. hours, rem = divmod(ellapsed_seconds, 3600)
  462. minutes, seconds = divmod(rem, 60)
  463. print("time {:02}:{:02}:{:02}".format(int(hours), int(minutes), int(seconds)))
  464. def raw_to_qcow2(prebuilt=False, reverse=False):
  465. global this_module
  466. if prebuilt:
  467. qemu_img_executable = this_module.qemu_img_basename
  468. else:
  469. qemu_img_executable = this_module.qemu_img_executable
  470. infmt = 'raw'
  471. outfmt = 'qcow2'
  472. infile = this_module.rootfs_raw_file
  473. outfile = this_module.qcow2_file
  474. if reverse:
  475. tmp = infmt
  476. infmt = outfmt
  477. outfmt = tmp
  478. tmp = infile
  479. infile = outfile
  480. outfile = tmp
  481. this_module.run_cmd([
  482. qemu_img_executable,
  483. # Prevent qemu-img from generating trace files like QEMU. Disgusting.
  484. '-T', 'pr_manager_run,file=/dev/null',
  485. 'convert',
  486. '-f', infmt,
  487. '-O', outfmt,
  488. infile,
  489. outfile,
  490. ])
  491. def raise_no_x86(arch):
  492. if (arch == 'x86_64'):
  493. raise Exception('x86_64 not yet supported')
  494. def resolve_args(defaults, args, extra_args):
  495. if extra_args is None:
  496. extra_args = {}
  497. argcopy = copy.copy(args)
  498. argcopy.__dict__ = dict(list(defaults.items()) + list(argcopy.__dict__.items()) + list(extra_args.items()))
  499. return argcopy
  500. def cp(src, dest):
  501. global this_module
  502. print_cmd(['cp', src, dest])
  503. if not this_module.dry_run:
  504. shutil.copy2(src, dest)
  505. def rmrf(path):
  506. print_cmd(['rm', '-r', '-f', path])
  507. if not this_module.dry_run and os.path.exists(path):
  508. shutil.rmtree(path)
  509. def run_cmd(
  510. cmd,
  511. cmd_file=None,
  512. out_file=None,
  513. show_stdout=True,
  514. show_cmd=True,
  515. extra_env=None,
  516. extra_paths=None,
  517. delete_env=None,
  518. dry_run=False,
  519. raise_on_failure=True,
  520. **kwargs
  521. ):
  522. '''
  523. Run a command. Write the command to stdout before running it.
  524. Wait until the command finishes execution.
  525. :param cmd: command to run
  526. :type cmd: List[str]
  527. :param cmd_file: if not None, write the command to be run to that file
  528. :type cmd_file: str
  529. :param out_file: if not None, write the stdout and stderr of the command the file
  530. :type out_file: str
  531. :param show_stdout: wether to show stdout and stderr on the terminal or not
  532. :type show_stdout: bool
  533. :param extra_env: extra environment variables to add when running the command
  534. :type extra_env: Dict[str,str]
  535. :param dry_run: don't run the commands, just potentially print them. Debug aid.
  536. :type dry_run: Bool
  537. '''
  538. if out_file is not None:
  539. stdout = subprocess.PIPE
  540. stderr = subprocess.STDOUT
  541. else:
  542. if show_stdout:
  543. stdout = None
  544. stderr = None
  545. else:
  546. stdout = subprocess.DEVNULL
  547. stderr = subprocess.DEVNULL
  548. if extra_env is None:
  549. extra_env = {}
  550. if delete_env is None:
  551. delete_env = []
  552. if 'cwd' in kwargs:
  553. cwd = kwargs['cwd']
  554. else:
  555. cwd = None
  556. env = os.environ.copy()
  557. env.update(extra_env)
  558. if extra_paths is not None:
  559. path = ':'.join(extra_paths)
  560. if 'PATH' in os.environ:
  561. path += ':' + os.environ['PATH']
  562. env['PATH'] = path
  563. for key in delete_env:
  564. if key in env:
  565. del env[key]
  566. if show_cmd:
  567. print_cmd(cmd, cwd=cwd, cmd_file=cmd_file, extra_env=extra_env, extra_paths=extra_paths)
  568. # Otherwise Ctrl + C gives:
  569. # - ugly Python stack trace for gem5 (QEMU takes over terminal and is fine).
  570. # - kills Python, and that then kills GDB: https://stackoverflow.com/questions/19807134/does-python-always-raise-an-exception-if-you-do-ctrlc-when-a-subprocess-is-exec
  571. sigint_old = signal.getsignal(signal.SIGINT)
  572. signal.signal(signal.SIGINT, signal.SIG_IGN)
  573. # Otherwise BrokenPipeError when piping through | grep
  574. # But if I do this_module, my terminal gets broken at the end. Why, why, why.
  575. # https://stackoverflow.com/questions/14207708/ioerror-errno-32-broken-pipe-python
  576. # Ignoring the exception is not enough as it prints a warning anyways.
  577. #sigpipe_old = signal.getsignal(signal.SIGPIPE)
  578. #signal.signal(signal.SIGPIPE, signal.SIG_DFL)
  579. if not dry_run and not this_module.dry_run:
  580. # https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously/52090802#52090802
  581. with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, env=env, **kwargs) as proc:
  582. if out_file is not None:
  583. os.makedirs(os.path.split(os.path.abspath(out_file))[0], exist_ok=True)
  584. with open(out_file, 'bw') as logfile:
  585. while True:
  586. byte = proc.stdout.read(1)
  587. if byte:
  588. if show_stdout:
  589. sys.stdout.buffer.write(byte)
  590. try:
  591. sys.stdout.flush()
  592. except BlockingIOError:
  593. # TODO understand. Why, Python, why.
  594. pass
  595. logfile.write(byte)
  596. else:
  597. break
  598. signal.signal(signal.SIGINT, sigint_old)
  599. #signal.signal(signal.SIGPIPE, sigpipe_old)
  600. returncode = proc.returncode
  601. if returncode != 0 and raise_on_failure:
  602. raise Exception('Command exited with status: {}'.format(returncode))
  603. return returncode
  604. else:
  605. return 0
  606. def setup(parser):
  607. '''
  608. Parse the command line arguments, and setup several variables based on them.
  609. Typically done after getting inputs from the command line arguments.
  610. '''
  611. global this_module
  612. args = parser.parse_args()
  613. if args.arch in this_module.arch_short_to_long_dict:
  614. args.arch = this_module.arch_short_to_long_dict[args.arch]
  615. if args.gem5_build_id is None:
  616. args.gem5_build_id = default_build_id
  617. gem5_build_id_given = False
  618. else:
  619. gem5_build_id_given = True
  620. if args.userland_build_id is None:
  621. args.userland_build_id = default_build_id
  622. this_module.userland_build_id_given = False
  623. else:
  624. this_module.userland_build_id_given = True
  625. if args.gem5_worktree is not None and not gem5_build_id_given:
  626. args.gem5_build_id = args.gem5_worktree
  627. this_module.machine = args.machine
  628. this_module.setup_dry_run_arguments(args)
  629. this_module.is_arm = False
  630. if args.arch == 'arm':
  631. this_module.armv = 7
  632. this_module.gem5_arch = 'ARM'
  633. this_module.mcpu = 'cortex-a15'
  634. this_module.buildroot_toolchain_prefix = 'arm-buildroot-linux-uclibcgnueabihf'
  635. this_module.crosstool_ng_toolchain_prefix = 'arm-unknown-eabi'
  636. this_module.ubuntu_toolchain_prefix = 'arm-linux-gnueabihf'
  637. if args.gem5:
  638. if this_module.machine is None:
  639. this_module.machine = 'VExpress_GEM5_V1'
  640. else:
  641. if this_module.machine is None:
  642. this_module.machine = 'virt'
  643. this_module.is_arm = True
  644. elif args.arch == 'aarch64':
  645. this_module.armv = 8
  646. this_module.gem5_arch = 'ARM'
  647. this_module.mcpu = 'cortex-a57'
  648. this_module.buildroot_toolchain_prefix = 'aarch64-buildroot-linux-uclibc'
  649. this_module.crosstool_ng_toolchain_prefix = 'aarch64-unknown-elf'
  650. this_module.ubuntu_toolchain_prefix = 'aarch64-linux-gnu'
  651. if args.gem5:
  652. if this_module.machine is None:
  653. this_module.machine = 'VExpress_GEM5_V1'
  654. else:
  655. if this_module.machine is None:
  656. this_module.machine = 'virt'
  657. this_module.is_arm = True
  658. elif args.arch == 'x86_64':
  659. this_module.crosstool_ng_toolchain_prefix = 'x86_64-unknown-elf'
  660. this_module.gem5_arch = 'X86'
  661. this_module.buildroot_toolchain_prefix = 'x86_64-buildroot-linux-uclibc'
  662. this_module.ubuntu_toolchain_prefix = 'x86_64-linux-gnu'
  663. if args.gem5:
  664. if this_module.machine is None:
  665. this_module.machine = 'TODO'
  666. else:
  667. if this_module.machine is None:
  668. this_module.machine = 'pc'
  669. this_module.buildroot_out_dir = os.path.join(this_module.out_dir, 'buildroot')
  670. this_module.buildroot_build_dir = os.path.join(this_module.buildroot_out_dir, 'build', args.buildroot_build_id, args.arch)
  671. this_module.buildroot_download_dir = os.path.join(this_module.buildroot_out_dir, 'download')
  672. this_module.buildroot_config_file = os.path.join(this_module.buildroot_build_dir, '.config')
  673. this_module.build_dir = os.path.join(this_module.buildroot_build_dir, 'build')
  674. this_module.qemu_build_dir = os.path.join(this_module.out_dir, 'qemu', args.qemu_build_id)
  675. this_module.qemu_executable_basename = 'qemu-system-{}'.format(args.arch)
  676. this_module.qemu_executable = os.path.join(this_module.qemu_build_dir, '{}-softmmu'.format(args.arch), this_module.qemu_executable_basename)
  677. this_module.qemu_img_basename = 'qemu-img'
  678. this_module.qemu_img_executable = os.path.join(this_module.qemu_build_dir, this_module.qemu_img_basename)
  679. this_module.qemu_guest_build_dir = os.path.join(this_module.build_dir, 'qemu-custom')
  680. this_module.host_dir = os.path.join(this_module.buildroot_build_dir, 'host')
  681. this_module.host_bin_dir = os.path.join(this_module.host_dir, 'usr', 'bin')
  682. this_module.buildroot_pkg_config = os.path.join(this_module.host_bin_dir, 'pkg-config')
  683. this_module.buildroot_images_dir = os.path.join(this_module.buildroot_build_dir, 'images')
  684. this_module.buildroot_rootfs_raw_file = os.path.join(this_module.buildroot_images_dir, 'rootfs.ext2')
  685. this_module.buildroot_qcow2_file = this_module.buildroot_rootfs_raw_file + '.qcow2'
  686. this_module.staging_dir = os.path.join(this_module.out_dir, 'staging', args.arch)
  687. this_module.buildroot_staging_dir = os.path.join(this_module.buildroot_build_dir, 'staging')
  688. this_module.target_dir = os.path.join(this_module.buildroot_build_dir, 'target')
  689. this_module.run_dir_base = os.path.join(this_module.out_dir, 'run')
  690. this_module.gem5_run_dir = os.path.join(this_module.run_dir_base, 'gem5', args.arch, str(args.run_id))
  691. this_module.m5out_dir = os.path.join(this_module.gem5_run_dir, 'm5out')
  692. this_module.stats_file = os.path.join(this_module.m5out_dir, 'stats.txt')
  693. this_module.trace_txt_file = os.path.join(this_module.m5out_dir, 'trace.txt')
  694. this_module.gem5_readfile = os.path.join(this_module.gem5_run_dir, 'readfile')
  695. this_module.gem5_termout_file = os.path.join(this_module.gem5_run_dir, 'termout.txt')
  696. this_module.qemu_run_dir = os.path.join(this_module.run_dir_base, 'qemu', args.arch, str(args.run_id))
  697. this_module.qemu_trace_basename = 'trace.bin'
  698. this_module.qemu_trace_file = os.path.join(this_module.qemu_run_dir, 'trace.bin')
  699. this_module.qemu_trace_txt_file = os.path.join(this_module.qemu_run_dir, 'trace.txt')
  700. this_module.qemu_termout_file = os.path.join(this_module.qemu_run_dir, 'termout.txt')
  701. this_module.qemu_rrfile = os.path.join(this_module.qemu_run_dir, 'rrfile')
  702. this_module.gem5_out_dir = os.path.join(this_module.out_dir, 'gem5')
  703. if args.gem5_build_dir is None:
  704. this_module.gem5_build_dir = os.path.join(this_module.gem5_out_dir, args.gem5_build_id, args.gem5_build_type)
  705. else:
  706. this_module.gem5_build_dir = args.gem5_build_dir
  707. this_module.gem5_fake_iso = os.path.join(this_module.gem5_out_dir, 'fake.iso')
  708. this_module.gem5_m5term = os.path.join(this_module.gem5_build_dir, 'm5term')
  709. this_module.gem5_build_build_dir = os.path.join(this_module.gem5_build_dir, 'build')
  710. this_module.gem5_executable = os.path.join(this_module.gem5_build_build_dir, gem5_arch, 'gem5.{}'.format(args.gem5_build_type))
  711. this_module.gem5_system_dir = os.path.join(this_module.gem5_build_dir, 'system')
  712. this_module.crosstool_ng_out_dir = os.path.join(this_module.out_dir, 'crosstool-ng')
  713. this_module.crosstool_ng_buildid_dir = os.path.join(this_module.crosstool_ng_out_dir, 'build', args.crosstool_ng_build_id)
  714. this_module.crosstool_ng_install_dir = os.path.join(this_module.crosstool_ng_buildid_dir, 'install', args.arch)
  715. this_module.crosstool_ng_bin_dir = os.path.join(this_module.crosstool_ng_install_dir, 'bin')
  716. this_module.crosstool_ng_util_dir = os.path.join(this_module.crosstool_ng_buildid_dir, 'util')
  717. this_module.crosstool_ng_config = os.path.join(this_module.crosstool_ng_util_dir, '.config')
  718. this_module.crosstool_ng_defconfig = os.path.join(this_module.crosstool_ng_util_dir, 'defconfig')
  719. this_module.crosstool_ng_executable = os.path.join(this_module.crosstool_ng_util_dir, 'ct-ng')
  720. this_module.crosstool_ng_build_dir = os.path.join(this_module.crosstool_ng_buildid_dir, 'build')
  721. this_module.crosstool_ng_download_dir = os.path.join(this_module.crosstool_ng_out_dir, 'download')
  722. this_module.gem5_default_src_dir = os.path.join(submodules_dir, 'gem5')
  723. if args.gem5_source_dir is not None:
  724. this_module.gem5_src_dir = args.gem5_source_dir
  725. assert(os.path.exists(args.gem5_source_dir))
  726. else:
  727. if args.gem5_worktree is not None:
  728. this_module.gem5_src_dir = os.path.join(this_module.gem5_non_default_src_root_dir, args.gem5_worktree)
  729. else:
  730. this_module.gem5_src_dir = this_module.gem5_default_src_dir
  731. this_module.gem5_m5_src_dir = os.path.join(this_module.gem5_src_dir, 'util', 'm5')
  732. this_module.gem5_m5_build_dir = os.path.join(this_module.out_dir, 'util', 'm5')
  733. if args.gem5:
  734. this_module.executable = this_module.gem5_executable
  735. this_module.run_dir = this_module.gem5_run_dir
  736. this_module.termout_file = this_module.gem5_termout_file
  737. else:
  738. this_module.executable = this_module.qemu_executable
  739. this_module.run_dir = this_module.qemu_run_dir
  740. this_module.termout_file = this_module.qemu_termout_file
  741. this_module.gem5_config_dir = os.path.join(this_module.gem5_src_dir, 'configs')
  742. this_module.gem5_se_file = os.path.join(this_module.gem5_config_dir, 'example', 'se.py')
  743. this_module.gem5_fs_file = os.path.join(this_module.gem5_config_dir, 'example', 'fs.py')
  744. this_module.run_cmd_file = os.path.join(this_module.run_dir, 'run.sh')
  745. # Linux
  746. this_module.linux_buildroot_build_dir = os.path.join(this_module.build_dir, 'linux-custom')
  747. this_module.linux_build_dir = os.path.join(this_module.out_dir, 'linux', args.linux_build_id, args.arch)
  748. this_module.vmlinux = os.path.join(this_module.linux_build_dir, "vmlinux")
  749. if args.arch == 'arm':
  750. this_module.linux_arch = 'arm'
  751. this_module.linux_image = os.path.join('arch', this_module.linux_arch, 'boot', 'zImage')
  752. elif args.arch == 'aarch64':
  753. this_module.linux_arch = 'arm64'
  754. this_module.linux_image = os.path.join('arch', this_module.linux_arch, 'boot', 'Image')
  755. elif args.arch == 'x86_64':
  756. this_module.linux_arch = 'x86'
  757. this_module.linux_image = os.path.join('arch', this_module.linux_arch, 'boot', 'bzImage')
  758. this_module.linux_image = os.path.join(this_module.linux_build_dir, linux_image)
  759. # Kernel modules.
  760. this_module.kernel_modules_build_base_dir = os.path.join(this_module.out_dir, 'kernel_modules')
  761. this_module.kernel_modules_build_dir = os.path.join(this_module.kernel_modules_build_base_dir, args.arch)
  762. this_module.kernel_modules_build_subdir = os.path.join(this_module.kernel_modules_build_dir, kernel_modules_subdir)
  763. this_module.kernel_modules_build_host_dir = os.path.join(this_module.kernel_modules_build_base_dir, 'host')
  764. this_module.kernel_modules_build_host_subdir = os.path.join(this_module.kernel_modules_build_host_dir, kernel_modules_subdir)
  765. this_module.userland_build_dir = os.path.join(this_module.out_dir, 'userland', args.userland_build_id, args.arch)
  766. this_module.out_rootfs_overlay_dir = os.path.join(this_module.out_dir, 'rootfs_overlay', args.arch)
  767. this_module.out_rootfs_overlay_bin_dir = os.path.join(this_module.out_rootfs_overlay_dir, 'bin')
  768. # Ports
  769. if args.port_offset is None:
  770. try:
  771. args.port_offset = int(args.run_id)
  772. except ValueError:
  773. args.port_offset = 0
  774. if args.gem5:
  775. this_module.gem5_telnet_port = 3456 + args.port_offset
  776. this_module.gdb_port = 7000 + args.port_offset
  777. else:
  778. this_module.qemu_base_port = 45454 + 10 * args.port_offset
  779. this_module.qemu_monitor_port = this_module.qemu_base_port + 0
  780. this_module.qemu_hostfwd_generic_port = this_module.qemu_base_port + 1
  781. this_module.qemu_hostfwd_ssh_port = this_module.qemu_base_port + 2
  782. this_module.qemu_gdb_port = this_module.qemu_base_port + 3
  783. this_module.extra_serial_port = this_module.qemu_base_port + 4
  784. this_module.gdb_port = this_module.qemu_gdb_port
  785. # Baremetal.
  786. this_module.baremetal = args.baremetal
  787. this_module.baremetal_lib_basename = 'lib'
  788. this_module.baremetal_src_dir = os.path.join(this_module.root_dir, 'baremetal')
  789. this_module.baremetal_src_lib_dir = os.path.join(this_module.baremetal_src_dir, this_module.baremetal_lib_basename)
  790. if args.gem5:
  791. this_module.simulator_name = 'gem5'
  792. else:
  793. this_module.simulator_name = 'qemu'
  794. this_module.baremetal_build_dir = os.path.join(out_dir, 'baremetal', args.arch, this_module.simulator_name, this_module.machine)
  795. this_module.baremetal_build_lib_dir = os.path.join(this_module.baremetal_build_dir, this_module.baremetal_lib_basename)
  796. this_module.baremetal_build_ext = '.elf'
  797. # Docker
  798. this_module.docker_build_dir = os.path.join(this_module.out_dir, 'docker', args.arch)
  799. this_module.docker_tar_dir = os.path.join(this_module.docker_build_dir, 'export')
  800. this_module.docker_tar_file = os.path.join(this_module.docker_build_dir, 'export.tar')
  801. this_module.docker_rootfs_raw_file = os.path.join(this_module.docker_build_dir, 'export.ext2')
  802. this_module.docker_qcow2_file = os.path.join(this_module.docker_rootfs_raw_file + '.qcow2')
  803. if args.docker:
  804. this_module.rootfs_raw_file = this_module.docker_rootfs_raw_file
  805. this_module.qcow2_file = this_module.docker_qcow2_file
  806. else:
  807. this_module.rootfs_raw_file = this_module.buildroot_rootfs_raw_file
  808. this_module.qcow2_file = this_module.buildroot_qcow2_file
  809. # Image.
  810. if args.baremetal is None:
  811. if args.gem5:
  812. this_module.image = this_module.vmlinux
  813. this_module.disk_image = this_module.rootfs_raw_file
  814. else:
  815. this_module.image = this_module.linux_image
  816. this_module.disk_image = this_module.qcow2_file
  817. else:
  818. this_module.disk_image = this_module.gem5_fake_iso
  819. if args.baremetal == 'all':
  820. path = args.baremetal
  821. else:
  822. path = this_module.resolve_executable(
  823. args.baremetal,
  824. this_module.baremetal_src_dir,
  825. this_module.baremetal_build_dir,
  826. this_module.baremetal_build_ext,
  827. )
  828. this_module.image = path
  829. return args
  830. def setup_dry_run_arguments(args):
  831. this_module.dry_run = args.dry_run
  832. def resolve_executable(in_path, magic_in_dir, magic_out_dir, out_ext):
  833. if os.path.isabs(in_path):
  834. return in_path
  835. else:
  836. paths = [
  837. os.path.join(magic_out_dir, in_path),
  838. os.path.join(
  839. magic_out_dir,
  840. os.path.relpath(in_path, magic_in_dir),
  841. )
  842. ]
  843. paths[:] = [os.path.splitext(path)[0] + out_ext for path in paths]
  844. for path in paths:
  845. if os.path.exists(path):
  846. return path
  847. raise Exception('Executable file not found. Tried:\n' + '\n'.join(paths))
  848. def resolve_userland(path):
  849. global this_module
  850. return this_module.resolve_executable(
  851. path,
  852. this_module.userland_src_dir,
  853. this_module.userland_build_dir,
  854. this_module.userland_build_ext,
  855. )
  856. def write_configs(config_path, configs, config_fragments=None):
  857. """
  858. Write extra configs into the Buildroot config file.
  859. TODO Can't get rid of these for now with nice fragments:
  860. http://stackoverflow.com/questions/44078245/is-it-possible-to-use-config-fragments-with-buildroots-config
  861. """
  862. global this_module
  863. if config_fragments is None:
  864. config_fragments = []
  865. with open(config_path, 'a') as config_file:
  866. for config_fragment in config_fragments:
  867. with open(config_fragment, 'r') as config_fragment_file:
  868. print_cmd(['cat', config_fragment, '>>', config_path])
  869. if not this_module.dry_run:
  870. for line in config_fragment_file:
  871. config_file.write(line)
  872. write_string_to_file(config_path, '\n'.join(configs), mode='a')