1 / 37

Ch 9. Hardware Initialization

Ch 9. Hardware Initialization. Contents. Plug and Play Architecture The Role of the Registry for Legacy Drivers Detecting Devices with Plug and Play The Role of Driver Layers in Plug and Play New WDM IRP Dispatch Functions Device Enumeration Device Interface Summary.

bishop
Download Presentation

Ch 9. Hardware Initialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch 9. Hardware Initialization

  2. Contents • Plug and Play Architecture • The Role of the Registry for Legacy Drivers • Detecting Devices with Plug and Play • The Role of Driver Layers in Plug and Play • New WDM IRP Dispatch Functions • Device Enumeration • Device Interface • Summary Ch 6. 초기화 및 정리 루틴

  3. 1. Plug and Play Architecture2. The Role of the Registry for Legacy Driver

  4. Goals of Plug and Play • Automatic detection of installed and removed hardware. • Device must allow for software configuration • Automatically load as needed by the OS • Support Hot plugging of device Ch 6. 초기화 및 정리 루틴

  5. Components of Plug and Play Setup & Config Component PnP Mgr Registry & INF Files User Mode Kernel Mode Executive I/O Mgr PnP Mgr Power Mgr NT PnP Drivers WDM Drivers HAL Ch 6. 초기화 및 정리 루틴

  6. The Role of Registry for Legacy Drivers • Techniques used by NT OS & Drivers • Installation program에 의존해서 Driver에서 사용할 자원과 디바이스를 규정 • Driver는 각 Device가 사용할 하드웨어 자원을 리스트로 유지, DriverEntry Routine에서 적절히 할당 및 Device객체 생성 • NT OS는 Preboot과정에서 적당히 Device와 자원을 탐색해서 Booting 시에 Reporting 및 Registry에 저장 • 정규화된 설정처리를 위해 자동 감지 기능 • 초기화 과정에 하드웨어 리스트 감지 및 디바이스 생성 • 수동으로도 동작 가능(드라이버 중심) • 실시간 삽입(Hot Plug in)에 따른 Load(디바이스중심) • 드라이버 환경에서의 호환성 Ch 6. 초기화 및 정리 루틴

  7. 3. Detecting Devices with Plug and Play 4. The Role of Driver Layers in Plug and Play

  8. Detecting Devices with Plug and Play • DriverEntry는 초기 진입점 이지만, 드라이버 내의 다른 루틴의 주소만 알려주는 역할을 함 -> 나머지는 AddDevice에서 생성 • Funtion Prototype for an AddDevice Routine Ch 6. 초기화 및 정리 루틴

  9. The Role of Driver Layers in Plug and Play • WDM (Windows Driver Model) • Device를 Stack형태로 구현함 • Physical Device Object (PDO) • 버스에 붙은 각각의 하드웨어를 위해 생성 • 저 수준의 디바이스를 제어함 • Function Device Object (FDO) • 논리적이고 추상적인 각각의 기능을 상위 레벨의 소프트웨어에 제공 • Bus FDO • Nonbus FDO • Filter Device Object • FDO 상,하위에서 I/O요청에 수정 이나 보다 향상된 서비스를 제공 Ch 6. 초기화 및 정리 루틴

  10. Nunbus FDO Nunbus FDO Nunbus FDO Filter DO Device PDO Device PDO Device PDO FDO … Filter DO … Nunbus FDO PDO Device PDO Device Stack The Role of Driver Layers in Plug and Play Ch 6. 초기화 및 정리 루틴

  11. AddDevice Routine을 호출하는 알고리즘 • System Registry에 있는 모든 버스를 탐색 , 열거 • 버스 드라이버가 load • 버스에 존재하는 디바이스 열거 및 PDO 생성 • 드라이버를 정의한 Registry에서 Filter DO 와 FDO를 위한 Device Class를 찾음 • Filter Driver나 Function Driver가 Load되지 않았으면 Driver를 load하고 DriverEntry호출 • FDO및 다른 디바이스 계층을 위한 AddDevice를 호출 디바이스 스택 구성 (IoCreateDevice , IoAttachDeviceToDeviceStack) Ch 6. 초기화 및 정리 루틴

  12. Device Stack • Funtion Prototype for IoAttachDeviceToDeviceStack • 디바이스 스택의 요소는 각 드라이버의 디바이스 Extension구조체에 연관관계를 유지 or pointer공간을 예약 • 하위 디바이스가 초기화 하는 동안 상위 디바이스 포인터는 하위 디바이스가 알려진 디바이스 타입이어야 안정적인 초기화가 가능함 • 디바이스 스택에서는 어떤 드라이버도 자신의 하위 드라이버보다 우선할 수 없다 Ch 6. 초기화 및 정리 루틴

  13. AddDevice NTSTATUS AddDevice ( IN PDRIVER_OBJECT pDriverObject, IN PDEVICE_OBJECT pdo ) { NTSTATUS status; PDEVICE_OBJECT pfdo; PDEVICE_EXTENSIONpDevExt; staticintulDeviceNumber = 0; // 내부 디바이스 이름 초기화 CUStringdevName("\\Device\\MINPNP"); devName += CUString(ulDeviceNumber); // 새로운 디바이스 객체 생성 status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &(UNICODE_STRING)devName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pfdo ); if (!NT_SUCCESS(status)) returnstatus; // Device Extension 초기화 pDevExt = (PDEVICE_EXTENSION)pfdo->DeviceExtension; pDevExt->pDevice = pfdo; // back pointer pDevExt->DeviceNumber = ulDeviceNumber; pDevExt->ustrDeviceName = devName; pDevExt->pIntObj = NULL; pDevExt->bInterruptExpected = FALSE; pDevExt->state = Stopped; Ch 6. 초기화 및 정리 루틴

  14. AddDevice(Cont.) // 새로 생성한 fdo를 스택 레이어의 맨 위에 쌓는다 (Push) pDevExt->pLowerDevice = // downward pointer IoAttachDeviceToDeviceStack( pfdo, pdo); // 상위 디바이스 객체의 포인터를 초기화 하기 위하여 상위 디바이스의 오프셋을 알아내고 하위// 디바이스 익스텐션의 형변환을 수행 // PLOWER_DEVEXT pLowerDevExt = (PLOWER_DEVEXT) // pDevExt->pLowerDevice->DeviceExtension; // pLowerDevExt->pUpperDevice = pfdo; // Symbolic Link Name의 초기화 및 생성 CUStringsymLinkName("\\??\\MPNP"); symLinkName += CUString(ulDeviceNumber+1); // 1 부터 시작함 pDevExt->ustrSymLinkName = symLinkName; status = IoCreateSymbolicLink( &(UNICODE_STRING)symLinkName, &(UNICODE_STRING)devName ); if (!NT_SUCCESS(status)) { // 생성에 실패하면 디바이스 객체를 삭제 IoDeleteDevice( pfdo ); returnstatus; } // 완료 return STATUS_SUCCESS; } Ch 6. 초기화 및 정리 루틴

  15. 5. The New WDM IRP Dispatch Function

  16. Dispatch Function for Plug and Play • 드라이버의 역할 • 디바이스에 요구되는 하드웨어 자원을 예약 , 설정 • 하드웨어를 사용하기 위한 하드웨어 초기화 -> IRP_MJ_PNP가 호출 되었을 때 수행 • PnP Events • 디바이스 초기화 ( insertion ) • 디바이스 셧 다운 ( removal ) • 설정 요청 ( Configuration queries ) -> PnP IRP 코드를 PnP Manager가 드라이버에게 송신 • IRP_MJ_PNP의 단일 dispatch routine을 사용 -> Miner Code를 사용한 루틴을 구성 (IRP_MN_XXX) Ch 6. 초기화 및 정리 루틴

  17. IRP Minor subcode • Major Function Dispatch Routine pDriverObject->MajorFuction[ IRP_MJ_PNP ] = DispPnp; • Minor Function Dispatch Routine NTSTATUS DispPnp ( IN PDEVICE_OBJECT pDO, IN PIRP pIrp ) { // IRP Stack location을 획득 ( Indexing ) PIO_STACK_LOCATION pIrpStack; pIrpStack = IoGetCurrentIrpStackLocation ( pIrp ); switch (pIrpStack->MinorFunction) { caseIRP_MN_START_DEVICE : … caseIRP_MN_STOP_DEVICE : … default : // 지원하지 않는 기능을 하위로 내려보냄 return PassDownPnP( pDO , pIrp ); } // switch 구문 각각의 모든 경우는 호출된 결과로 리턴함 } Ch 6. 초기화 및 정리 루틴

  18. Required Plug and Play IRPs • WDM과 호환되기 위해서 드라이버는 반드시 디바이스의 각 객체 타입에 따라 PnP IRP에 대한 지원을 해야 함 Ch 6. 초기화 및 정리 루틴

  19. PDO Plug and Play • 추가적인 상황을 드라이버에서 구현할 수 있도록 핸들링 함 Ch 6. 초기화 및 정리 루틴

  20. Prestart device states Device not present Device Physically inserted Device is present Bus driver enumerates Device is enumerated PnP Manager loads driver & invokes DriverEntry Drives loaded & initialized AddDevice() FDO & Filter DOs created & stacked Device Enumerated Ch 6. 초기화 및 정리 루틴

  21. Post-start device states Device not present Stopped state IRP_MN_REMOVE_DEVICE IRP_MN_REMOVE_DEVICE IRP_MN_STOP_DEVICE Remove pending IRP_MN_CANCEL _REMOVE_DEVICE IRP_MN_QUERY _REMOVE_DEVICE Stop pending Surprise removal IRP_MN_QUERY _STOP_DEVICE IRP_MN_CANCEL _STOP_DEVICE IRP_MN_START_DEVICE Device started IRP_MN_SURPRISE_REMOVAL Ch 6. 초기화 및 정리 루틴

  22. Passing Down PnP Requests • PnP Manager는 항상 Device Stack의 최상단에 위치한 디바이스의 드라이버로 PnP요청을 보냄 • 요청의 처리가 될때까지 스택을 따라 내려보냄 • IoCopyCurrentStackLocationToNext 와 IoCallDriver를 호출하여 디바이스 스택을 따라 내려감 • 상위 드라이버에서 하위 드라이버의 처리를 기다릴 필요가 없을 경우 현재 IRP스택 로케이션을 Skip하고 완료 루틴으로 IRP처리의 신뢰성을 보장 NTSTATUS PassDownPnP( IN PDEVICE_OBJECT pDO, IN PIRP pIrp) { IoSkipCurrentIrpStackLocation( pIrp ); PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION) pDO->DeviceExtension; return IoCallDriver(pDevExt-> pLowerDevice, pIrp); } Ch 6. 초기화 및 정리 루틴

  23. I/O Completion Routines • IoSetCompletionRoutine을 사용하여 상위레벨의 드라이버에 등록 • 하위레벨 드라이버에서 완료하면 IoCompleteRequest호출 • 하부에서 상부로 차례로 올라오면서 완료루틴이 호출됨 • Funtion Prototype for IoSetCompletionRoutine Ch 6. 초기화 및 정리 루틴

  24. I/O Completion Routines (cont.) • 가장 하위의 드라이버만 예외 • 완료루틴 (OnIoCompletion) 의 IRQL은 예측 불가능 함 • Funtion Prototype for I/O Completion Routine • PnP 핸들러를 PASSIVE_LEVEL에서 수행시키기 위해 커널 이벤트 객체를 사용함 Ch 6. 초기화 및 정리 루틴

  25. Bus Driver Plug and Play Requests • 버스 드라이버에서 지원하는 PnP IRP Minor Code Ch 6. 초기화 및 정리 루틴

  26. 6. Device Enumeration

  27. Device Enumeration • PnP 구성 관리자는 시스템 상에 발견된 하드웨어를 열거 • IRP_MN_START_DEVICE 를 통해서 디바이스에 자원을 할당, 제공 받을 수 있음 • 하드웨어 자원의 할당은 버스, 디바이스 하드웨어, PnP Manager, 디바이스 드라이버를 모두 포함하는 동적인 상호 처리 과정 Ch 6. 초기화 및 정리 루틴

  28. CM_RESOURCE_LIST Count CM_RESOURCE_DESCRIPTOR LIST[] Interface Type BusNumber PartialResourceList Port CM_PARTIAL_RESOURCE_DESCRIPTOR CM_PARTIAL_RESOURCE_LIST Interrupt Type Version ShareDisposition Revision Memory Flags Count Union u DMA Partial Descriptors[] Hardware Resource Descriptors • IRP_MN_START_DEVICE를 받으면 IRP스택 내의 두개의 필드에 하드웨어 자원을 리스트와 한다 (Parameters.StartDevice.AllocatedResourcesTranslated, Parameters.StartDevice.AllocatedResources) FDO Driver … … … Driver Ext. Ch 6. 초기화 및 정리 루틴

  29. Using Hardware Resources Within the Driver • Raw resource (원본 그대로의 자원) • 버스의 상대적인 주소(port, IRQLs, DMA Channel) • Translate resource와 1:1 대응 관계를 가짐(해석된 자원) • HAL 매크로들은 Translate resource를 인자로 받음 • 자원의 제한 • IRP_MN_FILTER_RESOURCE_REQUIREMENTS 를 호출 • PnP Manager에서 할당 받은 자원 리스트를 수정(삭제)가능 Ch 6. 초기화 및 정리 루틴

  30. 7. Device Interface

  31. Interface Definition • 구현자와 호출자 간의 함수 호출에 관련된 단순한 명세 혹은 약속 • Plug and Play를 구현하면서 디바이스 드라이버로 하여금 사용자 모드 코드로 인터페이스를 확장 하도록 함 • Interface 의 특징 • 임의로 변경 되어서는 안됨 • 유일한 숫자나 Interface Type(ID)로 식별가능 • 드라이버에서 제공되는 인터페이스는 Application에서도 인식 • 드라이버에서 사용하는 수준과 같은 기능을 보장 Ch 6. 초기화 및 정리 루틴

  32. Interface Construction • 적당한 ID가 생성되면, 명세를 위한 함수 포인터를 가지는 구조체로 생성함 • C++에서는 상속기법으로 정의 가능 Typedef VOID (*PINTERFACE_REFERENCE)(PVOID pContext); Typedef VOID (*PINTERFACE_DEREFERENCE)(PVOID pContext); Typedef struct _INTERFACE { USHORT Size; USHORT Version; PVOID Context; PINTERFACE_REFERENCE InterfaceReference; PINTERFACE_DEREFERENCE InterfaceDereference; // 인터페이스 명세 엔트리를 여기에 작성함 } INTERFACE, *PINTERFACE Ch 6. 초기화 및 정리 루틴

  33. Registering and Enabling an Interface • 인터페이스 생성 후 AddDevice내의 IoRegisterDeviceInterface 함수를 통해 Registering • 시스템에 생성된 Symbolic Link Name은 System Registry에 보존됨, Device Extension에도 저장 • Function Prototype for IoRegisterDeviceInterface Ch 6. 초기화 및 정리 루틴

  34. Registering and Enabling an Interface(cont.) • IRP_MV_START_DEVICE의 routine이 호출되면 활성화 시킴 • IoSetDeviceInterfaceState함수로 Enable or Disable • Function Prototype for IoSetDeviceInterfaceState Ch 6. 초기화 및 정리 루틴

  35. Registering and Enabling an Interface(cont.) • IRP_MN_QUERY_INTERFACE를 통해서 kernel Mode로 사용 • Parameters.QueryInterface.Interface field에 호출자에 할당된 구조체 • Interface를 구현한 함수의 pointer or Data는 드라이버가 책임을 지고 유효한 값으로 채움 Ch 6. 초기화 및 정리 루틴

  36. Summary

  37. Summary • 레거시 드라이버를 단순한 형태의 WDM드라이버로 변환 Ch 6. 초기화 및 정리 루틴

More Related