From b47bebdaaaf67418bbc936419c11f59e5c7ab605 Mon Sep 17 00:00:00 2001
From: Abinader <abinade2@illinois.edu>
Date: Mon, 30 Apr 2018 14:26:23 -0500
Subject: [PATCH] fixed user program squashing

---
 student-distrib/idt.c      |  8 +++++---
 student-distrib/keyboard.c |  3 +--
 student-distrib/paging.c   |  2 +-
 student-distrib/paging.h   |  2 +-
 student-distrib/syscall.c  |  3 ++-
 student-distrib/terminal.c | 28 +---------------------------
 student-distrib/terminal.h |  2 +-
 student-distrib/types.h    |  2 +-
 8 files changed, 13 insertions(+), 37 deletions(-)

diff --git a/student-distrib/idt.c b/student-distrib/idt.c
index a6de4c2..c3decf2 100644
--- a/student-distrib/idt.c
+++ b/student-distrib/idt.c
@@ -68,7 +68,7 @@ void exception_handler(int interrupt_index)
             printf("GENERAL PROTECTION EXCEPTION\n");
             break;
         case -15:
-            printf("PAGE FAULT EXCEPTION");
+            printf("PAGE FAULT EXCEPTION\n");
             break;
         case -17:
             printf("MATH FAULT EXCEPTION\n");
@@ -84,8 +84,10 @@ void exception_handler(int interrupt_index)
             break;
     }
 
-    cli();
-    while(1); // halt the kernel, since an exception occurs
+    printf("Squashing user level program...\n");
+
+    //return control to shell
+    halt(-1);
 }
 
 /*
diff --git a/student-distrib/keyboard.c b/student-distrib/keyboard.c
index dd10fb2..047361a 100644
--- a/student-distrib/keyboard.c
+++ b/student-distrib/keyboard.c
@@ -373,8 +373,7 @@ void KB_handler(void) {
 
     }
 
-    page_table[0xB8] = ((uint32_t)0xB8000) | USER | READ_WRITE | PRESENT;
-    flush_tlb();
+    remap1();
 
     // 0x15 = scancode for L
     // check if ctrl-L is pressed
diff --git a/student-distrib/paging.c b/student-distrib/paging.c
index 4be459a..cd6ee8e 100644
--- a/student-distrib/paging.c
+++ b/student-distrib/paging.c
@@ -49,7 +49,7 @@ void paging_initi(void){
 
 
 //term_id = current
-void remap1(int32_t term_id){
+void remap1(void){
     page_table[0xB8] = (uint32_t)(0xB8000) | USER | READ_WRITE | PRESENT;
     flush_tlb();
     return;
diff --git a/student-distrib/paging.h b/student-distrib/paging.h
index 8db45b5..8f3ce4b 100644
--- a/student-distrib/paging.h
+++ b/student-distrib/paging.h
@@ -58,7 +58,7 @@ void loadPageDirectory(unsigned int*);
  */
 void enablePaging(void);
 
-extern void remap1(int32_t term_id);
+extern void remap1(void);
 extern void remap2(int32_t term_id);
 
 
diff --git a/student-distrib/syscall.c b/student-distrib/syscall.c
index b1e412d..30b9d18 100644
--- a/student-distrib/syscall.c
+++ b/student-distrib/syscall.c
@@ -102,7 +102,6 @@ int32_t execute(const uint8_t* command)
     uint8_t argv[MAX_ARGUMENT_NUM][MAX_ARGUMENT_SIZE];
     int32_t ret, arg_num; //number that indicated special command
 
-    int32_t pid = set_pid();
     //if( pid == -1) return -1; // reach max process
 
     ret = parse_cmd(command, parsed_cmd, argv);
@@ -141,6 +140,8 @@ int32_t execute(const uint8_t* command)
             return -1;
         }
 
+        int32_t pid = set_pid();
+
         // Get a new pid
         total_prog_count++;
         terminal[running_terminal].terminal_prog_count++;
diff --git a/student-distrib/terminal.c b/student-distrib/terminal.c
index 94bb3e5..6d40071 100644
--- a/student-distrib/terminal.c
+++ b/student-distrib/terminal.c
@@ -218,7 +218,7 @@ int32_t switch_terminal(int32_t term_id)
      visible_terminal = term_id;
 
     //restore paging from 0xb8000 to prev_terminal video mapping
-    remap1(prev_terminal);
+    remap1();
 
     //store previous terminal data into memory
     memcpy( (void*) terminal[prev_terminal].vid_mem, (const void*)TERM_VID, _4_KB );
@@ -234,29 +234,3 @@ int32_t switch_terminal(int32_t term_id)
     //check if shell is running on that terminal
     return ( terminal[term_id].terminal_prog_count == 0) ? 0 : 1;
 }
-
-// NOT IN USE ANYMORE
-/*
- *  FUNCTIONALITY: helper function for copying terminal video memory data
- *
- *  INPUT: term_id -- the terminal id of the ther terminal that we want to switch to
- *         cmd: 0 -- copy from video memory to terminal info storage
- *              1 -- copy from terminal info storage to video memory
- *  OUTPUT: None
- */
-void copy_term_data(int32_t term_id, int32_t cmd)
-{
-    //validate parameters
-    if(cmd != 0 || cmd != 1 || term_id < 0 || term_id > 2)
-    {
-        printf("[copy_term_data] break early - cmd = %d, term_id = %d\n", cmd, term_id);
-        return;
-    }
-
-    if(cmd)
-        memcpy( (void*)TERM_VID, (const void*) terminal[term_id].vid_mem, _4_KB );
-    else
-        memcpy( (void*) terminal[term_id].vid_mem, (const void*)TERM_VID, _4_KB );
-
-    return;
-}
diff --git a/student-distrib/terminal.h b/student-distrib/terminal.h
index 103e1e1..117569c 100644
--- a/student-distrib/terminal.h
+++ b/student-distrib/terminal.h
@@ -16,7 +16,7 @@ extern int32_t terminal_write(int32_t fd, const void* buf, int32_t nbytes);
 extern int32_t terminal_open(const uint8_t* filename);
 extern int32_t terminal_close(int32_t fd);
 
-void remap1(int32_t term_id);
+void remap1(void);
 void remap2(int32_t term_id);
 
 extern int32_t switch_terminal(int32_t term_id);
diff --git a/student-distrib/types.h b/student-distrib/types.h
index 0c9c9dc..110fb31 100644
--- a/student-distrib/types.h
+++ b/student-distrib/types.h
@@ -20,7 +20,7 @@
 #define     MAX_FILE_NUM                8
 #define     MIN_FILE_NUM                0
 #define     MAX_COMMAND_SIZE            10
-#define     MAX_ARGUMENT_NUM            2
+#define     MAX_ARGUMENT_NUM            1
 #define     MAX_ARGUMENT_SIZE           40
 
 //for terminal
-- 
GitLab