runtimejs / runtime

[not maintained] Lightweight JavaScript library operating system for the cloud
http://runtimejs.org
Apache License 2.0
1.93k stars 128 forks source link

Calling Assembly #139

Open RossComputerGuy opened 7 years ago

RossComputerGuy commented 7 years ago

I'm trying to execute assembly and I'm having a problem figuring out how to impliment this system call in the NativesObject class. Does anyone know the best way? I think that if I have an enum that has all of the assembly opcodes that I could store them inside of a map so when someone executes the function it would read the map and execute the opcode.

Current Code:

NATIVE_FUNCTION(NativesObject,Assembly) {
  USEARG(0);
  char* cmd = (arg0.As<v8::String>())->Value();
  if(strcmp(cmd,"cli") == 0) {
   asm volatile ("cli");
  } else if(strcmp(cmd,"sti") == 0) {
   asm volatile ("sti");
  } else if(strcmp(cmd,"hlt") == 0) {
   asm volatile ("hlt");
  } else {
   args.GetReturnValue().Set(v8::Boolean::New(iv8,false));
  }
  args.GetReturnValue().Set(v8::Boolean::New(iv8,true));
}
iefserge commented 7 years ago

@SpaceboyRoss01 Might be easier to create an ArrayBuffer with x86-64 instructions in js (I think i've seen npm module for this), then pass it to a native function that'll run it. Just curious, what are you trying to do with it? 😃

RossComputerGuy commented 7 years ago

I'm going to make some things work better for some drivers.

piranna commented 7 years ago

I would not go that way... forget about assembler at all, you can do the same in plain C if you need some performance. So far only needed things to write the drivers in Javascript are bindings for memory access, catch interruptions and read & write on ports, everything else can be done in high-level Javascript.

RossComputerGuy commented 7 years ago

How many instructions are in assembly

RossComputerGuy commented 7 years ago

I'm trying to get to usermode in javascript

piranna commented 7 years ago

How many instructions are in assembly

It depends of the CPU, but about 200-300 is a rought estimation.

I'm trying to get to usermode in javascript

Are you trying to move from ring 0 to ring 3? Why? The purposse of runtime.js is just to have everything in ring 0 for performance and apply a language-based security system...

RossComputerGuy commented 7 years ago

I'm also doing this to work on executing binary files

facekapow commented 7 years ago

If it's mainly for performance reasons, you could write the drivers in C or C++ and compile them to WebAssembly via Binaryen. After that you load them by requiring the produced JavaScript file and wrapping the necessary functions so you can call them later.

You should know that you're going to be left in a mess if you try to do this. It's much cleaner to translate the programs to JavaScript, and also it's much more foolproof (compiling and running binaries on top of runtime.js is going to be very error prone).

By the way, if all you plan on doing is being able to execute assembly and just loading in programs on top of that, you're going to end up compiling the whole Linux kernel as well, because all programs depend on kernel instructions somewhere.