# SPDX-License-Identifier: GPL-2.0-only
config HAVE_LIVEPATCH_FTRACE
	bool
	help
	  Arch supports kernel live patching based on ftrace

config HAVE_LIVEPATCH_WO_FTRACE
	bool
	help
	  Arch supports kernel live patching without ftrace

if HAVE_LIVEPATCH_FTRACE || HAVE_LIVEPATCH_WO_FTRACE
menu "Enable Livepatch"


config LIVEPATCH
	bool "Kernel Live Patching"
	depends on MODULES
	depends on SYSFS
	depends on KALLSYMS_ALL
	depends on HAVE_LIVEPATCH_FTRACE || HAVE_LIVEPATCH_WO_FTRACE
	depends on !TRIM_UNUSED_KSYMS
	depends on DEBUG_INFO || LIBLINUX
	default n
	help
	  Say Y here if you want to support kernel live patching.
	  This option has no runtime impact until a kernel "patch"
	  module uses the interface provided by this option to register
	  a patch, causing calls to patched functions to be redirected
	  to new function code contained in the patch module.

choice
	prompt "live patching method"
	depends on LIVEPATCH
	help
	  Live patching implementation method configuration.

config LIVEPATCH_FTRACE
	bool "based on ftrace"
	depends on HAVE_LIVEPATCH_FTRACE
	depends on DYNAMIC_FTRACE_WITH_REGS
	select LIVEPATCH_PER_TASK_CONSISTENCY
	help
	  Supports kernel live patching based on ftrace

config LIVEPATCH_WO_FTRACE
	bool "without ftrace"
	depends on HAVE_LIVEPATCH_WO_FTRACE
	select LIVEPATCH_STOP_MACHINE_CONSISTENCY
	help
	  Supports kernel live patching without ftrace

endchoice

choice
	prompt "live patching consistency model"
	depends on LIVEPATCH
	default LIVEPATCH_PER_TASK_CONSISTENCY if LIVEPATCH_FTRACE
	default LIVEPATCH_STOP_MACHINE_CONSISTENCY if LIVEPATCH_WO_FTRACE
	help
	  Livepatch consistency model configuration.

config LIVEPATCH_PER_TASK_CONSISTENCY
	bool "per task consistency"
	depends on LIVEPATCH_FTRACE
	help
	  Use basic per-task consistency model
	  It's a hybrid of kGraft and kpatch:
	  uses kGraft's per-task consistency and syscall
	  barrier switching combined with kpatch's stack
	  trace switching. There are also a number of
	  fallback options which make it quite flexible.

config LIVEPATCH_STOP_MACHINE_CONSISTENCY
	bool "stop machine consistency"
	depends on LIVEPATCH_WO_FTRACE
	help
	  Use stop machine consistency model
	  stop-machine consistency and kpatch's stack
	  trace checking.

endchoice

config LIVEPATCH_SHADOW_VARS
        bool "Support livepatch shadow vars"
        depends on LIVEPATCH && !CONFIG_LIVEPATCH_PER_TASK_CONSISTENCY
        default y
        help
          support livepatch shadow vars.

config LIVEPATCH_STACK
	bool "Enforcing the patch stacking principle"
	depends on LIVEPATCH_FTRACE || LIVEPATCH_WO_FTRACE
	default y
	help
	  Say N here if you want to remove the patch stacking principle.

config LIVEPATCH_RESTRICT_KPROBE
	bool "Enforing check livepatch and kprobe restrict"
	depends on LIVEPATCH_WO_FTRACE
	depends on KPROBES
	default y
	help
	  Livepatch without ftrace and kprobe are conflicting.
	  We should not patch for the functions where registered with kprobe,
	  and vice versa.
	  Say Y here if you want to check those.

config RTOS_LIVEPATCH_X86_WALK_INTERRUPT_STACK
	bool "Do not exit stacktrace when kernel interrupt or exception"
	depends on LIVEPATCH_WO_FTRACE && X86
	default n
	help
	  Kernel mode registers on the stack indicate an in-kernel
	  interrupt or exception, which can make frame pointers
	  unreliable.
	  Say Y here if you do not exit stacktrace in this scenario.

config LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE
	bool "No stop_machine in breakpoint optimization mode"
	depends on LIVEPATCH_WO_FTRACE
	default n
	help
	  In breakpoint optimization mode, check tasks calltrace
	  in batches without using stop machine so that reduce the
	  service downtime.
	  Say N if you are unsure.

config EULEROS_LIVEPATCH_LONG_SYMBOL
	bool "support relocate symbol name whose length longer than 128 bytes"
	depends on LIVEPATCH
	default n
	help
	  symbols longer than KSYM_NAME_LEN will be truncated in strtab,
	  we can not find them using original name, so we use a new format
	  .klp.sym.objname-ref_name,ref_offset to find a unique symbol
	  and add ref_offset to get the long name symbol address.

config EULEROS_LIVEPATCH_LOAD_TIME_OPTIMIZATION
	bool "patch load time optimization"
	depends on LIVEPATCH
	default n
	help
	  module_kallsyms_on_each_symbol traverse every symbol in each module.
	  When we known the object module, we just traverse symbols in the
	  object module symbol list. we can reduce patch load time after apply
	  this patch.

config LIVEPATCH_ISOLATE_KPROBE
	bool "Isolating livepatch and kprobe"
	depends on LIVEPATCH_RESTRICT_KPROBE
	depends on DYNAMIC_FTRACE && (X86_64 || ARM64)
	default n
	help
	  Kprobe and livepatch_wo may modify the first several instructions of
	  a function at the same time which causing a conflict. Since dynamic
	  ftrace reserve instructions at non-notrace functions, we can allow
	  kprobe works on the reserved instructions and livepatch_wo work on
	  other instructions so as to avoid the conflict. But note that we also
	  do not allow both modify the same instruction when a function is
	  marked as 'notrace' and without the reserved instructions.

config EULEROS_LIVEPATCH_FORCE_PATCH
	bool "support force enable or disable patch"
	depends on LIVEPATCH && LIVEPATCH_STOP_MACHINE_CONSISTENCY
	default y
	help
	  some functions in IO path are called frequently, in this case,
	  patches can not be enabled due to stack check failing. We use
	  the klp_func->force to indicate functions that can be
	  enabled/disabled without stack check. Also, we add a new patch
	  state forcedisabled to indicate the patch being disabled and
	  could not being rmmod.

endmenu
endif
