├── .gitignore ├── README.md └── labcodes ├── autobuild.sh ├── autotest.sh ├── clangbuildall.sh ├── cleanall.sh ├── formatter.py ├── gccbuildall.sh ├── lab1 ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── init │ │ ├── init.c │ │ └── init.h │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ └── pmm.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab1.md ├── libs │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── printfmt.c │ ├── stdarg.h │ ├── stdio.h │ ├── string.c │ ├── string.h │ └── x86.h ├── pic │ ├── 7c00.png │ ├── kb.png │ └── make_debug.png ├── projchallenge-handin.tar.gz ├── readme.md └── tools │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab2 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── init │ │ ├── entry.S │ │ ├── init.c │ │ └── init.h │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── buddy_pmm.c │ │ ├── buddy_pmm.h │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ └── pmm.h │ ├── sync │ │ └── sync.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab2.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── list.h │ ├── printfmt.c │ ├── stdarg.h │ ├── stdio.h │ ├── string.c │ ├── string.h │ └── x86.h ├── pic │ ├── buddy.png │ └── equ.png ├── readme.md └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── kernel_nopage.ld │ ├── sign.c │ └── vector.c ├── lab3 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.c │ │ └── swapfs.h │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── sync │ │ └── sync.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab3.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ └── x86.h ├── pic │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── ans1.png │ ├── ans2.png │ └── ans3.png ├── readme.md └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab4 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.c │ │ └── swapfs.h │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── kmalloc.c │ │ ├── kmalloc.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── process │ │ ├── entry.S │ │ ├── proc.c │ │ ├── proc.h │ │ └── switch.S │ ├── schedule │ │ ├── sched.c │ │ └── sched.h │ ├── sync │ │ └── sync.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab4.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── hash.c │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ └── x86.h ├── readme.md └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab5 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.c │ │ └── swapfs.h │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── kmalloc.c │ │ ├── kmalloc.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── process │ │ ├── entry.S │ │ ├── proc.c │ │ ├── proc.h │ │ └── switch.S │ ├── schedule │ │ ├── sched.c │ │ └── sched.h │ ├── sync │ │ └── sync.h │ ├── syscall │ │ ├── syscall.c │ │ └── syscall.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab5.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── hash.c │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ ├── unistd.h │ └── x86.h ├── readme.md ├── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ ├── user.ld │ └── vector.c └── user │ ├── badarg.c │ ├── badsegment.c │ ├── divzero.c │ ├── exit.c │ ├── faultread.c │ ├── faultreadkernel.c │ ├── forktest.c │ ├── forktree.c │ ├── hello.c │ ├── libs │ ├── initcode.S │ ├── panic.c │ ├── stdio.c │ ├── syscall.c │ ├── syscall.h │ ├── ulib.c │ ├── ulib.h │ └── umain.c │ ├── pgdir.c │ ├── softint.c │ ├── spin.c │ ├── testbss.c │ ├── waitkill.c │ └── yield.c ├── lab6 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.c │ │ └── swapfs.h │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── kmalloc.c │ │ ├── kmalloc.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── process │ │ ├── entry.S │ │ ├── proc.c │ │ ├── proc.h │ │ └── switch.S │ ├── schedule │ │ ├── default_sched.c │ │ ├── default_sched.h │ │ ├── sched.c │ │ └── sched.h │ ├── sync │ │ └── sync.h │ ├── syscall │ │ ├── syscall.c │ │ └── syscall.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab6.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── hash.c │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── skew_heap.h │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ ├── unistd.h │ └── x86.h ├── pic │ └── prio.png ├── readme.md ├── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ ├── user.ld │ └── vector.c └── user │ ├── badarg.c │ ├── badsegment.c │ ├── divzero.c │ ├── exit.c │ ├── faultread.c │ ├── faultreadkernel.c │ ├── forktest.c │ ├── forktree.c │ ├── hello.c │ ├── libs │ ├── initcode.S │ ├── panic.c │ ├── stdio.c │ ├── syscall.c │ ├── syscall.h │ ├── ulib.c │ ├── ulib.h │ └── umain.c │ ├── matrix.c │ ├── pgdir.c │ ├── priority.c │ ├── softint.c │ ├── spin.c │ ├── testbss.c │ ├── waitkill.c │ └── yield.c ├── lab7 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.c │ │ └── swapfs.h │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── kmalloc.c │ │ ├── kmalloc.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── process │ │ ├── entry.S │ │ ├── proc.c │ │ ├── proc.h │ │ └── switch.S │ ├── schedule │ │ ├── default_sched.c │ │ ├── default_sched.h │ │ ├── default_sched_stride_c │ │ ├── sched.c │ │ └── sched.h │ ├── sync │ │ ├── check_sync.c │ │ ├── monitor.c │ │ ├── monitor.h │ │ ├── sem.c │ │ ├── sem.h │ │ ├── sync.h │ │ ├── wait.c │ │ └── wait.h │ ├── syscall │ │ ├── syscall.c │ │ └── syscall.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab7.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── hash.c │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── skew_heap.h │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ ├── unistd.h │ └── x86.h ├── readme.md ├── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ ├── user.ld │ └── vector.c └── user │ ├── badarg.c │ ├── badsegment.c │ ├── divzero.c │ ├── exit.c │ ├── faultread.c │ ├── faultreadkernel.c │ ├── forktest.c │ ├── forktree.c │ ├── hello.c │ ├── libs │ ├── initcode.S │ ├── panic.c │ ├── stdio.c │ ├── syscall.c │ ├── syscall.h │ ├── ulib.c │ ├── ulib.h │ └── umain.c │ ├── matrix.c │ ├── pgdir.c │ ├── priority.c │ ├── sleep.c │ ├── sleepkill.c │ ├── softint.c │ ├── spin.c │ ├── testbss.c │ ├── waitkill.c │ └── yield.c ├── lab8 ├── .projectile ├── Makefile ├── boot │ ├── asm.h │ ├── bootasm.S │ └── bootmain.c ├── kern │ ├── debug │ │ ├── assert.h │ │ ├── kdebug.c │ │ ├── kdebug.h │ │ ├── kmonitor.c │ │ ├── kmonitor.h │ │ ├── panic.c │ │ └── stab.h │ ├── driver │ │ ├── clock.c │ │ ├── clock.h │ │ ├── console.c │ │ ├── console.h │ │ ├── ide.c │ │ ├── ide.h │ │ ├── intr.c │ │ ├── intr.h │ │ ├── kbdreg.h │ │ ├── picirq.c │ │ └── picirq.h │ ├── fs │ │ ├── devs │ │ │ ├── dev.c │ │ │ ├── dev.h │ │ │ ├── dev_disk0.c │ │ │ ├── dev_stdin.c │ │ │ └── dev_stdout.c │ │ ├── file.c │ │ ├── file.h │ │ ├── fs.c │ │ ├── fs.h │ │ ├── iobuf.c │ │ ├── iobuf.h │ │ ├── sfs │ │ │ ├── bitmap.c │ │ │ ├── bitmap.h │ │ │ ├── sfs.c │ │ │ ├── sfs.h │ │ │ ├── sfs_fs.c │ │ │ ├── sfs_inode.c │ │ │ ├── sfs_io.c │ │ │ └── sfs_lock.c │ │ ├── swap │ │ │ ├── swapfs.c │ │ │ └── swapfs.h │ │ ├── sysfile.c │ │ ├── sysfile.h │ │ └── vfs │ │ │ ├── inode.c │ │ │ ├── inode.h │ │ │ ├── vfs.c │ │ │ ├── vfs.h │ │ │ ├── vfsdev.c │ │ │ ├── vfsfile.c │ │ │ ├── vfslookup.c │ │ │ └── vfspath.c │ ├── init │ │ ├── entry.S │ │ └── init.c │ ├── libs │ │ ├── readline.c │ │ ├── stdio.c │ │ └── string.c │ ├── mm │ │ ├── default_pmm.c │ │ ├── default_pmm.h │ │ ├── kmalloc.c │ │ ├── kmalloc.h │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── swap.c │ │ ├── swap.h │ │ ├── swap_fifo.c │ │ ├── swap_fifo.h │ │ ├── vmm.c │ │ └── vmm.h │ ├── process │ │ ├── entry.S │ │ ├── proc.c │ │ ├── proc.h │ │ └── switch.S │ ├── schedule │ │ ├── default_sched.c │ │ ├── default_sched.h │ │ ├── default_sched_stride_c │ │ ├── sched.c │ │ └── sched.h │ ├── sync │ │ ├── check_sync.c │ │ ├── monitor.c │ │ ├── monitor.h │ │ ├── sem.c │ │ ├── sem.h │ │ ├── sync.h │ │ ├── wait.c │ │ └── wait.h │ ├── syscall │ │ ├── syscall.c │ │ └── syscall.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── lab8.md ├── libs │ ├── atomic.h │ ├── defs.h │ ├── dirent.h │ ├── elf.h │ ├── error.h │ ├── hash.c │ ├── list.h │ ├── printfmt.c │ ├── rand.c │ ├── skew_heap.h │ ├── stat.h │ ├── stdarg.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.c │ ├── string.h │ ├── unistd.h │ └── x86.h ├── readme.md ├── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── mksfs.c │ ├── sign.c │ ├── user.ld │ └── vector.c └── user │ ├── badarg.c │ ├── badsegment.c │ ├── divzero.c │ ├── exit.c │ ├── faultread.c │ ├── faultreadkernel.c │ ├── forktest.c │ ├── forktree.c │ ├── hello.c │ ├── libs │ ├── dir.c │ ├── dir.h │ ├── file.c │ ├── file.h │ ├── initcode.S │ ├── lock.h │ ├── panic.c │ ├── stdio.c │ ├── syscall.c │ ├── syscall.h │ ├── ulib.c │ ├── ulib.h │ └── umain.c │ ├── ls.c │ ├── matrix.c │ ├── pgdir.c │ ├── priority.c │ ├── sfs_filetest1.c │ ├── sh.c │ ├── sleep.c │ ├── sleepkill.c │ ├── softint.c │ ├── spin.c │ ├── testbss.c │ ├── waitkill.c │ └── yield.c └── tools └── split_score_log.py /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | obj/ 3 | bin/ 4 | .gdb.in 5 | .qemu.out 6 | cscope.* 7 | ncscope.* 8 | tags 9 | *swp 10 | .cproject 11 | .project 12 | *.bak 13 | *.org 14 | .codelite 15 | *.project 16 | *.workspace 17 | a.out 18 | *.log 19 | *.exe 20 | chytesting 21 | disk0 22 | *.orig 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # os2019-Trinkle23897 -------------------------------------------------------------------------------- /labcodes/autobuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Nothing to be done here 4 | exit 0 5 | -------------------------------------------------------------------------------- /labcodes/clangbuildall.sh: -------------------------------------------------------------------------------- 1 | cd lab1; make clean; USELLVM=1 make; cd .. 2 | cd lab2; make clean; USELLVM=1 make; cd .. 3 | cd lab3; make clean; USELLVM=1 make; cd .. 4 | cd lab4; make clean; USELLVM=1 make; cd .. 5 | cd lab5; make clean; USELLVM=1 make; cd .. 6 | cd lab6; make clean; USELLVM=1 make; cd .. 7 | cd lab7; make clean; USELLVM=1 make; cd .. 8 | cd lab8; make clean; USELLVM=1 make; cd .. 9 | -------------------------------------------------------------------------------- /labcodes/cleanall.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | cd lab1; make clean; cd .. 3 | cd lab2; make clean; cd .. 4 | cd lab3; make clean; cd .. 5 | cd lab4; make clean; cd .. 6 | cd lab5; make clean; cd .. 7 | cd lab6; make clean; cd .. 8 | cd lab7; make clean; cd .. 9 | cd lab8; make clean; cd .. 10 | -------------------------------------------------------------------------------- /labcodes/gccbuildall.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | cd lab1; make clean; make; cd .. 3 | cd lab2; make clean; make; cd .. 4 | cd lab3; make clean; make; cd .. 5 | cd lab4; make clean; make; cd .. 6 | cd lab5; make clean; make; cd .. 7 | cd lab6; make clean; make; cd .. 8 | cd lab7; make clean; make; cd .. 9 | cd lab8; make clean; make; cd .. 10 | -------------------------------------------------------------------------------- /labcodes/lab1/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | 6 | void print_kerninfo(void); 7 | void print_stackframe(void); 8 | void print_debuginfo(uintptr_t eip); 9 | 10 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | 12 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 13 | 14 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/debug/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static bool is_panic = 0; 7 | 8 | /* * 9 | * __panic - __panic is called on unresolvable fatal errors. it prints 10 | * "panic: 'message'", and then enters the kernel monitor. 11 | * */ 12 | void 13 | __panic(const char *file, int line, const char *fmt, ...) { 14 | if (is_panic) { 15 | goto panic_dead; 16 | } 17 | is_panic = 1; 18 | 19 | // print the 'message' 20 | va_list ap; 21 | va_start(ap, fmt); 22 | cprintf("kernel panic at %s:%d:\n ", file, line); 23 | vcprintf(fmt, ap); 24 | cprintf("\n"); 25 | 26 | cprintf("stack trackback:\n"); 27 | print_stackframe(); 28 | 29 | va_end(ap); 30 | 31 | panic_dead: 32 | intr_disable(); 33 | while (1) { 34 | kmonitor(NULL); 35 | } 36 | } 37 | 38 | /* __warn - like panic, but don't */ 39 | void 40 | __warn(const char *file, int line, const char *fmt, ...) { 41 | va_list ap; 42 | va_start(ap, fmt); 43 | cprintf("kernel warning at %s:%d:\n ", file, line); 44 | vcprintf(fmt, ap); 45 | cprintf("\n"); 46 | va_end(ap); 47 | } 48 | 49 | bool 50 | is_kernel_panic(void) { 51 | return is_panic; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/init/init.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | void lab1_print_cur_status(void); 13 | void lab1_switch_to_user(void); 14 | void lab1_switch_to_kernel(void); 15 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/mm/memlayout.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_MEMLAYOUT_H__ 2 | #define __KERN_MM_MEMLAYOUT_H__ 3 | 4 | /* This file contains the definitions for memory management in our OS. */ 5 | 6 | /* global segment number */ 7 | #define SEG_KTEXT 1 8 | #define SEG_KDATA 2 9 | #define SEG_UTEXT 3 10 | #define SEG_UDATA 4 11 | #define SEG_TSS 5 12 | 13 | /* global descriptor numbers */ 14 | #define GD_KTEXT ((SEG_KTEXT) << 3) // kernel text 15 | #define GD_KDATA ((SEG_KDATA) << 3) // kernel data 16 | #define GD_UTEXT ((SEG_UTEXT) << 3) // user text 17 | #define GD_UDATA ((SEG_UDATA) << 3) // user data 18 | #define GD_TSS ((SEG_TSS) << 3) // task segment selector 19 | 20 | #define DPL_KERNEL (0) 21 | #define DPL_USER (3) 22 | 23 | #define KERNEL_CS ((GD_KTEXT) | DPL_KERNEL) 24 | #define KERNEL_DS ((GD_KDATA) | DPL_KERNEL) 25 | #define USER_CS ((GD_UTEXT) | DPL_USER) 26 | #define USER_DS ((GD_UDATA) | DPL_USER) 27 | 28 | #endif /* !__KERN_MM_MEMLAYOUT_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/mm/pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_PMM_H__ 2 | #define __KERN_MM_PMM_H__ 3 | 4 | void pmm_init(void); 5 | 6 | #endif /* !__KERN_MM_PMM_H__ */ 7 | 8 | -------------------------------------------------------------------------------- /labcodes/lab1/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | -------------------------------------------------------------------------------- /labcodes/lab1/libs/error.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_ERROR_H__ 2 | #define __LIBS_ERROR_H__ 3 | 4 | /* kernel error codes -- keep in sync with list in lib/printfmt.c */ 5 | #define E_UNSPECIFIED 1 // Unspecified or unknown problem 6 | #define E_BAD_PROC 2 // Process doesn't exist or otherwise 7 | #define E_INVAL 3 // Invalid parameter 8 | #define E_NO_MEM 4 // Request failed due to memory shortage 9 | #define E_NO_FREE_PROC 5 // Attempt to create a new process beyond 10 | #define E_FAULT 6 // Memory fault 11 | 12 | /* the maximum allowed */ 13 | #define MAXERROR 6 14 | 15 | #endif /* !__LIBS_ERROR_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab1/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab1/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab1/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab1/pic/7c00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab1/pic/7c00.png -------------------------------------------------------------------------------- /labcodes/lab1/pic/kb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab1/pic/kb.png -------------------------------------------------------------------------------- /labcodes/lab1/pic/make_debug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab1/pic/make_debug.png -------------------------------------------------------------------------------- /labcodes/lab1/projchallenge-handin.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab1/projchallenge-handin.tar.gz -------------------------------------------------------------------------------- /labcodes/lab1/readme.md: -------------------------------------------------------------------------------- 1 | lab1.md -------------------------------------------------------------------------------- /labcodes/lab1/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | -------------------------------------------------------------------------------- /labcodes/lab1/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab2/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab2/.projectile -------------------------------------------------------------------------------- /labcodes/lab2/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/init/init.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | void lab1_print_cur_status(void); 13 | void lab1_switch_to_user(void); 14 | void lab1_switch_to_kernel(void); 15 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/mm/buddy_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_BUDDY_PMM_H__ 2 | #define __KERN_MM_BUDDY_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager buddy_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_BUDDY_PMM_H__ */ 9 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | static inline bool 9 | __intr_save(void) { 10 | if (read_eflags() & FL_IF) { 11 | intr_disable(); 12 | return 1; 13 | } 14 | return 0; 15 | } 16 | 17 | static inline void 18 | __intr_restore(bool flag) { 19 | if (flag) { 20 | intr_enable(); 21 | } 22 | } 23 | 24 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 25 | #define local_intr_restore(x) __intr_restore(x); 26 | 27 | #endif /* !__KERN_SYNC_SYNC_H__ */ 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab2/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | -------------------------------------------------------------------------------- /labcodes/lab2/libs/error.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_ERROR_H__ 2 | #define __LIBS_ERROR_H__ 3 | 4 | /* kernel error codes -- keep in sync with list in lib/printfmt.c */ 5 | #define E_UNSPECIFIED 1 // Unspecified or unknown problem 6 | #define E_BAD_PROC 2 // Process doesn't exist or otherwise 7 | #define E_INVAL 3 // Invalid parameter 8 | #define E_NO_MEM 4 // Request failed due to memory shortage 9 | #define E_NO_FREE_PROC 5 // Attempt to create a new process beyond 10 | #define E_FAULT 6 // Memory fault 11 | 12 | /* the maximum allowed */ 13 | #define MAXERROR 6 14 | 15 | #endif /* !__LIBS_ERROR_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab2/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab2/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab2/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab2/pic/buddy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab2/pic/buddy.png -------------------------------------------------------------------------------- /labcodes/lab2/pic/equ.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab2/pic/equ.png -------------------------------------------------------------------------------- /labcodes/lab2/readme.md: -------------------------------------------------------------------------------- 1 | lab2.md -------------------------------------------------------------------------------- /labcodes/lab2/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab2/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab2/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab3/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/.projectile -------------------------------------------------------------------------------- /labcodes/lab3/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/fs/fs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_FS_H__ 2 | #define __KERN_FS_FS_H__ 3 | 4 | #include 5 | 6 | #define SECTSIZE 512 7 | #define PAGE_NSECT (PGSIZE / SECTSIZE) 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/fs/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAPFS_H__ 2 | #define __KERN_FS_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | static inline bool 9 | __intr_save(void) { 10 | if (read_eflags() & FL_IF) { 11 | intr_disable(); 12 | return 1; 13 | } 14 | return 0; 15 | } 16 | 17 | static inline void 18 | __intr_restore(bool flag) { 19 | if (flag) { 20 | intr_enable(); 21 | } 22 | } 23 | 24 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 25 | #define local_intr_restore(x) __intr_restore(x); 26 | 27 | #endif /* !__KERN_SYNC_SYNC_H__ */ 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab3/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/error.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_ERROR_H__ 2 | #define __LIBS_ERROR_H__ 3 | 4 | /* kernel error codes -- keep in sync with list in lib/printfmt.c */ 5 | #define E_UNSPECIFIED 1 // Unspecified or unknown problem 6 | #define E_BAD_PROC 2 // Process doesn't exist or otherwise 7 | #define E_INVAL 3 // Invalid parameter 8 | #define E_NO_MEM 4 // Request failed due to memory shortage 9 | #define E_NO_FREE_PROC 5 // Attempt to create a new process beyond 10 | #define E_FAULT 6 // Memory fault 11 | 12 | /* the maximum allowed */ 13 | #define MAXERROR 6 14 | 15 | #endif /* !__LIBS_ERROR_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | /* the largest number rand will return */ 5 | #define RAND_MAX 2147483647UL 6 | 7 | /* libs/rand.c */ 8 | int rand(void); 9 | void srand(unsigned int seed); 10 | 11 | #endif /* !__LIBS_RAND_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab3/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab3/pic/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/1.png -------------------------------------------------------------------------------- /labcodes/lab3/pic/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/2.png -------------------------------------------------------------------------------- /labcodes/lab3/pic/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/3.png -------------------------------------------------------------------------------- /labcodes/lab3/pic/ans1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/ans1.png -------------------------------------------------------------------------------- /labcodes/lab3/pic/ans2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/ans2.png -------------------------------------------------------------------------------- /labcodes/lab3/pic/ans3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab3/pic/ans3.png -------------------------------------------------------------------------------- /labcodes/lab3/readme.md: -------------------------------------------------------------------------------- 1 | lab3.md -------------------------------------------------------------------------------- /labcodes/lab3/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab3/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab3/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab4/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab4/.projectile -------------------------------------------------------------------------------- /labcodes/lab4/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/fs/fs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_FS_H__ 2 | #define __KERN_FS_FS_H__ 3 | 4 | #include 5 | 6 | #define SECTSIZE 512 7 | #define PAGE_NSECT (PGSIZE / SECTSIZE) 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/fs/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAPFS_H__ 2 | #define __KERN_FS_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_KMALLOC_H__ 2 | #define __KERN_MM_KMALLOC_H__ 3 | 4 | #include 5 | 6 | #define KMALLOC_MAX_ORDER 10 7 | 8 | void kmalloc_init(void); 9 | 10 | void *kmalloc(size_t n); 11 | void kfree(void *objp); 12 | 13 | #endif /* !__KERN_MM_KMALLOC_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | 5 | pushl %edx # push arg 6 | call *%ebx # call fn 7 | 8 | pushl %eax # save the return value of fn(arg) 9 | call do_exit # call do_exit to terminate current thread 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/schedule/sched.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void 8 | wakeup_proc(struct proc_struct *proc) { 9 | assert(proc->state != PROC_ZOMBIE && proc->state != PROC_RUNNABLE); 10 | proc->state = PROC_RUNNABLE; 11 | } 12 | 13 | void 14 | schedule(void) { 15 | bool intr_flag; 16 | list_entry_t *le, *last; 17 | struct proc_struct *next = NULL; 18 | local_intr_save(intr_flag); 19 | { 20 | current->need_resched = 0; 21 | last = (current == idleproc) ? &proc_list : &(current->list_link); 22 | le = last; 23 | do { 24 | if ((le = list_next(le)) != &proc_list) { 25 | next = le2proc(le, list_link); 26 | if (next->state == PROC_RUNNABLE) { 27 | break; 28 | } 29 | } 30 | } while (le != last); 31 | if (next == NULL || next->state != PROC_RUNNABLE) { 32 | next = idleproc; 33 | } 34 | next->runs ++; 35 | if (next != current) { 36 | proc_run(next); 37 | } 38 | } 39 | local_intr_restore(intr_flag); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/schedule/sched.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SCHEDULE_SCHED_H__ 2 | #define __KERN_SCHEDULE_SCHED_H__ 3 | 4 | #include 5 | 6 | void schedule(void); 7 | void wakeup_proc(struct proc_struct *proc); 8 | 9 | #endif /* !__KERN_SCHEDULE_SCHED_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | static inline bool 9 | __intr_save(void) { 10 | if (read_eflags() & FL_IF) { 11 | intr_disable(); 12 | return 1; 13 | } 14 | return 0; 15 | } 16 | 17 | static inline void 18 | __intr_restore(bool flag) { 19 | if (flag) { 20 | intr_enable(); 21 | } 22 | } 23 | 24 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 25 | #define local_intr_restore(x) __intr_restore(x); 26 | 27 | #endif /* !__KERN_SYNC_SYNC_H__ */ 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab4/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | .globl forkrets 46 | forkrets: 47 | # set stack to this new process's trapframe 48 | movl 4(%esp), %esp 49 | jmp __trapret 50 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/error.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_ERROR_H__ 2 | #define __LIBS_ERROR_H__ 3 | 4 | /* kernel error codes -- keep in sync with list in lib/printfmt.c */ 5 | #define E_UNSPECIFIED 1 // Unspecified or unknown problem 6 | #define E_BAD_PROC 2 // Process doesn't exist or otherwise 7 | #define E_INVAL 3 // Invalid parameter 8 | #define E_NO_MEM 4 // Request failed due to memory shortage 9 | #define E_NO_FREE_PROC 5 // Attempt to create a new process beyond 10 | #define E_FAULT 6 // Memory fault 11 | 12 | /* the maximum allowed */ 13 | #define MAXERROR 6 14 | 15 | #endif /* !__LIBS_ERROR_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 4 | #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 5 | 6 | /* * 7 | * hash32 - generate a hash value in the range [0, 2^@bits - 1] 8 | * @val: the input value 9 | * @bits: the number of bits in a return value 10 | * 11 | * High bits are more random, so we use them. 12 | * */ 13 | uint32_t 14 | hash32(uint32_t val, unsigned int bits) { 15 | uint32_t hash = val * GOLDEN_RATIO_PRIME_32; 16 | return (hash >> (32 - bits)); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | #include 5 | 6 | /* the largest number rand will return */ 7 | #define RAND_MAX 2147483647UL 8 | 9 | /* libs/rand.c */ 10 | int rand(void); 11 | void srand(unsigned int seed); 12 | 13 | /* libs/hash.c */ 14 | uint32_t hash32(uint32_t val, unsigned int bits); 15 | 16 | #endif /* !__LIBS_RAND_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab4/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab4/readme.md: -------------------------------------------------------------------------------- 1 | lab4.md -------------------------------------------------------------------------------- /labcodes/lab4/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab4/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab4/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab5/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab5/.projectile -------------------------------------------------------------------------------- /labcodes/lab5/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/fs/fs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_FS_H__ 2 | #define __KERN_FS_FS_H__ 3 | 4 | #include 5 | 6 | #define SECTSIZE 512 7 | #define PAGE_NSECT (PGSIZE / SECTSIZE) 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/fs/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAPFS_H__ 2 | #define __KERN_FS_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | extern free_area_t free_area; 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_KMALLOC_H__ 2 | #define __KERN_MM_KMALLOC_H__ 3 | 4 | #include 5 | 6 | #define KMALLOC_MAX_ORDER 10 7 | 8 | void kmalloc_init(void); 9 | 10 | void *kmalloc(size_t n); 11 | void kfree(void *objp); 12 | 13 | size_t kallocated(void); 14 | 15 | #endif /* !__KERN_MM_KMALLOC_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | 5 | pushl %edx # push arg 6 | call *%ebx # call fn 7 | 8 | pushl %eax # save the return value of fn(arg) 9 | call do_exit # call do_exit to terminate current thread 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/process/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl switch_to 3 | switch_to: # switch_to(from, to) 4 | 5 | # save from's registers 6 | movl 4(%esp), %eax # eax points to from 7 | popl 0(%eax) # save eip !popl 8 | movl %esp, 4(%eax) 9 | movl %ebx, 8(%eax) 10 | movl %ecx, 12(%eax) 11 | movl %edx, 16(%eax) 12 | movl %esi, 20(%eax) 13 | movl %edi, 24(%eax) 14 | movl %ebp, 28(%eax) 15 | 16 | # restore to's registers 17 | movl 4(%esp), %eax # not 8(%esp): popped return address already 18 | # eax now points to to 19 | movl 28(%eax), %ebp 20 | movl 24(%eax), %edi 21 | movl 20(%eax), %esi 22 | movl 16(%eax), %edx 23 | movl 12(%eax), %ecx 24 | movl 8(%eax), %ebx 25 | movl 4(%eax), %esp 26 | 27 | pushl 0(%eax) # push eip 28 | 29 | ret 30 | 31 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/schedule/sched.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SCHEDULE_SCHED_H__ 2 | #define __KERN_SCHEDULE_SCHED_H__ 3 | 4 | #include 5 | 6 | void schedule(void); 7 | void wakeup_proc(struct proc_struct *proc); 8 | 9 | #endif /* !__KERN_SCHEDULE_SCHED_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static inline bool 12 | __intr_save(void) { 13 | if (read_eflags() & FL_IF) { 14 | intr_disable(); 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | 20 | static inline void 21 | __intr_restore(bool flag) { 22 | if (flag) { 23 | intr_enable(); 24 | } 25 | } 26 | 27 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 28 | #define local_intr_restore(x) __intr_restore(x); 29 | 30 | typedef volatile bool lock_t; 31 | 32 | static inline void 33 | lock_init(lock_t *lock) { 34 | *lock = 0; 35 | } 36 | 37 | static inline bool 38 | try_lock(lock_t *lock) { 39 | return !test_and_set_bit(0, lock); 40 | } 41 | 42 | static inline void 43 | lock(lock_t *lock) { 44 | while (!try_lock(lock)) { 45 | schedule(); 46 | } 47 | } 48 | 49 | static inline void 50 | unlock(lock_t *lock) { 51 | if (!test_and_clear_bit(0, lock)) { 52 | panic("Unlock failed.\n"); 53 | } 54 | } 55 | 56 | #endif /* !__KERN_SYNC_SYNC_H__ */ 57 | 58 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/syscall/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYSCALL_SYSCALL_H__ 2 | #define __KERN_SYSCALL_SYSCALL_H__ 3 | 4 | void syscall(void); 5 | 6 | #endif /* !__KERN_SYSCALL_SYSCALL_H__ */ 7 | 8 | -------------------------------------------------------------------------------- /labcodes/lab5/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | .globl forkrets 46 | forkrets: 47 | # set stack to this new process's trapframe 48 | movl 4(%esp), %esp 49 | jmp __trapret 50 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 4 | #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 5 | 6 | /* * 7 | * hash32 - generate a hash value in the range [0, 2^@bits - 1] 8 | * @val: the input value 9 | * @bits: the number of bits in a return value 10 | * 11 | * High bits are more random, so we use them. 12 | * */ 13 | uint32_t 14 | hash32(uint32_t val, unsigned int bits) { 15 | uint32_t hash = val * GOLDEN_RATIO_PRIME_32; 16 | return (hash >> (32 - bits)); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | #include 5 | 6 | /* the largest number rand will return */ 7 | #define RAND_MAX 2147483647UL 8 | 9 | /* libs/rand.c */ 10 | int rand(void); 11 | void srand(unsigned int seed); 12 | 13 | /* libs/hash.c */ 14 | uint32_t hash32(uint32_t val, unsigned int bits); 15 | 16 | #endif /* !__LIBS_RAND_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab5/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | 24 | /* SYS_fork flags */ 25 | #define CLONE_VM 0x00000100 // set if VM shared between processes 26 | #define CLONE_THREAD 0x00000200 // thread group 27 | 28 | #endif /* !__LIBS_UNISTD_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab5/readme.md: -------------------------------------------------------------------------------- 1 | lab5.md -------------------------------------------------------------------------------- /labcodes/lab5/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab5/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab5/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab5/user/badarg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, exit_code; 7 | if ((pid = fork()) == 0) { 8 | cprintf("fork ok.\n"); 9 | int i; 10 | for (i = 0; i < 10; i ++) { 11 | yield(); 12 | } 13 | exit(0xbeaf); 14 | } 15 | assert(pid > 0); 16 | assert(waitpid(-1, NULL) != 0); 17 | assert(waitpid(pid, (void *)0xC0000000) != 0); 18 | assert(waitpid(pid, &exit_code) == 0 && exit_code == 0xbeaf); 19 | cprintf("badarg pass.\n"); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /labcodes/lab5/user/badsegment.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* try to load the kernel's TSS selector into the DS register */ 5 | 6 | int 7 | main(void) { 8 | asm volatile("movw $0x28,%ax; movw %ax,%ds"); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/user/divzero.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int zero; 5 | 6 | int 7 | main(void) { 8 | cprintf("value is %d.\n", 1 / zero); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab5/user/faultread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %8x from 0.\n", *(unsigned int *)0); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab5/user/faultreadkernel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %08x from 0xfac00000!\n", *(unsigned *)0xfac00000); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab5/user/forktest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int max_child = 32; 5 | 6 | int 7 | main(void) { 8 | int n, pid; 9 | for (n = 0; n < max_child; n ++) { 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am child %d\n", n); 12 | exit(0); 13 | } 14 | assert(pid > 0); 15 | } 16 | 17 | if (n > max_child) { 18 | panic("fork claimed to work %d times!\n", n); 19 | } 20 | 21 | for (; n > 0; n --) { 22 | if (wait() != 0) { 23 | panic("wait stopped early\n"); 24 | } 25 | } 26 | 27 | if (wait() == 0) { 28 | panic("wait got too many\n"); 29 | } 30 | 31 | cprintf("forktest pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab5/user/forktree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEPTH 4 6 | 7 | void forktree(const char *cur); 8 | 9 | void 10 | forkchild(const char *cur, char branch) { 11 | char nxt[DEPTH + 1]; 12 | 13 | if (strlen(cur) >= DEPTH) 14 | return; 15 | 16 | snprintf(nxt, DEPTH + 1, "%s%c", cur, branch); 17 | if (fork() == 0) { 18 | forktree(nxt); 19 | yield(); 20 | exit(0); 21 | } 22 | } 23 | 24 | void 25 | forktree(const char *cur) { 26 | cprintf("%04x: I am '%s'\n", getpid(), cur); 27 | 28 | forkchild(cur, '0'); 29 | forkchild(cur, '1'); 30 | } 31 | 32 | int 33 | main(void) { 34 | forktree(""); 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /labcodes/lab5/user/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("Hello world!!.\n"); 7 | cprintf("I am process %d.\n", getpid()); 8 | cprintf("hello pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/user/libs/initcode.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl _start 3 | _start: 4 | # set ebp for backtrace 5 | movl $0x0, %ebp 6 | 7 | # move down the esp register 8 | # since it may cause page fault in backtrace 9 | subl $0x20, %esp 10 | 11 | # call user-program function 12 | call umain 13 | 1: jmp 1b 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab5/user/libs/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void 8 | __panic(const char *file, int line, const char *fmt, ...) { 9 | // print the 'message' 10 | va_list ap; 11 | va_start(ap, fmt); 12 | cprintf("user panic at %s:%d:\n ", file, line); 13 | vcprintf(fmt, ap); 14 | cprintf("\n"); 15 | va_end(ap); 16 | exit(-E_PANIC); 17 | } 18 | 19 | void 20 | __warn(const char *file, int line, const char *fmt, ...) { 21 | va_list ap; 22 | va_start(ap, fmt); 23 | cprintf("user warning at %s:%d:\n ", file, line); 24 | vcprintf(fmt, ap); 25 | cprintf("\n"); 26 | va_end(ap); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab5/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int error_code); 5 | int sys_fork(void); 6 | int sys_wait(int pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int pid); 9 | int sys_getpid(void); 10 | int sys_putc(int c); 11 | int sys_pgdir(void); 12 | 13 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab5/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /labcodes/lab5/user/libs/umain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void); 4 | 5 | void 6 | umain(void) { 7 | int ret = main(); 8 | exit(ret); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab5/user/pgdir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I am %d, print pgdir.\n", getpid()); 7 | print_pgdir(); 8 | cprintf("pgdir pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab5/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | asm volatile("int $14"); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab5/user/spin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, ret; 7 | cprintf("I am the parent. Forking the child...\n"); 8 | if ((pid = fork()) == 0) { 9 | cprintf("I am the child. spinning ...\n"); 10 | while (1); 11 | } 12 | cprintf("I am the parent. Running the child...\n"); 13 | 14 | yield(); 15 | yield(); 16 | yield(); 17 | 18 | cprintf("I am the parent. Killing the child...\n"); 19 | 20 | assert((ret = kill(pid)) == 0); 21 | cprintf("kill returns %d\n", ret); 22 | 23 | assert((ret = waitpid(pid, NULL)) == 0); 24 | cprintf("wait returns %d\n", ret); 25 | 26 | cprintf("spin may pass.\n"); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab5/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /labcodes/lab5/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /labcodes/lab5/user/yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int i; 7 | cprintf("Hello, I am process %d.\n", getpid()); 8 | for (i = 0; i < 5; i ++) { 9 | yield(); 10 | cprintf("Back in process %d, iteration %d.\n", getpid(), i); 11 | } 12 | cprintf("All done in process %d.\n", getpid()); 13 | cprintf("yield pass.\n"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab6/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab6/.projectile -------------------------------------------------------------------------------- /labcodes/lab6/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/fs/fs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_FS_H__ 2 | #define __KERN_FS_FS_H__ 3 | 4 | #include 5 | 6 | #define SECTSIZE 512 7 | #define PAGE_NSECT (PGSIZE / SECTSIZE) 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/fs/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAPFS_H__ 2 | #define __KERN_FS_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | extern free_area_t free_area; 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_KMALLOC_H__ 2 | #define __KERN_MM_KMALLOC_H__ 3 | 4 | #include 5 | 6 | #define KMALLOC_MAX_ORDER 10 7 | 8 | void kmalloc_init(void); 9 | 10 | void *kmalloc(size_t n); 11 | void kfree(void *objp); 12 | 13 | size_t kallocated(void); 14 | 15 | #endif /* !__KERN_MM_KMALLOC_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | 5 | pushl %edx # push arg 6 | call *%ebx # call fn 7 | 8 | pushl %eax # save the return value of fn(arg) 9 | call do_exit # call do_exit to terminate current thread 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/process/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl switch_to 3 | switch_to: # switch_to(from, to) 4 | 5 | # save from's registers 6 | movl 4(%esp), %eax # eax points to from 7 | popl 0(%eax) # save eip !popl 8 | movl %esp, 4(%eax) 9 | movl %ebx, 8(%eax) 10 | movl %ecx, 12(%eax) 11 | movl %edx, 16(%eax) 12 | movl %esi, 20(%eax) 13 | movl %edi, 24(%eax) 14 | movl %ebp, 28(%eax) 15 | 16 | # restore to's registers 17 | movl 4(%esp), %eax # not 8(%esp): popped return address already 18 | # eax now points to to 19 | movl 28(%eax), %ebp 20 | movl 24(%eax), %edi 21 | movl 20(%eax), %esi 22 | movl 16(%eax), %edx 23 | movl 12(%eax), %ecx 24 | movl 8(%eax), %ebx 25 | movl 4(%eax), %esp 26 | 27 | pushl 0(%eax) # push eip 28 | 29 | ret 30 | 31 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/schedule/default_sched.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SCHEDULE_SCHED_RR_H__ 2 | #define __KERN_SCHEDULE_SCHED_RR_H__ 3 | 4 | #include 5 | 6 | extern struct sched_class default_sched_class; 7 | 8 | #endif /* !__KERN_SCHEDULE_SCHED_RR_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static inline bool 12 | __intr_save(void) { 13 | if (read_eflags() & FL_IF) { 14 | intr_disable(); 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | 20 | static inline void 21 | __intr_restore(bool flag) { 22 | if (flag) { 23 | intr_enable(); 24 | } 25 | } 26 | 27 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 28 | #define local_intr_restore(x) __intr_restore(x); 29 | 30 | typedef volatile bool lock_t; 31 | 32 | static inline void 33 | lock_init(lock_t *lock) { 34 | *lock = 0; 35 | } 36 | 37 | static inline bool 38 | try_lock(lock_t *lock) { 39 | return !test_and_set_bit(0, lock); 40 | } 41 | 42 | static inline void 43 | lock(lock_t *lock) { 44 | while (!try_lock(lock)) { 45 | schedule(); 46 | } 47 | } 48 | 49 | static inline void 50 | unlock(lock_t *lock) { 51 | if (!test_and_clear_bit(0, lock)) { 52 | panic("Unlock failed.\n"); 53 | } 54 | } 55 | 56 | #endif /* !__KERN_SYNC_SYNC_H__ */ 57 | 58 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/syscall/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYSCALL_SYSCALL_H__ 2 | #define __KERN_SYSCALL_SYSCALL_H__ 3 | 4 | void syscall(void); 5 | 6 | #endif /* !__KERN_SYSCALL_SYSCALL_H__ */ 7 | 8 | -------------------------------------------------------------------------------- /labcodes/lab6/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | .globl forkrets 46 | forkrets: 47 | # set stack to this new process's trapframe 48 | movl 4(%esp), %esp 49 | jmp __trapret 50 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 4 | #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 5 | 6 | /* * 7 | * hash32 - generate a hash value in the range [0, 2^@bits - 1] 8 | * @val: the input value 9 | * @bits: the number of bits in a return value 10 | * 11 | * High bits are more random, so we use them. 12 | * */ 13 | uint32_t 14 | hash32(uint32_t val, unsigned int bits) { 15 | uint32_t hash = val * GOLDEN_RATIO_PRIME_32; 16 | return (hash >> (32 - bits)); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | #include 5 | 6 | /* the largest number rand will return */ 7 | #define RAND_MAX 2147483647UL 8 | 9 | /* libs/rand.c */ 10 | int rand(void); 11 | void srand(unsigned int seed); 12 | 13 | /* libs/hash.c */ 14 | uint32_t hash32(uint32_t val, unsigned int bits); 15 | 16 | #endif /* !__LIBS_RAND_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab6/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | /* OLNY FOR LAB6 */ 24 | #define SYS_lab6_set_priority 255 25 | 26 | /* SYS_fork flags */ 27 | #define CLONE_VM 0x00000100 // set if VM shared between processes 28 | #define CLONE_THREAD 0x00000200 // thread group 29 | 30 | #endif /* !__LIBS_UNISTD_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /labcodes/lab6/pic/prio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab6/pic/prio.png -------------------------------------------------------------------------------- /labcodes/lab6/readme.md: -------------------------------------------------------------------------------- 1 | lab6.md -------------------------------------------------------------------------------- /labcodes/lab6/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab6/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab6/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab6/user/badarg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, exit_code; 7 | if ((pid = fork()) == 0) { 8 | cprintf("fork ok.\n"); 9 | int i; 10 | for (i = 0; i < 10; i ++) { 11 | yield(); 12 | } 13 | exit(0xbeaf); 14 | } 15 | assert(pid > 0); 16 | assert(waitpid(-1, NULL) != 0); 17 | assert(waitpid(pid, (void *)0xC0000000) != 0); 18 | assert(waitpid(pid, &exit_code) == 0 && exit_code == 0xbeaf); 19 | cprintf("badarg pass.\n"); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /labcodes/lab6/user/badsegment.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* try to load the kernel's TSS selector into the DS register */ 5 | 6 | int 7 | main(void) { 8 | asm volatile("movw $0x28,%ax; movw %ax,%ds"); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/user/divzero.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int zero; 5 | 6 | int 7 | main(void) { 8 | cprintf("value is %d.\n", 1 / zero); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab6/user/faultread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %8x from 0.\n", *(unsigned int *)0); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab6/user/faultreadkernel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %08x from 0xfac00000!\n", *(unsigned *)0xfac00000); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab6/user/forktest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int max_child = 32; 5 | 6 | int 7 | main(void) { 8 | int n, pid; 9 | for (n = 0; n < max_child; n ++) { 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am child %d\n", n); 12 | exit(0); 13 | } 14 | assert(pid > 0); 15 | } 16 | 17 | if (n > max_child) { 18 | panic("fork claimed to work %d times!\n", n); 19 | } 20 | 21 | for (; n > 0; n --) { 22 | if (wait() != 0) { 23 | panic("wait stopped early\n"); 24 | } 25 | } 26 | 27 | if (wait() == 0) { 28 | panic("wait got too many\n"); 29 | } 30 | 31 | cprintf("forktest pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab6/user/forktree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEPTH 4 6 | 7 | void forktree(const char *cur); 8 | 9 | void 10 | forkchild(const char *cur, char branch) { 11 | char nxt[DEPTH + 1]; 12 | 13 | if (strlen(cur) >= DEPTH) 14 | return; 15 | 16 | snprintf(nxt, DEPTH + 1, "%s%c", cur, branch); 17 | if (fork() == 0) { 18 | forktree(nxt); 19 | yield(); 20 | exit(0); 21 | } 22 | } 23 | 24 | void 25 | forktree(const char *cur) { 26 | cprintf("%04x: I am '%s'\n", getpid(), cur); 27 | 28 | forkchild(cur, '0'); 29 | forkchild(cur, '1'); 30 | } 31 | 32 | int 33 | main(void) { 34 | forktree(""); 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /labcodes/lab6/user/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("Hello world!!.\n"); 7 | cprintf("I am process %d.\n", getpid()); 8 | cprintf("hello pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/user/libs/initcode.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl _start 3 | _start: 4 | # set ebp for backtrace 5 | movl $0x0, %ebp 6 | 7 | # move down the esp register 8 | # since it may cause page fault in backtrace 9 | subl $0x20, %esp 10 | 11 | # call user-program function 12 | call umain 13 | 1: jmp 1b 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab6/user/libs/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void 8 | __panic(const char *file, int line, const char *fmt, ...) { 9 | // print the 'message' 10 | va_list ap; 11 | va_start(ap, fmt); 12 | cprintf("user panic at %s:%d:\n ", file, line); 13 | vcprintf(fmt, ap); 14 | cprintf("\n"); 15 | va_end(ap); 16 | exit(-E_PANIC); 17 | } 18 | 19 | void 20 | __warn(const char *file, int line, const char *fmt, ...) { 21 | va_list ap; 22 | va_start(ap, fmt); 23 | cprintf("user warning at %s:%d:\n ", file, line); 24 | vcprintf(fmt, ap); 25 | cprintf("\n"); 26 | va_end(ap); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab6/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int error_code); 5 | int sys_fork(void); 6 | int sys_wait(int pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int pid); 9 | int sys_getpid(void); 10 | int sys_putc(int c); 11 | int sys_pgdir(void); 12 | int sys_gettime(void); 13 | /* FOR LAB6 ONLY */ 14 | void sys_lab6_set_priority(uint32_t priority); 15 | 16 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab6/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | unsigned int 50 | gettime_msec(void) { 51 | return (unsigned int)sys_gettime(); 52 | } 53 | 54 | void 55 | lab6_set_priority(uint32_t priority) 56 | { 57 | sys_lab6_set_priority(priority); 58 | } 59 | -------------------------------------------------------------------------------- /labcodes/lab6/user/libs/umain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void); 4 | 5 | void 6 | umain(void) { 7 | int ret = main(); 8 | exit(ret); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab6/user/pgdir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I am %d, print pgdir.\n", getpid()); 7 | print_pgdir(); 8 | cprintf("pgdir pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab6/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | asm volatile("int $14"); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab6/user/spin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, ret; 7 | cprintf("I am the parent. Forking the child...\n"); 8 | if ((pid = fork()) == 0) { 9 | cprintf("I am the child. spinning ...\n"); 10 | while (1); 11 | } 12 | cprintf("I am the parent. Running the child...\n"); 13 | 14 | yield(); 15 | yield(); 16 | yield(); 17 | 18 | cprintf("I am the parent. Killing the child...\n"); 19 | 20 | assert((ret = kill(pid)) == 0); 21 | cprintf("kill returns %d\n", ret); 22 | 23 | assert((ret = waitpid(pid, NULL)) == 0); 24 | cprintf("wait returns %d\n", ret); 25 | 26 | cprintf("spin may pass.\n"); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab6/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /labcodes/lab6/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /labcodes/lab6/user/yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int i; 7 | cprintf("Hello, I am process %d.\n", getpid()); 8 | for (i = 0; i < 5; i ++) { 9 | yield(); 10 | cprintf("Back in process %d, iteration %d.\n", getpid(), i); 11 | } 12 | cprintf("All done in process %d.\n", getpid()); 13 | cprintf("yield pass.\n"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab7/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab7/.projectile -------------------------------------------------------------------------------- /labcodes/lab7/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/fs/fs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_FS_H__ 2 | #define __KERN_FS_FS_H__ 3 | 4 | #include 5 | 6 | #define SECTSIZE 512 7 | #define PAGE_NSECT (PGSIZE / SECTSIZE) 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/fs/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAPFS_H__ 2 | #define __KERN_FS_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | extern free_area_t free_area; 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_KMALLOC_H__ 2 | #define __KERN_MM_KMALLOC_H__ 3 | 4 | #include 5 | 6 | #define KMALLOC_MAX_ORDER 10 7 | 8 | void kmalloc_init(void); 9 | 10 | void *kmalloc(size_t n); 11 | void kfree(void *objp); 12 | 13 | size_t kallocated(void); 14 | 15 | #endif /* !__KERN_MM_KMALLOC_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | 5 | pushl %edx # push arg 6 | call *%ebx # call fn 7 | 8 | pushl %eax # save the return value of fn(arg) 9 | call do_exit # call do_exit to terminate current thread 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/process/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl switch_to 3 | switch_to: # switch_to(from, to) 4 | 5 | # save from's registers 6 | movl 4(%esp), %eax # eax points to from 7 | popl 0(%eax) # save eip !popl 8 | movl %esp, 4(%eax) 9 | movl %ebx, 8(%eax) 10 | movl %ecx, 12(%eax) 11 | movl %edx, 16(%eax) 12 | movl %esi, 20(%eax) 13 | movl %edi, 24(%eax) 14 | movl %ebp, 28(%eax) 15 | 16 | # restore to's registers 17 | movl 4(%esp), %eax # not 8(%esp): popped return address already 18 | # eax now points to to 19 | movl 28(%eax), %ebp 20 | movl 24(%eax), %edi 21 | movl 20(%eax), %esi 22 | movl 16(%eax), %edx 23 | movl 12(%eax), %ecx 24 | movl 8(%eax), %ebx 25 | movl 4(%eax), %esp 26 | 27 | pushl 0(%eax) # push eip 28 | 29 | ret 30 | 31 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/schedule/default_sched.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SCHEDULE_SCHED_RR_H__ 2 | #define __KERN_SCHEDULE_SCHED_RR_H__ 3 | 4 | #include 5 | 6 | extern struct sched_class default_sched_class; 7 | 8 | #endif /* !__KERN_SCHEDULE_SCHED_RR_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/sync/sem.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SEM_H__ 2 | #define __KERN_SYNC_SEM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct { 9 | int value; 10 | wait_queue_t wait_queue; 11 | } semaphore_t; 12 | 13 | void sem_init(semaphore_t *sem, int value); 14 | void up(semaphore_t *sem); 15 | void down(semaphore_t *sem); 16 | bool try_down(semaphore_t *sem); 17 | 18 | #endif /* !__KERN_SYNC_SEM_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static inline bool 12 | __intr_save(void) { 13 | if (read_eflags() & FL_IF) { 14 | intr_disable(); 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | 20 | static inline void 21 | __intr_restore(bool flag) { 22 | if (flag) { 23 | intr_enable(); 24 | } 25 | } 26 | 27 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 28 | #define local_intr_restore(x) __intr_restore(x); 29 | 30 | #endif /* !__KERN_SYNC_SYNC_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/syscall/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYSCALL_SYSCALL_H__ 2 | #define __KERN_SYSCALL_SYSCALL_H__ 3 | 4 | void syscall(void); 5 | 6 | #endif /* !__KERN_SYSCALL_SYSCALL_H__ */ 7 | 8 | -------------------------------------------------------------------------------- /labcodes/lab7/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | .globl forkrets 46 | forkrets: 47 | # set stack to this new process's trapframe 48 | movl 4(%esp), %esp 49 | jmp __trapret 50 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 4 | #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 5 | 6 | /* * 7 | * hash32 - generate a hash value in the range [0, 2^@bits - 1] 8 | * @val: the input value 9 | * @bits: the number of bits in a return value 10 | * 11 | * High bits are more random, so we use them. 12 | * */ 13 | uint32_t 14 | hash32(uint32_t val, unsigned int bits) { 15 | uint32_t hash = val * GOLDEN_RATIO_PRIME_32; 16 | return (hash >> (32 - bits)); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | #include 5 | 6 | /* the largest number rand will return */ 7 | #define RAND_MAX 2147483647UL 8 | 9 | /* libs/rand.c */ 10 | int rand(void); 11 | void srand(unsigned int seed); 12 | 13 | /* libs/hash.c */ 14 | uint32_t hash32(uint32_t val, unsigned int bits); 15 | 16 | #endif /* !__LIBS_RAND_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /labcodes/lab7/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | /* OLNY FOR LAB6 */ 24 | #define SYS_lab6_set_priority 255 25 | 26 | /* SYS_fork flags */ 27 | #define CLONE_VM 0x00000100 // set if VM shared between processes 28 | #define CLONE_THREAD 0x00000200 // thread group 29 | 30 | #endif /* !__LIBS_UNISTD_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /labcodes/lab7/readme.md: -------------------------------------------------------------------------------- 1 | lab7.md -------------------------------------------------------------------------------- /labcodes/lab7/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab7/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab7/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab7/user/badarg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, exit_code; 7 | if ((pid = fork()) == 0) { 8 | cprintf("fork ok.\n"); 9 | int i; 10 | for (i = 0; i < 10; i ++) { 11 | yield(); 12 | } 13 | exit(0xbeaf); 14 | } 15 | assert(pid > 0); 16 | assert(waitpid(-1, NULL) != 0); 17 | assert(waitpid(pid, (void *)0xC0000000) != 0); 18 | assert(waitpid(pid, &exit_code) == 0 && exit_code == 0xbeaf); 19 | cprintf("badarg pass.\n"); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /labcodes/lab7/user/badsegment.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* try to load the kernel's TSS selector into the DS register */ 5 | 6 | int 7 | main(void) { 8 | asm volatile("movw $0x28,%ax; movw %ax,%ds"); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/user/divzero.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int zero; 5 | 6 | int 7 | main(void) { 8 | cprintf("value is %d.\n", 1 / zero); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab7/user/faultread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %8x from 0.\n", *(unsigned int *)0); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab7/user/faultreadkernel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %08x from 0xfac00000!\n", *(unsigned *)0xfac00000); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab7/user/forktest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int max_child = 32; 5 | 6 | int 7 | main(void) { 8 | int n, pid; 9 | for (n = 0; n < max_child; n ++) { 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am child %d\n", n); 12 | exit(0); 13 | } 14 | assert(pid > 0); 15 | } 16 | 17 | if (n > max_child) { 18 | panic("fork claimed to work %d times!\n", n); 19 | } 20 | 21 | for (; n > 0; n --) { 22 | if (wait() != 0) { 23 | panic("wait stopped early\n"); 24 | } 25 | } 26 | 27 | if (wait() == 0) { 28 | panic("wait got too many\n"); 29 | } 30 | 31 | cprintf("forktest pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab7/user/forktree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEPTH 4 6 | 7 | void forktree(const char *cur); 8 | 9 | void 10 | forkchild(const char *cur, char branch) { 11 | char nxt[DEPTH + 1]; 12 | 13 | if (strlen(cur) >= DEPTH) 14 | return; 15 | 16 | snprintf(nxt, DEPTH + 1, "%s%c", cur, branch); 17 | if (fork() == 0) { 18 | forktree(nxt); 19 | yield(); 20 | exit(0); 21 | } 22 | } 23 | 24 | void 25 | forktree(const char *cur) { 26 | cprintf("%04x: I am '%s'\n", getpid(), cur); 27 | 28 | forkchild(cur, '0'); 29 | forkchild(cur, '1'); 30 | } 31 | 32 | int 33 | main(void) { 34 | forktree(""); 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /labcodes/lab7/user/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("Hello world!!.\n"); 7 | cprintf("I am process %d.\n", getpid()); 8 | cprintf("hello pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/user/libs/initcode.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl _start 3 | _start: 4 | # set ebp for backtrace 5 | movl $0x0, %ebp 6 | 7 | # move down the esp register 8 | # since it may cause page fault in backtrace 9 | subl $0x20, %esp 10 | 11 | # call user-program function 12 | call umain 13 | 1: jmp 1b 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab7/user/libs/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void 8 | __panic(const char *file, int line, const char *fmt, ...) { 9 | // print the 'message' 10 | va_list ap; 11 | va_start(ap, fmt); 12 | cprintf("user panic at %s:%d:\n ", file, line); 13 | vcprintf(fmt, ap); 14 | cprintf("\n"); 15 | va_end(ap); 16 | exit(-E_PANIC); 17 | } 18 | 19 | void 20 | __warn(const char *file, int line, const char *fmt, ...) { 21 | va_list ap; 22 | va_start(ap, fmt); 23 | cprintf("user warning at %s:%d:\n ", file, line); 24 | vcprintf(fmt, ap); 25 | cprintf("\n"); 26 | va_end(ap); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab7/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int error_code); 5 | int sys_fork(void); 6 | int sys_wait(int pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int pid); 9 | int sys_getpid(void); 10 | int sys_putc(int c); 11 | int sys_pgdir(void); 12 | int sys_gettime(void); 13 | /* FOR LAB6 ONLY */ 14 | void sys_lab6_set_priority(uint32_t priority); 15 | 16 | int sys_sleep(unsigned int time); 17 | 18 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab7/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | unsigned int 50 | gettime_msec(void) { 51 | return (unsigned int)sys_gettime(); 52 | } 53 | 54 | void 55 | lab6_set_priority(uint32_t priority) 56 | { 57 | sys_lab6_set_priority(priority); 58 | } 59 | 60 | int 61 | sleep(unsigned int time) { 62 | return sys_sleep(time); 63 | } 64 | -------------------------------------------------------------------------------- /labcodes/lab7/user/libs/umain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void); 4 | 5 | void 6 | umain(void) { 7 | int ret = main(); 8 | exit(ret); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab7/user/pgdir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I am %d, print pgdir.\n", getpid()); 7 | print_pgdir(); 8 | cprintf("pgdir pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab7/user/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | sleepy(int pid) { 6 | int i, time = 100; 7 | for (i = 0; i < 10; i ++) { 8 | sleep(time); 9 | cprintf("sleep %d x %d slices.\n", i + 1, time); 10 | } 11 | exit(0); 12 | } 13 | 14 | int 15 | main(void) { 16 | unsigned int time = gettime_msec(); 17 | int pid1, exit_code; 18 | 19 | if ((pid1 = fork()) == 0) { 20 | sleepy(pid1); 21 | } 22 | 23 | assert(waitpid(pid1, &exit_code) == 0 && exit_code == 0); 24 | cprintf("use %04d msecs.\n", gettime_msec() - time); 25 | cprintf("sleep pass.\n"); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab7/user/sleepkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid; 7 | if ((pid = fork()) == 0) { 8 | sleep(~0); 9 | exit(0xdead); 10 | } 11 | assert(pid > 0); 12 | 13 | sleep(100); 14 | assert(kill(pid) == 0); 15 | cprintf("sleepkill pass.\n"); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab7/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | asm volatile("int $14"); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab7/user/spin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, ret; 7 | cprintf("I am the parent. Forking the child...\n"); 8 | pid = fork(); 9 | if (pid== 0) { 10 | cprintf("I am the child. spinning ...\n"); 11 | while (1); 12 | }else if (pid<0) { 13 | panic("fork child error\n"); 14 | } 15 | cprintf("I am the parent. Running the child...\n"); 16 | 17 | yield(); 18 | yield(); 19 | yield(); 20 | 21 | cprintf("I am the parent. Killing the child...\n"); 22 | 23 | assert((ret = kill(pid)) == 0); 24 | cprintf("kill returns %d\n", ret); 25 | 26 | assert((ret = waitpid(pid, NULL)) == 0); 27 | cprintf("wait returns %d\n", ret); 28 | 29 | cprintf("spin may pass.\n"); 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /labcodes/lab7/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /labcodes/lab7/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /labcodes/lab7/user/yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int i; 7 | cprintf("Hello, I am process %d.\n", getpid()); 8 | for (i = 0; i < 5; i ++) { 9 | yield(); 10 | cprintf("Back in process %d, iteration %d.\n", getpid(), i); 11 | } 12 | cprintf("All done in process %d.\n", getpid()); 13 | cprintf("yield pass.\n"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab8/.projectile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trinkle23897/os2019/0274c5919a8356081e3c838fa1277394ff78cd54/labcodes/lab8/.projectile -------------------------------------------------------------------------------- /labcodes/lab8/boot/asm.h: -------------------------------------------------------------------------------- 1 | #ifndef __BOOT_ASM_H__ 2 | #define __BOOT_ASM_H__ 3 | 4 | /* Assembler macros to create x86 segments */ 5 | 6 | /* Normal segment */ 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | 17 | /* Application segment type bits */ 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | 25 | #endif /* !__BOOT_ASM_H__ */ 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/debug/kdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_KDEBUG_H__ 2 | #define __KERN_DEBUG_KDEBUG_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_kerninfo(void); 8 | void print_stackframe(void); 9 | void print_debuginfo(uintptr_t eip); 10 | 11 | #endif /* !__KERN_DEBUG_KDEBUG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CLOCK_H__ 2 | #define __KERN_DRIVER_CLOCK_H__ 3 | 4 | #include 5 | 6 | extern volatile size_t ticks; 7 | 8 | void clock_init(void); 9 | 10 | long SYSTEM_READ_TIMER( void ); 11 | 12 | 13 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/console.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_CONSOLE_H__ 2 | #define __KERN_DRIVER_CONSOLE_H__ 3 | 4 | void cons_init(void); 5 | void cons_putc(int c); 6 | int cons_getc(void); 7 | void serial_intr(void); 8 | void kbd_intr(void); 9 | 10 | #endif /* !__KERN_DRIVER_CONSOLE_H__ */ 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/ide.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_IDE_H__ 2 | #define __KERN_DRIVER_IDE_H__ 3 | 4 | #include 5 | 6 | void ide_init(void); 7 | bool ide_device_valid(unsigned short ideno); 8 | size_t ide_device_size(unsigned short ideno); 9 | 10 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, size_t nsecs); 11 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, size_t nsecs); 12 | 13 | #endif /* !__KERN_DRIVER_IDE_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void 6 | intr_enable(void) { 7 | sti(); 8 | } 9 | 10 | /* intr_disable - disable irq interrupt */ 11 | void 12 | intr_disable(void) { 13 | cli(); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/intr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_INTR_H__ 2 | #define __KERN_DRIVER_INTR_H__ 3 | 4 | void intr_enable(void); 5 | void intr_disable(void); 6 | 7 | #endif /* !__KERN_DRIVER_INTR_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/driver/picirq.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DRIVER_PICIRQ_H__ 2 | #define __KERN_DRIVER_PICIRQ_H__ 3 | 4 | void pic_init(void); 5 | void pic_enable(unsigned int irq); 6 | 7 | #define IRQ_OFFSET 32 8 | 9 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/devs/dev.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_DEVS_DEV_H__ 2 | #define __KERN_FS_DEVS_DEV_H__ 3 | 4 | #include 5 | 6 | struct inode; 7 | struct iobuf; 8 | 9 | /* 10 | * Filesystem-namespace-accessible device. 11 | * d_io is for both reads and writes; the iobuf will indicates the direction. 12 | */ 13 | struct device { 14 | size_t d_blocks; 15 | size_t d_blocksize; 16 | int (*d_open)(struct device *dev, uint32_t open_flags); 17 | int (*d_close)(struct device *dev); 18 | int (*d_io)(struct device *dev, struct iobuf *iob, bool write); 19 | int (*d_ioctl)(struct device *dev, int op, void *data); 20 | }; 21 | 22 | #define dop_open(dev, open_flags) ((dev)->d_open(dev, open_flags)) 23 | #define dop_close(dev) ((dev)->d_close(dev)) 24 | #define dop_io(dev, iob, write) ((dev)->d_io(dev, iob, write)) 25 | #define dop_ioctl(dev, op, data) ((dev)->d_ioctl(dev, op, data)) 26 | 27 | void dev_init(void); 28 | struct inode *dev_create_inode(void); 29 | 30 | #endif /* !__KERN_FS_DEVS_DEV_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/iobuf.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_IOBUF_H__ 2 | #define __KERN_FS_IOBUF_H__ 3 | 4 | #include 5 | 6 | /* 7 | * iobuf is a buffer Rd/Wr status record 8 | */ 9 | struct iobuf { 10 | void *io_base; // the base addr of buffer (used for Rd/Wr) 11 | off_t io_offset; // current Rd/Wr position in buffer, will have been incremented by the amount transferred 12 | size_t io_len; // the length of buffer (used for Rd/Wr) 13 | size_t io_resid; // current resident length need to Rd/Wr, will have been decremented by the amount transferred. 14 | }; 15 | 16 | #define iobuf_used(iob) ((size_t)((iob)->io_len - (iob)->io_resid)) 17 | 18 | struct iobuf *iobuf_init(struct iobuf *iob, void *base, size_t len, off_t offset); 19 | int iobuf_move(struct iobuf *iob, void *data, size_t len, bool m2b, size_t *copiedp); 20 | int iobuf_move_zeros(struct iobuf *iob, size_t len, size_t *copiedp); 21 | void iobuf_skip(struct iobuf *iob, size_t n); 22 | 23 | #endif /* !__KERN_FS_IOBUF_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/sfs/sfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* 7 | * sfs_init - mount sfs on disk0 8 | * 9 | * CALL GRAPH: 10 | * kern_init-->fs_init-->sfs_init 11 | */ 12 | void 13 | sfs_init(void) { 14 | int ret; 15 | if ((ret = sfs_mount("disk0")) != 0) { 16 | panic("failed: sfs: sfs_mount: %e.\n", ret); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/sfs/sfs_lock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /* 7 | * lock_sfs_fs - lock the process of SFS Filesystem Rd/Wr Disk Block 8 | * 9 | * called by: sfs_load_inode, sfs_sync, sfs_reclaim 10 | */ 11 | void 12 | lock_sfs_fs(struct sfs_fs *sfs) { 13 | down(&(sfs->fs_sem)); 14 | } 15 | 16 | /* 17 | * lock_sfs_io - lock the process of SFS File Rd/Wr Disk Block 18 | * 19 | * called by: sfs_rwblock, sfs_clear_block, sfs_sync_super 20 | */ 21 | void 22 | lock_sfs_io(struct sfs_fs *sfs) { 23 | down(&(sfs->io_sem)); 24 | } 25 | 26 | /* 27 | * unlock_sfs_fs - unlock the process of SFS Filesystem Rd/Wr Disk Block 28 | * 29 | * called by: sfs_load_inode, sfs_sync, sfs_reclaim 30 | */ 31 | void 32 | unlock_sfs_fs(struct sfs_fs *sfs) { 33 | up(&(sfs->fs_sem)); 34 | } 35 | 36 | /* 37 | * unlock_sfs_io - unlock the process of sfs Rd/Wr Disk Block 38 | * 39 | * called by: sfs_rwblock sfs_clear_block sfs_sync_super 40 | */ 41 | void 42 | unlock_sfs_io(struct sfs_fs *sfs) { 43 | up(&(sfs->io_sem)); 44 | } 45 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/swap/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/fs/swap/swapfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_SWAP_SWAPFS_H__ 2 | #define __KERN_FS_SWAP_SWAPFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | void swapfs_init(void); 8 | int swapfs_read(swap_entry_t entry, struct Page *page); 9 | int swapfs_write(swap_entry_t entry, struct Page *page); 10 | 11 | #endif /* !__KERN_FS_SWAP_SWAPFS_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/libs/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char * 5 | strdup(const char *src) { 6 | char *dst; 7 | size_t len = strlen(src); 8 | if ((dst = kmalloc(len + 1)) != NULL) { 9 | memcpy(dst, src, len); 10 | dst[len] = '\0'; 11 | } 12 | return dst; 13 | } 14 | 15 | char * 16 | stradd(const char *src1, const char *src2) { 17 | char *ret, *dst; 18 | size_t len1 = strlen(src1), len2 = strlen(src2); 19 | if ((ret = dst = kmalloc(len1 + len2 + 1)) != NULL) { 20 | memcpy(dst, src1, len1), dst += len1; 21 | memcpy(dst, src2, len2), dst += len2; 22 | *dst = '\0'; 23 | } 24 | return ret; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/mm/default_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_DEFAULT_PMM_H__ 2 | #define __KERN_MM_DEFAULT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager default_pmm_manager; 7 | extern free_area_t free_area; 8 | #endif /* ! __KERN_MM_DEFAULT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_KMALLOC_H__ 2 | #define __KERN_MM_KMALLOC_H__ 3 | 4 | #include 5 | 6 | #define KMALLOC_MAX_ORDER 10 7 | 8 | void kmalloc_init(void); 9 | 10 | void *kmalloc(size_t n); 11 | void kfree(void *objp); 12 | 13 | size_t kallocated(void); 14 | 15 | #endif /* !__KERN_MM_KMALLOC_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/mm/swap_fifo.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_FIFO_H__ 2 | #define __KERN_MM_SWAP_FIFO_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_fifo; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | 5 | pushl %edx # push arg 6 | call *%ebx # call fn 7 | 8 | pushl %eax # save the return value of fn(arg) 9 | call do_exit # call do_exit to terminate current thread 10 | 11 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/process/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl switch_to 3 | switch_to: # switch_to(from, to) 4 | 5 | # save from's registers 6 | movl 4(%esp), %eax # eax points to from 7 | popl 0(%eax) # save eip !popl 8 | movl %esp, 4(%eax) 9 | movl %ebx, 8(%eax) 10 | movl %ecx, 12(%eax) 11 | movl %edx, 16(%eax) 12 | movl %esi, 20(%eax) 13 | movl %edi, 24(%eax) 14 | movl %ebp, 28(%eax) 15 | 16 | # restore to's registers 17 | movl 4(%esp), %eax # not 8(%esp): popped return address already 18 | # eax now points to to 19 | movl 28(%eax), %ebp 20 | movl 24(%eax), %edi 21 | movl 20(%eax), %esi 22 | movl 16(%eax), %edx 23 | movl 12(%eax), %ecx 24 | movl 8(%eax), %ebx 25 | movl 4(%eax), %esp 26 | 27 | pushl 0(%eax) # push eip 28 | 29 | ret 30 | 31 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/schedule/default_sched.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SCHEDULE_SCHED_RR_H__ 2 | #define __KERN_SCHEDULE_SCHED_RR_H__ 3 | 4 | #include 5 | 6 | extern struct sched_class default_sched_class; 7 | 8 | #endif /* !__KERN_SCHEDULE_SCHED_RR_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/sync/sem.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SEM_H__ 2 | #define __KERN_SYNC_SEM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct { 9 | int value; 10 | wait_queue_t wait_queue; 11 | } semaphore_t; 12 | 13 | void sem_init(semaphore_t *sem, int value); 14 | void up(semaphore_t *sem); 15 | void down(semaphore_t *sem); 16 | bool try_down(semaphore_t *sem); 17 | 18 | #endif /* !__KERN_SYNC_SEM_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/sync/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYNC_SYNC_H__ 2 | #define __KERN_SYNC_SYNC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static inline bool 12 | __intr_save(void) { 13 | if (read_eflags() & FL_IF) { 14 | intr_disable(); 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | 20 | static inline void 21 | __intr_restore(bool flag) { 22 | if (flag) { 23 | intr_enable(); 24 | } 25 | } 26 | 27 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 28 | #define local_intr_restore(x) __intr_restore(x); 29 | 30 | #endif /* !__KERN_SYNC_SYNC_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/syscall/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_SYSCALL_SYSCALL_H__ 2 | #define __KERN_SYSCALL_SYSCALL_H__ 3 | 4 | void syscall(void); 5 | 6 | #endif /* !__KERN_SYSCALL_SYSCALL_H__ */ 7 | 8 | -------------------------------------------------------------------------------- /labcodes/lab8/kern/trap/trapentry.S: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | # vectors.S sends all traps here. 4 | .text 5 | .globl __alltraps 6 | __alltraps: 7 | # push registers to build a trap frame 8 | # therefore make the stack look like a struct trapframe 9 | pushl %ds 10 | pushl %es 11 | pushl %fs 12 | pushl %gs 13 | pushal 14 | 15 | # load GD_KDATA into %ds and %es to set up data segments for kernel 16 | movl $GD_KDATA, %eax 17 | movw %ax, %ds 18 | movw %ax, %es 19 | 20 | # push %esp to pass a pointer to the trapframe as an argument to trap() 21 | pushl %esp 22 | 23 | # call trap(tf), where tf=%esp 24 | call trap 25 | 26 | # pop the pushed stack pointer 27 | popl %esp 28 | 29 | # return falls through to trapret... 30 | .globl __trapret 31 | __trapret: 32 | # restore registers from stack 33 | popal 34 | 35 | # restore %ds, %es, %fs and %gs 36 | popl %gs 37 | popl %fs 38 | popl %es 39 | popl %ds 40 | 41 | # get rid of the trap number and error code 42 | addl $0x8, %esp 43 | iret 44 | 45 | .globl forkrets 46 | forkrets: 47 | # set stack to this new process's trapframe 48 | movl 4(%esp), %esp 49 | jmp __trapret 50 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/dirent.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_DIRENT_H__ 2 | #define __LIBS_DIRENT_H__ 3 | 4 | #include 5 | #include 6 | 7 | struct dirent { 8 | off_t offset; 9 | char name[FS_MAX_FNAME_LEN + 1]; 10 | }; 11 | 12 | #endif /* !__LIBS_DIRENT_H__ */ 13 | 14 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 4 | #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 5 | 6 | /* * 7 | * hash32 - generate a hash value in the range [0, 2^@bits - 1] 8 | * @val: the input value 9 | * @bits: the number of bits in a return value 10 | * 11 | * High bits are more random, so we use them. 12 | * */ 13 | uint32_t 14 | hash32(uint32_t val, unsigned int bits) { 15 | uint32_t hash = val * GOLDEN_RATIO_PRIME_32; 16 | return (hash >> (32 - bits)); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static unsigned long long next = 1; 5 | 6 | /* * 7 | * rand - returns a pseudo-random integer 8 | * 9 | * The rand() function return a value in the range [0, RAND_MAX]. 10 | * */ 11 | int 12 | rand(void) { 13 | next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); 14 | unsigned long long result = (next >> 12); 15 | return (int)do_div(result, RAND_MAX + 1); 16 | } 17 | 18 | /* * 19 | * srand - seed the random number generator with the given number 20 | * @seed: the required seed number 21 | * */ 22 | void 23 | srand(unsigned int seed) { 24 | next = seed; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDARG_H__ 2 | #define __LIBS_STDARG_H__ 3 | 4 | /* compiler provides size of save area */ 5 | typedef __builtin_va_list va_list; 6 | 7 | #define va_start(ap, last) (__builtin_va_start(ap, last)) 8 | #define va_arg(ap, type) (__builtin_va_arg(ap, type)) 9 | #define va_end(ap) /*nothing*/ 10 | 11 | #endif /* !__LIBS_STDARG_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *, int), int fd, void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *, int), int fd, void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDLIB_H__ 2 | #define __LIBS_STDLIB_H__ 3 | 4 | #include 5 | 6 | /* the largest number rand will return */ 7 | #define RAND_MAX 2147483647UL 8 | 9 | /* libs/rand.c */ 10 | int rand(void); 11 | void srand(unsigned int seed); 12 | 13 | /* libs/hash.c */ 14 | uint32_t hash32(uint32_t val, unsigned int bits); 15 | 16 | #endif /* !__LIBS_RAND_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /labcodes/lab8/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | char *strcat(char *dst, const char *src); 12 | char *strdup(const char *src); 13 | char *stradd(const char *src1, const char *src2); 14 | 15 | int strcmp(const char *s1, const char *s2); 16 | int strncmp(const char *s1, const char *s2, size_t n); 17 | 18 | char *strchr(const char *s, char c); 19 | char *strfind(const char *s, char c); 20 | long strtol(const char *s, char **endptr, int base); 21 | 22 | void *memset(void *s, char c, size_t n); 23 | void *memmove(void *dst, const void *src, size_t n); 24 | void *memcpy(void *dst, const void *src, size_t n); 25 | int memcmp(const void *v1, const void *v2, size_t n); 26 | 27 | #endif /* !__LIBS_STRING_H__ */ 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab8/readme.md: -------------------------------------------------------------------------------- 1 | lab8.md -------------------------------------------------------------------------------- /labcodes/lab8/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-i386") 2 | OUTPUT_ARCH(i386) 3 | 4 | SECTIONS { 5 | . = 0x7C00; 6 | 7 | .startup : { 8 | *bootasm.o(.text) 9 | } 10 | 11 | .text : { *(.text) } 12 | .data : { *(.data .rodata) } 13 | 14 | /DISCARD/ : { *(.eh_*) } 15 | } 16 | -------------------------------------------------------------------------------- /labcodes/lab8/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes/lab8/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if (i != 8 && (i < 10 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab8/user/badarg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, exit_code; 7 | if ((pid = fork()) == 0) { 8 | cprintf("fork ok.\n"); 9 | int i; 10 | for (i = 0; i < 10; i ++) { 11 | yield(); 12 | } 13 | exit(0xbeaf); 14 | } 15 | assert(pid > 0); 16 | assert(waitpid(-1, NULL) != 0); 17 | assert(waitpid(pid, (void *)0xC0000000) != 0); 18 | assert(waitpid(pid, &exit_code) == 0 && exit_code == 0xbeaf); 19 | cprintf("badarg pass.\n"); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /labcodes/lab8/user/badsegment.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* try to load the kernel's TSS selector into the DS register */ 5 | 6 | int 7 | main(void) { 8 | asm volatile("movw $0x28,%ax; movw %ax,%ds"); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab8/user/divzero.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int zero; 5 | 6 | int 7 | main(void) { 8 | cprintf("value is %d.\n", 1 / zero); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab8/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab8/user/faultread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %8x from 0.\n", *(unsigned int *)0); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab8/user/faultreadkernel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I read %08x from 0xfac00000!\n", *(unsigned *)0xfac00000); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab8/user/forktest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int max_child = 32; 5 | 6 | int 7 | main(void) { 8 | int n, pid; 9 | for (n = 0; n < max_child; n ++) { 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am child %d\n", n); 12 | exit(0); 13 | } 14 | assert(pid > 0); 15 | } 16 | 17 | if (n > max_child) { 18 | panic("fork claimed to work %d times!\n", n); 19 | } 20 | 21 | for (; n > 0; n --) { 22 | if (wait() != 0) { 23 | panic("wait stopped early\n"); 24 | } 25 | } 26 | 27 | if (wait() == 0) { 28 | panic("wait got too many\n"); 29 | } 30 | 31 | cprintf("forktest pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab8/user/forktree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEPTH 4 6 | #define SLEEP_TIME 400 7 | void forktree(const char *cur); 8 | 9 | void 10 | forkchild(const char *cur, char branch) { 11 | char nxt[DEPTH + 1]; 12 | 13 | if (strlen(cur) >= DEPTH) 14 | return; 15 | 16 | snprintf(nxt, DEPTH + 1, "%s%c", cur, branch); 17 | if (fork() == 0) { 18 | forktree(nxt); 19 | yield(); 20 | exit(0); 21 | } 22 | } 23 | 24 | void 25 | forktree(const char *cur) { 26 | cprintf("%04x: I am '%s'\n", getpid(), cur); 27 | 28 | forkchild(cur, '0'); 29 | forkchild(cur, '1'); 30 | } 31 | 32 | int 33 | main(void) { 34 | cprintf("forktree process will sleep %d ticks\n",SLEEP_TIME); 35 | sleep(SLEEP_TIME); 36 | forktree(""); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /labcodes/lab8/user/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("Hello world!!.\n"); 7 | cprintf("I am process %d.\n", getpid()); 8 | cprintf("hello pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | DIR dir, *dirp=&dir; 12 | DIR * 13 | opendir(const char *path) { 14 | 15 | if ((dirp->fd = open(path, O_RDONLY)) < 0) { 16 | goto failed; 17 | } 18 | struct stat __stat, *stat = &__stat; 19 | if (fstat(dirp->fd, stat) != 0 || !S_ISDIR(stat->st_mode)) { 20 | goto failed; 21 | } 22 | dirp->dirent.offset = 0; 23 | return dirp; 24 | 25 | failed: 26 | return NULL; 27 | } 28 | 29 | struct dirent * 30 | readdir(DIR *dirp) { 31 | if (sys_getdirentry(dirp->fd, &(dirp->dirent)) == 0) { 32 | return &(dirp->dirent); 33 | } 34 | return NULL; 35 | } 36 | 37 | void 38 | closedir(DIR *dirp) { 39 | close(dirp->fd); 40 | } 41 | 42 | int 43 | getcwd(char *buffer, size_t len) { 44 | return sys_getcwd(buffer, len); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/dir.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_DIR_H__ 2 | #define __USER_LIBS_DIR_H__ 3 | 4 | #include 5 | #include 6 | 7 | typedef struct { 8 | int fd; 9 | struct dirent dirent; 10 | } DIR; 11 | 12 | DIR *opendir(const char *path); 13 | struct dirent *readdir(DIR *dirp); 14 | void closedir(DIR *dirp); 15 | int chdir(const char *path); 16 | int getcwd(char *buffer, size_t len); 17 | 18 | #endif /* !__USER_LIBS_DIR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/file.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_FILE_H__ 2 | #define __USER_LIBS_FILE_H__ 3 | 4 | #include 5 | 6 | struct stat; 7 | 8 | int open(const char *path, uint32_t open_flags); 9 | int close(int fd); 10 | int read(int fd, void *base, size_t len); 11 | int write(int fd, void *base, size_t len); 12 | int seek(int fd, off_t pos, int whence); 13 | int fstat(int fd, struct stat *stat); 14 | int fsync(int fd); 15 | int dup(int fd); 16 | int dup2(int fd1, int fd2); 17 | int pipe(int *fd_store); 18 | int mkfifo(const char *name, uint32_t open_flags); 19 | 20 | void print_stat(const char *name, int fd, struct stat *stat); 21 | 22 | #endif /* !__USER_LIBS_FILE_H__ */ 23 | 24 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/initcode.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl _start 3 | _start: 4 | # set ebp for backtrace 5 | movl $0x0, %ebp 6 | 7 | # load argc and argv 8 | movl (%esp), %ebx 9 | lea 0x4(%esp), %ecx 10 | 11 | 12 | # move down the esp register 13 | # since it may cause page fault in backtrace 14 | subl $0x20, %esp 15 | 16 | # save argc and argv on stack 17 | pushl %ecx 18 | pushl %ebx 19 | 20 | # call user-program function 21 | call umain 22 | 1: jmp 1b 23 | 24 | 25 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_LOCK_H__ 2 | #define __USER_LIBS_LOCK_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define INIT_LOCK {0} 9 | 10 | typedef volatile bool lock_t; 11 | 12 | static inline void 13 | lock_init(lock_t *l) { 14 | *l = 0; 15 | } 16 | 17 | static inline bool 18 | try_lock(lock_t *l) { 19 | return test_and_set_bit(0, l); 20 | } 21 | 22 | static inline void 23 | lock(lock_t *l) { 24 | if (try_lock(l)) { 25 | int step = 0; 26 | do { 27 | yield(); 28 | if (++ step == 100) { 29 | step = 0; 30 | sleep(10); 31 | } 32 | } while (try_lock(l)); 33 | } 34 | } 35 | 36 | static inline void 37 | unlock(lock_t *l) { 38 | test_and_clear_bit(0, l); 39 | } 40 | 41 | #endif /* !__USER_LIBS_LOCK_H__ */ 42 | 43 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void 8 | __panic(const char *file, int line, const char *fmt, ...) { 9 | // print the 'message' 10 | va_list ap; 11 | va_start(ap, fmt); 12 | cprintf("user panic at %s:%d:\n ", file, line); 13 | vcprintf(fmt, ap); 14 | cprintf("\n"); 15 | va_end(ap); 16 | exit(-E_PANIC); 17 | } 18 | 19 | void 20 | __warn(const char *file, int line, const char *fmt, ...) { 21 | va_list ap; 22 | va_start(ap, fmt); 23 | cprintf("user warning at %s:%d:\n ", file, line); 24 | vcprintf(fmt, ap); 25 | cprintf("\n"); 26 | va_end(ap); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int error_code); 5 | int sys_fork(void); 6 | int sys_wait(int pid, int *store); 7 | int sys_exec(const char *name, int argc, const char **argv); 8 | int sys_yield(void); 9 | int sys_kill(int pid); 10 | int sys_getpid(void); 11 | int sys_putc(int c); 12 | int sys_pgdir(void); 13 | int sys_sleep(unsigned int time); 14 | int sys_gettime(void); 15 | 16 | struct stat; 17 | struct dirent; 18 | 19 | int sys_open(const char *path, uint32_t open_flags); 20 | int sys_close(int fd); 21 | int sys_read(int fd, void *base, size_t len); 22 | int sys_write(int fd, void *base, size_t len); 23 | int sys_seek(int fd, off_t pos, int whence); 24 | int sys_fstat(int fd, struct stat *stat); 25 | int sys_fsync(int fd); 26 | int sys_getcwd(char *buffer, size_t len); 27 | int sys_getdirentry(int fd, struct dirent *dirent); 28 | int sys_dup(int fd1, int fd2); 29 | void sys_lab6_set_priority(uint32_t priority); //only for lab6 30 | 31 | 32 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 33 | 34 | -------------------------------------------------------------------------------- /labcodes/lab8/user/libs/umain.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char *argv[]); 7 | 8 | static int 9 | initfd(int fd2, const char *path, uint32_t open_flags) { 10 | int fd1, ret; 11 | if ((fd1 = open(path, open_flags)) < 0) { 12 | return fd1; 13 | } 14 | if (fd1 != fd2) { 15 | close(fd2); 16 | ret = dup2(fd1, fd2); 17 | close(fd1); 18 | } 19 | return ret; 20 | } 21 | 22 | void 23 | umain(int argc, char *argv[]) { 24 | int fd; 25 | if ((fd = initfd(0, "stdin:", O_RDONLY)) < 0) { 26 | warn("open failed: %e.\n", fd); 27 | } 28 | if ((fd = initfd(1, "stdout:", O_WRONLY)) < 0) { 29 | warn("open failed: %e.\n", fd); 30 | } 31 | int ret = main(argc, argv); 32 | exit(ret); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /labcodes/lab8/user/pgdir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | cprintf("I am %d, print pgdir.\n", getpid()); 7 | print_pgdir(); 8 | cprintf("pgdir pass.\n"); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /labcodes/lab8/user/sfs_filetest1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define printf(...) fprintf(1, __VA_ARGS__) 11 | 12 | static int safe_open(const char *path, int open_flags) 13 | { 14 | int fd = open(path, open_flags); 15 | printf("fd is %d\n",fd); 16 | assert(fd >= 0); 17 | return fd; 18 | } 19 | 20 | static struct stat *safe_fstat(int fd) 21 | { 22 | static struct stat __stat, *stat = &__stat; 23 | int ret = fstat(fd, stat); 24 | assert(ret == 0); 25 | return stat; 26 | } 27 | 28 | 29 | static void safe_read(int fd, void *data, size_t len) 30 | { 31 | int ret = read(fd, data, len); 32 | assert(ret == len); 33 | } 34 | 35 | 36 | int main(void) 37 | { 38 | int fd1 = safe_open("sfs_filetest1", O_RDONLY); 39 | struct stat *stat = safe_fstat(fd1); 40 | assert(stat->st_size >= 0 && stat->st_blocks >= 0); 41 | printf("sfs_filetest1 pass.\n"); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /labcodes/lab8/user/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | sleepy(int pid) { 6 | int i, time = 100; 7 | for (i = 0; i < 10; i ++) { 8 | sleep(time); 9 | cprintf("sleep %d x %d slices.\n", i + 1, time); 10 | } 11 | exit(0); 12 | } 13 | 14 | int 15 | main(void) { 16 | unsigned int time = gettime_msec(); 17 | int pid1, exit_code; 18 | 19 | if ((pid1 = fork()) == 0) { 20 | sleepy(pid1); 21 | } 22 | 23 | assert(waitpid(pid1, &exit_code) == 0 && exit_code == 0); 24 | cprintf("use %04d msecs.\n", gettime_msec() - time); 25 | cprintf("sleep pass.\n"); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes/lab8/user/sleepkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid; 7 | if ((pid = fork()) == 0) { 8 | sleep(~0); 9 | exit(0xdead); 10 | } 11 | assert(pid > 0); 12 | 13 | sleep(100); 14 | assert(kill(pid) == 0); 15 | cprintf("sleepkill pass.\n"); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /labcodes/lab8/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | asm volatile("int $14"); 7 | panic("FAIL: T.T\n"); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /labcodes/lab8/user/spin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, ret; 7 | cprintf("I am the parent. Forking the child...\n"); 8 | if ((pid = fork()) == 0) { 9 | cprintf("I am the child. spinning ...\n"); 10 | while (1); 11 | } 12 | cprintf("I am the parent. Running the child...\n"); 13 | 14 | yield(); 15 | yield(); 16 | yield(); 17 | 18 | cprintf("I am the parent. Killing the child...\n"); 19 | 20 | assert((ret = kill(pid)) == 0); 21 | cprintf("kill returns %d\n", ret); 22 | 23 | assert((ret = waitpid(pid, NULL)) == 0); 24 | cprintf("wait returns %d\n", ret); 25 | 26 | cprintf("spin may pass.\n"); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /labcodes/lab8/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /labcodes/lab8/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /labcodes/lab8/user/yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int i; 7 | cprintf("Hello, I am process %d.\n", getpid()); 8 | for (i = 0; i < 5; i ++) { 9 | yield(); 10 | cprintf("Back in process %d, iteration %d.\n", getpid(), i); 11 | } 12 | cprintf("All done in process %d.\n", getpid()); 13 | cprintf("yield pass.\n"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /labcodes/tools/split_score_log.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Note: This script is intended to be executed at labcodes/labX 4 | 5 | import sys, os 6 | import re 7 | 8 | if len(sys.argv) < 2: 9 | print 'Usage: split_score_log.py ' 10 | sys.exit() 11 | 12 | raw_log_f = sys.argv[1] 13 | test_entry_title = re.compile('^([\w][\w -]+): *\([0-9.]*s\)') 14 | 15 | raw_log = open(raw_log_f, 'r') 16 | current_test = '' 17 | for line in raw_log.readlines(): 18 | line = line.strip('\n') 19 | m = test_entry_title.match(line) 20 | if m: 21 | print line 22 | current_test = m.group(1) 23 | error_log = open('.' + current_test.lower().replace(' ', '_') + '.error', 'w+') 24 | print >> error_log, line 25 | continue 26 | if (not line or line[0] == ' ') and current_test != '': 27 | print >> error_log, line 28 | if (line and line[0] != ' ') or line.find('-check') >= 0: 29 | print line 30 | --------------------------------------------------------------------------------