Home /Modules/cpu/SLGuestFileSystem.sleela

SLGuestFileSystem.sleela

lib/cpu/SLGuestFileSystem.sleela · 166 line(s) · sleela

<< lib/cpu/SLGuestFileSystem.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuestFileSystem — a minimal in-guest filesystem over
     the guest block device. It presents named files (a flat directory) backed
     by block storage, with an open-file table of descriptors supporting
     sequential read/write by byte. The guest kernel's syscall layer
     (SLGuestSyscall) uses it for open/read/write/close; the loader can read
     program images from it.
   
   This is a guest-side analogue of the host SLFileSystem but scoped to a guest
   VM and reached through the guest block device (SLGuestBlockDevice). Files are
   identified by an integer name/handle (the guest passes an interned id) to fit
   the integer-only syscall ABI; a small fixed directory (8 files) and fd table
   (8 descriptors) are modeled (SLeeLa surface has no arrays).
   
   Family: cpu. Composes SLGuestBlockDevice. >>
#sleela 2.5
material;
class SLGuestFileSystem {
  < ---- 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 = 10230;
  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; }
  static final int MAX_FILES = 8;
  static final int MAX_FDS   = 8;

  SLGuestBlockDevice blk;

  < Directory: file id -> start block, length (bytes), present. >
  int id0; int sb0; int ln0; int p0;
  int id1; int sb1; int ln1; int p1;
  int id2; int sb2; int ln2; int p2;
  int id3; int sb3; int ln3; int p3;
  int fileCount;
  int nextBlock;

  < Open-file table: fd -> file id, cursor (byte offset), open flag. >
  int fdFile0; int fdPos0; int fdOpen0;
  int fdFile1; int fdPos1; int fdOpen1;
  int fdFile2; int fdPos2; int fdOpen2;
  int fdFile3; int fdPos3; int fdOpen3;
  int openCount;

  void configure(SLGuestBlockDevice blockDevice, int dataStartBlock) {
    blk = blockDevice; fileCount = 0; nextBlock = dataStartBlock; openCount = 0;
    p0=0;p1=0;p2=0;p3=0; fdOpen0=0;fdOpen1=0;fdOpen2=0;fdOpen3=0;
  }

  < Create a file (by integer id) occupying `blocks` blocks; returns its index. >
  int create(int fileId, int blocks) {
    int idx = fileCount;
    if (idx >= 4) { return -1; }
    int start = nextBlock;
    if (idx == 0) { id0=fileId; sb0=start; ln0=0; p0=1; }
    if (idx == 1) { id1=fileId; sb1=start; ln1=0; p1=1; }
    if (idx == 2) { id2=fileId; sb2=start; ln2=0; p2=1; }
    if (idx == 3) { id3=fileId; sb3=start; ln3=0; p3=1; }
    nextBlock = nextBlock + blocks;
    fileCount = fileCount + 1;
    return idx;
  }

  int indexOfId(int fileId) {
    if (p0==1 && id0==fileId) { return 0; }
    if (p1==1 && id1==fileId) { return 1; }
    if (p2==1 && id2==fileId) { return 2; }
    if (p3==1 && id3==fileId) { return 3; }
    return -1;
  }
  int startBlock(int idx) {
    if (idx==0){return sb0;} if (idx==1){return sb1;}
    if (idx==2){return sb2;} if (idx==3){return sb3;} return -1;
  }

  < Open a file by id; returns a file descriptor (0..3) or -1. >
  int openByName(int fileId) {
    int fidx = indexOfId(fileId);
    < create on open >
    if (fidx < 0) { fidx = create(fileId, 4); if (fidx < 0) { return -1; } }
    int fd = openCount; if (fd >= 4) { return -1; }
    if (fd==0){ fdFile0=fidx; fdPos0=0; fdOpen0=1; }
    if (fd==1){ fdFile1=fidx; fdPos1=0; fdOpen1=1; }
    if (fd==2){ fdFile2=fidx; fdPos2=0; fdOpen2=1; }
    if (fd==3){ fdFile3=fidx; fdPos3=0; fdOpen3=1; }
    openCount = openCount + 1;
    < reserve 0,1,2 for stdin/out/err >
    return fd + 3;
  }

  boolean fdValid(int fd) { int i = fd - 3; return i >= 0 && i < 4; }

  < Write one byte to an open fd (append semantics, block-device backed); >
  < returns 1 on success. >
  int writeByte(int fd, int value) {
    if (!fdValid(fd)) { return -1; }
    int i = fd - 3;
    int fidx = (i==0)?fdFile0:(i==1)?fdFile1:(i==2)?fdFile2:fdFile3;
    int blockNo = startBlock(fidx);
    if (blk != null) { blk.write(blk.mmioBase+0, blockNo); blk.write(blk.mmioBase+1, value); blk.write(blk.mmioBase+2, 2); }
    bumpLength(fidx);
    return 1;
  }

  < Read one byte from an open fd; returns the byte or -1 at EOF/error. >
  int readByte(int fd) {
    if (!fdValid(fd)) { return -1; }
    int i = fd - 3;
    int fidx = (i==0)?fdFile0:(i==1)?fdFile1:(i==2)?fdFile2:fdFile3;
    int blockNo = startBlock(fidx);
    if (blk != null) { blk.write(blk.mmioBase+0, blockNo); blk.write(blk.mmioBase+2, 1); return blk.read(blk.mmioBase+0); }
    return -1;
  }

  void bumpLength(int fidx) {
    if (fidx==0){ln0=ln0+1;} if (fidx==1){ln1=ln1+1;}
    if (fidx==2){ln2=ln2+1;} if (fidx==3){ln3=ln3+1;}
  }

  int close(int fd) {
    if (!fdValid(fd)) { return -1; }
    int i = fd - 3;
    if (i==0){fdOpen0=0;} if (i==1){fdOpen1=0;} if (i==2){fdOpen2=0;} if (i==3){fdOpen3=0;}
    return 0;
  }

  int files() { return fileCount; }
  int openFiles() { return openCount; }
}