SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
Um estudo nos drivers
                de dispositivo do Linux



                             Tiago Maluta
                           maluta@unifei.edu.br

Monday, June 28, 2010
Evolução

         1991                                  i386


                          avr32, frv, m32r, microblaze, parisc, score, um,
                        alpha, blackfin, h8300, m68k, mips, powerpcsh, x86,
         2010
                        arm, cris, ia64, m68knommu, mn10300, s390, sparc,
                                              xtensa
                                                                 Fonte: www.kernel.org




Monday, June 28, 2010
Como gerenciar este
                           crescimento?



Monday, June 28, 2010
Monday, June 28, 2010
Controle de versão descentralizado


       •      Linus tem seu repositório:
              git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git       Linus
       •      Recebe um e-mail do mantenedor do
              um subsistema
              “Linus, please pull from: ssh://master.kernel.org/pub/scm/linux/


                                                                                     A    B      C
              kernel/git/mchehab/linux-2.6.git v4l_for_2.6.35”




       •      Ele [Linus] faz o merge no seu
              repositório no momento que achar
              adequado.




Monday, June 28, 2010
Módulos no kernel


                         Módulos de sistemas
                            vfat - suporte ao filesystem FAT32
                            usbnet - subsistema de rede com USB
                         Módulos de dispositivo
                            asix - controlador do conversor USB/Ethernet
                            nvidia - placa de vídeo



                        Podem existir módulos proprietários, mas algumas interfaces são
                        exclusivas para licenças livres

Monday, June 28, 2010
#include <stdio.h>

                        static int start(void) {
                            printf(“Hello from userspace!”);
                            return 0;
                        }

                        int main(void) {
                             return start();
                        }




Monday, June 28, 2010
#include <stdio.h>

                        static int start(void) {
                            printf(“Hello from userspace!”);
                            return 0;
                        }

                        int main(void) {
                             return start();
                        }




Monday, June 28, 2010
#include <stdio.h>

                        static int start(void) {
                            printf(“Hello from userspace!”);
                            return 0;
                        }     gcc -Wall main.c -o main
                        int main(void) {
                             return start();
                        }




Monday, June 28, 2010
#include <linux/kernel .h>
                        #include <linux/init .h>
                        #include <linux/module.h>

                        static int __init start(void) {

                            printk(“Hello from kernelspace!”);

                            return 0;
                        }

                        static	void	 __exit	 stop(void) { }

                        module_init(start);
                        module_exit(stop);

                        MODULE_AUTHOR(“”);
                        MODULE_DESCRIPTION(“”);
                        MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
#include <linux/kernel .h>
                        #include <linux/init .h>
                        #include <linux/module.h>

                        static int __init start(void) {

                            printk(“Hello from kernelspace!”);

                            return 0;
                        }

                        static	void	 __exit	 stop(void) { }

                        module_init(start);
                        module_exit(stop);

                        MODULE_AUTHOR(“”);
                        MODULE_DESCRIPTION(“”);
                        MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
#include <linux/kernel .h>
                        #include <linux/init .h>
                        #include <linux/module.h>

                        static int __init start(void) {

                            printk(“Hello from kernelspace!”);

                            return 0;
                        }

                        static	void	 __exit	 stop(void) { }

                        module_init(start);
                        module_exit(stop);

                        MODULE_AUTHOR(“”);
                        MODULE_DESCRIPTION(“”);
                        MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
#include <linux/kernel .h>
                        #include <linux/init .h>
                        #include <linux/module.h>

                        static int __init start(void) {

                            printk(“Hello from kernelspace!”);

                            return 0;
                        }

                        static	void	 __exit	 stop(void) { }

                        module_init(start);
                        module_exit(stop);

                        MODULE_AUTHOR(“”);
                        MODULE_DESCRIPTION(“”);
                        MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
#include <linux/kernel .h>
                        #include <linux/init .h>
                        #include <linux/module.h>

                        static int __init start(void) {

                            printk(“Hello from kernelspace!”);

                            return 0;
                        }

                        static	void	 __exit	 stop(void) { }

                        module_init(start);
                        module_exit(stop);

                        MODULE_AUTHOR(“”);
                        MODULE_DESCRIPTION(“”);
                        MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
#include <linux/kernel .h>
                             #include <linux/init .h>
                             #include <linux/module.h>

                             static int __init start(void) {

                                 printk(“Hello from kernelspace!”);
                        obj−m:= hello.o
                        KDIR := /lib/modules/$(shell uname −r)/build
                                  return 0;
                        PWD	}:= $(shell pwd)
                        default :
                            $(MAKE) −C __exit	 stop(void) { }
                             static	void	 $(KDIR) M=$(PWD) modules
                        clean :
                             module_init(start);
                            $(MAKE) −C $(KDIR) M=$(PWD) clean
                             module_exit(stop);

                             MODULE_AUTHOR(“”);
                             MODULE_DESCRIPTION(“”);
                             MODULE_LICENSE(“GPL”) ;

Monday, June 28, 2010
Liga


                                  BIOS/EFI


                        Bootloader (GRUB/Lilo/U-boot)


                              Kernel (modo real)


                           Kernel (modo protegido)


                                 Processo init


                            Processos dos usuários




Monday, June 28, 2010
Liga


                                  BIOS/EFI


                        Bootloader (GRUB/Lilo/U-boot)


                              Kernel (modo real)


                           Kernel (modo protegido)


                                 Processo init


                            Processos dos usuários




Monday, June 28, 2010
Liga


                                  BIOS/EFI


                        Bootloader (GRUB/Lilo/U-boot)


                              Kernel (modo real)


                           Kernel (modo protegido)


                                 Processo init


                            Processos dos usuários




Monday, June 28, 2010
Sincronismo

                        • Spinlocks

                        • Mutexes

                        • Operações atômicas




Monday, June 28, 2010
Sincronismo

                        • Spinlocks

                        • Mutexes

                        • Operações atômicas




Monday, June 28, 2010
Sincronismo
                        #include <linux/spinlock .h>

                        spinlock t mylock = SPIN_LOCK_ULOCKED;

                        spin_lock(&mylock );
                            •   Spinlocks
                        /∗ região crítica ∗/
                        spin_unlock(&mylock );


                            • Mutexes

                            • Operações atômicas




Monday, June 28, 2010
Sincronismo
                        #include <linux/spinlock .h>

                        spinlock t mylock = SPIN_LOCK_ULOCKED;

                        spin_lock(&mylock );
                            •   Spinlocks
                        /∗ região crítica ∗/
                        spin_unlock(&mylock );


                            • Mutexes

                            • Operações atômicas




Monday, June 28, 2010
Sincronismo
                         #include <linux/spinlock .h>

                         spinlock t mylock = SPIN_LOCK_ULOCKED;

                         spin_lock(&mylock );
                             •   Spinlocks
                         /∗ região crítica ∗/
                         spin_unlock(&mylock );


                             • Mutexes

                             • Operações atômicas
                        #include <linux/mutex .h>

                        static DEFINE_MUTEX(mymutex);

                        mutex_lock(&mymutex);
                        /∗ região crítica ∗/
                        mutex_unlock(&mymutex);




Monday, June 28, 2010
Facilitadores


                • Udev - para gerenciamento de nós /dev
                • Sysfs - exportar informações para os processos do usuário.
                • Kobjects e Krefs - contador de referências




Monday, June 28, 2010
Udev

                        /etc/udev/rules.d/70-persistent-cd.rules

         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrom", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrw", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvd", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvdrw", ENV{GENERATED}="1"




Monday, June 28, 2010
Udev

                        /etc/udev/rules.d/70-persistent-cd.rules

         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrom", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrw", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvd", ENV{GENERATED}="1"
         ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvdrw", ENV{GENERATED}="1"




Monday, June 28, 2010
sysfs

                           kernel-space         user-space

                             Objetos              Diretórios

                            Atributos             Arquivos

                            Relações         Links simbólicos




               /sys/class/mmc_host/mmc0/device/leds/mmc0::

                         $ echo “1” > brightness
                         $ cat brightness

Monday, June 28, 2010
kobject krefs

               Contador de referências, para o kernel gerenciar o número de
               objetos associados em uma mesma estrutura.


                        struct kobject {                         struct kref {
                            const char *name;                        atomic_t refcount;
                            struct list_head entry;              }
                            struct kobject *parent;
                            struct kset *kset;
                            struct kobj_type *ktype;
                            struct sysfs_dirent *sd;
                            struct kref kref;
                        }




Monday, June 28, 2010
Modelo de Driver do Linux

                O modelo propôs uma representação genérica de objetos:




                        • Devices
                        • Devices drivers
                        • Bus drivers
                        • Devices Classes
                        • Devices Interfaces




Monday, June 28, 2010
Modelo de Driver do Linux

                O modelo propôs uma representação genérica de objetos:




                        • Devices
                        • Devices drivers
                        • Bus drivers
                        • Devices Classes
                        • Devices Interfaces


                  Funções para operar e controlar cada tipo de objeto.




Monday, June 28, 2010
Modelo de Driver do Linux

                O modelo propôs uma representação genérica de objetos:




                        • Devices
                        • Devices drivers
                        • Bus drivers
                        • Devices Classes
                        • Devices Interfaces


                  Funções para operar e controlar cada tipo de objeto.




Monday, June 28, 2010
Modelo de Driver do Linux

                O modelo propôs uma representação genérica de objetos:




                        • Devices                     Um device driver implementa uma ou
                                                      mais interfaces (devices interfaces) de
                        • Devices drivers
                                                      uma    classe   de   dispositivos   (devices
                        • Bus drivers                 classes) para um grupo de dipositivos
                        • Devices Classes             (devices) em um barramento (bus driver)
                                                      específico.
                        • Devices Interfaces


                  Funções para operar e controlar cada tipo de objeto.




Monday, June 28, 2010
USB/Ethernet


                   Ethernet                  USB


                               Controlador




                                 asix.ko


Monday, June 28, 2010
USB/Ethernet

                Preenchimento de estruturas:




Monday, June 28, 2010
USB/Ethernet

                Preenchimento de estruturas:




Monday, June 28, 2010
USB/Ethernet
                        static struct usb_driver asix_driver = {
                            .name =      "asix",
                            .id_table = products,
                            .probe =     usbnet_probe,
                Preenchimento de estruturas:
                            .suspend =    usbnet_suspend,
                            .resume =     usbnet_resume,
                            .disconnect = usbnet_disconnect,
                            .supports_autosuspend = 1,
                        };




Monday, June 28, 2010
USB/Ethernet
                        static struct usb_driver asix_driver = {
                            .name =      "asix",
                            .id_table = products,
                            .probe =     usbnet_probe,
                Preenchimento de estruturas:
                            .suspend =    usbnet_suspend,
                            .resume =     usbnet_resume,
                            .disconnect = usbnet_disconnect,
                            .supports_autosuspend = 1,
                        };




Monday, June 28, 2010
USB/Ethernet
                        static struct usb_driver asix_driver = {
                            .name =      "asix",
                            .id_table = products,
                            .probe =     usbnet_probe,
                Preenchimento de estruturas:
                            .suspend =    usbnet_suspend,
                            .resume =     usbnet_resume,
                            .disconnect = usbnet_disconnect,
                            .supports_autosuspend = 1,
                        };



                        static const struct net_device_ops ax88172_netdev_ops = {
                            .ndo_open           = usbnet_open,
                            .ndo_stop          = usbnet_stop,
                            .ndo_start_xmit        = usbnet_start_xmit,
                            .ndo_tx_timeout         = usbnet_tx_timeout,
                            .ndo_change_mtu          = usbnet_change_mtu,
                            .ndo_set_mac_address = eth_mac_addr,
                            .ndo_validate_addr       = eth_validate_addr,
                            .ndo_do_ioctl        = asix_ioctl,
                            .ndo_set_multicast_list = ax88172_set_multicast,
                        };



Monday, June 28, 2010
USB/Ethernet

                Operações:




Monday, June 28, 2010
USB/Ethernet

                Operações:




Monday, June 28, 2010
USB/Ethernet
          asix_read_cmd             asix_read_medium_status   asix_ioctl
          asix_write_cmd            asix_write_medium_mode    asix_set_mac_address
                Operações:
          asix_async_cmd_callback   asix_write_gpio           ax88172_set_multicast
          asix_write_cmd_async      asix_set_multicast        ax88172_link_reset
          asix_rx_fixup              asix_mdio_read            ax88172_bind
          asix_tx_fixup              asix_mdio_write           ax88772_link_reset

          asix_status               asix_get_phyid            ax88772_bind
          asix_set_sw_mii           asix_get_wol              marvell_phy_init
          asix_set_hw_mii           asix_set_wol              marvell_led_status
          asix_get_phy_addr         asix_get_eeprom_len       ax88178_link_reset

          asix_sw_reset             asix_get_eeprom           ax88178_set_mfb
          asix_read_rx_ctl          asix_get_drvinfo          ax88178_change_mtu
          asix_write_rx_ctl         asix_get_link             ax88178_bind



Monday, June 28, 2010
Conclusão



Monday, June 28, 2010
Crescimento com
      pouca manutenção
      do código.


Monday, June 28, 2010
Padronização das
      estruturas básicas.



Monday, June 28, 2010
Sistemas
      Embarcados



Monday, June 28, 2010
LKML et all;
      Linux Kernel Mail
      List


Monday, June 28, 2010
--tm
Monday, June 28, 2010

Mais conteúdo relacionado

Semelhante a td

Linux Kernel Startup Code In Embedded Linux
Linux    Kernel    Startup  Code In  Embedded  LinuxLinux    Kernel    Startup  Code In  Embedded  Linux
Linux Kernel Startup Code In Embedded Linux
Emanuele Bonanni
 
Gregor modules
Gregor modulesGregor modules
Gregor modules
skyshaw
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
Yuren Ju
 

Semelhante a td (20)

Hardware Open Source
Hardware Open SourceHardware Open Source
Hardware Open Source
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
 
lesson03.ppt
lesson03.pptlesson03.ppt
lesson03.ppt
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
Grand Central Dispatch in Objective-C
Grand Central Dispatch in Objective-CGrand Central Dispatch in Objective-C
Grand Central Dispatch in Objective-C
 
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Linux Kernel Startup Code In Embedded Linux
Linux    Kernel    Startup  Code In  Embedded  LinuxLinux    Kernel    Startup  Code In  Embedded  Linux
Linux Kernel Startup Code In Embedded Linux
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architecture
 
Gregor modules
Gregor modulesGregor modules
Gregor modules
 
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
 
Sysprog 14
Sysprog 14Sysprog 14
Sysprog 14
 
Oop 1
Oop 1Oop 1
Oop 1
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++
 

Último

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

td

  • 1. Um estudo nos drivers de dispositivo do Linux Tiago Maluta maluta@unifei.edu.br Monday, June 28, 2010
  • 2. Evolução 1991 i386 avr32, frv, m32r, microblaze, parisc, score, um, alpha, blackfin, h8300, m68k, mips, powerpcsh, x86, 2010 arm, cris, ia64, m68knommu, mn10300, s390, sparc, xtensa Fonte: www.kernel.org Monday, June 28, 2010
  • 3. Como gerenciar este crescimento? Monday, June 28, 2010
  • 5. Controle de versão descentralizado • Linus tem seu repositório: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git Linus • Recebe um e-mail do mantenedor do um subsistema “Linus, please pull from: ssh://master.kernel.org/pub/scm/linux/ A B C kernel/git/mchehab/linux-2.6.git v4l_for_2.6.35” • Ele [Linus] faz o merge no seu repositório no momento que achar adequado. Monday, June 28, 2010
  • 6. Módulos no kernel Módulos de sistemas vfat - suporte ao filesystem FAT32 usbnet - subsistema de rede com USB Módulos de dispositivo asix - controlador do conversor USB/Ethernet nvidia - placa de vídeo Podem existir módulos proprietários, mas algumas interfaces são exclusivas para licenças livres Monday, June 28, 2010
  • 7. #include <stdio.h> static int start(void) { printf(“Hello from userspace!”); return 0; } int main(void) { return start(); } Monday, June 28, 2010
  • 8. #include <stdio.h> static int start(void) { printf(“Hello from userspace!”); return 0; } int main(void) { return start(); } Monday, June 28, 2010
  • 9. #include <stdio.h> static int start(void) { printf(“Hello from userspace!”); return 0; } gcc -Wall main.c -o main int main(void) { return start(); } Monday, June 28, 2010
  • 10. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); return 0; } static void __exit stop(void) { } module_init(start); module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 11. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); return 0; } static void __exit stop(void) { } module_init(start); module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 12. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); return 0; } static void __exit stop(void) { } module_init(start); module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 13. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); return 0; } static void __exit stop(void) { } module_init(start); module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 14. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); return 0; } static void __exit stop(void) { } module_init(start); module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 15. #include <linux/kernel .h> #include <linux/init .h> #include <linux/module.h> static int __init start(void) { printk(“Hello from kernelspace!”); obj−m:= hello.o KDIR := /lib/modules/$(shell uname −r)/build return 0; PWD }:= $(shell pwd) default : $(MAKE) −C __exit stop(void) { } static void $(KDIR) M=$(PWD) modules clean : module_init(start); $(MAKE) −C $(KDIR) M=$(PWD) clean module_exit(stop); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“”); MODULE_LICENSE(“GPL”) ; Monday, June 28, 2010
  • 16. Liga BIOS/EFI Bootloader (GRUB/Lilo/U-boot) Kernel (modo real) Kernel (modo protegido) Processo init Processos dos usuários Monday, June 28, 2010
  • 17. Liga BIOS/EFI Bootloader (GRUB/Lilo/U-boot) Kernel (modo real) Kernel (modo protegido) Processo init Processos dos usuários Monday, June 28, 2010
  • 18. Liga BIOS/EFI Bootloader (GRUB/Lilo/U-boot) Kernel (modo real) Kernel (modo protegido) Processo init Processos dos usuários Monday, June 28, 2010
  • 19. Sincronismo • Spinlocks • Mutexes • Operações atômicas Monday, June 28, 2010
  • 20. Sincronismo • Spinlocks • Mutexes • Operações atômicas Monday, June 28, 2010
  • 21. Sincronismo #include <linux/spinlock .h> spinlock t mylock = SPIN_LOCK_ULOCKED; spin_lock(&mylock ); • Spinlocks /∗ região crítica ∗/ spin_unlock(&mylock ); • Mutexes • Operações atômicas Monday, June 28, 2010
  • 22. Sincronismo #include <linux/spinlock .h> spinlock t mylock = SPIN_LOCK_ULOCKED; spin_lock(&mylock ); • Spinlocks /∗ região crítica ∗/ spin_unlock(&mylock ); • Mutexes • Operações atômicas Monday, June 28, 2010
  • 23. Sincronismo #include <linux/spinlock .h> spinlock t mylock = SPIN_LOCK_ULOCKED; spin_lock(&mylock ); • Spinlocks /∗ região crítica ∗/ spin_unlock(&mylock ); • Mutexes • Operações atômicas #include <linux/mutex .h> static DEFINE_MUTEX(mymutex); mutex_lock(&mymutex); /∗ região crítica ∗/ mutex_unlock(&mymutex); Monday, June 28, 2010
  • 24. Facilitadores • Udev - para gerenciamento de nós /dev • Sysfs - exportar informações para os processos do usuário. • Kobjects e Krefs - contador de referências Monday, June 28, 2010
  • 25. Udev /etc/udev/rules.d/70-persistent-cd.rules ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrom", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrw", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvd", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvdrw", ENV{GENERATED}="1" Monday, June 28, 2010
  • 26. Udev /etc/udev/rules.d/70-persistent-cd.rules ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrom", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="cdrw", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvd", ENV{GENERATED}="1" ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="ide-1:0", SYMLINK+="dvdrw", ENV{GENERATED}="1" Monday, June 28, 2010
  • 27. sysfs kernel-space user-space Objetos Diretórios Atributos Arquivos Relações Links simbólicos /sys/class/mmc_host/mmc0/device/leds/mmc0:: $ echo “1” > brightness $ cat brightness Monday, June 28, 2010
  • 28. kobject krefs Contador de referências, para o kernel gerenciar o número de objetos associados em uma mesma estrutura. struct kobject { struct kref { const char *name; atomic_t refcount; struct list_head entry; } struct kobject *parent; struct kset *kset; struct kobj_type *ktype; struct sysfs_dirent *sd; struct kref kref; } Monday, June 28, 2010
  • 29. Modelo de Driver do Linux O modelo propôs uma representação genérica de objetos: • Devices • Devices drivers • Bus drivers • Devices Classes • Devices Interfaces Monday, June 28, 2010
  • 30. Modelo de Driver do Linux O modelo propôs uma representação genérica de objetos: • Devices • Devices drivers • Bus drivers • Devices Classes • Devices Interfaces Funções para operar e controlar cada tipo de objeto. Monday, June 28, 2010
  • 31. Modelo de Driver do Linux O modelo propôs uma representação genérica de objetos: • Devices • Devices drivers • Bus drivers • Devices Classes • Devices Interfaces Funções para operar e controlar cada tipo de objeto. Monday, June 28, 2010
  • 32. Modelo de Driver do Linux O modelo propôs uma representação genérica de objetos: • Devices Um device driver implementa uma ou mais interfaces (devices interfaces) de • Devices drivers uma classe de dispositivos (devices • Bus drivers classes) para um grupo de dipositivos • Devices Classes (devices) em um barramento (bus driver) específico. • Devices Interfaces Funções para operar e controlar cada tipo de objeto. Monday, June 28, 2010
  • 33. USB/Ethernet Ethernet USB Controlador asix.ko Monday, June 28, 2010
  • 34. USB/Ethernet Preenchimento de estruturas: Monday, June 28, 2010
  • 35. USB/Ethernet Preenchimento de estruturas: Monday, June 28, 2010
  • 36. USB/Ethernet static struct usb_driver asix_driver = { .name = "asix", .id_table = products, .probe = usbnet_probe, Preenchimento de estruturas: .suspend = usbnet_suspend, .resume = usbnet_resume, .disconnect = usbnet_disconnect, .supports_autosuspend = 1, }; Monday, June 28, 2010
  • 37. USB/Ethernet static struct usb_driver asix_driver = { .name = "asix", .id_table = products, .probe = usbnet_probe, Preenchimento de estruturas: .suspend = usbnet_suspend, .resume = usbnet_resume, .disconnect = usbnet_disconnect, .supports_autosuspend = 1, }; Monday, June 28, 2010
  • 38. USB/Ethernet static struct usb_driver asix_driver = { .name = "asix", .id_table = products, .probe = usbnet_probe, Preenchimento de estruturas: .suspend = usbnet_suspend, .resume = usbnet_resume, .disconnect = usbnet_disconnect, .supports_autosuspend = 1, }; static const struct net_device_ops ax88172_netdev_ops = { .ndo_open = usbnet_open, .ndo_stop = usbnet_stop, .ndo_start_xmit = usbnet_start_xmit, .ndo_tx_timeout = usbnet_tx_timeout, .ndo_change_mtu = usbnet_change_mtu, .ndo_set_mac_address = eth_mac_addr, .ndo_validate_addr = eth_validate_addr, .ndo_do_ioctl = asix_ioctl, .ndo_set_multicast_list = ax88172_set_multicast, }; Monday, June 28, 2010
  • 39. USB/Ethernet Operações: Monday, June 28, 2010
  • 40. USB/Ethernet Operações: Monday, June 28, 2010
  • 41. USB/Ethernet asix_read_cmd asix_read_medium_status asix_ioctl asix_write_cmd asix_write_medium_mode asix_set_mac_address Operações: asix_async_cmd_callback asix_write_gpio ax88172_set_multicast asix_write_cmd_async asix_set_multicast ax88172_link_reset asix_rx_fixup asix_mdio_read ax88172_bind asix_tx_fixup asix_mdio_write ax88772_link_reset asix_status asix_get_phyid ax88772_bind asix_set_sw_mii asix_get_wol marvell_phy_init asix_set_hw_mii asix_set_wol marvell_led_status asix_get_phy_addr asix_get_eeprom_len ax88178_link_reset asix_sw_reset asix_get_eeprom ax88178_set_mfb asix_read_rx_ctl asix_get_drvinfo ax88178_change_mtu asix_write_rx_ctl asix_get_link ax88178_bind Monday, June 28, 2010
  • 43. Crescimento com pouca manutenção do código. Monday, June 28, 2010
  • 44. Padronização das estruturas básicas. Monday, June 28, 2010
  • 45. Sistemas Embarcados Monday, June 28, 2010
  • 46. LKML et all; Linux Kernel Mail List Monday, June 28, 2010