├── README.md
├── lab2
├── lab2
│ ├── kernel
│ │ ├── kernel
│ │ │ ├── Makefile
│ │ │ ├── idt.o
│ │ │ ├── kvm.o
│ │ │ ├── vga.o
│ │ │ ├── disk.o
│ │ │ ├── doIrq.o
│ │ │ ├── i8259.o
│ │ │ ├── serial.o
│ │ │ ├── keyboard.o
│ │ │ ├── irqHandle.o
│ │ │ ├── disk.c
│ │ │ ├── serial.c
│ │ │ ├── vga.c
│ │ │ ├── i8259.c
│ │ │ ├── doIrq.S
│ │ │ ├── idt.c
│ │ │ └── kvm.c
│ │ ├── main.o
│ │ ├── .DS_Store
│ │ ├── kMain.elf
│ │ ├── lib
│ │ │ ├── abort.o
│ │ │ └── abort.c
│ │ ├── include
│ │ │ ├── x86
│ │ │ │ ├── irq.h
│ │ │ │ ├── cpu.h
│ │ │ │ ├── io.h
│ │ │ │ └── memory.h
│ │ │ ├── device
│ │ │ │ ├── disk.h
│ │ │ │ ├── vga.h
│ │ │ │ ├── keyboard.h
│ │ │ │ └── serial.h
│ │ │ ├── common.h
│ │ │ ├── common
│ │ │ │ ├── const.h
│ │ │ │ ├── assert.h
│ │ │ │ └── types.h
│ │ │ ├── device.h
│ │ │ └── x86.h
│ │ ├── main.c
│ │ └── Makefile
│ ├── os.img
│ ├── .DS_Store
│ ├── app
│ │ ├── main.o
│ │ ├── uMain.elf
│ │ ├── Makefile
│ │ └── main.c
│ ├── lib
│ │ ├── syscall.o
│ │ ├── lib.h
│ │ └── types.h
│ ├── bootloader
│ │ ├── boot.o
│ │ ├── start.o
│ │ ├── bootloader.bin
│ │ ├── bootloader.elf
│ │ ├── boot.h
│ │ ├── Makefile
│ │ ├── start.S
│ │ └── boot.c
│ ├── .vscode
│ │ └── settings.json
│ ├── utils
│ │ ├── genKernel.pl
│ │ └── genBoot.pl
│ └── Makefile
└── report
│ └── 191220138.pdf
├── lab1
├── os.img
├── .DS_Store
├── app
│ ├── app.o
│ ├── app.bin
│ ├── app.elf
│ ├── Makefile
│ └── app.s
├── bootloader
│ ├── boot.o
│ ├── start.o
│ ├── bootloader.bin
│ ├── bootloader.elf
│ ├── Makefile
│ ├── boot.h
│ ├── boot.c
│ └── start.s
├── Makefile
└── utils
│ └── genboot.pl
├── lab3
├── lab3
│ ├── os.img
│ ├── .DS_Store
│ ├── app
│ │ ├── main.o
│ │ ├── uMain.elf
│ │ ├── Makefile
│ │ └── main.c
│ ├── kernel
│ │ ├── main.o
│ │ ├── .DS_Store
│ │ ├── kMain.elf
│ │ ├── kernel
│ │ │ ├── idt.o
│ │ │ ├── kvm.o
│ │ │ ├── vga.o
│ │ │ ├── disk.o
│ │ │ ├── doIrq.o
│ │ │ ├── i8259.o
│ │ │ ├── serial.o
│ │ │ ├── timer.o
│ │ │ ├── irqHandle.o
│ │ │ ├── timer.c
│ │ │ ├── disk.c
│ │ │ ├── serial.c
│ │ │ ├── vga.c
│ │ │ ├── i8259.c
│ │ │ ├── doIrq.S
│ │ │ ├── idt.c
│ │ │ └── kvm.c
│ │ ├── lib
│ │ │ ├── abort.o
│ │ │ └── abort.c
│ │ ├── include
│ │ │ ├── device
│ │ │ │ ├── timer.h
│ │ │ │ ├── disk.h
│ │ │ │ ├── serial.h
│ │ │ │ └── vga.h
│ │ │ ├── x86
│ │ │ │ ├── irq.h
│ │ │ │ ├── cpu.h
│ │ │ │ ├── io.h
│ │ │ │ └── memory.h
│ │ │ ├── common.h
│ │ │ ├── common
│ │ │ │ ├── const.h
│ │ │ │ ├── assert.h
│ │ │ │ └── types.h
│ │ │ ├── device.h
│ │ │ └── x86.h
│ │ ├── main.c
│ │ └── Makefile
│ ├── lib
│ │ ├── syscall.o
│ │ ├── types.h
│ │ └── lib.h
│ ├── bootloader
│ │ ├── boot.o
│ │ ├── start.o
│ │ ├── bootloader.bin
│ │ ├── bootloader.elf
│ │ ├── boot.h
│ │ ├── Makefile
│ │ ├── boot.c
│ │ └── start.S
│ ├── utils
│ │ ├── genKernel.pl
│ │ └── genBoot.pl
│ └── Makefile
└── report
│ └── 191220138.pdf
├── lab4
├── lab4
│ ├── os.img
│ ├── .DS_Store
│ ├── app
│ │ ├── main.o
│ │ ├── uMain.elf
│ │ ├── Makefile
│ │ └── main.c
│ ├── kernel
│ │ ├── main.o
│ │ ├── .DS_Store
│ │ ├── kMain.elf
│ │ ├── kernel
│ │ │ ├── idt.o
│ │ │ ├── kvm.o
│ │ │ ├── vga.o
│ │ │ ├── disk.o
│ │ │ ├── doIrq.o
│ │ │ ├── i8259.o
│ │ │ ├── serial.o
│ │ │ ├── timer.o
│ │ │ ├── keyboard.o
│ │ │ ├── irqHandle.o
│ │ │ ├── timer.c
│ │ │ ├── disk.c
│ │ │ ├── serial.c
│ │ │ ├── vga.c
│ │ │ ├── i8259.c
│ │ │ ├── doIrq.S
│ │ │ ├── idt.c
│ │ │ └── kvm.c
│ │ ├── lib
│ │ │ ├── abort.o
│ │ │ └── abort.c
│ │ ├── include
│ │ │ ├── .DS_Store
│ │ │ ├── device
│ │ │ │ ├── timer.h
│ │ │ │ ├── disk.h
│ │ │ │ ├── serial.h
│ │ │ │ ├── keyboard.h
│ │ │ │ └── vga.h
│ │ │ ├── x86
│ │ │ │ ├── irq.h
│ │ │ │ ├── cpu.h
│ │ │ │ ├── io.h
│ │ │ │ └── memory.h
│ │ │ ├── common.h
│ │ │ ├── common
│ │ │ │ ├── const.h
│ │ │ │ ├── assert.h
│ │ │ │ └── types.h
│ │ │ ├── device.h
│ │ │ └── x86.h
│ │ ├── main.c
│ │ └── Makefile
│ ├── lib
│ │ ├── syscall.o
│ │ ├── types.h
│ │ └── lib.h
│ ├── bootloader
│ │ ├── boot.o
│ │ ├── start.o
│ │ ├── bootloader.bin
│ │ ├── bootloader.elf
│ │ ├── boot.c
│ │ ├── boot.h
│ │ ├── Makefile
│ │ └── start.S
│ ├── utils
│ │ ├── genKernel.pl
│ │ └── genBoot.pl
│ └── Makefile
└── report
│ └── lab4.pdf
└── lab5
├── code
├── os.img
├── .DS_Store
├── app
│ ├── fs.bin
│ ├── main.o
│ ├── uMain.elf
│ ├── Makefile
│ └── main.c
├── lib
│ ├── utils.o
│ ├── syscall.o
│ ├── types.h
│ ├── utils.h
│ ├── lib.h
│ └── utils.c
├── kernel
│ ├── main.o
│ ├── .DS_Store
│ ├── kMain.elf
│ ├── kernel
│ │ ├── fs.o
│ │ ├── idt.o
│ │ ├── kvm.o
│ │ ├── vga.o
│ │ ├── disk.o
│ │ ├── doIrq.o
│ │ ├── i8259.o
│ │ ├── serial.o
│ │ ├── timer.o
│ │ ├── keyboard.o
│ │ ├── irqHandle.o
│ │ ├── timer.c
│ │ ├── serial.c
│ │ ├── i8259.c
│ │ ├── vga.c
│ │ ├── disk.c
│ │ ├── doIrq.S
│ │ └── idt.c
│ ├── lib
│ │ ├── abort.o
│ │ ├── utils.o
│ │ ├── abort.c
│ │ └── utils.c
│ ├── include
│ │ ├── .DS_Store
│ │ ├── device
│ │ │ ├── timer.h
│ │ │ ├── serial.h
│ │ │ ├── keyboard.h
│ │ │ ├── vga.h
│ │ │ └── disk.h
│ │ ├── x86
│ │ │ ├── irq.h
│ │ │ ├── cpu.h
│ │ │ └── io.h
│ │ ├── common
│ │ │ ├── const.h
│ │ │ ├── assert.h
│ │ │ ├── types.h
│ │ │ └── utils.h
│ │ ├── common.h
│ │ ├── device.h
│ │ ├── x86.h
│ │ ├── fs.h
│ │ └── fs
│ │ │ └── ext.h
│ ├── main.c
│ └── Makefile
├── bootloader
│ ├── boot.o
│ ├── start.o
│ ├── bootloader.bin
│ ├── bootloader.elf
│ ├── boot.h
│ ├── Makefile
│ ├── boot.c
│ └── start.S
├── utils
│ ├── .DS_Store
│ ├── genFS
│ │ ├── genFS
│ │ ├── .DS_Store
│ │ ├── genFS.dSYM
│ │ │ └── Contents
│ │ │ │ ├── Resources
│ │ │ │ └── DWARF
│ │ │ │ │ └── genFS
│ │ │ │ └── Info.plist
│ │ ├── types.h
│ │ ├── Makefile
│ │ ├── utils.h
│ │ ├── func.h
│ │ ├── main.c
│ │ ├── utils.c
│ │ └── data.h
│ ├── genKernel.pl
│ └── genBoot.pl
└── Makefile
└── report
└── 191220138.pdf
/README.md:
--------------------------------------------------------------------------------
1 | # oslab
2 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/Makefile:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/lab1/os.img:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/os.img
--------------------------------------------------------------------------------
/lab1/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/.DS_Store
--------------------------------------------------------------------------------
/lab1/app/app.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/app/app.o
--------------------------------------------------------------------------------
/lab1/app/app.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/app/app.bin
--------------------------------------------------------------------------------
/lab1/app/app.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/app/app.elf
--------------------------------------------------------------------------------
/lab2/lab2/os.img:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/os.img
--------------------------------------------------------------------------------
/lab3/lab3/os.img:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/os.img
--------------------------------------------------------------------------------
/lab4/lab4/os.img:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/os.img
--------------------------------------------------------------------------------
/lab5/code/os.img:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/os.img
--------------------------------------------------------------------------------
/lab2/lab2/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/.DS_Store
--------------------------------------------------------------------------------
/lab3/lab3/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/.DS_Store
--------------------------------------------------------------------------------
/lab4/lab4/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/.DS_Store
--------------------------------------------------------------------------------
/lab5/code/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/.DS_Store
--------------------------------------------------------------------------------
/lab1/bootloader/boot.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/bootloader/boot.o
--------------------------------------------------------------------------------
/lab2/lab2/app/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/app/main.o
--------------------------------------------------------------------------------
/lab3/lab3/app/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/app/main.o
--------------------------------------------------------------------------------
/lab4/lab4/app/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/app/main.o
--------------------------------------------------------------------------------
/lab4/report/lab4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/report/lab4.pdf
--------------------------------------------------------------------------------
/lab5/code/app/fs.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/app/fs.bin
--------------------------------------------------------------------------------
/lab5/code/app/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/app/main.o
--------------------------------------------------------------------------------
/lab5/code/lib/utils.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/lib/utils.o
--------------------------------------------------------------------------------
/lab1/bootloader/start.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/bootloader/start.o
--------------------------------------------------------------------------------
/lab2/lab2/app/uMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/app/uMain.elf
--------------------------------------------------------------------------------
/lab2/lab2/kernel/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/main.o
--------------------------------------------------------------------------------
/lab2/lab2/lib/syscall.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/lib/syscall.o
--------------------------------------------------------------------------------
/lab3/lab3/app/uMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/app/uMain.elf
--------------------------------------------------------------------------------
/lab3/lab3/kernel/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/main.o
--------------------------------------------------------------------------------
/lab3/lab3/lib/syscall.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/lib/syscall.o
--------------------------------------------------------------------------------
/lab4/lab4/app/uMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/app/uMain.elf
--------------------------------------------------------------------------------
/lab4/lab4/kernel/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/main.o
--------------------------------------------------------------------------------
/lab4/lab4/lib/syscall.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/lib/syscall.o
--------------------------------------------------------------------------------
/lab5/code/app/uMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/app/uMain.elf
--------------------------------------------------------------------------------
/lab5/code/kernel/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/main.o
--------------------------------------------------------------------------------
/lab5/code/lib/syscall.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/lib/syscall.o
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/boot.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/bootloader/boot.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/.DS_Store
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kMain.elf
--------------------------------------------------------------------------------
/lab2/report/191220138.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/report/191220138.pdf
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/boot.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/bootloader/boot.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/.DS_Store
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kMain.elf
--------------------------------------------------------------------------------
/lab3/report/191220138.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/report/191220138.pdf
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/boot.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/bootloader/boot.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/.DS_Store
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kMain.elf
--------------------------------------------------------------------------------
/lab5/code/bootloader/boot.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/bootloader/boot.o
--------------------------------------------------------------------------------
/lab5/code/kernel/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/.DS_Store
--------------------------------------------------------------------------------
/lab5/code/kernel/kMain.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kMain.elf
--------------------------------------------------------------------------------
/lab5/code/utils/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/utils/.DS_Store
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/genFS:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/utils/genFS/genFS
--------------------------------------------------------------------------------
/lab5/report/191220138.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/report/191220138.pdf
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/start.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/bootloader/start.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/idt.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/idt.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/kvm.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/kvm.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/vga.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/vga.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/lib/abort.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/lib/abort.o
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/start.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/bootloader/start.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/idt.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/idt.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/kvm.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/kvm.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/vga.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/vga.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/lib/abort.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/lib/abort.o
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/start.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/bootloader/start.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/idt.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/idt.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/kvm.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/kvm.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/vga.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/vga.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/lib/abort.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/lib/abort.o
--------------------------------------------------------------------------------
/lab5/code/bootloader/start.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/bootloader/start.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/fs.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/fs.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/idt.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/idt.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/kvm.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/kvm.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/vga.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/vga.o
--------------------------------------------------------------------------------
/lab5/code/kernel/lib/abort.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/lib/abort.o
--------------------------------------------------------------------------------
/lab5/code/kernel/lib/utils.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/lib/utils.o
--------------------------------------------------------------------------------
/lab1/bootloader/bootloader.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/bootloader/bootloader.bin
--------------------------------------------------------------------------------
/lab1/bootloader/bootloader.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab1/bootloader/bootloader.elf
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/disk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/disk.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/doIrq.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/doIrq.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/i8259.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/i8259.o
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/serial.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/serial.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/disk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/disk.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/doIrq.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/doIrq.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/i8259.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/i8259.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/serial.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/serial.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/timer.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/timer.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/disk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/disk.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/doIrq.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/doIrq.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/i8259.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/i8259.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/serial.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/serial.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/timer.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/timer.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/disk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/disk.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/doIrq.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/doIrq.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/i8259.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/i8259.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/serial.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/serial.o
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/timer.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/timer.o
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/utils/genFS/.DS_Store
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/keyboard.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/keyboard.o
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/include/.DS_Store
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/keyboard.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/keyboard.o
--------------------------------------------------------------------------------
/lab5/code/kernel/include/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/include/.DS_Store
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/keyboard.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/keyboard.o
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/bootloader.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/bootloader/bootloader.bin
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/bootloader.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/bootloader/bootloader.elf
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/irqHandle.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab2/lab2/kernel/kernel/irqHandle.o
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/bootloader.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/bootloader/bootloader.bin
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/bootloader.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/bootloader/bootloader.elf
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/irqHandle.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab3/lab3/kernel/kernel/irqHandle.o
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/bootloader.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/bootloader/bootloader.bin
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/bootloader.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/bootloader/bootloader.elf
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/irqHandle.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab4/lab4/kernel/kernel/irqHandle.o
--------------------------------------------------------------------------------
/lab5/code/bootloader/bootloader.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/bootloader/bootloader.bin
--------------------------------------------------------------------------------
/lab5/code/bootloader/bootloader.elf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/bootloader/bootloader.elf
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/irqHandle.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/kernel/kernel/irqHandle.o
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/device/timer.h:
--------------------------------------------------------------------------------
1 | #ifndef __TIMER_H__
2 | #define __TIMER_H__
3 |
4 | void initTimer();
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device/timer.h:
--------------------------------------------------------------------------------
1 | #ifndef __TIMER_H__
2 | #define __TIMER_H__
3 |
4 | void initTimer();
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device/timer.h:
--------------------------------------------------------------------------------
1 | #ifndef __TIMER_H__
2 | #define __TIMER_H__
3 |
4 | void initTimer();
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/lab2/lab2/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "files.associations": {
3 | "malloc.h": "c",
4 | "lib.h": "c"
5 | }
6 | }
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/x86/irq.h:
--------------------------------------------------------------------------------
1 | #ifndef __IRQ_H__
2 | #define __IRQ_H__
3 |
4 | /* 中断处理相关函数 */
5 | void initIdt(void);
6 | void initIntr(void);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/x86/irq.h:
--------------------------------------------------------------------------------
1 | #ifndef __IRQ_H__
2 | #define __IRQ_H__
3 |
4 | /* 中断处理相关函数 */
5 | void initIdt(void);
6 | void initIntr(void);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/x86/irq.h:
--------------------------------------------------------------------------------
1 | #ifndef __IRQ_H__
2 | #define __IRQ_H__
3 |
4 | /* 中断处理相关函数 */
5 | void initIdt(void);
6 | void initIntr(void);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/x86/irq.h:
--------------------------------------------------------------------------------
1 | #ifndef __IRQ_H__
2 | #define __IRQ_H__
3 |
4 | /* 中断处理相关函数 */
5 | void initIdt(void);
6 | void initIntr(void);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/device/disk.h:
--------------------------------------------------------------------------------
1 | #ifndef __DISK_H__
2 | #define __DISK_H__
3 |
4 | void waitDisk(void);
5 | void readSect(void *dst, int offset);
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/device/disk.h:
--------------------------------------------------------------------------------
1 | #ifndef __DISK_H__
2 | #define __DISK_H__
3 |
4 | void waitDisk(void);
5 | void readSect(void *dst, int offset);
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device/disk.h:
--------------------------------------------------------------------------------
1 | #ifndef __DISK_H__
2 | #define __DISK_H__
3 |
4 | void waitDisk(void);
5 | void readSect(void *dst, int offset);
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/genFS.dSYM/Contents/Resources/DWARF/genFS:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fengling-aat/oslab/HEAD/lab5/code/utils/genFS/genFS.dSYM/Contents/Resources/DWARF/genFS
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device/serial.h:
--------------------------------------------------------------------------------
1 | #ifndef __SERIAL_H__
2 | #define __SERIAL_H__
3 |
4 | void initSerial(void);
5 | void putChar(char);
6 | void putInt(int);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/device/serial.h:
--------------------------------------------------------------------------------
1 | #ifndef __SERIAL_H__
2 | #define __SERIAL_H__
3 |
4 | void initSerial(void);
5 | void putChar(char);
6 | #define SERIAL_PORT 0x3F8
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/common.h:
--------------------------------------------------------------------------------
1 | #ifndef __COMMON_H__
2 | #define __COMMON_H__
3 |
4 | #include "common/types.h"
5 | #include "common/const.h"
6 | #include "common/assert.h"
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/common.h:
--------------------------------------------------------------------------------
1 | #ifndef __COMMON_H__
2 | #define __COMMON_H__
3 |
4 | #include "common/types.h"
5 | #include "common/const.h"
6 | #include "common/assert.h"
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/common.h:
--------------------------------------------------------------------------------
1 | #ifndef __COMMON_H__
2 | #define __COMMON_H__
3 |
4 | #include "common/types.h"
5 | #include "common/const.h"
6 | #include "common/assert.h"
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | typedef unsigned int uint32_t;
5 | typedef unsigned short uint16_t;
6 | typedef unsigned char uint8_t;
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/common/const.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONST_H__
2 | #define __CONST_H__
3 |
4 | #define TRUE 1
5 | #define FALSE 0
6 |
7 | #define NULL ((void*)0)
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/common/const.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONST_H__
2 | #define __CONST_H__
3 |
4 | #define TRUE 1
5 | #define FALSE 0
6 |
7 | #define NULL ((void*)0)
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/common/const.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONST_H__
2 | #define __CONST_H__
3 |
4 | #define TRUE 1
5 | #define FALSE 0
6 |
7 | #define NULL ((void*)0)
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/common/const.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONST_H__
2 | #define __CONST_H__
3 |
4 | #define TRUE 1
5 | #define FALSE 0
6 |
7 | #define NULL ((void*)0)
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab1/app/Makefile:
--------------------------------------------------------------------------------
1 | app.bin: app.s
2 | gcc -c -m32 app.s -o app.o
3 | ld -m elf_i386 -e start -Ttext 0x8c00 app.o -o app.elf
4 | objcopy -S -j .text -O binary app.elf app.bin
5 |
6 | clean:
7 | rm -rf *.o *.elf *.bin
8 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/device/vga.h:
--------------------------------------------------------------------------------
1 | #ifndef __VGA_H__
2 | #define __VGA_H__
3 |
4 | void initVga();
5 | void clearScreen();
6 | void updateCursor(int row, int col);
7 | void scrollScreen();
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/device/vga.h:
--------------------------------------------------------------------------------
1 | #ifndef __VGA_H__
2 | #define __VGA_H__
3 |
4 | void initVga();
5 | void clearScreen();
6 | void updateCursor(int row, int col);
7 | void scrollScreen();
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device/serial.h:
--------------------------------------------------------------------------------
1 | #ifndef __SERIAL_H__
2 | #define __SERIAL_H__
3 |
4 | void initSerial(void);
5 | void putChar(char);
6 | void putString(const char *);
7 | void putInt(int);
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/device.h:
--------------------------------------------------------------------------------
1 | #ifndef __DEVICE_H__
2 | #define __DEVICE_H__
3 |
4 | #include "device/serial.h"
5 | #include "device/disk.h"
6 | #include "device/vga.h"
7 | #include "device/timer.h"
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/device.h:
--------------------------------------------------------------------------------
1 | #ifndef __DEVICE_H__
2 | #define __DEVICE_H__
3 |
4 | #include "device/serial.h"
5 | #include "device/disk.h"
6 | #include "device/vga.h"
7 | #include "device/keyboard.h"
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/common.h:
--------------------------------------------------------------------------------
1 | #ifndef __COMMON_H__
2 | #define __COMMON_H__
3 |
4 | #include "common/types.h"
5 | #include "common/const.h"
6 | #include "common/assert.h"
7 | #include "common/utils.h"
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/x86.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_H__
2 | #define __X86_H__
3 |
4 | #include "x86/cpu.h"
5 | #include "x86/memory.h"
6 | #include "x86/io.h"
7 | #include "x86/irq.h"
8 |
9 | void initSeg(void);
10 | void loadUMain(void);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/x86.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_H__
2 | #define __X86_H__
3 |
4 | #include "x86/cpu.h"
5 | #include "x86/memory.h"
6 | #include "x86/io.h"
7 | #include "x86/irq.h"
8 |
9 | void initSeg(void);
10 | void initProc(void);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device.h:
--------------------------------------------------------------------------------
1 | #ifndef __DEVICE_H__
2 | #define __DEVICE_H__
3 |
4 | #include "device/serial.h"
5 | #include "device/disk.h"
6 | #include "device/vga.h"
7 | #include "device/timer.h"
8 | #include "device/keyboard.h"
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device/keyboard.h:
--------------------------------------------------------------------------------
1 | #ifndef __KEYBOARD_H__
2 | #define __KEYBOARD_H__
3 |
4 | #define MAX_KEYBUFFER_SIZE 256
5 |
6 | void initKeyTable();
7 |
8 | uint32_t getKeyCode();
9 |
10 | char getChar(uint32_t code);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device.h:
--------------------------------------------------------------------------------
1 | #ifndef __DEVICE_H__
2 | #define __DEVICE_H__
3 |
4 | #include "device/serial.h"
5 | #include "device/disk.h"
6 | #include "device/vga.h"
7 | #include "device/timer.h"
8 | #include "device/keyboard.h"
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device/keyboard.h:
--------------------------------------------------------------------------------
1 | #ifndef __KEYBOARD_H__
2 | #define __KEYBOARD_H__
3 |
4 | #define MAX_KEYBUFFER_SIZE 256
5 |
6 | void initKeyTable();
7 |
8 | uint32_t getKeyCode();
9 |
10 | char getChar(uint32_t code);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/common/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef __ASSERT_H__
2 | #define __ASSERT_H__
3 |
4 | int abort(const char *, int);
5 |
6 | /* assert: 断言条件为真,若为假则蓝屏退出 */
7 | #define assert(cond) \
8 | ((cond) ? (0) : (abort(__FILE__, __LINE__)))
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/common/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef __ASSERT_H__
2 | #define __ASSERT_H__
3 |
4 | int abort(const char *, int);
5 |
6 | /* assert: 断言条件为真,若为假则蓝屏退出 */
7 | #define assert(cond) \
8 | ((cond) ? (0) : (abort(__FILE__, __LINE__)))
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/common/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef __ASSERT_H__
2 | #define __ASSERT_H__
3 |
4 | int abort(const char *, int);
5 |
6 | /* assert: 断言条件为真,若为假则蓝屏退出 */
7 | #define assert(cond) \
8 | ((cond) ? (0) : (abort(__FILE__, __LINE__)))
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/common/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef __ASSERT_H__
2 | #define __ASSERT_H__
3 |
4 | int abort(const char *, int);
5 |
6 | /* assert: 断言条件为真,若为假则蓝屏退出 */
7 | #define assert(cond) \
8 | ((cond) ? (0) : (abort(__FILE__, __LINE__)))
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/device/keyboard.h:
--------------------------------------------------------------------------------
1 | #ifndef __KEYBOARD_H__
2 | #define __KEYBOARD_H__
3 |
4 | #define MAX_KEYBUFFER_SIZE 256
5 |
6 | void initKeyTable();
7 |
8 | uint32_t getKeyCode();
9 |
10 | char getChar(uint32_t code);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/device/vga.h:
--------------------------------------------------------------------------------
1 | #ifndef __VGA_H__
2 | #define __VGA_H__
3 |
4 | #define MAX_ROW 25
5 | #define MAX_COL 80
6 |
7 | void initVga();
8 | void clearScreen();
9 | void updateCursor(int row, int col);
10 | void scrollScreen();
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device/vga.h:
--------------------------------------------------------------------------------
1 | #ifndef __VGA_H__
2 | #define __VGA_H__
3 |
4 | #define MAX_ROW 25
5 | #define MAX_COL 80
6 |
7 | void initVga();
8 | void clearScreen();
9 | void updateCursor(int row, int col);
10 | void scrollScreen();
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/device/serial.h:
--------------------------------------------------------------------------------
1 | #ifndef __SERIAL_H__
2 | #define __SERIAL_H__
3 |
4 | void initSerial(void);
5 | void putChar(char);
6 | void putString(const char *);
7 | void putInt(int);
8 | void putint(int);
9 |
10 | #define SERIAL_PORT 0x3F8
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 |
3 | CFLAGS = -g -m32 -O0
4 |
5 | CFILES = $(shell find ./ -name "*.c")
6 | OBJS = $(CFILES:.c=.o)
7 |
8 | genFS: $(CFILES)
9 | $(CC) $(CFLAGS) -o genFS $(CFILES)
10 |
11 | clean:
12 | @#rm -rf $(OBJS) genFS fs.bin
13 | rm -rf $(OBJS) genFS
14 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/x86.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_H__
2 | #define __X86_H__
3 |
4 | #include "x86/cpu.h"
5 | #include "x86/memory.h"
6 | #include "x86/io.h"
7 | #include "x86/irq.h"
8 |
9 | void initSeg(void);
10 | void initSem(void);
11 | void initDev(void);
12 | void initProc(void);
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/x86.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_H__
2 | #define __X86_H__
3 |
4 | #include "x86/cpu.h"
5 | #include "x86/memory.h"
6 | #include "x86/io.h"
7 | #include "x86/irq.h"
8 |
9 | void initSeg(void);
10 | void initSem(void);
11 | void initDev(void);
12 | void initProc(void);
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/lab1/Makefile:
--------------------------------------------------------------------------------
1 | os.img:
2 | cd bootloader; make bootloader.bin
3 | cd app; make app.bin
4 | cat bootloader/bootloader.bin app/app.bin > os.img
5 |
6 | clean:
7 | cd bootloader; make clean
8 | cd app; make clean
9 | rm -f os.img
10 |
11 | play:
12 | qemu-system-i386 os.img
13 |
14 | debug:
15 | qemu-system-i386 -s -S os.img
16 |
--------------------------------------------------------------------------------
/lab2/lab2/lib/lib.h:
--------------------------------------------------------------------------------
1 | #ifndef __lib_h__
2 | #define __lib_h__
3 |
4 | #define SYS_WRITE 0
5 | #define STD_OUT 0
6 |
7 | #define SYS_READ 1
8 | #define STD_IN 0
9 | #define STD_STR 1
10 |
11 | #define MAX_BUFFER_SIZE 256
12 |
13 | void printf(const char *format,...);
14 | char getChar();
15 | void getStr(char *str, int size);
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/lab2/lab2/utils/genKernel.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 100000);
6 |
7 | print STDERR "OK: Kernel is $n bytes - Extended to 200 sectors\n";
8 |
9 | $buf .= "\0" x (102400-$n);
10 |
11 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
12 | print SIG $buf;
13 | close SIG;
14 |
--------------------------------------------------------------------------------
/lab3/lab3/utils/genKernel.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 100000);
6 |
7 | print STDERR "OK: Kernel is $n bytes - Extended to 200 sectors\n";
8 |
9 | $buf .= "\0" x (102400-$n);
10 |
11 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
12 | print SIG $buf;
13 | close SIG;
14 |
--------------------------------------------------------------------------------
/lab4/lab4/utils/genKernel.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 100000);
6 |
7 | print STDERR "OK: Kernel is $n bytes - Extended to 200 sectors\n";
8 |
9 | $buf .= "\0" x (102400-$n);
10 |
11 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
12 | print SIG $buf;
13 | close SIG;
14 |
--------------------------------------------------------------------------------
/lab5/code/utils/genKernel.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 100000);
6 |
7 | print STDERR "OK: Kernel is $n bytes - Extended to 200 sectors\n";
8 |
9 | $buf .= "\0" x (102400-$n);
10 |
11 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
12 | print SIG $buf;
13 | close SIG;
14 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/common/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | /* 定义数据类型 */
5 | typedef unsigned int uint32_t;
6 | typedef int int32_t;
7 | typedef unsigned short uint16_t;
8 | typedef short int16_t;
9 | typedef unsigned char uint8_t;
10 | typedef char int8_t;
11 | typedef unsigned int size_t;
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/common/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | /* 定义数据类型 */
5 | typedef unsigned int uint32_t;
6 | typedef int int32_t;
7 | typedef unsigned short uint16_t;
8 | typedef short int16_t;
9 | typedef unsigned char uint8_t;
10 | typedef char int8_t;
11 | typedef unsigned int size_t;
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/common/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | /* 定义数据类型 */
5 | typedef unsigned int uint32_t;
6 | typedef int int32_t;
7 | typedef unsigned short uint16_t;
8 | typedef short int16_t;
9 | typedef unsigned char uint8_t;
10 | typedef char int8_t;
11 | typedef unsigned int size_t;
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/common/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | /* 定义数据类型 */
5 | typedef unsigned int uint32_t;
6 | typedef int int32_t;
7 | typedef unsigned short uint16_t;
8 | typedef short int16_t;
9 | typedef unsigned char uint8_t;
10 | typedef char int8_t;
11 | typedef unsigned int size_t;
12 | #endif
13 |
--------------------------------------------------------------------------------
/lab1/bootloader/Makefile:
--------------------------------------------------------------------------------
1 | bootloader.bin: start.s boot.c boot.h
2 | gcc -c -m32 start.s -o start.o
3 | gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
4 | ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
5 | objcopy -S -j .text -O binary bootloader.elf bootloader.bin
6 | ../utils/genboot.pl bootloader.bin
7 |
8 | clean:
9 | rm -rf *.o *.elf *.bin
10 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/timer.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define TIMER_PORT 0x40
4 | #define FREQ_8253 1193182
5 | #define HZ 100
6 | //#define HZ 1000
7 |
8 | void initTimer() {
9 | int counter = FREQ_8253 / HZ;
10 | //assert(TIMER_PORT < 65536);
11 | outByte(TIMER_PORT + 3, 0x34);
12 | outByte(TIMER_PORT + 0, counter % 256);
13 | outByte(TIMER_PORT + 0, counter / 256);
14 | }
15 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/timer.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define TIMER_PORT 0x40
5 | #define FREQ_8253 1193182
6 | #define HZ 100
7 | //#define HZ 1000
8 |
9 | void initTimer() {
10 | int counter = FREQ_8253 / HZ;
11 | //assert(TIMER_PORT < 65536);
12 | outByte(TIMER_PORT + 3, 0x34);
13 | outByte(TIMER_PORT + 0, counter % 256);
14 | outByte(TIMER_PORT + 0, counter / 256);
15 | }
16 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/timer.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define TIMER_PORT 0x40
5 | #define FREQ_8253 1193182
6 | #define HZ 100
7 | //#define HZ 1000
8 |
9 | void initTimer() {
10 | int counter = FREQ_8253 / HZ;
11 | //assert(TIMER_PORT < 65536);
12 | outByte(TIMER_PORT + 3, 0x34);
13 | outByte(TIMER_PORT + 0, counter % 256);
14 | outByte(TIMER_PORT + 0, counter / 256);
15 | }
16 |
--------------------------------------------------------------------------------
/lab2/lab2/lib/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | typedef unsigned int uint32_t;
5 | typedef int int32_t;
6 | typedef unsigned short uint16_t;
7 | typedef short int16_t;
8 | typedef unsigned char uint8_t;
9 | typedef char int8_t;
10 | typedef unsigned char boolean;
11 |
12 | typedef uint32_t size_t;
13 | typedef int32_t pid_t;
14 |
15 |
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/lab3/lab3/lib/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | typedef unsigned int uint32_t;
5 | typedef int int32_t;
6 | typedef unsigned short uint16_t;
7 | typedef short int16_t;
8 | typedef unsigned char uint8_t;
9 | typedef char int8_t;
10 | typedef unsigned char boolean;
11 |
12 | typedef uint32_t size_t;
13 | typedef int32_t pid_t;
14 |
15 |
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/lab3/lab3/lib/lib.h:
--------------------------------------------------------------------------------
1 | #ifndef __lib_h__
2 | #define __lib_h__
3 |
4 | #include "types.h"
5 |
6 | #define SYS_WRITE 0
7 | #define SYS_FORK 1
8 | #define SYS_EXEC 2
9 | #define SYS_SLEEP 3
10 | #define SYS_EXIT 4
11 |
12 | #define STD_OUT 0
13 |
14 | #define MAX_BUFFER_SIZE 256
15 |
16 | int printf(const char *format,...);
17 |
18 | pid_t fork();
19 |
20 | int sleep(uint32_t time);
21 |
22 | int exit();
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/device/disk.h:
--------------------------------------------------------------------------------
1 | #ifndef __DISK_H__
2 | #define __DISK_H__
3 |
4 | #define SECTOR_NUM 8196
5 |
6 | #define SECTOR_SIZE 512
7 |
8 | void waitDisk(void);
9 | void readSect(void *dst, int offset);
10 | void writeSect(void *src, int offset);
11 |
12 | void diskRead (void *destBuffer, int size, int num, int offset);
13 | void diskWrite (void *destBuffer, int size, int num, int offset);
14 |
15 | #endif
16 |
--------------------------------------------------------------------------------
/lab4/lab4/lib/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | typedef unsigned int uint32_t;
5 | typedef int int32_t;
6 | typedef unsigned short uint16_t;
7 | typedef short int16_t;
8 | typedef unsigned char uint8_t;
9 | typedef char int8_t;
10 | typedef unsigned char boolean;
11 |
12 | typedef uint32_t size_t;
13 | typedef int32_t pid_t;
14 | typedef int32_t sem_t;
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/main.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | void kEntry(void) {
6 |
7 | // Interruption is disabled in bootloader
8 |
9 | initSerial();// initialize serial port
10 | initIdt(); // initialize idt
11 | initIntr(); // iniialize 8259a
12 | initSeg(); // initialize gdt, tss
13 | initVga(); // initialize vga device
14 | initTimer(); // initialize timer device
15 | initProc();
16 | }
17 |
--------------------------------------------------------------------------------
/lab5/code/lib/types.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_H__
2 | #define __TYPES_H__
3 |
4 | typedef unsigned int uint32_t;
5 | typedef int int32_t;
6 | typedef unsigned short uint16_t;
7 | typedef short int16_t;
8 | typedef unsigned char uint8_t;
9 | typedef char int8_t;
10 | typedef unsigned char boolean;
11 |
12 | typedef uint32_t size_t;
13 | typedef int32_t pid_t;
14 | typedef int32_t sem_t;
15 |
16 | #define NULL ((void*)0)
17 |
18 | #endif
19 |
--------------------------------------------------------------------------------
/lab1/app/app.s:
--------------------------------------------------------------------------------
1 | .code32
2 |
3 | .global start
4 | start:
5 | pushl $13
6 | pushl $message
7 | calll displayStr
8 | loop:
9 | jmp loop
10 |
11 | message:
12 | .string "Hello, World!\n\0"
13 |
14 | displayStr:
15 | movl 4(%esp), %ebx
16 | movl 8(%esp), %ecx
17 | movl $((80*5+0)*2), %edi
18 | movb $0x0c, %ah
19 | nextChar:
20 | movb (%ebx), %al
21 | movw %ax, %gs:(%edi)
22 | addl $2, %edi
23 | incl %ebx
24 | loopnz nextChar # loopnz decrease ecx by 1
25 | ret
26 |
--------------------------------------------------------------------------------
/lab1/utils/genboot.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 1000);
6 |
7 | if($n > 510){
8 | print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
9 | exit 1;
10 | }
11 |
12 | print STDERR "OK: boot block is $n bytes (max 510)\n";
13 |
14 | $buf .= "\0" x (510-$n);
15 | $buf .= "\x55\xAA";
16 |
17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
18 | print SIG $buf;
19 | close SIG;
20 |
--------------------------------------------------------------------------------
/lab2/lab2/utils/genBoot.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 1000);
6 |
7 | if($n > 510){
8 | print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
9 | exit 1;
10 | }
11 |
12 | print STDERR "OK: boot block is $n bytes (max 510)\n";
13 |
14 | $buf .= "\0" x (510-$n);
15 | $buf .= "\x55\xAA";
16 |
17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
18 | print SIG $buf;
19 | close SIG;
20 |
--------------------------------------------------------------------------------
/lab3/lab3/utils/genBoot.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 1000);
6 |
7 | if($n > 510){
8 | print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
9 | exit 1;
10 | }
11 |
12 | print STDERR "OK: boot block is $n bytes (max 510)\n";
13 |
14 | $buf .= "\0" x (510-$n);
15 | $buf .= "\x55\xAA";
16 |
17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
18 | print SIG $buf;
19 | close SIG;
20 |
--------------------------------------------------------------------------------
/lab4/lab4/utils/genBoot.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 1000);
6 |
7 | if($n > 510){
8 | print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
9 | exit 1;
10 | }
11 |
12 | print STDERR "OK: boot block is $n bytes (max 510)\n";
13 |
14 | $buf .= "\0" x (510-$n);
15 | $buf .= "\x55\xAA";
16 |
17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
18 | print SIG $buf;
19 | close SIG;
20 |
--------------------------------------------------------------------------------
/lab5/code/utils/genBoot.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
4 |
5 | $n = sysread(SIG, $buf, 1000);
6 |
7 | if($n > 510){
8 | print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
9 | exit 1;
10 | }
11 |
12 | print STDERR "OK: boot block is $n bytes (max 510)\n";
13 |
14 | $buf .= "\0" x (510-$n);
15 | $buf .= "\x55\xAA";
16 |
17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
18 | print SIG $buf;
19 | close SIG;
20 |
--------------------------------------------------------------------------------
/lab5/code/lib/utils.h:
--------------------------------------------------------------------------------
1 | #ifndef __UTILS_H__
2 | #define __UTILS_H__
3 |
4 | int stringChr(const char *string, char token, int *size);
5 |
6 | int stringChrR (const char *string, char token, int *size);
7 |
8 | int stringLen(const char *string);
9 |
10 | int stringCmp(const char *srcString, const char *destString, int size);
11 |
12 | int stringCpy (const char *srcString, char *destString, int size);
13 |
14 | int setBuffer (uint8_t *buffer, int size, uint8_t value);
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/common/utils.h:
--------------------------------------------------------------------------------
1 | #ifndef __UTILS_H__
2 | #define __UTILS_H__
3 |
4 | int stringChr(const char *string, char token, int *size);
5 |
6 | int stringChrR (const char *string, char token, int *size);
7 |
8 | int stringLen(const char *string);
9 |
10 | int stringCmp(const char *srcString, const char *destString, int size);
11 |
12 | int stringCpy (const char *srcString, char *destString, int size);
13 |
14 | int setBuffer (uint8_t *buffer, int size, uint8_t value);
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/utils.h:
--------------------------------------------------------------------------------
1 | #ifndef __UTILS_H__
2 | #define __UTILS_H__
3 |
4 | #include "types.h"
5 |
6 | int stringChr(const char *string, char token, int *size);
7 |
8 | int stringChrR (const char *string, char token, int *size);
9 |
10 | int stringLen(const char *string);
11 |
12 | int stringCmp(const char *srcString, const char *destString, int size);
13 |
14 | int stringCpy (const char *srcString, char *destString, int size);
15 |
16 | int setBuffer (uint8_t *buffer, int size, uint8_t value);
17 |
18 | #endif
19 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/main.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | void kEntry(void) {
6 |
7 | // Interruption is disabled in bootloader
8 |
9 | initSerial();// initialize serial port
10 | // TODO: 做一系列初始化
11 | initIdt(); // initialize idt
12 | initIntr(); // iniialize 8259a
13 | initSeg(); // initialize gdt, tss
14 | initVga(); // initialize vga device
15 | initKeyTable(); // initialize keyboard device
16 | loadUMain(); // load user program, enter user space
17 |
18 | while(1);
19 | assert(0);
20 | }
21 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/disk.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define SECTSIZE 512
5 |
6 | void waitDisk(void) {
7 | while((inByte(0x1F7) & 0xC0) != 0x40);
8 | }
9 |
10 | void readSect(void *dst, int offset) {
11 | int i;
12 | waitDisk();
13 | outByte(0x1F2, 1);
14 | outByte(0x1F3, offset);
15 | outByte(0x1F4, offset >> 8);
16 | outByte(0x1F5, offset >> 16);
17 | outByte(0x1F6, (offset >> 24) | 0xE0);
18 | outByte(0x1F7, 0x20);
19 |
20 | waitDisk();
21 | for (i = 0; i < SECTSIZE / 4; i ++) {
22 | ((int *)dst)[i] = inLong(0x1F0);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/func.h:
--------------------------------------------------------------------------------
1 | #ifndef __FUNC_H__
2 | #define __FUNC_H__
3 |
4 | int format (const char *driver, int sectorNum, int sectorsPerBlock);
5 |
6 | int mkdir (const char *driver, const char *destDirPath);
7 |
8 | int rmdir (const char *driver, const char *destDirPath);
9 |
10 | int cp (const char *driver, const char *srcFilePath, const char *destFilePath);
11 |
12 | int rm (const char *driver, const char *destFilePath);
13 |
14 | int ls (const char *driver, const char *destFilePath);
15 |
16 | int touch (const char *driver, const char *destFilePath);
17 |
18 | #endif
19 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/disk.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define SECTSIZE 512
5 |
6 | void waitDisk(void) {
7 | while((inByte(0x1F7) & 0xC0) != 0x40);
8 | }
9 |
10 | void readSect(void *dst, int offset) {
11 | int i;
12 | waitDisk();
13 |
14 | outByte(0x1F2, 1);
15 | outByte(0x1F3, offset);
16 | outByte(0x1F4, offset >> 8);
17 | outByte(0x1F5, offset >> 16);
18 | outByte(0x1F6, (offset >> 24) | 0xE0);
19 | outByte(0x1F7, 0x20);
20 |
21 | waitDisk();
22 | for (i = 0; i < SECTSIZE / 4; i ++) {
23 | ((int *)dst)[i] = inLong(0x1F0);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/disk.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define SECTSIZE 512
5 |
6 | void waitDisk(void) {
7 | while((inByte(0x1F7) & 0xC0) != 0x40);
8 | }
9 |
10 | void readSect(void *dst, int offset) {
11 | int i;
12 | waitDisk();
13 |
14 | outByte(0x1F2, 1);
15 | outByte(0x1F3, offset);
16 | outByte(0x1F4, offset >> 8);
17 | outByte(0x1F5, offset >> 16);
18 | outByte(0x1F6, (offset >> 24) | 0xE0);
19 | outByte(0x1F7, 0x20);
20 |
21 | waitDisk();
22 | for (i = 0; i < SECTSIZE / 4; i ++) {
23 | ((int *)dst)[i] = inLong(0x1F0);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/serial.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | void initSerial(void) {
5 | outByte(SERIAL_PORT + 1, 0x00);
6 | outByte(SERIAL_PORT + 3, 0x80);
7 | outByte(SERIAL_PORT + 0, 0x01);
8 | outByte(SERIAL_PORT + 1, 0x00);
9 | outByte(SERIAL_PORT + 3, 0x03);
10 | outByte(SERIAL_PORT + 2, 0xC7);
11 | outByte(SERIAL_PORT + 4, 0x0B);
12 | }
13 |
14 | static inline int serialIdle(void) {
15 | return (inByte(SERIAL_PORT + 5) & 0x20) != 0;
16 | }
17 |
18 | void putChar(char ch) {
19 | while (serialIdle() != TRUE);
20 | outByte(SERIAL_PORT, ch);
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/lab1/bootloader/boot.h:
--------------------------------------------------------------------------------
1 | #ifndef BOOT_H
2 | #define BOOT_H
3 |
4 | void waitDisk(void);
5 |
6 | void readSect(void *dst, int offset);
7 |
8 | /* I/O functions */
9 | static inline char inByte(short port) {
10 | char data;
11 | asm volatile("in %1,%0" : "=a" (data) : "d" (port));
12 | return data;
13 | }
14 |
15 | static inline int inLong(short port) {
16 | int data;
17 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
18 | return data;
19 | }
20 |
21 | static inline void outByte(short port, char data) {
22 | asm volatile("out %0,%1" : : "a" (data), "d" (port));
23 | }
24 |
25 | #endif
26 |
--------------------------------------------------------------------------------
/lab2/lab2/Makefile:
--------------------------------------------------------------------------------
1 | QEMU = qemu-system-i386
2 |
3 | os.img:
4 | @cd bootloader; make
5 | @cd kernel; make
6 | @cd app; make
7 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.bin > os.img
8 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.elf > os.img
9 | cat bootloader/bootloader.bin kernel/kMain.elf app/uMain.elf > os.img
10 |
11 | play: os.img
12 | $(QEMU) -serial stdio os.img
13 |
14 | debug: os.img
15 | $(QEMU) -serial stdio -s -S os.img
16 |
17 | clean:
18 | @cd bootloader; make clean
19 | @cd kernel; make clean
20 | @cd app; make clean
21 | rm -f os.img
22 |
--------------------------------------------------------------------------------
/lab3/lab3/Makefile:
--------------------------------------------------------------------------------
1 | QEMU = qemu-system-i386
2 |
3 | os.img:
4 | @cd bootloader; make
5 | @cd kernel; make
6 | @cd app; make
7 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.bin > os.img
8 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.elf > os.img
9 | cat bootloader/bootloader.bin kernel/kMain.elf app/uMain.elf > os.img
10 |
11 | play: os.img
12 | $(QEMU) -serial stdio os.img
13 |
14 | debug: os.img
15 | $(QEMU) -serial stdio -s -S os.img
16 |
17 | clean:
18 | @cd bootloader; make clean
19 | @cd kernel; make clean
20 | @cd app; make clean
21 | rm -f os.img
22 |
--------------------------------------------------------------------------------
/lab4/lab4/Makefile:
--------------------------------------------------------------------------------
1 | QEMU = qemu-system-i386
2 |
3 | os.img:
4 | @cd bootloader; make
5 | @cd kernel; make
6 | @cd app; make
7 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.bin > os.img
8 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.elf > os.img
9 | cat bootloader/bootloader.bin kernel/kMain.elf app/uMain.elf > os.img
10 |
11 | play: os.img
12 | $(QEMU) -serial stdio os.img
13 |
14 | debug: os.img
15 | $(QEMU) -serial stdio -s -S os.img
16 |
17 | clean:
18 | @cd bootloader; make clean
19 | @cd kernel; make clean
20 | @cd app; make clean
21 | rm -f os.img
22 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/main.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | void kEntry(void) {
6 |
7 | // Interruption is disabled in bootloader
8 |
9 | initSerial();// initialize serial port
10 | initIdt(); // initialize idt
11 | initIntr(); // iniialize 8259a
12 | initSeg(); // initialize gdt, tss
13 | initVga(); // initialize vga device
14 | initTimer(); // initialize timer device
15 | initKeyTable(); // initialize keyboard device
16 | initSem(); // initialize semaphore list
17 | initDev(); // initialize device list
18 | initProc(); // initialize pcb & load user program
19 | }
20 |
--------------------------------------------------------------------------------
/lab5/code/Makefile:
--------------------------------------------------------------------------------
1 | QEMU = qemu-system-i386
2 |
3 | os.img:
4 | @cd utils/genFS; make
5 | @cd bootloader; make
6 | @cd kernel; make
7 | @cd app; make
8 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.bin > os.img
9 | @#cat bootloader/bootloader.bin kernel/kMain.bin app/uMain.elf > os.img
10 | @#cat bootloader/bootloader.bin kernel/kMain.elf app/uMain.elf > os.img
11 | cat bootloader/bootloader.bin kernel/kMain.elf app/fs.bin > os.img
12 |
13 | play: os.img
14 | $(QEMU) -serial stdio os.img
15 |
16 | debug: os.img
17 | $(QEMU) -serial stdio -s -S os.img
18 |
19 | clean:
20 | @cd utils/genFS; make clean
21 | @cd bootloader; make clean
22 | @cd kernel; make clean
23 | @cd app; make clean
24 | rm -f os.img
25 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/x86/cpu.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_CPU_H__
2 | #define __X86_CPU_H__
3 |
4 | #include "common.h"
5 |
6 | /* 将CPU置入休眠状态直到下次中断到来 */
7 | static inline void waitForInterrupt() {
8 | asm volatile("hlt");
9 | }
10 |
11 | /* 修改IDTR */
12 | static inline void saveIdt(void *addr, uint32_t size) {
13 | static volatile uint16_t data[3];
14 | data[0] = size - 1;
15 | data[1] = (uint32_t)addr;
16 | data[2] = ((uint32_t)addr) >> 16;
17 | asm volatile("lidt (%0)" : : "r"(data));
18 | }
19 |
20 | /* 打开外部中断 */
21 | static inline void enableInterrupt(void) {
22 | asm volatile("sti");
23 | }
24 |
25 | /* 关闭外部中断 */
26 | static inline void disableInterrupt(void) {
27 | asm volatile("cli");
28 | }
29 |
30 | #define NR_IRQ 256
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/x86/cpu.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_CPU_H__
2 | #define __X86_CPU_H__
3 |
4 | #include "common.h"
5 |
6 | /* 将CPU置入休眠状态直到下次中断到来 */
7 | static inline void waitForInterrupt() {
8 | asm volatile("hlt");
9 | }
10 |
11 | /* 修改IDTR */
12 | static inline void saveIdt(void *addr, uint32_t size) {
13 | static volatile uint16_t data[3];
14 | data[0] = size - 1;
15 | data[1] = (uint32_t)addr;
16 | data[2] = ((uint32_t)addr) >> 16;
17 | asm volatile("lidt (%0)" : : "r"(data));
18 | }
19 |
20 | /* 打开外部中断 */
21 | static inline void enableInterrupt(void) {
22 | asm volatile("sti");
23 | }
24 |
25 | /* 关闭外部中断 */
26 | static inline void disableInterrupt(void) {
27 | asm volatile("cli");
28 | }
29 |
30 | #define NR_IRQ 256
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/x86/cpu.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_CPU_H__
2 | #define __X86_CPU_H__
3 |
4 | #include "common.h"
5 |
6 | /* 将CPU置入休眠状态直到下次中断到来 */
7 | static inline void waitForInterrupt() {
8 | asm volatile("hlt");
9 | }
10 |
11 | /* 修改IDTR */
12 | static inline void saveIdt(void *addr, uint32_t size) {
13 | static volatile uint16_t data[3];
14 | data[0] = size - 1;
15 | data[1] = (uint32_t)addr;
16 | data[2] = ((uint32_t)addr) >> 16;
17 | asm volatile("lidt (%0)" : : "r"(data));
18 | }
19 |
20 | /* 打开外部中断 */
21 | static inline void enableInterrupt(void) {
22 | asm volatile("sti");
23 | }
24 |
25 | /* 关闭外部中断 */
26 | static inline void disableInterrupt(void) {
27 | asm volatile("cli");
28 | }
29 |
30 | #define NR_IRQ 256
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/x86/cpu.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_CPU_H__
2 | #define __X86_CPU_H__
3 |
4 | #include "common.h"
5 |
6 | /* 将CPU置入休眠状态直到下次中断到来 */
7 | static inline void waitForInterrupt() {
8 | asm volatile("hlt");
9 | }
10 |
11 | /* 修改IDTR */
12 | static inline void saveIdt(void *addr, uint32_t size) {
13 | static volatile uint16_t data[3];
14 | data[0] = size - 1;
15 | data[1] = (uint32_t)addr;
16 | data[2] = ((uint32_t)addr) >> 16;
17 | asm volatile("lidt (%0)" : : "r"(data));
18 | }
19 |
20 | /* 打开外部中断 */
21 | static inline void enableInterrupt(void) {
22 | asm volatile("sti");
23 | }
24 |
25 | /* 关闭外部中断 */
26 | static inline void disableInterrupt(void) {
27 | asm volatile("cli");
28 | }
29 |
30 | #define NR_IRQ 256
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/serial.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define SERIAL_PORT 0x3F8
5 |
6 | void initSerial(void) {
7 | outByte(SERIAL_PORT + 1, 0x00);
8 | outByte(SERIAL_PORT + 3, 0x80);
9 | outByte(SERIAL_PORT + 0, 0x01);
10 | outByte(SERIAL_PORT + 1, 0x00);
11 | outByte(SERIAL_PORT + 3, 0x03);
12 | outByte(SERIAL_PORT + 2, 0xC7);
13 | outByte(SERIAL_PORT + 4, 0x0B);
14 | }
15 |
16 | static inline int serialIdle(void) {
17 | return (inByte(SERIAL_PORT + 5) & 0x20) != 0;
18 | }
19 |
20 | void putChar(char ch) {
21 | while (serialIdle() != TRUE);
22 | outByte(SERIAL_PORT, ch);
23 | }
24 |
25 | void putInt(int a){
26 | if(a < 0){
27 | putChar(45);
28 | putInt(-a);
29 | }
30 | else
31 | putInt(a);
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/genFS.dSYM/Contents/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleIdentifier
8 | com.apple.xcode.dsym.genFS
9 | CFBundleInfoDictionaryVersion
10 | 6.0
11 | CFBundlePackageType
12 | dSYM
13 | CFBundleSignature
14 | ????
15 | CFBundleShortVersionString
16 | 1.0
17 | CFBundleVersion
18 | 1
19 |
20 |
21 |
--------------------------------------------------------------------------------
/lab5/code/kernel/main.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 | #include "fs.h"
4 |
5 | void kEntry(void) {
6 |
7 | // Interruption is disabled in bootloader
8 |
9 | initSerial();// initialize serial port
10 | initIdt(); // initialize idt
11 | initIntr(); // iniialize 8259a
12 | initSeg(); // initialize gdt, tss
13 | initVga(); // initialize vga device
14 | initTimer(); // initialize timer device
15 | initKeyTable(); // initialize keyboard device
16 | initSem(); // initialize semaphore list, i.e., semaphore descriptor
17 | initFS(); // initialize filesystem superBlock & groupDesc
18 | initDev(); // initialize device list, i.e., device descriptor
19 | initFile(); // initialize file list, i.e., file descriptor
20 | initProc(); // initialize pcb & load user program
21 | }
22 |
--------------------------------------------------------------------------------
/lab3/lab3/app/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O2 -I../lib
7 | LDFLAGS = -m elf_i386
8 |
9 | UCFILES = $(shell find ./ -name "*.c")
10 | LCFILES = $(shell find ../lib -name "*.c")
11 | UOBJS = $(UCFILES:.c=.o) $(LCFILES:.c=.o)
12 | #UOBJS = $(LCFILES:.c=.o) $(UCFILES:.c=.o)
13 |
14 | umain.bin: $(UOBJS)
15 | @#$(LD) $(LDFLAGS) -e uEntry -Ttext 0x00200000 -o uMain.elf $(UOBJS)
16 | $(LD) $(LDFLAGS) -e uEntry -Ttext 0x00000000 -o uMain.elf $(UOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary uMain.elf uMain.bin
18 | @#objcopy -O binary uMain.elf uMain.bin
19 |
20 | clean:
21 | @#rm -rf $(UOBJS) uMain.elf uMain.bin
22 | rm -rf $(UOBJS) uMain.elf
23 |
--------------------------------------------------------------------------------
/lab4/lab4/app/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O2 -I../lib
7 | LDFLAGS = -m elf_i386
8 |
9 | UCFILES = $(shell find ./ -name "*.c")
10 | LCFILES = $(shell find ../lib -name "*.c")
11 | UOBJS = $(UCFILES:.c=.o) $(LCFILES:.c=.o)
12 | #UOBJS = $(LCFILES:.c=.o) $(UCFILES:.c=.o)
13 |
14 | umain.bin: $(UOBJS)
15 | @#$(LD) $(LDFLAGS) -e uEntry -Ttext 0x00200000 -o uMain.elf $(UOBJS)
16 | $(LD) $(LDFLAGS) -e uEntry -Ttext 0x00000000 -o uMain.elf $(UOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary uMain.elf uMain.bin
18 | @#objcopy -O binary uMain.elf uMain.bin
19 |
20 | clean:
21 | @#rm -rf $(UOBJS) uMain.elf uMain.bin
22 | rm -rf $(UOBJS) uMain.elf
23 |
--------------------------------------------------------------------------------
/lab1/bootloader/boot.c:
--------------------------------------------------------------------------------
1 | #include "boot.h"
2 | //#include
3 |
4 | #define SECTSIZE 512
5 |
6 | void bootMain(void) {
7 | void (*ptr)(void);
8 | ptr = (void(*)(void))0x8c00;
9 | readSect((void*)ptr, 1);
10 | //void *elf = 0x8c00;
11 | //readSect(elf,1);
12 | ptr();
13 | }
14 |
15 | void waitDisk(void) { // waiting for disk
16 | while((inByte(0x1F7) & 0xC0) != 0x40);
17 | }
18 |
19 | void readSect(void *dst, int offset) { // reading a sector of disk
20 | int i;
21 | waitDisk();
22 | outByte(0x1F2, 1);
23 | outByte(0x1F3, offset);
24 | outByte(0x1F4, offset >> 8);
25 | outByte(0x1F5, offset >> 16);
26 | outByte(0x1F6, (offset >> 24) | 0xE0);
27 | outByte(0x1F7, 0x20);
28 |
29 | waitDisk();
30 | for (i = 0; i < SECTSIZE / 4; i ++) {
31 | ((int *)dst)[i] = inLong(0x1F0);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/lab2/lab2/app/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O0 -g -I../lib
7 | LDFLAGS = -m elf_i386
8 |
9 | UCFILES = $(shell find ./ -name "*.c")
10 | LCFILES = $(shell find ../lib -name "*.c")
11 | UOBJS = $(UCFILES:.c=.o) $(LCFILES:.c=.o)
12 | #UOBJS = $(LCFILES:.c=.o) $(UCFILES:.c=.o)
13 |
14 | umain.bin: $(UOBJS)
15 | $(LD) $(LDFLAGS) -e uEntry -Ttext 0x00200000 -o uMain.elf $(UOBJS)
16 | @#$(LD) $(LDFLAGS) -e uEntry -Ttext 0x00000000 -o uMain.elf $(UOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary uMain.elf uMain.bin
18 | @#objcopy -O binary uMain.elf uMain.bin
19 |
20 | clean:
21 | @#rm -rf $(UOBJS) uMain.elf uMain.bin
22 | rm -rf $(UOBJS) uMain.elf
23 | rm -f app.s
24 |
--------------------------------------------------------------------------------
/lab5/code/kernel/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O0 -I./include
7 | ASFLAGS = -m32
8 | LDFLAGS = -m elf_i386
9 |
10 | KCFILES = $(shell find ./ -name "*.c")
11 | KSFILES = $(shell find ./ -name "*.S")
12 | KOBJS = $(KCFILES:.c=.o) $(KSFILES:.S=.o)
13 | #KOBJS = $(KSFILES:.S=.o) $(KCFILES:.c=.o)
14 |
15 | kmain.bin: $(KOBJS)
16 | $(LD) $(LDFLAGS) -e kEntry -Ttext 0x00100000 -o kMain.elf $(KOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary kMain.elf kMain.bin
18 | @#objcopy -O binary kMain.elf kMain.bin
19 | @../utils/genKernel.pl kMain.elf
20 | @#../utils/genKernel.pl kMain.bin
21 |
22 |
23 | clean:
24 | @#rm -rf $(KOBJS) kMain.elf kMain.bin
25 | rm -rf $(KOBJS) kMain.elf
26 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O2 -I./include
7 | ASFLAGS = -m32
8 | LDFLAGS = -m elf_i386
9 |
10 | KCFILES = $(shell find ./ -name "*.c")
11 | KSFILES = $(shell find ./ -name "*.S")
12 | KOBJS = $(KCFILES:.c=.o) $(KSFILES:.S=.o)
13 | #KOBJS = $(KSFILES:.S=.o) $(KCFILES:.c=.o)
14 |
15 | kmain.bin: $(KOBJS)
16 | $(LD) $(LDFLAGS) -e kEntry -Ttext 0x00100000 -o kMain.elf $(KOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary kMain.elf kMain.bin
18 | @#objcopy -O binary kMain.elf kMain.bin
19 | @../utils/genKernel.pl kMain.elf
20 | @#../utils/genKernel.pl kMain.bin
21 |
22 |
23 | clean:
24 | @#rm -rf $(KOBJS) kMain.elf kMain.bin
25 | rm -rf $(KOBJS) kMain.elf kMain.bin
26 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O2 -I./include
7 | ASFLAGS = -m32
8 | LDFLAGS = -m elf_i386
9 |
10 | KCFILES = $(shell find ./ -name "*.c")
11 | KSFILES = $(shell find ./ -name "*.S")
12 | KOBJS = $(KCFILES:.c=.o) $(KSFILES:.S=.o)
13 | #KOBJS = $(KSFILES:.S=.o) $(KCFILES:.c=.o)
14 |
15 | kmain.bin: $(KOBJS)
16 | $(LD) $(LDFLAGS) -e kEntry -Ttext 0x00100000 -o kMain.elf $(KOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary kMain.elf kMain.bin
18 | @#objcopy -O binary kMain.elf kMain.bin
19 | @../utils/genKernel.pl kMain.elf
20 | @#../utils/genKernel.pl kMain.bin
21 |
22 |
23 | clean:
24 | @#rm -rf $(KOBJS) kMain.elf kMain.bin
25 | rm -rf $(KOBJS) kMain.elf kMain.bin
26 |
--------------------------------------------------------------------------------
/lab5/code/app/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O0 -I../lib
7 | LDFLAGS = -m elf_i386
8 |
9 | UCFILES = $(shell find ./ -name "*.c")
10 | LCFILES = $(shell find ../lib -name "*.c")
11 | UOBJS = $(UCFILES:.c=.o) $(LCFILES:.c=.o)
12 | #UOBJS = $(LCFILES:.c=.o) $(UCFILES:.c=.o)
13 |
14 | umain.bin: $(UOBJS)
15 | @#$(LD) $(LDFLAGS) -e uEntry -Ttext 0x00200000 -o uMain.elf $(UOBJS)
16 | $(LD) $(LDFLAGS) -e uEntry -Ttext 0x00000000 -o uMain.elf $(UOBJS)
17 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary uMain.elf uMain.bin
18 | @#objcopy -O binary uMain.elf uMain.bin
19 | @../utils/genFS/genFS uMain.elf
20 |
21 | clean:
22 | @#rm -rf $(UOBJS) uMain.elf uMain.bin
23 | rm -rf $(UOBJS) uMain.elf fs.bin
24 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | LD = ld
3 |
4 | CFLAGS = -m32 -march=i386 -static \
5 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
6 | -Wall -Werror -O2 -I./include
7 |
8 | ASFLAGS = -m32
9 | LDFLAGS = -m elf_i386
10 |
11 | KCFILES = $(shell find ./ -name "*.c")
12 | KSFILES = $(shell find ./ -name "*.S")
13 |
14 | KOBJS = $(KCFILES:.c=.o) $(KSFILES:.S=.o)
15 | #KOBJS = $(KSFILES:.S=.o) $(KCFILES:.c=.o)
16 |
17 | kmain.bin: $(KOBJS)
18 | $(LD) $(LDFLAGS) -e kEntry -Ttext 0x00100000 -o kMain.elf $(KOBJS)
19 | @#objcopy -S -j .text -j .rodata -j .eh_frame -j .data -j .bss -O binary kMain.elf kMain.bin
20 | @#objcopy -O binary kMain.elf kMain.bin
21 | @../utils/genKernel.pl kMain.elf
22 | @#../utils/genKernel.pl kMain.bin
23 |
24 |
25 | clean:
26 | @#rm -rf $(KOBJS) kMain.elf kMain.bin
27 | rm -rf $(KOBJS) kMain.elf kMain.bin
28 |
--------------------------------------------------------------------------------
/lab4/lab4/lib/lib.h:
--------------------------------------------------------------------------------
1 | #ifndef __lib_h__
2 | #define __lib_h__
3 |
4 | #include "types.h"
5 |
6 | #define SYS_WRITE 0
7 | #define SYS_READ 1
8 | #define SYS_FORK 2
9 | #define SYS_EXEC 3
10 | #define SYS_SLEEP 4
11 | #define SYS_EXIT 5
12 | #define SYS_SEM 6
13 |
14 | #define STD_OUT 0
15 | #define STD_IN 1
16 |
17 | #define SEM_INIT 0
18 | #define SEM_WAIT 1
19 | #define SEM_POST 2
20 | #define SEM_DESTROY 3
21 |
22 | #define MAX_BUFFER_SIZE 256
23 |
24 | int printf(const char *format,...);
25 |
26 | int scanf(const char *format,...);
27 |
28 | pid_t fork();
29 |
30 | int exec(void (*func)(void));
31 |
32 | int sleep(uint32_t time);
33 |
34 | int exit();
35 |
36 | int sem_init(sem_t *sem, uint32_t value);
37 |
38 | int sem_wait(sem_t *sem);
39 |
40 | int sem_post(sem_t *sem);
41 |
42 | int sem_destroy(sem_t *sem);
43 |
44 | int getpid();
45 |
46 | #endif
47 |
--------------------------------------------------------------------------------
/lab5/code/kernel/lib/abort.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | static char *i2A(int a) {
5 | static char buf[30];
6 | char *p = buf + sizeof(buf) - 1;
7 | do {
8 | *--p = '0' + a % 10;
9 | } while (a /= 10);
10 | return p;
11 | }
12 |
13 | static void append(char **p, const char *str) {
14 | while (*str) {
15 | *((*p) ++) = *str ++;
16 | }
17 | }
18 |
19 | /* 将文件名和assert fail的行号显示在屏幕上 */
20 | #define BLUE_SCREEN_TEXT "Assertion failed: "
21 | static void displayMessage(const char *file, int line) {
22 | static char buf[256] = BLUE_SCREEN_TEXT;
23 | char *p = buf + sizeof(BLUE_SCREEN_TEXT) - 1;
24 |
25 | append(&p, file);
26 | append(&p, ":");
27 | append(&p, i2A(line));
28 | append(&p, "\n");
29 |
30 | for (p = buf; *p; p ++) {
31 | putChar(*p);
32 | }
33 | }
34 |
35 | int abort(const char *fname, int line) {
36 | disableInterrupt();
37 | displayMessage(fname, line);
38 | while (TRUE) {
39 | waitForInterrupt();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/serial.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define SERIAL_PORT 0x3F8
4 |
5 | void initSerial(void) {
6 | outByte(SERIAL_PORT + 1, 0x00);
7 | outByte(SERIAL_PORT + 3, 0x80);
8 | outByte(SERIAL_PORT + 0, 0x01);
9 | outByte(SERIAL_PORT + 1, 0x00);
10 | outByte(SERIAL_PORT + 3, 0x03);
11 | outByte(SERIAL_PORT + 2, 0xC7);
12 | outByte(SERIAL_PORT + 4, 0x0B);
13 | }
14 |
15 | static inline int serialIdle(void) {
16 | return (inByte(SERIAL_PORT + 5) & 0x20) != 0;
17 | }
18 |
19 | void putChar(char ch) {
20 | while (serialIdle() != TRUE);
21 | outByte(SERIAL_PORT, ch);
22 | }
23 |
24 | void putString(const char *str) {
25 | int i = 0;
26 | if (str == NULL) {
27 | return;
28 | }
29 | while (str[i] != 0) {
30 | putChar(str[i++]);
31 | }
32 | }
33 |
34 | void putInt(int a) {
35 | char buf[32];
36 | char *p = buf + sizeof(buf) - 1;
37 | *p = '\0';
38 | *(--p) = '\n';
39 | do {
40 | *--p = '0' + a % 10;
41 | } while (a /= 10);
42 | putString(p);
43 | }
44 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/lib/abort.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | static char *i2A(int a) {
6 | static char buf[30];
7 | char *p = buf + sizeof(buf) - 1;
8 | do {
9 | *--p = '0' + a % 10;
10 | } while (a /= 10);
11 | return p;
12 | }
13 |
14 | static void append(char **p, const char *str) {
15 | while (*str) {
16 | *((*p) ++) = *str ++;
17 | }
18 | }
19 |
20 | /* 将文件名和assert fail的行号显示在屏幕上 */
21 | #define BLUE_SCREEN_TEXT "Assertion failed: "
22 | static void displayMessage(const char *file, int line) {
23 | static char buf[256] = BLUE_SCREEN_TEXT;
24 | char *p = buf + sizeof(BLUE_SCREEN_TEXT) - 1;
25 |
26 | append(&p, file);
27 | append(&p, ":");
28 | append(&p, i2A(line));
29 | append(&p, "\n");
30 |
31 | for (p = buf; *p; p ++) {
32 | putChar(*p);
33 | }
34 | }
35 |
36 | int abort(const char *fname, int line) {
37 | disableInterrupt();
38 | displayMessage(fname, line);
39 | while (TRUE) {
40 | waitForInterrupt();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/lib/abort.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | static char *i2A(int a) {
6 | static char buf[30];
7 | char *p = buf + sizeof(buf) - 1;
8 | do {
9 | *--p = '0' + a % 10;
10 | } while (a /= 10);
11 | return p;
12 | }
13 |
14 | static void append(char **p, const char *str) {
15 | while (*str) {
16 | *((*p) ++) = *str ++;
17 | }
18 | }
19 |
20 | /* 将文件名和assert fail的行号显示在屏幕上 */
21 | #define BLUE_SCREEN_TEXT "Assertion failed: "
22 | static void displayMessage(const char *file, int line) {
23 | static char buf[256] = BLUE_SCREEN_TEXT;
24 | char *p = buf + sizeof(BLUE_SCREEN_TEXT) - 1;
25 |
26 | append(&p, file);
27 | append(&p, ":");
28 | append(&p, i2A(line));
29 | append(&p, "\n");
30 |
31 | for (p = buf; *p; p ++) {
32 | putChar(*p);
33 | }
34 | }
35 |
36 | int abort(const char *fname, int line) {
37 | disableInterrupt();
38 | displayMessage(fname, line);
39 | while (TRUE) {
40 | waitForInterrupt();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/lib/abort.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 | #include "x86.h"
3 | #include "device.h"
4 |
5 | static char *i2A(int a) {
6 | static char buf[30];
7 | char *p = buf + sizeof(buf) - 1;
8 | do {
9 | *--p = '0' + a % 10;
10 | } while (a /= 10);
11 | return p;
12 | }
13 |
14 | static void append(char **p, const char *str) {
15 | while (*str) {
16 | *((*p) ++) = *str ++;
17 | }
18 | }
19 |
20 | /* 将文件名和assert fail的行号显示在屏幕上 */
21 | #define BLUE_SCREEN_TEXT "Assertion failed: "
22 | static void displayMessage(const char *file, int line) {
23 | static char buf[256] = BLUE_SCREEN_TEXT;
24 | char *p = buf + sizeof(BLUE_SCREEN_TEXT) - 1;
25 |
26 | append(&p, file);
27 | append(&p, ":");
28 | append(&p, i2A(line));
29 | append(&p, "\n");
30 |
31 | for (p = buf; *p; p ++) {
32 | putChar(*p);
33 | }
34 | }
35 |
36 | int abort(const char *fname, int line) {
37 | disableInterrupt();
38 | displayMessage(fname, line);
39 | while (TRUE) {
40 | waitForInterrupt();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "utils.h"
4 | #include "data.h"
5 | #include "func.h"
6 |
7 | int main(int argc, char *argv[]) {
8 | char driver[NAME_LENGTH];
9 | char srcFilePath[NAME_LENGTH];
10 | char destFilePath[NAME_LENGTH];
11 |
12 | stringCpy("fs.bin", driver, NAME_LENGTH - 1);
13 | format(driver, SECTOR_NUM, SECTORS_PER_BLOCK);
14 |
15 | stringCpy("/boot", destFilePath, NAME_LENGTH - 1);
16 | mkdir(driver, destFilePath);
17 | stringCpy(argv[1], srcFilePath, NAME_LENGTH - 1);
18 | stringCpy("/boot/initrd", destFilePath, NAME_LENGTH - 1);
19 | cp(driver, srcFilePath, destFilePath);
20 |
21 | stringCpy("/dev", destFilePath, NAME_LENGTH - 1);
22 | mkdir(driver, destFilePath);
23 | stringCpy("/dev/stdin", destFilePath, NAME_LENGTH - 1);
24 | touch(driver, destFilePath);
25 | stringCpy("/dev/stdout", destFilePath, NAME_LENGTH - 1);
26 | touch(driver, destFilePath);
27 |
28 | stringCpy("/usr", destFilePath, NAME_LENGTH - 1);
29 | mkdir(driver, destFilePath);
30 |
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/serial.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | void initSerial(void) {
5 | outByte(SERIAL_PORT + 1, 0x00);
6 | outByte(SERIAL_PORT + 3, 0x80);
7 | outByte(SERIAL_PORT + 0, 0x01);
8 | outByte(SERIAL_PORT + 1, 0x00);
9 | outByte(SERIAL_PORT + 3, 0x03);
10 | outByte(SERIAL_PORT + 2, 0xC7);
11 | outByte(SERIAL_PORT + 4, 0x0B);
12 | }
13 |
14 | static inline int serialIdle(void) {
15 | return (inByte(SERIAL_PORT + 5) & 0x20) != 0;
16 | }
17 |
18 | void putChar(char ch) {
19 | while (serialIdle() != TRUE);
20 | outByte(SERIAL_PORT, ch);
21 | }
22 |
23 | void putString(const char *str) {
24 | int i = 0;
25 | if (str == NULL) {
26 | return;
27 | }
28 | while (str[i] != 0) {
29 | putChar(str[i++]);
30 | }
31 | }
32 |
33 | void putInt(int a) {
34 | char buf[32];
35 | char *p = buf + sizeof(buf) - 1;
36 | *p = '\0';
37 | *(--p) = '\n';
38 | do {
39 | *--p = '0' + a % 10;
40 | } while (a /= 10);
41 | putString(p);
42 | }
43 |
44 |
45 | void putint(int a){
46 | if(a < 0){
47 | putChar(45);
48 | putInt(-a);
49 | }
50 | else
51 | putInt(a);
52 | }
--------------------------------------------------------------------------------
/lab5/code/kernel/include/fs.h:
--------------------------------------------------------------------------------
1 | #ifndef __FS_H__
2 | #define __FS_H__
3 |
4 | #include "fs/ext.h"
5 |
6 | int readGroupHeader (SuperBlock *superBlock, GroupDesc *groupDesc);
7 |
8 | int allocInode (SuperBlock *superBlock, GroupDesc *groupDesc,
9 | Inode *fatherInode, int fatherInodeOffset,
10 | Inode *destInode, int *destInodeOffset, const char *destFilename, int destFiletype);
11 | int freeInode (SuperBlock *superBlock, GroupDesc *groupDesc,
12 | Inode *fatherInode, int fatherInodeOffset,
13 | Inode *destInode, int *destInodeOffset, const char *destFilename, int destFiletype);
14 |
15 | int readInode (SuperBlock *superBlock, GroupDesc *groupDesc,
16 | Inode *destInode, int *inodeOffset, const char *destFilePath);
17 |
18 | int allocBlock (SuperBlock *superBlock, GroupDesc *groupDesc, Inode *inode, int inodeOffset);
19 |
20 | int readBlock (SuperBlock *superBlock, Inode *inode, int blockIndex, uint8_t *buffer);
21 |
22 | int writeBlock (SuperBlock *superBlock, Inode *inode, int blockIndex, uint8_t *buffer);
23 |
24 | int getDirEntry (SuperBlock *superBlock, Inode *inode, int dirIndex, DirEntry *destDirEntry);
25 |
26 | void initFS (void);
27 |
28 | void initFile (void);
29 |
30 | #endif
31 |
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/boot.c:
--------------------------------------------------------------------------------
1 | #include "boot.h"
2 |
3 | #define SECTSIZE 512
4 |
5 |
6 | void bootMain(void) {
7 | int i = 0;
8 | int phoff = 0x34;
9 | int offset = 0x1000;
10 | unsigned int elf = 0x100000;
11 | void (*kMainEntry)(void);
12 | kMainEntry = (void(*)(void))0x100000;
13 |
14 | for (i = 0; i < 200; i++) {
15 | readSect((void*)(elf + i*512), 1+i);
16 | }
17 |
18 | kMainEntry = (void(*)(void))((struct ELFHeader *)elf)->entry;
19 | phoff = ((struct ELFHeader *)elf)->phoff;
20 | offset = ((struct ProgramHeader *)(elf + phoff))->off;
21 |
22 | for (i = 0; i < 200 * 512; i++) {
23 | *(unsigned char *)(elf + i) = *(unsigned char *)(elf + i + offset);
24 | }
25 |
26 | kMainEntry();
27 | }
28 |
29 | void waitDisk(void) { // waiting for disk
30 | while((inByte(0x1F7) & 0xC0) != 0x40);
31 | }
32 |
33 | void readSect(void *dst, int offset) { // reading a sector of disk
34 | int i;
35 | waitDisk();
36 | outByte(0x1F2, 1);
37 | outByte(0x1F3, offset);
38 | outByte(0x1F4, offset >> 8);
39 | outByte(0x1F5, offset >> 16);
40 | outByte(0x1F6, (offset >> 24) | 0xE0);
41 | outByte(0x1F7, 0x20);
42 |
43 | waitDisk();
44 | for (i = 0; i < SECTSIZE / 4; i ++) {
45 | ((int *)dst)[i] = inLong(0x1F0);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/lab5/code/lib/lib.h:
--------------------------------------------------------------------------------
1 | #ifndef __lib_h__
2 | #define __lib_h__
3 |
4 | #define SYS_OPEN 0
5 | #define SYS_WRITE 1
6 | #define SYS_READ 2
7 | #define SYS_LSEEK 3
8 | #define SYS_CLOSE 4
9 | #define SYS_REMOVE 5
10 | #define SYS_FORK 6
11 | #define SYS_EXEC 7
12 | #define SYS_SLEEP 8
13 | #define SYS_EXIT 9
14 | #define SYS_SEM 10
15 |
16 | #define STD_OUT 0
17 | #define STD_IN 1
18 |
19 | #define O_WRITE 0x01
20 | #define O_READ 0x02
21 | #define O_CREATE 0x04
22 | #define O_DIRECTORY 0x08
23 |
24 | #define SEM_INIT 0
25 | #define SEM_WAIT 1
26 | #define SEM_POST 2
27 | #define SEM_DESTROY 3
28 |
29 | #define MAX_BUFFER_SIZE 256
30 |
31 | int open(char *path, int flags);
32 |
33 | int write(int fd, uint8_t *buffer, int size);
34 |
35 | int read(int fd, uint8_t *buffer, int size);
36 |
37 | int lseek(int fd, int offset, int whence);
38 |
39 | int close(int fd);
40 |
41 | int remove (char *path);
42 |
43 | int printf(const char *format,...);
44 |
45 | int scanf(const char *format,...);
46 |
47 | pid_t fork();
48 |
49 | int exec(void (*func)(void));
50 |
51 | int sleep(uint32_t time);
52 |
53 | int exit();
54 |
55 | int sem_init(sem_t *sem, uint32_t value);
56 |
57 | int sem_wait(sem_t *sem);
58 |
59 | int sem_post(sem_t *sem);
60 |
61 | int sem_destroy(sem_t *sem);
62 |
63 | #endif
64 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/x86/io.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_IO_H__
2 | #define __X86_IO_H__
3 | /* ELF32二进制文件头 */
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 |
35 | static inline int inLong(short port) {
36 | int data;
37 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
38 | return data;
39 | }
40 |
41 | /* 读I/O端口 */
42 | static inline uint8_t inByte(uint16_t port) {
43 | uint8_t data;
44 | asm volatile("in %1, %0" : "=a"(data) : "d"(port));
45 | return data;
46 | }
47 |
48 | /* 写I/O端口 */
49 | static inline void outByte(uint16_t port, int8_t data) {
50 | asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
51 | }
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/x86/io.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_IO_H__
2 | #define __X86_IO_H__
3 | /* ELF32二进制文件头 */
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 |
35 | static inline int inLong(short port) {
36 | int data;
37 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
38 | return data;
39 | }
40 |
41 | /* 读I/O端口 */
42 | static inline uint8_t inByte(uint16_t port) {
43 | uint8_t data;
44 | asm volatile("in %1, %0" : "=a"(data) : "d"(port));
45 | return data;
46 | }
47 |
48 | /* 写I/O端口 */
49 | static inline void outByte(uint16_t port, int8_t data) {
50 | asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
51 | }
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/x86/io.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_IO_H__
2 | #define __X86_IO_H__
3 | /* ELF32二进制文件头 */
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 |
35 | static inline int inLong(short port) {
36 | int data;
37 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
38 | return data;
39 | }
40 |
41 | /* 读I/O端口 */
42 | static inline uint8_t inByte(uint16_t port) {
43 | uint8_t data;
44 | asm volatile("in %1, %0" : "=a"(data) : "d"(port));
45 | return data;
46 | }
47 |
48 | /* 写I/O端口 */
49 | static inline void outByte(uint16_t port, int8_t data) {
50 | asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
51 | }
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/boot.h:
--------------------------------------------------------------------------------
1 | #ifndef BOOT_H
2 | #define BOOT_H
3 |
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 | void waitDisk(void);
35 |
36 | void readSect(void *dst, int offset);
37 |
38 | /* I/O functions */
39 | static inline char inByte(short port) {
40 | char data;
41 | asm volatile("in %1,%0" : "=a" (data) : "d" (port));
42 | return data;
43 | }
44 |
45 | static inline int inLong(short port) {
46 | int data;
47 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
48 | return data;
49 | }
50 |
51 | static inline void outByte(short port, char data) {
52 | asm volatile("out %0,%1" : : "a" (data), "d" (port));
53 | }
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/boot.h:
--------------------------------------------------------------------------------
1 | #ifndef BOOT_H
2 | #define BOOT_H
3 |
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 | void waitDisk(void);
35 |
36 | void readSect(void *dst, int offset);
37 |
38 | /* I/O functions */
39 | static inline char inByte(short port) {
40 | char data;
41 | asm volatile("in %1,%0" : "=a" (data) : "d" (port));
42 | return data;
43 | }
44 |
45 | static inline int inLong(short port) {
46 | int data;
47 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
48 | return data;
49 | }
50 |
51 | static inline void outByte(short port, char data) {
52 | asm volatile("out %0,%1" : : "a" (data), "d" (port));
53 | }
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/boot.h:
--------------------------------------------------------------------------------
1 | #ifndef BOOT_H
2 | #define BOOT_H
3 |
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 | void waitDisk(void);
35 |
36 | void readSect(void *dst, int offset);
37 |
38 | /* I/O functions */
39 | static inline char inByte(short port) {
40 | char data;
41 | asm volatile("in %1,%0" : "=a" (data) : "d" (port));
42 | return data;
43 | }
44 |
45 | static inline int inLong(short port) {
46 | int data;
47 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
48 | return data;
49 | }
50 |
51 | static inline void outByte(short port, char data) {
52 | asm volatile("out %0,%1" : : "a" (data), "d" (port));
53 | }
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/lab5/code/bootloader/boot.h:
--------------------------------------------------------------------------------
1 | #ifndef BOOT_H
2 | #define BOOT_H
3 |
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 | void waitDisk(void);
35 |
36 | void readSect(void *dst, int offset);
37 |
38 | /* I/O functions */
39 | static inline unsigned char inByte(unsigned short port) {
40 | char data;
41 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
42 | return data;
43 | }
44 |
45 | static inline unsigned int inLong(unsigned short port) {
46 | int data;
47 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
48 | return data;
49 | }
50 |
51 | static inline void outByte(unsigned short port, unsigned char data) {
52 | asm volatile("out %0, %1" : : "a" (data), "d" (port));
53 | }
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/Makefile:
--------------------------------------------------------------------------------
1 | #bootloader.bin: start.S boot.c boot.h
2 | # gcc -c -m32 start.S -o start.o
3 | # gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
4 | # ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
5 | # @#ld -m elf_i386 -e start -Ttext 0x7c00 boot.o start.o -o bootloader.elf
6 | # @#objcopy -S -j .text -O binary bootloader.elf bootloader.bin
7 | # objcopy -O binary bootloader.elf bootloader.bin
8 | # ../utils/genBoot.pl bootloader.bin
9 | #
10 | #clean:
11 | # rm -rf *.o *.elf *.bin
12 |
13 | # take care of link order of object files
14 | # -Ttext set the address of the first byte of the text segment
15 | # -e set the entry address in elf-header
16 | # i.e., the entry address may not be the address of the first byte of the text segment
17 |
18 | CC = gcc
19 | LD = ld
20 |
21 | CFLAGS = -m32 -march=i386 -static \
22 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
23 | -Wall -Werror -O2
24 | ASFLAGS = -m32
25 | LDFLAGS = -m elf_i386
26 |
27 | BSFILES = $(shell find ./ -name "*.S")
28 | BCFILES = $(shell find ./ -name "*.c")
29 | BOBJS = $(BSFILES:.S=.o) $(BCFILES:.c=.o)
30 |
31 | bootloader.bin: $(BOBJS)
32 | $(LD) $(LDFLAGS) -e start -Ttext 0x7c00 -o bootloader.elf $(BOBJS)
33 | objcopy -O binary bootloader.elf bootloader.bin
34 | @../utils/genBoot.pl bootloader.bin
35 |
36 | clean:
37 | rm -rf $(BOBJS) bootloader.elf bootloader.bin
38 |
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/Makefile:
--------------------------------------------------------------------------------
1 | #bootloader.bin: start.S boot.c boot.h
2 | # gcc -c -m32 start.S -o start.o
3 | # gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
4 | # ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
5 | # @#ld -m elf_i386 -e start -Ttext 0x7c00 boot.o start.o -o bootloader.elf
6 | # @#objcopy -S -j .text -O binary bootloader.elf bootloader.bin
7 | # objcopy -O binary bootloader.elf bootloader.bin
8 | # ../utils/genBoot.pl bootloader.bin
9 | #
10 | #clean:
11 | # rm -rf *.o *.elf *.bin
12 |
13 | # take care of link order of object files
14 | # -Ttext set the address of the first byte of the text segment
15 | # -e set the entry address in elf-header
16 | # i.e., the entry address may not be the address of the first byte of the text segment
17 |
18 | CC = gcc
19 | LD = ld
20 |
21 | CFLAGS = -m32 -march=i386 -static \
22 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
23 | -Wall -Werror -O2
24 | ASFLAGS = -m32
25 | LDFLAGS = -m elf_i386
26 |
27 | BSFILES = $(shell find ./ -name "*.S")
28 | BCFILES = $(shell find ./ -name "*.c")
29 | BOBJS = $(BSFILES:.S=.o) $(BCFILES:.c=.o)
30 |
31 | bootloader.bin: $(BOBJS)
32 | $(LD) $(LDFLAGS) -e start -Ttext 0x7c00 -o bootloader.elf $(BOBJS)
33 | objcopy -O binary bootloader.elf bootloader.bin
34 | @../utils/genBoot.pl bootloader.bin
35 |
36 | clean:
37 | rm -rf $(BOBJS) bootloader.elf bootloader.bin
38 |
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/Makefile:
--------------------------------------------------------------------------------
1 | #bootloader.bin: start.S boot.c boot.h
2 | # gcc -c -m32 start.S -o start.o
3 | # gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
4 | # ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
5 | # @#ld -m elf_i386 -e start -Ttext 0x7c00 boot.o start.o -o bootloader.elf
6 | # @#objcopy -S -j .text -O binary bootloader.elf bootloader.bin
7 | # objcopy -O binary bootloader.elf bootloader.bin
8 | # ../utils/genBoot.pl bootloader.bin
9 | #
10 | #clean:
11 | # rm -rf *.o *.elf *.bin
12 |
13 | # take care of link order of object files
14 | # -Ttext set the address of the first byte of the text segment
15 | # -e set the entry address in elf-header
16 | # i.e., the entry address may not be the address of the first byte of the text segment
17 |
18 | CC = gcc
19 | LD = ld
20 |
21 | CFLAGS = -m32 -march=i386 -static \
22 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
23 | -Wall -Werror -O2
24 | ASFLAGS = -m32
25 | LDFLAGS = -m elf_i386
26 |
27 | BSFILES = $(shell find ./ -name "*.S")
28 | BCFILES = $(shell find ./ -name "*.c")
29 | BOBJS = $(BSFILES:.S=.o) $(BCFILES:.c=.o)
30 |
31 | bootloader.bin: $(BOBJS)
32 | $(LD) $(LDFLAGS) -e start -Ttext 0x7c00 -o bootloader.elf $(BOBJS)
33 | objcopy -O binary bootloader.elf bootloader.bin
34 | @../utils/genBoot.pl bootloader.bin
35 |
36 | clean:
37 | rm -rf $(BOBJS) bootloader.elf bootloader.bin
38 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/vga.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | int displayRow = 0;
5 | int displayCol = 0;
6 | uint16_t displayMem[80*25];
7 | int displayClear = 0;
8 |
9 | void initVga() {
10 | displayRow = 0;
11 | displayCol = 0;
12 | displayClear = 0;
13 | clearScreen();
14 | updateCursor(0, 0);
15 | }
16 |
17 | void clearScreen() {
18 | int i = 0;
19 | int pos = 0;
20 | uint16_t data = 0 | (0x0c << 8);
21 | for (i = 0; i < 80 * 25; i++) {
22 | pos = i * 2;
23 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
24 | }
25 | }
26 |
27 | void updateCursor(int row, int col){
28 | int cursorPos = row * 80 + col;
29 | outByte(0x3d4, 0x0f);
30 | outByte(0x3d5, (unsigned char)(cursorPos & 0xff));
31 |
32 | outByte(0x3d4, 0x0e);
33 | outByte(0x3d5, (unsigned char)((cursorPos>>8) & 0xff));
34 | }
35 |
36 |
37 | //滚屏
38 | void scrollScreen() {
39 | int i = 0;
40 | int pos = 0;
41 | uint16_t data = 0;
42 | for (i = 0; i < 80 * 25; i++) {
43 | pos = i * 2;
44 | asm volatile("movw (%1), %0":"=r"(data):"r"(pos+0xb8000));
45 | displayMem[i] = data;
46 | }
47 | for (i = 0; i < 80 * 24; i++) {
48 | pos = i * 2;
49 | data = displayMem[i+80];
50 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
51 | }
52 | data = 0 | (0x0c << 8);
53 | for (i = 80 * 24; i < 80 * 25; i++) {
54 | pos = i * 2;
55 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/start.S:
--------------------------------------------------------------------------------
1 | /* Protected Mode Loading Hello World APP */
2 | .code16
3 |
4 | .global start
5 | start:
6 | movw %cs, %ax
7 | movw %ax, %ds
8 | movw %ax, %es
9 | movw %ax, %ss
10 | cli # clear interuption
11 | inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
12 | orb $0x02, %al
13 | outb %al, $0x92
14 | data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
15 | movl %cr0, %eax
16 | orb $0x01, %al
17 | movl %eax, %cr0 # setting cr0
18 | data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
19 |
20 | .code32
21 | start32:
22 | movw $0x10, %ax # setting data segment selector
23 | movw %ax, %ds
24 | movw %ax, %es
25 | movw %ax, %fs
26 | movw %ax, %ss
27 | movw $0x18, %ax # setting graphics data segment selector
28 | movw %ax, %gs
29 |
30 | # TODO: setting esp
31 | jmp bootMain # jump to bootMain in boot.c
32 |
33 | .p2align 2
34 | gdt: # 8 bytes for each table entry, at least 1 entry
35 | .word 0,0 # empty entry
36 | .byte 0,0,0,0
37 |
38 | .word 0xffff,0 # code segment entry
39 | .byte 0,0x9a,0xcf,0
40 |
41 | .word 0xffff,0 # data segment entry
42 | .byte 0,0x92,0xcf,0
43 |
44 | .word 0xffff,0x8000 # graphics segment entry
45 | .byte 0x0b,0x92,0xcf,0
46 |
47 | gdtDesc: # 6 bytes in total
48 | .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
49 | .long gdt # offset, i.e. linear address of the table itself
50 |
--------------------------------------------------------------------------------
/lab5/code/bootloader/Makefile:
--------------------------------------------------------------------------------
1 | #bootloader.bin: start.S boot.c boot.h
2 | # gcc -c -m32 start.S -o start.o
3 | # gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
4 | # ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
5 | # @#ld -m elf_i386 -e start -Ttext 0x7c00 boot.o start.o -o bootloader.elf
6 | # @#objcopy -S -j .text -O binary bootloader.elf bootloader.bin
7 | # objcopy -O binary bootloader.elf bootloader.bin
8 | # ../utils/genBoot.pl bootloader.bin
9 | #
10 | #clean:
11 | # rm -rf *.o *.elf *.bin
12 |
13 | # take care of link order of object files
14 | # -Ttext set the address of the first byte of the text segment
15 | # -e set the entry address in elf-header
16 | # i.e., the entry address may not be the address of the first byte of the text segment
17 |
18 | CC = gcc
19 | LD = ld
20 |
21 | CFLAGS = -m32 -march=i386 -static \
22 | -fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
23 | -Wall -Werror -O2
24 | ASFLAGS = -m32
25 | LDFLAGS = -m elf_i386
26 |
27 | BSFILES = $(shell find ./ -name "*.S")
28 | BCFILES = $(shell find ./ -name "*.c")
29 | BOBJS = $(BSFILES:.S=.o) $(BCFILES:.c=.o)
30 |
31 | bootloader.bin: $(BOBJS)
32 | $(LD) $(LDFLAGS) -e start -Ttext 0x7c00 -o bootloader.elf $(BOBJS)
33 | #objcopy -S -j .text -O binary bootloader.elf bootloader.bin
34 | objcopy -O binary bootloader.elf bootloader.bin
35 | @../utils/genBoot.pl bootloader.bin
36 |
37 | clean:
38 | rm -rf $(BOBJS) bootloader.elf bootloader.bin
39 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/x86/io.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_IO_H__
2 | #define __X86_IO_H__
3 | /* ELF32二进制文件头 */
4 | struct ELFHeader {
5 | unsigned int magic;
6 | unsigned char elf[12];
7 | unsigned short type;
8 | unsigned short machine;
9 | unsigned int version;
10 | unsigned int entry;
11 | unsigned int phoff;
12 | unsigned int shoff;
13 | unsigned int flags;
14 | unsigned short ehsize;
15 | unsigned short phentsize;
16 | unsigned short phnum;
17 | unsigned short shentsize;
18 | unsigned short shnum;
19 | unsigned short shstrndx;
20 | };
21 |
22 | /* ELF32 Program header */
23 | struct ProgramHeader {
24 | unsigned int type;
25 | unsigned int off;
26 | unsigned int vaddr;
27 | unsigned int paddr;
28 | unsigned int filesz;
29 | unsigned int memsz;
30 | unsigned int flags;
31 | unsigned int align;
32 | };
33 |
34 |
35 | static inline uint32_t inLong(uint16_t port) {
36 | int data;
37 | asm volatile("in %1, %0" : "=a" (data) : "d" (port));
38 | return data;
39 | }
40 |
41 | static inline void outLong(uint16_t port, uint32_t data) {
42 | asm volatile("out %0, %1" : : "a"(data), "d"(port));
43 | }
44 |
45 | /* 读I/O端口 */
46 | static inline uint8_t inByte(uint16_t port) {
47 | uint8_t data;
48 | asm volatile("in %1, %0" : "=a" (data) : "d"(port));
49 | return data;
50 | }
51 |
52 | /* 写I/O端口 */
53 | static inline void outByte(uint16_t port, uint8_t data) {
54 | asm volatile("out %0, %1" : : "a"(data), "d"(port));
55 | }
56 |
57 | #endif
58 |
--------------------------------------------------------------------------------
/lab4/lab4/bootloader/start.S:
--------------------------------------------------------------------------------
1 | /* Protected Mode Loading Hello World APP */
2 | .code16
3 |
4 | .global start
5 | start:
6 | movw %cs, %ax
7 | movw %ax, %ds
8 | movw %ax, %es
9 | movw %ax, %ss
10 | cli # clear interuption
11 | inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
12 | orb $0x02, %al
13 | outb %al, $0x92
14 | data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
15 | movl %cr0, %eax
16 | orb $0x01, %al
17 | movl %eax, %cr0 # setting cr0
18 | data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
19 |
20 | .code32
21 | start32:
22 | movw $0x10, %ax # setting data segment selector
23 | movw %ax, %ds
24 | movw %ax, %es
25 | movw %ax, %fs
26 | movw %ax, %ss
27 | movw $0x18, %ax # setting graphics data segment selector
28 | movw %ax, %gs
29 |
30 | movl $0x200000, %eax # setting esp
31 | movl %eax, %esp
32 | jmp bootMain # jump to bootMain in boot.c
33 |
34 | .p2align 2
35 | gdt: # 8 bytes for each table entry, at least 1 entry
36 | .word 0,0 # empty entry
37 | .byte 0,0,0,0
38 |
39 | .word 0xffff,0 # code segment entry
40 | .byte 0,0x9a,0xcf,0
41 |
42 | .word 0xffff,0 # data segment entry
43 | .byte 0,0x92,0xcf,0
44 |
45 | .word 0xffff,0x8000 # graphics segment entry
46 | .byte 0x0b,0x92,0xcf,0
47 |
48 | gdtDesc: # 6 bytes in total
49 | .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
50 | .long gdt # offset, i.e. linear address of the table itself
51 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/vga.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | int displayRow = 0; //TODO futher extend, display position, in .bss section, not in .data section
5 | int displayCol = 0;
6 | uint16_t displayMem[80*25];
7 | int displayClear = 0;
8 |
9 | void initVga() {
10 | displayRow = 0;
11 | displayCol = 0;
12 | displayClear = 0;
13 | clearScreen();
14 | updateCursor(0, 0);
15 | }
16 |
17 | void clearScreen() {
18 | int i = 0;
19 | int pos = 0;
20 | uint16_t data = 0 | (0x0c << 8);
21 | for (i = 0; i < 80 * 25; i++) {
22 | pos = i * 2;
23 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
24 | }
25 | }
26 |
27 | void updateCursor(int row, int col){
28 | int cursorPos = row * 80 + col;
29 | outByte(0x3d4, 0x0f);
30 | outByte(0x3d5, (unsigned char)(cursorPos & 0xff));
31 |
32 | outByte(0x3d4, 0x0e);
33 | outByte(0x3d5, (unsigned char)((cursorPos>>8) & 0xff));
34 | }
35 |
36 | void scrollScreen() {
37 | int i = 0;
38 | int pos = 0;
39 | uint16_t data = 0;
40 | for (i = 0; i < 80 * 25; i++) {
41 | pos = i * 2;
42 | asm volatile("movw (%1), %0":"=r"(data):"r"(pos+0xb8000));
43 | displayMem[i] = data;
44 | }
45 | for (i = 0; i < 80 * 24; i++) {
46 | pos = i * 2;
47 | data = displayMem[i+80];
48 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
49 | }
50 | data = 0 | (0x0c << 8);
51 | for (i = 80 * 24; i < 80 * 25; i++) {
52 | pos = i * 2;
53 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/vga.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | int displayRow = 0;
5 | int displayCol = 0;
6 |
7 | static uint16_t displayMem[MAX_ROW*MAX_COL];
8 | static int displayClear = 0;
9 |
10 | void initVga() {
11 | displayRow = 0;
12 | displayCol = 0;
13 | displayClear = 0;
14 | clearScreen();
15 | updateCursor(0, 0);
16 | }
17 |
18 | void clearScreen() {
19 | int i = 0;
20 | int pos = 0;
21 | uint16_t data = 0 | (0x0c << 8);
22 | for (i = 0; i < MAX_ROW * MAX_COL; i++) {
23 | pos = i * 2;
24 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
25 | }
26 | }
27 |
28 | void updateCursor(int row, int col){
29 | int cursorPos = row * MAX_COL + col;
30 | outByte(0x3d4, 0x0f);
31 | outByte(0x3d5, (unsigned char)(cursorPos & 0xff));
32 |
33 | outByte(0x3d4, 0x0e);
34 | outByte(0x3d5, (unsigned char)((cursorPos>>8) & 0xff));
35 | }
36 |
37 | void scrollScreen() {
38 | int i = 0;
39 | int pos = 0;
40 | uint16_t data = 0;
41 | for (i = 0; i < MAX_ROW * MAX_COL; i++) {
42 | pos = i * 2;
43 | asm volatile("movw (%1), %0":"=r"(data):"r"(pos+0xb8000));
44 | displayMem[i] = data;
45 | }
46 | for (i = 0; i < (MAX_ROW-1) * MAX_COL; i++) {
47 | pos = i * 2;
48 | data = displayMem[i+MAX_COL];
49 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
50 | }
51 | data = 0 | (0x0c << 8);
52 | for (i = (MAX_ROW-1) * MAX_COL; i < MAX_ROW * MAX_COL; i++) {
53 | pos = i * 2;
54 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/i8259.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define PORT_PIC_MASTER 0x20
4 | #define PORT_PIC_SLAVE 0xA0
5 | #define IRQ_SLAVE 2
6 |
7 | /* 初始化8259中断控制器:
8 | * 硬件中断IRQ从32号开始,自动发送EOI */
9 | void
10 | initIntr(void) {
11 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
12 | //outByte(PORT_PIC_SLAVE + 1 , 0xFF); // OCW1, Disable Slave PIC all IRQs
13 | outByte(PORT_PIC_MASTER, 0x11); // ICW1, Initialization command
14 | outByte(PORT_PIC_SLAVE, 0x11); // ICW1, Initialization command
15 | outByte(PORT_PIC_MASTER + 1, 32); // ICW2, Interrupt Vector Offset 0x20
16 | outByte(PORT_PIC_SLAVE + 1, 32 + 8); // ICW2, Interrupt Vector Offset 0x28
17 | outByte(PORT_PIC_MASTER + 1, 1 << 2); // ICW3, Tell Master PIC that there is a slave
18 | outByte(PORT_PIC_SLAVE + 1, 2); // ICW3, Tell Slave PIC its cascade identity
19 | outByte(PORT_PIC_MASTER + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
20 | outByte(PORT_PIC_SLAVE + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
21 |
22 | //outByte(PORT_PIC_MASTER, 0x68); // OCW3, Enable Special Mask Mode
23 | //outByte(PORT_PIC_MASTER, 0x0A); // OCW3, Read ISR of Master on next CMD Read
24 | //outByte(PORT_PIC_SLAVE, 0x68); // OCW3, Enable Special Mask Mode
25 | //outByte(PORT_PIC_SLAVE, 0x0A); // OCW3, Read ISR of Slave on next CMD Read
26 |
27 | //outByte(PORT_PIC_MASTER + 1, 0xFE); // OCW1, Enable Timer IRQ
28 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
29 | //outByte(PORT_PIC_SLAVE + 1, 0xFF); // OCW1, Disable Slave PIC all IRQs
30 | }
31 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/i8259.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define PORT_PIC_MASTER 0x20
4 | #define PORT_PIC_SLAVE 0xA0
5 | #define IRQ_SLAVE 2
6 |
7 | /* 初始化8259中断控制器:
8 | * 硬件中断IRQ从32号开始,自动发送EOI */
9 | void
10 | initIntr(void) {
11 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
12 | //outByte(PORT_PIC_SLAVE + 1 , 0xFF); // OCW1, Disable Slave PIC all IRQs
13 | outByte(PORT_PIC_MASTER, 0x11); // ICW1, Initialization command
14 | outByte(PORT_PIC_SLAVE, 0x11); // ICW1, Initialization command
15 | outByte(PORT_PIC_MASTER + 1, 32); // ICW2, Interrupt Vector Offset 0x20
16 | outByte(PORT_PIC_SLAVE + 1, 32 + 8); // ICW2, Interrupt Vector Offset 0x28
17 | outByte(PORT_PIC_MASTER + 1, 1 << 2); // ICW3, Tell Master PIC that there is a slave
18 | outByte(PORT_PIC_SLAVE + 1, 2); // ICW3, Tell Slave PIC its cascade identity
19 | outByte(PORT_PIC_MASTER + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
20 | outByte(PORT_PIC_SLAVE + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
21 |
22 | //outByte(PORT_PIC_MASTER, 0x68); // OCW3, Enable Special Mask Mode
23 | //outByte(PORT_PIC_MASTER, 0x0A); // OCW3, Read ISR of Master on next CMD Read
24 | //outByte(PORT_PIC_SLAVE, 0x68); // OCW3, Enable Special Mask Mode
25 | //outByte(PORT_PIC_SLAVE, 0x0A); // OCW3, Read ISR of Slave on next CMD Read
26 |
27 | //outByte(PORT_PIC_MASTER + 1, 0xFE); // OCW1, Enable Timer IRQ
28 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
29 | //outByte(PORT_PIC_SLAVE + 1, 0xFF); // OCW1, Disable Slave PIC all IRQs
30 | }
31 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/i8259.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define PORT_PIC_MASTER 0x20
4 | #define PORT_PIC_SLAVE 0xA0
5 | #define IRQ_SLAVE 2
6 |
7 | /* 初始化8259中断控制器:
8 | * 硬件中断IRQ从32号开始,自动发送EOI */
9 | void
10 | initIntr(void) {
11 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
12 | //outByte(PORT_PIC_SLAVE + 1 , 0xFF); // OCW1, Disable Slave PIC all IRQs
13 | outByte(PORT_PIC_MASTER, 0x11); // ICW1, Initialization command
14 | outByte(PORT_PIC_SLAVE, 0x11); // ICW1, Initialization command
15 | outByte(PORT_PIC_MASTER + 1, 32); // ICW2, Interrupt Vector Offset 0x20
16 | outByte(PORT_PIC_SLAVE + 1, 32 + 8); // ICW2, Interrupt Vector Offset 0x28
17 | outByte(PORT_PIC_MASTER + 1, 1 << 2); // ICW3, Tell Master PIC that there is a slave
18 | outByte(PORT_PIC_SLAVE + 1, 2); // ICW3, Tell Slave PIC its cascade identity
19 | outByte(PORT_PIC_MASTER + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
20 | outByte(PORT_PIC_SLAVE + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
21 |
22 | //outByte(PORT_PIC_MASTER, 0x68); // OCW3, Enable Special Mask Mode
23 | //outByte(PORT_PIC_MASTER, 0x0A); // OCW3, Read ISR of Master on next CMD Read
24 | //outByte(PORT_PIC_SLAVE, 0x68); // OCW3, Enable Special Mask Mode
25 | //outByte(PORT_PIC_SLAVE, 0x0A); // OCW3, Read ISR of Slave on next CMD Read
26 |
27 | //outByte(PORT_PIC_MASTER + 1, 0xFE); // OCW1, Enable Timer IRQ
28 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
29 | //outByte(PORT_PIC_SLAVE + 1, 0xFF); // OCW1, Disable Slave PIC all IRQs
30 | }
31 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/i8259.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define PORT_PIC_MASTER 0x20
4 | #define PORT_PIC_SLAVE 0xA0
5 | #define IRQ_SLAVE 2
6 |
7 | /* 初始化8259中断控制器:
8 | * 硬件中断IRQ从32号开始,自动发送EOI */
9 | void
10 | initIntr(void) {
11 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
12 | //outByte(PORT_PIC_SLAVE + 1 , 0xFF); // OCW1, Disable Slave PIC all IRQs
13 | outByte(PORT_PIC_MASTER, 0x11); // ICW1, Initialization command
14 | outByte(PORT_PIC_SLAVE, 0x11); // ICW1, Initialization command
15 | outByte(PORT_PIC_MASTER + 1, 32); // ICW2, Interrupt Vector Offset 0x20
16 | outByte(PORT_PIC_SLAVE + 1, 32 + 8); // ICW2, Interrupt Vector Offset 0x28
17 | outByte(PORT_PIC_MASTER + 1, 1 << 2); // ICW3, Tell Master PIC that there is a slave
18 | outByte(PORT_PIC_SLAVE + 1, 2); // ICW3, Tell Slave PIC its cascade identity
19 | outByte(PORT_PIC_MASTER + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
20 | outByte(PORT_PIC_SLAVE + 1, 0x3); // ICW4, Auto EOI in 8086/88 mode
21 |
22 | //outByte(PORT_PIC_MASTER, 0x68); // OCW3, Enable Special Mask Mode
23 | //outByte(PORT_PIC_MASTER, 0x0A); // OCW3, Read ISR of Master on next CMD Read
24 | //outByte(PORT_PIC_SLAVE, 0x68); // OCW3, Enable Special Mask Mode
25 | //outByte(PORT_PIC_SLAVE, 0x0A); // OCW3, Read ISR of Slave on next CMD Read
26 |
27 | //outByte(PORT_PIC_MASTER + 1, 0xFE); // OCW1, Enable Timer IRQ
28 | //outByte(PORT_PIC_MASTER + 1, 0xFF); // OCW1, Disable Master PIC all IRQs
29 | //outByte(PORT_PIC_SLAVE + 1, 0xFF); // OCW1, Disable Slave PIC all IRQs
30 | }
31 |
--------------------------------------------------------------------------------
/lab2/lab2/bootloader/boot.c:
--------------------------------------------------------------------------------
1 | #include "boot.h"
2 |
3 | #define SECTSIZE 512
4 |
5 | /*
6 | void bootMain(void) {
7 | int i = 0;
8 | void (*elf)(void);
9 | elf = (void(*)(void))0x100000; // kernel is loaded to location 0x100000
10 | for (i = 0; i < 200; i ++) {
11 | //readSect((void*)elf + i*512, i+1);
12 | readSect((void*)elf + i*512, i+9);
13 | }
14 | elf(); // jumping to the loaded program
15 | }
16 | */
17 |
18 | void bootMain(void) {
19 | int i = 0;
20 | //int phoff = 0x34;
21 | int offset = 0x1000;
22 | unsigned int elf = 0x100000;
23 | void (*kMainEntry)(void);
24 |
25 | for (i = 0; i < 200; i++) {
26 | readSect((void*)(elf + i*512), 1+i);
27 | }
28 |
29 | // TODO: 填写kMainEntry、phoff、offset
30 | kMainEntry = (void(*)(void))((struct ELFHeader *)elf)->entry;
31 | //phoff = ((struct ELFHeader *)elf)->phoff;
32 | //ffset = ((struct ProgramHeader *)(elf + phoff))->off;
33 |
34 | for (i = 0; i < 200 * 512; i++) {
35 | *(unsigned char *)(elf + i) = *(unsigned char *)(elf + i + offset);
36 | }
37 |
38 | kMainEntry();
39 | }
40 |
41 | void waitDisk(void) { // waiting for disk
42 | while((inByte(0x1F7) & 0xC0) != 0x40);
43 | }
44 |
45 | void readSect(void *dst, int offset) { // reading a sector of disk
46 | int i;
47 | waitDisk();
48 | outByte(0x1F2, 1);
49 | outByte(0x1F3, offset);
50 | outByte(0x1F4, offset >> 8);
51 | outByte(0x1F5, offset >> 16);
52 | outByte(0x1F6, (offset >> 24) | 0xE0);
53 | outByte(0x1F7, 0x20);
54 |
55 | waitDisk();
56 | for (i = 0; i < SECTSIZE / 4; i ++) {
57 | ((int *)dst)[i] = inLong(0x1F0);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/boot.c:
--------------------------------------------------------------------------------
1 | #include "boot.h"
2 |
3 | #define SECTSIZE 512
4 |
5 | /*
6 | void bootMain(void) {
7 | int i = 0;
8 | void (*elf)(void);
9 | elf = (void(*)(void))0x100000; // kernel is loaded to location 0x100000
10 | for (i = 0; i < 200; i ++) {
11 | //readSect((void*)elf + i*512, i+1);
12 | readSect((void*)elf + i*512, i+9);
13 | }
14 | elf(); // jumping to the loaded program
15 | }
16 | */
17 |
18 | void bootMain(void) {
19 | int i = 0;
20 | //int phoff = 0x34;
21 | int offset = 0x1000;
22 | unsigned int elf = 0x100000;
23 | void (*kMainEntry)(void);
24 | kMainEntry = (void(*)(void))0x100000;
25 |
26 | for (i = 0; i < 200; i++) {
27 | readSect((void*)(elf + i*512), 1+i);
28 | }
29 |
30 | kMainEntry = (void(*)(void))((struct ELFHeader *)elf)->entry;
31 | //phoff = ((struct ELFHeader *)elf)->phoff;
32 | //offset = ((struct ProgramHeader *)(elf + phoff))->off;
33 |
34 | for (i = 0; i < 200 * 512; i++) {
35 | *(unsigned char *)(elf + i) = *(unsigned char *)(elf + i + offset);
36 | }
37 |
38 | kMainEntry();
39 | }
40 |
41 | void waitDisk(void) { // waiting for disk
42 | while((inByte(0x1F7) & 0xC0) != 0x40);
43 | }
44 |
45 | void readSect(void *dst, int offset) { // reading a sector of disk
46 | int i;
47 | waitDisk();
48 | outByte(0x1F2, 1);
49 | outByte(0x1F3, offset);
50 | outByte(0x1F4, offset >> 8);
51 | outByte(0x1F5, offset >> 16);
52 | outByte(0x1F6, (offset >> 24) | 0xE0);
53 | outByte(0x1F7, 0x20);
54 |
55 | waitDisk();
56 | for (i = 0; i < SECTSIZE / 4; i ++) {
57 | ((int *)dst)[i] = inLong(0x1F0);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/lab5/code/bootloader/boot.c:
--------------------------------------------------------------------------------
1 | #include "boot.h"
2 |
3 | #define SECTSIZE 512
4 |
5 | /*
6 | void bootMain(void) {
7 | int i = 0;
8 | void (*elf)(void);
9 | elf = (void(*)(void))0x100000; // kernel is loaded to location 0x100000
10 | for (i = 0; i < 200; i ++) {
11 | //readSect((void*)elf + i*512, i+1);
12 | readSect((void*)elf + i*512, i+9);
13 | }
14 | elf(); // jumping to the loaded program
15 | }
16 | */
17 |
18 | void bootMain(void) {
19 | int i = 0;
20 | int phoff = 0x34;
21 | int offset = 0x1000;
22 | unsigned int elf = 0x100000;
23 | void (*kMainEntry)(void);
24 | kMainEntry = (void(*)(void))0x100000;
25 |
26 | for (i = 0; i < 200; i++) {
27 | readSect((void*)(elf + i*512), 1+i);
28 | }
29 |
30 | kMainEntry = (void(*)(void))((struct ELFHeader *)elf)->entry;
31 | phoff = ((struct ELFHeader *)elf)->phoff;
32 | offset = ((struct ProgramHeader *)(elf + phoff))->off;
33 |
34 | for (i = 0; i < 200 * 512; i++) {
35 | *(unsigned char *)(elf + i) = *(unsigned char *)(elf + i + offset);
36 | }
37 |
38 | kMainEntry();
39 | }
40 |
41 | void waitDisk(void) { // waiting for disk
42 | while((inByte(0x1F7) & 0xC0) != 0x40);
43 | }
44 |
45 | void readSect(void *dst, int offset) { // reading a sector of disk
46 | int i;
47 | waitDisk();
48 | outByte(0x1F2, 1);
49 | outByte(0x1F3, offset);
50 | outByte(0x1F4, offset >> 8);
51 | outByte(0x1F5, offset >> 16);
52 | outByte(0x1F6, (offset >> 24) | 0xE0);
53 | outByte(0x1F7, 0x20);
54 |
55 | waitDisk();
56 | for (i = 0; i < SECTSIZE / 4; i ++) {
57 | ((unsigned int *)dst)[i] = inLong(0x1F0);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/vga.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | int displayRow = 0; // futher extend, display position, in .bss section, not in .data section
5 | int displayCol = 0;
6 |
7 | static uint16_t displayMem[MAX_ROW*MAX_COL];
8 | static int displayClear = 0;
9 |
10 | void initVga() {
11 | displayRow = 0;
12 | displayCol = 0;
13 | displayClear = 0;
14 | clearScreen();
15 | updateCursor(0, 0);
16 | }
17 |
18 | void clearScreen() {
19 | int i = 0;
20 | int pos = 0;
21 | uint16_t data = 0 | (0x0c << 8);
22 | for (i = 0; i < MAX_ROW * MAX_COL; i++) {
23 | pos = i * 2;
24 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
25 | }
26 | }
27 |
28 | void updateCursor(int row, int col){
29 | int cursorPos = row * MAX_COL + col;
30 | outByte(0x3d4, 0x0f);
31 | outByte(0x3d5, (unsigned char)(cursorPos & 0xff));
32 |
33 | outByte(0x3d4, 0x0e);
34 | outByte(0x3d5, (unsigned char)((cursorPos>>8) & 0xff));
35 | }
36 |
37 | void scrollScreen() {
38 | int i = 0;
39 | int pos = 0;
40 | uint16_t data = 0;
41 | for (i = 0; i < MAX_ROW * MAX_COL; i++) {
42 | pos = i * 2;
43 | asm volatile("movw (%1), %0":"=r"(data):"r"(pos+0xb8000));
44 | displayMem[i] = data;
45 | }
46 | for (i = 0; i < (MAX_ROW-1) * MAX_COL; i++) {
47 | pos = i * 2;
48 | data = displayMem[i+MAX_COL];
49 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
50 | }
51 | data = 0 | (0x0c << 8);
52 | for (i = (MAX_ROW-1) * MAX_COL; i < MAX_ROW * MAX_COL; i++) {
53 | pos = i * 2;
54 | asm volatile("movw %0, (%1)"::"r"(data),"r"(pos+0xb8000));
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/doIrq.S:
--------------------------------------------------------------------------------
1 | .code32
2 |
3 | .global irqEmpty
4 | irqEmpty:
5 | pushl $0 // push dummy error code
6 | pushl $-1 // push interruption number into kernel
7 | jmp asmDoIrq
8 |
9 | .global irqErrorCode
10 | irqErrorCode:
11 | pushl $-1 // push interruption number into kernel
12 | jmp asmDoIrq
13 |
14 | .global irqDoubleFault
15 | irqDoubleFault:
16 | pushl $-1
17 | jmp asmDoIrq
18 |
19 | .global irqInvalidTSS
20 | irqInvalidTSS:
21 | pushl $-1
22 | jmp asmDoIrq
23 |
24 | .global irqSegNotPresent
25 | irqSegNotPresent:
26 | pushl $-1
27 | jmp asmDoIrq
28 |
29 | .global irqStackSegFault
30 | irqStackSegFault:
31 | pushl $-1
32 | jmp asmDoIrq
33 |
34 | .global irqGProtectFault
35 | irqGProtectFault:
36 | pushl $0xd
37 | jmp asmDoIrq
38 |
39 | .global irqPageFault
40 | irqPageFault:
41 | pushl $-1
42 | jmp asmDoIrq
43 |
44 | .global irqAlignCheck
45 | irqAlignCheck:
46 | pushl $-1
47 | jmp asmDoIrq
48 |
49 | .global irqSecException
50 | irqSecException:
51 | pushl $-1
52 | jmp asmDoIrq
53 |
54 | .global irqTimer
55 | irqTimer:
56 | pushl $0
57 | pushl $0x20
58 | jmp asmDoIrq
59 |
60 | .global irqKeyboard
61 | irqKeyboard:
62 | pushl $0
63 | pushl $0x21
64 | jmp asmDoIrq
65 |
66 | .global irqSyscall
67 | irqSyscall:
68 | pushl $0 // push dummy error code
69 | pushl $0x80 // push interruption number into kernel stack
70 | jmp asmDoIrq
71 |
72 | .global asmDoIrq
73 | asmDoIrq:
74 | pushal // push process state into kernel stack
75 | pushl %ds
76 | pushl %es
77 | pushl %fs
78 | pushl %gs
79 | pushl %esp //esp is treated as a parameter
80 | call irqHandle
81 | addl $4, %esp //esp is on top of kernel stack
82 | popl %gs
83 | popl %fs
84 | popl %es
85 | popl %ds
86 | popal
87 | addl $4, %esp //interrupt number is on top of kernel stack
88 | addl $4, %esp //error code is on top of kernel stack
89 | iret
90 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/doIrq.S:
--------------------------------------------------------------------------------
1 | /*
2 | otherwise need to reassign esp0 of tss in task switching for each user process
3 | note that for conforming code OR same PRL, no need to switch task
4 | that is TSS doesn't work
5 | 1. Trap Gate, IF won't be set
6 | 2. Interrupt Gate, IF is set automatically
7 | 3. System Gate
8 | */
9 |
10 | .code32
11 |
12 | .global irqEmpty
13 | irqEmpty:
14 | pushl $0 // push dummy error code
15 | pushl $-1 // push interruption number into kernel
16 | jmp asmDoIrq
17 |
18 | .global irqErrorCode
19 | irqErrorCode:
20 | pushl $-1 // push interruption number into kernel
21 | jmp asmDoIrq
22 |
23 | .global irqDoubleFault
24 | irqDoubleFault:
25 | pushl $-1
26 | jmp asmDoIrq
27 |
28 | .global irqInvalidTSS
29 | irqInvalidTSS:
30 | pushl $-1
31 | jmp asmDoIrq
32 |
33 | .global irqSegNotPresent
34 | irqSegNotPresent:
35 | pushl $-1
36 | jmp asmDoIrq
37 |
38 | .global irqStackSegFault
39 | irqStackSegFault:
40 | pushl $-1
41 | jmp asmDoIrq
42 |
43 | .global irqGProtectFault
44 | irqGProtectFault:
45 | pushl $0xd
46 | jmp asmDoIrq
47 |
48 | .global irqPageFault
49 | irqPageFault:
50 | pushl $-1
51 | jmp asmDoIrq
52 |
53 | .global irqAlignCheck
54 | irqAlignCheck:
55 | pushl $-1
56 | jmp asmDoIrq
57 |
58 | .global irqSecException
59 | irqSecException:
60 | pushl $-1
61 | jmp asmDoIrq
62 |
63 | .global irqKeyboard
64 | irqKeyboard:
65 | pushl $0
66 | # TODO: 将irqKeyboard的中断向量号压入栈
67 | pushl $0x21
68 | jmp asmDoIrq
69 |
70 | .global irqSyscall
71 | irqSyscall:
72 | pushl $0 // push dummy error code
73 | pushl $0x80 // push interruption number into kernel stack
74 | jmp asmDoIrq
75 |
76 | //.extern irqHandle //defined in irq_handle.c
77 |
78 | .global asmDoIrq
79 | asmDoIrq:
80 | pushal // push process state into kernel stack
81 | pushl %esp //esp is treated as a parameter
82 | call irqHandle
83 | addl $4, %esp //esp is on top of kernel stack
84 | popal
85 | addl $4, %esp //interrupt number is on top of kernel stack
86 | addl $4, %esp //error code is on top of kernel stack
87 | iret
88 |
--------------------------------------------------------------------------------
/lab2/lab2/app/main.c:
--------------------------------------------------------------------------------
1 | #include "lib.h"
2 | #include "types.h"
3 |
4 | int uEntry(void) {
5 | uint16_t selector;
6 | //uint16_t selector = 16;
7 | asm volatile("movw %%ss, %0":"=m"(selector)); //XXX necessary or not, iret may reset ds in QEMU
8 | asm volatile("movw %%ax, %%ds"::"a"(selector));
9 |
10 | printf("I/O test begin...\n");
11 | printf("the answer should be:\n");
12 | printf("#######################################################\n");
13 | printf("Hello, welcome to OSlab! I'm the body of the game.\n");
14 | printf("Now I will test your printf:\n");
15 | printf("1 + 1 = 2, 123 * 456 = 56088, 0, -1, -2147483648, -1412505855, -32768, 102030, 0, ffffffff, 80000000, abcdef01, ffff8000, 18e8e\n");
16 | printf("Now I will test your getChar: ");
17 | printf("1 + 1 = 2\n2 * 123 = 246\n");
18 | printf("Now I will test your getStr: ");
19 | printf("Alice is stronger than Bob\nBob is weaker than Alice\n");
20 | printf("#######################################################\n");
21 | printf("your answer:\n");
22 | printf("=======================================================\n");
23 | printf("%s %s%scome %co%s", "Hello,", "", "wel", 't', " ");
24 | printf("%c%c%c%c%c! ", 'O', 'S', 'l', 'a', 'b');
25 | printf("I'm the %s of %s.\n", "body", "the game");
26 | printf("Now I will test your printf:\n");
27 | printf("%d + %d = %d, %d * %d = %d, ", 1, 1, 1 + 1, 123, 456, 123 * 456);
28 | printf("%d, %d, %d, %d, %d, %d, ", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
29 | printf("%x, %x, %x, %x, %x, %x\n", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
30 | printf("Now I will test your getChar: ");
31 | printf("1 + 1 = ");
32 | char num = getChar();
33 | printf("%c * 123 = 246\n",num);
34 | printf("Now I will test your getStr: ");
35 | printf("Alice is stronger than ");
36 | char name[20];
37 | getStr(name,20);
38 | printf("%s is stronger than Alice\n",name);
39 | printf("=======================================================\n");
40 | printf("Test end!!! Good luck!!!\n");
41 |
42 | while(1);
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/doIrq.S:
--------------------------------------------------------------------------------
1 | /*TODO
2 | otherwise need to reassign esp0 of tss in task switching for each user process
3 | note that for conforming code OR same PRL, no need to switch task
4 | that is TSS doesn't work
5 | 1. Trap Gate, IF won't be set
6 | 2. Interrupt Gate, IF is set automatically
7 | 3. System Gate
8 | */
9 |
10 | .code32
11 |
12 | .global irqEmpty
13 | irqEmpty:
14 | pushl $0 // push dummy error code
15 | pushl $-1 // push interruption number into kernel
16 | jmp asmDoIrq
17 |
18 | .global irqErrorCode
19 | irqErrorCode:
20 | pushl $-1 // push interruption number into kernel
21 | jmp asmDoIrq
22 |
23 | .global irqDoubleFault
24 | irqDoubleFault:
25 | pushl $-1
26 | jmp asmDoIrq
27 |
28 | .global irqInvalidTSS
29 | irqInvalidTSS:
30 | pushl $-1
31 | jmp asmDoIrq
32 |
33 | .global irqSegNotPresent
34 | irqSegNotPresent:
35 | pushl $-1
36 | jmp asmDoIrq
37 |
38 | .global irqStackSegFault
39 | irqStackSegFault:
40 | pushl $-1
41 | jmp asmDoIrq
42 |
43 | .global irqGProtectFault
44 | irqGProtectFault:
45 | pushl $0xd
46 | jmp asmDoIrq
47 |
48 | .global irqPageFault
49 | irqPageFault:
50 | pushl $-1
51 | jmp asmDoIrq
52 |
53 | .global irqAlignCheck
54 | irqAlignCheck:
55 | pushl $-1
56 | jmp asmDoIrq
57 |
58 | .global irqSecException
59 | irqSecException:
60 | pushl $-1
61 | jmp asmDoIrq
62 |
63 | .global irqTimer
64 | irqTimer:
65 | pushl $0
66 | pushl $0x20
67 | jmp asmDoIrq
68 |
69 | .global irqSyscall
70 | irqSyscall:
71 | pushl $0 // push dummy error code
72 | pushl $0x80 // push interruption number into kernel stack
73 | jmp asmDoIrq
74 |
75 | .global asmDoIrq
76 | asmDoIrq:
77 | pushal // push process state into kernel stack
78 | pushl %ds
79 | pushl %es
80 | pushl %fs
81 | pushl %gs
82 | pushl %esp //esp is treated as a parameter
83 | call irqHandle
84 | addl $4, %esp //esp is on top of kernel stack
85 | popl %gs
86 | popl %fs
87 | popl %es
88 | popl %ds
89 | popal
90 | addl $4, %esp //interrupt number is on top of kernel stack
91 | addl $4, %esp //error code is on top of kernel stack
92 | iret
93 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/disk.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | void waitDisk(void) {
5 | while((inByte(0x1F7) & 0xC0) != 0x40);
6 | }
7 |
8 | void readSect(void *dst, int offset) {
9 | int i;
10 | waitDisk();
11 |
12 | outByte(0x1F2, 1);
13 | outByte(0x1F3, offset);
14 | outByte(0x1F4, offset >> 8);
15 | outByte(0x1F5, offset >> 16);
16 | outByte(0x1F6, (offset >> 24) | 0xE0);
17 | outByte(0x1F7, 0x20);
18 |
19 | waitDisk();
20 | for (i = 0; i < SECTOR_SIZE / 4; i ++) {
21 | ((uint32_t *)dst)[i] = inLong(0x1F0);
22 | }
23 | }
24 |
25 | void writeSect(void *src, int offset) {
26 | int i;
27 | waitDisk();
28 |
29 | outByte(0x1F2, 1);
30 | outByte(0x1F3, offset);
31 | outByte(0x1F4, offset >> 8);
32 | outByte(0x1F5, offset >> 16);
33 | outByte(0x1F6, (offset >> 24) | 0xE0);
34 | outByte(0x1F7, 0x30);
35 |
36 | waitDisk();
37 | for (i = 0; i < SECTOR_SIZE / 4; i ++) {
38 | outLong(0x1F0, ((uint32_t *)src)[i]);
39 | }
40 | }
41 |
42 | void diskRead (void *destBuffer, int size, int num, int offset) {
43 | int i = 0;
44 | int j = 0;
45 | uint8_t buffer[SECTOR_SIZE];
46 | int quotient = offset / SECTOR_SIZE;
47 | int remainder = offset % SECTOR_SIZE;
48 |
49 | readSect((void*)buffer, 201 + quotient + j);
50 | j ++;
51 | while (i < size * num) {
52 | ((uint8_t*)destBuffer)[i] = buffer[(remainder + i) % SECTOR_SIZE];
53 | i ++;
54 | if ((remainder + i) % SECTOR_SIZE == 0) {
55 | readSect((void*)buffer, 201 + quotient + j);
56 | j ++;
57 | }
58 | }
59 | }
60 |
61 | void diskWrite (void *destBuffer, int size, int num, int offset) {
62 | int i = 0;
63 | int j = 0;
64 | uint8_t buffer[SECTOR_SIZE];
65 | int quotient = offset / SECTOR_SIZE;
66 | int remainder = offset % SECTOR_SIZE;
67 |
68 | readSect((void*)buffer, 201 + quotient + j);
69 | while (i < size * num) {
70 | buffer[(remainder + i) % SECTOR_SIZE] = ((uint8_t*)destBuffer)[i];
71 | i ++;
72 | if ((remainder + i) % SECTOR_SIZE == 0) {
73 | writeSect((void*)buffer, 201 + quotient + j);
74 | j ++;
75 | readSect((void*)buffer, 201 + quotient + j);
76 | }
77 | }
78 | writeSect((void*)buffer, 201 + quotient + j);
79 | }
80 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/doIrq.S:
--------------------------------------------------------------------------------
1 | /*
2 | otherwise need to reassign esp0 of tss in task switching for each user process
3 | note that for conforming code OR same PRL, no need to switch task
4 | that is TSS doesn't work
5 | 1. Trap Gate, IF won't be set
6 | 2. Interrupt Gate, IF is set automatically
7 | 3. System Gate
8 | */
9 |
10 | .code32
11 |
12 | .global irqEmpty
13 | irqEmpty:
14 | pushl $0 // push dummy error code
15 | pushl $-1 // push interruption number into kernel
16 | jmp asmDoIrq
17 |
18 | .global irqErrorCode
19 | irqErrorCode:
20 | pushl $-1 // push interruption number into kernel
21 | jmp asmDoIrq
22 |
23 | .global irqDoubleFault
24 | irqDoubleFault:
25 | pushl $-1
26 | jmp asmDoIrq
27 |
28 | .global irqInvalidTSS
29 | irqInvalidTSS:
30 | pushl $-1
31 | jmp asmDoIrq
32 |
33 | .global irqSegNotPresent
34 | irqSegNotPresent:
35 | pushl $-1
36 | jmp asmDoIrq
37 |
38 | .global irqStackSegFault
39 | irqStackSegFault:
40 | pushl $-1
41 | jmp asmDoIrq
42 |
43 | .global irqGProtectFault
44 | irqGProtectFault:
45 | pushl $0xd
46 | jmp asmDoIrq
47 |
48 | .global irqPageFault
49 | irqPageFault:
50 | pushl $-1
51 | jmp asmDoIrq
52 |
53 | .global irqAlignCheck
54 | irqAlignCheck:
55 | pushl $-1
56 | jmp asmDoIrq
57 |
58 | .global irqSecException
59 | irqSecException:
60 | pushl $-1
61 | jmp asmDoIrq
62 |
63 | .global irqTimer
64 | irqTimer:
65 | pushl $0
66 | pushl $0x20
67 | jmp asmDoIrq
68 |
69 | .global irqKeyboard
70 | irqKeyboard:
71 | pushl $0
72 | pushl $0x21
73 | jmp asmDoIrq
74 |
75 | .global irqSyscall
76 | irqSyscall:
77 | pushl $0 // push dummy error code
78 | pushl $0x80 // push interruption number into kernel stack
79 | jmp asmDoIrq
80 |
81 | .global asmDoIrq
82 | asmDoIrq:
83 | pushal // push process state into kernel stack
84 | pushl %ds
85 | pushl %es
86 | pushl %fs
87 | pushl %gs
88 | pushl %esp //esp is treated as a parameter
89 | call irqHandle
90 | addl $4, %esp //esp is on top of kernel stack
91 | popl %gs
92 | popl %fs
93 | popl %es
94 | popl %ds
95 | popal
96 | addl $4, %esp //interrupt number is on top of kernel stack
97 | addl $4, %esp //error code is on top of kernel stack
98 | iret
99 |
--------------------------------------------------------------------------------
/lab5/code/app/main.c:
--------------------------------------------------------------------------------
1 | #include "types.h"
2 | #include "utils.h"
3 | #include "lib.h"
4 |
5 | union DirEntry {
6 | uint8_t byte[128];
7 | struct {
8 | uint32_t inode;
9 | char name[64];
10 | };
11 | };
12 |
13 | typedef union DirEntry DirEntry;
14 |
15 | int ls(char *destFilePath) {
16 | printf("ls %s\n", destFilePath);
17 | int i = 0;
18 | int fd = 0;
19 | int ret = 0;
20 | DirEntry *dirEntry = 0;
21 | uint8_t buffer[512 * 2];
22 | fd = open(destFilePath, O_READ | O_DIRECTORY);
23 | //printf("%d\n",fd);
24 | if (fd == -1)
25 | return -1;
26 | ret = read(fd, buffer, 512 * 2);
27 | //printf("%d\n",ret);
28 | while (ret != 0) {
29 | // TODO: Complete 'ls'.
30 | dirEntry = (DirEntry *)buffer;
31 | for (i = 0; i < 8; ++i)
32 | {
33 | //printf("%d\n",i);
34 | if (dirEntry[i].inode != 0)
35 | printf("%s ", dirEntry[i].name);
36 | }
37 | ret = read(fd, buffer, 512 * 2);
38 | }
39 | printf("\n");
40 | close(fd);
41 | return 0;
42 | }
43 |
44 | int cat(char *destFilePath) {
45 | printf("cat %s\n", destFilePath);
46 | int fd = 0;
47 | int ret = 0;
48 | uint8_t buffer[512 * 2];
49 | fd = open(destFilePath, O_READ);
50 | if (fd == -1)
51 | return -1;
52 | ret = read(fd, buffer, 512 * 2);
53 | while (ret != 0) {
54 | // TODO: COmplete 'cat'
55 | write(STD_OUT, buffer, ret);
56 | ret = read(fd, buffer, 512 * 2);
57 | }
58 | close(fd);
59 | return 0;
60 | }
61 |
62 | int uEntry(void) {
63 | int fd = 0;
64 | int i = 0;
65 | char tmp = 0;
66 |
67 | ls("/");
68 | ls("/boot/");
69 | ls("/dev/");
70 | ls("/usr/");
71 |
72 | printf("create /usr/test and write alphabets to it\n");
73 | fd = open("/usr/test", O_WRITE | O_READ | O_CREATE);
74 | for (i = 0; i < 26; i ++) {
75 | tmp = (char)(i % 26 + 'A');
76 | write(fd, (uint8_t*)&tmp, 1);
77 | }
78 | close(fd);
79 | ls("/usr/");
80 | cat("/usr/test");
81 | printf("\n");
82 | printf("rm /usr/test\n");
83 | remove("/usr/test");
84 | ls("/usr/");
85 | printf("rmdir /usr/\n");
86 | remove("/usr/");
87 | ls("/");
88 | printf("create /usr/\n");
89 | fd = open("/usr/", O_CREATE | O_DIRECTORY);
90 | close(fd);
91 | ls("/");
92 |
93 | exit();
94 | return 0;
95 | }
96 |
--------------------------------------------------------------------------------
/lab5/code/lib/utils.c:
--------------------------------------------------------------------------------
1 | #include "types.h"
2 |
3 | /*
4 | * find the first token in string
5 | * set *size as the number of bytes before token
6 | * if not found, set *size as the length of *string
7 | */
8 | int stringChr (const char *string, char token, int *size) {
9 | int i = 0;
10 | if (string == NULL) {
11 | *size = 0;
12 | return -1;
13 | }
14 | while (string[i] != 0) {
15 | if (token == string[i]) {
16 | *size = i;
17 | return 0;
18 | }
19 | else
20 | i ++;
21 | }
22 | *size = i;
23 | return -1;
24 | }
25 |
26 | /*
27 | * find the last token in string
28 | * set *size as the number of bytes before token
29 | * if not found, set *size as the length of *string
30 | */
31 | int stringChrR (const char *string, char token, int *size) {
32 | int i = 0;
33 | if (string == NULL) {
34 | *size = 0;
35 | return -1;
36 | }
37 | while (string[i] != 0)
38 | i ++;
39 | *size = i;
40 | while (i > -1) {
41 | if (token == string[i]) {
42 | *size = i;
43 | return 0;
44 | }
45 | else
46 | i --;
47 | }
48 | return -1;
49 | }
50 |
51 | int stringLen (const char *string) {
52 | int i = 0;
53 | if (string == NULL)
54 | return 0;
55 | while (string[i] != 0)
56 | i ++;
57 | return i;
58 | }
59 |
60 | int stringCmp (const char *srcString, const char *destString, int size) { // compre first 'size' bytes
61 | int i = 0;
62 | if (srcString == NULL || destString == NULL)
63 | return -1;
64 | while (i != size) {
65 | if (srcString[i] != destString[i])
66 | return -1;
67 | else if (srcString[i] == 0)
68 | return 0;
69 | else
70 | i ++;
71 | }
72 | return 0;
73 | }
74 |
75 | int stringCpy (const char *srcString, char *destString, int size) {
76 | int i = 0;
77 | if (srcString == NULL || destString == NULL)
78 | return -1;
79 | while (i != size) {
80 | if (srcString[i] != 0) {
81 | destString[i] = srcString[i];
82 | i++;
83 | }
84 | else
85 | break;
86 | }
87 | destString[i] = 0;
88 | return 0;
89 | }
90 |
91 | int setBuffer (uint8_t *buffer, int size, uint8_t value) {
92 | int i = 0;
93 | if (buffer == NULL)
94 | return -1;
95 | for (i = 0; i < size ; i ++)
96 | buffer[i] = value;
97 | return 0;
98 | }
99 |
--------------------------------------------------------------------------------
/lab5/code/kernel/lib/utils.c:
--------------------------------------------------------------------------------
1 | #include "common.h"
2 |
3 | /*
4 | * find the first token in string
5 | * set *size as the number of bytes before token
6 | * if not found, set *size as the length of *string
7 | */
8 | int stringChr (const char *string, char token, int *size) {
9 | int i = 0;
10 | if (string == NULL) {
11 | *size = 0;
12 | return -1;
13 | }
14 | while (string[i] != 0) {
15 | if (token == string[i]) {
16 | *size = i;
17 | return 0;
18 | }
19 | else
20 | i ++;
21 | }
22 | *size = i;
23 | return -1;
24 | }
25 |
26 | /*
27 | * find the last token in string
28 | * set *size as the number of bytes before token
29 | * if not found, set *size as the length of *string
30 | */
31 | int stringChrR (const char *string, char token, int *size) {
32 | int i = 0;
33 | if (string == NULL) {
34 | *size = 0;
35 | return -1;
36 | }
37 | while (string[i] != 0)
38 | i ++;
39 | *size = i;
40 | while (i > -1) {
41 | if (token == string[i]) {
42 | *size = i;
43 | return 0;
44 | }
45 | else
46 | i --;
47 | }
48 | return -1;
49 | }
50 |
51 | int stringLen (const char *string) {
52 | int i = 0;
53 | if (string == NULL)
54 | return 0;
55 | while (string[i] != 0)
56 | i ++;
57 | return i;
58 | }
59 |
60 | int stringCmp (const char *srcString, const char *destString, int size) { // compre first 'size' bytes
61 | int i = 0;
62 | if (srcString == NULL || destString == NULL)
63 | return -1;
64 | while (i != size) {
65 | if (srcString[i] != destString[i])
66 | return -1;
67 | else if (srcString[i] == 0)
68 | return 0;
69 | else
70 | i ++;
71 | }
72 | return 0;
73 | }
74 |
75 | int stringCpy (const char *srcString, char *destString, int size) {
76 | int i = 0;
77 | if (srcString == NULL || destString == NULL)
78 | return -1;
79 | while (i != size) {
80 | if (srcString[i] != 0) {
81 | destString[i] = srcString[i];
82 | i++;
83 | }
84 | else
85 | break;
86 | }
87 | destString[i] = 0;
88 | return 0;
89 | }
90 |
91 | int setBuffer (uint8_t *buffer, int size, uint8_t value) {
92 | int i = 0;
93 | if (buffer == NULL)
94 | return -1;
95 | for (i = 0; i < size ; i ++)
96 | buffer[i] = value;
97 | return 0;
98 | }
99 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/utils.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "utils.h"
3 |
4 | /*
5 | * find the first token in string
6 | * set *size as the number of bytes before token
7 | * if not found, set *size as the length of *string
8 | */
9 | int stringChr (const char *string, char token, int *size) {
10 | int i = 0;
11 | if (string == NULL) {
12 | *size = 0;
13 | return -1;
14 | }
15 | while (string[i] != 0) {
16 | if (token == string[i]) {
17 | *size = i;
18 | return 0;
19 | }
20 | else
21 | i ++;
22 | }
23 | *size = i;
24 | return -1;
25 | }
26 |
27 | /*
28 | * find the last token in string
29 | * set *size as the number of bytes before token
30 | * if not found, set *size as the length of *string
31 | */
32 | int stringChrR (const char *string, char token, int *size) {
33 | int i = 0;
34 | if (string == NULL) {
35 | *size = 0;
36 | return -1;
37 | }
38 | while (string[i] != 0)
39 | i ++;
40 | *size = i;
41 | while (i > -1) {
42 | if (token == string[i]) {
43 | *size = i;
44 | return 0;
45 | }
46 | else
47 | i --;
48 | }
49 | return -1;
50 | }
51 |
52 | int stringLen (const char *string) {
53 | int i = 0;
54 | if (string == NULL)
55 | return 0;
56 | while (string[i] != 0)
57 | i ++;
58 | return i;
59 | }
60 |
61 | int stringCmp (const char *srcString, const char *destString, int size) { // compre first 'size' bytes
62 | int i = 0;
63 | if (srcString == NULL || destString == NULL)
64 | return -1;
65 | while (i != size) {
66 | if (srcString[i] != destString[i])
67 | return -1;
68 | else if (srcString[i] == 0)
69 | return 0;
70 | else
71 | i ++;
72 | }
73 | return 0;
74 | }
75 |
76 | int stringCpy (const char *srcString, char *destString, int size) {
77 | int i = 0;
78 | if (srcString == NULL || destString == NULL)
79 | return -1;
80 | while (i != size) {
81 | if (srcString[i] != 0) {
82 | destString[i] = srcString[i];
83 | i++;
84 | }
85 | else
86 | break;
87 | }
88 | destString[i] = 0;
89 | return 0;
90 | }
91 |
92 | int setBuffer (uint8_t *buffer, int size, uint8_t value) {
93 | int i = 0;
94 | if (buffer == NULL)
95 | return -1;
96 | for (i = 0; i < size ; i ++)
97 | buffer[i] = value;
98 | return 0;
99 | }
100 |
--------------------------------------------------------------------------------
/lab4/lab4/app/main.c:
--------------------------------------------------------------------------------
1 | #include "lib.h"
2 | #include "types.h"
3 |
4 | #define N 5
5 | sem_t forks[5];
6 |
7 | void philosopher(int i){
8 | int id = getpid();
9 | while(1){
10 | printf("Philosopher %d : think\n",id);
11 | sleep(128);
12 | if(i%2 == 0){
13 | sem_wait(&forks[i]);
14 | sleep(128);
15 | sem_wait(&forks[(i+1)%N]);
16 | sleep(128);
17 | }
18 | else{
19 | sem_wait(&forks[(i+1)%N]);
20 | sleep(128);
21 | sem_wait(&forks[i]);
22 | sleep(128);
23 | }
24 | printf("Philosopher %d : eat\n",id);
25 | sleep(128);
26 | sem_post(&forks[i]);
27 | sleep(128);
28 | sem_post(&forks[(i+1)%N]);
29 | sleep(128);
30 | }
31 | }
32 |
33 |
34 |
35 | int uEntry(void) {
36 | // For lab4.1
37 | // Test 'scanf'
38 | //int dec = 0;
39 | //int hex = 0;
40 | //char str[6];
41 | //char cha = 0;
42 | int ret = 0;
43 | /*while(1){
44 | printf("Input:\" Test %%c Test %%6s %%d %%x\"\n");
45 | ret = scanf(" Test %c Test %6s %d %x", &cha, str, &dec, &hex);
46 | printf("Ret: %d; %c, %s, %d, %x.\n", ret, cha, str, dec, hex);
47 | if (ret == 4)
48 | break;
49 | }*/
50 |
51 | // For lab4.2
52 | // Test 'Semaphore'
53 | int i = 4;
54 |
55 | sem_t sem;
56 | printf("Father Process: Semaphore Initializing.\n");
57 | ret = sem_init(&sem, 2);
58 | if (ret == -1) {
59 | printf("Father Process: Semaphore Initializing Failed.\n");
60 | exit();
61 | }
62 |
63 | ret = fork();
64 | if (ret == 0) {
65 | while( i != 0) {
66 | i --;
67 | printf("Child Process: Semaphore Waiting.\n");
68 | sem_wait(&sem);
69 | printf("Child Process: In Critical Area.\n");
70 | }
71 | printf("Child Process: Semaphore Destroying.\n");
72 | sem_destroy(&sem);
73 | exit();
74 | }
75 | else if (ret != -1) {
76 | while( i != 0) {
77 | i --;
78 | printf("Father Process: Sleeping.\n");
79 | sleep(128);
80 | printf("Father Process: Semaphore Posting.\n");
81 | sem_post(&sem);
82 | }
83 | printf("Father Process: Semaphore Destroying.\n");
84 | sem_destroy(&sem);
85 | //exit();
86 | }
87 |
88 | // For lab4.3
89 | // TODO: You need to design and test the philosopher problem.
90 | // Note that you can create your own functions.
91 | // Requirements are demonstrated in the guide.
92 |
93 | printf("philosopher\n");
94 |
95 | for (int i = 0; i < 5; i++){
96 | sem_init(&forks[i], 1);
97 | }
98 |
99 | for (int i = 0; i < 5; i++){
100 | if(fork() == 0){
101 | philosopher(i);
102 | exit();
103 | }
104 | }
105 | exit();
106 | for (int i = 0; i < 5; i++){
107 | sem_destroy(&forks[i]);
108 | }
109 |
110 | return 0;
111 | }
112 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/idt.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define INTERRUPT_GATE_32 0xE
5 | #define TRAP_GATE_32 0xF
6 |
7 | /* IDT表的内容 */
8 | struct GateDescriptor idt[NR_IRQ]; // NR_IRQ=256, defined in x86/cpu.h
9 |
10 | /* 初始化一个中断门(interrupt gate) */
11 | static void setIntr(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
12 | // TODO: 初始化interrupt gate
13 | ptr->offset_15_0 = offset & 0xFFFF;
14 | ptr->segment = selector << 3;
15 | ptr->pad0 = 0;
16 | ptr->type = INTERRUPT_GATE_32;
17 | ptr->system = FALSE;
18 | ptr->privilege_level = dpl;
19 | ptr->present = TRUE;
20 | ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
21 | }
22 |
23 | /* 初始化一个陷阱门(trap gate) */
24 | static void setTrap(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
25 | // TODO: 初始化trap gate
26 | ptr->offset_15_0 = offset & 0xFFFF;
27 | ptr->segment = selector << 3;
28 | ptr->pad0 = 0;
29 | ptr->type = TRAP_GATE_32;
30 | ptr->system = FALSE;
31 | ptr->privilege_level = dpl;
32 | ptr->present = TRUE;
33 | ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
34 | }
35 |
36 | /* 声明函数,这些函数在汇编代码里定义 */
37 | void irqEmpty();
38 | void irqErrorCode();
39 |
40 | void irqDoubleFault(); // 0x8
41 | void irqInvalidTSS(); // 0xa
42 | void irqSegNotPresent(); // 0xb
43 | void irqStackSegFault(); // 0xc
44 | void irqGProtectFault(); // 0xd
45 | void irqPageFault(); // 0xe
46 | void irqAlignCheck(); // 0x11
47 | void irqSecException(); // 0x1e
48 | void irqKeyboard();
49 |
50 | void irqSyscall();
51 |
52 | void initIdt() {
53 | int i;
54 | /* 为了防止系统异常终止,所有irq都有处理函数(irqEmpty)。 */
55 | for (i = 0; i < NR_IRQ; i ++) {
56 | setTrap(idt + i, SEG_KCODE, (uint32_t)irqEmpty, DPL_KERN);
57 | }
58 | /*
59 | * init your idt here
60 | * 初始化 IDT 表, 为中断设置中断处理函数
61 | */
62 | /* Exceptions with error code */
63 | setTrap(idt + 0x8, SEG_KCODE, (uint32_t)irqDoubleFault, DPL_KERN);
64 | // TODO: 填好剩下的表项
65 | setTrap(idt + 0xa, SEG_KCODE, (uint32_t)irqInvalidTSS, DPL_KERN);
66 | setTrap(idt + 0xb, SEG_KCODE, (uint32_t)irqSegNotPresent, DPL_KERN);
67 | setTrap(idt + 0xc, SEG_KCODE, (uint32_t)irqStackSegFault, DPL_KERN);
68 | setTrap(idt + 0xd, SEG_KCODE, (uint32_t)irqGProtectFault, DPL_KERN);
69 | //setTrap(idt + 0xd, SEG_KCODE, (uint32_t)irqGProtectFault, DPL_USER);
70 | setTrap(idt + 0xe, SEG_KCODE, (uint32_t)irqPageFault, DPL_KERN);
71 | //setTrap(idt + 0xe, SEG_KCODE, (uint32_t)irqPageFault, DPL_USER);
72 | setTrap(idt + 0x11, SEG_KCODE, (uint32_t)irqAlignCheck, DPL_KERN);
73 | setTrap(idt + 0x1e, SEG_KCODE, (uint32_t)irqSecException, DPL_KERN);
74 |
75 | /* Exceptions with DPL = 3 */
76 | // TODO: 填好剩下的表项
77 | setTrap(idt + 0x21, SEG_KCODE, (uint32_t)irqKeyboard, DPL_KERN);
78 | setIntr(idt + 0x80, SEG_KCODE, (uint32_t)irqSyscall, DPL_USER); // for int 0x80, interrupt vector is 0x80, Interruption is disabled
79 | /* 写入IDT */
80 | saveIdt(idt, sizeof(idt));
81 | }
82 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/idt.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define INTERRUPT_GATE_32 0xE
5 | #define TRAP_GATE_32 0xF
6 |
7 | /* IDT表的内容 */
8 | struct GateDescriptor idt[NR_IRQ]; // NR_IRQ=256, defined in x86/cpu.h
9 |
10 | /* 初始化一个中断门(interrupt gate) */
11 | static void setIntr(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
12 | ptr->offset_15_0 = offset & 0xFFFF;
13 | ptr->segment = selector << 3;
14 | ptr->pad0 = 0;
15 | ptr->type = INTERRUPT_GATE_32;
16 | ptr->system = FALSE;
17 | ptr->privilege_level = dpl;
18 | ptr->present = TRUE;
19 | ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
20 | }
21 |
22 | /* 初始化一个陷阱门(trap gate) */
23 | //static void setTrap(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
24 | // ptr->offset_15_0 = offset & 0xFFFF;
25 | // ptr->segment = selector << 3;
26 | // ptr->pad0 = 0;
27 | // ptr->type = TRAP_GATE_32;
28 | // ptr->system = FALSE;
29 | // ptr->privilege_level = dpl;
30 | // ptr->present = TRUE;
31 | // ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
32 | //}
33 |
34 | /* 声明函数,这些函数在汇编代码里定义 */
35 | void irqEmpty();
36 | void irqErrorCode();
37 |
38 | void irqDoubleFault(); // 0x8
39 | void irqInvalidTSS(); // 0xa
40 | void irqSegNotPresent(); // 0xb
41 | void irqStackSegFault(); // 0xc
42 | void irqGProtectFault(); // 0xd
43 | void irqPageFault(); // 0xe
44 | void irqAlignCheck(); // 0x11
45 | void irqSecException(); // 0x1e
46 |
47 | void irqTimer();
48 |
49 | void irqSyscall();
50 |
51 | void initIdt() {
52 | int i;
53 | /* 为了防止系统异常终止,所有irq都有处理函数(irqEmpty)。 */
54 | for (i = 0; i < NR_IRQ; i ++) {
55 | setIntr(idt + i, SEG_KCODE, (uint32_t)irqEmpty, DPL_KERN);
56 | }
57 | /*
58 | * init your idt here
59 | * 初始化 IDT 表, 为中断设置中断处理函数
60 | */
61 | /* Exceptions with error code */
62 | setIntr(idt + 0x8, SEG_KCODE, (uint32_t)irqDoubleFault, DPL_KERN);
63 | setIntr(idt + 0xa, SEG_KCODE, (uint32_t)irqInvalidTSS, DPL_KERN);
64 | setIntr(idt + 0xb, SEG_KCODE, (uint32_t)irqSegNotPresent, DPL_KERN);
65 | setIntr(idt + 0xc, SEG_KCODE, (uint32_t)irqStackSegFault, DPL_KERN);
66 | setIntr(idt + 0xd, SEG_KCODE, (uint32_t)irqGProtectFault, DPL_KERN);
67 | setIntr(idt + 0xe, SEG_KCODE, (uint32_t)irqPageFault, DPL_KERN);
68 | setIntr(idt + 0x11, SEG_KCODE, (uint32_t)irqAlignCheck, DPL_KERN);
69 | setIntr(idt + 0x1e, SEG_KCODE, (uint32_t)irqSecException, DPL_KERN);
70 |
71 | setIntr(idt + 0x20, SEG_KCODE, (uint32_t)irqTimer, DPL_KERN);
72 | /* Exceptions with DPL = 3 */
73 | //setIntr(idt + 0x3, SEG_KCODE, , DPL_USER); // for int 3, interrupt vector is 0x3, Interruption is disabled
74 | //setIntr(idt + 0x4, SEG_KCODE, , DPL_USER); // for into, interrupt vector is 0x4, Interruption is disabled
75 | //setIntr(idt + 0x5, SEG_KCODE, , DPL_USER); // for bound, interrupt vector is 0x5, Interruption is disabled
76 | setIntr(idt + 0x80, SEG_KCODE, (uint32_t)irqSyscall, DPL_USER); // for int 0x80, interrupt vector is 0x80, Interruption is disabled
77 |
78 | /* 写入IDT */
79 | saveIdt(idt, sizeof(idt));
80 | }
81 |
--------------------------------------------------------------------------------
/lab5/code/kernel/kernel/idt.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 |
3 | #define INTERRUPT_GATE_32 0xE
4 | #define TRAP_GATE_32 0xF
5 |
6 | /* IDT表的内容 */
7 | struct GateDescriptor idt[NR_IRQ]; // NR_IRQ=256, defined in x86/cpu.h
8 |
9 | /* 初始化一个中断门(interrupt gate) */
10 | static void setIntr(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
11 | ptr->offset_15_0 = offset & 0xFFFF;
12 | ptr->segment = selector << 3;
13 | ptr->pad0 = 0;
14 | ptr->type = INTERRUPT_GATE_32;
15 | ptr->system = FALSE;
16 | ptr->privilege_level = dpl;
17 | ptr->present = TRUE;
18 | ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
19 | }
20 |
21 | /* 初始化一个陷阱门(trap gate) */
22 | //static void setTrap(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
23 | // ptr->offset_15_0 = offset & 0xFFFF;
24 | // ptr->segment = selector << 3;
25 | // ptr->pad0 = 0;
26 | // ptr->type = TRAP_GATE_32;
27 | // ptr->system = FALSE;
28 | // ptr->privilege_level = dpl;
29 | // ptr->present = TRUE;
30 | // ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
31 | //}
32 |
33 | /* 声明函数,这些函数在汇编代码里定义 */
34 | void irqEmpty();
35 | void irqErrorCode();
36 |
37 | void irqDoubleFault(); // 0x8
38 | void irqInvalidTSS(); // 0xa
39 | void irqSegNotPresent(); // 0xb
40 | void irqStackSegFault(); // 0xc
41 | void irqGProtectFault(); // 0xd
42 | void irqPageFault(); // 0xe
43 | void irqAlignCheck(); // 0x11
44 | void irqSecException(); // 0x1e
45 | void irqTimer();
46 | void irqKeyboard();
47 | void irqSyscall();
48 |
49 | void initIdt() {
50 | int i;
51 | /* 为了防止系统异常终止,所有irq都有处理函数(irqEmpty)。 */
52 | for (i = 0; i < NR_IRQ; i ++) {
53 | setIntr(idt + i, SEG_KCODE, (uint32_t)irqEmpty, DPL_KERN);
54 | }
55 | /*
56 | * init your idt here
57 | * 初始化 IDT 表, 为中断设置中断处理函数
58 | */
59 | /* Exceptions with error code */
60 | setIntr(idt + 0x8, SEG_KCODE, (uint32_t)irqDoubleFault, DPL_KERN);
61 | setIntr(idt + 0xa, SEG_KCODE, (uint32_t)irqInvalidTSS, DPL_KERN);
62 | setIntr(idt + 0xb, SEG_KCODE, (uint32_t)irqSegNotPresent, DPL_KERN);
63 | setIntr(idt + 0xc, SEG_KCODE, (uint32_t)irqStackSegFault, DPL_KERN);
64 | setIntr(idt + 0xd, SEG_KCODE, (uint32_t)irqGProtectFault, DPL_KERN);
65 | setIntr(idt + 0xe, SEG_KCODE, (uint32_t)irqPageFault, DPL_KERN);
66 | setIntr(idt + 0x11, SEG_KCODE, (uint32_t)irqAlignCheck, DPL_KERN);
67 | setIntr(idt + 0x1e, SEG_KCODE, (uint32_t)irqSecException, DPL_KERN);
68 |
69 | setIntr(idt + 0x20, SEG_KCODE, (uint32_t)irqTimer, DPL_KERN);
70 | setIntr(idt + 0x21, SEG_KCODE, (uint32_t)irqKeyboard, DPL_KERN);
71 | /* Exceptions with DPL = 3 */
72 | //setIntr(idt + 0x3, SEG_KCODE, , DPL_USER); // for int 3, interrupt vector is 0x3, Interruption is disabled
73 | //setIntr(idt + 0x4, SEG_KCODE, , DPL_USER); // for into, interrupt vector is 0x4, Interruption is disabled
74 | //setIntr(idt + 0x5, SEG_KCODE, , DPL_USER); // for bound, interrupt vector is 0x5, Interruption is disabled
75 | setIntr(idt + 0x80, SEG_KCODE, (uint32_t)irqSyscall, DPL_USER); // for int 0x80, interrupt vector is 0x80, Interruption is disabled
76 |
77 | /* 写入IDT */
78 | saveIdt(idt, sizeof(idt));
79 | }
80 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/kernel/kvm.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 |
5 | SegDesc gdt[NR_SEGMENTS]; // the new GDT, NR_SEGMENTS=7, defined in x86/memory.h
6 | TSS tss;
7 |
8 | void initSeg() { // setup kernel segements
9 | gdt[SEG_KCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, DPL_KERN);
10 | gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, DPL_KERN);
11 | gdt[SEG_UCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, DPL_USER);
12 | //gdt[SEG_UCODE] = SEG(STA_X | STA_R, 0x00000000,0xffffffff, DPL_USER);
13 | gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER);
14 | //gdt[SEG_UDATA] = SEG(STA_W, 0x00000000,0x000fffff, DPL_USER);
15 | gdt[SEG_TSS] = SEG16(STS_T32A, &tss, sizeof(TSS)-1, DPL_KERN);
16 | gdt[SEG_TSS].s = 0;
17 | setGdt(gdt, sizeof(gdt)); // gdt is set in bootloader, here reset gdt in kernel
18 |
19 | /*
20 | * 初始化TSS
21 | */
22 | tss.esp0 = 0x1fffff;
23 | tss.ss0 = KSEL(SEG_KDATA);
24 | asm volatile("ltr %%ax":: "a" (KSEL(SEG_TSS)));
25 |
26 | /*设置正确的段寄存器*/
27 | asm volatile("movw %%ax,%%ds":: "a" (KSEL(SEG_KDATA)));
28 | //asm volatile("movw %%ax,%%es":: "a" (KSEL(SEG_KDATA)));
29 | //asm volatile("movw %%ax,%%fs":: "a" (KSEL(SEG_KDATA)));
30 | //asm volatile("movw %%ax,%%gs":: "a" (KSEL(SEG_KDATA)));
31 | asm volatile("movw %%ax,%%ss":: "a" (KSEL(SEG_KDATA)));
32 |
33 | lLdt(0);
34 |
35 | }
36 |
37 | void enterUserSpace(uint32_t entry) {
38 | /*
39 | * Before enter user space
40 | * you should set the right segment registers here
41 | * and use 'iret' to jump to ring3
42 | */
43 | uint32_t EFLAGS = 0;
44 | asm volatile("pushl %0":: "r" (USEL(SEG_UDATA))); // push ss
45 | asm volatile("pushl %0":: "r" (0x2fffff));
46 | asm volatile("pushfl"); //push eflags, sti
47 | asm volatile("popl %0":"=r" (EFLAGS));
48 | asm volatile("pushl %0"::"r"(EFLAGS|0x200));
49 | asm volatile("pushl %0":: "r" (USEL(SEG_UCODE))); // push cs
50 | asm volatile("pushl %0":: "r" (entry));
51 | asm volatile("iret");
52 | }
53 |
54 | /*
55 | kernel is loaded to location 0x100000, i.e., 1MB
56 | size of kernel is not greater than 200*512 bytes, i.e., 100KB
57 | user program is loaded to location 0x200000, i.e., 2MB
58 | size of user program is not greater than 200*512 bytes, i.e., 100KB
59 | */
60 |
61 | void loadUMain(void) {
62 | // TODO: 参照bootloader加载内核的方式
63 | int i = 0;
64 | //int phoff = 0x34; // program header offset
65 | int offset = 0x1000; // .text section offset
66 | uint32_t elf = 0x200000; // physical memory addr to load
67 | uint32_t uMainEntry;
68 |
69 | for (i = 0; i < 200; i++) {
70 | readSect((void*)(elf + i*512), 201+i);
71 | }
72 |
73 | //putString("here\n");
74 |
75 | uMainEntry = ((struct ELFHeader *)elf)->entry; // entry address of the program
76 | //phoff = ((struct ELFHeader *)elf)->phoff;
77 | //offset = ((struct ProgramHeader *)(elf + phoff))->off;
78 |
79 | //putInt(uMainEntry);
80 | //putInt(phoff);
81 | //putInt(offset);
82 |
83 | for (i = 0; i < 200 * 512; i++) {
84 | *(unsigned char *)(elf + i) = *(unsigned char *)(elf + i + offset);
85 | }
86 | enterUserSpace(uMainEntry);
87 | }
88 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/idt.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | #define INTERRUPT_GATE_32 0xE
5 | #define TRAP_GATE_32 0xF
6 |
7 | /* IDT表的内容 */
8 | struct GateDescriptor idt[NR_IRQ]; // NR_IRQ=256, defined in x86/cpu.h
9 |
10 | /* 初始化一个中断门(interrupt gate) */
11 | static void setIntr(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
12 | ptr->offset_15_0 = offset & 0xFFFF;
13 | ptr->segment = selector << 3;
14 | ptr->pad0 = 0;
15 | ptr->type = INTERRUPT_GATE_32;
16 | ptr->system = FALSE;
17 | ptr->privilege_level = dpl;
18 | ptr->present = TRUE;
19 | ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
20 | }
21 |
22 | /* 初始化一个陷阱门(trap gate) */
23 | //static void setTrap(struct GateDescriptor *ptr, uint32_t selector, uint32_t offset, uint32_t dpl) {
24 | // ptr->offset_15_0 = offset & 0xFFFF;
25 | // ptr->segment = selector << 3;
26 | // ptr->pad0 = 0;
27 | // ptr->type = TRAP_GATE_32;
28 | // ptr->system = FALSE;
29 | // ptr->privilege_level = dpl;
30 | // ptr->present = TRUE;
31 | // ptr->offset_31_16 = (offset >> 16) & 0xFFFF;
32 | //}
33 |
34 | /* 声明函数,这些函数在汇编代码里定义 */
35 | void irqEmpty();
36 | void irqErrorCode();
37 |
38 | void irqDoubleFault(); // 0x8
39 | void irqInvalidTSS(); // 0xa
40 | void irqSegNotPresent(); // 0xb
41 | void irqStackSegFault(); // 0xc
42 | void irqGProtectFault(); // 0xd
43 | void irqPageFault(); // 0xe
44 | void irqAlignCheck(); // 0x11
45 | void irqSecException(); // 0x1e
46 | void irqTimer();
47 | void irqKeyboard();
48 | void irqSyscall();
49 |
50 | void initIdt() {
51 | int i;
52 | /* 为了防止系统异常终止,所有irq都有处理函数(irqEmpty)。 */
53 | for (i = 0; i < NR_IRQ; i ++) {
54 | setIntr(idt + i, SEG_KCODE, (uint32_t)irqEmpty, DPL_KERN);
55 | }
56 | /*
57 | * init your idt here
58 | * 初始化 IDT 表, 为中断设置中断处理函数
59 | */
60 | /* Exceptions with error code */
61 | setIntr(idt + 0x8, SEG_KCODE, (uint32_t)irqDoubleFault, DPL_KERN);
62 | setIntr(idt + 0xa, SEG_KCODE, (uint32_t)irqInvalidTSS, DPL_KERN);
63 | setIntr(idt + 0xb, SEG_KCODE, (uint32_t)irqSegNotPresent, DPL_KERN);
64 | setIntr(idt + 0xc, SEG_KCODE, (uint32_t)irqStackSegFault, DPL_KERN);
65 | setIntr(idt + 0xd, SEG_KCODE, (uint32_t)irqGProtectFault, DPL_KERN);
66 | setIntr(idt + 0xe, SEG_KCODE, (uint32_t)irqPageFault, DPL_KERN);
67 | setIntr(idt + 0x11, SEG_KCODE, (uint32_t)irqAlignCheck, DPL_KERN);
68 | setIntr(idt + 0x1e, SEG_KCODE, (uint32_t)irqSecException, DPL_KERN);
69 |
70 | setIntr(idt + 0x20, SEG_KCODE, (uint32_t)irqTimer, DPL_KERN);
71 | setIntr(idt + 0x21, SEG_KCODE, (uint32_t)irqKeyboard, DPL_KERN);
72 | /* Exceptions with DPL = 3 */
73 | //setIntr(idt + 0x3, SEG_KCODE, , DPL_USER); // for int 3, interrupt vector is 0x3, Interruption is disabled
74 | //setIntr(idt + 0x4, SEG_KCODE, , DPL_USER); // for into, interrupt vector is 0x4, Interruption is disabled
75 | //setIntr(idt + 0x5, SEG_KCODE, , DPL_USER); // for bound, interrupt vector is 0x5, Interruption is disabled
76 | setIntr(idt + 0x80, SEG_KCODE, (uint32_t)irqSyscall, DPL_USER); // for int 0x80, interrupt vector is 0x80, Interruption is disabled
77 |
78 | /* 写入IDT */
79 | saveIdt(idt, sizeof(idt));
80 | }
81 |
--------------------------------------------------------------------------------
/lab3/lab3/app/main.c:
--------------------------------------------------------------------------------
1 | #include "lib.h"
2 | #include "types.h"
3 |
4 | int data = 0;
5 |
6 | int uEntry(void) {
7 | //uint16_t selector;
8 | ////uint16_t selector = 16;
9 | //asm volatile("movw %%ss, %0":"=m"(selector)); //XXX necessary or not, iret may reset ds in QEMU
10 | //asm volatile("movw %%ax, %%ds"::"a"(selector));
11 |
12 | //asm volatile("cli"); //XXX test for CPL, will cause a #GP
13 | //asm volatile("int $0xe"); //XXX test for SWInterrupt/exception:fault, current instruction address is pushed into kernel stack, if idt[0xe].dpl>=cpl, o.w., cause #GP
14 | //asm volatile("int $0x80");//XXX test for SWInterrupt, next instruction address is pushed into kernel stack, if idt[0x80].dpl>=cpl, o.w., cause #GP
15 | //asm volatile("int 3 ...");//XXX equivalent to int 0x3?
16 | //asm volatile("into ...");//XXX equivalent to int 0x4?
17 | //asm volatile("bound ...");//XXX equivalent to int 0x5?
18 | //printf("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n");
19 | //printf("Scroll Screen Test.\n");
20 |
21 | //printf("printf test begin...\n");
22 | //printf("Task switching test begin...\n");
23 |
24 | int ret = fork();
25 | int i = 8;
26 | if (ret == 0) {
27 | //printf("the answer should be:\n");
28 | //printf("#######################################################\n");
29 | //printf("Hello, welcome to OSlab! I'm the body of the game. ");
30 | //printf("Bootblock loads me to the memory position of 0x100000, and Makefile also tells me that I'm at the location of 0x100000. ");
31 | //printf("\\%%~!@#/(^&*()_+`1234567890-=...... ");
32 | //printf("Now I will test your printf: ");
33 | //printf("1 + 1 = 2, 123 * 456 = 56088\n0, -1, -2147483648, -1412505855, -32768, 102030\n0, ffffffff, 80000000, abcdef01, ffff8000, 18e8e\n");
34 | //printf("#######################################################\n");
35 | data = 2;
36 | while( i != 0) {
37 | i --;
38 | printf("Child Process: Pong %d, %d;\n", data, i);
39 | //sleep(1);
40 | sleep(128);
41 | }
42 | exit();
43 | }
44 | else if (ret != -1) {
45 | //printf("your answer:\n");
46 | //printf("=======================================================\n");
47 | //printf("%s %s%scome %co%s", "Hello,", "", "wel", 't', " ");
48 | //printf("%c%c%c%c%c! ", 'O', 'S', 'l', 'a', 'b');
49 | //printf("I'm the %s of %s. %s 0x%x, %s 0x%x. ", "body", "the game", "Bootblock loads me to the memory position of", 0x100000, "and Makefile also tells me that I'm at the location of", 0x100000);
50 | //printf("\\%%~!@#/(^&*()_+`1234567890-=...... ");
51 | //printf("Now I will test your printf: ");
52 | //printf("%d + %d = %d, %d * %d = %d\n", 1, 1, 1 + 1, 123, 456, 123 * 456);
53 | //printf("%d, %d, %d, %d, %d, %d\n", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
54 | //printf("%x, %x, %x, %x, %x, %x\n", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
55 | //printf("=======================================================\n");
56 | //printf("Test end!!! Good luck!!!\n");
57 | data = 1;
58 | while( i != 0) {
59 | i --;
60 | printf("Father Process: Ping %d, %d;\n", data, i);
61 | //sleep(1);
62 | sleep(128);
63 | }
64 | exit();
65 | }
66 | //data = 'H';
67 | //printf("%c\n", data);
68 |
69 | //while(1);
70 | //return 0;
71 | return 0;
72 | }
73 |
--------------------------------------------------------------------------------
/lab1/bootloader/start.s:
--------------------------------------------------------------------------------
1 | /* Real Mode Hello World */
2 | /*.code16
3 |
4 | .global start
5 | start:
6 | movw %cs, %ax
7 | movw %ax, %ds
8 | movw %ax, %es
9 | movw %ax, %ss
10 | movw $0x7d00, %ax
11 | movw %ax, %sp # setting stack pointer to 0x7d00
12 | pushw $13 # pushing the size to print into stack
13 | pushw $message # pushing the address of message into stack
14 | callw displayStr # calling the display function
15 | loop:
16 | jmp loop
17 |
18 | message:
19 | .string "Hello, World!\n\0"
20 |
21 | displayStr:
22 | pushw %bp
23 | movw 4(%esp), %ax
24 | movw %ax, %bp
25 | movw 6(%esp), %cx
26 | movw $0x1301, %ax
27 | movw $0x000c, %bx
28 | movw $0x0000, %dx
29 | int $0x10
30 | popw %bp
31 | ret
32 | */
33 |
34 |
35 | /* Protected Mode Hello World */
36 | /*.code16
37 |
38 | .global start
39 | start:
40 | movw %cs, %ax
41 | movw %ax, %ds
42 | movw %ax, %es
43 | movw %ax, %ss
44 | cli
45 | inb $0x92,%al
46 | //into nan qiao xin pian
47 |
48 | orb $0x02,%al
49 | //open xin pian di er wei
50 |
51 | outb %al,$0x92
52 | //pei zhi fan hui gei xin pian
53 | data32 addr32 lgdt gdtDesc
54 | //orl $0x01,%cr0
55 |
56 | movl %cr0, %eax
57 | orb $0x01, %al
58 | movl %eax, %cr0
59 | //set cr0 first 1
60 | data32 ljmp $0x08, $start32
61 |
62 | .code32
63 | start32:
64 | movw $0x10, %ax # setting data segment selector
65 | movw %ax, %ds
66 | movw %ax, %es
67 | movw %ax, %fs
68 | movw %ax, %ss
69 | //movl $0x48656c6c,0xb8000
70 | movl $0x0f650f48,0xb8000
71 | movl $0x0f6c0f6c,0xb8004
72 | //movl $0x6f2c776f,0xb8004
73 | movl $0x0f2c0f6f,0xb8008
74 | movl $0x0f6f0f77,0xb800c
75 | //movl $0x726c6421,0xb8008
76 | movl $0x0f6c0f72,0xb8010
77 | movl $0x0f210f64,0xb8014
78 | loop32:
79 | jmp loop32
80 |
81 |
82 | .p2align 2
83 | gdt:
84 | .word 0,0
85 | .byte 0,0,0,0
86 |
87 | .word 0xffff,0
88 | .byte 0,0x9a,0xcf,0
89 |
90 | .word 0xffff,0
91 | .byte 0,0x92,0xcf,0
92 |
93 | .word 0xffff,0x8000
94 | .byte 0x0b,0x92,0xcf,0
95 | #GDT definition here
96 |
97 | gdtDesc:
98 | #gdtDesc definition here
99 | .word (gdtDesc-gdt-1)
100 | .long gdt
101 | */
102 | /* Protected Mode Loading Hello World APP */
103 | .code16
104 |
105 | .global start
106 | start:
107 | movw %cs, %ax
108 | movw %ax, %ds
109 | movw %ax, %es
110 | movw %ax, %ss
111 | #TODO: Protected Mode Here
112 | cli
113 | inb $0x92,%al
114 | //into nan qiao xin pian
115 |
116 | orb $0x02,%al
117 | //open xin pian di er wei
118 |
119 | outb %al,$0x92
120 | //pei zhi fan hui gei xin pian
121 | data32 addr32 lgdt gdtDesc
122 | //orl $0x01,%cr0
123 |
124 | movl %cr0, %eax
125 | orb $0x01, %al
126 | movl %eax, %cr0
127 | //set cr0 first 1
128 | data32 ljmp $0x08, $start32
129 |
130 | .code32
131 | start32:
132 | movw $0x10, %ax # setting data segment selector
133 | movw %ax, %ds
134 | movw %ax, %es
135 | movw %ax, %fs
136 | movw %ax, %ss
137 | movw $0x18, %ax # setting graphics data segment selector
138 | movw %ax, %gs
139 |
140 | movl $0x8000, %eax # setting esp
141 | movl %eax, %esp
142 | jmp bootMain # jump to bootMain in boot.c
143 |
144 | .p2align 2
145 | gdt:
146 | .word 0,0
147 | .byte 0,0,0,0
148 |
149 | .word 0xffff,0
150 | .byte 0,0x9a,0xcf,0
151 |
152 | .word 0xffff,0
153 | .byte 0,0x92,0xcf,0
154 |
155 | .word 0xffff,0x8000
156 | .byte 0x0b,0x92,0xcf,0
157 | #GDT definition here
158 |
159 | gdtDesc:
160 | #gdtDesc definition here
161 | .word (gdtDesc-gdt-1)
162 | .long gdt
163 |
164 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/kernel/kvm.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | SegDesc gdt[NR_SEGMENTS]; // the new GDT, NR_SEGMENTS=10, defined in x86/memory.h
5 | TSS tss;
6 |
7 | ProcessTable pcb[MAX_PCB_NUM]; // pcb
8 | int current; // current process
9 |
10 | /*
11 | MACRO
12 | SEG(type, base, lim, dpl) (SegDesc) {...};
13 | SEG_KCODE=1
14 | SEG_KDATA=2
15 | SEG_TSS=NR_SEGMENTS-1
16 | DPL_KERN=0
17 | DPL_USER=3
18 | KSEL(desc) (((desc)<<3) | DPL_KERN)
19 | USEL(desc) (((desc)<<3) | DPL_UERN)
20 | asm [volatile] (AssemblerTemplate : OutputOperands [ : InputOperands [ : Clobbers ] ])
21 | asm [volatile] (AssemblerTemplate : : InputOperands : Clobbers : GotoLabels)
22 | */
23 | void initSeg() { // setup kernel segements
24 | int i;
25 |
26 | gdt[SEG_KCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, DPL_KERN);
27 | gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, DPL_KERN);
28 |
29 | for (i = 1; i < MAX_PCB_NUM; i++) {
30 | gdt[1+i*2] = SEG(STA_X | STA_R, (i+1)*0x100000,0x00100000, DPL_USER);
31 | gdt[2+i*2] = SEG(STA_W, (i+1)*0x100000,0x00100000, DPL_USER);
32 | }
33 |
34 | gdt[SEG_TSS] = SEG16(STS_T32A, &tss, sizeof(TSS)-1, DPL_KERN);
35 | gdt[SEG_TSS].s = 0;
36 |
37 | setGdt(gdt, sizeof(gdt)); // gdt is set in bootloader, here reset gdt in kernel
38 |
39 | /* initialize TSS */
40 | tss.ss0 = KSEL(SEG_KDATA);
41 | asm volatile("ltr %%ax":: "a" (KSEL(SEG_TSS)));
42 |
43 | /* reassign segment register */
44 | asm volatile("movw %%ax,%%ds":: "a" (KSEL(SEG_KDATA)));
45 | asm volatile("movw %%ax,%%ss":: "a" (KSEL(SEG_KDATA)));
46 |
47 | lLdt(0);
48 |
49 | }
50 |
51 | uint32_t loadUMain(void);
52 |
53 | void initProc() {
54 | int i;
55 | for (i = 0; i < MAX_PCB_NUM; i++) {
56 | pcb[i].state = STATE_DEAD;
57 | }
58 | // kernel process
59 | pcb[0].stackTop = (uint32_t)&(pcb[0].stackTop);
60 | pcb[0].prevStackTop = (uint32_t)&(pcb[0].stackTop);
61 | pcb[0].state = STATE_RUNNING;
62 | pcb[0].timeCount = MAX_TIME_COUNT;
63 | pcb[0].sleepTime = 0;
64 | pcb[0].pid = 0;
65 | // user process
66 | pcb[1].stackTop = (uint32_t)&(pcb[1].regs);
67 | pcb[1].prevStackTop = (uint32_t)&(pcb[1].stackTop);
68 | pcb[1].state = STATE_RUNNABLE;
69 | pcb[1].timeCount = 0;
70 | pcb[1].sleepTime = 0;
71 | pcb[1].pid = 1;
72 | pcb[1].regs.ss = USEL(4);
73 | pcb[1].regs.esp = 0x100000;
74 | asm volatile("pushfl");
75 | asm volatile("popl %0":"=r"(pcb[1].regs.eflags));
76 | pcb[1].regs.eflags = pcb[1].regs.eflags | 0x200;
77 | pcb[1].regs.cs = USEL(3);
78 | pcb[1].regs.eip = loadUMain();
79 | pcb[1].regs.ds = USEL(4);
80 | pcb[1].regs.es = USEL(4);
81 | pcb[1].regs.fs = USEL(4);
82 | pcb[1].regs.gs = USEL(4);
83 |
84 | current = 0; // kernel idle process
85 | asm volatile("movl %0, %%esp"::"m"(pcb[0].stackTop)); // switch to kernel stack for kernel idle process
86 | enableInterrupt();
87 | asm volatile("int $0x20"); // trigger irqTimer
88 | putChar('a');
89 | while(1)
90 | waitForInterrupt();
91 | }
92 |
93 | /*
94 | kernel is loaded to location 0x100000, i.e., 1MB
95 | size of kernel is not greater than 200*512 bytes, i.e., 100KB
96 | user program is loaded to location 0x200000, i.e., 2MB
97 | size of user program is not greater than 200*512 bytes, i.e., 100KB
98 | */
99 |
100 | uint32_t loadUMain(void) {
101 | int i = 0;
102 | int phoff = 0x34; // program header offset
103 | int offset = 0x1000; // .text section offset
104 | uint32_t elf = 0x200000; // physical memory addr to load
105 | uint32_t uMainEntry = 0x200000;
106 |
107 | for (i = 0; i < 200; i++) {
108 | readSect((void*)(elf + i*512), 201+i);
109 | }
110 |
111 | uMainEntry = ((struct ELFHeader *)elf)->entry; // entry address of the program
112 | phoff = ((struct ELFHeader *)elf)->phoff;
113 | offset = ((struct ProgramHeader *)(elf + phoff))->off;
114 |
115 | for (i = 0; i < 200 * 512; i++) {
116 | *(uint8_t *)(elf + i) = *(uint8_t *)(elf + i + offset);
117 | }
118 | return uMainEntry;
119 | }
120 |
--------------------------------------------------------------------------------
/lab3/lab3/bootloader/start.S:
--------------------------------------------------------------------------------
1 | /* Real Mode Hello World */
2 | #.code16
3 | #
4 | #.global start
5 | #start:
6 | # movw %cs, %ax
7 | # movw %ax, %ds
8 | # movw %ax, %es
9 | # movw %ax, %ss
10 | # movw $0x7d00, %ax
11 | # movw %ax, %sp # setting stack pointer to 0x7d00
12 | # pushw $13 # pushing the size to print into stack
13 | # pushw $message # pushing the address of message into stack
14 | # callw displayStr # calling the display function
15 | #loop:
16 | # jmp loop
17 | #
18 | #message:
19 | # .string "Hello, World!\n\0"
20 | #
21 | #displayStr:
22 | # pushw %bp
23 | # movw 4(%esp), %ax
24 | # movw %ax, %bp
25 | # movw 6(%esp), %cx
26 | # movw $0x1301, %ax
27 | # movw $0x000c, %bx
28 | # movw $0x0000, %dx
29 | # int $0x10
30 | # popw %bp
31 | # ret
32 |
33 | /* Protected Mode Hello World */
34 | #.code16
35 | #
36 | #.global start
37 | #start:
38 | # movw %cs, %ax
39 | # movw %ax, %ds
40 | # movw %ax, %es
41 | # movw %ax, %ss
42 | # cli # clear interuption
43 | # inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
44 | # orb $0x02, %al
45 | # outb %al, $0x92
46 | # data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
47 | # movl %cr0, %eax
48 | # orb $0x01, %al
49 | # movl %eax, %cr0 # setting cr0
50 | # data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
51 | #
52 | #.code32
53 | #start32:
54 | # movw $0x10, %ax # setting data segment selector
55 | # movw %ax, %ds
56 | # movw %ax, %es
57 | # movw %ax, %fs
58 | # movw %ax, %ss
59 | # movw $0x18, %ax # setting graphics data segment selector
60 | # movw %ax, %gs
61 | #
62 | # movl $0x8000, %eax # setting esp
63 | # movl %eax, %esp
64 | # pushl $13
65 | # pushl $message
66 | # calll displayStr
67 | #loop32:
68 | # jmp loop32
69 | #
70 | #message:
71 | # .string "Hello, World!\n\0"
72 | #
73 | #displayStr:
74 | # movl 4(%esp), %ebx
75 | # movl 8(%esp), %ecx
76 | # movl $((80*5+0)*2), %edi
77 | # movb $0x0c, %ah
78 | #nextChar:
79 | # movb (%ebx), %al
80 | # movw %ax, %gs:(%edi)
81 | # addl $2, %edi
82 | # incl %ebx
83 | # loopnz nextChar # loopnz decrease ecx by 1
84 | # ret
85 | #
86 | #.p2align 2
87 | #gdt: # 8 bytes for each table entry, at least 1 entry
88 | # .word 0,0 # empty entry
89 | # .byte 0,0,0,0
90 | #
91 | # .word 0xffff,0 # code segment entry
92 | # .byte 0,0x9a,0xcf,0
93 | #
94 | # .word 0xffff,0 # data segment entry
95 | # .byte 0,0x92,0xcf,0
96 | #
97 | # .word 0xffff,0x8000 # graphics segment entry
98 | # .byte 0x0b,0x92,0xcf,0
99 | #
100 | #gdtDesc: # 6 bytes in total
101 | # .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
102 | # .long gdt # offset, i.e. linear address of the table itself
103 |
104 | /* Protected Mode Loading Hello World APP */
105 | .code16
106 |
107 | .global start
108 | start:
109 | movw %cs, %ax
110 | movw %ax, %ds
111 | movw %ax, %es
112 | movw %ax, %ss
113 | cli # clear interuption
114 | inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
115 | orb $0x02, %al
116 | outb %al, $0x92
117 | data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
118 | movl %cr0, %eax
119 | orb $0x01, %al
120 | movl %eax, %cr0 # setting cr0
121 | data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
122 |
123 | .code32
124 | start32:
125 | movw $0x10, %ax # setting data segment selector
126 | movw %ax, %ds
127 | movw %ax, %es
128 | movw %ax, %fs
129 | movw %ax, %ss
130 | movw $0x18, %ax # setting graphics data segment selector
131 | movw %ax, %gs
132 |
133 | movl $0x200000, %eax # setting esp
134 | movl %eax, %esp
135 | jmp bootMain # jump to bootMain in boot.c
136 |
137 | .p2align 2
138 | gdt: # 8 bytes for each table entry, at least 1 entry
139 | .word 0,0 # empty entry
140 | .byte 0,0,0,0
141 |
142 | .word 0xffff,0 # code segment entry
143 | .byte 0,0x9a,0xcf,0
144 |
145 | .word 0xffff,0 # data segment entry
146 | .byte 0,0x92,0xcf,0
147 |
148 | .word 0xffff,0x8000 # graphics segment entry
149 | .byte 0x0b,0x92,0xcf,0
150 |
151 | gdtDesc: # 6 bytes in total
152 | .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
153 | .long gdt # offset, i.e. linear address of the table itself
154 |
--------------------------------------------------------------------------------
/lab5/code/bootloader/start.S:
--------------------------------------------------------------------------------
1 | /* Real Mode Hello World */
2 | #.code16
3 | #
4 | #.global start
5 | #start:
6 | # movw %cs, %ax
7 | # movw %ax, %ds
8 | # movw %ax, %es
9 | # movw %ax, %ss
10 | # movw $0x7d00, %ax
11 | # movw %ax, %sp # setting stack pointer to 0x7d00
12 | # pushw $13 # pushing the size to print into stack
13 | # pushw $message # pushing the address of message into stack
14 | # callw displayStr # calling the display function
15 | #loop:
16 | # jmp loop
17 | #
18 | #message:
19 | # .string "Hello, World!\n\0"
20 | #
21 | #displayStr:
22 | # pushw %bp
23 | # movw 4(%esp), %ax
24 | # movw %ax, %bp
25 | # movw 6(%esp), %cx
26 | # movw $0x1301, %ax
27 | # movw $0x000c, %bx
28 | # movw $0x0000, %dx
29 | # int $0x10
30 | # popw %bp
31 | # ret
32 |
33 | /* Protected Mode Hello World */
34 | #.code16
35 | #
36 | #.global start
37 | #start:
38 | # movw %cs, %ax
39 | # movw %ax, %ds
40 | # movw %ax, %es
41 | # movw %ax, %ss
42 | # cli # clear interuption
43 | # inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
44 | # orb $0x02, %al
45 | # outb %al, $0x92
46 | # data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
47 | # movl %cr0, %eax
48 | # orb $0x01, %al
49 | # movl %eax, %cr0 # setting cr0
50 | # data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
51 | #
52 | #.code32
53 | #start32:
54 | # movw $0x10, %ax # setting data segment selector
55 | # movw %ax, %ds
56 | # movw %ax, %es
57 | # movw %ax, %fs
58 | # movw %ax, %ss
59 | # movw $0x18, %ax # setting graphics data segment selector
60 | # movw %ax, %gs
61 | #
62 | # movl $0x8000, %eax # setting esp
63 | # movl %eax, %esp
64 | # pushl $13
65 | # pushl $message
66 | # calll displayStr
67 | #loop32:
68 | # jmp loop32
69 | #
70 | #message:
71 | # .string "Hello, World!\n\0"
72 | #
73 | #displayStr:
74 | # movl 4(%esp), %ebx
75 | # movl 8(%esp), %ecx
76 | # movl $((80*5+0)*2), %edi
77 | # movb $0x0c, %ah
78 | #nextChar:
79 | # movb (%ebx), %al
80 | # movw %ax, %gs:(%edi)
81 | # addl $2, %edi
82 | # incl %ebx
83 | # loopnz nextChar # loopnz decrease ecx by 1
84 | # ret
85 | #
86 | #.p2align 2
87 | #gdt: # 8 bytes for each table entry, at least 1 entry
88 | # .word 0,0 # empty entry
89 | # .byte 0,0,0,0
90 | #
91 | # .word 0xffff,0 # code segment entry
92 | # .byte 0,0x9a,0xcf,0
93 | #
94 | # .word 0xffff,0 # data segment entry
95 | # .byte 0,0x92,0xcf,0
96 | #
97 | # .word 0xffff,0x8000 # graphics segment entry
98 | # .byte 0x0b,0x92,0xcf,0
99 | #
100 | #gdtDesc: # 6 bytes in total
101 | # .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
102 | # .long gdt # offset, i.e. linear address of the table itself
103 |
104 | /* Protected Mode Loading Hello World APP */
105 | .code16
106 |
107 | .global start
108 | start:
109 | movw %cs, %ax
110 | movw %ax, %ds
111 | movw %ax, %es
112 | movw %ax, %ss
113 | cli # clear interuption
114 | inb $0x92, %al # Fast setup A20 Line with port 0x92, necessary or not?
115 | orb $0x02, %al
116 | outb %al, $0x92
117 | data32 addr32 lgdt gdtDesc # loading gdtr, data32, addr32
118 | movl %cr0, %eax
119 | orb $0x01, %al
120 | movl %eax, %cr0 # setting cr0
121 | data32 ljmp $0x08, $start32 # reload code segment selector and ljmp to start32, data32
122 |
123 | .code32
124 | start32:
125 | movw $0x10, %ax # setting data segment selector
126 | movw %ax, %ds
127 | movw %ax, %es
128 | movw %ax, %fs
129 | movw %ax, %ss
130 | movw $0x18, %ax # setting graphics data segment selector
131 | movw %ax, %gs
132 |
133 | movl $0x200000, %eax # setting esp
134 | movl %eax, %esp
135 | jmp bootMain # jump to bootMain in boot.c
136 |
137 | .p2align 2
138 | gdt: # 8 bytes for each table entry, at least 1 entry
139 | .word 0,0 # empty entry
140 | .byte 0,0,0,0
141 |
142 | .word 0xffff,0 # code segment entry
143 | .byte 0,0x9a,0xcf,0
144 |
145 | .word 0xffff,0 # data segment entry
146 | .byte 0,0x92,0xcf,0
147 |
148 | .word 0xffff,0x8000 # graphics segment entry
149 | .byte 0x0b,0x92,0xcf,0
150 |
151 | gdtDesc: # 6 bytes in total
152 | .word (gdtDesc - gdt -1) # size of the table, 2 bytes, 65536-1 bytes, 8192 entries
153 | .long gdt # offset, i.e. linear address of the table itself
154 |
--------------------------------------------------------------------------------
/lab2/lab2/kernel/include/x86/memory.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_MEMORY_H__
2 | #define __X86_MEMORY_H__
3 |
4 | #define DPL_KERN 0
5 | #define DPL_USER 3
6 |
7 | // Application segment type bits
8 | #define STA_X 0x8 // Executable segment
9 | #define STA_W 0x2 // Writeable (non-executable segments)
10 | #define STA_R 0x2 // Readable (executable segments)
11 |
12 | // System segment type bits
13 | #define STS_T32A 0x9 // Available 32-bit TSS
14 | #define STS_IG32 0xE // 32-bit Interrupt Gate
15 | #define STS_TG32 0xF // 32-bit Trap Gate
16 |
17 | // GDT entries
18 | #define NR_SEGMENTS 7 // GDT size
19 | #define SEG_KCODE 1 // Kernel code
20 | #define SEG_KDATA 2 // Kernel data/stack
21 | #define SEG_UCODE 3 // User code
22 | #define SEG_UDATA 4 // User data/stack
23 | #define SEG_TSS 5 // Global unique task state segement
24 |
25 | // Selectors
26 | #define KSEL(desc) (((desc) << 3) | DPL_KERN)
27 | #define USEL(desc) (((desc) << 3) | DPL_USER)
28 |
29 | struct GateDescriptor {
30 | uint32_t offset_15_0 : 16;
31 | uint32_t segment : 16;
32 | uint32_t pad0 : 8;
33 | uint32_t type : 4;
34 | uint32_t system : 1;
35 | uint32_t privilege_level : 2;
36 | uint32_t present : 1;
37 | uint32_t offset_31_16 : 16;
38 | };
39 |
40 | struct TrapFrame {
41 | uint32_t edi, esi, ebp, xxx, ebx, edx, ecx, eax;
42 | int32_t irq;
43 | };
44 |
45 | /*
46 | 1. The number of bits in a bit field sets the limit to the range of values it can hold
47 | 2. Multiple adjacent bit fields are usually packed together (although this behavior is implementation-defined)
48 |
49 | Refer: en.cppreference.com/w/cpp/language/bit_field
50 | */
51 | struct SegDesc {
52 | uint32_t lim_15_0 : 16; // Low bits of segment limit
53 | uint32_t base_15_0 : 16; // Low bits of segment base address
54 | uint32_t base_23_16 : 8; // Middle bits of segment base address
55 | uint32_t type : 4; // Segment type (see STS_ constants)
56 | uint32_t s : 1; // 0 = system, 1 = application
57 | uint32_t dpl : 2; // Descriptor Privilege Level
58 | uint32_t p : 1; // Present
59 | uint32_t lim_19_16 : 4; // High bits of segment limit
60 | uint32_t avl : 1; // Unused (available for software use)
61 | uint32_t rsv1 : 1; // Reserved
62 | uint32_t db : 1; // 0 = 16-bit segment, 1 = 32-bit segment
63 | uint32_t g : 1; // Granularity: limit scaled by 4K when set
64 | uint32_t base_31_24 : 8; // High bits of segment base address
65 | };
66 | typedef struct SegDesc SegDesc;
67 |
68 | #define SEG(type, base, lim, dpl) (SegDesc) \
69 | { ((lim) >> 12) & 0xffff, (uint32_t)(base) & 0xffff, \
70 | ((uint32_t)(base) >> 16) & 0xff, type, 1, dpl, 1, \
71 | (uint32_t)(lim) >> 28, 0, 0, 1, 1, (uint32_t)(base) >> 24 }
72 |
73 | #define SEG16(type, base, lim, dpl) (SegDesc) \
74 | { (lim) & 0xffff, (uint32_t)(base) & 0xffff, \
75 | ((uint32_t)(base) >> 16) & 0xff, type, 0, dpl, 1, \
76 | (uint32_t)(lim) >> 16, 0, 0, 1, 0, (uint32_t)(base) >> 24 }
77 |
78 | // Task state segment format
79 | struct TSS {
80 | uint32_t link; // old ts selector
81 | uint32_t esp0; // Ring 0 Stack pointer and segment selector
82 | uint32_t ss0; // after an increase in privilege level
83 | union{
84 | struct{
85 | char dontcare[88];
86 | };
87 | struct{
88 | uint32_t esp1,ss1,esp2,ss2;
89 | uint32_t cr3, eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
90 | uint32_t es, cs, ss, ds, fs, gs, ldt;
91 | };
92 | };
93 | };
94 | typedef struct TSS TSS;
95 |
96 | static inline void setGdt(SegDesc *gdt, uint32_t size) {
97 | volatile static uint16_t data[3];
98 | data[0] = size - 1;
99 | data[1] = (uint32_t)gdt;
100 | data[2] = (uint32_t)gdt >> 16;
101 | asm volatile("lgdt (%0)" : : "r"(data));
102 | }
103 |
104 | static inline void lLdt(uint16_t sel)
105 | {
106 | asm volatile("lldt %0" :: "r"(sel));
107 | }
108 |
109 | #endif
110 |
--------------------------------------------------------------------------------
/lab5/code/utils/genFS/data.h:
--------------------------------------------------------------------------------
1 | #ifndef __DATA_H__
2 | #define __DATA_H__
3 |
4 | #include "types.h"
5 |
6 | /*
7 | * Layout of block group 0
8 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
9 | *| SuperBlock | GroupDescTable | InodeBitmap | BlockBitmap | InodeTable | DataBlocks |
10 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
11 | * 1024 Bytes Many Block 1 Block 1 Block Many Blocks Many More Blocks
12 | *
13 | * Layout of block group 1
14 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
15 | *| SuperBlock | GroupDescTable | InodeBitmap | BlockBitmap | InodeTable | DataBlocks |
16 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
17 | * 1024 Bytes Many Block 1 Block 1 Block Many Blocks Many More Blocks
18 |
19 | */
20 |
21 | /*
22 | * GROUP_SIZE is at least 1024 bytes + 3 blocks
23 | * s: SECTOR_NUM
24 | * b: SECTORS_PER_BLOCK
25 | * g: GROUP_NUM
26 | * g = \lfloor\frac{s}{2+\lceil(32g/512/b)\rceil*b+b+b+\lceil(128*512*b/512/b)\rceil*b+512*b*b}\rfloor
27 | */
28 |
29 | /* Independent Variables */
30 | //#define SECTOR_NUM 1024 // i.e., 512 KB, as we take file to simulate hard disk, SECTOR_NUM is bounded by (2^32 / 2^9) sectors for 32-bits system
31 | #define SECTOR_NUM 8196
32 | #define SECTOR_SIZE 512 // i.e., 512 B
33 | #define SECTORS_PER_BLOCK 2
34 | #define POINTER_NUM 12
35 | #define NAME_LENGTH 64
36 |
37 | /* Dependent Variables & Constants */
38 | #define BLOCK_SIZE (SECTOR_SIZE * SECTORS_PER_BLOCK)
39 |
40 | #define MAX_GROUP_NUM (SECTOR_NUM / SECTOR_SIZE / SECTORS_PER_BLOCK / 8 / SECTORS_PER_BLOCK + 1)
41 |
42 | #define SUPER_BLOCK_SIZE 1024
43 | #define GROUP_DESC_SIZE 32
44 | #define INODE_BITMAP_SIZE BLOCK_SIZE
45 | #define BLOCK_BITMAP_SIZE BLOCK_SIZE
46 | #define INODE_SIZE 128
47 | #define DIRENTRY_SIZE 128
48 |
49 | #define UNKNOWN_TYPE 0
50 | #define REGULAR_TYPE 1
51 | #define DIRECTORY_TYPE 2
52 | #define CHARACTER_TYPE 3
53 | #define BLOCK_TYPE 4
54 | #define FIFO_TYPE 5
55 | #define SOCKET_TYPE 6
56 | #define SYMBOLIC_TYPE 7
57 |
58 | union SuperBlock {
59 | uint8_t byte[SUPER_BLOCK_SIZE];
60 | struct {
61 | int32_t sectorNum; // total number of sectors
62 | int32_t inodeNum; // total number of inodes
63 | int32_t blockNum; // total number of data blocks
64 | int32_t availInodeNum; // total number of available inodes
65 | int32_t availBlockNum; // total number of available data blocks
66 | int32_t blockSize; // number of bytes in each block
67 | int32_t inodesPerGroup; // number of inodes in each group
68 | int32_t blocksPerGroup; // number of blocks in each group
69 | };
70 | };
71 |
72 | typedef union SuperBlock SuperBlock;
73 |
74 | union GroupDesc {
75 | uint8_t byte[GROUP_DESC_SIZE];
76 | struct {
77 | int32_t inodeBitmap; // sector as unit
78 | int32_t blockBitmap; // sector as unit
79 | int32_t inodeTable; // sector as unit
80 | int32_t availInodeNum;
81 | int32_t availBlockNum;
82 | };
83 | };
84 |
85 | typedef union GroupDesc GroupDesc;
86 |
87 | struct InodeBitmap {
88 | uint8_t byte[INODE_BITMAP_SIZE];
89 | };
90 |
91 | typedef struct InodeBitmap InodeBitmap;
92 |
93 | struct BlockBitmap {
94 | uint8_t byte[BLOCK_BITMAP_SIZE];
95 | };
96 |
97 | typedef struct BlockBitmap BlockBitmap;
98 |
99 | union Inode {
100 | uint8_t byte[INODE_SIZE];
101 | struct {
102 | int16_t type; // further implement privilege control, i.e., drwxrwxrwx, uid, gid, others
103 | int16_t linkCount;
104 | int32_t blockCount;
105 | int32_t size; // size of this file, byte as unit
106 | int32_t pointer[POINTER_NUM];
107 | int32_t singlyPointer;
108 | int32_t doublyPointer;
109 | int32_t triplyPointer;
110 | };
111 | };
112 |
113 | typedef union Inode Inode;
114 |
115 | union DirEntry {
116 | uint8_t byte[DIRENTRY_SIZE];
117 | struct {
118 | int32_t inode; // byte as unit? TODO inode index as unit
119 | //int32_t size; // size of this file, byte as unit
120 | //int8_t type; // may not be necessary
121 | char name[NAME_LENGTH];
122 | };
123 | };
124 |
125 | typedef union DirEntry DirEntry;
126 |
127 | #endif
128 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/kernel/kvm.c:
--------------------------------------------------------------------------------
1 | #include "x86.h"
2 | #include "device.h"
3 |
4 | SegDesc gdt[NR_SEGMENTS]; // the new GDT, NR_SEGMENTS=10, defined in x86/memory.h
5 | TSS tss;
6 |
7 | ProcessTable pcb[MAX_PCB_NUM]; // pcb
8 | int current; // current process
9 |
10 | Semaphore sem[MAX_SEM_NUM];
11 | Device dev[MAX_DEV_NUM];
12 |
13 | /*
14 | MACRO
15 | SEG(type, base, lim, dpl) (SegDesc) {...};
16 | SEG_KCODE=1
17 | SEG_KDATA=2
18 | SEG_TSS=NR_SEGMENTS-1
19 | DPL_KERN=0
20 | DPL_USER=3
21 | KSEL(desc) (((desc)<<3) | DPL_KERN)
22 | USEL(desc) (((desc)<<3) | DPL_UERN)
23 | asm [volatile] (AssemblerTemplate : OutputOperands [ : InputOperands [ : Clobbers ] ])
24 | asm [volatile] (AssemblerTemplate : : InputOperands : Clobbers : GotoLabels)
25 | */
26 | void initSeg() { // setup kernel segements
27 | int i;
28 |
29 | gdt[SEG_KCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, DPL_KERN);
30 | gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, DPL_KERN);
31 |
32 | for (i = 1; i < MAX_PCB_NUM; i++) {
33 | gdt[1+i*2] = SEG(STA_X | STA_R, (i+1)*0x100000,0x00100000, DPL_USER);
34 | gdt[2+i*2] = SEG(STA_W, (i+1)*0x100000,0x00100000, DPL_USER);
35 | }
36 |
37 | gdt[SEG_TSS] = SEG16(STS_T32A, &tss, sizeof(TSS)-1, DPL_KERN);
38 | gdt[SEG_TSS].s = 0;
39 |
40 | setGdt(gdt, sizeof(gdt)); // gdt is set in bootloader, here reset gdt in kernel
41 |
42 | /* initialize TSS */
43 | tss.ss0 = KSEL(SEG_KDATA);
44 | asm volatile("ltr %%ax":: "a" (KSEL(SEG_TSS)));
45 |
46 | /* reassign segment register */
47 | asm volatile("movw %%ax,%%ds":: "a" (KSEL(SEG_KDATA)));
48 | asm volatile("movw %%ax,%%ss":: "a" (KSEL(SEG_KDATA)));
49 |
50 | lLdt(0);
51 |
52 | }
53 |
54 | void initSem() {
55 | int i;
56 | for (i = 0; i < MAX_SEM_NUM; i++) {
57 | sem[i].state = 0; // 0: not in use; 1: in use;
58 | sem[i].value = 0; // >=0: no process blocked; -1: 1 process blocked; -2: 2 process blocked;...
59 | sem[i].pcb.next = &(sem[i].pcb);
60 | sem[i].pcb.prev = &(sem[i].pcb);
61 | }
62 | }
63 |
64 | void initDev() {
65 | int i;
66 | for (i = 0; i < MAX_DEV_NUM; i++) {
67 | dev[i].state = 1; // 0: not in use; 1: in use;
68 | dev[i].value = 0; // >=0: no blocked; -1: 1 process blocked; -2: 2 process blocked;...
69 | dev[i].pcb.next = &(dev[i].pcb);
70 | dev[i].pcb.prev = &(dev[i].pcb);
71 | }
72 | }
73 |
74 | uint32_t loadUMain(void);
75 |
76 | void initProc() {
77 | int i;
78 | for (i = 0; i < MAX_PCB_NUM; i++) {
79 | pcb[i].state = STATE_DEAD;
80 | }
81 | // kernel process
82 | pcb[0].stackTop = (uint32_t)&(pcb[0].stackTop);
83 | pcb[0].prevStackTop = (uint32_t)&(pcb[0].stackTop);
84 | pcb[0].state = STATE_RUNNING;
85 | pcb[0].timeCount = MAX_TIME_COUNT;
86 | pcb[0].sleepTime = 0;
87 | pcb[0].pid = 0;
88 | // user process
89 | pcb[1].stackTop = (uint32_t)&(pcb[1].regs);
90 | pcb[1].prevStackTop = (uint32_t)&(pcb[1].stackTop);
91 | pcb[1].state = STATE_RUNNABLE;
92 | pcb[1].timeCount = 0;
93 | pcb[1].sleepTime = 0;
94 | pcb[1].pid = 1;
95 | pcb[1].regs.ss = USEL(4);
96 | pcb[1].regs.esp = 0x100000;
97 | asm volatile("pushfl");
98 | asm volatile("popl %0":"=r"(pcb[1].regs.eflags));
99 | pcb[1].regs.eflags = pcb[1].regs.eflags | 0x200;
100 | pcb[1].regs.cs = USEL(3);
101 | pcb[1].regs.eip = loadUMain();
102 | pcb[1].regs.ds = USEL(4);
103 | pcb[1].regs.es = USEL(4);
104 | pcb[1].regs.fs = USEL(4);
105 | pcb[1].regs.gs = USEL(4);
106 |
107 | current = 0; // kernel idle process
108 | asm volatile("movl %0, %%esp"::"m"(pcb[0].stackTop)); // switch to kernel stack for kernel idle process
109 | enableInterrupt();
110 | asm volatile("int $0x20"); // trigger irqTimer
111 | while(1)
112 | waitForInterrupt();
113 | }
114 |
115 | /*
116 | kernel is loaded to location 0x100000, i.e., 1MB
117 | size of kernel is not greater than 200*512 bytes, i.e., 100KB
118 | user program is loaded to location 0x200000, i.e., 2MB
119 | size of user program is not greater than 200*512 bytes, i.e., 100KB
120 | */
121 |
122 | uint32_t loadUMain(void) {
123 | int i = 0;
124 | int phoff = 0x34; // program header offset
125 | int offset = 0x1000; // .text section offset
126 | uint32_t elf = 0x200000; // physical memory addr to load
127 | uint32_t uMainEntry = 0x200000;
128 |
129 | for (i = 0; i < 200; i++) {
130 | readSect((void*)(elf + i*512), 201+i);
131 | }
132 |
133 | uMainEntry = ((struct ELFHeader *)elf)->entry; // entry address of the program
134 | phoff = ((struct ELFHeader *)elf)->phoff;
135 | offset = ((struct ProgramHeader *)(elf + phoff))->off;
136 |
137 | for (i = 0; i < 200 * 512; i++) {
138 | *(uint8_t *)(elf + i) = *(uint8_t *)(elf + i + offset);
139 | }
140 |
141 | return uMainEntry;
142 | }
143 |
--------------------------------------------------------------------------------
/lab3/lab3/kernel/include/x86/memory.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_MEMORY_H__
2 | #define __X86_MEMORY_H__
3 |
4 | #define DPL_KERN 0
5 | #define DPL_USER 3
6 |
7 | // Application segment type bits
8 | #define STA_X 0x8 // Executable segment
9 | #define STA_W 0x2 // Writeable (non-executable segments)
10 | #define STA_R 0x2 // Readable (executable segments)
11 |
12 | // System segment type bits
13 | #define STS_T32A 0x9 // Available 32-bit TSS
14 | #define STS_IG32 0xE // 32-bit Interrupt Gate
15 | #define STS_TG32 0xF // 32-bit Trap Gate
16 |
17 | // GDT entries
18 | #define NR_SEGMENTS 10 // GDT size
19 | #define SEG_KCODE 1 // Kernel code
20 | #define SEG_KDATA 2 // Kernel data/stack
21 | #define SEG_TSS (NR_SEGMENTS-1)
22 |
23 | // Selectors
24 | #define KSEL(desc) (((desc) << 3) | DPL_KERN)
25 | #define USEL(desc) (((desc) << 3) | DPL_USER)
26 |
27 | struct GateDescriptor {
28 | uint32_t offset_15_0 : 16;
29 | uint32_t segment : 16;
30 | uint32_t pad0 : 8;
31 | uint32_t type : 4;
32 | uint32_t system : 1;
33 | uint32_t privilege_level : 2;
34 | uint32_t present : 1;
35 | uint32_t offset_31_16 : 16;
36 | };
37 |
38 | struct StackFrame {
39 | uint32_t gs, fs, es, ds;
40 | uint32_t edi, esi, ebp, xxx, ebx, edx, ecx, eax;
41 | uint32_t irq, error;
42 | uint32_t eip, cs, eflags, esp, ss;
43 | };
44 |
45 | #define MAX_STACK_SIZE 1024
46 | #define MAX_PCB_NUM ((NR_SEGMENTS-2)/2)
47 |
48 | #define STATE_RUNNABLE 0
49 | #define STATE_RUNNING 1
50 | #define STATE_BLOCKED 2
51 | #define STATE_DEAD 3
52 |
53 | #define MAX_TIME_COUNT 16
54 |
55 | struct ProcessTable {
56 | uint32_t stack[MAX_STACK_SIZE];
57 | struct StackFrame regs;
58 | uint32_t stackTop;
59 | uint32_t prevStackTop;
60 | int state;
61 | int timeCount;
62 | int sleepTime;
63 | uint32_t pid;
64 | char name[32];
65 | };
66 | typedef struct ProcessTable ProcessTable;
67 |
68 | /*
69 | 1. The number of bits in a bit field sets the limit to the range of values it can hold
70 | 2. Multiple adjacent bit fields are usually packed together (although this behavior is implementation-defined)
71 |
72 | Refer: en.cppreference.com/w/cpp/language/bit_field
73 | */
74 | struct SegDesc {
75 | uint32_t lim_15_0 : 16; // Low bits of segment limit
76 | uint32_t base_15_0 : 16; // Low bits of segment base address
77 | uint32_t base_23_16 : 8; // Middle bits of segment base address
78 | uint32_t type : 4; // Segment type (see STS_ constants)
79 | uint32_t s : 1; // 0 = system, 1 = application
80 | uint32_t dpl : 2; // Descriptor Privilege Level
81 | uint32_t p : 1; // Present
82 | uint32_t lim_19_16 : 4; // High bits of segment limit
83 | uint32_t avl : 1; // Unused (available for software use)
84 | uint32_t rsv1 : 1; // Reserved
85 | uint32_t db : 1; // 0 = 16-bit segment, 1 = 32-bit segment
86 | uint32_t g : 1; // Granularity: limit scaled by 4K when set
87 | uint32_t base_31_24 : 8; // High bits of segment base address
88 | };
89 | typedef struct SegDesc SegDesc;
90 |
91 | #define SEG(type, base, lim, dpl) (SegDesc) \
92 | { ((lim) >> 12) & 0xffff, (uint32_t)(base) & 0xffff, \
93 | ((uint32_t)(base) >> 16) & 0xff, type, 1, dpl, 1, \
94 | (uint32_t)(lim) >> 28, 0, 0, 1, 1, (uint32_t)(base) >> 24 }
95 |
96 | #define SEG16(type, base, lim, dpl) (SegDesc) \
97 | { (lim) & 0xffff, (uint32_t)(base) & 0xffff, \
98 | ((uint32_t)(base) >> 16) & 0xff, type, 0, dpl, 1, \
99 | (uint32_t)(lim) >> 16, 0, 0, 1, 0, (uint32_t)(base) >> 24 }
100 |
101 | // Task state segment format
102 | struct TSS {
103 | uint32_t link; // old ts selector
104 | uint32_t esp0; // Ring 0 Stack pointer and segment selector
105 | uint32_t ss0; // after an increase in privilege level
106 | union{
107 | struct{
108 | char dontcare[88];
109 | };
110 | struct{
111 | uint32_t esp1,ss1,esp2,ss2;
112 | uint32_t cr3, eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
113 | uint32_t es, cs, ss, ds, fs, gs, ldt;
114 | };
115 | };
116 | };
117 | typedef struct TSS TSS;
118 |
119 | static inline void setGdt(SegDesc *gdt, uint32_t size) {
120 | volatile static uint16_t data[3];
121 | data[0] = size - 1;
122 | data[1] = (uint32_t)gdt;
123 | data[2] = (uint32_t)gdt >> 16;
124 | asm volatile("lgdt (%0)" : : "r"(data));
125 | }
126 |
127 | static inline void lLdt(uint16_t sel)
128 | {
129 | asm volatile("lldt %0" :: "r"(sel));
130 | }
131 |
132 | #endif
133 |
--------------------------------------------------------------------------------
/lab5/code/kernel/include/fs/ext.h:
--------------------------------------------------------------------------------
1 | #ifndef __EXT_H__
2 | #define __EXT_H__
3 |
4 | /*
5 | EXT4 like filesystem
6 | */
7 |
8 | /*
9 | * Layout of block group 0
10 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
11 | *| SuperBlock | GroupDescTable | InodeBitmap | BlockBitmap | InodeTable | DataBlocks |
12 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
13 | * 1024 Bytes Many Block 1 Block 1 Block Many Blocks Many More Blocks
14 | *
15 | * Layout of block group 1
16 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
17 | *| SuperBlock | GroupDescTable | InodeBitmap | BlockBitmap | InodeTable | DataBlocks |
18 | *+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+
19 | * 1024 Bytes Many Block 1 Block 1 Block Many Blocks Many More Blocks
20 |
21 | */
22 |
23 | /*
24 | * GROUP_SIZE is at least 1024 bytes + 3 blocks
25 | * s: SECTOR_NUM
26 | * b: SECTORS_PER_BLOCK
27 | * g: GROUP_NUM
28 | * g = \lfloor\frac{s}{2+\lceil(32g/512/b)\rceil*b+b+b+\lceil(128*512*b/512/b)\rceil*b+512*b*b}\rfloor
29 | */
30 |
31 | /* Independent Variables */
32 | //#define SECTOR_NUM 1024 // i.e., 512 KB, as we take file to simulate hard disk, SECTOR_NUM is bounded by (2^32 / 2^9) sectors for 32-bits system
33 | //#define SECTOR_NUM 8196 // defined in disk.h
34 | //#define SECTOR_SIZE 512 // i.e., 512 B, defined in disk.h
35 | #define SECTORS_PER_BLOCK 2
36 | #define POINTER_NUM 12
37 | #define NAME_LENGTH 64
38 |
39 | /* Dependent Variables & Constants */
40 | #define BLOCK_SIZE (SECTOR_SIZE * SECTORS_PER_BLOCK)
41 |
42 | #define MAX_GROUP_NUM (SECTOR_NUM / SECTOR_SIZE / SECTORS_PER_BLOCK / 8 / SECTORS_PER_BLOCK + 1)
43 |
44 | #define SUPER_BLOCK_SIZE 1024
45 | #define GROUP_DESC_SIZE 32
46 | #define INODE_BITMAP_SIZE BLOCK_SIZE
47 | #define BLOCK_BITMAP_SIZE BLOCK_SIZE
48 | #define INODE_SIZE 128
49 | #define DIRENTRY_SIZE 128
50 |
51 | #define UNKNOWN_TYPE 0
52 | #define REGULAR_TYPE 1
53 | #define DIRECTORY_TYPE 2
54 | #define CHARACTER_TYPE 3
55 | #define BLOCK_TYPE 4
56 | #define FIFO_TYPE 5
57 | #define SOCKET_TYPE 6
58 | #define SYMBOLIC_TYPE 7
59 |
60 | union SuperBlock {
61 | uint8_t byte[SUPER_BLOCK_SIZE];
62 | struct {
63 | int32_t sectorNum; // total number of sectors
64 | int32_t inodeNum; // total number of inodes
65 | int32_t blockNum; // total number of data blocks
66 | int32_t availInodeNum; // total number of available inodes
67 | int32_t availBlockNum; // total number of available data blocks
68 | int32_t blockSize; // number of bytes in each block
69 | int32_t inodesPerGroup; // number of inodes in each group
70 | int32_t blocksPerGroup; // number of blocks in each group
71 | };
72 | };
73 |
74 | typedef union SuperBlock SuperBlock;
75 |
76 | union GroupDesc {
77 | uint8_t byte[GROUP_DESC_SIZE];
78 | struct {
79 | int32_t inodeBitmap; // sector as unit
80 | int32_t blockBitmap; // sector as unit
81 | int32_t inodeTable; // sector as unit
82 | int32_t availInodeNum;
83 | int32_t availBlockNum;
84 | };
85 | };
86 |
87 | typedef union GroupDesc GroupDesc;
88 |
89 | struct InodeBitmap {
90 | uint8_t byte[INODE_BITMAP_SIZE];
91 | };
92 |
93 | typedef struct InodeBitmap InodeBitmap;
94 |
95 | struct BlockBitmap {
96 | uint8_t byte[BLOCK_BITMAP_SIZE];
97 | };
98 |
99 | typedef struct BlockBitmap BlockBitmap;
100 |
101 | union Inode {
102 | uint8_t byte[INODE_SIZE];
103 | struct {
104 | int16_t type; // further implement privilege control, i.e., drwxrwxrwx, uid, gid, others
105 | int16_t linkCount;
106 | int32_t blockCount;
107 | int32_t size; // size of this file, byte as unit
108 | int32_t pointer[POINTER_NUM];
109 | int32_t singlyPointer;
110 | int32_t doublyPointer;
111 | int32_t triplyPointer;
112 | };
113 | };
114 |
115 | typedef union Inode Inode;
116 |
117 | union DirEntry {
118 | uint8_t byte[DIRENTRY_SIZE];
119 | struct {
120 | int32_t inode; // byte as unit? TODO inode index as unit
121 | //int32_t size; // size of this file, byte as unit
122 | //int8_t type; // may not be necessary
123 | char name[NAME_LENGTH];
124 | };
125 | };
126 |
127 | typedef union DirEntry DirEntry;
128 |
129 | #endif
130 |
--------------------------------------------------------------------------------
/lab4/lab4/kernel/include/x86/memory.h:
--------------------------------------------------------------------------------
1 | #ifndef __X86_MEMORY_H__
2 | #define __X86_MEMORY_H__
3 |
4 | #define DPL_KERN 0
5 | #define DPL_USER 3
6 |
7 | // Application segment type bits
8 | #define STA_X 0x8 // Executable segment
9 | #define STA_W 0x2 // Writeable (non-executable segments)
10 | #define STA_R 0x2 // Readable (executable segments)
11 |
12 | // System segment type bits
13 | #define STS_T32A 0x9 // Available 32-bit TSS
14 | #define STS_IG32 0xE // 32-bit Interrupt Gate
15 | #define STS_TG32 0xF // 32-bit Trap Gate
16 |
17 | // GDT entries
18 | #define NR_SEGMENTS 20 // GDT size
19 | #define SEG_KCODE 1 // Kernel code
20 | #define SEG_KDATA 2 // Kernel data/stack
21 | #define SEG_TSS (NR_SEGMENTS-1)
22 |
23 | // Selectors
24 | #define KSEL(desc) (((desc) << 3) | DPL_KERN)
25 | #define USEL(desc) (((desc) << 3) | DPL_USER)
26 |
27 | struct GateDescriptor {
28 | uint32_t offset_15_0 : 16;
29 | uint32_t segment : 16;
30 | uint32_t pad0 : 8;
31 | uint32_t type : 4;
32 | uint32_t system : 1;
33 | uint32_t privilege_level : 2;
34 | uint32_t present : 1;
35 | uint32_t offset_31_16 : 16;
36 | };
37 |
38 | struct StackFrame {
39 | uint32_t gs, fs, es, ds;
40 | uint32_t edi, esi, ebp, xxx, ebx, edx, ecx, eax;
41 | uint32_t irq, error;
42 | uint32_t eip, cs, eflags, esp, ss;
43 | };
44 |
45 | struct ListHead {
46 | struct ListHead *next;
47 | struct ListHead *prev;
48 | };
49 |
50 | #define MAX_SEM_NUM 8
51 |
52 | struct Semaphore {
53 | int state;
54 | int value;
55 | struct ListHead pcb; // link to all pcb ListHead blocked on this semaphore
56 | };
57 | typedef struct Semaphore Semaphore;
58 |
59 | #define MAX_DEV_NUM 4
60 |
61 | struct Device {
62 | int state;
63 | int value;
64 | struct ListHead pcb; // link to all pcb ListHead blocked on this device
65 | };
66 | typedef struct Device Device;
67 |
68 | #define MAX_STACK_SIZE 1024
69 | #define MAX_PCB_NUM ((NR_SEGMENTS-2)/2)
70 | //#define MAX_PCB_NUM 5
71 |
72 | #define STATE_RUNNABLE 0
73 | #define STATE_RUNNING 1
74 | #define STATE_BLOCKED 2
75 | #define STATE_DEAD 3
76 |
77 | #define MAX_TIME_COUNT 16
78 |
79 | struct ProcessTable {
80 | uint32_t stack[MAX_STACK_SIZE];
81 | struct StackFrame regs;
82 | uint32_t stackTop;
83 | uint32_t prevStackTop;
84 | int state;
85 | int timeCount;
86 | int sleepTime;
87 | uint32_t pid;
88 | char name[32];
89 | struct ListHead blocked; // sempahore, device, file blocked on
90 | };
91 | typedef struct ProcessTable ProcessTable;
92 |
93 | /*
94 | 1. The number of bits in a bit field sets the limit to the range of values it can hold
95 | 2. Multiple adjacent bit fields are usually packed together (although this behavior is implementation-defined)
96 |
97 | Refer: en.cppreference.com/w/cpp/language/bit_field
98 | */
99 | struct SegDesc {
100 | uint32_t lim_15_0 : 16; // Low bits of segment limit
101 | uint32_t base_15_0 : 16; // Low bits of segment base address
102 | uint32_t base_23_16 : 8; // Middle bits of segment base address
103 | uint32_t type : 4; // Segment type (see STS_ constants)
104 | uint32_t s : 1; // 0 = system, 1 = application
105 | uint32_t dpl : 2; // Descriptor Privilege Level
106 | uint32_t p : 1; // Present
107 | uint32_t lim_19_16 : 4; // High bits of segment limit
108 | uint32_t avl : 1; // Unused (available for software use)
109 | uint32_t rsv1 : 1; // Reserved
110 | uint32_t db : 1; // 0 = 16-bit segment, 1 = 32-bit segment
111 | uint32_t g : 1; // Granularity: limit scaled by 4K when set
112 | uint32_t base_31_24 : 8; // High bits of segment base address
113 | };
114 | typedef struct SegDesc SegDesc;
115 |
116 | #define SEG(type, base, lim, dpl) (SegDesc) \
117 | { ((lim) >> 12) & 0xffff, (uint32_t)(base) & 0xffff, \
118 | ((uint32_t)(base) >> 16) & 0xff, type, 1, dpl, 1, \
119 | (uint32_t)(lim) >> 28, 0, 0, 1, 1, (uint32_t)(base) >> 24 }
120 |
121 | #define SEG16(type, base, lim, dpl) (SegDesc) \
122 | { (lim) & 0xffff, (uint32_t)(base) & 0xffff, \
123 | ((uint32_t)(base) >> 16) & 0xff, type, 0, dpl, 1, \
124 | (uint32_t)(lim) >> 16, 0, 0, 1, 0, (uint32_t)(base) >> 24 }
125 |
126 | // Task state segment format
127 | struct TSS {
128 | uint32_t link; // old ts selector
129 | uint32_t esp0; // Ring 0 Stack pointer and segment selector
130 | uint32_t ss0; // after an increase in privilege level
131 | union{
132 | struct{
133 | char dontcare[88];
134 | };
135 | struct{
136 | uint32_t esp1,ss1,esp2,ss2;
137 | uint32_t cr3, eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
138 | uint32_t es, cs, ss, ds, fs, gs, ldt;
139 | };
140 | };
141 | };
142 | typedef struct TSS TSS;
143 |
144 | static inline void setGdt(SegDesc *gdt, uint32_t size) {
145 | volatile static uint16_t data[3];
146 | data[0] = size - 1;
147 | data[1] = (uint32_t)gdt;
148 | data[2] = (uint32_t)gdt >> 16;
149 | asm volatile("lgdt (%0)" : : "r"(data));
150 | }
151 |
152 | static inline void lLdt(uint16_t sel)
153 | {
154 | asm volatile("lldt %0" :: "r"(sel));
155 | }
156 |
157 | #endif
158 |
--------------------------------------------------------------------------------