开发者

c++ device driver development in linux

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/docume开发者_Go百科ntation details for the same.

Thanks

-Pravin


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.


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.


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.


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


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.


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

    • Global non-trivial variables ( there is no C++ runtime initializes it for your, using singleton would be better )
    • STL ( One of super powers of C++, you need to port C++ stdlib to make it working in kernel )
    • RTTI
    • Exceptions
  • 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.

  • C++ in the Linux kernel (2016)

  • Porting C++ code to Linux kernel (2009)

  • C++ article in OSDev.org

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜