#! @file system.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what a program asks of the system: its calls, which the machine #! makes as they are asked through `system::linux::syscall`, the variables it #! started with, and the signals it handles, which the machine has `system` #! note for it, so that the program's handlers run at its own safe points. import system; module sakura::machine; #! The number of the call that writes. const call_write: u64 = 1; #! Makes a system call a program asks for, failing as the kernel does, with #! the negated error number. What a program writes to its output or error is #! kept instead where the machine is asked to. #! @param number The call. #! @param a Its first argument. #! @param b Its second. #! @param c Its third. #! @param d Its fourth. #! @param e Its fifth. #! @param f Its sixth. #! @param running The machine. #! @returns What the call gives. pub def system_call(number: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64, running: &mut interpreter): u64 { if running.options.capture && number == call_write && (a == 1 || a == 2) { val bytes := b as *u8; for i in 0..c { running.output.append(bytes[i]); } return c; } return host_call(number, a, b, c, d, e, f); } when os == linux { # Makes the call, as the system running the machine is Linux. def host_call(number: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64): u64 { return system::linux::syscall(number as i64, a, b, c, d, e, f) as u64; } } when os == windows { # Windows has no calls a program makes itself, so a program checked for it # calls C instead, as `system` does there. What a program writes to its # output or error through the call of Linux is written still, as the # machine's tests write it, and every other call fails as Linux fails one it # does not have. def host_call(number: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64): u64 { if number == call_write && (a == 1 || a == 2) { val to: system::fd = a == 1 ? system::fd::stdout : system::fd::stderr; return to.write(b as *u8, c) as u64; } return (-38 as i64) as u64; } } #! Where the system put the variables the program started with: pointers to #! text ending in a zero, then a null pointer, made once. #! @param this The machine. #! @returns The address of the first pointer. pub def interpreter::environment(&mut this): u64 { return this.environ if this.environ != 0; val each := system::environment(); val made := raw_allocate_zeroed((each.length + 1) * 8) as *mut u64; for i in 0..each.length { made[i] = each[i].data as u64; } this.environ = made as u64; # Running with its memory checked, the program holds the pointers and the # text of each, to the zero ending it, as it does memory it mapped. if this.options.checks { this.add_run(made as u64, (each.length + 1) * 8); for i in 0..each.length { this.add_run(each[i].data as u64, each[i].length + 1); } } return this.environ; } #! The signals that came since the program last asked, one bit each by the #! system's number, as `system` noted them for the machine. pub var noted_signals: u64; #! Gathers the signals `system` noted for the machine since it last asked. #! @returns Whether any came that the program has not asked for. pub def signals_came(): bool { noted_signals = noted_signals | system::watched(); return noted_signals != 0; } when os == linux { #! Has a signal of the system's number noted when it comes, as a program asks. #! The machine asks itself whether one came, at the program's safe points, so #! no handler runs, and the machine's own code has no safe points. #! @param number The signal's number. #! @returns Zero, or the negated error number. pub def catch_signal(number: i64): i64 { return match system::signal_of(number) { some(which) => system::watch(which) as i64, else => -22 }; } } when os == windows { #! Has a signal of the system's number noted when it comes, thrown away, done #! as the system does by default, noted now, or gives the handle of the event #! the system sets where a signal is noted, as the bits above the number say, #! as a program asks. The machine asks the system running it to do each, so a #! signal reaches a program the machine runs as it does an executable, and a #! wait of the program's ends on one as the machine's own waits do. No handler #! of the machine's own runs, and the machine's own code has no safe points. #! @param number The signal's number, with what to do in the bits above it: #! 0 note it, 1 throw it away, 2 do as the system does, 3 note #! it now, 4 give the event's handle. #! @returns Zero, the event's handle, or the negated error number. pub def catch_signal(number: i64): i64 { val action := (number >> 8) & 0xFF; return match system::signal_of(number & 0xFF) { some(which) => match action { 0 => system::watch(which) as i64, 1 => system::ignore(which) as i64, 2 => system::restore(which) as i64, 3 => system::raise(which) as i64, 4 => system::signal_event() as i64, else => -22 }, else => action == 4 ? system::signal_event() as i64 : -22 }; } }