45
ISLab Flash Team ISLab Flash Team Ch 8. Interrupt I/O Ch 8. Interrupt I/O

Ch 8. Interrupt I/O

  • Upload
    kynan

  • View
    59

  • Download
    3

Embed Size (px)

DESCRIPTION

Ch 8. Interrupt I/O. Contents. Programed I/O 동작 방식 Driver Initialization and Cleanup Start I/O Routine 의 작성 Interrupt Service Routine 의 작성 DpcForIsr Routine 의 작성 Hardware Example : Parallel Port Example Code : Parallel Port Loopback Driver Testing The Parallel Port Loopback Driver - PowerPoint PPT Presentation

Citation preview

Page 1: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

Ch 8. Interrupt I/OCh 8. Interrupt I/O

Page 2: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 2

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

ContentsContents

1. Programed I/O 동작 방식2. Driver Initialization and Cleanup3. Start I/O Routine 의 작성4. Interrupt Service Routine 의 작성5. DpcForIsr Routine 의 작성6. Hardware Example : Parallel Port7. Example Code : Parallel Port Loopback Driver8. Testing The Parallel Port Loopback Driver9. Summary

Page 3: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

1. Programed I/O 1. Programed I/O 동작 방식동작 방식

Page 4: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 4

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Programmed I/O Programmed I/O 동작동작 (1)(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 지속적 반복

Page 5: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 5

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Programmed I/O Programmed I/O 동작동작 (2)(2)5. 모든 전송이 끝나면 , ISR 은 DpcForIsr Routine 을

시작하라는 Request 를 DPC Queue 에 삽입• DPC Routine 은 ISR 보다 낮은 IRQL 에서 실행

6. I/O Manager 의 DPC dispatcher 는 ISR 에 의해 예정된 DpcForIsr Routine 을 실행• DpcForIsr Routine 은 IRP 의 완료를 표시• Queuing 된 IRP 를 Start I/O Routine 에서 처리하도록 I/O

Manager 에게 통보

Page 6: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 6

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Synchronization of Driver RoutineSynchronization of Driver Routine

I/O Request

Start I/O

ISR

DpcForIsr

Page 7: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 7

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Synchronization of Driver Routine(3)Synchronization of Driver Routine(3)• IRQL 기법에서는 Memory 공유등의 상호배재를 해결해야함

• SynchCritSection Routine 을 사용해 자원접근을 함

BOOLEAN SynchCritSection IRQL == DIRQLParameter Contents

IN PVOID pContext KeSynchronizeExecution 에 전달되는 컨텍스트

Return Value • TRUE Success• FALSE Fail( 오류발생 )

Page 8: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 8

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Synchronization of Driver Routine(4)Synchronization of Driver Routine(4)• KeSynchronizeExcution IRQL 을 DIRQL 까지 올리고 , Spin Lock 을 획득

• SynchCritSection Routine 을 호출함

BOOLEAN KeSynchronizeExecution IRQL < DIRQLParameter Contents

IN PKINTERRUPT pInterruptObj Pointer for Interrupt Object

IN PKSYNCHRONIZE_ROUTINE pRoutine Call Back Routine of SynchCriteSection

IN PVOID pContext Context to SynchCritSection

Return Value SynchCritSection 의 Return Value

Page 9: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 9

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Synchronization of Driver Routine(5)Synchronization of Driver Routine(5)

KeSynchronizeExecution

SynchCritSection

< DIRQL

DIRQL

SpinLock 획득IRQL 상승

Context 와Routine add

전달

자원의 일시적인독점 가능

Page 10: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 10

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Synchronization of Driver Routine(6)Synchronization of Driver Routine(6)

KeSynchronizeExecution

SynchCritSection

< DIRQL

DIRQL

SpinLock 반환IRQL 복귀

Return Value

Page 11: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

2. 2. Driver Initialization and Driver Initialization and CleanupCleanup

Page 12: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 12

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Initialize Start I/O RoutineInitialize Start I/O RoutineNTSTATUS DriverEntry(… … … … …){

:pDO->MajorFunction[ IRP_MJ_CREATE ] = DispCreate;

:pDO->MajorFunction[ IRP_MJ_WRITE ] = DispWrite;

:}

NTSTATUS DriverEntry(… … … … …){

:pDO->FriverStartIO = StartIo;pDO->FriverUnload = DriverUnload;

:pDO->MajorFunction[ IRP_MJ_WRITE ] = DispWrite;

:}

• Initialization 해주지 않으면 IoStartPacket 호출시 접근위반 Error

Page 13: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 13

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Initialize DpcForIsr RoutineInitialize DpcForIsr Routine• DPC Object 와 각 Device Object 를 연결시킬수 있음

– DpcForIsr

• DpcForIsr 과 Device Object 와 연관지을수 있음– IoInitializeDpcRequest 호출– 보통은 DriverEntry or AddDevice 에서 해줌

• ISR Routine 에서 Interrupt 를 처리하는 동안 DPC 를 Scheduling 함– IoRequestDpc 호출

Page 14: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 14

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Linking Interrupt Source to Kernel(1)Linking Interrupt Source to Kernel(1)• 실제 Interrupt Routine Address(ISR) 를 Kernel 에게

알림

• I/O Manager 가 제공– IoConnectInterrupt Function

• ISR 의 Address 를 Parameter 로 전달받음• Interrupt Object 를 Parameter 로 전달받음

Page 15: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 15

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Linking Interrupt Source to Kernel(2)Linking Interrupt Source to Kernel(2)• Interrupt Object 사용시 주의점

– ISR Routine 이 하나이상의 Interrupt Vecter 를 처리하거나 Driver 가 하나 이상의 ISR 을 가지고 있을 시

• Service Context 충돌을 피하기 위해 Spin Lock 사용• SynchronizeIrql 에 지정된 DIRQL Value 는 Interrupt Vector 가

가지는 DIRQL Value 보다 커야함

– Driver Interrupt Routine 은 IoConnectInterrupt 호출시 바로 실행가능한 상태로 준비되어 있어야함

• IoConnectInterrupt 에서 지정된 IRQL 에서 발생된 Interrupt 는 Driver 에 의해 수행되는 추가적인 초기화 작업을 선점할수도 있음

• ISR 은 이러한 Interrupt 를 제 때 처리할수 있어야 함

Page 16: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 16

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Linking Interrupt Source to Kernel(3)Linking Interrupt Source to Kernel(3)1. IoInitializeDpcRequest 호출후 DpcForIsr Routine 실행을 위한

초기화 작업 수행

2. Device 에서 Interrupt 가 발생하지 않게 함• Device 에 적절한 Bit 를 설정함• Control register 를 설정함

3. ISR 에서 요구하는 Driver 의 초기화 수행

4. IoConnectInterrupt 를 호출 • ISR 과 Interrupt Source 를 연결• Interrupt Object 주소를 Device Extension 에 저장

5. SynchCritSection Routine 을 사용해 Device 를 초기화 상태로 바꿈 , Device 가 Interrupt 를 발생하게 함

Page 17: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 17

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Unlinking Interrupt Source to KernelUnlinking Interrupt Source to Kernel• Driver 가 Memory 에서 Unload 될 가능성이 있다면

– Memory 에서 Unload 전에 Kernel 과 Interrupt Souce연결 해제

– 만약 Unload 후에도 연결이 되어있다면 , Device 가 Interrupt 발생시 OS 가 멈춤

• 두가지 절차를 따름1. KeSynchronizeExecution Function 과 SynchCritSection

Function 을 사용해 Device 를 정지 시킴 .• Device 에서 Interrupt 발생 불가

2. IoDisconnectInterrupt Function 사용• Device Interrupt Object 를 Parameter 로 넘겨주어 Kernel 의

Interrupt Service Routine 목록에서 ISR 제거

Page 18: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

3. Start I/O Routine3. Start I/O Routine 의 작성의 작성

Parallel Port 에서 사용되는 Programmed I/O Driver 개발에 관련

Page 19: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 19

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Execution ContextExecution Context• I/O Manager 가 Start I/O 호출

• DISPATCH_LEVEL_IRQL 에서 실행됨– Paged Resource 에 접근해 Page Fault 를 발생하면 안됨

VOID StartIo IRQL == DISPATCH_LEVELParameter Contents

IN PDEVICE_OBJECT pDevObj Request 를 위한 Target Device

IN PIRP pIrp Request 를 설명하는 IRP

Return Value void

- StartIo Routine -

Page 20: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 20

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Start I/O RoutineStart I/O Routine 에서 하는 작업에서 하는 작업

1. 현재 IRP 의 Stack Location Pointer 를 얻음1. IoGetCurrentIrpStackLocation 호출

2. I/O Stack Location 의 MajorFuntion Field 를 확인하여 작업을 선택함

3. IRP 에 저장된 Bytes 수와 System buffer Pointer 를 저장1. Device Extension 에 저장하면 좋음

4. Device Extension 에 Interrupt 발생 예상 Flag 를 설정

5. Device 를 구동시킴

Page 21: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

4. Interrupt Service Routine4. Interrupt Service Routine 의 의 작성작성

Page 22: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 22

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Execution ContextExecution Context• Interrupt DIRQL 과 관계된 Interrupt Object 를

조사해 ISR 을 찾아서 호출

• Kernel Interrupt dispatcher 는 ISR 을 호출– 호출된 ISR 은 IoConnectInterrupt 를 호출할때 지정되었던

IRQL 에서 수행

• ISR 에서 해서는 안되는 작업 ( 높은 IRQL 때문에 )– Page Fault 를 발생해서는 안됨– System Resource 를 할당 , 해제하려하면 안됨 (ex. Memory)

• 구지 해야한다면 , DPC Routine 에게 위임

Page 23: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 23

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Interrupt Service RoutineInterrupt Service Routine 에서 하는 작업에서 하는 작업

1. 발생된 Interrupt 가 해당 Driver 에 관련된것인지 확인

2. Interrupt 를 인식한 Device 에 필요한 작업 요청

3. 전송할 Data 가 더 있으면 , 다음 작업 시작• 결국 다른 Interrupt 를 발생시키는 결과

4. Data 전송이 완료되거나 Error 발생시 IoRequestDpc를 호출하여 DPC Request 를 Queuing 함

5. TRUE Return

Page 24: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

5. DpcForIsr Routine5. DpcForIsr Routine 의 작성의 작성

Page 25: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 25

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Execution Context(1)Execution Context(1)• Driver 에서의 DpcForIsr Routine 의 임무

– 현재 IRP 의 최종상태를 확인– IRP 를 완료후 다음 IRP 시작

• ISR 이 IoRequestDpc 를 호출하면 DpcForIsr Routine은 DPC Queue 에 Queuing 됨

• DcpForIsr Routine 은 DISPATCH_LEVEL_IRQL 수준에서 실행– Paging 되는 주소에 접근할수 없다는 의미가 됨

• I/O Manager 는 DpcForIsr 이 실행될때까지 주어진 Device 에 대한 DPC Request(IoRequestDpc) 를 무시

Page 26: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 26

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Execution Context(2)Execution Context(2)

VOID DpcForIsr IRQL == DISPATCH_LEVELParameter Contents

IN PKDPC pDpc 이 호출에 대한 DPC

IN PDEVICE_OBJECT pDevObj I/O Request 를 위한 Target Device

IN PIRP pIrp 이 Request 를 설명하는 IRP

In PVOID pContext IoRequestDpc 에 전달되는 Context

Return Value Void

- DpcForIsr Routine -

Page 27: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 27

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

DpcForIsr RoutineDpcForIsr Routine 에서 처리하는 작업에서 처리하는 작업

1. IRP 의 I/O 상태를 설정 , Status 변수 설정 , Information 변수에 실제 전송된 Bytes 수를 설정

2. 적절한 우선순위값 설정후 IoComplateRequest 호출• IRP 를 완료하게 됨• IoComplateRequest 가 호출되면 IRP 에서는 어떤작업도 다시

수행해선 안됨

3. Start I/O Routine 에 다음 IRP 를 전달하기 위해 IoStartNextPacket 을 호출

Page 28: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 28

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

우선순위 증가우선순위 증가

• Windows 2000 의 Thread Manager 는 효율적 운영을 위해 우선순위값 사용

• Starvation 방지를 위해 우선순위를 증가시켜줌– IoComplateRequest 호출시 Parameter 로 넘겨줌– Mouse 나 Keyboard 같은 Device 에 우선순위를 중시하여

가중치를 줌

Page 29: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

6. Hardware Example : Parallel 6. Hardware Example : Parallel PortPort

Page 30: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 30

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Parallel PortParallel Port• RESET

– Device 를 초기화 시키고 싶을때 CPU 가 이 Line 을 통해 전송

• DATA– CPU 가 정보를 parallel 하게 전송할수 있는 Line

• STROBE#– CPU 가 DATA Line 에 보낼 정보가 있다는 신호를 보냄

Page 31: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 31

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Parallel PortParallel Port• BUSY

– CPU 에게 DATA 를 받을수 없다는 것을 Device 가 알림

• ACK#– Device 가 BUSY 상태를 벗어났음을 알림

• Errors– Device 가 CPU 에게 준비상태 or Error 상태를 알려지기 위한

몇 개의 Line

Page 32: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 32

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Parallel PortParallel Port 의 동작 순서의 동작 순서

1. CPU 는 DATA Line 에 data 를 준비하고 , 적어도 0.5ms을 기다림

2. CPU 는 STROBE# Line 을 적어도 0.5ms 정도 0 을 유지한뒤 1 로 올림• Device 에게 신호를 받으라는 신호임

3. 새로운 Data 가 오면 Device 는 BUSY Line 을 1 로 바꿈• Device 가 전송을 받기 시작함

4. 한 단위를 처리한뒤 BUSY Line 을 0 으로 ACK# Line을 1 로 바꿈

Page 33: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 33

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Device RegisterDevice Register• 세개의 Register 를 통해 정보를 주고 받음

– Data Register– Status Register– Control Register

• Parallel Port 는 자동 설정을 지원함– Configuration Manager 가 Data Register 의 기본주소를

찾아줌

Page 34: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 34

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

InterruptInterrupt 의 발생의 발생

• Ex) Printer 가 Interrupt 를 발생시키는 시기– 초기화 작업을 마칠 때– 한 문자를 처리하고 다음 문자를 처리할 준비가 되었을 때– 전원이 변경되었을 때– 종이가 없거나 연결이 되지 않았을 때

Page 35: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

7. Example Code7. Example Code(Parallel Port Loopback Driver)(Parallel Port Loopback Driver)

Page 36: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 36

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Purpose of this DriverPurpose of this Driver• Loopback Connector 가 부착된 Parallel Port 에

4Bits 를 전송함

• Loopback Connector 에 의해 Return 된 Data 는 Driver 의 임시 Buffer 에 저장

• 때문에 읽기 작업은 전송한 4Bits 와 동일함

• 추가로 4Bits 를 Left Shift 한 상태로 Return 함

Page 37: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 37

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Driver.hDriver.h

typedef struct _DEVICE_EXTENTION { PDEVICE_OBJECT pDevice; ULONG DeviceNumber; CUString ustrDeviceName; // 내부 Device Name CUString ustrSymLinkName; // SymbolicLinkName PUCHAR deviceBuffer; // 임시 Buffer ULONG deviceBufferSize; ULONG xferCount; // 현재 전송 Count ULONG maxXferCount; // 요청된 전송 Count ULONG portBase; // I/O register Addr ULONG Irq; // Parallel Port IRQPKINTERRUPT pIntObj; // Interrupt Object} DEVICE_EXTENSION, *PDEVICE_EXTENSION

Page 38: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 38

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Driver.cpp(1)Driver.cpp(1)• CREATEDEVICE

– PnP 이전에 연결된 Device 를 인식하기 위한 역할– ClaimResources Function 을 사용

• DISPATCHWRITE– Start I/O Routine 에 진입할 IRP 를 순서대로 대기열에 삽입

• DISPATCHREAD– Device Buffer 의 Data 를 사용자 Buffer 에 Return

Page 39: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 39

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Driver.cpp(2)Driver.cpp(2)• STARTIO

– IRP 가 대기열에서 빠져나올때 마다 I/O Manager 에 의해 호출– DispatchWrite 에서 시작된 작업을 완결시킴

• ISR– 사용자의 출력 buffer 에 마지막 Bytes 가 Printer Port 에

전송되면 DpcForIsr Routine 을 사용해 IRP 를 완료

• DPCFORISR– ISR 의 Request 에 따라 I/O Request 를 완료

Page 40: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

8. Testing The Parallel Port 8. Testing The Parallel Port Loopback DriverLoopback Driver

Page 41: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 41

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Conclusion of TestingConclusion of Testing• IRP 를 dispatch Routine 에서 Start I/O Routine 으로

전송

• Device Interrupt 에 반응

• Data 를 성공적으로 전송

• Request 를 완료

• Multiple User 로 부터 Request 를 관리

Page 42: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 42

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Test Program Test Program 작성 절차작성 절차

1. 각각의 IRP 가 도착하자마자 완료되는 단순한 Start I/O Routine 을 작성• Driver 의 dispatch Routine 과 Start I/O Routine 사이의 연결확인

2. 실제 Start I/O, ISR 그리고 DpcForIsr Routine 작성• Driver 가 Read, Write 를 모두 지원한다면 두 작업을 나누어 구현해

Test

3. Driver 의 모든 Data 전송 경로 Test• ReadFile , WriteFile 그리고 DeviceIoControl 을 호출하는 간단한

Win32 프로그램을 사용

4. 최대한 빨리 많은 수의 I/O Request 를 발생시키는 Program 을 이용해 Driver 의 극단적인 상황을 Test

Page 43: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 43

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

Test Program Test Program 작성 절차작성 절차

5. 한번에 여러 개의 Test Program 을 실행• Device 가 여러 Program 에서 공유된다면 다수의 Handle

에서 작동이 원활해야함

6. Driver 가 다수의 Physical Device 를 지원한다면 각 Device 와 Test 를 반복

7. Multiprocessor 환경에서 4~6 과정 반복• SMP 동기화를 검증하기 위함

Page 44: Ch 8. Interrupt I/O

ISLab Flash TeamISLab Flash Team

SummarySummary

Page 45: Ch 8. Interrupt I/O

Ch 8. Interrupt I/O 45

ISLab Flash TeamISLab Flash Team

Made By Hackereyes

SummarySummary• 실제로 동작하는 Driver 의 기본 골격을 제시

• Start I/O Routine 은 각각의 Request 을 초기화하고 ISR 에서 Interrupt 를 처리

• DpcForIsr Routine 은 Device 의 DIRQL 이하에서 IRP 를 올바르게 완료