Compare commits
2 Commits
fs
...
checkpoint
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
acfd874a05 | ||
|
|
8702675076 |
7
Makefile
7
Makefile
@ -257,15 +257,10 @@ endif
|
|||||||
|
|
||||||
ifeq ($(LAB),fs)
|
ifeq ($(LAB),fs)
|
||||||
UPROGS += \
|
UPROGS += \
|
||||||
$U/_bigfile\
|
$U/_bigfile
|
||||||
$U/_symlinktest
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
ifeq ($(LAB),mmap)
|
|
||||||
UPROGS += \
|
|
||||||
$U/_mmaptest
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(LAB),net)
|
ifeq ($(LAB),net)
|
||||||
UPROGS += \
|
UPROGS += \
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
LAB=fs
|
LAB=lock
|
||||||
|
|||||||
42
grade-lab-fs
42
grade-lab-fs
@ -1,42 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import re
|
|
||||||
from gradelib import *
|
|
||||||
|
|
||||||
r = Runner(save("xv6.out"))
|
|
||||||
|
|
||||||
@test(40, "running bigfile")
|
|
||||||
def test_bigfile():
|
|
||||||
r.run_qemu(shell_script([
|
|
||||||
'bigfile'
|
|
||||||
]), timeout=240)
|
|
||||||
r.match('^wrote 65803 blocks$')
|
|
||||||
r.match('^reading bigfile$')
|
|
||||||
r.match('^bigfile done; ok$')
|
|
||||||
|
|
||||||
@test(0, "running symlinktest")
|
|
||||||
def test_symlinktest():
|
|
||||||
r.run_qemu(shell_script([
|
|
||||||
'symlinktest'
|
|
||||||
]), timeout=20)
|
|
||||||
|
|
||||||
@test(20, "symlinktest: symlinks", parent=test_symlinktest)
|
|
||||||
def test_symlinktest_symlinks():
|
|
||||||
r.match("^test symlinks: ok$")
|
|
||||||
|
|
||||||
@test(20, "symlinktest: concurrent symlinks", parent=test_symlinktest)
|
|
||||||
def test_symlinktest_symlinks():
|
|
||||||
r.match("^test concurrent symlinks: ok$")
|
|
||||||
|
|
||||||
@test(19, "usertests")
|
|
||||||
def test_usertests():
|
|
||||||
r.run_qemu(shell_script([
|
|
||||||
'usertests -q'
|
|
||||||
]), timeout=360)
|
|
||||||
r.match('^ALL TESTS PASSED$')
|
|
||||||
|
|
||||||
@test(1, "time")
|
|
||||||
def test_time():
|
|
||||||
check_time()
|
|
||||||
|
|
||||||
run_tests()
|
|
||||||
66
grade-lab-lock
Executable file
66
grade-lab-lock
Executable file
@ -0,0 +1,66 @@
|
|||||||
|
#!/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()
|
||||||
@ -238,7 +238,7 @@ def make(*target):
|
|||||||
post_make()
|
post_make()
|
||||||
|
|
||||||
def show_command(cmd):
|
def show_command(cmd):
|
||||||
from shlex import quote
|
from pipes import quote
|
||||||
print("\n$", " ".join(map(quote, cmd)))
|
print("\n$", " ".join(map(quote, cmd)))
|
||||||
|
|
||||||
def maybe_unlink(*paths):
|
def maybe_unlink(*paths):
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
|
#ifdef LAB_MMAP
|
||||||
|
typedef unsigned long size_t;
|
||||||
|
typedef long int off_t;
|
||||||
|
#endif
|
||||||
struct buf;
|
struct buf;
|
||||||
struct context;
|
struct context;
|
||||||
struct file;
|
struct file;
|
||||||
@ -117,6 +121,10 @@ 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);
|
||||||
|
int atomic_read4(int *addr);
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
void freelock(struct spinlock*);
|
||||||
|
#endif
|
||||||
|
|
||||||
// sleeplock.c
|
// sleeplock.c
|
||||||
void acquiresleep(struct sleeplock*);
|
void acquiresleep(struct sleeplock*);
|
||||||
@ -173,6 +181,12 @@ 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);
|
||||||
int copyinstr(pagetable_t, char *, uint64, uint64);
|
int copyinstr(pagetable_t, char *, uint64, uint64);
|
||||||
|
#if defined(LAB_PGTBL) || defined(SOL_MMAP)
|
||||||
|
void vmprint(pagetable_t);
|
||||||
|
#endif
|
||||||
|
#ifdef LAB_PGTBL
|
||||||
|
pte_t* pgpte(pagetable_t, uint64);
|
||||||
|
#endif
|
||||||
|
|
||||||
// plic.c
|
// plic.c
|
||||||
void plicinit(void);
|
void plicinit(void);
|
||||||
@ -188,4 +202,38 @@ 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]))
|
||||||
|
|
||||||
#define DEBUG() printf("File: %s, Line: %d, Function: %s\n", __FILE__, __LINE__, __func__)
|
|
||||||
|
|
||||||
|
#ifdef LAB_PGTBL
|
||||||
|
// vmcopyin.c
|
||||||
|
int copyin_new(pagetable_t, char *, uint64, uint64);
|
||||||
|
int copyinstr_new(pagetable_t, char *, uint64, uint64);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
// stats.c
|
||||||
|
void statsinit(void);
|
||||||
|
void statsinc(void);
|
||||||
|
|
||||||
|
// sprintf.c
|
||||||
|
int snprintf(char*, unsigned long, const char*, ...);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef KCSAN
|
||||||
|
void kcsaninit();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LAB_NET
|
||||||
|
// pci.c
|
||||||
|
void pci_init();
|
||||||
|
|
||||||
|
// e1000.c
|
||||||
|
void e1000_init(uint32 *);
|
||||||
|
void e1000_intr(void);
|
||||||
|
int e1000_transmit(char *, int);
|
||||||
|
|
||||||
|
// net.c
|
||||||
|
void netinit(void);
|
||||||
|
void net_rx(char *buf, int len);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@ -3,4 +3,3 @@
|
|||||||
#define O_RDWR 0x002
|
#define O_RDWR 0x002
|
||||||
#define O_CREATE 0x200
|
#define O_CREATE 0x200
|
||||||
#define O_TRUNC 0x400
|
#define O_TRUNC 0x400
|
||||||
#define O_NOFOLLOW 0x004
|
|
||||||
@ -26,7 +26,7 @@ struct inode {
|
|||||||
short minor;
|
short minor;
|
||||||
short nlink;
|
short nlink;
|
||||||
uint size;
|
uint size;
|
||||||
uint addrs[NDIRECT+2];
|
uint addrs[NDIRECT+1];
|
||||||
};
|
};
|
||||||
|
|
||||||
// map major device number to device functions.
|
// map major device number to device functions.
|
||||||
@ -38,3 +38,4 @@ struct devsw {
|
|||||||
extern struct devsw devsw[];
|
extern struct devsw devsw[];
|
||||||
|
|
||||||
#define CONSOLE 1
|
#define CONSOLE 1
|
||||||
|
#define STATS 2
|
||||||
|
|||||||
104
kernel/fs.c
104
kernel/fs.c
@ -295,11 +295,11 @@ ilock(struct inode *ip)
|
|||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
struct dinode *dip;
|
struct dinode *dip;
|
||||||
|
|
||||||
if(ip == 0 || ip->ref < 1)
|
if(ip == 0 || atomic_read4(&ip->ref) < 1)
|
||||||
panic("ilock");
|
panic("ilock");
|
||||||
|
|
||||||
acquiresleep(&ip->lock);
|
acquiresleep(&ip->lock);
|
||||||
|
|
||||||
if(ip->valid == 0){
|
if(ip->valid == 0){
|
||||||
bp = bread(ip->dev, IBLOCK(ip->inum, sb));
|
bp = bread(ip->dev, IBLOCK(ip->inum, sb));
|
||||||
dip = (struct dinode*)bp->data + ip->inum%IPB;
|
dip = (struct dinode*)bp->data + ip->inum%IPB;
|
||||||
@ -320,7 +320,7 @@ ilock(struct inode *ip)
|
|||||||
void
|
void
|
||||||
iunlock(struct inode *ip)
|
iunlock(struct inode *ip)
|
||||||
{
|
{
|
||||||
if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1)
|
if(ip == 0 || !holdingsleep(&ip->lock) || atomic_read4(&ip->ref) < 1)
|
||||||
panic("iunlock");
|
panic("iunlock");
|
||||||
|
|
||||||
releasesleep(&ip->lock);
|
releasesleep(&ip->lock);
|
||||||
@ -398,61 +398,24 @@ bmap(struct inode *ip, uint bn)
|
|||||||
|
|
||||||
if(bn < NINDIRECT){
|
if(bn < NINDIRECT){
|
||||||
// Load indirect block, allocating if necessary.
|
// Load indirect block, allocating if necessary.
|
||||||
if(bn<NINDIRECT1){
|
if((addr = ip->addrs[NDIRECT]) == 0){
|
||||||
if((addr = ip->addrs[NDIRECT]) == 0){
|
addr = balloc(ip->dev);
|
||||||
addr = balloc(ip->dev);
|
if(addr == 0)
|
||||||
if(addr == 0)
|
return 0;
|
||||||
return 0;
|
ip->addrs[NDIRECT] = addr;
|
||||||
ip->addrs[NDIRECT] = addr;
|
|
||||||
}
|
|
||||||
bp = bread(ip->dev, addr);
|
|
||||||
a = (uint*)bp->data;
|
|
||||||
if((addr = a[bn]) == 0){
|
|
||||||
addr = balloc(ip->dev);
|
|
||||||
if(addr){
|
|
||||||
a[bn] = addr;
|
|
||||||
log_write(bp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
brelse(bp);
|
|
||||||
return addr;
|
|
||||||
}else{
|
|
||||||
bn-=NINDIRECT1;
|
|
||||||
if((addr = ip->addrs[NDIRECT+1])==0){
|
|
||||||
addr = balloc(ip->dev);
|
|
||||||
if(addr == 0)
|
|
||||||
return 0;
|
|
||||||
ip->addrs[NDIRECT+1] = addr;
|
|
||||||
}
|
|
||||||
bp=bread(ip->dev,addr);
|
|
||||||
a = (uint *)bp->data;
|
|
||||||
const uint offset=bn%NINDIRECT1;
|
|
||||||
bn/=NINDIRECT1;
|
|
||||||
if((addr = a[bn])==0){
|
|
||||||
addr=balloc(ip->dev);
|
|
||||||
if(addr){
|
|
||||||
a[bn] = addr;
|
|
||||||
log_write(bp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
struct buf *bp=bread(ip->dev,addr);
|
|
||||||
a = (uint *)bp->data;
|
|
||||||
if((addr = a[offset])==0){
|
|
||||||
addr=balloc(ip->dev);
|
|
||||||
if(addr){
|
|
||||||
a[offset]=addr;
|
|
||||||
log_write(bp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
brelse(bp);
|
|
||||||
}
|
|
||||||
brelse(bp);
|
|
||||||
return addr;
|
|
||||||
}
|
}
|
||||||
|
bp = bread(ip->dev, addr);
|
||||||
|
a = (uint*)bp->data;
|
||||||
|
if((addr = a[bn]) == 0){
|
||||||
|
addr = balloc(ip->dev);
|
||||||
|
if(addr){
|
||||||
|
a[bn] = addr;
|
||||||
|
log_write(bp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
brelse(bp);
|
||||||
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
panic("bmap: out of range");
|
panic("bmap: out of range");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,7 +438,7 @@ itrunc(struct inode *ip)
|
|||||||
if(ip->addrs[NDIRECT]){
|
if(ip->addrs[NDIRECT]){
|
||||||
bp = bread(ip->dev, ip->addrs[NDIRECT]);
|
bp = bread(ip->dev, ip->addrs[NDIRECT]);
|
||||||
a = (uint*)bp->data;
|
a = (uint*)bp->data;
|
||||||
for(j = 0; j < NINDIRECT1; j++){
|
for(j = 0; j < NINDIRECT; j++){
|
||||||
if(a[j])
|
if(a[j])
|
||||||
bfree(ip->dev, a[j]);
|
bfree(ip->dev, a[j]);
|
||||||
}
|
}
|
||||||
@ -483,25 +446,7 @@ itrunc(struct inode *ip)
|
|||||||
bfree(ip->dev, ip->addrs[NDIRECT]);
|
bfree(ip->dev, ip->addrs[NDIRECT]);
|
||||||
ip->addrs[NDIRECT] = 0;
|
ip->addrs[NDIRECT] = 0;
|
||||||
}
|
}
|
||||||
if(ip->addrs[NDIRECT+1]){
|
|
||||||
bp = bread(ip->dev,ip->addrs[NDIRECT+1]);
|
|
||||||
a = (uint*)bp->data;
|
|
||||||
for(j = 0;j<NINDIRECT1;++j){
|
|
||||||
if(a[j]){
|
|
||||||
struct buf *bp=bread(ip->dev,a[j]);
|
|
||||||
for(int j=0;j<NINDIRECT1;++j){
|
|
||||||
uint *a=(uint*)bp->data;
|
|
||||||
if(a[j])
|
|
||||||
bfree(ip->dev,a[j]);
|
|
||||||
}
|
|
||||||
brelse(bp);
|
|
||||||
bfree(ip->dev,a[j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
brelse(bp);
|
|
||||||
bfree(ip->dev,ip->addrs[NDIRECT+1]);
|
|
||||||
ip->addrs[NDIRECT+1] = 0;
|
|
||||||
}
|
|
||||||
ip->size = 0;
|
ip->size = 0;
|
||||||
iupdate(ip);
|
iupdate(ip);
|
||||||
}
|
}
|
||||||
@ -562,12 +507,11 @@ writei(struct inode *ip, int user_src, uint64 src, uint off, uint n)
|
|||||||
uint tot, m;
|
uint tot, m;
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
|
|
||||||
if(off > ip->size || off + n < off){
|
if(off > ip->size || off + n < off)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
if(off + n > MAXFILE*BSIZE)
|
||||||
if(off + n > MAXFILE*BSIZE){
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
for(tot=0; tot<n; tot+=m, off+=m, src+=m){
|
for(tot=0; tot<n; tot+=m, off+=m, src+=m){
|
||||||
uint addr = bmap(ip, off/BSIZE);
|
uint addr = bmap(ip, off/BSIZE);
|
||||||
if(addr == 0)
|
if(addr == 0)
|
||||||
|
|||||||
@ -24,9 +24,8 @@ struct superblock {
|
|||||||
|
|
||||||
#define FSMAGIC 0x10203040
|
#define FSMAGIC 0x10203040
|
||||||
|
|
||||||
#define NDIRECT 11
|
#define NDIRECT 12
|
||||||
#define NINDIRECT1 (BSIZE / sizeof(uint))
|
#define NINDIRECT (BSIZE / sizeof(uint))
|
||||||
#define NINDIRECT (NINDIRECT1 + NINDIRECT1 * NINDIRECT1)
|
|
||||||
#define MAXFILE (NDIRECT + NINDIRECT)
|
#define MAXFILE (NDIRECT + NINDIRECT)
|
||||||
|
|
||||||
// On-disk inode structure
|
// On-disk inode structure
|
||||||
@ -36,7 +35,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+2]; // Data block addresses
|
uint addrs[NDIRECT+1]; // Data block addresses
|
||||||
};
|
};
|
||||||
|
|
||||||
// Inodes per block.
|
// Inodes per block.
|
||||||
|
|||||||
323
kernel/kcsan.c
Normal file
323
kernel/kcsan.c
Normal file
@ -0,0 +1,323 @@
|
|||||||
|
#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,6 +12,9 @@ main()
|
|||||||
{
|
{
|
||||||
if(cpuid() == 0){
|
if(cpuid() == 0){
|
||||||
consoleinit();
|
consoleinit();
|
||||||
|
#if defined(LAB_LOCK)
|
||||||
|
statsinit();
|
||||||
|
#endif
|
||||||
printfinit();
|
printfinit();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("xv6 kernel is booting\n");
|
printf("xv6 kernel is booting\n");
|
||||||
@ -28,11 +31,17 @@ main()
|
|||||||
iinit(); // inode table
|
iinit(); // inode table
|
||||||
fileinit(); // file table
|
fileinit(); // file table
|
||||||
virtio_disk_init(); // emulated hard disk
|
virtio_disk_init(); // emulated hard disk
|
||||||
|
#ifdef LAB_NET
|
||||||
|
pci_init();
|
||||||
|
#endif
|
||||||
userinit(); // first user process
|
userinit(); // first user process
|
||||||
|
#ifdef KCSAN
|
||||||
|
kcsaninit();
|
||||||
|
#endif
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
started = 1;
|
started = 1;
|
||||||
} else {
|
} else {
|
||||||
while(started == 0)
|
while(atomic_read4((int *) &started) == 0)
|
||||||
;
|
;
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
printf("hart %d starting\n", cpuid());
|
printf("hart %d starting\n", cpuid());
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
#ifdef LAB_FS
|
#define NPROC 64 // maximum number of processes
|
||||||
#define NPROC 10 // maximum number of processes
|
|
||||||
#else
|
|
||||||
#define NPROC 64 // maximum number of processes (speedsup bigfile)
|
|
||||||
#endif
|
|
||||||
#define NCPU 8 // maximum number of CPUs
|
#define NCPU 8 // maximum number of CPUs
|
||||||
#define NOFILE 16 // open files per process
|
#define NOFILE 16 // open files per process
|
||||||
#define NFILE 100 // open files per system
|
#define NFILE 100 // open files per system
|
||||||
@ -13,21 +9,7 @@
|
|||||||
#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
|
||||||
#ifdef LAB_FS
|
#define FSSIZE 2000 // size of file system in blocks
|
||||||
#define FSSIZE 200000 // size of file system in blocks
|
|
||||||
#else
|
|
||||||
#ifdef LAB_LOCK
|
|
||||||
#define FSSIZE 10000 // size of file system in blocks
|
|
||||||
#else
|
|
||||||
#define FSSIZE 2000 // size of file system in blocks
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#define MAXPATH 128 // maximum file path name
|
#define MAXPATH 128 // maximum file path name
|
||||||
|
|
||||||
#ifdef LAB_UTIL
|
|
||||||
#define USERSTACK 2 // user stack pages
|
|
||||||
#else
|
|
||||||
#define USERSTACK 1 // user stack pages
|
#define USERSTACK 1 // user stack pages
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -68,6 +68,9 @@ pipeclose(struct pipe *pi, int writable)
|
|||||||
}
|
}
|
||||||
if(pi->readopen == 0 && pi->writeopen == 0){
|
if(pi->readopen == 0 && pi->writeopen == 0){
|
||||||
release(&pi->lock);
|
release(&pi->lock);
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
freelock(&pi->lock);
|
||||||
|
#endif
|
||||||
kfree((char*)pi);
|
kfree((char*)pi);
|
||||||
} else
|
} else
|
||||||
release(&pi->lock);
|
release(&pi->lock);
|
||||||
|
|||||||
@ -98,6 +98,7 @@ allocpid()
|
|||||||
pid = nextpid;
|
pid = nextpid;
|
||||||
nextpid = nextpid + 1;
|
nextpid = nextpid + 1;
|
||||||
release(&pid_lock);
|
release(&pid_lock);
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +132,6 @@ found:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// An empty user page table.
|
// An empty user page table.
|
||||||
p->pagetable = proc_pagetable(p);
|
p->pagetable = proc_pagetable(p);
|
||||||
if(p->pagetable == 0){
|
if(p->pagetable == 0){
|
||||||
@ -202,7 +202,6 @@ proc_pagetable(struct proc *p)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return pagetable;
|
return pagetable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +263,6 @@ growproc(int n)
|
|||||||
struct proc *p = myproc();
|
struct proc *p = myproc();
|
||||||
|
|
||||||
sz = p->sz;
|
sz = p->sz;
|
||||||
|
|
||||||
if(n > 0){
|
if(n > 0){
|
||||||
if((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0) {
|
if((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -289,7 +287,7 @@ fork(void)
|
|||||||
if((np = allocproc()) == 0){
|
if((np = allocproc()) == 0){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy user memory from parent to child.
|
// Copy user memory from parent to child.
|
||||||
if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
|
if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
|
||||||
freeproc(np);
|
freeproc(np);
|
||||||
@ -298,7 +296,6 @@ fork(void)
|
|||||||
}
|
}
|
||||||
np->sz = p->sz;
|
np->sz = p->sz;
|
||||||
|
|
||||||
|
|
||||||
// copy saved user registers.
|
// copy saved user registers.
|
||||||
*(np->trapframe) = *(p->trapframe);
|
*(np->trapframe) = *(p->trapframe);
|
||||||
|
|
||||||
@ -316,14 +313,13 @@ fork(void)
|
|||||||
pid = np->pid;
|
pid = np->pid;
|
||||||
|
|
||||||
release(&np->lock);
|
release(&np->lock);
|
||||||
|
|
||||||
acquire(&wait_lock);
|
acquire(&wait_lock);
|
||||||
np->parent = p;
|
np->parent = p;
|
||||||
release(&wait_lock);
|
release(&wait_lock);
|
||||||
|
|
||||||
acquire(&np->lock);
|
acquire(&np->lock);
|
||||||
np->state = RUNNABLE;
|
np->state = RUNNABLE;
|
||||||
|
|
||||||
release(&np->lock);
|
release(&np->lock);
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
@ -364,7 +360,6 @@ exit(int status)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
begin_op();
|
begin_op();
|
||||||
iput(p->cwd);
|
iput(p->cwd);
|
||||||
end_op();
|
end_op();
|
||||||
@ -459,33 +454,28 @@ scheduler(void)
|
|||||||
// processes are waiting.
|
// processes are waiting.
|
||||||
intr_on();
|
intr_on();
|
||||||
|
|
||||||
int nproc = 0;
|
int found = 0;
|
||||||
for(p = proc; p < &proc[NPROC]; p++) {
|
for(p = proc; p < &proc[NPROC]; p++) {
|
||||||
acquire(&p->lock);
|
acquire(&p->lock);
|
||||||
if(p->state != UNUSED) {
|
|
||||||
nproc++;
|
|
||||||
}
|
|
||||||
if(p->state == RUNNABLE) {
|
if(p->state == RUNNABLE) {
|
||||||
// Switch to chosen process. It is the process's job
|
// Switch to chosen process. It is the process's job
|
||||||
// to release its lock and then reacquire it
|
// to release its lock and then reacquire it
|
||||||
// before jumping back to us.
|
// before jumping back to us.
|
||||||
p->state = RUNNING;
|
p->state = RUNNING;
|
||||||
c->proc = p;
|
c->proc = p;
|
||||||
|
|
||||||
swtch(&c->context, &p->context);
|
swtch(&c->context, &p->context);
|
||||||
|
|
||||||
// Process is done running for now.
|
// Process is done running for now.
|
||||||
// It should have changed its p->state before coming back.
|
// It should have changed its p->state before coming back.
|
||||||
c->proc = 0;
|
c->proc = 0;
|
||||||
|
found = 1;
|
||||||
}
|
}
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
}
|
}
|
||||||
if(nproc <= 2) { // only init and sh exist
|
if(found == 0) {
|
||||||
// nothing to run; stop running on this core until an interrupt.
|
// nothing to run; stop running on this core until an interrupt.
|
||||||
intr_on();
|
intr_on();
|
||||||
#ifndef LAB_FS
|
|
||||||
asm volatile("wfi");
|
asm volatile("wfi");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -534,7 +524,7 @@ void
|
|||||||
forkret(void)
|
forkret(void)
|
||||||
{
|
{
|
||||||
static int first = 1;
|
static int first = 1;
|
||||||
|
|
||||||
// Still holding p->lock from scheduler.
|
// Still holding p->lock from scheduler.
|
||||||
release(&myproc()->lock);
|
release(&myproc()->lock);
|
||||||
|
|
||||||
@ -703,6 +693,3 @@ procdump(void)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -204,7 +204,7 @@ r_menvcfg()
|
|||||||
static inline void
|
static inline void
|
||||||
w_menvcfg(uint64 x)
|
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));
|
asm volatile("csrw 0x30a, %0" : : "r" (x));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,6 +314,14 @@ r_sp()
|
|||||||
return x;
|
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
|
// 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
|
||||||
@ -354,6 +362,11 @@ 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
|
||||||
|
|
||||||
|
#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 PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
||||||
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
|
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
|
||||||
|
|
||||||
@ -363,6 +376,12 @@ typedef uint64 *pagetable_t; // 512 PTEs
|
|||||||
#define PTE_X (1L << 3)
|
#define PTE_X (1L << 3)
|
||||||
#define PTE_U (1L << 4) // user can access
|
#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.
|
// shift a physical address to the right place for a PTE.
|
||||||
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
|
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
|
||||||
|
|
||||||
|
|||||||
@ -8,12 +8,52 @@
|
|||||||
#include "proc.h"
|
#include "proc.h"
|
||||||
#include "defs.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
|
void
|
||||||
initlock(struct spinlock *lk, char *name)
|
initlock(struct spinlock *lk, char *name)
|
||||||
{
|
{
|
||||||
lk->name = name;
|
lk->name = name;
|
||||||
lk->locked = 0;
|
lk->locked = 0;
|
||||||
lk->cpu = 0;
|
lk->cpu = 0;
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
lk->nts = 0;
|
||||||
|
lk->n = 0;
|
||||||
|
findslot(lk);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the lock.
|
// Acquire the lock.
|
||||||
@ -25,12 +65,21 @@ acquire(struct spinlock *lk)
|
|||||||
if(holding(lk))
|
if(holding(lk))
|
||||||
panic("acquire");
|
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:
|
// On RISC-V, sync_lock_test_and_set turns into an atomic swap:
|
||||||
// a5 = 1
|
// a5 = 1
|
||||||
// s1 = &lk->locked
|
// s1 = &lk->locked
|
||||||
// amoswap.w.aq a5, a5, (s1)
|
// amoswap.w.aq a5, a5, (s1)
|
||||||
while(__sync_lock_test_and_set(&lk->locked, 1) != 0)
|
while(__sync_lock_test_and_set(&lk->locked, 1) != 0) {
|
||||||
;
|
#ifdef LAB_LOCK
|
||||||
|
__sync_fetch_and_add(&(lk->nts), 1);
|
||||||
|
#else
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Tell the C compiler and the processor to not move loads or stores
|
// Tell the C compiler and the processor to not move loads or stores
|
||||||
// past this point, to ensure that the critical section's memory
|
// past this point, to ensure that the critical section's memory
|
||||||
@ -108,3 +157,61 @@ pop_off(void)
|
|||||||
if(c->noff == 0 && c->intena)
|
if(c->noff == 0 && c->intena)
|
||||||
intr_on();
|
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,5 +5,9 @@ struct spinlock {
|
|||||||
// For debugging:
|
// For debugging:
|
||||||
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.
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
int nts;
|
||||||
|
int n;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
88
kernel/sprintf.c
Normal file
88
kernel/sprintf.c
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#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,6 +32,11 @@ start()
|
|||||||
w_mideleg(0xffff);
|
w_mideleg(0xffff);
|
||||||
w_sie(r_sie() | SIE_SEIE | SIE_STIE | SIE_SSIE);
|
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
|
// configure Physical Memory Protection to give supervisor mode
|
||||||
// access to all of physical memory.
|
// access to all of physical memory.
|
||||||
w_pmpaddr0(0x3fffffffffffffull);
|
w_pmpaddr0(0x3fffffffffffffull);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#define T_DIR 1 // Directory
|
#define T_DIR 1 // Directory
|
||||||
#define T_FILE 2 // File
|
#define T_FILE 2 // File
|
||||||
#define T_DEVICE 3 // Device
|
#define T_DEVICE 3 // Device
|
||||||
#define T_SYMLINK 4
|
|
||||||
struct stat {
|
struct stat {
|
||||||
int dev; // File system's disk device
|
int dev; // File system's disk device
|
||||||
uint ino; // Inode number
|
uint ino; // Inode number
|
||||||
|
|||||||
69
kernel/stats.c
Normal file
69
kernel/stats.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#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,7 @@ extern uint64 sys_unlink(void);
|
|||||||
extern uint64 sys_link(void);
|
extern uint64 sys_link(void);
|
||||||
extern uint64 sys_mkdir(void);
|
extern uint64 sys_mkdir(void);
|
||||||
extern uint64 sys_close(void);
|
extern uint64 sys_close(void);
|
||||||
extern uint64 sys_symlink(void);
|
|
||||||
// An array mapping syscall numbers from syscall.h
|
// An array mapping syscall numbers from syscall.h
|
||||||
// to the function that handles the system call.
|
// to the function that handles the system call.
|
||||||
static uint64 (*syscalls[])(void) = {
|
static uint64 (*syscalls[])(void) = {
|
||||||
@ -126,7 +126,6 @@ static uint64 (*syscalls[])(void) = {
|
|||||||
[SYS_link] sys_link,
|
[SYS_link] sys_link,
|
||||||
[SYS_mkdir] sys_mkdir,
|
[SYS_mkdir] sys_mkdir,
|
||||||
[SYS_close] sys_close,
|
[SYS_close] sys_close,
|
||||||
[SYS_symlink] sys_symlink
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@ -20,4 +20,3 @@
|
|||||||
#define SYS_link 19
|
#define SYS_link 19
|
||||||
#define SYS_mkdir 20
|
#define SYS_mkdir 20
|
||||||
#define SYS_close 21
|
#define SYS_close 21
|
||||||
#define SYS_symlink 22
|
|
||||||
@ -327,27 +327,6 @@ sys_open(void)
|
|||||||
end_op();
|
end_op();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int dep=0;
|
|
||||||
if((omode & O_NOFOLLOW)==0){
|
|
||||||
while(1){
|
|
||||||
ilock(ip);
|
|
||||||
if(ip->type!=T_SYMLINK){
|
|
||||||
iunlock(ip);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
readi(ip,0,(uint64)path,0,MAXPATH);
|
|
||||||
iunlockput(ip);
|
|
||||||
if((ip = namei(path)) == 0){
|
|
||||||
end_op();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
++dep;
|
|
||||||
if(dep>=10){
|
|
||||||
end_op();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ilock(ip);
|
ilock(ip);
|
||||||
if(ip->type == T_DIR && omode != O_RDONLY){
|
if(ip->type == T_DIR && omode != O_RDONLY){
|
||||||
iunlockput(ip);
|
iunlockput(ip);
|
||||||
@ -369,7 +348,7 @@ sys_open(void)
|
|||||||
end_op();
|
end_op();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ip->type == T_DEVICE){
|
if(ip->type == T_DEVICE){
|
||||||
f->type = FD_DEVICE;
|
f->type = FD_DEVICE;
|
||||||
f->major = ip->major;
|
f->major = ip->major;
|
||||||
@ -495,31 +474,6 @@ sys_exec(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64 sys_symlink(){
|
|
||||||
char target[MAXPATH],path[MAXPATH];
|
|
||||||
if(argstr(0, target, MAXPATH) < 0 || argstr(1, path, MAXPATH) < 0)
|
|
||||||
goto ERR1;
|
|
||||||
begin_op();
|
|
||||||
struct inode *ip;
|
|
||||||
if((ip = namei(path)) == 0){
|
|
||||||
if((ip = create(path, T_SYMLINK,0,0))==0)
|
|
||||||
goto ERR2;
|
|
||||||
}else
|
|
||||||
ilock(ip);
|
|
||||||
if(writei(ip,0,(uint64)target,0,MAXPATH)<0){
|
|
||||||
goto ERR3;
|
|
||||||
}
|
|
||||||
iunlockput(ip);
|
|
||||||
end_op();
|
|
||||||
return 0;
|
|
||||||
ERR3:
|
|
||||||
iunlockput(ip);
|
|
||||||
ERR2:
|
|
||||||
end_op();
|
|
||||||
ERR1:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64
|
uint64
|
||||||
sys_pipe(void)
|
sys_pipe(void)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -212,6 +212,28 @@ alloc3_desc(int *idx)
|
|||||||
return 0;
|
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
|
void
|
||||||
virtio_disk_rw(struct buf *b, int write)
|
virtio_disk_rw(struct buf *b, int write)
|
||||||
{
|
{
|
||||||
@ -219,6 +241,10 @@ virtio_disk_rw(struct buf *b, int write)
|
|||||||
|
|
||||||
acquire(&disk.vdisk_lock);
|
acquire(&disk.vdisk_lock);
|
||||||
|
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
checkbuf(b);
|
||||||
|
#endif
|
||||||
|
|
||||||
// the spec's Section 5.2 says that legacy block operations use
|
// the spec's Section 5.2 says that legacy block operations use
|
||||||
// three descriptors: one for type/reserved/sector, one for the
|
// three descriptors: one for type/reserved/sector, one for the
|
||||||
// data, one for a 1-byte status result.
|
// data, one for a 1-byte status result.
|
||||||
|
|||||||
400
user/bcachetest.c
Normal file
400
user/bcachetest.c
Normal file
@ -0,0 +1,400 @@
|
|||||||
|
#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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,63 +0,0 @@
|
|||||||
#include "kernel/types.h"
|
|
||||||
#include "kernel/stat.h"
|
|
||||||
#include "user/user.h"
|
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
#include "kernel/fs.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main()
|
|
||||||
{
|
|
||||||
char buf[BSIZE];
|
|
||||||
int fd, i, blocks, readblocks;
|
|
||||||
|
|
||||||
fd = open("big.file", O_CREATE | O_WRONLY);
|
|
||||||
if(fd < 0){
|
|
||||||
printf("bigfile: cannot open big.file for writing\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
blocks = 0;
|
|
||||||
while(1){
|
|
||||||
*(int*)buf = blocks;
|
|
||||||
int cc = write(fd, buf, sizeof(buf));
|
|
||||||
if(cc <= 0)
|
|
||||||
break;
|
|
||||||
blocks++;
|
|
||||||
if (blocks % 100 == 0)
|
|
||||||
printf(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nwrote %d blocks\n", blocks);
|
|
||||||
if(blocks != 65803) {
|
|
||||||
printf("bigfile: file is too small\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
fd = open("big.file", O_RDONLY);
|
|
||||||
printf("reading bigfile\n");
|
|
||||||
if(fd < 0){
|
|
||||||
printf("bigfile: cannot re-open big.file for reading\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
readblocks = 0;
|
|
||||||
for(i = 0; i < blocks; i++){
|
|
||||||
int cc = read(fd, buf, sizeof(buf));
|
|
||||||
if(cc <= 0){
|
|
||||||
printf("bigfile: read error at block %d\n", i);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
if(*(int*)buf != i){
|
|
||||||
printf("bigfile: read the wrong data (%d) for block %d\n",
|
|
||||||
*(int*)buf, i);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
readblocks++;
|
|
||||||
if (readblocks % 100 == 0)
|
|
||||||
printf(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nbigfile done; ok\n");
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
@ -18,6 +18,7 @@ main(void)
|
|||||||
|
|
||||||
if(open("console", O_RDWR) < 0){
|
if(open("console", O_RDWR) < 0){
|
||||||
mknod("console", CONSOLE, 0);
|
mknod("console", CONSOLE, 0);
|
||||||
|
mknod("statistics", STATS, 0);
|
||||||
open("console", O_RDWR);
|
open("console", O_RDWR);
|
||||||
}
|
}
|
||||||
dup(0); // stdout
|
dup(0); // stdout
|
||||||
|
|||||||
198
user/kalloctest.c
Normal file
198
user/kalloctest.c
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
24
user/statistics.c
Normal file
24
user/statistics.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#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
Normal file
24
user/stats.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#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,244 +0,0 @@
|
|||||||
#include "kernel/param.h"
|
|
||||||
#include "kernel/types.h"
|
|
||||||
#include "kernel/stat.h"
|
|
||||||
#include "kernel/riscv.h"
|
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
#include "kernel/spinlock.h"
|
|
||||||
#include "kernel/sleeplock.h"
|
|
||||||
#include "kernel/fs.h"
|
|
||||||
#include "kernel/file.h"
|
|
||||||
#include "user/user.h"
|
|
||||||
|
|
||||||
#define fail(msg) do {printf("FAILURE: " msg "\n"); failed = 1; goto done;} while (0);
|
|
||||||
static int failed = 0;
|
|
||||||
|
|
||||||
static void testsymlink(void);
|
|
||||||
static void concur(void);
|
|
||||||
static void cleanup(void);
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
cleanup();
|
|
||||||
testsymlink();
|
|
||||||
concur();
|
|
||||||
exit(failed);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
cleanup(void)
|
|
||||||
{
|
|
||||||
unlink("/testsymlink/a");
|
|
||||||
unlink("/testsymlink/b");
|
|
||||||
unlink("/testsymlink/c");
|
|
||||||
unlink("/testsymlink/1");
|
|
||||||
unlink("/testsymlink/2");
|
|
||||||
unlink("/testsymlink/3");
|
|
||||||
unlink("/testsymlink/4");
|
|
||||||
unlink("/testsymlink/z");
|
|
||||||
unlink("/testsymlink/y");
|
|
||||||
for(int i = 0; i < NINODE+2; i++){
|
|
||||||
char name[32];
|
|
||||||
memset(name, 0, sizeof(name));
|
|
||||||
const char *base = "/testsymlink/";
|
|
||||||
strcpy(name, base);
|
|
||||||
name[strlen(base)+0] = 'a' + (i / 26);
|
|
||||||
name[strlen(base)+1] = 'a' + (i % 26);
|
|
||||||
unlink(name);
|
|
||||||
}
|
|
||||||
unlink("/testsymlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
// stat a symbolic link using O_NOFOLLOW
|
|
||||||
static int
|
|
||||||
stat_slink(char *pn, struct stat *st)
|
|
||||||
{
|
|
||||||
int fd = open(pn, O_RDONLY | O_NOFOLLOW);
|
|
||||||
if(fd < 0)
|
|
||||||
return -1;
|
|
||||||
if(fstat(fd, st) != 0)
|
|
||||||
return -1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
testsymlink(void)
|
|
||||||
{
|
|
||||||
int r, fd1 = -1, fd2 = -1;
|
|
||||||
char buf[4] = {'a', 'b', 'c', 'd'};
|
|
||||||
char c = 0, c2 = 0;
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
printf("Start: test symlinks\n");
|
|
||||||
|
|
||||||
mkdir("/testsymlink");
|
|
||||||
|
|
||||||
fd1 = open("/testsymlink/a", O_CREATE | O_RDWR);
|
|
||||||
if(fd1 < 0) fail("failed to open a");
|
|
||||||
|
|
||||||
r = symlink("/testsymlink/a", "/testsymlink/b");
|
|
||||||
if(r < 0)
|
|
||||||
fail("symlink b -> a failed");
|
|
||||||
|
|
||||||
if(write(fd1, buf, sizeof(buf)) != 4)
|
|
||||||
fail("failed to write to a");
|
|
||||||
|
|
||||||
if (stat_slink("/testsymlink/b", &st) != 0)
|
|
||||||
fail("failed to stat b");
|
|
||||||
if(st.type != T_SYMLINK)
|
|
||||||
fail("b isn't a symlink");
|
|
||||||
|
|
||||||
fd2 = open("/testsymlink/b", O_RDWR);
|
|
||||||
if(fd2 < 0)
|
|
||||||
fail("failed to open b");
|
|
||||||
read(fd2, &c, 1);
|
|
||||||
if (c != 'a')
|
|
||||||
fail("failed to read bytes from b");
|
|
||||||
|
|
||||||
unlink("/testsymlink/a");
|
|
||||||
if(open("/testsymlink/b", O_RDWR) >= 0)
|
|
||||||
fail("Should not be able to open b after deleting a");
|
|
||||||
|
|
||||||
r = symlink("/testsymlink/b", "/testsymlink/a");
|
|
||||||
if(r < 0)
|
|
||||||
fail("symlink a -> b failed");
|
|
||||||
|
|
||||||
r = open("/testsymlink/b", O_RDWR);
|
|
||||||
if(r >= 0)
|
|
||||||
fail("Should not be able to open b (cycle b->a->b->..)\n");
|
|
||||||
|
|
||||||
r = symlink("/testsymlink/nonexistent", "/testsymlink/c");
|
|
||||||
if(r != 0)
|
|
||||||
fail("Symlinking to nonexistent file should succeed\n");
|
|
||||||
|
|
||||||
r = symlink("/testsymlink/2", "/testsymlink/1");
|
|
||||||
if(r) fail("Failed to link 1->2");
|
|
||||||
r = symlink("/testsymlink/3", "/testsymlink/2");
|
|
||||||
if(r) fail("Failed to link 2->3");
|
|
||||||
r = symlink("/testsymlink/4", "/testsymlink/3");
|
|
||||||
if(r) fail("Failed to link 3->4");
|
|
||||||
|
|
||||||
close(fd1);
|
|
||||||
close(fd2);
|
|
||||||
fd1 = fd2 = -1;
|
|
||||||
|
|
||||||
fd1 = open("/testsymlink/4", O_CREATE | O_RDWR);
|
|
||||||
if(fd1<0) fail("Failed to create 4\n");
|
|
||||||
fd2 = open("/testsymlink/1", O_RDWR);
|
|
||||||
if(fd2<0) fail("Failed to open 1\n");
|
|
||||||
|
|
||||||
c = '#';
|
|
||||||
r = write(fd2, &c, 1);
|
|
||||||
if(r!=1) fail("Failed to write to 1\n");
|
|
||||||
r = read(fd1, &c2, 1);
|
|
||||||
if(r!=1) fail("Failed to read from 4\n");
|
|
||||||
if(c!=c2)
|
|
||||||
fail("Value read from 4 differed from value written to 1\n");
|
|
||||||
|
|
||||||
|
|
||||||
close(fd1);
|
|
||||||
close(fd2);
|
|
||||||
fd1 = fd2 = -1;
|
|
||||||
//
|
|
||||||
// check that many symlinks can co-exist.
|
|
||||||
//
|
|
||||||
for(int i = 0; i < NINODE+2; i++){
|
|
||||||
char name[32];
|
|
||||||
memset(name, 0, sizeof(name));
|
|
||||||
const char *base = "/testsymlink/";
|
|
||||||
strcpy(name, base);
|
|
||||||
name[strlen(base)+0] = 'a' + (i / 26);
|
|
||||||
name[strlen(base)+1] = 'a' + (i % 26);
|
|
||||||
r = symlink("/testsymlink/4", name);
|
|
||||||
if(r) fail("symlink() failed in many test");
|
|
||||||
}
|
|
||||||
for(int i = 0; i < NINODE+2; i++){
|
|
||||||
char name[32];
|
|
||||||
memset(name, 0, sizeof(name));
|
|
||||||
const char *base = "/testsymlink/";
|
|
||||||
strcpy(name, base);
|
|
||||||
name[strlen(base)+0] = 'a' + (i / 26);
|
|
||||||
name[strlen(base)+1] = 'a' + (i % 26);
|
|
||||||
fd1 = open(name, O_RDONLY);
|
|
||||||
if(fd1 < 0)
|
|
||||||
fail("open() failed in many test");
|
|
||||||
char buf[16];
|
|
||||||
buf[0] = '\0';
|
|
||||||
if(read(fd1, buf, sizeof(buf)) != 1)
|
|
||||||
fail("read() failed in many test");
|
|
||||||
if(buf[0] != '#')
|
|
||||||
fail("wrong content in many test");
|
|
||||||
close(fd1);
|
|
||||||
fd1 = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
unlink("/testsymlink/a");
|
|
||||||
if(symlink("/README", "/testsymlink/a") != 0)
|
|
||||||
fail("could not link to /README");
|
|
||||||
fd1 = open("/testsymlink/a", O_RDONLY);
|
|
||||||
if(fd1 < 0)
|
|
||||||
fail("could not open symlink pointing to /README");
|
|
||||||
close(fd1);
|
|
||||||
fd1 = -1;
|
|
||||||
|
|
||||||
printf("test symlinks: ok\n");
|
|
||||||
done:
|
|
||||||
close(fd1);
|
|
||||||
close(fd2);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
concur(void)
|
|
||||||
{
|
|
||||||
int pid, i;
|
|
||||||
int fd;
|
|
||||||
struct stat st;
|
|
||||||
int nchild = 2;
|
|
||||||
|
|
||||||
printf("Start: test concurrent symlinks\n");
|
|
||||||
|
|
||||||
fd = open("/testsymlink/z", O_CREATE | O_RDWR);
|
|
||||||
if(fd < 0) {
|
|
||||||
printf("FAILED: open failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
for(int j = 0; j < nchild; j++) {
|
|
||||||
pid = fork();
|
|
||||||
if(pid < 0){
|
|
||||||
printf("FAILED: fork failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if(pid == 0) {
|
|
||||||
int m = 0;
|
|
||||||
unsigned int x = (pid ? 1 : 97);
|
|
||||||
for(i = 0; i < 100; i++){
|
|
||||||
x = x * 1103515245 + 12345;
|
|
||||||
if((x % 3) == 0) {
|
|
||||||
symlink("/testsymlink/z", "/testsymlink/y");
|
|
||||||
if (stat_slink("/testsymlink/y", &st) == 0) {
|
|
||||||
m++;
|
|
||||||
if(st.type != T_SYMLINK) {
|
|
||||||
printf("FAILED: type %d not a symbolic link\n", st.type);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unlink("/testsymlink/y");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int r;
|
|
||||||
for(int j = 0; j < nchild; j++) {
|
|
||||||
wait(&r);
|
|
||||||
if(r != 0) {
|
|
||||||
printf("test concurrent symlinks: failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("test concurrent symlinks: ok\n");
|
|
||||||
}
|
|
||||||
20
user/user.h
20
user/user.h
@ -1,3 +1,7 @@
|
|||||||
|
#ifdef LAB_MMAP
|
||||||
|
typedef unsigned long size_t;
|
||||||
|
typedef long int off_t;
|
||||||
|
#endif
|
||||||
struct stat;
|
struct stat;
|
||||||
|
|
||||||
// system calls
|
// system calls
|
||||||
@ -22,7 +26,18 @@ int getpid(void);
|
|||||||
char* sbrk(int);
|
char* sbrk(int);
|
||||||
int sleep(int);
|
int sleep(int);
|
||||||
int uptime(void);
|
int uptime(void);
|
||||||
int symlink(const char *,const char *);
|
#ifdef LAB_NET
|
||||||
|
int bind(uint16);
|
||||||
|
int unbind(uint16);
|
||||||
|
int send(uint16, uint32, uint16, char *, uint32);
|
||||||
|
int recv(uint16, uint32*, uint16*, char *, uint32);
|
||||||
|
#endif
|
||||||
|
#ifdef LAB_PGTBL
|
||||||
|
int ugetpid(void);
|
||||||
|
uint64 pgpte(void*);
|
||||||
|
void kpgtbl(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
// 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*);
|
||||||
@ -37,6 +52,9 @@ void* memset(void*, int, uint);
|
|||||||
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);
|
||||||
|
#ifdef LAB_LOCK
|
||||||
|
int statistics(void*, int);
|
||||||
|
#endif
|
||||||
|
|
||||||
// umalloc.c
|
// umalloc.c
|
||||||
void* malloc(uint);
|
void* malloc(uint);
|
||||||
|
|||||||
@ -36,4 +36,3 @@ entry("getpid");
|
|||||||
entry("sbrk");
|
entry("sbrk");
|
||||||
entry("sleep");
|
entry("sleep");
|
||||||
entry("uptime");
|
entry("uptime");
|
||||||
entry("symlink");
|
|
||||||
Loading…
Reference in New Issue
Block a user