Question

Can Windows drivers be written in Python?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

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 :)

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.

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 ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top