1 diff -u src/threads/thread.c~ src/threads/thread.c
2 --- src/threads/thread.c~ 2005-06-02 14:35:12.000000000 -0700
3 +++ src/threads/thread.c 2005-06-08 13:45:28.000000000 -0700
5 #include "threads/synch.h"
7 #include "userprog/process.h"
8 +#include "userprog/syscall.h"
11 /* Random value for struct thread's `magic' member.
12 @@ -251,16 +252,19 @@ thread_tid (void)
16 + struct thread *t = thread_current ();
18 ASSERT (!intr_context ());
26 /* Just set our status to dying and schedule another process.
27 We will be destroyed during the call to schedule_tail(). */
29 - thread_current ()->status = THREAD_DYING;
30 + t->status = THREAD_DYING;
34 @@ -400,6 +404,11 @@ init_thread (struct thread *t, const cha
35 strlcpy (t->name, name, sizeof t->name);
36 t->stack = (uint8_t *) t + PGSIZE;
37 t->priority = priority;
38 + list_init (&t->children);
40 + t->wait_status = NULL;
41 + list_init (&t->fds);
43 t->magic = THREAD_MAGIC;
46 diff -u src/threads/thread.h~ src/threads/thread.h
47 --- src/threads/thread.h~ 2005-06-02 14:32:36.000000000 -0700
48 +++ src/threads/thread.h 2005-06-08 13:47:09.000000000 -0700
53 +#include "threads/synch.h"
55 /* States in a thread's life cycle. */
57 @@ -89,6 +90,11 @@ struct thread
58 uint8_t *stack; /* Saved stack pointer. */
59 int priority; /* Priority. */
61 + /* Owned by process.c. */
62 + int exit_code; /* Exit code. */
63 + struct wait_status *wait_status; /* This process's completion status. */
64 + struct list children; /* Completion status of children. */
66 /* Shared between thread.c and synch.c. */
67 struct list_elem elem; /* List element. */
69 @@ -96,11 +102,31 @@ struct thread
70 /* Owned by userprog/process.c. */
71 uint32_t *pagedir; /* Page directory. */
73 + struct file *bin_file; /* Executable. */
75 + /* Owned by syscall.c. */
76 + struct list fds; /* List of file descriptors. */
77 + int next_handle; /* Next handle value. */
79 /* Owned by thread.c. */
80 unsigned magic; /* Detects stack overflow. */
83 +/* Tracks the completion of a process.
84 + Reference held by both the parent, in its `children' list,
85 + and by the child, in its `wait_status' pointer. */
88 + struct list_elem elem; /* `children' list element. */
89 + struct lock lock; /* Protects ref_cnt. */
90 + int ref_cnt; /* 2=child and parent both alive,
91 + 1=either child or parent alive,
92 + 0=child and parent both dead. */
93 + tid_t tid; /* Child thread id. */
94 + int exit_code; /* Child exit code, if dead. */
95 + struct semaphore dead; /* 1=child alive, 0=child dead. */
98 void thread_init (void);
99 void thread_start (void);
100 void thread_tick (void);
101 diff -u src/userprog/exception.c~ src/userprog/exception.c
102 --- src/userprog/exception.c~ 2005-01-01 18:09:59.000000000 -0800
103 +++ src/userprog/exception.c 2005-06-08 13:45:28.000000000 -0700
104 @@ -150,6 +150,14 @@ page_fault (struct intr_frame *f)
105 write = (f->error_code & PF_W) != 0;
106 user = (f->error_code & PF_U) != 0;
108 + /* Handle bad dereferences from system call implementations. */
111 + f->eip = (void (*) (void)) f->eax;
116 /* To implement virtual memory, delete the rest of the function
117 body, and replace it with code that brings in the page to
118 which fault_addr refers. */
119 diff -u src/userprog/process.c~ src/userprog/process.c
120 --- src/userprog/process.c~ 2005-05-26 13:19:48.000000000 -0700
121 +++ src/userprog/process.c 2005-06-08 13:49:13.000000000 -0700
123 #include "threads/init.h"
124 #include "threads/interrupt.h"
125 +#include "threads/malloc.h"
126 #include "threads/palloc.h"
127 #include "threads/thread.h"
128 #include "threads/vaddr.h"
130 static thread_func execute_thread NO_RETURN;
131 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
132 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
134 +/* Data structure shared between process_execute() in the
135 + invoking thread and execute_thread() in the newly invoked
139 + const char *filename; /* Program to load. */
140 + struct semaphore load_done; /* "Up"ed when loading complete. */
141 + struct wait_status *wait_status; /* Child process. */
142 + bool success; /* Program successfully loaded? */
145 /* Starts a new thread running a user program loaded from
146 FILENAME. The new thread may be scheduled (and may even exit)
147 @@ -27,29 +39,37 @@ static bool load (const char *cmdline, v
149 process_execute (const char *filename)
152 + struct exec_info exec;
153 + char thread_name[16];
157 - /* Make a copy of FILENAME.
158 - Otherwise there's a race between the caller and load(). */
159 - fn_copy = palloc_get_page (0);
160 - if (fn_copy == NULL)
162 - strlcpy (fn_copy, filename, PGSIZE);
163 + /* Initialize exec_info. */
164 + exec.filename = filename;
165 + sema_init (&exec.load_done, 0);
167 /* Create a new thread to execute FILENAME. */
168 - tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy);
169 - if (tid == TID_ERROR)
170 - palloc_free_page (fn_copy);
171 + strlcpy (thread_name, filename, sizeof thread_name);
172 + strtok_r (thread_name, " ", &save_ptr);
173 + tid = thread_create (thread_name, PRI_DEFAULT, execute_thread, &exec);
174 + if (tid != TID_ERROR)
176 + sema_down (&exec.load_done);
178 + list_push_back (&thread_current ()->children, &exec.wait_status->elem);
186 /* A thread function that loads a user process and starts it
189 -execute_thread (void *filename_)
190 +execute_thread (void *exec_)
192 - char *filename = filename_;
193 + struct exec_info *exec = exec_;
194 struct intr_frame if_;
197 @@ -58,10 +78,28 @@ execute_thread (void *filename_)
198 if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
200 if_.eflags = FLAG_IF | FLAG_MBS;
201 - success = load (filename, &if_.eip, &if_.esp);
202 + success = load (exec->filename, &if_.eip, &if_.esp);
204 + /* Allocate wait_status. */
207 + exec->wait_status = thread_current ()->wait_status
208 + = malloc (sizeof *exec->wait_status);
209 + success = exec->wait_status != NULL;
212 - /* If load failed, quit. */
213 - palloc_free_page (filename);
214 + /* Initialize wait_status. */
217 + lock_init (&exec->wait_status->lock);
218 + exec->wait_status->ref_cnt = 2;
219 + exec->wait_status->tid = thread_current ()->tid;
220 + sema_init (&exec->wait_status->dead, 0);
223 + /* Notify parent thread and clean up. */
224 + exec->success = success;
225 + sema_up (&exec->load_done);
229 @@ -75,18 +113,47 @@ execute_thread (void *filename_)
233 +/* Releases one reference to CS and, if it is now unreferenced,
236 +release_child (struct wait_status *cs)
240 + lock_acquire (&cs->lock);
241 + new_ref_cnt = --cs->ref_cnt;
242 + lock_release (&cs->lock);
244 + if (new_ref_cnt == 0)
248 /* Waits for thread TID to die and returns its exit status. If
249 it was terminated by the kernel (i.e. killed due to an
250 exception), returns -1. If TID is invalid or if it was not a
251 child of the calling process, or if process_wait() has already
252 been successfully called for the given TID, returns -1
253 - immediately, without waiting.
255 - This function will be implemented in problem 2-2. For now, it
257 + immediately, without waiting. */
259 -process_wait (tid_t child_tid UNUSED)
260 +process_wait (tid_t child_tid)
262 + struct thread *cur = thread_current ();
263 + struct list_elem *e;
265 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
268 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
269 + if (cs->tid == child_tid)
273 + sema_down (&cs->dead);
274 + exit_code = cs->exit_code;
275 + release_child (cs);
282 @@ -95,8 +162,32 @@ void
285 struct thread *cur = thread_current ();
286 + struct list_elem *e, *next;
289 + printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
291 + /* Close executable (and allow writes). */
292 + file_close (cur->bin_file);
294 + /* Notify parent that we're dead. */
295 + if (cur->wait_status != NULL)
297 + struct wait_status *cs = cur->wait_status;
298 + cs->exit_code = cur->exit_code;
299 + sema_up (&cs->dead);
300 + release_child (cs);
303 + /* Free entries of children list. */
304 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
307 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
308 + next = list_remove (e);
309 + release_child (cs);
312 /* Destroy the current process's page directory and switch back
313 to the kernel-only page directory. */
315 @@ -193,7 +284,7 @@ struct Elf32_Phdr
316 #define PF_W 2 /* Writable. */
317 #define PF_R 4 /* Readable. */
319 -static bool setup_stack (void **esp);
320 +static bool setup_stack (const char *cmd_line, void **esp);
321 static bool validate_segment (const struct Elf32_Phdr *, struct file *);
322 static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
324 @@ -209,13 +300,15 @@ static bool setup_stack (void **esp);
325 and its initial stack pointer into *ESP.
326 Returns true if successful, false otherwise. */
328 -load (const char *filename, void (**eip) (void), void **esp)
329 +load (const char *cmd_line, void (**eip) (void), void **esp)
331 struct thread *t = thread_current ();
332 + char filename[NAME_MAX + 2];
333 struct Elf32_Ehdr ehdr;
334 struct file *file = NULL;
336 bool success = false;
340 /* Allocate and activate page directory. */
341 @@ -224,13 +317,22 @@ load (const char *filename, void (**eip)
345 + /* Extract filename from command line. */
346 + while (*cmd_line == ' ')
348 + strlcpy (filename, cmd_line, sizeof filename);
349 + cp = strchr (filename, ' ');
353 /* Open executable file. */
354 - file = filesys_open (filename);
355 + t->bin_file = file = filesys_open (filename);
358 printf ("load: %s: open failed\n", filename);
361 + file_deny_write (file);
363 /* Read and verify executable header. */
364 if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
365 @@ -284,7 +386,7 @@ load (const char *filename, void (**eip)
369 - if (!setup_stack (esp))
370 + if (!setup_stack (cmd_line, esp))
374 @@ -294,7 +396,6 @@ load (const char *filename, void (**eip)
377 /* We arrive here whether the load is successful or not. */
382 @@ -393,10 +494,92 @@ load_segment (struct file *file, const s
386 -/* Create a minimal stack by mapping a zeroed page at the top of
387 - user virtual memory. */
388 +/* Reverse the order of the ARGC pointers to char in ARGV. */
390 +reverse (int argc, char **argv)
392 + for (; argc > 1; argc -= 2, argv++)
394 + char *tmp = argv[0];
395 + argv[0] = argv[argc - 1];
396 + argv[argc - 1] = tmp;
400 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
401 + page-relative stack pointer is *OFS, and then adjusts *OFS
402 + appropriately. The bytes pushed are rounded to a 32-bit
405 + If successful, returns a pointer to the newly pushed object.
406 + On failure, returns a null pointer. */
408 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size)
410 + size_t padsize = ROUND_UP (size, sizeof (uint32_t));
411 + if (*ofs < padsize)
415 + memcpy (kpage + *ofs + (padsize - size), buf, size);
416 + return kpage + *ofs + (padsize - size);
419 +/* Sets up command line arguments in KPAGE, which will be mapped
420 + to UPAGE in user space. The command line arguments are taken
421 + from CMD_LINE, separated by spaces. Sets *ESP to the initial
422 + stack pointer for the process. */
424 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
427 + size_t ofs = PGSIZE;
428 + char *const null = NULL;
429 + char *cmd_line_copy;
430 + char *karg, *saveptr;
434 + /* Push command line string. */
435 + cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
436 + if (cmd_line_copy == NULL)
439 + if (push (kpage, &ofs, &null, sizeof null) == NULL)
442 + /* Parse command line into arguments
443 + and push them in reverse order. */
445 + for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
446 + karg = strtok_r (NULL, " ", &saveptr))
448 + void *uarg = upage + (karg - (char *) kpage);
449 + if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
454 + /* Reverse the order of the command line arguments. */
455 + argv = (char **) (upage + ofs);
456 + reverse (argc, (char **) (kpage + ofs));
458 + /* Push argv, argc, "return address". */
459 + if (push (kpage, &ofs, &argv, sizeof argv) == NULL
460 + || push (kpage, &ofs, &argc, sizeof argc) == NULL
461 + || push (kpage, &ofs, &null, sizeof null) == NULL)
464 + /* Set initial stack pointer. */
465 + *esp = upage + ofs;
469 +/* Create a minimal stack for T by mapping a page at the
470 + top of user virtual memory. Fills in the page using CMD_LINE
471 + and sets *ESP to the stack pointer. */
473 -setup_stack (void **esp)
474 +setup_stack (const char *cmd_line, void **esp)
477 bool success = false;
478 @@ -404,9 +587,9 @@ setup_stack (void **esp)
479 kpage = palloc_get_page (PAL_USER | PAL_ZERO);
482 - success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
485 + uint8_t *upage = ((uint8_t *) PHYS_BASE) - PGSIZE;
486 + if (install_page (upage, kpage, true))
487 + success = init_cmd_line (kpage, upage, cmd_line, esp);
489 palloc_free_page (kpage);
491 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
492 --- src/userprog/syscall.c~ 2004-09-26 14:15:17.000000000 -0700
493 +++ src/userprog/syscall.c 2005-06-08 13:45:28.000000000 -0700
495 #include "userprog/syscall.h"
498 #include <syscall-nr.h>
499 +#include "userprog/process.h"
500 +#include "userprog/pagedir.h"
501 +#include "devices/kbd.h"
502 +#include "filesys/filesys.h"
503 +#include "filesys/file.h"
504 +#include "threads/init.h"
505 #include "threads/interrupt.h"
506 +#include "threads/malloc.h"
507 +#include "threads/palloc.h"
508 #include "threads/thread.h"
509 +#include "threads/vaddr.h"
513 +static int sys_halt (void);
514 +static int sys_exit (int status);
515 +static int sys_exec (const char *ufile);
516 +static int sys_wait (tid_t);
517 +static int sys_create (const char *ufile, unsigned initial_size);
518 +static int sys_remove (const char *ufile);
519 +static int sys_open (const char *ufile);
520 +static int sys_filesize (int handle);
521 +static int sys_read (int handle, void *udst_, unsigned size);
522 +static int sys_write (int handle, void *usrc_, unsigned size);
523 +static int sys_seek (int handle, unsigned position);
524 +static int sys_tell (int handle);
525 +static int sys_close (int handle);
527 static void syscall_handler (struct intr_frame *);
529 +static void copy_in (void *, const void *, size_t);
531 +static struct lock fs_lock;
536 intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
537 + lock_init (&fs_lock);
540 +/* System call handler. */
542 +syscall_handler (struct intr_frame *f)
544 + typedef int syscall_function (int, int, int);
546 + /* A system call. */
549 + size_t arg_cnt; /* Number of arguments. */
550 + syscall_function *func; /* Implementation. */
553 + /* Table of system calls. */
554 + static const struct syscall syscall_table[] =
556 + {0, (syscall_function *) sys_halt},
557 + {1, (syscall_function *) sys_exit},
558 + {1, (syscall_function *) sys_exec},
559 + {1, (syscall_function *) sys_wait},
560 + {2, (syscall_function *) sys_create},
561 + {1, (syscall_function *) sys_remove},
562 + {1, (syscall_function *) sys_open},
563 + {1, (syscall_function *) sys_filesize},
564 + {3, (syscall_function *) sys_read},
565 + {3, (syscall_function *) sys_write},
566 + {2, (syscall_function *) sys_seek},
567 + {1, (syscall_function *) sys_tell},
568 + {1, (syscall_function *) sys_close},
571 + const struct syscall *sc;
575 + /* Get the system call. */
576 + copy_in (&call_nr, f->esp, sizeof call_nr);
577 + if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
579 + sc = syscall_table + call_nr;
581 + /* Get the system call arguments. */
582 + ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
583 + memset (args, 0, sizeof args);
584 + copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
586 + /* Execute the system call,
587 + and set the return value. */
588 + f->eax = sc->func (args[0], args[1], args[2]);
591 +/* Returns true if UADDR is a valid, mapped user address,
592 + false otherwise. */
594 +verify_user (const void *uaddr)
596 + return (uaddr < PHYS_BASE
597 + && pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL);
600 +/* Copies a byte from user address USRC to kernel address DST.
601 + USRC must be below PHYS_BASE.
602 + Returns true if successful, false if a segfault occurred. */
604 +get_user (uint8_t *dst, const uint8_t *usrc)
607 + asm ("movl $1f, %%eax; movb %2, %%al; movb %%al, %0; 1:"
608 + : "=m" (*dst), "=&a" (eax) : "m" (*usrc));
612 +/* Writes BYTE to user address UDST.
613 + UDST must be below PHYS_BASE.
614 + Returns true if successful, false if a segfault occurred. */
616 +put_user (uint8_t *udst, uint8_t byte)
619 + asm ("movl $1f, %%eax; movb %b2, %0; 1:"
620 + : "=m" (*udst), "=&a" (eax) : "r" (byte));
624 +/* Copies SIZE bytes from user address USRC to kernel address
626 + Call thread_exit() if any of the user accesses are invalid. */
628 -syscall_handler (struct intr_frame *f UNUSED)
629 +copy_in (void *dst_, const void *usrc_, size_t size)
631 + uint8_t *dst = dst_;
632 + const uint8_t *usrc = usrc_;
634 + for (; size > 0; size--, dst++, usrc++)
635 + if (usrc >= (uint8_t *) PHYS_BASE || !get_user (dst, usrc))
639 +/* Creates a copy of user string US in kernel memory
640 + and returns it as a page that must be freed with
641 + palloc_free_page().
642 + Truncates the string at PGSIZE bytes in size.
643 + Call thread_exit() if any of the user accesses are invalid. */
645 +copy_in_string (const char *us)
650 + ks = palloc_get_page (0);
654 + for (length = 0; length < PGSIZE; length++)
656 + if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++))
659 + if (ks[length] == '\0')
662 + ks[PGSIZE - 1] = '\0';
666 +/* Halt system call. */
673 +/* Exit system call. */
675 +sys_exit (int exit_code)
677 + thread_current ()->exit_code = exit_code;
682 +/* Exec system call. */
684 +sys_exec (const char *ufile)
687 + char *kfile = copy_in_string (ufile);
689 + lock_acquire (&fs_lock);
690 + tid = process_execute (kfile);
691 + lock_release (&fs_lock);
693 + palloc_free_page (kfile);
698 +/* Wait system call. */
700 +sys_wait (tid_t child)
702 + return process_wait (child);
705 +/* Create system call. */
707 +sys_create (const char *ufile, unsigned initial_size)
709 + char *kfile = copy_in_string (ufile);
712 + lock_acquire (&fs_lock);
713 + ok = filesys_create (kfile, initial_size);
714 + lock_release (&fs_lock);
716 + palloc_free_page (kfile);
721 +/* Remove system call. */
723 +sys_remove (const char *ufile)
725 - printf ("system call!\n");
726 + char *kfile = copy_in_string (ufile);
729 + lock_acquire (&fs_lock);
730 + ok = filesys_remove (kfile);
731 + lock_release (&fs_lock);
733 + palloc_free_page (kfile);
738 +/* A file descriptor, for binding a file handle to a file. */
739 +struct file_descriptor
741 + struct list_elem elem; /* List element. */
742 + struct file *file; /* File. */
743 + int handle; /* File handle. */
746 +/* Open system call. */
748 +sys_open (const char *ufile)
750 + char *kfile = copy_in_string (ufile);
751 + struct file_descriptor *fd;
754 + fd = malloc (sizeof *fd);
757 + lock_acquire (&fs_lock);
758 + fd->file = filesys_open (kfile);
759 + if (fd->file != NULL)
761 + struct thread *cur = thread_current ();
762 + handle = fd->handle = cur->next_handle++;
763 + list_push_front (&cur->fds, &fd->elem);
767 + lock_release (&fs_lock);
770 + palloc_free_page (kfile);
774 +/* Returns the file descriptor associated with the given handle.
775 + Terminates the process if HANDLE is not associated with an
777 +static struct file_descriptor *
778 +lookup_fd (int handle)
780 + struct thread *cur = thread_current ();
781 + struct list_elem *e;
783 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
786 + struct file_descriptor *fd;
787 + fd = list_entry (e, struct file_descriptor, elem);
788 + if (fd->handle == handle)
795 +/* Filesize system call. */
797 +sys_filesize (int handle)
799 + struct file_descriptor *fd = lookup_fd (handle);
802 + lock_acquire (&fs_lock);
803 + size = file_length (fd->file);
804 + lock_release (&fs_lock);
809 +/* Read system call. */
811 +sys_read (int handle, void *udst_, unsigned size)
813 + uint8_t *udst = udst_;
814 + struct file_descriptor *fd;
815 + int bytes_read = 0;
817 + /* Handle keyboard reads. */
818 + if (handle == STDIN_FILENO)
820 + for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++)
821 + if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, kbd_getc ()))
826 + /* Handle all other reads. */
827 + fd = lookup_fd (handle);
828 + lock_acquire (&fs_lock);
831 + /* How much to read into this page? */
832 + size_t page_left = PGSIZE - pg_ofs (udst);
833 + size_t read_amt = size < page_left ? size : page_left;
836 + /* Check that touching this page is okay. */
837 + if (!verify_user (udst))
839 + lock_release (&fs_lock);
843 + /* Read from file into page. */
844 + retval = file_read (fd->file, udst, read_amt);
847 + if (bytes_read == 0)
851 + bytes_read += retval;
853 + /* If it was a short read we're done. */
854 + if (retval != (off_t) read_amt)
861 + lock_release (&fs_lock);
866 +/* Write system call. */
868 +sys_write (int handle, void *usrc_, unsigned size)
870 + uint8_t *usrc = usrc_;
871 + struct file_descriptor *fd = NULL;
872 + int bytes_written = 0;
874 + /* Lookup up file descriptor. */
875 + if (handle != STDOUT_FILENO)
876 + fd = lookup_fd (handle);
878 + lock_acquire (&fs_lock);
881 + /* How much bytes to write to this page? */
882 + size_t page_left = PGSIZE - pg_ofs (usrc);
883 + size_t write_amt = size < page_left ? size : page_left;
886 + /* Check that we can touch this user page. */
887 + if (!verify_user (usrc))
889 + lock_release (&fs_lock);
893 + /* Do the write. */
894 + if (handle == STDOUT_FILENO)
896 + putbuf (usrc, write_amt);
897 + retval = write_amt;
900 + retval = file_write (fd->file, usrc, write_amt);
903 + if (bytes_written == 0)
904 + bytes_written = -1;
907 + bytes_written += retval;
909 + /* If it was a short write we're done. */
910 + if (retval != (off_t) write_amt)
917 + lock_release (&fs_lock);
919 + return bytes_written;
922 +/* Seek system call. */
924 +sys_seek (int handle, unsigned position)
926 + struct file_descriptor *fd = lookup_fd (handle);
928 + lock_acquire (&fs_lock);
929 + if ((off_t) position >= 0)
930 + file_seek (fd->file, position);
931 + lock_release (&fs_lock);
936 +/* Tell system call. */
938 +sys_tell (int handle)
940 + struct file_descriptor *fd = lookup_fd (handle);
943 + lock_acquire (&fs_lock);
944 + position = file_tell (fd->file);
945 + lock_release (&fs_lock);
950 +/* Close system call. */
952 +sys_close (int handle)
954 + struct file_descriptor *fd = lookup_fd (handle);
955 + lock_acquire (&fs_lock);
956 + file_close (fd->file);
957 + lock_release (&fs_lock);
958 + list_remove (&fd->elem);
963 +/* On thread exit, close all open files. */
967 + struct thread *cur = thread_current ();
968 + struct list_elem *e, *next;
970 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
972 + struct file_descriptor *fd;
973 + fd = list_entry (e, struct file_descriptor, elem);
974 + next = list_next (e);
975 + lock_acquire (&fs_lock);
976 + file_close (fd->file);
977 + lock_release (&fs_lock);
981 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
982 --- src/userprog/syscall.h~ 2004-09-05 22:38:45.000000000 -0700
983 +++ src/userprog/syscall.h 2005-06-08 13:45:28.000000000 -0700
985 #define USERPROG_SYSCALL_H
987 void syscall_init (void);
988 +void syscall_exit (void);
990 #endif /* userprog/syscall.h */