5.3 User threads

nachos.userprog.UThread extends KThread with the main difference in how the thread objects run. When the user thread runs on the CPU, the PC register and the stack pointer will be initialized accordingly. The memory address of the command line arguments argc and argv will be stored in two argument registers (See UThread.initRegisters()). Next, the process’ state will be restored after the context switch. If a linear page table is used for memory management (more details see Section 6), an array of translation entries is specified by calling Processor.setPageTable(). The page table provides the mapping between virtual address to physical address. From here on, the processor will fetch the instructions from the memory location indicated by the PC register and execute the user program. Execeptions occur in a number of situations, namely, a system call, an invalid instruction, page faults, TLB miss, etc. A complete list of exceptions can be found in Processor.exceptionNames.

A user thread may be context switched before it finishes. In this case, registers are stored in UThread.userRegisters, which will be restored if the thread is scheduled again. Methods UThread.saveState() and UThread.restoreState() deal with saving and loading states during context switches.