提交 350e63f7 创建 作者: rtm's avatar rtm

no more proc[] entry per cpu for idle loop

each cpu[] has its own gdt and tss no per-proc gdt or tss, re-write cpu's in scheduler (you win, cliff) main0() switches to cpu[0].mpstack
上级 69332d19
......@@ -67,6 +67,10 @@ usertests : usertests.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o usertests usertests.o $(ULIB)
$(OBJDUMP) -S usertests > usertests.asm
fstests : fstests.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o fstests fstests.o $(ULIB)
$(OBJDUMP) -S fstests > fstests.asm
echo : echo.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o echo echo.o $(ULIB)
$(OBJDUMP) -S echo > echo.asm
......@@ -102,12 +106,12 @@ rm : rm.o $(ULIB)
mkfs : mkfs.c fs.h
cc -o mkfs mkfs.c
fs.img : mkfs userfs usertests echo cat readme init sh ls mkdir rm
./mkfs fs.img userfs usertests echo cat readme init sh ls mkdir rm
fs.img : mkfs userfs usertests echo cat readme init sh ls mkdir rm fstests
./mkfs fs.img userfs usertests echo cat readme init sh ls mkdir rm fstests
-include *.d
clean :
rm -f *.o *.d *.asm vectors.S parport.out \
bootblock kernel xv6.img user1 userfs usertests \
fs.img mkfs echo init
fs.img mkfs echo init fstests
差异被折叠。
#include "user.h"
#include "fcntl.h"
char buf[512];
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
void
sharedfd()
{
int fd, pid, i, n, nc, np;
char buf[10];
unlink("sharedfd");
fd = open("sharedfd", O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for writing");
return;
}
pid = fork();
memset(buf, pid==0?'c':'p', sizeof(buf));
for(i = 0; i < 100; i++){
if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
printf(1, "fstests: write sharedfd failed\n");
break;
}
}
if(pid == 0)
exit();
else
wait();
close(fd);
fd = open("sharedfd", 0);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for reading\n");
return;
}
nc = np = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(i = 0; i < sizeof(buf); i++){
if(buf[i] == 'c')
nc++;
if(buf[i] == 'p')
np++;
}
}
close(fd);
unlink("sharedfd");
if(nc == 1000 && np == 1000)
printf(1, "sharedfd ok\n");
else
printf(1, "sharedfd oops %d %d\n", nc, np);
}
// two processes write two different files at the same
// time, to test block allocation.
void
twofiles()
{
int fd, pid, i, j, n, total;
char *fname;
unlink("f1");
unlink("f2");
pid = fork();
if(pid < 0){
puts("fork failed\n");
return;
}
fname = pid ? "f1" : "f2";
fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){
puts("create failed\n");
exit();
}
memset(buf, pid?'p':'c', 512);
for(i = 0; i < 12; i++){
if((n = write(fd, buf, 500)) != 500){
printf(1, "write failed %d\n", n);
exit();
}
}
close(fd);
if(pid)
wait();
else
exit();
for(i = 0; i < 2; i++){
fd = open(i?"f1":"f2", 0);
total = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(j = 0; j < n; j++){
if(buf[j] != (i?'p':'c')){
puts("wrong char\n");
exit();
}
}
total += n;
}
close(fd);
if(total != 12*500){
printf(1, "wrong length %d\n", total);
exit();
}
}
unlink("f1");
unlink("f2");
puts("twofiles ok\n");
}
// two processes create and delete files in same directory
void
createdelete()
{
int pid, i, fd;
int n = 20;
char name[32];
pid = fork();
if(pid < 0){
puts("fork failed\n");
exit();
}
name[0] = pid ? 'p' : 'c';
name[2] = '\0';
for(i = 0; i < n; i++){
name[1] = '0' + i;
fd = open(name, O_CREATE | O_RDWR);
if(fd < 0){
puts("create failed\n");
exit();
}
close(fd);
if(i > 0 && (i % 2 ) == 0){
name[1] = '0' + (i / 2);
if(unlink(name) < 0){
puts("unlink failed\n");
exit();
}
}
}
if(pid)
wait();
else
exit();
for(i = 0; i < n; i++){
name[0] = 'p';
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= n/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
} else if((i >= 1 && i < n/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
}
if(fd >= 0)
close(fd);
name[0] = 'c';
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= n/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
} else if((i >= 1 && i < n/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
}
if(fd >= 0)
close(fd);
}
for(i = 0; i < n; i++){
name[0] = 'p';
name[1] = '0' + i;
unlink(name);
name[0] = 'c';
unlink(name);
}
printf(1, "createdelete ok\n");
}
// can I unlink a file and still read it?
void
unlinkread()
{
int fd, fd1;
fd = open("unlinkread", O_CREATE | O_RDWR);
if(fd < 0){
puts("create unlinkread failed\n");
exit();
}
write(fd, "hello", 5);
close(fd);
fd = open("unlinkread", O_RDWR);
if(fd < 0){
puts("open unlinkread failed\n");
exit();
}
if(unlink("unlinkread") != 0){
puts("unlink unlinkread failed\n");
exit();
}
fd1 = open("xxx", O_CREATE | O_RDWR);
write(fd1, "yyy", 3);
close(fd1);
if(read(fd, buf, sizeof(buf)) != 5){
puts("unlinkread read failed");
exit();
}
if(buf[0] != 'h'){
puts("unlinkread wrong data\n");
exit();
}
if(write(fd, buf, 10) != 10){
puts("unlinkread write failed\n");
exit();
}
close(fd);
unlink("xxx");
puts("unlinkread ok\n");
}
void
linktest()
{
int fd;
unlink("lf1");
unlink("lf2");
fd = open("lf1", O_CREATE|O_RDWR);
if(fd < 0){
puts("create lf1 failed\n");
exit();
}
if(write(fd, "hello", 5) != 5){
puts("write lf1 failed\n");
exit();
}
close(fd);
if(link("lf1", "lf2") < 0){
puts("link lf1 lf2 failed\n");
exit();
}
unlink("lf1");
if(open("lf1", 0) >= 0){
puts("unlinked lf1 but it is still there!\n");
exit();
}
fd = open("lf2", 0);
if(fd < 0){
puts("open lf2 failed\n");
exit();
}
if(read(fd, buf, sizeof(buf)) != 5){
puts("read lf2 failed\n");
exit();
}
close(fd);
if(link("lf2", "lf2") >= 0){
puts("link lf2 lf2 succeeded! oops\n");
exit();
}
unlink("lf2");
if(link("lf2", "lf1") >= 0){
puts("link non-existant succeeded! oops\n");
exit();
}
if(link(".", "lf1") >= 0){
puts("link . lf1 succeeded! oops\n");
exit();
}
puts("linktest ok\n");
}
// test concurrent create of the same file
void
concreate()
{
char file[3];
int i, pid, n, fd;
char fa[40];
struct {
unsigned short inum;
char name[14];
} de;
file[0] = 'C';
file[2] = '\0';
for(i = 0; i < 40; i++){
file[1] = '0' + i;
unlink(file);
pid = fork();
if(pid && (i % 3) == 1){
link("C0", file);
} else if(pid == 0 && (i % 5) == 1){
link("C0", file);
} else {
fd = open(file, O_CREATE | O_RDWR);
if(fd < 0){
puts("concreate create failed\n");
exit();
}
close(fd);
}
if(pid == 0)
exit();
else
wait();
}
memset(fa, 0, sizeof(fa));
fd = open(".", 0);
n = 0;
while(read(fd, &de, sizeof(de)) > 0){
if(de.inum == 0)
continue;
if(de.name[0] == 'C' && de.name[2] == '\0'){
i = de.name[1] - '0';
if(i < 0 || i >= sizeof(fa)){
printf(1, "concreate weird file %s\n", de.name);
exit();
}
if(fa[i]){
printf(1, "concreate duplicate file %s\n", de.name);
exit();
}
fa[i] = 1;
n++;
}
}
close(fd);
if(n != 40){
puts("concreate not enough files in directory listing\n");
exit();
}
for(i = 0; i < 40; i++){
file[1] = '0' + i;
unlink(file);
}
puts("concreate ok\n");
}
int
main(int argc, char *argv[])
{
puts("fstests starting\n");
concreate();
linktest();
unlinkread();
createdelete();
twofiles();
sharedfd();
puts("fstests finished\n");
exit();
}
......@@ -29,10 +29,14 @@ main0(void)
// clear BSS
memset(edata, 0, end - edata);
// switch to cpu0's cpu stack
asm volatile("movl %0, %%esp" : : "r" (cpus[0].mpstack + MPSTACK - 32));
asm volatile("movl %0, %%ebp" : : "r" (cpus[0].mpstack + MPSTACK));
// Make sure interrupts stay disabled on all processors
// until each signals it is ready, by pretending to hold
// an extra lock.
// xxx maybe replace w/ acquire remembering if FL_IF
// xxx maybe replace w/ acquire remembering if FL_IF was already clear
for(i=0; i<NCPU; i++){
cpus[i].nlock++;
cpus[i].guard1 = 0xdeadbeef;
......@@ -55,16 +59,9 @@ main0(void)
fd_init();
iinit();
// create a fake process per CPU
// so each CPU always has a tss and a gdt
for(p = &proc[0]; p < &proc[NCPU]; p++){
p->state = IDLEPROC;
p->kstack = cpus[p-proc].mpstack;
p->pid = p - proc;
}
// fix process 0 so that copyproc() will work
p = &proc[0];
p->state = IDLEPROC;
p->sz = 4 * PAGE;
p->mem = kalloc(p->sz);
memset(p->mem, 0, p->sz);
......@@ -74,8 +71,9 @@ main0(void)
p->tf->es = p->tf->ds = p->tf->ss = (SEG_UDATA << 3) | 3;
p->tf->cs = (SEG_UCODE << 3) | 3;
p->tf->eflags = FL_IF;
setupsegs(p);
// curproc[cpu()] = p;
// make sure there's a TSS
setupsegs(0);
// initialize I/O devices, let them enable interrupts
console_init();
......@@ -117,7 +115,8 @@ mpmain(void)
lapic_timerinit();
lapic_enableintr();
setupsegs(&proc[cpu()]);
// make sure there's a TSS
setupsegs(0);
cpuid(0, 0, 0, 0, 0); // memory barrier
cpus[cpu()].booted = 1;
......
......@@ -11,7 +11,7 @@ struct spinlock proc_table_lock;
struct proc proc[NPROC];
struct proc *curproc[NCPU];
int next_pid = NCPU;
int next_pid = 1;
extern void forkret(void);
extern void forkret1(struct trapframe*);
......@@ -22,28 +22,39 @@ pinit(void)
}
/*
* set up a process's task state and segment descriptors
* correctly, given its current size and address in memory.
* this should be called whenever the latter change.
* doesn't change the cpu's current segmentation setup.
* set up CPU's segment descriptors and task state for a
* given process. If p==0, set up for "idle" state for
* when scheduler() isn't running any process.
*/
void
setupsegs(struct proc *p)
{
memset(&p->ts, 0, sizeof(struct taskstate));
p->ts.ss0 = SEG_KDATA << 3;
p->ts.esp0 = (uint)(p->kstack + KSTACKSIZE);
struct cpu *c = &cpus[cpu()];
c->ts.ss0 = SEG_KDATA << 3;
if(p){
c->ts.esp0 = (uint)(p->kstack + KSTACKSIZE);
} else {
c->ts.esp0 = 0xffffffff;
}
// XXX it may be wrong to modify the current segment table!
p->gdt[0] = SEG_NULL;
p->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0x100000 + 64*1024, 0); // xxx
p->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
p->gdt[SEG_TSS] = SEG16(STS_T32A, (uint) &p->ts,
sizeof(p->ts), 0);
p->gdt[SEG_TSS].s = 0;
p->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (uint)p->mem, p->sz, 3);
p->gdt[SEG_UDATA] = SEG(STA_W, (uint)p->mem, p->sz, 3);
c->gdt[0] = SEG_NULL;
c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0x100000 + 64*1024, 0); // xxx
c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
c->gdt[SEG_TSS] = SEG16(STS_T32A, (uint) &c->ts, sizeof(c->ts), 0);
c->gdt[SEG_TSS].s = 0;
if(p){
c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (uint)p->mem, p->sz, 3);
c->gdt[SEG_UDATA] = SEG(STA_W, (uint)p->mem, p->sz, 3);
} else {
c->gdt[SEG_UCODE] = SEG_NULL;
c->gdt[SEG_UDATA] = SEG_NULL;
}
lgdt(c->gdt, sizeof c->gdt);
ltr(SEG_TSS << 3);
}
// Look in the process table for an UNUSED proc.
......@@ -101,9 +112,6 @@ copyproc(struct proc* p)
np->state = UNUSED;
return 0;
}
// Initialize segment table.
setupsegs(np);
// Copy trapframe registers from parent.
np->tf = (struct trapframe*)(np->kstack + KSTACKSIZE) - 1;
......@@ -159,26 +167,11 @@ scheduler(void)
if(p->state != RUNNABLE)
continue;
// Run this process.
// XXX move this into swtch or trapret or something.
// It can run on the other stack.
// h/w sets busy bit in TSS descriptor sometimes, and faults
// if it's set in LTR. so clear tss descriptor busy bit.
p->gdt[SEG_TSS].type = STS_T32A;
// XXX should probably have an lgdt() function in x86.h
// to confine all the inline assembly.
// XXX probably ought to lgdt on trap return too, in case
// a system call has moved a program or changed its size.
lgdt(p->gdt, sizeof p->gdt);
// asm volatile("lgdt %0" : : "g" (p->gdt_pd.lim));
ltr(SEG_TSS << 3);
// Switch to chosen process. It is the process's job
// to release proc_table_lock and then reacquire it
// before jumping back to us.
if(0) cprintf("cpu%d: run %d\n", cpu(), p-proc);
setupsegs(p);
curproc[cpu()] = p;
p->state = RUNNING;
if(setjmp(&cpus[cpu()].jmpbuf) == 0)
......@@ -199,9 +192,7 @@ scheduler(void)
panic("scheduler lock");
}
setupsegs(&proc[cpu()]);
// XXX if not holding proc_table_lock panic.
setupsegs(0);
}
release(&proc_table_lock);
......
......@@ -48,8 +48,6 @@ struct proc{
struct fd *fds[NOFILE];
struct inode *cwd;
struct taskstate ts; // only to give cpu address of kernel stack
struct segdesc gdt[NSEGS];
uint esp; // kernel stack pointer
uint ebp; // kernel frame pointer
......@@ -67,6 +65,8 @@ extern struct proc *curproc[NCPU]; // can be NULL if no proc running.
struct cpu {
uchar apicid; // Local APIC ID
struct jmpbuf jmpbuf;
struct taskstate ts; // only to give cpu address of kernel stack
struct segdesc gdt[NSEGS];
int guard1;
char mpstack[MPSTACK]; // per-cpu start-up stack
int guard2;
......
......@@ -41,8 +41,8 @@ trap(struct trapframe *tf)
panic("interrupt while holding a lock");
}
if(cpu() == 1 && curproc[cpu()] == 0){
if(&tf < cpus[cpu()].mpstack || &tf > cpus[cpu()].mpstack + 512){
if(curproc[cpu()] == 0){
if(&tf < cpus[cpu()].mpstack || &tf > cpus[cpu()].mpstack + MPSTACK){
cprintf("&tf %x mpstack %x\n", &tf, cpus[cpu()].mpstack);
panic("trap cpu stack");
}
......@@ -125,7 +125,8 @@ trap(struct trapframe *tf)
return;
}
cprintf("trap %d\n", v);
cprintf("trap %d from cpu %d eip %x\n", v, cpu(), tf->eip);
panic("trap");
return;
}
......@@ -115,305 +115,11 @@ exitwait(void)
puts("exitwait ok\n");
}
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
void
sharedfd()
{
int fd, pid, i, n, nc, np;
char buf[10];
unlink("sharedfd");
fd = open("sharedfd", O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "usertests: cannot open sharedfd for writing");
return;
}
pid = fork();
memset(buf, pid==0?'c':'p', sizeof(buf));
for(i = 0; i < 100; i++){
if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
printf(1, "usertests: write sharedfd failed\n");
break;
}
}
if(pid == 0)
exit();
else
wait();
close(fd);
fd = open("sharedfd", 0);
if(fd < 0){
printf(1, "usertests: cannot open sharedfd for reading\n");
return;
}
nc = np = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(i = 0; i < sizeof(buf); i++){
if(buf[i] == 'c')
nc++;
if(buf[i] == 'p')
np++;
}
}
close(fd);
unlink("sharedfd");
if(nc == 1000 && np == 1000)
printf(1, "sharedfd ok\n");
else
printf(1, "sharedfd oops %d %d\n", nc, np);
}
// two processes write two different files at the same
// time, to test block allocation.
void
twofiles()
{
int fd, pid, i, j, n, total;
char *fname;
unlink("f1");
unlink("f2");
pid = fork();
if(pid < 0){
puts("fork failed\n");
return;
}
fname = pid ? "f1" : "f2";
fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){
puts("create failed\n");
exit();
}
memset(buf, pid?'p':'c', 512);
for(i = 0; i < 12; i++){
if((n = write(fd, buf, 500)) != 500){
printf(1, "write failed %d\n", n);
exit();
}
}
close(fd);
if(pid)
wait();
else
exit();
for(i = 0; i < 2; i++){
fd = open(i?"f1":"f2", 0);
total = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(j = 0; j < n; j++){
if(buf[j] != (i?'p':'c')){
puts("wrong char\n");
exit();
}
}
total += n;
}
close(fd);
if(total != 12*500){
printf(1, "wrong length %d\n", total);
exit();
}
}
unlink("f1");
unlink("f2");
puts("twofiles ok\n");
}
// two processes create and delete files in same directory
void
createdelete()
{
int pid, i, fd;
int n = 20;
char name[32];
pid = fork();
if(pid < 0){
puts("fork failed\n");
exit();
}
name[0] = pid ? 'p' : 'c';
name[2] = '\0';
for(i = 0; i < n; i++){
name[1] = '0' + i;
fd = open(name, O_CREATE | O_RDWR);
if(fd < 0){
puts("create failed\n");
exit();
}
close(fd);
if(i > 0 && (i % 2 ) == 0){
name[1] = '0' + (i / 2);
if(unlink(name) < 0){
puts("unlink failed\n");
exit();
}
}
}
if(pid)
wait();
else
exit();
for(i = 0; i < n; i++){
name[0] = 'p';
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= n/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
} else if((i >= 1 && i < n/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
}
if(fd >= 0)
close(fd);
name[0] = 'c';
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= n/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
} else if((i >= 1 && i < n/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
}
if(fd >= 0)
close(fd);
}
for(i = 0; i < n; i++){
name[0] = 'p';
name[1] = '0' + i;
unlink(name);
name[0] = 'c';
unlink(name);
}
printf(1, "createdelete ok\n");
}
// can I unlink a file and still read it?
void
unlinkread()
{
int fd, fd1;
fd = open("unlinkread", O_CREATE | O_RDWR);
if(fd < 0){
puts("create unlinkread failed\n");
exit();
}
write(fd, "hello", 5);
close(fd);
fd = open("unlinkread", O_RDWR);
if(fd < 0){
puts("open unlinkread failed\n");
exit();
}
if(unlink("unlinkread") != 0){
puts("unlink unlinkread failed\n");
exit();
}
fd1 = open("xxx", O_CREATE | O_RDWR);
write(fd1, "yyy", 3);
close(fd1);
if(read(fd, buf, sizeof(buf)) != 5){
puts("unlinkread read failed");
exit();
}
if(buf[0] != 'h'){
puts("unlinkread wrong data\n");
exit();
}
if(write(fd, buf, 10) != 10){
puts("unlinkread write failed\n");
exit();
}
close(fd);
unlink("xxx");
puts("unlinkread ok\n");
}
void
linktest()
{
int fd;
unlink("lf1");
unlink("lf2");
fd = open("lf1", O_CREATE|O_RDWR);
if(fd < 0){
puts("create lf1 failed\n");
exit();
}
if(write(fd, "hello", 5) != 5){
puts("write lf1 failed\n");
exit();
}
close(fd);
if(link("lf1", "lf2") < 0){
puts("link lf1 lf2 failed\n");
exit();
}
unlink("lf1");
if(open("lf1", 0) >= 0){
puts("unlinked lf1 but it is still there!\n");
exit();
}
fd = open("lf2", 0);
if(fd < 0){
puts("open lf2 failed\n");
exit();
}
if(read(fd, buf, sizeof(buf)) != 5){
puts("read lf2 failed\n");
exit();
}
close(fd);
if(link("lf2", "lf2") >= 0){
puts("link lf2 lf2 succeeded! oops\n");
exit();
}
unlink("lf2");
if(link("lf2", "lf1") >= 0){
puts("link non-existant succeeded! oops\n");
exit();
}
if(link(".", "lf1") >= 0){
puts("link . lf1 succeeded! oops\n");
exit();
}
puts("linktest ok\n");
}
int
main(int argc, char *argv[])
{
puts("usertests starting\n");
linktest();
unlinkread();
createdelete();
twofiles();
sharedfd();
pipe1();
preempt();
exitwait();
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论