c++ Linux下的设备驱动开发

发布于 2024-10-06 05:51:33 字数 118 浏览 6 评论 0原文

我想获得有关使用 c++ for Linux box 编写图形设备驱动程序和音频设备驱动程序的更多详细信息。 我是开发设备驱动程序的新手,请向我提供相同的开发/文档详细信息。

谢谢

-普拉文

I wanted to get more details for writing Graphics device drivers and audio device drivers using c++ for Linux box.
I am newbie at developing device drivers , Please provide me development/documentation details for the same.

Thanks

-Pravin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

So尛奶瓶 2024-10-13 05:51:33

迟到了这一页,这个问题本身已经由 Chris Stratton 回答了,但重要的是要纠正 Chris Becke 在这里提出的一些对不熟悉 C++ 的人来说常见的误解:

  • C++ 不会创建隐式代码或数据,正是您所要求的。即使对于普通的 C++ 程序员来说,也不会有额外的代码或数据。我通过了解 C++ 背后的汇编发现了这一点,但只要阅读 Scott Meyers 的书就足够了。
  • 异常在 C++ 中不仅是可选的,而且对于大多数现有工具来说,它们的整个代码都可以在链接中排除。这实际上是在 RT 应用程序中完成的。

这是为了解决此处发布的误解。不过要补充的是:

1)新手 C++ 程序员可能会胡说八道,但是新手 C 程序员试图自己实现多态性和继承,就像在内核中一次又一次地调用它一样,而不是这样调用它,会做很多效率低下且不可调试的事情废话。

2)也就是说,在基础C++中唯一可以创建的是虚拟指针(如果您需要它并指定“虚拟”),然后C程序员通常也只是创建这样一个指针,自己操作它,添加查找表并获得很多由于它,更难的错误会出现。与 C++ 中一样,如果您不提及“虚拟”,那么您甚至不会获得此指针。继承和封装当然是完全没有开销的。

3) 如果您不明确请求特殊功能,C++ 会创建与 C 相同数量的 asm 和内存,但在一种常见情况下 C++ 更高效 - 在传递函数指针时。如果您使用 C++ 的仿函数,您可以内联指向的函数。这在嵌入式应用程序中非常有用。

4) 如果嵌入式 RT 使用 C++,为什么 linux 不使用?只是因为神话,所以请仔细阅读此消息,并参考 scott meyers 或更好的是 asm 本身。我在 RT 工作了 20 年,当我 14 年前转行时,我对 C++ 也有同样的不信任,但事实并没有证实任何这种不信任。

TL;DR - 用 C++ 编写高效且通常情况下更高效的代码非常容易,关于这个主题的研究、许多行业经验和书籍比比皆是。

Coming to this page late, the question itself has been answered by Chris Stratton, but it's important to correct a couple of things Chris Becke put here that are common misconceptions with people that are not familiar with C++:

  • C++ does not create implicit code or data, just what you request. Even for an average C++ programmer, there will be no extra code or data. I found it out through knowing the asm behind C++, but just read Scott Meyers books it's good enough.
  • Not only are exceptions optional in C++, their entire code can be excluded in linkage for mostly every tool out there. This is in fact done in RT apps.

This is to address the misconceptions posted here. To add more however:

1) A novice C++ programmer may do nonsense, but a novice C programmer trying to implement by himself polymorphism and inheritance as done time and time again in the kernel just without calling it as such, will do lots more inefficient undebuggable nonsense.

2) Saying that, the only thing that may be created in base C++ is a virtual pointer IF YOU NEED IT and specify "virtual", and then also C programmers usually just create such a pointer manipulate it by themselves add lookup tables and get much harder bugs down the line due to it. As always in C++, if you don't mention "virtual" then you don't even get this pointer. Inheritance and encapsulation are of course completely free of overhead.

3) C++ creates the same amount of asm and memory as C if you don't EXPLICITLY request special features, but there is a common case when C++ is more efficient - when passing function pointers. If you use C++'s functors you can inline the pointed function. This is EXTREMELY useful in embedded apps.

4) If embedded RT uses C++ why linux doesn't? Just because of myths, so please do read this message carefully, and refer to scott meyers or better yet the asm itself. I am 20 years in RT and had the same disbelief in C++ when I switch 14 years ago, but the facts do not confirm any such distrust.

TL;DR - it's very easy to write as efficient and in a common case more efficient code in C++, studies, much industry experience and books are abound on this subject.

戏剧牡丹亭 2024-10-13 05:51:33

Linux 内核设备驱动程序是用 C 而不是 C++ 编写的。

大多数设备驱动程序都是通过特殊的设备文件(/dev/yourdevice0)访问的,可以在该文件上执行控制以及读写操作。

用户模式客户端程序和用户模式驱动程序打开设备文件并将其用作与内核模式驱动程序对话的途径。这些用户模式驱动程序可以用 C++ 或任何其他语言编写。

一般来说,最好的入门方法是拥有一个需要驱动程序的设备,并了解编写它所需的内容。通常,最好的方法是为相关设备或具有类似接口范例的设备找到现有驱动程序,然后首先对其进行修改,直到它适用于您的新设备或同样适用。

Linux kernel device drivers are written in C rather than C++.

Most device drivers are accessed via a special device file (/dev/yourdevice0) on which control as well as read and write operations can be performed.

User mode client programs and user mode drivers open the device file and use it as a pathway to talk to the kernel mode driver. These user mode drivers could conceivably be written in C++ or any other language.

Generally the best way to get started is to have a device which needs a driver, and learn what you need to in order to write it. And often the best way to do that is to find an existing driver for either a related device, or one with similar interface paradigms, and start by modifying that until it works for your new device instead or as well.

夜未央樱花落 2024-10-13 05:51:33

由于内核中没有 C++ 运行时,您很快就会遇到问题。我想您可以创建一个在内核中运行的 C++ 运行时,但这需要一些相当好的技能。比用 C 编写驱动程序的技能要高得多。

而且,你会立即被 Linux 内核开发人员打倒。我的意思是真的放下。他们会狠狠地攻击你,让你永远无法从中恢复过来。你很可能会说“去死吧 Linux 和他们的精英混蛋”。

我不想听起来消极,但与你从其他人那里听到的声音相比,我的声音温和且合适。

As there is no C++ runtime in the kernel, you will run into problems quickly. I suppose you could make a C++ runtime to run inside the kernel, but it would require some pretty good skills. Much greater skills than writing the driver in C.

Also, you would be put down instantly by Linux kernel developers. I mean REALLY put down. They'd flame you so bad, you'd never recover from it. Chances are that you would say "Screw Linux and their elitist bastards".

I don't want to sound negative, but I'm a mild and suitable voice in comparison to what you'd hear from others.

无人问我粥可暖 2024-10-13 05:51:33

Linux 驱动程序是用 C 语言开发的。如果您想了解有关 Linux 驱动程序开发的更多信息,您应该阅读这本免费电子书:http ://lwn.net/Kernel/LDD3/
还提供所有 pdf 章节的 tarball:http://lwn.net/images /pdf/LDD3/ldd3_pdf.tar.bz2

Linux drivers are developed in C. If you want to learn more about Linux drivers development, you should read this free eBook: http://lwn.net/Kernel/LDD3/
A tarball of all pdf chapters is also available: http://lwn.net/images/pdf/LDD3/ldd3_pdf.tar.bz2

方圜几里 2024-10-13 05:51:33

用于编写(内核模式)设备驱动程序的语言是 C,而不是 C++,最终原因很简单:C++ 是一种不适合用来编写驱动程序软件的语言。这样做的副作用是,内核模式下没有可用的 C++ 运行时。

至于为什么 C++ 不合适: 至少有两个原因:

  • 所有操作系统上的设备驱动程序都需要严格的代码放置 - 某些代码需要位于不可分页的块中,而不可分页的内存是有限的资源。 c++ 生成大量隐式代码,隐式代码不可能 (a) 审计,以及 (b) 用必要的指令括起来以保证放置。
  • 在 C++ 中,异常已成为非可选。 c++ 异常通常根据 CPU 异常来实现,并且许多驱动程序代码在不能容忍 (cpu) 异常的级别上执行(因此需要不可分页的代码块)。

我认为还有一些其他方面我忘记了,但是,ideomatic c++ 违反了对驱动程序的许多限制。这就是为什么 C 是首选的原因。

C, not C++ is the language for writing (kernel mode) device drivers, and the reason ultimately is simple: C++ is an inappropriate language to use to write driver software. As a side effect of this, there is no c++ runtime available in kernel mode.

As to why c++ is inappropriate: There are at least two reasons:

  • Device drivers on all OS require strict code placement - some code needs to be in non pageable blocks, and non pageable memory is a limited resource. c++ generates lots of implicit code, being implicit its impossible to (a) audit, and (b) bracket with the necessary directives to guarantee placement.
  • exceptions have become non optional in c++. c++ exceptions are typically implemented in terms of CPU exceptions and a lot of driver code on is executed at levels where (cpu) exceptions cannot be tolerated (hence the requirement for non pageable blocks of code).

I think there are some other aspects I am forgetting, but, ideomatic c++ violates a number of constraints placed on drivers. Which is why C is preferred.

倾城花音 2024-10-13 05:51:33

这是一篇旧帖子,但我决定写一个答案,因为没有答案解释如何做到这一点而不适得其反。

简短回答:

答案是“是的,你可以”......付出巨大的努力。让我们忽略 Linus Trovald 关于 C++ 的意见

长答案:

我的建议是写它当你真正需要它时,使用 C++ 。
在内核中运行C++的方式存在很多陷阱。

我最近研究了这个主题,这是我的总结:

如果您想了解更深入的知识,请查看以下文章。

此外 korisk 在 github 用于准系统内核模块。

结论

再次,我真诚地认为,在开始之前评估在内核模块中运行 C++ 的工作量。

第三次,最好在开始之前评估一下在内核模块中运行 C++ 的工作量!

This is an old post, but I decide to write an answer since there is no answer explaining about how to do it without getting backfired.

Short Answer:

The answer is "yes, you can" … with tremendous effort. Let's just ignore Linus Trovald's opinion about C++

Long Answer:

My suggestion is writing it in C++ when you REALLY need it.
There are many pitfalls in the way of running C++ in kernel.

I recently study this subject and here’s my summarize:

  • There is no implementation for new and delete

    It is the easiest thing to overcome.

  • The stack size on kernel is less than 8 kb (for 32bit) and 16kb (for 64bit).

    Opps...not getting stack overrun would be challenging.

  • Following are not allowed

  • It’s PIA to let C++ read a kernel header. If you like challenges, please at least read C++ in the Linux kernel before you go. Don't forget it has to be done every time while upgrading old code to a newer kernel.

If you’d like to know more in-depth knowledge, check out following articles.

Also korisk has a demo repo in github for a barebone kernel module.

Conclusion

Again, my sincere opinion, assess the effort for running C++ in kernel module before you go.

Third time, it's better to assess the effort for running C++ in kernel module before you go!

鹿港巷口少年归 2024-10-13 05:51:33

不幸的是,当前的 Linux 头文件无法在 C++ 中编译,因为它们使用 new 作为变量名,声明 false、true 并具有其他一些内容问题。

您可以在 Linux 模块中使用 C++,但如果不包含 Linux 头文件,它就毫无用处。所以你不能远离最简单的 hello-world 模块。

Unfortunately, the current Linux header files are not compilable in C++ as they use new as a variable name, declare false, true and have some other issues.

You can use C++ in a Linux module but it's useless without including Linux headers. So you can't go far away from the simplest hello-world module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文