Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can kernel be written in other than assembly language?

Tags:

java

kernel

I never did kernel programming. I am a good programmer in the Java language and frequently use it. Now i feel to do something interesting with kernels. A kernel resides between hardware and OS. It communicates with hardware using system calls. Every programming language require a compiler to compile the code written in high level language and then it generate low level code, which is generally assembly language code. Here comes my doubt, if we have kernel written in C, then should we have a C compiler installed on the machine? At the end, when kernel interacts with hardware it uses assembly language, can i create kernel in Java language? If yes, then what are the requirements for the same? Thank you.

like image 553
Ravi Joshi Avatar asked Nov 28 '22 17:11

Ravi Joshi


2 Answers

Yes a kernel can be written in Java, see the JNode. It would have the advantage of having no problems with: dangling pointers, mix up of pointers and array addresses, unitialised data, and many more features of C.

like image 30
Joop Eggen Avatar answered Dec 11 '22 10:12

Joop Eggen


A kernel resides between hardware and OS

Usually, the kernel is considered to be part of the operating system.

It communicates with hardware using system calls

System calls are the interface that is provided by the OS to user applications. The operating system communicates with the hardware through other mechanisms (for example interrupts or memory-mapped registers).

Every programming language require a compiler to compile the code written in high level language and then it generate low level code, which is generally assembly language code.

The compiler output is typically either native machine code or a language-specific bytecode (like in the case of Java). Sometimes, compilers also target another programming language such as C or Javascript (transpilation).

Here comes my doubt, if we have kernel written in C, then should we have a C compiler installed on the machine?

That's not necessary. The C compiler produces output that can execute directly on the hardware without interpretation.

At the end, when kernel interacts with hardware it uses assembly language

The CPU doesn't understand assembly. It understands machine code.

can i create kernel in Java language?

It has been done.

If yes, then what are the requirements for the same?

If you want to write a kernel in Java, then you either have to

  • compile your entire Java codebase to machine code
  • get yourself a CPU that can execute Java bytecode
  • find or build a Java VM and runtime that can run on bare metal and run your Java code in it (if you do it cleverly, you can write much of the runtime and maybe also parts of the VM in Java itself).

Now to the unspoken, almost rhethorical question:

Is this a good idea?

Probably not. Why? First of all, because it would take ages to set up. Second, because you couldn't just code the way you develop an average business application. You'd have to think about performance of very time-critical code (e.g. context switching, which often requires hand-tuned assembly to be fast enough), manual memory management (as in: your MRU might expect you to give it the physical address where the page table lies), system-/hardware-specific mechanisms (how to access a XYZ controller on this particular architecture?), ...

So you would lose many of the advantages that Java has over a low-level language like C in the first place.

like image 132
Niklas B. Avatar answered Dec 11 '22 09:12

Niklas B.