Escreva seu primeiro
MÓDULO DE
KERNEL
(LINUX)
Em quatro atos
●
Hello World!
●
Character driver
●
Miscellaneous driver
●
Ramdisk
0 – Hello World
●
Olá mundo!
●
Diego Ramos Ruggeri
●
http://vai.la/62u3
0 – Hello World
●
Linux é monolítico
●
Módulos carregados em tempo de
execução
●
Ciclo de vida
●
Básico: Inicializar e Lim...
0 – Hello World
Porque?
●
Hardware livre
●
Entender o Linux
●
OOP em C
●
Projeto de software livre
static int __init hello_module(void)
{
printk(KERN_INFO "Hello World!n");
return 0;
}
static void __exit goodbye_module(vo...
0 – Hello World
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build
M=$(PWD) modules
clean:
make -C /lib/mo...
0 – Hello World
●
modinfo
●
insmod
●
lsmod
●
rmmod
●
dmesg
1 – Char Dev
●
ioctls
●
net
●
/dev
●
/sys
●
/dev
●
Block device
●
Character device
●
Major Minor
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_...
2 – Misc Dev
●
mknod /dev/fisl15 c $MAJOR $MINOR
●
udev
●
Miscellaneous API
static struct file_operations fops = { /*...*/ };
static struct miscdevice mdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = D...
3 - Ramdisk
●
Char device +
●
Misc api +
●
Aloca memória (slab) +
●
open, close, read, write
static int __init hello_module(void)
{
disk = (char*) kzalloc(DISK_SIZE *
sizeof(char), GFP_KERNEL);
/*…*/
}
static void _...
static ssize_t device_read(struct file *filp, char
*buffer, size_t length,loff_t * offset)
{
/*...*/
copy_to_user(buffer, ...
Obrigado
diego@ruggeri.net.br
http://github.com/diegor2
Upcoming SlideShare
Loading in …5
×

Primeiro módulo de kernel

376 views

Published on

Escrevendo seu primeiro módulo de kernel (Linux)

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
376
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Primeiro módulo de kernel

  1. 1. Escreva seu primeiro MÓDULO DE KERNEL (LINUX)
  2. 2. Em quatro atos ● Hello World! ● Character driver ● Miscellaneous driver ● Ramdisk
  3. 3. 0 – Hello World ● Olá mundo! ● Diego Ramos Ruggeri ● http://vai.la/62u3
  4. 4. 0 – Hello World ● Linux é monolítico ● Módulos carregados em tempo de execução ● Ciclo de vida ● Básico: Inicializar e Limpar
  5. 5. 0 – Hello World Porque? ● Hardware livre ● Entender o Linux ● OOP em C ● Projeto de software livre
  6. 6. static int __init hello_module(void) { printk(KERN_INFO "Hello World!n"); return 0; } static void __exit goodbye_module(void) { printk(KERN_INFO "Adiosn"); } module_init(hello_module); module_exit(goodbye_module); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Diego Ruggeri"); MODULE_DESCRIPTION("Hello World module");
  7. 7. 0 – Hello World obj-m += hello.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  8. 8. 0 – Hello World ● modinfo ● insmod ● lsmod ● rmmod ● dmesg
  9. 9. 1 – Char Dev ● ioctls ● net ● /dev ● /sys ● /dev ● Block device ● Character device ● Major Minor
  10. 10. static struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; static int __init hello_module(void) { major = register_chrdev(0, DEVICE, &fops); /* … */ } static void __exit goodbye_module(void) { unregister_chrdev(major, DEVICE); }
  11. 11. 2 – Misc Dev ● mknod /dev/fisl15 c $MAJOR $MINOR ● udev ● Miscellaneous API
  12. 12. static struct file_operations fops = { /*...*/ }; static struct miscdevice mdev = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE, .fops = &fops, }; static int __init hello_module(void) { misc_register(&mdev) /* … */ } static void __exit goodbye_module(void) { misc_deregister(&mdev); }
  13. 13. 3 - Ramdisk ● Char device + ● Misc api + ● Aloca memória (slab) + ● open, close, read, write
  14. 14. static int __init hello_module(void) { disk = (char*) kzalloc(DISK_SIZE * sizeof(char), GFP_KERNEL); /*…*/ } static void __exit goodbye_module(void) { kfree(disk); /*...*/ }
  15. 15. static ssize_t device_read(struct file *filp, char *buffer, size_t length,loff_t * offset) { /*...*/ copy_to_user(buffer, message, len); /*...*/ } static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off) { copy_from_user(disk, buff, len) }
  16. 16. Obrigado diego@ruggeri.net.br http://github.com/diegor2

×