Formatted *.h files

feat/start
hheik 2024-04-30 00:59:43 +03:00
parent d9faacecaf
commit 8d44d9c964
18 changed files with 555 additions and 649 deletions

View File

@ -9,4 +9,3 @@ struct buf {
struct buf *next; struct buf *next;
uchar data[BSIZE]; uchar data[BSIZE];
}; };

View File

@ -11,11 +11,11 @@ struct superblock;
// bio.c // bio.c
void binit(void); void binit(void);
struct buf* bread(uint, uint); struct buf *bread(uint, uint);
void brelse(struct buf*); void brelse(struct buf *);
void bwrite(struct buf*); void bwrite(struct buf *);
void bpin(struct buf*); void bpin(struct buf *);
void bunpin(struct buf*); void bunpin(struct buf *);
// console.c // console.c
void consoleinit(void); void consoleinit(void);
@ -23,62 +23,62 @@ void consoleintr(int);
void consputc(int); void consputc(int);
// exec.c // exec.c
int exec(char*, char**); int exec(char *, char **);
// file.c // file.c
struct file* filealloc(void); struct file *filealloc(void);
void fileclose(struct file*); void fileclose(struct file *);
struct file* filedup(struct file*); struct file *filedup(struct file *);
void fileinit(void); void fileinit(void);
int fileread(struct file*, uint64, int n); int fileread(struct file *, uint64, int n);
int filestat(struct file*, uint64 addr); int filestat(struct file *, uint64 addr);
int filewrite(struct file*, uint64, int n); int filewrite(struct file *, uint64, int n);
// fs.c // fs.c
void fsinit(int); void fsinit(int);
int dirlink(struct inode*, char*, uint); int dirlink(struct inode *, char *, uint);
struct inode* dirlookup(struct inode*, char*, uint*); struct inode *dirlookup(struct inode *, char *, uint *);
struct inode* ialloc(uint, short); struct inode *ialloc(uint, short);
struct inode* idup(struct inode*); struct inode *idup(struct inode *);
void iinit(); void iinit();
void ilock(struct inode*); void ilock(struct inode *);
void iput(struct inode*); void iput(struct inode *);
void iunlock(struct inode*); void iunlock(struct inode *);
void iunlockput(struct inode*); void iunlockput(struct inode *);
void iupdate(struct inode*); void iupdate(struct inode *);
int namecmp(const char*, const char*); int namecmp(const char *, const char *);
struct inode* namei(char*); struct inode *namei(char *);
struct inode* nameiparent(char*, char*); struct inode *nameiparent(char *, char *);
int readi(struct inode*, int, uint64, uint, uint); int readi(struct inode *, int, uint64, uint, uint);
void stati(struct inode*, struct stat*); void stati(struct inode *, struct stat *);
int writei(struct inode*, int, uint64, uint, uint); int writei(struct inode *, int, uint64, uint, uint);
void itrunc(struct inode*); void itrunc(struct inode *);
// ramdisk.c // ramdisk.c
void ramdiskinit(void); void ramdiskinit(void);
void ramdiskintr(void); void ramdiskintr(void);
void ramdiskrw(struct buf*); void ramdiskrw(struct buf *);
// kalloc.c // kalloc.c
void* kalloc(void); void *kalloc(void);
void kfree(void *); void kfree(void *);
void kinit(void); void kinit(void);
// log.c // log.c
void initlog(int, struct superblock*); void initlog(int, struct superblock *);
void log_write(struct buf*); void log_write(struct buf *);
void begin_op(void); void begin_op(void);
void end_op(void); void end_op(void);
// pipe.c // pipe.c
int pipealloc(struct file**, struct file**); int pipealloc(struct file **, struct file **);
void pipeclose(struct pipe*, int); void pipeclose(struct pipe *, int);
int piperead(struct pipe*, uint64, int); int piperead(struct pipe *, uint64, int);
int pipewrite(struct pipe*, uint64, int); int pipewrite(struct pipe *, uint64, int);
// printf.c // printf.c
void printf(char*, ...); void printf(char *, ...);
void panic(char*) __attribute__((noreturn)); void panic(char *) __attribute__((noreturn));
void printfinit(void); void printfinit(void);
// proc.c // proc.c
@ -90,55 +90,55 @@ void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *); pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64); void proc_freepagetable(pagetable_t, uint64);
int kill(int); int kill(int);
int killed(struct proc*); int killed(struct proc *);
void setkilled(struct proc*); void setkilled(struct proc *);
struct cpu* mycpu(void); struct cpu *mycpu(void);
struct cpu* getmycpu(void); struct cpu *getmycpu(void);
struct proc* myproc(); struct proc *myproc();
void procinit(void); void procinit(void);
void scheduler(void) __attribute__((noreturn)); void scheduler(void) __attribute__((noreturn));
void sched(void); void sched(void);
void sleep(void*, struct spinlock*); void sleep(void *, struct spinlock *);
void userinit(void); void userinit(void);
int wait(uint64); int wait(uint64);
void wakeup(void*); void wakeup(void *);
void yield(void); void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len); int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len); int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void); void procdump(void);
// swtch.S // swtch.S
void swtch(struct context*, struct context*); void swtch(struct context *, struct context *);
// spinlock.c // spinlock.c
void acquire(struct spinlock*); void acquire(struct spinlock *);
int holding(struct spinlock*); int holding(struct spinlock *);
void initlock(struct spinlock*, char*); void initlock(struct spinlock *, char *);
void release(struct spinlock*); void release(struct spinlock *);
void push_off(void); void push_off(void);
void pop_off(void); void pop_off(void);
// sleeplock.c // sleeplock.c
void acquiresleep(struct sleeplock*); void acquiresleep(struct sleeplock *);
void releasesleep(struct sleeplock*); void releasesleep(struct sleeplock *);
int holdingsleep(struct sleeplock*); int holdingsleep(struct sleeplock *);
void initsleeplock(struct sleeplock*, char*); void initsleeplock(struct sleeplock *, char *);
// string.c // string.c
int memcmp(const void*, const void*, uint); int memcmp(const void *, const void *, uint);
void* memmove(void*, const void*, uint); void *memmove(void *, const void *, uint);
void* memset(void*, int, uint); void *memset(void *, int, uint);
char* safestrcpy(char*, const char*, int); char *safestrcpy(char *, const char *, int);
int strlen(const char*); int strlen(const char *);
int strncmp(const char*, const char*, uint); int strncmp(const char *, const char *, uint);
char* strncpy(char*, const char*, int); char *strncpy(char *, const char *, int);
// syscall.c // syscall.c
void argint(int, int*); void argint(int, int *);
int argstr(int, char*, int); int argstr(int, char *, int);
void argaddr(int, uint64 *); void argaddr(int, uint64 *);
int fetchstr(uint64, char*, int); int fetchstr(uint64, char *, int);
int fetchaddr(uint64, uint64*); int fetchaddr(uint64, uint64 *);
void syscall(); void syscall();
// trap.c // trap.c
@ -168,7 +168,7 @@ int uvmcopy(pagetable_t, pagetable_t, uint64);
void uvmfree(pagetable_t, uint64); void uvmfree(pagetable_t, uint64);
void uvmunmap(pagetable_t, uint64, uint64, int); void uvmunmap(pagetable_t, uint64, uint64, int);
void uvmclear(pagetable_t, uint64); void uvmclear(pagetable_t, uint64);
pte_t * walk(pagetable_t, uint64, int); pte_t *walk(pagetable_t, uint64, int);
uint64 walkaddr(pagetable_t, uint64); uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64); int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64); int copyin(pagetable_t, char *, uint64, uint64);
@ -186,4 +186,4 @@ void virtio_disk_rw(struct buf *, int);
void virtio_disk_intr(void); void virtio_disk_intr(void);
// number of elements in fixed-size array // number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0])) #define NELEM(x) (sizeof(x) / sizeof((x)[0]))

View File

@ -11,7 +11,7 @@ struct file {
#define major(dev) ((dev) >> 16 & 0xFFFF) #define major(dev) ((dev) >> 16 & 0xFFFF)
#define minor(dev) ((dev) & 0xFFFF) #define minor(dev) ((dev) & 0xFFFF)
#define mkdev(m,n) ((uint)((m)<<16| (n))) #define mkdev(m, n) ((uint)((m) << 16 | (n)))
// in-memory copy of an inode // in-memory copy of an inode
struct inode { struct inode {
@ -26,7 +26,7 @@ struct inode {
short minor; short minor;
short nlink; short nlink;
uint size; uint size;
uint addrs[NDIRECT+1]; uint addrs[NDIRECT + 1];
}; };
// map major device number to device functions. // map major device number to device functions.

View File

@ -1,7 +1,6 @@
// On-disk file system format. // On-disk file system format.
// Both the kernel and user programs use this header file. // Both the kernel and user programs use this header file.
#define ROOTINO 1 // root i-number #define ROOTINO 1 // root i-number
#define BSIZE 1024 // block size #define BSIZE 1024 // block size
@ -35,7 +34,7 @@ struct dinode {
short minor; // Minor device number (T_DEVICE only) short minor; // Minor device number (T_DEVICE only)
short nlink; // Number of links to inode in file system short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes) uint size; // Size of file (bytes)
uint addrs[NDIRECT+1]; // Data block addresses uint addrs[NDIRECT + 1]; // Data block addresses
}; };
// Inodes per block. // Inodes per block.
@ -45,10 +44,10 @@ struct dinode {
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart) #define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
// Bitmap bits per block // Bitmap bits per block
#define BPB (BSIZE*8) #define BPB (BSIZE * 8)
// Block of free map containing bit for block b // Block of free map containing bit for block b
#define BBLOCK(b, sb) ((b)/BPB + sb.bmapstart) #define BBLOCK(b, sb) ((b) / BPB + sb.bmapstart)
// Directory is a file containing a sequence of dirent structures. // Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14 #define DIRSIZ 14
@ -57,4 +56,3 @@ struct dirent {
ushort inum; ushort inum;
char name[DIRSIZ]; char name[DIRSIZ];
}; };

View File

@ -3,9 +3,7 @@
#include "memlayout.h" #include "memlayout.h"
#include "riscv.h" #include "riscv.h"
#include "defs.h" #include "defs.h"
#include <stdint.h> #include "rust.h"
extern int32_t add(int32_t right, int32_t left);
volatile static int started = 0; volatile static int started = 0;

View File

@ -27,25 +27,25 @@
// core local interruptor (CLINT), which contains the timer. // core local interruptor (CLINT), which contains the timer.
#define CLINT 0x2000000L #define CLINT 0x2000000L
#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid)) #define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8 * (hartid))
#define CLINT_MTIME (CLINT + 0xBFF8) // cycles since boot. #define CLINT_MTIME (CLINT + 0xBFF8) // cycles since boot.
// qemu puts platform-level interrupt controller (PLIC) here. // qemu puts platform-level interrupt controller (PLIC) here.
#define PLIC 0x0c000000L #define PLIC 0x0c000000L
#define PLIC_PRIORITY (PLIC + 0x0) #define PLIC_PRIORITY (PLIC + 0x0)
#define PLIC_PENDING (PLIC + 0x1000) #define PLIC_PENDING (PLIC + 0x1000)
#define PLIC_MENABLE(hart) (PLIC + 0x2000 + (hart)*0x100) #define PLIC_MENABLE(hart) (PLIC + 0x2000 + (hart) * 0x100)
#define PLIC_SENABLE(hart) (PLIC + 0x2080 + (hart)*0x100) #define PLIC_SENABLE(hart) (PLIC + 0x2080 + (hart) * 0x100)
#define PLIC_MPRIORITY(hart) (PLIC + 0x200000 + (hart)*0x2000) #define PLIC_MPRIORITY(hart) (PLIC + 0x200000 + (hart) * 0x2000)
#define PLIC_SPRIORITY(hart) (PLIC + 0x201000 + (hart)*0x2000) #define PLIC_SPRIORITY(hart) (PLIC + 0x201000 + (hart) * 0x2000)
#define PLIC_MCLAIM(hart) (PLIC + 0x200004 + (hart)*0x2000) #define PLIC_MCLAIM(hart) (PLIC + 0x200004 + (hart) * 0x2000)
#define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart)*0x2000) #define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart) * 0x2000)
// the kernel expects there to be RAM // the kernel expects there to be RAM
// for use by the kernel and user pages // for use by the kernel and user pages
// from physical address 0x80000000 to PHYSTOP. // from physical address 0x80000000 to PHYSTOP.
#define KERNBASE 0x80000000L #define KERNBASE 0x80000000L
#define PHYSTOP (KERNBASE + 128*1024*1024) #define PHYSTOP (KERNBASE + 128 * 1024 * 1024)
// map the trampoline page to the highest address, // map the trampoline page to the highest address,
// in both user and kernel space. // in both user and kernel space.
@ -53,7 +53,7 @@
// map kernel stacks beneath the trampoline, // map kernel stacks beneath the trampoline,
// each surrounded by invalid guard pages. // each surrounded by invalid guard pages.
#define KSTACK(p) (TRAMPOLINE - ((p)+1)* 2*PGSIZE) #define KSTACK(p) (TRAMPOLINE - ((p) + 1) * 2 * PGSIZE)
// User memory layout. // User memory layout.
// Address zero first: // Address zero first:

View File

@ -7,7 +7,7 @@
#define ROOTDEV 1 // device number of file system root disk #define ROOTDEV 1 // device number of file system root disk
#define MAXARG 32 // max exec arguments #define MAXARG 32 // max exec arguments
#define MAXOPBLOCKS 10 // max # of blocks any FS op writes #define MAXOPBLOCKS 10 // max # of blocks any FS op writes
#define LOGSIZE (MAXOPBLOCKS*3) // max data blocks in on-disk log #define LOGSIZE (MAXOPBLOCKS * 3) // max data blocks in on-disk log
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache #define NBUF (MAXOPBLOCKS * 3) // size of disk block cache
#define FSSIZE 2000 // size of file system in blocks #define FSSIZE 2000 // size of file system in blocks
#define MAXPATH 128 // maximum file path name #define MAXPATH 128 // maximum file path name

View File

@ -1,11 +1,9 @@
#ifndef __ASSEMBLER__ #ifndef __ASSEMBLER__
// which hart (core) is this? // which hart (core) is this?
static inline uint64 static inline uint64 r_mhartid() {
r_mhartid()
{
uint64 x; uint64 x;
asm volatile("csrr %0, mhartid" : "=r" (x) ); asm volatile("csrr %0, mhartid" : "=r"(x));
return x; return x;
} }
@ -17,27 +15,21 @@ r_mhartid()
#define MSTATUS_MPP_U (0L << 11) #define MSTATUS_MPP_U (0L << 11)
#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable. #define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable.
static inline uint64 static inline uint64 r_mstatus() {
r_mstatus()
{
uint64 x; uint64 x;
asm volatile("csrr %0, mstatus" : "=r" (x) ); asm volatile("csrr %0, mstatus" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_mstatus(uint64 x) {
w_mstatus(uint64 x) asm volatile("csrw mstatus, %0" : : "r"(x));
{
asm volatile("csrw mstatus, %0" : : "r" (x));
} }
// machine exception program counter, holds the // machine exception program counter, holds the
// instruction address to which a return from // instruction address to which a return from
// exception will go. // exception will go.
static inline void static inline void w_mepc(uint64 x) {
w_mepc(uint64 x) asm volatile("csrw mepc, %0" : : "r"(x));
{
asm volatile("csrw mepc, %0" : : "r" (x));
} }
// Supervisor Status Register, sstatus // Supervisor Status Register, sstatus
@ -48,152 +40,108 @@ w_mepc(uint64 x)
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable #define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
#define SSTATUS_UIE (1L << 0) // User Interrupt Enable #define SSTATUS_UIE (1L << 0) // User Interrupt Enable
static inline uint64 static inline uint64 r_sstatus() {
r_sstatus()
{
uint64 x; uint64 x;
asm volatile("csrr %0, sstatus" : "=r" (x) ); asm volatile("csrr %0, sstatus" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_sstatus(uint64 x) {
w_sstatus(uint64 x) asm volatile("csrw sstatus, %0" : : "r"(x));
{
asm volatile("csrw sstatus, %0" : : "r" (x));
} }
// Supervisor Interrupt Pending // Supervisor Interrupt Pending
static inline uint64 static inline uint64 r_sip() {
r_sip()
{
uint64 x; uint64 x;
asm volatile("csrr %0, sip" : "=r" (x) ); asm volatile("csrr %0, sip" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_sip(uint64 x) { asm volatile("csrw sip, %0" : : "r"(x)); }
w_sip(uint64 x)
{
asm volatile("csrw sip, %0" : : "r" (x));
}
// Supervisor Interrupt Enable // Supervisor Interrupt Enable
#define SIE_SEIE (1L << 9) // external #define SIE_SEIE (1L << 9) // external
#define SIE_STIE (1L << 5) // timer #define SIE_STIE (1L << 5) // timer
#define SIE_SSIE (1L << 1) // software #define SIE_SSIE (1L << 1) // software
static inline uint64 static inline uint64 r_sie() {
r_sie()
{
uint64 x; uint64 x;
asm volatile("csrr %0, sie" : "=r" (x) ); asm volatile("csrr %0, sie" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_sie(uint64 x) { asm volatile("csrw sie, %0" : : "r"(x)); }
w_sie(uint64 x)
{
asm volatile("csrw sie, %0" : : "r" (x));
}
// Machine-mode Interrupt Enable // Machine-mode Interrupt Enable
#define MIE_MEIE (1L << 11) // external #define MIE_MEIE (1L << 11) // external
#define MIE_MTIE (1L << 7) // timer #define MIE_MTIE (1L << 7) // timer
#define MIE_MSIE (1L << 3) // software #define MIE_MSIE (1L << 3) // software
static inline uint64 static inline uint64 r_mie() {
r_mie()
{
uint64 x; uint64 x;
asm volatile("csrr %0, mie" : "=r" (x) ); asm volatile("csrr %0, mie" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_mie(uint64 x) { asm volatile("csrw mie, %0" : : "r"(x)); }
w_mie(uint64 x)
{
asm volatile("csrw mie, %0" : : "r" (x));
}
// supervisor exception program counter, holds the // supervisor exception program counter, holds the
// instruction address to which a return from // instruction address to which a return from
// exception will go. // exception will go.
static inline void static inline void w_sepc(uint64 x) {
w_sepc(uint64 x) asm volatile("csrw sepc, %0" : : "r"(x));
{
asm volatile("csrw sepc, %0" : : "r" (x));
} }
static inline uint64 static inline uint64 r_sepc() {
r_sepc()
{
uint64 x; uint64 x;
asm volatile("csrr %0, sepc" : "=r" (x) ); asm volatile("csrr %0, sepc" : "=r"(x));
return x; return x;
} }
// Machine Exception Delegation // Machine Exception Delegation
static inline uint64 static inline uint64 r_medeleg() {
r_medeleg()
{
uint64 x; uint64 x;
asm volatile("csrr %0, medeleg" : "=r" (x) ); asm volatile("csrr %0, medeleg" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_medeleg(uint64 x) {
w_medeleg(uint64 x) asm volatile("csrw medeleg, %0" : : "r"(x));
{
asm volatile("csrw medeleg, %0" : : "r" (x));
} }
// Machine Interrupt Delegation // Machine Interrupt Delegation
static inline uint64 static inline uint64 r_mideleg() {
r_mideleg()
{
uint64 x; uint64 x;
asm volatile("csrr %0, mideleg" : "=r" (x) ); asm volatile("csrr %0, mideleg" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_mideleg(uint64 x) {
w_mideleg(uint64 x) asm volatile("csrw mideleg, %0" : : "r"(x));
{
asm volatile("csrw mideleg, %0" : : "r" (x));
} }
// Supervisor Trap-Vector Base Address // Supervisor Trap-Vector Base Address
// low two bits are mode. // low two bits are mode.
static inline void static inline void w_stvec(uint64 x) {
w_stvec(uint64 x) asm volatile("csrw stvec, %0" : : "r"(x));
{
asm volatile("csrw stvec, %0" : : "r" (x));
} }
static inline uint64 static inline uint64 r_stvec() {
r_stvec()
{
uint64 x; uint64 x;
asm volatile("csrr %0, stvec" : "=r" (x) ); asm volatile("csrr %0, stvec" : "=r"(x));
return x; return x;
} }
// Machine-mode interrupt vector // Machine-mode interrupt vector
static inline void static inline void w_mtvec(uint64 x) {
w_mtvec(uint64 x) asm volatile("csrw mtvec, %0" : : "r"(x));
{
asm volatile("csrw mtvec, %0" : : "r" (x));
} }
// Physical Memory Protection // Physical Memory Protection
static inline void static inline void w_pmpcfg0(uint64 x) {
w_pmpcfg0(uint64 x) asm volatile("csrw pmpcfg0, %0" : : "r"(x));
{
asm volatile("csrw pmpcfg0, %0" : : "r" (x));
} }
static inline void static inline void w_pmpaddr0(uint64 x) {
w_pmpaddr0(uint64 x) asm volatile("csrw pmpaddr0, %0" : : "r"(x));
{
asm volatile("csrw pmpaddr0, %0" : : "r" (x));
} }
// use riscv's sv39 page table scheme. // use riscv's sv39 page table scheme.
@ -203,126 +151,88 @@ w_pmpaddr0(uint64 x)
// supervisor address translation and protection; // supervisor address translation and protection;
// holds the address of the page table. // holds the address of the page table.
static inline void static inline void w_satp(uint64 x) {
w_satp(uint64 x) asm volatile("csrw satp, %0" : : "r"(x));
{
asm volatile("csrw satp, %0" : : "r" (x));
} }
static inline uint64 static inline uint64 r_satp() {
r_satp()
{
uint64 x; uint64 x;
asm volatile("csrr %0, satp" : "=r" (x) ); asm volatile("csrr %0, satp" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_mscratch(uint64 x) {
w_mscratch(uint64 x) asm volatile("csrw mscratch, %0" : : "r"(x));
{
asm volatile("csrw mscratch, %0" : : "r" (x));
} }
// Supervisor Trap Cause // Supervisor Trap Cause
static inline uint64 static inline uint64 r_scause() {
r_scause()
{
uint64 x; uint64 x;
asm volatile("csrr %0, scause" : "=r" (x) ); asm volatile("csrr %0, scause" : "=r"(x));
return x; return x;
} }
// Supervisor Trap Value // Supervisor Trap Value
static inline uint64 static inline uint64 r_stval() {
r_stval()
{
uint64 x; uint64 x;
asm volatile("csrr %0, stval" : "=r" (x) ); asm volatile("csrr %0, stval" : "=r"(x));
return x; return x;
} }
// Machine-mode Counter-Enable // Machine-mode Counter-Enable
static inline void static inline void w_mcounteren(uint64 x) {
w_mcounteren(uint64 x) asm volatile("csrw mcounteren, %0" : : "r"(x));
{
asm volatile("csrw mcounteren, %0" : : "r" (x));
} }
static inline uint64 static inline uint64 r_mcounteren() {
r_mcounteren()
{
uint64 x; uint64 x;
asm volatile("csrr %0, mcounteren" : "=r" (x) ); asm volatile("csrr %0, mcounteren" : "=r"(x));
return x; return x;
} }
// machine-mode cycle counter // machine-mode cycle counter
static inline uint64 static inline uint64 r_time() {
r_time()
{
uint64 x; uint64 x;
asm volatile("csrr %0, time" : "=r" (x) ); asm volatile("csrr %0, time" : "=r"(x));
return x; return x;
} }
// enable device interrupts // enable device interrupts
static inline void static inline void intr_on() { w_sstatus(r_sstatus() | SSTATUS_SIE); }
intr_on()
{
w_sstatus(r_sstatus() | SSTATUS_SIE);
}
// disable device interrupts // disable device interrupts
static inline void static inline void intr_off() { w_sstatus(r_sstatus() & ~SSTATUS_SIE); }
intr_off()
{
w_sstatus(r_sstatus() & ~SSTATUS_SIE);
}
// are device interrupts enabled? // are device interrupts enabled?
static inline int static inline int intr_get() {
intr_get()
{
uint64 x = r_sstatus(); uint64 x = r_sstatus();
return (x & SSTATUS_SIE) != 0; return (x & SSTATUS_SIE) != 0;
} }
static inline uint64 static inline uint64 r_sp() {
r_sp()
{
uint64 x; uint64 x;
asm volatile("mv %0, sp" : "=r" (x) ); asm volatile("mv %0, sp" : "=r"(x));
return x; return x;
} }
// read and write tp, the thread pointer, which xv6 uses to hold // read and write tp, the thread pointer, which xv6 uses to hold
// this core's hartid (core number), the index into cpus[]. // this core's hartid (core number), the index into cpus[].
static inline uint64 static inline uint64 r_tp() {
r_tp()
{
uint64 x; uint64 x;
asm volatile("mv %0, tp" : "=r" (x) ); asm volatile("mv %0, tp" : "=r"(x));
return x; return x;
} }
static inline void static inline void w_tp(uint64 x) { asm volatile("mv tp, %0" : : "r"(x)); }
w_tp(uint64 x)
{
asm volatile("mv tp, %0" : : "r" (x));
}
static inline uint64 static inline uint64 r_ra() {
r_ra()
{
uint64 x; uint64 x;
asm volatile("mv %0, ra" : "=r" (x) ); asm volatile("mv %0, ra" : "=r"(x));
return x; return x;
} }
// flush the TLB. // flush the TLB.
static inline void static inline void sfence_vma() {
sfence_vma()
{
// the zero, zero means flush all TLB entries. // the zero, zero means flush all TLB entries.
asm volatile("sfence.vma zero, zero"); asm volatile("sfence.vma zero, zero");
} }
@ -335,8 +245,8 @@ typedef uint64 *pagetable_t; // 512 PTEs
#define PGSIZE 4096 // bytes per page #define PGSIZE 4096 // bytes per page
#define PGSHIFT 12 // bits of offset within a page #define PGSHIFT 12 // bits of offset within a page
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1)) #define PGROUNDUP(sz) (((sz) + PGSIZE - 1) & ~(PGSIZE - 1))
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1)) #define PGROUNDDOWN(a) (((a)) & ~(PGSIZE - 1))
#define PTE_V (1L << 0) // valid #define PTE_V (1L << 0) // valid
#define PTE_R (1L << 1) #define PTE_R (1L << 1)
@ -353,8 +263,8 @@ typedef uint64 *pagetable_t; // 512 PTEs
// extract the three 9-bit page table indices from a virtual address. // extract the three 9-bit page table indices from a virtual address.
#define PXMASK 0x1FF // 9 bits #define PXMASK 0x1FF // 9 bits
#define PXSHIFT(level) (PGSHIFT+(9*(level))) #define PXSHIFT(level) (PGSHIFT + (9 * (level)))
#define PX(level, va) ((((uint64) (va)) >> PXSHIFT(level)) & PXMASK) #define PX(level, va) ((((uint64)(va)) >> PXSHIFT(level)) & PXMASK)
// one beyond the highest possible virtual address. // one beyond the highest possible virtual address.
// MAXVA is actually one bit less than the max allowed by // MAXVA is actually one bit less than the max allowed by

View File

@ -7,4 +7,3 @@ struct sleeplock {
char *name; // Name of lock. char *name; // Name of lock.
int pid; // Process holding lock int pid; // Process holding lock
}; };

View File

@ -6,4 +6,3 @@ struct spinlock {
char *name; // Name of lock. char *name; // Name of lock.
struct cpu *cpu; // The cpu holding the lock. struct cpu *cpu; // The cpu holding the lock.
}; };

View File

@ -23,11 +23,14 @@
#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only #define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only
#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only #define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only
#define VIRTIO_MMIO_STATUS 0x070 // read/write #define VIRTIO_MMIO_STATUS 0x070 // read/write
#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 // physical address for descriptor table, write-only #define VIRTIO_MMIO_QUEUE_DESC_LOW \
0x080 // physical address for descriptor table, write-only
#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 #define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084
#define VIRTIO_MMIO_DRIVER_DESC_LOW 0x090 // physical address for available ring, write-only #define VIRTIO_MMIO_DRIVER_DESC_LOW \
0x090 // physical address for available ring, write-only
#define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x094 #define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x094
#define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only #define VIRTIO_MMIO_DEVICE_DESC_LOW \
0x0a0 // physical address for used ring, write-only
#define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4 #define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4
// status register bits, from qemu virtio_config.h // status register bits, from qemu virtio_config.h

View File

@ -3,39 +3,39 @@ struct stat;
// system calls // system calls
int fork(void); int fork(void);
int exit(int) __attribute__((noreturn)); int exit(int) __attribute__((noreturn));
int wait(int*); int wait(int *);
int pipe(int*); int pipe(int *);
int write(int, const void*, int); int write(int, const void *, int);
int read(int, void*, int); int read(int, void *, int);
int close(int); int close(int);
int kill(int); int kill(int);
int exec(const char*, char**); int exec(const char *, char **);
int open(const char*, int); int open(const char *, int);
int mknod(const char*, short, short); int mknod(const char *, short, short);
int unlink(const char*); int unlink(const char *);
int fstat(int fd, struct stat*); int fstat(int fd, struct stat *);
int link(const char*, const char*); int link(const char *, const char *);
int mkdir(const char*); int mkdir(const char *);
int chdir(const char*); int chdir(const char *);
int dup(int); int dup(int);
int getpid(void); int getpid(void);
char* sbrk(int); char *sbrk(int);
int sleep(int); int sleep(int);
int uptime(void); int uptime(void);
// ulib.c // ulib.c
int stat(const char*, struct stat*); int stat(const char *, struct stat *);
char* strcpy(char*, const char*); char *strcpy(char *, const char *);
void *memmove(void*, const void*, int); void *memmove(void *, const void *, int);
char* strchr(const char*, char c); char *strchr(const char *, char c);
int strcmp(const char*, const char*); int strcmp(const char *, const char *);
void fprintf(int, const char*, ...); void fprintf(int, const char *, ...);
void printf(const char*, ...); void printf(const char *, ...);
char* gets(char*, int max); char *gets(char *, int max);
uint strlen(const char*); uint strlen(const char *);
void* memset(void*, int, uint); void *memset(void *, int, uint);
void* malloc(uint); void *malloc(uint);
void free(void*); void free(void *);
int atoi(const char*); int atoi(const char *);
int memcmp(const void *, const void *, uint); int memcmp(const void *, const void *, uint);
void *memcpy(void *, const void *, uint); void *memcpy(void *, const void *, uint);