Home /Modules/cpu/SLHypervisor.sleela

SLHypervisor.sleela

lib/cpu/SLHypervisor.sleela · 133 line(s) · sleela

<< lib/cpu/SLHypervisor.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLHypervisor โ€” a type-2 (hosted) hypervisor running on
     the Sleela OS. It creates and schedules guest virtual machines, each with
     its own virtual CPU view, guest physical memory window, and trap-based exit
     handling. This is the layer that lets a guest operating system โ€” e.g. a
     Linux-style kernel โ€” run on top of the Sleela CPU model.
   
   Virtualization model: each guest gets a memory window carved from host RAM
   translated by a real MMU (SLMMU, page tables + protection), a device model
   (SLGuestDeviceBus: timer, console, block device) exposed via guest MMIO, and
   the host core's TRAP vector as the VM-exit path (a guest privileged op/page
   fault/MMIO traps to the hypervisor, which emulates it and resumes the guest).
   Guest code runs on the guest's own SLSleelaVM opcode substrate.
   
   SCOPE NOTE: this is an architectural model of hosted virtualization, not a
   hardware-virtualization (VT-x/AMD-V) implementation and not a bootable
   upstream Linux. See CPU.md and EXECUTION.md for the exact boundary.
   
   Family: cpu. Composes SLCPU/SLCore, SLRAM, SLGuestVM, SLMMU, SLGuestDeviceBus. >>
#sleela 2.5
material;
class SLHypervisor {
  < ---- 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 = 10247;
  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; }
  SLCPU host;
  SLRAM hostMemory;
  < host-backed drive the guest block device uses >
  SLHardDrive bootDisk;
  SLGuestVM g0; SLGuestVM g1; SLGuestVM g2; SLGuestVM g3;
  int guestCount;
  < total VM-exits handled >
  int exits;
  int current;

  void configure(SLCPU hostCpu, SLRAM ram) {
    host = hostCpu; hostMemory = ram; guestCount = 0; exits = 0; current = -1;
  }

  < Optionally provide a host drive to back guests' virtual block devices. >
  void attachBootDisk(SLHardDrive drive) { bootDisk = drive; }

  < Create a guest VM with a guest-physical window [base, base+size) in host RAM. >
  < A per-guest MMU identity-maps the window with full permissions as the boot >
  < address space (the guest kernel may install its own page tables after). >
  SLGuestVM createGuest(String name, int base, int size, int guestEntry) {
    SLGuestVM vm = new SLGuestVM();
    vm.configure(name, host.bootCore(), hostMemory, base, size, guestEntry);

    SLMMU mmu = new SLMMU();
    int pageSize = 256;
    int pages = (size / pageSize) + 1;
    mmu.configure(pageSize, pages);
    mmu.identityMap(pages, base / pageSize, mmu.PERM_WRITE + mmu.PERM_EXEC);
    vm.attachMMU(mmu);

    < Guest device model (step 3): timer (IRQ0), console, block device (IRQ2), >
    < and a guest interrupt controller aggregating their lines. >
    SLGuestTimer timer = new SLGuestTimer(); timer.init(57344, 0);
    SLGuestConsole con = new SLGuestConsole(); con.init(57360);
    SLGuestBlockDevice blk = new SLGuestBlockDevice(); blk.init(57376, 2, bootDisk);
    SLInterruptController gpic = new SLInterruptController(); gpic.configure(60000);
    SLGuestDeviceBus bus = new SLGuestDeviceBus(); bus.configure(timer, con, blk, gpic);
    vm.attachDevices(bus);

    int idx = guestCount;
    if (idx == 0) { g0 = vm; } if (idx == 1) { g1 = vm; }
    if (idx == 2) { g2 = vm; } if (idx == 3) { g3 = vm; }
    guestCount = guestCount + 1;
    < The host core's TRAP vector is the VM-exit entry. >
    host.bootCore().controlUnit().installTrapVector(0);
    return vm;
  }

  SLGuestVM guest(int idx) {
    if (idx == 0) { return g0; } if (idx == 1) { return g1; }
    if (idx == 2) { return g2; } if (idx == 3) { return g3; }
    return g0;
  }

  < Run a guest for a bounded number of guest instructions, handling VM-exits. >
  int runGuest(int idx, int budget) {
    SLGuestVM vm = guest(idx);
    if (vm == null) { return 0; }
    current = idx;
    int ran = vm.run(budget);
    exits = exits + vm.exitCount();
    return ran;
  }

  int guests() { return guestCount; }
  int vmExits() { return exits; }
}