Post on 09-Oct-2015
description
1Linux Device Driver
E b dd d O ti S t L bE b dd d O ti S t L bEmbedded Operating System Lab.Embedded Operating System Lab.Department of CS. Soongsil Univ.Department of CS. Soongsil Univ.
Phone : 010Phone : 010--39053905--17451745EE il i i @ ilil i i @ ilEE--mail : tipsiness@gmail.commail : tipsiness@gmail.com
Linux Device Driver
2Contents
ARM Architecture 9 9 9 9 9 9 9 9 9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
3
Linux Device Driverhttp://eoslab.ssu.ac.kr
4 9 S/W Component
( )9 ( )
9 ( )9 Dynamic loading/unloading ( )9 Dynamic loading/unloading ( )9 (access control, queuing, )9
Linux Device Driverhttp://eoslab.ssu.ac.kr
5 Traditional PC
Secondary storage
Main memory
Input device
Output device
p
CPU
Linux Device Driverhttp://eoslab.ssu.ac.kr
Communication Device
6 Embedded System (for example: phone)9 CPU: ARM9, XScale, DSP, 9 Memory: NOR flash, SRAM9 Storage: NAND flash9 Input: Key pad Touch Screen9 Input: Key pad, Touch Screen9 Output: LCD, Sound, Multimedia9 Communication
WLAN CDMA IrDA Bluetooth IrDA, Bluetooth UART, USB
Linux Device Driverhttp://eoslab.ssu.ac.kr
7 Key Button (Keyboard)9 an array of switch
Linux Device Driverhttp://eoslab.ssu.ac.kr
8 7-Segment Display
Linux Device Driverhttp://eoslab.ssu.ac.kr
9 LCD
Linux Device Driverhttp://eoslab.ssu.ac.kr
10 Touch screen
Linux Device Driverhttp://eoslab.ssu.ac.kr
11 Flash memory
Technology NOR NAND DINOR AND
Product
I lIntel28F128J3A-15028F320D18B110
AMD
SSamsungK9F5608UOM
ToshibaTH58512FTI
MitsubishiM5M29GB/T160BVP 80
HitachiHN29W25611
Am29LV641DUAm29DL322D
AMDAm30LV0064D
T160BVP-80
Density Low High Low HighE L L L llS llErase
Block sizeLarge
(64 128KB)Large
(16 128KB)Large
(32 64KB)Small(2KB)
ddAddressingsize 1, 2, 8, 32B 512B, 2KB 1, 2, 256B 2KB
C it L Hi h L Hi hCapacity Low High Low High
St tStructure
Linux Device Driverhttp://eoslab.ssu.ac.kr) Flash Disk ?
12 DSP chip
Linux Device Driverhttp://eoslab.ssu.ac.kr
13 CDMA chip (Modem chip)9 Baseband RF, GPS, DSP, Application Processor
Linux Device Driverhttp://eoslab.ssu.ac.kr
14 Timers9 adder (or subtractor), register, reset, input, output9 i t i di l k i di i l9 input: periodic clock, aperiodic signal
Linux Device Driverhttp://eoslab.ssu.ac.kr
15 Interrupt controller
Linux Device Driverhttp://eoslab.ssu.ac.kr
16
Linux Device Driverhttp://eoslab.ssu.ac.kr
17
Statusregister
Devicedata/address
CPU PCControlregister
mechanism
interrupt
Device
9 ) ARM?9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
9
18Embedded Board
S3C2410X Board
Linux Device Driverhttp://eoslab.ssu.ac.kr
19ARM 32bit RISC Processor9 load store architecture9 Fixed & Uniform length instruction9 Thumb mode support
37 (32bit) registers37 (32bit) registers9 General special (eg. Link register) 9 Bank register9 Bank register
Big/Little Endian SupportFast Interrupt ResponseFast Interrupt ResponseHigh-level Language Support9 auto-increment/decrement addressing modeauto c e e t/dec e e t add ess g ode9 multiple registers load/store instructions
Memory mapped I/O
Linux Device Driverhttp://eoslab.ssu.ac.kr
20ARM Programming Model
ARM Register Model
Linux Device Driverhttp://eoslab.ssu.ac.kr
21ARM Programming Model
ARM Register Model9 16 general-purpose 32-bit register (r0~r15)
general special register r13: stack pointer (sp) r14: link registerg r15: program counter (pc)
9 Current Program Status Register (CPSR)
9 System mode Registers ( ) Supervisor mode (r13~r14_svc, SPSR_svc) IRQ mode (r13~r14_irq, SPSR_irq) Abort mode (r13~r14 abt SPSR abt) Abort mode (r13~r14_abt, SPSR_abt) Undefined mode (r13~r14_und, SPSR_und) FIQ mode (r8~r14_fiq, SPSR_fiq)
l 37 i
Linux Device Driverhttp://eoslab.ssu.ac.kr
total 37 registers
22ARM Programming Model
ARM Register Model9 CPSR
status of the result of data processing instructions control processor mode
Linux Device Driverhttp://eoslab.ssu.ac.kr
23ARM Programming Model
ARM Register Model9 Banked Register: Processor mode instance
i t access register
R13 USERR13_USER
R13_SVC
R13 ABORTR13
_
R13_UNDEF
Selector=CPSR
Linux Device Driverhttp://eoslab.ssu.ac.kr
24ARM Programming Model
ARM Memory Model9 memory
a linear array of bytes numbered from 0 to 232-1 access unit: bytes, half word, word (32 bits)
word : 4 bytes align ( 2 0) half word : 2 bytes align ( 1 0)
little endian big endian
memory mapped I/O for peripheral controls registers in these devices appear within the ARM memory map.
Linux Device Driverhttp://eoslab.ssu.ac.kr
g pp y p may be read and written using the same (load-store) instructions
25ARM Programming Model
ARM Memory Model9 little vs big endian (another view)
Linux Device Driverhttp://eoslab.ssu.ac.kr
26ARM Programming Model
ARM Exceptions9 Exception
Abort: current memory access can not be completed Prefetch abort Data abort
Software Interrupt Undefined
9 Interrupt9 Interrupt IRQ: Normal Interrupt FIQ: Fast Interrupt, Higher Priority
9 Exception Vectors 0x0 ~ 0x1c
Linux Device Driverhttp://eoslab.ssu.ac.kr
27ARM Programming ModelARM E tiARM Exceptions9 Entry
copy PC into r14 exc CPSR into SPSR exccopy PC into r14_exc, CPSR into SPSR_exc change processor mode into the appropriate exception mode set PC a value between 0x0~0x1c (depending on the type of
exceptions)exceptions) Each Exception vector usually contains a branch instruction to an
exception handler Each handler uses a dedicated stack (r13_exc), save some user
registers in the stack
9 Exit restore user registers, CPSR, PC
d fi d ti : undefined exception (memory error ), S/W
emulation ( coprocessor emulation)2: IA Vector ARM
Linux Device Driverhttp://eoslab.ssu.ac.kr
2: IA Vector . ARM (B instruction) . IA CPU Vector pc ARM Vector () PC .
28ARM Programming Model
ARM Exception: IRQ
Linux Device Driverhttp://eoslab.ssu.ac.kr
29ARM Programming Model
ARM Exception:
Linux Device Driverhttp://eoslab.ssu.ac.kr
30ARM Programming Model
ARM Exception: SWI Interrupt
Linux Device Driverhttp://eoslab.ssu.ac.kr
31ARM Programming Model
ARM Exception: Reset
Linux Device Driverhttp://eoslab.ssu.ac.kr
32ARM Programming Model
ARM Exception Priority
Linux Device Driverhttp://eoslab.ssu.ac.kr
33ARM Programming Model
ARM ISA (Instruction Set Architecture) 9 load store architecture9 3 address instruction9 conditional execution 9 multiple load/store instructions9 multiple load/store instructions9 perform shift and ALU operations in a single clock cycle9 link register g9 instruction set extension 9 Thumb mode support
Linux Device Driverhttp://eoslab.ssu.ac.kr
34ARM Programming Model
C 9 , 9 , , 9 9 9 9
ARM instruction 9 data transfer instructions
move values between memory and registers move values between memory and registers9 data processing instructions
operate on values in registers9 control flow instructions
change the program counter
Linux Device Driverhttp://eoslab.ssu.ac.kr
35ARM Programming Model
ARM instruction 9 data processing instructions
ADD ADC SUB SBC RSB RSC MUL MLA ADD, ADC, SUB, SBC, RSB, RSC, MUL, MLA AND, ORR, EOR, BIC LSL, LSR, ASL, ASR, ROR, RRX // not separate instruction
CMP CMN TST TEQ CMP, CMN, TST, TEQ MOV, MVN
9 data transfer instructions LDR, STR LDM, STM SWP ADR // directive
9 control flow instructions B (BEQ BGT BLT BCS )B (BEQ, BGT, BLT, BCS, ) BL SWI
Linux Device Driverhttp://eoslab.ssu.ac.kr
) IA
36ARM Programming Model
ARM case label: names to memory locations
location1 location1 ADR r4, aADR r4, aLDR r0 [r4]LDR r0 [r4] ; load a; load a
location1 location1 ADR r4, aADR r4, aLDR r0 [r4]LDR r0 [r4] ; load a; load aLDR r0, [r4]LDR r0, [r4] ; load a; load aADR r4, bADR r4, bLDR r1, [r4]LDR r1, [r4] ; load b; load bADD r2 r1 r0 LSL #3ADD r2 r1 r0 LSL #3
LDR r0, [r4]LDR r0, [r4] ; load a; load aADR r4, bADR r4, bLDR r1, [r4]LDR r1, [r4] ; load b; load bADD r2 r1 r0 LSL #3ADD r2 r1 r0 LSL #3ADD r2, r1, r0, LSL #3ADD r2, r1, r0, LSL #3LDR r4, cLDR r4, cSTR r2, [r4]STR r2, [r4] ; store c; store c
ADD r2, r1, r0, LSL #3ADD r2, r1, r0, LSL #3LDR r4, cLDR r4, cSTR r2, [r4]STR r2, [r4] ; store c; store c
comments
instruction: opcode (what to do)
instruction: operand (reg., memory)
Linux Device Driverhttp://eoslab.ssu.ac.kr
37ARM Programming Model
Operand : immediate, register, memory, Addressing Mode9 Immediate addressing9 Absolute addressing
I di t dd i9 Indirect addressing9 Register addressing9 Register Indirect addressing9 Register Indirect addressing9 Base plus Offset addressing9 Base plus Index addressing9 Base plus Scaled Index addressing9 Base plus Index plus Offset addressing
St k dd i9 Stack addressing ARM: load/store architecture
- data processing instruction: register addressingd t t i t ti i t i di t dd i
Linux Device Driverhttp://eoslab.ssu.ac.kr
- data movement instruction: register indirect addressing base plus index(or offset) addressing
38ARM Programming Model
Some Features in ARM Assembly Instruction9 conditional execution
CMP r0, #5 ;BEQ BYPASS ; if (r0 != 5) {ADD 1 1 0 1 1 0 2ADD r1, r1, r0 ; r1 := r1 + r0 r2;SUB r1, r1, r2 ; }
BYPASS
CMP r0 #5 ; if (r0 != 5) {
may be replaced by
CMP r0, #5 ; if (r0 != 5) {ADDNE r1, r1, r0 ; r1 := r1 + r0 r2;SUBNE r1, r1, r2 ; }
The new sequence is both smaller and faster than the old one
Linux Device Driverhttp://eoslab.ssu.ac.kr
39ARM Programming Model
Some Features in ARM Assembly Instruction9 Multiple Load/Store Instruction
9 Auto Indexing in a single cycleg g y
Linux Device Driverhttp://eoslab.ssu.ac.kr
40ARM Programming Model
Some Features in ARM Assembly Instruction 9 Multiple Data Transfer
Linux Device Driverhttp://eoslab.ssu.ac.kr
41ARM Programming Model
Some Features in ARM Assembly Instruction9 Function Call
caller callee callee
BL r14=pc pc=addr_of_label
BL (?)
Linux Device Driverhttp://eoslab.ssu.ac.krmov pc, r14
42ARM Programming Model
Some Features in ARM Assembly Instruction9 Function Call
caller callee/caller callee
BL STMDB r13!, {r0-r2, r14} r14=pc pc=addr_of_label
BLSTMDB r13!, {r0 r2, r14}
mov pc, r14
LDMIA r13!, {r0-r2, pc}
Linux Device Driverhttp://eoslab.ssu.ac.kr
43ARM Programming Model
Some Features in ARM Assembly Instruction9 Supervisor calls (SWI)
caller callee
supervisor mode dedicated stack exception vector
B
SWI14
stack ()
r14_svc=pc SPSR_svc = CPSR CPSR pc=0x8 pc 0x8
stack CPSR = SPSR_svc pc = r14_svc
Linux Device Driverhttp://eoslab.ssu.ac.kr
44ARM Programming Model
Some Features in ARM Assembly Instruction9 Thumb Mode
for program size optimization 16 bit instruction set limited register accessg
Linux Device Driverhttp://eoslab.ssu.ac.kr
45CPU Performance Consideration
What processor do?
Instruction type Dynamic usageyp y g
Data movement 43%
Control flow 23%
Arithmetic operations 15%
Comparisons 13%
Logic operations 5%g p
Other 1%
9 data movement optimization 9 data movement optimization CPU cache(L1, L2, L3), Write buffer
9 CPU component CPU component , Pipelining Superscalar, VLIW, EPIC
Linux Device Driverhttp://eoslab.ssu.ac.kr
46CPU Performance Consideration
Typical Instruction Execution Flow
Linux Device Driverhttp://eoslab.ssu.ac.kr
47CPU Performance Consideration
Pipeline9 instruction (stage)
fetch the instruction from memory (fetch) decode it to see what sort of instruction it is (dec) access any operands that may be required from the register bank access any operands that may be required from the register bank
(reg) Combine the operands to form the results or a memory address
(ALU)(ALU) Access memory for a data operand, if necessary (mem) Write the result back to the register bank (res)
fetch dec reg ALU mem res
fetch dec reg ALU mem res
fetch dec reg ALU mem res
Linux Device Driverhttp://eoslab.ssu.ac.kr latency vs throughput
48CPU Performance Consideration
Pipeline 9 CPU components 9 ( RISC)9 worst
pipelining full
speed-up 9 components
multiple units hyper threading
Linux Device Driverhttp://eoslab.ssu.ac.kr
49CPU Performance Consideration
Pipeline 9 pipeline hazard
fetch dec reg ALU mem res
read-after-write hazard
fetch dec reg ALU mem res
branch hazardfetch dec reg ALU mem res
fetch dec reg ALU mem res
f h d ALUfetch dec reg ALU mem res
fetch dec reg ALU mem res
fetch dec reg ALU mem res code reordering branch prediction (static vs dynamic) dynamic execution (IA) forwarding path (ARM9)
fetch dec eg U e es
Linux Device Driverhttp://eoslab.ssu.ac.kr
forwarding path (ARM9) pipeline hazard instruction stage . (trade off)
(ARM720T 3, ARM920T 5, ARM11E 8-stage pipeline )
50CPU Performance Consideration
RISC9 features
single cycle per instructionsingle cycle per instruction (32bit), (3-operand), load/store architecture,
9 advantage simple instruction set greatly simplifies the design of the pipelinessimple instruction set greatly simplifies the design of the pipelines small die size
fewer transistors, less silicon area Cache, MMU, FPU,
shorter development time (time to market) higher performance
pipeline : two or three times speedup (UC Berkeley RISC I, II) higher clock rate higher clock rate
low power consumption (5V, 3V, 1~2V, power save mode, NVRAM)
9 disadvantagedisadvantage poor code density
fixed instruction length, reduced instruction more memory use, more memory access
Linux Device Driverhttp://eoslab.ssu.ac.kr
dont execute x86 code
51CPU Performance Consideration
I-cache+4
fetch
nextpc
pc + 4
ARM 920T Details
I decode
register read
instructiondecoder15
pc+8
register readimmediatefields
postLDM/STM
mul
ALUexecute
forwardingpaths
regshiftpost-index
pre-index
STM+4
mux
shift
byte repl.
p
SUBS pc
B, BLMOV pc
rot/sgn ex
D-cache buffer/dataload/storeaddress
LDR pc
Linux Device Driverhttp://eoslab.ssu.ac.krwrite-back
LDR pc
register write
52ARM ARM Architecture and Cores
core families
architecture
core families
from http://www.arm.com/products/CPUs/architecture.html
Linux Device Driverhttp://eoslab.ssu.ac.kr
p pfrom http://www.arm.com/products/CPUs/core_selector.html
53ARM ARM Architecture9 ISA (Instruction Set Architecture)
ISA is the interface between SW and HW ISA defines the external view of processor architecture
Addressing size, Thumb mode Enhanced DSP instruction/Jazelle/Media instruction/TrustZone Enhanced DSP instruction/Jazelle/Media instruction/TrustZone
9 ARMv1, v2, v3, v4, v5, v6 (with variants)
ARM cores9 ARM Architecture (basic fetch/execution engine) + Debugging
support + Cache/MMU + additional feature (pipelining FPU )support + Cache/MMU + additional feature (pipelining, FPU, )9 ARM7TDMI, ARM720T, ARM920T, ARM926EJ, ARM1020E ,
ARM1176JZ
Linux Device Driverhttp://eoslab.ssu.ac.kr
54ARM ARM Architecture Versions and Variants 9 ARMv1
developed at Acorn Computer Limited between 1983 and 1985 developed at Acorn Computer Limited between 1983 and 1985 26-bit addressing no multiplication, CPSR, (never used in a commercial product)
9 ARMv2 still 26-bit addressing (version 1 extension) 32 bit result multiply (with accumulate) and coprocessor support 32-bit result multiply (with accumulate) and coprocessor support atomic load and store instruction (SWP) support (ARMv2a)
9 ARMv3 developed at ARM Limited (1990) 32-bit addressing CPSR SPSR ( R15 )CPSR SPSR ( R15 ) multi mode support: Undefined, Abort ARM6, ARM610, ARM7, ARM710 core (Apple Newton
PDA )
Linux Device Driverhttp://eoslab.ssu.ac.kr
PDA )
55ARM ARM Architecture Versions and Variants 9 ARMv4
fully 32 bit addressing fully 32-bit addressing ARM ARMv4 ARMv4T: Thumb mode (T variant) ARMv4 StrongARM, ARM810 . ARMv4T ARM7TDMI,
ARM720T, ARM9TDMI, ARM920T, ARM940T 9 ARMv5TE{J}ARMv5TE{J}
Superset of ARMv4 (1999 ) Enhanced DSP Instruction
J ll J H/W A l ti Jazelle : Java H/W Acceleration ARM9E, ARM10TDMI, ARM1020E, XScale
9 ARMv6{Z|T2}{ | } Media instruction (SIMD) (2001 ) ARMv6Z(TrustZone), ARMv6T2(Thumb-2)
ARM1136J ARM1176JZ
Linux Device Driverhttp://eoslab.ssu.ac.kr
ARM1136J, ARM1176JZ
56ARM ARM Architecture Variants
from http://www.arm.com/products/CPUs/architecture.html Thumb mode: memory savings of up to 35% over the equivalent 32-bit code
(density vs performance tradeoff) DSP: up to 70% performance improvement for audio DSP applications DSP: up to 70% performance improvement for audio DSP applications Jazelle: 80% reduction in power consumption compared to a non Java-accelerated core
(JVM vs H/W accelerator) Media (SIMD): increase performance by up to four times for video and audio codec
Linux Device Driverhttp://eoslab.ssu.ac.kr
( ) p y p TrustZone: two separate address space for mobile banking(secure world, non secure world) Thumb 2 : 32-bit instruction execution in Thumb state
57ARM ARM Cores9 ARM7 Thumb Family9 ARM9 Thumb Family 9 ARM9E Family9 ARM10E Family9 ARM10E Family9 ARM11 Family
9 application core PDA, Settop, Vphone, GPOS support
9 embedded core automotive, storage, automotive, storage, RT feature support
Linux Device Driverhttp://eoslab.ssu.ac.kr
58ARM ARM Cores: ARM7 Thumb Family9 ARM7TDMI
ARMv4T 3-stage pipeline : 80~133MHz, up to 120 MIPS , p Variants: T(Thumb), D(Debug), M(64 bits Multiply), I(Embedded ICE) Debugg
JTAG EmbeddedICE RT ETM (Embedded Trace MacroCell) JTAG, EmbeddedICE-RT, ETM (Embedded Trace MacroCell)
9 ARM7TDMI-S A synthesizable version of ARM7TDMI
9 ARM7EJ-S Enhanced DSP Jazelle Enhanced DSP Jazelle
9 ARM720T integer core with MMU, unified cache (8KB)
Linux Device Driverhttp://eoslab.ssu.ac.kr
59ARM ARM Cores: ARM9 Thumb Family9 ARM9TDMI
ARMv4T 5-stage pipeline : ~ 200MHz (up to 300 MIPS)( p ) Integrated instruction and data caches MMU supporting Windows CE, Symbian OS, Linux, Palm OS
(MPU supporting )(MPU supporting ) 8-entry write buffer
9 ARM920T: Dual 16k caches 9 ARM922T: Dual 8k caches9 ARM940T: Dual 4k caches9 ARM940T: Dual 4k caches
Linux Device Driverhttp://eoslab.ssu.ac.kr
60ARM ARM Cores: ARM9E Family9 ARM9E
ARMv5TE (ARMv5TEJ) 5-stage pipeline : 180 ~ 250MHz (up to 300 MIPS) ( p ) Instruction and data cache/MMU
DSP-enhanced 32-bit RISC processors (Saturated arithmetic for GSM protocol and FFT) Tightly coupled memory (TCM) interfaces Tightly coupled memory (TCM) interfaces Optional VFP9 delivers high floating-point performance
9 ARM926EJ-S: for application core, Jazelle, caches (4K - 128K)9 ARM946E-S: for embedded core, cache (0K- 1M)9 ARM966E S: for "hard real time" applications
Linux Device Driverhttp://eoslab.ssu.ac.kr
9 ARM966E-S: for hard real-time applications
61ARM Intel StrongARM 9 ARMv4 compliant
DEC 1995 ARMv4 . Intel DEC SA1110
9 5-stage pipelineg p p9 16KB I-Cache, 8KB D-Cache 9 : ~205MHz9 ARM architecture MMU, 9 : RTC, Interrupt, UART, LCD, Touch, GPIO,
Watchdog, IIS, IrDA, PCMCIA, USB, Power Management Watchdog, IIS, IrDA, PCMCIA, USB, Power Management
9 SA-1110 (SA-1110 PXA255 PXA27* )
from http://developer.intel.com/design/pca/applicationsprocessors/1110 brf.htm
Linux Device Driverhttp://eoslab.ssu.ac.kr
p p g p pp p _
62ARM Intel XScale9 ARMv5TE compliant9 7-stage pipeline 9 : ~400MHz, 9 32KB dual cache DSP enhanced Branch prediction9 32KB dual cache, DSP enhanced, Branch prediction
9 PXA210 : 200MHz, 16 bits External Data Bus PXA250 : 400MHz, 32 bits External Data Bus PXA255 : PXA250 Internal memory PXA255 : PXA250 , Internal memory
(100200MHz), Cache Write-back bug , (1.5V1.3V) PXA26* : PXA25* Small size , Intel Strata Flash
PXA27* : (bulverde), MMX , 312~624MHz, 64MB Intel Strata Flash .from http://developer intel com/design/pca/prodbref/252780 htm
Linux Device Driverhttp://eoslab.ssu.ac.kr
from http://developer.intel.com/design/pca/prodbref/252780.htm
63 S3C2410X
Linux Device Driverhttp://eoslab.ssu.ac.kr
64 AMI Block Diagram
Linux Device Driverhttp://eoslab.ssu.ac.kr
65 S3C2410X Users Manual1: Product Overview 14: Interrupt Controller2 P M d l 15 LCD C t ll2: Programmers Model 15: LCD Controller3: ARM Instruction Set 16: A/D Converter4: Thumb Instruction Set 17: RTC5: Memory Controller 18: Watch Dog Timer6: NAND Flash Controller 19: SDI Interface7: Clock & Power Management 20: IIC Bus Interface7: Clock & Power Management 20: IIC-Bus Interface8: DMA 21: IIS-Bus Interface9: I/O ports 22: SPI Interface10: PWM Timer 23: Bus Priorities11: UART 24: Electrical Data12: USB Host Controller 25: Mechanical Data12: USB Host Controller 25: Mechanical Data13: USB Device
(source: www.samsung.com/Products/Semiconductor/SystemLSI/MobileSolutions/
Linux Device Driverhttp://eoslab.ssu.ac.kr
MobileASSP/MobileComputing/S3C2410X/um_s3c2410s_rev12_030428.pdf)
66 Memory controller 9 8 memory banks support
6 for ROM, SRAM, and others 2 for ROM, SRAM, SDRAM
9 128MB for each bank (total 1GB)128MB for each bank (total 1GB)9 supports programmable 8/16/32-bit data bus width9 fully programmable access cycles for all memory bank (except
bank 0)9 fixed bank start address for bank 0~6, programmable bank start
address for bank 7address for bank 7
9 Little/Big endian support9 support various type of ROM for booting
(NAND/NOR/EEPROM/)9 support self refresh and power down mode in SDRAM
Linux Device Driverhttp://eoslab.ssu.ac.kr
9 support self refresh and power down mode in SDRAM
67 Memory controller of S3C2410 board
Linux Device Driverhttp://eoslab.ssu.ac.kr
68 Clock & Power Manager9 3.6864 MHz main crystal, 32.768 KHz optional crystal9 main clock PLL (Phase Locked Loop) clock frequency9 main clock PLL (Phase Locked Loop) clock frequency
(MPLL, UPLL, )9 optional clock RTC, Power Management (cost vs power
tradeoff)tradeoff)9 Clock can be fed selectively to each function block by software
9 Power management: Normal, Slow, Idle, and Power-off mode normal mode: normal operation mode Slow mode: low frequency clock without PLLq y Idle mode: The clock for only CPU is stopped Power-off mode: The core power including
all peripherals is shut downp p
Linux Device Driverhttp://eoslab.ssu.ac.kr
69 Timer with Pulse Width Modulation (PWM)9 Five 16-bit Timer with PWM9 9
register for reserving value (initial register) counting register with adder (or subtracter)
comparator comparator input line (periodic: timer, aperiodic: counter) output line
t (i iti li ti ) li reset (initialization) line9 Watchdog timer
16-bit Watchdog Timer Interrupt request or system reset at timeout
9 PWM Frequency duty cycle LCD contrast, brightness
Linux Device Driverhttp://eoslab.ssu.ac.kr
70 RTC (R l ti l k)RTC (Real-time clock)9 Full clock feature (BCD number)
second, minute, hour, date, day, month, year9 Alarm function: alarm interrupt or wakeup from power off mode9 Support millisecond tick time for RTOS9 Independent power pin
main or optional crystal optional crystal power save ,
optional crystal
Linux Device Driverhttp://eoslab.ssu.ac.kr
71 Interrupt ControllerInterrupt Controller9 56 Interrupt sources
1 watchdog timer, 5 timers, 9 UARTs, 24 external interrupt, 4 DMA, 2 RTC, 2 C C S S S C fADC, 1 IIC, 2 SPI, 1 SDI, 2 USB, 1 LCD, 1 battery fail
9 Level/Edge mode (Programmable)9 FIQ support9 masking support
Linux Device Driverhttp://eoslab.ssu.ac.kr
72 GPIO (G l P I t/O t t P t )GPIO (General Purpose Input/Output Ports)9 General purpose I/O pin9 input or output (easily configurable)
9 alternative functions: /
9 AMI Key scan Buzzer GPIO 9 AMI Key scan, Buzzer GPIO
Linux Device Driverhttp://eoslab.ssu.ac.kr
73 DMA Controller9 4-channel DMA controller9 memory to memory, IO to memory, memory to IO, IO to IOy y, y, y ,9 Components: source, destination, counter, mode9 Three state FSM (Finite State Machine)
state 0 (initial state), state 1 (loading state), state 2 (operation state)state 0 (initial state), state 1 (loading state), state 2 (operation state)
Linux Device Driverhttp://eoslab.ssu.ac.kr
74 LCD ControllerLCD Controller9 transfer LCD image from a frame buffer to an external LCD driver9 Support various type LCD
D di t d DMA ( lk f d )9 Dedicated DMA (vclk, vframe, vd, )
9 STN LCD Display (passive)h d 2 bit i l (4 l l ) 4 bit i l(16 ) t i monochrome mode, 2-bit per pixel (4-gray levels), 4-bit per pixel(16-gray) support using
time based dithering algorithm 8-bit per pixel 256 colors, 12-bit per pixel 4096 colors support 4-bit dual scan, 4-bit single scan, 8-bit single scan display type support, g , g p y yp pp support multi screen size (640*480, 320*240, 160*160, )
9 TFT LCD Display (active) support 1, 2, 4, 8 bit-per-pixel for palettized TFT color LCD panel support 16, 24 bit-per-pixel non palettized true-color display support multi screen size (640*480, 320*240, 160*160, )
Linux Device Driverhttp://eoslab.ssu.ac.kr
75 A/D Converter9 8-ch analog input9 10-bit digital output (ADCDAT register)9 500kSPS
9 separate X/Y position conversionf TP t for TP support
Linux Device Driverhttp://eoslab.ssu.ac.kr
76 UARTUART9 Universal Asynchronous Receiver or Transmitter9 3-channel9 Programmable: Baud rates, IrDA mode, one or two stop bits, 5/6/7/8-bits
data width, handshake 9 Components: Baud rate generator, Transmitter, Receiver, control unit
Linux Device Driverhttp://eoslab.ssu.ac.kr
77 IIC-Bus Interface9 general purpose 2-pin serial communication port 9 one for data, the other for clock9 Serial, 8-bit bi-directional data transfer
standard mode: 100kbps, fast mode: 400kbps
IISIIS9 serial link to standard IIS CODEC (normal or MSB-Justified)9 128 Bytes FIFO for Tx/Rx with DMA9 Serial 8 /16 bit per channel data transfers9 Serial, 8-/16-bit per channel data transfers
SDI9 Secure digital interface for SD memory card or MMC9 Secure digital interface for SD memory card or MMC9 Compatible with SD card Spec. 1.0/MMC spec. 2.11
USB Host/DeviceUSB Host/Device9 2-port USB host9 1-port USB Device9 5 endpoints for USB Device
Linux Device Driverhttp://eoslab.ssu.ac.kr
9 5 endpoints for USB Device9 Compatible with USB Spec. 1.1
78 JTAG9 CPU
Flash memory fusing
9 5 (TDI, TMS, TCK, nTRST, TDO)
Linux Device Driverhttp://eoslab.ssu.ac.kr
79 ARM 9 ARM memory-mapped I/O 9 (Special
function register)9 polling vs interrupt 9 polling vs interrupt
Linux Device Driverhttp://eoslab.ssu.ac.kr
80 Special Function Register9 S3C2410X memory map
Linux Device Driverhttp://eoslab.ssu.ac.kr
81 S i l F ti R i tSpecial Function Register9 Ex) Memory Controller
Linux Device Driverhttp://eoslab.ssu.ac.kr
82Registers for Embedded DevicesS i l F ti R i tSpecial Function Register9 Ex) Interrupt Controller
Linux Device Driverhttp://eoslab.ssu.ac.kr
83Registers for Embedded DevicesS i l F ti R i tSpecial Function Register9 Ex) I/O Port
Linux Device Driverhttp://eoslab.ssu.ac.kr
84
Linux Device Driverhttp://eoslab.ssu.ac.kr
85
?9 (Resource Manager) (Resource Manager)9 (Computing Environment)
9 : , , , , , ...9 : , /, , ,
, , , ...
Linux Device Driverhttp://eoslab.ssu.ac.kr
86 Linux
Linux Device Driverhttp://eoslab.ssu.ac.kr
87
edit test.cint sum = 0;
a.out.data
.align 4int main(){
int i;
for (i=0; i
2 test.txt /dev/pts/2#include #include
#define MAX_BUF 4char fname[] = "test.txt";
abcdefchar fname[] test.txt ;
char tmp_data[] = "abcdefghijklmn";
int main(){
i t fd i
inode
int fd, size;char buf[MAX_BUF];
fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);write(fd tmp data sizeof(tmp data));write(fd, tmp_data, sizeof(tmp_data));close(fd);
fd = open(fname, O_RDONLY);lseek(fd, 5, SEEK_SET);i (f b f M X U ) ) !!size = read(fd, buf, MAX_BUF);
close(fd);fd=open(/dev/pts/2, O_WRONLY);write(fd buf MAX BUF);
) : !!
Linux Device Driverhttp://eoslab.ssu.ac.kr
write(fd, buf, MAX_BUF);close(fd);
}
89 (Example of Linux)
kernel ipc lib
/usr/src/linuxDoc
kernel ipc lib mmscripts
driver
blockcdromchararch
includeinitfs netdriver
alphaarm
asm-alpha802appletalk
charnetpci
dm68kmipsppc boot
asm alphaasm-arm
appletalkdecnetethernet
pnpsbusscsi
codaext2hpfspp
sparci386
bootkernellib
asm-i386linuxnet
ipv4unixsunrpc
soundvideo
msdosnfsisofs
math-emumm
scsivideo
x25ntfs
Linux Device Driverhttp://eoslab.ssu.ac.kr
)
90 1. (configure) : see Documentation/Configuration.help or
chapter 3 of The LINUX Network)p ) make config (menuconfig, xconfig, oldconfig, make mrproper)
2. (dependency check) make dep (make clean)
3. (compile) make zImage (make bzImage)cf) - make bzdisk (#dd bs=8192 if=$(BOOTIMAZGE) of=/dev/fd0)
- make zlilo (#cat $(BOOTIMAGE) > $(INSTALL PATH)/vmlinuz)- make zlilo (#cat $(BOOTIMAGE) > $(INSTALL_PATH)/vmlinuz)/etc/lilo.conf
- mkbootdisk --device /dev/fd0 bzImage,
4. (if required) make modules, make modules_install
/lib/modules/ version
Linux Device Driverhttp://eoslab.ssu.ac.kr
/lib/modules/_version insmod (modprobe)
91 : 9 (program instance),
9 9 (, , )
9 9 .
9 fork() : 9 fork() : 9 exec() : 9 wait(), waitpid() : () p ()9 exit() :
Linux Device Driverhttp://eoslab.ssu.ac.kr
92 : 9 , , , heap
Linux Device Driverhttp://eoslab.ssu.ac.kr
93 : : fork/* test_fork2.c: . 2003 9 22. choijm@dku.edu */
/#include #include
int glob = 6; char buf[] = "a write to stdoutn";char buf[] = a write to stdoutn ;
int main(void){
int var = 88; int var 88; pid_t pid;
if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {perror(write error); exit(1);
}}printf("before forkn"); /* we don't flush stdout */
if ( (pid = fork()) < 0) {perror(fork error); exit(1);perror( fork error ); exit(1);
} else if (pid == 0) { /* child */glob++; var++; /* modify variables */
} elsesleep(2); /* parent */
Linux Device Driverhttp://eoslab.ssu.ac.kr
sleep(2); / parent /printf("pid = %d, glob = %d, var = %dn", getpid(), glob, var);exit(0);
}
94 : :
Linux Device Driverhttp://eoslab.ssu.ac.kr
95 : : Details
memory glob, buf
textdataparent task
pid = 8327 glob++; var++
stackvar, pid
stack
glob, bufchild task
datapid = 8328
stack
var, piddd t ti b i
Linux Device Driverhttp://eoslab.ssu.ac.kr
) address : protection barrier) COW(Copy_on_Write)
96 : execl() /* execl_test.c: ls . 2003 9 22. choijm@dku.edu */#i l d < /t h>#include #include
int main(int argc, char *argv[]) What does this comment mean?int main(int argc, char argv[]){
pid_t pid, d_pid; int exit_status;
if (( id f k()) 1) {
What does this comment mean?
if ((pid = fork()) == -1) {// fork error
} else if (pid == 0) { // childexecl(/bin/ls, ls, -l, (char *)0);( / / , , , ( ) )printf(Child.. Im herenn);// execl exit(1);
} l { // t} else { // parent printf(pid = %dn, pid);d_pid = wait(&exit_status);printf(exit status of task %d is %dn, d_pid, exit_status);
Linux Device Driverhttp://eoslab.ssu.ac.kr
p ( , _p , _ )}
}
97 : execl()
Linux Device Driverhttp://eoslab.ssu.ac.kr
98 : exec() details9 , ,
memory
datatask
pid = 8328
headera.outtexttextdatai it
stack
init. stack
text
data
stack
data
) exec()
Linux Device Driverhttp://eoslab.ssu.ac.kr
) exec() eip text , esp top
99 : 9 (context)
: pid, fd, signal, state, priority, information
9 round robin (O(1) in Linux) round robin (O(1) in Linux) Real time scheduling
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
100 : 9 9 9 9 9 9
task 1 task 2 task n.User modeSystem mode
.System call interface
Virtual File System
ext2 nfs coda dosfs ntfs procfsext2 nfs coda dosfs . ntfs procfsbuffer cache File System
Linux Device Driverhttp://eoslab.ssu.ac.kr
device driver
101 : (NAND )9
9
0 1 2 3 4 56 7 8 9 10 116 7 8 9 10 1112 13 14 ..15 16
Linux Device Driverhttp://eoslab.ssu.ac.kr
102 : 9 free 9
14 K 4K ( 4 ) 4K ( 4 ) (
) ? (disk block allocation problem) ? (disk block allocation problem)
0 1 2 3 4 56 7 8 9 10 116 7 8 9 10 1112 13 14.. 15 16 17
Linux Device Driverhttp://eoslab.ssu.ac.kr
103 : 9 9
block chain, index block, FAT
Linux Device Driverhttp://eoslab.ssu.ac.kr
104 : ?9 index block 9 inode
, , , , , ,
i_inode_numberinode type (4bit) u g s r w x r w x r w x
S IFSOCKi_modei_nlink, i_rdevi_uid, gidi op i size
S_IFSOCKS_IFLNKS_IFREGS_IFBLKi_op, i_size
i_atime, ctime, mtimeS_IFDIRS_IFCHRS_IFIFO
713
24315512 direct block
.55
6772 77
8396 99
Linux Device Driverhttp://eoslab.ssu.ac.kr
3 indirect block 96 99123 125 128 131
105 : : inode 9 (pipe)
(i di bl k) (indirect block) readers, writers, read pointer, write pointer
9 9 , (direct block) (indirect block)
i_rdev : (device number) (major number) (minor number)
: (device type) : (device unit)
9 (socket)
Linux Device Driverhttp://eoslab.ssu.ac.kr
106 : 9 9 4 : boot super inode data blocks9 4 : boot, super, inode, data blocks9 mke2fs, newfs
system(PC)
/dev/hda /dev/hdb
(PC)
/dev/hda1 /dev/hda3/dev/hda2 boot
superi di-node
disk blocks
Linux Device Driverhttp://eoslab.ssu.ac.kr
blocks
107 : (super block) 9 ( inode
)9 : ext2, nfs, msdos, coda, proc,
9 ext2 free inode free block bitmap block group (ffs cylinder group )
9 nfs (Network File System by Sun) RPC XDR (weak consistency)
9 JFFS LFS (Log-structured file system)
append only log garbage collection (utilization ) RAID storage system (Flash memory )
Journaling
Linux Device Driverhttp://eoslab.ssu.ac.kr
Journaling intent log
108 : (Virtual memory)9 9 (virtual address), (physical address)
4GBtask 1
4GBtask 2
3GB
4GB
3GB
4GB
stack
3GB
stack stack
task 1
heap heaptask 2
text
data
text
data
Linux Device Driverhttp://eoslab.ssu.ac.kr
0 0virtual memory virtual memoryphysical memory
109 : : 9 .
(page frame) . 4/8KB
physical memory
Linux Device Driverhttp://eoslab.ssu.ac.kr
) page frame, page !!
110 : : ( execve() )9 test.exe (ELF format)
text 12KB, data 8KB, stack 4KB malloc
test.exe
stack Disk
inode t1 t2d1t3
d2 t1
t2
pf3
pf5
text
data d2
) test.exe physical memory
t1d1
ppf2
Linux Device Driverhttp://eoslab.ssu.ac.krvirtual memory
physical memory
111 : : 9 (loading)
f
pf6s1
t1 pf3
t2 pf5
page page frame
t2 pf5pf6s1
t3 -
d1 pf2
d2 - t1d1
pf3pf2
Page table
s1 pf6
d
physical memory
) (task struct) page table :
Linux Device Driverhttp://eoslab.ssu.ac.kr
) 1000 ?) 5000 ? 9000 ?
112 : : 9
task struct
t1 pf3t2 pf5
t2 pf5
t2 pf5
t3 -
d1 pf2
pf6s1
pf4t3
pf4
t1d1
pf3pf2
d2 -
Page table
s1 pf6 Disk
pt3
physical memoryPage table
test exe
inode t1 t2d1t3
d2
Linux Device Driverhttp://eoslab.ssu.ac.kr
test.exe d2
113 :
new task new task2
t1 pf3
page page frame t1 pf2
page page frame
Page table
t2 pf5
Page table
t2 pf4
Page table Page table
physical memory
Linux Device Driverhttp://eoslab.ssu.ac.kr
114 : 9
(32bit CPU 4G) (demand paging) ( )
9 ? (no free lunch) (address translation)
TLB (Translation Lookahead Buffer) TLB (Translation Lookahead Buffer) HAT (Hardware Address Translation)
Linux Device Driverhttp://eoslab.ssu.ac.kr) page vs segment?
115 : 9
9 9 pipe 9 FIFOFIFO9 System V IPC (InterProcess Communication)
message queue shared memory semaphore
9 socketsocket9 RPC (Remote Procedure Call)
Linux Device Driverhttp://eoslab.ssu.ac.kr
116 : ipipe
/* pipe example */#include #include #include
int main(void) {int main(void) {int fd[2], nbytes; pid_t childpid; char string[] = "Hello, world!n"; char readbuffer[80];
pipe(fd);if((childpid = fork()) == -1) {
perror("fork"); exit(1);} else if(childpid == 0) {} else if(childpid == 0) {
close(fd[1]);nbytes = read(fd[0], readbuffer, sizeof(readbuffer));printf("Received string: %s", readbuffer);exit(0);
} else { close(fd[0]); write(fd[1] string strlen(string));
Linux Device Driverhttp://eoslab.ssu.ac.kr
write(fd[1], string, strlen(string));} return(0);
}
117 : S t V IPC 3 System V IPC 3 9 IPC (identifier) 9 IPC 9 IPC 9 Key identifier (ftok)9 9 9 ***id_ds (eg: msqid_ds)9 IPC : ipcs ipcrm9 IPC : ipcs, ipcrm, ..
9 msqid = msgget (key, flag), msgsnd (msqid, msgp, msgsz, flag), msgrcv
(msqid, msgp, msgsz, msgtype, flag), msgctl(msqid, cmd, msqid_ds) shmid = shmget (key, size, flag), shmat (shmid, shmaddr, shmflag,
raddr), shmdt (shmaddr), shmctl(shmid, cmd, shmid_ds) semid = semget (key, nsems, flag), semop (semid, sops, nsops),
semctl(semid, semnum, cmd, *arg)
Linux Device Driverhttp://eoslab.ssu.ac.kr
118 : message queue
/* msq_test.h */#include #include
/* msq_server.c */#include msg_test.h"
#include
#define MSG_KEY 5678struct my_msgbuf {
int main(void) {int qid, result, length;struct my_msgbuf msgbuf;
long mtype;char data[100];
};qid = msgget(MSG_KEY, IPC_CREAT|0660);length = sizeof(struct my_msgbuf)
sizeof(long);/* msq_client.c */#include msg_test.h"int main(int argc char **argv) {
sizeof(long);
result = msgrcv(qid, &msgbuf, length, 0, 0);printf("Received: %sn", msgbuf.data);int main(int argc, char **argv) {
struct my_msgbuf msgbuf;int qid, result, length; qid = msgget(MSG KEY IPC CREAT | 0660 );
msgctl(qid, IPC_RMID, 0); // ..}
qid msgget(MSG_KEY, IPC_CREAT | 0660 );// .. by choijmmsgbuf.mtype = 1;strcpy(msgbuf.data, argv[1]);
( ) ( )
Linux Device Driverhttp://eoslab.ssu.ac.kr
length = sizeof(struct my_msgbuf) - sizeof(long);result = msgsnd(qid, &msgbuf, length, 0);
}
119 : message queue
Linux Device Driverhttp://eoslab.ssu.ac.kr
120 : hsemaphore
/* sem_test.c */#include
int p(int semid) {struct sembuf p;p sem num = 0;#include
#include #include #include
p.sem_num = 0;p.sem_op = -1;p.sem_flg = SEM_UNDO;if(semop(semid &p 1) == -1) {#include
/* semaphore initialize */int initsem(int semkey, int initval) {
if(semop(semid, &p, 1) == -1) {perror("p(semid)");return -1;
}{int semid; union semun sem_union;semid = semget(semkey, 1, 0666 |
return 0;}int v(int semid) {semid semget(semkey, 1, 0666 |
IPC_CREAT | IPC_EXCL);if (semid==-1 && errno==EEXIST)// ,
t t( k 1 0)
struct sembuf v;v.sem_num = 0;v.sem_op = 1;v sem flg = SEM UNDO;return semget(semkey, 1, 0);
// , sem_union.val = initval;
tl( id 0 SETVAL i )
v.sem_flg = SEM_UNDO;if(semop(semid, &v, 1) == -1) {
perror("v(semid)");return -1;
Linux Device Driverhttp://eoslab.ssu.ac.kr
semctl(semid, 0, SETVAL, sem_union); return semid;
}
return 1;}return 0;
}
121 : h dshared memory
/* shm_server.c */#include "shm test.h
/* shm_test.h */#include _
int main() {int i, shmid;i t h t ;
#define SHM_KEY 7984#define SHM_SIZE 100
/* shm_client.c */#include "shm_test.h"
int *shmptr;
shmid = shmget(SHM_KEY, SHM_SIZE, 0666|IPC_CREAT);
int main() {int i, shmid;int *shmptr;
| _ )shmptr = (int *)shmat(shmid, 0, 0);
for(i=0; i
123 : h d (fi l) shared memory (final)
Linux Device Driverhttp://eoslab.ssu.ac.kr
124 :
server (connection oriented protocol)
socket()
bind()
listen()
accept() socket()client
accept()connect()
it ()blocks until connection from a client
connect established
read()write()
data (request)
write() read()
processing request
Linux Device Driverhttp://eoslab.ssu.ac.kr
write() read()data (reply)
125 : (TCP/IP & Ethernet)
/ f / d it /
()/* net/socket.c */
sys_write()/* fs/read_write.c */
VFS
sock_write()
inet_sendmsg()/* net/ipv4/af_inet.c */
BSD socket
inet socket
tcp_send_skb()/* net/ipv4/tcp_output.c */
TCP
ip_queue_xmit()/* net/ipv4/ip_output.c */
/* driver/net/3c509 c */IP
el3_start_xmit()/ driver/net/3c509.c /
Linux kernelDevice
Linux Device Driverhttp://eoslab.ssu.ac.kr
126 : (TCP/IP & Ethernet)
/ f / d i /
/* net/socket.c */
sys_read()/* fs/read_write.c */
VFS
sock_read()
inet_recvmsg()/* net/ipv4/af_inet.c */
BSD socket
inet socket wake up
tcp_recvmsg()/* net/ipv4/tcp.c */
inet socket
TCP tcp_rcv_state_process()/* net/ipv4/tcp_input.c */
IP/* net/core/dev c */
ip_rcv()/* net/ipv4/ip_input.c */
sleep
Linux kernelDevice
el3_interrupt()/* driver/net/3c509.c */ net_bh()
/* net/core/dev.c */
Linux Device Driverhttp://eoslab.ssu.ac.kr
127 :
Linux 9 Linux (monolithic kernel or integrated kernel)
. (trivial modification)
. , .
9 module: steps toward micro-kernelized Linux (minimal kernel) (small and clean kernel)
.
Linux Device Driverhttp://eoslab.ssu.ac.kr
128 : 9 (Micro Kernel) 9 (Integrated kernel)
System Service API System Service API y
Server Server Server
y
Micro Kernel Architecture
Integrated kernel Architecture
Hardware
Architecture Architecture
Hardware
Linux Device Driverhttp://eoslab.ssu.ac.kr
129 :
9
.9
9 Linux file system register_filesystem, unregister_filesystem
dread_super, put_superblock device driver register_blkdev, unregister_blkdev
open, releasecharacter device driver register chrdev, unregister chrdevcharacter device driver register_chrdev, unregister_chrdev
open, releasenetwork device driver register_netdev, unregister_netdev
open, closed i i t d iexec domain register_exec_domain,
unregister_exec_domainload_binary, personality
binary format register binfmt, unregister binfmt
Linux Device Driverhttp://eoslab.ssu.ac.kr
binary format register_binfmt, unregister_binfmtload_binary
.
130 : 9 init_module(), cleanup_module()
9 9
9 # gcc -D__KERNEL__ -DLINUX -DMODULE -c new_module.c
Enable loadable module support (CONFIG_MODULES) [Y/n/?]MSDOS fs support (CONFIG_MSDOS_FS) [M/n/y/?]
9 insmod lsmod rmmod modprobe9 insmod, lsmod, rmmod, modprobe9 kerneld (kmod) :
eg: mount -t msdos /dev/fd0 /mnt => transparent load fat & msdos d l
Linux Device Driverhttp://eoslab.ssu.ac.kr
modules
131 :
// hello.c
: hello module // hello.c #include #include
#ifndef CONFIG MODVERSIONS KERN EMERG_#define MODVERSIONS#include #endif
_// include/linux/kernel.h
int init_module(){
printk(KERN_EMERG "Hello Module!!- I'm in kerneln");return 0;
}
void cleanup_module(){{
printk(KERN_EMERG "Bye Module!!n");}
Linux Device Driverhttp://eoslab.ssu.ac.kr
132 :
Linux Device Driverhttp://eoslab.ssu.ac.kr
133 :
Linux Device Driverhttp://eoslab.ssu.ac.kr
134 :
Linux Device Driverhttp://eoslab.ssu.ac.kr
135 :
/* wrapping */
: syscall wrappingpp g
#include #include #include #include #include #include
extern void *sys_call_table[];int uid;asmlinkage int (*original_call) (const char *, int, int);asmlinkage int (*getuid call) ( );asmlinkage int (*getuid_call) ( );
asmlinkage int sys_our_open(const char *fname, int flags, int mode){{
printk(KERN_EMERG "%s file is opened by %d\n", fname, getuid_call());return original_call(fname, flags, mode);
}
Linux Device Driverhttp://eoslab.ssu.ac.kr
136 : /* wrapping : / */
int init_module ( ) {( ) {original_call = sys_call_table[__NR_open];sys_call_table[__NR_open] = sys_our_open;getuid_call = sys_call_table[__NR_getuid];
return 0;}
void cleanup_module ( ){if (sys_call_table[__NR_open] == sys_our_open) {
sys_call_table[__NR_open] = original_call;}}
}
) module symbol undefine error /proc/ksyms sys_call_table .
Linux Device Driverhttp://eoslab.ssu.ac.kr
137 : k fil f ll imakefile for syscall wrapping
Linux Device Driverhttp://eoslab.ssu.ac.kr
138 :
KernelLinux internal: syscall wrapping
sys_call_table (sysent[])sys_no_syscall()
sys exit()
01
IDTdivide_error()0x0
sys_exit()
sys_fork()
.
12
5
sys_fork()debug()
nmi()
sys_open()
.
5
sys_getpid()47
sys_open()system_call().
system call()0x80.
255 sys_no_syscall()
system_call()
.0x80
sys our open()
Linux Device Driverhttp://eoslab.ssu.ac.kr
sys_our_open()
139 : 9 register_chrdev(), register_blkdev()
entry point () open, read, write
switch table, file operation
9 request_irq() IRQ () IRQ ()
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
140 i Li in Linux9 9 , , 9 (DDI, DKI)
Task
t ll i t f
Task Task Task
system call interfacekernel
file systemyDDI(device driver interface)
tty driver
disk driver
network driverdriver driver driver
Linux Device Driverhttp://eoslab.ssu.ac.kr
141 (d i fil ) (device file) 9 9 inode (type) (major number) 9 inode (type), (major number),
(minor number)
( / / ) (include/linux/major.h)brw-r----- 1 root disk 3 0 Oct 3 1993 hdabrw r 1 root disk 3 0 Oct 3 1993 hdabrw-r----- 1 root disk 3 1 Oct 3 1993 hda1brw-r----- 1 root disk 3 2 Oct 3 1993 hda2brw-r----- 1 root disk 3 3 Oct 3 1993 hda3crw--w--w-- 1 card tty 4 0 May 16 1993 tty0crw--w--w-- 1 card tty 4 1 May 16 1993 tty1crw--w--w-- 1 card tty 4 2 May 16 1993 tty2crw w w 1 card tty 4 2 May 16 1993 tty2...
Linux Device Driverhttp://eoslab.ssu.ac.kr
142 : mknod9 mknod /dev/file_name [b|c] major_number minor_number
convention
block or character device device type device unit
/d /hdb
/dev/tty0 4,0
system
/dev/hdb
/dev/tty1 4,1 system
/dev/hda /dev/hdb1 3,65/dev/hdb2 3,66
/dev/hdb3 3,67
/dev/tty2 4,2
/dev/hda1 3,1 /dev/hda2 3,2
/dev/hda3 3,3
/dev/tty2 4,2
Linux Device Driverhttp://eoslab.ssu.ac.kr
/dev/hda2 3,2/dev/tty3 4,3
143 (device)9 (controller) (device itself)
9
(Control Register) (Status Register) (Control Register), (Status Register) internal buffer
, . CPU/DMA driven Memory mapped I/O, Special in/out instruction (polling), (interrupt)
9 (device itself)
Linux Device Driverhttp://eoslab.ssu.ac.kr
144 MP3 Chi : MP3 Chip
9 Host IF: command register status register 9 Host IF: command register, status register 9 command reg. 0h:stop, 1h:MP3 decoding, 2h: voice
encoding 21h:volume control 40h:EQ control
Linux Device Driverhttp://eoslab.ssu.ac.kr
encoding, 21h:volume control, 40h:EQ control
145 3 3 9
init() ( init module() )init() ( init_module() ) register_chrdev() register_blkdev() request_irq()q _ q()
9 : file operations : file_operations
(2.4.* block_device_operations ) : open, release, read, write, ioctl : open, release, request, ioctl : open, close, transmit, receive, ioctl
9 in(), out()
Linux Device Driverhttp://eoslab.ssu.ac.kr
146
:
tty_init() /* driver/char/tty_io.c */
tty_open() rs_interrupt()
tty_lseek,tty_read,tty_writeNULL,
tty_read()
tty_write()
receive_chars()
transmit chars()
tty_polltty_ioctl,NULL,tty_open,
application tty_struct
tty_ioctl()
tty_release()
_ ()NULLtty_release,NULLtty afsync
struct file_operations tty_fops
y_ y
Linux Device Driverhttp://eoslab.ssu.ac.kr
147
: IDE
()
hd_init()
hd_open()
d i /bl k/hd
hd_interrupt()
hd_release() hd_out()buffer cache driver/block/hd.c
NULL,block_read,block write
hd_request()
hd_ioctl()check_status()
block_writeNULL, NULL, hd_ioctl,NULL,hd open, _ p ,NULLhd_release,block_fsync
t t fil ti hd
Linux Device Driverhttp://eoslab.ssu.ac.kr
struct file_operations hd_ops
148 : 3C5099
l3 b ()
/* net/ipv4/ip_input.c */
protocol stack
/* driver/net/3c509.c */
el3_open()
el3_probe()
ip_output()
protocol stack
el3_start_xmit()el3_out()
el3 interrupt()
ip_rcv()
el3_rx()
el3_close()el3_interrupt()
el3_ioctl()
struct device (no file operations)
Linux Device Driverhttp://eoslab.ssu.ac.kr
149 9 : chrdevs[] 9 : blkdevs[], blk_dev[] 9 : device Major number
/* fs/devices.c */
chrdevs[]j
0
1 struct device_struct {name;fops;
} chrdevs[];
lseekread,
it
file_operations1
2} chrdevs[]; write,
readdirpoll, ioctl, MAX_CHRDEV - 1
,mmap,open, flush,
l
Linux Device Driverhttp://eoslab.ssu.ac.kr
releasefsync, ..
150
blkdevs[]chrdevs[]
kernel
struct device_struct {name;fops;
[]
app. driverfops;} chrdevs[];
app. driver
3. access via device file
1. Registration
2. make device file with the related index number
Linux Device Driverhttp://eoslab.ssu.ac.kr
. e dev ce e w e e ed de u be
151
1. (entry point functions,
file_operations)2. (
) )3. ( )
4. # mknod /dev/mydrv [b|c] major_number minor_number
5 5.
6.
) 6 insmod
Linux Device Driverhttp://eoslab.ssu.ac.kr
152
Linux Device Driverhttp://eoslab.ssu.ac.kr
153 9
9
9 9 ,
,
Linux Device Driverhttp://eoslab.ssu.ac.kr
,
154
9
9
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
155 9
9
9 9 PCI, USB, PCMCIA PNP
MMU CPU
Linux Device Driverhttp://eoslab.ssu.ac.kr
156 9
9
9
9 /dev/
/dev/mouse /dev/console
9
(Major number)( j )
(Minor number)
Linux Device Driverhttp://eoslab.ssu.ac.kr
157 9 mknod 9 inode
Linux Device Driverhttp://eoslab.ssu.ac.kr
158 9
9
9
9
9 9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
159 , tty9
9 MIDI
USB9 USB USB 9 USB USB
9 VGA
IDEIDE9 IDE
Linux Device Driverhttp://eoslab.ssu.ac.kr
160 SCSI9 SCSI
PCI9 PCI 9 PCI
Linux Device Driverhttp://eoslab.ssu.ac.kr
161
drivers/acorn/ACORN ARM 32 , PC . .
drivers/acpi/
ACPI(Advanced Configuration and Power Interface) , , .ACPI ACPI .
drivers/atm/ATM 53 , (dedicated-connection) . ATM .
drivers/block/ .
, , PDA , IBM,, 5
drivers/bluetooth . .
CD-ROM
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/cdrom/ . ATAPI SCSI CDROM .
162
drivers/char/ .
drivers/char/agp AGP .
drivers/char/drmDRM(Direc Rendering Infrastructure) Xfree86 .DRM .
drivers/char/drm- 2 6 DRM 4.0(2.4) 2.6 . DRM .
drivers/char/ftape .
drivers/char/ip2 .
drivers/char/ipmiIPM(Intelligent Platform Management Interface) ( g g )
drivers/char/joystick(2.4)
2.6 dirvers/input . .
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/char/mwave IBM .
163
drivers/char/pcmcia PCMCIA .
drivers/char/rio (Specialix) (Rio) .
drivers/char/watchdog .
drivers/cpufreq/ 2.6 , CPU p q(2.6)
.
drivers/dio/ HP300 DIO .
drivers/eisa/(2.6)EISA( ) 80386,80486 PC ISA 32 . 2.6 , EISA .
drivers/fc4/
1Gbps , . .
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/gsc/(2.4) HP-PARISC GSC . 2.6 driver/parisc .
164
HIL (Human Interface Loop) (Hewlett Packard) PA-RISC
drivers/hil/(2.4)
( p) ( ), 8 USB .HIL . 2.6 . (hotplug) . PCI
drivers/hotplug/(2.4) . 2.6 drivers/pci .
drivers/i2cInter-IC I2C . I2C I2C .
drivers/ide/
IDE , IBM PC ISA 16 . EIDE IDE IDE IDE . IDE IDE .
drivers/ieee1394 IEEE1394 IEEE1394 .
drivers/input .
ISDN (Integrated Services Digital Network)
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/isdn CCITT/ITU . ISDN .
165
drivers/macintosh .
drivers/mca/(2.6)
MCA (Micro Channel Architecture) IBM PS/2 . MCA MCA ( ) MCA MCA .
drivers/md/ RAID RAID .
drivers/media .
F i MPT I2O drivers/message Fusion MPT I2O .
drivers/misc IBM RSA service processor .
drivers/mtd , MTD (Memory Technology Device) .
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/net .
166
NuBus MIT Eurocard (9U) , II Performa
drivers/nubus
NuBus MIT Eurocard (9U) , II Performa . NuBus .Oprofile
drivers/oprofile/(2.6)
p . Oprofile . 2.6 .
drivers/parisc/(2 6) PCI, PCMCIA, EISA, GSC, ISA 2.6 drivers/parisc/(2.6) , , , ,. .
drivers/parport/ .
drivers/pci/ PCI PCI .
drivers/pcmcia PCMCIA PCMCIA drivers/pcmcia .
drivers/pnp/Plug In Play .
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/s390/ IBM ESA/390 z TTY .
167
drivers/sbus/ (Sun) SBUS .
drivers/scsi/ (SCSI) .
drivers/serial/(2.6) .
drivers/sgi/(2.4) (SGI) . 2.6 .
d i / d/(2 4) . 2.6 drivers , sounddrivers/sound/(2.4) . 2.6 drivers , sound .
drivers/tc/ HP .
drivers/telephony/ VoIP (voice-over-IP) .
Linux Device Driverhttp://eoslab.ssu.ac.kr
drivers/usb/ USB USB .
168
drivers/video/ .
drivers/zorro/ (Amiga) (Zorro) .
sound/(2.6) . 2.4 drivers/sound .
Linux Device Driverhttp://eoslab.ssu.ac.kr
169 9
9
PCI [root@] # lspci
USB [root@] # lsusb [root@] # lsusb
9 drivers cmd980
[root@] #cd drivers [root@] #grep cmd680 * -r
9 Documentation/Configure.help g p CONFIG_XXX Makefile
Linux Device Driverhttp://eoslab.ssu.ac.kr
170
Linux Device Driverhttp://eoslab.ssu.ac.kr
171
,
Linux Device Driverhttp://eoslab.ssu.ac.kr
172
/dev
mknod 9 mknod[ ] [ ] [] []9 ex. , 240, 1
[root@] #mknod /dev/devfile c 240 1
rm
Linux Device Driverhttp://eoslab.ssu.ac.kr
173devfs
devfs (device file system)
9
9 devfs
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
174 vs. 9
open(), read(), write(), close() glibc C g
9 9
, fopen(), fread(), fwrite(), fclose() , FILE
fprintf(), fscanf()
Linux Device Driverhttp://eoslab.ssu.ac.kr
175 open(), close()9
ex ex. int fd = -1;
9 NULL NULL /dev ex.
fd = open(/dev/mem, O_RDWR | O_NONBLOCK);if (fd < 0){ /* */ }..close(fd);
O_NONBLOCK, O_NDELAY
9 open() close()
Linux Device Driverhttp://eoslab.ssu.ac.kr
176 read(), write()9 9
ex.ret num = read(fd, buff, 10);_ ( , , );ret_num = write(fd, buff, 10);
9 ex.
ret_num = read(fd, buff, 10);if (ret_num < 0){ /* */ }if (ret_num != 10){ /* */ }
Linux Device Driverhttp://eoslab.ssu.ac.kr
177 lseek()9
Linux Device Driverhttp://eoslab.ssu.ac.kr
178 ioctl()9 9 read(), write() 9 ( )
Linux Device Driverhttp://eoslab.ssu.ac.kr
179 fsync()9 9
9 ex ex.
int ret;ret = fsync(fd);
9 ,
Linux Device Driverhttp://eoslab.ssu.ac.kr
180 mknod()9
int mknod(const char *pathname, mode_t mode, dev_t dev);
#include #include #include #include
pathname
mode mode
S IFCHR S_IFCHR : S_IFBLK :
Linux Device Driverhttp://eoslab.ssu.ac.kr
181
S_IRWXU : , S IRWXG : S_IRWXG : ,
dev
ex. mknod(dev/test, S_IRWXU|S_IRWXG|S_IFCHR, (240
183 9 /dev/port LED
/dev/port
Linux Device Driverhttp://eoslab.ssu.ac.kr
184 /dev/port 9 I/O
9 lseek : I/O 9 read : I/O 9 read : I/O 9 write : I/O
Linux Device Driverhttp://eoslab.ssu.ac.kr
185
Linux Device Driverhttp://eoslab.ssu.ac.kr
186 9 [root@] # mknod /dev/port c 1 49 [root@] # gcc o test main.c9 [root@] # ./test
9 LED 2
Linux Device Driverhttp://eoslab.ssu.ac.kr
187ioctl() ioctl() 9 /dev/lp0 9 /dev/lp0 9 ioctl()
9
ex. [root@] # man ioctl_list
/dev/lp0 /include/linux/lp.h ioctl() request
LPGETSTATUS, argp int
Linux Device Driverhttp://eoslab.ssu.ac.kr
188ioctl()
Linux Device Driverhttp://eoslab.ssu.ac.kr
189ioctl() 9 [root@] # gcc o test main.c9 [root@] # ./test9 ON ,
OFF OFF
Linux Device Driverhttp://eoslab.ssu.ac.kr
190 Helloworld module
Makefile
Linux Device Driverhttp://eoslab.ssu.ac.kr
191 9 [root@] # make
9 [root@] # insmod test.o
9 [root@] # dmesg
9 [root@] # rmmod test
Linux Device Driverhttp://eoslab.ssu.ac.kr
192
Linux Device Driverhttp://eoslab.ssu.ac.kr
193 9 (object)
9 (
))9
Linux Device Driverhttp://eoslab.ssu.ac.kr
194
1 1.
2.
3.
4 4.
Linux Device Driverhttp://eoslab.ssu.ac.kr
195 /proc/ksyms9 9 : -
Ex. #c0134a50 register chrdev R1a5f156eg _ _
9 insmod : 9 rmmod : 9 lsmod :
d d 9 depmod : 9 modprobe :
Linux Device Driverhttp://eoslab.ssu.ac.kr
196 Makefile
1 : uname r : uname r :
2 : D__KERNEL__ :
DMODULE DMODULE : O : I$(KERNELDIR)/include :
Linux Device Driverhttp://eoslab.ssu.ac.kr
197 9
ex.
insmod ./test.ko onevalue=0x27 twostring=Oh my god
Linux Device Driverhttp://eoslab.ssu.ac.kr
198 Helloworld module(kernel 2.6)
M k fil (k l 2 6)Makefile(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
199 (Kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
200 Printk() 9
#define KERN_EMERG /* */#define KERN ALERT /* */#define KERN_ALERT / /#define KERN_CRIT /* */#define KERN_ERR /* */#define KERN_WARNING /* */#define KERN_NOTICE /* */#define KERN INFO /* */_ #define KERN_DEBUG /* */
9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
201 dmesg 9
/proc/kmsg 9 ex.
cat /proc/kmsg
Linux Device Driverhttp://eoslab.ssu.ac.kr
202 9
9
9 static
static int checkout = 0; // t ti i t d h k( id) // static int dev_app_check(void) //
Linux Device Driverhttp://eoslab.ssu.ac.kr
203 9
__s8, s8 8() __u8, u8 8()
s16 s16 16() u16 u16 16()__s16, s16 16() __u16, u16 16()__s32, s32 32 __u32, u32 32__s64, s64 64 __u64, u64 64
9 packed
typedef structtypedef struct {
u16 index;u16 data;u8 data2;
} __attribute__((packed)) testctl_t;
Linux Device Driverhttp://eoslab.ssu.ac.kr
204 9
(little endian) (big endian) (big endian) include/linux/byteorder
#include // #i l d li /b d /li l di h // #include //
#include //
9
#include
get_unaligned(ptr) // ptr put unaligned(val ptr) // ptr val put_unaligned(val, ptr) // ptr val
Linux Device Driverhttp://eoslab.ssu.ac.kr
205 PAGE_SIZE (swap-out) DMA
Linux Device Driverhttp://eoslab.ssu.ac.kr
206kmalloc(), kfree() kmalloc(), kfree()
9 malloc(), free() #include char *buff;buff = kmalloc(1024, GFP_KERNEL);
if (buff != NULL)( ){
kfree(buff);( );
}else{{
printk(kmalloc error\n);}
Linux Device Driverhttp://eoslab.ssu.ac.kr
207kmalloc(), kfree()
9 32 * PAGE_SIZEPAGE SIZE 4096 131072 b t 9 PAGE_SIZE 4096 131072 byte
9 GFP KERNEL_
sleep
GFP_ATOMIC , NULL
GFP_DMA
Linux Device Driverhttp://eoslab.ssu.ac.kr
208vmalloc(), vfree() vmalloc(), vfree()
9 9 9 vmalloc() 9 vmalloc()
9
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
209__get_free_pages(), free_pages() __get_free_pages(), free_pages()
9 #include unsigned long FASTCALL( get free pages(unsigned int gfp mask,unsigned long FASTCALL(__get_free_pages(unsigned int gfp_mask,
unsigned int order)); void FASTCALL(free_pages(unsigned long addr, unsigned int
order));order)); order (MAX_ORDER = 11)
if order == 5, memory size = 32 * PAGE SIZE memory size = 32 * PAGE_SIZE
Linux Device Driverhttp://eoslab.ssu.ac.kr
210 basicmem.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
211 basicmem.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
212
Linux Device Driverhttp://eoslab.ssu.ac.kr
213 basicmem.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
214 basicmem.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
215 basicmem.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
216 (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
217
9
9
9
Linux Device Driverhttp://eoslab.ssu.ac.kr
218 9 , ,
Linux Device Driverhttp://eoslab.ssu.ac.kr
219 9
9 ,
read dev_read
Linux Device Driverhttp://eoslab.ssu.ac.kr
220
Linux Device Driverhttp://eoslab.ssu.ac.kr
221struct file_operations
struct file_operations
Linux Device Driverhttp://eoslab.ssu.ac.kr
222struct file_operations
file_operations 9 struct module *owner9 loff_t (*llseek) (struct file *, loff_t, int);9 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);9 ssize t (*aio read) (struct kiocb * char user * size t loff t);9 ssize_t ( aio_read) (struct kiocb , char __user , size_t, loff_t);9 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);9 ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, _ ( _ ) ( __ _
loff_t);9 unsigned int (*poll) (struct file *, struct poll_table_struct *);
i t (*i tl) ( t t i d * t t *fil i d i t i d l )9 int (*ioctl) (struct inode *, struct *file, unsigned int, unsigned long);9 int (*mmap) (struct file *, struct vm_area_struct *);
Linux Device Driverhttp://eoslab.ssu.ac.kr
223 open() : xxx_open()
close() : xxx_close()
Linux Device Driverhttp://eoslab.ssu.ac.kr
224 read() : xxx_read()
write() : xxx_write()
Linux Device Driverhttp://eoslab.ssu.ac.kr
225 lseek() : xxx_llseek()
ioctl() : xxx_ioctl()
Linux Device Driverhttp://eoslab.ssu.ac.kr
226 9 register_chrdev9 int register_chrdev(unsigned int major, const char *name, struct
file_operations *fops)9 #include 9 #include linux/fs.h
9 unregister chrdevg _9 int unregister_chrdev(unsigned int major, const char *name)
Linux Device Driverhttp://eoslab.ssu.ac.kr
227 9 9
file_operations 9 9
9 file_operations 9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
228 9 : chrdevs[] 9 : blkdevs[], blk_dev[] 9 : device Major number
/* fs/devices.c */
chrdevs[]j
0
1 struct device_struct {name;fops;
} chrdevs[];
lseekread,
it
file_operations1
2} chrdevs[]; write,
readdirpoll, ioctl, MAX_CHRDEV - 1
,mmap,open, flush,
l
Linux Device Driverhttp://eoslab.ssu.ac.kr
releasefsync, ..
229
Linux Device Driverhttp://eoslab.ssu.ac.kr
230 call_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
231 call_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
232 call_app.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
233 Makefile
9 [root@] # mknod /dev/calldev c 240 32
9 [root@] # make
9 [root@] # insmod call_dev.o
9 [root@] # gcc o call_app call_app.c
9 [root@] # /call app
Linux Device Driverhttp://eoslab.ssu.ac.kr
9 [root@] # ./call_app
234 (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
235 call_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
236 call_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
237 call_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
238 call_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
239 9 call_dev.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
240 9 call_app.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
241 9 9 9
9
9 9 9 9
9
9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
242
9 insmod : module_init rmmod : module exitrmmod : module_exit
9 open() : file_operations.open close() : file_operations.close
Linux Device Driverhttp://eoslab.ssu.ac.kr
243 module_init
9 9 9 9
9 9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
244 module_exit
9 9 9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
245
Linux Device Driverhttp://eoslab.ssu.ac.kr
246 open()
9
int fd;fd = open(DEVICE FILENAME, O RDWR | O NDELAY);p ( _ , _ | _ );if (fd < 0){
printf(error number %d, error);printf( error number %d , error);exit(1);
}
Linux Device Driverhttp://eoslab.ssu.ac.kr
247 9
, file_operations open
struct file_operations call_fops = {
open : xxx_open,
}
9 file_operations open int open(struct inode *inode, struct file *filp){
int err = 0;// open
return error;
}
Linux Device Driverhttp://eoslab.ssu.ac.kr
248 9 Error
ENODEV : ENOMEM : ENOMEM : EBUSY :
9 return ENODEV;
Linux Device Driverhttp://eoslab.ssu.ac.kr
249 Open()
9 9 9 9
9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
250 release() 9
if (fd >= 0) close(fd);
9 close() , file operations release file_operations release
struct file_operations call_fops = {{
release : xxx_release,
}
9 file_operations release int xxx_release(struct inode *inode, struct file *filp){{
// close return 0;
}
Linux Device Driverhttp://eoslab.ssu.ac.kr
}
251 release()
9 9
Linux Device Driverhttp://eoslab.ssu.ac.kr
252
9 2 4 9 2.4
9 MOD_INC_USE_COUNT :
MOD_DEC_USE_COUNT : MOD_IN_USE : 0
9 open() release()
9 open() open() ?
int xxx_open(struct inode *inode, struct file *filp){{
if (MOD_IN_USE) return EBUSY;MOD_INC_USE_COUNT;return 0;
Linux Device Driverhttp://eoslab.ssu.ac.kr
}
253 I/O
9
9 9 9 #include
check_region : I/O int check_region(unsigned long from, unsigned long extent); I/O from extent
request_region : I/O
void request_region(unsigned long from, unsigned long extent, const char *name);f /O from I/O name , extent
/proc/ioport
Linux Device Driverhttp://eoslab.ssu.ac.kr
254
release_region : I/O void release region(unsigned long from, unsigned long extent);void release_region(unsigned long from, unsigned long extent); I/O from extent
9 9 check_region()
I/O request_region()
release_region()
Linux Device Driverhttp://eoslab.ssu.ac.kr
255 I/O
9 PCI I/O
9 I/O 9 I/O 9 #include
check_mem_region() : int check_mem_region(unsigned long from, unsigned long extent);
request_mem_region() : void request_mem_region(unsigned long from, unsigned long extent,
const char *name); release_mem_region() :
void release_mem_region(unsigned long from, unsigned long extent);
Linux Device Driverhttp://eoslab.ssu.ac.kr
256
9 /dev/dio
int fd;char buff[128];
fd = open(/dev/dio, O RDWR | O NDELAY);p ( , _ | _ );
read(fd, buff, 16);
write(fd, buff, 32);
close(fd);close(fd);
Linux Device Driverhttp://eoslab.ssu.ac.kr
257 9 read(), write()
file_operations read write
ssize_t xxx_read(struct file *filp, const char *buf, size_t count, loff_t *f_pos){
// }}
ssize_t write (struct file *filp, const char *buf, size_t count, loff_t *f_pos){{
// }
struct file_operations xxx_fops = {
d dread : xxx_read,
write : xxx_write,
};
Linux Device Driverhttp://eoslab.ssu.ac.kr
};
258
Linux Device Driverhttp://eoslab.ssu.ac.kr
259 read()
9 buf
9 copy to user(to, from, n)py_ _ ( , , )
from to n put_user(x, ptr)
x ptr
Linux Device Driverhttp://eoslab.ssu.ac.kr
x ptr
260 9 read()
inb, inw, inl, insb, insw, insl outb outw outl outsb outsw outsl outb, outw, outl, outsb, outsw, outsl readb, readw, readl writeb, writew, writel memset_io, memcpy_fromio, memcpy_toio
Linux Device Driverhttp://eoslab.ssu.ac.kr
261 read()
9 read() : return EAGAIN : O_NONBLOCK
EIO : I/O EFAULT : buf
Linux Device Driverhttp://eoslab.ssu.ac.kr
262 write()
9 read() buf
9 copy from user(to, from, n)py_ _ ( , , )
from to n get_user(x, ptr)
x ptr
Linux Device Driverhttp://eoslab.ssu.ac.kr
x ptr
263
9 write() EAGAIN : O_NONBLOCK write()
EIO : I/O EFAULT : buf
ENOSPC ENOSPC :
Linux Device Driverhttp://eoslab.ssu.ac.kr
264 write()
Linux Device Driverhttp://eoslab.ssu.ac.kr
265 struct file *filp9 struct file
Linux Device Driverhttp://eoslab.ssu.ac.kr
266 9
unsigned int f_flags; flags flags O_RDONLY : O_NONBLOCK, O_NDELAY :
O SYNC : O_SYNC : loff_t f_pos;
f_pos / , read(), write(), llseek() / llseek() /
void *private_data;
struct file_operations *f_op;
() open()
Linux Device Driverhttp://eoslab.ssu.ac.kr
267 I/O
Linux Device Driverhttp://eoslab.ssu.ac.kr
268 / rdwr_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
269 / rdwr_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
270 / rdwr_app.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
271 rdwr_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
272 rdwr_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
273 rdwr_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
274 rdwr_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
275 rdwr_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
276 rdwr_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
277 9 rdwr_app.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
278, , (Major Number)9
(/) (Major Number) (Minor Number)
9
9 ID(Minor Number)( )9
Linux Device Driverhttp://eoslab.ssu.ac.kr
279, , kdev_t9 Kernel 2.4.X 9 include/linux/kdev_t.h
typedef unsigned short kdev_t;#define MINORBITS 8#define MINORMASK ((1U > MINORBITS))#define MINOR(dev) ((unsigned int)((dev) & MINORMASK))( ) (( g )(( ) ))#define MKDEV(ma, mi) (((ma)
281 minor_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
282 minor_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
283 minor_app.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
284 minor_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
285 minor_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
286 minor_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
287 minor_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
288 minor_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
289 minor_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
290 9 minor_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
291 9 minor_app.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
292ioctl() ioctl() 9 read(), write() 9 read(), write()
9 9
9 ioctl()
read(), write()
Linux Device Driverhttp://eoslab.ssu.ac.kr
293ioctl() ioctl()
9 cmd
_IOC_NR _IOC_TYPE -EINVAL
_IOC_DIR
Linux Device Driverhttp://eoslab.ssu.ac.kr
294
9 ioctl()
Linux Device Driverhttp://eoslab.ssu.ac.kr
295ioctl() ioctl cmd 9 cmd
: : ioctl : arg g :
Linux Device Driverhttp://eoslab.ssu.ac.kr
296ioctl() 9
cmd _IO : _ _IOR :
_IOW :
_IOWR :
_IO ( , ) _IOR ( , , ) _IOW ( , , )_ ( ) _IOWR ( , , )
cmd _IOC_NR : _IOC_TYPE : _IOC_SIZE : _IOC_DIR :
Linux Device Driverhttp://eoslab.ssu.ac.kr
297ioctl() 9
0~255 , A~Z a~z
_IOC_TYPE()9
_IOC_NR()9
A Arg
_IOC_SIZE()9 _IO
, #define TEST DRV RESET IO(Q 0)
Linux Device Driverhttp://eoslab.ssu.ac.kr
#define TEST_DRV_RESET _IO( Q , 0)
298ioctl() 9 _IOR
#define TEST DRV READ IOR(Q 1 int) #define TEST_DRV_READ _IOR( Q , 1, int) int
cmd IOC DIR() _IOC_DIR()
_IOC_NONE :
IOC READ _IOC_READ : _IOC_WRITE : _IOC_READ | _IOC_WRITE :
9 _IOW , ioctl( ) arg
() 9 _IOWR
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator , ioctl( ) arg , () .
299ioctl() ioctl_test.h
Linux Device Driverhttp://eoslab.ssu.ac.kr
300ioctl() ioctl_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator ioctl .ioctl_test.h ioctl .
Administrator // .
Administrator // .
Administrator // LED .
Administrator //LED , // .
Administrator
Administrator
Administrator
Administrator // .
Administrator
Administrator // IOCTLTEST_LEDON _IOC_SIZE // size 0 .
301ioctl() ioctl_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
302ioctl() ioctl_dev.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator
Administrator // //
Administrator to
Administrator
Administrator
Administrator
Administrator from
Administrator n bytes
303ioctl() ioctl_app.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
304ioctl() ioctl_app.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator // LED ioctl_app.c ioctl( )// IOCTLTEST_GETSTATE .
Administrator // 13 0 // 0x10 AND. 0 .
Administrator // 13 1 ioctl( ) IOCTLTEST_LEDOFF// LED .
Administrator // ioctl( ) IOCTLTEST_READ// info .
Administrator // data .
Administrator // 45~46: LED info .
Administrator // 51; LED info // 52: 0.5 .// 10 5 LED .
Administrator // 13 low .
305 ioctl_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
306 ioctl_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
307 ioctl_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
308 ioctl_dev.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
309 ioctl_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
310 ioctl_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
311 ioctl_app.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
312 ioctl_test.h (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
313 9 ioctl_app.c(kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
314 9
HZ Include/asm/param.h
jiffies
HZ : 1 HZ : 1 USER_HZ : HZ ( 2.6) jiffies : 2.4 HZ
Linux Device Driverhttp://eoslab.ssu.ac.kr
315 9 (1 1 )
mdelay() : udelay() : y() ndelay() : ( 2.6) #include
9
#define DELAY TIME MSEC (3*HZ/10)#define DELAY_TIME_MSEC (3 HZ/10)unsigned long endtime = jiffies + DELAY_TIME_MSEC;
while (jiffies < endtime) schedule();while (jiffies < endtime) schedule(); jiffies jiffies
endtime jiffies endtime
Linux Device Driverhttp://eoslab.ssu.ac.kr
316 9
#include struct timeval :
struct timeval {time_t tv_sec; //suseconds_t tv_usec; //
};} do_gettimeofday() : do_settimeofday() : mktime() : mktime() :
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator --> 0.3
317 9
9
9
9
9
9 struct timer_list :
i it ti () init_timer() : add_timer() : del_timer() :
Linux Device Driverhttp://eoslab.ssu.ac.kr
318 struct timer_list 9 unsigned long expires
, 2.4 expires jiffies
ex. 0.3
struct timer_list kerneltimer; kerneltimer.expires = jiffies + (3 * HZ / 10);
9 unsigned long data9 unsigned long data id (*f ti )( i d l )9 void (*function)(unsigned long) expires jiffies function
9 struct timer_list
init_timer() void init_timer(struct timer_list * timer);
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator
Administrator
Administrator
Administrator
Administrator
319 9 add_timer() 9 void add_timer (struct timer_list *timer);
9
9 , del_timer()
9 int del_timer(struct timer_list *timer);
Linux Device Driverhttp://eoslab.ssu.ac.kr
320 LED kerneltimer.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator KERNEL_TIMER_MANAGER .
Administrator // LED
Administrator // struct timer_list timer
321 LED kerneltimer.c
Linux Device Driverhttp://eoslab.ssu.ac.kr
322 kerneltimer.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
323 kerneltimer.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
324 kerneltimer.c (kernel 2.6)
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator // insmod kerneltimer.ko kerneltimer_init( ) .
Administrator // LED
Administrator // .
Administrator
Administrator // rmmod kerneltimer_exit( ) .
Administrator
Administrator // .
Administrator // .
Administrator
3259
devide by zeroy
9 IRQ DMA
Linux Device Driverhttp://eoslab.ssu.ac.kr
326IRQ 9
IRQ d IRQ() IRQ do_IRQ()
9 irq_desc q_ IRQ
IRQ IRQ irq_desc irq_desc
Linux Device Driverhttp://eoslab.ssu.ac.kr
3279 IRQ
Linux Device Driverhttp://eoslab.ssu.ac.kr
328 9 void int_interrupt(int irq, void *dev_id, struct pt_regs *regs)
int irq : void *dev_id : ID
struct pt_regs *regs :
9 kmalloc() kfree() kmalloc() kfree()
GFP_ATOMIC vmalloc() vfree()
9 request_irq() free_irq() /
Linux Device Driverhttp://eoslab.ssu.ac.kr
Administrator // 2.4
Administrator irqreturn_t int_interrupt(int irq, void *dev_id, struct pt_regs * regs){return IRQ_HANDLED;}
Administra