1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0-only 3# 4# Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org> 5# 6# Abstract class for generating kernel runtime verification monitors from specification file 7 8import platform 9import os 10 11 12class RVGenerator: 13 rv_dir = "kernel/trace/rv" 14 15 def __init__(self, extra_params={}): 16 self.name = extra_params.get("model_name") 17 self.parent = extra_params.get("parent") 18 self.abs_template_dir = \ 19 os.path.join(os.path.dirname(__file__), "templates", self.template_dir) 20 self.main_c = self._read_template_file("main.c") 21 self.kconfig = self._read_template_file("Kconfig") 22 self.description = extra_params.get("description", self.name) or "auto-generated" 23 self.auto_patch = extra_params.get("auto_patch") 24 if self.auto_patch: 25 self.__fill_rv_kernel_dir() 26 27 def __fill_rv_kernel_dir(self): 28 29 # first try if we are running in the kernel tree root 30 if os.path.exists(self.rv_dir): 31 return 32 33 # offset if we are running inside the kernel tree from verification/dot2 34 kernel_path = os.path.join("../..", self.rv_dir) 35 36 if os.path.exists(kernel_path): 37 self.rv_dir = kernel_path 38 return 39 40 if platform.system() != "Linux": 41 raise OSError("I can only run on Linux.") 42 43 kernel_path = os.path.join(f"/lib/modules/{platform.release()}/build", self.rv_dir) 44 45 # if the current kernel is from a distro this may not be a full kernel tree 46 # verify that one of the files we are going to modify is available 47 if os.path.exists(os.path.join(kernel_path, "rv_trace.h")): 48 self.rv_dir = kernel_path 49 return 50 51 raise FileNotFoundError("Could not find the rv directory, do you have the kernel source installed?") 52 53 def _read_file(self, path): 54 with open(path, 'r') as fd: 55 content = fd.read() 56 return content 57 58 def _read_template_file(self, file): 59 try: 60 path = os.path.join(self.abs_template_dir, file) 61 return self._read_file(path) 62 except OSError: 63 # Specific template file not found. Try the generic template file in the template/ 64 # directory, which is one level up 65 path = os.path.join(self.abs_template_dir, "..", file) 66 return self._read_file(path) 67 68 def fill_parent(self): 69 return f"&rv_{self.parent}" if self.parent else "NULL" 70 71 def fill_include_parent(self): 72 if self.parent: 73 return f"#include <monitors/{self.parent}/{self.parent}.h>\n" 74 return "" 75 76 def fill_tracepoint_handlers_skel(self): 77 return "NotImplemented" 78 79 def fill_tracepoint_attach_probe(self): 80 return "NotImplemented" 81 82 def fill_tracepoint_detach_helper(self): 83 return "NotImplemented" 84 85 def fill_main_c(self): 86 main_c = self.main_c 87 tracepoint_handlers = self.fill_tracepoint_handlers_skel() 88 tracepoint_attach = self.fill_tracepoint_attach_probe() 89 tracepoint_detach = self.fill_tracepoint_detach_helper() 90 parent = self.fill_parent() 91 parent_include = self.fill_include_parent() 92 93 main_c = main_c.replace("%%MODEL_NAME%%", self.name) 94 main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers) 95 main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach) 96 main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach) 97 main_c = main_c.replace("%%DESCRIPTION%%", self.description) 98 main_c = main_c.replace("%%PARENT%%", parent) 99 main_c = main_c.replace("%%INCLUDE_PARENT%%", parent_include) 100 101 return main_c 102 103 def fill_model_h(self): 104 return "NotImplemented" 105 106 def fill_monitor_class_type(self): 107 return "NotImplemented" 108 109 def fill_monitor_class(self): 110 return "NotImplemented" 111 112 def fill_tracepoint_args_skel(self, tp_type): 113 return "NotImplemented" 114 115 def fill_monitor_deps(self): 116 buff = [] 117 buff.append(" # XXX: add dependencies if there") 118 if self.parent: 119 buff.append(f" depends on RV_MON_{self.parent.upper()}") 120 buff.append(" default y") 121 return '\n'.join(buff) 122 123 def fill_kconfig(self): 124 kconfig = self.kconfig 125 monitor_class_type = self.fill_monitor_class_type() 126 monitor_deps = self.fill_monitor_deps() 127 kconfig = kconfig.replace("%%MODEL_NAME%%", self.name) 128 kconfig = kconfig.replace("%%MODEL_NAME_UP%%", self.name.upper()) 129 kconfig = kconfig.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type) 130 kconfig = kconfig.replace("%%DESCRIPTION%%", self.description) 131 kconfig = kconfig.replace("%%MONITOR_DEPS%%", monitor_deps) 132 return kconfig 133 134 def _patch_file(self, file, marker, line): 135 assert self.auto_patch 136 file_to_patch = os.path.join(self.rv_dir, file) 137 content = self._read_file(file_to_patch) 138 content = content.replace(marker, line + "\n" + marker) 139 self.__write_file(file_to_patch, content) 140 141 def fill_tracepoint_tooltip(self): 142 monitor_class_type = self.fill_monitor_class_type() 143 if self.auto_patch: 144 self._patch_file("rv_trace.h", 145 f"// Add new monitors based on CONFIG_{monitor_class_type} here", 146 f"#include <monitors/{self.name}/{self.name}_trace.h>") 147 return f" - Patching {self.rv_dir}/rv_trace.h, double check the result" 148 149 return f""" - Edit {self.rv_dir}/rv_trace.h: 150Add this line where other tracepoints are included and {monitor_class_type} is defined: 151#include <monitors/{self.name}/{self.name}_trace.h> 152""" 153 154 def _kconfig_marker(self, container=None) -> str: 155 return f"# Add new {container + ' ' if container else ''}monitors here" 156 157 def fill_kconfig_tooltip(self): 158 if self.auto_patch: 159 # monitors with a container should stay together in the Kconfig 160 self._patch_file("Kconfig", 161 self._kconfig_marker(self.parent), 162 f"source \"kernel/trace/rv/monitors/{self.name}/Kconfig\"") 163 return f" - Patching {self.rv_dir}/Kconfig, double check the result" 164 165 return f""" - Edit {self.rv_dir}/Kconfig: 166Add this line where other monitors are included: 167source \"kernel/trace/rv/monitors/{self.name}/Kconfig\" 168""" 169 170 def fill_makefile_tooltip(self): 171 name = self.name 172 name_up = name.upper() 173 if self.auto_patch: 174 self._patch_file("Makefile", 175 "# Add new monitors here", 176 f"obj-$(CONFIG_RV_MON_{name_up}) += monitors/{name}/{name}.o") 177 return f" - Patching {self.rv_dir}/Makefile, double check the result" 178 179 return f""" - Edit {self.rv_dir}/Makefile: 180Add this line where other monitors are included: 181obj-$(CONFIG_RV_MON_{name_up}) += monitors/{name}/{name}.o 182""" 183 184 def fill_monitor_tooltip(self): 185 if self.auto_patch: 186 return f" - Monitor created in {self.rv_dir}/monitors/{self.name}" 187 return f" - Move {self.name}/ to the kernel's monitor directory ({self.rv_dir}/monitors)" 188 189 def __create_directory(self): 190 path = self.name 191 if self.auto_patch: 192 path = os.path.join(self.rv_dir, "monitors", path) 193 try: 194 os.mkdir(path) 195 except FileExistsError: 196 return 197 198 def __write_file(self, file_name, content): 199 with open(file_name, 'w') as file: 200 file.write(content) 201 202 def _create_file(self, file_name, content): 203 path = f"{self.name}/{file_name}" 204 if self.auto_patch: 205 path = os.path.join(self.rv_dir, "monitors", path) 206 self.__write_file(path, content) 207 208 def print_files(self): 209 main_c = self.fill_main_c() 210 211 self.__create_directory() 212 213 path = f"{self.name}.c" 214 self._create_file(path, main_c) 215 216 model_h = self.fill_model_h() 217 path = f"{self.name}.h" 218 self._create_file(path, model_h) 219 220 kconfig = self.fill_kconfig() 221 self._create_file("Kconfig", kconfig) 222 223 224class Monitor(RVGenerator): 225 monitor_types = {"global": 1, "per_cpu": 2, "per_task": 3, "per_obj": 4} 226 227 def __init__(self, extra_params={}): 228 super().__init__(extra_params) 229 self.trace_h = self._read_template_file("trace.h") 230 231 def fill_trace_h(self): 232 trace_h = self.trace_h 233 monitor_class = self.fill_monitor_class() 234 monitor_class_type = self.fill_monitor_class_type() 235 tracepoint_args_skel_event = self.fill_tracepoint_args_skel("event") 236 tracepoint_args_skel_error = self.fill_tracepoint_args_skel("error") 237 tracepoint_args_skel_error_env = self.fill_tracepoint_args_skel("error_env") 238 trace_h = trace_h.replace("%%MODEL_NAME%%", self.name) 239 trace_h = trace_h.replace("%%MODEL_NAME_UP%%", self.name.upper()) 240 trace_h = trace_h.replace("%%MONITOR_CLASS%%", monitor_class) 241 trace_h = trace_h.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type) 242 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_EVENT%%", tracepoint_args_skel_event) 243 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_ERROR%%", tracepoint_args_skel_error) 244 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_ERROR_ENV%%", tracepoint_args_skel_error_env) 245 return trace_h 246 247 def print_files(self): 248 super().print_files() 249 trace_h = self.fill_trace_h() 250 path = f"{self.name}_trace.h" 251 self._create_file(path, trace_h) 252