设备驱动程序代码在哪里执行?内核空间还是用户空间? [英] where is device driver code executed? Kernel space or User space?

查看:821
本文介绍了设备驱动程序代码在哪里执行?内核空间还是用户空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1部分:

对于那里的linux/unix专家,请您帮助我了解有关设备驱动程序的信息.据我了解,驱动程序是一段直接与硬件交互并公开一些api来访问设备的代码.我的问题是这段代码在哪里运行,用户空间还是内核空间?

我知道在内核空间中执行的代码具有一些额外的特权,例如访问任何内存位置(如果我错了,请正确).如果我们安装了第三方驱动程序,并且该驱动程序在内核空间中运行,那么这对整个系统有害吗?操作系统如何处理?

第2部分:

让我们以USB设备(相机,键盘..)为例,系统如何识别这些设备?系统如何知道要安装哪个驱动程序?驱动程序如何知道用于读取和写入数据的设备的地址?

(如果太大而无法在这里回答,请提供一些好的文档或教程的链接..,我已经尝试过并且找不到答案.请帮忙)

解决方案

第1部分

在linux上,驱动程序在内核空间中运行.是的,正如您所说的,这对安全性有重大影响.驱动程序中的大多数异常都会导致内核崩溃,并可能破坏内核内存(造成各种后果).笨拙的驱动程序也会影响系统安全性,恶意驱动程序绝对可以执行他们想要的任何操作.

在MacOSX和Window NT内核上看到的趋势是用户空间驱动程序.微软一段时间以来一直在推动 Fuse 允许实现归档用户空间中的系统.

第2部分

USB,Firewire,MCI(以及PCI-e)都具有枚举机制,总线驱动程序可通过该枚举机制将设备与驱动程序进行匹配.实际上,这意味着所有设备都公开描述其内容的元数据.

元数据中包含设备ID,供应商ID以及设备提供的功能和相关的ClassID的描述. ClassID有助于通用类驱动程序.

从概念上讲,操作系统将尝试查找专门支持VendorID和DeviceID的驱动程序,然后回退到支持ClassID的驱动程序.

将设备匹配到驱动程序是 Linux设备模型的核心概念,并且用于匹配的确切匹配标准是特定总线驱动程序中的match()函数.

一旦将设备驱动程序绑定到设备,它将使用总线驱动程序(或设备给定的寻址信息)执行读取和写入操作.对于PCI和Firewire,这是一个内存映射的IO地址.对于USB it总线寻址信息.

Linux文档树提供了有关Linux设备模型设计的一些见解,但没有提供真正的入门级阅读.

我还建议阅读 Linux设备驱动程序(第三版)

Part1:

To the linux/unix experts out there, Could you please help me understanding about device drivers. As i understood, a driver is a piece of code that directly interacts with hardware and exposes some apis to access the device. My question is where does this piece of code runs, User space or Kernel space?

I know that code that is executed in kernel space has some extra privileges like accessing any memory location(pls correct if i'm wrong). If we install a third party driver and if it runs in kernel space, wouldn't this be harmful for the whole system? How any OS handles this?

Part2:

Lets take an example of USB device(camera, keyboard..), How the system recognizes these devices? how does the system know which driver to install? How does the driver know the address of the device to read and write the data?

(if this is too big to answer here, pls provide links of some good documentation or tutorials.., I've tried and couldn't find answers for these. pls help)

解决方案

Part 1

On linux, drivers run in kernel space. And yes, as you state there a significant security implications to this. Most exceptions in drivers will take down the kernel, potentially corrupt kernel memory (with all manner of consequences). Buggy drivers also have an impact on system security, and malicious drivers can do absolutely anything they want.

A trend seen on MacOSX and Window NT kernels is user-space drivers. Microsoft has for some time been pushing the Windows Userspace Driver Framework, and MacOSX has long provided user-space APIs for Firewire and USB drivers, and class-compliant drivers for many USB peripherals. it is quite unusual to install 3rd party kernel-mode device drivers on MacOSX.

Arguably, the bad reputation Windows used to have for kernel panics can be attributed to the (often poor quality) kernel mode drivers that came with just about every mobile phone, camera and printer.

Linux graphics drivers are pretty much all implemented in user-space with a minimal kernel-resident portion, and Fuse allows the implementation of filing systems in user-space.

Part 2

USB, Firewire, MCI (and also PCI-e) all have enumeration mechanisms through which a bus driver can match the device to a driver. In practice this means that all devices expose metadata describing what they are.

Contained within the metadata is a DeviceID, VendorID and a description of functions the device provides and associated ClassIDs. ClassIDs facilitate generic Class Drivers.

Conceptually, the operating system will attempt to find a driver that specifically supports the VendorID and DeviceID, and then fall back to one that supports the ClassID(s).

Matching devices to drivers is a core concept at the heart of the Linux Device Model, and exact matching criteria used for matching is match() function in the specific bus driver.

Once device drivers are bound to a device, it uses the bus-driver (or addressing information given by it) to perform read and writes. In the case of PCI and Firewire, this is a memory mapped IO address. For USB it bus addressing information.

The Linux Documentation tree provides some insight into the design of the Linux Device Model, but isn't really entry-level reading.

I'd also recommend reading Linux Device Driver (3rd Edition)

这篇关于设备驱动程序代码在哪里执行?内核空间还是用户空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆