ISLab Flash Team Ch 8 Interrupt IOWindows 2000
ISLab Flash Team Ch 8. Interrupt I/OWindows 2000 Device Driver
Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. Programed I/O 동작 방식 Driver Initialization and Cleanup Start I/O Routine의 작성 Interrupt Service Routine의 작성 Dpc. For. Isr Routine의 작성 Hardware Example : Parallel Port Example Code : Parallel Port Loopback Driver Testing The Parallel Port Loopback Driver Summary ISLab Flash Team Ch 8. Interrupt I/O 2 Made By Hackereyes
ISLab Flash Team 1. Programed I/O 동작 방식 Ch 8. Interrupt I/OWindows 2000 Device Driver
Programmed I/O 동작(1) 1. IRP는 I/O Request처리를 위해 Device와 Data전송이 필요한지 판단 • Data 전송이 필요하면, Start I/O Routine에 전달하기 위한 IRP 를 Queuing 함 2. Start I/O Routine은 IRP의 Function Code에 기초해 전처리 작업과 설정작업 수행 후, I/O Process를 진행 3. 수행후, Device는 Interrupt 발생, Kernel은 발생된 Interrupt를 Driver의 Interrupt Service Routine으로전달 4. Device가 더 전송을 받아야 하면 ISR은 다음 전송 시작 모든 Data 전송이 끝날때 까지 3~4 지속적 반복 ISLab Flash Team Ch 8. Interrupt I/O 4 Made By Hackereyes
Programmed I/O 동작(2) 5. 모든 전송이 끝나면, ISR은 Dpc. For. Isr Routine을 시작 하라는 Request를 DPC Queue에 삽입 • DPC Routine은 ISR보다 낮은 IRQL에서 실행 6. I/O Manager의 DPC dispatcher는 ISR에 의해 예정된 Dpc. For. Isr Routine을 실행 • • Dpc. For. Isr Routine은 IRP의 완료를 표시 Queuing된 IRP를 Start I/O Routine에서 처리하도록 I/O Manager에게 통보 ISLab Flash Team Ch 8. Interrupt I/O 5 Made By Hackereyes
Synchronization of Driver Routine ISR Start I/O Dpc. For. Isr I/O Request ISLab Flash Team Ch 8. Interrupt I/O 6 Made By Hackereyes
Synchronization of Driver Routine(3) • IRQL 기법에서는 Memory공유등의 상호배재를 해결해야함 • Synch. Crit. Section Routine을 사용해 자원접근을 함 BOOLEAN Synch. Crit. Section IRQL == DIRQL Parameter Contents IN PVOID p. Context Ke. Synchronize. Execution에 전달되는 컨텍스트 Return Value • TRUE • FALSE Success Fail(오류발생) ISLab Flash Team Ch 8. Interrupt I/O 7 Made By Hackereyes
Synchronization of Driver Routine(4) • Ke. Synchronize. Excution IRQL을 DIRQL까지 올리고, Spin Lock을 획득 • Synch. Crit. Section Routine을 호출함 BOOLEAN Ke. Synchronize. Execution IRQL < DIRQL Parameter Contents IN PKINTERRUPT p. Interrupt. Obj Pointer for Interrupt Object IN PKSYNCHRONIZE_ROUTINE p. Routine Call Back Routine of Synch. Crite. Section IN PVOID p. Context to Synch. Crit. Section Return Value Synch. Crit. Section 의 Return Value ISLab Flash Team Ch 8. Interrupt I/O 8 Made By Hackereyes
Synchronization of Driver Routine(5) DIRQL 자원의 일시적인 Synch. Crit. Section 독점 가능 Context와 Routine add 전달 Spin. Lock 획득 < DIRQL Ke. Synchronize. Execution IRQL 상승 ISLab Flash Team Ch 8. Interrupt I/O 9 Made By Hackereyes
Synchronization of Driver Routine(6) DIRQL Synch. Crit. Section Return Value Spin. Lock 반환 < DIRQL Ke. Synchronize. Execution IRQL 복귀 ISLab Flash Team Ch 8. Interrupt I/O 10 Made By Hackereyes
ISLab Flash Team 2. Driver Initialization and Cleanup Ch 8. Interrupt I/OWindows 2000 Device Driver
Initialize Start I/O Routine NTSTATUS Driver. Entry(… … …) { : p. DO->Major. Function[ IRP_MJ_CREATE ] = Disp. Create; : p. DO->Major. Function[ IRP_MJ_WRITE ] = Disp. Write; : } NTSTATUS Driver. Entry(… … …) { : p. DO->Friver. Start. IO = Start. Io; p. DO->Friver. Unload = Driver. Unload; : p. DO->Major. Function[ IRP_MJ_WRITE ] = Disp. Write; : } • Initialization 해주지 않으면 Io. Start. Packet 호출시 접근위반 Error ISLab Flash Team Ch 8. Interrupt I/O 12 Made By Hackereyes
Initialize Dpc. For. Isr Routine • DPC Object와 각 Device Object를 연결시킬수 있음 – Dpc. For. Isr • Dpc. For. Isr 과 Device Object와 연관지을수 있음 – Io. Initialize. Dpc. Request 호출 – 보통은 Driver. Entry or Add. Device에서 해줌 • ISR Routine에서 Interrupt를 처리하는 동안 DPC를 Scheduling 함 – Io. Request. Dpc 호출 ISLab Flash Team Ch 8. Interrupt I/O 13 Made By Hackereyes
Linking Interrupt Source to Kernel(1) • 실제 Interrupt Routine Address(ISR)를 Kernel에게 알림 • I/O Manager가 제공 – Io. Connect. Interrupt Function • ISR의 Address를 Parameter로 전달받음 • Interrupt Object를 Parameter로 전달받음 ISLab Flash Team Ch 8. Interrupt I/O 14 Made By Hackereyes
Linking Interrupt Source to Kernel(2) • Interrupt Object 사용시 주의점 – ISR Routine이 하나이상의 Interrupt Vecter를 처리하거나 Driver가 하나 이상의 ISR을 가지고 있을 시 • Service Context 충돌을 피하기 위해 Spin Lock 사용 • Synchronize. Irql에 지정된 DIRQL Value는 Interrupt Vector가 가지 는 DIRQL Value보다 커야함 – Driver Interrupt Routine은 Io. Connect. Interrupt 호출시 바로 실 행가능한 상태로 준비되어 있어야함 • Io. Connect. Interrupt에서 지정된 IRQL에서 발생된 Interrupt는 Driver 에 의해 수행되는 추가적인 초기화 작업을 선점할수도 있음 • ISR은 이러한 Interrupt를 제 때 처리할수 있어야 함 ISLab Flash Team Ch 8. Interrupt I/O 15 Made By Hackereyes
Linking Interrupt Source to Kernel(3) 1. Io. Initialize. Dpc. Request 호출후 Dpc. For. Isr Routine 실행을 위한 초 기화 작업 수행 2. Device에서 Interrupt가 발생하지 않게 함 • • Device에 적절한 Bit를 설정함 Control register를 설정함 3. ISR에서 요구하는 Driver의 초기화 수행 4. Io. Connect. Interrupt를 호출 • • ISR과 Interrupt Source를 연결 Interrupt Object 주소를 Device Extension에 저장 5. Synch. Crit. Section Routine을 사용해 Device를 초기화 상태로 바꿈 , Device가 Interrupt를 발생하게 함 ISLab Flash Team Ch 8. Interrupt I/O 16 Made By Hackereyes
Unlinking Interrupt Source to Kernel • Driver가 Memory에서 Unload될 가능성이 있다면 – Memory에서 Unload 전에 Kernel과 Interrupt Souce연결 해제 – 만약 Unload 후에도 연결이 되어있다면, Device가 Interrupt 발 생시 OS가 멈춤 • 두가지 절차를 따름 1. Ke. Synchronize. Execution Function과 Synch. Crit. Section Function을 사용해 Device를 정지 시킴. • Device에서 Interrupt 발생 불가 2. Io. Disconnect. Interrupt Function 사용 • Device Interrupt Object를 Parameter로 넘겨주어 Kernel의 Interrupt Service Routine 목록에서 ISR 제거 ISLab Flash Team Ch 8. Interrupt I/O 17 Made By Hackereyes
ISLab Flash Team 3. Start I/O Routine의 작성 Parallel Port 에서 사용되는 Programmed I/O Driver 개발에 관련 Ch 8. Interrupt I/OWindows 2000 Device Driver
Execution Context • I/O Manager가 Start I/O 호출 • DISPATCH_LEVEL_IRQL에서 실행됨 – Paged Resource에 접근해 Page Fault를 발생하면 안됨 VOID Start. Io IRQL == DISPATCH_LEVEL Parameter Contents IN PDEVICE_OBJECT p. Dev. Obj Request 를 위한 Target Device IN PIRP p. Irp Request를 설명하는 IRP Return Value void - Start. Io Routine ISLab Flash Team Ch 8. Interrupt I/O 19 Made By Hackereyes
Start I/O Routine에서 하는 작업 1. 현재 IRP의 Stack Location Pointer를 얻음 1. Io. Get. Current. Irp. Stack. Location 호출 2. I/O Stack Location의 Major. Funtion Field를 확인하여 작업을 선택함 3. IRP에 저장된 Bytes수와 System buffer Pointer를 저장 1. Device Extension에 저장하면 좋음 4. Device Extension에 Interrupt 발생 예상 Flag를 설정 5. Device를 구동시킴 ISLab Flash Team Ch 8. Interrupt I/O 20 Made By Hackereyes
ISLab Flash Team 4. Interrupt Service Routine의 작성 Ch 8. Interrupt I/OWindows 2000 Device Driver
Execution Context • Interrupt DIRQL과 관계된 Interrupt Object를 조사해 ISR을 찾아서 호출 • Kernel Interrupt dispatcher는 ISR을 호출 – 호출된 ISR은 Io. Connect. Interrupt를 호출할때 지정되었던 IRQL 에서 수행 • ISR에서 해서는 안되는 작업(높은 IRQL때문에) – Page Fault 를 발생해서는 안됨 – System Resource를 할당, 해제하려하면 안됨(ex. Memory) • 구지 해야한다면, DPC Routine에게 위임 ISLab Flash Team Ch 8. Interrupt I/O 22 Made By Hackereyes
Interrupt Service Routine에서 하는 작업 1. 발생된 Interrupt가 해당 Driver에 관련된것인지 확인 2. Interrupt를 인식한 Device에 필요한 작업 요청 3. 전송할 Data가 더 있으면, 다음 작업 시작 • 결국 다른 Interrupt를 발생시키는 결과 4. Data 전송이 완료되거나 Error발생시 Io. Request. Dpc를 호출하여 DPC Request를 Queuing 함 5. TRUE Return ISLab Flash Team Ch 8. Interrupt I/O 23 Made By Hackereyes
ISLab Flash Team 5. Dpc. For. Isr Routine의 작성 Ch 8. Interrupt I/OWindows 2000 Device Driver
Execution Context(1) • Driver에서의 Dpc. For. Isr Routine의 임무 – 현재 IRP의 최종상태를 확인 – IRP를 완료후 다음 IRP 시작 • ISR이 Io. Request. Dpc를 호출하면 Dpc. For. Isr Routine은 DPC Queue에 Queuing 됨 • Dcp. For. Isr Routine은 DISPATCH_LEVEL_IRQL 수준에서 실행 – Paging되는 주소에 접근할수 없다는 의미가 됨 • I/O Manager는 Dpc. For. Isr이 실행될때까지 주어진 Device에 대한 DPC Request(Io. Request. Dpc)를 무시 ISLab Flash Team Ch 8. Interrupt I/O 25 Made By Hackereyes
Execution Context(2) VOID Dpc. For. Isr IRQL == DISPATCH_LEVEL Parameter Contents IN PKDPC p. Dpc 이 호출에 대한 DPC IN PDEVICE_OBJECT p. Dev. Obj I/O Request를 위한 Target Device IN PIRP p. Irp 이 Request를 설명하는 IRP In PVOID p. Context Io. Request. Dpc에 전달되는 Context Return Value Void - Dpc. For. Isr Routine - ISLab Flash Team Ch 8. Interrupt I/O 26 Made By Hackereyes
Dpc. For. Isr Routine에서 처리하는 작업 1. IRP의 I/O 상태를 설정, Status 변수 설정, Information 변수에 실제 전송된 Bytes수를 설정 2. 적절한 우선순위값 설정후 Io. Complate. Request 호출 • • IRP를 완료하게 됨 Io. Complate. Request가 호출되면 IRP에서는 어떤작업도 다시 수 행해선 안됨 3. Start I/O Routine에 다음 IRP를 전달하기 위해 Io. Start. Next. Packet을 호출 ISLab Flash Team Ch 8. Interrupt I/O 27 Made By Hackereyes
우선순위 증가 • Windows 2000의 Thread Manager는 효율적 운영을 위 해 우선순위값 사용 • Starvation 방지를 위해 우선순위를 증가시켜줌 – Io. Complate. Request 호출시 Parameter로 넘겨줌 – Mouse나 Keyboard같은 Device에 우선순위를 중시하여 가중치 를줌 ISLab Flash Team Ch 8. Interrupt I/O 28 Made By Hackereyes
ISLab Flash Team 6. Hardware Example : Parallel Port Ch 8. Interrupt I/OWindows 2000 Device Driver
Parallel Port • RESET – Device를 초기화 시키고 싶을때 CPU가 이 Line을 통해 전송 • DATA – CPU가 정보를 parallel 하게 전송할수 있는 Line • STROBE# – CPU가 DATA Line에 보낼 정보가 있다는 신호를 보냄 ISLab Flash Team Ch 8. Interrupt I/O 30 Made By Hackereyes
Parallel Port • BUSY – CPU에게 DATA를 받을수 없다는 것을 Device가 알림 • ACK# – Device가 BUSY 상태를 벗어났음을 알림 • Errors – Device가 CPU에게 준비상태 or Error상태를 알려지기 위한 몇 개의 Line ISLab Flash Team Ch 8. Interrupt I/O 31 Made By Hackereyes
Device Register • 세개의 Register를 통해 정보를 주고 받음 – Data Register – Status Register – Control Register • Parallel Port 는 자동 설정을 지원함 – Configuration Manager가 Data Register의 기본주소를 찾아줌 ISLab Flash Team Ch 8. Interrupt I/O 33 Made By Hackereyes
ISLab Flash Team 7. Example Code (Parallel Port Loopback Driver) Ch 8. Interrupt I/OWindows 2000 Device Driver
Purpose of this Driver • Loopback Connector가 부착된 Parallel Port에 4 Bits를 전송함 • Loopback Connector에 의해 Return 된 Data는 Driver 의 임시 Buffer에 저장 • 때문에 읽기 작업은 전송한 4 Bits와 동일함 • 추가로 4 Bits를 Left Shift 한 상태로 Return 함 ISLab Flash Team Ch 8. Interrupt I/O 36 Made By Hackereyes
Driver. h typedef struct _DEVICE_EXTENTION { PDEVICE_OBJECT p. Device; ULONG Device. Number; CUString ustr. Device. Name; // 내부 Device Name CUString ustr. Sym. Link. Name; // Symbolic. Link. Name PUCHAR device. Buffer; // 임시 Buffer ULONG device. Buffer. Size; ULONG xfer. Count; // 현재 전송 Count ULONG max. Xfer. Count; // 요청된 전송 Count ULONG port. Base; // I/O register Addr ULONG Irq; // Parallel Port IRQPKINTERRUPT p. Int. Obj; // Interrupt Object } DEVICE_EXTENSION, *PDEVICE_EXTENSION ISLab Flash Team Ch 8. Interrupt I/O 37 Made By Hackereyes
Driver. cpp(1) • CREATEDEVICE – Pn. P 이전에 연결된 Device를 인식하기 위한 역할 – Claim. Resources Function을 사용 • DISPATCHWRITE – Start I/O Routine에 진입할 IRP를 순서대로 대기열에 삽입 • DISPATCHREAD – Device Buffer의 Data를 사용자 Buffer에 Return ISLab Flash Team Ch 8. Interrupt I/O 38 Made By Hackereyes
Driver. cpp(2) • STARTIO – IRP가 대기열에서 빠져나올때 마다 I/O Manager에 의해 호출 – Dispatch. Write에서 시작된 작업을 완결시킴 • ISR – 사용자의 출력 buffer에 마지막 Bytes가 Printer Port에 전송되면 Dpc. For. Isr Routine을 사용해 IRP를 완료 • DPCFORISR – ISR의 Request에 따라 I/O Request를 완료 ISLab Flash Team Ch 8. Interrupt I/O 39 Made By Hackereyes
ISLab Flash Team 8. Testing The Parallel Port Loopback Driver Ch 8. Interrupt I/OWindows 2000 Device Driver
Conclusion of Testing • IRP를 dispatch Routine에서 Start I/O Routine으로 전송 • Device Interrupt에 반응 • Data를 성공적으로 전송 • Request를 완료 • Multiple User로 부터 Request를 관리 ISLab Flash Team Ch 8. Interrupt I/O 41 Made By Hackereyes
Test Program 작성 절차 1. 각각의 IRP가 도착하자마자 완료되는 단순한 Start I/O Routine을 작성 • Driver의dispatch Routine과 Start I/O Routine사이의 연결확인 2. 실제 Start I/O, ISR 그리고 Dpc. For. Isr Routine 작성 • Driver가 Read, Write를 모두 지원한다면 두 작업을 나누어 구 현해 Test 3. Driver의 모든 Data전송 경로 Test • Read. File , Write. File 그리고 Device. Io. Control을 호출하는 간단 한 Win 32 프로그램을 사용 4. 최대한 빨리 많은 수의 I/O Request를 발생시키는 Program을 이용해 Driver의 극단적인 상황을 Test ISLab Flash Team Ch 8. Interrupt I/O 42 Made By Hackereyes
Test Program 작성 절차 5. 한번에 여러 개의 Test Program을 실행 • Device가 여러 Program에서 공유된다면 다수의 Handle에서 작 동이 원활해야함 6. Driver가 다수의 Physical Device를 지원한다면 각 Device와 Test를 반복 7. Multiprocessor 환경에서 4~6과정 반복 • SMP 동기화를 검증하기 위함 ISLab Flash Team Ch 8. Interrupt I/O 43 Made By Hackereyes
ISLab Flash Team Summary Ch 8. Interrupt I/OWindows 2000 Device Driver
Summary • 실제로 동작하는 Driver의 기본 골격을 제시 • Start I/O Routine은 각각의 Request을 초기화하고 ISR 에서 Interrupt를 처리 • Dpc. For. Isr Routine은 Device의 DIRQL이하에서 IRP를 올바르게 완료 ISLab Flash Team Ch 8. Interrupt I/O 45 Made By Hackereyes
- Slides: 45