├── userprog ├── read.txt ├── write.txt ├── list.o ├── main.o ├── nachos ├── temp1 ├── temp2 ├── bitmap.o ├── stats.o ├── switch.o ├── synch.o ├── sysdep.o ├── system.o ├── temp_mem ├── thread.o ├── timer.o ├── addrspace.o ├── console.o ├── elevator.o ├── exception.o ├── interrupt.o ├── machine.o ├── mipssim.o ├── progtest.o ├── scheduler.o ├── synchlist.o ├── translate.o ├── utility.o ├── threadtest.o ├── elevatortest.o ├── swtch.s ├── addrspace.h ├── bitmap.h ├── progtest.cc ├── syscall.h └── bitmap.cc ├── filesys ├── test │ ├── small │ ├── medium │ └── big ├── DISK ├── disk.o ├── list.o ├── main.o ├── nachos ├── stats.o ├── synch.o ├── timer.o ├── bitmap.o ├── console.o ├── filehdr.o ├── filesys.o ├── fstest.o ├── machine.o ├── mipssim.o ├── switch.o ├── sysdep.o ├── system.o ├── thread.o ├── utility.o ├── addrspace.o ├── directory.o ├── elevator.o ├── exception.o ├── interrupt.o ├── openfile.o ├── progtest.o ├── scheduler.o ├── synchdisk.o ├── synchlist.o ├── threadtest.o ├── translate.o ├── elevatortest.o ├── file1 ├── swtch.s ├── synchdisk.h ├── filehdr.h ├── directory.h ├── openfile.h └── filesys.h ├── bin ├── halt ├── coff2noff ├── coff2noff.o ├── instr.h ├── noff.h ├── Makefile ├── int.h ├── opstrings.c ├── encode.h ├── coff.h ├── system.c ├── coff2flat.c ├── main.c ├── disasm.c └── d.c ├── test ├── halt ├── shell ├── sort ├── halt.o ├── matmult ├── shell.o ├── sort.o ├── start.o ├── halt.coff ├── matmult.o ├── shell.coff ├── sort.coff ├── matmult.coff ├── script ├── halt.c ├── matmult.c ├── sort.c ├── shell.c ├── Makefile ├── start.c └── start.s ├── vm ├── list.o ├── main.o ├── nachos ├── stats.o ├── synch.o ├── timer.o ├── bitmap.o ├── console.o ├── machine.o ├── mipssim.o ├── switch.o ├── sysdep.o ├── system.o ├── thread.o ├── utility.o ├── addrspace.o ├── elevator.o ├── exception.o ├── interrupt.o ├── progtest.o ├── scheduler.o ├── synchlist.o ├── threadtest.o ├── translate.o ├── elevatortest.o └── swtch.s ├── README.md ├── network ├── disk.o ├── list.o ├── main.o ├── nachos ├── post.o ├── stats.o ├── synch.o ├── timer.o ├── bitmap.o ├── console.o ├── filehdr.o ├── filesys.o ├── fstest.o ├── machine.o ├── mipssim.o ├── nettest.o ├── network.o ├── switch.o ├── sysdep.o ├── system.o ├── thread.o ├── utility.o ├── addrspace.o ├── directory.o ├── elevator.o ├── exception.o ├── interrupt.o ├── openfile.o ├── progtest.o ├── scheduler.o ├── synchdisk.o ├── synchlist.o ├── threadtest.o ├── translate.o ├── elevatortest.o ├── swtch.s ├── README └── nettest.cc ├── threads ├── list.o ├── main.o ├── nachos ├── stats.o ├── synch.o ├── timer.o ├── switch.o ├── sysdep.o ├── system.o ├── thread.o ├── utility.o ├── elevator.o ├── interrupt.o ├── scheduler.o ├── synchlist.o ├── threadtest.o ├── elevatortest.o ├── bool.h ├── copyright.h ├── scheduler.h ├── out4 ├── out3 ├── synchlist.h ├── swtch.s ├── system.h ├── utility.cc ├── list.h ├── synchlist.cc ├── utility.h ├── switch-old.s ├── out1 ├── out2 ├── switch.h └── synch.h ├── machine ├── elevatortest.h ├── stats.cc ├── timer.h ├── translate.h ├── stats.h ├── sysdep.h ├── timer.cc ├── console.h ├── disk.h ├── network.h ├── network.cc └── interrupt.h ├── Makefile.dep ├── COPYRIGHT ├── Makefile └── Makefile.common /userprog/read.txt: -------------------------------------------------------------------------------- 1 | myread 2 | -------------------------------------------------------------------------------- /userprog/write.txt: -------------------------------------------------------------------------------- 1 | myread 2 | -------------------------------------------------------------------------------- /filesys/test/small: -------------------------------------------------------------------------------- 1 | This is the spring of our discontent. 2 | -------------------------------------------------------------------------------- /bin/halt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/bin/halt -------------------------------------------------------------------------------- /test/halt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/halt -------------------------------------------------------------------------------- /test/shell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/shell -------------------------------------------------------------------------------- /test/sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/sort -------------------------------------------------------------------------------- /vm/list.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/list.o -------------------------------------------------------------------------------- /vm/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/main.o -------------------------------------------------------------------------------- /vm/nachos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/nachos -------------------------------------------------------------------------------- /vm/stats.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/stats.o -------------------------------------------------------------------------------- /vm/synch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/synch.o -------------------------------------------------------------------------------- /vm/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/timer.o -------------------------------------------------------------------------------- /filesys/DISK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/DISK -------------------------------------------------------------------------------- /test/halt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/halt.o -------------------------------------------------------------------------------- /test/matmult: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/matmult -------------------------------------------------------------------------------- /test/shell.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/shell.o -------------------------------------------------------------------------------- /test/sort.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/sort.o -------------------------------------------------------------------------------- /test/start.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/start.o -------------------------------------------------------------------------------- /vm/bitmap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/bitmap.o -------------------------------------------------------------------------------- /vm/console.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/console.o -------------------------------------------------------------------------------- /vm/machine.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/machine.o -------------------------------------------------------------------------------- /vm/mipssim.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/mipssim.o -------------------------------------------------------------------------------- /vm/switch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/switch.o -------------------------------------------------------------------------------- /vm/sysdep.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/sysdep.o -------------------------------------------------------------------------------- /vm/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/system.o -------------------------------------------------------------------------------- /vm/thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/thread.o -------------------------------------------------------------------------------- /vm/utility.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/utility.o -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nachos-project 2 | pku nachos lab 3 | 4 | now the lab 7 is compeleted 5 | -------------------------------------------------------------------------------- /bin/coff2noff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/bin/coff2noff -------------------------------------------------------------------------------- /bin/coff2noff.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/bin/coff2noff.o -------------------------------------------------------------------------------- /filesys/disk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/disk.o -------------------------------------------------------------------------------- /filesys/list.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/list.o -------------------------------------------------------------------------------- /filesys/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/main.o -------------------------------------------------------------------------------- /filesys/nachos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/nachos -------------------------------------------------------------------------------- /filesys/stats.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/stats.o -------------------------------------------------------------------------------- /filesys/synch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/synch.o -------------------------------------------------------------------------------- /filesys/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/timer.o -------------------------------------------------------------------------------- /network/disk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/disk.o -------------------------------------------------------------------------------- /network/list.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/list.o -------------------------------------------------------------------------------- /network/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/main.o -------------------------------------------------------------------------------- /network/nachos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/nachos -------------------------------------------------------------------------------- /network/post.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/post.o -------------------------------------------------------------------------------- /network/stats.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/stats.o -------------------------------------------------------------------------------- /network/synch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/synch.o -------------------------------------------------------------------------------- /network/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/timer.o -------------------------------------------------------------------------------- /test/halt.coff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/halt.coff -------------------------------------------------------------------------------- /test/matmult.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/matmult.o -------------------------------------------------------------------------------- /test/shell.coff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/shell.coff -------------------------------------------------------------------------------- /test/sort.coff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/sort.coff -------------------------------------------------------------------------------- /threads/list.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/list.o -------------------------------------------------------------------------------- /threads/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/main.o -------------------------------------------------------------------------------- /threads/nachos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/nachos -------------------------------------------------------------------------------- /threads/stats.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/stats.o -------------------------------------------------------------------------------- /threads/synch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/synch.o -------------------------------------------------------------------------------- /threads/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/timer.o -------------------------------------------------------------------------------- /userprog/list.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/list.o -------------------------------------------------------------------------------- /userprog/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/main.o -------------------------------------------------------------------------------- /userprog/nachos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/nachos -------------------------------------------------------------------------------- /userprog/temp1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/temp1 -------------------------------------------------------------------------------- /userprog/temp2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/temp2 -------------------------------------------------------------------------------- /vm/addrspace.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/addrspace.o -------------------------------------------------------------------------------- /vm/elevator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/elevator.o -------------------------------------------------------------------------------- /vm/exception.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/exception.o -------------------------------------------------------------------------------- /vm/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/interrupt.o -------------------------------------------------------------------------------- /vm/progtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/progtest.o -------------------------------------------------------------------------------- /vm/scheduler.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/scheduler.o -------------------------------------------------------------------------------- /vm/synchlist.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/synchlist.o -------------------------------------------------------------------------------- /vm/threadtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/threadtest.o -------------------------------------------------------------------------------- /vm/translate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/translate.o -------------------------------------------------------------------------------- /filesys/bitmap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/bitmap.o -------------------------------------------------------------------------------- /filesys/console.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/console.o -------------------------------------------------------------------------------- /filesys/filehdr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/filehdr.o -------------------------------------------------------------------------------- /filesys/filesys.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/filesys.o -------------------------------------------------------------------------------- /filesys/fstest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/fstest.o -------------------------------------------------------------------------------- /filesys/machine.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/machine.o -------------------------------------------------------------------------------- /filesys/mipssim.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/mipssim.o -------------------------------------------------------------------------------- /filesys/switch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/switch.o -------------------------------------------------------------------------------- /filesys/sysdep.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/sysdep.o -------------------------------------------------------------------------------- /filesys/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/system.o -------------------------------------------------------------------------------- /filesys/thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/thread.o -------------------------------------------------------------------------------- /filesys/utility.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/utility.o -------------------------------------------------------------------------------- /network/bitmap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/bitmap.o -------------------------------------------------------------------------------- /network/console.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/console.o -------------------------------------------------------------------------------- /network/filehdr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/filehdr.o -------------------------------------------------------------------------------- /network/filesys.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/filesys.o -------------------------------------------------------------------------------- /network/fstest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/fstest.o -------------------------------------------------------------------------------- /network/machine.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/machine.o -------------------------------------------------------------------------------- /network/mipssim.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/mipssim.o -------------------------------------------------------------------------------- /network/nettest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/nettest.o -------------------------------------------------------------------------------- /network/network.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/network.o -------------------------------------------------------------------------------- /network/switch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/switch.o -------------------------------------------------------------------------------- /network/sysdep.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/sysdep.o -------------------------------------------------------------------------------- /network/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/system.o -------------------------------------------------------------------------------- /network/thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/thread.o -------------------------------------------------------------------------------- /network/utility.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/utility.o -------------------------------------------------------------------------------- /test/matmult.coff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/test/matmult.coff -------------------------------------------------------------------------------- /threads/switch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/switch.o -------------------------------------------------------------------------------- /threads/sysdep.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/sysdep.o -------------------------------------------------------------------------------- /threads/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/system.o -------------------------------------------------------------------------------- /threads/thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/thread.o -------------------------------------------------------------------------------- /threads/utility.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/utility.o -------------------------------------------------------------------------------- /userprog/bitmap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/bitmap.o -------------------------------------------------------------------------------- /userprog/stats.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/stats.o -------------------------------------------------------------------------------- /userprog/switch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/switch.o -------------------------------------------------------------------------------- /userprog/synch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/synch.o -------------------------------------------------------------------------------- /userprog/sysdep.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/sysdep.o -------------------------------------------------------------------------------- /userprog/system.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/system.o -------------------------------------------------------------------------------- /userprog/temp_mem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/temp_mem -------------------------------------------------------------------------------- /userprog/thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/thread.o -------------------------------------------------------------------------------- /userprog/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/timer.o -------------------------------------------------------------------------------- /vm/elevatortest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/vm/elevatortest.o -------------------------------------------------------------------------------- /filesys/addrspace.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/addrspace.o -------------------------------------------------------------------------------- /filesys/directory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/directory.o -------------------------------------------------------------------------------- /filesys/elevator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/elevator.o -------------------------------------------------------------------------------- /filesys/exception.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/exception.o -------------------------------------------------------------------------------- /filesys/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/interrupt.o -------------------------------------------------------------------------------- /filesys/openfile.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/openfile.o -------------------------------------------------------------------------------- /filesys/progtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/progtest.o -------------------------------------------------------------------------------- /filesys/scheduler.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/scheduler.o -------------------------------------------------------------------------------- /filesys/synchdisk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/synchdisk.o -------------------------------------------------------------------------------- /filesys/synchlist.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/synchlist.o -------------------------------------------------------------------------------- /filesys/threadtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/threadtest.o -------------------------------------------------------------------------------- /filesys/translate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/translate.o -------------------------------------------------------------------------------- /network/addrspace.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/addrspace.o -------------------------------------------------------------------------------- /network/directory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/directory.o -------------------------------------------------------------------------------- /network/elevator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/elevator.o -------------------------------------------------------------------------------- /network/exception.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/exception.o -------------------------------------------------------------------------------- /network/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/interrupt.o -------------------------------------------------------------------------------- /network/openfile.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/openfile.o -------------------------------------------------------------------------------- /network/progtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/progtest.o -------------------------------------------------------------------------------- /network/scheduler.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/scheduler.o -------------------------------------------------------------------------------- /network/synchdisk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/synchdisk.o -------------------------------------------------------------------------------- /network/synchlist.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/synchlist.o -------------------------------------------------------------------------------- /network/threadtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/threadtest.o -------------------------------------------------------------------------------- /network/translate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/translate.o -------------------------------------------------------------------------------- /threads/elevator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/elevator.o -------------------------------------------------------------------------------- /threads/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/interrupt.o -------------------------------------------------------------------------------- /threads/scheduler.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/scheduler.o -------------------------------------------------------------------------------- /threads/synchlist.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/synchlist.o -------------------------------------------------------------------------------- /threads/threadtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/threadtest.o -------------------------------------------------------------------------------- /userprog/addrspace.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/addrspace.o -------------------------------------------------------------------------------- /userprog/console.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/console.o -------------------------------------------------------------------------------- /userprog/elevator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/elevator.o -------------------------------------------------------------------------------- /userprog/exception.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/exception.o -------------------------------------------------------------------------------- /userprog/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/interrupt.o -------------------------------------------------------------------------------- /userprog/machine.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/machine.o -------------------------------------------------------------------------------- /userprog/mipssim.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/mipssim.o -------------------------------------------------------------------------------- /userprog/progtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/progtest.o -------------------------------------------------------------------------------- /userprog/scheduler.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/scheduler.o -------------------------------------------------------------------------------- /userprog/synchlist.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/synchlist.o -------------------------------------------------------------------------------- /userprog/translate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/translate.o -------------------------------------------------------------------------------- /userprog/utility.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/utility.o -------------------------------------------------------------------------------- /filesys/elevatortest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/filesys/elevatortest.o -------------------------------------------------------------------------------- /filesys/file1: -------------------------------------------------------------------------------- 1 | ddddddddddddddddd 2 | ddddddddddddddd 3 | ddddddddddddd 4 | d 5 | ddddddd 6 | dd 7 | 8 | -------------------------------------------------------------------------------- /network/elevatortest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/network/elevatortest.o -------------------------------------------------------------------------------- /threads/elevatortest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/threads/elevatortest.o -------------------------------------------------------------------------------- /userprog/threadtest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/threadtest.o -------------------------------------------------------------------------------- /userprog/elevatortest.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MakiSzz/Nachos-project/HEAD/userprog/elevatortest.o -------------------------------------------------------------------------------- /threads/bool.h: -------------------------------------------------------------------------------- 1 | #ifndef __NACHBOOL_H__ 2 | #define __NACHBOOL_H__ 1 3 | 4 | #define FALSE 0 5 | #define TRUE 1 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /filesys/test/medium: -------------------------------------------------------------------------------- 1 | This is the spring of our discontent. 2 | This is the spring of our discontent. 3 | This is the spring of our discontent. 4 | This is the spring of our discontent. 5 | -------------------------------------------------------------------------------- /machine/elevatortest.h: -------------------------------------------------------------------------------- 1 | // elevatortest.h 2 | // Defines self test module for elevator device. 3 | // 4 | // Copyright (c) 1996 The Regents of the University of California. 5 | // All rights reserved. See copyright.h for copyright notice and limitation 6 | // of liability and disclaimer of warranty provisions. 7 | 8 | #ifndef ELEVATORTEST_H 9 | #define ELEVATORTEST_H 10 | 11 | #include "copyright.h" 12 | 13 | extern void ElevatorSelfTest(); 14 | 15 | #endif ELEVATORTEST_H -------------------------------------------------------------------------------- /test/script: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("ecoff-littlemips") 2 | ENTRY(__start) 3 | SECTIONS 4 | { 5 | .text 0 : { 6 | _ftext = . ; 7 | *(.init) 8 | eprol = .; 9 | *(.text) 10 | *(.fini) 11 | etext = .; 12 | _etext = .; 13 | } 14 | .rdata . : { 15 | *(.rdata) 16 | } 17 | _fdata = .; 18 | .data . : { 19 | *(.data) 20 | CONSTRUCTORS 21 | } 22 | edata = .; 23 | _edata = .; 24 | _fbss = .; 25 | .sbss . : { 26 | *(.sbss) 27 | *(.scommon) 28 | } 29 | .bss . : { 30 | *(.bss) 31 | *(COMMON) 32 | } 33 | end = .; 34 | _end = .; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /filesys/test/big: -------------------------------------------------------------------------------- 1 | This is the spring of our discontent. 2 | This is the spring of our discontent. 3 | This is the spring of our discontent. 4 | This is the spring of our discontent. 5 | This is the spring of our discontent. 6 | This is the spring of our discontent. 7 | This is the spring of our discontent. 8 | This is the spring of our discontent. 9 | This is the spring of our discontent. 10 | This is the spring of our discontent. 11 | This is the spring of our discontent. 12 | This is the spring of our discontent. 13 | This is the spring of our discontent. 14 | This is the spring of our discontent. 15 | This is the spring of our discontent. 16 | This is the spring of our discontent. 17 | -------------------------------------------------------------------------------- /bin/instr.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | 9 | /* Instruction formats */ 10 | 11 | #define rd(i) (((i) >> 11) & 0x1f) 12 | #define rt(i) (((i) >> 16) & 0x1f) 13 | #define rs(i) (((i) >> 21) & 0x1f) 14 | #define shamt(i) (((i) >> 6) & 0x1f) 15 | #define immed(i) (((i) & 0x8000) ? (i)|(-0x8000) : (i)&0x7fff) 16 | 17 | #define off26(i) (((i)&((1<<26)-1))<<2) 18 | #define top4(i) (((i)&(~((1<<28)-1)))) 19 | #define off16(i) (immed(i)<<2) 20 | 21 | #define extend(i, hibitmask) (((i)&(hibitmask)) ? ((i)|(-(hibitmask))) : (i)) 22 | -------------------------------------------------------------------------------- /Makefile.dep: -------------------------------------------------------------------------------- 1 | # This is part of a GNU-Makefile, to specify system-dependent 2 | # parts of the Makefile enviroment. 3 | # 4 | # This gets included as part of the GNU-Makefile used in each of 5 | # the subdirectories. 6 | # 7 | # Depending on your platform, you need to select the correct definition. 8 | # Also, you need to edit the Makefile in the bin subdirectory. 9 | 10 | # DEC MIPS, Ultrix 11 | # HOST = -DHOST_MIPS 12 | 13 | # SUN SPARC, Sun 4.xx 14 | # HOST = -DHOST_SPARC -DHOST_IS_BIG_ENDIAN 15 | # LDFLAGS = -lsocket -lnls 16 | 17 | # HP PA-RISC, HP_UX 18 | # HOST = -DHOST_SNAKE -DHOST_IS_BIG_ENDIAN 19 | 20 | # 386, 386BSD Unix, or NetBSD Unix (available via anon ftp 21 | # from agate.berkeley.edu) 22 | # also, Linux 23 | HOST = -DHOST_i386 24 | LDFLAGS = 25 | 26 | # slight variant for 386 FreeBSD 27 | # HOST = -DHOST_i386 -DFreeBSD 28 | # CPP=/usr/bin/cpp 29 | -------------------------------------------------------------------------------- /test/halt.c: -------------------------------------------------------------------------------- 1 | /* halt.c 2 | * Simple program to test whether running a user program works. 3 | * 4 | * Just do a "syscall" that shuts down the OS. 5 | * 6 | * NOTE: for some reason, user programs with global data structures 7 | * sometimes haven't worked in the Nachos environment. So be careful 8 | * out there! One option is to allocate data structures as 9 | * automatics within a procedure, but if you do this, you have to 10 | * be careful to allocate a big enough stack to hold the automatics! 11 | */ 12 | 13 | /* 14 | int fd1,fd2; 15 | int result; 16 | char buffer[20]; 17 | Create("write.txt"); 18 | fd1= Open("read.txt"); 19 | fd2 = Open("write.txt"); 20 | result = Read(buffer,20,fd1); 21 | Write(buffer,result,fd2); 22 | Close(fd1); 23 | Close(fd2); 24 | */ 25 | #include "syscall.h" 26 | 27 | int 28 | main() 29 | { 30 | // Yield(); 31 | Exit(0); 32 | } 33 | -------------------------------------------------------------------------------- /bin/noff.h: -------------------------------------------------------------------------------- 1 | /* noff.h 2 | * Data structures defining the Nachos Object Code Format 3 | * 4 | * Basically, we only know about three types of segments: 5 | * code (read-only), initialized data, and unitialized data 6 | */ 7 | 8 | #define NOFFMAGIC 0xbadfad /* magic number denoting Nachos 9 | * object code file 10 | */ 11 | 12 | typedef struct segment { 13 | int virtualAddr; /* location of segment in virt addr space */ 14 | int inFileAddr; /* location of segment in this file */ 15 | int size; /* size of segment */ 16 | } Segment; 17 | 18 | typedef struct noffHeader { 19 | int noffMagic; /* should be NOFFMAGIC */ 20 | Segment code; /* executable code segment */ 21 | Segment initData; /* initialized data segment */ 22 | Segment uninitData; /* uninitialized data segment -- 23 | * should be zero'ed before use 24 | */ 25 | } NoffHeader; 26 | -------------------------------------------------------------------------------- /test/matmult.c: -------------------------------------------------------------------------------- 1 | /* matmult.c 2 | * Test program to do matrix multiplication on large arrays. 3 | * 4 | * Intended to stress virtual memory system. 5 | * 6 | * Ideally, we could read the matrices off of the file system, 7 | * and store the result back to the file system! 8 | */ 9 | 10 | #include "syscall.h" 11 | 12 | #define Dim 16 /* sum total of the arrays doesn't fit in 13 | * physical memory 14 | */ 15 | 16 | int A[Dim][Dim]; 17 | int B[Dim][Dim]; 18 | int C[Dim][Dim]; 19 | 20 | int 21 | main() 22 | { 23 | int i, j, k; 24 | 25 | for (i = 0; i < Dim; i++) /* first initialize the matrices */ 26 | for (j = 0; j < Dim; j++) { 27 | A[i][j] = i; 28 | B[i][j] = j; 29 | C[i][j] = 0; 30 | } 31 | 32 | for (i = 0; i < Dim; i++) /* then multiply them together */ 33 | for (j = 0; j < Dim; j++) 34 | for (k = 0; k < Dim; k++) 35 | C[i][j] += A[i][k] * B[k][j]; 36 | Exit(0); 37 | // Exit(C[Dim-1][Dim-1]); /* and then we're done */ 38 | } 39 | -------------------------------------------------------------------------------- /test/sort.c: -------------------------------------------------------------------------------- 1 | /* sort.c 2 | * Test program to sort a large number of integers. 3 | * 4 | * Intention is to stress virtual memory system. 5 | * 6 | * Ideally, we could read the unsorted array off of the file system, 7 | * and store the result back to the file system! 8 | */ 9 | 10 | #include "syscall.h" 11 | 12 | //int A[1024]; /* size of physical memory; with code, we'll run out of space!*/ 13 | int A[100]; 14 | /* 15 | void 16 | func(){ 17 | 18 | Create("test1.txt"); 19 | } 20 | */ 21 | int 22 | main() 23 | { 24 | 25 | // Exit(0); 26 | // Create("test2.txt"); 27 | // Fork(func); 28 | int i, j, tmp; 29 | // int id = Exec("../test/halt"); 30 | // Join(id); 31 | 32 | 33 | for (i = 0; i < 100; i++) 34 | A[i] = 100 - i; 35 | 36 | 37 | 38 | for (i = 0; i < 100; i++) 39 | for (j = 0; j < (100 - i); j++) 40 | if (A[j] > A[j + 1]) { 41 | tmp = A[j]; 42 | A[j] = A[j + 1]; 43 | A[j + 1] = tmp; 44 | } 45 | Exit(A[0]); 46 | } 47 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 1992-1993 The Regents of the University of California. 2 | All rights reserved. 3 | 4 | Permission to use, copy, modify, and distribute this software and its 5 | documentation for any purpose, without fee, and without written agreement is 6 | hereby granted, provided that the above copyright notice and the following 7 | two paragraphs appear in all copies of this software. 8 | 9 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR 10 | DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 11 | OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF 12 | CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | 14 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 15 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 16 | AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 17 | ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO 18 | PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 19 | -------------------------------------------------------------------------------- /bin/Makefile: -------------------------------------------------------------------------------- 1 | # Use normal make for this Makefile 2 | # 3 | # Makefile for: 4 | # coff2noff -- converts a normal MIPS executable into a Nachos executable 5 | # disassemble -- disassembles a normal MIPS executable 6 | # 7 | # Copyright (c) 1992 The Regents of the University of California. 8 | # All rights reserved. See copyright.h for copyright notice and limitation 9 | # of liability and disclaimer of warranty provisions. 10 | 11 | # If the host is big endian (SPARC, SNAKE, etc): 12 | # change to (disassemble and coff2flat don't support big endian yet): 13 | 14 | CC=gcc 15 | # Sparc/Solaris 16 | # CFLAGS= -I./ -I../threads -DHOST_IS_BIG_ENDIAN 17 | # Linux 18 | CFLAGS=-I./ -I../threads -g 19 | 20 | LD=gcc 21 | 22 | all: coff2noff 23 | 24 | # converts a COFF file to Nachos object format 25 | coff2noff: coff2noff.o 26 | $(LD) coff2noff.o -o coff2noff 27 | 28 | # converts a COFF file to a flat address space (for Nachos version 2) 29 | coff2flat: coff2flat.o 30 | $(LD) coff2flat.o -o coff2flat 31 | 32 | # dis-assembles a COFF file 33 | disassemble: out.o opstrings.o 34 | $(LD) out.o opstrings.o -o disassemble 35 | -------------------------------------------------------------------------------- /bin/int.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Copyright (c) 1992-1993 The Regents of the University of California. 4 | All rights reserved. See copyright.h for copyright notice and limitation 5 | of liability and disclaimer of warranty provisions. 6 | */ 7 | 8 | #include "copyright.h" 9 | 10 | 11 | #define MEMSIZE (1<<24) 12 | #define memoffset 0x10000000 13 | 14 | /* centralized memory-access primitives */ 15 | #define amark(x) x 16 | #define imark(x) x 17 | 18 | #define ifetch(addr) (*(int *)(int) (&(mem-memoffset)[imark(addr)])) 19 | #define fetch(addr) (*(int *)(int) (&(mem-memoffset)[amark(addr)])) 20 | #define sfetch(addr) (*(short *)(int) (&(mem-memoffset)[amark(addr)])) 21 | #define usfetch(addr) (*(unsigned short *)(int)(&(mem-memoffset)[amark(addr)])) 22 | #define cfetch(addr) (*(char *)(int) (&(mem-memoffset)[amark(addr)])) 23 | #define ucfetch(addr) (*(unsigned char *)(int)(&(mem-memoffset)[amark(addr)])) 24 | 25 | #define store(addr, i) \ 26 | ((*(int *)(int) (&(mem-memoffset)[amark(addr)]) = (i))) 27 | #define sstore(addr, i) \ 28 | ((*(short *)(int) (&(mem-memoffset)[amark(addr)]) = (i))) 29 | #define cstore(addr, i) \ 30 | (((mem-memoffset)[amark(addr)] = (i))) 31 | 32 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1992 The Regents of the University of California. 2 | # All rights reserved. See copyright.h for copyright notice and limitation 3 | # of liability and disclaimer of warranty provisions. 4 | 5 | MAKE = make 6 | LPR = lpr 7 | 8 | all: 9 | cd threads; $(MAKE) depend 10 | cd threads; $(MAKE) nachos 11 | cd userprog; $(MAKE) depend 12 | cd userprog; $(MAKE) nachos 13 | cd vm; $(MAKE) depend 14 | cd vm; $(MAKE) nachos 15 | cd filesys; $(MAKE) depend 16 | cd filesys; $(MAKE) nachos 17 | cd network; $(MAKE) depend 18 | cd network; $(MAKE) nachos 19 | cd bin; make all 20 | cd test; make all 21 | 22 | # don't delete executables in "test" in case there is no cross-compiler 23 | clean: 24 | /bin/csh -c "rm -f *~ */{core,nachos,DISK,*.o,swtch.s,*~} test/{*.coff} bin/{coff2flat,coff2noff,disassemble,out}" 25 | 26 | print: 27 | /bin/csh -c "$(LPR) Makefile* */Makefile" 28 | /bin/csh -c "$(LPR) threads/*.h threads/*.cc threads/*.s" 29 | /bin/csh -c "$(LPR) userprog/*.h userprog/*.cc" 30 | /bin/csh -c "$(LPR) filesys/*.h filesys/*.cc 31 | /bin/csh -c "$(LPR) network/*.h network/*.cc 32 | /bin/csh -c "$(LPR) machine/*.h machine/*.cc 33 | /bin/csh -c "$(LPR) bin/noff.h bin/coff.h bin/coff2noff.c" 34 | /bin/csh -c "$(LPR) test/*.h test/*.c test/*.s" 35 | -------------------------------------------------------------------------------- /threads/copyright.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. 4 | 5 | Permission to use, copy, modify, and distribute this software and its 6 | documentation for any purpose, without fee, and without written agreement is 7 | hereby granted, provided that the above copyright notice and the following 8 | two paragraphs appear in all copies of this software. 9 | 10 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR 11 | DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 12 | OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF 13 | CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 | 15 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 16 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 | AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 18 | ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO 19 | PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 20 | */ 21 | 22 | #ifdef MAIN /* include the copyright message in every executable */ 23 | static char *copyright = "Copyright (c) 1992-1993 The Regents of the University of California. All rights reserved."; 24 | #endif // MAIN 25 | -------------------------------------------------------------------------------- /threads/scheduler.h: -------------------------------------------------------------------------------- 1 | // scheduler.h 2 | // Data structures for the thread dispatcher and scheduler. 3 | // Primarily, the list of threads that are ready to run. 4 | // 5 | // Copyright (c) 1992-1993 The Regents of the University of California. 6 | // All rights reserved. See copyright.h for copyright notice and limitation 7 | // of liability and disclaimer of warranty provisions. 8 | 9 | #ifndef SCHEDULER_H 10 | #define SCHEDULER_H 11 | 12 | #include "copyright.h" 13 | #include "list.h" 14 | #include "thread.h" 15 | 16 | // The following class defines the scheduler/dispatcher abstraction -- 17 | // the data structures and operations needed to keep track of which 18 | // thread is running, and which threads are ready but not running. 19 | 20 | class Scheduler { 21 | public: 22 | Scheduler(); // Initialize list of ready threads 23 | ~Scheduler(); // De-allocate ready list 24 | 25 | void ReadyToRun(Thread* thread); // Thread can be dispatched. 26 | Thread* FindNextToRun(); // Dequeue first thread on the ready 27 | // list, if any, and return thread. 28 | void Run(Thread* nextThread); // Cause nextThread to start running 29 | void Print(); // Print contents of ready list 30 | 31 | private: 32 | List *readyList; // queue of threads that are ready to run, 33 | // but not running 34 | public: 35 | List* getReadyList(){ 36 | return readyList; 37 | } 38 | }; 39 | 40 | #endif // SCHEDULER_H 41 | -------------------------------------------------------------------------------- /test/shell.c: -------------------------------------------------------------------------------- 1 | #include "syscall.h" 2 | 3 | int 4 | main() 5 | { 6 | SpaceId newProc; 7 | OpenFileId input = ConsoleInput; 8 | OpenFileId output = ConsoleOutput; 9 | char prompt[2], ch, buffer[60]; 10 | int i; 11 | 12 | prompt[0] = '-'; 13 | prompt[1] = '-'; 14 | 15 | 16 | while( 1 ) 17 | { 18 | Write(prompt, 2, output); 19 | 20 | i = 0; 21 | 22 | do { 23 | 24 | Read(&buffer[i], 1, input); 25 | 26 | } while( buffer[i++] != '\n' ); 27 | 28 | buffer[--i] = '\0'; 29 | 30 | 31 | if(buffer[0] == 'x' && buffer[1] == ' ') { 32 | newProc = Exec(buffer+2); 33 | Join(newProc); 34 | } 35 | else if(buffer[0] == 'p' && buffer[1] == 'w' && buffer[2] == 'd') 36 | Pwd(); 37 | else if(buffer[0] == 'l' && buffer[1] == 's') 38 | Ls(); 39 | else if(buffer[0] == 'c' && buffer[1] == 'd') 40 | Cd(buffer+3); 41 | else if(buffer[0] == 'q' && buffer[1] == '\0') 42 | Exit(0); 43 | else if(buffer[0] == 'c' && buffer[1] == 'f') 44 | Create(buffer+3); 45 | else if(buffer[0] == 'r' && buffer[1] == 'm' && buffer[2] == ' ') 46 | Remove(buffer+3); 47 | else if(buffer[0] == 'm' && buffer[1] == 'k' && buffer[2] == 'd' && buffer[3] == 'i' && buffer[4] == 'r') 48 | CDir(buffer+6); 49 | else if(buffer[0] == 'r' && buffer[1] == 'm' && buffer[2] == 'd' && buffer[3] == 'i' && buffer[4] == 'r') 50 | RDir(buffer+6); 51 | else if(buffer[0] == 'h' && buffer[1] == 'e' && buffer[2] == 'l' && buffer[3] == 'p') 52 | Help(); 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /threads/out4: -------------------------------------------------------------------------------- 1 | This function set_tid_() has been called. 2 | This function set_uid_() has been called. 3 | This function set_tid_() has been called. 4 | This function set_uid_() has been called. 5 | This function set_tid_() has been called. 6 | This function set_uid_() has been called. 7 | This function set_tid_() has been called. 8 | This function set_uid_() has been called. 9 | This function get_tid_() has been called. 10 | thread name forked thread1 tid_ 1 status: RUNNING 11 | This function get_tid_() has been called. 12 | name forked thread2 tid_ 2 status: READY 13 | This function get_tid_() has been called. 14 | name forked thread3 tid_ 3 status: READY 15 | This function get_tid_() has been called. 16 | thread name forked thread2 tid_ 2 status: RUNNING 17 | This function get_tid_() has been called. 18 | name forked thread3 tid_ 3 status: READY 19 | This function get_tid_() has been called. 20 | name forked thread1 tid_ 1 status: READY 21 | This function get_tid_() has been called. 22 | thread name forked thread3 tid_ 3 status: RUNNING 23 | This function get_tid_() has been called. 24 | name forked thread1 tid_ 1 status: READY 25 | This function get_tid_() has been called. 26 | name forked thread2 tid_ 2 status: READY 27 | No threads ready or runnable, and no pending interrupts. 28 | Assuming the program completed. 29 | Machine halting! 30 | 31 | Ticks: total 100, idle 0, system 100, user 0 32 | Disk I/O: reads 0, writes 0 33 | Console I/O: reads 0, writes 0 34 | Paging: faults 0 35 | Network I/O: packets received 0, sent 0 36 | 37 | Cleaning up... 38 | -------------------------------------------------------------------------------- /threads/out3: -------------------------------------------------------------------------------- 1 | This function set_tid_() has been called. 2 | This function set_uid_() has been called. 3 | This function set_tid_() has been called. 4 | This function set_uid_() has been called. 5 | This function set_tid_() has been called. 6 | This function set_uid_() has been called. 7 | This function set_tid_() has been called. 8 | This function set_uid_() has been called. 9 | This function get_tid_() has been called. 10 | thread name forked thread1 tid_ 1 status: RUNNING 11 | This function get_tid_() has been called. 12 | name forked thread1 tid_ 1 status: RUNNING 13 | This function get_tid_() has been called. 14 | name forked thread1 tid_ 1 status: RUNNING 15 | This function get_tid_() has been called. 16 | thread name forked thread2 tid_ 2 status: RUNNING 17 | This function get_tid_() has been called. 18 | name forked thread2 tid_ 2 status: RUNNING 19 | This function get_tid_() has been called. 20 | name forked thread2 tid_ 2 status: RUNNING 21 | This function get_tid_() has been called. 22 | thread name forked thread3 tid_ 3 status: RUNNING 23 | This function get_tid_() has been called. 24 | name forked thread3 tid_ 3 status: RUNNING 25 | This function get_tid_() has been called. 26 | name forked thread3 tid_ 3 status: RUNNING 27 | No threads ready or runnable, and no pending interrupts. 28 | Assuming the program completed. 29 | Machine halting! 30 | 31 | Ticks: total 100, idle 0, system 100, user 0 32 | Disk I/O: reads 0, writes 0 33 | Console I/O: reads 0, writes 0 34 | Paging: faults 0 35 | Network I/O: packets received 0, sent 0 36 | 37 | Cleaning up... 38 | -------------------------------------------------------------------------------- /threads/synchlist.h: -------------------------------------------------------------------------------- 1 | // synchlist.h 2 | // Data structures for synchronized access to a list. 3 | // 4 | // Implemented by surrounding the List abstraction 5 | // with synchronization routines. 6 | // 7 | // Copyright (c) 1992-1993 The Regents of the University of California. 8 | // All rights reserved. See copyright.h for copyright notice and limitation 9 | // of liability and disclaimer of warranty provisions. 10 | 11 | #ifndef SYNCHLIST_H 12 | #define SYNCHLIST_H 13 | 14 | #include "copyright.h" 15 | #include "list.h" 16 | #include "synch.h" 17 | 18 | // The following class defines a "synchronized list" -- a list for which: 19 | // these constraints hold: 20 | // 1. Threads trying to remove an item from a list will 21 | // wait until the list has an element on it. 22 | // 2. One thread at a time can access list data structures 23 | 24 | class SynchList { 25 | public: 26 | SynchList(); // initialize a synchronized list 27 | ~SynchList(); // de-allocate a synchronized list 28 | 29 | void Append(void *item); // append item to the end of the list, 30 | // and wake up any thread waiting in remove 31 | void *Remove(); // remove the first item from the front of 32 | // the list, waiting if the list is empty 33 | // apply function to every item in the list 34 | void Mapcar(VoidFunctionPtr func); 35 | 36 | private: 37 | List *list; // the unsynchronized list 38 | Lock *lock; // enforce mutual exclusive access to the list 39 | Condition *listEmpty; // wait in Remove if the list is empty 40 | }; 41 | 42 | #endif // SYNCHLIST_H 43 | -------------------------------------------------------------------------------- /vm/swtch.s: -------------------------------------------------------------------------------- 1 | .text 2 | .align 2 3 | .globl ThreadRoot 4 | ThreadRoot: 5 | pushl %ebp 6 | movl %esp,%ebp 7 | pushl %edx 8 | call *%ecx 9 | call *%esi 10 | call *%edi 11 | movl %ebp,%esp 12 | popl %ebp 13 | ret 14 | .comm _eax_save,4 15 | .globl SWITCH 16 | SWITCH: 17 | movl %eax,_eax_save # save the value of eax 18 | movl 4(%esp),%eax # move pointer to t1 into eax 19 | movl %ebx,8(%eax) # save registers 20 | movl %ecx,12(%eax) 21 | movl %edx,16(%eax) 22 | movl %esi,24(%eax) 23 | movl %edi,28(%eax) 24 | movl %ebp,20(%eax) 25 | movl %esp,0(%eax) # save stack pointer 26 | movl _eax_save,%ebx # get the saved value of eax 27 | movl %ebx,4(%eax) # store it 28 | movl 0(%esp),%ebx # get return address from stack into ebx 29 | movl %ebx,32(%eax) # save it into the pc storage 30 | movl 8(%esp),%eax # move pointer to t2 into eax 31 | movl 4(%eax),%ebx # get new value for eax into ebx 32 | movl %ebx,_eax_save # save it 33 | movl 8(%eax),%ebx # retore old registers 34 | movl 12(%eax),%ecx 35 | movl 16(%eax),%edx 36 | movl 24(%eax),%esi 37 | movl 28(%eax),%edi 38 | movl 20(%eax),%ebp 39 | movl 0(%eax),%esp # restore stack pointer 40 | movl 32(%eax),%eax # restore return address into eax 41 | movl %eax,4(%esp) # copy over the ret address on the stack 42 | movl _eax_save,%eax 43 | ret 44 | -------------------------------------------------------------------------------- /filesys/swtch.s: -------------------------------------------------------------------------------- 1 | .text 2 | .align 2 3 | .globl ThreadRoot 4 | ThreadRoot: 5 | pushl %ebp 6 | movl %esp,%ebp 7 | pushl %edx 8 | call *%ecx 9 | call *%esi 10 | call *%edi 11 | movl %ebp,%esp 12 | popl %ebp 13 | ret 14 | .comm _eax_save,4 15 | .globl SWITCH 16 | SWITCH: 17 | movl %eax,_eax_save # save the value of eax 18 | movl 4(%esp),%eax # move pointer to t1 into eax 19 | movl %ebx,8(%eax) # save registers 20 | movl %ecx,12(%eax) 21 | movl %edx,16(%eax) 22 | movl %esi,24(%eax) 23 | movl %edi,28(%eax) 24 | movl %ebp,20(%eax) 25 | movl %esp,0(%eax) # save stack pointer 26 | movl _eax_save,%ebx # get the saved value of eax 27 | movl %ebx,4(%eax) # store it 28 | movl 0(%esp),%ebx # get return address from stack into ebx 29 | movl %ebx,32(%eax) # save it into the pc storage 30 | movl 8(%esp),%eax # move pointer to t2 into eax 31 | movl 4(%eax),%ebx # get new value for eax into ebx 32 | movl %ebx,_eax_save # save it 33 | movl 8(%eax),%ebx # retore old registers 34 | movl 12(%eax),%ecx 35 | movl 16(%eax),%edx 36 | movl 24(%eax),%esi 37 | movl 28(%eax),%edi 38 | movl 20(%eax),%ebp 39 | movl 0(%eax),%esp # restore stack pointer 40 | movl 32(%eax),%eax # restore return address into eax 41 | movl %eax,4(%esp) # copy over the ret address on the stack 42 | movl _eax_save,%eax 43 | ret 44 | -------------------------------------------------------------------------------- /network/swtch.s: -------------------------------------------------------------------------------- 1 | .text 2 | .align 2 3 | .globl ThreadRoot 4 | ThreadRoot: 5 | pushl %ebp 6 | movl %esp,%ebp 7 | pushl %edx 8 | call *%ecx 9 | call *%esi 10 | call *%edi 11 | movl %ebp,%esp 12 | popl %ebp 13 | ret 14 | .comm _eax_save,4 15 | .globl SWITCH 16 | SWITCH: 17 | movl %eax,_eax_save # save the value of eax 18 | movl 4(%esp),%eax # move pointer to t1 into eax 19 | movl %ebx,8(%eax) # save registers 20 | movl %ecx,12(%eax) 21 | movl %edx,16(%eax) 22 | movl %esi,24(%eax) 23 | movl %edi,28(%eax) 24 | movl %ebp,20(%eax) 25 | movl %esp,0(%eax) # save stack pointer 26 | movl _eax_save,%ebx # get the saved value of eax 27 | movl %ebx,4(%eax) # store it 28 | movl 0(%esp),%ebx # get return address from stack into ebx 29 | movl %ebx,32(%eax) # save it into the pc storage 30 | movl 8(%esp),%eax # move pointer to t2 into eax 31 | movl 4(%eax),%ebx # get new value for eax into ebx 32 | movl %ebx,_eax_save # save it 33 | movl 8(%eax),%ebx # retore old registers 34 | movl 12(%eax),%ecx 35 | movl 16(%eax),%edx 36 | movl 24(%eax),%esi 37 | movl 28(%eax),%edi 38 | movl 20(%eax),%ebp 39 | movl 0(%eax),%esp # restore stack pointer 40 | movl 32(%eax),%eax # restore return address into eax 41 | movl %eax,4(%esp) # copy over the ret address on the stack 42 | movl _eax_save,%eax 43 | ret 44 | -------------------------------------------------------------------------------- /threads/swtch.s: -------------------------------------------------------------------------------- 1 | .text 2 | .align 2 3 | .globl ThreadRoot 4 | ThreadRoot: 5 | pushl %ebp 6 | movl %esp,%ebp 7 | pushl %edx 8 | call *%ecx 9 | call *%esi 10 | call *%edi 11 | movl %ebp,%esp 12 | popl %ebp 13 | ret 14 | .comm _eax_save,4 15 | .globl SWITCH 16 | SWITCH: 17 | movl %eax,_eax_save # save the value of eax 18 | movl 4(%esp),%eax # move pointer to t1 into eax 19 | movl %ebx,8(%eax) # save registers 20 | movl %ecx,12(%eax) 21 | movl %edx,16(%eax) 22 | movl %esi,24(%eax) 23 | movl %edi,28(%eax) 24 | movl %ebp,20(%eax) 25 | movl %esp,0(%eax) # save stack pointer 26 | movl _eax_save,%ebx # get the saved value of eax 27 | movl %ebx,4(%eax) # store it 28 | movl 0(%esp),%ebx # get return address from stack into ebx 29 | movl %ebx,32(%eax) # save it into the pc storage 30 | movl 8(%esp),%eax # move pointer to t2 into eax 31 | movl 4(%eax),%ebx # get new value for eax into ebx 32 | movl %ebx,_eax_save # save it 33 | movl 8(%eax),%ebx # retore old registers 34 | movl 12(%eax),%ecx 35 | movl 16(%eax),%edx 36 | movl 24(%eax),%esi 37 | movl 28(%eax),%edi 38 | movl 20(%eax),%ebp 39 | movl 0(%eax),%esp # restore stack pointer 40 | movl 32(%eax),%eax # restore return address into eax 41 | movl %eax,4(%esp) # copy over the ret address on the stack 42 | movl _eax_save,%eax 43 | ret 44 | -------------------------------------------------------------------------------- /userprog/swtch.s: -------------------------------------------------------------------------------- 1 | .text 2 | .align 2 3 | .globl ThreadRoot 4 | ThreadRoot: 5 | pushl %ebp 6 | movl %esp,%ebp 7 | pushl %edx 8 | call *%ecx 9 | call *%esi 10 | call *%edi 11 | movl %ebp,%esp 12 | popl %ebp 13 | ret 14 | .comm _eax_save,4 15 | .globl SWITCH 16 | SWITCH: 17 | movl %eax,_eax_save # save the value of eax 18 | movl 4(%esp),%eax # move pointer to t1 into eax 19 | movl %ebx,8(%eax) # save registers 20 | movl %ecx,12(%eax) 21 | movl %edx,16(%eax) 22 | movl %esi,24(%eax) 23 | movl %edi,28(%eax) 24 | movl %ebp,20(%eax) 25 | movl %esp,0(%eax) # save stack pointer 26 | movl _eax_save,%ebx # get the saved value of eax 27 | movl %ebx,4(%eax) # store it 28 | movl 0(%esp),%ebx # get return address from stack into ebx 29 | movl %ebx,32(%eax) # save it into the pc storage 30 | movl 8(%esp),%eax # move pointer to t2 into eax 31 | movl 4(%eax),%ebx # get new value for eax into ebx 32 | movl %ebx,_eax_save # save it 33 | movl 8(%eax),%ebx # retore old registers 34 | movl 12(%eax),%ecx 35 | movl 16(%eax),%edx 36 | movl 24(%eax),%esi 37 | movl 28(%eax),%edi 38 | movl 20(%eax),%ebp 39 | movl 0(%eax),%esp # restore stack pointer 40 | movl 32(%eax),%eax # restore return address into eax 41 | movl %eax,4(%esp) # copy over the ret address on the stack 42 | movl _eax_save,%eax 43 | ret 44 | -------------------------------------------------------------------------------- /userprog/addrspace.h: -------------------------------------------------------------------------------- 1 | // addrspace.h 2 | // Data structures to keep track of executing user programs 3 | // (address spaces). 4 | // 5 | // For now, we don't keep any information about address spaces. 6 | // The user level CPU state is saved and restored in the thread 7 | // executing the user program (see thread.h). 8 | // 9 | // Copyright (c) 1992-1993 The Regents of the University of California. 10 | // All rights reserved. See copyright.h for copyright notice and limitation 11 | // of liability and disclaimer of warranty provisions. 12 | 13 | #ifndef ADDRSPACE_H 14 | #define ADDRSPACE_H 15 | 16 | #include "copyright.h" 17 | #include "filesys.h" 18 | 19 | #define UserStackSize 1024 // increase this as necessary! 20 | 21 | class AddrSpace { 22 | public: 23 | AddrSpace(); 24 | AddrSpace(OpenFile *executable,char *filename); // Create an address space, 25 | // initializing it with the program 26 | // stored in the file "executable" 27 | ~AddrSpace(); // De-allocate an address space 28 | 29 | void InitRegisters(); // Initialize user-level CPU registers, 30 | // before jumping to user code 31 | 32 | void SaveState(); // Save/restore address space-specific 33 | void RestoreState(); // info on a context switch 34 | void AddrSpaceCopy(AddrSpace* tspace); 35 | // private: 36 | TranslationEntry *pageTable; // Assume linear page table translation 37 | // for now! 38 | unsigned int numPages; // Number of pages in the virtual 39 | // address space 40 | char *fileName; 41 | }; 42 | 43 | class Info{ 44 | public: 45 | AddrSpace *space; 46 | int pc; 47 | Info(); 48 | ~Info(); 49 | }; 50 | #endif // ADDRSPACE_H 51 | -------------------------------------------------------------------------------- /machine/stats.cc: -------------------------------------------------------------------------------- 1 | // stats.h 2 | // Routines for managing statistics about Nachos performance. 3 | // 4 | // DO NOT CHANGE -- these stats are maintained by the machine emulation. 5 | // 6 | // Copyright (c) 1992-1993 The Regents of the University of California. 7 | // All rights reserved. See copyright.h for copyright notice and limitation 8 | // of liability and disclaimer of warranty provisions. 9 | 10 | #include "copyright.h" 11 | #include "utility.h" 12 | #include "stats.h" 13 | 14 | //---------------------------------------------------------------------- 15 | // Statistics::Statistics 16 | // Initialize performance metrics to zero, at system startup. 17 | //---------------------------------------------------------------------- 18 | 19 | Statistics::Statistics() 20 | { 21 | totalTicks = idleTicks = systemTicks = userTicks = 0; 22 | numDiskReads = numDiskWrites = 0; 23 | numConsoleCharsRead = numConsoleCharsWritten = 0; 24 | numPageFaults = numPacketsSent = numPacketsRecvd = 0; 25 | } 26 | 27 | //---------------------------------------------------------------------- 28 | // Statistics::Print 29 | // Print performance metrics, when we've finished everything 30 | // at system shutdown. 31 | //---------------------------------------------------------------------- 32 | 33 | void 34 | Statistics::Print() 35 | { 36 | printf("Ticks: total %d, idle %d, system %d, user %d\n", totalTicks, 37 | idleTicks, systemTicks, userTicks); 38 | printf("Disk I/O: reads %d, writes %d\n", numDiskReads, numDiskWrites); 39 | printf("Console I/O: reads %d, writes %d\n", numConsoleCharsRead, 40 | numConsoleCharsWritten); 41 | printf("Paging: faults %d\n", numPageFaults); 42 | printf("Network I/O: packets received %d, sent %d\n", numPacketsRecvd, 43 | numPacketsSent); 44 | } 45 | -------------------------------------------------------------------------------- /machine/timer.h: -------------------------------------------------------------------------------- 1 | // timer.h 2 | // Data structures to emulate a hardware timer. 3 | // 4 | // A hardware timer generates a CPU interrupt every X milliseconds. 5 | // This means it can be used for implementing time-slicing, or for 6 | // having a thread go to sleep for a specific period of time. 7 | // 8 | // We emulate a hardware timer by scheduling an interrupt to occur 9 | // every time stats->totalTicks has increased by TimerTicks. 10 | // 11 | // In order to introduce some randomness into time-slicing, if "doRandom" 12 | // is set, then the interrupt comes after a random number of ticks. 13 | // 14 | // DO NOT CHANGE -- part of the machine emulation 15 | // 16 | // Copyright (c) 1992-1993 The Regents of the University of California. 17 | // All rights reserved. See copyright.h for copyright notice and limitation 18 | // of liability and disclaimer of warranty provisions. 19 | 20 | #ifndef TIMER_H 21 | #define TIMER_H 22 | 23 | #include "copyright.h" 24 | #include "utility.h" 25 | 26 | // The following class defines a hardware timer. 27 | class Timer { 28 | public: 29 | Timer(VoidFunctionPtr timerHandler, int callArg, bool doRandom); 30 | // Initialize the timer, to call the interrupt 31 | // handler "timerHandler" every time slice. 32 | ~Timer() {} 33 | 34 | // Internal routines to the timer emulation -- DO NOT call these 35 | 36 | void TimerExpired(); // called internally when the hardware 37 | // timer generates an interrupt 38 | 39 | int TimeOfNextInterrupt(); // figure out when the timer will generate 40 | // its next interrupt 41 | 42 | private: 43 | bool randomize; // set if we need to use a random timeout delay 44 | VoidFunctionPtr handler; // timer interrupt handler 45 | int arg; // argument to pass to interrupt handler 46 | 47 | }; 48 | 49 | #endif // TIMER_H 50 | -------------------------------------------------------------------------------- /machine/translate.h: -------------------------------------------------------------------------------- 1 | // translate.h 2 | // Data structures for managing the translation from 3 | // virtual page # -> physical page #, used for managing 4 | // physical memory on behalf of user programs. 5 | // 6 | // The data structures in this file are "dual-use" - they 7 | // serve both as a page table entry, and as an entry in 8 | // a software-managed translation lookaside buffer (TLB). 9 | // Either way, each entry is of the form: 10 | // . 11 | // 12 | // DO NOT CHANGE -- part of the machine emulation 13 | // 14 | // Copyright (c) 1992-1993 The Regents of the University of California. 15 | // All rights reserved. See copyright.h for copyright notice and limitation 16 | // of liability and disclaimer of warranty provisions. 17 | 18 | #ifndef TLB_H 19 | #define TLB_H 20 | 21 | #include "copyright.h" 22 | #include "utility.h" 23 | 24 | // The following class defines an entry in a translation table -- either 25 | // in a page table or a TLB. Each entry defines a mapping from one 26 | // virtual page to one physical page. 27 | // In addition, there are some extra bits for access control (valid and 28 | // read-only) and some bits for usage information (use and dirty). 29 | 30 | class TranslationEntry { 31 | public: 32 | int virtualPage; // The page number in virtual memory. 33 | int physicalPage; // The page number in real memory (relative to the 34 | // start of "mainMemory" 35 | bool valid; // If this bit is set, the translation is ignored. 36 | // (In other words, the entry hasn't been initialized.) 37 | bool readOnly; // If this bit is set, the user program is not allowed 38 | // to modify the contents of the page. 39 | bool use; // This bit is set by the hardware every time the 40 | // page is referenced or modified. 41 | bool dirty; // This bit is set by the hardware every time the 42 | // page is modified. 43 | int threadId; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /network/README: -------------------------------------------------------------------------------- 1 | From: cs162@po.EECS.Berkeley.EDU (Class Master) 2 | Subject: Answer to problems running assignment 5 3 | 4 | Some people have been having problems running assignment 5. 5 | Unfortunately, the way the network gets initialized, if things 6 | don't get done in exactly the right order, you can get an error 7 | in sending the first packet to the socket. 8 | 9 | I have modified synch.h and synch.cc in code/, and re-compiled 10 | the contents of code/network. I was then able to run the 11 | test case successfully, at least one out of two times. See below. 12 | So, be careful out there! 13 | 14 | tom 15 | ----------- 16 | po.EECS.Berkeley.EDU:network>!19 17 | ./nachos -m 0 -o 1 > & log & ; ./nachos -m 1 -o 0 > & log2 & 18 | [1] 10882 19 | [2] 10883 20 | po.EECS.Berkeley.EDU:network> 21 | [2] Illegal instruction ./nachos -m 1 -o 0 >& log2 (core dumped) 22 | 23 | [1] Illegal instruction ./nachos -m 0 -o 1 >& log (core dumped) 24 | 25 | po.EECS.Berkeley.EDU:network>rm -f log* SOCKET* 26 | po.EECS.Berkeley.EDU:network>!19 27 | ./nachos -m 0 -o 1 > & log & ; ./nachos -m 1 -o 0 > & log2 & 28 | [1] 10895 29 | [2] 10896 30 | po.EECS.Berkeley.EDU:network> 31 | [1] Done ./nachos -m 0 -o 1 >& log 32 | 33 | [2] Done ./nachos -m 1 -o 0 >& log2 34 | more log 35 | Got "Hello there!" from 1, box 1 36 | Got "Got it!" from 1, box 1 37 | Machine halting! 38 | 39 | Ticks: total 57080, idle 56810, system 270, user 0 40 | Disk I/O: reads 2, writes 0 41 | Console I/O: reads 0, writes 0 42 | Paging: faults 0 43 | Network I/O: packets received 2, sent 2 44 | 45 | 46 | 47 | Cleaning up... 48 | po.EECS.Berkeley.EDU:network>more log2 49 | Got "Hello there!" from 0, box 1 50 | Got "Got it!" from 0, box 1 51 | Machine halting! 52 | 53 | Ticks: total 58530, idle 58260, system 270, user 0 54 | Disk I/O: reads 2, writes 0 55 | Console I/O: reads 0, writes 0 56 | Paging: faults 0 57 | Network I/O: packets received 2, sent 2 58 | 59 | Cleaning up... 60 | -------------------------------------------------------------------------------- /threads/system.h: -------------------------------------------------------------------------------- 1 | // system.h 2 | // All global variables used in Nachos are defined here. 3 | // 4 | // Copyright (c) 1992-1993 The Regents of the University of California. 5 | // All rights reserved. See copyright.h for copyright notice and limitation 6 | // of liability and disclaimer of warranty provisions. 7 | 8 | #ifndef SYSTEM_H 9 | #define SYSTEM_H 10 | 11 | #include "copyright.h" 12 | #include "utility.h" 13 | #include "thread.h" 14 | #include "scheduler.h" 15 | #include "interrupt.h" 16 | #include "stats.h" 17 | #include "timer.h" 18 | 19 | //------------------------------------------------------------------- 20 | #define MaxNumOfThread 128 21 | extern int AvaNum[MaxNumOfThread]; 22 | //------------------------------------------------------------------- 23 | 24 | // Initialization and cleanup routines 25 | extern void Initialize(int argc, char **argv); // Initialization, 26 | // called before anything else 27 | extern void Cleanup(); // Cleanup, called when 28 | // Nachos is done. 29 | 30 | extern Thread *currentThread; // the thread holding the CPU 31 | extern Thread *threadToBeDestroyed; // the thread that just finished 32 | extern Scheduler *scheduler; // the ready list 33 | extern Interrupt *interrupt; // interrupt status 34 | extern Statistics *stats; // performance metrics 35 | extern Timer *timer; // the hardware alarm clock 36 | extern int lastSwitchTime; 37 | extern int TimerTicks1; 38 | extern int TimerTicks2; 39 | extern int TimerTicks3; 40 | #ifdef USER_PROGRAM 41 | 42 | #include "machine.h" 43 | //#include "synchdisk.h" 44 | 45 | //extern SynchDisk *synchDisk; 46 | extern Machine* machine; // user program memory and registers 47 | #endif 48 | 49 | #ifdef FILESYS_NEEDED // FILESYS or FILESYS_STUB 50 | #include "filesys.h" 51 | extern FileSystem *fileSystem; 52 | #endif 53 | 54 | #ifdef FILESYS 55 | #include "synchdisk.h" 56 | extern SynchDisk *synchDisk; 57 | #endif 58 | 59 | #ifdef NETWORK 60 | #include "post.h" 61 | extern PostOffice* postOffice; 62 | #endif 63 | 64 | #endif // SYSTEM_H 65 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | # use normal make for this Makefile 2 | # 3 | # Makefile for building user programs to run on top of Nachos 4 | # 5 | # Several things to be aware of: 6 | # 7 | # Nachos assumes that the location of the program startup routine (the 8 | # location the kernel jumps to when the program initially starts up) 9 | # is at location 0. This means: start.o must be the first .o passed 10 | # to ld, in order for the routine "Start" to be loaded at location 0 11 | # 12 | 13 | # if you are cross-compiling, you need to point to the right executables 14 | # and change the flags to ld and the build procedure for as 15 | 16 | GCCDIR = ../../../gnu-decstation-ultrix/decstation-ultrix/2.95.3/ 17 | 18 | LDFLAGS = -T script -N 19 | ASFLAGS = -mips2 20 | CPPFLAGS = $(INCDIR) 21 | 22 | # if you aren't cross-compiling: 23 | # GCCDIR = 24 | # LDFLAGS = -N -T 0 25 | # ASFLAGS = 26 | # CPPFLAGS = -P $(INCDIR) 27 | 28 | PATH = $(GCCDIR):/lib:/usr/bin:/bin 29 | 30 | CC = $(GCCDIR)gcc -B../../../gnu-decstation-ultrix/ 31 | AS = $(GCCDIR)as 32 | LD = $(GCCDIR)ld 33 | 34 | CPP = gcc -E 35 | INCDIR =-I../userprog -I../threads 36 | CFLAGS = -G 0 -c $(INCDIR) 37 | 38 | all: halt shell matmult sort 39 | 40 | start.o: start.s ../userprog/syscall.h 41 | $(CPP) $(CPPFLAGS) start.c > strt.s 42 | $(AS) $(ASFLAGS) -o start.o strt.s 43 | rm strt.s 44 | 45 | 46 | halt.o: halt.c 47 | $(CC) $(CFLAGS) -c halt.c 48 | halt: halt.o start.o 49 | $(LD) $(LDFLAGS) start.o halt.o -o halt.coff 50 | ../bin/coff2noff halt.coff halt 51 | 52 | shell.o: shell.c 53 | $(CC) $(CFLAGS) -c shell.c 54 | shell: shell.o start.o 55 | $(LD) $(LDFLAGS) start.o shell.o -o shell.coff 56 | ../bin/coff2noff shell.coff shell 57 | 58 | sort.o: sort.c 59 | $(CC) $(CFLAGS) -c sort.c 60 | sort: sort.o start.o 61 | $(LD) $(LDFLAGS) start.o sort.o -o sort.coff 62 | ../bin/coff2noff sort.coff sort 63 | 64 | matmult.o: matmult.c 65 | $(CC) $(CFLAGS) -c matmult.c 66 | matmult: matmult.o start.o 67 | $(LD) $(LDFLAGS) start.o matmult.o -o matmult.coff 68 | ../bin/coff2noff matmult.coff matmult 69 | -------------------------------------------------------------------------------- /bin/opstrings.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | 9 | char *normalops[] = { 10 | "special", 11 | "bcond", 12 | "j", 13 | "jal", 14 | "beq", 15 | "bne", 16 | "blez", 17 | "bgtz", 18 | "addi", 19 | "addiu", 20 | "slti", 21 | "sltiu", 22 | "andi", 23 | "ori", 24 | "xori", 25 | "lui", 26 | "cop0", 27 | "cop1", 28 | "cop2", 29 | "cop3", 30 | "024", 31 | "025", 32 | "026", 33 | "027", 34 | "030", 35 | "031", 36 | "032", 37 | "033", 38 | "034", 39 | "035", 40 | "036", 41 | "037", 42 | "lb", 43 | "lh", 44 | "lwl", 45 | "lw", 46 | "lbu", 47 | "lhu", 48 | "lwr", 49 | "047", 50 | "sb", 51 | "sh", 52 | "swl", 53 | "sw", 54 | "054", 55 | "055", 56 | "swr", 57 | "057", 58 | "lwc0", 59 | "lwc1", 60 | "lwc2", 61 | "lwc3", 62 | "064", 63 | "065", 64 | "066", 65 | "067", 66 | "swc0", 67 | "swc1", 68 | "swc2", 69 | "swc3", 70 | "074", 71 | "075", 72 | "076", 73 | "077" 74 | }; 75 | 76 | char *specialops[] = { 77 | "sll", 78 | "001", 79 | "srl", 80 | "sra", 81 | "sllv", 82 | "005", 83 | "srlv", 84 | "srav", 85 | "jr", 86 | "jalr", 87 | "012", 88 | "013", 89 | "syscall", 90 | "break", 91 | "016", 92 | "017", 93 | "mfhi", 94 | "mthi", 95 | "mflo", 96 | "mtlo", 97 | "024", 98 | "025", 99 | "026", 100 | "027", 101 | "mult", 102 | "multu", 103 | "div", 104 | "divu", 105 | "034", 106 | "035", 107 | "036", 108 | "037", 109 | "add", 110 | "addu", 111 | "sub", 112 | "subu", 113 | "and", 114 | "or", 115 | "xor", 116 | "nor", 117 | "050", 118 | "051", 119 | "slt", 120 | "sltu", 121 | "054", 122 | "055", 123 | "056", 124 | "057", 125 | "060", 126 | "061", 127 | "062", 128 | "063", 129 | "064", 130 | "065", 131 | "066", 132 | "067", 133 | "070", 134 | "071", 135 | "072", 136 | "073", 137 | "074", 138 | "075", 139 | "076", 140 | "077", 141 | }; 142 | 143 | -------------------------------------------------------------------------------- /bin/encode.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | 9 | 10 | /* normal opcodes */ 11 | 12 | #define I_SPECIAL 000 13 | #define I_BCOND 001 14 | #define I_J 002 15 | #define I_JAL 003 16 | #define I_BEQ 004 17 | #define I_BNE 005 18 | #define I_BLEZ 006 19 | #define I_BGTZ 007 20 | #define I_ADDI 010 21 | #define I_ADDIU 011 22 | #define I_SLTI 012 23 | #define I_SLTIU 013 24 | #define I_ANDI 014 25 | #define I_ORI 015 26 | #define I_XORI 016 27 | #define I_LUI 017 28 | #define I_COP0 020 29 | #define I_COP1 021 30 | #define I_COP2 022 31 | #define I_COP3 023 32 | 33 | #define I_LB 040 34 | #define I_LH 041 35 | #define I_LWL 042 36 | #define I_LW 043 37 | #define I_LBU 044 38 | #define I_LHU 045 39 | #define I_LWR 046 40 | 41 | #define I_SB 050 42 | #define I_SH 051 43 | #define I_SWL 052 44 | #define I_SW 053 45 | 46 | #define I_SWR 056 47 | 48 | #define I_LWC0 060 49 | #define I_LWC1 061 50 | #define I_LWC2 062 51 | #define I_LWC3 063 52 | 53 | #define I_SWC0 070 54 | #define I_SWC1 071 55 | #define I_SWC2 072 56 | #define I_SWC3 073 57 | 58 | /* special opcodes */ 59 | 60 | #define I_SLL 000 61 | 62 | #define I_SRL 002 63 | #define I_SRA 003 64 | #define I_SLLV 004 65 | 66 | #define I_SRLV 006 67 | #define I_SRAV 007 68 | #define I_JR 010 69 | #define I_JALR 011 70 | 71 | #define I_SYSCALL 014 72 | #define I_BREAK 015 73 | 74 | #define I_MFHI 020 75 | #define I_MTHI 021 76 | #define I_MFLO 022 77 | #define I_MTLO 023 78 | 79 | #define I_MULT 030 80 | #define I_MULTU 031 81 | #define I_DIV 032 82 | #define I_DIVU 033 83 | 84 | #define I_ADD 040 85 | #define I_ADDU 041 86 | #define I_SUB 042 87 | #define I_SUBU 043 88 | #define I_AND 044 89 | #define I_OR 045 90 | #define I_XOR 046 91 | #define I_NOR 047 92 | 93 | #define I_SLT 052 94 | #define I_SLTU 053 95 | 96 | /* bcond opcodes */ 97 | 98 | 99 | #define I_BLTZ 000 100 | #define I_BGEZ 001 101 | 102 | #define I_BLTZAL 020 103 | #define I_BGEZAL 021 104 | 105 | /* whole instructions */ 106 | 107 | #define I_NOP 000 108 | -------------------------------------------------------------------------------- /threads/utility.cc: -------------------------------------------------------------------------------- 1 | // utility.cc 2 | // Debugging routines. Allows users to control whether to 3 | // print DEBUG statements, based on a command line argument. 4 | // 5 | // Copyright (c) 1992-1993 The Regents of the University of California. 6 | // All rights reserved. See copyright.h for copyright notice and limitation 7 | // of liability and disclaimer of warranty provisions. 8 | 9 | #include "copyright.h" 10 | #include "utility.h" 11 | 12 | // this seems to be dependent on how the compiler is configured. 13 | // if you have problems with va_start, try both of these alternatives 14 | #ifdef HOST_SNAKE 15 | #include 16 | #else 17 | #ifdef HOST_SPARC 18 | #include 19 | #else 20 | #include "stdarg.h" 21 | #endif 22 | #endif 23 | 24 | static char *enableFlags = NULL; // controls which DEBUG messages are printed 25 | 26 | //---------------------------------------------------------------------- 27 | // DebugInit 28 | // Initialize so that only DEBUG messages with a flag in flagList 29 | // will be printed. 30 | // 31 | // If the flag is "+", we enable all DEBUG messages. 32 | // 33 | // "flagList" is a string of characters for whose DEBUG messages are 34 | // to be enabled. 35 | //---------------------------------------------------------------------- 36 | 37 | void 38 | DebugInit(char *flagList) 39 | { 40 | enableFlags = flagList; 41 | } 42 | 43 | //---------------------------------------------------------------------- 44 | // DebugIsEnabled 45 | // Return TRUE if DEBUG messages with "flag" are to be printed. 46 | //---------------------------------------------------------------------- 47 | 48 | bool 49 | DebugIsEnabled(char flag) 50 | { 51 | if (enableFlags != NULL) 52 | return (strchr(enableFlags, flag) != 0) 53 | || (strchr(enableFlags, '+') != 0); 54 | else 55 | return FALSE; 56 | } 57 | 58 | //---------------------------------------------------------------------- 59 | // DEBUG 60 | // Print a debug message, if flag is enabled. Like printf, 61 | // only with an extra argument on the front. 62 | //---------------------------------------------------------------------- 63 | 64 | void 65 | DEBUG(char flag, char *format, ...) 66 | { 67 | if (DebugIsEnabled(flag)) { 68 | va_list ap; 69 | // You will get an unused variable message here -- ignore it. 70 | va_start(ap, format); 71 | vfprintf(stdout, format, ap); 72 | va_end(ap); 73 | fflush(stdout); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /userprog/bitmap.h: -------------------------------------------------------------------------------- 1 | // bitmap.h 2 | // Data structures defining a bitmap -- an array of bits each of which 3 | // can be either on or off. 4 | // 5 | // Represented as an array of unsigned integers, on which we do 6 | // modulo arithmetic to find the bit we are interested in. 7 | // 8 | // The bitmap can be parameterized with with the number of bits being 9 | // managed. 10 | // 11 | // Copyright (c) 1992-1993 The Regents of the University of California. 12 | // All rights reserved. See copyright.h for copyright notice and limitation 13 | // of liability and disclaimer of warranty provisions. 14 | 15 | #ifndef BITMAP_H 16 | #define BITMAP_H 17 | 18 | #include "copyright.h" 19 | #include "utility.h" 20 | #include "openfile.h" 21 | 22 | // Definitions helpful for representing a bitmap as an array of integers 23 | #define BitsInByte 8 24 | #define BitsInWord 32 25 | 26 | // The following class defines a "bitmap" -- an array of bits, 27 | // each of which can be independently set, cleared, and tested. 28 | // 29 | // Most useful for managing the allocation of the elements of an array -- 30 | // for instance, disk sectors, or main memory pages. 31 | // Each bit represents whether the corresponding sector or page is 32 | // in use or free. 33 | 34 | class BitMap { 35 | public: 36 | BitMap(int nitems); // Initialize a bitmap, with "nitems" bits 37 | // initially, all bits are cleared. 38 | ~BitMap(); // De-allocate bitmap 39 | 40 | void Mark(int which); // Set the "nth" bit 41 | void Clear(int which); // Clear the "nth" bit 42 | bool Test(int which); // Is the "nth" bit set? 43 | int Find(); // Return the # of a clear bit, and as a side 44 | // effect, set the bit. 45 | // If no bits are clear, return -1. 46 | int NumClear(); // Return the number of clear bits 47 | 48 | void Print(); // Print contents of bitmap 49 | 50 | // These aren't needed until FILESYS, when we will need to read and 51 | // write the bitmap to a file 52 | void FetchFrom(OpenFile *file); // fetch contents from disk 53 | void WriteBack(OpenFile *file); // write contents to disk 54 | int myFind(int cnt); 55 | private: 56 | int numBits; // number of bits in the bitmap 57 | int numWords; // number of words of bitmap storage 58 | // (rounded up if numBits is not a 59 | // multiple of the number of bits in 60 | // a word) 61 | unsigned int *map; // bit storage 62 | }; 63 | 64 | #endif // BITMAP_H 65 | -------------------------------------------------------------------------------- /bin/coff.h: -------------------------------------------------------------------------------- 1 | /* coff.h 2 | * Data structures that describe the MIPS COFF format. 3 | */ 4 | 5 | struct filehdr { 6 | unsigned short f_magic; /* magic number */ 7 | unsigned short f_nscns; /* number of sections */ 8 | long f_timdat; /* time & date stamp */ 9 | long f_symptr; /* file pointer to symbolic header */ 10 | long f_nsyms; /* sizeof(symbolic hdr) */ 11 | unsigned short f_opthdr; /* sizeof(optional hdr) */ 12 | unsigned short f_flags; /* flags */ 13 | }; 14 | 15 | #define MIPSELMAGIC 0x0162 16 | 17 | #define OMAGIC 0407 18 | #define SOMAGIC 0x0701 19 | 20 | typedef struct aouthdr { 21 | short magic; /* see above */ 22 | short vstamp; /* version stamp */ 23 | long tsize; /* text size in bytes, padded to DW bdry*/ 24 | long dsize; /* initialized data " " */ 25 | long bsize; /* uninitialized data " " */ 26 | long entry; /* entry pt. */ 27 | long text_start; /* base of text used for this file */ 28 | long data_start; /* base of data used for this file */ 29 | long bss_start; /* base of bss used for this file */ 30 | long gprmask; /* general purpose register mask */ 31 | long cprmask[4]; /* co-processor register masks */ 32 | long gp_value; /* the gp value used for this object */ 33 | } AOUTHDR; 34 | #define AOUTHSZ sizeof(AOUTHDR) 35 | 36 | 37 | struct scnhdr { 38 | char s_name[8]; /* section name */ 39 | long s_paddr; /* physical address, aliased s_nlib */ 40 | long s_vaddr; /* virtual address */ 41 | long s_size; /* section size */ 42 | long s_scnptr; /* file ptr to raw data for section */ 43 | long s_relptr; /* file ptr to relocation */ 44 | long s_lnnoptr; /* file ptr to gp histogram */ 45 | unsigned short s_nreloc; /* number of relocation entries */ 46 | unsigned short s_nlnno; /* number of gp histogram entries */ 47 | long s_flags; /* flags */ 48 | }; 49 | 50 | -------------------------------------------------------------------------------- /filesys/synchdisk.h: -------------------------------------------------------------------------------- 1 | // synchdisk.h 2 | // Data structures to export a synchronous interface to the raw 3 | // disk device. 4 | // 5 | // Copyright (c) 1992-1993 The Regents of the University of California. 6 | // All rights reserved. See copyright.h for copyright notice and limitation 7 | // of liability and disclaimer of warranty provisions. 8 | 9 | #include "copyright.h" 10 | 11 | #ifndef SYNCHDISK_H 12 | #define SYNCHDISK_H 13 | 14 | #include "disk.h" 15 | #include "synch.h" 16 | 17 | class Cache{ 18 | public: 19 | bool valid; 20 | bool dirty; 21 | int sector; 22 | int lru; 23 | char data[SectorSize]; 24 | }; 25 | // The following class defines a "synchronous" disk abstraction. 26 | // As with other I/O devices, the raw physical disk is an asynchronous device -- 27 | // requests to read or write portions of the disk return immediately, 28 | // and an interrupt occurs later to signal that the operation completed. 29 | // (Also, the physical characteristics of the disk device assume that 30 | // only one operation can be requested at a time). 31 | // 32 | // This class provides the abstraction that for any individual thread 33 | // making a request, it waits around until the operation finishes before 34 | // returning. 35 | class SynchDisk { 36 | public: 37 | SynchDisk(char* name); // Initialize a synchronous disk, 38 | // by initializing the raw Disk. 39 | ~SynchDisk(); // De-allocate the synch disk data 40 | 41 | void ReadSector(int sectorNumber, char* data); 42 | // Read/write a disk sector, returning 43 | // only once the data is actually read 44 | // or written. These call 45 | // Disk::ReadRequest/WriteRequest and 46 | // then wait until the request is done. 47 | void WriteSector(int sectorNumber, char* data); 48 | 49 | void RequestDone(); // Called by the disk device interrupt 50 | // handler, to signal that the 51 | // current disk operation is complete. 52 | void plusReader(int sector); 53 | void minusReader(int sector); 54 | void startWrite(int sector); 55 | void endWrite(int sector); 56 | int numVis[NumSectors]; 57 | int numReader[NumSectors]; 58 | private: 59 | Disk *disk; // Raw disk device 60 | Semaphore *semaphore; // To synchronize requesting thread 61 | // with the interrupt handler 62 | Lock *lock; // Only one read/write request 63 | // can be sent to the disk at a time 64 | Semaphore *mutex[NumSectors]; 65 | 66 | Lock *readerLock; 67 | Cache *cache; 68 | }; 69 | 70 | #endif // SYNCHDISK_H 71 | -------------------------------------------------------------------------------- /machine/stats.h: -------------------------------------------------------------------------------- 1 | // stats.h 2 | // Data structures for gathering statistics about Nachos performance. 3 | // 4 | // DO NOT CHANGE -- these stats are maintained by the machine emulation 5 | // 6 | // 7 | // Copyright (c) 1992-1993 The Regents of the University of California. 8 | // All rights reserved. See copyright.h for copyright notice and limitation 9 | // of liability and disclaimer of warranty provisions. 10 | 11 | #ifndef STATS_H 12 | #define STATS_H 13 | 14 | #include "copyright.h" 15 | 16 | // The following class defines the statistics that are to be kept 17 | // about Nachos behavior -- how much time (ticks) elapsed, how 18 | // many user instructions executed, etc. 19 | // 20 | // The fields in this class are public to make it easier to update. 21 | 22 | class Statistics { 23 | public: 24 | int totalTicks; // Total time running Nachos 25 | int idleTicks; // Time spent idle (no threads to run) 26 | int systemTicks; // Time spent executing system code 27 | int userTicks; // Time spent executing user code 28 | // (this is also equal to # of 29 | // user instructions executed) 30 | 31 | int numDiskReads; // number of disk read requests 32 | int numDiskWrites; // number of disk write requests 33 | int numConsoleCharsRead; // number of characters read from the keyboard 34 | int numConsoleCharsWritten; // number of characters written to the display 35 | int numPageFaults; // number of virtual memory page faults 36 | int numPacketsSent; // number of packets sent over the network 37 | int numPacketsRecvd; // number of packets received over the network 38 | 39 | Statistics(); // initialize everything to zero 40 | 41 | void Print(); // print collected statistics 42 | }; 43 | 44 | // Constants used to reflect the relative time an operation would 45 | // take in a real system. A "tick" is a just a unit of time -- if you 46 | // like, a microsecond. 47 | // 48 | // Since Nachos kernel code is directly executed, and the time spent 49 | // in the kernel measured by the number of calls to enable interrupts, 50 | // these time constants are none too exact. 51 | 52 | #define UserTick 1 // advance for each user-level instruction 53 | #define SystemTick 10 // advance each time interrupts are enabled 54 | #define RotationTime 500 // time disk takes to rotate one sector 55 | #define SeekTime 500 // time disk takes to seek past one track 56 | #define ConsoleTime 100 // time to read or write one character 57 | #define NetworkTime 100 // time to send or receive one packet 58 | #define TimerTicks 100 // (average) time between timer interrupts 59 | 60 | #endif // STATS_H 61 | -------------------------------------------------------------------------------- /threads/list.h: -------------------------------------------------------------------------------- 1 | // list.h 2 | // Data structures to manage LISP-like lists. 3 | // 4 | // As in LISP, a list can contain any type of data structure 5 | // as an item on the list: thread control blocks, 6 | // pending interrupts, etc. That is why each item is a "void *", 7 | // or in other words, a "pointers to anything". 8 | // 9 | // Copyright (c) 1992-1993 The Regents of the University of California. 10 | // All rights reserved. See copyright.h for copyright notice and limitation 11 | // of liability and disclaimer of warranty provisions. 12 | 13 | #ifndef LIST_H 14 | #define LIST_H 15 | 16 | #include "copyright.h" 17 | #include "utility.h" 18 | 19 | // The following class defines a "list element" -- which is 20 | // used to keep track of one item on a list. It is equivalent to a 21 | // LISP cell, with a "car" ("next") pointing to the next element on the list, 22 | // and a "cdr" ("item") pointing to the item on the list. 23 | // 24 | // Internal data structures kept public so that List operations can 25 | // access them directly. 26 | 27 | class ListElement { 28 | public: 29 | ListElement(void *itemPtr, int sortKey); // initialize a list element 30 | 31 | ListElement *next; // next element on list, 32 | // NULL if this is the last 33 | int key; // priority, for a sorted list 34 | void *item; // pointer to item on the list 35 | }; 36 | 37 | // The following class defines a "list" -- a singly linked list of 38 | // list elements, each of which points to a single item on the list. 39 | // 40 | // By using the "Sorted" functions, the list can be kept in sorted 41 | // in increasing order by "key" in ListElement. 42 | 43 | class List { 44 | public: 45 | List(); // initialize the list 46 | ~List(); // de-allocate the list 47 | 48 | void Prepend(void *item); // Put item at the beginning of the list 49 | void Append(void *item); // Put item at the end of the list 50 | void *Remove(); // Take item off the front of the list 51 | 52 | void Remove(void *item); // Remove specific item from list 53 | 54 | void Mapcar(VoidFunctionPtr func); // Apply "func" to every element 55 | // on the list 56 | unsigned int NumInList() { return numInList;}; 57 | 58 | bool IsEmpty(); // is the list empty? 59 | 60 | 61 | // Routines to put/get items on/off list in order (sorted by key) 62 | void SortedInsert(void *item, int sortKey); // Put item into list 63 | void *SortedRemove(int *keyPtr); // Remove first item from list 64 | 65 | private: 66 | ListElement *first; // Head of the list, NULL if list is empty 67 | ListElement *last; // Last element of list 68 | int numInList; // number of elements in list 69 | }; 70 | 71 | #endif // LIST_H 72 | -------------------------------------------------------------------------------- /network/nettest.cc: -------------------------------------------------------------------------------- 1 | // nettest.cc 2 | // Test out message delivery between two "Nachos" machines, 3 | // using the Post Office to coordinate delivery. 4 | // 5 | // Two caveats: 6 | // 1. Two copies of Nachos must be running, with machine ID's 0 and 1: 7 | // ./nachos -m 0 -o 1 & 8 | // ./nachos -m 1 -o 0 & 9 | // 10 | // 2. You need an implementation of condition variables, 11 | // which is *not* provided as part of the baseline threads 12 | // implementation. The Post Office won't work without 13 | // a correct implementation of condition variables. 14 | // 15 | // Copyright (c) 1992-1993 The Regents of the University of California. 16 | // All rights reserved. See copyright.h for copyright notice and limitation 17 | // of liability and disclaimer of warranty provisions. 18 | 19 | #include "copyright.h" 20 | 21 | #include "system.h" 22 | #include "network.h" 23 | #include "post.h" 24 | #include "interrupt.h" 25 | 26 | // Test out message delivery, by doing the following: 27 | // 1. send a message to the machine with ID "farAddr", at mail box #0 28 | // 2. wait for the other machine's message to arrive (in our mailbox #0) 29 | // 3. send an acknowledgment for the other machine's message 30 | // 4. wait for an acknowledgement from the other machine to our 31 | // original message 32 | 33 | void 34 | MailTest(int farAddr) 35 | { 36 | PacketHeader outPktHdr, inPktHdr; 37 | MailHeader outMailHdr, inMailHdr; 38 | char *data = "Hello there!"; 39 | char *ack = "Got it!"; 40 | char buffer[MaxMailSize]; 41 | 42 | // construct packet, mail header for original message 43 | // To: destination machine, mailbox 0 44 | // From: our machine, reply to: mailbox 1 45 | outPktHdr.to = farAddr; 46 | outMailHdr.to = 0; 47 | outMailHdr.from = 1; 48 | outMailHdr.length = strlen(data) + 1; 49 | 50 | // Send the first message 51 | postOffice->Send(outPktHdr, outMailHdr, data); 52 | 53 | // Wait for the first message from the other machine 54 | postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer); 55 | printf("Got \"%s\" from %d, box %d\n",buffer,inPktHdr.from,inMailHdr.from); 56 | fflush(stdout); 57 | 58 | // Send acknowledgement to the other machine (using "reply to" mailbox 59 | // in the message that just arrived 60 | outPktHdr.to = inPktHdr.from; 61 | outMailHdr.to = inMailHdr.from; 62 | outMailHdr.length = strlen(ack) + 1; 63 | postOffice->Send(outPktHdr, outMailHdr, ack); 64 | 65 | // Wait for the ack from the other machine to the first message we sent. 66 | postOffice->Receive(1, &inPktHdr, &inMailHdr, buffer); 67 | printf("Got \"%s\" from %d, box %d\n",buffer,inPktHdr.from,inMailHdr.from); 68 | fflush(stdout); 69 | 70 | // Then we're done! 71 | interrupt->Halt(); 72 | } 73 | -------------------------------------------------------------------------------- /machine/sysdep.h: -------------------------------------------------------------------------------- 1 | // sysdep.h 2 | // System-dependent interface. Nachos uses the routines defined 3 | // here, rather than directly calling the UNIX library functions, to 4 | // simplify porting between versions of UNIX, and even to 5 | // other systems, such as MSDOS and the Macintosh. 6 | // 7 | // Copyright (c) 1992-1993 The Regents of the University of California. 8 | // All rights reserved. See copyright.h for copyright notice and limitation 9 | // of liability and disclaimer of warranty provisions. 10 | 11 | #ifndef SYSDEP_H 12 | #define SYSDEP_H 13 | 14 | #include "copyright.h" 15 | 16 | // Check file to see if there are any characters to be read. 17 | // If no characters in the file, return without waiting. 18 | extern bool PollFile(int fd); 19 | 20 | // File operations: open/read/write/lseek/close, and check for error 21 | // For simulating the disk and the console devices. 22 | extern int OpenForWrite(char *name); 23 | extern int OpenForReadWrite(char *name, bool crashOnError); 24 | extern void Read(int fd, char *buffer, int nBytes); 25 | extern int ReadPartial(int fd, char *buffer, int nBytes); 26 | extern void WriteFile(int fd, char *buffer, int nBytes); 27 | extern void Lseek(int fd, int offset, int whence); 28 | extern int Tell(int fd); 29 | extern void Close(int fd); 30 | extern bool Unlink(char *name); 31 | 32 | // Interprocess communication operations, for simulating the network 33 | extern int OpenSocket(); 34 | extern void CloseSocket(int sockID); 35 | extern void AssignNameToSocket(char *socketName, int sockID); 36 | extern void DeAssignNameToSocket(char *socketName); 37 | extern bool PollSocket(int sockID); 38 | extern void ReadFromSocket(int sockID, char *buffer, int packetSize); 39 | extern void SendToSocket(int sockID, char *buffer, int packetSize,char *toName); 40 | 41 | // Process control: abort, exit, and sleep 42 | extern void Abort(); 43 | extern void Exit(int exitCode); 44 | extern void Delay(int seconds); 45 | 46 | // Initialize system so that cleanUp routine is called when user hits ctl-C 47 | extern void CallOnUserAbort(VoidNoArgFunctionPtr cleanUp); 48 | 49 | // Initialize the pseudo random number generator 50 | extern void RandomInit(unsigned seed); 51 | extern int Random(); 52 | 53 | // Allocate, de-allocate an array, such that de-referencing 54 | // just beyond either end of the array will cause an error 55 | extern char *AllocBoundedArray(int size); 56 | extern void DeallocBoundedArray(char *p, int size); 57 | 58 | // Other C library routines that are used by Nachos. 59 | // These are assumed to be portable, so we don't include a wrapper. 60 | extern "C" { 61 | int atoi(const char *str); 62 | double atof(const char *str); 63 | int abs(int i); 64 | 65 | #include // for printf, fprintf 66 | #include // for DEBUG, etc. 67 | //#include 68 | } 69 | 70 | #endif // SYSDEP_H 71 | -------------------------------------------------------------------------------- /filesys/filehdr.h: -------------------------------------------------------------------------------- 1 | // filehdr.h 2 | // Data structures for managing a disk file header. 3 | // 4 | // A file header describes where on disk to find the data in a file, 5 | // along with other information about the file (for instance, its 6 | // length, owner, etc.) 7 | // 8 | // Copyright (c) 1992-1993 The Regents of the University of California. 9 | // All rights reserved. See copyright.h for copyright notice and limitation 10 | // of liability and disclaimer of warranty provisions. 11 | 12 | #include "copyright.h" 13 | 14 | #ifndef FILEHDR_H 15 | #define FILEHDR_H 16 | 17 | #include "disk.h" 18 | #include "bitmap.h" 19 | 20 | #define SecondIndex 5 21 | #define NumDirect ((SectorSize - (2+SecondIndex) * sizeof(int) - (3*25)*sizeof(char)) / sizeof(int)) 22 | #define SecondDirect SectorSize/sizeof(int) 23 | 24 | #define totalFirst NumDirect+SecondIndex 25 | #define totalDirect NumDirect + SecondIndex*SecondDirect 26 | 27 | #define MaxFileSize (totalDirect * SectorSize) 28 | 29 | // The following class defines the Nachos "file header" (in UNIX terms, 30 | // the "i-node"), describing where on disk to find all of the data in the file. 31 | // The file header is organized as a simple table of pointers to 32 | // data blocks. 33 | // 34 | // The file header data structure can be stored in memory or on disk. 35 | // When it is on disk, it is stored in a single sector -- this means 36 | // that we assume the size of this data structure to be the same 37 | // as one disk sector. Without indirect addressing, this 38 | // limits the maximum file length to just under 4K bytes. 39 | // 40 | // There is no constructor; rather the file header can be initialized 41 | // by allocating blocks for the file (if it is a new file), or by 42 | // reading it from disk. 43 | 44 | class FileHeader { 45 | public: 46 | bool Allocate(BitMap *bitMap, int fileSize);// Initialize a file header, 47 | // including allocating space 48 | // on disk for the file data 49 | void Deallocate(BitMap *bitMap); // De-allocate this file's 50 | // data blocks 51 | 52 | void FetchFrom(int sectorNumber); // Initialize file header from disk 53 | void WriteBack(int sectorNumber); // Write modifications to file header 54 | // back to disk 55 | 56 | int ByteToSector(int offset); // Convert a byte offset into the file 57 | // to the disk sector containing 58 | // the byte 59 | 60 | int FileLength(); // Return the length of the file 61 | // in bytes 62 | 63 | void Print(); // Print the contents of the file. 64 | 65 | char createTime[25]; 66 | char lastReadTime[25]; 67 | char lastWriteTime[25]; 68 | 69 | void setCreateTime(); 70 | void setLastReadTime(); 71 | void setLastWriteTime(); 72 | bool extendLen(BitMap *bitMap,int fileSize); 73 | int numBytes; 74 | private: 75 | // int numBytes; // Number of bytes in the file 76 | int numSectors; // Number of data sectors in the file 77 | int dataSectors[11]; // Disk sector numbers for each data 78 | // block in the file 79 | }; 80 | 81 | #endif // FILEHDR_H 82 | -------------------------------------------------------------------------------- /threads/synchlist.cc: -------------------------------------------------------------------------------- 1 | // synchlist.cc 2 | // Routines for synchronized access to a list. 3 | // 4 | // Implemented by surrounding the List abstraction 5 | // with synchronization routines. 6 | // 7 | // Implemented in "monitor"-style -- surround each procedure with a 8 | // lock acquire and release pair, using condition signal and wait for 9 | // synchronization. 10 | // 11 | // Copyright (c) 1992-1993 The Regents of the University of California. 12 | // All rights reserved. See copyright.h for copyright notice and limitation 13 | // of liability and disclaimer of warranty provisions. 14 | 15 | #include "copyright.h" 16 | #include "synchlist.h" 17 | 18 | //---------------------------------------------------------------------- 19 | // SynchList::SynchList 20 | // Allocate and initialize the data structures needed for a 21 | // synchronized list, empty to start with. 22 | // Elements can now be added to the list. 23 | //---------------------------------------------------------------------- 24 | 25 | SynchList::SynchList() 26 | { 27 | list = new List(); 28 | lock = new Lock("list lock"); 29 | listEmpty = new Condition("list empty cond"); 30 | } 31 | 32 | //---------------------------------------------------------------------- 33 | // SynchList::~SynchList 34 | // De-allocate the data structures created for synchronizing a list. 35 | //---------------------------------------------------------------------- 36 | 37 | SynchList::~SynchList() 38 | { 39 | delete list; 40 | delete lock; 41 | delete listEmpty; 42 | } 43 | 44 | //---------------------------------------------------------------------- 45 | // SynchList::Append 46 | // Append an "item" to the end of the list. Wake up anyone 47 | // waiting for an element to be appended. 48 | // 49 | // "item" is the thing to put on the list, it can be a pointer to 50 | // anything. 51 | //---------------------------------------------------------------------- 52 | 53 | void 54 | SynchList::Append(void *item) 55 | { 56 | lock->Acquire(); // enforce mutual exclusive access to the list 57 | list->Append(item); 58 | listEmpty->Signal(lock); // wake up a waiter, if any 59 | lock->Release(); 60 | } 61 | 62 | //---------------------------------------------------------------------- 63 | // SynchList::Remove 64 | // Remove an "item" from the beginning of the list. Wait if 65 | // the list is empty. 66 | // Returns: 67 | // The removed item. 68 | //---------------------------------------------------------------------- 69 | 70 | void * 71 | SynchList::Remove() 72 | { 73 | void *item; 74 | 75 | lock->Acquire(); // enforce mutual exclusion 76 | while (list->IsEmpty()) 77 | listEmpty->Wait(lock); // wait until list isn't empty 78 | item = list->Remove(); 79 | ASSERT(item != NULL); 80 | lock->Release(); 81 | return item; 82 | } 83 | 84 | //---------------------------------------------------------------------- 85 | // SynchList::Mapcar 86 | // Apply function to every item on the list. Obey mutual exclusion 87 | // constraints. 88 | // 89 | // "func" is the procedure to be applied. 90 | //---------------------------------------------------------------------- 91 | 92 | void 93 | SynchList::Mapcar(VoidFunctionPtr func) 94 | { 95 | lock->Acquire(); 96 | list->Mapcar(func); 97 | lock->Release(); 98 | } 99 | -------------------------------------------------------------------------------- /threads/utility.h: -------------------------------------------------------------------------------- 1 | // utility.h 2 | // Miscellaneous useful definitions, including debugging routines. 3 | // 4 | // The debugging routines allow the user to turn on selected 5 | // debugging messages, controllable from the command line arguments 6 | // passed to Nachos (-d). You are encouraged to add your own 7 | // debugging flags. The pre-defined debugging flags are: 8 | // 9 | // '+' -- turn on all debug messages 10 | // 't' -- thread system 11 | // 's' -- semaphores, locks, and conditions 12 | // 'i' -- interrupt emulation 13 | // 'm' -- machine emulation (USER_PROGRAM) 14 | // 'd' -- disk emulation (FILESYS) 15 | // 'f' -- file system (FILESYS) 16 | // 'a' -- address spaces (USER_PROGRAM) 17 | // 'n' -- network emulation (NETWORK) 18 | // 19 | // Copyright (c) 1992-1993 The Regents of the University of California. 20 | // All rights reserved. See copyright.h for copyright notice and limitation 21 | // of liability and disclaimer of warranty provisions. 22 | 23 | #ifndef UTILITY_H 24 | #define UTILITY_H 25 | 26 | #include "copyright.h" 27 | 28 | // Miscellaneous useful routines 29 | 30 | #include "bool.h" 31 | // Boolean values. 32 | // This is the same definition 33 | // as in the g++ library. 34 | 35 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 36 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 37 | 38 | // Divide and either round up or down 39 | #define divRoundDown(n,s) ((n) / (s)) 40 | #define divRoundUp(n,s) (((n) / (s)) + ((((n) % (s)) > 0) ? 1 : 0)) 41 | 42 | // This declares the type "VoidFunctionPtr" to be a "pointer to a 43 | // function taking an integer argument and returning nothing". With 44 | // such a function pointer (say it is "func"), we can call it like this: 45 | // 46 | // (*func) (17); 47 | // 48 | // This is used by Thread::Fork and for interrupt handlers, as well 49 | // as a couple of other places. 50 | 51 | typedef void (*VoidFunctionPtr)(int arg); 52 | typedef void (*VoidNoArgFunctionPtr)(); 53 | 54 | 55 | // Include interface that isolates us from the host machine system library. 56 | // Requires definition of bool, and VoidFunctionPtr 57 | #include "sysdep.h" 58 | 59 | // Interface to debugging routines. 60 | 61 | extern void DebugInit(char* flags); // enable printing debug messages 62 | 63 | extern bool DebugIsEnabled(char flag); // Is this debug flag enabled? 64 | 65 | extern void DEBUG (char flag, char* format, ...); // Print debug message 66 | // if flag is enabled 67 | 68 | //---------------------------------------------------------------------- 69 | // ASSERT 70 | // If condition is false, print a message and dump core. 71 | // Useful for documenting assumptions in the code. 72 | // 73 | // NOTE: needs to be a #define, to be able to print the location 74 | // where the error occurred. 75 | //---------------------------------------------------------------------- 76 | #define ASSERT(condition) \ 77 | if (!(condition)) { \ 78 | fprintf(stderr, "Assertion failed: line %d, file \"%s\"\n", \ 79 | __LINE__, __FILE__); \ 80 | fflush(stderr); \ 81 | Abort(); \ 82 | } 83 | 84 | 85 | #endif // UTILITY_H 86 | -------------------------------------------------------------------------------- /machine/timer.cc: -------------------------------------------------------------------------------- 1 | // timer.cc 2 | // Routines to emulate a hardware timer device. 3 | // 4 | // A hardware timer generates a CPU interrupt every X milliseconds. 5 | // This means it can be used for implementing time-slicing. 6 | // 7 | // We emulate a hardware timer by scheduling an interrupt to occur 8 | // every time stats->totalTicks has increased by TimerTicks. 9 | // 10 | // In order to introduce some randomness into time-slicing, if "doRandom" 11 | // is set, then the interrupt is comes after a random number of ticks. 12 | // 13 | // Remember -- nothing in here is part of Nachos. It is just 14 | // an emulation for the hardware that Nachos is running on top of. 15 | // 16 | // DO NOT CHANGE -- part of the machine emulation 17 | // 18 | // Copyright (c) 1992-1993 The Regents of the University of California. 19 | // All rights reserved. See copyright.h for copyright notice and limitation 20 | // of liability and disclaimer of warranty provisions. 21 | 22 | #include "copyright.h" 23 | #include "timer.h" 24 | #include "system.h" 25 | 26 | // dummy function because C++ does not allow pointers to member functions 27 | static void TimerHandler(int arg) 28 | { Timer *p = (Timer *)arg; p->TimerExpired(); } 29 | 30 | //---------------------------------------------------------------------- 31 | // Timer::Timer 32 | // Initialize a hardware timer device. Save the place to call 33 | // on each interrupt, and then arrange for the timer to start 34 | // generating interrupts. 35 | // 36 | // "timerHandler" is the interrupt handler for the timer device. 37 | // It is called with interrupts disabled every time the 38 | // the timer expires. 39 | // "callArg" is the parameter to be passed to the interrupt handler. 40 | // "doRandom" -- if true, arrange for the interrupts to occur 41 | // at random, instead of fixed, intervals. 42 | //---------------------------------------------------------------------- 43 | 44 | Timer::Timer(VoidFunctionPtr timerHandler, int callArg, bool doRandom) 45 | { 46 | randomize = doRandom; 47 | handler = timerHandler; 48 | arg = callArg; 49 | 50 | // schedule the first interrupt from the timer device 51 | interrupt->Schedule(TimerHandler, (int) this, TimeOfNextInterrupt(), 52 | TimerInt); 53 | } 54 | 55 | //---------------------------------------------------------------------- 56 | // Timer::TimerExpired 57 | // Routine to simulate the interrupt generated by the hardware 58 | // timer device. Schedule the next interrupt, and invoke the 59 | // interrupt handler. 60 | //---------------------------------------------------------------------- 61 | void 62 | Timer::TimerExpired() 63 | { 64 | // schedule the next timer device interrupt 65 | interrupt->Schedule(TimerHandler, (int) this, TimeOfNextInterrupt(), 66 | TimerInt); 67 | 68 | // invoke the Nachos interrupt handler for this device 69 | (*handler)(arg); 70 | } 71 | 72 | //---------------------------------------------------------------------- 73 | // Timer::TimeOfNextInterrupt 74 | // Return when the hardware timer device will next cause an interrupt. 75 | // If randomize is turned on, make it a (pseudo-)random delay. 76 | //---------------------------------------------------------------------- 77 | 78 | int 79 | Timer::TimeOfNextInterrupt() 80 | { 81 | if (randomize) 82 | return 1 + (Random() % (TimerTicks * 2)); 83 | else 84 | return TimerTicks; 85 | } 86 | -------------------------------------------------------------------------------- /filesys/directory.h: -------------------------------------------------------------------------------- 1 | // directory.h 2 | // Data structures to manage a UNIX-like directory of file names. 3 | // 4 | // A directory is a table of pairs: , 5 | // giving the name of each file in the directory, and 6 | // where to find its file header (the data structure describing 7 | // where to find the file's data blocks) on disk. 8 | // 9 | // We assume mutual exclusion is provided by the caller. 10 | // 11 | // Copyright (c) 1992-1993 The Regents of the University of California. 12 | // All rights reserved. See copyright.h for copyright notice and limitation 13 | // of liability and disclaimer of warranty provisions. 14 | 15 | #include "copyright.h" 16 | 17 | #ifndef DIRECTORY_H 18 | #define DIRECTORY_H 19 | 20 | #include "openfile.h" 21 | 22 | #define FileNameMaxLen 20 // for simplicity, we assume 23 | // file names are <= 9 characters long 24 | #define DirType 0 25 | #define FileType 1 26 | // The following class defines a "directory entry", representing a file 27 | // in the directory. Each entry gives the name of the file, and where 28 | // the file's header is to be found on disk. 29 | // 30 | // Internal data structures kept public so that Directory operations can 31 | // access them directly. 32 | 33 | class DirectoryEntry { 34 | public: 35 | bool inUse; // Is this directory entry in use? 36 | int sector; // Location on disk to find the 37 | char *name; 38 | 39 | int fileType; 40 | int fileSize; 41 | char *fname; 42 | // char name[FileNameMaxLen + 1]; 43 | 44 | }; 45 | 46 | // The following class defines a UNIX-like "directory". Each entry in 47 | // the directory describes a file, and where to find it on disk. 48 | // 49 | // The directory data structure can be stored in memory, or on disk. 50 | // When it is on disk, it is stored as a regular Nachos file. 51 | // 52 | // The constructor initializes a directory structure in memory; the 53 | // FetchFrom/WriteBack operations shuffle the directory information 54 | // from/to disk. 55 | 56 | class Directory { 57 | public: 58 | Directory(int size); // Initialize an empty directory 59 | // with space for "size" files 60 | ~Directory(); // De-allocate the directory 61 | 62 | void FetchFrom(OpenFile *file); // Init directory contents from disk 63 | void WriteBack(OpenFile *file); // Write modifications to 64 | // directory contents back to disk 65 | 66 | int Find(char *name); // Find the sector number of the 67 | // FileHeader for file: "name" 68 | 69 | bool Add(char *name, int newSector); // Add a file name into the directory 70 | 71 | bool Remove(char *name); // Remove a file from the directory 72 | 73 | void List(); // Print the names of all the files 74 | // in the directory 75 | void Print(); // Verbose print of the contents 76 | // of the directory -- all the file 77 | // names and their contents. 78 | // int getCDSector(char *dirPath); 79 | // bool AddDirectory(char *absPath,char *dirPath,int newSector); 80 | void recordName(char *name); 81 | void getPath(char *path); 82 | char* printName(); 83 | void listFile(char *filename); 84 | private: 85 | int tableSize; // Number of directory entries 86 | DirectoryEntry *table; // Table of pairs: 87 | // 88 | 89 | int FindIndex(char *name); // Find the index into the directory 90 | // table corresponding to "name" 91 | }; 92 | 93 | #endif // DIRECTORY_H 94 | -------------------------------------------------------------------------------- /bin/system.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | #include 9 | #include 10 | 11 | #include "int.h" 12 | 13 | extern int Reg[]; 14 | extern char mem[]; 15 | extern int Traptrace; 16 | 17 | char *u_to_int_addr(); 18 | 19 | /* handle system calls */ 20 | system_break() 21 | { 22 | if ( Traptrace ) 23 | printf("**breakpoint "); 24 | system_trap(); 25 | } 26 | 27 | system_trap() 28 | { 29 | int o0, o1, o2; /* user out register values */ 30 | int syscallno; 31 | extern long lseek(); 32 | 33 | if ( Traptrace ) 34 | { 35 | printf("**System call %d\n", Reg[2]); 36 | dump_reg(); 37 | } 38 | 39 | /* if (Reg[1] == 0) 40 | /* { /* SYS_indir */ 41 | /* syscallno = Reg[8]; /* out reg 0 */ 42 | /* o0 = Reg[9]; 43 | /* o1 = Reg[10]; 44 | /* o2 = Reg[11]; 45 | /* } 46 | /* else /* */ 47 | { 48 | syscallno = Reg[2]; 49 | o0 = Reg[4]; 50 | o1 = Reg[5]; 51 | o2 = Reg[6]; 52 | } 53 | 54 | switch (syscallno) 55 | { 56 | case SYS_exit: /*1*/ 57 | printstatistics(); 58 | fflush(stdout); 59 | exit(0); 60 | break; 61 | case SYS_read: /*3*/ 62 | Reg[1] = 63 | read(u_to_int_fd(o0), u_to_int_addr(o1), o2); 64 | break; 65 | case SYS_write: /*4*/ 66 | Reg[1] = 67 | write(u_to_int_fd(o0), u_to_int_addr(o1), o2); 68 | break; 69 | 70 | case SYS_open: /*5*/ 71 | Reg[1] = open(u_to_int_addr(o0), o1, o2); /* */ 72 | break; 73 | 74 | case SYS_close: /*6*/ 75 | Reg[1] = 0; /* hack */ 76 | break; 77 | 78 | case 17: /* 17 */ 79 | /* old sbreak. where did it go? */ 80 | Reg[1] = ((o0 / 8192) + 1) * 8192; 81 | break; 82 | 83 | case SYS_lseek: /*19*/ 84 | Reg[1] = (int) lseek(u_to_int_fd(o0), (long) o1, o2); 85 | break; 86 | 87 | case SYS_ioctl:/* 54 */ 88 | { /* copied from sas -- I don't understand yet. */ 89 | /* see dave weaver */ 90 | #define IOCPARM_MASK 0x7f /* parameters must be < 128 bytes */ 91 | int size = (o1 >> 16) & IOCPARM_MASK; 92 | char ioctl_group = (o1 >> 8) & 0x00ff; 93 | if ((ioctl_group == 't') && (size == 8)) 94 | { 95 | size = 6; 96 | o1 = (o1 & ~((IOCPARM_MASK << 16))) 97 | | (size << 16); 98 | } 99 | } 100 | Reg[1] = ioctl(u_to_int_fd(o0),o1,u_to_int_addr(o2)); 101 | Reg[1] = 0; /* hack */ 102 | break; 103 | 104 | case SYS_fstat: /* 62 */ 105 | Reg[1] = fstat(o1, o2); 106 | break; 107 | 108 | case SYS_getpagesize: /* 64 */ 109 | Reg[1] = getpagesize(); 110 | break; 111 | 112 | default: 113 | printf("Unknown System call %d\n", syscallno); 114 | if ( ! Traptrace ) 115 | dump_reg(); 116 | exit(2); 117 | break; 118 | } 119 | if ( Traptrace ) 120 | { 121 | printf("**Afterwards:\n"); 122 | dump_reg(); 123 | } 124 | } 125 | 126 | char *u_to_int_addr(ptr) 127 | int ptr; 128 | { /* convert a user pointer to the real address */ 129 | /* used in the interpreter */ 130 | 131 | return ((char *) ((int) mem - memoffset + ptr)); 132 | } 133 | 134 | u_to_int_fd(fd) 135 | { 136 | if (fd > 2) 137 | { 138 | /* 139 | printf("No general file descriptors yet\n"); 140 | exit(2); 141 | */ 142 | } 143 | return (fd); /* assume we can handle it for now */ 144 | } 145 | -------------------------------------------------------------------------------- /filesys/openfile.h: -------------------------------------------------------------------------------- 1 | // openfile.h 2 | // Data structures for opening, closing, reading and writing to 3 | // individual files. The operations supported are similar to 4 | // the UNIX ones -- type 'man open' to the UNIX prompt. 5 | // 6 | // There are two implementations. One is a "STUB" that directly 7 | // turns the file operations into the underlying UNIX operations. 8 | // (cf. comment in filesys.h). 9 | // 10 | // The other is the "real" implementation, that turns these 11 | // operations into read and write disk sector requests. 12 | // In this baseline implementation of the file system, we don't 13 | // worry about concurrent accesses to the file system 14 | // by different threads -- this is part of the assignment. 15 | // 16 | // Copyright (c) 1992-1993 The Regents of the University of California. 17 | // All rights reserved. See copyright.h for copyright notice and limitation 18 | // of liability and disclaimer of warranty provisions. 19 | 20 | #ifndef OPENFILE_H 21 | #define OPENFILE_H 22 | 23 | #include "copyright.h" 24 | #include "utility.h" 25 | 26 | #ifdef FILESYS_STUB // Temporarily implement calls to 27 | // Nachos file system as calls to UNIX! 28 | // See definitions listed under #else 29 | class OpenFile { 30 | public: 31 | OpenFile(int f) { file = f; currentOffset = 0; } // open the file 32 | ~OpenFile() { Close(file); } // close the file 33 | 34 | int ReadAt(char *into, int numBytes, int position) { 35 | Lseek(file, position, 0); 36 | return ReadPartial(file, into, numBytes); 37 | } 38 | int WriteAt(char *from, int numBytes, int position) { 39 | Lseek(file, position, 0); 40 | WriteFile(file, from, numBytes); 41 | return numBytes; 42 | } 43 | int Read(char *into, int numBytes) { 44 | int numRead = ReadAt(into, numBytes, currentOffset); 45 | currentOffset += numRead; 46 | return numRead; 47 | } 48 | int Write(char *from, int numBytes) { 49 | int numWritten = WriteAt(from, numBytes, currentOffset); 50 | currentOffset += numWritten; 51 | return numWritten; 52 | } 53 | 54 | int Length() { Lseek(file, 0, 2); return Tell(file); } 55 | 56 | private: 57 | int file; 58 | int currentOffset; 59 | }; 60 | 61 | #else // FILESYS 62 | class FileHeader; 63 | 64 | class OpenFile { 65 | public: 66 | OpenFile(int sector); // Open a file whose header is located 67 | // at "sector" on the disk 68 | ~OpenFile(); // Close the file 69 | 70 | void Seek(int position); // Set the position from which to 71 | // start reading/writing -- UNIX lseek 72 | 73 | int Read(char *into, int numBytes); // Read/write bytes from the file, 74 | // starting at the implicit position. 75 | // Return the # actually read/written, 76 | // and increment position in file. 77 | int Write(char *from, int numBytes); 78 | 79 | int ReadAt(char *into, int numBytes, int position); 80 | // Read/write bytes from the file, 81 | // bypassing the implicit position. 82 | int WriteAt(char *from, int numBytes, int position); 83 | 84 | int Length(); // Return the number of bytes in the 85 | // file (this interface is simpler 86 | // than the UNIX idiom -- lseek to 87 | // end of file, tell, lseek back 88 | int theSector; 89 | private: 90 | FileHeader *hdr; // Header for this file 91 | int seekPosition; // Current position within the file 92 | }; 93 | 94 | #endif // FILESYS 95 | 96 | #endif // OPENFILE_H 97 | -------------------------------------------------------------------------------- /machine/console.h: -------------------------------------------------------------------------------- 1 | // console.h 2 | // Data structures to simulate the behavior of a terminal 3 | // I/O device. A terminal has two parts -- a keyboard input, 4 | // and a display output, each of which produces/accepts 5 | // characters sequentially. 6 | // 7 | // The console hardware device is asynchronous. When a character is 8 | // written to the device, the routine returns immediately, and an 9 | // interrupt handler is called later when the I/O completes. 10 | // For reads, an interrupt handler is called when a character arrives. 11 | // 12 | // The user of the device can specify the routines to be called when 13 | // the read/write interrupts occur. There is a separate interrupt 14 | // for read and write, and the device is "duplex" -- a character 15 | // can be outgoing and incoming at the same time. 16 | // 17 | // DO NOT CHANGE -- part of the machine emulation 18 | // 19 | // Copyright (c) 1992-1993 The Regents of the University of California. 20 | // All rights reserved. See copyright.h for copyright notice and limitation 21 | // of liability and disclaimer of warranty provisions. 22 | 23 | #ifndef CONSOLE_H 24 | #define CONSOLE_H 25 | 26 | #include "copyright.h" 27 | #include "utility.h" 28 | #include "synch.h" 29 | // The following class defines a hardware console device. 30 | // Input and output to the device is simulated by reading 31 | // and writing to UNIX files ("readFile" and "writeFile"). 32 | // 33 | // Since the device is asynchronous, the interrupt handler "readAvail" 34 | // is called when a character has arrived, ready to be read in. 35 | // The interrupt handler "writeDone" is called when an output character 36 | // has been "put", so that the next character can be written. 37 | 38 | class Console { 39 | public: 40 | Console(char *readFile, char *writeFile, VoidFunctionPtr readAvail, 41 | VoidFunctionPtr writeDone, int callArg); 42 | // initialize the hardware console device 43 | ~Console(); // clean up console emulation 44 | 45 | // external interface -- Nachos kernel code can call these 46 | void putChar(char ch); // Write "ch" to the console display, 47 | // and return immediately. "writeHandler" 48 | // is called when the I/O completes. 49 | 50 | char getChar(); // Poll the console input. If a char is 51 | // available, return it. Otherwise, return EOF. 52 | // "readHandler" is called whenever there is 53 | // a char to be gotten 54 | 55 | // internal emulation routines -- DO NOT call these. 56 | void WriteDone(); // internal routines to signal I/O completion 57 | void CheckCharAvail(); 58 | 59 | private: 60 | int readFileNo; // UNIX file emulating the keyboard 61 | int writeFileNo; // UNIX file emulating the display 62 | VoidFunctionPtr writeHandler; // Interrupt handler to call when 63 | // the PutChar I/O completes 64 | VoidFunctionPtr readHandler; // Interrupt handler to call when 65 | // a character arrives from the keyboard 66 | int handlerArg; // argument to be passed to the 67 | // interrupt handlers 68 | bool putBusy; // Is a PutChar operation in progress? 69 | // If so, you can't do another one! 70 | char incoming; // Contains the character to be read, 71 | // if there is one available. 72 | // Otherwise contains EOF. 73 | }; 74 | 75 | 76 | class SynchConsole{ 77 | public: 78 | SynchConsole(char *readFile, char *writeFile); 79 | ~SynchConsole(); 80 | char getChar(); 81 | void putChar(char ch); 82 | private: 83 | Console *console; 84 | Lock *readLock; 85 | Lock *writeLock; 86 | }; 87 | #endif // CONSOLE_H 88 | -------------------------------------------------------------------------------- /test/start.c: -------------------------------------------------------------------------------- 1 | /* Start.s 2 | * Assembly language assist for user programs running on top of Nachos. 3 | * 4 | * Since we don't want to pull in the entire C library, we define 5 | * what we need for a user program here, namely Start and the system 6 | * calls. 7 | */ 8 | 9 | #define IN_ASM 10 | #include "syscall.h" 11 | 12 | .text 13 | .align 2 14 | 15 | /* ------------------------------------------------------------- 16 | * __start 17 | * Initialize running a C program, by calling "main". 18 | * 19 | * NOTE: This has to be first, so that it gets loaded at location 0. 20 | * The Nachos kernel always starts a program by jumping to location 0. 21 | * ------------------------------------------------------------- 22 | */ 23 | 24 | .globl __start 25 | .ent __start 26 | __start: 27 | jal main 28 | move $4,$0 29 | jal Exit /* if we return from main, exit(0) */ 30 | .end __start 31 | 32 | /* ------------------------------------------------------------- 33 | * System call stubs: 34 | * Assembly language assist to make system calls to the Nachos kernel. 35 | * There is one stub per system call, that places the code for the 36 | * system call into register r2, and leaves the arguments to the 37 | * system call alone (in other words, arg1 is in r4, arg2 is 38 | * in r5, arg3 is in r6, arg4 is in r7) 39 | * 40 | * The return value is in r2. This follows the standard C calling 41 | * convention on the MIPS. 42 | * ------------------------------------------------------------- 43 | */ 44 | 45 | .globl Halt 46 | .ent Halt 47 | Halt: 48 | addiu $2,$0,SC_Halt 49 | syscall 50 | j $31 51 | .end Halt 52 | 53 | .globl Exit 54 | .ent Exit 55 | Exit: 56 | addiu $2,$0,SC_Exit 57 | syscall 58 | j $31 59 | .end Exit 60 | 61 | .globl Exec 62 | .ent Exec 63 | Exec: 64 | addiu $2,$0,SC_Exec 65 | syscall 66 | j $31 67 | .end Exec 68 | 69 | .globl Join 70 | .ent Join 71 | Join: 72 | addiu $2,$0,SC_Join 73 | syscall 74 | j $31 75 | .end Join 76 | 77 | .globl Create 78 | .ent Create 79 | Create: 80 | addiu $2,$0,SC_Create 81 | syscall 82 | j $31 83 | .end Create 84 | 85 | .globl Open 86 | .ent Open 87 | Open: 88 | addiu $2,$0,SC_Open 89 | syscall 90 | j $31 91 | .end Open 92 | 93 | .globl Read 94 | .ent Read 95 | Read: 96 | addiu $2,$0,SC_Read 97 | syscall 98 | j $31 99 | .end Read 100 | 101 | .globl Write 102 | .ent Write 103 | Write: 104 | addiu $2,$0,SC_Write 105 | syscall 106 | j $31 107 | .end Write 108 | 109 | .globl Close 110 | .ent Close 111 | Close: 112 | addiu $2,$0,SC_Close 113 | syscall 114 | j $31 115 | .end Close 116 | 117 | .globl Fork 118 | .ent Fork 119 | Fork: 120 | addiu $2,$0,SC_Fork 121 | syscall 122 | j $31 123 | .end Fork 124 | 125 | .globl Yield 126 | .ent Yield 127 | Yield: 128 | addiu $2,$0,SC_Yield 129 | syscall 130 | j $31 131 | .end Yield 132 | 133 | .globl Pwd 134 | .ent Pwd 135 | Pwd: 136 | addiu $2,$0,SC_Pwd 137 | syscall 138 | j $31 139 | .end Pwd 140 | 141 | .globl Ls 142 | .ent Ls 143 | Ls: 144 | addiu $2,$0,SC_Ls 145 | syscall 146 | j $31 147 | .end Ls 148 | 149 | .globl Cd 150 | .ent Cd 151 | Cd: 152 | addiu $2,$0,SC_Cd 153 | syscall 154 | j $31 155 | .end Cd 156 | 157 | .globl Remove 158 | .ent Remove 159 | Remove: 160 | addiu $2,$0,SC_Remove 161 | syscall 162 | j $31 163 | .end Remove 164 | 165 | .globl CDir 166 | .ent CDir 167 | CDir: 168 | addiu $2,$0,SC_CDir 169 | syscall 170 | j $31 171 | .end CDir 172 | 173 | .globl RDir 174 | .ent RDir 175 | RDir: 176 | addiu $2,$0,SC_RDir 177 | syscall 178 | j $31 179 | .end RDir 180 | 181 | .globl Help 182 | .ent Help 183 | Help: 184 | addiu $2,$0,SC_Help 185 | syscall 186 | j $31 187 | .end Help 188 | 189 | /* dummy function to keep gcc happy */ 190 | .globl __main 191 | .ent __main 192 | __main: 193 | j $31 194 | .end __main 195 | 196 | -------------------------------------------------------------------------------- /test/start.s: -------------------------------------------------------------------------------- 1 | /* Start.s 2 | * Assembly language assist for user programs running on top of Nachos. 3 | * 4 | * Since we don't want to pull in the entire C library, we define 5 | * what we need for a user program here, namely Start and the system 6 | * calls. 7 | */ 8 | 9 | #define IN_ASM 10 | #include "syscall.h" 11 | 12 | .text 13 | .align 2 14 | 15 | /* ------------------------------------------------------------- 16 | * __start 17 | * Initialize running a C program, by calling "main". 18 | * 19 | * NOTE: This has to be first, so that it gets loaded at location 0. 20 | * The Nachos kernel always starts a program by jumping to location 0. 21 | * ------------------------------------------------------------- 22 | */ 23 | 24 | .globl __start 25 | .ent __start 26 | __start: 27 | jal main 28 | move $4,$0 29 | jal Exit /* if we return from main, exit(0) */ 30 | .end __start 31 | 32 | /* ------------------------------------------------------------- 33 | * System call stubs: 34 | * Assembly language assist to make system calls to the Nachos kernel. 35 | * There is one stub per system call, that places the code for the 36 | * system call into register r2, and leaves the arguments to the 37 | * system call alone (in other words, arg1 is in r4, arg2 is 38 | * in r5, arg3 is in r6, arg4 is in r7) 39 | * 40 | * The return value is in r2. This follows the standard C calling 41 | * convention on the MIPS. 42 | * ------------------------------------------------------------- 43 | */ 44 | 45 | .globl Halt 46 | .ent Halt 47 | Halt: 48 | addiu $2,$0,SC_Halt 49 | syscall 50 | j $31 51 | .end Halt 52 | 53 | .globl Exit 54 | .ent Exit 55 | Exit: 56 | addiu $2,$0,SC_Exit 57 | syscall 58 | j $31 59 | .end Exit 60 | 61 | .globl Exec 62 | .ent Exec 63 | Exec: 64 | addiu $2,$0,SC_Exec 65 | syscall 66 | j $31 67 | .end Exec 68 | 69 | .globl Join 70 | .ent Join 71 | Join: 72 | addiu $2,$0,SC_Join 73 | syscall 74 | j $31 75 | .end Join 76 | 77 | .globl Create 78 | .ent Create 79 | Create: 80 | addiu $2,$0,SC_Create 81 | syscall 82 | j $31 83 | .end Create 84 | 85 | .globl Open 86 | .ent Open 87 | Open: 88 | addiu $2,$0,SC_Open 89 | syscall 90 | j $31 91 | .end Open 92 | 93 | .globl Read 94 | .ent Read 95 | Read: 96 | addiu $2,$0,SC_Read 97 | syscall 98 | j $31 99 | .end Read 100 | 101 | .globl Write 102 | .ent Write 103 | Write: 104 | addiu $2,$0,SC_Write 105 | syscall 106 | j $31 107 | .end Write 108 | 109 | .globl Close 110 | .ent Close 111 | Close: 112 | addiu $2,$0,SC_Close 113 | syscall 114 | j $31 115 | .end Close 116 | 117 | .globl Fork 118 | .ent Fork 119 | Fork: 120 | addiu $2,$0,SC_Fork 121 | syscall 122 | j $31 123 | .end Fork 124 | 125 | .globl Yield 126 | .ent Yield 127 | Yield: 128 | addiu $2,$0,SC_Yield 129 | syscall 130 | j $31 131 | .end Yield 132 | 133 | .globl Pwd 134 | .ent Pwd 135 | Pwd: 136 | addiu $2,$0,SC_Pwd 137 | syscall 138 | j $31 139 | .end Pwd 140 | 141 | .globl Ls 142 | .ent Ls 143 | Ls: 144 | addiu $2,$0,SC_Ls 145 | syscall 146 | j $31 147 | .end Ls 148 | 149 | .globl Cd 150 | .ent Cd 151 | Cd: 152 | addiu $2,$0,SC_Cd 153 | syscall 154 | j $31 155 | .end Cd 156 | 157 | .globl Remove 158 | .ent Remove 159 | Remove: 160 | addiu $2,$0,SC_Remove 161 | syscall 162 | j $31 163 | .end Remove 164 | 165 | .globl CDir 166 | .ent CDir 167 | CDir: 168 | addiu $2,$0,SC_CDir 169 | syscall 170 | j $31 171 | .end CDir 172 | 173 | .globl RDir 174 | .ent RDir 175 | RDir: 176 | addiu $2,$0,SC_RDir 177 | syscall 178 | j $31 179 | .end RDir 180 | 181 | .globl Help 182 | .ent Help 183 | Help: 184 | addiu $2,$0,SC_Help 185 | syscall 186 | j $31 187 | .end Help 188 | 189 | /* dummy function to keep gcc happy */ 190 | .globl __main 191 | .ent __main 192 | __main: 193 | j $31 194 | .end __main 195 | 196 | -------------------------------------------------------------------------------- /filesys/filesys.h: -------------------------------------------------------------------------------- 1 | // filesys.h 2 | // Data structures to represent the Nachos file system. 3 | // 4 | // A file system is a set of files stored on disk, organized 5 | // into directories. Operations on the file system have to 6 | // do with "naming" -- creating, opening, and deleting files, 7 | // given a textual file name. Operations on an individual 8 | // "open" file (read, write, close) are to be found in the OpenFile 9 | // class (openfile.h). 10 | // 11 | // We define two separate implementations of the file system. 12 | // The "STUB" version just re-defines the Nachos file system 13 | // operations as operations on the native UNIX file system on the machine 14 | // running the Nachos simulation. This is provided in case the 15 | // multiprogramming and virtual memory assignments (which make use 16 | // of the file system) are done before the file system assignment. 17 | // 18 | // The other version is a "real" file system, built on top of 19 | // a disk simulator. The disk is simulated using the native UNIX 20 | // file system (in a file named "DISK"). 21 | // 22 | // In the "real" implementation, there are two key data structures used 23 | // in the file system. There is a single "root" directory, listing 24 | // all of the files in the file system; unlike UNIX, the baseline 25 | // system does not provide a hierarchical directory structure. 26 | // In addition, there is a bitmap for allocating 27 | // disk sectors. Both the root directory and the bitmap are themselves 28 | // stored as files in the Nachos file system -- this causes an interesting 29 | // bootstrap problem when the simulated disk is initialized. 30 | // 31 | // Copyright (c) 1992-1993 The Regents of the University of California. 32 | // All rights reserved. See copyright.h for copyright notice and limitation 33 | // of liability and disclaimer of warranty provisions. 34 | 35 | #ifndef FS_H 36 | #define FS_H 37 | 38 | #include "copyright.h" 39 | #include "openfile.h" 40 | #include "filehdr.h" 41 | #ifdef FILESYS_STUB // Temporarily implement file system calls as 42 | // calls to UNIX, until the real file system 43 | // implementation is available 44 | class FileSystem { 45 | public: 46 | FileSystem(bool format) {} 47 | 48 | bool Create(char *name, int initialSize) { 49 | int fileDescriptor = OpenForWrite(name); 50 | 51 | if (fileDescriptor == -1) return FALSE; 52 | 53 | Close(fileDescriptor); 54 | return TRUE; 55 | } 56 | 57 | OpenFile* Open(char *name) { 58 | 59 | int fileDescriptor = OpenForReadWrite(name, FALSE); 60 | 61 | if (fileDescriptor == -1) return NULL; 62 | 63 | return new OpenFile(fileDescriptor); 64 | } 65 | 66 | bool Remove(char *name) { return Unlink(name) == 0; } 67 | 68 | }; 69 | 70 | #else // FILESYS 71 | class FileSystem { 72 | public: 73 | FileSystem(bool format); // Initialize the file system. 74 | // Must be called *after* "synchDisk" 75 | // has been initialized. 76 | // If "format", there is nothing on 77 | // the disk, so initialize the directory 78 | // and the bitmap of free blocks. 79 | 80 | bool Create(char *name,int initialSize,int fileType); 81 | // Create a file (UNIX creat) 82 | // bool createDir(char *absPath,char *dirPath); 83 | OpenFile* Open(char *name); // Open a file (UNIX open) 84 | // OpenFile* Open(char *absPath); 85 | bool Remove(char *name); // Delete a file (UNIX unlink) 86 | 87 | void List(); // List all the files in the file system 88 | 89 | void Print(); // List all the files and their contents 90 | 91 | void changePWD(char *name); 92 | 93 | void listOne(char *filename); 94 | bool extendFile(char *name,int extrasize); 95 | int readPipe(char *data); 96 | void writePipe(char *data,int length); 97 | FileHeader *pipehdr; 98 | char *currentPath; 99 | private: 100 | 101 | OpenFile* freeMapFile; // Bit map of free disk blocks, 102 | // represented as a file 103 | OpenFile* directoryFile; // "Root" directory -- list of 104 | // file names, represented as a file 105 | }; 106 | 107 | #endif // FILESYS 108 | 109 | #endif // FS_H 110 | -------------------------------------------------------------------------------- /bin/coff2flat.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | /* This program reads in a COFF format file, and outputs a flat file -- 8 | * the flat file can then be copied directly to virtual memory and executed. 9 | * In other words, the various pieces of the object code are loaded at 10 | * the appropriate offset in the flat file. 11 | * 12 | * Assumes coff file compiled with -N -T 0 to make sure it's not shared text. 13 | */ 14 | 15 | #define MAIN 16 | #include "copyright.h" 17 | #undef MAIN 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | /* NOTE -- once you have implemented large files, it's ok to make this bigger! */ 31 | #define StackSize 1024 /* in bytes */ 32 | #define ReadStruct(f,s) Read(f,(char *)&s,sizeof(s)) 33 | 34 | extern char *malloc(); 35 | 36 | /* read and check for error */ 37 | void Read(int fd, char *buf, int nBytes) 38 | { 39 | if (read(fd, buf, nBytes) != nBytes) { 40 | fprintf(stderr, "File is too short\n"); 41 | exit(1); 42 | } 43 | } 44 | 45 | /* write and check for error */ 46 | void Write(int fd, char *buf, int nBytes) 47 | { 48 | if (write(fd, buf, nBytes) != nBytes) { 49 | fprintf(stderr, "Unable to write file\n"); 50 | exit(1); 51 | } 52 | } 53 | 54 | /* do the real work */ 55 | main (int argc, char **argv) 56 | { 57 | int fdIn, fdOut, numsections, i, top, tmp; 58 | struct filehdr fileh; 59 | struct aouthdr systemh; 60 | struct scnhdr *sections; 61 | char *buffer; 62 | 63 | if (argc < 2) { 64 | fprintf(stderr, "Usage: %s \n", argv[0]); 65 | exit(1); 66 | } 67 | 68 | /* open the object file (input) */ 69 | fdIn = open(argv[1], O_RDONLY, 0); 70 | if (fdIn == -1) { 71 | perror(argv[1]); 72 | exit(1); 73 | } 74 | 75 | /* open the flat file (output) */ 76 | fdOut = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666); 77 | if (fdIn == -1) { 78 | perror(argv[2]); 79 | exit(1); 80 | } 81 | 82 | /* Read in the file header and check the magic number. */ 83 | ReadStruct(fdIn,fileh); 84 | if (fileh.f_magic != MIPSELMAGIC) { 85 | fprintf(stderr, "File is not a MIPSEL COFF file\n"); 86 | exit(1); 87 | } 88 | 89 | /* Read in the system header and check the magic number */ 90 | ReadStruct(fdIn,systemh); 91 | if (systemh.magic != OMAGIC) { 92 | fprintf(stderr, "File is not a OMAGIC file\n"); 93 | exit(1); 94 | } 95 | 96 | /* Read in the section headers. */ 97 | numsections = fileh.f_nscns; 98 | sections = (struct scnhdr *)malloc(fileh.f_nscns * sizeof(struct scnhdr)); 99 | Read(fdIn, (char *) sections, fileh.f_nscns * sizeof(struct scnhdr)); 100 | 101 | /* Copy the segments in */ 102 | printf("Loading %d sections:\n", fileh.f_nscns); 103 | for (top = 0, i = 0; i < fileh.f_nscns; i++) { 104 | printf("\t\"%s\", filepos 0x%x, mempos 0x%x, size 0x%x\n", 105 | sections[i].s_name, sections[i].s_scnptr, 106 | sections[i].s_paddr, sections[i].s_size); 107 | if ((sections[i].s_paddr + sections[i].s_size) > top) 108 | top = sections[i].s_paddr + sections[i].s_size; 109 | if (strcmp(sections[i].s_name, ".bss") && /* no need to copy if .bss */ 110 | strcmp(sections[i].s_name, ".sbss")) { 111 | lseek(fdIn, sections[i].s_scnptr, 0); 112 | buffer = malloc(sections[i].s_size); 113 | Read(fdIn, buffer, sections[i].s_size); 114 | Write(fdOut, buffer, sections[i].s_size); 115 | free(buffer); 116 | } 117 | } 118 | /* put a blank word at the end, so we know where the end is! */ 119 | printf("Adding stack of size: %d\n", StackSize); 120 | lseek(fdOut, top + StackSize - 4, 0); 121 | tmp = 0; 122 | Write(fdOut, (char *)&tmp, 4); 123 | 124 | close(fdIn); 125 | close(fdOut); 126 | } 127 | -------------------------------------------------------------------------------- /machine/disk.h: -------------------------------------------------------------------------------- 1 | // disk.h 2 | // Data structures to emulate a physical disk. A physical disk 3 | // can accept (one at a time) requests to read/write a disk sector; 4 | // when the request is satisfied, the CPU gets an interrupt, and 5 | // the next request can be sent to the disk. 6 | // 7 | // Disk contents are preserved across machine crashes, but if 8 | // a file system operation (eg, create a file) is in progress when the 9 | // system shuts down, the file system may be corrupted. 10 | // 11 | // DO NOT CHANGE -- part of the machine emulation 12 | // 13 | // Copyright (c) 1992-1993 The Regents of the University of California. 14 | // All rights reserved. See copyright.h for copyright notice and limitation 15 | // of liability and disclaimer of warranty provisions. 16 | 17 | #ifndef DISK_H 18 | #define DISK_H 19 | 20 | #include "copyright.h" 21 | #include "utility.h" 22 | 23 | // The following class defines a physical disk I/O device. The disk 24 | // has a single surface, split up into "tracks", and each track split 25 | // up into "sectors" (the same number of sectors on each track, and each 26 | // sector has the same number of bytes of storage). 27 | // 28 | // Addressing is by sector number -- each sector on the disk is given 29 | // a unique number: track * SectorsPerTrack + offset within a track. 30 | // 31 | // As with other I/O devices, the raw physical disk is an asynchronous device -- 32 | // requests to read or write portions of the disk return immediately, 33 | // and an interrupt is invoked later to signal that the operation completed. 34 | // 35 | // The physical disk is in fact simulated via operations on a UNIX file. 36 | // 37 | // To make life a little more realistic, the simulated time for 38 | // each operation reflects a "track buffer" -- RAM to store the contents 39 | // of the current track as the disk head passes by. The idea is that the 40 | // disk always transfers to the track buffer, in case that data is requested 41 | // later on. This has the benefit of eliminating the need for 42 | // "skip-sector" scheduling -- a read request which comes in shortly after 43 | // the head has passed the beginning of the sector can be satisfied more 44 | // quickly, because its contents are in the track buffer. Most 45 | // disks these days now come with a track buffer. 46 | // 47 | // The track buffer simulation can be disabled by compiling with -DNOTRACKBUF 48 | 49 | #define SectorSize 128 // number of bytes per disk sector 50 | #define SectorsPerTrack 32 // number of sectors per disk track 51 | #define NumTracks 32 // number of tracks per disk 52 | #define NumSectors (SectorsPerTrack * NumTracks) 53 | // total # of sectors per disk 54 | 55 | class Disk { 56 | public: 57 | Disk(char* name, VoidFunctionPtr callWhenDone, int callArg); 58 | // Create a simulated disk. 59 | // Invoke (*callWhenDone)(callArg) 60 | // every time a request completes. 61 | ~Disk(); // Deallocate the disk. 62 | 63 | void ReadRequest(int sectorNumber, char* data); 64 | // Read/write an single disk sector. 65 | // These routines send a request to 66 | // the disk and return immediately. 67 | // Only one request allowed at a time! 68 | void WriteRequest(int sectorNumber, char* data); 69 | 70 | void HandleInterrupt(); // Interrupt handler, invoked when 71 | // disk request finishes. 72 | 73 | int ComputeLatency(int newSector, bool writing); 74 | // Return how long a request to 75 | // newSector will take: 76 | // (seek + rotational delay + transfer) 77 | 78 | private: 79 | int fileno; // UNIX file number for simulated disk 80 | VoidFunctionPtr handler; // Interrupt handler, to be invoked 81 | // when any disk request finishes 82 | int handlerArg; // Argument to interrupt handler 83 | bool active; // Is a disk operation in progress? 84 | int lastSector; // The previous disk request 85 | int bufferInit; // When the track buffer started 86 | // being loaded 87 | 88 | int TimeToSeek(int newSector, int *rotate); // time to get to the new track 89 | int ModuloDiff(int to, int from); // # sectors between to and from 90 | void UpdateLast(int newSector); 91 | }; 92 | 93 | #endif // DISK_H 94 | -------------------------------------------------------------------------------- /userprog/progtest.cc: -------------------------------------------------------------------------------- 1 | // progtest.cc 2 | // Test routines for demonstrating that Nachos can load 3 | // a user program and execute it. 4 | // 5 | // Also, routines for testing the Console hardware device. 6 | // 7 | // Copyright (c) 1992-1993 The Regents of the University of California. 8 | // All rights reserved. See copyright.h for copyright notice and limitation 9 | // of liability and disclaimer of warranty provisions. 10 | 11 | #include "copyright.h" 12 | #include "system.h" 13 | #include "console.h" 14 | #include "addrspace.h" 15 | #include "synch.h" 16 | 17 | //---------------------------------------------------------------------- 18 | // StartProcess 19 | // Run a user program. Open the executable, load it into 20 | // memory, and jump to it. 21 | //---------------------------------------------------------------------- 22 | 23 | void 24 | newThread(){ 25 | printf("The new thread is running !!!\n"); 26 | machine->Run(); 27 | 28 | 29 | } 30 | /* 31 | void 32 | StartProcess(char *filename) 33 | { 34 | 35 | OpenFile *executable = fileSystem->Open("../test/sort"); 36 | OpenFile *executable_new = fileSystem->Open(filename); 37 | AddrSpace *space; 38 | AddrSpace *space_new; 39 | if (executable == NULL) { 40 | printf("Unable to open file %s\n", filename); 41 | return; 42 | } 43 | printf("Init old thread.\n"); 44 | space = new AddrSpace(executable,"../test/sort"); 45 | printf("Init new thread.\n"); 46 | space_new = new AddrSpace(executable_new,filename); 47 | Thread *thread_new = new Thread("new thread"); 48 | 49 | currentThread->space = space; 50 | printf("currentThread %s",currentThread->getName()); 51 | space_new->InitRegisters(); 52 | space_new->RestoreState(); 53 | thread_new->space = space_new; 54 | 55 | thread_new->Fork(newThread,0); 56 | currentThread->Yield(); 57 | 58 | delete executable; 59 | delete executable_new; 60 | space->InitRegisters(); 61 | space->RestoreState(); 62 | 63 | printf("the new thread suspend now !.\n"); 64 | thread_new->Suspend(); 65 | printf("The old thread starts.\n"); 66 | machine->Run(); 67 | 68 | 69 | ASSERT(FALSE); 70 | } 71 | */ 72 | 73 | void 74 | StartProcess(char *filename) 75 | { 76 | OpenFile *executable = fileSystem->Open(filename); 77 | AddrSpace *space; 78 | 79 | if (executable == NULL) { 80 | printf("Unable to open file %s\n", filename); 81 | return; 82 | } 83 | 84 | printf(" start process filename %s.\n",filename); 85 | space = new AddrSpace(executable,filename); 86 | currentThread->space = space; 87 | 88 | delete executable; 89 | 90 | space->InitRegisters(); 91 | space->RestoreState(); 92 | 93 | machine->Run(); 94 | 95 | ASSERT(FALSE); 96 | } 97 | 98 | 99 | // Data structures needed for the console test. Threads making 100 | // I/O requests wait on a Semaphore to delay until the I/O completes. 101 | 102 | static Console *console; 103 | 104 | static Semaphore *readAvail; 105 | static Semaphore *writeDone; 106 | 107 | //---------------------------------------------------------------------- 108 | // ConsoleInterruptHandlers 109 | // Wake up the thread that requested the I/O. 110 | //---------------------------------------------------------------------- 111 | 112 | static void ReadAvail(int arg) { readAvail->V(); } 113 | static void WriteDone(int arg) { writeDone->V(); } 114 | 115 | //---------------------------------------------------------------------- 116 | // ConsoleTest 117 | // Test the console by echoing characters typed at the input onto 118 | // the output. Stop when the user types a 'q'. 119 | //---------------------------------------------------------------------- 120 | 121 | void 122 | ConsoleTest (char *in, char *out) 123 | { 124 | char ch; 125 | 126 | // synchconsole = new SynchConsole(in, out); 127 | readAvail = new Semaphore("read avail", 0); 128 | writeDone = new Semaphore("write done", 0); 129 | 130 | for (;;) { 131 | readAvail->P(); // wait for character to arrive 132 | ch = console->getChar(); 133 | console->putChar(ch); // echo it! 134 | writeDone->P() ; // wait for write to finish 135 | if (ch == 'q') return; // if q, quit 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /threads/switch-old.s: -------------------------------------------------------------------------------- 1 | /* switch.s 2 | * Machine dependent context switch routines. DO NOT MODIFY THESE! 3 | * 4 | * Context switching is inherently machine dependent, since 5 | * the registers to be saved, how to set up an initial 6 | * call frame, etc, are all specific to a processor architecture. 7 | * 8 | * This file currently supports the following architectures: 9 | * DEC MIPS 10 | * SUN SPARC 11 | * HP PA-RISC 12 | * Intel 386 13 | * 14 | * We define two routines for each architecture: 15 | * 16 | * ThreadRoot(InitialPC, InitialArg, WhenDonePC, StartupPC) 17 | * InitialPC - The program counter of the procedure to run 18 | * in this thread. 19 | * InitialArg - The single argument to the thread. 20 | * WhenDonePC - The routine to call when the thread returns. 21 | * StartupPC - Routine to call when the thread is started. 22 | * 23 | * ThreadRoot is called from the SWITCH() routine to start 24 | * a thread for the first time. 25 | * 26 | * SWITCH(oldThread, newThread) 27 | * oldThread - The current thread that was running, where the 28 | * CPU register state is to be saved. 29 | * newThread - The new thread to be run, where the CPU register 30 | * state is to be loaded from. 31 | */ 32 | 33 | /* 34 | Copyright (c) 1992-1993 The Regents of the University of California. 35 | All rights reserved. See copyright.h for copyright notice and limitation 36 | of liability and disclaimer of warranty provisions. 37 | */ 38 | 39 | #include "copyright.h" 40 | #include "switch.h" 41 | 42 | .text 43 | .align 2 44 | 45 | .globl ThreadRoot 46 | 47 | /* void ThreadRoot( void ) 48 | ** 49 | ** expects the following registers to be initialized: 50 | ** eax points to startup function (interrupt enable) 51 | ** edx contains inital argument to thread function 52 | ** esi points to thread function 53 | ** edi point to Thread::Finish() 54 | */ 55 | ThreadRoot: 56 | pushl %ebp 57 | movl %esp,%ebp 58 | pushl InitialArg 59 | call *StartupPC 60 | call *InitialPC 61 | call *WhenDonePC 62 | 63 | # NOT REACHED 64 | movl %ebp,%esp 65 | popl %ebp 66 | ret 67 | 68 | 69 | 70 | /* void SWITCH( thread *t1, thread *t2 ) 71 | ** 72 | ** on entry, stack looks like this: 73 | ** 8(esp) -> thread *t2 74 | ** 4(esp) -> thread *t1 75 | ** (esp) -> return address 76 | ** 77 | ** we push the current eax on the stack so that we can use it as 78 | ** a pointer to t1, this decrements esp by 4, so when we use it 79 | ** to reference stuff on the stack, we add 4 to the offset. 80 | */ 81 | .comm _eax_save,4 82 | 83 | .globl SWITCH 84 | SWITCH: 85 | movl %eax,_eax_save # save the value of eax 86 | movl 4(%esp),%eax # move pointer to t1 into eax 87 | movl %ebx,_EBX(%eax) # save registers 88 | movl %ecx,_ECX(%eax) 89 | movl %edx,_EDX(%eax) 90 | movl %esi,_ESI(%eax) 91 | movl %edi,_EDI(%eax) 92 | movl %ebp,_EBP(%eax) 93 | movl %esp,_ESP(%eax) # save stack pointer 94 | movl _eax_save,%ebx # get the saved value of eax 95 | movl %ebx,_EAX(%eax) # store it 96 | movl 0(%esp),%ebx # get return address from stack into ebx 97 | movl %ebx,_PC(%eax) # save it into the pc storage 98 | 99 | movl 8(%esp),%eax # move pointer to t2 into eax 100 | 101 | movl _EAX(%eax),%ebx # get new value for eax into ebx 102 | movl %ebx,_eax_save # save it 103 | movl _EBX(%eax),%ebx # retore old registers 104 | movl _ECX(%eax),%ecx 105 | movl _EDX(%eax),%edx 106 | movl _ESI(%eax),%esi 107 | movl _EDI(%eax),%edi 108 | movl _EBP(%eax),%ebp 109 | movl _ESP(%eax),%esp # restore stack pointer 110 | movl _PC(%eax),%eax # restore return address into eax 111 | movl %eax,4(%esp) # copy over the ret address on the stack 112 | movl _eax_save,%eax 113 | 114 | ret 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /bin/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | 9 | /* MIPS instruction interpreter */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "int.h" 17 | 18 | static FILE *fp; 19 | static LDFILE *ldptr; 20 | static SCNHDR texthead, rdatahead, datahead, sdatahead, sbsshead, bsshead; 21 | 22 | static char filename[1000] = "a.out"; /* default a.out file */ 23 | static char self[256]; /* name of invoking program */ 24 | 25 | char mem[MEMSIZE]; /* main memory. use malloc later */ 26 | int TRACE, Traptrace, Regtrace; 27 | int NROWS=64, ASSOC=1, LINESIZE=4, RAND=0, LRD=0; 28 | 29 | extern char *strcpy(); 30 | 31 | main(argc, argv) 32 | int argc; 33 | char *argv[]; 34 | { 35 | register char *s; 36 | char *fakeargv[3]; 37 | 38 | strcpy(self, argv[0]); 39 | while ( argc > 1 && argv[1][0] == '-' ) 40 | { 41 | --argc; ++argv; 42 | for ( s=argv[0]+1; *s != '\0'; ++s ) 43 | switch ( *s ) 44 | { 45 | case 't': TRACE = 1; break; 46 | case 'T': Traptrace = 1; break; 47 | case 'r': Regtrace = 1; break; 48 | case 'm': 49 | NROWS = atoi(*++argv); 50 | ASSOC = atoi(*++argv); 51 | LINESIZE = atoi(*++argv); 52 | RAND = ((*++argv)[0] == 'r'); 53 | LRD = ((*argv)[0] == 'l') 54 | && ((*argv)[1] == 'r') 55 | && ((*argv)[2] == 'd'); 56 | argc -= 4; 57 | break; 58 | } 59 | } 60 | 61 | if (argc >= 2) 62 | strcpy(filename, argv[1]); 63 | fp = fopen(filename, "r"); 64 | if (fp == NULL) 65 | { 66 | fprintf(stderr, "%s: Could not open '%s'\n", self, filename); 67 | exit(0); 68 | } 69 | fclose(fp); 70 | load_program(filename); 71 | if ( argv[1] == NULL ) 72 | { 73 | fakeargv[1] = "a.out"; 74 | fakeargv[2] = NULL; 75 | argv = fakeargv; 76 | ++argc; 77 | } 78 | runprogram(memoffset, argc-1, argv+1); /* where things normally start */ 79 | } 80 | 81 | char *string(s) 82 | char *s; 83 | { 84 | char *p; 85 | extern char *malloc(); 86 | 87 | p = malloc((unsigned) strlen(s)+1); 88 | strcpy(p, s); 89 | return p; 90 | } 91 | 92 | load_program(filename) 93 | char *filename; 94 | { 95 | register int pc, i, j, strindex, stl; 96 | char str[1111]; 97 | int rc1, rc2; 98 | 99 | ldptr = ldopen(filename, NULL); 100 | if ( ldptr == NULL ) 101 | { 102 | fprintf(stderr, "%s: Load read error on %s\n", self, filename); 103 | exit(0); 104 | } 105 | if ( TYPE(ldptr) != 0x162 ) 106 | { 107 | fprintf(stderr, 108 | "big-endian object file (little-endian interp)\n"); 109 | exit(0); 110 | } 111 | 112 | #define LOADSECTION(head) \ 113 | if ( head.s_scnptr != 0 ) \ 114 | { \ 115 | /* printf("loading %s\n", head.s_name); /* */ \ 116 | pc = head.s_vaddr; \ 117 | FSEEK(ldptr, head.s_scnptr, 0); \ 118 | for ( i=0; i= MEMSIZE) \ 121 | { printf("MEMSIZE too small. Fix and recompile.\n"); \ 122 | exit(1); } \ 123 | } 124 | 125 | if ( ldnshread(ldptr, ".text", &texthead) != 1 ) 126 | printf("text section header missing\n"); 127 | else 128 | LOADSECTION(texthead) 129 | 130 | if ( ldnshread(ldptr, ".rdata", &rdatahead) != 1 ) 131 | printf("rdata section header missing\n"); 132 | else 133 | LOADSECTION(rdatahead) 134 | 135 | if ( ldnshread(ldptr, ".data", &datahead) != 1 ) 136 | printf("data section header missing\n"); 137 | else 138 | LOADSECTION(datahead) 139 | 140 | if ( ldnshread(ldptr, ".sdata", &sdatahead) != 1 ) 141 | printf("sdata section header missing\n"); 142 | else 143 | LOADSECTION(sdatahead) 144 | 145 | if ( ldnshread(ldptr, ".sbss", &sbsshead) != 1 ) 146 | printf("sbss section header missing\n"); 147 | else 148 | LOADSECTION(sbsshead) 149 | 150 | if ( ldnshread(ldptr, ".bss", &bsshead) != 1 ) 151 | printf("bss section header missing\n"); 152 | else 153 | LOADSECTION(bsshead) 154 | 155 | /* BSS is already zeroed (statically-allocated mem) */ 156 | /* this version ignores relocation info */ 157 | } 158 | 159 | 160 | int *m_alloc(n) 161 | int n; 162 | { 163 | extern char *malloc(); 164 | 165 | return (int *) (int) malloc((unsigned) n); 166 | } 167 | 168 | -------------------------------------------------------------------------------- /threads/out1: -------------------------------------------------------------------------------- 1 | This function set_tid_() has been called. 2 | This function set_uid_() has been called. 3 | This function set_tid_() has been called. 4 | This function set_uid_() has been called. 5 | This function set_tid_() has been called. 6 | This function set_uid_() has been called. 7 | This function set_tid_() has been called. 8 | This function set_uid_() has been called. 9 | This function get_tid_() has been called. 10 | This function get_tid_() has been called. 11 | This function get_tid_() has been called. 12 | This function get_tid_() has been called. 13 | This function get_uid_() has been called. 14 | *** thread 0 name main uid_ 1000 tid_ 0 looped 0 times 15 | Status RUNNING 16 | This function get_tid_() has been called. 17 | This function get_uid_() has been called. 18 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 0 times 19 | Status RUNNING 20 | This function get_tid_() has been called. 21 | This function get_uid_() has been called. 22 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 0 times 23 | Status RUNNING 24 | This function get_tid_() has been called. 25 | This function get_uid_() has been called. 26 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 0 times 27 | Status RUNNING 28 | This function get_tid_() has been called. 29 | This function get_uid_() has been called. 30 | *** thread 0 name main uid_ 1000 tid_ 0 looped 1 times 31 | Status RUNNING 32 | This function get_tid_() has been called. 33 | This function get_uid_() has been called. 34 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 1 times 35 | Status RUNNING 36 | This function get_tid_() has been called. 37 | This function get_uid_() has been called. 38 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 1 times 39 | Status RUNNING 40 | This function get_tid_() has been called. 41 | This function get_uid_() has been called. 42 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 1 times 43 | Status RUNNING 44 | This function get_tid_() has been called. 45 | This function get_uid_() has been called. 46 | *** thread 0 name main uid_ 1000 tid_ 0 looped 2 times 47 | Status RUNNING 48 | This function get_tid_() has been called. 49 | This function get_uid_() has been called. 50 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 2 times 51 | Status RUNNING 52 | This function get_tid_() has been called. 53 | This function get_uid_() has been called. 54 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 2 times 55 | Status RUNNING 56 | This function get_tid_() has been called. 57 | This function get_uid_() has been called. 58 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 2 times 59 | Status RUNNING 60 | This function get_tid_() has been called. 61 | This function get_uid_() has been called. 62 | *** thread 0 name main uid_ 1000 tid_ 0 looped 3 times 63 | Status RUNNING 64 | This function get_tid_() has been called. 65 | This function get_uid_() has been called. 66 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 3 times 67 | Status RUNNING 68 | This function get_tid_() has been called. 69 | This function get_uid_() has been called. 70 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 3 times 71 | Status RUNNING 72 | This function get_tid_() has been called. 73 | This function get_uid_() has been called. 74 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 3 times 75 | Status RUNNING 76 | This function get_tid_() has been called. 77 | This function get_uid_() has been called. 78 | *** thread 0 name main uid_ 1000 tid_ 0 looped 4 times 79 | Status RUNNING 80 | This function get_tid_() has been called. 81 | This function get_uid_() has been called. 82 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 4 times 83 | Status RUNNING 84 | This function get_tid_() has been called. 85 | This function get_uid_() has been called. 86 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 4 times 87 | Status RUNNING 88 | This function get_tid_() has been called. 89 | This function get_uid_() has been called. 90 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 4 times 91 | Status RUNNING 92 | No threads ready or runnable, and no pending interrupts. 93 | Assuming the program completed. 94 | Machine halting! 95 | 96 | Ticks: total 270, idle 0, system 270, user 0 97 | Disk I/O: reads 0, writes 0 98 | Console I/O: reads 0, writes 0 99 | Paging: faults 0 100 | Network I/O: packets received 0, sent 0 101 | 102 | Cleaning up... 103 | -------------------------------------------------------------------------------- /threads/out2: -------------------------------------------------------------------------------- 1 | This function set_tid_() has been called. 2 | This function set_uid_() has been called. 3 | This function set_tid_() has been called. 4 | This function set_uid_() has been called. 5 | This function set_tid_() has been called. 6 | This function set_uid_() has been called. 7 | This function set_tid_() has been called. 8 | This function set_uid_() has been called. 9 | This function get_tid_() has been called. 10 | This function get_tid_() has been called. 11 | This function get_tid_() has been called. 12 | This function get_tid_() has been called. 13 | This function get_uid_() has been called. 14 | *** thread 0 name main uid_ 1000 tid_ 0 looped 0 times 15 | Status RUNNING 16 | This function get_tid_() has been called. 17 | This function get_uid_() has been called. 18 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 0 times 19 | Status RUNNING 20 | This function get_tid_() has been called. 21 | This function get_uid_() has been called. 22 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 0 times 23 | Status RUNNING 24 | This function get_tid_() has been called. 25 | This function get_uid_() has been called. 26 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 0 times 27 | Status RUNNING 28 | This function get_tid_() has been called. 29 | This function get_uid_() has been called. 30 | *** thread 0 name main uid_ 1000 tid_ 0 looped 1 times 31 | Status RUNNING 32 | This function get_tid_() has been called. 33 | This function get_uid_() has been called. 34 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 1 times 35 | Status RUNNING 36 | This function get_tid_() has been called. 37 | This function get_uid_() has been called. 38 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 1 times 39 | Status RUNNING 40 | This function get_tid_() has been called. 41 | This function get_uid_() has been called. 42 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 1 times 43 | Status RUNNING 44 | This function get_tid_() has been called. 45 | This function get_uid_() has been called. 46 | *** thread 0 name main uid_ 1000 tid_ 0 looped 2 times 47 | Status RUNNING 48 | This function get_tid_() has been called. 49 | This function get_uid_() has been called. 50 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 2 times 51 | Status RUNNING 52 | This function get_tid_() has been called. 53 | This function get_uid_() has been called. 54 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 2 times 55 | Status RUNNING 56 | This function get_tid_() has been called. 57 | This function get_uid_() has been called. 58 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 2 times 59 | Status RUNNING 60 | This function get_tid_() has been called. 61 | This function get_uid_() has been called. 62 | *** thread 0 name main uid_ 1000 tid_ 0 looped 3 times 63 | Status RUNNING 64 | This function get_tid_() has been called. 65 | This function get_uid_() has been called. 66 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 3 times 67 | Status RUNNING 68 | This function get_tid_() has been called. 69 | This function get_uid_() has been called. 70 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 3 times 71 | Status RUNNING 72 | This function get_tid_() has been called. 73 | This function get_uid_() has been called. 74 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 3 times 75 | Status RUNNING 76 | This function get_tid_() has been called. 77 | This function get_uid_() has been called. 78 | *** thread 0 name main uid_ 1000 tid_ 0 looped 4 times 79 | Status RUNNING 80 | This function get_tid_() has been called. 81 | This function get_uid_() has been called. 82 | *** thread 1 name forked thread1 uid_ 1000 tid_ 1 looped 4 times 83 | Status RUNNING 84 | This function get_tid_() has been called. 85 | This function get_uid_() has been called. 86 | *** thread 2 name forked thread2 uid_ 1000 tid_ 2 looped 4 times 87 | Status RUNNING 88 | This function get_tid_() has been called. 89 | This function get_uid_() has been called. 90 | *** thread 3 name forked thread3 uid_ 1000 tid_ 3 looped 4 times 91 | Status RUNNING 92 | No threads ready or runnable, and no pending interrupts. 93 | Assuming the program completed. 94 | Machine halting! 95 | 96 | Ticks: total 270, idle 0, system 270, user 0 97 | Disk I/O: reads 0, writes 0 98 | Console I/O: reads 0, writes 0 99 | Paging: faults 0 100 | Network I/O: packets received 0, sent 0 101 | 102 | Cleaning up... 103 | -------------------------------------------------------------------------------- /machine/network.h: -------------------------------------------------------------------------------- 1 | // network.h 2 | // Data structures to emulate a physical network connection. 3 | // The network provides the abstraction of ordered, unreliable, 4 | // fixed-size packet delivery to other machines on the network. 5 | // 6 | // You may note that the interface to the network is similar to 7 | // the console device -- both are full duplex channels. 8 | // 9 | // DO NOT CHANGE -- part of the machine emulation 10 | // 11 | // Copyright (c) 1992-1993 The Regents of the University of California. 12 | // All rights reserved. See copyright.h for copyright notice and limitation 13 | // of liability and disclaimer of warranty provisions. 14 | 15 | #ifndef NETWORK_H 16 | #define NETWORK_H 17 | 18 | #include "copyright.h" 19 | #include "utility.h" 20 | 21 | // Network address -- uniquely identifies a machine. This machine's ID 22 | // is given on the command line. 23 | typedef int NetworkAddress; 24 | 25 | // The following class defines the network packet header. 26 | // The packet header is prepended to the data payload by the Network driver, 27 | // before the packet is sent over the wire. The format on the wire is: 28 | // packet header (PacketHeader) 29 | // data (containing MailHeader from the PostOffice!) 30 | 31 | class PacketHeader { 32 | public: 33 | NetworkAddress to; // Destination machine ID 34 | NetworkAddress from; // source machine ID 35 | unsigned length; // bytes of packet data, excluding the 36 | // packet header (but including the 37 | // MailHeader prepended by the post office) 38 | }; 39 | 40 | #define MaxWireSize 64 // largest packet that can go out on the wire 41 | #define MaxPacketSize (MaxWireSize - sizeof(struct PacketHeader)) 42 | // data "payload" of the largest packet 43 | 44 | 45 | // The following class defines a physical network device. The network 46 | // is capable of delivering fixed sized packets, in order but unreliably, 47 | // to other machines connected to the network. 48 | // 49 | // The "reliability" of the network can be specified to the constructor. 50 | // This number, between 0 and 1, is the chance that the network will lose 51 | // a packet. Note that you can change the seed for the random number 52 | // generator, by changing the arguments to RandomInit() in Initialize(). 53 | // The random number generator is used to choose which packets to drop. 54 | 55 | class Network { 56 | public: 57 | Network(NetworkAddress addr, double reliability, 58 | VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg); 59 | // Allocate and initialize network driver 60 | ~Network(); // De-allocate the network driver data 61 | 62 | void Send(PacketHeader hdr, char* data); 63 | // Send the packet data to a remote machine, 64 | // specified by "hdr". Returns immediately. 65 | // "writeHandler" is invoked once the next 66 | // packet can be sent. Note that writeHandler 67 | // is called whether or not the packet is 68 | // dropped, and note that the "from" field of 69 | // the PacketHeader is filled in automatically 70 | // by Send(). 71 | 72 | PacketHeader Receive(char* data); 73 | // Poll the network for incoming messages. 74 | // If there is a packet waiting, copy the 75 | // packet into "data" and return the header. 76 | // If no packet is waiting, return a header 77 | // with length 0. 78 | 79 | void SendDone(); // Interrupt handler, called when message is 80 | // sent 81 | void CheckPktAvail(); // Check if there is an incoming packet 82 | 83 | private: 84 | NetworkAddress ident; // This machine's network address 85 | double chanceToWork; // Likelihood packet will be dropped 86 | int sock; // UNIX socket number for incoming packets 87 | char sockName[32]; // File name corresponding to UNIX socket 88 | VoidFunctionPtr writeHandler; // Interrupt handler, signalling next packet 89 | // can be sent. 90 | VoidFunctionPtr readHandler; // Interrupt handler, signalling packet has 91 | // arrived. 92 | int handlerArg; // Argument to be passed to interrupt handler 93 | // (pointer to post office) 94 | bool sendBusy; // Packet is being sent. 95 | bool packetAvail; // Packet has arrived, can be pulled off of 96 | // network 97 | PacketHeader inHdr; // Information about arrived packet 98 | char inbox[MaxPacketSize]; // Data for arrived packet 99 | }; 100 | 101 | #endif // NETWORK_H 102 | -------------------------------------------------------------------------------- /bin/disasm.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | 9 | /* MIPS instruction disassembler */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "int.h" 17 | 18 | static FILE *fp; 19 | static LDFILE *ldptr; 20 | static SCNHDR texthead, rdatahead, datahead, sdatahead, sbsshead, bsshead; 21 | 22 | static char filename[1000] = "a.out"; /* default a.out file */ 23 | static char self[256]; /* name of invoking program */ 24 | 25 | char mem[MEMSIZE]; /* main memory. use malloc later */ 26 | int TRACE, Traptrace, Regtrace; 27 | int NROWS=64, ASSOC=1, LINESIZE=4, RAND=0, LRD=0; 28 | int pc; 29 | 30 | extern char *strcpy(); 31 | 32 | main(argc, argv) 33 | int argc; 34 | char *argv[]; 35 | { 36 | register char *s; 37 | char *fakeargv[3]; 38 | 39 | strcpy(self, argv[0]); 40 | while ( argc > 1 && argv[1][0] == '-' ) 41 | { 42 | --argc; ++argv; 43 | for ( s=argv[0]+1; *s != '\0'; ++s ) 44 | switch ( *s ) 45 | { 46 | } 47 | } 48 | 49 | if (argc >= 2) 50 | strcpy(filename, argv[1]); 51 | fp = fopen(filename, "r"); 52 | if (fp == NULL) 53 | { 54 | fprintf(stderr, "%s: Could not open '%s'\n", self, filename); 55 | exit(0); 56 | } 57 | fclose(fp); 58 | load_program(filename); 59 | if ( argv[1] == NULL ) 60 | { 61 | fakeargv[1] = "a.out"; 62 | fakeargv[2] = NULL; 63 | argv = fakeargv; 64 | ++argc; 65 | } 66 | disasm(memoffset, argc-1, argv+1); /* where things normally start */ 67 | } 68 | 69 | #define LOADSECTION(head) load_section(&head); 70 | 71 | load_section(hd) 72 | register SCNHDR *hd; 73 | { 74 | register int pc, i; 75 | if ( hd->s_scnptr != 0 ) { 76 | /* printf("loading %s\n", hd->s_name); */ 77 | pc = hd->s_vaddr; 78 | FSEEK(ldptr, hd->s_scnptr, 0); 79 | for ( i=0; is_size; ++i ) { 80 | if (pc-memoffset >= MEMSIZE) 81 | { printf("MEMSIZE too small. Fix and recompile.\n"); 82 | exit(1); } 83 | *(char *) ((mem-memoffset)+pc++) = getc(fp); 84 | } 85 | } 86 | } 87 | 88 | load_program(filename) 89 | char *filename; 90 | { 91 | ldptr = ldopen(filename, NULL); 92 | if ( ldptr == NULL ) 93 | { 94 | fprintf(stderr, "%s: Load read error on %s\n", self, filename); 95 | exit(0); 96 | } 97 | if ( TYPE(ldptr) != 0x162 ) 98 | { 99 | fprintf(stderr, 100 | "big-endian object file (little-endian interp)\n"); 101 | exit(0); 102 | } 103 | 104 | if ( ldnshread(ldptr, ".text", &texthead) != 1 ) 105 | printf("text section header missing\n"); 106 | else 107 | LOADSECTION(texthead) 108 | 109 | if ( ldnshread(ldptr, ".rdata", &rdatahead) != 1 ) 110 | printf("rdata section header missing\n"); 111 | else 112 | LOADSECTION(rdatahead) 113 | 114 | if ( ldnshread(ldptr, ".data", &datahead) != 1 ) 115 | printf("data section header missing\n"); 116 | else 117 | LOADSECTION(datahead) 118 | 119 | if ( ldnshread(ldptr, ".sdata", &sdatahead) != 1 ) 120 | printf("sdata section header missing\n"); 121 | else 122 | LOADSECTION(sdatahead) 123 | 124 | if ( ldnshread(ldptr, ".sbss", &sbsshead) != 1 ) 125 | printf("sbss section header missing\n"); 126 | else 127 | LOADSECTION(sbsshead) 128 | 129 | if ( ldnshread(ldptr, ".bss", &bsshead) != 1 ) 130 | printf("bss section header missing\n"); 131 | else 132 | LOADSECTION(bsshead) 133 | 134 | 135 | /* BSS is already zeroed (statically-allocated mem) */ 136 | /* this version ignores relocation info */ 137 | } 138 | 139 | 140 | int *m_alloc(n) 141 | int n; 142 | { 143 | extern char *malloc(); 144 | 145 | return (int *) (int) malloc((unsigned) n); 146 | } 147 | 148 | disasm(startpc, argc, argv) 149 | int startpc, argc; 150 | char *argv[]; 151 | { 152 | int i; 153 | 154 | pc = memoffset; 155 | for ( i=0; i swtch.s 133 | $(AS) -o switch.o swtch.s 134 | 135 | depend: $(CFILES) $(HFILES) 136 | $(CC) $(INCPATH) $(DEFINES) $(HOST) -DCHANGED -M $(CFILES) > makedep 137 | echo '/^# DO NOT DELETE THIS LINE/+2,$$d' >eddep 138 | echo '$$r makedep' >>eddep 139 | echo 'w' >>eddep 140 | ed - Makefile < eddep 141 | rm eddep makedep 142 | echo '# DEPENDENCIES MUST END AT END OF FILE' >> Makefile 143 | echo '# IF YOU PUT STUFF HERE IT WILL GO AWAY' >> Makefile 144 | echo '# see make depend above' >> Makefile 145 | -------------------------------------------------------------------------------- /userprog/syscall.h: -------------------------------------------------------------------------------- 1 | /* syscalls.h 2 | * Nachos system call interface. These are Nachos kernel operations 3 | * that can be invoked from user programs, by trapping to the kernel 4 | * via the "syscall" instruction. 5 | * 6 | * This file is included by user programs and by the Nachos kernel. 7 | * 8 | * Copyright (c) 1992-1993 The Regents of the University of California. 9 | * All rights reserved. See copyright.h for copyright notice and limitation 10 | * of liability and disclaimer of warranty provisions. 11 | */ 12 | 13 | #ifndef SYSCALLS_H 14 | #define SYSCALLS_H 15 | 16 | #include "copyright.h" 17 | 18 | /* system call codes -- used by the stubs to tell the kernel which system call 19 | * is being asked for 20 | */ 21 | #define SC_Halt 0 22 | #define SC_Exit 1 23 | #define SC_Exec 2 24 | #define SC_Join 3 25 | #define SC_Create 4 26 | #define SC_Open 5 27 | #define SC_Read 6 28 | #define SC_Write 7 29 | #define SC_Close 8 30 | #define SC_Fork 9 31 | #define SC_Yield 10 32 | #define SC_Pwd 11 33 | #define SC_Ls 12 34 | #define SC_Cd 13 35 | #define SC_Remove 14 36 | #define SC_CDir 15 37 | #define SC_RDir 16 38 | #define SC_Help 17 39 | #ifndef IN_ASM 40 | void Pwd(); 41 | void Ls(); 42 | void Cd(char *name); 43 | void Remove(char *name); 44 | void CDir(char *name); 45 | void RDir(char *name); 46 | void Help(); 47 | /* The system call interface. These are the operations the Nachos 48 | * kernel needs to support, to be able to run user programs. 49 | * 50 | * Each of these is invoked by a user program by simply calling the 51 | * procedure; an assembly language stub stuffs the system call code 52 | * into a register, and traps to the kernel. The kernel procedures 53 | * are then invoked in the Nachos kernel, after appropriate error checking, 54 | * from the system call entry point in exception.cc. 55 | */ 56 | 57 | /* Stop Nachos, and print out performance stats */ 58 | void Halt(); 59 | 60 | 61 | /* Address space control operations: Exit, Exec, and Join */ 62 | 63 | /* This user program is done (status = 0 means exited normally). */ 64 | void Exit(int status); 65 | 66 | /* A unique identifier for an executing user program (address space) */ 67 | typedef int SpaceId; 68 | 69 | /* Run the executable, stored in the Nachos file "name", and return the 70 | * address space identifier 71 | */ 72 | SpaceId Exec(char *name); 73 | 74 | /* Only return once the the user program "id" has finished. 75 | * Return the exit status. 76 | */ 77 | int Join(SpaceId id); 78 | 79 | 80 | /* File system operations: Create, Open, Read, Write, Close 81 | * These functions are patterned after UNIX -- files represent 82 | * both files *and* hardware I/O devices. 83 | * 84 | * If this assignment is done before doing the file system assignment, 85 | * note that the Nachos file system has a stub implementation, which 86 | * will work for the purposes of testing out these routines. 87 | */ 88 | 89 | /* A unique identifier for an open Nachos file. */ 90 | typedef int OpenFileId; 91 | 92 | /* when an address space starts up, it has two open files, representing 93 | * keyboard input and display output (in UNIX terms, stdin and stdout). 94 | * Read and Write can be used directly on these, without first opening 95 | * the console device. 96 | */ 97 | 98 | #define ConsoleInput 0 99 | #define ConsoleOutput 1 100 | 101 | /* Create a Nachos file, with "name" */ 102 | void Create(char *name); 103 | 104 | /* Open the Nachos file "name", and return an "OpenFileId" that can 105 | * be used to read and write to the file. 106 | */ 107 | OpenFileId Open(char *name); 108 | 109 | /* Write "size" bytes from "buffer" to the open file. */ 110 | void Write(char *buffer, int size, OpenFileId id); 111 | 112 | /* Read "size" bytes from the open file into "buffer". 113 | * Return the number of bytes actually read -- if the open file isn't 114 | * long enough, or if it is an I/O device, and there aren't enough 115 | * characters to read, return whatever is available (for I/O devices, 116 | * you should always wait until you can return at least one character). 117 | */ 118 | int Read(char *buffer, int size, OpenFileId id); 119 | 120 | /* Close the file, we're done reading and writing to it. */ 121 | void Close(OpenFileId id); 122 | 123 | 124 | 125 | /* User-level thread operations: Fork and Yield. To allow multiple 126 | * threads to run within a user program. 127 | */ 128 | 129 | /* Fork a thread to run a procedure ("func") in the *same* address space 130 | * as the current thread. 131 | */ 132 | void Fork(void (*func)()); 133 | 134 | /* Yield the CPU to another runnable thread, whether in this address space 135 | * or not. 136 | */ 137 | void Yield(); 138 | 139 | #endif /* IN_ASM */ 140 | 141 | #endif /* SYSCALL_H */ 142 | -------------------------------------------------------------------------------- /machine/network.cc: -------------------------------------------------------------------------------- 1 | // network.cc 2 | // Routines to simulate a network interface, using UNIX sockets 3 | // to deliver packets between multiple invocations of nachos. 4 | // 5 | // DO NOT CHANGE -- part of the machine emulation 6 | // 7 | // Copyright (c) 1992-1993 The Regents of the University of California. 8 | // All rights reserved. See copyright.h for copyright notice and limitation 9 | // of liability and disclaimer of warranty provisions. 10 | 11 | #include "copyright.h" 12 | #include "system.h" 13 | #ifdef HOST_SPARC 14 | #include 15 | #endif 16 | 17 | // Dummy functions because C++ can't call member functions indirectly 18 | static void NetworkReadPoll(int arg) 19 | { Network *net = (Network *)arg; net->CheckPktAvail(); } 20 | static void NetworkSendDone(int arg) 21 | { Network *net = (Network *)arg; net->SendDone(); } 22 | 23 | // Initialize the network emulation 24 | // addr is used to generate the socket name 25 | // reliability says whether we drop packets to emulate unreliable links 26 | // readAvail, writeDone, callArg -- analogous to console 27 | Network::Network(NetworkAddress addr, double reliability, 28 | VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg) 29 | { 30 | ident = addr; 31 | if (reliability < 0) chanceToWork = 0; 32 | else if (reliability > 1) chanceToWork = 1; 33 | else chanceToWork = reliability; 34 | 35 | // set up the stuff to emulate asynchronous interrupts 36 | writeHandler = writeDone; 37 | readHandler = readAvail; 38 | handlerArg = callArg; 39 | sendBusy = FALSE; 40 | inHdr.length = 0; 41 | 42 | sock = OpenSocket(); 43 | sprintf(sockName, "SOCKET_%d", (int)addr); 44 | AssignNameToSocket(sockName, sock); // Bind socket to a filename 45 | // in the current directory. 46 | 47 | // start polling for incoming packets 48 | interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); 49 | } 50 | 51 | Network::~Network() 52 | { 53 | CloseSocket(sock); 54 | DeAssignNameToSocket(sockName); 55 | } 56 | 57 | // if a packet is already buffered, we simply delay reading 58 | // the incoming packet. In real life, the incoming 59 | // packet might be dropped if we can't read it in time. 60 | void 61 | Network::CheckPktAvail() 62 | { 63 | // schedule the next time to poll for a packet 64 | interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); 65 | 66 | if (inHdr.length != 0) // do nothing if packet is already buffered 67 | return; 68 | if (!PollSocket(sock)) // do nothing if no packet to be read 69 | return; 70 | 71 | // otherwise, read packet in 72 | char *buffer = new char[MaxWireSize]; 73 | ReadFromSocket(sock, buffer, MaxWireSize); 74 | 75 | // divide packet into header and data 76 | inHdr = *(PacketHeader *)buffer; 77 | ASSERT((inHdr.to == ident) && (inHdr.length <= MaxPacketSize)); 78 | bcopy(buffer + sizeof(PacketHeader), inbox, inHdr.length); 79 | delete []buffer ; 80 | 81 | DEBUG('n', "Network received packet from %d, length %d...\n", 82 | (int) inHdr.from, inHdr.length); 83 | stats->numPacketsRecvd++; 84 | 85 | // tell post office that the packet has arrived 86 | (*readHandler)(handlerArg); 87 | } 88 | 89 | // notify user that another packet can be sent 90 | void 91 | Network::SendDone() 92 | { 93 | sendBusy = FALSE; 94 | stats->numPacketsSent++; 95 | (*writeHandler)(handlerArg); 96 | } 97 | 98 | // send a packet by concatenating hdr and data, and schedule 99 | // an interrupt to tell the user when the next packet can be sent 100 | // 101 | // Note we always pad out a packet to MaxWireSize before putting it into 102 | // the socket, because it's simpler at the receive end. 103 | void 104 | Network::Send(PacketHeader hdr, char* data) 105 | { 106 | char toName[32]; 107 | 108 | sprintf(toName, "SOCKET_%d", (int)hdr.to); 109 | 110 | ASSERT((sendBusy == FALSE) && (hdr.length > 0) 111 | && (hdr.length <= MaxPacketSize) && (hdr.from == ident)); 112 | DEBUG('n', "Sending to addr %d, %d bytes... ", hdr.to, hdr.length); 113 | 114 | interrupt->Schedule(NetworkSendDone, (int)this, NetworkTime, NetworkSendInt); 115 | 116 | if (Random() % 100 >= chanceToWork * 100) { // emulate a lost packet 117 | DEBUG('n', "oops, lost it!\n"); 118 | return; 119 | } 120 | 121 | // concatenate hdr and data into a single buffer, and send it out 122 | char *buffer = new char[MaxWireSize]; 123 | *(PacketHeader *)buffer = hdr; 124 | bcopy(data, buffer + sizeof(PacketHeader), hdr.length); 125 | SendToSocket(sock, buffer, MaxWireSize, toName); 126 | delete []buffer; 127 | } 128 | 129 | // read a packet, if one is buffered 130 | PacketHeader 131 | Network::Receive(char* data) 132 | { 133 | PacketHeader hdr = inHdr; 134 | 135 | inHdr.length = 0; 136 | if (hdr.length != 0) 137 | bcopy(inbox, data, hdr.length); 138 | return hdr; 139 | } 140 | -------------------------------------------------------------------------------- /bin/d.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 1992-1993 The Regents of the University of California. 3 | All rights reserved. See copyright.h for copyright notice and limitation 4 | of liability and disclaimer of warranty provisions. 5 | */ 6 | 7 | #include "copyright.h" 8 | #include "instr.h" 9 | #include "encode.h" 10 | 11 | #define NULL 0 12 | 13 | int sptr; 14 | int longdis = 1; 15 | 16 | extern char *normalops[], *specialops[]; 17 | 18 | 19 | char *regstrings[] = 20 | { 21 | "0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", 22 | "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", 23 | "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27", "gp", "sp", 24 | "r30", "r31" 25 | }; 26 | 27 | #define R(i) regstrings[i] 28 | 29 | 30 | dump_ascii(instruction, pc) 31 | int instruction, pc; 32 | { 33 | int addr; 34 | char *s; 35 | int opcode; 36 | 37 | if ( longdis ) printf("%08x: %08x ", pc, instruction); 38 | printf("\t"); 39 | opcode = (unsigned) instruction >> 26; 40 | if ( instruction == I_NOP) { 41 | printf("nop"); 42 | } 43 | else if ( opcode == I_SPECIAL ) 44 | { 45 | opcode = instruction & 0x3f; 46 | printf("%s\t", specialops[opcode]); 47 | 48 | switch( opcode ) 49 | { 50 | 51 | /* rd,rt,shamt */ 52 | case I_SLL: 53 | case I_SRL: 54 | case I_SRA: 55 | printf("%s,%s,0x%x", 56 | R(rd(instruction)), 57 | R(rt(instruction)), 58 | shamt(instruction)); 59 | break; 60 | 61 | /* rd,rt,rs */ 62 | case I_SLLV: 63 | case I_SRLV: 64 | case I_SRAV: 65 | printf("%s,%s,%s", 66 | R(rd(instruction)), 67 | R(rt(instruction)), 68 | R(rs(instruction))); 69 | break; 70 | 71 | /* rs */ 72 | case I_JR: 73 | case I_JALR: 74 | case I_MFLO: 75 | case I_MTLO: 76 | printf("%s", R(rs(instruction))); 77 | break; 78 | 79 | case I_SYSCALL: 80 | case I_BREAK: 81 | break; 82 | 83 | /* rd */ 84 | case I_MFHI: 85 | case I_MTHI: 86 | printf("%s", R(rd(instruction))); 87 | break; 88 | 89 | /* rs,rt */ 90 | case I_MULT: 91 | case I_MULTU: 92 | case I_DIV: 93 | case I_DIVU: 94 | printf("%s,%s", 95 | R(rs(instruction)), 96 | R(rt(instruction))); 97 | break; 98 | 99 | /* rd,rs,rt */ 100 | case I_ADD: 101 | case I_ADDU: 102 | case I_SUB: 103 | case I_SUBU: 104 | case I_AND: 105 | case I_OR: 106 | case I_XOR: 107 | case I_NOR: 108 | case I_SLT: 109 | case I_SLTU: 110 | printf("%s,%s,%s", 111 | R(rd(instruction)), 112 | R(rs(instruction)), 113 | R(rt(instruction))); 114 | break; 115 | 116 | } 117 | } 118 | else if ( opcode == I_BCOND ) 119 | { 120 | switch ( rt(instruction) ) /* this field encodes the op */ 121 | { 122 | case I_BLTZ: 123 | printf("bltz"); 124 | break; 125 | case I_BGEZ: 126 | printf("bgez"); 127 | break; 128 | case I_BLTZAL: 129 | printf("bltzal"); 130 | break; 131 | case I_BGEZAL: 132 | printf("bgezal"); 133 | break; 134 | default : 135 | printf("BCOND"); 136 | } 137 | printf("\t%s,%08x", 138 | R(rs(instruction)), 139 | off16(instruction)+pc+4); 140 | } 141 | else 142 | { 143 | printf("%s\t", normalops[opcode]); 144 | 145 | switch ( opcode ) 146 | { 147 | /* 26-bit_target */ 148 | case I_J: 149 | case I_JAL: 150 | printf("%08x", 151 | top4(pc)|off26(instruction)); 152 | break; 153 | 154 | /* rs,rt,16-bit_offset */ 155 | case I_BEQ: 156 | case I_BNE: 157 | printf("%s,%s,%08x", 158 | R(rt(instruction)), 159 | R(rs(instruction)), 160 | off16(instruction)+pc+4); 161 | break; 162 | 163 | /* rt,rs,immediate */ 164 | case I_ADDI: 165 | case I_ADDIU: 166 | case I_SLTI: 167 | case I_SLTIU: 168 | case I_ANDI: 169 | case I_ORI: 170 | case I_XORI: 171 | printf("%s,%s,0x%x", 172 | R(rt(instruction)), 173 | R(rs(instruction)), 174 | immed(instruction)); 175 | break; 176 | 177 | /* rt, immed */ 178 | case I_LUI: 179 | printf("%s,0x%x", 180 | R(rt(instruction)), 181 | immed(instruction)); 182 | break; 183 | 184 | /* coprocessor garbage */ 185 | case I_COP0: 186 | case I_COP1: 187 | case I_COP2: 188 | case I_COP3: 189 | break; 190 | 191 | /* rt,offset(rs) */ 192 | case I_LB: 193 | case I_LH: 194 | case I_LWL: 195 | case I_LW: 196 | case I_LBU: 197 | case I_LHU: 198 | case I_LWR: 199 | case I_SB: 200 | case I_SH: 201 | case I_SWL: 202 | case I_SW: 203 | case I_SWR: 204 | case I_LWC0: 205 | case I_LWC1: 206 | case I_LWC2: 207 | case I_LWC3 : 208 | case I_SWC0: 209 | case I_SWC1: 210 | case I_SWC2: 211 | case I_SWC3: 212 | printf("%s,0x%x(%s)", 213 | R(rt(instruction)), 214 | immed(instruction), 215 | R(rs(instruction))); 216 | break; 217 | } 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /machine/interrupt.h: -------------------------------------------------------------------------------- 1 | // interrupt.h 2 | // Data structures to emulate low-level interrupt hardware. 3 | // 4 | // The hardware provides a routine (SetLevel) to enable or disable 5 | // interrupts. 6 | // 7 | // In order to emulate the hardware, we need to keep track of all 8 | // interrupts the hardware devices would cause, and when they 9 | // are supposed to occur. 10 | // 11 | // This module also keeps track of simulated time. Time advances 12 | // only when the following occur: 13 | // interrupts are re-enabled 14 | // a user instruction is executed 15 | // there is nothing in the ready queue 16 | // 17 | // As a result, unlike real hardware, interrupts (and thus time-slice 18 | // context switches) cannot occur anywhere in the code where interrupts 19 | // are enabled, but rather only at those places in the code where 20 | // simulated time advances (so that it becomes time to invoke an 21 | // interrupt in the hardware simulation). 22 | // 23 | // NOTE: this means that incorrectly synchronized code may work 24 | // fine on this hardware simulation (even with randomized time slices), 25 | // but it wouldn't work on real hardware. (Just because we can't 26 | // always detect when your program would fail in real life, does not 27 | // mean it's ok to write incorrectly synchronized code!) 28 | // 29 | // DO NOT CHANGE -- part of the machine emulation 30 | // 31 | // Copyright (c) 1992-1993 The Regents of the University of California. 32 | // All rights reserved. See copyright.h for copyright notice and limitation 33 | // of liability and disclaimer of warranty provisions. 34 | 35 | #ifndef INTERRUPT_H 36 | #define INTERRUPT_H 37 | 38 | #include "copyright.h" 39 | #include "list.h" 40 | 41 | // Interrupts can be disabled (IntOff) or enabled (IntOn) 42 | enum IntStatus { IntOff, IntOn }; 43 | 44 | // Nachos can be running kernel code (SystemMode), user code (UserMode), 45 | // or there can be no runnable thread, because the ready list 46 | // is empty (IdleMode). 47 | enum MachineStatus {IdleMode, SystemMode, UserMode}; 48 | 49 | // IntType records which hardware device generated an interrupt. 50 | // In Nachos, we support a hardware timer device, a disk, a console 51 | // display and keyboard, and a network. 52 | enum IntType { TimerInt, DiskInt, ConsoleWriteInt, ConsoleReadInt, 53 | ElevatorInt, NetworkSendInt, NetworkRecvInt}; 54 | 55 | // The following class defines an interrupt that is scheduled 56 | // to occur in the future. The internal data structures are 57 | // left public to make it simpler to manipulate. 58 | 59 | class PendingInterrupt { 60 | public: 61 | PendingInterrupt(VoidFunctionPtr func, int param, int time, IntType kind); 62 | // initialize an interrupt that will 63 | // occur in the future 64 | 65 | VoidFunctionPtr handler; // The function (in the hardware device 66 | // emulator) to call when the interrupt occurs 67 | int arg; // The argument to the function. 68 | int when; // When the interrupt is supposed to fire 69 | IntType type; // for debugging 70 | }; 71 | 72 | // The following class defines the data structures for the simulation 73 | // of hardware interrupts. We record whether interrupts are enabled 74 | // or disabled, and any hardware interrupts that are scheduled to occur 75 | // in the future. 76 | 77 | class Interrupt { 78 | public: 79 | Interrupt(); // initialize the interrupt simulation 80 | ~Interrupt(); // de-allocate data structures 81 | 82 | IntStatus SetLevel(IntStatus level);// Disable or enable interrupts 83 | // and return previous setting. 84 | 85 | void Enable(); // Enable interrupts. 86 | IntStatus getLevel() {return level;}// Return whether interrupts 87 | // are enabled or disabled 88 | 89 | void Idle(); // The ready queue is empty, roll 90 | // simulated time forward until the 91 | // next interrupt 92 | 93 | void Halt(); // quit and print out stats 94 | 95 | void YieldOnReturn(); // cause a context switch on return 96 | // from an interrupt handler 97 | 98 | MachineStatus getStatus() { return status; } // idle, kernel, user 99 | void setStatus(MachineStatus st) { status = st; } 100 | 101 | void DumpState(); // Print interrupt state 102 | 103 | 104 | // NOTE: the following are internal to the hardware simulation code. 105 | // DO NOT call these directly. I should make them "private", 106 | // but they need to be public since they are called by the 107 | // hardware device simulators. 108 | 109 | void Schedule(VoidFunctionPtr handler,// Schedule an interrupt to occur 110 | int arg, int when, IntType type);// at time ``when''. This is called 111 | // by the hardware device simulators. 112 | 113 | void OneTick(); // Advance simulated time 114 | 115 | private: 116 | IntStatus level; // are interrupts enabled or disabled? 117 | List *pending; // the list of interrupts scheduled 118 | // to occur in the future 119 | bool inHandler; // TRUE if we are running an interrupt handler 120 | bool yieldOnReturn; // TRUE if we are to context switch 121 | // on return from the interrupt handler 122 | MachineStatus status; // idle, kernel mode, user mode 123 | 124 | // these functions are internal to the interrupt simulation code 125 | 126 | bool CheckIfDue(bool advanceClock); // Check if an interrupt is supposed 127 | // to occur now 128 | 129 | void ChangeLevel(IntStatus old, // SetLevel, without advancing the 130 | IntStatus now); // simulated time 131 | }; 132 | 133 | #endif // INTERRRUPT_H 134 | -------------------------------------------------------------------------------- /userprog/bitmap.cc: -------------------------------------------------------------------------------- 1 | // bitmap.c 2 | // Routines to manage a bitmap -- an array of bits each of which 3 | // can be either on or off. Represented as an array of integers. 4 | // 5 | // Copyright (c) 1992-1993 The Regents of the University of California. 6 | // All rights reserved. See copyright.h for copyright notice and limitation 7 | // of liability and disclaimer of warranty provisions. 8 | 9 | #include "copyright.h" 10 | #include "bitmap.h" 11 | #include "thread.h" 12 | #include "system.h" 13 | //---------------------------------------------------------------------- 14 | // BitMap::BitMap 15 | // Initialize a bitmap with "nitems" bits, so that every bit is clear. 16 | // it can be added somewhere on a list. 17 | // 18 | // "nitems" is the number of bits in the bitmap. 19 | //---------------------------------------------------------------------- 20 | 21 | BitMap::BitMap(int nitems) 22 | { 23 | numBits = nitems; 24 | numWords = divRoundUp(numBits, BitsInWord); 25 | map = new unsigned int[numWords]; 26 | 27 | for (int i = 0; i < numBits; i++) 28 | Clear(i); 29 | } 30 | 31 | //---------------------------------------------------------------------- 32 | // BitMap::~BitMap 33 | // De-allocate a bitmap. 34 | //---------------------------------------------------------------------- 35 | 36 | BitMap::~BitMap() 37 | { 38 | delete map; 39 | } 40 | 41 | //---------------------------------------------------------------------- 42 | // BitMap::Set 43 | // Set the "nth" bit in a bitmap. 44 | // 45 | // "which" is the number of the bit to be set. 46 | //---------------------------------------------------------------------- 47 | 48 | void 49 | BitMap::Mark(int which) 50 | { 51 | ASSERT(which >= 0 && which < numBits); 52 | map[which / BitsInWord] |= 1 << (which % BitsInWord); 53 | 54 | } 55 | 56 | //---------------------------------------------------------------------- 57 | // BitMap::Clear 58 | // Clear the "nth" bit in a bitmap. 59 | // 60 | // "which" is the number of the bit to be cleared. 61 | //---------------------------------------------------------------------- 62 | 63 | void 64 | BitMap::Clear(int which) 65 | { 66 | ASSERT(which >= 0 && which < numBits); 67 | map[which / BitsInWord] &= ~(1 << (which % BitsInWord)); 68 | printf("BitMap->Clear() clear %d.\n",which); 69 | } 70 | 71 | //---------------------------------------------------------------------- 72 | // BitMap::Test 73 | // Return TRUE if the "nth" bit is set. 74 | // 75 | // "which" is the number of the bit to be tested. 76 | //---------------------------------------------------------------------- 77 | 78 | bool 79 | BitMap::Test(int which) 80 | { 81 | ASSERT(which >= 0 && which < numBits); 82 | 83 | if (map[which / BitsInWord] & (1 << (which % BitsInWord))) 84 | return TRUE; 85 | else 86 | return FALSE; 87 | } 88 | 89 | //---------------------------------------------------------------------- 90 | // BitMap::Find 91 | // Return the number of the first bit which is clear. 92 | // As a side effect, set the bit (mark it as in use). 93 | // (In other words, find and allocate a bit.) 94 | // 95 | // If no bits are clear, return -1. 96 | //---------------------------------------------------------------------- 97 | 98 | int 99 | BitMap::Find() 100 | { 101 | for (int i = 0; i < numBits; i++) 102 | if (!Test(i)) { 103 | Mark(i); 104 | printf("BitMap->Find() find the page %d.\n",i); 105 | 106 | return i; 107 | } 108 | return -1; 109 | } 110 | 111 | //---------------------------------------------------------------------- 112 | // BitMap::NumClear 113 | // Return the number of clear bits in the bitmap. 114 | // (In other words, how many bits are unallocated?) 115 | //---------------------------------------------------------------------- 116 | 117 | int 118 | BitMap::NumClear() 119 | { 120 | int count = 0; 121 | 122 | for (int i = 0; i < numBits; i++) 123 | if (!Test(i)) count++; 124 | return count; 125 | } 126 | 127 | //---------------------------------------------------------------------- 128 | // BitMap::Print 129 | // Print the contents of the bitmap, for debugging. 130 | // 131 | // Could be done in a number of ways, but we just print the #'s of 132 | // all the bits that are set in the bitmap. 133 | //---------------------------------------------------------------------- 134 | 135 | void 136 | BitMap::Print() 137 | { 138 | printf("Bitmap set:\n"); 139 | for (int i = 0; i < numBits; i++) 140 | if (Test(i)) 141 | printf("%d, ", i); 142 | printf("\n"); 143 | } 144 | 145 | // These aren't needed until the FILESYS assignment 146 | 147 | //---------------------------------------------------------------------- 148 | // BitMap::FetchFromFile 149 | // Initialize the contents of a bitmap from a Nachos file. 150 | // 151 | // "file" is the place to read the bitmap from 152 | //---------------------------------------------------------------------- 153 | 154 | void 155 | BitMap::FetchFrom(OpenFile *file) 156 | { 157 | file->ReadAt((char *)map, numWords * sizeof(unsigned), 0); 158 | } 159 | 160 | //---------------------------------------------------------------------- 161 | // BitMap::WriteBack 162 | // Store the contents of a bitmap to a Nachos file. 163 | // 164 | // "file" is the place to write the bitmap to 165 | //---------------------------------------------------------------------- 166 | 167 | void 168 | BitMap::WriteBack(OpenFile *file) 169 | { 170 | file->WriteAt((char *)map, numWords * sizeof(unsigned), 0); 171 | } 172 | 173 | int 174 | BitMap::myFind(int cnt) 175 | { 176 | for (int i = 0; i < numBits; i++){ 177 | int flag = 1; 178 | for(int j = 0; j < cnt; j++) 179 | if(Test(i+j)) flag = 0; 180 | if(flag){ 181 | for(int j = 0; j < cnt; j++) 182 | Mark(i+j); 183 | return i; 184 | } 185 | } 186 | return -1; 187 | } 188 | -------------------------------------------------------------------------------- /threads/synch.h: -------------------------------------------------------------------------------- 1 | // synch.h 2 | // Data structures for synchronizing threads. 3 | // 4 | // Three kinds of synchronization are defined here: semaphores, 5 | // locks, and condition variables. The implementation for 6 | // semaphores is given; for the latter two, only the procedure 7 | // interface is given -- they are to be implemented as part of 8 | // the first assignment. 9 | // 10 | // Note that all the synchronization objects take a "name" as 11 | // part of the initialization. This is solely for debugging purposes. 12 | // 13 | // Copyright (c) 1992-1993 The Regents of the University of California. 14 | // All rights reserved. See copyright.h for copyright notice and limitation 15 | // synch.h -- synchronization primitives. 16 | 17 | #ifndef SYNCH_H 18 | #define SYNCH_H 19 | 20 | #include "copyright.h" 21 | #include "thread.h" 22 | #include "list.h" 23 | 24 | // The following class defines a "semaphore" whose value is a non-negative 25 | // integer. The semaphore has only two operations P() and V(): 26 | // 27 | // P() -- waits until value > 0, then decrement 28 | // 29 | // V() -- increment, waking up a thread waiting in P() if necessary 30 | // 31 | // Note that the interface does *not* allow a thread to read the value of 32 | // the semaphore directly -- even if you did read the value, the 33 | // only thing you would know is what the value used to be. You don't 34 | // know what the value is now, because by the time you get the value 35 | // into a register, a context switch might have occurred, 36 | // and some other thread might have called P or V, so the true value might 37 | // now be different. 38 | 39 | class Semaphore { 40 | public: 41 | Semaphore(char* debugName, int initialValue); // set initial value 42 | Semaphore(); 43 | ~Semaphore(); // de-allocate semaphore 44 | char* getName() { return name;} // debugging assist 45 | 46 | void P(); // these are the only operations on a semaphore 47 | void V(); // they are both *atomic* 48 | 49 | private: 50 | char* name; // useful for debugging 51 | int value; // semaphore value, always >= 0 52 | List *queue; // threads waiting in P() for the value to be > 0 53 | }; 54 | 55 | // The following class defines a "lock". A lock can be BUSY or FREE. 56 | // There are only two operations allowed on a lock: 57 | // 58 | // Acquire -- wait until the lock is FREE, then set it to BUSY 59 | // 60 | // Release -- set lock to be FREE, waking up a thread waiting 61 | // in Acquire if necessary 62 | // 63 | // In addition, by convention, only the thread that acquired the lock 64 | // may release it. As with semaphores, you can't read the lock value 65 | // (because the value might change immediately after you read it). 66 | 67 | class Lock { 68 | public: 69 | Lock(char* debugName); // initialize lock to be FREE 70 | ~Lock(); // deallocate lock 71 | char* getName() { return name; } // debugging assist 72 | 73 | void Acquire(); // these are the only operations on a lock 74 | void Release(); // they are both *atomic* 75 | 76 | bool isHeldByCurrentThread(); // true if the current thread 77 | // holds this lock. Useful for 78 | // checking in Release, and in 79 | // Condition variable ops below. 80 | 81 | private: 82 | char* name; // for debugging 83 | Semaphore *lock; 84 | Thread *owner; 85 | // plus some other stuff you'll need to define 86 | }; 87 | 88 | // The following class defines a "condition variable". A condition 89 | // variable does not have a value, but threads may be queued, waiting 90 | // on the variable. These are only operations on a condition variable: 91 | // 92 | // Wait() -- release the lock, relinquish the CPU until signaled, 93 | // then re-acquire the lock 94 | // 95 | // Signal() -- wake up a thread, if there are any waiting on 96 | // the condition 97 | // 98 | // Broadcast() -- wake up all threads waiting on the condition 99 | // 100 | // All operations on a condition variable must be made while 101 | // the current thread has acquired a lock. Indeed, all accesses 102 | // to a given condition variable must be protected by the same lock. 103 | // In other words, mutual exclusion must be enforced among threads calling 104 | // the condition variable operations. 105 | // 106 | // In Nachos, condition variables are assumed to obey *Mesa*-style 107 | // semantics. When a Signal or Broadcast wakes up another thread, 108 | // it simply puts the thread on the ready list, and it is the responsibility 109 | // of the woken thread to re-acquire the lock (this re-acquire is 110 | // taken care of within Wait()). By contrast, some define condition 111 | // variables according to *Hoare*-style semantics -- where the signalling 112 | // thread gives up control over the lock and the CPU to the woken thread, 113 | // which runs immediately and gives back control over the lock to the 114 | // signaller when the woken thread leaves the critical section. 115 | // 116 | // The consequence of using Mesa-style semantics is that some other thread 117 | // can acquire the lock, and change data structures, before the woken 118 | // thread gets a chance to run. 119 | 120 | class Condition { 121 | public: 122 | Condition(char* debugName); // initialize condition to 123 | // "no one waiting" 124 | ~Condition(); // deallocate the condition 125 | char* getName() { return (name); } 126 | 127 | void Wait(Lock *conditionLock); // these are the 3 operations on 128 | // condition variables; releasing the 129 | // lock and going to sleep are 130 | // *atomic* in Wait() 131 | void Signal(Lock *conditionLock); // conditionLock must be held by 132 | void Broadcast(Lock *conditionLock);// the currentThread for all of 133 | // these operations 134 | 135 | private: 136 | char* name; 137 | List *waitQueue; 138 | // plus some other stuff you'll need to define 139 | }; 140 | #endif // SYNCH_H 141 | --------------------------------------------------------------------------------