Пример #1
0
def copyEngineSources(engine_dir, output_dir):
    logger.info("Copying policy-engine sources")
    engine_output_dir = os.path.join(output_dir, "engine")
    isp_utils.doMkDir(engine_output_dir)

    try:
        shutil.copytree(os.path.join(engine_dir, "validator"),
                        os.path.join(engine_output_dir, "validator"))
        shutil.copytree(os.path.join(engine_dir, "tagging_tools"),
                        os.path.join(engine_output_dir, "tagging_tools"))
        shutil.copy(os.path.join(engine_dir, "Makefile.isp"),
                    engine_output_dir)
        shutil.copy(os.path.join(engine_dir, "CMakeLists.txt"),
                    engine_output_dir)

    except Exception as e:
        logger.error("Copying engine sources failed with error: {}".format(
            str(e)))
        return False

    result = subprocess.call(["make", "-f", "Makefile.isp", "clean"],
                             stdout=subprocess.DEVNULL,
                             stderr=subprocess.STDOUT,
                             cwd=engine_output_dir)
    if result != 0:
        logger.error("Failed to clean engine directory")
        return False

    return True
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        description="Build and install ISP kernels with policies")
    parser.add_argument("policy",
                        type=str,
                        help='''
    The name of the policy to compile and install
    ''')
    parser.add_argument("-o",
                        "--output",
                        type=str,
                        default="",
                        help='''
    Directory where the compiled pex kernel is stored
    Default is ISP_PREFIX/kernels or current working directory if ISP_PREFIX is not set
    ''')
    parser.add_argument("-d",
                        "--debug",
                        action="store_true",
                        help='''
    Enable debug logging
    ''')

    args = parser.parse_args()

    isp_prefix = isp_utils.getIspPrefix()
    policies_dir = os.path.join(isp_prefix, "sources", "policies")
    engine_dir = os.path.join(isp_prefix, "sources", "policy-engine")
    policy_out_dir = os.path.join(engine_dir, "policy")
    soc_cfg_path = os.path.join(engine_dir, "soc_cfg")
    entities_dir = os.path.join(policies_dir, "entities")

    base_output_dir = os.getcwd()

    if args.output == "":
        kernels_dir = os.path.join(isp_utils.getIspPrefix(), "kernels")
        if os.path.isdir(kernels_dir):
            base_output_dir = kernels_dir
    else:
        base_output_dir = args.output

    output_dir = os.path.abspath(os.path.join(base_output_dir, args.policy))
    if args.debug is True:
        output_dir = output_dir + "-debug"

    shutil.rmtree(output_dir, ignore_errors=True)
    isp_utils.doMkDir(output_dir)

    copyEngineSources(engine_dir, output_dir)
    copyPolicyYaml(args.policy, policies_dir, entities_dir, output_dir)
    if runPolicyTool(args.policy, policies_dir, entities_dir, output_dir,
                     args.debug) is False:
        print("Failed to run policy tool")
        sys.exit(1)

    if buildPolicyKernel(args.policy, policies_dir, entities_dir,
                         output_dir) is False:
        print("Failed to build policy kernel")
        sys.exit(1)
Пример #3
0
def copyEngineSources(engine_dir, output_dir):
    engine_output_dir = os.path.join(output_dir, "engine")
    isp_utils.doMkDir(engine_output_dir)

    shutil.copytree(os.path.join(engine_dir, "validator"),
                    os.path.join(engine_output_dir, "validator"))
    shutil.copytree(os.path.join(engine_dir, "tagging_tools"),
                    os.path.join(engine_output_dir, "tagging_tools"))
    shutil.copy(os.path.join(engine_dir, "Makefile.isp"), engine_output_dir)
    shutil.copy(os.path.join(engine_dir, "CMakeLists.txt"), engine_output_dir)

    shutil.copytree(os.path.join(engine_dir, "soc_cfg"),
                    os.path.join(output_dir, "soc_cfg"))
Пример #4
0
def doInstall(build_dir, template_dir, runtime, sim, stock):
    if not os.path.isdir(build_dir):
        return retVals.NO_TEST

    if not os.path.isdir(template_dir):
        return retVals.NO_TEMPLATE

    runtime_dir = os.path.join(build_dir, "isp-runtime-{}".format(runtime))

    isp_utils.removeIfExists(runtime_dir)
    isp_utils.doMkDir(runtime_dir)

    shutil.copy(os.path.join(template_dir, "isp_utils.h"),
                os.path.join(runtime_dir, "isp_utils.h"))

    runtime_main_c = os.path.join(template_dir, (runtime + "_main.c"))
    sim_utils_c = os.path.join(template_dir, sim, "isp_utils.c")

    makefile_path = os.path.join(template_dir, sim)
    if stock:
        makefile_path = os.path.join(makefile_path, "stock")

    makefile = os.path.join(makefile_path, (runtime + ".mk"))
    try:
        shutil.copy(runtime_main_c, runtime_dir)
        shutil.copy(sim_utils_c, runtime_dir)
        shutil.copy(
            makefile,
            os.path.join(build_dir, ("isp-runtime-" + runtime + ".mk")))

        if "bare" == runtime:
            shutil.copytree(
                os.path.join(isp_utils.getIspPrefix(), bare_bsp[sim]),
                os.path.join(runtime_dir, "bsp"))

        if "sel4" == runtime:
            sel4_setup_source(build_dir, template_dir)
            sel4_dir = os.path.join(runtime_dir, "sel4")
            if stock:
                sel4_dir = os.path.join(runtime_dir, "stock_sel4")
            isp_utils.doMkDir(sel4_dir)
    except:
        logging.error("Runtime {} is incompatible with sim {}".format(
            runtime, sim))
        return retVals.NO_RUNTIME

    return retVals.SUCCESS
Пример #5
0
def doInstall(build_dir, template_dir, runtime):
    if not os.path.isdir(build_dir):
        return retVals.NO_TEST

    if not os.path.isdir(template_dir):
        return retVals.NO_TEMPLATE

    runtime_dir = os.path.join(build_dir, "isp-runtime-{}".format(runtime))

    isp_utils.removeIfExists(runtime_dir)
    isp_utils.doMkDir(runtime_dir)

    shutil.copy(os.path.join(template_dir, "isp_utils.h"),
                os.path.join(runtime_dir, "isp_utils.h"))
    shutil.copy(os.path.join(template_dir, "mem.h"),
                os.path.join(runtime_dir, "mem.h"))

    if "frtos" == runtime:
        frtos_dir = os.path.join(runtime_dir, "frtos")
        isp_utils.doMkDir(frtos_dir)
        shutil.copy(os.path.join(template_dir, "frtos.mk"),
                    os.path.join(build_dir, "isp-runtime-frtos.mk"))

    elif "sel4" == runtime:
        sel4_setup_source(build_dir, template_dir)

        sel4_dir = os.path.join(runtime_dir, "sel4")
        isp_utils.doMkDir(sel4_dir)

        shutil.copy(os.path.join(template_dir, "sel4.mk"),
                    os.path.join(build_dir, "isp-runtime-sel4.mk"))

    elif "bare" == runtime:
        shutil.copy(os.path.join(template_dir, "bare.c"),
                    os.path.join(runtime_dir, "bare.c"))
        shutil.copy(os.path.join(template_dir, "bare.mk"),
                    os.path.join(build_dir, "isp-runtime-bare.mk"))
        shutil.copytree(os.path.join(isp_utils.getIspPrefix(), "hifive_bsp"),
                        os.path.join(runtime_dir, "bsp"))

    elif "stock_frtos" == runtime:
        frtos_dir = os.path.join(runtime_dir, "stock_frtos")
        isp_utils.doMkDir(frtos_dir)
        shutil.copy(os.path.join(template_dir, "stock_frtos.mk"),
                    os.path.join(build_dir, "isp-runtime-stock_frtos.mk"))

    elif "stock_sel4" == runtime:
        sel4_setup_source(build_dir, template_dir)

        sel4_dir = os.path.join(runtime_dir, "stock_sel4")
        isp_utils.doMkDir(sel4_dir)
        shutil.copy(os.path.join(template_dir, "stock_sel4.mk"),
                    os.path.join(build_dir, "isp-runtime-stock_sel4.mk"))

    elif "stock_bare" == runtime:
        shutil.copy(os.path.join(template_dir, "bare.c"),
                    os.path.join(runtime_dir, "bare.c"))
        shutil.copy(os.path.join(template_dir, "stock_bare.mk"),
                    os.path.join(build_dir, "isp-runtime-stock_bare.mk"))
        shutil.copytree(os.path.join(isp_utils.getIspPrefix(), "hifive_bsp"),
                        os.path.join(runtime_dir, "bsp"))

    else:
        return retVals.NO_RUNTIME

    return retVals.SUCCESS
Пример #6
0
def main():
    global sim_module
    parser = argparse.ArgumentParser(
        description="Run standalone ISP applications")
    parser.add_argument("exe_path",
                        type=str,
                        help='''
    Path of the executable to run
    ''')
    parser.add_argument("-p",
                        "--policies",
                        nargs='+',
                        default=["none"],
                        help='''
    List of policies to apply to run, or path to a policy directory
    Default is none
    ''')
    parser.add_argument("-P",
                        "--global-policies",
                        nargs='+',
                        help='''
    List of global policies to apply to run
    Default is none
    ''')
    parser.add_argument("-s",
                        "--simulator",
                        type=str,
                        default="qemu",
                        help='''
    Module for simulating/running application. Must be installed to $ISP_PREFIX/runtime_modules
    ''')
    parser.add_argument("-r",
                        "--runtime",
                        type=str,
                        default="bare",
                        help='''
    Currently supported: frtos, sel4, bare (bare metal) (default), stock_frtos, stock_sel4, stock_bare
    ''')
    parser.add_argument("-a",
                        "--arch",
                        type=str,
                        help='''
    Architecture of executable. Currently supported: {}. Autodetect by default
    '''.format(isp_utils.supportedArchs))
    parser.add_argument("-o",
                        "--output",
                        type=str,
                        default="",
                        help='''
    Location of simulator output directory. Contains supporting files and
    runtime logs.
    Default is current working directory.
    ''')
    parser.add_argument("-u",
                        "--uart",
                        action="store_true",
                        help='''
    Forward UART output from the simulator to stdout
    ''')
    parser.add_argument("-g",
                        "--gdb",
                        type=int,
                        default=0,
                        help='''
    Start the simulator in gdbserver mode on specified port
    ''')
    parser.add_argument("-d",
                        "--debug",
                        action="store_true",
                        help='''
    Enable debug logging in this script
    ''')
    parser.add_argument("-D",
                        "--policy-debug",
                        action="store_true",
                        help='''
    Use a debug policy
    ''')
    parser.add_argument("-t",
                        "--tag-only",
                        action="store_true",
                        help='''
    Run the tagging tools without running the application
    ''')
    parser.add_argument("-T",
                        "--tagfile",
                        default=None,
                        help='''
    Path of tag file to use rather than generating it based on policy
    ''')
    parser.add_argument("-C",
                        "--rule-cache-name",
                        type=str,
                        default="",
                        help='''
    Name of the rule cache
    ''')
    parser.add_argument("-c",
                        "--rule-cache-size",
                        type=int,
                        default=16,
                        help='''
    Size of the rule cache (if name is provided). Default is 16
    ''')
    parser.add_argument("-e",
                        "--extra",
                        nargs="+",
                        help='''
    Extra command line arguments for the simulator
    ''')
    parser.add_argument("-S",
                        "--suffix",
                        type=str,
                        help='''
    Extra suffix to add to the test directory name
    ''')
    parser.add_argument("--soc",
                        type=str,
                        help='''
    SOC configuration YAML file
    ''')
    parser.add_argument("-N",
                        "--no_validator",
                        action="store_true",
                        help='''
    Do not use the validator and run the stock version of the simulator (which
    must be located at ISP_PREFIX/stock-tools/bin/qemu-system-riscv32.
    ''')
    parser.add_argument("--disable-colors",
                        action="store_true",
                        help='''
    Disable colored logging
    ''')
    parser.add_argument("--pex",
                        type=str,
                        help='''
    Path to a custom PEX implementation (validator lib, kernel, etc)
    ''')

    args = parser.parse_args()

    log_level = logging.INFO
    if args.debug is True:
        log_level = logging.DEBUG

    logger = isp_utils.setupLogger(log_level, (not args.disable_colors))

    sim_module = __import__("isp_" + args.simulator)

    if not os.path.isfile(args.exe_path):
        logger.error("No binary found to run")
        exit(1)

    if args.output == "":
        output_dir = os.getcwd()
    else:
        output_dir = os.path.abspath(args.output)

    if args.runtime not in [
            "frtos", "sel4", "bare", "stock_frtos", "stock_sel4", "stock_bare"
    ]:
        logger.error(
            "Invalid choice of runtime. Valid choices: frtos, sel4, bare, stock_frtos, stock_sel4, stock_bare"
        )
        return

    if not args.arch:
        arch = isp_utils.getArch(args.exe_path)
        if not arch:
            logger.error(
                "Invalid choice of architecture. Valid choices: {}".format(
                    isp_utils.supportedArchs))
            return

        logger.debug("Executable has architecture {}".format(arch))
    else:
        arch = args.arch

    if args.rule_cache_name not in ["", "finite", "infinite", "dmhc"]:
        logger.error(
            "Invalid choice of rule cache name. Valid choices: finite, infinite, dmhc"
        )
        return

    policies = args.policies
    policy_dir = ""
    policy_name = ""

    use_validator = not args.no_validator

    # Policy Directory Building
    # Force policy to none if we're using stock
    if "stock_" in args.simulator or "stock_" in args.runtime:
        logger.info(
            "Using a stock simulator or runtime, setting policy to 'none'")
        policies = ["none"]

    # use exiting policy directory if -p arg refers to path
    if (len(policies) == 1 and "/" in args.policies[0]
            and os.path.isdir(policies[0])):
        policy_dir = os.path.abspath(policies[0])
        policy_name = os.path.basename(policy_dir)
    else:
        policy_name = isp_utils.getPolicyFullName(policies,
                                                  args.global_policies,
                                                  args.policy_debug)
        policy_dir = os.path.join(isp_prefix, "policies", policy_name)

    pex_path = args.pex
    if not pex_path:
        pex_path = sim_module.defaultPexPath(policy_name, arch, args.extra)
    else:
        pex_path = os.path.realpath(args.pex)

    args.exe_path = os.path.realpath(args.exe_path)
    exe_name = os.path.basename(args.exe_path)
    run_dir = os.path.join(output_dir,
                           "isp-run-{}-{}".format(exe_name, policy_name))

    if args.rule_cache_name != "":
        run_dir = run_dir + "-{}-{}".format(args.rule_cache_name,
                                            args.rule_cache_size)

    if args.suffix:
        run_dir = run_dir + "-" + args.suffix

    isp_utils.removeIfExists(run_dir)
    isp_utils.doMkDir(run_dir)

    if "stock_" not in args.runtime and use_validator == True:
        if not os.path.isdir(policy_dir):
            if compileMissingPolicy(policies, args.global_policies,
                                    args.policy_debug) is False:
                logger.error("Failed to compile missing policy")
                sys.exit(1)

        if not os.path.isfile(pex_path):
            if compileMissingPex(policy_dir, pex_path, args.simulator, arch,
                                 args.extra) is False:
                logger.error("Failed to compile missing PEX binary")
                sys.exit(1)

        logger.debug("Using PEX at path: {}".format(pex_path))

        doEntitiesFile(run_dir, exe_name)

    logger.debug("Starting simulator...")
    result = sim_module.runSim(args.exe_path, run_dir, policy_dir, pex_path,
                               args.runtime,
                               (args.rule_cache_name, args.rule_cache_size),
                               args.gdb, args.tagfile, args.soc, arch,
                               args.extra, use_validator, args.tag_only)

    if result != isp_utils.retVals.SUCCESS:
        logger.error(result)
        os._exit(-1)

    if args.tag_only is True:
        return

    if args.uart is True:
        printUartOutput(run_dir)

    process_exit_code = getProcessExitCode(run_dir, args.runtime)
    logger.debug("Process exited with code {}".format(process_exit_code))

    if result is not isp_utils.retVals.SUCCESS:
        logger.error("Failed to run application: {}".format(result))
def main():
    parser = argparse.ArgumentParser(description="Build and install policies and PEX binaries")
    parser.add_argument("-p", "--policies", nargs='+', required=True, help='''
    List of policies to compose and install, or path to a policy directory
    ''')
    parser.add_argument("-P", "--global-policies", nargs='+', help='''
    List of global policies to compose and install
    ''')
    parser.add_argument("-s", "--sim", type=str, help='''
    Simulator for which to build kernel/validator
    Currently supported: qemu
    If omitted, only the policy tool will run
    ''')
    parser.add_argument("-o", "--output", type=str, default=os.getcwd(), help='''
    Output directory for compiled kernel/validator
    Default is current working directory
    This option is redundant if -s/--sim is not specified
    ''')
    parser.add_argument("-O", "--policy-output", type=str, default=os.getcwd(), help='''
    Output directory for policy
    Default is current working directory
    ''')
    parser.add_argument("-d", "--debug", action="store_true", help='''
    Enable debug logging in this script
    ''')
    parser.add_argument("-D", "--policy-debug", action="store_true", help='''
    Build policy with debug logging
    ''')
    parser.add_argument("--disable-colors", action="store_true", help='''
    Disable colored logging
    ''')
    parser.add_argument("--arch", type=str, default="rv32", help='''
    Currently supported: rv32 (default), rv64
    ''')
    parser.add_argument("-e", "--extra", nargs="+", help='''
    Extra command line arguments for the simulator
    ''')

    args = parser.parse_args()

    log_level = logging.INFO
    if args.debug is True:
        log_level = logging.DEBUG

    logger = isp_utils.setupLogger(log_level, (not args.disable_colors))

    policies_dir = os.path.join(isp_prefix, "sources", "policies")
    soc_cfg_path = os.path.join(isp_prefix, "soc_cfg")
    entities_dir = os.path.join(policies_dir, "entities")

    policy_name = ""
    policy_out_dir = ""

    # use existing policy directory if -p arg refers to path
    if (len(args.policies) == 1 and
        "/" in args.policies[0] and
        os.path.isdir(args.policies[0])):
        policy_out_dir = os.path.abspath(args.policies[0])
        policy_name = os.path.basename(policy_out_dir)
    else:
        policy_name = isp_utils.getPolicyFullName(args.policies, args.global_policies, args.policy_debug)
        policy_out_dir = os.path.join(args.policy_output, policy_name)
        policies = args.policies
        if args.global_policies:
            policies += args.global_policies

        if runPolicyTool(policies, policies_dir, entities_dir,
                         policy_out_dir, args.policy_debug) is False:
            logger.error('''
                         Policy tool failed to run to completion.
                         See {}/policy_tool.log for more info
                         '''.format(policy_out_dir))
            sys.exit(1)

        entity_output_path = os.path.join(policy_out_dir,
                                          ".".join(["composite_entities", "yml"]))
        logger.info("Generating composite policy entity file at {}".format(entity_output_path))
        generateCompositeEntities(policies, entities_dir, entity_output_path)

    logger.debug("Policy directory is {}".format(policy_out_dir))

    if args.sim:
        base_output_dir = args.output

        output_dir = os.path.abspath(os.path.join(base_output_dir, "-".join(["isp", "install", policy_name])))

        shutil.rmtree(output_dir, ignore_errors=True)
        isp_utils.doMkDir(output_dir)
        sim_module = __import__("isp_" + args.sim)
        logger.debug("SIM module is {}".format(sim_module))
        if not sim_module.installPex(policy_out_dir, output_dir, args.arch, args.extra):
            sys.exit(1)