13

Can Windows drivers be written in Python?

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
Armageddon
  • 163
  • 1
  • 1
  • 6

7 Answers7

18

Yes. You cannot create the "classic" kernel-mode drivers. However, starting with XP, Windows offers a User-Mode Driver Framework. They can't do everything, obviously - any driver used in booting the OS obviously has to be kernel-mode. But with UMDF, you only need to implement COM components.

Besides boot-time drivers, you also can't write UMDF drivers that:

  • Handle interrupts
  • Directly access hardware, such as direct memory access (DMA)
  • have strict timing loops
  • Use nonpaged pool or other resources that are reserved for kernel mode
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I'm a linux driver developer for my company and now we are exploring python to make our driver platform independent. Suggested by the higher management and doing that RND these days. – sandun dhammika Aug 01 '16 at 13:16
  • 1
    @sandundhammika: That ain't gonna happen, to put it colloquially. Drivers are not platform-independent because the kernel dictates what they must do, and different platforms have completely different expectations. So Windows expects UMDF drivers to communicate via COM, whereas Linux doesn't even have COM. – MSalters Aug 01 '16 at 13:53
  • @MSalters The original issue is to distribute same binary driver for different distributions. We are delivering our C++ native binary release now , but their library names are platform dependent, so even same ABI it breaks on library names. – sandun dhammika Aug 01 '16 at 14:15
  • @sandundhammika: Sounds like the better solution would be a driver _installer_ written in Python, with the actual driver still in C++. That might be sufficient to check off your manager's wish. – MSalters Aug 01 '16 at 14:25
  • Does someone has a newer link please ? (the User-Mode Driver Framework) – Maskim Nov 29 '21 at 10:48
4

The definitive answer is not without embedding an interpreter in your otherwise C/assembly driver. Unless someone has a framework available, then the answer is no. Once you have the interpreter and bindings in place then the rest of the logic could be done in Python.

However, writing drivers is one of the things for which C is best suited. I imagine the resulting Python code would look a whole lot like C code and defeat the purpose of the interpreter overhead.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
3

A good way to gain insight why this is practically impossible is by reading Microsoft's advice on the use of C++ in drivers. As a derivative of C, the use of C++ appears to be straightforward. In practice, not so.

For instance, you must decide for every function (and really every assembly instruction) whether it's in pageable or non-pageable memory. This requires extensions to C, careful use of new C++ features, or in this case a special extension to the Python language and VM. In addition, your driver-compatible VM would also have to deal with the different IRQLs - there's a hierarchy of "levels" which restrict what you can and cannot do.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

Python runs in a virtual machine, so no.

BUT:

You could write a compiler that translates Python code to machine language. Once you've done that, you can do it.

Mark Lutton
  • 6,959
  • 7
  • 41
  • 57
  • 2
    or you could embed a Python interpreter in the driver. Or talk Microsoft into including a Python interpreter in the kernel. Technically, there's no reason why it couldn't be done. Windows just doesn't currently offer you a lot of support for it. ;) – jalf Jun 11 '09 at 14:03
1

I don't know the restrictions on drivers on windows (memory allocation schemes, dynamic load of libraries and all), but you may be able to embed a python interpreter in your driver, at which point you can do whatever you want. Not that I think it is a good idea :)

David Cournapeau
  • 78,318
  • 8
  • 63
  • 70
0

Never say never but eh.. no

You might be able to hack something together to run user-mode parts of drivers in python. But kernel-mode stuff can only be done in C or assembly.

Mendelt
  • 36,795
  • 6
  • 74
  • 97
  • booh :( thank you anyway :) i guess i have to learn C or assembly.. ;) – Armageddon Jun 11 '09 at 14:01
  • What about integrating the interpreter (written in C) into the driver and then executing Python script also stored in the driver as data? – Judge Maygarden Jun 11 '09 at 14:03
  • @Judge Maygarden: That might work in user-mode but kernel-mode is very restrictive on what calls can be made. Chances are your interpreter will not run there either. Kernel mode development is kind of a black art. – Mendelt Jun 11 '09 at 14:49
0

No they cannot. Windows drivers must be written in a language that can

  1. Interface with the C based API
  2. Compile down to machine code

Then again, there's nothing stopping you from writing a compiler that translates python to machine code ;)

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • hummm.. if i could be able to write this kind of compiler... i really wouldn't care of writing a driver in python :D – Armageddon Jun 11 '09 at 14:05