├── .gitignore ├── LICENSE ├── README ├── labcodes ├── autobuild.sh ├── autotest.sh ├── clangbuildall.sh ├── cleanall.sh ├── gccbuildall.sh ├── lab1 │ ├── .ycm_extra_conf.pyc │ ├── 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 │ │ ├── libs │ │ │ ├── readline.c │ │ │ └── stdio.c │ │ ├── mm │ │ │ ├── memlayout.h │ │ │ ├── mmu.h │ │ │ ├── pmm.c │ │ │ └── pmm.h │ │ └── trap │ │ │ ├── trap.c │ │ │ ├── trap.h │ │ │ ├── trapentry.S │ │ │ └── vectors.S │ ├── libs │ │ ├── defs.h │ │ ├── elf.h │ │ ├── error.h │ │ ├── printfmt.c │ │ ├── stdarg.h │ │ ├── stdio.h │ │ ├── string.c │ │ ├── string.h │ │ └── x86.h │ └── tools │ │ ├── function.mk │ │ ├── gdbinit │ │ ├── grade.sh │ │ ├── kernel.ld │ │ ├── sign.c │ │ └── vector.c ├── lab2 │ ├── 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 │ │ ├── libs │ │ │ ├── readline.c │ │ │ └── stdio.c │ │ ├── mm │ │ │ ├── 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 │ ├── libs │ │ ├── atomic.h │ │ ├── defs.h │ │ ├── elf.h │ │ ├── error.h │ │ ├── list.h │ │ ├── printfmt.c │ │ ├── stdarg.h │ │ ├── stdio.h │ │ ├── string.c │ │ ├── string.h │ │ └── x86.h │ └── tools │ │ ├── boot.ld │ │ ├── function.mk │ │ ├── gdbinit │ │ ├── grade.sh │ │ ├── kernel.ld │ │ ├── kernel_nopage.ld │ │ ├── sign.c │ │ └── vector.c ├── lab3 │ ├── 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 │ ├── 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 │ └── tools │ │ ├── boot.ld │ │ ├── function.mk │ │ ├── gdbinit │ │ ├── grade.sh │ │ ├── kernel.ld │ │ ├── sign.c │ │ └── vector.c ├── lab4 │ ├── 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 │ ├── 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 │ └── tools │ │ ├── boot.ld │ │ ├── function.mk │ │ ├── gdbinit │ │ ├── grade.sh │ │ ├── kernel.ld │ │ ├── sign.c │ │ └── vector.c ├── lab5 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ │ │ └── sync.h │ │ ├── syscall │ │ │ ├── syscall.c │ │ │ └── syscall.h │ │ └── trap │ │ │ ├── trap.c │ │ │ ├── trap.h │ │ │ ├── trapentry.S │ │ │ └── vectors.S │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ ├── sh.c │ ├── sleep.c │ ├── sleepkill.c │ ├── softint.c │ ├── spin.c │ ├── testbss.c │ ├── waitkill.c │ └── yield.c └── labcodes_answer ├── lab1_result ├── 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 │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── memlayout.h │ │ ├── mmu.h │ │ ├── pmm.c │ │ └── pmm.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── libs │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── printfmt.c │ ├── stdarg.h │ ├── stdio.h │ ├── string.c │ ├── string.h │ └── x86.h ├── report.txt └── tools │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── lab1init │ ├── moninit │ ├── sign.c │ └── vector.c ├── lab2_result ├── 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 │ ├── libs │ │ ├── readline.c │ │ └── stdio.c │ ├── mm │ │ ├── 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 ├── libs │ ├── atomic.h │ ├── defs.h │ ├── elf.h │ ├── error.h │ ├── list.h │ ├── printfmt.c │ ├── stdarg.h │ ├── stdio.h │ ├── string.c │ ├── string.h │ └── x86.h └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab3_result ├── 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 ├── 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 └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab4_result ├── 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 │ │ ├── rb_tree.c │ │ ├── rb_tree.h │ │ ├── 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 ├── 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 └── tools │ ├── boot.ld │ ├── function.mk │ ├── gdbinit │ ├── grade.sh │ ├── kernel.ld │ ├── sign.c │ └── vector.c ├── lab5_result ├── 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 │ │ ├── rb_tree.c │ │ ├── rb_tree.h │ │ ├── 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 ├── 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 ├── 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_result ├── 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 │ │ ├── rb_tree.c │ │ ├── rb_tree.h │ │ ├── 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 │ │ └── sync.h │ ├── syscall │ │ ├── syscall.c │ │ └── syscall.h │ └── trap │ │ ├── trap.c │ │ ├── trap.h │ │ ├── trapentry.S │ │ └── vectors.S ├── 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 ├── 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_result ├── 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 │ │ ├── rb_tree.c │ │ ├── rb_tree.h │ │ ├── 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 ├── 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 ├── 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_result ├── 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 ├── 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 ├── 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 ├── sh.c ├── sleep.c ├── sleepkill.c ├── softint.c ├── spin.c ├── testbss.c ├── waitkill.c └── yield.c /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | obj/ 3 | *~ 4 | *.bak 5 | *.d 6 | *.o 7 | *.org 8 | .gdb.in 9 | .qemu.out 10 | 11 | -------------------------------------------------------------------------------- /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 | cd lab1; make clean; cd .. 2 | cd lab2; make clean; cd .. 3 | cd lab3; make clean; cd .. 4 | cd lab4; make clean; cd .. 5 | cd lab5; make clean; cd .. 6 | cd lab6; make clean; cd .. 7 | cd lab7; make clean; cd .. 8 | cd lab8; make clean; cd .. 9 | -------------------------------------------------------------------------------- /labcodes/gccbuildall.sh: -------------------------------------------------------------------------------- 1 | cd lab1; make clean; make; cd .. 2 | cd lab2; make clean; make; cd .. 3 | cd lab3; make clean; make; cd .. 4 | cd lab4; make clean; make; cd .. 5 | cd lab5; make clean; make; cd .. 6 | cd lab6; make clean; make; cd .. 7 | cd lab7; make clean; make; cd .. 8 | cd lab8; make clean; make; cd .. 9 | -------------------------------------------------------------------------------- /labcodes/lab1/.ycm_extra_conf.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yusong-shen/mooc_os_lab/ece483e524c3bf05c51134e45aa88edea9dac900/labcodes/lab1/.ycm_extra_conf.pyc -------------------------------------------------------------------------------- /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/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/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/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/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | continue -------------------------------------------------------------------------------- /labcodes/lab1/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf(".text\n"); 6 | printf(".globl __alltraps\n"); 7 | 8 | int i; 9 | for (i = 0; i < 256; i ++) { 10 | printf(".globl vector%d\n", i); 11 | printf("vector%d:\n", i); 12 | if ((i < 8 || i > 14) && i != 17) { 13 | printf(" pushl \\$0\n"); 14 | } 15 | printf(" pushl $%d\n", i); 16 | printf(" jmp __alltraps\n"); 17 | } 18 | printf("\n"); 19 | printf("# vector table\n"); 20 | printf(".data\n"); 21 | printf(".globl __vectors\n"); 22 | printf("__vectors:\n"); 23 | for (i = 0; i < 256; i ++) { 24 | printf(" .long vector%d\n", i); 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /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/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/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/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/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/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/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.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/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/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/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/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/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.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.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/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/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/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/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/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.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/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/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/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/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/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/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/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/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/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/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/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.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/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/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/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/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/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/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/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/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/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/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/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.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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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.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/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/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/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/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/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_answer/lab1_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | continue -------------------------------------------------------------------------------- /labcodes_answer/lab1_result/tools/lab1init: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | set architecture i8086 4 | b *0x7c00 5 | continue 6 | x /2i $pc 7 | -------------------------------------------------------------------------------- /labcodes_answer/lab1_result/tools/moninit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab1_result/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf(".text\n"); 6 | printf(".globl __alltraps\n"); 7 | 8 | int i; 9 | for (i = 0; i < 256; i ++) { 10 | printf(".globl vector%d\n", i); 11 | printf("vector%d:\n", i); 12 | if ((i < 8 || i > 14) && i != 17) { 13 | printf(" pushl \\$0\n"); 14 | } 15 | printf(" pushl $%d\n", i); 16 | printf(" jmp __alltraps\n"); 17 | } 18 | printf("\n"); 19 | printf("# vector table\n"); 20 | printf(".data\n"); 21 | printf(".globl __vectors\n"); 22 | printf("__vectors:\n"); 23 | for (i = 0; i < 256; i ++) { 24 | printf(" .long vector%d\n", i); 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /labcodes_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/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_answer/lab2_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/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_answer/lab3_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SLAB_H__ 2 | #define __KERN_MM_SLAB_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_SLAB_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /labcodes_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/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_answer/lab4_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SLAB_H__ 2 | #define __KERN_MM_SLAB_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_SLAB_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab5_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SLAB_H__ 2 | #define __KERN_MM_SLAB_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_SLAB_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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 | /* FOR LAB6 ONLY */ 13 | void sys_lab6_set_priority(uint32_t priority); 14 | 15 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab6_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SLAB_H__ 2 | #define __KERN_MM_SLAB_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_SLAB_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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 | /* FOR LAB6 ONLY */ 13 | void sys_lab6_set_priority(uint32_t priority); 14 | 15 | int sys_sleep(unsigned int time); 16 | 17 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 18 | 19 | -------------------------------------------------------------------------------- /labcodes_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab7_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/kern/mm/kmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SLAB_H__ 2 | #define __KERN_MM_SLAB_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_SLAB_H__ */ 16 | 17 | -------------------------------------------------------------------------------- /labcodes_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /labcodes_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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_answer/lab8_result/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 | --------------------------------------------------------------------------------