Can Windows drivers be written in Python?
Asked Answered
S

7

13

Can Windows drivers be written in Python?

Schmit answered 11/6, 2009 at 13:50 Comment(0)
S
17

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
Sextant answered 11/6, 2009 at 14:32 Comment(5)
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.Casias
@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.Sextant
@Sextant 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.Casias
@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.Sextant
Does someone has a newer link please ? (the User-Mode Driver Framework)Tripalmitin
B
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.

Brost answered 11/6, 2009 at 14:8 Comment(0)
S
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.

Sextant answered 11/6, 2009 at 14:26 Comment(0)
A
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.

Ashjian answered 11/6, 2009 at 13:53 Comment(1)
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. ;)Silvia
V
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 :)

Verbid answered 11/6, 2009 at 14:1 Comment(5)
it will make a "fat" and "slow" driver :)Schmit
I was thinking the same thing. However, Lua would be a much preferable language for doing so IMHO.Brost
It'd be perfect for writing a driver for a 300 baud modem. ;-)Transport
@Judge Maygarden, I can't see writing drivers in Lua either. C or assembly makes the most sense. If you don't do it in a language close to the metal, I don't see why one interpreted language would be very superior to another.Transport
But you know what would be cool? A language that looks like Python but is procedural like C.Transport
C
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.

Cuttlebone answered 11/6, 2009 at 13:54 Comment(3)
booh :( thank you anyway :) i guess i have to learn C or assembly.. ;)Schmit
What about integrating the interpreter (written in C) into the driver and then executing Python script also stored in the driver as data?Brost
@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.Cuttlebone
M
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 ;)

Masticate answered 11/6, 2009 at 13:59 Comment(1)
hummm.. if i could be able to write this kind of compiler... i really wouldn't care of writing a driver in python :DSchmit

© 2022 - 2024 — McMap. All rights reserved.