Compare commits
2 Commits
checkpoint
...
mmap
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce9cb0d6c0 | ||
|
|
6dd1443e74 |
5
Makefile
5
Makefile
@ -197,7 +197,6 @@ UPROGS=\
|
||||
|
||||
|
||||
|
||||
|
||||
ifeq ($(LAB),syscall)
|
||||
UPROGS += \
|
||||
$U/_attack\
|
||||
@ -261,6 +260,10 @@ UPROGS += \
|
||||
endif
|
||||
|
||||
|
||||
ifeq ($(LAB),mmap)
|
||||
UPROGS += \
|
||||
$U/_mmaptest
|
||||
endif
|
||||
|
||||
ifeq ($(LAB),net)
|
||||
UPROGS += \
|
||||
|
||||
582
]
Normal file
582
]
Normal file
@ -0,0 +1,582 @@
|
||||
//
|
||||
// File-system system calls.
|
||||
// Mostly argument checking, since we don't trust
|
||||
// user code, and calls into file.c and fs.c.
|
||||
//
|
||||
|
||||
#include "types.h"
|
||||
#include "riscv.h"
|
||||
#include "defs.h"
|
||||
#include "param.h"
|
||||
#include "stat.h"
|
||||
#include "spinlock.h"
|
||||
#include "proc.h"
|
||||
#include "fs.h"
|
||||
#include "sleeplock.h"
|
||||
#include "file.h"
|
||||
#include "fcntl.h"
|
||||
|
||||
// Fetch the nth word-sized system call argument as a file descriptor
|
||||
// and return both the descriptor and the corresponding struct file.
|
||||
static int
|
||||
argfd(int n, int *pfd, struct file **pf)
|
||||
{
|
||||
int fd;
|
||||
struct file *f;
|
||||
|
||||
argint(n, &fd);
|
||||
if(fd < 0 || fd >= NOFILE || (f=myproc()->ofile[fd]) == 0)
|
||||
return -1;
|
||||
if(pfd)
|
||||
*pfd = fd;
|
||||
if(pf)
|
||||
*pf = f;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Allocate a file descriptor for the given file.
|
||||
// Takes over file reference from caller on success.
|
||||
static int
|
||||
fdalloc(struct file *f)
|
||||
{
|
||||
int fd;
|
||||
struct proc *p = myproc();
|
||||
|
||||
for(fd = 0; fd < NOFILE; fd++){
|
||||
if(p->ofile[fd] == 0){
|
||||
p->ofile[fd] = f;
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_dup(void)
|
||||
{
|
||||
struct file *f;
|
||||
int fd;
|
||||
|
||||
if(argfd(0, 0, &f) < 0)
|
||||
return -1;
|
||||
if((fd=fdalloc(f)) < 0)
|
||||
return -1;
|
||||
filedup(f);
|
||||
return fd;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_read(void)
|
||||
{
|
||||
struct file *f;
|
||||
int n;
|
||||
uint64 p;
|
||||
|
||||
argaddr(1, &p);
|
||||
argint(2, &n);
|
||||
if(argfd(0, 0, &f) < 0)
|
||||
return -1;
|
||||
return fileread(f, p, n);
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_write(void)
|
||||
{
|
||||
struct file *f;
|
||||
int n;
|
||||
uint64 p;
|
||||
|
||||
argaddr(1, &p);
|
||||
argint(2, &n);
|
||||
if(argfd(0, 0, &f) < 0)
|
||||
return -1;
|
||||
|
||||
return filewrite(f, p, n);
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_close(void)
|
||||
{
|
||||
int fd;
|
||||
struct file *f;
|
||||
|
||||
if(argfd(0, &fd, &f) < 0)
|
||||
return -1;
|
||||
myproc()->ofile[fd] = 0;
|
||||
fileclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_fstat(void)
|
||||
{
|
||||
struct file *f;
|
||||
uint64 st; // user pointer to struct stat
|
||||
|
||||
argaddr(1, &st);
|
||||
if(argfd(0, 0, &f) < 0)
|
||||
return -1;
|
||||
return filestat(f, st);
|
||||
}
|
||||
|
||||
// Create the path new as a link to the same inode as old.
|
||||
uint64
|
||||
sys_link(void)
|
||||
{
|
||||
char name[DIRSIZ], new[MAXPATH], old[MAXPATH];
|
||||
struct inode *dp, *ip;
|
||||
|
||||
if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0)
|
||||
return -1;
|
||||
|
||||
begin_op();
|
||||
if((ip = namei(old)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ilock(ip);
|
||||
if(ip->type == T_DIR){
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ip->nlink++;
|
||||
iupdate(ip);
|
||||
iunlock(ip);
|
||||
|
||||
if((dp = nameiparent(new, name)) == 0)
|
||||
goto bad;
|
||||
ilock(dp);
|
||||
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
|
||||
iunlockput(dp);
|
||||
goto bad;
|
||||
}
|
||||
iunlockput(dp);
|
||||
iput(ip);
|
||||
|
||||
end_op();
|
||||
|
||||
return 0;
|
||||
|
||||
bad:
|
||||
ilock(ip);
|
||||
ip->nlink--;
|
||||
iupdate(ip);
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Is the directory dp empty except for "." and ".." ?
|
||||
static int
|
||||
isdirempty(struct inode *dp)
|
||||
{
|
||||
int off;
|
||||
struct dirent de;
|
||||
|
||||
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
|
||||
if(readi(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
||||
panic("isdirempty: readi");
|
||||
if(de.inum != 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_unlink(void)
|
||||
{
|
||||
struct inode *ip, *dp;
|
||||
struct dirent de;
|
||||
char name[DIRSIZ], path[MAXPATH];
|
||||
uint off;
|
||||
|
||||
if(argstr(0, path, MAXPATH) < 0)
|
||||
return -1;
|
||||
|
||||
begin_op();
|
||||
if((dp = nameiparent(path, name)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ilock(dp);
|
||||
|
||||
// Cannot unlink "." or "..".
|
||||
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
|
||||
goto bad;
|
||||
|
||||
if((ip = dirlookup(dp, name, &off)) == 0)
|
||||
goto bad;
|
||||
ilock(ip);
|
||||
|
||||
if(ip->nlink < 1)
|
||||
panic("unlink: nlink < 1");
|
||||
if(ip->type == T_DIR && !isdirempty(ip)){
|
||||
iunlockput(ip);
|
||||
goto bad;
|
||||
}
|
||||
|
||||
memset(&de, 0, sizeof(de));
|
||||
if(writei(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
||||
panic("unlink: writei");
|
||||
if(ip->type == T_DIR){
|
||||
dp->nlink--;
|
||||
iupdate(dp);
|
||||
}
|
||||
iunlockput(dp);
|
||||
|
||||
ip->nlink--;
|
||||
iupdate(ip);
|
||||
iunlockput(ip);
|
||||
|
||||
end_op();
|
||||
|
||||
return 0;
|
||||
|
||||
bad:
|
||||
iunlockput(dp);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct inode*
|
||||
create(char *path, short type, short major, short minor)
|
||||
{
|
||||
struct inode *ip, *dp;
|
||||
char name[DIRSIZ];
|
||||
|
||||
if((dp = nameiparent(path, name)) == 0)
|
||||
return 0;
|
||||
|
||||
ilock(dp);
|
||||
|
||||
if((ip = dirlookup(dp, name, 0)) != 0){
|
||||
iunlockput(dp);
|
||||
ilock(ip);
|
||||
if(type == T_FILE && (ip->type == T_FILE || ip->type == T_DEVICE))
|
||||
return ip;
|
||||
iunlockput(ip);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if((ip = ialloc(dp->dev, type)) == 0){
|
||||
iunlockput(dp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ilock(ip);
|
||||
ip->major = major;
|
||||
ip->minor = minor;
|
||||
ip->nlink = 1;
|
||||
iupdate(ip);
|
||||
|
||||
if(type == T_DIR){ // Create . and .. entries.
|
||||
// No ip->nlink++ for ".": avoid cyclic ref count.
|
||||
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if(dirlink(dp, name, ip->inum) < 0)
|
||||
goto fail;
|
||||
|
||||
if(type == T_DIR){
|
||||
// now that success is guaranteed:
|
||||
dp->nlink++; // for ".."
|
||||
iupdate(dp);
|
||||
}
|
||||
|
||||
iunlockput(dp);
|
||||
|
||||
return ip;
|
||||
|
||||
fail:
|
||||
// something went wrong. de-allocate ip.
|
||||
ip->nlink = 0;
|
||||
iupdate(ip);
|
||||
iunlockput(ip);
|
||||
iunlockput(dp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_open(void)
|
||||
{
|
||||
char path[MAXPATH];
|
||||
int fd, omode;
|
||||
struct file *f;
|
||||
struct inode *ip;
|
||||
int n;
|
||||
|
||||
argint(1, &omode);
|
||||
if((n = argstr(0, path, MAXPATH)) < 0)
|
||||
return -1;
|
||||
|
||||
begin_op();
|
||||
|
||||
if(omode & O_CREATE){
|
||||
ip = create(path, T_FILE, 0, 0);
|
||||
if(ip == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if((ip = namei(path)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
ilock(ip);
|
||||
if(ip->type == T_DIR && omode != O_RDONLY){
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
|
||||
if(f)
|
||||
fileclose(f);
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(ip->type == T_DEVICE){
|
||||
f->type = FD_DEVICE;
|
||||
f->major = ip->major;
|
||||
} else {
|
||||
f->type = FD_INODE;
|
||||
f->off = 0;
|
||||
}
|
||||
f->ip = ip;
|
||||
f->readable = !(omode & O_WRONLY);
|
||||
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
|
||||
|
||||
if((omode & O_TRUNC) && ip->type == T_FILE){
|
||||
itrunc(ip);
|
||||
}
|
||||
|
||||
iunlock(ip);
|
||||
end_op();
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_mkdir(void)
|
||||
{
|
||||
char path[MAXPATH];
|
||||
struct inode *ip;
|
||||
|
||||
begin_op();
|
||||
if(argstr(0, path, MAXPATH) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_mknod(void)
|
||||
{
|
||||
struct inode *ip;
|
||||
char path[MAXPATH];
|
||||
int major, minor;
|
||||
|
||||
begin_op();
|
||||
argint(1, &major);
|
||||
argint(2, &minor);
|
||||
if((argstr(0, path, MAXPATH)) < 0 ||
|
||||
(ip = create(path, T_DEVICE, major, minor)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_chdir(void)
|
||||
{
|
||||
char path[MAXPATH];
|
||||
struct inode *ip;
|
||||
struct proc *p = myproc();
|
||||
|
||||
begin_op();
|
||||
if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0){
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
ilock(ip);
|
||||
if(ip->type != T_DIR){
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -1;
|
||||
}
|
||||
iunlock(ip);
|
||||
iput(p->cwd);
|
||||
end_op();
|
||||
p->cwd = ip;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_exec(void)
|
||||
{
|
||||
char path[MAXPATH], *argv[MAXARG];
|
||||
int i;
|
||||
uint64 uargv, uarg;
|
||||
|
||||
argaddr(1, &uargv);
|
||||
if(argstr(0, path, MAXPATH) < 0) {
|
||||
return -1;
|
||||
}
|
||||
memset(argv, 0, sizeof(argv));
|
||||
for(i=0;; i++){
|
||||
if(i >= NELEM(argv)){
|
||||
goto bad;
|
||||
}
|
||||
if(fetchaddr(uargv+sizeof(uint64)*i, (uint64*)&uarg) < 0){
|
||||
goto bad;
|
||||
}
|
||||
if(uarg == 0){
|
||||
argv[i] = 0;
|
||||
break;
|
||||
}
|
||||
argv[i] = kalloc();
|
||||
if(argv[i] == 0)
|
||||
goto bad;
|
||||
if(fetchstr(uarg, argv[i], PGSIZE) < 0)
|
||||
goto bad;
|
||||
}
|
||||
|
||||
int ret = exec(path, argv);
|
||||
|
||||
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
|
||||
kfree(argv[i]);
|
||||
|
||||
return ret;
|
||||
|
||||
bad:
|
||||
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
|
||||
kfree(argv[i]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_pipe(void)
|
||||
{
|
||||
uint64 fdarray; // user pointer to array of two integers
|
||||
struct file *rf, *wf;
|
||||
int fd0, fd1;
|
||||
struct proc *p = myproc();
|
||||
|
||||
argaddr(0, &fdarray);
|
||||
if(pipealloc(&rf, &wf) < 0)
|
||||
return -1;
|
||||
fd0 = -1;
|
||||
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
|
||||
if(fd0 >= 0)
|
||||
p->ofile[fd0] = 0;
|
||||
fileclose(rf);
|
||||
fileclose(wf);
|
||||
return -1;
|
||||
}
|
||||
if(copyout(p->pagetable, fdarray, (char*)&fd0, sizeof(fd0)) < 0 ||
|
||||
copyout(p->pagetable, fdarray+sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0){
|
||||
p->ofile[fd0] = 0;
|
||||
p->ofile[fd1] = 0;
|
||||
fileclose(rf);
|
||||
fileclose(wf);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct vma Null= {.addr=0,.len=0,.prot=0,.flags=0,.file=(struct file*)0,.offset=0};
|
||||
uint64 sys_mmap(void) {
|
||||
uint64 addr,len;
|
||||
int prot, flags, fd, offset;
|
||||
argaddr(0, &addr);
|
||||
argaddr(1, &len), argint(2, &prot), argint(3, &flags), argint(4, &fd), argint(5, &offset);
|
||||
struct proc *p = myproc();
|
||||
struct vma *vma = p->vma;
|
||||
uint64 ret = p->sz;
|
||||
for (int i = 0; i < NVMA; ++i) // don't process when it's full
|
||||
if (vma[i].len == Null.len) { // getting a mem size of 0 seems crazy
|
||||
vma[i].addr=ret;//addr is always 0, and ret is the position
|
||||
vma[i].len=len;
|
||||
vma[i].prot=prot;
|
||||
vma[i].flags=flags;// which is MAP flags
|
||||
vma[i].file=p->ofile[fd];
|
||||
filedup(vma[i].file);//increased ref here
|
||||
vma[i].offset=offset;
|
||||
break;
|
||||
}
|
||||
p->sz += len;
|
||||
return ret;
|
||||
}
|
||||
uint64 sys_munmap(void) {
|
||||
uint64 addr,len;
|
||||
argaddr(0,&addr),argaddr(1,&len);
|
||||
// well it's possible that the passed addr and len does not match a munmap
|
||||
|
||||
return 0;
|
||||
}
|
||||
int lazymap(){
|
||||
struct proc *p=myproc();
|
||||
uint64 va=PGROUNDDOWN(r_stval());
|
||||
if(va>=p->sz)
|
||||
goto err1;
|
||||
for(int i=0;i<NVMA;++i) {
|
||||
const uint64 addr=p->vma[i].addr;
|
||||
const int vma_flag=p->vma[i].prot;
|
||||
const int offset=p->vma[i].offset;
|
||||
struct file *file=p->vma[i].file;
|
||||
struct inode *ip=file->ip;
|
||||
const int len=p->vma[i].len;
|
||||
// each addr and len won't intersect with each other, it's defined by sys_mmap process
|
||||
if(PGROUNDDOWN(addr) + len>= va){// it's considered that addr is page-aligned
|
||||
char *mem=kalloc();
|
||||
memset(mem,0,PGSIZE);
|
||||
if(mem==0)
|
||||
goto err1;
|
||||
int pte_flag=0;
|
||||
if(vma_flag & PROT_READ)
|
||||
pte_flag|=PTE_R;
|
||||
if(vma_flag & PROT_WRITE)
|
||||
pte_flag|=PTE_W;
|
||||
if(vma_flag & PROT_EXEC)
|
||||
pte_flag|=PTE_X;
|
||||
pte_flag|=PTE_U;
|
||||
if(mappages(p->pagetable,va,PGSIZE,(uint64)mem,pte_flag)<0){
|
||||
kfree(mem);
|
||||
goto err1;
|
||||
}
|
||||
ilock(ip);
|
||||
// readi won't increase inode ref
|
||||
if(!readi(ip,0,(uint64)mem,offset + (va-addr),PGSIZE))
|
||||
goto err2;
|
||||
DEBUG();
|
||||
// file pointer handles ref to inode, so don't increase inode ref there
|
||||
iunlock(ip);
|
||||
return 0;
|
||||
err2:
|
||||
iunlock(ip);
|
||||
goto err1;
|
||||
}
|
||||
}
|
||||
err1:
|
||||
return -1;
|
||||
}
|
||||
@ -1 +1 @@
|
||||
LAB=lock
|
||||
LAB=mmap
|
||||
|
||||
@ -1,66 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import re
|
||||
from gradelib import *
|
||||
|
||||
r = Runner(save("xv6.out"))
|
||||
|
||||
@test(0, "running kalloctest")
|
||||
def test_kalloctest():
|
||||
r.run_qemu(shell_script([
|
||||
'kalloctest'
|
||||
]), timeout=300)
|
||||
|
||||
@test(10, "kalloctest: test1", parent=test_kalloctest)
|
||||
def test_kalloctest_test1():
|
||||
r.match('^test1 OK$')
|
||||
|
||||
@test(10, "kalloctest: test2", parent=test_kalloctest)
|
||||
def test_kalloctest_test2():
|
||||
r.match('^test2 OK$')
|
||||
|
||||
@test(10, "kalloctest: test3", parent=test_kalloctest)
|
||||
def test_kalloctest_test3():
|
||||
r.match('^test3 OK$')
|
||||
|
||||
@test(10, "kalloctest: sbrkmuch")
|
||||
def test_sbrkmuch():
|
||||
r.run_qemu(shell_script([
|
||||
'usertests sbrkmuch'
|
||||
]), timeout=90)
|
||||
r.match('^ALL TESTS PASSED$')
|
||||
|
||||
@test(0, "running bcachetest")
|
||||
def test_bcachetest():
|
||||
r.run_qemu(shell_script([
|
||||
'bcachetest'
|
||||
]), timeout=150)
|
||||
|
||||
@test(20, "bcachetest: test0", parent=test_bcachetest)
|
||||
def test_bcachetest_test0():
|
||||
r.match('^test0: OK$')
|
||||
|
||||
@test(10, "bcachetest: test1", parent=test_bcachetest)
|
||||
def test_bcachetest_test1():
|
||||
r.match('^test1 OK$')
|
||||
|
||||
@test(10, "bcachetest: test2", parent=test_bcachetest)
|
||||
def test_bcachetest_test2():
|
||||
r.match('^test2 OK$')
|
||||
|
||||
@test(10, "bcachetest: test3", parent=test_bcachetest)
|
||||
def test_bcachetest_test3():
|
||||
r.match('^test3 OK$')
|
||||
|
||||
@test(19, "usertests")
|
||||
def test_usertests():
|
||||
r.run_qemu(shell_script([
|
||||
'usertests -q'
|
||||
]), timeout=300)
|
||||
r.match('^ALL TESTS PASSED$')
|
||||
|
||||
@test(1, "time")
|
||||
def test_time():
|
||||
check_time()
|
||||
|
||||
run_tests()
|
||||
69
grade-lab-mmap
Executable file
69
grade-lab-mmap
Executable file
@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import re
|
||||
from gradelib import *
|
||||
|
||||
r = Runner(save("xv6.out"))
|
||||
|
||||
@test(0, "running mmaptest")
|
||||
def test_mmaptest():
|
||||
r.run_qemu(shell_script([
|
||||
'mmaptest'
|
||||
]), timeout=180)
|
||||
|
||||
@test(20, "mmaptest: mmap basic", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_basic():
|
||||
r.match('^test basic mmap: OK$')
|
||||
|
||||
@test(10, "mmaptest: mmap private", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_private():
|
||||
r.match('^test mmap private: OK$')
|
||||
|
||||
@test(10, "mmaptest: mmap read-only", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_readonly():
|
||||
r.match('^test mmap read-only: OK$')
|
||||
|
||||
@test(10, "mmaptest: mmap read/write", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_readwrite():
|
||||
r.match('^test mmap read/write: OK$')
|
||||
|
||||
@test(10, "mmaptest: mmap dirty", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_dirty():
|
||||
r.match('^test mmap dirty: OK$')
|
||||
|
||||
@test(10, "mmaptest: not-mapped unmap", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_unmap():
|
||||
r.match('^test not-mapped unmap: OK$')
|
||||
|
||||
@test(10, "mmaptest: lazy access", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_unmap():
|
||||
r.match('^test lazy access: OK$')
|
||||
|
||||
@test(10, "mmaptest: two files", parent=test_mmaptest)
|
||||
def test_mmaptest_mmap_two():
|
||||
r.match('^test mmap two files: OK$')
|
||||
|
||||
@test(40, "mmaptest: fork_test", parent=test_mmaptest)
|
||||
def test_mmaptest_fork_test():
|
||||
r.match('^test fork: OK$')
|
||||
|
||||
@test(10, "mmaptest: munmap_noaccess", parent=test_mmaptest)
|
||||
def test_mmaptest_munmap_noaccess():
|
||||
r.match('^test munmap prevents access: OK$')
|
||||
|
||||
@test(10, "mmaptest: read_only_write", parent=test_mmaptest)
|
||||
def test_mmaptest_read_only_write():
|
||||
r.match('^test writes to read-only mapped memory: OK$')
|
||||
|
||||
@test(19, "usertests")
|
||||
def test_usertests():
|
||||
r.run_qemu(shell_script([
|
||||
'usertests -q'
|
||||
]), timeout=300)
|
||||
r.match('^ALL TESTS PASSED$')
|
||||
|
||||
@test(1, "time")
|
||||
def test_time():
|
||||
check_time()
|
||||
|
||||
run_tests()
|
||||
@ -238,7 +238,7 @@ def make(*target):
|
||||
post_make()
|
||||
|
||||
def show_command(cmd):
|
||||
from pipes import quote
|
||||
from shlex import quote
|
||||
print("\n$", " ".join(map(quote, cmd)))
|
||||
|
||||
def maybe_unlink(*paths):
|
||||
|
||||
@ -118,9 +118,7 @@ brelse(struct buf *b)
|
||||
{
|
||||
if(!holdingsleep(&b->lock))
|
||||
panic("brelse");
|
||||
|
||||
releasesleep(&b->lock);
|
||||
|
||||
acquire(&bcache.lock);
|
||||
b->refcnt--;
|
||||
if (b->refcnt == 0) {
|
||||
|
||||
@ -237,3 +237,4 @@ void netinit(void);
|
||||
void net_rx(char *buf, int len);
|
||||
|
||||
#endif
|
||||
#define DEBUG() printf("File %s, Line %d, Function %s\n",__FILE__,__LINE__,__func__);
|
||||
|
||||
@ -3,3 +3,13 @@
|
||||
#define O_RDWR 0x002
|
||||
#define O_CREATE 0x200
|
||||
#define O_TRUNC 0x400
|
||||
|
||||
#ifdef LAB_MMAP
|
||||
#define PROT_NONE 0x0
|
||||
#define PROT_READ 0x1
|
||||
#define PROT_WRITE 0x2
|
||||
#define PROT_EXEC 0x4
|
||||
|
||||
#define MAP_SHARED 0x01
|
||||
#define MAP_PRIVATE 0x02
|
||||
#endif
|
||||
|
||||
@ -38,4 +38,3 @@ struct devsw {
|
||||
extern struct devsw devsw[];
|
||||
|
||||
#define CONSOLE 1
|
||||
#define STATS 2
|
||||
|
||||
10
kernel/fs.c
10
kernel/fs.c
@ -295,11 +295,11 @@ ilock(struct inode *ip)
|
||||
struct buf *bp;
|
||||
struct dinode *dip;
|
||||
|
||||
if(ip == 0 || atomic_read4(&ip->ref) < 1)
|
||||
if(ip == 0 || ip->ref < 1)
|
||||
panic("ilock");
|
||||
|
||||
acquiresleep(&ip->lock);
|
||||
|
||||
|
||||
if(ip->valid == 0){
|
||||
bp = bread(ip->dev, IBLOCK(ip->inum, sb));
|
||||
dip = (struct dinode*)bp->data + ip->inum%IPB;
|
||||
@ -320,7 +320,7 @@ ilock(struct inode *ip)
|
||||
void
|
||||
iunlock(struct inode *ip)
|
||||
{
|
||||
if(ip == 0 || !holdingsleep(&ip->lock) || atomic_read4(&ip->ref) < 1)
|
||||
if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1)
|
||||
panic("iunlock");
|
||||
|
||||
releasesleep(&ip->lock);
|
||||
@ -416,6 +416,7 @@ bmap(struct inode *ip, uint bn)
|
||||
brelse(bp);
|
||||
return addr;
|
||||
}
|
||||
|
||||
panic("bmap: out of range");
|
||||
}
|
||||
|
||||
@ -446,7 +447,7 @@ itrunc(struct inode *ip)
|
||||
bfree(ip->dev, ip->addrs[NDIRECT]);
|
||||
ip->addrs[NDIRECT] = 0;
|
||||
}
|
||||
|
||||
|
||||
ip->size = 0;
|
||||
iupdate(ip);
|
||||
}
|
||||
@ -477,7 +478,6 @@ readi(struct inode *ip, int user_dst, uint64 dst, uint off, uint n)
|
||||
return 0;
|
||||
if(off + n > ip->size)
|
||||
n = ip->size - off;
|
||||
|
||||
for(tot=0; tot<n; tot+=m, off+=m, dst+=m){
|
||||
uint addr = bmap(ip, off/BSIZE);
|
||||
if(addr == 0)
|
||||
|
||||
323
kernel/kcsan.c
323
kernel/kcsan.c
@ -1,323 +0,0 @@
|
||||
#include "types.h"
|
||||
#include "param.h"
|
||||
#include "memlayout.h"
|
||||
#include "spinlock.h"
|
||||
#include "riscv.h"
|
||||
#include "proc.h"
|
||||
#include "defs.h"
|
||||
|
||||
//
|
||||
// Race detector using gcc's thread sanitizer. It delays all stores
|
||||
// and loads and monitors if any other CPU is using the same address.
|
||||
// If so, we have a race and print out the backtrace of the thread
|
||||
// that raced and the thread that set the watchpoint.
|
||||
//
|
||||
|
||||
//
|
||||
// To run with kcsan:
|
||||
// make clean
|
||||
// make KCSAN=1 qemu
|
||||
//
|
||||
|
||||
// The number of watch points.
|
||||
#define NWATCH (NCPU)
|
||||
|
||||
// The number of cycles to delay stores, whatever that means on qemu.
|
||||
//#define DELAY_CYCLES 20000
|
||||
#define DELAY_CYCLES 200000
|
||||
|
||||
#define MAXTRACE 20
|
||||
|
||||
int
|
||||
trace(uint64 *trace, int maxtrace)
|
||||
{
|
||||
uint64 i = 0;
|
||||
|
||||
push_off();
|
||||
|
||||
uint64 fp = r_fp();
|
||||
uint64 ra, low = PGROUNDDOWN(fp) + 16, high = PGROUNDUP(fp);
|
||||
|
||||
while(!(fp & 7) && fp >= low && fp < high){
|
||||
ra = *(uint64*)(fp - 8);
|
||||
fp = *(uint64*)(fp - 16);
|
||||
trace[i++] = ra;
|
||||
if(i >= maxtrace)
|
||||
break;
|
||||
}
|
||||
|
||||
pop_off();
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
struct watch {
|
||||
uint64 addr;
|
||||
int write;
|
||||
int race;
|
||||
uint64 trace[MAXTRACE];
|
||||
int tracesz;
|
||||
};
|
||||
|
||||
struct {
|
||||
struct spinlock lock;
|
||||
struct watch points[NWATCH];
|
||||
int on;
|
||||
} tsan;
|
||||
|
||||
static struct watch*
|
||||
wp_lookup(uint64 addr)
|
||||
{
|
||||
for(struct watch *w = &tsan.points[0]; w < &tsan.points[NWATCH]; w++) {
|
||||
if(w->addr == addr) {
|
||||
return w;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
wp_install(uint64 addr, int write)
|
||||
{
|
||||
for(struct watch *w = &tsan.points[0]; w < &tsan.points[NWATCH]; w++) {
|
||||
if(w->addr == 0) {
|
||||
w->addr = addr;
|
||||
w->write = write;
|
||||
w->tracesz = trace(w->trace, MAXTRACE);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
panic("wp_install");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
wp_remove(uint64 addr)
|
||||
{
|
||||
for(struct watch *w = &tsan.points[0]; w < &tsan.points[NWATCH]; w++) {
|
||||
if(w->addr == addr) {
|
||||
w->addr = 0;
|
||||
w->tracesz = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic("remove");
|
||||
}
|
||||
|
||||
static void
|
||||
printtrace(uint64 *t, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
printf("%p\n", (void*) t[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
race(char *s, struct watch *w) {
|
||||
uint64 t[MAXTRACE];
|
||||
int n;
|
||||
|
||||
n = trace(t, MAXTRACE);
|
||||
printf("== race detected ==\n");
|
||||
printf("backtrace for racing %s\n", s);
|
||||
printtrace(t, n);
|
||||
printf("backtrace for watchpoint:\n");
|
||||
printtrace(w->trace, w->tracesz);
|
||||
printf("==========\n");
|
||||
}
|
||||
|
||||
// cycle counter
|
||||
static inline uint64
|
||||
r_cycle()
|
||||
{
|
||||
uint64 x;
|
||||
asm volatile("rdcycle %0" : "=r" (x) );
|
||||
return x;
|
||||
}
|
||||
|
||||
static void delay(void) __attribute__((noinline));
|
||||
static void delay() {
|
||||
uint64 stop = r_cycle() + DELAY_CYCLES;
|
||||
uint64 c = r_cycle();
|
||||
while(c < stop) {
|
||||
c = r_cycle();
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
kcsan_read(uint64 addr, int sz)
|
||||
{
|
||||
struct watch *w;
|
||||
|
||||
acquire(&tsan.lock);
|
||||
if((w = wp_lookup(addr)) != 0) {
|
||||
if(w->write) {
|
||||
race("load", w);
|
||||
}
|
||||
release(&tsan.lock);
|
||||
return;
|
||||
}
|
||||
release(&tsan.lock);
|
||||
}
|
||||
|
||||
static void
|
||||
kcsan_write(uint64 addr, int sz)
|
||||
{
|
||||
struct watch *w;
|
||||
|
||||
acquire(&tsan.lock);
|
||||
if((w = wp_lookup(addr)) != 0) {
|
||||
race("store", w);
|
||||
release(&tsan.lock);
|
||||
}
|
||||
|
||||
// no watchpoint; try to install one
|
||||
if(wp_install(addr, 1)) {
|
||||
|
||||
release(&tsan.lock);
|
||||
|
||||
// XXX maybe read value at addr before and after delay to catch
|
||||
// races of unknown origins (e.g., device).
|
||||
|
||||
delay();
|
||||
|
||||
acquire(&tsan.lock);
|
||||
|
||||
wp_remove(addr);
|
||||
}
|
||||
release(&tsan.lock);
|
||||
}
|
||||
|
||||
// tsan.on will only have effect with "make KCSAN=1"
|
||||
void
|
||||
kcsaninit(void)
|
||||
{
|
||||
initlock(&tsan.lock, "tsan");
|
||||
tsan.on = 1;
|
||||
__sync_synchronize();
|
||||
}
|
||||
|
||||
//
|
||||
// Calls inserted by compiler into kernel binary, except for this file.
|
||||
//
|
||||
|
||||
void
|
||||
__tsan_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_read1(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
// kcsan_read(addr, 1);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_read2(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_read(addr, 2);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_read4(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_read(addr, 4);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_read8(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_read(addr, 8);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_read_range(uint64 addr, uint64 size)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_read(addr, size);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_write1(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
// kcsan_write(addr, 1);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_write2(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_write(addr, 2);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_write4(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_write(addr, 4);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_write8(uint64 addr)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_write(addr, 8);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_write_range(uint64 addr, uint64 size)
|
||||
{
|
||||
if(!tsan.on)
|
||||
return;
|
||||
kcsan_write(addr, size);
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_atomic_thread_fence(int order)
|
||||
{
|
||||
__sync_synchronize();
|
||||
}
|
||||
|
||||
uint32
|
||||
__tsan_atomic32_load(uint *ptr, uint *val, int order)
|
||||
{
|
||||
uint t;
|
||||
__atomic_load(ptr, &t, __ATOMIC_SEQ_CST);
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
__tsan_atomic32_store(uint *ptr, uint val, int order)
|
||||
{
|
||||
__atomic_store(ptr, &val, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
// We don't use this
|
||||
void
|
||||
__tsan_func_entry(uint64 pc)
|
||||
{
|
||||
}
|
||||
|
||||
// We don't use this
|
||||
void
|
||||
__tsan_func_exit(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -12,9 +12,6 @@ main()
|
||||
{
|
||||
if(cpuid() == 0){
|
||||
consoleinit();
|
||||
#if defined(LAB_LOCK)
|
||||
statsinit();
|
||||
#endif
|
||||
printfinit();
|
||||
printf("\n");
|
||||
printf("xv6 kernel is booting\n");
|
||||
@ -31,17 +28,11 @@ main()
|
||||
iinit(); // inode table
|
||||
fileinit(); // file table
|
||||
virtio_disk_init(); // emulated hard disk
|
||||
#ifdef LAB_NET
|
||||
pci_init();
|
||||
#endif
|
||||
userinit(); // first user process
|
||||
#ifdef KCSAN
|
||||
kcsaninit();
|
||||
#endif
|
||||
__sync_synchronize();
|
||||
started = 1;
|
||||
} else {
|
||||
while(atomic_read4((int *) &started) == 0)
|
||||
while(started == 0)
|
||||
;
|
||||
__sync_synchronize();
|
||||
printf("hart %d starting\n", cpuid());
|
||||
|
||||
@ -68,9 +68,6 @@ pipeclose(struct pipe *pi, int writable)
|
||||
}
|
||||
if(pi->readopen == 0 && pi->writeopen == 0){
|
||||
release(&pi->lock);
|
||||
#ifdef LAB_LOCK
|
||||
freelock(&pi->lock);
|
||||
#endif
|
||||
kfree((char*)pi);
|
||||
} else
|
||||
release(&pi->lock);
|
||||
|
||||
@ -273,7 +273,7 @@ growproc(int n)
|
||||
p->sz = sz;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern int lazymap(uint64);
|
||||
// Create a new process, copying the parent.
|
||||
// Sets up child kernel stack to return as if from fork() system call.
|
||||
int
|
||||
@ -282,12 +282,35 @@ fork(void)
|
||||
int i, pid;
|
||||
struct proc *np;
|
||||
struct proc *p = myproc();
|
||||
|
||||
// Allocate process.
|
||||
if((np = allocproc()) == 0){
|
||||
return -1;
|
||||
}
|
||||
|
||||
// not lazy anymore, forcefully alloc all pages in VMA
|
||||
release(&np->lock);
|
||||
for(int i=0;i<NVMA;++i){
|
||||
np->vma[i]=p->vma[i];
|
||||
if(p->vma[i].len==0)
|
||||
continue;
|
||||
const int addr=p->vma[i].addr;
|
||||
const int len=p->vma[i].len;
|
||||
const int vis=p->vma[i].vis;
|
||||
filedup(np->vma[i].file);
|
||||
for(int j=0;j<len;j+=PGSIZE){
|
||||
const int cur=addr+j;
|
||||
const int x=j/PGSIZE;
|
||||
if((1<<x)&vis)
|
||||
continue;
|
||||
pte_t *pte=walk(p->pagetable,cur,0);
|
||||
if(pte!=0 &&(*pte & PTE_V)!=0)
|
||||
continue;
|
||||
if(lazymap(cur)<0){
|
||||
freeproc(np);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
acquire(&np->lock);
|
||||
// Copy user memory from parent to child.
|
||||
if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
|
||||
freeproc(np);
|
||||
@ -321,7 +344,6 @@ fork(void)
|
||||
acquire(&np->lock);
|
||||
np->state = RUNNABLE;
|
||||
release(&np->lock);
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
@ -343,6 +365,7 @@ reparent(struct proc *p)
|
||||
// Exit the current process. Does not return.
|
||||
// An exited process remains in the zombie state
|
||||
// until its parent calls wait().
|
||||
extern int munmap(uint64,uint64);
|
||||
void
|
||||
exit(int status)
|
||||
{
|
||||
@ -364,7 +387,30 @@ exit(int status)
|
||||
iput(p->cwd);
|
||||
end_op();
|
||||
p->cwd = 0;
|
||||
|
||||
#ifdef CHECK_IF_VALID_ADDR
|
||||
int cnt=0;
|
||||
#endif
|
||||
for(int i=0;i<NVMA;++i){
|
||||
int x=0;
|
||||
for(int j=1;j<NVMA;++j)
|
||||
if(p->vma[j].addr>p->vma[x].addr)
|
||||
x=j;
|
||||
if(p->vma[x].len==0)
|
||||
break;
|
||||
#ifdef CHECK_IF_VALID_ADDR
|
||||
printf("current sz=%lu\n",p->sz);
|
||||
if(p->vma[x].addr+p->vma[x].len!=p->sz){
|
||||
printf("addr=%lu,len=%d,cnt=%d\n",p->vma[x].addr,p->vma[x].len,cnt);
|
||||
panic("invalid addr");
|
||||
}
|
||||
++cnt;
|
||||
#endif
|
||||
if(p->vma[x].len)
|
||||
munmap(p->vma[x].addr,p->vma[x].len);
|
||||
}
|
||||
#ifdef CHECK_IF_VALID_ADDR
|
||||
printf("it's still alive, sz=%lu\n",p->sz);
|
||||
#endif
|
||||
acquire(&wait_lock);
|
||||
|
||||
// Give any children to init.
|
||||
@ -379,7 +425,6 @@ exit(int status)
|
||||
p->state = ZOMBIE;
|
||||
|
||||
release(&wait_lock);
|
||||
|
||||
// Jump into the scheduler, never to return.
|
||||
sched();
|
||||
panic("zombie exit");
|
||||
@ -446,7 +491,6 @@ scheduler(void)
|
||||
{
|
||||
struct proc *p;
|
||||
struct cpu *c = mycpu();
|
||||
|
||||
c->proc = 0;
|
||||
for(;;){
|
||||
// The most recent process to run may have had interrupts
|
||||
|
||||
@ -20,7 +20,7 @@ struct context {
|
||||
|
||||
// Per-CPU state.
|
||||
struct cpu {
|
||||
struct proc *proc; // The process running on this cpu, or null.
|
||||
struct proc *proc; // The process running on this cpu, or Null.
|
||||
struct context context; // swtch() here to enter scheduler().
|
||||
int noff; // Depth of push_off() nesting.
|
||||
int intena; // Were interrupts enabled before push_off()?
|
||||
@ -80,7 +80,14 @@ struct trapframe {
|
||||
};
|
||||
|
||||
enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
|
||||
|
||||
#define NVMA 16
|
||||
struct vma{
|
||||
uint64 addr;
|
||||
int len,prot,flags,offset;
|
||||
struct file *file;
|
||||
int vis;// validity bitmask used, bit 1 stands for being unmapped, init be 0
|
||||
// the 0-th bit stands for addr, then 1-st bit stands for addr+PGSIZE, etc
|
||||
};
|
||||
// Per-process state
|
||||
struct proc {
|
||||
struct spinlock lock;
|
||||
@ -104,4 +111,5 @@ struct proc {
|
||||
struct file *ofile[NOFILE]; // Open files
|
||||
struct inode *cwd; // Current directory
|
||||
char name[16]; // Process name (debugging)
|
||||
struct vma vma[NVMA];
|
||||
};
|
||||
|
||||
@ -204,7 +204,7 @@ r_menvcfg()
|
||||
static inline void
|
||||
w_menvcfg(uint64 x)
|
||||
{
|
||||
//asm volatile("csrw menvcfg, %0" : : "r" (x));
|
||||
// asm volatile("csrw menvcfg, %0" : : "r" (x));
|
||||
asm volatile("csrw 0x30a, %0" : : "r" (x));
|
||||
}
|
||||
|
||||
@ -314,14 +314,6 @@ r_sp()
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline uint64
|
||||
r_fp()
|
||||
{
|
||||
uint64 x;
|
||||
asm volatile("mv %0, s0" : "=r" (x) );
|
||||
return x;
|
||||
}
|
||||
|
||||
// read and write tp, the thread pointer, which xv6 uses to hold
|
||||
// this core's hartid (core number), the index into cpus[].
|
||||
static inline uint64
|
||||
@ -362,11 +354,6 @@ typedef uint64 *pagetable_t; // 512 PTEs
|
||||
#define PGSIZE 4096 // bytes per page
|
||||
#define PGSHIFT 12 // bits of offset within a page
|
||||
|
||||
#ifdef LAB_PGTBL
|
||||
#define SUPERPGSIZE (2 * (1 << 20)) // bytes per page
|
||||
#define SUPERPGROUNDUP(sz) (((sz)+SUPERPGSIZE-1) & ~(SUPERPGSIZE-1))
|
||||
#endif
|
||||
|
||||
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
||||
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
|
||||
|
||||
@ -376,12 +363,6 @@ typedef uint64 *pagetable_t; // 512 PTEs
|
||||
#define PTE_X (1L << 3)
|
||||
#define PTE_U (1L << 4) // user can access
|
||||
|
||||
|
||||
|
||||
#if defined(LAB_MMAP) || defined(LAB_PGTBL)
|
||||
#define PTE_LEAF(pte) (((pte) & PTE_R) | ((pte) & PTE_W) | ((pte) & PTE_X))
|
||||
#endif
|
||||
|
||||
// shift a physical address to the right place for a PTE.
|
||||
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
|
||||
|
||||
|
||||
@ -8,52 +8,12 @@
|
||||
#include "proc.h"
|
||||
#include "defs.h"
|
||||
|
||||
#ifdef LAB_LOCK
|
||||
#define NLOCK 500
|
||||
|
||||
static struct spinlock *locks[NLOCK];
|
||||
struct spinlock lock_locks;
|
||||
|
||||
void
|
||||
freelock(struct spinlock *lk)
|
||||
{
|
||||
acquire(&lock_locks);
|
||||
int i;
|
||||
for (i = 0; i < NLOCK; i++) {
|
||||
if(locks[i] == lk) {
|
||||
locks[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
release(&lock_locks);
|
||||
}
|
||||
|
||||
static void
|
||||
findslot(struct spinlock *lk) {
|
||||
acquire(&lock_locks);
|
||||
int i;
|
||||
for (i = 0; i < NLOCK; i++) {
|
||||
if(locks[i] == 0) {
|
||||
locks[i] = lk;
|
||||
release(&lock_locks);
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic("findslot");
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
initlock(struct spinlock *lk, char *name)
|
||||
{
|
||||
lk->name = name;
|
||||
lk->locked = 0;
|
||||
lk->cpu = 0;
|
||||
#ifdef LAB_LOCK
|
||||
lk->nts = 0;
|
||||
lk->n = 0;
|
||||
findslot(lk);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Acquire the lock.
|
||||
@ -65,21 +25,12 @@ acquire(struct spinlock *lk)
|
||||
if(holding(lk))
|
||||
panic("acquire");
|
||||
|
||||
#ifdef LAB_LOCK
|
||||
__sync_fetch_and_add(&(lk->n), 1);
|
||||
#endif
|
||||
|
||||
// On RISC-V, sync_lock_test_and_set turns into an atomic swap:
|
||||
// a5 = 1
|
||||
// s1 = &lk->locked
|
||||
// amoswap.w.aq a5, a5, (s1)
|
||||
while(__sync_lock_test_and_set(&lk->locked, 1) != 0) {
|
||||
#ifdef LAB_LOCK
|
||||
__sync_fetch_and_add(&(lk->nts), 1);
|
||||
#else
|
||||
;
|
||||
#endif
|
||||
}
|
||||
while(__sync_lock_test_and_set(&lk->locked, 1) != 0)
|
||||
;
|
||||
|
||||
// Tell the C compiler and the processor to not move loads or stores
|
||||
// past this point, to ensure that the critical section's memory
|
||||
@ -157,61 +108,3 @@ pop_off(void)
|
||||
if(c->noff == 0 && c->intena)
|
||||
intr_on();
|
||||
}
|
||||
|
||||
// Read a shared 32-bit value without holding a lock
|
||||
int
|
||||
atomic_read4(int *addr) {
|
||||
uint32 val;
|
||||
__atomic_load(addr, &val, __ATOMIC_SEQ_CST);
|
||||
return val;
|
||||
}
|
||||
|
||||
#ifdef LAB_LOCK
|
||||
int
|
||||
snprint_lock(char *buf, int sz, struct spinlock *lk)
|
||||
{
|
||||
int n = 0;
|
||||
if(lk->n > 0) {
|
||||
n = snprintf(buf, sz, "lock: %s: #test-and-set %d #acquire() %d\n",
|
||||
lk->name, lk->nts, lk->n);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
statslock(char *buf, int sz) {
|
||||
int n;
|
||||
int tot = 0;
|
||||
|
||||
acquire(&lock_locks);
|
||||
n = snprintf(buf, sz, "--- lock kmem/bcache stats\n");
|
||||
for(int i = 0; i < NLOCK; i++) {
|
||||
if(locks[i] == 0)
|
||||
break;
|
||||
if(strncmp(locks[i]->name, "bcache", strlen("bcache")) == 0 ||
|
||||
strncmp(locks[i]->name, "kmem", strlen("kmem")) == 0) {
|
||||
tot += locks[i]->nts;
|
||||
n += snprint_lock(buf +n, sz-n, locks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
n += snprintf(buf+n, sz-n, "--- top 5 contended locks:\n");
|
||||
int last = 100000000;
|
||||
// stupid way to compute top 5 contended locks
|
||||
for(int t = 0; t < 5; t++) {
|
||||
int top = 0;
|
||||
for(int i = 0; i < NLOCK; i++) {
|
||||
if(locks[i] == 0)
|
||||
break;
|
||||
if(locks[i]->nts > locks[top]->nts && locks[i]->nts < last) {
|
||||
top = i;
|
||||
}
|
||||
}
|
||||
n += snprint_lock(buf+n, sz-n, locks[top]);
|
||||
last = locks[top]->nts;
|
||||
}
|
||||
n += snprintf(buf+n, sz-n, "tot= %d\n", tot);
|
||||
release(&lock_locks);
|
||||
return n;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -5,9 +5,5 @@ struct spinlock {
|
||||
// For debugging:
|
||||
char *name; // Name of lock.
|
||||
struct cpu *cpu; // The cpu holding the lock.
|
||||
#ifdef LAB_LOCK
|
||||
int nts;
|
||||
int n;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@ -1,88 +0,0 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "param.h"
|
||||
#include "spinlock.h"
|
||||
#include "sleeplock.h"
|
||||
#include "fs.h"
|
||||
#include "file.h"
|
||||
#include "riscv.h"
|
||||
#include "defs.h"
|
||||
|
||||
static char digits[] = "0123456789abcdef";
|
||||
|
||||
static int
|
||||
sputc(char *s, char c)
|
||||
{
|
||||
*s = c;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
sprintint(char *s, int xx, int base, int sign)
|
||||
{
|
||||
char buf[16];
|
||||
int i, n;
|
||||
uint x;
|
||||
|
||||
if(sign && (sign = xx < 0))
|
||||
x = -xx;
|
||||
else
|
||||
x = xx;
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
buf[i++] = digits[x % base];
|
||||
} while((x /= base) != 0);
|
||||
|
||||
if(sign)
|
||||
buf[i++] = '-';
|
||||
|
||||
n = 0;
|
||||
while(--i >= 0)
|
||||
n += sputc(s+n, buf[i]);
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
snprintf(char *buf, unsigned long sz, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int i, c;
|
||||
int off = 0;
|
||||
char *s;
|
||||
|
||||
va_start(ap, fmt);
|
||||
for(i = 0; off < sz && (c = fmt[i] & 0xff) != 0; i++){
|
||||
if(c != '%'){
|
||||
off += sputc(buf+off, c);
|
||||
continue;
|
||||
}
|
||||
c = fmt[++i] & 0xff;
|
||||
if(c == 0)
|
||||
break;
|
||||
switch(c){
|
||||
case 'd':
|
||||
off += sprintint(buf+off, va_arg(ap, int), 10, 1);
|
||||
break;
|
||||
case 'x':
|
||||
off += sprintint(buf+off, va_arg(ap, int), 16, 1);
|
||||
break;
|
||||
case 's':
|
||||
if((s = va_arg(ap, char*)) == 0)
|
||||
s = "(null)";
|
||||
for(; *s && off < sz; s++)
|
||||
off += sputc(buf+off, *s);
|
||||
break;
|
||||
case '%':
|
||||
off += sputc(buf+off, '%');
|
||||
break;
|
||||
default:
|
||||
// Print unknown % sequence to draw attention.
|
||||
off += sputc(buf+off, '%');
|
||||
off += sputc(buf+off, c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return off;
|
||||
}
|
||||
@ -32,11 +32,6 @@ start()
|
||||
w_mideleg(0xffff);
|
||||
w_sie(r_sie() | SIE_SEIE | SIE_STIE | SIE_SSIE);
|
||||
|
||||
#ifdef KCSAN
|
||||
// allow supervisor to read cycle counter register
|
||||
w_mcounteren(r_mcounteren()|0x3);
|
||||
#endif
|
||||
|
||||
// configure Physical Memory Protection to give supervisor mode
|
||||
// access to all of physical memory.
|
||||
w_pmpaddr0(0x3fffffffffffffull);
|
||||
|
||||
@ -1,69 +0,0 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "param.h"
|
||||
#include "spinlock.h"
|
||||
#include "sleeplock.h"
|
||||
#include "fs.h"
|
||||
#include "file.h"
|
||||
#include "riscv.h"
|
||||
#include "defs.h"
|
||||
|
||||
#define BUFSZ 4096
|
||||
static struct {
|
||||
struct spinlock lock;
|
||||
char buf[BUFSZ];
|
||||
int sz;
|
||||
int off;
|
||||
} stats;
|
||||
|
||||
int statscopyin(char*, int);
|
||||
int statslock(char*, int);
|
||||
|
||||
int
|
||||
statswrite(int user_src, uint64 src, int n)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
statsread(int user_dst, uint64 dst, int n)
|
||||
{
|
||||
int m;
|
||||
|
||||
acquire(&stats.lock);
|
||||
|
||||
if(stats.sz == 0) {
|
||||
#ifdef LAB_PGTBL
|
||||
stats.sz = statscopyin(stats.buf, BUFSZ);
|
||||
#endif
|
||||
#ifdef LAB_LOCK
|
||||
stats.sz = statslock(stats.buf, BUFSZ);
|
||||
#endif
|
||||
}
|
||||
m = stats.sz - stats.off;
|
||||
|
||||
if (m > 0) {
|
||||
if(m > n)
|
||||
m = n;
|
||||
if(either_copyout(user_dst, dst, stats.buf+stats.off, m) != -1) {
|
||||
stats.off += m;
|
||||
}
|
||||
} else {
|
||||
m = -1;
|
||||
stats.sz = 0;
|
||||
stats.off = 0;
|
||||
}
|
||||
release(&stats.lock);
|
||||
return m;
|
||||
}
|
||||
|
||||
void
|
||||
statsinit(void)
|
||||
{
|
||||
initlock(&stats.lock, "stats");
|
||||
|
||||
devsw[STATS].read = statsread;
|
||||
devsw[STATS].write = statswrite;
|
||||
}
|
||||
|
||||
@ -101,7 +101,8 @@ extern uint64 sys_unlink(void);
|
||||
extern uint64 sys_link(void);
|
||||
extern uint64 sys_mkdir(void);
|
||||
extern uint64 sys_close(void);
|
||||
|
||||
extern uint64 sys_mmap(void);
|
||||
extern uint64 sys_munmap(void);
|
||||
// An array mapping syscall numbers from syscall.h
|
||||
// to the function that handles the system call.
|
||||
static uint64 (*syscalls[])(void) = {
|
||||
@ -126,6 +127,8 @@ static uint64 (*syscalls[])(void) = {
|
||||
[SYS_link] sys_link,
|
||||
[SYS_mkdir] sys_mkdir,
|
||||
[SYS_close] sys_close,
|
||||
[SYS_mmap] sys_mmap,
|
||||
[SYS_munmap] sys_munmap
|
||||
};
|
||||
|
||||
void
|
||||
|
||||
@ -20,3 +20,5 @@
|
||||
#define SYS_link 19
|
||||
#define SYS_mkdir 20
|
||||
#define SYS_close 21
|
||||
#define SYS_mmap 22
|
||||
#define SYS_munmap 23
|
||||
|
||||
183
kernel/sysfile.c
183
kernel/sysfile.c
@ -503,3 +503,186 @@ sys_pipe(void)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct vma Null= {.addr=0,.len=0,.prot=0,.flags=0,.file=(struct file*)0,.offset=0,.vis=0};
|
||||
uint64 sys_mmap(void) {
|
||||
uint64 addr,len;
|
||||
int prot, flags, fd, offset;
|
||||
argaddr(0, &addr);
|
||||
argaddr(1, &len), argint(2, &prot), argint(3, &flags), argint(4, &fd), argint(5, &offset);
|
||||
struct proc *p = myproc();
|
||||
struct vma *vma = p->vma;
|
||||
if((!p->ofile[fd]->readable) && (prot & PROT_READ))
|
||||
goto err1;
|
||||
if((!p->ofile[fd]->writable) && (prot &PROT_WRITE) && (flags & MAP_SHARED))
|
||||
goto err1;
|
||||
uint64 ret = p->sz;
|
||||
for (int i = 0; i < NVMA; ++i) // don't process when it's full
|
||||
if (vma[i].len == Null.len) { // getting a mem size of 0 seems crazy
|
||||
vma[i].addr=ret;//addr is always 0, and ret is the position
|
||||
vma[i].len=len;
|
||||
vma[i].prot=prot;
|
||||
vma[i].flags=flags;// which is MAP flags
|
||||
vma[i].file=p->ofile[fd];
|
||||
filedup(vma[i].file);//increased ref here
|
||||
vma[i].offset=offset;
|
||||
vma[i].vis=0;
|
||||
p->sz+=len;
|
||||
return ret;
|
||||
}
|
||||
err1:
|
||||
return -1;
|
||||
}
|
||||
void shrink_vma(){
|
||||
struct proc *p=myproc();
|
||||
//DEBUG();
|
||||
while(1){
|
||||
int found=0;
|
||||
int i;
|
||||
for(i=0;i<NVMA;++i)
|
||||
if(p->vma[i].vis==-1 && p->vma[i].addr+p->vma[i].len==p->sz){
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
if(!found)
|
||||
break;
|
||||
#ifdef CHECK_IF_EXISTS
|
||||
printf("current sz=%lu\n",p->sz);
|
||||
#endif
|
||||
p->sz-=p->vma[i].len;
|
||||
p->vma[i]=Null;
|
||||
}
|
||||
}
|
||||
int munmap(uint64 Argaddr,uint64 Arglen){
|
||||
struct proc *p=myproc();
|
||||
// well it's possible that the passed addr and len does not match a munmap
|
||||
// how do I know that all of the pages of an mmap zone has been removed --using vis
|
||||
// don't do crazy things like unmap two different mmap area at same time --it's fine, no such operation in test
|
||||
for(int i=0;i<NVMA;++i){// all of which are changable
|
||||
uint64 addr=p->vma[i].addr;
|
||||
const int offset=p->vma[i].offset;
|
||||
struct file *file=p->vma[i].file;
|
||||
struct inode *ip=file->ip;
|
||||
const int len=p->vma[i].len;
|
||||
int *vis=&p->vma[i].vis;
|
||||
const int map_flag=p->vma[i].flags;
|
||||
if(*vis!=-1 && len >0 && PGROUNDDOWN(addr) + len >= Argaddr+Arglen && PGROUNDDOWN(addr) <= Argaddr){
|
||||
// for(int i=0;i<Arglen;i+=PGSIZE){
|
||||
// const int cur = Argaddr + i;
|
||||
// const int x = (cur-addr)/PGSIZE;
|
||||
// delete it, even if being already deleted
|
||||
//if((1<<x)&(*vis))
|
||||
// goto err1; //attempting to visit a dellocated page
|
||||
// }
|
||||
for(int i=0;i<Arglen;i+=PGSIZE){
|
||||
const uint64 cur = Argaddr + i;
|
||||
const int x = (cur-addr)/PGSIZE;
|
||||
pte_t *pte=walk(p->pagetable,cur,0);
|
||||
if(pte!=0 && (*pte & PTE_V)!=0){
|
||||
// this page may not actually allocated
|
||||
// wait what happens for uvmunmap, if there's a mmap page between normal pages... --I bet this won't happen
|
||||
if(map_flag & (MAP_SHARED)){// write back
|
||||
begin_op(); // don't forget begin_op
|
||||
ilock(ip);
|
||||
const uint sz=ip->size;
|
||||
const int pos=offset+(cur-addr);
|
||||
const uint remain=sz-pos;
|
||||
if(writei(ip,1,cur,pos,PGSIZE<remain?PGSIZE:remain)<0)
|
||||
goto err2;
|
||||
iunlock(ip);
|
||||
end_op();
|
||||
}
|
||||
uvmunmap(p->pagetable,cur,1,1);
|
||||
}
|
||||
*vis |= 1<<x;// add dellocated sign
|
||||
//kfree((char *)cur); it's not kernel memory space
|
||||
// delete such memory page
|
||||
}
|
||||
int all_clear=1;
|
||||
for(int i=0;i<len/PGSIZE;++i)
|
||||
all_clear&=(*vis)>>i;
|
||||
if(all_clear){ // clear this vma
|
||||
// wait, what happens if this vma is the highest address?
|
||||
// and, it's responsible for exit, to call munmap in a dereasing order
|
||||
//if(p->sz == addr+len)
|
||||
//p->sz-=len;
|
||||
// release the file
|
||||
fileclose(file);
|
||||
// clean this vma
|
||||
//p->vma[i]=Null;// well, I don't need those pointers at all...
|
||||
p->vma[i].vis=-1;
|
||||
shrink_vma();
|
||||
}
|
||||
return 0;
|
||||
err2:
|
||||
iunlock(ip);
|
||||
goto err1;
|
||||
}
|
||||
}
|
||||
err1:
|
||||
// hey! why did a uint64 func return -1??
|
||||
return -1; //default not found
|
||||
}
|
||||
uint64 sys_munmap(void) {
|
||||
uint64 Argaddr,Arglen;
|
||||
argaddr(0,&Argaddr),argaddr(1,&Arglen);
|
||||
return munmap(Argaddr,Arglen);
|
||||
}
|
||||
int lazymap(uint64 va){
|
||||
struct proc *p=myproc();
|
||||
if(va>=p->sz)
|
||||
goto err1;
|
||||
uint64 scause=r_scause();
|
||||
for(int i=0;i<NVMA;++i) {
|
||||
const uint64 addr=p->vma[i].addr;
|
||||
const int vma_flag=p->vma[i].prot;
|
||||
const int offset=p->vma[i].offset;
|
||||
struct file *file=p->vma[i].file;
|
||||
struct inode *ip=file->ip;
|
||||
const int len=p->vma[i].len;
|
||||
const int vis=p->vma[i].vis;
|
||||
// each addr and len won't intersect with each other, it's defined by sys_mmap process
|
||||
// it's > not >=, however
|
||||
// we must make sure that this va is within the range
|
||||
if(vis!=-1 && len > 0 && PGROUNDDOWN(addr) + len>va && PGROUNDDOWN(addr)<=va){// it's considered that addr is page-aligned
|
||||
const int x=(va-addr)/PGSIZE;
|
||||
if((1<<x)&vis)
|
||||
goto err1;// attempting to visit a dellocated page
|
||||
if(scause == 13 && (vma_flag & PROT_READ)==0)
|
||||
goto err1;
|
||||
if(scause ==15 && (vma_flag & PROT_WRITE)==0)
|
||||
goto err1;
|
||||
char *mem=kalloc();
|
||||
memset(mem,0,PGSIZE);
|
||||
if(mem==0)
|
||||
goto err1;
|
||||
int pte_flag=0;
|
||||
if(vma_flag & PROT_READ)
|
||||
pte_flag|=PTE_R;
|
||||
if(vma_flag & PROT_WRITE)
|
||||
pte_flag|=PTE_W;
|
||||
if(vma_flag & PROT_EXEC)
|
||||
pte_flag|=PTE_X;
|
||||
pte_flag|=PTE_U;
|
||||
if(mappages(p->pagetable,va,PGSIZE,(uint64)mem,pte_flag)<0){
|
||||
kfree(mem);
|
||||
goto err1;
|
||||
}
|
||||
// why I need to call readi??
|
||||
// oh, that's because I can't get file+offset directly...
|
||||
ilock(ip);
|
||||
// readi won't increase inode ref
|
||||
if(readi(ip,0,(uint64)mem,offset + (va-addr),PGSIZE)<=0)
|
||||
goto err2;
|
||||
// file pointer handles ref to inode, so don't increase inode ref there
|
||||
iunlock(ip);
|
||||
return 0;
|
||||
err2:
|
||||
uvmunmap(p->pagetable,va,1,1);
|
||||
iunlock(ip);
|
||||
goto err1;
|
||||
}
|
||||
}
|
||||
err1:
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -5,62 +5,45 @@
|
||||
#include "memlayout.h"
|
||||
#include "spinlock.h"
|
||||
#include "proc.h"
|
||||
|
||||
uint64
|
||||
sys_exit(void)
|
||||
{
|
||||
uint64 sys_exit(void) {
|
||||
int n;
|
||||
argint(0, &n);
|
||||
exit(n);
|
||||
return 0; // not reached
|
||||
return 0; // not reached
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_getpid(void)
|
||||
{
|
||||
return myproc()->pid;
|
||||
}
|
||||
uint64 sys_getpid(void) { return myproc()->pid; }
|
||||
|
||||
uint64
|
||||
sys_fork(void)
|
||||
{
|
||||
return fork();
|
||||
}
|
||||
uint64 sys_fork(void) { return fork(); }
|
||||
|
||||
uint64
|
||||
sys_wait(void)
|
||||
{
|
||||
uint64 sys_wait(void) {
|
||||
uint64 p;
|
||||
argaddr(0, &p);
|
||||
return wait(p);
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_sbrk(void)
|
||||
{
|
||||
uint64 sys_sbrk(void) {
|
||||
uint64 addr;
|
||||
int n;
|
||||
|
||||
argint(0, &n);
|
||||
addr = myproc()->sz;
|
||||
if(growproc(n) < 0)
|
||||
if (growproc(n) < 0)
|
||||
return -1;
|
||||
return addr;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_sleep(void)
|
||||
{
|
||||
uint64 sys_sleep(void) {
|
||||
int n;
|
||||
uint ticks0;
|
||||
|
||||
argint(0, &n);
|
||||
if(n < 0)
|
||||
if (n < 0)
|
||||
n = 0;
|
||||
acquire(&tickslock);
|
||||
ticks0 = ticks;
|
||||
while(ticks - ticks0 < n){
|
||||
if(killed(myproc())){
|
||||
while (ticks - ticks0 < n) {
|
||||
if (killed(myproc())) {
|
||||
release(&tickslock);
|
||||
return -1;
|
||||
}
|
||||
@ -70,20 +53,17 @@ sys_sleep(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64
|
||||
sys_kill(void)
|
||||
{
|
||||
uint64 sys_kill(void) {
|
||||
int pid;
|
||||
|
||||
argint(0, &pid);
|
||||
return kill(pid);
|
||||
}
|
||||
|
||||
|
||||
// return how many clock tick interrupts have occurred
|
||||
// since start.
|
||||
uint64
|
||||
sys_uptime(void)
|
||||
{
|
||||
uint64 sys_uptime(void) {
|
||||
uint xticks;
|
||||
|
||||
acquire(&tickslock);
|
||||
@ -91,3 +71,4 @@ sys_uptime(void)
|
||||
release(&tickslock);
|
||||
return xticks;
|
||||
}
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ extern char trampoline[], uservec[], userret[];
|
||||
void kernelvec();
|
||||
|
||||
extern int devintr();
|
||||
|
||||
extern int lazymap(uint64);
|
||||
void
|
||||
trapinit(void)
|
||||
{
|
||||
@ -65,7 +65,14 @@ usertrap(void)
|
||||
intr_on();
|
||||
|
||||
syscall();
|
||||
} else if((which_dev = devintr()) != 0){
|
||||
}else if(r_scause() == 13 || r_scause() == 15){ //on handling page fault
|
||||
if(killed(p))
|
||||
exit(-1);// for sure?
|
||||
if(lazymap(PGROUNDDOWN(r_stval()))<0)
|
||||
setkilled(p);
|
||||
// kill will call exit(), and exit() will handle munmap
|
||||
// do I need to call intr_on()? no
|
||||
}else if((which_dev = devintr()) != 0){
|
||||
// ok
|
||||
} else {
|
||||
printf("usertrap(): unexpected scause 0x%lx pid=%d\n", r_scause(), p->pid);
|
||||
@ -77,9 +84,9 @@ usertrap(void)
|
||||
exit(-1);
|
||||
|
||||
// give up the CPU if this is a timer interrupt.
|
||||
if(which_dev == 2)
|
||||
if(which_dev == 2){
|
||||
yield();
|
||||
|
||||
}
|
||||
usertrapret();
|
||||
}
|
||||
|
||||
@ -151,9 +158,9 @@ kerneltrap()
|
||||
}
|
||||
|
||||
// give up the CPU if this is a timer interrupt.
|
||||
if(which_dev == 2 && myproc() != 0)
|
||||
if(which_dev == 2 && myproc() != 0){
|
||||
yield();
|
||||
|
||||
}
|
||||
// the yield() may have caused some traps to occur,
|
||||
// so restore trap registers for use by kernelvec.S's sepc instruction.
|
||||
w_sepc(sepc);
|
||||
|
||||
@ -212,28 +212,6 @@ alloc3_desc(int *idx)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef LAB_LOCK
|
||||
//
|
||||
// check that there are at most NBUF distinct
|
||||
// struct buf's, which the lock lab requires.
|
||||
//
|
||||
static struct buf *xbufs[NBUF];
|
||||
static void
|
||||
checkbuf(struct buf *b)
|
||||
{
|
||||
for(int i = 0; i < NBUF; i++){
|
||||
if(xbufs[i] == b){
|
||||
return;
|
||||
}
|
||||
if(xbufs[i] == 0){
|
||||
xbufs[i] = b;
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic("more than NBUF bufs");
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
virtio_disk_rw(struct buf *b, int write)
|
||||
{
|
||||
@ -241,10 +219,6 @@ virtio_disk_rw(struct buf *b, int write)
|
||||
|
||||
acquire(&disk.vdisk_lock);
|
||||
|
||||
#ifdef LAB_LOCK
|
||||
checkbuf(b);
|
||||
#endif
|
||||
|
||||
// the spec's Section 5.2 says that legacy block operations use
|
||||
// three descriptors: one for type/reserved/sector, one for the
|
||||
// data, one for a 1-byte status result.
|
||||
|
||||
@ -1,400 +0,0 @@
|
||||
#include "kernel/fcntl.h"
|
||||
#include "kernel/param.h"
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/riscv.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "user/user.h"
|
||||
|
||||
void test0();
|
||||
void test1();
|
||||
void test2();
|
||||
void test3();
|
||||
|
||||
#define SZ 4096
|
||||
char buf[SZ];
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
test0();
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
createfile(char *file, int nblock)
|
||||
{
|
||||
int fd;
|
||||
char buf[BSIZE];
|
||||
int i;
|
||||
|
||||
fd = open(file, O_CREATE | O_RDWR);
|
||||
if(fd < 0){
|
||||
printf("createfile %s failed\n", file);
|
||||
exit(-1);
|
||||
}
|
||||
for(i = 0; i < nblock; i++) {
|
||||
if(write(fd, buf, sizeof(buf)) != sizeof(buf)) {
|
||||
printf("write %s failed\n", file);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void
|
||||
readfile(char *file, int nbytes, int inc)
|
||||
{
|
||||
char buf[BSIZE];
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
if(inc > BSIZE) {
|
||||
printf("readfile: inc too large\n");
|
||||
exit(-1);
|
||||
}
|
||||
if ((fd = open(file, O_RDONLY)) < 0) {
|
||||
printf("readfile open %s failed\n", file);
|
||||
exit(-1);
|
||||
}
|
||||
for (i = 0; i < nbytes; i += inc) {
|
||||
if(read(fd, buf, inc) != inc) {
|
||||
printf("read %s failed for block %d (%d)\n", file, i, nbytes);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int ntas(int print)
|
||||
{
|
||||
int n;
|
||||
char *c;
|
||||
|
||||
if (statistics(buf, SZ) <= 0) {
|
||||
fprintf(2, "ntas: no stats\n");
|
||||
}
|
||||
c = strchr(buf, '=');
|
||||
n = atoi(c+2);
|
||||
if(print)
|
||||
printf("%s", buf);
|
||||
return n;
|
||||
}
|
||||
|
||||
// Test reading small files concurrently
|
||||
void
|
||||
test0()
|
||||
{
|
||||
char file[2];
|
||||
char dir[2];
|
||||
enum { N = 10, NCHILD = 3 };
|
||||
int m, n;
|
||||
|
||||
dir[0] = '0';
|
||||
dir[1] = '\0';
|
||||
file[0] = 'F';
|
||||
file[1] = '\0';
|
||||
|
||||
printf("start test0\n");
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
dir[0] = '0' + i;
|
||||
mkdir(dir);
|
||||
if (chdir(dir) < 0) {
|
||||
printf("chdir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
unlink(file);
|
||||
createfile(file, N);
|
||||
if (chdir("..") < 0) {
|
||||
printf("chdir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
m = ntas(0);
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
dir[0] = '0' + i;
|
||||
int pid = fork();
|
||||
if(pid < 0){
|
||||
printf("fork failed");
|
||||
exit(-1);
|
||||
}
|
||||
if(pid == 0){
|
||||
if (chdir(dir) < 0) {
|
||||
printf("chdir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
readfile(file, N*BSIZE, 1);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
wait(&status);
|
||||
if (status != 0) {
|
||||
printf("FAIL: a child failed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("test0 results:\n");
|
||||
n = ntas(1);
|
||||
if (n-m < 500)
|
||||
printf("test0: OK\n");
|
||||
else
|
||||
printf("test0: FAIL\n");
|
||||
}
|
||||
|
||||
// Test bcache evictions by reading a large file concurrently
|
||||
void test1()
|
||||
{
|
||||
char file[3];
|
||||
enum { N = 200, BIG=100, NCHILD=2 };
|
||||
|
||||
printf("start test1\n");
|
||||
file[0] = 'B';
|
||||
file[2] = '\0';
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
file[1] = '0' + i;
|
||||
unlink(file);
|
||||
if (i == 0) {
|
||||
createfile(file, BIG);
|
||||
} else {
|
||||
createfile(file, 1);
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
file[1] = '0' + i;
|
||||
int pid = fork();
|
||||
if(pid < 0){
|
||||
printf("fork failed");
|
||||
exit(-1);
|
||||
}
|
||||
if(pid == 0){
|
||||
if (i==0) {
|
||||
for (i = 0; i < N; i++) {
|
||||
readfile(file, BIG*BSIZE, BSIZE);
|
||||
}
|
||||
unlink(file);
|
||||
exit(0);
|
||||
} else {
|
||||
for (i = 0; i < N*20; i++) {
|
||||
readfile(file, 1, BSIZE);
|
||||
}
|
||||
unlink(file);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
wait(&status);
|
||||
if (status != 0) {
|
||||
printf("FAIL: a child failed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\ntest1 OK\n");
|
||||
}
|
||||
|
||||
//
|
||||
// test concurrent creates.
|
||||
//
|
||||
void
|
||||
test2()
|
||||
{
|
||||
int nc = 4;
|
||||
char file[16];
|
||||
|
||||
printf("start test2\n");
|
||||
|
||||
mkdir("d2");
|
||||
|
||||
file[0] = 'd';
|
||||
file[1] = '2';
|
||||
file[2] = '/';
|
||||
|
||||
// remove any stale existing files.
|
||||
for(int i = 0; i < 50; i++){
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
file[3] = 'a' + ci;
|
||||
file[4] = '0' + i;
|
||||
file[5] = '\0';
|
||||
unlink(file);
|
||||
}
|
||||
}
|
||||
|
||||
int pids[nc];
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
pids[ci] = fork();
|
||||
if(pids[ci] < 0){
|
||||
printf("test2: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pids[ci] == 0){
|
||||
char me = "abcdefghijklmnop"[ci];
|
||||
int pid = getpid();
|
||||
int nf = (ci == 0 ? 10 : 15);
|
||||
|
||||
// create nf files.
|
||||
for(int i = 0; i < nf; i++){
|
||||
file[3] = me;
|
||||
file[4] = '0' + i;
|
||||
file[5] = '\0';
|
||||
int fd = open(file, O_CREATE | O_RDWR);
|
||||
if(fd < 0){
|
||||
printf("test2: create %s failed\n", file);
|
||||
exit(1);
|
||||
}
|
||||
int xx = (pid << 16) | i;
|
||||
for(int nw = 0; nw < 2; nw++){
|
||||
// the sleep() increases the chance of simultaneous
|
||||
// calls to bget().
|
||||
sleep(1);
|
||||
if(write(fd, &xx, sizeof(xx)) <= 0){
|
||||
printf("test2: write %s failed\n", file);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
// read back the nf files.
|
||||
for(int i = 0; i < nf; i++){
|
||||
file[3] = me;
|
||||
file[4] = '0' + i;
|
||||
file[5] = '\0';
|
||||
// printf("r %s\n", file);
|
||||
int fd = open(file, O_RDWR);
|
||||
if(fd < 0){
|
||||
printf("test2: open %s failed\n", file);
|
||||
exit(1);
|
||||
}
|
||||
int xx = (pid << 16) | i;
|
||||
for(int nr = 0; nr < 2; nr++){
|
||||
int z = 0;
|
||||
sleep(1);
|
||||
int n = read(fd, &z, sizeof(z));
|
||||
if(n != sizeof(z)){
|
||||
printf("test2: read %s returned %d, expected %ld\n", file, n, sizeof(z));
|
||||
exit(1);
|
||||
}
|
||||
if(z != xx){
|
||||
printf("test2: file %s contained %d, not %d\n", file, z, xx);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
// delete the nf files.
|
||||
for(int i = 0; i < nf; i++){
|
||||
file[3] = me;
|
||||
file[4] = '0' + i;
|
||||
file[5] = '\0';
|
||||
//printf("u %s\n", file);
|
||||
if(unlink(file) != 0){
|
||||
printf("test2: unlink %s failed\n", file);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int ok = 1;
|
||||
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
int st = 0;
|
||||
int ret = wait(&st);
|
||||
if(ret <= 0){
|
||||
printf("test2: wait() failed\n");
|
||||
ok = 0;
|
||||
}
|
||||
if(st != 0)
|
||||
ok = 0;
|
||||
}
|
||||
if(ok) {
|
||||
printf("\ntest2 OK\n");
|
||||
} else {
|
||||
printf("test2 failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// generate big log transactions to check that bget() can
|
||||
// make use of any of the NBUF buffers for any block number.
|
||||
//
|
||||
void
|
||||
test3()
|
||||
{
|
||||
int nc = 5;
|
||||
char file[16];
|
||||
|
||||
printf("start test3\n");
|
||||
|
||||
mkdir("d2");
|
||||
|
||||
file[0] = 'd';
|
||||
file[1] = '2';
|
||||
file[2] = '/';
|
||||
|
||||
int pids[nc];
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
pids[ci] = fork();
|
||||
if(pids[ci] < 0){
|
||||
printf("test3: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pids[ci] == 0){
|
||||
file[3] = 'a' + ci;
|
||||
file[4] = '\0';
|
||||
unlink(file);
|
||||
int fd = open(file, O_CREATE | O_RDWR);
|
||||
if(fd < 0){
|
||||
printf("test3: create %s failed\n", file);
|
||||
exit(1);
|
||||
}
|
||||
write(fd, "x", 1);
|
||||
static char junk[12*512];
|
||||
for(int i = 0; i < 12; i++){
|
||||
sleep(1);
|
||||
write(fd, junk, sizeof(junk));
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int ok = 1;
|
||||
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
int st = 0;
|
||||
int ret = wait(&st);
|
||||
if(ret <= 0){
|
||||
printf("test3: wait() failed\n");
|
||||
ok = 0;
|
||||
}
|
||||
if(st != 0)
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
for(int ci = 0; ci < nc; ci++){
|
||||
file[3] = 'a' + ci;
|
||||
file[4] = '\0';
|
||||
unlink(file);
|
||||
}
|
||||
|
||||
if(ok) {
|
||||
printf("\ntest3 OK\n");
|
||||
} else {
|
||||
printf("test3 failed\n");
|
||||
}
|
||||
}
|
||||
@ -18,7 +18,6 @@ main(void)
|
||||
|
||||
if(open("console", O_RDWR) < 0){
|
||||
mknod("console", CONSOLE, 0);
|
||||
mknod("statistics", STATS, 0);
|
||||
open("console", O_RDWR);
|
||||
}
|
||||
dup(0); // stdout
|
||||
|
||||
@ -1,198 +0,0 @@
|
||||
#include "kernel/param.h"
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/riscv.h"
|
||||
#include "kernel/memlayout.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "user/user.h"
|
||||
|
||||
#define NCHILD 2
|
||||
#define N 100000
|
||||
#define SZ 4096
|
||||
|
||||
void test1(void);
|
||||
void test2(void);
|
||||
void test3(void);
|
||||
char buf[SZ];
|
||||
|
||||
int countfree();
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int ntas(int print)
|
||||
{
|
||||
int n;
|
||||
char *c;
|
||||
|
||||
if (statistics(buf, SZ) <= 0) {
|
||||
fprintf(2, "ntas: no stats\n");
|
||||
}
|
||||
c = strchr(buf, '=');
|
||||
n = atoi(c+2);
|
||||
if(print)
|
||||
printf("%s", buf);
|
||||
return n;
|
||||
}
|
||||
|
||||
// Test concurrent kallocs and kfrees
|
||||
void test1(void)
|
||||
{
|
||||
void *a, *a1;
|
||||
int n, m;
|
||||
|
||||
printf("start test1\n");
|
||||
m = ntas(0);
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
int pid = fork();
|
||||
if(pid < 0){
|
||||
printf("fork failed");
|
||||
exit(-1);
|
||||
}
|
||||
if(pid == 0){
|
||||
for(i = 0; i < N; i++) {
|
||||
a = sbrk(4096);
|
||||
*(int *)(a+4) = 1;
|
||||
a1 = sbrk(-4096);
|
||||
if (a1 != a + 4096) {
|
||||
printf("test1: FAIL wrong sbrk\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
int status = 0;
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
wait(&status);
|
||||
if (status != 0) {
|
||||
printf("FAIL: a child failed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
printf("test1 results:\n");
|
||||
n = ntas(1);
|
||||
if(n-m < 10)
|
||||
printf("test1 OK\n");
|
||||
else
|
||||
printf("test1 FAIL\n");
|
||||
}
|
||||
|
||||
|
||||
// Test stealing
|
||||
void test2() {
|
||||
int free0 = countfree();
|
||||
int free1;
|
||||
int n = (PHYSTOP-KERNBASE)/PGSIZE;
|
||||
printf("start test2\n");
|
||||
printf("total free number of pages: %d (out of %d)\n", free0, n);
|
||||
if(n - free0 > 1000) {
|
||||
printf("test2 FAILED: cannot allocate enough memory");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 0; i < 50; i++) {
|
||||
free1 = countfree();
|
||||
if(i % 10 == 9)
|
||||
printf(".");
|
||||
if(free1 != free0) {
|
||||
printf("test2 FAIL: losing pages %d %d\n", free0, free1);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
printf("\ntest2 OK\n");
|
||||
}
|
||||
|
||||
// Test concurrent kalloc/kfree and stealing
|
||||
void test3(void)
|
||||
{
|
||||
uint64 a, a1;
|
||||
int n, m;
|
||||
|
||||
m = ntas(0);
|
||||
printf("start test3\n");
|
||||
int pid;
|
||||
|
||||
for(int i = 0; i < NCHILD; i++){
|
||||
pid = fork();
|
||||
if(pid < 0){
|
||||
printf("fork failed");
|
||||
exit(-1);
|
||||
}
|
||||
if(pid == 0){
|
||||
if (i == 0) {
|
||||
for(i = 0; i < N; i++) {
|
||||
a = (uint64) sbrk(4096);
|
||||
if(a == 0xffffffffffffffff){
|
||||
// no freemem
|
||||
continue;
|
||||
}
|
||||
*(int *)(a+4) = 1;
|
||||
a1 = (uint64) sbrk(-4096);
|
||||
if (a1 != a + 4096) {
|
||||
printf("test3 FAIL: wrong sbrk\n");
|
||||
exit(1);
|
||||
}
|
||||
if ((i + 1) % 10000 == 0) {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
printf("child done %d\n", i);
|
||||
exit(0);
|
||||
} else {
|
||||
while (1) {
|
||||
int free0 = countfree();
|
||||
int free1 = countfree();
|
||||
if(free0 - free1 > 1) {
|
||||
printf("test3 FAIL: losing pages %d %d\n", free0, free1);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
for(int i = 0; i < NCHILD-1; i++){
|
||||
wait(&status);
|
||||
if (status != 0) {
|
||||
printf("a child failed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
kill(pid);
|
||||
|
||||
n = ntas(1);
|
||||
if(n-m < 4000)
|
||||
printf("\ntest3 OK\n");
|
||||
else
|
||||
printf("test3 FAIL m %d n %d\n", m, n);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// countfree() from usertests.c
|
||||
//
|
||||
int
|
||||
countfree()
|
||||
{
|
||||
uint64 sz0 = (uint64)sbrk(0);
|
||||
int n = 0;
|
||||
|
||||
while(1){
|
||||
uint64 a = (uint64) sbrk(4096);
|
||||
if(a == 0xffffffffffffffff){
|
||||
break;
|
||||
}
|
||||
// modify the memory to make sure it's really allocated.
|
||||
*(char *)(a + 4096 - 1) = 1;
|
||||
n += 1;
|
||||
}
|
||||
sbrk(-((uint64)sbrk(0) - sz0));
|
||||
return n;
|
||||
}
|
||||
444
user/mmaptest.c
Normal file
444
user/mmaptest.c
Normal file
@ -0,0 +1,444 @@
|
||||
#include "kernel/param.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/riscv.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "user/user.h"
|
||||
|
||||
void mmap_test();
|
||||
void fork_test();
|
||||
void more_test();
|
||||
char buf[PGSIZE];
|
||||
|
||||
#define MAP_FAILED ((char *) -1)
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
mmap_test();
|
||||
fork_test();
|
||||
more_test();
|
||||
printf("mmaptest: all tests succeeded\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
err(char *why)
|
||||
{
|
||||
printf("mmaptest failure: %s, pid=%d\n", why, getpid());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
// check the content of the two mapped pages.
|
||||
//
|
||||
void
|
||||
_v1(char *p)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < PGSIZE*2; i++) {
|
||||
if (i < PGSIZE + (PGSIZE/2)) {
|
||||
if (p[i] != 'A') {
|
||||
printf("mismatch at %d, wanted 'A', got 0x%x\n", i, p[i]);
|
||||
err("v1 mismatch (1)");
|
||||
}
|
||||
} else {
|
||||
if (p[i] != 0) {
|
||||
printf("mismatch at %d, wanted zero, got 0x%x\n", i, p[i]);
|
||||
err("v1 mismatch (2)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// create a file to be mapped, containing
|
||||
// 1.5 pages of 'A' and half a page of zeros.
|
||||
//
|
||||
void
|
||||
makefile(const char *f)
|
||||
{
|
||||
int i;
|
||||
int n = PGSIZE/BSIZE;
|
||||
|
||||
unlink(f);
|
||||
int fd = open(f, O_WRONLY | O_CREATE);
|
||||
if (fd == -1)
|
||||
err("open");
|
||||
memset(buf, 'A', BSIZE);
|
||||
// write 1.5 page
|
||||
for (i = 0; i < n + n/2; i++) {
|
||||
if (write(fd, buf, BSIZE) != BSIZE)
|
||||
err("write 0 makefile");
|
||||
}
|
||||
if (close(fd) == -1)
|
||||
err("close");
|
||||
}
|
||||
|
||||
void
|
||||
mmap_test(void)
|
||||
{
|
||||
int fd;
|
||||
int i;
|
||||
const char * const f = "mmap.dur";
|
||||
|
||||
//
|
||||
// create a file with known content, map it into memory, check that
|
||||
// the mapped memory has the same bytes as originally written to the
|
||||
// file.
|
||||
//
|
||||
makefile(f);
|
||||
if ((fd = open(f, O_RDONLY)) == -1)
|
||||
err("open (1)");
|
||||
|
||||
printf("test basic mmap\n");
|
||||
//
|
||||
// this call to mmap() asks the kernel to map the content
|
||||
// of open file fd into the address space. the first
|
||||
// 0 argument indicates that the kernel should choose the
|
||||
// virtual address. the second argument indicates how many
|
||||
// bytes to map. the third argument indicates that the
|
||||
// mapped memory should be read-only. the fourth argument
|
||||
// indicates that, if the process modifies the mapped memory,
|
||||
// that the modifications should not be written back to
|
||||
// the file nor shared with other processes mapping the
|
||||
// same file (of course in this case updates are prohibited
|
||||
// due to PROT_READ). the fifth argument is the file descriptor
|
||||
// of the file to be mapped. the last argument is the starting
|
||||
// offset in the file.
|
||||
//
|
||||
char *p = mmap(0, PGSIZE*2, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap (1)");
|
||||
_v1(p);
|
||||
if (munmap(p, PGSIZE*2) == -1)
|
||||
err("munmap (1)");
|
||||
|
||||
printf("test basic mmap: OK\n");
|
||||
|
||||
printf("test mmap private\n");
|
||||
// should be able to map file opened read-only with private writable
|
||||
// mapping
|
||||
p = mmap(0, PGSIZE*2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap (2)");
|
||||
if (close(fd) == -1)
|
||||
err("close (1)");
|
||||
_v1(p);
|
||||
for (i = 0; i < PGSIZE*2; i++)
|
||||
p[i] = 'Z';
|
||||
if (munmap(p, PGSIZE*2) == -1)
|
||||
err("munmap (2)");
|
||||
close(fd);
|
||||
|
||||
// file should not have been modified.
|
||||
if((fd = open(f, O_RDONLY)) < 0) err("open");
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE) err("read");
|
||||
if(buf[0] != 'A')
|
||||
err("write to MAP_PRIVATE was written to file");
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE/2) err("read");
|
||||
if(buf[0] != 'A')
|
||||
err("write to MAP_PRIVATE was written to file");
|
||||
close(fd);
|
||||
|
||||
printf("test mmap private: OK\n");
|
||||
|
||||
printf("test mmap read-only\n");
|
||||
|
||||
// check that mmap doesn't allow read/write mapping of a
|
||||
// file opened read-only.
|
||||
if ((fd = open(f, O_RDONLY)) == -1)
|
||||
err("open (2)");
|
||||
p = mmap(0, PGSIZE*2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p != MAP_FAILED)
|
||||
err("mmap (3)");
|
||||
if (close(fd) == -1)
|
||||
err("close (2)");
|
||||
|
||||
printf("test mmap read-only: OK\n");
|
||||
|
||||
printf("test mmap read/write\n");
|
||||
|
||||
// check that mmap does allow read/write mapping of a
|
||||
// file opened read/write.
|
||||
if ((fd = open(f, O_RDWR)) == -1)
|
||||
err("open (3)");
|
||||
p = mmap(0, PGSIZE*3, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap (4)");
|
||||
if (close(fd) == -1)
|
||||
err("close (3)");
|
||||
// check that the mapping still works after close(fd).
|
||||
_v1(p);
|
||||
// write the mapped memory.
|
||||
for (i = 0; i < PGSIZE; i++)
|
||||
p[i] = 'B';
|
||||
for (i = PGSIZE; i < PGSIZE*2; i++)
|
||||
p[i] = 'C';
|
||||
// unmap just the first two of three pages of mapped memory.
|
||||
if (munmap(p, PGSIZE*2) == -1)
|
||||
err("munmap (3)");
|
||||
|
||||
printf("test mmap read/write: OK\n");
|
||||
|
||||
printf("test mmap dirty\n");
|
||||
|
||||
// check that the writes to the mapped memory were
|
||||
// written to the file.
|
||||
if ((fd = open(f, O_RDONLY)) == -1)
|
||||
err("open (4)");
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE)
|
||||
err("dirty read #1");
|
||||
for (i = 0; i < PGSIZE; i++){
|
||||
if (buf[i] != 'B')
|
||||
err("file page 0 does not contain modifications");
|
||||
}
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE/2)
|
||||
err("dirty read #2");
|
||||
for (i = 0; i < PGSIZE/2; i++){
|
||||
if (buf[i] != 'C')
|
||||
err("file page 1 does not contain modifications");
|
||||
}
|
||||
if (close(fd) == -1)
|
||||
err("close (4)");
|
||||
|
||||
printf("test mmap dirty: OK\n");
|
||||
|
||||
printf("test not-mapped unmap\n");
|
||||
|
||||
// unmap the rest of the mapped memory.
|
||||
if (munmap(p+PGSIZE*2, PGSIZE) == -1)
|
||||
err("munmap (4)");
|
||||
|
||||
printf("test not-mapped unmap: OK\n");
|
||||
|
||||
printf("test lazy access\n");
|
||||
|
||||
if(unlink(f) != 0) err("unlink");
|
||||
makefile(f);
|
||||
|
||||
if ((fd = open(f, O_RDWR)) == -1)
|
||||
err("open");
|
||||
p = mmap(0, PGSIZE*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap");
|
||||
close(fd);
|
||||
|
||||
// mmap() should not have read the file at this point,
|
||||
// so that the file modification we're about to make
|
||||
// ought to be visible to a subsequent read of the
|
||||
// mapped memory.
|
||||
|
||||
if((fd = open(f, O_RDWR)) == -1)
|
||||
err("open");
|
||||
if(write(fd, "m", 1) != 1)
|
||||
err("write");
|
||||
close(fd);
|
||||
|
||||
if(*p != 'm')
|
||||
err("read was not lazy");
|
||||
|
||||
if(munmap(p, PGSIZE*2) == -1)
|
||||
err("munmap");
|
||||
|
||||
printf("test lazy access: OK\n");
|
||||
|
||||
printf("test mmap two files\n");
|
||||
|
||||
//
|
||||
// mmap two different files at the same time.
|
||||
//
|
||||
int fd1;
|
||||
if((fd1 = open("mmap1", O_RDWR|O_CREATE)) < 0)
|
||||
err("open (5)");
|
||||
if(write(fd1, "12345", 5) != 5)
|
||||
err("write (1)");
|
||||
char *p1 = mmap(0, PGSIZE, PROT_READ, MAP_PRIVATE, fd1, 0);
|
||||
if(p1 == MAP_FAILED)
|
||||
err("mmap (5)");
|
||||
if (close(fd1) == -1)
|
||||
err("close (5)");
|
||||
if (unlink("mmap1") == -1)
|
||||
err("unlink (1)");
|
||||
|
||||
int fd2;
|
||||
if((fd2 = open("mmap2", O_RDWR|O_CREATE)) < 0)
|
||||
err("open (6)");
|
||||
if(write(fd2, "67890", 5) != 5)
|
||||
err("write (2)");
|
||||
char *p2 = mmap(0, PGSIZE, PROT_READ, MAP_PRIVATE, fd2, 0);
|
||||
if(p2 == MAP_FAILED)
|
||||
err("mmap (6)");
|
||||
if (close(fd2) == -1)
|
||||
err("close (6)");
|
||||
if (unlink("mmap2") == -1)
|
||||
err("unlink (2)");
|
||||
|
||||
if(memcmp(p1, "12345", 5) != 0)
|
||||
err("mmap1 mismatch");
|
||||
if(memcmp(p2, "67890", 5) != 0)
|
||||
err("mmap2 mismatch");
|
||||
|
||||
if (munmap(p1, PGSIZE) == -1)
|
||||
err("munmap (5)");
|
||||
if(memcmp(p2, "67890", 5) != 0)
|
||||
err("mmap2 mismatch (2)");
|
||||
if (munmap(p2, PGSIZE) == -1)
|
||||
err("munmap (6)");
|
||||
|
||||
printf("test mmap two files: OK\n");
|
||||
}
|
||||
|
||||
//
|
||||
// mmap a file, then fork.
|
||||
// check that the child sees the mapped file.
|
||||
//
|
||||
void
|
||||
fork_test(void)
|
||||
{
|
||||
int fd;
|
||||
int pid;
|
||||
const char * const f = "mmap.dur";
|
||||
|
||||
printf("test fork\n");
|
||||
|
||||
// mmap the file twice.
|
||||
makefile(f);
|
||||
if ((fd = open(f, O_RDONLY)) == -1)
|
||||
err("open (7)");
|
||||
if (unlink(f) == -1)
|
||||
err("unlink (3)");
|
||||
char *p1 = mmap(0, PGSIZE*2, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (p1 == MAP_FAILED)
|
||||
err("mmap (7)");
|
||||
char *p2 = mmap(0, PGSIZE*2, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (p2 == MAP_FAILED)
|
||||
err("mmap (8)");
|
||||
|
||||
// read just 2nd page.
|
||||
if(*(p1+PGSIZE) != 'A')
|
||||
err("fork mismatch (1)");
|
||||
|
||||
if((pid = fork()) < 0)
|
||||
err("fork");
|
||||
if (pid == 0) {
|
||||
_v1(p1);
|
||||
if (munmap(p1, PGSIZE) == -1) // just the first page
|
||||
err("munmap (7)");
|
||||
exit(0); // tell the parent that the mapping looks OK.
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
wait(&status);
|
||||
|
||||
if(status != 0){
|
||||
printf("fork_test failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// check that the parent's mappings are still there.
|
||||
_v1(p1);
|
||||
_v1(p2);
|
||||
|
||||
printf("test fork: OK\n");
|
||||
}
|
||||
|
||||
void
|
||||
more_test()
|
||||
{
|
||||
int fd, pid;
|
||||
char *p;
|
||||
const char * const f = "mmap.dur";
|
||||
|
||||
printf("test munmap prevents access\n");
|
||||
|
||||
makefile(f);
|
||||
if ((fd = open(f, O_RDWR)) == -1)
|
||||
err("open");
|
||||
p = mmap(0, PGSIZE*2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap");
|
||||
close(fd);
|
||||
|
||||
*p = 'X';
|
||||
*(p+PGSIZE) = 'Y';
|
||||
|
||||
pid = fork();
|
||||
if(pid < 0) err("fork");
|
||||
if(pid == 0){
|
||||
*p = 'a';
|
||||
*(p+PGSIZE) = 'b';
|
||||
if(munmap(p+PGSIZE, PGSIZE) == -1)
|
||||
err("munmap");
|
||||
// this should cause a fatal fault
|
||||
printf("*(p+PGSIZE) = %x\n", *(p+PGSIZE));
|
||||
exit(0);
|
||||
}
|
||||
int st = 0;
|
||||
wait(&st);
|
||||
if(st != -1)
|
||||
err("child #1 read unmapped memory");
|
||||
|
||||
pid = fork();
|
||||
if(pid < 0) err("fork");
|
||||
if(pid == 0){
|
||||
*p = 'c';
|
||||
*(p+PGSIZE) = 'd';
|
||||
if(munmap(p, PGSIZE) == -1)
|
||||
err("munmap");
|
||||
// this should cause a fatal fault
|
||||
printf("*p = %x\n", *p);
|
||||
exit(0);
|
||||
}
|
||||
st = 0;
|
||||
wait(&st);
|
||||
if(st != -1)
|
||||
err("child #2 read unmapped memory");
|
||||
|
||||
// parent should still be able to access the memory.
|
||||
*p = 'P';
|
||||
*(p+PGSIZE) = 'Q';
|
||||
|
||||
if(munmap(p, PGSIZE) == -1)
|
||||
err("munmap");
|
||||
|
||||
*(p+PGSIZE) = 'R';
|
||||
if(munmap(p+PGSIZE, PGSIZE) == -1)
|
||||
err("munmap");
|
||||
|
||||
// read the file, check that the first page starts
|
||||
// with P and the second page with R.
|
||||
fd = open(f, O_RDONLY);
|
||||
if(fd < 0) err("open");
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE) err("read");
|
||||
if(buf[0] != 'P') err("first byte of file is wrong");
|
||||
if(read(fd, buf, PGSIZE) != PGSIZE/2) err("read");
|
||||
if(buf[0] != 'R') err("first byte of 2nd page of file is wrong");
|
||||
close(fd);
|
||||
|
||||
printf("test munmap prevents access: OK\n");
|
||||
|
||||
printf("test writes to read-only mapped memory\n");
|
||||
|
||||
makefile(f);
|
||||
pid = fork();
|
||||
if(pid < 0) err("fork");
|
||||
if(pid == 0){
|
||||
if ((fd = open(f, O_RDWR)) == -1)
|
||||
err("open");
|
||||
p = mmap(0, PGSIZE*2, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED)
|
||||
err("mmap");
|
||||
// this should cause a fatal fault
|
||||
*p = 0;
|
||||
exit(*p);
|
||||
}
|
||||
|
||||
st = 0;
|
||||
wait(&st);
|
||||
if(st != -1)
|
||||
err("child wrote read-only mapping");
|
||||
|
||||
printf("test writes to read-only mapped memory: OK\n");
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
statistics(void *buf, int sz)
|
||||
{
|
||||
int fd, i, n;
|
||||
|
||||
fd = open("statistics", O_RDONLY);
|
||||
if(fd < 0) {
|
||||
fprintf(2, "stats: open failed\n");
|
||||
exit(1);
|
||||
}
|
||||
for (i = 0; i < sz; ) {
|
||||
if ((n = read(fd, buf+i, sz-i)) < 0) {
|
||||
break;
|
||||
}
|
||||
i += n;
|
||||
}
|
||||
close(fd);
|
||||
return i;
|
||||
}
|
||||
24
user/stats.c
24
user/stats.c
@ -1,24 +0,0 @@
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "user/user.h"
|
||||
|
||||
#define SZ 4096
|
||||
char buf[SZ];
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int i, n;
|
||||
|
||||
while (1) {
|
||||
n = statistics(buf, SZ);
|
||||
for (i = 0; i < n; i++) {
|
||||
write(1, buf+i, 1);
|
||||
}
|
||||
if (n != SZ)
|
||||
break;
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
#ifdef LAB_MMAP
|
||||
|
||||
typedef unsigned long size_t;
|
||||
typedef long int off_t;
|
||||
void *mmap(void*,size_t,int,int,int,off_t);
|
||||
int munmap(void*,size_t);
|
||||
#endif
|
||||
struct stat;
|
||||
|
||||
@ -59,3 +62,5 @@ int statistics(void*, int);
|
||||
// umalloc.c
|
||||
void* malloc(uint);
|
||||
void free(void*);
|
||||
|
||||
#define DEBUG() printf("File %s, Line %d, Function %s\n",__FILE__,__LINE__,__func__);
|
||||
|
||||
@ -36,3 +36,5 @@ entry("getpid");
|
||||
entry("sbrk");
|
||||
entry("sleep");
|
||||
entry("uptime");
|
||||
entry("mmap");
|
||||
entry("munmap");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user