Device drivers Pipes Drivers no Linux
MC504/MC514 - Sistemas OperacionaisEntrada e Saıda
Pipes
Islene Calciolari Garcia
Instituto de Computacao - Unicamp
Segundo Semestre de 2014
Device drivers Pipes Drivers no Linux
Sumario
1 Device drivers
2 Pipes
3 Drivers no Linux
Device drivers Pipes Drivers no Linux
Device drivers
“I know about car and bus drivers, pilots, and evenscrewdrivers; but what is this device driver?” queriedShweta, puzzled.
A driver drives, manages, controls, directs and monitorsthe entity under its command. What a bus driver doeswith a bus, a device driver does with a computer device(any piece of hardware connected to a computer) like amouse, keyboard, monitor, hard disk, Web-camera, clock,and more.
Ref:Device Drivers, Part 1: Linux Device Drivers for Your Girl Friend
Device drivers Pipes Drivers no Linux
Dispositivos de I/O e controladores
Tanenbaum: Figura 1.5
O sistema operacional deve interagir com os controladores
Device drivers Pipes Drivers no Linux
Character device
Tanenbaum: Figura 5.34
Acesso sequencial, caractere a caractereExecute ls -l /dev
Device drivers Pipes Drivers no Linux
Block device
Tanenbaum: Figura 1.8
Acesso nao sequencial a blocos de informacaoExecute ls -l /dev
Device drivers Pipes Drivers no Linux
Device drivers
Tanenbaum: Figura 5.11
Device drivers Pipes Drivers no Linux
Device driversLinux device driver partition
http://www.opensourceforu.com/2010/11/
understanding-linux-device-drivers/
Device drivers Pipes Drivers no Linux
Device drivers
Software que “conversa” com o controlador
Os fabricantes devem fornecer dados detalhados para a escritados device drivers
Como acoplar um device driver ao kernel:
relink e rebootentrada em um arquivo e rebooton-the-flyveja o comando lsmod
Device drivers Pipes Drivers no Linux
Dynamic loading drivers
He explained, “As we know, a typical driver installationon Windows needs a reboot for it to get activated. Thatis really not acceptable; suppose we need to do it on aserver? That’s where Linux wins. In Linux, we can loador unload a driver on the fly, and it is active for useinstantly after loading. Also, it is instantly disabled whenunloaded. This is called dynamic loading and unloadingof drivers in Linux.”
Ref:Device Drivers, Part 2: Writing Your First Linux Driver in the
Classroom
Device drivers Pipes Drivers no Linux
Dynamic loading drivers
lsmod: lists currently loaded modules
insmod <module file>: inserts/loads the specified modulefile
modprobe <module>: inserts/loads the module, along withany dependencies
rmmod <module>: removes/unloads the module
Ref:Device Drivers, Part 2: Writing Your First Linux Driver in the
Classroom
Device drivers Pipes Drivers no Linux
Device driversSem ou com uma interface padrao
Tanenbaum: Figura 5.13
Device drivers Pipes Drivers no Linux
Device driversLinux kernel overview
http://www.opensourceforu.com/2010/11/
understanding-linux-device-drivers/
Device drivers Pipes Drivers no Linux
Camadas de software
Tanenbaum: Figura 5.10
Device drivers Pipes Drivers no Linux
Tratamento de interrupcoes
Tanenbaum: Figura 1.10
Device drivers Pipes Drivers no Linux
Tratamento de interrupcoes
Tanenbaum: Figura 5.5
Device drivers Pipes Drivers no Linux
Como programar os dispositivos?
Instrucoes especiais
IN REG, PORTOUT PORT, REG
Memory-mapped I/O
MOV REG, ADDR
Conforme o valor de ADDR, a instrucao MOV fara acesso auma palavra de memoria ou dispositivo
Device drivers Pipes Drivers no Linux
Como programar os dispositivos?
Tanenbaum: Figura 5.2
Device drivers Pipes Drivers no Linux
Barramento simples e dual
Tanenbaum: Figura 5.3
Device drivers Pipes Drivers no Linux
Direct Memory Access (DMA)
Tanenbaum: Figura 5.4
Device drivers Pipes Drivers no Linux
Imprimindo uma string
Tanenbaum: Figura 5.6
Device drivers Pipes Drivers no Linux
Imprimindo uma stringProgrammed I/O
Tanenbaum: Figura 5.7
Trecho de codigo do kernel
Device drivers Pipes Drivers no Linux
Imprimindo uma stringInterrupt-driven I/O
Tanenbaum: Figura 5.8
(a) Trecho de codigo do kernel(b) Tratador da interrupcao
Device drivers Pipes Drivers no Linux
Imprimindo uma stringDMA
(a) Trecho de codigo do kernel(b) Tratador de interrupcao
Device drivers Pipes Drivers no Linux
copy to user e copy from user
Confidently, Shweta took on the challenge, and modifiedmy read() and my write() ...
static char c;static ssize_t my_read(struct file *f, char __user *buf,
size_t len, loff_t *off) {printk(KERN_INFO ‘‘Driver: read()\n’’);buf[0] = c;return 1;
}static ssize_t my_write(struct file *f, const char __user *buf,
size_t len, loff_t *off) {printk(KERN_INFO ‘‘Driver: write()\n’’);c = buf[len - 1];return len;
}Ref:Device Drivers, Part 6: Decoding Character Device File Operations
Device drivers Pipes Drivers no Linux
copy to user e copy from user
“Almost there, but what if the user has provided aninvalid buffer, or if the user buffer is swapped out.Wouldn’t this direct access of the user-space buf justcrash and oops the kernel?” pounced Pugs.
static char c;
static ssize_t my_read(struct file *f, char __user *buf,size_t len, loff_t *off) {
printk(KERN_INFO ‘‘Driver: read()\n’’);if (copy_to_user(buf, &c, 1) != 0)
return -EFAULT;else
return 1;}Ref:Device Drivers, Part 6: Decoding Character Device File Operations
Device drivers Pipes Drivers no Linux
Pipes
$ grep xxx log.txt > log-xxx.txt$ wc -l log-xxx.txt$ rm log-xxx.txt
$ grep xxx log.txt | wc -l
Device drivers Pipes Drivers no Linux
pipe()
int pipe (int FILEDES[2])
The ‘pipe’ function creates a pipe and puts the filedescriptors for the reading and writing ends of the pipe(respectively) into ‘FILEDES[0]’ and ‘FILEDES[1]’.
Veja o codigo: mypipe.c
Device drivers Pipes Drivers no Linux
Pipe com entrada e saıda padrao?
int dup2(int oldfd, int newfd);
dup2 makes newfd be the copy of oldfd, closing newfdfirst if necessary. After successful return of dup or dup2,the old and new descriptors may be used interchangeably.
Veja o codigo: mypipe2.c
Device drivers Pipes Drivers no Linux
Processos conectados de maneira transparente
$ cm1 <args1> | cmd2 <args2>
A modificacao da entrada e saıda padrao deve ser feita antesda chamada a execve().
Veja o codigo: minishell.c
Device drivers Pipes Drivers no Linux
popen()
FILE *popen(const char *command,const char *type);
int pclose(FILE *stream);
The popen() function opens a process by creating a pipe,forking, and invoking the shell. Since a pipe is bydefinition unidirectional, the type argument may specifyonly reading or writing, not both; the resulting stream iscorrespondingly read-only or write-only.
Veja o codigo: mypopen.c e mypopen2.c
Device drivers Pipes Drivers no Linux
Programando um device driver
Veja a serie: Device drivers de Anil Kumar Pugalia
Exemplo numero 1: ofd.c
Desafio: como implementar um device driver comcomportamento de pipe?