task2 finished

This commit is contained in:
2025-05-27 15:59:57 +08:00
parent 5292fdc6ac
commit bd21012f73
6 changed files with 70 additions and 4 deletions

View File

@ -123,14 +123,20 @@ 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);
@ -155,6 +161,9 @@ found:
static void
freeproc(struct proc *p)
{
if (p->usyscall) {
kfree((void*)p->usyscall);
}
if(p->trapframe)
kfree((void*)p->trapframe);
p->trapframe = 0;
@ -202,6 +211,16 @@ 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;
}
@ -210,6 +229,7 @@ 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);