-3

Is it possible to make some privileged instructions (eg sti, cli) available via some Java's API?

Why not? (Or if it's is, could you give me some outline/basic idea of how this would be implemented?)

Thanks

2 Answers2

3

STI and CLI are low-level platform dependent interrupts.

When you write source code in Java (the language), you are targeting the Java Virtual Machine, which executes Java bytecode. So your source code instructions are not directly translated to machine code for the physical machine. It is converted to bytecode that is then run (and also recompiled at runtime by the JIT compiler) to target your physical machine.

Therefore you don't have the option (as it wasn't really Java's design goal and it wouldn't make much sense to expose this) to easily use these privileged instructions. They are meant to be abstracted.

We use "higher-level" languages for a reason.

haylem
  • 22,460
  • 3
  • 67
  • 96
  • So is it really impossible (ie., one couldn't possibly do it using any clever hackery, ...)? Or is it just not an easy thing to do? I've heard of the `native` interface, which would allow you to interact with C-code, which could subsequently get down to assembly language where you could do the `sti`, `cli` instructions. But I'm not sure this is correct. Please correct me if I'm wrong. And thanks again for your answers! – user1801813 Nov 06 '12 at 02:36
  • 1
    @user1801813: would you use a shotgun to hammer in a nail? What haylem's getting at is use the proper tool for the job. 1+ to answer. – Hovercraft Full Of Eels Nov 06 '12 at 02:38
  • @HovercraftFullOfEels No, I'm not actually going to do something like this in production code! I'm just asking from a `student's` prospective, that is just out of curiosity... – user1801813 Nov 06 '12 at 02:42
  • @user1801813: If you're asking if you can inline code like this with Java, then no. You can of course interface with lower-level languages through JNI, but understand that when you do so, you lose all platform independence. – Hovercraft Full Of Eels Nov 06 '12 at 02:44
  • @user1801813: you can indeed interface with external code via either [JNI](http://en.wikipedia.org/wiki/Java_Native_Interface) or [JNA](http://en.wikipedia.org/wiki/Java_Native_Access). So, no, you CANNOT do this in Java (the language), but CAN do it from Java (the platform), though by resorting to external code. – haylem Nov 06 '12 at 02:56
  • @haylem: So you can absolutely have a static method called `cli()` that could be called with `MyAPI.cli()`an that would actually clear the `IF` flag? – user1801813 Nov 06 '12 at 03:06
  • @user1801813: seems doable, yes. Sorry, hadn't understood you meant it like this in your original question. – haylem Nov 06 '12 at 10:32
0

In a conventional Java implementation, your only option is to implement the code that executes the privileged instructions in a native method that you call using JNI or JNA. And it won't do you any good ... because the hardware / operating system will prevent the instructions from running. (You will get an illegal instruction trap ... and the program will be killed.)

Why not?

Basically, if a regular Java application (or any other user-space application) could execute privileged instructions, it could trivially crash the operating system. That is a really bad idea.


In a bare-metal implementation of Java, it may be possible to execute privileged instructions via native code, or possibly by other means. (For instance, in JNode the native code compiler will replace certain "magic" method calls with privileged instructions ... under carefully controlled circumstances.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216