From 3c9e16a3e9a2f737f5f3d1732a7697b1a5bd2e0d Mon Sep 17 00:00:00 2001 From: Smita Ghosh Date: Wed, 16 Nov 2022 13:26:57 -0800 Subject: [PATCH] securemsm: Add support for Bazel/Kleaf Steps to test 1. Create symlink cd kernel_platform mkdir techpacks cd techpacks mkdir securemsm-kernel cd securemsm-kernel ln /vendor/qcom/open-source/securemsm-kernel/* . 2. To compile cd kernel_platfrom tools/bazel run --lto=thin //techpacks/securemsm-kernel:pineapple_consolidate_securemsm-kernel_dist 3. Test done: Able to compile smcinvoke/tz_log/qseecom DLKM and able to install it at out/target/product//dlkm/lib/modules Change-Id: I8258a1351281a0acb05a2e909bbc421d576b4ee0 --- BUILD.bazel | 31 +++++++++++ pineapple.bzl | 12 ++++ securemsm_kernel.bzl | 106 ++++++++++++++++++++++++++++++++++++ securemsm_modules.bzl | 76 ++++++++++++++++++++++++++ smcinvoke/trace_smcinvoke.h | 10 +++- 5 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 BUILD.bazel create mode 100644 pineapple.bzl create mode 100644 securemsm_kernel.bzl create mode 100644 securemsm_modules.bzl diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000000..92cf723805 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,31 @@ +package( + default_visibility = [ + "//visibility:public", + ], +) + +load("//build/kernel/kleaf:kernel.bzl", "ddk_headers") + +ddk_headers( + name = "smcinvoke_kernel_headers", + hdrs = [ + "include/linux/smcinvoke*.h", + "include/linux/IClientE*.h", + "linux/misc/qseecom_kernel.h", + "linux/misc/qseecom_priv.h" + ], + includes = ["include/linux", "linux"] +) + +ddk_headers( + name = "securemsm_kernel_headers", + hdrs = [ + "linux/misc/qseecom_kernel.h", + "linux/misc/qseecom_priv.h" + ], + includes = ["linux"] +) + +load("pineapple.bzl", "define_pineapple") + +define_pineapple() diff --git a/pineapple.bzl b/pineapple.bzl new file mode 100644 index 0000000000..35c7ca0bf9 --- /dev/null +++ b/pineapple.bzl @@ -0,0 +1,12 @@ +load(":securemsm_kernel.bzl", "define_consolidate_gki_modules") + +def define_pineapple(): + define_consolidate_gki_modules( + target = "pineapple", + modules = [ + "smcinvoke_dlkm", + "tz_log_dlkm", + ], + extra_options = [ + "CONFIG_QCOM_SMCINVOKE"] + ) diff --git a/securemsm_kernel.bzl b/securemsm_kernel.bzl new file mode 100644 index 0000000000..cb94684157 --- /dev/null +++ b/securemsm_kernel.bzl @@ -0,0 +1,106 @@ +load("//build/kernel/kleaf:kernel.bzl", "kernel_modules_install", + "ddk_module") +load(":securemsm_modules.bzl", "securemsm_modules", + "securemsm_modules_by_config") +load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir") + +def _replace_formatting_codes(target, variant, s): + kernel_build = "{}_{}".format(target, variant) + + return s.replace("%b", kernel_build).replace("%t", target) + +def _console_print(target, variant, module, message): + if module: + print('{}: {}: securemsm-kernel: {}: {}'.format(target, variant, module, message)) + else: + print('{}: {}: securemsm-kernel: {} '.format(target, variant, message)) + +def _get_options(target, variant, target_config_option, modules, extra_options): + all_options = {option: True for option in extra_options} + + redundant_options = [] + + for option in securemsm_modules_by_config: + module_name = securemsm_modules_by_config[option] + + if option in all_options: + if module_name in modules: + redundant_options.append(option) + else: + _console_print(target, variant, None, 'WARNING: Config option "{}" corresponds to securemsm module {}, but this module is not listed in module list!'.format(option, module_name)) + else: + all_options[option] = True + + if target_config_option in all_options: + redundant_options.append(target_config_option) + else: + all_options[target_config_option] = True + + if redundant_options: + _console_print(target, variant, None, 'INFO: The following options are already declared either by a module or the target, no need to redeclare: \n{}'.format('\n'.join(redundant_options))) + + return all_options + +def _get_module_srcs(target, variant, module, options): + srcs = [] + module["default_srcs"] + module["srcs"] + module_path = "{}/".format(module["path"]) if module["path"] else "" + + for option in module["config_srcs"]: + srcs.extend(module["config_srcs"][option].get(option in options, [])) + + globbed_srcs = native.glob(["{}{}".format(module_path, _replace_formatting_codes(target, variant, src)) for src in srcs]) + + if not globbed_srcs: + _console_print(target, variant, module["name"], 'WARNING: Module has no sources attached!') + + return globbed_srcs + +def define_target_variant_modules(target, variant, modules, extra_options = [], config_option = None): + kernel_build_variant = "{}_{}".format(target, variant) + options = _get_options(target, variant, config_option, modules, extra_options) + module_rules = [] + target_local_defines = [] + modules = [securemsm_modules[module_name] for module_name in modules] + tv = "{}_{}".format(target, variant) + + target_local_defines = ["SMCINVOKE_TRACE_INCLUDE_PATH=../../../{}/smcinvoke".format(native.package_name())] + + for config in extra_options: + target_local_defines.append(config) + for module in modules: + rule_name = "{}_{}".format(kernel_build_variant, module["name"]) + module_srcs = _get_module_srcs(target, variant, module, options) + + ddk_module( + name = rule_name, + kernel_build = "//msm-kernel:{}".format(kernel_build_variant), + srcs = module_srcs, + out = "{}.ko".format(module["name"]), + deps = ["//msm-kernel:all_headers"] + [_replace_formatting_codes(target, variant, dep) for dep in module["deps"]], + local_defines = target_local_defines, + copts = module["copts"] + + ) + module_rules.append(rule_name) + + copy_to_dist_dir( + name = "{}_securemsm-kernel_dist".format(kernel_build_variant), + data = module_rules, + dist_dir = "out/target/product/{}/dlkm/lib/modules/".format(target), + flat = True, + wipe_dist_dir = False, + allow_duplicate_filenames = False, + mode_overrides = {"**/*": "644"}, + log = "info", + ) + + + kernel_modules_install( + name = "{}_modules_install".format(kernel_build_variant), + kernel_build = "//msm-kernel:{}".format(kernel_build_variant), + kernel_modules = module_rules + ) + +def define_consolidate_gki_modules(target, modules, extra_options = [], config_option = None): + define_target_variant_modules(target, "consolidate", modules, extra_options, config_option) + define_target_variant_modules(target, "gki", modules, extra_options, config_option) diff --git a/securemsm_modules.bzl b/securemsm_modules.bzl new file mode 100644 index 0000000000..f131f3ef72 --- /dev/null +++ b/securemsm_modules.bzl @@ -0,0 +1,76 @@ +SMCINVOKE_PATH = "smcinvoke" +QSEECOM_PATH = "qseecom" +TZLOG_PATH = "tz_log" + +# This dictionary holds all the securemsm-kernel modules included by calling register_securemsm_module +securemsm_modules = {} +securemsm_modules_by_config = {} + +# Registers securemsm module to kernel build system. +# name: The name of the module. The name of the file generated for this module will be {name}.ko. +# path: The path that will be prepended to all sources listed for this module. +# config_option: If this module is enabled, the config optiont that will get enabled if so. Not all modules have this, and this is an optional parameter. +# config_srcs: A dictionary of sources to be added to the module depending on if a configuration option is enabled or not. The keys to the dictionary are +# the name of the config option, and the value depends If it is a list, it will just be the list of sources to be added to the module if the config option +# is enabled. If the value is another dictionary, then you can specify sources to be added if the config option is DISABLED by having a list under the +# default_srcs: A list of sources to be added to the module regardless of configuration options. +# deps: A list of kernel_module or ddk_module rules that this module depends on. + +def register_securemsm_module(name, path = None, config_option = None, default_srcs = [], config_srcs = {}, deps = [], srcs = [], copts = []): + processed_config_srcs = {} + + for config_src_name in config_srcs: + config_src = config_srcs[config_src_name] + + if type(config_src) == "list": + processed_config_srcs[config_src_name] = { True: config_src } + else: + processed_config_srcs[config_src_name] = config_src + + module = { + "name": name, + "path": path, + "default_srcs": default_srcs, + "config_srcs": processed_config_srcs, + "config_option": config_option, + "deps": deps, + "copts": copts, + "srcs": srcs, + } + + securemsm_modules[name] = module + + if config_option: + securemsm_modules_by_config[config_option] = name + + +# ------------------------------------ SECUREMSM MODULE DEFINITIONS --------------------------------- +register_securemsm_module( + name = "smcinvoke_dlkm", + path = SMCINVOKE_PATH, + default_srcs = [ + "smcinvoke.c", + "smcinvoke_kernel.c", + "trace_smcinvoke.h", + "IQSEEComCompat.h", + "IQSEEComCompatAppLoader.h", + + ], + deps = [":smcinvoke_kernel_headers"], +) + +register_securemsm_module( + name = "qseecom_dlkm", + path = QSEECOM_PATH, + default_srcs = ["qseecom.c", + "ice.h"], + deps = [":securemsm_kernel_headers"], + srcs = ["config/sec-kernel_defconfig_qseecom.h"], + copts = ["-include", "config/sec-kernel_defconfig_qseecom.h"], +) + +register_securemsm_module( + name = "tz_log_dlkm", + path = TZLOG_PATH, + default_srcs = ["tz_log.c"], +) diff --git a/smcinvoke/trace_smcinvoke.h b/smcinvoke/trace_smcinvoke.h index 97a66069c9..e6698e4662 100644 --- a/smcinvoke/trace_smcinvoke.h +++ b/smcinvoke/trace_smcinvoke.h @@ -487,10 +487,16 @@ TRACE_EVENT(smcinvoke_release, ); #endif /* _TRACE_SMCINVOKE_H */ +/* +* Path must be relative to location of 'define_trace.h' header in kernel +* Define path if not defined in bazel file + */ +#ifndef SMCINVOKE_TRACE_INCLUDE_PATH +#define SMCINVOKE_TRACE_INCLUDE_PATH ../../../../vendor/qcom/opensource/securemsm-kernel/smcinvoke +#endif #undef TRACE_INCLUDE_PATH -#define TRACE_INCLUDE_PATH ../../../../vendor/qcom/opensource/securemsm-kernel/smcinvoke - +#define TRACE_INCLUDE_PATH SMCINVOKE_TRACE_INCLUDE_PATH #undef TRACE_INCLUDE_FILE #define TRACE_INCLUDE_FILE trace_smcinvoke