Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to code a device driver in Java?

Tags:

Introduction

I heard something about writing device drivers in Java (heard as in "with my ears", not from the internet) and was wondering... I always thought device drivers operated on an operating system level and thus must be written in the same language as the OS (thus mostly C I suppose)

Questions

  1. Am I generally wrong with this assumption? (it seems so)
  2. How can a driver in an "alien" language be used in the OS?
  3. What are the requirements (from a programming language point of view) for a device driver anyway?

Thanks for reading

like image 548
Philip Stark Avatar asked Mar 25 '09 22:03

Philip Stark


People also ask

How are device drivers coded?

Use these guidelines when you write the code for your driver: Use a prefix based on the name of your driver to give global variables and functions unique names. The name of each function, data element, and driver preprocessor definition must be unique for each driver. A driver module is linked into the kernel.

What language are device drivers written in?

Device drivers are typically written in C, using the Driver Development Kit (DDK). There are functional and object-oriented ways to program drivers, depending on the language chosen to write in. It is generally not possible to program a driver in Visual Basic or other high-level languages.

Can I write drivers in 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.

How are drivers programmed?

A driver communicates with the device through the computer bus or communications subsystem to which the hardware connects. When a calling program invokes a routine in the driver, the driver issues commands to the device (drives it).


2 Answers

There are a couple of ways this can be done.

First, code running at "OS level" does not need to be written in the same language as the OS. It merely has to be able to be linked together with OS code. Virtually all languages can interoperate with C, which is really all that's needed.

So language-wise, there is technically no problem. Java functions can call C functions, and C functions can call Java functions. And if the OS isn't written in C (let's say, for the sake of argument that it's written in C++), then the OS C++ code can call into some intermediate C code, which forwards to your Java, and vice versa. C is pretty much a lingua franca of programming.

Once a program has been compiled (to native code), its source language is no longer relevant. Assembler looks much the same regardless of which language the source code was written in before compilation. As long as you use the same calling convention as the OS, it's no problem.

A bigger problem is runtime support. Not a lot of software services are available in the OS. There usually is no Java virtual machine, for example. (There is no reason why there technically couldn't be, but usually, but usually, it's safe to assume that it's not present).

Unfortunately, in its "default" representation, as Java bytecode, a Java program requires a lot of infrastructure. It needs the Java VM to interpret and JIT the bytecode, and it needs the class library and so on.

But there are two ways around this:

  • Support Java in the kernel. This would be an unusual step, but it could be done.
  • Or compile your Java source code to a native format. A Java program doesn't have to be compiled to Java bytecode. You could compile it to x86 assembler. The same goes for whatever class libraries you use. Those too could be compiled all the way to assembler. Of course, parts of the Java class library requires certain OS features that won't be available, but then use of those classes could be avoided.

So yes, it can be done. But it's not straightforward, and it's unclear what you'd gain.

Of course another problem may be that Java won't let you access arbitrary memory locations, which would make a lot of hardware communication pretty tricky. But that could be worked around too, perhaps by calling into very simple C functions which simply return the relevant memory areas as arrays for Java to work on.

like image 129
jalf Avatar answered Nov 03 '22 10:11

jalf


Writing Solaris Device Drivers in Java covers a A RAM disk device written in Java.

Another one for Linux. Goes more in depth on why you might want a DD in Java as well (since some people were wondering by the looks of the other posts and comments)

like image 25
TofuBeer Avatar answered Nov 03 '22 08:11

TofuBeer