---
# System prepended metadata

title: Operating System HW1
tags: [CS342301]

---

# Operating System HW1
###### Nachos MP1: System Call

## :microscope: Trace Code
### 1. SC_Halt
* Machine/mipssims.cc
* 模擬 MIPS P2/3000 processor
```c
Machine:Run()
// 模擬執行 program in thread
// 切換到 UserMode
Machine::OneInstruction()
// 定義執行一個 instruction 所需的參數與步驟
// 例如：一個 byte 大小, rs, rt, rd 等 register
// 判斷 opCode 並執行對應的 instuction
```
* #### Machine/machine.cc
* :bulb: 判斷 host 的 位元組順序 big-endian 或 little-endian
```c
Machine::RaiseException()
// 當 interrupt 或是 exception 發生時，紀錄 program 當前位置
// 設定 User Mode -> Kernel Mode
// 呼叫 Exception Handler
// 設定 Kernel Mode -> User Mode
```
* #### userprog/exception.cc
```c
ExceptionHandler()
// entry point
// 定義 exception 發生時所對應的指令
// syscall (interrupt) 或 exception 發生時會呼叫 (Halt 為其中一種 syscall)
// 後續呼叫 SysHalt()
```
* #### userprog/ksyscall.h
* Kernel interface
```c
SysHalt()
// 連接至 Nachos 的 Halt function
// kernel->interrupt->Halt()
```
* #### machine/interrupt.cc
```c
Interrupt::Halt()
// Show halt 的訊息
// delete kernel <- 這個動作?
```

### 2. SC_Create

* #### userprog/exception.cc
```c
ExceptionHandler()
// Read Register 4
```

* #### userprog/ksyscall.h
```c
SysCreate()
// 呼叫 FileSystem 的 create
```

* #### filesys/filesys.h
```c
FileSystem::Create()
// fileDescriptor
// OpenForWrite
```

### 3. SC_PrintInt

* #### userprog/exception.cc
```c
ExceptionHandler()
```

* #### userprog/ksyscall.h
```c
SysPrintInt()
```

* #### userprog/synchconsole.cc
```c
SynchConsoleOutput::PutInt()
SynchConsoleOutput::PutChar()
```

* #### machine/console.cc
```c
ConsoleOutput::PutChar()
```

* #### machine/interrupt.cc
```c
Interrupt::Schedule()
```

* #### machine/mipssim.cc
```c
Machine::Run()
```

* #### machine/interrupt.cc
```c
Machine::OneTick()
```

* #### machine/interrupt.cc
```c
Interrupt::CheckIfDue()
```

* #### machine/console.cc
```c
ConsoleOutput::CallBack()
```

* #### userprog/synchconsole.cc
```c
SynchConsoleOutput::CallBack()
```

## :wrench: Implementation

### OpenFileId Open(char *name);

### int Write(char *buffer, int size, OpenFileId id);

### int Read(char *buffer, int size, OpenFileId id);

### int Close(OpenFileId id);

###### tags: `CS342301`