This commit is contained in:
Cheng Jian
2020-06-30 16:11:40 +08:00
parent 80b98e06ce
commit 88bb106201
6 changed files with 116 additions and 8 deletions
@@ -0,0 +1,14 @@
obj-m := test_kthread_run.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
@@ -0,0 +1,59 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h> // error: implicit declaration of function msleep
static struct task_struct *ptask;
static int thread_function(void *data)
{
int time_count = 0;
do
{
printk(KERN_INFO "thread_function: %d times", ++time_count);
msleep(1000);
}while(!kthread_should_stop()
&& time_count<=30);
return time_count;
}
static int test_kthread_run_init(void)
{
int cpu = 1;
printk(KERN_INFO "test_kthread_run, world!\n");
ptask = kthread_run(thread_function, NULL, "mythread%d", cpu);
kthread_bind(ptask, cpu);
if (IS_ERR(ptask))
{
printk(KERN_INFO "create kthread failed!\n");
}
else
{
printk(KERN_INFO "create ktrhead ok!\n");
}
return 0;
}
static void test_kthread_run_exit(void)
{
printk(KERN_INFO "test_kthread_run, exit!\n");
if (!IS_ERR(ptask))
{
int ret = kthread_stop(ptask);
printk(KERN_INFO "thread function has run %ds\n", ret);
}
}
module_exit(test_kthread_run_exit);
module_init(test_kthread_run_init);
MODULE_LICENSE("Dual BSD/GPL");
@@ -16,7 +16,7 @@ va 接收待查询的虚拟地址
#include <linux/printk.h>
#include <linux/memblock.h>
#include <linux/kallsyms.h>
#include <asm/fixmap.h>
MODULE_LICENSE("GPL");
@@ -39,11 +39,8 @@ void show_layout(void)
#define MLM(b, t) b, t, ((t) - (b)) >> 20
#define MLG(b, t) b, t, ((t) - (b)) >> 30
#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
#if 0
pr_notice("Virtual kernel memory layout:\n");
pr_notice(" memory : 0x%16lx - 0x%16lx (%6ld MB)\n",
MLM((unsigned long)phys_to_virt(SYMS_FUN("memblock_start_of_DRAM")),
(unsigned long)high_memory));
#ifdef CONFIG_KASAN
pr_notice(" kasan : 0x%16lx - 0x%16lx (%6ld GB)\n",
MLG(KASAN_SHADOW_START, KASAN_SHADOW_END));
@@ -70,14 +67,17 @@ void show_layout(void)
MLM(PCI_IO_START, PCI_IO_END));
#ifdef CONFIG_SPARSEMEM_VMEMMAP
pr_notice(" vmemmap : 0x%16lx - 0x%16lx (%6ld GB maximum)\n",
MLG(VMEMMAP_START, VMEMMAP_START + VMEMMAP_SIZE));
MLG(SYMS_VAL("vmemmap"), SYMS_VAL("vmemmap") + VMEMMAP_SIZE));
pr_notice(" 0x%16lx - 0x%16lx (%6ld MB actual)\n",
MLM((unsigned long)phys_to_page(SYMS_FUN("memblock_start_of_DRAM")),
(unsigned long)virt_to_page(high_memory)));
#endif
pr_notice(" memory : 0x%16lx - 0x%16lx (%6ld MB)\n",
MLM((unsigned long)phys_to_virt(SYMS_FUN("memblock_start_of_DRAM")),
(unsigned long)high_memory));
#endif
//pr_notice("KIMAGE_VADDR: 0x%16lx\n", KIMAGE_VADDR);
pr_notice("PAGE_OFFSET : 0x%16lx\n", PAGE_OFFSET);
pr_notice("KIMAGE_VADDR: 0x%16lx\n", KIMAGE_VADDR);
#undef MLK
#undef MLM
#undef MLK_ROUNDUP
Binary file not shown.
@@ -0,0 +1,35 @@
#include <stdio.h>
int func3(int a, int b)
{
int c = a + b;
printf("c = %d\n", c);
return a + b;
}
int func2(int a)
{
int b = 20;
b = func3(a, b);
printf("b = %d\n", b);
return b;
}
int func1(void)
{
int a;
a = func2(10);
printf("a = %d\n", a);
return a;
}
int main(int argc, const char *argv[])
{
func1();
return 0;
}