summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile4
-rw-r--r--cr0.c22
-rw-r--r--handlers/Makefile1
-rw-r--r--handlers/chardev.c33
-rw-r--r--hello/Makefile1
-rw-r--r--hello/hello-chardev.c23
-rw-r--r--lists.c41
8 files changed, 122 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ec70db6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+*.o*
+*.ko*
+*.mod.*
+Module.symvers
+modules.order
+.tmp_versions
diff --git a/Makefile b/Makefile
index 332678d..f314971 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,6 @@
-obj-m += syscall.o
+#obj-m += syscall.o
+#obj-m += cr0.o
+obj-m += lists.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
diff --git a/cr0.c b/cr0.c
new file mode 100644
index 0000000..9265d61
--- /dev/null
+++ b/cr0.c
@@ -0,0 +1,22 @@
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <asm/system.h>
+
+int init_module(void)
+{
+ int cr0 = native_read_cr0();
+ printk(KERN_INFO "cr0 before: %x\n", cr0);
+ cr0 = cr0 >> 1;
+ cr0 = cr0 << 1;
+ native_write_cr0(cr0);
+ cr0 = native_read_cr0();
+ printk(KERN_INFO "cr0 after: %x\n", cr0);
+ return 0;
+}
+
+void cleanup_module(void)
+{
+ printk(KERN_INFO "cr0 unloaded\n");
+}
+
+MODULE_LICENSE("GPL");
diff --git a/handlers/Makefile b/handlers/Makefile
index 10043eb..c9a417c 100644
--- a/handlers/Makefile
+++ b/handlers/Makefile
@@ -1,5 +1,6 @@
obj-m += chardev.o
obj-m += procfs1.o
+export-objs += chardev.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
diff --git a/handlers/chardev.c b/handlers/chardev.c
index 3594bce..1c1f923 100644
--- a/handlers/chardev.c
+++ b/handlers/chardev.c
@@ -1,6 +1,7 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
+#include <linux/sched.h>
#include <asm/uaccess.h>
#include "chardev.h"
@@ -11,9 +12,15 @@
static int Major;
static int opened = 0;
-static char msg[BUF_LEN];
+static char chardev_buffer[BUF_LEN];
static char* msg_ptr;
+void dumb(void)
+{
+ printk(KERN_INFO "DUMB EXECUTED\n");
+}
+EXPORT_SYMBOL(dumb);
+
static struct file_operations fops = {
.read = device_read,
.write = device_write,
@@ -23,6 +30,7 @@ static struct file_operations fops = {
int init_module(void)
{
+
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
@@ -53,9 +61,9 @@ static int device_open(struct inode* inode, struct file* filp)
opened++;
if (counter++ == 0)
- sprintf(msg, "Come on, write something here :D\n");
+ sprintf(chardev_buffer, "Come on, write something here :D\n");
- msg_ptr = msg;
+ msg_ptr = chardev_buffer;
try_module_get(THIS_MODULE);
return SUCCESS;
@@ -70,18 +78,27 @@ static int device_release(struct inode* inode, struct file* filp)
return 0;
}
-static ssize_t device_read(struct file* filp, char* buffer, size_t len, loff_t* off)
+static ssize_t device_read(struct file* filp, char* buff, size_t len, loff_t* off)
{
int bytes = 0;
+ char strpid[14];
+ snprintf(strpid, 14, "PID: %d", (current->pid));
if (*msg_ptr == 0)
return 0;
while (len-- && *msg_ptr) {
- put_user(*msg_ptr++, buffer++);
+ put_user(*msg_ptr++, buff++);
bytes++;
}
+ copy_to_user(buff, strpid, 14);
+ bytes += 14;
+ buff += 14;
+
+ put_user('\n', buff++);
+ bytes++;
+
return bytes;
}
@@ -95,11 +112,11 @@ static ssize_t device_write(struct file* filp, const char* buff, size_t len, lof
}
while (len--) {
- get_user(msg[bytes], buff++);
+ get_user(chardev_buffer[bytes], buff++);
bytes++;
}
- msg[bytes] = '\0';
+ chardev_buffer[bytes] = '\0';
- msg_ptr = msg;
+ msg_ptr = chardev_buffer;
return bytes;
}
diff --git a/hello/Makefile b/hello/Makefile
index d63aa3d..f6299e0 100644
--- a/hello/Makefile
+++ b/hello/Makefile
@@ -2,6 +2,7 @@ obj-m += hello-1.o
obj-m += hello-2.o
obj-m += hello-3.o
obj-m += hello-4.o
+obj-m += hello-chardev.o
obj-m += startstop.o
startstop-objs := start.o stop.o
diff --git a/hello/hello-chardev.c b/hello/hello-chardev.c
new file mode 100644
index 0000000..a8a80d8
--- /dev/null
+++ b/hello/hello-chardev.c
@@ -0,0 +1,23 @@
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+
+extern void dumb(void);
+
+static int __init hello_2_init(void)
+{
+// printk(KERN_INFO "Buffer: %s (%p)\n", chardev_buffer, chardev_buffer);
+ dumb();
+ return 0;
+}
+
+static void __exit hello_2_exit(void)
+{
+}
+
+module_init(hello_2_init);
+module_exit(hello_2_exit);
+
+MODULE_AUTHOR("Guillermo Ramos");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Módulo tonto");
diff --git a/lists.c b/lists.c
new file mode 100644
index 0000000..2eee70c
--- /dev/null
+++ b/lists.c
@@ -0,0 +1,41 @@
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#define LENGTH 5
+
+struct node {
+ struct list_head l;
+ int dato;
+};
+
+int init_module(void)
+{
+ int i;
+ struct node head, aux[LENGTH], *aux2;
+ struct list_head* iter;
+
+ INIT_LIST_HEAD(&head.l);
+ head.dato = 666;
+
+ for (i = 0; i < LENGTH; i++) {
+ aux[i].dato = i;
+
+ list_add_tail(&aux[i].l, &head.l);
+ }
+
+ list_for_each(iter, &head.l) {
+ aux2 = list_entry(iter, struct node, l);
+ printk(KERN_INFO "%d", aux2->dato);
+ }
+
+ printk(KERN_INFO "Por cierto, en 0x0 hay: %d", ((struct node *)0)->dato);
+
+ return 0;
+}
+
+void cleanup_module(void)
+{
+}
+
+MODULE_AUTHOR("Guillermo Ramos");
+MODULE_LICENSE("GPL");