trapslab initialized

This commit is contained in:
2025-06-12 10:45:04 +08:00
parent 2a8fad821b
commit 028cf61d61
40 changed files with 366 additions and 3188 deletions

View File

@ -123,20 +123,14 @@ allocproc(void)
found:
p->pid = allocpid();
p->state = USED;
// Allocate a trapframe page.
if((p->trapframe = (struct trapframe *)kalloc()) == 0){
release(&p->lock);
return 0;
}
// Allocate a usyscall page.
if((p->usyscall = (struct usyscall *)kalloc()) == 0){
freeproc(p);
release(&p->lock);
return 0;
}
p->usyscall->pid = p->pid ;
// An empty user page table.
p->pagetable = proc_pagetable(p);
@ -161,9 +155,6 @@ found:
static void
freeproc(struct proc *p)
{
if (p->usyscall) {
kfree((void*)p->usyscall);
}
if(p->trapframe)
kfree((void*)p->trapframe);
p->trapframe = 0;
@ -211,16 +202,6 @@ proc_pagetable(struct proc *p)
return 0;
}
// map the usyscall just below TRAMPOFRAME, for trampoline.S.
// 这个页需要设置PTE_U为使得用户态可以访问
if(mappages(pagetable, USYSCALL, PGSIZE,
(uint64)(p->usyscall), PTE_R | PTE_U) < 0){
uvmunmap(pagetable, TRAPFRAME, 1, 0);
uvmunmap(pagetable, TRAMPOLINE, 1, 0);
uvmfree(pagetable, 0);
return 0;
}
return pagetable;
}
@ -229,7 +210,6 @@ proc_pagetable(struct proc *p)
void
proc_freepagetable(pagetable_t pagetable, uint64 sz)
{
uvmunmap(pagetable, USYSCALL, 1, 0);
uvmunmap(pagetable, TRAMPOLINE, 1, 0);
uvmunmap(pagetable, TRAPFRAME, 1, 0);
uvmfree(pagetable, sz);
@ -401,20 +381,8 @@ exit(int status)
release(&wait_lock);
// Jump into the scheduler, never to return.
// If we somehow return from sched(), we're in a bad state
sched();
// If we reach here, something is very wrong.
// But instead of panicking immediately, try to become truly unrunnable
acquire(&p->lock);
p->state = UNUSED; // Mark as unused to prevent rescheduling
release(&p->lock);
// Try one more time to schedule
sched();
// If we still reach here after marking as UNUSED, panic
panic("zombie exit: process returned from sched twice");
panic("zombie exit");
}
// Wait for a child process to exit and return its pid.