1 diff --git a/src/threads/thread.c b/src/threads/thread.c
2 index 86614f5..7c19894 100644
3 --- a/src/threads/thread.c
4 +++ b/src/threads/thread.c
6 #include "threads/vaddr.h"
8 #include "userprog/process.h"
9 +#include "userprog/syscall.h"
12 /* Random value for struct thread's `magic' member.
13 @@ -350,6 +351,7 @@ thread_exit (void)
20 /* Remove thread from all threads list, set our status to dying,
21 @@ -608,6 +610,10 @@ init_thread (struct thread *t, const char *name, int priority)
22 strlcpy (t->name, name, sizeof t->name);
23 t->stack = (uint8_t *) t + PGSIZE;
24 t->priority = t->normal_priority = priority;
25 + list_init (&t->children);
26 + t->wait_status = NULL;
27 + list_init (&t->fds);
29 t->magic = THREAD_MAGIC;
30 sema_init (&t->timer_sema, 0);
31 list_init (&t->donors);
32 diff --git a/src/threads/thread.h b/src/threads/thread.h
33 index 6601963..2c85d88 100644
34 --- a/src/threads/thread.h
35 +++ b/src/threads/thread.h
36 @@ -101,6 +101,10 @@ struct thread
37 fixed_point_t recent_cpu; /* Recent amount of CPU time. */
38 struct list_elem allelem; /* List element for all threads list. */
40 + /* Owned by process.c. */
41 + struct wait_status *wait_status; /* This process's completion status. */
42 + struct list children; /* Completion status of children. */
44 /* Shared between thread.c and synch.c. */
45 struct list_elem elem; /* List element. */
47 @@ -113,11 +117,31 @@ struct thread
48 /* Owned by userprog/process.c. */
49 uint32_t *pagedir; /* Page directory. */
51 + struct file *bin_file; /* Executable. */
53 + /* Owned by syscall.c. */
54 + struct list fds; /* List of file descriptors. */
55 + int next_handle; /* Next handle value. */
57 /* Owned by thread.c. */
58 unsigned magic; /* Detects stack overflow. */
61 +/* Tracks the completion of a process.
62 + Reference held by both the parent, in its `children' list,
63 + and by the child, in its `wait_status' pointer. */
66 + struct list_elem elem; /* `children' list element. */
67 + struct lock lock; /* Protects ref_cnt. */
68 + int ref_cnt; /* 2=child and parent both alive,
69 + 1=either child or parent alive,
70 + 0=child and parent both dead. */
71 + tid_t tid; /* Child thread id. */
72 + int exit_code; /* Child exit code, if dead. */
73 + struct semaphore dead; /* 1=child alive, 0=child dead. */
76 /* If false (default), use round-robin scheduler.
77 If true, use multi-level feedback queue scheduler.
78 Controlled by kernel command-line option "-o mlfqs". */
79 diff --git a/src/userprog/exception.c b/src/userprog/exception.c
80 index 19aca12..3682478 100644
81 --- a/src/userprog/exception.c
82 +++ b/src/userprog/exception.c
83 @@ -148,6 +148,14 @@ page_fault (struct intr_frame *f)
84 write = (f->error_code & PF_W) != 0;
85 user = (f->error_code & PF_U) != 0;
87 + /* Handle bad dereferences from system call implementations. */
90 + f->eip = (void (*) (void)) f->eax;
95 /* To implement virtual memory, delete the rest of the function
96 body, and replace it with code that brings in the page to
97 which fault_addr refers. */
98 diff --git a/src/userprog/process.c b/src/userprog/process.c
99 index c0e5215..06ff27e 100644
100 --- a/src/userprog/process.c
101 +++ b/src/userprog/process.c
103 #include "threads/flags.h"
104 #include "threads/init.h"
105 #include "threads/interrupt.h"
106 +#include "threads/malloc.h"
107 #include "threads/palloc.h"
108 #include "threads/thread.h"
109 #include "threads/vaddr.h"
111 static thread_func start_process NO_RETURN;
112 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
113 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
115 +/* Data structure shared between process_execute() in the
116 + invoking thread and start_process() in the newly invoked
120 + const char *file_name; /* Program to load. */
121 + struct semaphore load_done; /* "Up"ed when loading complete. */
122 + struct wait_status *wait_status; /* Child process. */
123 + bool success; /* Program successfully loaded? */
126 /* Starts a new thread running a user program loaded from
127 FILENAME. The new thread may be scheduled (and may even exit)
128 @@ -28,29 +40,37 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp);
130 process_execute (const char *file_name)
133 + struct exec_info exec;
134 + char thread_name[16];
138 - /* Make a copy of FILE_NAME.
139 - Otherwise there's a race between the caller and load(). */
140 - fn_copy = palloc_get_page (0);
141 - if (fn_copy == NULL)
143 - strlcpy (fn_copy, file_name, PGSIZE);
144 + /* Initialize exec_info. */
145 + exec.file_name = file_name;
146 + sema_init (&exec.load_done, 0);
148 /* Create a new thread to execute FILE_NAME. */
149 - tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy);
150 - if (tid == TID_ERROR)
151 - palloc_free_page (fn_copy);
152 + strlcpy (thread_name, file_name, sizeof thread_name);
153 + strtok_r (thread_name, " ", &save_ptr);
154 + tid = thread_create (thread_name, PRI_DEFAULT, start_process, &exec);
155 + if (tid != TID_ERROR)
157 + sema_down (&exec.load_done);
159 + list_push_back (&thread_current ()->children, &exec.wait_status->elem);
167 /* A thread function that loads a user process and starts it
170 -start_process (void *file_name_)
171 +start_process (void *exec_)
173 - char *file_name = file_name_;
174 + struct exec_info *exec = exec_;
175 struct intr_frame if_;
178 @@ -59,10 +79,29 @@ start_process (void *file_name_)
179 if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
181 if_.eflags = FLAG_IF | FLAG_MBS;
182 - success = load (file_name, &if_.eip, &if_.esp);
183 + success = load (exec->file_name, &if_.eip, &if_.esp);
185 + /* Allocate wait_status. */
188 + exec->wait_status = thread_current ()->wait_status
189 + = malloc (sizeof *exec->wait_status);
190 + success = exec->wait_status != NULL;
193 - /* If load failed, quit. */
194 - palloc_free_page (file_name);
195 + /* Initialize wait_status. */
198 + lock_init (&exec->wait_status->lock);
199 + exec->wait_status->ref_cnt = 2;
200 + exec->wait_status->tid = thread_current ()->tid;
201 + exec->wait_status->exit_code = -1;
202 + sema_init (&exec->wait_status->dead, 0);
205 + /* Notify parent thread and clean up. */
206 + exec->success = success;
207 + sema_up (&exec->load_done);
211 @@ -76,18 +115,47 @@ start_process (void *file_name_)
215 +/* Releases one reference to CS and, if it is now unreferenced,
218 +release_child (struct wait_status *cs)
222 + lock_acquire (&cs->lock);
223 + new_ref_cnt = --cs->ref_cnt;
224 + lock_release (&cs->lock);
226 + if (new_ref_cnt == 0)
230 /* Waits for thread TID to die and returns its exit status. If
231 it was terminated by the kernel (i.e. killed due to an
232 exception), returns -1. If TID is invalid or if it was not a
233 child of the calling process, or if process_wait() has already
234 been successfully called for the given TID, returns -1
235 - immediately, without waiting.
237 - This function will be implemented in problem 2-2. For now, it
239 + immediately, without waiting. */
241 -process_wait (tid_t child_tid UNUSED)
242 +process_wait (tid_t child_tid)
244 + struct thread *cur = thread_current ();
245 + struct list_elem *e;
247 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
250 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
251 + if (cs->tid == child_tid)
255 + sema_down (&cs->dead);
256 + exit_code = cs->exit_code;
257 + release_child (cs);
264 @@ -96,8 +164,30 @@ void
267 struct thread *cur = thread_current ();
268 + struct list_elem *e, *next;
271 + /* Close executable (and allow writes). */
272 + file_close (cur->bin_file);
274 + /* Notify parent that we're dead. */
275 + if (cur->wait_status != NULL)
277 + struct wait_status *cs = cur->wait_status;
278 + printf ("%s: exit(%d)\n", cur->name, cs->exit_code);
279 + sema_up (&cs->dead);
280 + release_child (cs);
283 + /* Free entries of children list. */
284 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
287 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
288 + next = list_remove (e);
289 + release_child (cs);
292 /* Destroy the current process's page directory and switch back
293 to the kernel-only page directory. */
295 @@ -195,7 +285,7 @@ struct Elf32_Phdr
296 #define PF_W 2 /* Writable. */
297 #define PF_R 4 /* Readable. */
299 -static bool setup_stack (void **esp);
300 +static bool setup_stack (const char *cmd_line, void **esp);
301 static bool validate_segment (const struct Elf32_Phdr *, struct file *);
302 static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
303 uint32_t read_bytes, uint32_t zero_bytes,
304 @@ -206,13 +296,15 @@ static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
305 and its initial stack pointer into *ESP.
306 Returns true if successful, false otherwise. */
308 -load (const char *file_name, void (**eip) (void), void **esp)
309 +load (const char *cmd_line, void (**eip) (void), void **esp)
311 struct thread *t = thread_current ();
312 + char file_name[NAME_MAX + 2];
313 struct Elf32_Ehdr ehdr;
314 struct file *file = NULL;
316 bool success = false;
320 /* Allocate and activate page directory. */
321 @@ -221,13 +313,22 @@ load (const char *file_name, void (**eip) (void), void **esp)
325 + /* Extract file_name from command line. */
326 + while (*cmd_line == ' ')
328 + strlcpy (file_name, cmd_line, sizeof file_name);
329 + cp = strchr (file_name, ' ');
333 /* Open executable file. */
334 - file = filesys_open (file_name);
335 + t->bin_file = file = filesys_open (file_name);
338 printf ("load: %s: open failed\n", file_name);
341 + file_deny_write (file);
343 /* Read and verify executable header. */
344 if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
345 @@ -302,7 +403,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
349 - if (!setup_stack (esp))
350 + if (!setup_stack (cmd_line, esp))
354 @@ -312,7 +413,6 @@ load (const char *file_name, void (**eip) (void), void **esp)
357 /* We arrive here whether the load is successful or not. */
362 @@ -424,10 +524,92 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage,
366 -/* Create a minimal stack by mapping a zeroed page at the top of
367 - user virtual memory. */
368 +/* Reverse the order of the ARGC pointers to char in ARGV. */
370 +reverse (int argc, char **argv)
372 + for (; argc > 1; argc -= 2, argv++)
374 + char *tmp = argv[0];
375 + argv[0] = argv[argc - 1];
376 + argv[argc - 1] = tmp;
380 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
381 + page-relative stack pointer is *OFS, and then adjusts *OFS
382 + appropriately. The bytes pushed are rounded to a 32-bit
385 + If successful, returns a pointer to the newly pushed object.
386 + On failure, returns a null pointer. */
388 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size)
390 + size_t padsize = ROUND_UP (size, sizeof (uint32_t));
391 + if (*ofs < padsize)
395 + memcpy (kpage + *ofs + (padsize - size), buf, size);
396 + return kpage + *ofs + (padsize - size);
399 +/* Sets up command line arguments in KPAGE, which will be mapped
400 + to UPAGE in user space. The command line arguments are taken
401 + from CMD_LINE, separated by spaces. Sets *ESP to the initial
402 + stack pointer for the process. */
404 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
407 + size_t ofs = PGSIZE;
408 + char *const null = NULL;
409 + char *cmd_line_copy;
410 + char *karg, *saveptr;
414 + /* Push command line string. */
415 + cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
416 + if (cmd_line_copy == NULL)
419 + if (push (kpage, &ofs, &null, sizeof null) == NULL)
422 + /* Parse command line into arguments
423 + and push them in reverse order. */
425 + for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
426 + karg = strtok_r (NULL, " ", &saveptr))
428 + void *uarg = upage + (karg - (char *) kpage);
429 + if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
434 + /* Reverse the order of the command line arguments. */
435 + argv = (char **) (upage + ofs);
436 + reverse (argc, (char **) (kpage + ofs));
438 + /* Push argv, argc, "return address". */
439 + if (push (kpage, &ofs, &argv, sizeof argv) == NULL
440 + || push (kpage, &ofs, &argc, sizeof argc) == NULL
441 + || push (kpage, &ofs, &null, sizeof null) == NULL)
444 + /* Set initial stack pointer. */
445 + *esp = upage + ofs;
449 +/* Create a minimal stack for T by mapping a page at the
450 + top of user virtual memory. Fills in the page using CMD_LINE
451 + and sets *ESP to the stack pointer. */
453 -setup_stack (void **esp)
454 +setup_stack (const char *cmd_line, void **esp)
457 bool success = false;
458 @@ -435,9 +617,9 @@ setup_stack (void **esp)
459 kpage = palloc_get_page (PAL_USER | PAL_ZERO);
462 - success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
465 + uint8_t *upage = ((uint8_t *) PHYS_BASE) - PGSIZE;
466 + if (install_page (upage, kpage, true))
467 + success = init_cmd_line (kpage, upage, cmd_line, esp);
469 palloc_free_page (kpage);
471 diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c
472 index 370c89b..ef31316 100644
473 --- a/src/userprog/syscall.c
474 +++ b/src/userprog/syscall.c
476 #include "userprog/syscall.h"
479 #include <syscall-nr.h>
480 +#include "userprog/process.h"
481 +#include "userprog/pagedir.h"
482 +#include "devices/input.h"
483 +#include "devices/shutdown.h"
484 +#include "filesys/filesys.h"
485 +#include "filesys/file.h"
486 #include "threads/interrupt.h"
487 +#include "threads/malloc.h"
488 +#include "threads/palloc.h"
489 #include "threads/thread.h"
491 +#include "threads/vaddr.h"
494 +static int sys_halt (void);
495 +static int sys_exit (int status);
496 +static int sys_exec (const char *ufile);
497 +static int sys_wait (tid_t);
498 +static int sys_create (const char *ufile, unsigned initial_size);
499 +static int sys_remove (const char *ufile);
500 +static int sys_open (const char *ufile);
501 +static int sys_filesize (int handle);
502 +static int sys_read (int handle, void *udst_, unsigned size);
503 +static int sys_write (int handle, void *usrc_, unsigned size);
504 +static int sys_seek (int handle, unsigned position);
505 +static int sys_tell (int handle);
506 +static int sys_close (int handle);
508 static void syscall_handler (struct intr_frame *);
510 +static void copy_in (void *, const void *, size_t);
512 +/* Serializes file system operations. */
513 +static struct lock fs_lock;
518 intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
519 + lock_init (&fs_lock);
522 +/* System call handler. */
524 +syscall_handler (struct intr_frame *f)
526 + typedef int syscall_function (int, int, int);
528 + /* A system call. */
531 + size_t arg_cnt; /* Number of arguments. */
532 + syscall_function *func; /* Implementation. */
535 + /* Table of system calls. */
536 + static const struct syscall syscall_table[] =
538 + {0, (syscall_function *) sys_halt},
539 + {1, (syscall_function *) sys_exit},
540 + {1, (syscall_function *) sys_exec},
541 + {1, (syscall_function *) sys_wait},
542 + {2, (syscall_function *) sys_create},
543 + {1, (syscall_function *) sys_remove},
544 + {1, (syscall_function *) sys_open},
545 + {1, (syscall_function *) sys_filesize},
546 + {3, (syscall_function *) sys_read},
547 + {3, (syscall_function *) sys_write},
548 + {2, (syscall_function *) sys_seek},
549 + {1, (syscall_function *) sys_tell},
550 + {1, (syscall_function *) sys_close},
553 + const struct syscall *sc;
557 + /* Get the system call. */
558 + copy_in (&call_nr, f->esp, sizeof call_nr);
559 + if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
561 + sc = syscall_table + call_nr;
563 + /* Get the system call arguments. */
564 + ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
565 + memset (args, 0, sizeof args);
566 + copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
568 + /* Execute the system call,
569 + and set the return value. */
570 + f->eax = sc->func (args[0], args[1], args[2]);
573 +/* Returns true if UADDR is a valid, mapped user address,
574 + false otherwise. */
576 +verify_user (const void *uaddr)
578 + return (uaddr < PHYS_BASE
579 + && pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL);
582 +/* Copies a byte from user address USRC to kernel address DST.
583 + USRC must be below PHYS_BASE.
584 + Returns true if successful, false if a segfault occurred. */
586 +get_user (uint8_t *dst, const uint8_t *usrc)
589 + asm ("movl $1f, %%eax; movb %2, %%al; movb %%al, %0; 1:"
590 + : "=m" (*dst), "=&a" (eax) : "m" (*usrc));
594 +/* Writes BYTE to user address UDST.
595 + UDST must be below PHYS_BASE.
596 + Returns true if successful, false if a segfault occurred. */
598 +put_user (uint8_t *udst, uint8_t byte)
601 + asm ("movl $1f, %%eax; movb %b2, %0; 1:"
602 + : "=m" (*udst), "=&a" (eax) : "q" (byte));
606 +/* Copies SIZE bytes from user address USRC to kernel address
608 + Call thread_exit() if any of the user accesses are invalid. */
610 -syscall_handler (struct intr_frame *f UNUSED)
611 +copy_in (void *dst_, const void *usrc_, size_t size)
613 + uint8_t *dst = dst_;
614 + const uint8_t *usrc = usrc_;
616 + for (; size > 0; size--, dst++, usrc++)
617 + if (usrc >= (uint8_t *) PHYS_BASE || !get_user (dst, usrc))
621 +/* Creates a copy of user string US in kernel memory
622 + and returns it as a page that must be freed with
623 + palloc_free_page().
624 + Truncates the string at PGSIZE bytes in size.
625 + Call thread_exit() if any of the user accesses are invalid. */
627 +copy_in_string (const char *us)
632 + ks = palloc_get_page (0);
636 + for (length = 0; length < PGSIZE; length++)
638 + if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++))
640 + palloc_free_page (ks);
644 + if (ks[length] == '\0')
647 + ks[PGSIZE - 1] = '\0';
651 +/* Halt system call. */
655 + shutdown_power_off ();
658 +/* Exit system call. */
660 +sys_exit (int exit_code)
662 + thread_current ()->wait_status->exit_code = exit_code;
667 +/* Exec system call. */
669 +sys_exec (const char *ufile)
672 + char *kfile = copy_in_string (ufile);
674 + lock_acquire (&fs_lock);
675 + tid = process_execute (kfile);
676 + lock_release (&fs_lock);
678 + palloc_free_page (kfile);
683 +/* Wait system call. */
685 +sys_wait (tid_t child)
687 + return process_wait (child);
690 +/* Create system call. */
692 +sys_create (const char *ufile, unsigned initial_size)
694 + char *kfile = copy_in_string (ufile);
697 + lock_acquire (&fs_lock);
698 + ok = filesys_create (kfile, initial_size);
699 + lock_release (&fs_lock);
701 + palloc_free_page (kfile);
706 +/* Remove system call. */
708 +sys_remove (const char *ufile)
710 - printf ("system call!\n");
711 + char *kfile = copy_in_string (ufile);
714 + lock_acquire (&fs_lock);
715 + ok = filesys_remove (kfile);
716 + lock_release (&fs_lock);
718 + palloc_free_page (kfile);
723 +/* A file descriptor, for binding a file handle to a file. */
724 +struct file_descriptor
726 + struct list_elem elem; /* List element. */
727 + struct file *file; /* File. */
728 + int handle; /* File handle. */
731 +/* Open system call. */
733 +sys_open (const char *ufile)
735 + char *kfile = copy_in_string (ufile);
736 + struct file_descriptor *fd;
739 + fd = malloc (sizeof *fd);
742 + lock_acquire (&fs_lock);
743 + fd->file = filesys_open (kfile);
744 + if (fd->file != NULL)
746 + struct thread *cur = thread_current ();
747 + handle = fd->handle = cur->next_handle++;
748 + list_push_front (&cur->fds, &fd->elem);
752 + lock_release (&fs_lock);
755 + palloc_free_page (kfile);
759 +/* Returns the file descriptor associated with the given handle.
760 + Terminates the process if HANDLE is not associated with an
762 +static struct file_descriptor *
763 +lookup_fd (int handle)
765 + struct thread *cur = thread_current ();
766 + struct list_elem *e;
768 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
771 + struct file_descriptor *fd;
772 + fd = list_entry (e, struct file_descriptor, elem);
773 + if (fd->handle == handle)
780 +/* Filesize system call. */
782 +sys_filesize (int handle)
784 + struct file_descriptor *fd = lookup_fd (handle);
787 + lock_acquire (&fs_lock);
788 + size = file_length (fd->file);
789 + lock_release (&fs_lock);
794 +/* Read system call. */
796 +sys_read (int handle, void *udst_, unsigned size)
798 + uint8_t *udst = udst_;
799 + struct file_descriptor *fd;
800 + int bytes_read = 0;
802 + /* Handle keyboard reads. */
803 + if (handle == STDIN_FILENO)
805 + for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++)
806 + if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, input_getc ()))
811 + /* Handle all other reads. */
812 + fd = lookup_fd (handle);
813 + lock_acquire (&fs_lock);
816 + /* How much to read into this page? */
817 + size_t page_left = PGSIZE - pg_ofs (udst);
818 + size_t read_amt = size < page_left ? size : page_left;
821 + /* Check that touching this page is okay. */
822 + if (!verify_user (udst))
824 + lock_release (&fs_lock);
828 + /* Read from file into page. */
829 + retval = file_read (fd->file, udst, read_amt);
832 + if (bytes_read == 0)
836 + bytes_read += retval;
838 + /* If it was a short read we're done. */
839 + if (retval != (off_t) read_amt)
846 + lock_release (&fs_lock);
851 +/* Write system call. */
853 +sys_write (int handle, void *usrc_, unsigned size)
855 + uint8_t *usrc = usrc_;
856 + struct file_descriptor *fd = NULL;
857 + int bytes_written = 0;
859 + /* Lookup up file descriptor. */
860 + if (handle != STDOUT_FILENO)
861 + fd = lookup_fd (handle);
863 + lock_acquire (&fs_lock);
866 + /* How much bytes to write to this page? */
867 + size_t page_left = PGSIZE - pg_ofs (usrc);
868 + size_t write_amt = size < page_left ? size : page_left;
871 + /* Check that we can touch this user page. */
872 + if (!verify_user (usrc))
874 + lock_release (&fs_lock);
878 + /* Do the write. */
879 + if (handle == STDOUT_FILENO)
881 + putbuf (usrc, write_amt);
882 + retval = write_amt;
885 + retval = file_write (fd->file, usrc, write_amt);
888 + if (bytes_written == 0)
889 + bytes_written = -1;
892 + bytes_written += retval;
894 + /* If it was a short write we're done. */
895 + if (retval != (off_t) write_amt)
902 + lock_release (&fs_lock);
904 + return bytes_written;
907 +/* Seek system call. */
909 +sys_seek (int handle, unsigned position)
911 + struct file_descriptor *fd = lookup_fd (handle);
913 + lock_acquire (&fs_lock);
914 + if ((off_t) position >= 0)
915 + file_seek (fd->file, position);
916 + lock_release (&fs_lock);
921 +/* Tell system call. */
923 +sys_tell (int handle)
925 + struct file_descriptor *fd = lookup_fd (handle);
928 + lock_acquire (&fs_lock);
929 + position = file_tell (fd->file);
930 + lock_release (&fs_lock);
935 +/* Close system call. */
937 +sys_close (int handle)
939 + struct file_descriptor *fd = lookup_fd (handle);
940 + lock_acquire (&fs_lock);
941 + file_close (fd->file);
942 + lock_release (&fs_lock);
943 + list_remove (&fd->elem);
948 +/* On thread exit, close all open files. */
952 + struct thread *cur = thread_current ();
953 + struct list_elem *e, *next;
955 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
957 + struct file_descriptor *fd;
958 + fd = list_entry (e, struct file_descriptor, elem);
959 + next = list_next (e);
960 + lock_acquire (&fs_lock);
961 + file_close (fd->file);
962 + lock_release (&fs_lock);
966 diff --git a/src/userprog/syscall.h b/src/userprog/syscall.h
967 index 9059096..9d156f0 100644
968 --- a/src/userprog/syscall.h
969 +++ b/src/userprog/syscall.h
971 #define USERPROG_SYSCALL_H
973 void syscall_init (void);
974 +void syscall_exit (void);
976 #endif /* userprog/syscall.h */