ݺߣ

ݺߣShare a Scribd company logo
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 Limpar
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(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");
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
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_release
};
static int __init hello_module(void)
{
major = register_chrdev(0, DEVICE, &fops);
/* … */
}
static void __exit goodbye_module(void)
{
unregister_chrdev(major, 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 = DEVICE,
.fops = &fops,
};
static int __init hello_module(void)
{
misc_register(&mdev)
/* … */
}
static void __exit goodbye_module(void)
{
misc_deregister(&mdev);
}
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 __exit goodbye_module(void)
{
kfree(disk);
/*...*/
}
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)
}
Obrigado
diego@ruggeri.net.br
http://github.com/diegor2

More Related Content

Primeiro módulo de kernel

  • 1. Escreva seu primeiro MÓDULO DE KERNEL (LINUX)
  • 2. Em quatro atos ● Hello World! ● Character driver ● Miscellaneous driver ● Ramdisk
  • 3. 0 – Hello World ● Olá mundo! ● Diego Ramos Ruggeri ● http://vai.la/62u3
  • 4. 0 – Hello World ● Linux é monolítico ● Módulos carregados em tempo de execução ● Ciclo de vida ● Básico: Inicializar e Limpar
  • 5. 0 – Hello World Porque? ● Hardware livre ● Entender o Linux ● OOP em C ● Projeto de software livre
  • 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. 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. 0 – Hello World ● modinfo ● insmod ● lsmod ● rmmod ● dmesg
  • 9. 1 – Char Dev ● ioctls ● net ● /dev ● /sys ● /dev ● Block device ● Character device ● Major Minor
  • 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. 2 – Misc Dev ● mknod /dev/fisl15 c $MAJOR $MINOR ● udev ● Miscellaneous API
  • 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. 3 - Ramdisk ● Char device + ● Misc api + ● Aloca memória (slab) + ● open, close, read, write
  • 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. 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) }