简体   繁体   English

设备驱动程序和Windows

[英]Device drivers and Windows

I am trying to complete the picture of how the PC and the OS interacts together. 我试图完成PC和操作系统如何相互作用的图片。 And I am at point, where I am little out of guess when it comes to device drivers. 我就是这样,在设备驱动程序方面,我几乎没有猜测。

Please, don´t write things like its too complicated, or you don´t need to know when using high programming laguage and winapi functions. 请不要写太复杂的东西,或者在使用高编程语言和winapi函数时你不需要知道。 I want to know, it´s for study purposes. 我想知道,这是出于学习目的。

So, the very basic structure of how OS and PC (by PC I mean of course HW) is how I see it is that all other than direct CPU commands, which can CPU do on itself (arithmetic operation, its registers access and memory access) must pass thru OS. 因此,操作系统和PC的基本结构(通过PC我的意思当然是HW)是我看到的除了直接CPU命令之外的其他所有CPU本身可以做的事情(算术运算,其寄存器访问和内存访问) )必须通过OS。 Mainly becouse from ring level 3 you cannot use in and out intructions which are used for acesing other HW. 主要是因为3级环,你不能使用进出其他硬件的入口。 I know that there is MMIO,but it must be set by port comunication first. 我知道有MMIO,但必须首先通过端口通信设置。

It was not like this all the time. 它不是一直这样。 Even I am bit young to remember MSDOS, I know you could access HW directly, becouse there ws no limitation, no ring mode. 即使我有点年轻还记得MSDOS,我知道你可以直接访问HW,因为没有限制,没有响铃模式。 So you could to write string to diplay use wheather DOS function, or directly acess video card memory and write it by yourself. 所以你可以写字符串到diplay使用DOS函数,或直接访问显卡内存并自己编写。

But as OS developed, there is no longer this possibility. 但随着操作系统的发展,不再存在这种可能性。 But it is fine, since OS now handles all the HW comunication, and frankly it more convinient and much more safe (I would say the only option) in multitasking environment. 但它很好,因为操作系统现在处理所有的硬件通信,坦率地说,它在多任务处理环境中更方便,更安全(我会说是唯一的选择)。 So nowdays you instead of using int instructions to use BIOS mapped function or DOS function you call dll which internally than handles everything you don´t need to know about. 所以现在你不是使用int指令来使用BIOS映射函数或DOS函数,而是在内部调用dll,而不是处理你不需要知道的所有内容。

I understand this. 我理解这一点。 I also undrstand that device drivers is the piece of code that runs in ring level 0, so it can do all the HW interactions. 我还可以理解,设备驱动程序是在0级环中运行的代码段,因此它可以执行所有HW交互。 But what I don´t understand is connection between OS and device driver. 但我不明白的是OS和设备驱动程序之间的联系。 Let´s take a example - I want to make a sound card make a sound. 我们举一个例子 - 我想让声卡发出声音。 So I call windows API to acess sound card, but what happens than? 所以我将Windows API称为访问声卡,但会发生什么? Does windows call device drivers to do so? Windows是否会调用设备驱动程序?

But if it does call device driver, does it mean, that all device drivers which can be called by winAPI function, must have routines named in some specific way? 但是,如果它确实调用了设备驱动程序,是否意味着,所有可以通过winAPI函数调用的设备驱动程序,必须具有以某种特定方式命名的例程? I mean, when I have new sound card, must its drivers have functions named same as the old one? 我的意思是,当我有新的声卡时,它的驱动程序是否必须具有与旧声卡相同的功能? So Windows can actually call the same function from its perspective? 那么Windows实际上可以从它的角度调用相同的函数吗? But if Windows have predefined sets of functions requored by device drivers, that it cannot use new drivers that doesent existed before last version of OS came out. 但是,如果Windows具有由设备驱动程序处理的预定义功能集,则它不能使用在上一版操作系统出现之前存在的新驱动程序。

Please, help me understand this mess. 拜托,帮我理解这个烂摊子。 I am really getting mad. 我真的生气了。 Thanks. 谢谢。

A Windows device driver is a bit like a DLL: except that instead of an application dynamic linking/loading it, it's the O/S that dynamic links/loads it. Windows设备驱动程序有点像DLL:除了动态链接/加载它的应用程序,它是动态链接/加载它的O / S.

Registry entries tell the O/S what device drivers exist (so that the O/S knows which device drivers to dynamic-link/load). 注册表项告诉O / S存在哪些设备驱动程序(以便O / S知道动态链接/加载哪些设备驱动程序)。

The device drivers run in ring 0. In ring zero, they (device drivers) don't have access to (can't link to or use) Windows APIs: instead they have access to various NT kernel APIs. 设备驱动程序在环0中运行。在零环中,它们(设备驱动程序)无权访问(无法链接或使用)Windows API:相反,它们可以访问各种NT内核API。

But if it does call device driver, does it mean, that all device drivers which can be called by winAPI function, must have routines named in some specific way? 但是,如果它确实调用了设备驱动程序,是否意味着,所有可以通过winAPI函数调用的设备驱动程序,必须具有以某种特定方式命名的例程? I mean, when I have new sound card, must its drivers have functions named same as the old one? 我的意思是,当我有新的声卡时,它的驱动程序是否必须具有与旧声卡相同的功能? So Windows can actually call the same function from its perspective? 那么Windows实际上可以从它的角度调用相同的函数吗?

Basically yes. 基本上是的。 All the device drivers within a given type or class (eg all video drivers, or all disk drivers) have a similar API, which is invoked by the O/S (and/or invoked by higher-level drivers, for example disk drivers are used/invoked by file system drivers). 给定类型或类中的所有设备驱动程序(例如,所有视频驱动程序或所有磁盘驱动程序)都具有类似的API,由O / S调用(和/或由更高级别的驱动程序调用,例如磁盘驱动程序,文件系统驱动程序使用/调用的)。

The Windows Device Driver Kit defines the various APIs and includes sample drivers for the various types of device. Windows设备驱动程序工具包定义了各种API,并包含各种类型设备的示例驱动程序。

But if Windows have predefined sets of functions requored by device drivers, that it cannot use new drivers that doesent existed before last version of OS came out. 但是,如果Windows具有由设备驱动程序处理的预定义功能集,则它不能使用在上一版操作系统出现之前存在的新驱动程序。

The O/S is dynamic-linking to the device driver functions: because device driver APIs are predefined, device drivers are interchangeable as far as the O/S is concerned; O / S是动态链接到设备驱动程序功能:因为设备驱动程序API是预定义的,所以设备驱动程序就O / S而言是可互换的; new device drivers can be written, provided they support (are backward-compatible with) the standard device driver API. 只要新设备驱动程序支持(向后兼容)标准设备驱动程序API,就可以编写新设备驱动程序。

The dynamic-linking mechanism is very similar to the way in which COM objects or C++ classes implement any predefined pure-abstract interface: a header file in the DDK declares the pure-abstract interface (like virtual functions), device drivers implement these functions, and the O/S loads the drivers and invokes these functions. 动态链接机制非常类似于COM对象或C ++类实现任何预定义的纯抽象接口的方式:DDK中的头文件声明纯抽象接口(如虚函数),设备驱动程序实现这些功能,并且O / S加载驱动程序并调用这些函数。

The basics: Please note that this explanation is simplified and sometime only true for most cases and not all. 基础知识: 请注意,这种解释是简化的,有时仅适用于大多数情况,而不是全部。

Most HW devices you will ever encounter will have these basic operations: Write to memory(or Registers) on them. 您将遇到的大多数硬件设备都将具有以下基本操作:写入内存(或寄存器)。 Read from memory(or Registers) on them. 从内存(或寄存器)读取它们。

This is enough to control the HW, to give it the data it needs, and to get the data you want from it. 这足以控制硬件,为其提供所需的数据,并从中获取所需的数据。

These memory areas are mapped by the BIOS and/or the OS to the Physical memory range on your PC (which may in turn be accessed by your driver.) 这些内存区域由BIOS和/或操作系统映射到PC上的物理内存范围(驱动程序可以访问这些内存范围)。

So we now have two operations READ and WRITE that the device driver knows to do. 所以我们现在有两个操作READ和WRITE,设备驱动程序知道这样做。

In addition, the driver can read and write in a manner that does not involve the cpu. 此外,驱动程序可以以不涉及cpu的方式进行读写。 This is called Direct Memory Access (DMA) and usually performed by your HW. 这称为直接内存访问(DMA),通常由您的HW执行。

The last type of operation is called INTERRUPTS and is meant for your HW to notify your driver of something that just happend. 最后一种操作称为INTERRUPTS,用于让您的硬件通知您的驱动程序刚刚发生的事情。 This is usually done by the HW interrupting the CPU and calling your driver to perform some operation in high priority. 这通常由硬件中断CPU并调用驱动程序以高优先级执行某些操作来完成。 For example: an image is ready in the HW to be read by the driver. 例如:在HW中准备好要由驱动程序读取的映像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM