syscall lab finished

This commit is contained in:
2025-03-25 11:36:21 +08:00
parent a087b429df
commit 992f76ca30
11 changed files with 223 additions and 2 deletions

View File

@@ -63,6 +63,7 @@ void ramdiskrw(struct buf *);
void *kalloc(void);
void kfree(void *);
void kinit(void);
int freemem(void);
// log.c
void initlog(int, struct superblock *);
@@ -106,6 +107,7 @@ void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
int proc_size(void);
// swtch.S
void swtch(struct context *, struct context *);

View File

@@ -7,6 +7,7 @@
#include "memlayout.h"
#include "spinlock.h"
#include "riscv.h"
#include "proc.h"
#include "defs.h"
void freerange(void *pa_start, void *pa_end);
@@ -80,3 +81,17 @@ kalloc(void)
memset((char*)r, 5, PGSIZE); // fill with junk
return (void*)r;
}
int
freemem(void)
{
struct run* p = kmem.freelist;
uint64 num = 0;
while (p) {
num += 1;
p = p->next;
}
return num * PGSIZE;
}

View File

@@ -691,3 +691,13 @@ procdump(void)
printf("\n");
}
}
int
proc_size()
{
int i =0, n = 0;
for (; i < NPROC; ++i) {
if (proc[i].state != UNUSED) n += 1;
}
return n;
}

View File

@@ -8,9 +8,9 @@
#include "defs.h"
// 保留系统调用别名
char* syscalls_name[24] = {"", "fork", "exit", "wait", "pipe", "read", "kill", "exec",
char* syscalls_name[25] = {"", "fork", "exit", "wait", "pipe", "read", "kill", "exec",
"fstat", "chdir", "dup", "getpid", "sbrk", "sleep", "uptime",
"open", "write", "mknod", "unlink", "link", "mkdir", "close", "trace"};
"open", "write", "mknod", "unlink", "link", "mkdir", "close", "trace", "sysinfo"};
// Fetch the uint64 at addr from the current process.
int
@@ -107,6 +107,7 @@ extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
extern uint64 sys_trace(void);
extern uint64 sys_sysinfo(void);
// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
@@ -133,6 +134,7 @@ static uint64 (*syscalls[])(void) = {
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_trace] sys_trace,
[SYS_sysinfo] sys_sysinfo,
};
void

View File

@@ -21,3 +21,4 @@
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_trace 22
#define SYS_sysinfo 23

6
kernel/sysinfo.h Normal file
View File

@@ -0,0 +1,6 @@
#include "kernel/types.h"
struct sysinfo {
uint64 freemem;
uint64 nproc;
uint64 unused_proc_num;
};

View File

@@ -5,6 +5,7 @@
#include "memlayout.h"
#include "spinlock.h"
#include "proc.h"
#include "sysinfo.h"
uint64
sys_exit(void)
@@ -101,3 +102,19 @@ sys_trace(void)
myproc()->tracemask = n;
return 0;
}
uint64
sys_sysinfo(void)
{
struct sysinfo info;
uint64 addr;
argaddr(0, &addr);
if (addr < 0) return -1;
struct proc* p = myproc();
info.nproc = proc_size();
info.freemem = freemem();
info.unused_proc_num = NPROC - info.nproc;
if (copyout(p->pagetable, addr, (char*)&info, sizeof(info)) < 0)
return -1;
return 0;
}