first sample

This commit is contained in:
ioacademy-jikim
2015-08-19 19:26:23 +09:00
commit e92d98c6d0
62 changed files with 2573 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
obj-m = misc_dev.o
KDIR = /usr/src/tegra
PWD = $(shell pwd)
default:
make -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
make -C $(KDIR) SUBDIRS=$(PWD) clean
+21
View File
@@ -0,0 +1,21 @@
#include <linux/module.h> // my_init
#include <linux/kernel.h> // printk
#include <linux/sched.h> // task_struct
int my_init(void)
{
struct task_struct *p;
for_each_process(p)
printk("pid=%d , comm=%s\n", p->pid, p->comm);
return 0;
}
void my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+8
View File
@@ -0,0 +1,8 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -fPIE
LOCAL_LDFLAGS := -fPIE -pie
LOCAL_MODULE := myapp
LOCAL_SRC_FILES := myapp.c
include $(BUILD_EXECUTABLE)
+9
View File
@@ -0,0 +1,9 @@
#include <fcntl.h>
int main()
{
int fd;
fd = open( "/dev/mydev", O_RDONLY );
close(fd);
return 0;
}
+155
View File
@@ -0,0 +1,155 @@
#if 1
#include <stdio.h>
#ifdef MODULE
#define module_init( fn ) \
int init_module(void) __attribute__((alias(#fn)))
#else
#define module_init( fn ) __initcall(x);
#endif
int my_init(void)
{
printf("init_module()\n");
return 0;
}
module_init( my_init );
int main()
{
init_module();
return 0;
}
#endif
#if 0
#include <stdio.h>
void bar(void)
{
printf("bar()\n");
}
void foo(void) __attribute__((alias("bar")));
int main()
{
foo();
return 0;
}
#endif
#if 0
#include <stdio.h>
#define ___PASTE(a,b) a##b
#define __PASTE(a,b) ___PASTE(a,b)
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
#define __MODULE_LICENSE( license, info ) \
static const char __UNIQUE_ID(license)[] = "license" "=" info
__MODULE_LICENSE(license, "GPL"); __MODULE_LICENSE(license, "BSD");
int main()
{
return 0;
}
#endif
#if 0
#include <stdio.h>
#define __FOO(a,b) a##b
#define FOO( a,b ) __FOO(a,b)
#define __MODULE_LICENSE( license, info ) \
static const char FOO(license,__LINE__)[] = "license" "=" info
__MODULE_LICENSE(license, "GPL"); __MODULE_LICENSE(license, "BSD");
int main()
{
return 0;
}
#endif
#if 0
#include <stdio.h>
#define BAR hello
int main()
{
//printf("%s\n", "license" "=" "GPL" );
printf("%s\n", "license=GPL" );
return 0;
}
#endif
#if 0
#include <stdio.h>
#define __FOO(x) #x
#define FOO(x) __FOO(x)
#define BAR hello
int main()
{
printf("%s\n", FOO(BAR) );
return 0;
}
#endif
#if 0
#include <stdio.h>
#define FOO(x) #x
#define BAR hello
int main()
{
printf("%s\n", FOO(BAR) );
return 0;
}
#endif
#if 0
#include <stdio.h>
//#define __stringify_1(x...) #x
//#define __stringify(x...) __stringify_1(x)
#define __stringify(x...) #x
int main()
{
printf("%s\n", __stringify(license));
return 0;
}
#endif
#if 0
#include <stdio.h>
# define __used __attribute__((__used__))
int main()
{
int __used __attribute__((unused)) glob;
//printf("glob=%d\n", glob );
return 0;
}
#endif
#if 0
static const char a[]
__attribute__((section(".modinfo")))
= "hello";
int main()
{
return 0;
}
#endif
#if 0
static const char a[] = "hello";
int main()
{
return 0;
}
#endif
+44
View File
@@ -0,0 +1,44 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> //misc_register
int my_open (struct inode *inode, struct file *filp)
{
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
};
static struct miscdevice misc =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &fops,
};
int my_init(void)
{
misc_register( &misc );
return 0;
}
void my_exit(void)
{
misc_deregister( &misc );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+15
View File
@@ -0,0 +1,15 @@
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk("init_module()\n");
return 0;
}
void cleanup_module(void)
{
printk("cleanup_module()\n");
}
+16
View File
@@ -0,0 +1,16 @@
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk("init_module()\n");
return 0;
}
void cleanup_module(void)
{
printk("cleanup_module()\n");
}
MODULE_LICENSE("GPL");
+18
View File
@@ -0,0 +1,18 @@
#include <linux/module.h>
#include <linux/kernel.h>
int my_init(void)
{
printk("init_module()\n");
return 0;
}
void my_exit(void)
{
printk("cleanup_module()\n");
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+36
View File
@@ -0,0 +1,36 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
int my_open (struct inode *inode, struct file *filp)
{
printk("my_open()\n");
return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
printk("my_close()\n");
return 0;
}
static struct file_operations fops =
{
.open = my_open,
.release = my_close,
};
int my_init(void)
{
register_chrdev(200, "mydev", &fops );
return 0;
}
void my_exit(void)
{
unregister_chrdev( 200, "mydev" );
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+19
View File
@@ -0,0 +1,19 @@
#include <linux/module.h> // my_init
#include <linux/kernel.h> // printk
#include <linux/sched.h> // task_struct
int my_init(void)
{
struct task_struct *p = current;
printk("pid=%d, comm=%s\n", p->pid, p->comm );
return 0;
}
void my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );
+31
View File
@@ -0,0 +1,31 @@
#include <linux/module.h> // my_init
#include <linux/kernel.h> // printk
#include <linux/sched.h> // task_struct
#include <linux/list.h> // kernel list
int my_init(void)
{
struct task_struct *p;
struct list_head *temp;
for( temp = current->tasks.next;
temp!= &current->tasks;
temp = temp->next )
{
p = list_entry( temp, struct task_struct, tasks );
printk("pid=%d , comm=%s\n", p->pid, p->comm);
}
p = list_entry( temp, struct task_struct, tasks );
printk("pid=%d , comm=%s\n", p->pid, p->comm);
return 0;
}
void my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init( my_init );
module_exit( my_exit );