static void* allocate_stack ( yarn_context_t* ctx ) { void* ptr = page_allocate(STACK_SIZE, 0, PAGE_READ | PAGE_WRITE); yarn_context_set_stack(ctx, ptr, STACK_SIZE); yarn_check_stack(ptr); DEBUG("allocated a stack at %p\n", ptr); return ptr; }
static int sys_mmap( int fd, void *vaddr ) { struct thread *curr = thread_current(); struct list_elem *e; struct opened_file_elem *opened_elem; int check_valid = 0; int count = 0; // mmap_lock_acquire(); // printf("read bytes : %d\n",read_bytes); // printf("aa\n"); if(vaddr==0) { // mmap_lock_release(); return -1; } // printf("%p\n",(int)vaddr&0xfff); if( ((int)vaddr & 0xfff) !=0 || ( (int)vaddr>=PHYS_BASE -8*1024*1024 && (int)vaddr <=PHYS_BASE ) ) { // printf("misalign\n"); // mmap_lock_release(); return -1; } struct page *check_page = page_lookup(curr,vaddr); if( check_page!=NULL&& (check_page->writable == 0 || check_page->type==p_file) ) { // mmap_lock_release(); return -1; } if(fd<2) { // mmap_lock_release(); return -1; } for( e = list_begin(&curr->openfile_list) ; e != list_end(&curr->openfile_list) ; e = list_next(e) ) { opened_elem=list_entry(e,struct opened_file_elem,elem_f); if(opened_elem->fd==fd) { check_valid=1; break; } } if(check_valid==0) { // mmap_lock_release(); return -1; } for( e = list_begin(&curr->mapfile_list) ; e != list_end(&curr->mapfile_list) ; e = list_next(e) ) { // printf("check\n"); struct mapped_file_elem* mapped_elem=list_entry(e,struct mapped_file_elem,elem_m); // printf("%d\n",mapped_elem->size); if( mapped_elem->vaddr<=vaddr && vaddr<=mapped_elem->vaddr+mapped_elem->size) { // mmap_lock_release(); return -1; } } struct mapped_file_elem *mapped_elem = (struct mapped_file_elem*) malloc(sizeof(struct mapped_file_elem)); // char *name = (char*)malloc(strlen(opened_elem->name)); // strlcpy(name, opened_elem->name , strlen(opened_elem->name)+1); int read_bytes = file_length(opened_elem->opened_file); mapped_elem->mapped_file = opened_elem->opened_file; mapped_elem->vaddr = vaddr; mapped_elem->size = read_bytes; mapped_elem->mapid = opened_elem->fd; // mapped_elem->name = name; list_push_back(&curr->mapfile_list,&mapped_elem->elem_m); // printf("mapid: %d\n",opened_elem->mapid); while(read_bytes >0) { // frame_lock_acquire(); int page_read_bytes = read_bytes<PGSIZE ? read_bytes:PGSIZE; // printf("doing mmap\n"); // printf(" read bytes: %d\n",read_bytes); page_allocate(vaddr, true, p_mmap, page_read_bytes, PGSIZE-read_bytes, count*PGSIZE, mapped_elem->mapid); // printf("mapid : %d\n",page_lookup(curr,vaddr)->mapid); if(page_read_bytes < PGSIZE) { void *kpage=frame_elem_allocate(vaddr,true,PAL_USER); file_read_at( mapped_elem->mapped_file, kpage, page_read_bytes, count*PGSIZE ); pagedir_set_page( thread_current()->pagedir,vaddr,kpage,true ); } count+=1; read_bytes-=PGSIZE; vaddr+=PGSIZE; // frame_lock_release(); } // printf(" page allocate num:%d\n",count); // printf("mmap finish\n"); // mmap_lock_release(); return mapped_elem->mapid; }