├── .gitignore ├── README.md ├── docs ├── amd64-abi.pdf ├── dsohowto.pdf ├── elf.pdf ├── mcs.pdf ├── multiboot.pdf └── slab.txt ├── week1 ├── asm │ ├── Makefile │ ├── abi.tex │ ├── cmds.tex │ ├── img │ │ ├── call0.png │ │ ├── call0.svg │ │ ├── call1.png │ │ ├── call1.svg │ │ ├── popq.png │ │ ├── popq.svg │ │ ├── pushq.png │ │ ├── pushq.svg │ │ ├── stack.png │ │ └── stack.svg │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ └── lecv.tex ├── intro │ ├── Makefile │ ├── img │ │ ├── os-inner-view.png │ │ ├── os-inner-view.svg │ │ ├── os-outer-view.png │ │ └── os-outer-view.svg │ ├── intro.tex │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ └── lecv.tex ├── ints │ ├── Makefile │ ├── exceptions │ │ ├── Makefile │ │ ├── genints.py │ │ ├── inc │ │ │ ├── backtrace.h │ │ │ ├── ctype.h │ │ │ ├── ints.h │ │ │ ├── ioport.h │ │ │ ├── memory.h │ │ │ ├── multiboot.h │ │ │ ├── print.h │ │ │ ├── stdlib.h │ │ │ ├── string.h │ │ │ └── vga.h │ │ ├── kernel.ld │ │ ├── src │ │ │ ├── backtrace.c │ │ │ ├── bootstrap.S │ │ │ ├── ctype.c │ │ │ ├── ints.c │ │ │ ├── main.c │ │ │ ├── print.c │ │ │ ├── stdlib.c │ │ │ ├── string.c │ │ │ └── vga.c │ │ └── start.sh │ ├── img │ │ ├── desc.png │ │ ├── desc.svg │ │ ├── intstack.png │ │ └── intstack.svg │ ├── interrupts │ │ ├── Makefile │ │ ├── genints.py │ │ ├── inc │ │ │ ├── apic.h │ │ │ ├── backtrace.h │ │ │ ├── ctype.h │ │ │ ├── ints.h │ │ │ ├── ioport.h │ │ │ ├── memory.h │ │ │ ├── multiboot.h │ │ │ ├── print.h │ │ │ ├── stdlib.h │ │ │ ├── string.h │ │ │ ├── time.h │ │ │ └── vga.h │ │ ├── kernel.ld │ │ ├── src │ │ │ ├── apic.c │ │ │ ├── backtrace.c │ │ │ ├── bootstrap.S │ │ │ ├── ctype.c │ │ │ ├── ints.c │ │ │ ├── main.c │ │ │ ├── print.c │ │ │ ├── stdlib.c │ │ │ ├── string.c │ │ │ ├── time.c │ │ │ └── vga.c │ │ └── start.sh │ ├── ints.tex │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ └── lecv.tex └── load │ ├── Makefile │ ├── bios-boot │ ├── Makefile │ ├── boot.S │ ├── linker.ld │ └── start.sh │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── load.tex │ └── multiboot-boot │ ├── Makefile │ ├── inc │ ├── ctype.h │ ├── memory.h │ ├── multiboot.h │ ├── print.h │ ├── stdlib.h │ ├── string.h │ └── vga.h │ ├── kernel.ld │ ├── src │ ├── bootstrap.S │ ├── ctype.c │ ├── main.c │ ├── print.c │ ├── stdlib.c │ ├── string.c │ └── vga.c │ └── start.sh ├── week2 ├── alloc │ ├── Makefile │ ├── buddy-allocator │ │ ├── Makefile │ │ ├── genints.py │ │ ├── inc │ │ │ ├── backtrace.h │ │ │ ├── balloc.h │ │ │ ├── buddy.h │ │ │ ├── ctype.h │ │ │ ├── ints.h │ │ │ ├── ioport.h │ │ │ ├── list.h │ │ │ ├── memory.h │ │ │ ├── multiboot.h │ │ │ ├── paging.h │ │ │ ├── print.h │ │ │ ├── stdlib.h │ │ │ ├── string.h │ │ │ └── vga.h │ │ ├── kernel.ld │ │ ├── src │ │ │ ├── backtrace.c │ │ │ ├── balloc.c │ │ │ ├── bootstrap.S │ │ │ ├── buddy.c │ │ │ ├── ctype.c │ │ │ ├── ints.c │ │ │ ├── list.c │ │ │ ├── main.c │ │ │ ├── paging.c │ │ │ ├── print.c │ │ │ ├── stdlib.c │ │ │ ├── string.c │ │ │ └── vga.c │ │ └── start.sh │ ├── buddy.tex │ ├── img │ │ ├── bud0.png │ │ ├── bud0.svg │ │ ├── bud1.png │ │ ├── bud1.svg │ │ ├── bud2.png │ │ ├── bud2.svg │ │ ├── bud3.png │ │ ├── bud3.svg │ │ ├── bud4.png │ │ ├── bud4.svg │ │ ├── bud5.png │ │ ├── bud5.svg │ │ ├── lst0.png │ │ ├── lst0.svg │ │ ├── lst1.png │ │ ├── lst1.svg │ │ ├── lst2.png │ │ ├── lst2.svg │ │ ├── lst3.png │ │ ├── lst3.svg │ │ ├── lst4.png │ │ ├── lst4.svg │ │ ├── lst5.png │ │ ├── lst5.svg │ │ ├── lst6.png │ │ ├── lst6.svg │ │ ├── lst7.png │ │ ├── lst7.svg │ │ ├── lst8.png │ │ ├── lst8.svg │ │ ├── lst9.png │ │ ├── lst9.svg │ │ ├── slab0.png │ │ ├── slab0.svg │ │ ├── slab1.png │ │ └── slab1.svg │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── simple.tex │ └── slab.tex ├── log_mem │ ├── Makefile │ ├── bootstrap-allocator │ │ ├── Makefile │ │ ├── genints.py │ │ ├── inc │ │ │ ├── backtrace.h │ │ │ ├── balloc.h │ │ │ ├── ctype.h │ │ │ ├── ints.h │ │ │ ├── ioport.h │ │ │ ├── memory.h │ │ │ ├── multiboot.h │ │ │ ├── print.h │ │ │ ├── stdlib.h │ │ │ ├── string.h │ │ │ └── vga.h │ │ ├── kernel.ld │ │ ├── src │ │ │ ├── backtrace.c │ │ │ ├── balloc.c │ │ │ ├── bootstrap.S │ │ │ ├── ctype.c │ │ │ ├── ints.c │ │ │ ├── main.c │ │ │ ├── print.c │ │ │ ├── stdlib.c │ │ │ ├── string.c │ │ │ └── vga.c │ │ └── start.sh │ ├── fork │ │ ├── Makefile │ │ └── main.c │ ├── img │ │ ├── dt.png │ │ ├── dt.svg │ │ ├── gdt32.png │ │ ├── gdt32.svg │ │ ├── gdttr.png │ │ ├── gdttr.svg │ │ ├── page1gb.png │ │ ├── page1gb.svg │ │ ├── pagemap.png │ │ ├── pagemap.svg │ │ ├── pagetr.png │ │ ├── pagetr.svg │ │ ├── pagex86.png │ │ ├── pagex86.svg │ │ ├── pte.png │ │ ├── pte.svg │ │ ├── sel.png │ │ └── sel.svg │ ├── initial-paging │ │ ├── Makefile │ │ ├── genints.py │ │ ├── inc │ │ │ ├── backtrace.h │ │ │ ├── balloc.h │ │ │ ├── ctype.h │ │ │ ├── ints.h │ │ │ ├── ioport.h │ │ │ ├── memory.h │ │ │ ├── multiboot.h │ │ │ ├── paging.h │ │ │ ├── print.h │ │ │ ├── stdlib.h │ │ │ ├── string.h │ │ │ └── vga.h │ │ ├── kernel.ld │ │ ├── src │ │ │ ├── backtrace.c │ │ │ ├── balloc.c │ │ │ ├── bootstrap.S │ │ │ ├── ctype.c │ │ │ ├── ints.c │ │ │ ├── main.c │ │ │ ├── paging.c │ │ │ ├── print.c │ │ │ ├── stdlib.c │ │ │ ├── string.c │ │ │ └── vga.c │ │ └── start.sh │ ├── kern │ │ ├── Makefile │ │ └── getseg.c │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── log.tex │ └── loop │ │ ├── Makefile │ │ └── main.c └── phys_mem │ ├── Makefile │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── phys.tex │ └── physmem-map │ ├── Makefile │ ├── inc │ ├── ctype.h │ ├── memory.h │ ├── multiboot.h │ ├── print.h │ ├── stdlib.h │ ├── string.h │ └── vga.h │ ├── kernel.ld │ ├── src │ ├── bootstrap.S │ ├── ctype.c │ ├── main.c │ ├── print.c │ ├── stdlib.c │ ├── string.c │ └── vga.c │ └── start.sh ├── week3 └── threads │ ├── Makefile │ ├── img │ ├── multithreading.png │ ├── multithreading.svg │ ├── preempt.png │ ├── preempt.svg │ ├── rr0.png │ ├── rr0.svg │ ├── rr1.png │ ├── rr1.svg │ ├── rr10.png │ ├── rr10.svg │ ├── rr11.png │ ├── rr11.svg │ ├── rr12.png │ ├── rr12.svg │ ├── rr13.png │ ├── rr13.svg │ ├── rr14.png │ ├── rr14.svg │ ├── rr15.png │ ├── rr15.svg │ ├── rr16.png │ ├── rr16.svg │ ├── rr17.png │ ├── rr17.svg │ ├── rr18.png │ ├── rr18.svg │ ├── rr19.png │ ├── rr19.svg │ ├── rr2.png │ ├── rr2.svg │ ├── rr20.png │ ├── rr20.svg │ ├── rr21.png │ ├── rr21.svg │ ├── rr22.png │ ├── rr22.svg │ ├── rr23.png │ ├── rr23.svg │ ├── rr24.png │ ├── rr24.svg │ ├── rr25.png │ ├── rr25.svg │ ├── rr26.png │ ├── rr26.svg │ ├── rr27.png │ ├── rr27.svg │ ├── rr3.png │ ├── rr3.svg │ ├── rr4.png │ ├── rr4.svg │ ├── rr5.png │ ├── rr5.svg │ ├── rr6.png │ ├── rr6.svg │ ├── rr7.png │ ├── rr7.svg │ ├── rr8.png │ ├── rr8.svg │ ├── rr9.png │ ├── rr9.svg │ ├── sched0.png │ ├── sched0.svg │ ├── sched1.png │ ├── sched1.svg │ ├── sched2.png │ ├── sched2.svg │ ├── sched3.png │ ├── sched3.svg │ ├── switch.png │ └── switch.svg │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── preempt.tex │ ├── preempt │ ├── Makefile │ ├── main.c │ └── switch.S │ ├── real.tex │ ├── scheduler.tex │ ├── thread-switch │ ├── Makefile │ ├── main.c │ └── switch.S │ ├── thread.tex │ └── threads │ ├── Makefile │ ├── genints.py │ ├── inc │ ├── apic.h │ ├── backtrace.h │ ├── balloc.h │ ├── buddy.h │ ├── ctype.h │ ├── ints.h │ ├── ioport.h │ ├── list.h │ ├── memory.h │ ├── multiboot.h │ ├── paging.h │ ├── print.h │ ├── slab.h │ ├── stdlib.h │ ├── string.h │ ├── threads.h │ ├── time.h │ └── vga.h │ ├── kernel.ld │ ├── src │ ├── apic.c │ ├── backtrace.c │ ├── balloc.c │ ├── bootstrap.S │ ├── buddy.c │ ├── ctype.c │ ├── ints.c │ ├── list.c │ ├── main.c │ ├── paging.c │ ├── print.c │ ├── slab.c │ ├── stdlib.c │ ├── string.c │ ├── switch.S │ ├── threads.c │ ├── time.c │ └── vga.c │ └── start.sh ├── week4 └── sync │ ├── Makefile │ ├── conditions │ ├── Makefile │ ├── genints.py │ ├── inc │ │ ├── apic.h │ │ ├── backtrace.h │ │ ├── balloc.h │ │ ├── buddy.h │ │ ├── condition.h │ │ ├── ctype.h │ │ ├── ints.h │ │ ├── ioport.h │ │ ├── list.h │ │ ├── lock.h │ │ ├── memory.h │ │ ├── multiboot.h │ │ ├── mutex.h │ │ ├── paging.h │ │ ├── print.h │ │ ├── slab.h │ │ ├── stdlib.h │ │ ├── string.h │ │ ├── threads.h │ │ ├── time.h │ │ └── vga.h │ ├── kernel.ld │ ├── src │ │ ├── apic.c │ │ ├── backtrace.c │ │ ├── balloc.c │ │ ├── bootstrap.S │ │ ├── buddy.c │ │ ├── condition.c │ │ ├── ctype.c │ │ ├── ints.c │ │ ├── list.c │ │ ├── lock.c │ │ ├── main.c │ │ ├── mutex.c │ │ ├── paging.c │ │ ├── print.c │ │ ├── slab.c │ │ ├── stdlib.c │ │ ├── string.c │ │ ├── switch.S │ │ ├── threads.c │ │ ├── time.c │ │ └── vga.c │ └── start.sh │ ├── cv.tex │ ├── deadlock.tex │ ├── img │ ├── cv.png │ ├── cv.svg │ ├── nthreads.png │ ├── nthreads.svg │ ├── order.png │ ├── order.svg │ ├── waitdie.png │ ├── waitdie.svg │ ├── waitfor.png │ └── waitfor.svg │ ├── lec.pdf │ ├── lec.tex │ ├── lecv.pdf │ ├── lecv.tex │ ├── locks │ ├── Makefile │ ├── genints.py │ ├── inc │ │ ├── apic.h │ │ ├── backtrace.h │ │ ├── balloc.h │ │ ├── buddy.h │ │ ├── ctype.h │ │ ├── ints.h │ │ ├── ioport.h │ │ ├── list.h │ │ ├── lock.h │ │ ├── memory.h │ │ ├── multiboot.h │ │ ├── paging.h │ │ ├── print.h │ │ ├── slab.h │ │ ├── stdlib.h │ │ ├── string.h │ │ ├── threads.h │ │ ├── time.h │ │ └── vga.h │ ├── kernel.ld │ ├── src │ │ ├── apic.c │ │ ├── backtrace.c │ │ ├── balloc.c │ │ ├── bootstrap.S │ │ ├── buddy.c │ │ ├── ctype.c │ │ ├── ints.c │ │ ├── list.c │ │ ├── lock.c │ │ ├── main.c │ │ ├── paging.c │ │ ├── print.c │ │ ├── slab.c │ │ ├── stdlib.c │ │ ├── string.c │ │ ├── switch.S │ │ ├── threads.c │ │ ├── time.c │ │ └── vga.c │ └── start.sh │ ├── mutex.tex │ ├── mutexes │ ├── Makefile │ ├── genints.py │ ├── inc │ │ ├── apic.h │ │ ├── backtrace.h │ │ ├── balloc.h │ │ ├── buddy.h │ │ ├── ctype.h │ │ ├── ints.h │ │ ├── ioport.h │ │ ├── list.h │ │ ├── lock.h │ │ ├── memory.h │ │ ├── multiboot.h │ │ ├── mutex.h │ │ ├── paging.h │ │ ├── print.h │ │ ├── slab.h │ │ ├── stdlib.h │ │ ├── string.h │ │ ├── threads.h │ │ ├── time.h │ │ └── vga.h │ ├── kernel.ld │ ├── src │ │ ├── apic.c │ │ ├── backtrace.c │ │ ├── balloc.c │ │ ├── bootstrap.S │ │ ├── buddy.c │ │ ├── ctype.c │ │ ├── ints.c │ │ ├── list.c │ │ ├── lock.c │ │ ├── main.c │ │ ├── mutex.c │ │ ├── paging.c │ │ ├── print.c │ │ ├── slab.c │ │ ├── stdlib.c │ │ ├── string.c │ │ ├── switch.S │ │ ├── threads.c │ │ ├── time.c │ │ └── vga.c │ └── start.sh │ ├── producer-consumer │ ├── Makefile │ └── example.cpp │ ├── race.tex │ ├── rmwreg.tex │ ├── rwlock.tex │ └── rwreg.tex └── week5 ├── exec ├── Makefile ├── dyn.tex ├── exe.tex ├── img │ ├── address-space.png │ ├── address-space.svg │ ├── amd64.png │ ├── amd64.svg │ ├── x86.png │ └── x86.svg ├── lec.pdf ├── lec.tex ├── lecv.pdf ├── lecv.tex ├── shared │ ├── Makefile │ ├── call │ ├── call.c │ ├── foo.c │ ├── foo.h │ ├── libfoo.so │ └── run.sh ├── simple-elf │ └── main.c └── src │ └── main.c ├── initramfs ├── Makefile ├── genints.py ├── inc │ ├── apic.h │ ├── backtrace.h │ ├── balloc.h │ ├── buddy.h │ ├── condition.h │ ├── ctype.h │ ├── initramfs.h │ ├── ints.h │ ├── ioport.h │ ├── list.h │ ├── lock.h │ ├── memory.h │ ├── misc.h │ ├── multiboot.h │ ├── mutex.h │ ├── paging.h │ ├── print.h │ ├── ramfs.h │ ├── slab.h │ ├── stdlib.h │ ├── string.h │ ├── threads.h │ ├── time.h │ └── vga.h ├── initramfs │ └── README ├── initrd.img ├── initrd.sh ├── kernel.ld ├── src │ ├── apic.c │ ├── backtrace.c │ ├── balloc.c │ ├── bootstrap.S │ ├── buddy.c │ ├── condition.c │ ├── ctype.c │ ├── initramfs.c │ ├── ints.c │ ├── list.c │ ├── lock.c │ ├── main.c │ ├── misc.c │ ├── mutex.c │ ├── paging.c │ ├── print.c │ ├── ramfs.c │ ├── slab.c │ ├── stdlib.c │ ├── string.c │ ├── switch.S │ ├── threads.c │ ├── time.c │ └── vga.c └── start.sh ├── ipc ├── Makefile ├── create.tex ├── exec │ └── main.c ├── exit │ └── main.c ├── fd.tex ├── fd1 │ ├── Makefile │ └── cat.c ├── fd2 │ ├── Makefile │ └── touch.c ├── fd3 │ ├── Makefile │ └── copy.c ├── fd4 │ ├── Makefile │ ├── data │ └── race.c ├── fifo │ ├── rcv.c │ └── snd.c ├── fork │ └── main.c ├── lec.pdf ├── lec.tex ├── lecv.pdf ├── lecv.tex ├── pipe │ └── main.c ├── ptrace │ ├── example1.c │ └── example2.c ├── shm │ └── main.c └── types.tex ├── load ├── Makefile ├── genints.py ├── inc │ ├── apic.h │ ├── backtrace.h │ ├── balloc.h │ ├── buddy.h │ ├── condition.h │ ├── ctype.h │ ├── exec.h │ ├── initramfs.h │ ├── ints.h │ ├── ioport.h │ ├── list.h │ ├── lock.h │ ├── memory.h │ ├── misc.h │ ├── mm.h │ ├── multiboot.h │ ├── mutex.h │ ├── paging.h │ ├── print.h │ ├── ramfs.h │ ├── slab.h │ ├── stdlib.h │ ├── string.h │ ├── threads.h │ ├── time.h │ └── vga.h ├── initramfs │ ├── Makefile │ ├── README │ ├── test │ └── test.S ├── initrd.img ├── initrd.sh ├── kernel.ld ├── src │ ├── apic.c │ ├── backtrace.c │ ├── balloc.c │ ├── bootstrap.S │ ├── buddy.c │ ├── condition.c │ ├── ctype.c │ ├── entry.S │ ├── exec.c │ ├── initramfs.c │ ├── ints.c │ ├── list.c │ ├── lock.c │ ├── main.c │ ├── misc.c │ ├── mm.c │ ├── mutex.c │ ├── paging.c │ ├── print.c │ ├── ramfs.c │ ├── slab.c │ ├── stdlib.c │ ├── string.c │ ├── threads.c │ ├── time.c │ └── vga.c └── start.sh ├── mm ├── Makefile ├── genints.py ├── inc │ ├── apic.h │ ├── backtrace.h │ ├── balloc.h │ ├── buddy.h │ ├── condition.h │ ├── ctype.h │ ├── initramfs.h │ ├── ints.h │ ├── ioport.h │ ├── list.h │ ├── lock.h │ ├── memory.h │ ├── misc.h │ ├── mm.h │ ├── multiboot.h │ ├── mutex.h │ ├── paging.h │ ├── print.h │ ├── ramfs.h │ ├── slab.h │ ├── stdlib.h │ ├── string.h │ ├── threads.h │ ├── time.h │ └── vga.h ├── initramfs │ └── README ├── initrd.img ├── initrd.sh ├── kernel.ld ├── src │ ├── apic.c │ ├── backtrace.c │ ├── balloc.c │ ├── bootstrap.S │ ├── buddy.c │ ├── condition.c │ ├── ctype.c │ ├── initramfs.c │ ├── ints.c │ ├── list.c │ ├── lock.c │ ├── main.c │ ├── misc.c │ ├── mm.c │ ├── mutex.c │ ├── paging.c │ ├── print.c │ ├── ramfs.c │ ├── slab.c │ ├── stdlib.c │ ├── string.c │ ├── switch.S │ ├── threads.c │ ├── time.c │ └── vga.c └── start.sh ├── sysc ├── Makefile ├── img │ ├── desc.png │ ├── desc.svg │ ├── intstack.png │ ├── intstack.svg │ ├── tss.png │ └── tss.svg ├── lec.pdf ├── lec.tex ├── lecv.pdf ├── lecv.tex └── syscall.tex ├── syscall ├── Makefile ├── genints.py ├── inc │ ├── apic.h │ ├── backtrace.h │ ├── balloc.h │ ├── buddy.h │ ├── condition.h │ ├── ctype.h │ ├── exec.h │ ├── initramfs.h │ ├── ints.h │ ├── ioport.h │ ├── list.h │ ├── lock.h │ ├── memory.h │ ├── misc.h │ ├── mm.h │ ├── multiboot.h │ ├── mutex.h │ ├── paging.h │ ├── print.h │ ├── ramfs.h │ ├── slab.h │ ├── stdlib.h │ ├── string.h │ ├── syscall.h │ ├── threads.h │ ├── time.h │ └── vga.h ├── initramfs │ ├── Makefile │ ├── README │ ├── test │ └── test.S ├── initrd.img ├── initrd.sh ├── kernel.ld ├── src │ ├── apic.c │ ├── backtrace.c │ ├── balloc.c │ ├── bootstrap.S │ ├── buddy.c │ ├── condition.c │ ├── ctype.c │ ├── entry.S │ ├── exec.c │ ├── initramfs.c │ ├── ints.c │ ├── list.c │ ├── lock.c │ ├── main.c │ ├── misc.c │ ├── mm.c │ ├── mutex.c │ ├── paging.c │ ├── print.c │ ├── ramfs.c │ ├── slab.c │ ├── stdlib.c │ ├── string.c │ ├── syscall.c │ ├── threads.c │ ├── time.c │ └── vga.c └── start.sh └── tss ├── Makefile ├── genints.py ├── inc ├── apic.h ├── backtrace.h ├── balloc.h ├── buddy.h ├── condition.h ├── ctype.h ├── initramfs.h ├── ints.h ├── ioport.h ├── list.h ├── lock.h ├── memory.h ├── misc.h ├── multiboot.h ├── mutex.h ├── paging.h ├── print.h ├── ramfs.h ├── slab.h ├── stdlib.h ├── string.h ├── threads.h ├── time.h └── vga.h ├── initramfs └── README ├── initrd.img ├── initrd.sh ├── kernel.ld ├── src ├── apic.c ├── backtrace.c ├── balloc.c ├── bootstrap.S ├── buddy.c ├── condition.c ├── ctype.c ├── initramfs.c ├── ints.c ├── list.c ├── lock.c ├── main.c ├── misc.c ├── mutex.c ├── paging.c ├── print.c ├── ramfs.c ├── slab.c ├── stdlib.c ├── string.c ├── switch.S ├── threads.c ├── time.c └── vga.c └── start.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.vrb 2 | *.toc 3 | *.snm 4 | *.out 5 | *.nav 6 | *.log 7 | *.aux 8 | 9 | *.cmd 10 | *.o 11 | *.d 12 | intentry.S 13 | kernel 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Repository for online CSC OS course on stepik.org 2 | -------------------------------------------------------------------------------- /docs/amd64-abi.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/docs/amd64-abi.pdf -------------------------------------------------------------------------------- /docs/dsohowto.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/docs/dsohowto.pdf -------------------------------------------------------------------------------- /docs/elf.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/docs/elf.pdf -------------------------------------------------------------------------------- /docs/mcs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/docs/mcs.pdf -------------------------------------------------------------------------------- /docs/multiboot.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/docs/multiboot.pdf -------------------------------------------------------------------------------- /week1/asm/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week1/asm/img/call0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/img/call0.png -------------------------------------------------------------------------------- /week1/asm/img/call1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/img/call1.png -------------------------------------------------------------------------------- /week1/asm/img/popq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/img/popq.png -------------------------------------------------------------------------------- /week1/asm/img/pushq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/img/pushq.png -------------------------------------------------------------------------------- /week1/asm/img/stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/img/stack.png -------------------------------------------------------------------------------- /week1/asm/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/lec.pdf -------------------------------------------------------------------------------- /week1/asm/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Язык ассемблера} 30 | \date{\today} 31 | 32 | \begin{document} 33 | \begin{frame} 34 | \titlepage 35 | \end{frame} 36 | \input{cmds} 37 | \input{abi} 38 | \end{document} 39 | -------------------------------------------------------------------------------- /week1/asm/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/asm/lecv.pdf -------------------------------------------------------------------------------- /week1/intro/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week1/intro/img/os-inner-view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/intro/img/os-inner-view.png -------------------------------------------------------------------------------- /week1/intro/img/os-outer-view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/intro/img/os-outer-view.png -------------------------------------------------------------------------------- /week1/intro/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/intro/lec.pdf -------------------------------------------------------------------------------- /week1/intro/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\rasebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Введение} 30 | \date{\today} 31 | 32 | \begin{document} 33 | \begin{frame} 34 | \titlepage 35 | \end{frame} 36 | \input{intro} 37 | \end{document} 38 | -------------------------------------------------------------------------------- /week1/intro/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/intro/lecv.pdf -------------------------------------------------------------------------------- /week1/intro/lecv.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,t]{beamer} 2 | 3 | \setbeamersize{text margin left=0pt, text margin right=4cm} 4 | \hypersetup{colorlinks=true,linkcolor=red} 5 | \usetheme{boxes} 6 | 7 | \usepackage[utf8]{inputenc} 8 | \usepackage[russian,english]{babel} 9 | \usepackage[T2A]{fontenc} 10 | \usepackage{hyperref} 11 | \usepackage[final]{listings} 12 | \usepackage{breakurl} 13 | \usepackage{cite} 14 | \usepackage{perpage} 15 | 16 | \graphicspath{{img/}} 17 | 18 | \def\Url\Breaks{\do\/\do-} 19 | \lstset{ 20 | frame=single, 21 | breaklines=true, 22 | basicstyle=\small, 23 | postbreak=\rasebox{0ex}{\ensuremath{\hookrightarrow\space}}, 24 | numbers=left 25 | } 26 | 27 | \MakePerPage{footnote} 28 | 29 | \title{Операционные Системы} 30 | \subtitle{Введение} 31 | \date{\today} 32 | 33 | \begin{document} 34 | \begin{frame} 35 | \titlepage 36 | \end{frame} 37 | \input{intro} 38 | \end{document} 39 | -------------------------------------------------------------------------------- /week1/ints/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | 10 | #endif /*__STRING_H__*/ 11 | -------------------------------------------------------------------------------- /week1/ints/exceptions/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week1/ints/exceptions/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week1/ints/exceptions/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week1/ints/exceptions/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static void qemu_gdb_hang(void) 10 | { 11 | #ifdef DEBUG 12 | static volatile int wait = 1; 13 | 14 | while (wait); 15 | #endif 16 | } 17 | 18 | 19 | static void div_by_zero_handler(void) 20 | { 21 | printf("Ops... Devision By Zero!\n"); 22 | while (1); 23 | } 24 | 25 | void main(void) 26 | { 27 | qemu_gdb_hang(); 28 | vga_clr(); 29 | ints_setup(); 30 | 31 | register_exception_handler(INTNO_DIVBYZERO, &div_by_zero_handler); 32 | 33 | static volatile int zero; 34 | 35 | printf("1 / %d = %d\n", zero, 1 / zero); 36 | } 37 | -------------------------------------------------------------------------------- /week1/ints/exceptions/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t strlen(const char *str) 4 | { 5 | const char *begin = str; 6 | 7 | while (*str++); 8 | return str - begin - 1; 9 | } 10 | 11 | void *memcpy(void *dst, const void *src, size_t size) 12 | { 13 | char *to = dst; 14 | const char *from = src; 15 | 16 | while (size--) 17 | *to++ = *from++; 18 | return dst; 19 | } 20 | 21 | void *memset(void *dst, int fill, size_t size) 22 | { 23 | char *to = dst; 24 | 25 | while (size--) 26 | *to++ = fill; 27 | return dst; 28 | } 29 | -------------------------------------------------------------------------------- /week1/ints/exceptions/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week1/ints/img/desc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/ints/img/desc.png -------------------------------------------------------------------------------- /week1/ints/img/intstack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/ints/img/intstack.png -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | 10 | #endif /*__STRING_H__*/ 11 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week1/ints/interrupts/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week1/ints/interrupts/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week1/ints/interrupts/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week1/ints/interrupts/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | static void qemu_gdb_hang(void) 11 | { 12 | #ifdef DEBUG 13 | static volatile int wait = 1; 14 | 15 | while (wait); 16 | #endif 17 | } 18 | 19 | void main(void) 20 | { 21 | qemu_gdb_hang(); 22 | vga_clr(); 23 | ints_setup(); 24 | time_setup(); 25 | local_int_enable(); 26 | 27 | while (1); 28 | } 29 | -------------------------------------------------------------------------------- /week1/ints/interrupts/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t strlen(const char *str) 4 | { 5 | const char *begin = str; 6 | 7 | while (*str++); 8 | return str - begin - 1; 9 | } 10 | 11 | void *memcpy(void *dst, const void *src, size_t size) 12 | { 13 | char *to = dst; 14 | const char *from = src; 15 | 16 | while (size--) 17 | *to++ = *from++; 18 | return dst; 19 | } 20 | 21 | void *memset(void *dst, int fill, size_t size) 22 | { 23 | char *to = dst; 24 | 25 | while (size--) 26 | *to++ = fill; 27 | return dst; 28 | } 29 | -------------------------------------------------------------------------------- /week1/ints/interrupts/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | printf("Tick!\n"); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week1/ints/interrupts/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week1/ints/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/ints/lec.pdf -------------------------------------------------------------------------------- /week1/ints/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Прерывания} 30 | \date{\today} 31 | 32 | \begin{document} 33 | \begin{frame} 34 | \titlepage 35 | \end{frame} 36 | \input{ints} 37 | \end{document} 38 | -------------------------------------------------------------------------------- /week1/ints/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/ints/lecv.pdf -------------------------------------------------------------------------------- /week1/ints/lecv.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,t]{beamer} 2 | 3 | \setbeamersize{text margin left=0pt, text margin right=4cm} 4 | \hypersetup{colorlinks=true,linkcolor=red} 5 | \usetheme{boxes} 6 | 7 | \usepackage[utf8]{inputenc} 8 | \usepackage[russian,english]{babel} 9 | \usepackage[T2A]{fontenc} 10 | \usepackage{hyperref} 11 | \usepackage[final]{listings} 12 | \usepackage{breakurl} 13 | \usepackage{cite} 14 | \usepackage{perpage} 15 | 16 | \graphicspath{{img/}} 17 | 18 | \def\Url\Breaks{\do\/\do-} 19 | \lstset{ 20 | frame=single, 21 | breaklines=true, 22 | basicstyle=\small, 23 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 24 | numbers=left 25 | } 26 | 27 | \MakePerPage{footnote} 28 | 29 | \title{Операционные Системы} 30 | \subtitle{Прерывания} 31 | \date{\today} 32 | 33 | \begin{document} 34 | \begin{frame} 35 | \titlepage 36 | \end{frame} 37 | \input{ints} 38 | \end{document} 39 | -------------------------------------------------------------------------------- /week1/load/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week1/load/bios-boot/Makefile: -------------------------------------------------------------------------------- 1 | LD ?= ld 2 | CC ?= gcc 3 | 4 | CFLAGS = -ffreestanding -m32 5 | AFLAGS = -ffreestanding -m32 6 | 7 | all: boot.bin 8 | 9 | OBJS = boot.o 10 | 11 | boot.bin: $(OBJS) linker.ld 12 | $(LD) -T linker.ld $(OBJS) -o boot.bin 13 | 14 | %.o: %.S 15 | $(CC) $(AFLAGS) -c $< -o $@ 16 | 17 | clean: 18 | rm *.o boot.bin 19 | 20 | .PHONY: clean 21 | -------------------------------------------------------------------------------- /week1/load/bios-boot/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(binary) 2 | OUTPUT_ARCH(i8086) 3 | ENTRY(start) 4 | 5 | SECTIONS { 6 | . = 0x7C00; 7 | .text : { *(.text) } 8 | . = 0x7DFE; 9 | /* BIOS determine MBR by special signature in 10 | * the last 2 bytes of 512 byte long sector. */ 11 | .signature : { SHORT(0xAA55) } 12 | } 13 | -------------------------------------------------------------------------------- /week1/load/bios-boot/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #qemu-system-i386 -hda boot.bin 4 | qemu-system-i386 -drive file=boot.bin,index=0,media=disk,format=raw 5 | -------------------------------------------------------------------------------- /week1/load/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/load/lec.pdf -------------------------------------------------------------------------------- /week1/load/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Процесс загрузки} 30 | \date{\today} 31 | 32 | \begin{document} 33 | \begin{frame} 34 | \titlepage 35 | \end{frame} 36 | \input{load} 37 | \end{document} 38 | -------------------------------------------------------------------------------- /week1/load/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week1/load/lecv.pdf -------------------------------------------------------------------------------- /week1/load/lecv.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,t]{beamer} 2 | 3 | \setbeamersize{text margin left=0pt, text margin right=4cm} 4 | \hypersetup{colorlinks=true,linkcolor=red} 5 | \usetheme{boxes} 6 | 7 | \usepackage[utf8]{inputenc} 8 | \usepackage[russian,english]{babel} 9 | \usepackage[T2A]{fontenc} 10 | \usepackage{hyperref} 11 | \usepackage[final]{listings} 12 | \usepackage{breakurl} 13 | \usepackage{cite} 14 | \usepackage{perpage} 15 | 16 | \graphicspath{{img/}} 17 | 18 | \def\Url\Breaks{\do\/\do-} 19 | \lstset{ 20 | frame=single, 21 | breaklines=true, 22 | basicstyle=\small, 23 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 24 | numbers=left 25 | } 26 | 27 | \MakePerPage{footnote} 28 | 29 | \title{Операционные Системы} 30 | \subtitle{Процесс загрузки} 31 | \date{\today} 32 | 33 | \begin{document} 34 | \begin{frame} 35 | \titlepage 36 | \end{frame} 37 | \input{load} 38 | \end{document} 39 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | 10 | #endif /*__STRING_H__*/ 11 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | static void qemu_gdb_hang(void) 9 | { 10 | #ifdef DEBUG 11 | static volatile int wait = 1; 12 | 13 | while (wait); 14 | #endif 15 | } 16 | 17 | 18 | void main(void) 19 | { 20 | qemu_gdb_hang(); 21 | vga_clr(); 22 | printf("Hello, World!\n"); 23 | } 24 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t strlen(const char *str) 4 | { 5 | const char *begin = str; 6 | 7 | while (*str++); 8 | return str - begin - 1; 9 | } 10 | 11 | void *memcpy(void *dst, const void *src, size_t size) 12 | { 13 | char *to = dst; 14 | const char *from = src; 15 | 16 | while (size--) 17 | *to++ = *from++; 18 | return dst; 19 | } 20 | 21 | void *memset(void *dst, int fill, size_t size) 22 | { 23 | char *to = dst; 24 | 25 | while (size--) 26 | *to++ = fill; 27 | return dst; 28 | } 29 | -------------------------------------------------------------------------------- /week1/load/multiboot-boot/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week2/alloc/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | void list_del(struct list_head *node); 15 | int list_empty(const struct list_head *list); 16 | 17 | #endif /*__LIST_H__*/ 18 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week2/alloc/buddy-allocator/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week2/alloc/img/bud0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud0.png -------------------------------------------------------------------------------- /week2/alloc/img/bud1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud1.png -------------------------------------------------------------------------------- /week2/alloc/img/bud2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud2.png -------------------------------------------------------------------------------- /week2/alloc/img/bud3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud3.png -------------------------------------------------------------------------------- /week2/alloc/img/bud4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud4.png -------------------------------------------------------------------------------- /week2/alloc/img/bud5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/bud5.png -------------------------------------------------------------------------------- /week2/alloc/img/lst0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst0.png -------------------------------------------------------------------------------- /week2/alloc/img/lst1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst1.png -------------------------------------------------------------------------------- /week2/alloc/img/lst2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst2.png -------------------------------------------------------------------------------- /week2/alloc/img/lst3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst3.png -------------------------------------------------------------------------------- /week2/alloc/img/lst4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst4.png -------------------------------------------------------------------------------- /week2/alloc/img/lst5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst5.png -------------------------------------------------------------------------------- /week2/alloc/img/lst6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst6.png -------------------------------------------------------------------------------- /week2/alloc/img/lst7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst7.png -------------------------------------------------------------------------------- /week2/alloc/img/lst8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst8.png -------------------------------------------------------------------------------- /week2/alloc/img/lst9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/lst9.png -------------------------------------------------------------------------------- /week2/alloc/img/slab0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/slab0.png -------------------------------------------------------------------------------- /week2/alloc/img/slab1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/img/slab1.png -------------------------------------------------------------------------------- /week2/alloc/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/lec.pdf -------------------------------------------------------------------------------- /week2/alloc/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Алгоритмы аллокации памяти} 30 | \date{\today} 31 | 32 | \begin{document} 33 | 34 | \begin{frame} 35 | \titlepage 36 | \end{frame} 37 | \input{simple} 38 | \input{buddy} 39 | \input{slab} 40 | \end{document} 41 | -------------------------------------------------------------------------------- /week2/alloc/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/alloc/lecv.pdf -------------------------------------------------------------------------------- /week2/log_mem/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | static void qemu_gdb_hang(void) 11 | { 12 | #ifdef DEBUG 13 | static volatile int wait = 1; 14 | 15 | while (wait); 16 | #endif 17 | } 18 | 19 | 20 | void main(uintptr_t mb_info_phys) 21 | { 22 | const struct multiboot_info *info = va(mb_info_phys); 23 | 24 | qemu_gdb_hang(); 25 | vga_clr(); 26 | ints_setup(); 27 | balloc_setup(info); 28 | 29 | while (1); 30 | } 31 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week2/log_mem/bootstrap-allocator/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week2/log_mem/fork/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= cc 2 | 3 | default: fork 4 | 5 | fork: main.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, default 9 | clean: 10 | rm fork 11 | -------------------------------------------------------------------------------- /week2/log_mem/fork/main.c: -------------------------------------------------------------------------------- 1 | #include /* waitpid */ 2 | #include /* fork */ 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | static int value = 42; 9 | const pid_t pid = fork(); 10 | 11 | if (pid == -1) { 12 | perror("failed to fork"); 13 | return 1; 14 | } 15 | 16 | if (!pid) value = 43; 17 | else waitpid(pid, NULL, 0); 18 | 19 | printf("value (%p) = %d\n", &value, value); 20 | 21 | return 0; 22 | }; 23 | -------------------------------------------------------------------------------- /week2/log_mem/img/dt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/dt.png -------------------------------------------------------------------------------- /week2/log_mem/img/gdt32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/gdt32.png -------------------------------------------------------------------------------- /week2/log_mem/img/gdttr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/gdttr.png -------------------------------------------------------------------------------- /week2/log_mem/img/page1gb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/page1gb.png -------------------------------------------------------------------------------- /week2/log_mem/img/pagemap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/pagemap.png -------------------------------------------------------------------------------- /week2/log_mem/img/pagetr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/pagetr.png -------------------------------------------------------------------------------- /week2/log_mem/img/pagex86.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/pagex86.png -------------------------------------------------------------------------------- /week2/log_mem/img/pte.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/pte.png -------------------------------------------------------------------------------- /week2/log_mem/img/sel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/img/sel.png -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week2/log_mem/initial-paging/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week2/log_mem/kern/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | 3 | obj-m := getseg.o 4 | CFLAGS_getseg.o += -DDEBUG 5 | 6 | else 7 | 8 | KDIR ?= /lib/modules/`uname -r`/build 9 | 10 | default: 11 | $(MAKE) -C "$(KDIR)" M=$$PWD 12 | 13 | clean: 14 | rm -rf *.o *.ko *.order *.symvers *.mod.c 15 | 16 | endif 17 | -------------------------------------------------------------------------------- /week2/log_mem/kern/getseg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int __init getseg_init(void) 6 | { 7 | unsigned short sel; 8 | 9 | __asm__ volatile ("movw %%cs, %0" : "=r"(sel)); 10 | 11 | pr_debug("CS = %#hx\n", sel); 12 | 13 | return 0; 14 | } 15 | 16 | static void __exit getseg_fini(void) 17 | { 18 | } 19 | 20 | module_init(getseg_init); 21 | module_exit(getseg_fini); 22 | MODULE_LICENSE("GPL"); 23 | -------------------------------------------------------------------------------- /week2/log_mem/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/lec.pdf -------------------------------------------------------------------------------- /week2/log_mem/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Логическая память} 30 | \date{\today} 31 | 32 | \begin{document} 33 | 34 | \begin{frame} 35 | \titlepage 36 | \end{frame} 37 | \input{log} 38 | \end{document} 39 | -------------------------------------------------------------------------------- /week2/log_mem/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/log_mem/lecv.pdf -------------------------------------------------------------------------------- /week2/log_mem/loop/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= cc 2 | 3 | default: hang 4 | 5 | hang: main.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, default 9 | clean: 10 | rm hang 11 | -------------------------------------------------------------------------------- /week2/log_mem/loop/main.c: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | while (1); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /week2/phys_mem/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week2/phys_mem/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/phys_mem/lec.pdf -------------------------------------------------------------------------------- /week2/phys_mem/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=single, 20 | breaklines=true, 21 | basicstyle=\small, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left 24 | } 25 | 26 | \MakePerPage{footnote} 27 | 28 | \title{Операционные Системы} 29 | \subtitle{Физическая память} 30 | \date{\today} 31 | 32 | \begin{document} 33 | \begin{frame} 34 | \titlepage 35 | \end{frame} 36 | \input{phys} 37 | \end{document} 38 | -------------------------------------------------------------------------------- /week2/phys_mem/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week2/phys_mem/lecv.pdf -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | 10 | #endif /*__STRING_H__*/ 11 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t strlen(const char *str) 4 | { 5 | const char *begin = str; 6 | 7 | while (*str++); 8 | return str - begin - 1; 9 | } 10 | 11 | void *memcpy(void *dst, const void *src, size_t size) 12 | { 13 | char *to = dst; 14 | const char *from = src; 15 | 16 | while (size--) 17 | *to++ = *from++; 18 | return dst; 19 | } 20 | 21 | void *memset(void *dst, int fill, size_t size) 22 | { 23 | char *to = dst; 24 | 25 | while (size--) 26 | *to++ = fill; 27 | return dst; 28 | } 29 | -------------------------------------------------------------------------------- /week2/phys_mem/physmem-map/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week3/threads/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week3/threads/img/multithreading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/multithreading.png -------------------------------------------------------------------------------- /week3/threads/img/preempt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/preempt.png -------------------------------------------------------------------------------- /week3/threads/img/rr0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr0.png -------------------------------------------------------------------------------- /week3/threads/img/rr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr1.png -------------------------------------------------------------------------------- /week3/threads/img/rr10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr10.png -------------------------------------------------------------------------------- /week3/threads/img/rr11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr11.png -------------------------------------------------------------------------------- /week3/threads/img/rr12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr12.png -------------------------------------------------------------------------------- /week3/threads/img/rr13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr13.png -------------------------------------------------------------------------------- /week3/threads/img/rr14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr14.png -------------------------------------------------------------------------------- /week3/threads/img/rr15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr15.png -------------------------------------------------------------------------------- /week3/threads/img/rr16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr16.png -------------------------------------------------------------------------------- /week3/threads/img/rr17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr17.png -------------------------------------------------------------------------------- /week3/threads/img/rr18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr18.png -------------------------------------------------------------------------------- /week3/threads/img/rr19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr19.png -------------------------------------------------------------------------------- /week3/threads/img/rr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr2.png -------------------------------------------------------------------------------- /week3/threads/img/rr20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr20.png -------------------------------------------------------------------------------- /week3/threads/img/rr21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr21.png -------------------------------------------------------------------------------- /week3/threads/img/rr22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr22.png -------------------------------------------------------------------------------- /week3/threads/img/rr23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr23.png -------------------------------------------------------------------------------- /week3/threads/img/rr24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr24.png -------------------------------------------------------------------------------- /week3/threads/img/rr25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr25.png -------------------------------------------------------------------------------- /week3/threads/img/rr26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr26.png -------------------------------------------------------------------------------- /week3/threads/img/rr27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr27.png -------------------------------------------------------------------------------- /week3/threads/img/rr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr3.png -------------------------------------------------------------------------------- /week3/threads/img/rr4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr4.png -------------------------------------------------------------------------------- /week3/threads/img/rr5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr5.png -------------------------------------------------------------------------------- /week3/threads/img/rr6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr6.png -------------------------------------------------------------------------------- /week3/threads/img/rr7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr7.png -------------------------------------------------------------------------------- /week3/threads/img/rr8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr8.png -------------------------------------------------------------------------------- /week3/threads/img/rr9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/rr9.png -------------------------------------------------------------------------------- /week3/threads/img/sched0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/sched0.png -------------------------------------------------------------------------------- /week3/threads/img/sched1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/sched1.png -------------------------------------------------------------------------------- /week3/threads/img/sched2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/sched2.png -------------------------------------------------------------------------------- /week3/threads/img/sched3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/sched3.png -------------------------------------------------------------------------------- /week3/threads/img/switch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/img/switch.png -------------------------------------------------------------------------------- /week3/threads/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/lec.pdf -------------------------------------------------------------------------------- /week3/threads/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week3/threads/lecv.pdf -------------------------------------------------------------------------------- /week3/threads/preempt/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS := -Wall -Werror -g 2 | 3 | .PHONY: all clean 4 | 5 | all: example 6 | 7 | example: switch.o main.o 8 | $(CC) -g $^ -o $@ 9 | 10 | switch.o: switch.S 11 | $(CC) $(CFLAGS) -c $< -o $@ 12 | 13 | main.o: main.c 14 | $(CC) $(CFLAGS) -c $< -o $@ 15 | 16 | clean: 17 | rm -rf *.o example 18 | -------------------------------------------------------------------------------- /week3/threads/preempt/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | 5 | __switch_threads: 6 | pushq %rbx 7 | pushq %rbp 8 | pushq %r12 9 | pushq %r13 10 | pushq %r14 11 | pushq %r15 12 | pushfq 13 | 14 | movq %rsp, (%rdi) 15 | movq %rsi, %rsp 16 | 17 | popfq 18 | popq %r15 19 | popq %r14 20 | popq %r13 21 | popq %r12 22 | popq %rbp 23 | popq %rbx 24 | 25 | retq 26 | -------------------------------------------------------------------------------- /week3/threads/thread-switch/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS := -Wall -Werror -g 2 | 3 | .PHONY: all clean 4 | 5 | all: example 6 | 7 | example: switch.o main.o 8 | $(CC) -g $^ -o $@ 9 | 10 | switch.o: switch.S 11 | $(CC) $(CFLAGS) -c $< -o $@ 12 | 13 | main.o: main.c 14 | $(CC) $(CFLAGS) -c $< -o $@ 15 | 16 | clean: 17 | rm -rf *.o example 18 | -------------------------------------------------------------------------------- /week3/threads/thread-switch/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | 5 | __switch_threads: 6 | pushq %rbx 7 | pushq %rbp 8 | pushq %r12 9 | pushq %r13 10 | pushq %r14 11 | pushq %r15 12 | pushfq 13 | 14 | movq %rsp, (%rdi) 15 | movq %rsi, %rsp 16 | 17 | popfq 18 | popq %r15 19 | popq %r14 20 | popq %r13 21 | popq %r12 22 | popq %rbp 23 | popq %rbx 24 | 25 | retq 26 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | void list_del(struct list_head *node); 15 | void list_splice(struct list_head *from, struct list_head *to); 16 | void list_splice_tail(struct list_head *from, struct list_head *to); 17 | int list_empty(const struct list_head *list); 18 | 19 | #endif /*__LIST_H__*/ 20 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week3/threads/threads/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week3/threads/threads/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week3/threads/threads/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week3/threads/threads/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week3/threads/threads/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week3/threads/threads/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week3/threads/threads/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week4/sync/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | void list_del(struct list_head *node); 15 | void list_splice(struct list_head *from, struct list_head *to); 16 | void list_splice_tail(struct list_head *from, struct list_head *to); 17 | int list_empty(const struct list_head *list); 18 | 19 | #endif /*__LIST_H__*/ 20 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/conditions/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/conditions/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week4/sync/conditions/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week4/sync/conditions/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week4/sync/conditions/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week4/sync/conditions/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week4/sync/conditions/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week4/sync/img/cv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/img/cv.png -------------------------------------------------------------------------------- /week4/sync/img/nthreads.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/img/nthreads.png -------------------------------------------------------------------------------- /week4/sync/img/order.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/img/order.png -------------------------------------------------------------------------------- /week4/sync/img/waitdie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/img/waitdie.png -------------------------------------------------------------------------------- /week4/sync/img/waitfor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/img/waitfor.png -------------------------------------------------------------------------------- /week4/sync/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/lec.pdf -------------------------------------------------------------------------------- /week4/sync/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week4/sync/lecv.pdf -------------------------------------------------------------------------------- /week4/sync/locks/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | void list_del(struct list_head *node); 15 | void list_splice(struct list_head *from, struct list_head *to); 16 | void list_splice_tail(struct list_head *from, struct list_head *to); 17 | int list_empty(const struct list_head *list); 18 | 19 | #endif /*__LIST_H__*/ 20 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/locks/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/locks/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week4/sync/locks/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week4/sync/locks/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week4/sync/locks/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week4/sync/locks/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week4/sync/locks/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | void list_del(struct list_head *node); 15 | void list_splice(struct list_head *from, struct list_head *to); 16 | void list_splice_tail(struct list_head *from, struct list_head *to); 17 | int list_empty(const struct list_head *list); 18 | 19 | #endif /*__LIST_H__*/ 20 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | void *memcpy(void *dst, const void *src, size_t size); 8 | void *memset(void *dst, int fill, size_t size); 9 | void *memmove(void *dst, const void *src, size_t size); 10 | 11 | #endif /*__STRING_H__*/ 12 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/mutexes/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week4/sync/mutexes/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week4/sync/mutexes/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week4/sync/mutexes/src/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | size_t strlen(const char *str) 6 | { 7 | const char *begin = str; 8 | 9 | while (*str++); 10 | return str - begin - 1; 11 | } 12 | 13 | void *memcpy(void *dst, const void *src, size_t size) 14 | { 15 | char *to = dst; 16 | const char *from = src; 17 | 18 | while (size--) 19 | *to++ = *from++; 20 | return dst; 21 | } 22 | 23 | void *memset(void *dst, int fill, size_t size) 24 | { 25 | char *to = dst; 26 | 27 | while (size--) 28 | *to++ = fill; 29 | return dst; 30 | } 31 | 32 | void *memmove(void *dst, const void *src, size_t size) 33 | { 34 | if ((uintptr_t)dst < (uintptr_t)src) 35 | return memcpy(dst, src, size); 36 | 37 | if (dst != src) { 38 | char *to = dst; 39 | const char *from = src; 40 | 41 | while (size--) 42 | to[size] = from[size]; 43 | } 44 | return dst; 45 | } 46 | -------------------------------------------------------------------------------- /week4/sync/mutexes/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week4/sync/mutexes/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week4/sync/mutexes/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-x86_64 -kernel kernel 4 | -------------------------------------------------------------------------------- /week4/sync/producer-consumer/Makefile: -------------------------------------------------------------------------------- 1 | CXX := g++ 2 | CFLAGS := -Wextra -Wall -Werror -pedantic -std=c++11 3 | LFLAGS := -pthread 4 | 5 | 6 | default: example 7 | 8 | example: example.o 9 | $(CXX) $(LFLAGS) $< -o $@ 10 | 11 | example.o: example.cpp 12 | $(CXX) $(CFLAGS) $< -c -o $@ 13 | 14 | .PHONY: clean, exmaple 15 | clean: 16 | rm *.o example 17 | -------------------------------------------------------------------------------- /week5/exec/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week5/exec/img/address-space.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/img/address-space.png -------------------------------------------------------------------------------- /week5/exec/img/amd64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/img/amd64.png -------------------------------------------------------------------------------- /week5/exec/img/x86.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/img/x86.png -------------------------------------------------------------------------------- /week5/exec/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/lec.pdf -------------------------------------------------------------------------------- /week5/exec/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/lecv.pdf -------------------------------------------------------------------------------- /week5/exec/shared/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= cc 2 | 3 | default: libfoo.so call 4 | 5 | libfoo.so: foo.c foo.h 6 | $(CC) -fpic -shared $< -o $@ 7 | 8 | call: call.c foo.h 9 | $(CC) $< -L. -lfoo -o $@ 10 | 11 | .PHONY: clean, default 12 | clean: 13 | rm libfoo.so call 14 | -------------------------------------------------------------------------------- /week5/exec/shared/call: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/shared/call -------------------------------------------------------------------------------- /week5/exec/shared/call.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "foo.h" 3 | 4 | int main(void) 5 | { 6 | printf("bar = %d\n", bar); 7 | foo(); 8 | printf("bar = %d\n", bar); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /week5/exec/shared/foo.c: -------------------------------------------------------------------------------- 1 | #include "foo.h" 2 | 3 | int bar; 4 | 5 | void foo(void) 6 | { ++bar; } 7 | -------------------------------------------------------------------------------- /week5/exec/shared/foo.h: -------------------------------------------------------------------------------- 1 | #ifndef __FOO_H__ 2 | #define __FOO_H__ 3 | 4 | extern void foo(void); 5 | extern int bar; 6 | 7 | #endif /*__FOO_H__*/ 8 | -------------------------------------------------------------------------------- /week5/exec/shared/libfoo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/exec/shared/libfoo.so -------------------------------------------------------------------------------- /week5/exec/shared/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | LD_LIBRARY_PATH=. ./call 4 | -------------------------------------------------------------------------------- /week5/exec/simple-elf/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello, World!\n"); 6 | printf("main address: %p\n", &main); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /week5/exec/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello, World!\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /week5/initramfs/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week5/initramfs/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week5/initramfs/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week5/initramfs/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week5/initramfs/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week5/initramfs/inc/initramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __INITRAMFS_H__ 2 | #define __INITRAMFS_H__ 3 | 4 | 5 | /** 6 | * The function parses initrd/initramfs image and populates 7 | * ramfs with the content of the image. 8 | **/ 9 | 10 | void initramfs_setup(void); 11 | 12 | #endif /*__INITRAMFS_H__*/ 13 | -------------------------------------------------------------------------------- /week5/initramfs/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | 15 | 16 | static inline void list_add_after(struct list_head *node, 17 | struct list_head *ptr) 18 | { 19 | list_add(node, ptr); 20 | } 21 | 22 | static inline void list_add_before(struct list_head *node, 23 | struct list_head *ptr) 24 | { 25 | list_add_tail(node, ptr); 26 | } 27 | 28 | void list_del(struct list_head *node); 29 | void list_splice(struct list_head *from, struct list_head *to); 30 | void list_splice_tail(struct list_head *from, struct list_head *to); 31 | int list_empty(const struct list_head *list); 32 | 33 | #endif /*__LIST_H__*/ 34 | -------------------------------------------------------------------------------- /week5/initramfs/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week5/initramfs/inc/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISC_H__ 2 | #define __MISC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | extern uintptr_t mmap_begin; 9 | extern uintptr_t mmap_end; 10 | 11 | extern uintptr_t initrd_begin; 12 | extern uintptr_t initrd_end; 13 | 14 | 15 | struct multiboot_info; 16 | 17 | void misc_setup(const struct multiboot_info *info); 18 | 19 | #endif /*__MISC_H__*/ 20 | -------------------------------------------------------------------------------- /week5/initramfs/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week5/initramfs/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week5/initramfs/inc/ramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __RAMFS_H__ 2 | #define __RAMFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * We need a some kind of file system, so this is a very primitive 10 | * in-ram file system, that doesn't remove (usually called unlink) 11 | * operation and directories. 12 | **/ 13 | #define RAMFS_MAX_NAME 255 14 | 15 | 16 | struct file { 17 | struct list_head ll; 18 | long size; 19 | char name[RAMFS_MAX_NAME + 1]; 20 | 21 | struct list_head data; 22 | }; 23 | 24 | 25 | int ramfs_create(const char *name, struct file **res); 26 | int ramfs_open(const char *name, struct file **res); 27 | void ramfs_close(struct file *file); 28 | 29 | long ramfs_readat(struct file *file, void *data, long size, long offs); 30 | long ramfs_writeat(struct file *file, const void *data, long size, long offs); 31 | 32 | void ramfs_setup(void); 33 | 34 | #endif /*__RAMFS_H__*/ 35 | -------------------------------------------------------------------------------- /week5/initramfs/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week5/initramfs/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | int strcmp(const char *l, const char *r); 8 | char *strcpy(char *dst, const char *src); 9 | char *strchr(const char *str, int c); 10 | int memcmp(const void *l, const void *r, size_t size); 11 | void *memcpy(void *dst, const void *src, size_t size); 12 | void *memset(void *dst, int fill, size_t size); 13 | void *memmove(void *dst, const void *src, size_t size); 14 | 15 | #endif /*__STRING_H__*/ 16 | -------------------------------------------------------------------------------- /week5/initramfs/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week5/initramfs/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week5/initramfs/initramfs/README: -------------------------------------------------------------------------------- 1 | Everything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | -------------------------------------------------------------------------------- /week5/initramfs/initrd.img: -------------------------------------------------------------------------------- 1 | 0707010058ECA4000041ED000003E8000003E8000000025C30B9EE00000000000001030000000500000000000000000000000A00000000initramfs07070100587E80000081A4000003E8000003E8000000015C30B9EE0000005C000001030000000500000000000000000000001100000000initramfs/READMEEverything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | 07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!! -------------------------------------------------------------------------------- /week5/initramfs/initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=initramfs 4 | INITRD=$(pwd)/initrd.img 5 | 6 | find $DIR -print0 | cpio --null -ov --format=newc > $INITRD 7 | -------------------------------------------------------------------------------- /week5/initramfs/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week5/initramfs/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week5/initramfs/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week5/initramfs/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week5/initramfs/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source initrd.sh 4 | qemu-system-x86_64 -kernel kernel -initrd initrd.img 5 | -------------------------------------------------------------------------------- /week5/ipc/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week5/ipc/exit/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | 9 | int main() 10 | { 11 | const pid_t pid = fork(); 12 | 13 | if (pid < 0) { 14 | printf("fork failed\n"); 15 | return -1; 16 | } 17 | 18 | if (pid) { 19 | int status; 20 | 21 | waitpid(pid, &status, 0); 22 | printf("Return code: %d\n", WEXITSTATUS(status)); 23 | } else { 24 | for (char c = 'n'; tolower(c) != 'y'; scanf("%c", &c)) 25 | printf("Kill me? (y/n)\n"); 26 | exit(42); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /week5/ipc/fd1/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | 3 | default: cat 4 | 5 | cat: cat.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, deafult 9 | clean: 10 | rm cat 11 | -------------------------------------------------------------------------------- /week5/ipc/fd2/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | 3 | default: touch 4 | 5 | touch: touch.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, deafult 9 | clean: 10 | rm touch 11 | -------------------------------------------------------------------------------- /week5/ipc/fd2/touch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | int main(int argc, char **argv) 9 | { 10 | int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 11 | 12 | for (int i = 1; i < argc; ++i) { 13 | int fd = open(argv[i], O_WRONLY | O_CREAT, perm); 14 | 15 | if (fd < 0) { 16 | fprintf(stderr, "Failed to create %s\n", argv[i]); 17 | return -1; 18 | } 19 | close(fd); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /week5/ipc/fd3/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | 3 | default: copy 4 | 5 | copy: copy.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, deafult 9 | clean: 10 | rm copy 11 | -------------------------------------------------------------------------------- /week5/ipc/fd4/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | 3 | default: race 4 | 5 | race: race.c 6 | $(CC) $< -o $@ 7 | 8 | .PHONY: clean, deafult 9 | clean: 10 | rm race 11 | -------------------------------------------------------------------------------- /week5/ipc/fd4/data: -------------------------------------------------------------------------------- 1 | Hello, World! 2 | Goodbye, World! 3 | -------------------------------------------------------------------------------- /week5/ipc/fd4/race.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | static const char *FILE_NAME = "data"; 9 | 10 | 11 | static void read_file(int fd) 12 | { 13 | char buf[2]; 14 | ssize_t rd; 15 | 16 | while ((rd = read(fd, buf, sizeof(buf) - 1))) { 17 | if (rd < 0) { 18 | fprintf(stderr, "Failed to read\n"); 19 | return; 20 | } 21 | 22 | buf[rd] = '\0'; 23 | printf("%d: %s\n", getpid(), buf); 24 | } 25 | } 26 | 27 | int main(void) 28 | { 29 | int fd = open(FILE_NAME, O_RDONLY); 30 | 31 | if (fd < 0) { 32 | fprintf(stderr, "Failed to open %s\n", FILE_NAME); 33 | return -1; 34 | } 35 | 36 | if (fork() < 0) { 37 | fprintf(stderr, "Failed to fork\n"); 38 | close(fd); 39 | return -1; 40 | } 41 | 42 | read_file(fd); 43 | 44 | close(fd); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /week5/ipc/fifo/rcv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #define FIFO "myfifo" 9 | 10 | int main() 11 | { 12 | mknod(FIFO, S_IFIFO | 0666, 0); 13 | 14 | printf("Wait for a writer...\n"); 15 | int fd = open(FIFO, O_RDONLY); 16 | printf("A writer connected, receiving...\n"); 17 | int size; 18 | char buf[4096]; 19 | 20 | while ((size = read(fd, buf, sizeof(buf) - 1)) > 0) { 21 | buf[size] = '\0'; 22 | printf("Received: %s\n", buf); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /week5/ipc/fifo/snd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #define FIFO "myfifo" 9 | 10 | int main() 11 | { 12 | mknod(FIFO, S_IFIFO | 0666, 0); 13 | 14 | printf("wait for a reader...\n"); 15 | int fd = open(FIFO, O_WRONLY); 16 | printf("A reader connected, sending...\n"); 17 | int size; 18 | char buf[4096]; 19 | 20 | while ((size = read(0, buf, sizeof(buf) - 1)) > 0) { 21 | buf[size] = '\0'; 22 | printf("Send: %s\n", buf); 23 | write(fd, buf, size); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /week5/ipc/fork/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | const pid_t pid = fork(); 8 | 9 | if (pid < 0) { 10 | printf("fork failed\n"); 11 | return -1; 12 | } 13 | 14 | if (pid) 15 | printf("%d: I'm your father %d\n", 16 | (int)getpid(), (int)pid); 17 | else 18 | printf("%d: Noooooo!!!\n", 19 | (int)getpid()); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /week5/ipc/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/ipc/lec.pdf -------------------------------------------------------------------------------- /week5/ipc/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/ipc/lecv.pdf -------------------------------------------------------------------------------- /week5/ipc/pipe/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | char buf[4096]; 8 | int size; 9 | int fd[2]; 10 | 11 | if (pipe(fd) < 0) { 12 | printf("pipe failed"); 13 | return -1; 14 | } 15 | 16 | const pid_t pid = fork(); 17 | 18 | if (pid < 0) { 19 | printf("fork failed"); 20 | return -1; 21 | } 22 | 23 | if (pid) { 24 | while ((size = read(0, buf, sizeof(buf) - 1)) > 0) { 25 | buf[size] = 0; 26 | printf("Send: %s\n", buf); 27 | write(fd[1], buf, size); 28 | } 29 | } else { 30 | while ((size = read(fd[0], buf, sizeof(buf) - 1)) > 0) { 31 | buf[size] = '\0'; 32 | printf("Received: %s\n", buf); 33 | } 34 | } 35 | 36 | close(fd[0]); 37 | close(fd[1]); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /week5/ipc/shm/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char **argv) 12 | { 13 | const key_t id = 3148; 14 | const int size = 4096; 15 | const int fd = shmget(id, size, 0666 | IPC_CREAT); 16 | 17 | if (fd < 0) { 18 | printf("shmget failed\n"); 19 | return -1; 20 | } 21 | 22 | void *ptr = shmat(fd, (void *)0, 0); 23 | 24 | if (ptr == (void *)-1) { 25 | printf("shmat failed\n"); 26 | return -1; 27 | } 28 | 29 | char c; 30 | while ((c = getopt(argc, argv, "p:g")) != -1) { 31 | switch (c) { 32 | case 'p': 33 | strcpy(ptr, optarg); 34 | break; 35 | case 'g': 36 | puts(ptr); 37 | break; 38 | } 39 | } 40 | shmdt(ptr); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /week5/load/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week5/load/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week5/load/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week5/load/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week5/load/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week5/load/inc/exec.h: -------------------------------------------------------------------------------- 1 | #ifndef __EXEC_H__ 2 | #define __EXEC_H__ 3 | 4 | int exec(int argc, const char **argv); 5 | 6 | #endif /*__EXEC_H__*/ 7 | -------------------------------------------------------------------------------- /week5/load/inc/initramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __INITRAMFS_H__ 2 | #define __INITRAMFS_H__ 3 | 4 | 5 | /** 6 | * The function parses initrd/initramfs image and populates 7 | * ramfs with the content of the image. 8 | **/ 9 | 10 | void initramfs_setup(void); 11 | 12 | #endif /*__INITRAMFS_H__*/ 13 | -------------------------------------------------------------------------------- /week5/load/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week5/load/inc/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISC_H__ 2 | #define __MISC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | extern uintptr_t mmap_begin; 9 | extern uintptr_t mmap_end; 10 | 11 | extern uintptr_t initrd_begin; 12 | extern uintptr_t initrd_end; 13 | 14 | 15 | struct multiboot_info; 16 | 17 | void misc_setup(const struct multiboot_info *info); 18 | 19 | #endif /*__MISC_H__*/ 20 | -------------------------------------------------------------------------------- /week5/load/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week5/load/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week5/load/inc/ramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __RAMFS_H__ 2 | #define __RAMFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * We need a some kind of file system, so this is a very primitive 10 | * in-ram file system, that doesn't remove (usually called unlink) 11 | * operation and directories. 12 | **/ 13 | #define RAMFS_MAX_NAME 255 14 | 15 | 16 | struct file { 17 | struct list_head ll; 18 | long size; 19 | char name[RAMFS_MAX_NAME + 1]; 20 | 21 | struct list_head data; 22 | }; 23 | 24 | 25 | int ramfs_create(const char *name, struct file **res); 26 | int ramfs_open(const char *name, struct file **res); 27 | void ramfs_close(struct file *file); 28 | 29 | long ramfs_readat(struct file *file, void *data, long size, long offs); 30 | long ramfs_writeat(struct file *file, const void *data, long size, long offs); 31 | 32 | void ramfs_setup(void); 33 | 34 | #endif /*__RAMFS_H__*/ 35 | -------------------------------------------------------------------------------- /week5/load/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week5/load/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | int strcmp(const char *l, const char *r); 8 | char *strcpy(char *dst, const char *src); 9 | char *strchr(const char *str, int c); 10 | int memcmp(const void *l, const void *r, size_t size); 11 | void *memcpy(void *dst, const void *src, size_t size); 12 | void *memset(void *dst, int fill, size_t size); 13 | void *memmove(void *dst, const void *src, size_t size); 14 | 15 | #endif /*__STRING_H__*/ 16 | -------------------------------------------------------------------------------- /week5/load/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week5/load/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week5/load/initramfs/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= cc 2 | CFLAGS := -ffreestanding -m64 -g 3 | LFLAGS := -nostdlib -z max-page-size=0x1000 -static 4 | 5 | default: test 6 | 7 | test: test.S 8 | $(CC) $< $(CFLAGS) $(LFLAGS) -o $@ 9 | 10 | .PHONY: clean default 11 | clean: 12 | rm test 13 | -------------------------------------------------------------------------------- /week5/load/initramfs/README: -------------------------------------------------------------------------------- 1 | Everything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | -------------------------------------------------------------------------------- /week5/load/initramfs/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/load/initramfs/test -------------------------------------------------------------------------------- /week5/load/initramfs/test.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global _start 4 | _start: 5 | jmp _start 6 | -------------------------------------------------------------------------------- /week5/load/initrd.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/load/initrd.img -------------------------------------------------------------------------------- /week5/load/initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=initramfs 4 | INITRD=$(pwd)/initrd.img 5 | 6 | find $DIR -print0 | cpio --null -ov --format=newc > $INITRD 7 | -------------------------------------------------------------------------------- /week5/load/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week5/load/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week5/load/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week5/load/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source initrd.sh 4 | qemu-system-x86_64 -kernel kernel -initrd initrd.img 5 | -------------------------------------------------------------------------------- /week5/mm/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week5/mm/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week5/mm/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week5/mm/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week5/mm/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week5/mm/inc/initramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __INITRAMFS_H__ 2 | #define __INITRAMFS_H__ 3 | 4 | 5 | /** 6 | * The function parses initrd/initramfs image and populates 7 | * ramfs with the content of the image. 8 | **/ 9 | 10 | void initramfs_setup(void); 11 | 12 | #endif /*__INITRAMFS_H__*/ 13 | -------------------------------------------------------------------------------- /week5/mm/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week5/mm/inc/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISC_H__ 2 | #define __MISC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | extern uintptr_t mmap_begin; 9 | extern uintptr_t mmap_end; 10 | 11 | extern uintptr_t initrd_begin; 12 | extern uintptr_t initrd_end; 13 | 14 | 15 | struct multiboot_info; 16 | 17 | void misc_setup(const struct multiboot_info *info); 18 | 19 | #endif /*__MISC_H__*/ 20 | -------------------------------------------------------------------------------- /week5/mm/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week5/mm/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week5/mm/inc/ramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __RAMFS_H__ 2 | #define __RAMFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * We need a some kind of file system, so this is a very primitive 10 | * in-ram file system, that doesn't remove (usually called unlink) 11 | * operation and directories. 12 | **/ 13 | #define RAMFS_MAX_NAME 255 14 | 15 | 16 | struct file { 17 | struct list_head ll; 18 | long size; 19 | char name[RAMFS_MAX_NAME + 1]; 20 | 21 | struct list_head data; 22 | }; 23 | 24 | 25 | int ramfs_create(const char *name, struct file **res); 26 | int ramfs_open(const char *name, struct file **res); 27 | void ramfs_close(struct file *file); 28 | 29 | long ramfs_readat(struct file *file, void *data, long size, long offs); 30 | long ramfs_writeat(struct file *file, const void *data, long size, long offs); 31 | 32 | void ramfs_setup(void); 33 | 34 | #endif /*__RAMFS_H__*/ 35 | -------------------------------------------------------------------------------- /week5/mm/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week5/mm/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | int strcmp(const char *l, const char *r); 8 | char *strcpy(char *dst, const char *src); 9 | char *strchr(const char *str, int c); 10 | int memcmp(const void *l, const void *r, size_t size); 11 | void *memcpy(void *dst, const void *src, size_t size); 12 | void *memset(void *dst, int fill, size_t size); 13 | void *memmove(void *dst, const void *src, size_t size); 14 | 15 | #endif /*__STRING_H__*/ 16 | -------------------------------------------------------------------------------- /week5/mm/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week5/mm/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week5/mm/initramfs/README: -------------------------------------------------------------------------------- 1 | Everything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | -------------------------------------------------------------------------------- /week5/mm/initrd.img: -------------------------------------------------------------------------------- 1 | 0707010058ECB8000041ED000003E8000003E8000000025C30B9EE00000000000001030000000500000000000000000000000A00000000initramfs07070100587F10000081A4000003E8000003E8000000015C30B9EE0000005C000001030000000500000000000000000000001100000000initramfs/READMEEverything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | 07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!! -------------------------------------------------------------------------------- /week5/mm/initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=initramfs 4 | INITRD=$(pwd)/initrd.img 5 | 6 | find $DIR -print0 | cpio --null -ov --format=newc > $INITRD 7 | -------------------------------------------------------------------------------- /week5/mm/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week5/mm/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week5/mm/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week5/mm/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week5/mm/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source initrd.sh 4 | qemu-system-x86_64 -kernel kernel -initrd initrd.img 5 | -------------------------------------------------------------------------------- /week5/sysc/Makefile: -------------------------------------------------------------------------------- 1 | SRC := $(wildcard ./*.tex) 2 | SRCS := $(filter-out lecv.tex, $(SRC)) 3 | SRCV := $(filter-out lev.tex, $(SRC)) 4 | IMG := $(wildcard ./img/*.png) 5 | 6 | all: lec.pdf lecv.pdf 7 | 8 | lecv.pdf: $(SRCV) $(IMG) 9 | pdflatex lecv 10 | 11 | lec.pdf: $(SRCS) $(IMG) 12 | pdflatex lec 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -f *.dvi *.ps *.pdf *.aux *.toc *.log *.blg *.bbl *.out *.snm *.nav *.vrb 17 | -------------------------------------------------------------------------------- /week5/sysc/img/desc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/sysc/img/desc.png -------------------------------------------------------------------------------- /week5/sysc/img/intstack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/sysc/img/intstack.png -------------------------------------------------------------------------------- /week5/sysc/img/tss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/sysc/img/tss.png -------------------------------------------------------------------------------- /week5/sysc/lec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/sysc/lec.pdf -------------------------------------------------------------------------------- /week5/sysc/lec.tex: -------------------------------------------------------------------------------- 1 | \documentclass[handout,12pt]{beamer} 2 | 3 | \hypersetup{colorlinks=true,linkcolor=red} 4 | \usetheme{boxes} 5 | 6 | \usepackage[utf8]{inputenc} 7 | \usepackage[russian,english]{babel} 8 | \usepackage[T2A]{fontenc} 9 | \usepackage{hyperref} 10 | \usepackage[final]{listings} 11 | \usepackage{breakurl} 12 | \usepackage{cite} 13 | \usepackage{perpage} 14 | 15 | \graphicspath{{img/}} 16 | 17 | \def\Url\Breaks{\do\/\do-} 18 | \lstset{ 19 | frame=none, 20 | breaklines=true, 21 | basicstyle=\lst@ifdisplaystyle\tiny\fi, 22 | postbreak=\raisebox{0ex}{\ensuremath{\hookrightarrow\space}}, 23 | numbers=left, 24 | showlines=true 25 | } 26 | 27 | \MakePerPage{footnote} 28 | 29 | \title{Операционные Системы} 30 | \subtitle{Системные вызовы} 31 | \date{\today} 32 | 33 | \begin{document} 34 | 35 | \begin{frame} 36 | \titlepage 37 | \end{frame} 38 | \input{syscall} 39 | \end{document} 40 | -------------------------------------------------------------------------------- /week5/sysc/lecv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/sysc/lecv.pdf -------------------------------------------------------------------------------- /week5/syscall/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week5/syscall/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week5/syscall/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week5/syscall/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week5/syscall/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week5/syscall/inc/exec.h: -------------------------------------------------------------------------------- 1 | #ifndef __EXEC_H__ 2 | #define __EXEC_H__ 3 | 4 | int exec(int argc, const char **argv); 5 | 6 | #endif /*__EXEC_H__*/ 7 | -------------------------------------------------------------------------------- /week5/syscall/inc/initramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __INITRAMFS_H__ 2 | #define __INITRAMFS_H__ 3 | 4 | 5 | /** 6 | * The function parses initrd/initramfs image and populates 7 | * ramfs with the content of the image. 8 | **/ 9 | 10 | void initramfs_setup(void); 11 | 12 | #endif /*__INITRAMFS_H__*/ 13 | -------------------------------------------------------------------------------- /week5/syscall/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week5/syscall/inc/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISC_H__ 2 | #define __MISC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | extern uintptr_t mmap_begin; 9 | extern uintptr_t mmap_end; 10 | 11 | extern uintptr_t initrd_begin; 12 | extern uintptr_t initrd_end; 13 | 14 | 15 | struct multiboot_info; 16 | 17 | void misc_setup(const struct multiboot_info *info); 18 | 19 | #endif /*__MISC_H__*/ 20 | -------------------------------------------------------------------------------- /week5/syscall/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week5/syscall/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week5/syscall/inc/ramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __RAMFS_H__ 2 | #define __RAMFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * We need a some kind of file system, so this is a very primitive 10 | * in-ram file system, that doesn't remove (usually called unlink) 11 | * operation and directories. 12 | **/ 13 | #define RAMFS_MAX_NAME 255 14 | 15 | 16 | struct file { 17 | struct list_head ll; 18 | long size; 19 | char name[RAMFS_MAX_NAME + 1]; 20 | 21 | struct list_head data; 22 | }; 23 | 24 | 25 | int ramfs_create(const char *name, struct file **res); 26 | int ramfs_open(const char *name, struct file **res); 27 | void ramfs_close(struct file *file); 28 | 29 | long ramfs_readat(struct file *file, void *data, long size, long offs); 30 | long ramfs_writeat(struct file *file, const void *data, long size, long offs); 31 | 32 | void ramfs_setup(void); 33 | 34 | #endif /*__RAMFS_H__*/ 35 | -------------------------------------------------------------------------------- /week5/syscall/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week5/syscall/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | int strcmp(const char *l, const char *r); 8 | char *strcpy(char *dst, const char *src); 9 | char *strchr(const char *str, int c); 10 | int memcmp(const void *l, const void *r, size_t size); 11 | void *memcpy(void *dst, const void *src, size_t size); 12 | void *memset(void *dst, int fill, size_t size); 13 | void *memmove(void *dst, const void *src, size_t size); 14 | 15 | #endif /*__STRING_H__*/ 16 | -------------------------------------------------------------------------------- /week5/syscall/inc/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef __SYSCALL_H__ 2 | #define __SYSCALL_H__ 3 | 4 | void handle_syscall(void); 5 | 6 | #endif /*__SYSCALL_H__*/ 7 | -------------------------------------------------------------------------------- /week5/syscall/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week5/syscall/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week5/syscall/initramfs/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= cc 2 | CFLAGS := -ffreestanding -m64 -g 3 | LFLAGS := -nostdlib -z max-page-size=0x1000 -static 4 | 5 | default: test 6 | 7 | test: test.S 8 | $(CC) $< $(CFLAGS) $(LFLAGS) -o $@ 9 | 10 | .PHONY: clean default 11 | clean: 12 | rm test 13 | -------------------------------------------------------------------------------- /week5/syscall/initramfs/README: -------------------------------------------------------------------------------- 1 | Everything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | -------------------------------------------------------------------------------- /week5/syscall/initramfs/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/syscall/initramfs/test -------------------------------------------------------------------------------- /week5/syscall/initramfs/test.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global _start 4 | _start: 5 | int $0x80 6 | jmp _start 7 | -------------------------------------------------------------------------------- /week5/syscall/initrd.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cscenter/OS_online_course/b73f0de04de274df6a1376a17337db7da5d222c9/week5/syscall/initrd.img -------------------------------------------------------------------------------- /week5/syscall/initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=initramfs 4 | INITRD=$(pwd)/initrd.img 5 | 6 | find $DIR -print0 | cpio --null -ov --format=newc > $INITRD 7 | -------------------------------------------------------------------------------- /week5/syscall/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week5/syscall/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week5/syscall/src/syscall.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void handle_syscall(void) 6 | { 7 | printf("syscall!\n"); 8 | } 9 | -------------------------------------------------------------------------------- /week5/syscall/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week5/syscall/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source initrd.sh 4 | qemu-system-x86_64 -kernel kernel -initrd initrd.img 5 | -------------------------------------------------------------------------------- /week5/tss/inc/apic.h: -------------------------------------------------------------------------------- 1 | #ifndef __APIC_H__ 2 | #define __APIC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | #define LOCAL_APIC_TPR 0x80 9 | #define LOCAL_APIC_EOI 0xb0 10 | #define LOCAL_APIC_DFR 0xd0 11 | #define LOCAL_APIC_LDR 0xe0 12 | #define LOCAL_APIC_SPURIOUS 0xf0 13 | #define LOCAL_APIC_TIMER_LVT 0x320 14 | #define LOCAL_APIC_TIMER_INIT 0x380 15 | #define LOCAL_APIC_TIMER_COUNT 0x390 16 | #define LOCAL_APIC_TIMER_DIVIDER 0x3e0 17 | 18 | 19 | void local_apic_write(int reg, uint32_t value); 20 | uint32_t local_apic_read(int reg); 21 | 22 | void local_apic_setup(void); 23 | 24 | #endif /*__APIC_H__*/ 25 | -------------------------------------------------------------------------------- /week5/tss/inc/backtrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __BACKTRACE_H__ 2 | #define __BACKTRACE_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top); 9 | 10 | #endif /*__BACKTRACE_H__*/ 11 | -------------------------------------------------------------------------------- /week5/tss/inc/buddy.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUDDY_H__ 2 | #define __BUDDY_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* Maximum possible allocation size 2^20 pages */ 8 | #define MAX_ORDER 20 9 | 10 | struct page { 11 | struct list_head ll; 12 | unsigned long flags; 13 | int order; 14 | }; 15 | 16 | 17 | void buddy_setup(void); 18 | 19 | /** 20 | * Buddy alloc/free routines are given in two versions: 21 | * - one returns descriptor (struct page) 22 | * - other resutrns physical address 23 | **/ 24 | struct page *__buddy_alloc(int order); 25 | uintptr_t buddy_alloc(int order); 26 | void __buddy_free(struct page *page, int order); 27 | void buddy_free(uintptr_t phys, int order); 28 | 29 | 30 | /* Convertion routines: descriptor to physical address and vice versa. */ 31 | uintptr_t page_addr(const struct page *page); 32 | struct page *addr_page(uintptr_t phys); 33 | 34 | #endif /*__BUDDY_H__*/ 35 | -------------------------------------------------------------------------------- /week5/tss/inc/condition.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDITION_H__ 2 | #define __CONDITION_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | struct condition { 9 | struct spinlock lock; 10 | struct list_head wait; 11 | }; 12 | 13 | struct mutex; 14 | 15 | 16 | void condition_setup(struct condition *cv); 17 | 18 | /* Two versions of wait: one works with spinlock, other works with mutex. */ 19 | void condition_wait_spin(struct condition *cv, struct spinlock *lock); 20 | void condition_wait_spin_int(struct condition *cv, struct spinlock *lock); 21 | void condition_wait(struct condition *cv, struct mutex *lock); 22 | 23 | void notify_one(struct condition *cv); 24 | void notify_all(struct condition *cv); 25 | 26 | #endif /*__CONDITION_H__*/ 27 | -------------------------------------------------------------------------------- /week5/tss/inc/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef __CTYPE_H__ 2 | #define __CTYPE_H__ 3 | 4 | int isprint(int c); 5 | int isalpha(int c); 6 | int isdigit(int c); 7 | int isxdigit(int c); 8 | int isspace(int c); 9 | int islower(int c); 10 | int isupper(int c); 11 | 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif /*__CTYPE_H__*/ 16 | -------------------------------------------------------------------------------- /week5/tss/inc/initramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __INITRAMFS_H__ 2 | #define __INITRAMFS_H__ 3 | 4 | 5 | /** 6 | * The function parses initrd/initramfs image and populates 7 | * ramfs with the content of the image. 8 | **/ 9 | 10 | void initramfs_setup(void); 11 | 12 | #endif /*__INITRAMFS_H__*/ 13 | -------------------------------------------------------------------------------- /week5/tss/inc/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST_H__ 2 | #define __LIST_H__ 3 | 4 | 5 | struct list_head { 6 | struct list_head *next; 7 | struct list_head *prev; 8 | }; 9 | 10 | 11 | void list_init(struct list_head *list); 12 | void list_add_tail(struct list_head *node, struct list_head *list); 13 | void list_add(struct list_head *node, struct list_head *list); 14 | 15 | 16 | static inline void list_add_after(struct list_head *node, 17 | struct list_head *ptr) 18 | { 19 | list_add(node, ptr); 20 | } 21 | 22 | static inline void list_add_before(struct list_head *node, 23 | struct list_head *ptr) 24 | { 25 | list_add_tail(node, ptr); 26 | } 27 | 28 | void list_del(struct list_head *node); 29 | void list_splice(struct list_head *from, struct list_head *to); 30 | void list_splice_tail(struct list_head *from, struct list_head *to); 31 | int list_empty(const struct list_head *list); 32 | 33 | #endif /*__LIST_H__*/ 34 | -------------------------------------------------------------------------------- /week5/tss/inc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOCK_H__ 2 | #define __LOCK_H__ 3 | 4 | 5 | /** 6 | * In a single processor system we don't need to store anything 7 | * inside lock. But we still going to call it spinlock. 8 | **/ 9 | struct spinlock { int dummy; }; 10 | 11 | 12 | void spin_setup(struct spinlock *lock); 13 | 14 | 15 | /** 16 | * Use this to protect data that is cannot be accessed from 17 | * an interrupt handler. 18 | **/ 19 | void spin_lock(struct spinlock *lock); 20 | void spin_unlock(struct spinlock *lock); 21 | 22 | /** 23 | * Use this to protect datat that can be accessed from 24 | * an interrupt handler. 25 | **/ 26 | int spin_lock_int_save(struct spinlock *lock); 27 | void spin_unlock_int_restore(struct spinlock *lock, int enable); 28 | 29 | #endif /*__LOCK_H__*/ 30 | -------------------------------------------------------------------------------- /week5/tss/inc/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISC_H__ 2 | #define __MISC_H__ 3 | 4 | 5 | #include 6 | 7 | 8 | extern uintptr_t mmap_begin; 9 | extern uintptr_t mmap_end; 10 | 11 | extern uintptr_t initrd_begin; 12 | extern uintptr_t initrd_end; 13 | 14 | 15 | struct multiboot_info; 16 | 17 | void misc_setup(const struct multiboot_info *info); 18 | 19 | #endif /*__MISC_H__*/ 20 | -------------------------------------------------------------------------------- /week5/tss/inc/mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef __MUTEX_H__ 2 | #define __MUTEX_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Mutex guaranties mutual exclusion as spinlock does, but if a 9 | * thread tries to acquire a mutex while it's being held by 10 | * another thread the thread trying to acquire the mutex will 11 | * be blocked. 12 | **/ 13 | struct thread; 14 | 15 | struct mutex { 16 | struct spinlock lock; 17 | struct list_head wait; 18 | struct thread *owner; 19 | }; 20 | 21 | 22 | void mutex_setup(struct mutex *mutex); 23 | 24 | void mutex_lock(struct mutex *mutex); 25 | void mutex_unlock(struct mutex *mutex); 26 | 27 | #endif /*__MUTEX_H__*/ 28 | -------------------------------------------------------------------------------- /week5/tss/inc/print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRINT_H__ 2 | #define __PRINT_H__ 3 | 4 | #include 5 | #include 6 | 7 | int snprintf(char *buf, size_t size, const char *fmt, ...); 8 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 9 | int printf(const char *fmt, ...); 10 | int vprintf(const char *fmt, va_list args); 11 | 12 | #endif /*__PRINT_H__*/ 13 | -------------------------------------------------------------------------------- /week5/tss/inc/ramfs.h: -------------------------------------------------------------------------------- 1 | #ifndef __RAMFS_H__ 2 | #define __RAMFS_H__ 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * We need a some kind of file system, so this is a very primitive 10 | * in-ram file system, that doesn't remove (usually called unlink) 11 | * operation and directories. 12 | **/ 13 | #define RAMFS_MAX_NAME 255 14 | 15 | 16 | struct file { 17 | struct list_head ll; 18 | long size; 19 | char name[RAMFS_MAX_NAME + 1]; 20 | 21 | struct list_head data; 22 | }; 23 | 24 | 25 | int ramfs_create(const char *name, struct file **res); 26 | int ramfs_open(const char *name, struct file **res); 27 | void ramfs_close(struct file *file); 28 | 29 | long ramfs_readat(struct file *file, void *data, long size, long offs); 30 | long ramfs_writeat(struct file *file, const void *data, long size, long offs); 31 | 32 | void ramfs_setup(void); 33 | 34 | #endif /*__RAMFS_H__*/ 35 | -------------------------------------------------------------------------------- /week5/tss/inc/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDLIB_H__ 2 | #define __STDLIB_H__ 3 | 4 | unsigned long strtoul(const char *str, char **endptr, int base); 5 | 6 | char *ulltoa(unsigned long long value, char *str, int base); 7 | char *lltoa(long long value, char *str, int base); 8 | char *ultoa(unsigned long value, char *str, int base); 9 | char *ltoa(long value, char *str, int base); 10 | char *utoa(unsigned value, char *str, int base); 11 | char *itoa(int value, char *str, int base); 12 | 13 | #endif /*__STDLIB_H__*/ 14 | -------------------------------------------------------------------------------- /week5/tss/inc/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRING_H__ 2 | #define __STRING_H__ 3 | 4 | #include 5 | 6 | size_t strlen(const char *str); 7 | int strcmp(const char *l, const char *r); 8 | char *strcpy(char *dst, const char *src); 9 | char *strchr(const char *str, int c); 10 | int memcmp(const void *l, const void *r, size_t size); 11 | void *memcpy(void *dst, const void *src, size_t size); 12 | void *memset(void *dst, int fill, size_t size); 13 | void *memmove(void *dst, const void *src, size_t size); 14 | 15 | #endif /*__STRING_H__*/ 16 | -------------------------------------------------------------------------------- /week5/tss/inc/time.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIME_H__ 2 | #define __TIME_H__ 3 | 4 | 5 | void time_setup(void); 6 | 7 | #endif /*__TIME_H__*/ 8 | -------------------------------------------------------------------------------- /week5/tss/inc/vga.h: -------------------------------------------------------------------------------- 1 | #ifndef __VGA_H__ 2 | #define __VGA_H__ 3 | 4 | void vga_write(const char *data, size_t size); 5 | void vga_clr(void); 6 | 7 | #endif /*__VGA_H__*/ 8 | -------------------------------------------------------------------------------- /week5/tss/initramfs/README: -------------------------------------------------------------------------------- 1 | Everything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | -------------------------------------------------------------------------------- /week5/tss/initrd.img: -------------------------------------------------------------------------------- 1 | 0707010058ECC2000041ED000003E8000003E8000000025C30B9EE00000000000001030000000500000000000000000000000A00000000initramfs07070100587F94000081A4000003E8000003E8000000015C30B9EE0000005C000001030000000500000000000000000000001100000000initramfs/READMEEverything you put in this directory will be added to the 2 | initramfs image file (initrd.img) 3 | 07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!! -------------------------------------------------------------------------------- /week5/tss/initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=initramfs 4 | INITRD=$(pwd)/initrd.img 5 | 6 | find $DIR -print0 | cpio --null -ov --format=newc > $INITRD 7 | -------------------------------------------------------------------------------- /week5/tss/kernel.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf64-x86-64) 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start32) 4 | 5 | PAGE_SIZE = 0x1000; 6 | VIRTUAL_BASE = 0xffffffff80000000; 7 | 8 | SECTIONS 9 | { 10 | . = 1M + SIZEOF_HEADERS; 11 | 12 | text_phys_begin = .; 13 | .bootstrap : { *(.bootstrap) } 14 | 15 | . += VIRTUAL_BASE; 16 | .text : AT(ADDR(.bootstrap) + SIZEOF(.bootstrap)) 17 | { *(.text) *(.text.*) } 18 | 19 | data_phys_begin = . - VIRTUAL_BASE; 20 | .rodata : { *(.rodata) *(.rodata.*) } 21 | .data : { *(.data) *(.data.*) *(.got) *(.got.*) } 22 | data_phys_end = . - VIRTUAL_BASE; 23 | . = ALIGN(PAGE_SIZE); 24 | 25 | bss_phys_begin = . - VIRTUAL_BASE; 26 | .bss : { *(.bss) *(.bss.*) } 27 | bss_phys_end = . - VIRTUAL_BASE; 28 | } 29 | -------------------------------------------------------------------------------- /week5/tss/src/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void backtrace(uintptr_t rbp, uintptr_t stack_bottom, uintptr_t stack_top) 6 | { 7 | int frame_index = 0; 8 | 9 | while (rbp >= stack_bottom && rbp + 16 <= stack_top) { 10 | const uint64_t *frame = (const uint64_t *)rbp; 11 | const uintptr_t prev_rbp = frame[0]; 12 | const uintptr_t prev_rip = frame[1]; 13 | 14 | if (prev_rbp <= rbp) 15 | break; 16 | 17 | printf("%d: RIP 0x%llx\n", frame_index, 18 | (unsigned long long)prev_rip); 19 | rbp = prev_rbp; 20 | ++frame_index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week5/tss/src/switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | .code64 3 | .global __switch_threads 4 | .global __thread_entry 5 | .extern thread_entry 6 | 7 | __thread_entry: 8 | movq %r15, %rdi 9 | movq %r14, %rsi 10 | movq %r13, %rdx 11 | 12 | cld 13 | call thread_entry 14 | 15 | 16 | __switch_threads: 17 | pushq %rbx 18 | pushq %rbp 19 | pushq %r12 20 | pushq %r13 21 | pushq %r14 22 | pushq %r15 23 | pushfq 24 | 25 | movq %rsp, (%rdi) 26 | movq %rsi, %rsp 27 | 28 | popfq 29 | popq %r15 30 | popq %r14 31 | popq %r13 32 | popq %r12 33 | popq %rbp 34 | popq %rbx 35 | 36 | retq 37 | -------------------------------------------------------------------------------- /week5/tss/src/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | static const uint32_t TIMER_PERIODIC = (1 << 17); 10 | static const uint32_t TIMER_DIV128 = 10; 11 | static const uint32_t TIMER_INIT = 262144; 12 | 13 | 14 | static void timer_handler(void) 15 | { 16 | scheduler_tick(); 17 | } 18 | 19 | static void local_apic_timer_setup(void) 20 | { 21 | const int intno = allocate_interrupt(); 22 | 23 | register_interrupt_handler(intno, &timer_handler); 24 | 25 | local_apic_write(LOCAL_APIC_TIMER_DIVIDER, TIMER_DIV128); 26 | local_apic_write(LOCAL_APIC_TIMER_LVT, TIMER_PERIODIC | intno); 27 | local_apic_write(LOCAL_APIC_TIMER_INIT, TIMER_INIT); 28 | } 29 | 30 | void time_setup(void) 31 | { 32 | local_apic_timer_setup(); 33 | } 34 | -------------------------------------------------------------------------------- /week5/tss/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source initrd.sh 4 | qemu-system-x86_64 -kernel kernel -initrd initrd.img 5 | --------------------------------------------------------------------------------