linux device drivers
play

Linux Device Drivers Linux Device Drivers Kernel 2.6 1. - PowerPoint PPT Presentation

Linux Device Drivers Linux Device Drivers Kernel 2.6 1. Introduction Dr. Wolfgang Koch 2. Kernel Modules Friedrich Schiller University Jena 3. Char Drivers Department of Mathematics and 4. Advanced Char Drivers Computer Science 5.


  1. Linux Device Drivers Linux Device Drivers Kernel 2.6 1. Introduction Dr. Wolfgang Koch 2. Kernel Modules Friedrich Schiller University Jena 3. Char Drivers Department of Mathematics and 4. Advanced Char Drivers Computer Science 5. Interrupts Jena, Germany wolfgang.koch@uni-jena.de 1. Introduction 2. Kernel Modules � "Hello World" Module � Device Driver � Debug-Print, printk etc. � I/O Control � Loading, Unloading, insmod � Using Ports in User Space � Compiling, Kernelversion � gcc Inline Assembler � Monitoring, Parallel Port � Kernel Space � Writing to tty � Literature, Web Sites � Command Line Arguments

  2. 3. Char Drivers 3. Char Drivers – cont. � File Operations � read( ), put_user( ) � Device Files, Major & Minor Numbers � open( ), release( ), Usage Count � file_operations Structure � file Structure, llseek( ) � register_chardev, Choice of Major Number � write( ), get_user( ) � mknod � Race conditions, Atomic Variables � register_chardev_region – the new way � Spinlocks, Semaphores 4. Advanced Char Drivers 5. Interrupts � Use of Minor Numbers � Blocking I/O � Device Control, ioctl( ) � poll( ) � Asynchronous Notification � Access Control � Hardware Management � Writing to /proc Files � Interrupts � Memory, kmalloc() � The Bottom Half � Time, Delay

  3. Device Driver Device Driver Driver: A set of functions that manipulate a hardware device – as a part of (an extension to) the operating system: A set of functions (of software) that manipulate a hardware device � OS needs / provides access to I/O devices Today as a part of (better: an extension to) the � uniform programming surface, "Virtual Device" operating system � necessary privileges A container for a collection of subroutines that the OS calls to perform various operations that relate � concurrency, multi tasking system – to a hardware device. applications share physical hardware 10 11 Device Driver Kernel Modules A set of functions that manipulate a hardware device – as a part of (an extension to) the operating system: A set of functions that manipulate a hardware device a software layer that lies between the applications and the actual – as a part of (an extension to) the operating system: device – hides the details, provides a standardized surface applications use system calls – in Linux: Kernel Modules open( ), read( ), write( ), ioctl( ), ... close( ) can be built separately from the rest of the kernel if we exchange a device, we simply change the driver – the and loaded at runtime when needed applications are not affected 12 13

  4. Kernel Modules I/O Control in Linux: Kernel Modules this lectures: Addr hands-on tutorial to write kernel module drivers Data BUS CPU Contr (simple character drivers, without DMA, PnP, USB, ... no multiprocessor (SMP) machines - synchronisation) Mem Dev1 Dev2 Kernel version 2.6 (also 2.4) Warning: we have full privileges, can do a lot of damage 14 15 I/O Control Ports Addr Addr Data BUS Data BUS CPU Contr Contr Address Mem Port1 Port2 Recognition Data Reg External Control Reg Device Status Reg external Dev1 Dev2 16 17

  5. Synchronisation by Polling Ports Addr Addr Data BUS Data BUS Contr Contr Address Recognition Data Reg Data Data Reg External Address Recognition: Device Ready e.g. 1st serial port (COM1): 0x3F8 – 0x3FF (0011 1111 1...) Status Reg 1 memory mapped or separate I/O space (Intel) memory space: Load, Store, Move; I/O ports: In, Out do ; while ( (inb(SR) & 0x80) == 0 ); // busy wait base address: flash ROM (old: DIL switches) outb ( Databyte, DR ); // impl. reset of SR lower address bits: address of internal registers 18 19 Synchronisation by Interrupt Polling vs. Interrupt Addr Data BUS The CPU is responsible for synchronisation, Contr IRQ addressing and byte counting The device must be slower than the CPU Data Data Reg � Polling – waste of CPU time, fast reaction Control Reg 1 External � Interrupt – takes time to accept IR-enable Device & Ready �→ Interrupt at the beginning of a block, then polling Status Reg 1 Interrupt lines are scarce 20 21

  6. DMA – Direct Memory Access Handshaking If the CPU is responsible for addressing and byte counting: For fast devices we may need a second status line Start: Load R2, Count from the port to the device – Acknowledge Load AR1, StartAddr Loop: In R1, DR // synchr. not considered Store (AR1), R1 device and port "shake hands“ Inc AR1 Dec R2 Jnz Loop // very expensive ==> DMA � again with polling or interrupt → 22 23 Using Ports in User Space DMA Controller #include <sys/io.h> Addr Data BUS CPU unsigned char inb (unsigned short int port); Contr void outb (unsigned char value, unsigned short int port); Mem DMAC Port analogous: inw(), inl(), outw(), outl(), inb_p() ... DMAC is 2nd Bus Master responsible for addressing, counting, synchronisation inline functions, compile: gcc -O -Wall ... Cycle steal modus or Burst modus 24 25

  7. Using Ports in User Space Using Ports in User Space #include <sys/io.h> #define PORT 0x378 // parallel port #include <sys/io.h> int main() { int iopl(int level); // level = 3 outb(0x25, PORT); int ioperm(ulong from, ulong num, int on); ---------------------------------------------- > ./exe (or root: # ./exe) require root privileges Speicherzugriffsfehler export LANG=en_US ==> Segmentation fault export LANG=hu_HU ==> Szegmens hiba ( 2.4: Szegmentálási hiba ) 26 27 Using Ports in User Space Using Ports in User Space int k = iopl(3); provide root privileges to an executable program: printf(" Result iopl: %d \n",k); perror(" iopl() "); > ll exe // ls -l // if (k<0) { perror(" iopl"); exit 1; } -rwxr-xr-x 1 nwk users 3160 ... exe -------------------------------------------- > ./exe # chown root:root exe Result iopl: -1 # chmod a+s exe iopl() : Operation not permitted -rwsr-sr-x 1 root root 3160 ... exe -------------------------------------------- # ./exe > ./exe ==> Result iopl: 0 Result iopl: 0 s - setuid, setgid, process gets eff. user ID/ group ID iopl() : Success // outb() works of the file owner (root), not of the caller 28 29

  8. gcc Inline Assembler gcc Inline Assembler unsigned char GNU assembler syntax inb(unsigned short int port) { mov from, to unsigned char val; movl %eax, %ebx // longword (32 bit) asm volatile("inb %1,%0": "=a" (val): "d" (port)); movw %ax, %bx // word (16 bit) return val; movb %al, %bl // byte ( 8 bit) } asm(instructions : outp operands : inp operands movl $0x386, %edi // immediate operand [: clobbered registers]); movb (%esi), %al // indirect memory reference operands: "constraints" (C_expr) -> %0, %1 ... ( look at the output of 'gcc -S source.c' ) > gcc -O -S ==> inb %dx,%al 30 31 gcc Inline Assembler gcc Inline Assembler constraints: asm volatile("outb %b0,%w1"::"a" (val),"Nd" (port)); "=" - output "r" - general register no output operands "m" - memory operand %0: byte, "a" val -> %al "g" - general register, memory or immediate operand %1: word; "Nd" port -> byte constant "&" - different from all input operands or %dx "a" - %eax ... "d" - %edx ==> outb %al, $255 "D" - %edi, "S" - %esi or outb %al, %dx "N" - byte constant (0 .. 255) 32 33

  9. gcc Inline Assembler gcc Inline Assembler asm ("movl %2,%0; incl %2; movl %2,%1" : int xy=5; "=r" (m), "=r"(k) : "r" (n)); asm("rorb %b0": "=r" (xy): "0" (xy)); --> n=37, m=38, k=38 not correct!! printf(" %02x \n",xy); ( movl %eax,%eax; incl %eax; movl %eax,%edx ) // rotate right 1 byte: 0x05 -> 0x82 asm ("movl %2,%0; incl %2; movl %2,%1" : output operand %0 "=&r" (m), "=r"(k) : "r" (n)); input operand "0" -> same register as %0 --> n=37, m=37, k=38 OK --> rorb %al ( movl %eax,%ecx; incl %eax; movl %eax,%edx ) ("rorb %0" --> rorb %eax --> Warning) 34 35 gcc Inline Assembler User Space – Kernel Space kernel, modules – kernel space references (Inline Assembler): applications – user space gcc documentation: http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Extended-Asm.html one task of the OS: independent operation of programs and protection against unauthorized access to resources http://www-106.ibm.com/developerworks/library/l-ia.html ( + links) the CPU enforces protection of system software from applications Intel IA-32 Architecture Software Developer's Manuals: ( i386: 4 rings – supervisor mode (kernel space): ring 0, http://cedar.intel.com/cgi-bin/ids.dll/topic.jsp?catCode=BME user mode (user space): ring 3 ) 36 37

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend