From d8eaa13f2efc1eeca4e5f230d5a2d535aaf27338 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 2 Jan 2026 00:47:16 +0800 Subject: dev(file-system): establish VFS and Read-only FAT32 File System --- include/kernel/buf.h | 30 ++++++++++++++++++++++++++++++ include/kernel/elf.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ include/kernel/exec.h | 6 ++++++ include/kernel/mm.h | 1 + include/kernel/proc_def.h | 6 ++++++ 5 files changed, 89 insertions(+) create mode 100644 include/kernel/buf.h create mode 100644 include/kernel/elf.h create mode 100644 include/kernel/exec.h (limited to 'include/kernel') diff --git a/include/kernel/buf.h b/include/kernel/buf.h new file mode 100644 index 0000000..099523d --- /dev/null +++ b/include/kernel/buf.h @@ -0,0 +1,30 @@ +#pragma once +#include "common/types.h" +#include "kernel/spinlock.h" + +#define BSIZE 1024 +#define NBUF 30 + +struct Buf +{ + int valid; // Whether the data has been read from the disk (1=valid, 0=invalid) + int disk; // Whether it has been held by the driver (dirty bit/write-back flag is usually extended here) + uint32 dev; // Device Number + uint32 blockno; // Block number + uint32 refcnt; // Reference Counting + struct Buf *prev; // LRU linked list + struct Buf *next; + uchar data[BSIZE]; + + Mutex lock; // Sleep Lock +}; + +namespace BufferCache +{ + void init(); + struct Buf *bread(uint32 dev, uint32 blockno); + void bwrite(struct Buf *b); + void brelse(struct Buf *b); + void bpin(struct Buf *b); + void bunpin(struct Buf *b); +} // namespace BufferCache diff --git a/include/kernel/elf.h b/include/kernel/elf.h new file mode 100644 index 0000000..c216f1c --- /dev/null +++ b/include/kernel/elf.h @@ -0,0 +1,46 @@ +#pragma once +#include "common/types.h" + +#define ELF_MAGIC 0x464C457F // "\x7FELF" in little endian + +// ELF File Header +struct elfhdr +{ + uint32 magic; // Must equal ELF_MAGIC + uint8 elf[12]; + uint16 type; + uint16 machine; + uint32 version; + uint64 entry; // Program Entry Point (Virtual Address) + uint64 phoff; // File offset of the Program Header Table + uint64 shoff; + uint32 flags; + uint16 ehsize; + uint16 phentsize; + uint16 phnum; // Number of Program Headers + uint16 shentsize; + uint16 shnum; + uint16 shstrndx; +}; + +// Program Header +// Describes how to load segments from a file into memory +struct proghdr +{ + uint32 type; + uint32 flags; + uint64 off; // Offset of the section in the file + uint64 vaddr; // Virtual address of the segment in memory + uint64 paddr; + uint64 filesz; // Size of the section in the file + uint64 memsz; // The size of the segment in memory (memsz >= filesz) + uint64 align; +}; + +// Values for Proghdr type +#define ELF_PROG_LOAD 1 + +// Flag bits for Proghdr flags +#define ELF_PROG_FLAG_EXEC 1 +#define ELF_PROG_FLAG_WRITE 2 +#define ELF_PROG_FLAG_READ 4 \ No newline at end of file diff --git a/include/kernel/exec.h b/include/kernel/exec.h new file mode 100644 index 0000000..964e3bb --- /dev/null +++ b/include/kernel/exec.h @@ -0,0 +1,6 @@ +#pragma once + +namespace Exec +{ + int exec(char *path, char **argv); +} \ No newline at end of file diff --git a/include/kernel/mm.h b/include/kernel/mm.h index 363ce74..1850574 100644 --- a/include/kernel/mm.h +++ b/include/kernel/mm.h @@ -18,6 +18,7 @@ namespace VM int copyout(uint64 *pagetable, uint64 dstva, char *src, uint64 len); int copyin(uint64 *pagetable, char *dst, uint64 srcva, uint64 len); + int copyinstr(uint64 *pagetable, char *dst, uint64 srcva, uint64 max); int handle_cow_fault(uint64 *pagetable, uint64 va); int mappages(uint64 *pagetable, uint64 va, uint64 size, uint64 pa, int perm); diff --git a/include/kernel/proc_def.h b/include/kernel/proc_def.h index e5a2cbc..06c80aa 100644 --- a/include/kernel/proc_def.h +++ b/include/kernel/proc_def.h @@ -1,8 +1,11 @@ #pragma once #include "common/types.h" +#include "common/fcntl.h" struct cpu; struct Trapframe; +class Inode; +struct file; enum ProcState { @@ -51,6 +54,9 @@ struct Proc void *chan; int xstate; + Inode *cwd; + struct file *ofile[NOFILE]; + struct Proc *next; struct Proc *prev; int priority; -- cgit v1.2.3