Home /Modules/cpu/SLGuestInit.sleela

SLGuestInit.sleela

lib/cpu/SLGuestInit.sleela · 207 line(s) · sleela

<< lib/cpu/SLGuestInit.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuestInit — the guest's init process (PID 1). After the
     guest kernel boots, init loads user programs (SLGuestProgram) into the
     guest substrate and hands each to the guest scheduler as a task
     (SLGuestTask). This is how a guest OS goes from "kernel running" to "running
     user processes": init is the first task and the parent of the rest.
   
   Models the Unix init contract: PID 1 is spawned by the kernel, then spawns
   the system's initial programs; the timer-driven scheduler multitasks them.
   
   Family: cpu. Composes SLGuestLinux, SLGuestProgram, SLGuestTask, SLSleelaVM. >>
#sleela 2.5
material;
class SLGuestInit {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10231;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-06";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLGuestLinux kernel;
  SLSleelaVM substrate;
  int nextPid;
  int spawned;

  void configure(SLGuestLinux guestKernel, SLSleelaVM guestSubstrate) {
    kernel = guestKernel; substrate = guestSubstrate;
    < PID 1 is init itself; children start at 2 >
    nextPid = 2;
    spawned = 0;
  }

  < Single pid allocator for the whole guest: init-spawned programs and >
  < fork()'d children both draw from this counter so pids never collide. >
  int allocPid() { int pid = nextPid; nextPid = nextPid + 1; return pid; }

  < Load a user program into the guest substrate and admit it as a task. The >
  < program's load base becomes the task's entry/resume IP. >
  SLGuestTask spawn(SLGuestProgram program) {
    int base = program.loadInto(substrate);
    int pid = allocPid();
    SLGuestTask task = kernel.spawnTask(pid, program.programName(), base);
    spawned = spawned + 1;
    return task;
  }

  < Compile a program from source and spawn it (exec-style convenience). >
  SLGuestTask exec(String name, String sourcePath, int langId) {
    SLGuestProgram prog = new SLGuestProgram(); prog.configure(name, 4096);
    prog.buildFrom(sourcePath, langId);
    return spawn(prog);
  }

  < Load a program FROM DISK: given a prepared executable image, load it into >
  < the guest substrate and admit it as a task. This is the "run a program from >
  < the filesystem" path (as opposed to inline compilation via exec()). >
  SLGuestTask execFromImage(String name, SLGuestExecutable image) {
    int base = image.loadInto(substrate);
    < invalid image >
    if (base < 0) { return null; }
    int pid = allocPid();
    SLGuestTask task = kernel.spawnTask(pid, name, base);
    spawned = spawned + 1;
    return task;
  }

  < Compile a program, serialize it to the on-disk executable format, write it >
  < to the guest filesystem under `fileId`, then load it back from disk and >
  < spawn it — the full compile -> install -> load-from-disk -> run cycle. >
  SLGuestTask installAndExec(String name, String sourcePath, int langId, int fileId) {
    SLGuestProgram prog = new SLGuestProgram(); prog.configure(name, 4096);
    prog.buildFrom(sourcePath, langId);
    SLGuestExecutable exe = new SLGuestExecutable(); exe.configure(prog.size() * 2 + 8);
    exe.serialize(prog, 0);
    SLGuestFileSystem fs = kernel.fileSystem();
    < persist to disk >
    if (fs != null) { exe.writeToFile(fs, fileId); }
    < load the image back + run >
    return execFromImage(name, exe);
  }

  < execve support: replace an EXISTING task's image with a freshly compiled >
  < program, loaded into the shared guest substrate, and repoint that task's >
  < resume IP at the new image's entry. Unlike spawn()/exec(), this does NOT >
  < create a new task — the caller keeps its tid and scheduling slot but now >
  < runs different code, exactly as Unix execve() replaces the process image in >
  < place. The program is resolved through the same toolchain; progId/srcId are >
  < modeled identifiers the kernel maps to a source path/name. Returns the new >
  < entry index, or -1 on failure. >
  int execInto(SLGuestTask task, int progId, int srcId, int langId) {
    if (task == null) { return -1; }
    SLGuestProgram prog = new SLGuestProgram();
    prog.configure(execName(progId), 4096);
    prog.buildFrom(execSourcePath(srcId), langId);
    int base = prog.loadInto(substrate);
    if (base < 0) { return -1; }
    < the task resumes in the new image >
    task.saveIP(base);
    task.markReady();
    < count the exec as a program launch >
    spawned = spawned + 1;
    return base;
  }

  < Map a modeled program/source identifier to a name/path. Kept trivial and >
  < deterministic so the exec path is reproducible in the model; a real kernel >
  < would resolve these from the execve() argument vector. >
  String execName(int progId) { return "execd"; }
  String execSourcePath(int srcId) { return "guest/exec.sleela"; }

  < Launch the guest shell as init's first child: build a shell bound to the >
  < kernel's syscall table and admit it as a task (the first interactive user >
  < program after boot). Returns the shell object so a driver can feed it >
  < commands. >
  SLGuestShell launchShell() {
    SLGuestShell shell = new SLGuestShell();
    shell.configure(kernel.syscallTable());
    < Register the shell as a task so the scheduler accounts for it. >
    int pid = allocPid();
    kernel.spawnTask(pid, "sh", 0);
    spawned = spawned + 1;
    return shell;
  }

  < Demonstrate the full process-creation cycle through the real syscall ABI: >
  < spawn a base task, then from that task's context fork a child, execve the >
  < child onto a freshly compiled image, let the child exit, and wait() to reap >
  < it. Returns the reaped child's tid (>0) on success, or a negative error. >
  < This is the end-to-end fork/exec/wait path a Unix shell relies on. >
  int forkExecWaitDemo(int childLang) {
    SLGuestSyscall sys = kernel.syscallTable();
    SLGuestScheduler sched = kernel.taskScheduler();

    < A parent task must be the "running" task so fork()/wait() have a caller. >
    SLGuestProgram parentProg = new SLGuestProgram();
    parentProg.configure("parent", 64);
    < (modeled parent body) >
    parentProg.append(28, 0);
    SLGuestTask parent = spawn(parentProg);
    < make the parent the running task >
    sched.onTimerTick();

    < fork() >
    int childTid = sys.dispatch(sys.SYS_FORK, 0, 0, 0);
    if (childTid < 0) { return childTid; }

    < execve() the child onto a new image, then let the CHILD itself issue the >
    < terminating exit(status) syscall — the exit flows through the real >
    < SYS_EXIT path (which records the status on the running task), so the >
    < zombie wait() later reaps is produced by the child, not fabricated. >
    int childSlot = sched.slotOfTid(childTid);
    sched.markRunningSlot(childSlot);
    < execve() >
    int entry = sys.dispatch(sys.SYS_EXECVE, 1, 1, childLang);
    if (entry < 0) { return entry; }
    < child is current >
    sched.markRunningSlot(childSlot);
    < child exit(42) >
    sys.dispatch(sys.SYS_EXIT, 42, 0, 0);

    < Back in the parent, wait() reaps the finished child and reports status 42. >
    sched.markRunningSlot(sched.slotOfTid(parent.taskId()));
    < wait() >
    int reaped = sys.dispatch(sys.SYS_WAIT, 0, 0, 0);
    return reaped;
  }

  int pid() { return 1; }
  int childrenSpawned() { return spawned; }
}