Home /Modules/cpu/SLGuestDeviceBus.sleela

SLGuestDeviceBus.sleela

lib/cpu/SLGuestDeviceBus.sleela · 125 line(s) · sleela

<< lib/cpu/SLGuestDeviceBus.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuestDeviceBus — the virtual device bus the hypervisor
     presents to a guest VM. It holds the guest's devices (timer, console, block
     device), routes a guest MMIO access to the device that owns the address,
     and aggregates device interrupts into the guest's interrupt controller.
     This is the "chipset" of the guest machine (step 3 of the OS-VM roadmap).
   
   MMIO dispatch: a guest read/write to a device-owned address is a VM-exit the
   hypervisor services by calling the owning device's register handler. IRQ
   aggregation: after a tick, any device with a raised line is posted to the
   guest interrupt controller for the guest kernel to service.
   
   Family: cpu. Composes SLGuestTimer/Console/BlockDevice and SLInterruptController. >>
#sleela 2.5
material;
class SLGuestDeviceBus {
  < ---- 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 = 10228;
  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; }
  SLGuestTimer timer;
  SLGuestConsole console;
  SLGuestBlockDevice blk;
  < guest interrupt controller >
  SLInterruptController pic;
  int mmioAccesses;

  void configure(SLGuestTimer t, SLGuestConsole c, SLGuestBlockDevice b, SLInterruptController controller) {
    timer = t; console = c; blk = b; pic = controller;
    mmioAccesses = 0;
    if (timer != null && timer.irq() >= 0) { pic.enable(timer.irq()); }
    if (blk != null && blk.irq() >= 0) { pic.enable(blk.irq()); }
  }

  < Route a guest MMIO read to the owning device. >
  int mmioRead(int addr) {
    mmioAccesses = mmioAccesses + 1;
    if (timer != null && timer.owns(addr)) { return timer.read(addr); }
    if (console != null && console.owns(addr)) { return console.read(addr); }
    if (blk != null && blk.owns(addr)) { return blk.read(addr); }
    return 0;
  }

  < Route a guest MMIO write to the owning device. >
  void mmioWrite(int addr, int value) {
    mmioAccesses = mmioAccesses + 1;
    if (timer != null && timer.owns(addr)) { timer.write(addr, value); return; }
    if (console != null && console.owns(addr)) { console.write(addr, value); return; }
    if (blk != null && blk.owns(addr)) { blk.write(addr, value); return; }
  }

  < Advance device time by one tick and post any raised interrupts into the >
  < guest interrupt controller. >
  void tick() {
    if (timer != null) {
      timer.tick();
      if (timer.irqRaised()) { pic.raise(timer.irq()); timer.clearIRQ(); }
    }
    if (blk != null && blk.irqRaised()) { pic.raise(blk.irq()); blk.clearIRQ(); }
  }

  < The guest kernel's ISR dispatch asks for the highest-priority pending IRQ. >
  int pendingInterrupt() { if (pic.hasPending()) { return pic.highestPriorityPending(); } return -1; }
  void acknowledge(int line) { pic.acknowledge(line); }

  < Console convenience for the hypervisor's VM-exit router: write one char to >
  < the guest console device's data register. >
  boolean hasConsole() { return console != null; }
  void consoleWrite(int code) { if (console != null) { console.write(console.mmioBase, code); } }

  < Program and enable the guest timer (reload value + enable) so its expiries >
  < drive the guest scheduler. Convenience for the guest kernel bring-up. >
  void programTimer(int reload) {
    if (timer != null) {
      < reload register >
      timer.write(timer.mmioBase + 0, reload);
      < control: enable >
      timer.write(timer.mmioBase + 2, 1);
    }
  }
  SLGuestTimer timerDevice() { return timer; }
  SLGuestBlockDevice blockDevice() { return blk; }

  int mmioCount() { return mmioAccesses; }
  SLInterruptController interruptController() { return pic; }
}