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# Abtract 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("/lib/modules/%s/build" % platform.release(), 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 try: 55 fd = open(path, 'r') 56 except OSError: 57 raise Exception("Cannot open the file: %s" % path) 58 59 content = fd.read() 60 61 fd.close() 62 return content 63 64 def _read_template_file(self, file): 65 try: 66 path = os.path.join(self.abs_template_dir, file) 67 return self._read_file(path) 68 except Exception: 69 # Specific template file not found. Try the generic template file in the template/ 70 # directory, which is one level up 71 path = os.path.join(self.abs_template_dir, "..", file) 72 return self._read_file(path) 73 74 def fill_parent(self): 75 return "&rv_%s" % self.parent if self.parent else "NULL" 76 77 def fill_include_parent(self): 78 if self.parent: 79 return "#include <monitors/%s/%s.h>\n" % (self.parent, self.parent) 80 return "" 81 82 def fill_tracepoint_handlers_skel(self): 83 return "NotImplemented" 84 85 def fill_tracepoint_attach_probe(self): 86 return "NotImplemented" 87 88 def fill_tracepoint_detach_helper(self): 89 return "NotImplemented" 90 91 def fill_main_c(self): 92 main_c = self.main_c 93 tracepoint_handlers = self.fill_tracepoint_handlers_skel() 94 tracepoint_attach = self.fill_tracepoint_attach_probe() 95 tracepoint_detach = self.fill_tracepoint_detach_helper() 96 parent = self.fill_parent() 97 parent_include = self.fill_include_parent() 98 99 main_c = main_c.replace("%%MODEL_NAME%%", self.name) 100 main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers) 101 main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach) 102 main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach) 103 main_c = main_c.replace("%%DESCRIPTION%%", self.description) 104 main_c = main_c.replace("%%PARENT%%", parent) 105 main_c = main_c.replace("%%INCLUDE_PARENT%%", parent_include) 106 107 return main_c 108 109 def fill_model_h(self): 110 return "NotImplemented" 111 112 def fill_monitor_class_type(self): 113 return "NotImplemented" 114 115 def fill_monitor_class(self): 116 return "NotImplemented" 117 118 def fill_tracepoint_args_skel(self, tp_type): 119 return "NotImplemented" 120 121 def fill_monitor_deps(self): 122 buff = [] 123 buff.append(" # XXX: add dependencies if there") 124 if self.parent: 125 buff.append(" depends on RV_MON_%s" % self.parent.upper()) 126 buff.append(" default y") 127 return '\n'.join(buff) 128 129 def fill_kconfig(self): 130 kconfig = self.kconfig 131 monitor_class_type = self.fill_monitor_class_type() 132 monitor_deps = self.fill_monitor_deps() 133 kconfig = kconfig.replace("%%MODEL_NAME%%", self.name) 134 kconfig = kconfig.replace("%%MODEL_NAME_UP%%", self.name.upper()) 135 kconfig = kconfig.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type) 136 kconfig = kconfig.replace("%%DESCRIPTION%%", self.description) 137 kconfig = kconfig.replace("%%MONITOR_DEPS%%", monitor_deps) 138 return kconfig 139 140 def _patch_file(self, file, marker, line): 141 assert self.auto_patch 142 file_to_patch = os.path.join(self.rv_dir, file) 143 content = self._read_file(file_to_patch) 144 content = content.replace(marker, line + "\n" + marker) 145 self.__write_file(file_to_patch, content) 146 147 def fill_tracepoint_tooltip(self): 148 monitor_class_type = self.fill_monitor_class_type() 149 if self.auto_patch: 150 self._patch_file("rv_trace.h", 151 "// Add new monitors based on CONFIG_%s here" % monitor_class_type, 152 "#include <monitors/%s/%s_trace.h>" % (self.name, self.name)) 153 return " - Patching %s/rv_trace.h, double check the result" % self.rv_dir 154 155 return """ - Edit %s/rv_trace.h: 156Add this line where other tracepoints are included and %s is defined: 157#include <monitors/%s/%s_trace.h> 158""" % (self.rv_dir, monitor_class_type, self.name, self.name) 159 160 def _kconfig_marker(self, container=None) -> str: 161 return "# Add new %smonitors here" % (container + " " 162 if container else "") 163 164 def fill_kconfig_tooltip(self): 165 if self.auto_patch: 166 # monitors with a container should stay together in the Kconfig 167 self._patch_file("Kconfig", 168 self._kconfig_marker(self.parent), 169 "source \"kernel/trace/rv/monitors/%s/Kconfig\"" % (self.name)) 170 return " - Patching %s/Kconfig, double check the result" % self.rv_dir 171 172 return """ - Edit %s/Kconfig: 173Add this line where other monitors are included: 174source \"kernel/trace/rv/monitors/%s/Kconfig\" 175""" % (self.rv_dir, self.name) 176 177 def fill_makefile_tooltip(self): 178 name = self.name 179 name_up = name.upper() 180 if self.auto_patch: 181 self._patch_file("Makefile", 182 "# Add new monitors here", 183 "obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o" % (name_up, name, name)) 184 return " - Patching %s/Makefile, double check the result" % self.rv_dir 185 186 return """ - Edit %s/Makefile: 187Add this line where other monitors are included: 188obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o 189""" % (self.rv_dir, name_up, name, name) 190 191 def fill_monitor_tooltip(self): 192 if self.auto_patch: 193 return " - Monitor created in %s/monitors/%s" % (self.rv_dir, self. name) 194 return " - Move %s/ to the kernel's monitor directory (%s/monitors)" % (self.name, self.rv_dir) 195 196 def __create_directory(self): 197 path = self.name 198 if self.auto_patch: 199 path = os.path.join(self.rv_dir, "monitors", path) 200 try: 201 os.mkdir(path) 202 except FileExistsError: 203 return 204 except: 205 print("Fail creating the output dir: %s" % self.name) 206 207 def __write_file(self, file_name, content): 208 try: 209 file = open(file_name, 'w') 210 except: 211 print("Fail writing to file: %s" % file_name) 212 213 file.write(content) 214 215 file.close() 216 217 def _create_file(self, file_name, content): 218 path = "%s/%s" % (self.name, file_name) 219 if self.auto_patch: 220 path = os.path.join(self.rv_dir, "monitors", path) 221 self.__write_file(path, content) 222 223 def __get_main_name(self): 224 path = "%s/%s" % (self.name, "main.c") 225 if not os.path.exists(path): 226 return "main.c" 227 return "__main.c" 228 229 def print_files(self): 230 main_c = self.fill_main_c() 231 232 self.__create_directory() 233 234 path = "%s.c" % self.name 235 self._create_file(path, main_c) 236 237 model_h = self.fill_model_h() 238 path = "%s.h" % self.name 239 self._create_file(path, model_h) 240 241 kconfig = self.fill_kconfig() 242 self._create_file("Kconfig", kconfig) 243 244 245class Monitor(RVGenerator): 246 monitor_types = { "global" : 1, "per_cpu" : 2, "per_task" : 3 } 247 248 def __init__(self, extra_params={}): 249 super().__init__(extra_params) 250 self.trace_h = self._read_template_file("trace.h") 251 252 def fill_trace_h(self): 253 trace_h = self.trace_h 254 monitor_class = self.fill_monitor_class() 255 monitor_class_type = self.fill_monitor_class_type() 256 tracepoint_args_skel_event = self.fill_tracepoint_args_skel("event") 257 tracepoint_args_skel_error = self.fill_tracepoint_args_skel("error") 258 trace_h = trace_h.replace("%%MODEL_NAME%%", self.name) 259 trace_h = trace_h.replace("%%MODEL_NAME_UP%%", self.name.upper()) 260 trace_h = trace_h.replace("%%MONITOR_CLASS%%", monitor_class) 261 trace_h = trace_h.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type) 262 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_EVENT%%", tracepoint_args_skel_event) 263 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_ERROR%%", tracepoint_args_skel_error) 264 return trace_h 265 266 def print_files(self): 267 super().print_files() 268 trace_h = self.fill_trace_h() 269 path = "%s_trace.h" % self.name 270 self._create_file(path, trace_h) 271