init: init nachos hw01

This commit is contained in:
AFS_TA
2026-03-27 10:12:37 +08:00
commit 1e698d2426
240 changed files with 76969 additions and 0 deletions

12
code/test/AFS_students.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
make clean
make
../build.linux/nachos -e consoleIO_test1
echo "====================================="
../build.linux/nachos -e consoleIO_test2
echo "====================================="
../build.linux/nachos -e fileIO_test1
echo "====================================="
../build.linux/nachos -e fileIO_test2
echo "====================================="

BIN
code/test/DISK_0 Executable file

Binary file not shown.

165
code/test/Makefile Executable file
View File

@@ -0,0 +1,165 @@
#
# Makefile for building user programs to run on top of Nachos
#
# Use "make" to build the test executable(s)
# Use "make clean" to remove .o files and .coff files
# Use "make distclean" to remove all files produced by make, including
# the test executables
#
# This is a GNU Makefile. It must be used with the GNU make program.
# At UW, the GNU make program is /software/gnu/bin/make.
# In many other places it is known as "gmake".
# You may wish to include /software/gnu/bin/ early in your command
# search path, so that you will be using GNU make when you type "make".
#
# Several things to be aware of:
#
# It should not be necessary to build the test executables for
# every type of host machine on which Nachos runs. You should
# be able to build them once, and then use them regardless of
# the host machine type. That is because the test executables
# run on the simulated MIPS machine, and not on the host.
#
# However:
# (1) if you are experiencing problems with the test executables,
# it would be prudent to rebuild them on the host machine
# on which you are currently running Nachos. To do this,
# just type "make distclean", and then "make"
#
# (2) the procedure used to build the test executables does
# depend on the host machine you are on. All of the machine
# dependencies are isolated in the Makefile.dep file.
# It should be possible to build the test executables on
# any MFCF machine. In the MFCF environment, this makefile
# should automatically figure out what type of host you are
# on, and should use the appropriate procedure.
# However, if you are working outside the MFCF environment,
# you will need to build a cross-compiler, build coff2noff,
# and edit Makefile.dep in this directory before you
# can build the test programs.
#
# Nachos assumes that the location of the program startup routine (the
# location the kernel jumps to when the program initially starts up)
# is at location 0. This means: start.o must be the first .o passed
# to ld, in order for the routine "Start" to be loaded at location 0
#
# When you make the test programs, you will see messages like these:
# numsections 3
# Loading 3 sections:
# ".text", filepos 0xd0, mempos 0x0, size 0x440
# ".data", filepos 0x510, mempos 0x440, size 0x0
# ".bss", filepos 0x0, mempos 0x440, size 0x12c0
# These messages are normal. They come from the coff2noff program.
# They are useful in that they tell you how big the various parts of your
# compiled user program are, and where in the address space
# coff2noff is going to place them. This information is also
# recorded in the header of the executable file that coff2noff
# creates. See the method AddrSpace::Load (in userprog/addrspace.cc)
# for an example of how this header is used by the Nachos OS to set up the
# address space for a new process that will run the executable.
#
#
# Adding New Test Programs:
#
# You are free to write new test programs, and to modify the
# existing programs. If you write a new program, you will
# need to modify this makefile so that the new program will
# get built.
# You will need to make the following changes for each program
# you add:
# (1) add the program's name to PROGRAMS variable definition
# (2) add dependencies and build commands for the new
# program. The easiest way to do this is to
# copy the dependencies and commands for an
# existing program, and then change the names.
#
# For example, if you write a test program in foo.c, for which
# the executable is to be called foo, you should do the following:
#
# change the PROGRAMS definition to look like this:
#
# PROGRAMS = halt shell matmult sort foo
#
# add these dependencies/commands:
#
# foo.o: foo.c
# $(CC) $(CFLAGS) -c foo.c
# foo: foo.o start.o
# $(LD) $(LDFLAGS) start.o foo.o -o foo.coff
# $(COFF2NOFF) foo.coff foo
#
# Be careful when you copy the commands! The commands
# must be indented with a *TAB*, not a bunch of spaces.
#
#
#############################################################################
# Makefile.dep contains all machine-dependent definitions
# If you are trying to build coff2noff somewhere outside
# of the MFCF environment, you will almost certainly want
# to visit and edit Makefile.dep before doing so
#############################################################################
include Makefile.dep
CC = $(GCCDIR)gcc
AS = $(GCCDIR)as
LD = $(GCCDIR)ld
INCDIR =-I../userprog -I../lib
CFLAGS = -G 0 -c $(INCDIR) -B/usr/bin/local/nachos/lib/gcc-lib/decstation-ultrix/2.95.2/ -B/usr/bin/local/nachos/decstation-ultrix/bin/
ifeq ($(hosttype),unknown)
PROGRAMS = unknownhost
else
# change this if you create a new test program!
PROGRAMS = halt consoleIO_test1 consoleIO_test2 fileIO_test1 fileIO_test2
endif
all: $(PROGRAMS)
start.o: start.S ../userprog/syscall.h
$(CC) $(CFLAGS) $(ASFLAGS) -c start.S
halt.o: halt.c
$(CC) $(CFLAGS) -c halt.c
halt: halt.o start.o
$(LD) $(LDFLAGS) start.o halt.o -o halt.coff
$(COFF2NOFF) halt.coff halt
consoleIO_test1.o: consoleIO_test1.c
$(CC) $(CFLAGS) -c consoleIO_test1.c
consoleIO_test1: consoleIO_test1.o start.o
$(LD) $(LDFLAGS) start.o consoleIO_test1.o -o consoleIO_test1.coff
$(COFF2NOFF) consoleIO_test1.coff consoleIO_test1
consoleIO_test2.o: consoleIO_test2.c
$(CC) $(CFLAGS) -c consoleIO_test2.c
consoleIO_test2: consoleIO_test2.o start.o
$(LD) $(LDFLAGS) start.o consoleIO_test2.o -o consoleIO_test2.coff
$(COFF2NOFF) consoleIO_test2.coff consoleIO_test2
fileIO_test1.o: fileIO_test1.c
$(CC) $(CFLAGS) -c fileIO_test1.c
fileIO_test1: fileIO_test1.o start.o
$(LD) $(LDFLAGS) start.o fileIO_test1.o -o fileIO_test1.coff
$(COFF2NOFF) fileIO_test1.coff fileIO_test1
fileIO_test2.o: fileIO_test2.c
$(CC) $(CFLAGS) -c fileIO_test2.c
fileIO_test2: fileIO_test2.o start.o
$(LD) $(LDFLAGS) start.o fileIO_test2.o -o fileIO_test2.coff
$(COFF2NOFF) fileIO_test2.coff fileIO_test2
clean:
$(RM) -f *.o *.ii
$(RM) -f *.coff
distclean: clean
$(RM) -f $(PROGRAMS)
unknownhost:
@echo Host type could not be determined.
@echo make is terminating.
@echo If you are on an MFCF machine, contact the instructor to report this problem
@echo Otherwise, edit Makefile.dep and try again.

63
code/test/Makefile.dep Executable file
View File

@@ -0,0 +1,63 @@
#############################################################################
# Machine-specific definitions
#
# If you are not in the MFCF environment, you can either add a new
# automatic test for your machine/OS type, or you should set the
# necessary variables "manually" here
#############################################################################
# unfortunately, command line arguments to uname are not
# very consistent across UNIX flavours. However, the following
# seem to work almost everywhere in MFCF land
osname = $(shell uname -s)
osrelease = $(shell uname -r)
hosttype = unknown
# Test for x86 Linux
# !!! COMMENT THE FOLLOWING LINES OUT IF BUILDING FOR SOLARIS HOST !!!
# !!! ADD PATH TO CPP and CROSS COMPILER
ifeq ($(osname),Linux)
# full path name of your cpp program i.e.:
CPP = ../../usr/local/nachos/lib/gcc-lib/decstation-ultrix/2.95.2/cpp
# directory in which your gcc cross-compiler lives i.e.:
GCCDIR = ../../usr/local/nachos/bin/decstation-ultrix-
LDFLAGS = -T script -N
ASFLAGS = -mips2
CPPFLAGS = $(INCDIR)
COFF2NOFF = ../../coff2noff/coff2noff.x86Linux
hosttype = x86Linux
endif
ifeq ($(osname),Windows)
CPP = /usr/local/nachosxdev/lib/gcc-lib/decstation-ultrix/2.95.3/cpp0
# directory in which your gcc cross-compiler lives i.e.:
GCCDIR = /usr/local/nachosxdev/bin/decstation-ultrix-
LDFLAGS = -T script -N
ASFLAGS = -mips2
CPPFLAGS = $(INCDIR)
COFF2NOFF = ../../coff2noff/coff2noff.Windows
hosttype = Windows
endif
# Note:
# If you are trying to build on MacOS X
# try something like this, substituting whatever
# uname -s returns on your machine for the XXX
#
#ifeq ($(osname),XXX)
#CPP = full path name of your cpp program
#GCCDIR = directory in which your gcc cross-compiler lives
#LDFLAGS = -T script -N
#ASFLAGS = -mips2
#CPPFLAGS = $(INCDIR)
#COFF2NOFF = full pathname of your coff2noff program
# Note: it has been moved to part of the Nachos distribution
# COFF2NOFF = ../../coff2noff.mipsUltrix
#hosttype = MacOS
#endif

7
code/test/compile.sh Executable file
View File

@@ -0,0 +1,7 @@
cd ../build.linux
make depend
make clean
make
cd ../test
make clean
make

12
code/test/consoleIO_test1.c Executable file
View File

@@ -0,0 +1,12 @@
#include "syscall.h"
int
main()
{
int n;
for (n=9;n>5;n--) {
PrintInt(n);
}
Halt();
}

13
code/test/consoleIO_test2.c Executable file
View File

@@ -0,0 +1,13 @@
#include "syscall.h"
int
main()
{
int n;
for (n=15;n<=19;n++){
PrintInt(n);
}
Halt();
}

20
code/test/fileIO_test1.c Executable file
View File

@@ -0,0 +1,20 @@
#include "syscall.h"
int main(void)
{
char test[] = "abcdefghijklmnopqrstuvwxyz";
int success = Create("file1.test");
OpenFileId fid;
int i;
if (success != 1) MSG("Failed on creating file");
fid = Open("file1.test");
if (fid <= 0) MSG("Failed on opening file");
for (i = 0; i < 26; ++i) {
int count = Write(test + i, 1, fid);
if (count != 1) MSG("Failed on writing file");
}
success = Close(fid);
if (success != 1) MSG("Failed on closing file");
Halt();
}

22
code/test/fileIO_test2.c Executable file
View File

@@ -0,0 +1,22 @@
#include "syscall.h"
int main(void)
{
// you should run fileIO_test1 first before running this one
char test[26];
char check[] = "abcdefghijklmnopqrstuvwxyz";
OpenFileId fid;
int count, success, i;
fid = Open("file1.test");
if (fid <= 0) MSG("Failed on opening file");
count = Read(test, 26, fid);
if (count != 26) MSG("Failed on reading file");
success = Close(fid);
if (success != 1) MSG("Failed on closing file");
for (i = 0; i < 26; ++i) {
if (test[i] != check[i]) MSG("Failed: reading wrong result");
}
MSG("Passed! ^_^");
Halt();
}

20
code/test/halt.c Executable file
View File

@@ -0,0 +1,20 @@
/* halt.c
* Simple program to test whether running a user program works.
*
* Just do a "syscall" that shuts down the OS.
*
* NOTE: for some reason, user programs with global data structures
* sometimes haven't worked in the Nachos environment. So be careful
* out there! One option is to allocate data structures as
* automatics within a procedure, but if you do this, you have to
* be careful to allocate a big enough stack to hold the automatics!
*/
#include "syscall.h"
int
main()
{
Halt();
/* not reached */
}

36
code/test/script Executable file
View File

@@ -0,0 +1,36 @@
OUTPUT_FORMAT("ecoff-littlemips")
ENTRY(__start)
SECTIONS
{
.text 0 : {
_ftext = . ;
*(.init)
eprol = .;
*(.text)
*(.fini)
etext = .;
_etext = .;
}
.rdata . : {
*(.rdata)
}
_fdata = .;
.data . : {
*(.data)
CONSTRUCTORS
}
edata = .;
_edata = .;
_fbss = .;
.sbss . : {
*(.sbss)
*(.scommon)
}
.bss . : {
*(.bss)
*(COMMON)
}
end = .;
_end = .;
}

196
code/test/start.S Executable file
View File

@@ -0,0 +1,196 @@
/* Start.s
* Assembly language assist for user programs running on top of Nachos.
*
* Since we don't want to pull in the entire C library, we define
* what we need for a user program here, namely Start and the system
* calls.
*/
#define IN_ASM
#include "syscall.h"
.text
.align 2
/* -------------------------------------------------------------
* __start
* Initialize running a C program, by calling "main".
*
* NOTE: This has to be first, so that it gets loaded at location 0.
* The Nachos kernel always starts a program by jumping to location 0.
* -------------------------------------------------------------
*/
.globl __start
.ent __start
__start:
jal main
move $4,$0
jal Exit /* if we return from main, exit(0) */
.end __start
/* -------------------------------------------------------------
* System call stubs:
* Assembly language assist to make system calls to the Nachos kernel.
* There is one stub per system call, that places the code for the
* system call into register r2, and leaves the arguments to the
* system call alone (in other words, arg1 is in r4, arg2 is
* in r5, arg3 is in r6, arg4 is in r7)
*
* The return value is in r2. This follows the standard C calling
* convention on the MIPS.
* -------------------------------------------------------------
*/
.globl Halt
.ent Halt
Halt:
addiu $2,$0,SC_Halt
syscall
j $31
.end Halt
.globl MSG
.ent MSG
MSG:
addiu $2,$0,SC_MSG
syscall
j $31
.end MSG
.globl Add
.ent Add
Add:
addiu $2,$0,SC_Add
syscall
j $31
.end Add
.globl Exit
.ent Exit
Exit:
addiu $2,$0,SC_Exit
syscall
j $31
.end Exit
.globl Exec
.ent Exec
Exec:
addiu $2,$0,SC_Exec
syscall
j $31
.end Exec
.globl ExecV
.ent ExecV
ExecV:
addiu $2,$0,SC_ExecV
syscall
j $31
.end ExecV
.globl Join
.ent Join
Join:
addiu $2,$0,SC_Join
syscall
j $31
.end Join
.globl Create
.ent Create
Create:
addiu $2,$0,SC_Create
syscall
j $31
.end Create
.globl Remove
.ent Remove
Remove:
addiu $2,$0,SC_Remove
syscall
j $31
.end Remove
.globl Open
.ent Open
Open:
addiu $2,$0,SC_Open
syscall
j $31
.end Open
.globl Read
.ent Read
Read:
addiu $2,$0,SC_Read
syscall
j $31
.end Read
.globl Write
.ent Write
Write:
addiu $2,$0,SC_Write
syscall
j $31
.end Write
.globl Close
.ent Close
Close:
addiu $2,$0,SC_Close
syscall
j $31
.end Close
.globl Seek
.ent Seek
Seek:
addiu $2,$0,SC_Seek
syscall
j $31
.end Seek
.globl ThreadFork
.ent ThreadFork
ThreadFork:
addiu $2,$0,SC_ThreadFork
syscall
j $31
.end ThreadFork
.globl ThreadYield
.ent ThreadYield
ThreadYield:
addiu $2,$0,SC_ThreadYield
syscall
j $31
.end ThreadYield
.globl ThreadExit
.ent ThreadExit
ThreadExit:
addiu $2, $0, SC_ThreadExit
syscall
j $31
.end ThreadExit
.globl ThreadJoin
.ent ThreadJoin
ThreadJoin:
addiu $2, $0, SC_ThreadJoin
syscall
j $31
.end ThreadJoin
/* dummy function to keep gcc happy */
.globl __main
.ent __main
__main:
j $31
.end __main