SHARE
TWEET

Untitled

a guest Nov 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "userprog/syscall.h"
  2. #include <stdio.h>
  3. #include <syscall-nr.h>
  4. #include "threads/interrupt.h"
  5. #include "threads/thread.h"
  6.  
  7. /* Added by Adrian Colesa - multithreading */
  8. #include "userprog/process.h"
  9. #include "threads/synch.h"
  10. #include "lib/kernel/console.h"
  11.  
  12. /* Added by Adrian Colesa - multithreading */
  13. static struct semaphore join_sema[TH_NO];
  14. static struct uthread_args process_uthreads_args[TH_NO];
  15. static tid_t process_uthreads_tids[TH_NO];
  16. static int process_uthread_status[TH_NO];
  17.  
  18. static void syscall_handler (struct intr_frame *);
  19.  
  20. void
  21. syscall_init (void)
  22. {
  23.   intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
  24. }
  25.  
  26. /*
  27.  * Added by Adrian Colesa - multithreading
  28.  * This should be normally associated to each process, but since we currently support
  29.  * just one process, declared it global
  30.  */
  31. static crt_thread_id = 0;
  32.  
  33.  
  34. static void
  35. syscall_handler (struct intr_frame *f)
  36. {
  37.     /* Added by Adrian Colesa - multithreading */
  38.     int syscall_no = ((int*)f->esp)[0];
  39.     int fd, no;
  40.     char *buf;
  41.     int waited_tid;
  42.     int *status, th_status, i;
  43.  
  44.     printf ("system call no %d!\n", syscall_no);
  45.  
  46.     /* Added by Adrian Colesa - multithreading */
  47.     switch (syscall_no) {
  48.         case SYS_EXIT:
  49.             printf ("SYS_EXIT system call from thread %d!\n", thread_current()->tid);
  50.             thread_exit();
  51.             return;
  52.         case SYS_WRITE:
  53.             printf ("SYS_WRITE system call from thread %d!\n", thread_current()->tid);
  54.  
  55.             fd = ((int*)f->esp)[1];
  56.             buf = (char*) ((int*)f->esp)[2];
  57.             no = ((int*)f->esp)[3];
  58.  
  59.             if (fd == 1) {
  60.                 putbuf (buf, no);
  61.             }
  62.  
  63.             f->eax = no;
  64.             return;
  65.         case SYS_UTHREAD_CREATE:
  66.             printf ("SYS_UTHREAD_CREATE system call from thread %d!\n", thread_current()->tid);
  67.  
  68.             /* Thread function and its argument prepared for thread creation */
  69.             process_uthreads_args[crt_thread_id].th_fc_addr = ((int*)f->esp)[1];
  70.             process_uthreads_args[crt_thread_id].th_fc_arg = ((int*)f->esp)[2];
  71.  
  72.             /* Thread name is based on the process' one and thread no */
  73.             snprintf(process_uthreads_args[crt_thread_id].th_name,
  74.                     sizeof(process_uthreads_args[crt_thread_id].th_name),
  75.                     "th-%d", crt_thread_id);
  76.  
  77.             /* Thread pid equals that of the current process */
  78.             process_uthreads_args[crt_thread_id].th_pid = thread_current()->pid;
  79.  
  80.             /* Thread pagedir equals that of the current process */
  81.             process_uthreads_args[crt_thread_id].th_pagedir = thread_current()->pagedir;
  82.  
  83.             /* Process internal thread id (no) */
  84.             process_uthreads_args[crt_thread_id].th_no = crt_thread_id;
  85.  
  86.             /* Call the function to create the thread with the prepared arguments */
  87.             process_uthreads_tids[crt_thread_id] = process_uthread_execute(&process_uthreads_args[crt_thread_id]);
  88.  
  89.             /* Check if thread successfully created */
  90.             if (process_uthreads_tids[crt_thread_id] != -1) {
  91.                 /* TO DO: Initialize the corresponding semaphore for possible joiner */
  92.                 // ...
  93.                 sema_init(&join_sema[crt_thread_id], 0);
  94.  
  95.                 f->eax = crt_thread_id;
  96.             } else {
  97.                 f->eax = -1;
  98.             }
  99.  
  100.             crt_thread_id++;
  101.             return;
  102.         case SYS_UTHREAD_JOIN:
  103.             printf ("SYS_UTHREAD_JOIN system call from thread %d!\n", thread_current()->tid);
  104.  
  105.             waited_tid = ((int*)f->esp)[1];
  106.             status = (int*)((int*)f->esp)[2];
  107.  
  108.             if (process_uthreads_tids[waited_tid] != -1) {
  109.                 /* TO DO: Use the corresponding semaphore to wait for the desired thread */
  110.                 // ....
  111.  
  112.                 sema_down(&join_sema[waited_tid]);
  113.  
  114.                 /* TO DO: Return the exit code of the waited thread */
  115.                 // ...
  116.                
  117.                 sema_up(&join_sema[waited_tid]);
  118.                
  119.  
  120.                 /* Return success */
  121.                 f->eax = 0;
  122.                 return process_uthread_status[waited_tid];
  123.             } else
  124.                 /* Return error - non existent thread */
  125.                 f->eax = -1;
  126.  
  127.             return;
  128.         case SYS_UTHREAD_EXIT:
  129.             printf ("SYS_UTHREAD_EXIT system call from thread %d!\n", thread_current()->tid);
  130.             th_status = ((int*)f->esp)[1];
  131.  
  132.             /* TO DO: Save the exit code for a possible joining thread */
  133.             // ...
  134.  
  135.             for(i=0;i<TH_NO;i++)
  136.             {
  137.                 if(process_uthreads_tids[i] == thread_current()->tid)
  138.                 {
  139.                     no = i;
  140.                     break;
  141.                 }
  142.             }
  143.  
  144.             process_uthread_status[i] = th_status;
  145.  
  146.             /* TO DO: use the corresponding semaphore to wake up the possible blocked joining thread */
  147.             // ...
  148.            
  149.             for(i=0;i<TH_NO;i++)
  150.             {
  151.                 if(process_uthreads_tids[i] == thread_current()->tid)
  152.                 {
  153.                     no = i;
  154.                     break;
  155.                 }
  156.             }
  157.  
  158.             sema_up(&join_sema[no]);
  159.  
  160.             // Terminate the thread
  161.             thread_exit();
  162.             // not reached
  163.  
  164.             f->eax=0;
  165.             return;
  166.         case SYS_UTHREAD_GETPID:
  167.             printf ("SYS_UTHREAD_GETPID system call from thread %d!\n", thread_current()->tid);
  168.             for(i=0;i<TH_NO;i++)
  169.             {
  170.                 if(process_uthreads_tids[i] == thread_current()->tid)
  171.                 {
  172.                     no = i;
  173.                     break;
  174.                 }
  175.             }
  176.             f->eax = process_uthreads_args[no].th_pid;
  177.             return;
  178.         case SYS_UTHREAD_GETTID:
  179.             printf ("SYS_UTHREAD_GETTID system call from thread %d!\n", thread_current()->tid);
  180.  
  181.             /* TO DO: return the thread id: this could be tid or the internal thread number */
  182.             // ...
  183.            
  184.             for(i=0;i<TH_NO;i++)
  185.             {
  186.                 if(process_uthreads_tids[i] == thread_current()->tid)
  187.                 {
  188.                     no = i;
  189.                     break;
  190.                 }
  191.             }
  192.  
  193.             f->eax = process_uthreads_args[no].th_pid;
  194.             return;
  195.         }
  196.  
  197.  
  198.     thread_exit ();
  199. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top