From a0c6239d1e83240ba931fe419e1e2e980de0ee41 Mon Sep 17 00:00:00 2001 From: forever-learner <2360561713@qq.com> Date: Fri, 1 Jul 2022 00:31:40 +0800 Subject: [PATCH] final version of munmap --- linux/kernel/exit.c | 12 ++++++++++-- linux/mm/memory.c | 17 +++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/linux/kernel/exit.c b/linux/kernel/exit.c index 2406ebe..839737c 100644 --- a/linux/kernel/exit.c +++ b/linux/kernel/exit.c @@ -15,7 +15,7 @@ int sys_pause(void); int sys_close(int fd); - +extern int sys_munmap(void * start ,size_t size); void release(struct task_struct * p) { int i; @@ -102,7 +102,15 @@ static void tell_father(int pid) int do_exit(long code) { int i; - + struct mmap_struct *temp=current->mmap; + while(temp) + { + printk("%x\n.",temp->address); + printk("%x\n.",temp->endaddr); + printk("%d\n.",temp->next); + sys_munmap((void *)temp->address,temp->size); + temp=temp->next; + } free_page_tables(get_base(current->ldt[1]),get_limit(0x0f)); free_page_tables(get_base(current->ldt[2]),get_limit(0x17)); for (i=0 ; i>12]++; } - *(unsigned long *)from_page &= (prot&PROT_WRITE); //分配读写权限 + *(unsigned long *)from_page &= (prot&PROT_WRITE)?0xffffffff:0xfffffffd; //分配读写权限 if(flags & MAP_SHARED) { @@ -709,9 +709,9 @@ int sys_munmap(void * start ,size_t size) if(!pre) { if(current->mmap_tail==current->mmap)current->mmap_tail=NULL; - current->mmap==current->mmap->next; + current->mmap=current->mmap->next; } - if(temp->next=NULL) + else if(temp->next=NULL) { current->mmap_tail=pre; pre->next=NULL; @@ -726,12 +726,13 @@ int sys_munmap(void * start ,size_t size) unsigned long phys_addr; unsigned long tpage; unsigned long address=temp->address; + printk("%lx\n",address); if(!(temp->ps & MAP_SHARED)) goto mytag; //若为共享,执行下面的代码 while (address < temp ->endaddr) { - printk("addr: %lx\n",address); + //printk("addr: %lx\n",address); from_page = ((address>>20) & 0xffc); from_page += ((current->start_code>>20) & 0xffc);//页目录 from=*(unsigned long *)from_page; //页表项 @@ -741,7 +742,8 @@ int sys_munmap(void * start ,size_t size) //printk("addr: %lx",*(unsigned long *)from_page); if(!(*(unsigned long *)from_page & 1)) { - printk("error of deleting page."); + printk("error of deleting page at %x.\n",*(unsigned long *)from_page); + printk("current address:%x\n",address); return -1; } if(temp->ps & MAP_SHARED) @@ -778,8 +780,10 @@ int sys_munmap(void * start ,size_t size) } mytag: //取消物理页写权限 + address=temp->address; while (address < temp ->endaddr) { + //printk("%lx\n",address); from_page = ((address>>20) & 0xffc); from_page += ((current->start_code>>20) & 0xffc);//页目录 from=*(unsigned long *)from_page; //页表项 @@ -789,7 +793,8 @@ mytag: //printk("addr: %lx",*(unsigned long *)from_page); if(!(*(unsigned long *)from_page & 1)) { - printk("error of deleting page."); + printk("error of deleting page at %x.\n",*(unsigned long *)from_page); + printk("current address:%x\n",address); return -1; } else{