1 Index: src/threads/synch.c
2 ===================================================================
3 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/synch.c,v
4 retrieving revision 1.15
5 diff -u -p -r1.15 synch.c
6 --- src/threads/synch.c 31 Dec 2004 21:13:38 -0000 1.15
7 +++ src/threads/synch.c 1 Jan 2005 02:13:41 -0000
8 @@ -330,3 +330,45 @@ cond_name (const struct condition *cond)
13 +/* Initializes LATCH and names it NAME (for debugging purposes).
14 + A latch is a boolean condition. Until it is released for the
15 + first time, all threads block attempting to acquire. After it
16 + is released once, all ongoing and subsequent acquisitions
17 + "fall through" immediately. Releases after the first have no
18 + additional effect. */
20 +latch_init (struct latch *latch, const char *name)
22 + latch->released = false;
23 + lock_init (&latch->monitor_lock, name);
24 + cond_init (&latch->rel_cond, name);
27 +/* Acquires LATCH, blocking until it is released for the first
30 +latch_acquire (struct latch *latch)
32 + lock_acquire (&latch->monitor_lock);
33 + if (!latch->released)
35 + cond_wait (&latch->rel_cond, &latch->monitor_lock);
36 + ASSERT (latch->released);
38 + lock_release (&latch->monitor_lock);
41 +/* Releases LATCH, causing all ongoing and subsequent
42 + acquisitions to pass through immediately. */
44 +latch_release (struct latch *latch)
46 + lock_acquire (&latch->monitor_lock);
47 + if (!latch->released)
49 + latch->released = true;
50 + cond_signal (&latch->rel_cond, &latch->monitor_lock);
52 + lock_release (&latch->monitor_lock);
54 Index: src/threads/synch.h
55 ===================================================================
56 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/synch.h,v
57 retrieving revision 1.7
58 diff -u -p -r1.7 synch.h
59 --- src/threads/synch.h 29 Sep 2004 01:04:09 -0000 1.7
60 +++ src/threads/synch.h 1 Jan 2005 02:13:41 -0000
61 @@ -44,4 +44,16 @@ void cond_signal (struct condition *, st
62 void cond_broadcast (struct condition *, struct lock *);
63 const char *cond_name (const struct condition *);
68 + bool released; /* Released yet? */
69 + struct lock monitor_lock; /* Monitor lock. */
70 + struct condition rel_cond; /* Signaled when released. */
73 +void latch_init (struct latch *, const char *);
74 +void latch_acquire (struct latch *);
75 +void latch_release (struct latch *);
77 #endif /* threads/synch.h */
78 Index: src/threads/thread.c
79 ===================================================================
80 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/thread.c,v
81 retrieving revision 1.48
82 diff -u -p -r1.48 thread.c
83 --- src/threads/thread.c 9 Oct 2004 18:01:37 -0000 1.48
84 +++ src/threads/thread.c 1 Jan 2005 02:13:42 -0000
86 #include "threads/synch.h"
88 #include "userprog/process.h"
89 +#include "userprog/syscall.h"
92 /* Random value for struct thread's `magic' member.
93 @@ -81,6 +82,7 @@ thread_init (void)
94 init_thread (initial_thread, "main", PRI_DEFAULT);
95 initial_thread->status = THREAD_RUNNING;
96 initial_thread->tid = allocate_tid ();
97 + sema_up (&initial_thread->can_die);
100 /* Starts preemptive thread scheduling by enabling interrupts.
101 @@ -149,6 +151,7 @@ thread_create (const char *name, int pri
102 /* Initialize thread. */
103 init_thread (t, name, priority);
104 tid = t->tid = allocate_tid ();
105 + list_push_back (&thread_current ()->children, &t->children_elem);
107 /* Stack frame for kernel_thread(). */
108 kf = alloc_frame (t, sizeof *kf);
109 @@ -241,16 +244,36 @@ thread_tid (void)
113 + struct thread *t = thread_current ();
114 + list_elem *e, *next;
116 ASSERT (!intr_context ());
123 + /* Notify our parent that we're dying. */
124 + latch_release (&t->ready_to_die);
126 + /* Notify our children that they can die. */
127 + for (e = list_begin (&t->children); e != list_end (&t->children);
130 + struct thread *child = list_entry (e, struct thread, children_elem);
131 + next = list_next (e);
133 + sema_up (&child->can_die);
136 + /* Wait until our parent is ready for us to die. */
137 + sema_down (&t->can_die);
139 /* Just set our status to dying and schedule another process.
140 We will be destroyed during the call to schedule_tail(). */
142 - thread_current ()->status = THREAD_DYING;
143 + t->status = THREAD_DYING;
147 @@ -283,8 +290,22 @@ thread_block (void)
148 This function will be implemented in problem 1-2. For now, it
151 -thread_join (tid_t child_tid UNUSED)
154 +thread_join (tid_t child_tid)
156 + struct thread *cur = thread_current ();
159 + for (e = list_begin (&cur->children); e != list_end (&cur->children); )
161 + struct thread *child = list_entry (e, struct thread, children_elem);
163 + if (child->tid == child_tid)
165 + latch_acquire (&child->ready_to_die);
166 + return child->exit_code;
172 /* Sets the current thread's priority to NEW_PRIORITY. */
173 @@ -336,6 +379,12 @@ init_thread (struct thread *t, const cha
174 strlcpy (t->name, name, sizeof t->name);
175 t->stack = (uint8_t *) t + PGSIZE;
176 t->priority = priority;
177 + latch_init (&t->ready_to_die, "ready-to-die");
178 + sema_init (&t->can_die, 0, "can-die");
179 + list_init (&t->children);
181 + list_init (&t->fds);
182 + t->next_handle = 2;
183 t->magic = THREAD_MAGIC;
186 Index: src/threads/thread.h
187 ===================================================================
188 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/thread.h,v
189 retrieving revision 1.28
190 diff -u -p -r1.28 thread.h
191 --- src/threads/thread.h 29 Sep 2004 01:04:20 -0000 1.28
192 +++ src/threads/thread.h 1 Jan 2005 02:13:42 -0000
197 +#include "threads/synch.h"
199 /* States in a thread's life cycle. */
201 @@ -89,12 +90,23 @@ struct thread
202 uint8_t *stack; /* Saved stack pointer. */
203 int priority; /* Priority. */
205 + /* Members for implementing thread_join(). */
206 + struct latch ready_to_die; /* Release when thread about to die. */
207 + struct semaphore can_die; /* Up when thread allowed to die. */
208 + struct list children; /* List of child threads. */
209 + list_elem children_elem; /* Element of `children' list. */
210 + int exit_code; /* Return status. */
212 /* Shared between thread.c and synch.c. */
213 list_elem elem; /* List element. */
216 /* Owned by userprog/process.c. */
217 uint32_t *pagedir; /* Page directory. */
219 + /* Owned by syscall.c. */
220 + struct list fds; /* List of file descriptors. */
221 + int next_handle; /* Next handle value. */
224 /* Owned by thread.c. */
225 @@ -120,7 +132,7 @@ void thread_exit (void) NO_RETURN;
226 void thread_exit (void) NO_RETURN;
227 void thread_yield (void);
229 -void thread_join (tid_t);
230 +int thread_join (tid_t);
232 void thread_set_priority (int);
233 int thread_get_priority (void);
234 Index: src/userprog/exception.c
235 ===================================================================
236 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/userprog/exception.c,v
237 retrieving revision 1.10
238 diff -u -p -r1.10 exception.c
239 --- src/userprog/exception.c 26 Sep 2004 21:15:17 -0000 1.10
240 +++ src/userprog/exception.c 1 Jan 2005 02:13:42 -0000
241 @@ -147,6 +147,14 @@ page_fault (struct intr_frame *f)
242 write = (f->error_code & PF_W) != 0;
243 user = (f->error_code & PF_U) != 0;
245 + /* Handle bad dereferences from system call implementations. */
248 + f->eip = (void (*) (void)) f->eax;
253 /* To implement virtual memory, delete the rest of the function
254 body, and replace it with code that brings in the page to
255 which fault_addr refers. */
256 Index: src/userprog/process.c
257 ===================================================================
258 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/userprog/process.c,v
259 retrieving revision 1.6
260 diff -u -p -r1.6 process.c
261 --- src/userprog/process.c 15 Dec 2004 02:32:02 -0000 1.6
262 +++ src/userprog/process.c 1 Jan 2005 02:13:43 -0000
264 #include "threads/thread.h"
266 static thread_func execute_thread NO_RETURN;
267 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
268 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
270 +/* Data structure shared between process_execute() in the
271 + invoking thread and execute_thread() in the newly invoked
275 + const char *filename; /* Program to load. */
276 + struct semaphore load_done; /* "Up"ed when loading complete. */
277 + bool success; /* True if program successfully loaded. */
280 /* Starts a new thread running a user program loaded from
281 FILENAME. The new thread may be scheduled before
282 @@ -26,31 +36,32 @@ static bool load (const char *cmdline, v
284 process_execute (const char *filename)
287 + struct exec_info exec;
290 - /* Make a copy of FILENAME.
291 - Otherwise there's a race between the caller and load(). */
292 - fn_copy = palloc_get_page (0);
293 - if (fn_copy == NULL)
295 - strlcpy (fn_copy, filename, PGSIZE);
296 + /* Initialize exec_info. */
297 + exec.filename = filename;
298 + sema_init (&exec.load_done, 0, "load done");
300 /* Create a new thread to execute FILENAME. */
301 - tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy);
302 - if (tid == TID_ERROR)
303 - palloc_free_page (fn_copy);
304 + tid = thread_create (filename, PRI_DEFAULT, execute_thread, &exec);
305 + if (tid != TID_ERROR)
307 + sema_down (&exec.load_done);
314 /* A thread function that loads a user process and starts it
317 -execute_thread (void *filename_)
318 +execute_thread (void *exec_)
320 - char *filename = filename_;
321 + struct exec_info *exec = exec_;
322 struct intr_frame if_;
325 /* Initialize interrupt frame and load executable. */
326 memset (&if_, 0, sizeof if_);
327 @@ -59,11 +69,9 @@ execute_thread (void *filename_)
329 if_.eflags = FLAG_IF | FLAG_MBS;
331 - success = load (filename, &if_.eip, &if_.esp);
333 - /* If load failed, quit. */
334 - palloc_free_page (filename);
335 + success = exec->success = load (exec->filename, &if_.eip, &if_.esp);
336 + sema_up (&exec->load_done);
340 /* Switch page tables. */
341 @@ -89,6 +97,8 @@ process_exit (void)
342 struct thread *cur = thread_current ();
345 + printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
347 /* Destroy the current process's page directory and switch back
348 to the kernel-only page directory. We have to set
349 cur->pagedir to NULL before switching page directories, or a
350 @@ -182,7 +192,7 @@ struct Elf32_Phdr
351 #define PF_R 4 /* Readable. */
353 static bool load_segment (struct file *, const struct Elf32_Phdr *);
354 -static bool setup_stack (void **esp);
355 +static bool setup_stack (const char *cmd_line, void **esp);
357 /* Aborts loading an executable, with an error message. */
358 #define LOAD_ERROR(MSG) \
359 @@ -198,13 +208,15 @@ static bool setup_stack (void **esp);
360 and its initial stack pointer into *ESP.
361 Returns true if successful, false otherwise. */
363 -load (const char *filename, void (**eip) (void), void **esp)
364 +load (const char *cmd_line, void (**eip) (void), void **esp)
366 struct thread *t = thread_current ();
367 + char filename[NAME_MAX + 2];
368 struct Elf32_Ehdr ehdr;
369 struct file *file = NULL;
371 bool success = false;
375 /* Allocate page directory. */
376 @@ -212,6 +224,14 @@ load (const char *filename, void (**eip)
377 if (t->pagedir == NULL)
378 LOAD_ERROR (("page directory allocation failed"));
380 + /* Extract filename from command line. */
381 + while (*cmd_line == ' ')
383 + strlcpy (filename, cmd_line, sizeof filename);
384 + cp = strchr (filename, ' ');
388 /* Open executable file. */
389 file = filesys_open (filename);
391 @@ -272,7 +292,7 @@ load (const char *filename, void (**eip)
395 - if (!setup_stack (esp))
396 + if (!setup_stack (cmd_line, esp))
400 @@ -381,10 +401,92 @@ load_segment (struct file *file, const s
404 -/* Create a minimal stack by mapping a zeroed page at the top of
405 - user virtual memory. */
406 +/* Reverse the order of the ARGC pointers to char in ARGV. */
408 +reverse (int argc, char **argv)
410 + for (; argc > 1; argc -= 2, argv++)
412 + char *tmp = argv[0];
413 + argv[0] = argv[argc - 1];
414 + argv[argc - 1] = tmp;
418 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
419 + page-relative stack pointer is *OFS, and then adjusts *OFS
420 + appropriately. The bytes pushed are rounded to a 32-bit
423 + If successful, returns a pointer to the newly pushed object.
424 + On failure, returns a null pointer. */
426 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size)
428 + size_t padsize = ROUND_UP (size, sizeof (uint32_t));
429 + if (*ofs < padsize)
433 + memcpy (kpage + *ofs + (padsize - size), buf, size);
434 + return kpage + *ofs + (padsize - size);
437 +/* Sets up command line arguments in KPAGE, which will be mapped
438 + to UPAGE in user space. The command line arguments are taken
439 + from CMD_LINE, separated by spaces. Sets *ESP to the initial
440 + stack pointer for the process. */
442 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
445 + size_t ofs = PGSIZE;
446 + char *const null = NULL;
447 + char *cmd_line_copy;
448 + char *karg, *saveptr;
452 + /* Push command line string. */
453 + cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
454 + if (cmd_line_copy == NULL)
457 + if (push (kpage, &ofs, &null, sizeof null) == NULL)
460 + /* Parse command line into arguments
461 + and push them in reverse order. */
463 + for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
464 + karg = strtok_r (NULL, " ", &saveptr))
466 + char *uarg = upage + (karg - (char *) kpage);
467 + if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
472 + /* Reverse the order of the command line arguments. */
473 + argv = (char **) (upage + ofs);
474 + reverse (argc, (char **) (kpage + ofs));
476 + /* Push argv, argc, "return address". */
477 + if (push (kpage, &ofs, &argv, sizeof argv) == NULL
478 + || push (kpage, &ofs, &argc, sizeof argc) == NULL
479 + || push (kpage, &ofs, &null, sizeof null) == NULL)
482 + /* Set initial stack pointer. */
483 + *esp = upage + ofs;
487 +/* Create a minimal stack for T by mapping a page at the
488 + top of user virtual memory. Fills in the page using CMD_LINE
489 + and sets *ESP to the stack pointer. */
491 -setup_stack (void **esp)
492 +setup_stack (const char *cmd_line, void **esp)
495 bool success = false;
496 @@ -392,9 +494,9 @@ setup_stack (void **esp)
497 kpage = palloc_get_page (PAL_USER | PAL_ZERO);
500 - success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
503 + uint8_t *upage = ((uint8_t *) PHYS_BASE) - PGSIZE;
504 + if (install_page (upage, kpage))
505 + success = init_cmd_line (kpage, upage, cmd_line, esp);
507 palloc_free_page (kpage);
509 Index: src/userprog/syscall.c
510 ===================================================================
511 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/userprog/syscall.c,v
512 retrieving revision 1.4
513 diff -u -p -r1.4 syscall.c
514 --- src/userprog/syscall.c 26 Sep 2004 21:15:17 -0000 1.4
515 +++ src/userprog/syscall.c 1 Jan 2005 02:13:43 -0000
517 #include "userprog/syscall.h"
520 #include <syscall-nr.h>
521 +#include "userprog/process.h"
522 +#include "userprog/pagedir.h"
523 +#include "devices/kbd.h"
524 +#include "filesys/filesys.h"
525 +#include "filesys/file.h"
526 +#include "threads/init.h"
527 #include "threads/interrupt.h"
528 +#include "threads/malloc.h"
529 +#include "threads/mmu.h"
530 +#include "threads/palloc.h"
531 #include "threads/thread.h"
535 +static int sys_halt (void);
536 +static int sys_exit (int status);
537 +static int sys_exec (const char *ufile);
538 +static int sys_join (tid_t);
539 +static int sys_create (const char *ufile, unsigned initial_size);
540 +static int sys_remove (const char *ufile);
541 +static int sys_open (const char *ufile);
542 +static int sys_filesize (int handle);
543 +static int sys_read (int handle, void *udst_, unsigned size);
544 +static int sys_write (int handle, void *usrc_, unsigned size);
545 +static int sys_seek (int handle, unsigned position);
546 +static int sys_tell (int handle);
547 +static int sys_close (int handle);
549 static void syscall_handler (struct intr_frame *);
551 +static void copy_in (void *, const void *, size_t);
553 +static struct lock fs_lock;
558 intr_register (0x30, 3, INTR_ON, syscall_handler, "syscall");
559 + lock_init (&fs_lock, "fs");
562 +/* System call handler. */
564 +syscall_handler (struct intr_frame *f)
566 + typedef int syscall_function (int, int, int);
568 + /* A system call. */
571 + size_t arg_cnt; /* Number of arguments. */
572 + syscall_function *func; /* Implementation. */
575 + /* Table of system calls. */
576 + static const struct syscall syscall_table[] =
578 + {0, (syscall_function *) sys_halt},
579 + {1, (syscall_function *) sys_exit},
580 + {1, (syscall_function *) sys_exec},
581 + {1, (syscall_function *) sys_join},
582 + {2, (syscall_function *) sys_create},
583 + {1, (syscall_function *) sys_remove},
584 + {1, (syscall_function *) sys_open},
585 + {1, (syscall_function *) sys_filesize},
586 + {3, (syscall_function *) sys_read},
587 + {3, (syscall_function *) sys_write},
588 + {2, (syscall_function *) sys_seek},
589 + {1, (syscall_function *) sys_tell},
590 + {1, (syscall_function *) sys_close},
593 + struct syscall *sc;
597 + /* Get the system call. */
598 + copy_in (&call_nr, f->esp, sizeof call_nr);
599 + if (call_nr < 0 || call_nr >= sizeof syscall_table / sizeof *syscall_table)
601 + sc = syscall_table + call_nr;
603 + /* Get the system call arguments. */
604 + ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
605 + memset (args, 0, sizeof args);
606 + copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
608 + /* Execute the system call,
609 + and set the return value. */
610 + f->eax = sc->func (args[0], args[1], args[2]);
613 +/* Returns true if UADDR is a valid, mapped user address,
614 + false otherwise. */
616 +verify_user (const void *uaddr)
618 + return pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL;
621 +/* Copies a byte from user address USRC to kernel address DST.
622 + USRC must be below PHYS_BASE.
623 + Returns true if successful, false if a segfault occurred. */
625 +get_user (uint8_t *dst, const uint8_t *usrc)
628 + asm ("mov %%eax, offset 1f; mov %%al, %2; mov %0, %%al; 1:"
629 + : "=m" (*dst), "=&a" (eax) : "m" (*usrc));
633 +/* Writes BYTE to user address UDST.
634 + UDST must be below PHYS_BASE.
635 + Returns true if successful, false if a segfault occurred. */
637 +put_user (uint8_t *udst, uint8_t byte)
640 + asm ("mov %%eax, offset 1f; mov %0, %b2; 1:"
641 + : "=m" (*udst), "=&a" (eax) : "r" (byte));
645 +/* Copies SIZE bytes from user address USRC to kernel address
647 + Call thread_exit() if any of the user accesses are invalid. */
649 -syscall_handler (struct intr_frame *f UNUSED)
650 +copy_in (void *dst_, const void *usrc_, size_t size)
652 + uint8_t *dst = dst_;
653 + const uint8_t *usrc = usrc_;
655 + for (; size > 0; size--, dst++, usrc++)
656 + if (usrc >= (uint8_t *) PHYS_BASE || !get_user (dst, usrc))
660 +/* Creates a copy of user string US in kernel memory
661 + and returns it as a page that must be freed with
662 + palloc_free_page().
663 + Truncates the string at PGSIZE bytes in size.
664 + Call thread_exit() if any of the user accesses are invalid. */
666 +copy_in_string (const char *us)
671 + ks = palloc_get_page (0);
675 + for (length = 0; length < PGSIZE; length++)
677 + if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++))
680 + if (ks[length] == '\0')
683 + ks[PGSIZE - 1] = '\0';
687 +/* Halt system call. */
694 +/* Exit system call. */
696 +sys_exit (int exit_code)
698 + thread_current ()->exit_code = exit_code;
703 +/* Exec system call. */
705 +sys_exec (const char *ufile)
708 + char *kfile = copy_in_string (ufile);
710 + lock_acquire (&fs_lock);
711 + tid = process_execute (kfile);
712 + lock_release (&fs_lock);
714 + palloc_free_page (kfile);
719 +/* Join system call. */
721 +sys_join (tid_t child)
723 + return thread_join (child);
726 +/* Create system call. */
728 +sys_create (const char *ufile, unsigned initial_size)
730 + char *kfile = copy_in_string (ufile);
733 + lock_acquire (&fs_lock);
734 + ok = filesys_create (kfile, initial_size);
735 + lock_release (&fs_lock);
737 + palloc_free_page (kfile);
742 +/* Remove system call. */
744 +sys_remove (const char *ufile)
746 - printf ("system call!\n");
747 + char *kfile = copy_in_string (ufile);
750 + lock_acquire (&fs_lock);
751 + ok = filesys_remove (kfile);
752 + lock_release (&fs_lock);
754 + palloc_free_page (kfile);
759 +/* A file descriptor, for binding a file handle to a file. */
760 +struct file_descriptor
762 + list_elem elem; /* List element. */
763 + struct file *file; /* File. */
764 + int handle; /* File handle. */
767 +/* Open system call. */
769 +sys_open (const char *ufile)
771 + char *kfile = copy_in_string (ufile);
772 + struct file_descriptor *fd;
775 + fd = malloc (sizeof *fd);
778 + lock_acquire (&fs_lock);
779 + fd->file = filesys_open (kfile);
780 + if (fd->file != NULL)
782 + struct thread *cur = thread_current ();
783 + handle = fd->handle = cur->next_handle++;
784 + list_push_front (&cur->fds, &fd->elem);
788 + lock_release (&fs_lock);
791 + palloc_free_page (kfile);
795 +/* Returns the file descriptor associated with the given handle.
796 + Terminates the process if HANDLE is not associated with an
798 +static struct file_descriptor *
799 +lookup_fd (int handle)
801 + struct thread *cur = thread_current ();
804 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
807 + struct file_descriptor *fd;
808 + fd = list_entry (e, struct file_descriptor, elem);
809 + if (fd->handle == handle)
816 +/* Filesize system call. */
818 +sys_filesize (int handle)
820 + struct file_descriptor *fd = lookup_fd (handle);
823 + lock_acquire (&fs_lock);
824 + size = file_length (fd->file);
825 + lock_release (&fs_lock);
830 +/* Read system call. */
832 +sys_read (int handle, void *udst_, unsigned size)
834 + uint8_t *udst = udst_;
835 + struct file_descriptor *fd;
836 + int bytes_read = 0;
838 + /* Handle keyboard reads. */
839 + if (handle == STDIN_FILENO)
841 + for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++)
842 + if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, kbd_getc ()))
847 + /* Handle all other reads. */
848 + fd = lookup_fd (handle);
849 + lock_acquire (&fs_lock);
852 + /* How much to read into this page? */
853 + size_t page_left = PGSIZE - pg_ofs (udst);
854 + size_t read_amt = size < page_left ? size : page_left;
857 + /* Check that touching this page is okay. */
858 + if (!verify_user (udst))
860 + lock_release (&fs_lock);
864 + /* Read from file into page. */
865 + retval = file_read (fd->file, udst, read_amt);
868 + if (bytes_read == 0)
872 + bytes_read += retval;
874 + /* If it was a short read we're done. */
875 + if (retval != (off_t) read_amt)
882 + lock_release (&fs_lock);
887 +/* Write system call. */
889 +sys_write (int handle, void *usrc_, unsigned size)
891 + uint8_t *usrc = usrc_;
892 + struct file_descriptor *fd = NULL;
893 + int bytes_written = 0;
895 + /* Lookup up file descriptor. */
896 + if (handle != STDOUT_FILENO)
897 + fd = lookup_fd (handle);
899 + lock_acquire (&fs_lock);
902 + /* How much bytes to write to this page? */
903 + size_t page_left = PGSIZE - pg_ofs (usrc);
904 + size_t write_amt = size < page_left ? size : page_left;
907 + /* Check that we can touch this user page. */
908 + if (!verify_user (usrc))
910 + lock_release (&fs_lock);
914 + /* Do the write. */
915 + if (handle == STDOUT_FILENO)
917 + putbuf (usrc, write_amt);
918 + retval = write_amt;
921 + retval = file_write (fd->file, usrc, write_amt);
924 + if (bytes_written == 0)
925 + bytes_written = -1;
928 + bytes_written += retval;
930 + /* If it was a short write we're done. */
931 + if (retval != (off_t) write_amt)
938 + lock_release (&fs_lock);
940 + return bytes_written;
943 +/* Seek system call. */
945 +sys_seek (int handle, unsigned position)
947 + struct file_descriptor *fd = lookup_fd (handle);
949 + lock_acquire (&fs_lock);
950 + file_seek (fd->file, position);
951 + lock_release (&fs_lock);
956 +/* Tell system call. */
958 +sys_tell (int handle)
960 + struct file_descriptor *fd = lookup_fd (handle);
963 + lock_acquire (&fs_lock);
964 + position = file_tell (fd->file);
965 + lock_release (&fs_lock);
970 +/* Close system call. */
972 +sys_close (int handle)
974 + struct file_descriptor *fd = lookup_fd (handle);
975 + lock_acquire (&fs_lock);
976 + file_close (fd->file);
977 + lock_release (&fs_lock);
978 + list_remove (&fd->elem);
983 +/* On thread exit, close all open files. */
987 + struct thread *cur = thread_current ();
988 + list_elem *e, *next;
990 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
992 + struct file_descriptor *fd;
993 + fd = list_entry (e, struct file_descriptor, elem);
994 + next = list_next (e);
995 + file_close (fd->file);
999 Index: src/userprog/syscall.h
1000 ===================================================================
1001 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/userprog/syscall.h,v
1002 retrieving revision 1.2
1003 diff -u -p -r1.2 syscall.h
1004 --- src/userprog/syscall.h 6 Sep 2004 05:38:45 -0000 1.2
1005 +++ src/userprog/syscall.h 1 Jan 2005 02:13:43 -0000
1007 #define USERPROG_SYSCALL_H
1009 void syscall_init (void);
1010 +void syscall_exit (void);
1012 #endif /* userprog/syscall.h */