├── lab1 ├── .projectile ├── .gdb.in ├── tools │ ├── gdbinit │ └── vector.c ├── kern │ ├── mm │ │ ├── pmm.c │ │ ├── pmm.h │ │ ├── mmu.h │ │ └── memlayout.h │ ├── driver │ │ ├── intr.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ └── console.c │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ ├── assert.h │ │ └── panic.c │ └── init │ │ └── entry.S ├── libs │ ├── stdarg.h │ ├── error.h │ ├── stdio.h │ ├── string.h │ └── sbi.h └── .score ├── lab2 ├── .projectile ├── tools │ ├── gdbinit │ ├── boot.ld │ └── vector.c ├── kern │ ├── driver │ │ ├── intr.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ └── console.c │ ├── mm │ │ ├── default_pmm.h │ │ ├── best_fit_pmm.h │ │ └── buddy_system_pmm.h │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h │ └── sync │ │ └── sync.h ├── .score └── libs │ ├── stdarg.h │ ├── error.h │ ├── stdio.h │ ├── string.h │ └── sbi.h ├── lab8 ├── giveitatry.pyq ├── tools │ ├── gdbinit │ ├── boot.ld │ ├── vector.c │ └── user.ld ├── .gdb.in ├── disk0 │ ├── sh │ ├── exit │ ├── spin │ ├── badarg │ ├── divzero │ ├── hello │ ├── matrix │ ├── pgdir │ ├── sleep │ ├── softint │ ├── testbss │ ├── yield │ ├── faultread │ ├── forktest │ ├── forktree │ ├── priority │ ├── sleepkill │ ├── waitkill │ ├── badsegment │ └── faultreadkernel ├── kern │ ├── driver │ │ ├── ramdisk.h │ │ ├── picirq.c │ │ ├── intr.h │ │ ├── picirq.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ └── clock.c │ ├── process │ │ └── entry.S │ ├── mm │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ └── kmalloc.h │ ├── syscall │ │ └── syscall.h │ ├── schedule │ │ └── default_sched.h │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h │ ├── fs │ │ ├── swap │ │ │ ├── swapfs.h │ │ │ └── swapfs.c │ │ ├── sfs │ │ │ ├── sfs.c │ │ │ └── sfs_lock.c │ │ ├── iobuf.h │ │ └── devs │ │ │ └── dev.h │ ├── sync │ │ ├── sem.h │ │ └── sync.h │ └── libs │ │ └── string.c ├── user │ ├── softint.c │ ├── faultread.c │ ├── divzero.c │ ├── faultreadkernel.c │ ├── pgdir.c │ ├── hello.c │ ├── badsegment.c │ ├── libs │ │ ├── initcode.S │ │ ├── dir.h │ │ ├── file.h │ │ ├── panic.c │ │ ├── umain.c │ │ ├── lock.h │ │ ├── dir.c │ │ └── syscall.h │ ├── sleepkill.c │ ├── yield.c │ ├── badarg.c │ ├── sleep.c │ ├── forktree.c │ ├── spin.c │ ├── forktest.c │ ├── exit.c │ ├── testbss.c │ └── waitkill.c └── libs │ ├── dirent.h │ ├── stdlib.h │ ├── stdarg.h │ ├── hash.c │ ├── rand.c │ ├── stdio.h │ └── string.h ├── lab3 ├── tools │ ├── gdbinit │ ├── boot.ld │ └── vector.c ├── kern │ ├── mm │ │ ├── swap_lru.h │ │ ├── swap_clock.h │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ ├── best_fit_pmm.h │ │ └── buddy_system_pmm.h │ ├── driver │ │ ├── intr.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ ├── ide.h │ │ ├── ide.c │ │ └── console.c │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.h │ │ └── swapfs.c │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h │ └── sync │ │ └── sync.h └── libs │ ├── stdlib.h │ ├── stdarg.h │ ├── rand.c │ ├── error.h │ ├── stdio.h │ └── string.h ├── lab4 ├── tools │ ├── gdbinit │ ├── boot.ld │ └── vector.c ├── .gdb.in ├── kern │ ├── process │ │ └── entry.S │ ├── driver │ │ ├── picirq.c │ │ ├── intr.h │ │ ├── picirq.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ ├── ide.h │ │ ├── ide.c │ │ └── console.c │ ├── mm │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ └── kmalloc.h │ ├── schedule │ │ └── sched.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.h │ │ └── swapfs.c │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h │ └── sync │ │ └── sync.h └── libs │ ├── stdlib.h │ ├── stdarg.h │ ├── hash.c │ ├── rand.c │ ├── error.h │ ├── stdio.h │ └── string.h ├── lab5 ├── tools │ ├── gdbinit │ ├── boot.ld │ ├── vector.c │ └── user.ld ├── .gdb.in ├── user │ ├── libs │ │ ├── initcode.S │ │ ├── umain.c │ │ ├── syscall.h │ │ ├── panic.c │ │ └── ulib.c │ ├── faultread.c │ ├── divzero.c │ ├── softint.c │ ├── faultreadkernel.c │ ├── pgdir.c │ ├── hello.c │ ├── badsegment.c │ ├── yield.c │ ├── badarg.c │ ├── forktree.c │ ├── spin.c │ ├── forktest.c │ ├── exit.c │ ├── testbss.c │ └── waitkill.c ├── kern │ ├── process │ │ └── entry.S │ ├── driver │ │ ├── picirq.c │ │ ├── intr.h │ │ ├── picirq.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ ├── ide.h │ │ ├── ide.c │ │ └── console.c │ ├── mm │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ └── kmalloc.h │ ├── syscall │ │ └── syscall.h │ ├── schedule │ │ └── sched.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.h │ │ └── swapfs.c │ └── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h └── libs │ ├── stdlib.h │ ├── stdarg.h │ ├── hash.c │ ├── rand.c │ ├── stdio.h │ ├── string.h │ └── unistd.h ├── lab6 ├── tools │ ├── gdbinit │ ├── boot.ld │ ├── vector.c │ └── user.ld ├── .gdb.in ├── user │ ├── libs │ │ ├── umain.c │ │ ├── initcode.S │ │ ├── syscall.h │ │ ├── panic.c │ │ └── ulib.c │ ├── softint.c │ ├── faultread.c │ ├── divzero.c │ ├── faultreadkernel.c │ ├── pgdir.c │ ├── hello.c │ ├── badsegment.c │ ├── yield.c │ ├── badarg.c │ ├── forktree.c │ ├── spin.c │ ├── forktest.c │ ├── exit.c │ ├── testbss.c │ └── waitkill.c ├── kern │ ├── process │ │ └── entry.S │ ├── driver │ │ ├── picirq.c │ │ ├── intr.h │ │ ├── picirq.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ ├── ide.h │ │ ├── ide.c │ │ ├── console.c │ │ └── clock.c │ ├── mm │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ └── kmalloc.h │ ├── syscall │ │ └── syscall.h │ ├── schedule │ │ └── default_sched.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.h │ │ └── swapfs.c │ └── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h └── libs │ ├── stdlib.h │ ├── stdarg.h │ ├── hash.c │ ├── rand.c │ ├── stdio.h │ ├── string.h │ └── unistd.h ├── lab7 ├── tools │ ├── gdbinit │ ├── boot.ld │ ├── vector.c │ └── user.ld ├── .gdb.in ├── user │ ├── libs │ │ ├── umain.c │ │ ├── initcode.S │ │ ├── syscall.h │ │ ├── panic.c │ │ └── ulib.c │ ├── softint.c │ ├── faultread.c │ ├── divzero.c │ ├── faultreadkernel.c │ ├── pgdir.c │ ├── hello.c │ ├── badsegment.c │ ├── sleepkill.c │ ├── yield.c │ ├── badarg.c │ ├── sleep.c │ ├── forktree.c │ ├── spin.c │ ├── forktest.c │ ├── exit.c │ ├── testbss.c │ └── waitkill.c ├── kern │ ├── process │ │ └── entry.S │ ├── driver │ │ ├── picirq.c │ │ ├── intr.h │ │ ├── picirq.h │ │ ├── clock.h │ │ ├── intr.c │ │ ├── console.h │ │ ├── ide.h │ │ ├── ide.c │ │ ├── console.c │ │ └── clock.c │ ├── mm │ │ ├── swap_fifo.h │ │ ├── default_pmm.h │ │ └── kmalloc.h │ ├── syscall │ │ └── syscall.h │ ├── schedule │ │ └── default_sched.h │ ├── fs │ │ ├── fs.h │ │ ├── swapfs.h │ │ └── swapfs.c │ ├── debug │ │ ├── kdebug.h │ │ ├── kmonitor.h │ │ └── assert.h │ └── sync │ │ ├── sem.h │ │ └── sync.h └── libs │ ├── stdlib.h │ ├── stdarg.h │ ├── hash.c │ ├── rand.c │ ├── stdio.h │ ├── string.h │ └── unistd.h ├── assets └── OS.png ├── lab0 ├── kern │ ├── mm │ │ ├── mmu.h │ │ └── memlayout.h │ ├── driver │ │ ├── console.h │ │ └── console.c │ └── init │ │ ├── init.c │ │ └── entry.S └── libs │ ├── stdarg.h │ ├── sbi.h │ ├── error.h │ ├── stdio.h │ ├── string.h │ └── sbi.c ├── README.md └── .gitignore /lab1/.projectile: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lab2/.projectile: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lab8/giveitatry.pyq: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lab1/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0x 3 | continue 4 | -------------------------------------------------------------------------------- /lab2/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab3/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab4/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab5/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab6/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab7/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /lab8/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | -------------------------------------------------------------------------------- /assets/OS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/assets/OS.png -------------------------------------------------------------------------------- /lab4/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0xffffffffc0200118 3 | continue 4 | -------------------------------------------------------------------------------- /lab5/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0xffffffffc0200154 3 | continue 4 | -------------------------------------------------------------------------------- /lab6/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0xffffffffc020009a 3 | continue 4 | -------------------------------------------------------------------------------- /lab7/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0xffffffffc020009a 3 | continue 4 | -------------------------------------------------------------------------------- /lab8/.gdb.in: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | break *0xffffffffc0200164 3 | continue 4 | -------------------------------------------------------------------------------- /lab8/disk0/sh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/sh -------------------------------------------------------------------------------- /lab1/tools/gdbinit: -------------------------------------------------------------------------------- 1 | file bin/kernel 2 | target remote :1234 3 | break kern_init 4 | continue -------------------------------------------------------------------------------- /lab8/disk0/exit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/exit -------------------------------------------------------------------------------- /lab8/disk0/spin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/spin -------------------------------------------------------------------------------- /lab8/disk0/badarg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/badarg -------------------------------------------------------------------------------- /lab8/disk0/divzero: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/divzero -------------------------------------------------------------------------------- /lab8/disk0/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/hello -------------------------------------------------------------------------------- /lab8/disk0/matrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/matrix -------------------------------------------------------------------------------- /lab8/disk0/pgdir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/pgdir -------------------------------------------------------------------------------- /lab8/disk0/sleep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/sleep -------------------------------------------------------------------------------- /lab8/disk0/softint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/softint -------------------------------------------------------------------------------- /lab8/disk0/testbss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/testbss -------------------------------------------------------------------------------- /lab8/disk0/yield: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/yield -------------------------------------------------------------------------------- /lab8/disk0/faultread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/faultread -------------------------------------------------------------------------------- /lab8/disk0/forktest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/forktest -------------------------------------------------------------------------------- /lab8/disk0/forktree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/forktree -------------------------------------------------------------------------------- /lab8/disk0/priority: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/priority -------------------------------------------------------------------------------- /lab8/disk0/sleepkill: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/sleepkill -------------------------------------------------------------------------------- /lab8/disk0/waitkill: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/waitkill -------------------------------------------------------------------------------- /lab8/disk0/badsegment: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/badsegment -------------------------------------------------------------------------------- /lab8/kern/driver/ramdisk.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ramdisk_init(int devno, struct ide_device *dev); 4 | -------------------------------------------------------------------------------- /lab8/disk0/faultreadkernel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-forever/NKU_OS_24fall/HEAD/lab8/disk0/faultreadkernel -------------------------------------------------------------------------------- /lab1/kern/mm/pmm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief Do nothing here in lab 1 5 | */ 6 | void pmm_init(void) {} 7 | -------------------------------------------------------------------------------- /lab5/user/libs/initcode.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl _start 3 | _start: 4 | # call user-program function 5 | call umain 6 | 1: j 1b 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab4/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | move a0, s1 5 | jalr s0 6 | 7 | jal do_exit 8 | -------------------------------------------------------------------------------- /lab5/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | move a0, s1 5 | jalr s0 6 | 7 | jal do_exit 8 | -------------------------------------------------------------------------------- /lab6/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | move a0, s1 5 | jalr s0 6 | 7 | jal do_exit 8 | -------------------------------------------------------------------------------- /lab7/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | move a0, s1 5 | jalr s0 6 | 7 | jal do_exit 8 | -------------------------------------------------------------------------------- /lab8/kern/process/entry.S: -------------------------------------------------------------------------------- 1 | .text 2 | .globl kernel_thread_entry 3 | kernel_thread_entry: # void kernel_thread(void) 4 | move a0, s1 5 | jalr s0 6 | 7 | jal do_exit 8 | -------------------------------------------------------------------------------- /lab3/kern/mm/swap_lru.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_LRU_H__ 2 | #define __KERN_MM_SWAP_LRU_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_lru; 6 | 7 | #endif -------------------------------------------------------------------------------- /lab3/kern/mm/swap_clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_SWAP_CLOCK_H__ 2 | #define __KERN_MM_SWAP_CLOCK_H__ 3 | 4 | #include 5 | extern struct swap_manager swap_manager_clock; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab4/kern/driver/picirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pic_enable(unsigned int irq) {} 4 | 5 | /* pic_init - initialize the 8259A interrupt controllers */ 6 | void pic_init(void) {} 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab5/kern/driver/picirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pic_enable(unsigned int irq) {} 4 | 5 | /* pic_init - initialize the 8259A interrupt controllers */ 6 | void pic_init(void) {} 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab6/kern/driver/picirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pic_enable(unsigned int irq) {} 4 | 5 | /* pic_init - initialize the 8259A interrupt controllers */ 6 | void pic_init(void) {} 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab7/kern/driver/picirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pic_enable(unsigned int irq) {} 4 | 5 | /* pic_init - initialize the 8259A interrupt controllers */ 6 | void pic_init(void) {} 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab8/kern/driver/picirq.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pic_enable(unsigned int irq) {} 4 | 5 | /* pic_init - initialize the 8259A interrupt controllers */ 6 | void pic_init(void) {} 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab6/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | // asm volatile("int $14"); 7 | exit(0); 8 | panic("FAIL: T.T\n"); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /lab7/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | // asm volatile("int $14"); 7 | exit(0); 8 | panic("FAIL: T.T\n"); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /lab8/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | // asm volatile("int $14"); 7 | exit(0); 8 | panic("FAIL: T.T\n"); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /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 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /lab5/user/softint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | // Never mind 7 | // asm volatile("int $14"); 8 | exit(0); 9 | panic("FAIL: T.T\n"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /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 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /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 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /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 | #endif /* !__KERN_DRIVER_PICIRQ_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /lab2/.score: -------------------------------------------------------------------------------- 1 | Check PMM: (2.3s) 2 | -check pmm: OK 3 | -check page table: OK 4 | -check ticks: OK 5 | Total Score: 50/50 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab2/kern/mm/best_fit_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_BEST_FIT_PMM_H__ 2 | #define __KERN_MM_BEST_FIT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager best_fit_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_BEST_FIT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /lab2/kern/mm/buddy_system_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_BUDDY_SYSTEM_PMM_H__ 2 | #define __KERN_MM_BUDDY_SYSTEM_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager buddy_system_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_BUDDY_SYSTEM_PMM_H__ */ -------------------------------------------------------------------------------- /lab3/kern/mm/best_fit_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_BEST_FIT_PMM_H__ 2 | #define __KERN_MM_BEST_FIT_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager best_fit_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_BEST_FIT_PMM_H__ */ 9 | 10 | -------------------------------------------------------------------------------- /lab3/kern/mm/buddy_system_pmm.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_BUDDY_SYSTEM_PMM_H__ 2 | #define __KERN_MM_BUDDY_SYSTEM_PMM_H__ 3 | 4 | #include 5 | 6 | extern const struct pmm_manager buddy_system_pmm_manager; 7 | 8 | #endif /* ! __KERN_MM_BUDDY_SYSTEM_PMM_H__ */ -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/kern/mm/mmu.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_MMU_H__ 2 | #define __KERN_MM_MMU_H__ 3 | 4 | #define PGSIZE 4096 // bytes mapped by a page 5 | #define PGSHIFT 12 // log2(PGSIZE) 6 | 7 | #endif /* !__KERN_MM_MMU_H__ */ 8 | -------------------------------------------------------------------------------- /lab1/kern/mm/mmu.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_MMU_H__ 2 | #define __KERN_MM_MMU_H__ 3 | 4 | #define PGSIZE 4096 // bytes mapped by a page 5 | #define PGSHIFT 12 // log2(PGSIZE) 6 | 7 | #endif /* !__KERN_MM_MMU_H__ */ 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /lab1/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /lab2/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /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 | void clock_set_next_event(void); 10 | 11 | #endif /* !__KERN_DRIVER_CLOCK_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /lab3/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab4/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab5/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab6/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab7/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab8/kern/driver/intr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* intr_enable - enable irq interrupt */ 5 | void intr_enable(void) { set_csr(sstatus, SSTATUS_SIE); } 6 | 7 | /* intr_disable - disable irq interrupt */ 8 | void intr_disable(void) { clear_csr(sstatus, SSTATUS_SIE); } 9 | -------------------------------------------------------------------------------- /lab0/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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) // 一页需要 (4096 / 512 = 8) 个磁盘扇区 8 | 9 | #define SWAP_DEV_NO 1 10 | 11 | #endif /* !__KERN_FS_FS_H__ */ -------------------------------------------------------------------------------- /lab3/tools/boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf64-rv64") 2 | OUTPUT_ARCH(rv64) 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/kern/mm/memlayout.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_MEMLAYOUT_H__ 2 | #define __KERN_MM_MEMLAYOUT_H__ 3 | 4 | #define KSTACKPAGE 2 // # of pages in kernel stack 5 | #define KSTACKSIZE (KSTACKPAGE * PGSIZE) // sizeof kernel stack 6 | 7 | #endif /* !__KERN_MM_MEMLAYOUT_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /lab1/kern/mm/memlayout.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_MM_MEMLAYOUT_H__ 2 | #define __KERN_MM_MEMLAYOUT_H__ 3 | 4 | #define KSTACKPAGE 2 // # of pages in kernel stack 5 | #define KSTACKSIZE (KSTACKPAGE * PGSIZE) // sizeof kernel stack 6 | 7 | #endif /* !__KERN_MM_MEMLAYOUT_H__ */ 8 | 9 | -------------------------------------------------------------------------------- /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 | // There is no such thing as TSS in RISC-V 9 | // asm volatile("movw $0x28,%ax; movw %ax,%ds"); 10 | panic("FAIL: T.T\n"); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /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: j 1b 14 | -------------------------------------------------------------------------------- /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: j 1b 14 | -------------------------------------------------------------------------------- /lab8/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: j 1b 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NKU_OS_24fall 2 | nku(Nankai University)南开大学操作系统课程实验 2024Fall 3 | 4 | ## Collaborators 5 | 6 | - [@lazy-forever](https://github.com/lazy-forever) 7 | - [@KaiserTT](https://github.com/KaiserTT) 8 | - [@GH2050](https://github.com/GH2050) 9 | 10 | Please star and follow us !!! 11 | 12 | ![os.png](assets/OS.png) 13 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/kern/init/init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int kern_init(void) __attribute__((noreturn)); 5 | 6 | int kern_init(void) { 7 | extern char edata[], end[]; 8 | memset(edata, 0, end - edata); 9 | 10 | const char *message = "(THU.CST) os is loading ...\n"; 11 | cprintf("%s\n\n", message); 12 | while (1) 13 | ; 14 | } 15 | -------------------------------------------------------------------------------- /lab0/kern/init/entry.S: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | .section .text,"ax",%progbits 5 | .globl kern_entry 6 | kern_entry: 7 | la sp, bootstacktop 8 | 9 | tail kern_init 10 | 11 | .section .data 12 | # .align 2^12 13 | .align PGSHIFT 14 | .global bootstack 15 | bootstack: 16 | .space KSTACKSIZE 17 | .global bootstacktop 18 | bootstacktop: -------------------------------------------------------------------------------- /lab1/kern/init/entry.S: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | .section .text,"ax",%progbits 5 | .globl kern_entry 6 | kern_entry: 7 | la sp, bootstacktop 8 | 9 | tail kern_init 10 | 11 | .section .data 12 | # .align 2^12 13 | .align PGSHIFT 14 | .global bootstack 15 | bootstack: 16 | .space KSTACKSIZE 17 | .global bootstacktop 18 | bootstacktop: -------------------------------------------------------------------------------- /lab5/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int64_t error_code); 5 | int sys_fork(void); 6 | int sys_wait(int64_t pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int64_t pid); 9 | int sys_getpid(void); 10 | int sys_putc(int64_t c); 11 | int sys_pgdir(void); 12 | 13 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 14 | 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab6/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int64_t error_code); 5 | int sys_fork(void); 6 | int sys_wait(int64_t pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int64_t pid); 9 | int sys_getpid(void); 10 | int sys_putc(int64_t c); 11 | int sys_pgdir(void); 12 | int sys_gettime(void); 13 | /* FOR LAB6 ONLY */ 14 | void sys_lab6_set_priority(uint64_t priority); 15 | 16 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 17 | 18 | -------------------------------------------------------------------------------- /lab7/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int64_t error_code); 5 | int sys_fork(void); 6 | int sys_wait(int64_t pid, int *store); 7 | int sys_yield(void); 8 | int sys_kill(int64_t pid); 9 | int sys_getpid(void); 10 | int sys_putc(int64_t c); 11 | int sys_pgdir(void); 12 | int sys_gettime(void); 13 | /* FOR LAB6 ONLY */ 14 | void sys_lab6_set_priority(uint64_t priority); 15 | 16 | int sys_sleep(uint64_t time); 17 | 18 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/libs/sbi.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_RISCV_SBI_H 2 | #define _ASM_RISCV_SBI_H 3 | 4 | typedef struct { 5 | unsigned long base; 6 | unsigned long size; 7 | unsigned long node_id; 8 | } memory_block_info; 9 | 10 | unsigned long sbi_query_memory(unsigned long id, memory_block_info *p); 11 | 12 | void sbi_set_timer(unsigned long long stime_value); 13 | void sbi_send_ipi(unsigned long hart_id); 14 | unsigned long sbi_clear_ipi(void); 15 | void sbi_shutdown(void); 16 | 17 | void sbi_console_putchar(unsigned char ch); 18 | int sbi_console_getchar(void); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 __intr_save(void) { 9 | if (read_csr(sstatus) & SSTATUS_SIE) { 10 | intr_disable(); 11 | return 1; 12 | } 13 | return 0; 14 | } 15 | 16 | static inline void __intr_restore(bool flag) { 17 | if (flag) { 18 | intr_enable(); 19 | } 20 | } 21 | 22 | #define local_intr_save(x) \ 23 | do { \ 24 | x = __intr_save(); \ 25 | } while (0) 26 | #define local_intr_restore(x) __intr_restore(x); 27 | 28 | #endif /* !__KERN_SYNC_SYNC_H__ */ 29 | -------------------------------------------------------------------------------- /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 __intr_save(void) { 9 | if (read_csr(sstatus) & SSTATUS_SIE) { 10 | intr_disable(); 11 | return 1; 12 | } 13 | return 0; 14 | } 15 | 16 | static inline void __intr_restore(bool flag) { 17 | if (flag) { 18 | intr_enable(); 19 | } 20 | } 21 | 22 | #define local_intr_save(x) \ 23 | do { \ 24 | x = __intr_save(); \ 25 | } while (0) 26 | #define local_intr_restore(x) __intr_restore(x); 27 | 28 | #endif /* !__KERN_SYNC_SYNC_H__ */ 29 | -------------------------------------------------------------------------------- /lab0/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 __intr_save(void) { 9 | if (read_csr(sstatus) & SSTATUS_SIE) { 10 | intr_disable(); 11 | return 1; 12 | } 13 | return 0; 14 | } 15 | 16 | static inline void __intr_restore(bool flag) { 17 | if (flag) { 18 | intr_enable(); 19 | } 20 | } 21 | 22 | #define local_intr_save(x) \ 23 | do { \ 24 | x = __intr_save(); \ 25 | } while (0) 26 | 27 | #define local_intr_restore(x) __intr_restore(x); 28 | 29 | #endif /* !__KERN_SYNC_SYNC_H__ */ 30 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 __intr_save(void) { 12 | if (read_csr(sstatus) & SSTATUS_SIE) { 13 | intr_disable(); 14 | return 1; 15 | } 16 | return 0; 17 | } 18 | 19 | static inline void __intr_restore(bool flag) { 20 | if (flag) { 21 | intr_enable(); 22 | } 23 | } 24 | 25 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 26 | #define local_intr_restore(x) __intr_restore(x); 27 | 28 | #endif /* !__KERN_SYNC_SYNC_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /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 __intr_save(void) { 12 | if (read_csr(sstatus) & SSTATUS_SIE) { 13 | intr_disable(); 14 | return 1; 15 | } 16 | return 0; 17 | } 18 | 19 | static inline void __intr_restore(bool flag) { 20 | if (flag) { 21 | intr_enable(); 22 | } 23 | } 24 | 25 | #define local_intr_save(x) do { x = __intr_save(); } while (0) 26 | #define local_intr_restore(x) __intr_restore(x); 27 | 28 | #endif /* !__KERN_SYNC_SYNC_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* kbd_intr - try to feed input characters from keyboard */ 5 | void kbd_intr(void) {} 6 | 7 | /* serial_intr - try to feed input characters from serial port */ 8 | void serial_intr(void) {} 9 | 10 | /* cons_init - initializes the console devices */ 11 | void cons_init(void) {} 12 | 13 | /* cons_putc - print a single character @c to console devices */ 14 | void cons_putc(int c) { sbi_console_putchar((unsigned char)c); } 15 | 16 | /* * 17 | * cons_getc - return the next input character from console, 18 | * or 0 if none waiting. 19 | * */ 20 | int cons_getc(void) { 21 | int c = 0; 22 | c = sbi_console_getchar(); 23 | return c; 24 | } 25 | -------------------------------------------------------------------------------- /lab1/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* kbd_intr - try to feed input characters from keyboard */ 5 | void kbd_intr(void) {} 6 | 7 | /* serial_intr - try to feed input characters from serial port */ 8 | void serial_intr(void) {} 9 | 10 | /* cons_init - initializes the console devices */ 11 | void cons_init(void) {} 12 | 13 | /* cons_putc - print a single character @c to console devices */ 14 | void cons_putc(int c) { sbi_console_putchar((unsigned char)c); } 15 | 16 | /* * 17 | * cons_getc - return the next input character from console, 18 | * or 0 if none waiting. 19 | * */ 20 | int cons_getc(void) { 21 | int c = 0; 22 | c = sbi_console_getchar(); 23 | return c; 24 | } 25 | -------------------------------------------------------------------------------- /lab2/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* kbd_intr - try to feed input characters from keyboard */ 5 | void kbd_intr(void) {} 6 | 7 | /* serial_intr - try to feed input characters from serial port */ 8 | void serial_intr(void) {} 9 | 10 | /* cons_init - initializes the console devices */ 11 | void cons_init(void) {} 12 | 13 | /* cons_putc - print a single character @c to console devices */ 14 | void cons_putc(int c) { sbi_console_putchar((unsigned char)c); } 15 | 16 | /* * 17 | * cons_getc - return the next input character from console, 18 | * or 0 if none waiting. 19 | * */ 20 | int cons_getc(void) { 21 | int c = 0; 22 | c = sbi_console_getchar(); 23 | return c; 24 | } 25 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab8/user/forktree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEPTH 3 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab7/user/spin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | main(void) { 6 | int pid, ret; 7 | cprintf("I am the parent. Forking the child...\n"); 8 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # lab 55 | lab*/obj 56 | lab*/bin 57 | 58 | # ide 59 | .idea/ -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lab0/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab2/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab3/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab4/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab5/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab6/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab7/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab8/tools/vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) { 5 | printf("# handler\n"); 6 | printf(".text\n"); 7 | printf(".globl __alltraps\n"); 8 | 9 | int i; 10 | for (i = 0; i < 256; i ++) { 11 | printf(".globl vector%d\n", i); 12 | printf("vector%d:\n", i); 13 | if ((i < 8 || i > 14) && i != 17) { 14 | printf(" pushl $0\n"); 15 | } 16 | printf(" pushl $%d\n", i); 17 | printf(" jmp __alltraps\n"); 18 | } 19 | printf("\n"); 20 | printf("# vector table\n"); 21 | printf(".data\n"); 22 | printf(".globl __vectors\n"); 23 | printf("__vectors:\n"); 24 | for (i = 0; i < 256; i ++) { 25 | printf(" .long vector%d\n", i); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /lab1/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab2/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab3/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab4/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab5/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab6/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab7/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *), void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *), void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab5/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /lab8/libs/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STDIO_H__ 2 | #define __LIBS_STDIO_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* kern/libs/stdio.c */ 8 | int cprintf(const char *fmt, ...); 9 | int vcprintf(const char *fmt, va_list ap); 10 | void cputchar(int c); 11 | int cputs(const char *str); 12 | int getchar(void); 13 | 14 | /* kern/libs/readline.c */ 15 | char *readline(const char *prompt); 16 | 17 | /* libs/printfmt.c */ 18 | void printfmt(void (*putch)(int, void *, int), int fd, void *putdat, const char *fmt, ...); 19 | void vprintfmt(void (*putch)(int, void *, int), int fd, void *putdat, const char *fmt, va_list ap); 20 | int snprintf(char *str, size_t size, const char *fmt, ...); 21 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 22 | 23 | #endif /* !__LIBS_STDIO_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab3/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab4/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab5/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab6/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab7/kern/fs/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab0/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab1/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab2/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab2/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab3/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab3/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab4/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab4/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab5/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab5/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab6/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab6/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab7/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab7/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | 12 | int strcmp(const char *s1, const char *s2); 13 | int strncmp(const char *s1, const char *s2, size_t n); 14 | 15 | char *strchr(const char *s, char c); 16 | char *strfind(const char *s, char c); 17 | long strtol(const char *s, char **endptr, int base); 18 | 19 | void *memset(void *s, char c, size_t n); 20 | void *memmove(void *dst, const void *src, size_t n); 21 | void *memcpy(void *dst, const void *src, size_t n); 22 | int memcmp(const void *v1, const void *v2, size_t n); 23 | 24 | #endif /* !__LIBS_STRING_H__ */ 25 | 26 | -------------------------------------------------------------------------------- /lab8/kern/debug/kmonitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_MONITOR_H__ 2 | #define __KERN_DEBUG_MONITOR_H__ 3 | 4 | #include 5 | 6 | void kmonitor(struct trapframe *tf); 7 | 8 | int mon_help(int argc, char **argv, struct trapframe *tf); 9 | int mon_kerninfo(int argc, char **argv, struct trapframe *tf); 10 | int mon_backtrace(int argc, char **argv, struct trapframe *tf); 11 | int mon_continue(int argc, char **argv, struct trapframe *tf); 12 | int mon_step(int argc, char **argv, struct trapframe *tf); 13 | int mon_breakpoint(int argc, char **argv, struct trapframe *tf); 14 | int mon_watchpoint(int argc, char **argv, struct trapframe *tf); 15 | int mon_delete_dr(int argc, char **argv, struct trapframe *tf); 16 | int mon_list_dr(int argc, char **argv, struct trapframe *tf); 17 | 18 | #endif /* !__KERN_DEBUG_MONITOR_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /lab8/kern/fs/swap/swapfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | swapfs_init(void) { 11 | static_assert((PGSIZE % SECTSIZE) == 0); 12 | if (!ide_device_valid(SWAP_DEV_NO)) { 13 | panic("swap fs isn't available.\n"); 14 | } 15 | max_swap_offset = ide_device_size(SWAP_DEV_NO) / (PGSIZE / SECTSIZE); 16 | } 17 | 18 | int 19 | swapfs_read(swap_entry_t entry, struct Page *page) { 20 | return ide_read_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 21 | } 22 | 23 | int 24 | swapfs_write(swap_entry_t entry, struct Page *page) { 25 | return ide_write_secs(SWAP_DEV_NO, swap_offset(entry) * PAGE_NSECT, page2kva(page), PAGE_NSECT); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /lab8/user/libs/umain.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char *argv[]); 7 | 8 | static int 9 | initfd(int fd2, const char *path, uint32_t open_flags) { 10 | int fd1, ret; 11 | if ((fd1 = open(path, open_flags)) < 0) { 12 | return fd1; 13 | } 14 | if (fd1 != fd2) { 15 | close(fd2); 16 | ret = dup2(fd1, fd2); 17 | close(fd1); 18 | } 19 | return ret; 20 | } 21 | 22 | void 23 | umain(int argc, char *argv[]) { 24 | int fd; 25 | if ((fd = initfd(0, "stdin:", O_RDONLY)) < 0) { 26 | warn("open failed: %e.\n", fd); 27 | } 28 | if ((fd = initfd(1, "stdout:", O_WRONLY)) < 0) { 29 | warn("open failed: %e.\n", fd); 30 | } 31 | int ret = main(argc, argv); 32 | exit(ret); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab5/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab6/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab7/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab8/user/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int magic = -0x10384; 5 | 6 | int 7 | main(void) { 8 | int pid, code; 9 | cprintf("I am the parent. Forking the child...\n"); 10 | if ((pid = fork()) == 0) { 11 | cprintf("I am the child.\n"); 12 | yield(); 13 | yield(); 14 | yield(); 15 | yield(); 16 | yield(); 17 | yield(); 18 | yield(); 19 | exit(magic); 20 | } 21 | else { 22 | cprintf("I am parent, fork a child pid %d\n",pid); 23 | } 24 | assert(pid > 0); 25 | cprintf("I am the parent, waiting now..\n"); 26 | 27 | assert(waitpid(pid, &code) == 0 && code == magic); 28 | assert(waitpid(pid, &code) != 0 && wait() != 0); 29 | cprintf("waitpid %d ok.\n", pid); 30 | 31 | cprintf("exit pass.\n"); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab8/user/libs/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_LOCK_H__ 2 | #define __USER_LIBS_LOCK_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define INIT_LOCK {0} 9 | 10 | typedef volatile bool lock_t; 11 | 12 | static inline void 13 | lock_init(lock_t *l) { 14 | *l = 0; 15 | } 16 | 17 | static inline bool 18 | try_lock(lock_t *l) { 19 | return test_and_set_bit(0, l); 20 | } 21 | 22 | static inline void 23 | lock(lock_t *l) { 24 | if (try_lock(l)) { 25 | int step = 0; 26 | do { 27 | yield(); 28 | if (++ step == 100) { 29 | step = 0; 30 | sleep(10); 31 | } 32 | } while (try_lock(l)); 33 | } 34 | } 35 | 36 | static inline void 37 | unlock(lock_t *l) { 38 | test_and_clear_bit(0, l); 39 | } 40 | 41 | #endif /* !__USER_LIBS_LOCK_H__ */ 42 | 43 | -------------------------------------------------------------------------------- /lab1/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) __warn(__FILE__, __LINE__, __VA_ARGS__) 10 | 11 | #define panic(...) __panic(__FILE__, __LINE__, __VA_ARGS__) 12 | 13 | #define assert(x) \ 14 | do { \ 15 | if (!(x)) { \ 16 | panic("assertion failed: %s", #x); \ 17 | } \ 18 | } while (0) 19 | 20 | // static_assert(x) will generate a compile-time error if 'x' is false. 21 | #define static_assert(x) switch (x){case 0 : case (x):; } 22 | 23 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 24 | -------------------------------------------------------------------------------- /lab6/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | // asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /lab7/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | // asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /lab8/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | // asm volatile ("int $0x14"); 31 | panic("FAIL: T.T\n"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /lab5/user/testbss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ARRAYSIZE (1024*1024) 5 | 6 | uint32_t bigarray[ARRAYSIZE]; 7 | 8 | int 9 | main(void) { 10 | cprintf("Making sure bss works right...\n"); 11 | int i; 12 | for (i = 0; i < ARRAYSIZE; i ++) { 13 | if (bigarray[i] != 0) { 14 | panic("bigarray[%d] isn't cleared!\n", i); 15 | } 16 | } 17 | for (i = 0; i < ARRAYSIZE; i ++) { 18 | bigarray[i] = i; 19 | } 20 | for (i = 0; i < ARRAYSIZE; i ++) { 21 | if (bigarray[i] != i) { 22 | panic("bigarray[%d] didn't hold its value!\n", i); 23 | } 24 | } 25 | 26 | cprintf("Yes, good. Now doing a wild write off the end...\n"); 27 | cprintf("testbss may pass.\n"); 28 | 29 | bigarray[ARRAYSIZE + 1024] = 0; 30 | // asm volatile ("int $0x14"); 31 | exit(0); 32 | panic("FAIL: T.T\n"); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /lab5/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | 24 | /* SYS_fork flags */ 25 | #define CLONE_VM 0x00000100 // set if VM shared between processes 26 | #define CLONE_THREAD 0x00000200 // thread group 27 | 28 | #endif /* !__LIBS_UNISTD_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /lab5/tools/user.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for ucore user-level programs. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_ARCH(riscv) 5 | ENTRY(_start) 6 | 7 | SECTIONS { 8 | /* Load programs at this address: "." means the current address */ 9 | . = 0x800020; 10 | 11 | .text : { 12 | *(.text .stub .text.* .gnu.linkonce.t.*) 13 | } 14 | 15 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 16 | 17 | .rodata : { 18 | *(.rodata .rodata.* .gnu.linkonce.r.*) 19 | } 20 | 21 | /* Adjust the address for the data segment to the next page */ 22 | . = ALIGN(0x1000); 23 | 24 | .data : { 25 | *(.data) 26 | } 27 | 28 | PROVIDE(edata = .); 29 | 30 | .bss : { 31 | *(.bss) 32 | } 33 | 34 | PROVIDE(end = .); 35 | 36 | /DISCARD/ : { 37 | *(.eh_frame .note.GNU-stack .comment) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lab6/tools/user.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for ucore user-level programs. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_ARCH(riscv) 5 | ENTRY(_start) 6 | 7 | SECTIONS { 8 | /* Load programs at this address: "." means the current address */ 9 | . = 0x800020; 10 | 11 | .text : { 12 | *(.text .stub .text.* .gnu.linkonce.t.*) 13 | } 14 | 15 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 16 | 17 | .rodata : { 18 | *(.rodata .rodata.* .gnu.linkonce.r.*) 19 | } 20 | 21 | /* Adjust the address for the data segment to the next page */ 22 | . = ALIGN(0x1000); 23 | 24 | .data : { 25 | *(.data) 26 | } 27 | 28 | PROVIDE(edata = .); 29 | 30 | .bss : { 31 | *(.bss) 32 | } 33 | 34 | PROVIDE(end = .); 35 | 36 | /DISCARD/ : { 37 | *(.eh_frame .note.GNU-stack .comment) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lab7/tools/user.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for ucore user-level programs. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_ARCH(riscv) 5 | ENTRY(_start) 6 | 7 | SECTIONS { 8 | /* Load programs at this address: "." means the current address */ 9 | . = 0x800020; 10 | 11 | .text : { 12 | *(.text .stub .text.* .gnu.linkonce.t.*) 13 | } 14 | 15 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 16 | 17 | .rodata : { 18 | *(.rodata .rodata.* .gnu.linkonce.r.*) 19 | } 20 | 21 | /* Adjust the address for the data segment to the next page */ 22 | . = ALIGN(0x1000); 23 | 24 | .data : { 25 | *(.data) 26 | } 27 | 28 | PROVIDE(edata = .); 29 | 30 | .bss : { 31 | *(.bss) 32 | } 33 | 34 | PROVIDE(end = .); 35 | 36 | /DISCARD/ : { 37 | *(.eh_frame .note.GNU-stack .comment) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lab8/tools/user.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for ucore user-level programs. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_ARCH(riscv) 5 | ENTRY(_start) 6 | 7 | SECTIONS { 8 | /* Load programs at this address: "." means the current address */ 9 | . = 0x800020; 10 | 11 | .text : { 12 | *(.text .stub .text.* .gnu.linkonce.t.*) 13 | } 14 | 15 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 16 | 17 | .rodata : { 18 | *(.rodata .rodata.* .gnu.linkonce.r.*) 19 | } 20 | 21 | /* Adjust the address for the data segment to the next page */ 22 | . = ALIGN(0x1000); 23 | 24 | .data : { 25 | *(.data) 26 | } 27 | 28 | PROVIDE(edata = .); 29 | 30 | .bss : { 31 | *(.bss) 32 | } 33 | 34 | PROVIDE(end = .); 35 | 36 | /DISCARD/ : { 37 | *(.eh_frame .note.GNU-stack .comment) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lab8/libs/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_STRING_H__ 2 | #define __LIBS_STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *s); 7 | size_t strnlen(const char *s, size_t len); 8 | 9 | char *strcpy(char *dst, const char *src); 10 | char *strncpy(char *dst, const char *src, size_t len); 11 | char *strcat(char *dst, const char *src); 12 | char *strdup(const char *src); 13 | char *stradd(const char *src1, const char *src2); 14 | 15 | int strcmp(const char *s1, const char *s2); 16 | int strncmp(const char *s1, const char *s2, size_t n); 17 | 18 | char *strchr(const char *s, char c); 19 | char *strfind(const char *s, char c); 20 | long strtol(const char *s, char **endptr, int base); 21 | 22 | void *memset(void *s, char c, size_t n); 23 | void *memmove(void *dst, const void *src, size_t n); 24 | void *memcpy(void *dst, const void *src, size_t n); 25 | int memcmp(const void *v1, const void *v2, size_t n); 26 | 27 | #endif /* !__LIBS_STRING_H__ */ 28 | 29 | -------------------------------------------------------------------------------- /lab3/kern/driver/ide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void ide_init(void) {} 11 | 12 | #define MAX_IDE 2 13 | #define MAX_DISK_NSECS 56 14 | static char ide[MAX_DISK_NSECS * SECTSIZE]; 15 | 16 | bool ide_device_valid(unsigned short ideno) { return ideno < MAX_IDE; } 17 | 18 | size_t ide_device_size(unsigned short ideno) { return MAX_DISK_NSECS; } 19 | 20 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, 21 | size_t nsecs) { 22 | int iobase = secno * SECTSIZE; 23 | memcpy(dst, &ide[iobase], nsecs * SECTSIZE); 24 | return 0; 25 | } 26 | 27 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, 28 | size_t nsecs) { 29 | int iobase = secno * SECTSIZE; 30 | memcpy(&ide[iobase], src, nsecs * SECTSIZE); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /lab6/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | /* OLNY FOR LAB6 */ 24 | #define SYS_lab6_set_priority 255 25 | 26 | /* SYS_fork flags */ 27 | #define CLONE_VM 0x00000100 // set if VM shared between processes 28 | #define CLONE_THREAD 0x00000200 // thread group 29 | 30 | #endif /* !__LIBS_UNISTD_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /lab7/libs/unistd.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBS_UNISTD_H__ 2 | #define __LIBS_UNISTD_H__ 3 | 4 | #define T_SYSCALL 0x80 5 | 6 | /* syscall number */ 7 | #define SYS_exit 1 8 | #define SYS_fork 2 9 | #define SYS_wait 3 10 | #define SYS_exec 4 11 | #define SYS_clone 5 12 | #define SYS_yield 10 13 | #define SYS_sleep 11 14 | #define SYS_kill 12 15 | #define SYS_gettime 17 16 | #define SYS_getpid 18 17 | #define SYS_brk 19 18 | #define SYS_mmap 20 19 | #define SYS_munmap 21 20 | #define SYS_shmem 22 21 | #define SYS_putc 30 22 | #define SYS_pgdir 31 23 | /*only for lab6*/ 24 | #define SYS_lab6_set_priority 255 25 | 26 | /* SYS_fork flags */ 27 | #define CLONE_VM 0x00000100 // set if VM shared between processes 28 | #define CLONE_THREAD 0x00000200 // thread group 29 | 30 | #endif /* !__LIBS_UNISTD_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /lab4/kern/driver/ide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void ide_init(void) {} 12 | 13 | #define MAX_IDE 2 14 | #define MAX_DISK_NSECS 56 15 | static char ide[MAX_DISK_NSECS * SECTSIZE]; 16 | 17 | bool ide_device_valid(unsigned short ideno) { return ideno < MAX_IDE; } 18 | 19 | size_t ide_device_size(unsigned short ideno) { return MAX_DISK_NSECS; } 20 | 21 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, 22 | size_t nsecs) { 23 | int iobase = secno * SECTSIZE; 24 | memcpy(dst, &ide[iobase], nsecs * SECTSIZE); 25 | return 0; 26 | } 27 | 28 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, 29 | size_t nsecs) { 30 | int iobase = secno * SECTSIZE; 31 | memcpy(&ide[iobase], src, nsecs * SECTSIZE); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /lab5/kern/driver/ide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void ide_init(void) {} 12 | 13 | #define MAX_IDE 2 14 | #define MAX_DISK_NSECS 56 15 | static char ide[MAX_DISK_NSECS * SECTSIZE]; 16 | 17 | bool ide_device_valid(unsigned short ideno) { return ideno < MAX_IDE; } 18 | 19 | size_t ide_device_size(unsigned short ideno) { return MAX_DISK_NSECS; } 20 | 21 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, 22 | size_t nsecs) { 23 | int iobase = secno * SECTSIZE; 24 | memcpy(dst, &ide[iobase], nsecs * SECTSIZE); 25 | return 0; 26 | } 27 | 28 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, 29 | size_t nsecs) { 30 | int iobase = secno * SECTSIZE; 31 | memcpy(&ide[iobase], src, nsecs * SECTSIZE); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /lab6/kern/driver/ide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void ide_init(void) {} 12 | 13 | #define MAX_IDE 2 14 | #define MAX_DISK_NSECS 56 15 | static char ide[MAX_DISK_NSECS * SECTSIZE]; 16 | 17 | bool ide_device_valid(unsigned short ideno) { return ideno < MAX_IDE; } 18 | 19 | size_t ide_device_size(unsigned short ideno) { return MAX_DISK_NSECS; } 20 | 21 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, 22 | size_t nsecs) { 23 | int iobase = secno * SECTSIZE; 24 | memcpy(dst, &ide[iobase], nsecs * SECTSIZE); 25 | return 0; 26 | } 27 | 28 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, 29 | size_t nsecs) { 30 | int iobase = secno * SECTSIZE; 31 | memcpy(&ide[iobase], src, nsecs * SECTSIZE); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /lab7/kern/driver/ide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void ide_init(void) {} 12 | 13 | #define MAX_IDE 2 14 | #define MAX_DISK_NSECS 56 15 | static char ide[MAX_DISK_NSECS * SECTSIZE]; 16 | 17 | bool ide_device_valid(unsigned short ideno) { return ideno < MAX_IDE; } 18 | 19 | size_t ide_device_size(unsigned short ideno) { return MAX_DISK_NSECS; } 20 | 21 | int ide_read_secs(unsigned short ideno, uint32_t secno, void *dst, 22 | size_t nsecs) { 23 | int iobase = secno * SECTSIZE; 24 | memcpy(dst, &ide[iobase], nsecs * SECTSIZE); 25 | return 0; 26 | } 27 | 28 | int ide_write_secs(unsigned short ideno, uint32_t secno, const void *src, 29 | size_t nsecs) { 30 | int iobase = secno * SECTSIZE; 31 | memcpy(&ide[iobase], src, nsecs * SECTSIZE); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /lab6/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | unsigned int 50 | gettime_msec(void) { 51 | return (unsigned int)sys_gettime(); 52 | } 53 | 54 | void 55 | lab6_setpriority(uint32_t priority) 56 | { 57 | sys_lab6_set_priority(priority); 58 | } 59 | -------------------------------------------------------------------------------- /lab8/user/libs/dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | DIR dir, *dirp=&dir; 12 | DIR * 13 | opendir(const char *path) { 14 | 15 | if ((dirp->fd = open(path, O_RDONLY)) < 0) { 16 | goto failed; 17 | } 18 | struct stat __stat, *stat = &__stat; 19 | if (fstat(dirp->fd, stat) != 0 || !S_ISDIR(stat->st_mode)) { 20 | goto failed; 21 | } 22 | dirp->dirent.offset = 0; 23 | return dirp; 24 | 25 | failed: 26 | return NULL; 27 | } 28 | 29 | struct dirent * 30 | readdir(DIR *dirp) { 31 | if (sys_getdirentry(dirp->fd, &(dirp->dirent)) == 0) { 32 | return &(dirp->dirent); 33 | } 34 | return NULL; 35 | } 36 | 37 | void 38 | closedir(DIR *dirp) { 39 | close(dirp->fd); 40 | } 41 | 42 | int 43 | getcwd(char *buffer, size_t len) { 44 | return sys_getcwd(buffer, len); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /lab8/kern/fs/iobuf.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_IOBUF_H__ 2 | #define __KERN_FS_IOBUF_H__ 3 | 4 | #include 5 | 6 | /* 7 | * iobuf is a buffer Rd/Wr status record 8 | */ 9 | struct iobuf { 10 | void *io_base; // the base addr of buffer (used for Rd/Wr) 11 | off_t io_offset; // current Rd/Wr position in buffer, will have been incremented by the amount transferred 12 | size_t io_len; // the length of buffer (used for Rd/Wr) 13 | size_t io_resid; // current resident length need to Rd/Wr, will have been decremented by the amount transferred. 14 | }; 15 | 16 | #define iobuf_used(iob) ((size_t)((iob)->io_len - (iob)->io_resid)) 17 | 18 | struct iobuf *iobuf_init(struct iobuf *iob, void *base, size_t len, off_t offset); 19 | int iobuf_move(struct iobuf *iob, void *data, size_t len, bool m2b, size_t *copiedp); 20 | int iobuf_move_zeros(struct iobuf *iob, size_t len, size_t *copiedp); 21 | void iobuf_skip(struct iobuf *iob, size_t n); 22 | 23 | #endif /* !__KERN_FS_IOBUF_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /lab3/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* kbd_intr - try to feed input characters from keyboard */ 7 | void kbd_intr(void) {} 8 | 9 | /* serial_intr - try to feed input characters from serial port */ 10 | void serial_intr(void) {} 11 | 12 | /* cons_init - initializes the console devices */ 13 | void cons_init(void) {} 14 | 15 | /* cons_putc - print a single character @c to console devices */ 16 | void cons_putc(int c) { 17 | bool intr_flag; 18 | local_intr_save(intr_flag); 19 | { 20 | sbi_console_putchar((unsigned char)c); 21 | } 22 | local_intr_restore(intr_flag); 23 | } 24 | 25 | /* * 26 | * cons_getc - return the next input character from console, 27 | * or 0 if none waiting. 28 | * */ 29 | int cons_getc(void) { 30 | int c = 0; 31 | bool intr_flag; 32 | local_intr_save(intr_flag); 33 | { 34 | c = sbi_console_getchar(); 35 | } 36 | local_intr_restore(intr_flag); 37 | return c; 38 | } 39 | -------------------------------------------------------------------------------- /lab4/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* kbd_intr - try to feed input characters from keyboard */ 7 | void kbd_intr(void) {} 8 | 9 | /* serial_intr - try to feed input characters from serial port */ 10 | void serial_intr(void) {} 11 | 12 | /* cons_init - initializes the console devices */ 13 | void cons_init(void) {} 14 | 15 | /* cons_putc - print a single character @c to console devices */ 16 | void cons_putc(int c) { 17 | bool intr_flag; 18 | local_intr_save(intr_flag); 19 | { 20 | sbi_console_putchar((unsigned char)c); 21 | } 22 | local_intr_restore(intr_flag); 23 | } 24 | 25 | /* * 26 | * cons_getc - return the next input character from console, 27 | * or 0 if none waiting. 28 | * */ 29 | int cons_getc(void) { 30 | int c = 0; 31 | bool intr_flag; 32 | local_intr_save(intr_flag); 33 | { 34 | c = sbi_console_getchar(); 35 | } 36 | local_intr_restore(intr_flag); 37 | return c; 38 | } 39 | -------------------------------------------------------------------------------- /lab5/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* kbd_intr - try to feed input characters from keyboard */ 7 | void kbd_intr(void) {} 8 | 9 | /* serial_intr - try to feed input characters from serial port */ 10 | void serial_intr(void) {} 11 | 12 | /* cons_init - initializes the console devices */ 13 | void cons_init(void) {} 14 | 15 | /* cons_putc - print a single character @c to console devices */ 16 | void cons_putc(int c) { 17 | bool intr_flag; 18 | local_intr_save(intr_flag); 19 | { 20 | sbi_console_putchar((unsigned char)c); 21 | } 22 | local_intr_restore(intr_flag); 23 | } 24 | 25 | /* * 26 | * cons_getc - return the next input character from console, 27 | * or 0 if none waiting. 28 | * */ 29 | int cons_getc(void) { 30 | int c = 0; 31 | bool intr_flag; 32 | local_intr_save(intr_flag); 33 | { 34 | c = sbi_console_getchar(); 35 | } 36 | local_intr_restore(intr_flag); 37 | return c; 38 | } 39 | -------------------------------------------------------------------------------- /lab6/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* kbd_intr - try to feed input characters from keyboard */ 7 | void kbd_intr(void) {} 8 | 9 | /* serial_intr - try to feed input characters from serial port */ 10 | void serial_intr(void) {} 11 | 12 | /* cons_init - initializes the console devices */ 13 | void cons_init(void) {} 14 | 15 | /* cons_putc - print a single character @c to console devices */ 16 | void cons_putc(int c) { 17 | bool intr_flag; 18 | local_intr_save(intr_flag); 19 | { 20 | sbi_console_putchar((unsigned char)c); 21 | } 22 | local_intr_restore(intr_flag); 23 | } 24 | 25 | /* * 26 | * cons_getc - return the next input character from console, 27 | * or 0 if none waiting. 28 | * */ 29 | int cons_getc(void) { 30 | int c = 0; 31 | bool intr_flag; 32 | local_intr_save(intr_flag); 33 | { 34 | c = sbi_console_getchar(); 35 | } 36 | local_intr_restore(intr_flag); 37 | return c; 38 | } 39 | -------------------------------------------------------------------------------- /lab7/kern/driver/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* kbd_intr - try to feed input characters from keyboard */ 7 | void kbd_intr(void) {} 8 | 9 | /* serial_intr - try to feed input characters from serial port */ 10 | void serial_intr(void) {} 11 | 12 | /* cons_init - initializes the console devices */ 13 | void cons_init(void) {} 14 | 15 | /* cons_putc - print a single character @c to console devices */ 16 | void cons_putc(int c) { 17 | bool intr_flag; 18 | local_intr_save(intr_flag); 19 | { 20 | sbi_console_putchar((unsigned char)c); 21 | } 22 | local_intr_restore(intr_flag); 23 | } 24 | 25 | /* * 26 | * cons_getc - return the next input character from console, 27 | * or 0 if none waiting. 28 | * */ 29 | int cons_getc(void) { 30 | int c = 0; 31 | bool intr_flag; 32 | local_intr_save(intr_flag); 33 | { 34 | c = sbi_console_getchar(); 35 | } 36 | local_intr_restore(intr_flag); 37 | return c; 38 | } 39 | -------------------------------------------------------------------------------- /lab8/kern/fs/sfs/sfs_lock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /* 7 | * lock_sfs_fs - lock the process of SFS Filesystem Rd/Wr Disk Block 8 | * 9 | * called by: sfs_load_inode, sfs_sync, sfs_reclaim 10 | */ 11 | void 12 | lock_sfs_fs(struct sfs_fs *sfs) { 13 | down(&(sfs->fs_sem)); 14 | } 15 | 16 | /* 17 | * lock_sfs_io - lock the process of SFS File Rd/Wr Disk Block 18 | * 19 | * called by: sfs_rwblock, sfs_clear_block, sfs_sync_super 20 | */ 21 | void 22 | lock_sfs_io(struct sfs_fs *sfs) { 23 | down(&(sfs->io_sem)); 24 | } 25 | 26 | /* 27 | * unlock_sfs_fs - unlock the process of SFS Filesystem Rd/Wr Disk Block 28 | * 29 | * called by: sfs_load_inode, sfs_sync, sfs_reclaim 30 | */ 31 | void 32 | unlock_sfs_fs(struct sfs_fs *sfs) { 33 | up(&(sfs->fs_sem)); 34 | } 35 | 36 | /* 37 | * unlock_sfs_io - unlock the process of sfs Rd/Wr Disk Block 38 | * 39 | * called by: sfs_rwblock sfs_clear_block sfs_sync_super 40 | */ 41 | void 42 | unlock_sfs_io(struct sfs_fs *sfs) { 43 | up(&(sfs->io_sem)); 44 | } 45 | -------------------------------------------------------------------------------- /lab1/libs/sbi.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_RISCV_SBI_H 2 | #define _ASM_RISCV_SBI_H 3 | 4 | typedef struct { 5 | unsigned long base; 6 | unsigned long size; 7 | unsigned long node_id; 8 | } memory_block_info; 9 | 10 | unsigned long sbi_query_memory(unsigned long id, memory_block_info *p); 11 | 12 | unsigned long sbi_hart_id(void); 13 | unsigned long sbi_num_harts(void); 14 | unsigned long sbi_timebase(void); 15 | void sbi_set_timer(unsigned long long stime_value); 16 | void sbi_send_ipi(unsigned long hart_id); 17 | unsigned long sbi_clear_ipi(void); 18 | void sbi_shutdown(void); 19 | 20 | void sbi_console_putchar(unsigned char ch); 21 | int sbi_console_getchar(void); 22 | 23 | void sbi_remote_sfence_vm(unsigned long hart_mask_ptr, unsigned long asid); 24 | void sbi_remote_sfence_vm_range(unsigned long hart_mask_ptr, unsigned long asid, unsigned long start, unsigned long size); 25 | void sbi_remote_fence_i(unsigned long hart_mask_ptr); 26 | 27 | unsigned long sbi_mask_interrupt(unsigned long which); 28 | unsigned long sbi_unmask_interrupt(unsigned long which); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /lab2/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab2/libs/sbi.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_RISCV_SBI_H 2 | #define _ASM_RISCV_SBI_H 3 | 4 | typedef struct { 5 | unsigned long base; 6 | unsigned long size; 7 | unsigned long node_id; 8 | } memory_block_info; 9 | 10 | unsigned long sbi_query_memory(unsigned long id, memory_block_info *p); 11 | 12 | unsigned long sbi_hart_id(void); 13 | unsigned long sbi_num_harts(void); 14 | unsigned long sbi_timebase(void); 15 | void sbi_set_timer(unsigned long long stime_value); 16 | void sbi_send_ipi(unsigned long hart_id); 17 | unsigned long sbi_clear_ipi(void); 18 | void sbi_shutdown(void); 19 | 20 | void sbi_console_putchar(unsigned char ch); 21 | int sbi_console_getchar(void); 22 | 23 | void sbi_remote_sfence_vm(unsigned long hart_mask_ptr, unsigned long asid); 24 | void sbi_remote_sfence_vm_range(unsigned long hart_mask_ptr, unsigned long asid, unsigned long start, unsigned long size); 25 | void sbi_remote_fence_i(unsigned long hart_mask_ptr); 26 | 27 | unsigned long sbi_mask_interrupt(unsigned long which); 28 | unsigned long sbi_unmask_interrupt(unsigned long which); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /lab3/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab4/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab5/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab6/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab7/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab8/kern/debug/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_DEBUG_ASSERT_H__ 2 | #define __KERN_DEBUG_ASSERT_H__ 3 | 4 | #include 5 | 6 | void __warn(const char *file, int line, const char *fmt, ...); 7 | void __noreturn __panic(const char *file, int line, const char *fmt, ...); 8 | 9 | #define warn(...) \ 10 | __warn(__FILE__, __LINE__, __VA_ARGS__) 11 | 12 | #define panic(...) \ 13 | __panic(__FILE__, __LINE__, __VA_ARGS__) 14 | 15 | #define assert(x) \ 16 | do { \ 17 | if (!(x)) { \ 18 | panic("assertion failed: %s", #x); \ 19 | } \ 20 | } while (0) 21 | 22 | // static_assert(x) will generate a compile-time error if 'x' is false. 23 | #define static_assert(x) \ 24 | switch (x) { case 0: case (x): ; } 25 | 26 | #endif /* !__KERN_DEBUG_ASSERT_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /lab8/kern/fs/devs/dev.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_FS_DEVS_DEV_H__ 2 | #define __KERN_FS_DEVS_DEV_H__ 3 | 4 | #include 5 | 6 | struct inode; 7 | struct iobuf; 8 | 9 | /* 10 | * Filesystem-namespace-accessible device. 11 | * d_io is for both reads and writes; the iobuf will indicates the direction. 12 | */ 13 | struct device { 14 | size_t d_blocks; 15 | size_t d_blocksize; 16 | int (*d_open)(struct device *dev, uint32_t open_flags); 17 | int (*d_close)(struct device *dev); 18 | int (*d_io)(struct device *dev, struct iobuf *iob, bool write); 19 | int (*d_ioctl)(struct device *dev, int op, void *data); 20 | }; 21 | 22 | #define dop_open(dev, open_flags) ((dev)->d_open(dev, open_flags)) 23 | #define dop_close(dev) ((dev)->d_close(dev)) 24 | #define dop_io(dev, iob, write) ((dev)->d_io(dev, iob, write)) 25 | #define dop_ioctl(dev, op, data) ((dev)->d_ioctl(dev, op, data)) 26 | 27 | void dev_init(void); 28 | struct inode *dev_create_inode(void); 29 | 30 | #endif /* !__KERN_FS_DEVS_DEV_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /lab1/.score: -------------------------------------------------------------------------------- 1 | Check Output: (2.3s) 2 | -check ring 0: WRONG 3 | -e !! error: missing '0: @ring 0' 4 | !! error: missing '0: cs = 8' 5 | !! error: missing '0: ds = 10' 6 | !! error: missing '0: es = 10' 7 | !! error: missing '0: ss = 10' 8 | 9 | -check switch to ring 3: WRONG 10 | -e !! error: missing '+++ switch to user mode +++' 11 | !! error: missing '1: @ring 3' 12 | !! error: missing '1: cs = 1b' 13 | !! error: missing '1: ds = 23' 14 | !! error: missing '1: es = 23' 15 | !! error: missing '1: ss = 23' 16 | 17 | -check switch to ring 0: WRONG 18 | -e !! error: missing '+++ switch to kernel mode +++' 19 | !! error: missing '2: @ring 0' 20 | !! error: missing '2: cs = 8' 21 | !! error: missing '2: ds = 10' 22 | !! error: missing '2: es = 10' 23 | !! error: missing '2: ss = 10' 24 | 25 | -check ticks: OK 26 | Total Score: 10/40 27 | Makefile:240: recipe for target 'grade' failed 28 | make: *** [grade] Error 1 29 | -------------------------------------------------------------------------------- /lab7/user/libs/ulib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | exit(int error_code) { 8 | sys_exit(error_code); 9 | cprintf("BUG: exit failed.\n"); 10 | while (1); 11 | } 12 | 13 | int 14 | fork(void) { 15 | return sys_fork(); 16 | } 17 | 18 | int 19 | wait(void) { 20 | return sys_wait(0, NULL); 21 | } 22 | 23 | int 24 | waitpid(int pid, int *store) { 25 | return sys_wait(pid, store); 26 | } 27 | 28 | void 29 | yield(void) { 30 | sys_yield(); 31 | } 32 | 33 | int 34 | kill(int pid) { 35 | return sys_kill(pid); 36 | } 37 | 38 | int 39 | getpid(void) { 40 | return sys_getpid(); 41 | } 42 | 43 | //print_pgdir - print the PDT&PT 44 | void 45 | print_pgdir(void) { 46 | sys_pgdir(); 47 | } 48 | 49 | unsigned int 50 | gettime_msec(void) { 51 | return (unsigned int)sys_gettime(); 52 | } 53 | 54 | void 55 | lab6_set_priority(uint32_t priority) 56 | { 57 | sys_lab6_set_priority(priority); 58 | } 59 | 60 | int 61 | sleep(unsigned int time) { 62 | return sys_sleep(time); 63 | } 64 | -------------------------------------------------------------------------------- /lab8/user/libs/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIBS_SYSCALL_H__ 2 | #define __USER_LIBS_SYSCALL_H__ 3 | 4 | int sys_exit(int64_t error_code); 5 | int sys_fork(void); 6 | int sys_wait(int64_t pid, int64_t *store); 7 | int sys_exec(const char *name, int64_t argc, const char **argv); 8 | int sys_yield(void); 9 | int sys_kill(int64_t pid); 10 | int sys_getpid(void); 11 | int sys_putc(int64_t c); 12 | int sys_pgdir(void); 13 | int sys_sleep(int64_t time); 14 | int sys_gettime(void); 15 | 16 | struct stat; 17 | struct dirent; 18 | 19 | int sys_open(const char *path, uint64_t open_flags); 20 | int sys_close(int64_t fd); 21 | int sys_read(int64_t fd, void *base, size_t len); 22 | int sys_write(int64_t fd, void *base, size_t len); 23 | int sys_seek(int64_t fd, off_t pos, int64_t whence); 24 | int sys_fstat(int64_t fd, struct stat *stat); 25 | int sys_fsync(int64_t fd); 26 | int sys_getcwd(char *buffer, size_t len); 27 | int sys_getdirentry(int64_t fd, struct dirent *dirent); 28 | int sys_dup(int64_t fd1, int64_t fd2); 29 | void sys_lab6_set_priority(uint64_t priority); //only for lab6 30 | 31 | 32 | #endif /* !__USER_LIBS_SYSCALL_H__ */ 33 | 34 | -------------------------------------------------------------------------------- /lab6/kern/driver/clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | volatile size_t ticks; 8 | 9 | static inline uint64_t get_cycles(void) { 10 | #if __riscv_xlen == 64 11 | uint64_t n; 12 | __asm__ __volatile__("rdtime %0" : "=r"(n)); 13 | return n; 14 | #else 15 | uint32_t lo, hi, tmp; 16 | __asm__ __volatile__( 17 | "1:\n" 18 | "rdtimeh %0\n" 19 | "rdtime %1\n" 20 | "rdtimeh %2\n" 21 | "bne %0, %2, 1b" 22 | : "=&r"(hi), "=&r"(lo), "=&r"(tmp)); 23 | return ((uint64_t)hi << 32) | lo; 24 | #endif 25 | } 26 | 27 | static uint64_t timebase = 100000; 28 | 29 | /* * 30 | * clock_init - initialize 8253 clock to interrupt 100 times per second, 31 | * and then enable IRQ_TIMER. 32 | * */ 33 | void clock_init(void) { 34 | set_csr(sie, MIP_STIP); 35 | 36 | clock_set_next_event(); 37 | // initialize time counter 'ticks' to zero 38 | ticks = 0; 39 | 40 | cprintf("++ setup timer interrupts\n"); 41 | } 42 | 43 | void clock_set_next_event(void) { sbi_set_timer(get_cycles() + timebase); } 44 | -------------------------------------------------------------------------------- /lab7/kern/driver/clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | volatile size_t ticks; 8 | 9 | static inline uint64_t get_cycles(void) { 10 | #if __riscv_xlen == 64 11 | uint64_t n; 12 | __asm__ __volatile__("rdtime %0" : "=r"(n)); 13 | return n; 14 | #else 15 | uint32_t lo, hi, tmp; 16 | __asm__ __volatile__( 17 | "1:\n" 18 | "rdtimeh %0\n" 19 | "rdtime %1\n" 20 | "rdtimeh %2\n" 21 | "bne %0, %2, 1b" 22 | : "=&r"(hi), "=&r"(lo), "=&r"(tmp)); 23 | return ((uint64_t)hi << 32) | lo; 24 | #endif 25 | } 26 | 27 | static uint64_t timebase = 100000; 28 | 29 | /* * 30 | * clock_init - initialize 8253 clock to interrupt 100 times per second, 31 | * and then enable IRQ_TIMER. 32 | * */ 33 | void clock_init(void) { 34 | set_csr(sie, MIP_STIP); 35 | 36 | clock_set_next_event(); 37 | // initialize time counter 'ticks' to zero 38 | ticks = 0; 39 | 40 | cprintf("++ setup timer interrupts\n"); 41 | } 42 | 43 | void clock_set_next_event(void) { sbi_set_timer(get_cycles() + timebase); } 44 | -------------------------------------------------------------------------------- /lab8/kern/driver/clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | volatile size_t ticks; 8 | 9 | static inline uint64_t get_cycles(void) { 10 | #if __riscv_xlen == 64 11 | uint64_t n; 12 | __asm__ __volatile__("rdtime %0" : "=r"(n)); 13 | return n; 14 | #else 15 | uint32_t lo, hi, tmp; 16 | __asm__ __volatile__( 17 | "1:\n" 18 | "rdtimeh %0\n" 19 | "rdtime %1\n" 20 | "rdtimeh %2\n" 21 | "bne %0, %2, 1b" 22 | : "=&r"(hi), "=&r"(lo), "=&r"(tmp)); 23 | return ((uint64_t)hi << 32) | lo; 24 | #endif 25 | } 26 | 27 | static uint64_t timebase = 100000; 28 | 29 | /* * 30 | * clock_init - initialize 8253 clock to interrupt 100 times per second, 31 | * and then enable IRQ_TIMER. 32 | * */ 33 | void clock_init(void) { 34 | set_csr(sie, MIP_STIP); 35 | 36 | clock_set_next_event(); 37 | // initialize time counter 'ticks' to zero 38 | ticks = 0; 39 | 40 | cprintf("++ setup timer interrupts\n"); 41 | } 42 | 43 | void clock_set_next_event(void) { sbi_set_timer(get_cycles() + timebase); } 44 | -------------------------------------------------------------------------------- /lab1/kern/debug/panic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static bool is_panic = 0; 7 | 8 | /* * 9 | * __panic - __panic is called on unresolvable fatal errors. it prints 10 | * "panic: 'message'", and then enters the kernel monitor. 11 | * */ 12 | void __panic(const char *file, int line, const char *fmt, ...) { 13 | if (is_panic) { 14 | goto panic_dead; 15 | } 16 | is_panic = 1; 17 | 18 | // print the 'message' 19 | va_list ap; 20 | va_start(ap, fmt); 21 | cprintf("kernel panic at %s:%d:\n ", file, line); 22 | vcprintf(fmt, ap); 23 | cprintf("\n"); 24 | va_end(ap); 25 | 26 | panic_dead: 27 | intr_disable(); 28 | while (1) { 29 | kmonitor(NULL); 30 | } 31 | } 32 | 33 | /* __warn - like panic, but don't */ 34 | void __warn(const char *file, int line, const char *fmt, ...) { 35 | va_list ap; 36 | va_start(ap, fmt); 37 | cprintf("kernel warning at %s:%d:\n ", file, line); 38 | vcprintf(fmt, ap); 39 | cprintf("\n"); 40 | va_end(ap); 41 | } 42 | 43 | bool is_kernel_panic(void) { return is_panic; } 44 | -------------------------------------------------------------------------------- /lab5/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /lab6/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /lab7/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /lab8/user/waitkill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | do_yield(void) { 6 | yield(); 7 | yield(); 8 | yield(); 9 | yield(); 10 | yield(); 11 | yield(); 12 | } 13 | 14 | int parent, pid1, pid2; 15 | 16 | void 17 | loop(void) { 18 | cprintf("child 1.\n"); 19 | while (1); 20 | } 21 | 22 | void 23 | work(void) { 24 | cprintf("child 2.\n"); 25 | do_yield(); 26 | if (kill(parent) == 0) { 27 | cprintf("kill parent ok.\n"); 28 | do_yield(); 29 | if (kill(pid1) == 0) { 30 | cprintf("kill child1 ok.\n"); 31 | exit(0); 32 | } 33 | } 34 | exit(-1); 35 | } 36 | 37 | int 38 | main(void) { 39 | parent = getpid(); 40 | if ((pid1 = fork()) == 0) { 41 | loop(); 42 | } 43 | 44 | assert(pid1 > 0); 45 | 46 | if ((pid2 = fork()) == 0) { 47 | work(); 48 | } 49 | if (pid2 > 0) { 50 | cprintf("wait child 1.\n"); 51 | waitpid(pid1, NULL); 52 | panic("waitpid %d returns\n", pid1); 53 | } 54 | else { 55 | kill(pid1); 56 | } 57 | panic("FAIL: T.T\n"); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /lab0/libs/sbi.c: -------------------------------------------------------------------------------- 1 | // libs/sbi.c 2 | #include 3 | #include 4 | 5 | 6 | uint64_t SBI_SET_TIMER = 0; 7 | uint64_t SBI_CONSOLE_PUTCHAR = 1; 8 | uint64_t SBI_CONSOLE_GETCHAR = 2; 9 | uint64_t SBI_CLEAR_IPI = 3; 10 | uint64_t SBI_SEND_IPI = 4; 11 | uint64_t SBI_REMOTE_FENCE_I = 5; 12 | uint64_t SBI_REMOTE_SFENCE_VMA = 6; 13 | uint64_t SBI_REMOTE_SFENCE_VMA_ASID = 7; 14 | uint64_t SBI_SHUTDOWN = 8; 15 | 16 | uint64_t sbi_call(uint64_t sbi_type, uint64_t arg0, uint64_t arg1, uint64_t arg2) { 17 | uint64_t ret_val; 18 | __asm__ volatile ( 19 | "mv x17, %[sbi_type]\n" 20 | "mv x10, %[arg0]\n" 21 | "mv x11, %[arg1]\n" 22 | "mv x12, %[arg2]\n" 23 | "ecall\n" 24 | "mv %[ret_val], x10" 25 | : [ret_val] "=r" (ret_val) 26 | : [sbi_type] "r" (sbi_type), [arg0] "r" (arg0), [arg1] "r" (arg1), [arg2] "r" (arg2) 27 | : "memory" 28 | ); 29 | return ret_val; 30 | } 31 | 32 | void sbi_console_putchar(unsigned char ch) { 33 | sbi_call(SBI_CONSOLE_PUTCHAR, ch, 0, 0); 34 | } 35 | 36 | void sbi_set_timer(unsigned long long stime_value) { 37 | sbi_call(SBI_SET_TIMER, stime_value, 0, 0); 38 | } 39 | --------------------------------------------------------------------------------