diff --git a/01-Debug/01-Panic/Makefile b/01-Debug/01-Panic/Makefile new file mode 100644 index 000000000..71b7ef1c6 --- /dev/null +++ b/01-Debug/01-Panic/Makefile @@ -0,0 +1,85 @@ +#******************************************************************************* +# HangZhou Hikvision Digital Technology Co., Ltd. All Right Reserved. +# Author : +# Version : V1.0.0 2020.10.21 +# Description : +# Note : yegaoyang@hikvision.com.cn Modified 2020.10.21 +#******************************************************************************* + +#******************************************************************************* +# Path information +#******************************************************************************* +LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) +KERNEL_DIR ?= /home/gaoyang3513/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd +#KERNEL_DIR ?= $(HOME)/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd/ + +# ParDirectory +LIBS_DIR := +INCLUDE_DIR := + +# Subdirectory + +# Output +OUTPUT_DIR := $(LOCAL_DIR)/output +INSTALL_DIR ?= $(LOCAL_DIR)/__install + +#******************************************************************************* +# Variables +#******************************************************************************* +MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) + +MOD_NAME := test_panic + +#******************************************************************************* +# Compile configure +#******************************************************************************* +ARCH ?= arm64 +CROSS_COMPILE ?= ${HOME}/Source/06-SG20x/02-Projects/SDK_SG200x_V2/host-tools/gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- + +CC := $(CROSS_COMPILE)gcc +LD := $(CROSS_COMPILE)ld +AR := $(CROSS_COMPILE)ar +STRIP := $(CROSS_COMPILE)strip + +export ARCH CROSS_COMPILE + +#******************************************************************************* +# Targets +#******************************************************************************* +.PHONY: all install clean + +ifeq ($(KERNELRELEASE),) +all: init + @$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES) +# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private + +clean: +# File + @for n in *.o *.ko *.mod.c *.mod *.cmd *.symvers *.order; do \ + find $(LOCAL_DIR) -type f -name $$n -exec rm {} \;;\ + done +# Directory + @if [ -d $(LOCAL_DIR)/output ]; then rm -rf $(LOCAL_DIR)/output; fi; + @if [ -d $(LOCAL_DIR)/__install ]; then rm -rf $(LOCAL_DIR)/__install; fi; + @if [ -d $(LOCAL_DIR)/.tmp_versions ]; then rm -rf $(LOCAL_DIR)/.tmp_versions; fi; + + +install: + @mkdir -p $(INSTALL_DIR)/lib/modules/private + @install -m 644 -D $(MOD_NAME).ko $(INSTALL_DIR)/lib/modules/private; + +init: + @mkdir -p $(OUTPUT_DIR) + +else +obj-m := $(MOD_NAME).o + +$(MOD_NAME)-objs := test_ps.o + +INC_DIRS := + +ccflags-y += + +EXTRA_CFLAGS += -Wno-error=date-time # Fix compile error on gcc 4.9 and later +EXTRA_CFLAGS += -Wno-date-time +endif # ifeq ($(KERNELRELEASE),) diff --git a/01-Debug/01-Panic/test_ps.c b/01-Debug/01-Panic/test_ps.c new file mode 100644 index 000000000..b70c4c948 --- /dev/null +++ b/01-Debug/01-Panic/test_ps.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned int test_type; +module_param(test_type, uint, 0400); +MODULE_PARM_DESC(test_type, "set to 1 to try to OOM (default 0)"); + +static int trigger_oops(void) +{ + int *ptr = (int *)0; // 强制类型转换0地址为指针并尝试读取 + + printk(KERN_ALERT "Dereferenced NULL pointer value: %d\n", *ptr); + + return 0; +} + +static int trigger_oom(void) +{ + void *memory = NULL; + size_t memory_size = 0; + + printk(KERN_INFO "oom_trigger: Initializing the oom_trigger LKM\n"); + + memory_size = 100*1024*1024; + while (1) { // 尝试分配大块内存 + memory = vmalloc(memory_size); + if (!memory) { + printk(KERN_ALERT "oom_trigger: Memory allocation failed\n"); + break; + } + + // 仅为防止编译器优化,实际不访问分配的内存 + memset(memory, 0, memory_size); + } + + return 0; +} + +static int test_data = 0; +static struct rcu_head test_rcu_head; + +static void bad_rcu_callback(struct rcu_head *head) +{ + // 违规操作:在RCU回调函数中睡眠,这可能导致RCU grace period过长 + msleep(1000); + + // 违规操作:修改被RCU保护的数据结构 + test_data++; // 不应在RCU回调中修改全局变量 + + pr_info("Bad RCU callback executed\n"); +} + +static void trigger_rcu(void) +{ + pr_info("RCU error module loaded\n"); + + call_rcu(&test_rcu_head, bad_rcu_callback); +} + +static int is_ready_thread1, is_ready_thread2; +static struct task_struct *thread1; +static struct task_struct *thread2; + +static DEFINE_MUTEX(mutex_a); +static DEFINE_MUTEX(mutex_b); + +static int thread_func(void *data) +{ + char *name = (char *) data; + + if (!strcmp(name, "Thread1")) { + printk(KERN_INFO "%s: Acquiring mutex A...\n", name); + mutex_lock(&mutex_a); + is_ready_thread1 = 1; + while (!is_ready_thread2); + printk(KERN_INFO "%s: Got mutex A. Now trying to acquire mutex B...\n", name); + mutex_lock(&mutex_b); // This will block, causing deadlock + printk(KERN_INFO "%s: Got mutex B.\n", name); + } else { + printk(KERN_INFO "%s: Acquiring mutex B...\n", name); + mutex_lock(&mutex_b); + is_ready_thread2 = 1; + while (!is_ready_thread1); + printk(KERN_INFO "%s: Got mutex B. Now trying to acquire mutex A...\n", name); + mutex_lock(&mutex_a); // This will block, causing deadlock + printk(KERN_INFO "%s: Got mutex A.\n", name); + } + + return 0; +} + +static int trigger_deadlock(void) +{ + thread1 = kthread_run(thread_func, (void *) "Thread1", "thread1"); + if (IS_ERR(thread1)) { + printk(KERN_ERR "Failed to create thread1\n"); + return PTR_ERR(thread1); + } + + thread2 = kthread_run(thread_func, (void *) "Thread2", "thread2"); + if (IS_ERR(thread2)) { + printk(KERN_ERR "Failed to create thread2\n"); + kthread_stop(thread1); + return PTR_ERR(thread2); + } + + return 0; +} + +static int trigger_init(void) { + if (test_type == 0) + trigger_oops(); + else if (test_type == 1) + trigger_oom(); + else if (test_type == 2) + trigger_rcu(); + else if (test_type == 3) + trigger_deadlock(); + + return 0; // 加载模块不应该有返回值,但这里返回0避免编译警告。 +} + +static void __exit trigger_exit(void) { + if (test_type == 3) { + kthread_stop(thread1); + kthread_stop(thread2); + printk(KERN_INFO "Deadlock trigger exit.\n"); + } +} + +module_init(trigger_init); +module_exit(trigger_exit); + +MODULE_LICENSE("GPL"); diff --git a/01-Debug/Makefile b/01-Debug/Makefile new file mode 100644 index 000000000..bfd5e1b2a --- /dev/null +++ b/01-Debug/Makefile @@ -0,0 +1,81 @@ +#******************************************************************************* +# HangZhou Hikvision Digital Technology Co., Ltd. All Right Reserved. +# Author : +# Version : V1.0.0 2020.10.21 +# Description : +# Note : yegaoyang@hikvision.com.cn Modified 2020.10.21 +#******************************************************************************* + +#******************************************************************************* +# Path information +#******************************************************************************* +LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) +KERNEL_DIR ?= /home/gaoyang3513/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd +#KERNEL_DIR ?= $(HOME)/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd/ + +# ParDirectory +LIBS_DIR := +INCLUDE_DIR := + +# Subdirectory +SUB_DIRS := $(LOCAL_DIR)/01-Panic + +# Output +OUTPUT_DIR := $(LOCAL_DIR)/output +INSTALL_DIR ?= $(LOCAL_DIR)/__install + +#******************************************************************************* +# Variables +#******************************************************************************* +MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) + +MOD_NAME := test_panic + +#******************************************************************************* +# Compile configure +#******************************************************************************* +ARCH ?= arm64 +CROSS_COMPILE ?= ${HOME}/Source/06-SG20x/02-Projects/SDK_SG200x_V2/host-tools/gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- + +CC := $(CROSS_COMPILE)gcc +LD := $(CROSS_COMPILE)ld +AR := $(CROSS_COMPILE)ar +STRIP := $(CROSS_COMPILE)strip + +export ARCH CROSS_COMPILE + +#******************************************************************************* +# Targets +#******************************************************************************* +.PHONY: init all clean install menuconfig + +ifeq ($(KERNELRELEASE),) +all: init + @for sub in $(SUB_DIRS); do \ + $(MAKE) -C $$sub || exit "$$?"; \ + $(MAKE) install INSTALL_DIR=$(OUTPUT_DIR) -C $$sub || exit "$$?"; \ + done; + +clean: + @for sub in $(SUB_DIRS); do \ + $(MAKE) clean -C $$sub || exit "$$?"; \ + done; +# Directory + @if [ -d $(LOCAL_DIR)/output ]; then rm -rf $(LOCAL_DIR)/output; fi; + @if [ -d $(LOCAL_DIR)/__install ]; then rm -rf $(LOCAL_DIR)/__install; fi; + +install: + @mkdir -p $(INSTALL_DIR) + @cp -arf $(OUTPUT_DIR)/. $(INSTALL_DIR) + +init: + @mkdir -p $(OUTPUT_DIR); +else +# called from kernel build system: just declare what our modules are +obj-y += $(SUB_DIRS) + +INC_DIRS := + +subdir-ccflags-y += $(addprefix -I, $(INC_DIRS)) + +endif # ifeq ($(KERNELRELEASE),) \ No newline at end of file diff --git a/Makefile b/Makefile index 725bdc297..3d4b7d55a 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ INSTALL_DIR ?= $(LOCAL_DIR)/__install #******************************************************************************* MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) -SUB_DIRS := spi/ +SUB_DIRS := 01-Debug/ #******************************************************************************* # Compile configure diff --git a/spi/01-NFC_PN5180/Makefile b/spi/01-NFC_PN5180/Makefile index 20e7b9e15..7d2d83035 100644 --- a/spi/01-NFC_PN5180/Makefile +++ b/spi/01-NFC_PN5180/Makefile @@ -49,7 +49,7 @@ else all: init @$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES) - @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private +# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private clean: # File diff --git a/spi/02-Sub1G_CMT2301/Makefile b/spi/02-Sub1G_CMT2301/Makefile index 179e62774..ffef528af 100644 --- a/spi/02-Sub1G_CMT2301/Makefile +++ b/spi/02-Sub1G_CMT2301/Makefile @@ -49,7 +49,7 @@ else all: init @$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES) - @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private +# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private clean: # File diff --git a/spi/Makefile b/spi/Makefile index 7a35281f4..591c75710 100644 --- a/spi/Makefile +++ b/spi/Makefile @@ -12,8 +12,8 @@ LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) # Subdirectory -PN5180_DIR := 01-NFC_PN5180 -CMT2301_DIR := 02-Sub1G_CMT2301 +PN5180_DIR := $(LOCAL_DIR)/01-NFC_PN5180 +CMT2301_DIR := $(LOCAL_DIR)/02-Sub1G_CMT2301 OUTPUT_DIR := $(LOCAL_DIR)/output INSTALL_DIR ?= $(LOCAL_DIR)/__install