├── .cvsignore ├── .dir-locals.el ├── .gdbinit.tmpl ├── .gitignore ├── BUGS ├── LICENSE ├── Makefile ├── Notes ├── README ├── README.md ├── TRICKS ├── asm.h ├── bio.c ├── bootasm.S ├── bootmain.c ├── buf.h ├── cat.c ├── console.c ├── cuth ├── defs.h ├── dot-bochsrc ├── echo.c ├── elf.h ├── entry.S ├── entryother.S ├── exec.c ├── fcntl.h ├── file.c ├── file.h ├── forktest.c ├── fs.c ├── fs.h ├── gdbutil ├── grep.c ├── ide.c ├── init.c ├── initcode.S ├── ioapic.c ├── kalloc.c ├── kbd.c ├── kbd.h ├── kernel.ld ├── kill.c ├── lapic.c ├── ln.c ├── log.c ├── ls.c ├── main.c ├── memide.c ├── memlayout.h ├── mkdir.c ├── mkfs.c ├── mmu.h ├── mp.c ├── mp.h ├── param.h ├── picirq.c ├── pipe.c ├── pr.pl ├── printf.c ├── printpcs ├── proc.c ├── proc.h ├── rm.c ├── runoff ├── runoff.list ├── runoff.spec ├── runoff1 ├── sh.c ├── show1 ├── sign.pl ├── sleep1.p ├── spinlock.c ├── spinlock.h ├── spinp ├── stat.h ├── stressfs.c ├── string.c ├── swtch.S ├── symlink.patch ├── syscall.c ├── syscall.h ├── sysfile.c ├── sysproc.c ├── timer.c ├── toc.ftr ├── toc.hdr ├── trap.c ├── trapasm.S ├── traps.h ├── types.h ├── uart.c ├── ulib.c ├── umalloc.c ├── user.h ├── usertests.c ├── usys.S ├── vectors.pl ├── vm.c ├── wc.c ├── x86.h └── zombie.c /.cvsignore: -------------------------------------------------------------------------------- 1 | *.asm 2 | *.d 3 | *.sym 4 | _* 5 | kernel 6 | user1 7 | userfs 8 | usertests 9 | xv6.img 10 | vectors.S 11 | bochsout.txt 12 | bootblock 13 | bootother 14 | bootother.out 15 | parport.out 16 | fmt 17 | -------------------------------------------------------------------------------- /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ((c-mode 2 | (indent-tabs-mode . nil) 3 | (c-file-style . "bsd") 4 | (c-basic-offset . 2))) 5 | -------------------------------------------------------------------------------- /.gdbinit.tmpl: -------------------------------------------------------------------------------- 1 | set $lastcs = -1 2 | 3 | define hook-stop 4 | # There doesn't seem to be a good way to detect if we're in 16- or 5 | # 32-bit mode, but in 32-bit mode we always run with CS == 8 in the 6 | # kernel and CS == 35 in user space 7 | if $cs == 8 || $cs == 35 8 | if $lastcs != 8 && $lastcs != 35 9 | set architecture i386 10 | end 11 | x/i $pc 12 | else 13 | if $lastcs == -1 || $lastcs == 8 || $lastcs == 35 14 | set architecture i8086 15 | end 16 | # Translate the segment:offset into a physical address 17 | printf "[%4x:%4x] ", $cs, $eip 18 | x/i $cs*16+$eip 19 | end 20 | set $lastcs = $cs 21 | end 22 | 23 | echo + target remote localhost:1234\n 24 | target remote localhost:1234 25 | 26 | echo + symbol-file kernel\n 27 | symbol-file kernel 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | _* 3 | *.o 4 | *.d 5 | *.asm 6 | *.sym 7 | *.img 8 | vectors.S 9 | bootblock 10 | entryother 11 | initcode 12 | initcode.out 13 | kernel 14 | kernelmemfs 15 | mkfs 16 | .gdbinit 17 | -------------------------------------------------------------------------------- /BUGS: -------------------------------------------------------------------------------- 1 | formatting: 2 | need to fix PAGEBREAK mechanism 3 | 4 | sh: 5 | can't always runcmd in child -- breaks cd. 6 | maybe should hard-code PATH=/ ? 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The xv6 software is: 2 | 3 | Copyright (c) 2006-2009 Frans Kaashoek, Robert Morris, Russ Cox, 4 | Massachusetts Institute of Technology 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OBJS = \ 2 | bio.o\ 3 | console.o\ 4 | exec.o\ 5 | file.o\ 6 | fs.o\ 7 | ide.o\ 8 | ioapic.o\ 9 | kalloc.o\ 10 | kbd.o\ 11 | lapic.o\ 12 | log.o\ 13 | main.o\ 14 | mp.o\ 15 | picirq.o\ 16 | pipe.o\ 17 | proc.o\ 18 | spinlock.o\ 19 | string.o\ 20 | swtch.o\ 21 | syscall.o\ 22 | sysfile.o\ 23 | sysproc.o\ 24 | timer.o\ 25 | trapasm.o\ 26 | trap.o\ 27 | uart.o\ 28 | vectors.o\ 29 | vm.o\ 30 | 31 | # Cross-compiling (e.g., on Mac OS X) 32 | #TOOLPREFIX = i386-jos-elf- 33 | 34 | # Using native tools (e.g., on X86 Linux) 35 | #TOOLPREFIX = 36 | 37 | # Try to infer the correct TOOLPREFIX if not set 38 | ifndef TOOLPREFIX 39 | TOOLPREFIX := $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \ 40 | then echo 'i386-jos-elf-'; \ 41 | elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \ 42 | then echo ''; \ 43 | else echo "***" 1>&2; \ 44 | echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \ 45 | echo "*** Is the directory with i386-jos-elf-gcc in your PATH?" 1>&2; \ 46 | echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \ 47 | echo "*** prefix other than 'i386-jos-elf-', set your TOOLPREFIX" 1>&2; \ 48 | echo "*** environment variable to that prefix and run 'make' again." 1>&2; \ 49 | echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \ 50 | echo "***" 1>&2; exit 1; fi) 51 | endif 52 | 53 | # If the makefile can't find QEMU, specify its path here 54 | #QEMU = 55 | 56 | # Try to infer the correct QEMU 57 | ifndef QEMU 58 | QEMU = $(shell if which qemu > /dev/null; \ 59 | then echo qemu; exit; \ 60 | else \ 61 | qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-softmmu; \ 62 | if test -x $$qemu; then echo $$qemu; exit; fi; fi; \ 63 | echo "***" 1>&2; \ 64 | echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \ 65 | echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \ 66 | echo "*** or have you tried setting the QEMU variable in Makefile?" 1>&2; \ 67 | echo "***" 1>&2; exit 1) 68 | endif 69 | 70 | CC = $(TOOLPREFIX)gcc 71 | AS = $(TOOLPREFIX)gas 72 | LD = $(TOOLPREFIX)ld 73 | OBJCOPY = $(TOOLPREFIX)objcopy 74 | OBJDUMP = $(TOOLPREFIX)objdump 75 | #CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer 76 | CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer 77 | CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) 78 | ASFLAGS = -m32 -gdwarf-2 -Wa,-divide 79 | # FreeBSD ld wants ``elf_i386_fbsd'' 80 | LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null) 81 | 82 | xv6.img: bootblock kernel fs.img 83 | dd if=/dev/zero of=xv6.img count=10000 84 | dd if=bootblock of=xv6.img conv=notrunc 85 | dd if=kernel of=xv6.img seek=1 conv=notrunc 86 | 87 | xv6memfs.img: bootblock kernelmemfs 88 | dd if=/dev/zero of=xv6memfs.img count=10000 89 | dd if=bootblock of=xv6memfs.img conv=notrunc 90 | dd if=kernelmemfs of=xv6memfs.img seek=1 conv=notrunc 91 | 92 | bootblock: bootasm.S bootmain.c 93 | $(CC) $(CFLAGS) -fno-pic -O -nostdinc -I. -c bootmain.c 94 | $(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm.S 95 | $(LD) $(LDFLAGS) -N -e start -Ttext 0x7C00 -o bootblock.o bootasm.o bootmain.o 96 | $(OBJDUMP) -S bootblock.o > bootblock.asm 97 | $(OBJCOPY) -S -O binary -j .text bootblock.o bootblock 98 | ./sign.pl bootblock 99 | 100 | entryother: entryother.S 101 | $(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c entryother.S 102 | $(LD) $(LDFLAGS) -N -e start -Ttext 0x7000 -o bootblockother.o entryother.o 103 | $(OBJCOPY) -S -O binary -j .text bootblockother.o entryother 104 | $(OBJDUMP) -S bootblockother.o > entryother.asm 105 | 106 | initcode: initcode.S 107 | $(CC) $(CFLAGS) -nostdinc -I. -c initcode.S 108 | $(LD) $(LDFLAGS) -N -e start -Ttext 0 -o initcode.out initcode.o 109 | $(OBJCOPY) -S -O binary initcode.out initcode 110 | $(OBJDUMP) -S initcode.o > initcode.asm 111 | 112 | kernel: $(OBJS) entry.o entryother initcode kernel.ld 113 | $(LD) $(LDFLAGS) -T kernel.ld -o kernel entry.o $(OBJS) -b binary initcode entryother 114 | $(OBJDUMP) -S kernel > kernel.asm 115 | $(OBJDUMP) -t kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym 116 | 117 | # kernelmemfs is a copy of kernel that maintains the 118 | # disk image in memory instead of writing to a disk. 119 | # This is not so useful for testing persistent storage or 120 | # exploring disk buffering implementations, but it is 121 | # great for testing the kernel on real hardware without 122 | # needing a scratch disk. 123 | MEMFSOBJS = $(filter-out ide.o,$(OBJS)) memide.o 124 | kernelmemfs: $(MEMFSOBJS) entry.o entryother initcode fs.img 125 | $(LD) $(LDFLAGS) -Ttext 0x100000 -e main -o kernelmemfs entry.o $(MEMFSOBJS) -b binary initcode entryother fs.img 126 | $(OBJDUMP) -S kernelmemfs > kernelmemfs.asm 127 | $(OBJDUMP) -t kernelmemfs | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernelmemfs.sym 128 | 129 | tags: $(OBJS) entryother.S _init 130 | etags *.S *.c 131 | 132 | vectors.S: vectors.pl 133 | perl vectors.pl > vectors.S 134 | 135 | ULIB = ulib.o usys.o printf.o umalloc.o 136 | 137 | _%: %.o $(ULIB) 138 | $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^ 139 | $(OBJDUMP) -S $@ > $*.asm 140 | $(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym 141 | 142 | _forktest: forktest.o $(ULIB) 143 | # forktest has less library code linked in - needs to be small 144 | # in order to be able to max out the proc table. 145 | $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o _forktest forktest.o ulib.o usys.o 146 | $(OBJDUMP) -S _forktest > forktest.asm 147 | 148 | mkfs: mkfs.c fs.h 149 | gcc -Werror -Wall -o mkfs mkfs.c 150 | 151 | # Prevent deletion of intermediate files, e.g. cat.o, after first build, so 152 | # that disk image changes after first build are persistent until clean. More 153 | # details: 154 | # http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html 155 | .PRECIOUS: %.o 156 | 157 | UPROGS=\ 158 | _cat\ 159 | _echo\ 160 | _forktest\ 161 | _grep\ 162 | _init\ 163 | _kill\ 164 | _ln\ 165 | _ls\ 166 | _mkdir\ 167 | _rm\ 168 | _sh\ 169 | _stressfs\ 170 | _usertests\ 171 | _wc\ 172 | _zombie\ 173 | 174 | fs.img: mkfs README $(UPROGS) 175 | ./mkfs fs.img README $(UPROGS) 176 | 177 | -include *.d 178 | 179 | clean: 180 | rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ 181 | *.o *.d *.asm *.sym vectors.S bootblock entryother \ 182 | initcode initcode.out kernel xv6.img fs.img kernelmemfs mkfs \ 183 | .gdbinit \ 184 | $(UPROGS) 185 | 186 | # make a printout 187 | FILES = $(shell grep -v '^\#' runoff.list) 188 | PRINT = runoff.list runoff.spec README toc.hdr toc.ftr $(FILES) 189 | 190 | xv6.pdf: $(PRINT) 191 | ./runoff 192 | ls -l xv6.pdf 193 | 194 | print: xv6.pdf 195 | 196 | # run in emulators 197 | 198 | bochs : fs.img xv6.img 199 | if [ ! -e .bochsrc ]; then ln -s dot-bochsrc .bochsrc; fi 200 | bochs -q 201 | 202 | # try to generate a unique GDB port 203 | GDBPORT = $(shell expr `id -u` % 5000 + 25000) 204 | # QEMU's gdb stub command line changed in 0.11 205 | QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \ 206 | then echo "-gdb tcp::$(GDBPORT)"; \ 207 | else echo "-s -p $(GDBPORT)"; fi) 208 | ifndef CPUS 209 | CPUS := 2 210 | endif 211 | QEMUOPTS = -hdb fs.img xv6.img -smp $(CPUS) -m 512 $(QEMUEXTRA) 212 | 213 | qemu: fs.img xv6.img 214 | $(QEMU) -serial mon:stdio $(QEMUOPTS) 215 | 216 | qemu-memfs: xv6memfs.img 217 | $(QEMU) xv6memfs.img -smp $(CPUS) 218 | 219 | qemu-nox: fs.img xv6.img 220 | $(QEMU) -nographic $(QEMUOPTS) 221 | 222 | .gdbinit: .gdbinit.tmpl 223 | sed "s/localhost:1234/localhost:$(GDBPORT)/" < $^ > $@ 224 | 225 | qemu-gdb: fs.img xv6.img .gdbinit 226 | @echo "*** Now run 'gdb'." 1>&2 227 | $(QEMU) -serial mon:stdio $(QEMUOPTS) -S $(QEMUGDB) 228 | 229 | qemu-nox-gdb: fs.img xv6.img .gdbinit 230 | @echo "*** Now run 'gdb'." 1>&2 231 | $(QEMU) -nographic $(QEMUOPTS) -S $(QEMUGDB) 232 | 233 | # CUT HERE 234 | # prepare dist for students 235 | # after running make dist, probably want to 236 | # rename it to rev0 or rev1 or so on and then 237 | # check in that version. 238 | 239 | EXTRA=\ 240 | mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\ 241 | ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\ 242 | printf.c umalloc.c\ 243 | README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\ 244 | .gdbinit.tmpl gdbutil\ 245 | 246 | dist: 247 | rm -rf dist 248 | mkdir dist 249 | for i in $(FILES); \ 250 | do \ 251 | grep -v PAGEBREAK $$i >dist/$$i; \ 252 | done 253 | sed '/CUT HERE/,$$d' Makefile >dist/Makefile 254 | echo >dist/runoff.spec 255 | cp $(EXTRA) dist 256 | 257 | dist-test: 258 | rm -rf dist 259 | make dist 260 | rm -rf dist-test 261 | mkdir dist-test 262 | cp dist/* dist-test 263 | cd dist-test; $(MAKE) print 264 | cd dist-test; $(MAKE) bochs || true 265 | cd dist-test; $(MAKE) qemu 266 | 267 | # update this rule (change rev#) when it is time to 268 | # make a new revision. 269 | tar: 270 | rm -rf /tmp/xv6 271 | mkdir -p /tmp/xv6 272 | cp dist/* dist/.gdbinit.tmpl /tmp/xv6 273 | (cd /tmp; tar cf - xv6) | gzip >xv6-rev5.tar.gz 274 | 275 | .PHONY: dist-test dist 276 | -------------------------------------------------------------------------------- /Notes: -------------------------------------------------------------------------------- 1 | bochs 2.2.6: 2 | ./configure --enable-smp --enable-disasm --enable-debugger --enable-all-optimizations --enable-4meg-pages --enable-global-pages --enable-pae --disable-reset-on-triple-fault 3 | bochs CVS after 2.2.6: 4 | ./configure --enable-smp --enable-disasm --enable-debugger --enable-all-optimizations --enable-4meg-pages --enable-global-pages --enable-pae 5 | 6 | bootmain.c doesn't work right if the ELF sections aren't 7 | sector-aligned. so you can't use ld -N. and the sections may also need 8 | to be non-zero length, only really matters for tiny "kernels". 9 | 10 | kernel loaded at 1 megabyte. stack same place that bootasm.S left it. 11 | 12 | kinit() should find real mem size 13 | and rescue useable memory below 1 meg 14 | 15 | no paging, no use of page table hardware, just segments 16 | 17 | no user area: no magic kernel stack mapping 18 | so no copying of kernel stack during fork 19 | though there is a kernel stack page for each process 20 | 21 | no kernel malloc(), just kalloc() for user core 22 | 23 | user pointers aren't valid in the kernel 24 | 25 | are interrupts turned on in the kernel? yes. 26 | 27 | pass curproc explicitly, or implicit from cpu #? 28 | e.g. argument to newproc()? 29 | hmm, you need a global curproc[cpu] for trap() &c 30 | 31 | no stack expansion 32 | 33 | test running out of memory, process slots 34 | 35 | we can't really use a separate stack segment, since stack addresses 36 | need to work correctly as ordinary pointers. the same may be true of 37 | data vs text. how can we have a gap between data and stack, so that 38 | both can grow, without committing 4GB of physical memory? does this 39 | mean we need paging? 40 | 41 | perhaps have fixed-size stack, put it in the data segment? 42 | 43 | oops, if kernel stack is in contiguous user phys mem, then moving 44 | users' memory (e.g. to expand it) will wreck any pointers into the 45 | kernel stack. 46 | 47 | do we need to set fs and gs? so user processes can't abuse them? 48 | 49 | setupsegs() may modify current segment table, is that legal? 50 | 51 | trap() ought to lgdt on return, since currently only done in swtch() 52 | 53 | protect hardware interrupt vectors from user INT instructions? 54 | 55 | test out-of-fd cases for creating pipe. 56 | test pipe reader closes then write 57 | test two readers, two writers. 58 | test children being inherited by grandparent &c 59 | 60 | some sleep()s should be interruptible by kill() 61 | 62 | locks 63 | init_lock 64 | sequences CPU startup 65 | proc_table_lock 66 | also protects next_pid 67 | per-fd lock *just* protects count read-modify-write 68 | also maybe freeness? 69 | memory allocator 70 | printf 71 | 72 | in general, the table locks protect both free-ness and 73 | public variables of table elements 74 | in many cases you can use table elements w/o a lock 75 | e.g. if you are the process, or you are using an fd 76 | 77 | lock order 78 | per-pipe lock 79 | proc_table_lock fd_table_lock kalloc_lock 80 | console_lock 81 | 82 | do you have to be holding the mutex in order to call wakeup()? yes 83 | 84 | device interrupts don't clear FL_IF 85 | so a recursive timer interrupt is possible 86 | 87 | what does inode->busy mean? 88 | might be held across disk reads 89 | no-one is allowed to do anything to the inode 90 | protected by inode_table_lock 91 | inode->count counts in-memory pointers to the struct 92 | prevents inode[] element from being re-used 93 | protected by inode_table_lock 94 | 95 | blocks and inodes have ad-hoc sleep-locks 96 | provide a single mechanism? 97 | 98 | kalloc() can return 0; do callers handle this right? 99 | 100 | test: one process unlinks a file while another links to it 101 | test: one process opens a file while another deletes it 102 | test: deadlock d/.. vs ../d, two processes. 103 | test: dup() shared fd->off 104 | test: does echo foo > x truncate x? 105 | 106 | sh: ioredirection incorrect now we have pipes 107 | sh: chain of pipes won't work, also ugly that parent closes fdarray entries too 108 | sh: dynamic memory allocation? 109 | sh: should sh support ; () & 110 | sh: stop stdin on ctrl-d (for cat > y) 111 | 112 | really should have bdwrite() for file content 113 | and make some inode updates async 114 | so soft updates make sense 115 | 116 | disk scheduling 117 | echo foo > bar should truncate bar 118 | so O_CREATE should not truncate 119 | but O_TRUNC should 120 | 121 | make it work on a real machine 122 | release before acquire at end of sleep? 123 | check 2nd disk (i.e. if not in .bochsrc) 124 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix 2 | Version 6 (v6). xv6 loosely follows the structure and style of v6, 3 | but is implemented for a modern x86-based multiprocessor using ANSI C. 4 | 5 | ACKNOWLEDGMENTS 6 | 7 | xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer 8 | to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14, 9 | 2000)). See also http://pdos.csail.mit.edu/6.828/2012/v6.html, which 10 | provides pointers to on-line resources for v6. 11 | 12 | xv6 borrows code from the following sources: 13 | JOS (asm.h, elf.h, mmu.h, bootasm.S, ide.c, console.c, and others) 14 | Plan 9 (entryother.S, mp.h, mp.c, lapic.c) 15 | FreeBSD (ioapic.c) 16 | NetBSD (console.c) 17 | 18 | The following people have made contributions: 19 | Russ Cox (context switching, locking) 20 | Cliff Frey (MP) 21 | Xiao Yu (MP) 22 | Nickolai Zeldovich 23 | Austin Clements 24 | 25 | In addition, we are grateful for the patches contributed by Greg 26 | Price, Yandong Mao, and Hitoshi Mitake. 27 | 28 | The code in the files that constitute xv6 is 29 | Copyright 2006-2012 Frans Kaashoek, Robert Morris, and Russ Cox. 30 | 31 | ERROR REPORTS 32 | 33 | If you spot errors or have suggestions for improvement, please send 34 | email to Frans Kaashoek and Robert Morris (kaashoek,rtm@csail.mit.edu). 35 | 36 | BUILDING AND RUNNING XV6 37 | 38 | To build xv6 on an x86 ELF machine (like Linux or FreeBSD), run "make". 39 | On non-x86 or non-ELF machines (like OS X, even on x86), you will 40 | need to install a cross-compiler gcc suite capable of producing x86 ELF 41 | binaries. See http://pdos.csail.mit.edu/6.828/2012/tools.html. 42 | Then run "make TOOLPREFIX=i386-jos-elf-". 43 | 44 | To run xv6, you can use the Bochs or QEMU PC simulators. Bochs makes 45 | debugging easier, but QEMU is much faster. To run in Bochs, run "make 46 | bochs" and then type "c" at the bochs prompt. To run in QEMU, run 47 | "make qemu". 48 | 49 | To create a typeset version of the code, run "make xv6.pdf". This 50 | requires the "mpage" utility. See http://www.mesa.nl/pub/mpage/. 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##学习xv6 2 | 3 | xv6是由麻省理工学院(MIT)为操作系统工程的课程(代号6.828),开发的一个教学目的的操作系统。Xv6是在x86处理器上(x即指x86)用ANSI标准C重新实现的Unix第六版(Unix V6,通常直接被称为V6)。 4 | 5 | 更为详细的介绍可以参看: 6 | 7 | [百度百科上关于 xv6 的介绍](http://baike.baidu.com/view/4715931.htm?fr=aladdin) 8 | 9 | [维基百科上关于 xv6 的介绍](https://en.wikipedia.org/wiki/Xv6) 10 | 11 | [xv6 的官网](http://pdos.csail.mit.edu/6.828/2012/xv6.html) 12 | 13 | 这个代码库的 master 上是从 xv6 官方代码库直接 clone 的代码 14 | 15 | ```git 16 | git clone git://pdos.csail.mit.edu/xv6/xv6.git 17 | ``` 18 | 19 | 代码库的 learn 分支则是我在学习 xv6 代码时一边学一边在源码文件上增加了自己的理解和学习笔记作为注释。 20 | 21 | ###注意: 22 | 23 | 由于 learn 分支上的 xv6 代码,特别时汇编文件被我增加了注释,目前无法顺利编译,后续我可能会解决这个问题。如果向编译 xv6 一睹其芳容的各位,建议编译我这个代码库的 master 或者直接 clone 官方代码库的代码编译。 24 | 25 | ###笔记: 26 | 27 | 除了会在 xv6 的源代码上增加注释以外,我还会陆陆续续在[我的个人 Blog](http://leenjewel.github.io)上写一些列的文章来总结我的 xv6 学习过程。 28 | 29 | ###2014年7月24日 第 1 次学习: 30 | 31 | 如何通过 804x 键盘控制器打卡 A20 门进入保护模式。主要笔记文件: 32 | 33 | ``` 34 | bootasm.S 35 | ``` 36 | 37 | ###2014年7月25日 第 2 次学习: 38 | 39 | 关于 GDT 表和 GDT 表项的建立。主要笔记文件: 40 | 41 | ``` 42 | bootasm.S 43 | ``` 44 | 45 | ###2014年7月29日 第 3 次学习: 46 | 47 | 关于通过 cr0 控制寄存器进入保护模式以及在基于分段式的保护模式下的寻址,主要文件: 48 | 49 | ``` 50 | bootasm.S 51 | ``` 52 | 53 | ###2015年 5月26日 第 4 次学习: 54 | 55 | 内核的加载与运行,主要文件: 56 | 57 | ``` 58 | bootmain.c 59 | ``` 60 | 61 | -------------------------------------------------------------------------------- /TRICKS: -------------------------------------------------------------------------------- 1 | This file lists subtle things that might not be commented 2 | as well as they should be in the source code and that 3 | might be worth pointing out in a longer explanation or in class. 4 | 5 | --- 6 | 7 | [2009/07/12: No longer relevant; forkret1 changed 8 | and this is now cleaner.] 9 | 10 | forkret1 in trapasm.S is called with a tf argument. 11 | In order to use it, forkret1 copies the tf pointer into 12 | %esp and then jumps to trapret, which pops the 13 | register state out of the trap frame. If an interrupt 14 | came in between the mov tf, %esp and the iret that 15 | goes back out to user space, the interrupt stack frame 16 | would end up scribbling over the tf and whatever memory 17 | lay under it. 18 | 19 | Why is this safe? Because forkret1 is only called 20 | the first time a process returns to user space, and 21 | at that point, cp->tf is set to point to a trap frame 22 | constructed at the top of cp's kernel stack. So tf 23 | *is* a valid %esp that can hold interrupt state. 24 | 25 | If other tf's were used in forkret1, we could add 26 | a cli before the mov tf, %esp. 27 | 28 | --- 29 | 30 | In pushcli, must cli() no matter what. It is not safe to do 31 | 32 | if(cpus[cpu()].ncli == 0) 33 | cli(); 34 | cpus[cpu()].ncli++; 35 | 36 | because if interrupts are off then we might call cpu(), get 37 | rescheduled to a different cpu, look at cpus[oldcpu].ncli, 38 | and wrongly decide not to disable interrupts on the new cpu. 39 | 40 | Instead do 41 | 42 | cli(); 43 | cpus[cpu()].ncli++; 44 | 45 | always. 46 | 47 | --- 48 | 49 | There is a (harmless) race in pushcli, which does 50 | 51 | eflags = readeflags(); 52 | cli(); 53 | if(c->ncli++ == 0) 54 | c->intena = eflags & FL_IF; 55 | 56 | Consider a bottom-level pushcli. 57 | If interrupts are disabled already, then the right thing 58 | happens: read_eflags finds that FL_IF is not set, 59 | and intena = 0. If interrupts are enabled, then 60 | it is less clear that the right thing happens: 61 | the readeflags can execute, then the process 62 | can get preempted and rescheduled on another cpu, 63 | and then once it starts running, perhaps with 64 | interrupts disabled (can happen since the scheduler 65 | only enables interrupts once per scheduling loop, 66 | not every time it schedules a process), it will 67 | incorrectly record that interrupts *were* enabled. 68 | This doesn't matter, because if it was safe to be 69 | running with interrupts enabled before the context 70 | switch, it is still safe (and arguably more correct) 71 | to run with them enabled after the context switch too. 72 | 73 | In fact it would be safe if scheduler always set 74 | c->intena = 1; 75 | before calling swtch, and perhaps it should. 76 | 77 | --- 78 | 79 | The x86's processor-ordering memory model 80 | matches spin locks well, so no explicit memory 81 | synchronization instructions are required in 82 | acquire and release. 83 | 84 | Consider two sequences of code on different CPUs: 85 | 86 | CPU0 87 | A; 88 | release(lk); 89 | 90 | and 91 | 92 | CPU1 93 | acquire(lk); 94 | B; 95 | 96 | We want to make sure that: 97 | - all reads in B see the effects of writes in A. 98 | - all reads in A do *not* see the effects of writes in B. 99 | 100 | The x86 guarantees that writes in A will go out 101 | to memory before the write of lk->locked = 0 in 102 | release(lk). It further guarantees that CPU1 103 | will observe CPU0's write of lk->locked = 0 only 104 | after observing the earlier writes by CPU0. 105 | So any reads in B are guaranteed to observe the 106 | effects of writes in A. 107 | 108 | According to the Intel manual behavior spec, the 109 | second condition requires a serialization instruction 110 | in release, to avoid reads in A happening after giving 111 | up lk. No Intel SMP processor in existence actually 112 | moves reads down after writes, but the language in 113 | the spec allows it. There is no telling whether future 114 | processors will need it. 115 | 116 | --- 117 | 118 | The code in fork needs to read np->pid before 119 | setting np->state to RUNNABLE. 120 | 121 | int 122 | fork(void) 123 | { 124 | ... 125 | pid = np->pid; 126 | np->state = RUNNABLE; 127 | return pid; 128 | } 129 | 130 | After setting np->state to RUNNABLE, some other CPU 131 | might run the process, it might exit, and then it might 132 | get reused for a different process (with a new pid), all 133 | before the return statement. So it's not safe to just do 134 | "return np->pid;". 135 | 136 | This works because proc.h marks the pid as volatile. 137 | -------------------------------------------------------------------------------- /asm.h: -------------------------------------------------------------------------------- 1 | // 2 | // assembler macros to create x86 segments 3 | // 4 | 5 | #define SEG_NULLASM \ 6 | .word 0, 0; \ 7 | .byte 0, 0, 0, 0 8 | 9 | // The 0xC0 means the limit is in 4096-byte units 10 | // and (for executable segments) 32-bit mode. 11 | #define SEG_ASM(type,base,lim) \ 12 | .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \ 13 | .byte (((base) >> 16) & 0xff), (0x90 | (type)), \ 14 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 15 | 16 | #define STA_X 0x8 // Executable segment 17 | #define STA_E 0x4 // Expand down (non-executable segments) 18 | #define STA_C 0x4 // Conforming code segment (executable only) 19 | #define STA_W 0x2 // Writeable (non-executable segments) 20 | #define STA_R 0x2 // Readable (executable segments) 21 | #define STA_A 0x1 // Accessed 22 | -------------------------------------------------------------------------------- /bio.c: -------------------------------------------------------------------------------- 1 | // Buffer cache. 2 | // 3 | // The buffer cache is a linked list of buf structures holding 4 | // cached copies of disk block contents. Caching disk blocks 5 | // in memory reduces the number of disk reads and also provides 6 | // a synchronization point for disk blocks used by multiple processes. 7 | // 8 | // Interface: 9 | // * To get a buffer for a particular disk block, call bread. 10 | // * After changing buffer data, call bwrite to write it to disk. 11 | // * When done with the buffer, call brelse. 12 | // * Do not use the buffer after calling brelse. 13 | // * Only one process at a time can use a buffer, 14 | // so do not keep them longer than necessary. 15 | // 16 | // The implementation uses three state flags internally: 17 | // * B_BUSY: the block has been returned from bread 18 | // and has not been passed back to brelse. 19 | // * B_VALID: the buffer data has been read from the disk. 20 | // * B_DIRTY: the buffer data has been modified 21 | // and needs to be written to disk. 22 | 23 | #include "types.h" 24 | #include "defs.h" 25 | #include "param.h" 26 | #include "spinlock.h" 27 | #include "buf.h" 28 | 29 | struct { 30 | struct spinlock lock; 31 | struct buf buf[NBUF]; 32 | 33 | // Linked list of all buffers, through prev/next. 34 | // head.next is most recently used. 35 | struct buf head; 36 | } bcache; 37 | 38 | void 39 | binit(void) 40 | { 41 | struct buf *b; 42 | 43 | initlock(&bcache.lock, "bcache"); 44 | 45 | //PAGEBREAK! 46 | // Create linked list of buffers 47 | bcache.head.prev = &bcache.head; 48 | bcache.head.next = &bcache.head; 49 | for(b = bcache.buf; b < bcache.buf+NBUF; b++){ 50 | b->next = bcache.head.next; 51 | b->prev = &bcache.head; 52 | b->dev = -1; 53 | bcache.head.next->prev = b; 54 | bcache.head.next = b; 55 | } 56 | } 57 | 58 | // Look through buffer cache for sector on device dev. 59 | // If not found, allocate fresh block. 60 | // In either case, return B_BUSY buffer. 61 | static struct buf* 62 | bget(uint dev, uint sector) 63 | { 64 | struct buf *b; 65 | 66 | acquire(&bcache.lock); 67 | 68 | loop: 69 | // Is the sector already cached? 70 | for(b = bcache.head.next; b != &bcache.head; b = b->next){ 71 | if(b->dev == dev && b->sector == sector){ 72 | if(!(b->flags & B_BUSY)){ 73 | b->flags |= B_BUSY; 74 | release(&bcache.lock); 75 | return b; 76 | } 77 | sleep(b, &bcache.lock); 78 | goto loop; 79 | } 80 | } 81 | 82 | // Not cached; recycle some non-busy and clean buffer. 83 | for(b = bcache.head.prev; b != &bcache.head; b = b->prev){ 84 | if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){ 85 | b->dev = dev; 86 | b->sector = sector; 87 | b->flags = B_BUSY; 88 | release(&bcache.lock); 89 | return b; 90 | } 91 | } 92 | panic("bget: no buffers"); 93 | } 94 | 95 | // Return a B_BUSY buf with the contents of the indicated disk sector. 96 | struct buf* 97 | bread(uint dev, uint sector) 98 | { 99 | struct buf *b; 100 | 101 | b = bget(dev, sector); 102 | if(!(b->flags & B_VALID)) 103 | iderw(b); 104 | return b; 105 | } 106 | 107 | // Write b's contents to disk. Must be B_BUSY. 108 | void 109 | bwrite(struct buf *b) 110 | { 111 | if((b->flags & B_BUSY) == 0) 112 | panic("bwrite"); 113 | b->flags |= B_DIRTY; 114 | iderw(b); 115 | } 116 | 117 | // Release a B_BUSY buffer. 118 | // Move to the head of the MRU list. 119 | void 120 | brelse(struct buf *b) 121 | { 122 | if((b->flags & B_BUSY) == 0) 123 | panic("brelse"); 124 | 125 | acquire(&bcache.lock); 126 | 127 | b->next->prev = b->prev; 128 | b->prev->next = b->next; 129 | b->next = bcache.head.next; 130 | b->prev = &bcache.head; 131 | bcache.head.next->prev = b; 132 | bcache.head.next = b; 133 | 134 | b->flags &= ~B_BUSY; 135 | wakeup(b); 136 | 137 | release(&bcache.lock); 138 | } 139 | 140 | -------------------------------------------------------------------------------- /bootasm.S: -------------------------------------------------------------------------------- 1 | #include "asm.h" 2 | #include "memlayout.h" 3 | #include "mmu.h" 4 | 5 | # Start the first CPU: switch to 32-bit protected mode, jump into C. 6 | # The BIOS loads this code from the first sector of the hard disk into 7 | # memory at physical address 0x7c00 and starts executing in real mode 8 | # with %cs=0 %ip=7c00. 9 | 10 | .code16 # Assemble for 16-bit mode 11 | .globl start 12 | start: 13 | cli # BIOS enabled interrupts; disable 14 | 15 | # Zero data segment registers DS, ES, and SS. 16 | xorw %ax,%ax # Set %ax to zero 17 | movw %ax,%ds # -> Data Segment 18 | movw %ax,%es # -> Extra Segment 19 | movw %ax,%ss # -> Stack Segment 20 | 21 | # Physical address line A20 is tied to zero so that the first PCs 22 | # with 2 MB would run software that assumed 1 MB. Undo that. 23 | # /////////////////////////////////////////////////////////////// 24 | # 这里用了通过键盘控制器端口的方法打开 A20 地址 25 | # 打开 A20 地址线的方法有三种,具体可参考: 26 | # http://kernelx.weebly.com/a20-address-line.html 27 | # 28 | # 下面都是针对 804x 键盘控制器端口的操作,这里只介绍用到的两个端口 29 | # 0x64 从该端口执行 in 读取操作时,读取状态寄存器,8 位,第二 30 | # 位标识输入缓冲是否有数据所以下面用 0x2 来 test 输入缓冲是否有数据 31 | # 向该端口执行 out 写操作时,代表向键盘控制器发送命令,发送 0xd1 代 32 | # 表要向键盘控制器的 P2 端口写入数据,要写入的数据通过 0x60 端口传入 33 | # 34 | # P2 端口,8位,第二位就是 A20 开关,所以在 seta20.2 代码段将 0xdf 通过 35 | # 0x60 端口写入到 P2 时就会把 A20 置位,A20 打开后,进入保护模式 36 | seta20.1: 37 | inb $0x64,%al # Wait for not busy 38 | testb $0x2,%al 39 | jnz seta20.1 # 通过 0x64 状态寄存器的第二位判断键盘缓冲区里是否有数据,如果还有数据,则等待 40 | 41 | movb $0xd1,%al # 0xd1 -> port 0x64 42 | outb %al,$0x64 # 0xd1 代表向 804x 控制器的 P2 写数据 43 | 44 | seta20.2: 45 | inb $0x64,%al # Wait for not busy 46 | testb $0x2,%al 47 | jnz seta20.2 # 同上,继续判断键盘缓冲区是否有数据,如果有数据,则等待 48 | 49 | movb $0xdf,%al # 0xdf -> port 0x60 50 | outb %al,$0x60 # 将 0xdf 11011111 写入键盘控制器的 P2 口,意味着 A20 打开,进入保护模式 51 | 52 | # Switch from real to protected mode. Use a bootstrap GDT that makes 53 | # virtual addresses map directly to physical addresses so that the 54 | # effective memory map does not change during the transition. 55 | # 控制寄存器 CR0 为 32 位寄存器,和保护模式相关的控制位 56 | # **注意大小端** 57 | # 58 | # |31|--------30~1--------| 0| 59 | # ---------------------------- 60 | # |PG|--------------------|PE| 61 | # PG : 开启分页式 62 | # PE : 开启保护模式 63 | # CR0_PE 在 mmu.h 中定义的 32 位用于控制 CR0 64 | # CR0_PE : 0x00000001 65 | # | 0|-------都是0--------| 1| 66 | # CR0_PE 与 cr0 做或运算保证 PG = 0, PE = 1 67 | # 至此开启了保护模式,基于分段式,没有打开分页式 68 | lgdt gdtdesc # 先把 GDT 告诉 CPU 69 | movl %cr0, %eax # 把 CR0 寄存器的值复制给 eax 寄存器 70 | orl $CR0_PE, %eax # 与 CR0_PE 做或运算,打开保护模式 71 | movl %eax, %cr0 72 | 73 | //PAGEBREAK! 74 | # Complete transition to 32-bit protected mode by using long jmp 75 | # to reload %cs and %eip. The segment descriptors are set up with no 76 | # translation, so that the mapping is still the identity mapping. 77 | # 78 | # 到这里已经进入保护模式了,这里做代码跳转的时候就变成了基于分段式的跳转 79 | # 分段式跳转 ljmp 段选择子, 段内偏移量 80 | # 在 GDT 中我们的代码段下标是 1 ,所以这里段选择子是 1 << 3 = 0000 0000 0000 1000 81 | # 在 16 位的段选择子中前 13 位是 GDT 段表下标,这里前 13 位值是 1 代表选择代码段 82 | # 而我们的代码段是从 0 开始到 4GB 结尾的,所以这里偏移量不用做转换,还是原偏移即可 83 | ljmp $(SEG_KCODE<<3), $start32 84 | 85 | .code32 # Tell assembler to generate 32-bit code now. 86 | start32: 87 | # Set up the protected-mode data segment registers 88 | # 像上面讲 ljmp 时所说的,这时候已经在保护模式下了 89 | # 数据段在 GDT 中的下标是 2,所以这里数据段的段选择子是 2 << 3 = 0000 0000 0001 0000 90 | # 这 16 位的段选择子中的前 13 位是 GDT 段表下标,这里前 13 位的值是 2 代表选择了数据段 91 | # 这里将 3 个数据段寄存器都赋值成数据段段选择子的值 92 | movw $(SEG_KDATA<<3), %ax # Our data segment selector 段选择子赋值给 ax 寄存器 93 | movw %ax, %ds # -> DS: Data Segment 初始化数据段寄存器 94 | movw %ax, %es # -> ES: Extra Segment 初始化扩展段寄存器 95 | movw %ax, %ss # -> SS: Stack Segment 初始化堆栈段寄存器 96 | movw $0, %ax # Zero segments not ready for use ax 寄存器清零 97 | movw %ax, %fs # -> FS 辅助寄存器清零 98 | movw %ax, %gs # -> GS 辅助寄存器清零 99 | 100 | # Set up the stack pointer and call into C. 101 | movl $start, %esp # 栈顶被设置为 0x7C00 处 102 | call bootmain 103 | 104 | # If bootmain returns (it should not), trigger a Bochs 105 | # breakpoint if running under Bochs, then loop. 106 | movw $0x8a00, %ax # 0x8a00 -> port 0x8a00 107 | movw %ax, %dx 108 | outw %ax, %dx 109 | movw $0x8ae0, %ax # 0x8ae0 -> port 0x8a00 110 | outw %ax, %dx 111 | spin: 112 | jmp spin 113 | 114 | # **注意大小端** 115 | # Bootstrap GDT 116 | # 每个 GDT 项 8 字节 117 | # 118 | # |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9|8|7|6|5|4|3|2|1|0| 119 | # |--------------------------------------------------------------------------------------| 120 | # | 基地址 | G|DB|XX|AA| Limit | P| DPL | S| E|ED|RW|A| 基地址 | 121 | # |--------------------------------------------------------------------------------------| 122 | # | 基地址 | Limit | 123 | # |--------------------------------------------------------------------------------------| 124 | # 125 | # 标记位解释: 126 | # P: 0 本段不在内存中 127 | # DPL: 访问该段内存所需权限等级 00 -- 11,0为最大权限级别 128 | # S: 1 代表数据段、代码段或堆栈段,0 代表系统段如中断门或调用门 129 | # E: 1 代表代码段,可执行标记,0 代表数据段 130 | # ED: 0 代表忽略特权级,1 代表遵守特权级 131 | # RW: 如果是数据段(E=0)则1 代表可写入,0 代表只读 132 | # 如果是代码段(E=1)则1 代表可读取,0 代表不可读取 133 | # A: 1 表示该段内存访问过,0 表示没有被访问过 134 | # G: 1 表示 20 位段界限单位是 4KB,最大长度 4GB, 135 | # 0 表示 20 位段界限单位是 1 字节,最大长度 1MB 136 | # DB: 1 表示地址和操作数是 32 位,0 表示地址和操作数是 16 位 137 | # XX: 保留位永远是 0 138 | # AA: 给系统提供的保留位 139 | # 140 | # |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9|8|7|6|5|4|3|2|1|0| 141 | # |--------------------------------------------------------------------------------------| 142 | # | 基地址 | G|DB|XX|AA| Limit | P| DPL | S| E|ED|RW|A| 基地址 | 143 | # |--------------------------------------------------------------------------------------| 144 | # | 基地址 | Limit | 145 | # |--------------------------------------------------------------------------------------| 146 | # 147 | # 一共 3 个 GDT 项 148 | # 第一个 GDT 项目置空不用 149 | # 第二个 GDT 为代码段,可执行,只读(写好的程序指令是不允许更改的) 150 | # 第三个 GDT 为数据段,可读,可写,不可执行 151 | # 采用平坦内存布局,每个分段均从地址 0x0 开始到 0xffffffff 结束,涵盖整个 4GB 内存寻址空间 152 | # SEG_ASM 宏在 asm.h 中,下面翻译一下最后两个 GDT 项目的数据值 153 | # 代码段 GDT 的布局: 154 | # 0xffff,0x0000,0x00,0x9a,0xcf,0x00 155 | # 156 | # |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0| 157 | # |-------------------------------------------------------------------------------------| 158 | # | 0x00 | 1| 1| 0| 0| f | 1|0 0| 1| 1 0 1 0| 0x00 | 159 | # |-------------------------------------------------------------------------------------| 160 | # | 0x0000 | 0xffff | 161 | # |-------------------------------------------------------------------------------------| 162 | # 163 | # 数据段 GDT 的布局: 164 | # 0xffff,0x0000,0x00,0x92,0xcf,0x00 165 | # 166 | # |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0| 167 | # |-------------------------------------------------------------------------------------| 168 | # | 0x00 | 1| 1| 0| 0| f | 1|0 0| 1| 0 0 1 0| 0x00 | 169 | # |-------------------------------------------------------------------------------------| 170 | # | 0x0000 | 0xffff | 171 | # |-------------------------------------------------------------------------------------| 172 | # 173 | .p2align 2 # force 4 byte alignment 174 | gdt: 175 | SEG_NULLASM # null seg 176 | SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff) # code seg 177 | SEG_ASM(STA_W, 0x0, 0xffffffff) # data seg 178 | 179 | gdtdesc: 180 | .word (gdtdesc - gdt - 1) # 16 位的 gdt 大小sizeof(gdt) - 1 181 | .long gdt # 32 位的 gdt 所在物理地址 182 | 183 | -------------------------------------------------------------------------------- /bootmain.c: -------------------------------------------------------------------------------- 1 | // Boot loader. 2 | // 3 | // Part of the boot sector, along with bootasm.S, which calls bootmain(). 4 | // bootasm.S has put the processor into protected 32-bit mode. 5 | // bootmain() loads an ELF kernel image from the disk starting at 6 | // sector 1 and then jumps to the kernel entry routine. 7 | 8 | #include "types.h" 9 | #include "elf.h" 10 | #include "x86.h" 11 | #include "memlayout.h" 12 | 13 | #define SECTSIZE 512 // 硬盘扇区大小 512 字节 14 | 15 | void readseg(uchar*, uint, uint); 16 | 17 | void 18 | bootmain(void) 19 | { 20 | struct elfhdr *elf; 21 | struct proghdr *ph, *eph; 22 | void (*entry)(void); 23 | uchar* pa; 24 | 25 | elf = (struct elfhdr*)0x10000; // 从 0xa0000 到 0xfffff 的物理地址范围属于设备空间,所以内核放置在 0x10000 处开始 26 | 27 | // 从内核所在硬盘位置读取一内存页 4kb 数据 28 | readseg((uchar*)elf, 4096, 0); 29 | 30 | // 判断是否为 ELF 文件格式 31 | if(elf->magic != ELF_MAGIC) 32 | return; // let bootasm.S handle error 33 | 34 | // 加载 ELF 文件中的程序段 (ignores ph flags). 35 | // 找到内核 ELF 文件的程序头表 36 | ph = (struct proghdr*)((uchar*)elf + elf->phoff); 37 | // 内核 ELF 文件程序头表的结束位置 38 | eph = ph + elf->phnum; 39 | // 开始将内核 ELF 文件程序头表载入内存 40 | for(; ph < eph; ph++){ 41 | pa = (uchar*)ph->paddr; 42 | readseg(pa, ph->filesz, ph->off); 43 | // 如果内存大小大于文件大小,用 0 补齐内存空位 44 | if(ph->memsz > ph->filesz) 45 | stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz); 46 | } 47 | 48 | // Call the entry point from the ELF header. 49 | // Does not return! 50 | // 从内核 ELF 文件入口点开始执行内核 51 | entry = (void(*)(void))(elf->entry); 52 | entry(); 53 | } 54 | 55 | void 56 | waitdisk(void) 57 | { 58 | // Wait for disk ready. 59 | // xxxxxxxx & 11000000 != 01000000 60 | // 0x1F7 状态位 61 | // bit 7 = 1 控制器忙 62 | // bit 6 = 1 驱动器就绪 63 | // bit 5 = 1 设备错误 64 | // bit 4 N/A 65 | // bit 3 = 1 扇区缓冲区错误 66 | // bit 2 = 1 磁盘已被读校验 67 | // bit 1 N/A 68 | // bit 0 = 1 上一次命令执行失败 69 | while((inb(0x1F7) & 0xC0) != 0x40) 70 | ; 71 | } 72 | 73 | // Read a single sector at offset into dst. 74 | // 这里使用的是 LBA 磁盘寻址模式 75 | // LBA是非常单纯的一种寻址模式﹔从0开始编号来定位区块, 76 | // 第一区块LBA=0,第二区块LBA=1,依此类推 77 | void 78 | readsect(void *dst, uint offset) // 0x10000, 1 79 | { 80 | // Issue command. 81 | waitdisk(); 82 | outb(0x1F2, 1); // 要读取的扇区数量 count = 1 83 | outb(0x1F3, offset); // 扇区 LBA 地址的 0-7 位 84 | outb(0x1F4, offset >> 8); // 扇区 LBA 地址的 8-15 位 85 | outb(0x1F5, offset >> 16); // 扇区 LBA 地址的 16-23 位 86 | outb(0x1F6, (offset >> 24) | 0xE0); // offset | 11100000 保证高三位恒为 1 87 | // 第7位 恒为1 88 | // 第6位 LBA模式的开关,置1为LBA模式 89 | // 第5位 恒为1 90 | // 第4位 为0代表主硬盘、为1代表从硬盘 91 | // 第3~0位 扇区 LBA 地址的 24-27 位 92 | outb(0x1F7, 0x20); // 20h为读,30h为写 93 | 94 | // Read data. 95 | waitdisk(); 96 | insl(0x1F0, dst, SECTSIZE/4); 97 | } 98 | 99 | // Read 'count' bytes at 'offset' from kernel into physical address 'pa'. 100 | // Might copy more than asked. 101 | void 102 | readseg(uchar* pa, uint count, uint offset) // 0x10000, 4096(0x1000), 0 103 | { 104 | uchar* epa; 105 | 106 | epa = pa + count; // 0x11000 107 | 108 | // 根据扇区大小 512 字节做对齐 109 | pa -= offset % SECTSIZE; 110 | 111 | // bootblock 引导区在第一扇区(下标为 0),内核在第二个扇区(下标为 1) 112 | // 这里做 +1 操作是统一略过引导区 113 | offset = (offset / SECTSIZE) + 1; 114 | 115 | // If this is too slow, we could read lots of sectors at a time. 116 | // We'd write more to memory than asked, but it doesn't matter -- 117 | // we load in increasing order. 118 | // 一次读取一个扇区 512 字节的数据 119 | for(; pa < epa; pa += SECTSIZE, offset++) 120 | readsect(pa, offset); 121 | } 122 | -------------------------------------------------------------------------------- /buf.h: -------------------------------------------------------------------------------- 1 | struct buf { 2 | int flags; 3 | uint dev; 4 | uint sector; 5 | struct buf *prev; // LRU cache list 6 | struct buf *next; 7 | struct buf *qnext; // disk queue 8 | uchar data[512]; 9 | }; 10 | #define B_BUSY 0x1 // buffer is locked by some process 11 | #define B_VALID 0x2 // buffer has been read from disk 12 | #define B_DIRTY 0x4 // buffer needs to be written to disk 13 | 14 | -------------------------------------------------------------------------------- /cat.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | char buf[512]; 6 | 7 | void 8 | cat(int fd) 9 | { 10 | int n; 11 | 12 | while((n = read(fd, buf, sizeof(buf))) > 0) 13 | write(1, buf, n); 14 | if(n < 0){ 15 | printf(1, "cat: read error\n"); 16 | exit(); 17 | } 18 | } 19 | 20 | int 21 | main(int argc, char *argv[]) 22 | { 23 | int fd, i; 24 | 25 | if(argc <= 1){ 26 | cat(0); 27 | exit(); 28 | } 29 | 30 | for(i = 1; i < argc; i++){ 31 | if((fd = open(argv[i], 0)) < 0){ 32 | printf(1, "cat: cannot open %s\n", argv[i]); 33 | exit(); 34 | } 35 | cat(fd); 36 | close(fd); 37 | } 38 | exit(); 39 | } 40 | -------------------------------------------------------------------------------- /console.c: -------------------------------------------------------------------------------- 1 | // Console input and output. 2 | // Input is from the keyboard or serial port. 3 | // Output is written to the screen and serial port. 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "param.h" 8 | #include "traps.h" 9 | #include "spinlock.h" 10 | #include "fs.h" 11 | #include "file.h" 12 | #include "memlayout.h" 13 | #include "mmu.h" 14 | #include "proc.h" 15 | #include "x86.h" 16 | 17 | static void consputc(int); 18 | 19 | static int panicked = 0; 20 | 21 | static struct { 22 | struct spinlock lock; 23 | int locking; 24 | } cons; 25 | 26 | static void 27 | printint(int xx, int base, int sign) 28 | { 29 | static char digits[] = "0123456789abcdef"; 30 | char buf[16]; 31 | int i; 32 | uint x; 33 | 34 | if(sign && (sign = xx < 0)) 35 | x = -xx; 36 | else 37 | x = xx; 38 | 39 | i = 0; 40 | do{ 41 | buf[i++] = digits[x % base]; 42 | }while((x /= base) != 0); 43 | 44 | if(sign) 45 | buf[i++] = '-'; 46 | 47 | while(--i >= 0) 48 | consputc(buf[i]); 49 | } 50 | //PAGEBREAK: 50 51 | 52 | // Print to the console. only understands %d, %x, %p, %s. 53 | void 54 | cprintf(char *fmt, ...) 55 | { 56 | int i, c, locking; 57 | uint *argp; 58 | char *s; 59 | 60 | locking = cons.locking; 61 | if(locking) 62 | acquire(&cons.lock); 63 | 64 | if (fmt == 0) 65 | panic("null fmt"); 66 | 67 | argp = (uint*)(void*)(&fmt + 1); 68 | for(i = 0; (c = fmt[i] & 0xff) != 0; i++){ 69 | if(c != '%'){ 70 | consputc(c); 71 | continue; 72 | } 73 | c = fmt[++i] & 0xff; 74 | if(c == 0) 75 | break; 76 | switch(c){ 77 | case 'd': 78 | printint(*argp++, 10, 1); 79 | break; 80 | case 'x': 81 | case 'p': 82 | printint(*argp++, 16, 0); 83 | break; 84 | case 's': 85 | if((s = (char*)*argp++) == 0) 86 | s = "(null)"; 87 | for(; *s; s++) 88 | consputc(*s); 89 | break; 90 | case '%': 91 | consputc('%'); 92 | break; 93 | default: 94 | // Print unknown % sequence to draw attention. 95 | consputc('%'); 96 | consputc(c); 97 | break; 98 | } 99 | } 100 | 101 | if(locking) 102 | release(&cons.lock); 103 | } 104 | 105 | void 106 | panic(char *s) 107 | { 108 | int i; 109 | uint pcs[10]; 110 | 111 | cli(); 112 | cons.locking = 0; 113 | cprintf("cpu%d: panic: ", cpu->id); 114 | cprintf(s); 115 | cprintf("\n"); 116 | getcallerpcs(&s, pcs); 117 | for(i=0; i<10; i++) 118 | cprintf(" %p", pcs[i]); 119 | panicked = 1; // freeze other CPU 120 | for(;;) 121 | ; 122 | } 123 | 124 | //PAGEBREAK: 50 125 | #define BACKSPACE 0x100 126 | #define CRTPORT 0x3d4 127 | static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory 128 | 129 | static void 130 | cgaputc(int c) 131 | { 132 | int pos; 133 | 134 | // Cursor position: col + 80*row. 135 | outb(CRTPORT, 14); 136 | pos = inb(CRTPORT+1) << 8; 137 | outb(CRTPORT, 15); 138 | pos |= inb(CRTPORT+1); 139 | 140 | if(c == '\n') 141 | pos += 80 - pos%80; 142 | else if(c == BACKSPACE){ 143 | if(pos > 0) --pos; 144 | } else 145 | crt[pos++] = (c&0xff) | 0x0700; // black on white 146 | 147 | if((pos/80) >= 24){ // Scroll up. 148 | memmove(crt, crt+80, sizeof(crt[0])*23*80); 149 | pos -= 80; 150 | memset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos)); 151 | } 152 | 153 | outb(CRTPORT, 14); 154 | outb(CRTPORT+1, pos>>8); 155 | outb(CRTPORT, 15); 156 | outb(CRTPORT+1, pos); 157 | crt[pos] = ' ' | 0x0700; 158 | } 159 | 160 | void 161 | consputc(int c) 162 | { 163 | if(panicked){ 164 | cli(); 165 | for(;;) 166 | ; 167 | } 168 | 169 | if(c == BACKSPACE){ 170 | uartputc('\b'); uartputc(' '); uartputc('\b'); 171 | } else 172 | uartputc(c); 173 | cgaputc(c); 174 | } 175 | 176 | #define INPUT_BUF 128 177 | struct { 178 | struct spinlock lock; 179 | char buf[INPUT_BUF]; 180 | uint r; // Read index 181 | uint w; // Write index 182 | uint e; // Edit index 183 | } input; 184 | 185 | #define C(x) ((x)-'@') // Control-x 186 | 187 | void 188 | consoleintr(int (*getc)(void)) 189 | { 190 | int c; 191 | 192 | acquire(&input.lock); 193 | while((c = getc()) >= 0){ 194 | switch(c){ 195 | case C('P'): // Process listing. 196 | procdump(); 197 | break; 198 | case C('U'): // Kill line. 199 | while(input.e != input.w && 200 | input.buf[(input.e-1) % INPUT_BUF] != '\n'){ 201 | input.e--; 202 | consputc(BACKSPACE); 203 | } 204 | break; 205 | case C('H'): case '\x7f': // Backspace 206 | if(input.e != input.w){ 207 | input.e--; 208 | consputc(BACKSPACE); 209 | } 210 | break; 211 | default: 212 | if(c != 0 && input.e-input.r < INPUT_BUF){ 213 | c = (c == '\r') ? '\n' : c; 214 | input.buf[input.e++ % INPUT_BUF] = c; 215 | consputc(c); 216 | if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ 217 | input.w = input.e; 218 | wakeup(&input.r); 219 | } 220 | } 221 | break; 222 | } 223 | } 224 | release(&input.lock); 225 | } 226 | 227 | int 228 | consoleread(struct inode *ip, char *dst, int n) 229 | { 230 | uint target; 231 | int c; 232 | 233 | iunlock(ip); 234 | target = n; 235 | acquire(&input.lock); 236 | while(n > 0){ 237 | while(input.r == input.w){ 238 | if(proc->killed){ 239 | release(&input.lock); 240 | ilock(ip); 241 | return -1; 242 | } 243 | sleep(&input.r, &input.lock); 244 | } 245 | c = input.buf[input.r++ % INPUT_BUF]; 246 | if(c == C('D')){ // EOF 247 | if(n < target){ 248 | // Save ^D for next time, to make sure 249 | // caller gets a 0-byte result. 250 | input.r--; 251 | } 252 | break; 253 | } 254 | *dst++ = c; 255 | --n; 256 | if(c == '\n') 257 | break; 258 | } 259 | release(&input.lock); 260 | ilock(ip); 261 | 262 | return target - n; 263 | } 264 | 265 | int 266 | consolewrite(struct inode *ip, char *buf, int n) 267 | { 268 | int i; 269 | 270 | iunlock(ip); 271 | acquire(&cons.lock); 272 | for(i = 0; i < n; i++) 273 | consputc(buf[i] & 0xff); 274 | release(&cons.lock); 275 | ilock(ip); 276 | 277 | return n; 278 | } 279 | 280 | void 281 | consoleinit(void) 282 | { 283 | initlock(&cons.lock, "console"); 284 | initlock(&input.lock, "input"); 285 | 286 | devsw[CONSOLE].write = consolewrite; 287 | devsw[CONSOLE].read = consoleread; 288 | cons.locking = 1; 289 | 290 | picenable(IRQ_KBD); 291 | ioapicenable(IRQ_KBD, 0); 292 | } 293 | 294 | -------------------------------------------------------------------------------- /cuth: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | $| = 1; 4 | 5 | sub writefile($@){ 6 | my ($file, @lines) = @_; 7 | 8 | sleep(1); 9 | open(F, ">$file") || die "open >$file: $!"; 10 | print F @lines; 11 | close(F); 12 | } 13 | 14 | # Cut out #include lines that don't contribute anything. 15 | for($i=0; $i<@ARGV; $i++){ 16 | $file = $ARGV[$i]; 17 | if(!open(F, $file)){ 18 | print STDERR "open $file: $!\n"; 19 | next; 20 | } 21 | @lines = ; 22 | close(F); 23 | 24 | $obj = "$file.o"; 25 | $obj =~ s/\.c\.o$/.o/; 26 | system("touch $file"); 27 | 28 | if(system("make CC='gcc -Werror' $obj >/dev/null 2>\&1") != 0){ 29 | print STDERR "make $obj failed: $rv\n"; 30 | next; 31 | } 32 | 33 | system("cp $file =$file"); 34 | for($j=@lines-1; $j>=0; $j--){ 35 | if($lines[$j] =~ /^#include/){ 36 | $old = $lines[$j]; 37 | $lines[$j] = "/* CUT-H */\n"; 38 | writefile($file, @lines); 39 | if(system("make CC='gcc -Werror' $obj >/dev/null 2>\&1") != 0){ 40 | $lines[$j] = $old; 41 | }else{ 42 | print STDERR "$file $old"; 43 | } 44 | } 45 | } 46 | writefile($file, grep {!/CUT-H/} @lines); 47 | system("rm =$file"); 48 | } 49 | -------------------------------------------------------------------------------- /defs.h: -------------------------------------------------------------------------------- 1 | struct buf; 2 | struct context; 3 | struct file; 4 | struct inode; 5 | struct pipe; 6 | struct proc; 7 | struct spinlock; 8 | struct stat; 9 | struct superblock; 10 | 11 | // bio.c 12 | void binit(void); 13 | struct buf* bread(uint, uint); 14 | void brelse(struct buf*); 15 | void bwrite(struct buf*); 16 | 17 | // console.c 18 | void consoleinit(void); 19 | void cprintf(char*, ...); 20 | void consoleintr(int(*)(void)); 21 | void panic(char*) __attribute__((noreturn)); 22 | 23 | // exec.c 24 | int exec(char*, char**); 25 | 26 | // file.c 27 | struct file* filealloc(void); 28 | void fileclose(struct file*); 29 | struct file* filedup(struct file*); 30 | void fileinit(void); 31 | int fileread(struct file*, char*, int n); 32 | int filestat(struct file*, struct stat*); 33 | int filewrite(struct file*, char*, int n); 34 | 35 | // fs.c 36 | void readsb(int dev, struct superblock *sb); 37 | int dirlink(struct inode*, char*, uint); 38 | struct inode* dirlookup(struct inode*, char*, uint*); 39 | struct inode* ialloc(uint, short); 40 | struct inode* idup(struct inode*); 41 | void iinit(void); 42 | void ilock(struct inode*); 43 | void iput(struct inode*); 44 | void iunlock(struct inode*); 45 | void iunlockput(struct inode*); 46 | void iupdate(struct inode*); 47 | int namecmp(const char*, const char*); 48 | struct inode* namei(char*); 49 | struct inode* nameiparent(char*, char*); 50 | int readi(struct inode*, char*, uint, uint); 51 | void stati(struct inode*, struct stat*); 52 | int writei(struct inode*, char*, uint, uint); 53 | 54 | // ide.c 55 | void ideinit(void); 56 | void ideintr(void); 57 | void iderw(struct buf*); 58 | 59 | // ioapic.c 60 | void ioapicenable(int irq, int cpu); 61 | extern uchar ioapicid; 62 | void ioapicinit(void); 63 | 64 | // kalloc.c 65 | char* kalloc(void); 66 | void kfree(char*); 67 | void kinit1(void*, void*); 68 | void kinit2(void*, void*); 69 | 70 | // kbd.c 71 | void kbdintr(void); 72 | 73 | // lapic.c 74 | int cpunum(void); 75 | extern volatile uint* lapic; 76 | void lapiceoi(void); 77 | void lapicinit(void); 78 | void lapicstartap(uchar, uint); 79 | void microdelay(int); 80 | 81 | // log.c 82 | void initlog(void); 83 | void log_write(struct buf*); 84 | void begin_trans(); 85 | void commit_trans(); 86 | 87 | // mp.c 88 | extern int ismp; 89 | int mpbcpu(void); 90 | void mpinit(void); 91 | void mpstartthem(void); 92 | 93 | // picirq.c 94 | void picenable(int); 95 | void picinit(void); 96 | 97 | // pipe.c 98 | int pipealloc(struct file**, struct file**); 99 | void pipeclose(struct pipe*, int); 100 | int piperead(struct pipe*, char*, int); 101 | int pipewrite(struct pipe*, char*, int); 102 | 103 | //PAGEBREAK: 16 104 | // proc.c 105 | struct proc* copyproc(struct proc*); 106 | void exit(void); 107 | int fork(void); 108 | int growproc(int); 109 | int kill(int); 110 | void pinit(void); 111 | void procdump(void); 112 | void scheduler(void) __attribute__((noreturn)); 113 | void sched(void); 114 | void sleep(void*, struct spinlock*); 115 | void userinit(void); 116 | int wait(void); 117 | void wakeup(void*); 118 | void yield(void); 119 | 120 | // swtch.S 121 | void swtch(struct context**, struct context*); 122 | 123 | // spinlock.c 124 | void acquire(struct spinlock*); 125 | void getcallerpcs(void*, uint*); 126 | int holding(struct spinlock*); 127 | void initlock(struct spinlock*, char*); 128 | void release(struct spinlock*); 129 | void pushcli(void); 130 | void popcli(void); 131 | 132 | // string.c 133 | int memcmp(const void*, const void*, uint); 134 | void* memmove(void*, const void*, uint); 135 | void* memset(void*, int, uint); 136 | char* safestrcpy(char*, const char*, int); 137 | int strlen(const char*); 138 | int strncmp(const char*, const char*, uint); 139 | char* strncpy(char*, const char*, int); 140 | 141 | // syscall.c 142 | int argint(int, int*); 143 | int argptr(int, char**, int); 144 | int argstr(int, char**); 145 | int fetchint(uint, int*); 146 | int fetchstr(uint, char**); 147 | void syscall(void); 148 | 149 | // timer.c 150 | void timerinit(void); 151 | 152 | // trap.c 153 | void idtinit(void); 154 | extern uint ticks; 155 | void tvinit(void); 156 | extern struct spinlock tickslock; 157 | 158 | // uart.c 159 | void uartinit(void); 160 | void uartintr(void); 161 | void uartputc(int); 162 | 163 | // vm.c 164 | void seginit(void); 165 | void kvmalloc(void); 166 | void vmenable(void); 167 | pde_t* setupkvm(void); 168 | char* uva2ka(pde_t*, char*); 169 | int allocuvm(pde_t*, uint, uint); 170 | int deallocuvm(pde_t*, uint, uint); 171 | void freevm(pde_t*); 172 | void inituvm(pde_t*, char*, uint); 173 | int loaduvm(pde_t*, char*, struct inode*, uint, uint); 174 | pde_t* copyuvm(pde_t*, uint); 175 | void switchuvm(struct proc*); 176 | void switchkvm(void); 177 | int copyout(pde_t*, uint, void*, uint); 178 | void clearpteu(pde_t *pgdir, char *uva); 179 | 180 | // number of elements in fixed-size array 181 | #define NELEM(x) (sizeof(x)/sizeof((x)[0])) 182 | -------------------------------------------------------------------------------- /echo.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | int i; 9 | 10 | for(i = 1; i < argc; i++) 11 | printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n"); 12 | exit(); 13 | } 14 | -------------------------------------------------------------------------------- /elf.h: -------------------------------------------------------------------------------- 1 | // Format of an ELF executable file 2 | 3 | #define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian 4 | 5 | // File header 6 | struct elfhdr { 7 | uint magic; // 4 字节,始终为 0x7felf 表名该文件是个 ELF 格式文件 8 | 9 | uchar elf[12]; // 12 字节,每字节对应意义如下: 10 | // 0 : 1 = 32 位程序;2 = 64 位程序 11 | // 1 : 数据编码方式,0 = 无效;1 = 小端模式;2 = 大端模式 12 | // 2 : 只是版本,固定为 0x1 13 | // 3 : 目标操作系统架构 14 | // 4 : 目标操作系统版本 15 | // 5 ~ 11 : 固定为 0 16 | 17 | ushort type; // 2 字节,表明该文件类型,意义如下: 18 | // 0x0 : 未知目标文件格式 19 | // 0x1 : 可重定位文件 20 | // 0x2 : 可执行文件 21 | // 0x3 : 共享目标文件 22 | // 0x4 : 转储文件 23 | // 0xff00 : 特定处理器文件 24 | // 0xffff : 特定处理器文件 25 | 26 | ushort machine; // 2 字节,表明运行该程序需要的计算机体系架构,这里我们只需要知道 0x0 为未指定;0x3 为 x86 架构 27 | 28 | uint version; // 4 字节,表示该文件的版本号 29 | 30 | uint entry; // 4 字节,该文件的入口地址,没有入口(非可执行文件)则为 0 31 | 32 | uint phoff; // 4 字节,表示该文件的“程序头部表”相对于文件的位置,单位是字节 33 | 34 | uint shoff; // 4 字节,表示该文件的“节区头部表”相对于文件的位置,单位是字节 35 | 36 | uint flags; // 4 字节,特定处理器标志 37 | 38 | ushort ehsize; // 2 字节,ELF文件头部的大小,单位是字节 39 | 40 | ushort phentsize; // 2 字节,表示程序头部表中一个入口的大小,单位是字节 41 | 42 | ushort phnum; // 2 字节,表示程序头部表的入口个数,phnum * phentsize = 程序头部表大小(单位是字节) 43 | 44 | ushort shentsize; // 2 字节,节区头部表入口大小,单位是字节 45 | 46 | ushort shnum; // 2 字节,节区头部表入口个数,shnum * shentsize = 节区头部表大小(单位是字节) 47 | 48 | ushort shstrndx; // 2 字节,表示字符表相关入口的节区头部表索引 49 | }; 50 | 51 | // Program section header 52 | struct proghdr { 53 | uint type; // 4 字节, 段类型 54 | // 1 PT_LOAD : 可载入的段 55 | // 2 PT_DYNAMIC : 动态链接信息 56 | // 3 PT_INTERP : 指定要作为解释程序调用的以空字符结尾的路径名的位置和大小 57 | // 4 PT_NOTE : 指定辅助信息的位置和大小 58 | // 5 PT_SHLIB : 保留类型,但具有未指定的语义 59 | // 6 PT_PHDR : 指定程序头表在文件及程序内存映像中的位置和大小 60 | // 7 PT_TLS : 指定线程局部存储模板 61 | uint off; // 4 字节, 段的第一个字节在文件中的偏移 62 | uint vaddr; // 4 字节, 段的第一个字节在内存中的虚拟地址 63 | uint paddr; // 4 字节, 段的第一个字节在内存中的物理地址(适用于物理内存定位型的系统) 64 | uint filesz; // 4 字节, 段在文件中的长度 65 | uint memsz; // 4 字节, 段在内存中的长度 66 | uint flags; // 4 字节, 段标志 67 | // 1 : 可执行 68 | // 2 : 可写入 69 | // 4 : 可读取 70 | uint align; // 4 字节, 段在文件及内存中如何对齐 71 | }; 72 | 73 | // Values for Proghdr type 74 | #define ELF_PROG_LOAD 1 75 | 76 | // Flag bits for Proghdr flags 77 | #define ELF_PROG_FLAG_EXEC 1 78 | #define ELF_PROG_FLAG_WRITE 2 79 | #define ELF_PROG_FLAG_READ 4 80 | -------------------------------------------------------------------------------- /entry.S: -------------------------------------------------------------------------------- 1 | # Multiboot header, for multiboot boot loaders like GNU Grub. 2 | # http://www.gnu.org/software/grub/manual/multiboot/multiboot.html 3 | # 4 | # Using GRUB 2, you can boot xv6 from a file stored in a 5 | # Linux file system by copying kernel or kernelmemfs to /boot 6 | # and then adding this menu entry: 7 | # 8 | # menuentry "xv6" { 9 | # insmod ext2 10 | # set root='(hd0,msdos1)' 11 | # set kernel='/boot/kernel' 12 | # echo "Loading ${kernel}..." 13 | # multiboot ${kernel} ${kernel} 14 | # boot 15 | # } 16 | 17 | #include "asm.h" 18 | #include "memlayout.h" 19 | #include "mmu.h" 20 | #include "param.h" 21 | 22 | # Multiboot header. Data to direct multiboot loader. 23 | .p2align 2 24 | .text 25 | .globl multiboot_header 26 | multiboot_header: 27 | #define magic 0x1badb002 28 | #define flags 0 29 | .long magic 30 | .long flags 31 | .long (-magic-flags) 32 | 33 | # By convention, the _start symbol specifies the ELF entry point. 34 | # Since we haven't set up virtual memory yet, our entry point is 35 | # the physical address of 'entry'. 36 | .globl _start 37 | _start = V2P_WO(entry) 38 | 39 | # Entering xv6 on boot processor, with paging off. 40 | .globl entry 41 | entry: 42 | # Turn on page size extension for 4Mbyte pages 43 | movl %cr4, %eax 44 | orl $(CR4_PSE), %eax 45 | movl %eax, %cr4 46 | # Set page directory 47 | movl $(V2P_WO(entrypgdir)), %eax 48 | movl %eax, %cr3 49 | # Turn on paging. 50 | movl %cr0, %eax 51 | orl $(CR0_PG|CR0_WP), %eax 52 | movl %eax, %cr0 53 | 54 | # Set up the stack pointer. 55 | movl $(stack + KSTACKSIZE), %esp 56 | 57 | # Jump to main(), and switch to executing at 58 | # high addresses. The indirect call is needed because 59 | # the assembler produces a PC-relative instruction 60 | # for a direct jump. 61 | mov $main, %eax 62 | jmp *%eax 63 | 64 | .comm stack, KSTACKSIZE 65 | -------------------------------------------------------------------------------- /entryother.S: -------------------------------------------------------------------------------- 1 | #include "asm.h" 2 | #include "memlayout.h" 3 | #include "mmu.h" 4 | 5 | # Each non-boot CPU ("AP") is started up in response to a STARTUP 6 | # IPI from the boot CPU. Section B.4.2 of the Multi-Processor 7 | # Specification says that the AP will start in real mode with CS:IP 8 | # set to XY00:0000, where XY is an 8-bit value sent with the 9 | # STARTUP. Thus this code must start at a 4096-byte boundary. 10 | # 11 | # Because this code sets DS to zero, it must sit 12 | # at an address in the low 2^16 bytes. 13 | # 14 | # Startothers (in main.c) sends the STARTUPs one at a time. 15 | # It copies this code (start) at 0x7000. It puts the address of 16 | # a newly allocated per-core stack in start-4,the address of the 17 | # place to jump to (mpenter) in start-8, and the physical address 18 | # of entrypgdir in start-12. 19 | # 20 | # This code is identical to bootasm.S except: 21 | # - it does not need to enable A20 22 | # - it uses the address at start-4, start-8, and start-12 23 | 24 | .code16 25 | .globl start 26 | start: 27 | cli 28 | 29 | xorw %ax,%ax 30 | movw %ax,%ds 31 | movw %ax,%es 32 | movw %ax,%ss 33 | 34 | lgdt gdtdesc 35 | movl %cr0, %eax 36 | orl $CR0_PE, %eax 37 | movl %eax, %cr0 38 | 39 | //PAGEBREAK! 40 | ljmpl $(SEG_KCODE<<3), $(start32) 41 | 42 | .code32 43 | start32: 44 | movw $(SEG_KDATA<<3), %ax 45 | movw %ax, %ds 46 | movw %ax, %es 47 | movw %ax, %ss 48 | movw $0, %ax 49 | movw %ax, %fs 50 | movw %ax, %gs 51 | 52 | # Turn on page size extension for 4Mbyte pages 53 | movl %cr4, %eax 54 | orl $(CR4_PSE), %eax 55 | movl %eax, %cr4 56 | # Use enterpgdir as our initial page table 57 | movl (start-12), %eax 58 | movl %eax, %cr3 59 | # Turn on paging. 60 | movl %cr0, %eax 61 | orl $(CR0_PE|CR0_PG|CR0_WP), %eax 62 | movl %eax, %cr0 63 | 64 | # Switch to the stack allocated by startothers() 65 | movl (start-4), %esp 66 | # Call mpenter() 67 | call *(start-8) 68 | 69 | movw $0x8a00, %ax 70 | movw %ax, %dx 71 | outw %ax, %dx 72 | movw $0x8ae0, %ax 73 | outw %ax, %dx 74 | spin: 75 | jmp spin 76 | 77 | .p2align 2 78 | gdt: 79 | SEG_NULLASM 80 | SEG_ASM(STA_X|STA_R, 0, 0xffffffff) 81 | SEG_ASM(STA_W, 0, 0xffffffff) 82 | 83 | 84 | gdtdesc: 85 | .word (gdtdesc - gdt - 1) 86 | .long gdt 87 | 88 | -------------------------------------------------------------------------------- /exec.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "param.h" 3 | #include "memlayout.h" 4 | #include "mmu.h" 5 | #include "proc.h" 6 | #include "defs.h" 7 | #include "x86.h" 8 | #include "elf.h" 9 | 10 | int 11 | exec(char *path, char **argv) 12 | { 13 | char *s, *last; 14 | int i, off; 15 | uint argc, sz, sp, ustack[3+MAXARG+1]; 16 | struct elfhdr elf; 17 | struct inode *ip; 18 | struct proghdr ph; 19 | pde_t *pgdir, *oldpgdir; 20 | 21 | if((ip = namei(path)) == 0) 22 | return -1; 23 | ilock(ip); 24 | pgdir = 0; 25 | 26 | // Check ELF header 27 | if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf)) 28 | goto bad; 29 | if(elf.magic != ELF_MAGIC) 30 | goto bad; 31 | 32 | if((pgdir = setupkvm()) == 0) 33 | goto bad; 34 | 35 | // Load program into memory. 36 | sz = 0; 37 | for(i=0, off=elf.phoff; i= MAXARG) 63 | goto bad; 64 | sp = (sp - (strlen(argv[argc]) + 1)) & ~3; 65 | if(copyout(pgdir, sp, argv[argc], strlen(argv[argc]) + 1) < 0) 66 | goto bad; 67 | ustack[3+argc] = sp; 68 | } 69 | ustack[3+argc] = 0; 70 | 71 | ustack[0] = 0xffffffff; // fake return PC 72 | ustack[1] = argc; 73 | ustack[2] = sp - (argc+1)*4; // argv pointer 74 | 75 | sp -= (3+argc+1) * 4; 76 | if(copyout(pgdir, sp, ustack, (3+argc+1)*4) < 0) 77 | goto bad; 78 | 79 | // Save program name for debugging. 80 | for(last=s=path; *s; s++) 81 | if(*s == '/') 82 | last = s+1; 83 | safestrcpy(proc->name, last, sizeof(proc->name)); 84 | 85 | // Commit to the user image. 86 | oldpgdir = proc->pgdir; 87 | proc->pgdir = pgdir; 88 | proc->sz = sz; 89 | proc->tf->eip = elf.entry; // main 90 | proc->tf->esp = sp; 91 | switchuvm(proc); 92 | freevm(oldpgdir); 93 | return 0; 94 | 95 | bad: 96 | if(pgdir) 97 | freevm(pgdir); 98 | if(ip) 99 | iunlockput(ip); 100 | return -1; 101 | } 102 | -------------------------------------------------------------------------------- /fcntl.h: -------------------------------------------------------------------------------- 1 | #define O_RDONLY 0x000 2 | #define O_WRONLY 0x001 3 | #define O_RDWR 0x002 4 | #define O_CREATE 0x200 5 | -------------------------------------------------------------------------------- /file.c: -------------------------------------------------------------------------------- 1 | // 2 | // File descriptors 3 | // 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "param.h" 8 | #include "fs.h" 9 | #include "file.h" 10 | #include "spinlock.h" 11 | 12 | struct devsw devsw[NDEV]; 13 | struct { 14 | struct spinlock lock; 15 | struct file file[NFILE]; 16 | } ftable; 17 | 18 | void 19 | fileinit(void) 20 | { 21 | initlock(&ftable.lock, "ftable"); 22 | } 23 | 24 | // Allocate a file structure. 25 | struct file* 26 | filealloc(void) 27 | { 28 | struct file *f; 29 | 30 | acquire(&ftable.lock); 31 | for(f = ftable.file; f < ftable.file + NFILE; f++){ 32 | if(f->ref == 0){ 33 | f->ref = 1; 34 | release(&ftable.lock); 35 | return f; 36 | } 37 | } 38 | release(&ftable.lock); 39 | return 0; 40 | } 41 | 42 | // Increment ref count for file f. 43 | struct file* 44 | filedup(struct file *f) 45 | { 46 | acquire(&ftable.lock); 47 | if(f->ref < 1) 48 | panic("filedup"); 49 | f->ref++; 50 | release(&ftable.lock); 51 | return f; 52 | } 53 | 54 | // Close file f. (Decrement ref count, close when reaches 0.) 55 | void 56 | fileclose(struct file *f) 57 | { 58 | struct file ff; 59 | 60 | acquire(&ftable.lock); 61 | if(f->ref < 1) 62 | panic("fileclose"); 63 | if(--f->ref > 0){ 64 | release(&ftable.lock); 65 | return; 66 | } 67 | ff = *f; 68 | f->ref = 0; 69 | f->type = FD_NONE; 70 | release(&ftable.lock); 71 | 72 | if(ff.type == FD_PIPE) 73 | pipeclose(ff.pipe, ff.writable); 74 | else if(ff.type == FD_INODE){ 75 | begin_trans(); 76 | iput(ff.ip); 77 | commit_trans(); 78 | } 79 | } 80 | 81 | // Get metadata about file f. 82 | int 83 | filestat(struct file *f, struct stat *st) 84 | { 85 | if(f->type == FD_INODE){ 86 | ilock(f->ip); 87 | stati(f->ip, st); 88 | iunlock(f->ip); 89 | return 0; 90 | } 91 | return -1; 92 | } 93 | 94 | // Read from file f. 95 | int 96 | fileread(struct file *f, char *addr, int n) 97 | { 98 | int r; 99 | 100 | if(f->readable == 0) 101 | return -1; 102 | if(f->type == FD_PIPE) 103 | return piperead(f->pipe, addr, n); 104 | if(f->type == FD_INODE){ 105 | ilock(f->ip); 106 | if((r = readi(f->ip, addr, f->off, n)) > 0) 107 | f->off += r; 108 | iunlock(f->ip); 109 | return r; 110 | } 111 | panic("fileread"); 112 | } 113 | 114 | //PAGEBREAK! 115 | // Write to file f. 116 | int 117 | filewrite(struct file *f, char *addr, int n) 118 | { 119 | int r; 120 | 121 | if(f->writable == 0) 122 | return -1; 123 | if(f->type == FD_PIPE) 124 | return pipewrite(f->pipe, addr, n); 125 | if(f->type == FD_INODE){ 126 | // write a few blocks at a time to avoid exceeding 127 | // the maximum log transaction size, including 128 | // i-node, indirect block, allocation blocks, 129 | // and 2 blocks of slop for non-aligned writes. 130 | // this really belongs lower down, since writei() 131 | // might be writing a device like the console. 132 | int max = ((LOGSIZE-1-1-2) / 2) * 512; 133 | int i = 0; 134 | while(i < n){ 135 | int n1 = n - i; 136 | if(n1 > max) 137 | n1 = max; 138 | 139 | begin_trans(); 140 | ilock(f->ip); 141 | if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) 142 | f->off += r; 143 | iunlock(f->ip); 144 | commit_trans(); 145 | 146 | if(r < 0) 147 | break; 148 | if(r != n1) 149 | panic("short filewrite"); 150 | i += r; 151 | } 152 | return i == n ? n : -1; 153 | } 154 | panic("filewrite"); 155 | } 156 | 157 | -------------------------------------------------------------------------------- /file.h: -------------------------------------------------------------------------------- 1 | struct file { 2 | enum { FD_NONE, FD_PIPE, FD_INODE } type; 3 | int ref; // reference count 4 | char readable; 5 | char writable; 6 | struct pipe *pipe; 7 | struct inode *ip; 8 | uint off; 9 | }; 10 | 11 | 12 | // in-memory copy of an inode 13 | struct inode { 14 | uint dev; // Device number 15 | uint inum; // Inode number 16 | int ref; // Reference count 17 | int flags; // I_BUSY, I_VALID 18 | 19 | short type; // copy of disk inode 20 | short major; 21 | short minor; 22 | short nlink; 23 | uint size; 24 | uint addrs[NDIRECT+1]; 25 | }; 26 | #define I_BUSY 0x1 27 | #define I_VALID 0x2 28 | 29 | // table mapping major device number to 30 | // device functions 31 | struct devsw { 32 | int (*read)(struct inode*, char*, int); 33 | int (*write)(struct inode*, char*, int); 34 | }; 35 | 36 | extern struct devsw devsw[]; 37 | 38 | #define CONSOLE 1 39 | -------------------------------------------------------------------------------- /forktest.c: -------------------------------------------------------------------------------- 1 | // Test that fork fails gracefully. 2 | // Tiny executable so that the limit can be filling the proc table. 3 | 4 | #include "types.h" 5 | #include "stat.h" 6 | #include "user.h" 7 | 8 | #define N 1000 9 | 10 | void 11 | printf(int fd, char *s, ...) 12 | { 13 | write(fd, s, strlen(s)); 14 | } 15 | 16 | void 17 | forktest(void) 18 | { 19 | int n, pid; 20 | 21 | printf(1, "fork test\n"); 22 | 23 | for(n=0; n 0; n--){ 37 | if(wait() < 0){ 38 | printf(1, "wait stopped early\n"); 39 | exit(); 40 | } 41 | } 42 | 43 | if(wait() != -1){ 44 | printf(1, "wait got too many\n"); 45 | exit(); 46 | } 47 | 48 | printf(1, "fork test OK\n"); 49 | } 50 | 51 | int 52 | main(void) 53 | { 54 | forktest(); 55 | exit(); 56 | } 57 | -------------------------------------------------------------------------------- /fs.h: -------------------------------------------------------------------------------- 1 | // On-disk file system format. 2 | // Both the kernel and user programs use this header file. 3 | 4 | // Block 0 is unused. 5 | // Block 1 is super block. 6 | // Blocks 2 through sb.ninodes/IPB hold inodes. 7 | // Then free bitmap blocks holding sb.size bits. 8 | // Then sb.nblocks data blocks. 9 | // Then sb.nlog log blocks. 10 | 11 | #define ROOTINO 1 // root i-number 12 | #define BSIZE 512 // block size 13 | 14 | // File system super block 15 | struct superblock { 16 | uint size; // Size of file system image (blocks) 17 | uint nblocks; // Number of data blocks 18 | uint ninodes; // Number of inodes. 19 | uint nlog; // Number of log blocks 20 | }; 21 | 22 | #define NDIRECT 12 23 | #define NINDIRECT (BSIZE / sizeof(uint)) 24 | #define MAXFILE (NDIRECT + NINDIRECT) 25 | 26 | // On-disk inode structure 27 | struct dinode { 28 | short type; // File type 29 | short major; // Major device number (T_DEV only) 30 | short minor; // Minor device number (T_DEV only) 31 | short nlink; // Number of links to inode in file system 32 | uint size; // Size of file (bytes) 33 | uint addrs[NDIRECT+1]; // Data block addresses 34 | }; 35 | 36 | // Inodes per block. 37 | #define IPB (BSIZE / sizeof(struct dinode)) 38 | 39 | // Block containing inode i 40 | #define IBLOCK(i) ((i) / IPB + 2) 41 | 42 | // Bitmap bits per block 43 | #define BPB (BSIZE*8) 44 | 45 | // Block containing bit for block b 46 | #define BBLOCK(b, ninodes) (b/BPB + (ninodes)/IPB + 3) 47 | 48 | // Directory is a file containing a sequence of dirent structures. 49 | #define DIRSIZ 14 50 | 51 | struct dirent { 52 | ushort inum; 53 | char name[DIRSIZ]; 54 | }; 55 | 56 | -------------------------------------------------------------------------------- /gdbutil: -------------------------------------------------------------------------------- 1 | # -*- gdb-script -*- 2 | 3 | # Utility functions to pretty-print x86 segment/interrupt descriptors. 4 | # To load this file, run "source gdbutil" in gdb. 5 | # printdesc and printdescs are the main entry points. 6 | 7 | # IA32 2007, Volume 3A, Table 3-2 8 | set $STS_T16A = 0x1 9 | set $STS_LDT = 0x2 10 | set $STS_T16B = 0x3 11 | set $STS_CG16 = 0x4 12 | set $STS_TG = 0x5 13 | set $STS_IG16 = 0x6 14 | set $STS_TG16 = 0x7 15 | set $STS_T32A = 0x9 16 | set $STS_T32B = 0xB 17 | set $STS_CG32 = 0xC 18 | set $STS_IG32 = 0xE 19 | set $STS_TG32 = 0xF 20 | 21 | define outputsts 22 | while 1 23 | if $arg0 == $STS_T16A 24 | echo STS_T16A 25 | loop_break 26 | end 27 | if $arg0 == $STS_LDT 28 | echo STS_LDT\ 29 | loop_break 30 | end 31 | if $arg0 == $STS_T16B 32 | echo STS_T16B 33 | loop_break 34 | end 35 | if $arg0 == $STS_CG16 36 | echo STS_CG16 37 | loop_break 38 | end 39 | if $arg0 == $STS_TG 40 | echo STS_TG\ \ 41 | loop_break 42 | end 43 | if $arg0 == $STS_IG16 44 | echo STS_IG16 45 | loop_break 46 | end 47 | if $arg0 == $STS_TG16 48 | echo STS_TG16 49 | loop_break 50 | end 51 | if $arg0 == $STS_T32A 52 | echo STS_T32A 53 | loop_break 54 | end 55 | if $arg0 == $STS_T32B 56 | echo STS_T32B 57 | loop_break 58 | end 59 | if $arg0 == $STS_CG32 60 | echo STS_CG32 61 | loop_break 62 | end 63 | if $arg0 == $STS_IG32 64 | echo STS_IG32 65 | loop_break 66 | end 67 | if $arg0 == $STS_TG32 68 | echo STS_TG32 69 | loop_break 70 | end 71 | echo Reserved 72 | loop_break 73 | end 74 | end 75 | 76 | # IA32 2007, Volume 3A, Table 3-1 77 | set $STA_X = 0x8 78 | set $STA_E = 0x4 79 | set $STA_C = 0x4 80 | set $STA_W = 0x2 81 | set $STA_R = 0x2 82 | set $STA_A = 0x1 83 | 84 | define outputsta 85 | if $arg0 & $STA_X 86 | # Code segment 87 | echo code 88 | if $arg0 & $STA_C 89 | echo |STA_C 90 | end 91 | if $arg0 & $STA_R 92 | echo |STA_R 93 | end 94 | else 95 | # Data segment 96 | echo data 97 | if $arg0 & $STA_E 98 | echo |STA_E 99 | end 100 | if $arg0 & $STA_W 101 | echo |STA_W 102 | end 103 | end 104 | if $arg0 & $STA_A 105 | echo |STA_A 106 | else 107 | printf " " 108 | end 109 | end 110 | 111 | # xv6-specific 112 | set $SEG_KCODE = 1 113 | set $SEG_KDATA = 2 114 | set $SEG_KCPU = 3 115 | set $SEG_UCODE = 4 116 | set $SEG_UDATA = 5 117 | set $SEG_TSS = 6 118 | 119 | define outputcs 120 | if ($arg0 & 4) == 0 121 | if $arg0 >> 3 == $SEG_KCODE 122 | printf "SEG_KCODE<<3" 123 | end 124 | if $arg0 >> 3 == $SEG_KDATA 125 | printf "SEG_KDATA<<3" 126 | end 127 | if $arg0 >> 3 == $SEG_KCPU 128 | printf "SEG_KCPU<<3" 129 | end 130 | if $arg0 >> 3 == $SEG_UCODE 131 | printf "SEG_UCODE<<3" 132 | end 133 | if $arg0 >> 3 == $SEG_UDATA 134 | printf "SEG_UDATA<<3" 135 | end 136 | if $arg0 >> 3 == $SEG_TSS 137 | printf "SEG_TSS<<3" 138 | end 139 | if ($arg0 >> 3 < 1) + ($arg0 >> 3 > 6) 140 | printf "GDT[%d]", $arg0 >> 3 141 | end 142 | else 143 | printf "LDT[%d]", $arg0 >> 3 144 | end 145 | if ($arg0 & 3) > 0 146 | printf "|" 147 | outputdpl ($arg0&3) 148 | end 149 | end 150 | 151 | define outputdpl 152 | if $arg0 == 0 153 | printf "DPL_KERN" 154 | else 155 | if $arg0 == 3 156 | printf "DPL_USER" 157 | else 158 | printf "DPL%d", $arg0 159 | end 160 | end 161 | end 162 | 163 | define printdesc 164 | if $argc != 1 165 | echo Usage: printdesc expr 166 | else 167 | _printdesc ((uint*)&($arg0))[0] ((uint*)&($arg0))[1] 168 | printf "\n" 169 | end 170 | end 171 | 172 | document printdesc 173 | Print an x86 segment or gate descriptor. 174 | printdesc EXPR 175 | EXPR must evaluate to a descriptor value. It can be of any C type. 176 | end 177 | 178 | define _printdesc 179 | _printdesc1 $arg0 $arg1 ($arg1>>15&1) ($arg1>>13&3) ($arg1>>12&1) ($arg1>>8&15) 180 | end 181 | 182 | define _printdesc1 183 | # 2:P 3:DPL 4:S 5:Type 184 | if $arg2 == 0 185 | printf "P = 0 (Not present)" 186 | else 187 | printf "type = " 188 | if $arg4 == 0 189 | # System segment 190 | outputsts $arg5 191 | printf " (0x%x) ", $arg5 192 | _printsysdesc $arg0 $arg1 $arg5 193 | else 194 | # Code/data segment 195 | outputsta $arg5 196 | printf " " 197 | _printsegdesc $arg0 $arg1 198 | end 199 | 200 | printf " DPL = " 201 | outputdpl $arg3 202 | printf " (%d)", $arg3 203 | end 204 | end 205 | 206 | define _printsysdesc 207 | # 2:Type 208 | # GDB's || is buggy 209 | if ($arg2 == $STS_TG) + (($arg2&7) == $STS_IG16) + (($arg2&7) == $STS_TG16) 210 | # Gate descriptor 211 | _printgate $arg2 ($arg0>>16) ($arg0&0xFFFF) ($arg1>>16) 212 | else 213 | # System segment descriptor 214 | _printsegdesc $arg0 $arg1 215 | end 216 | end 217 | 218 | define _printgate 219 | # IA32 2007, Voume 3A, Figure 5-2 220 | # 0:Type 1:CS 2:Offset 15..0 3:Offset 31..16 221 | printf "CS = " 222 | outputcs $arg1 223 | printf " (%d)", $arg1 224 | 225 | if (($arg0&7) == $STS_IG16) + (($arg0&7) == $STS_TG16) 226 | printf " Offset = " 227 | output/a $arg3 << 16 | $arg2 228 | end 229 | end 230 | 231 | define _printsegdesc 232 | # IA32 20007, Volume 3A, Figure 3-8 and Figure 4-1 233 | _printsegdesc1 ($arg0>>16) ($arg1&0xFF) ($arg1>>24) ($arg0&0xFFFF) ($arg1>>16&15) ($arg1>>23&1) 234 | if ($arg1>>12&1) == 1 235 | printf " AVL = %d", $arg1>>20&1 236 | if ($arg1>>11&1) == 0 237 | # Data segment 238 | if ($arg1>>22&1) == 0 239 | printf " B = small (0) " 240 | else 241 | printf " B = big (1) " 242 | end 243 | else 244 | # Code segment 245 | printf " D = " 246 | if ($arg1>>22&1) == 0 247 | printf "16-bit (0)" 248 | else 249 | printf "32-bit (1)" 250 | end 251 | end 252 | end 253 | end 254 | 255 | define _printsegdesc1 256 | # 0:Base 0..15 1:Base 16..23 2:Base 24..32 3:Limit 0..15 4:Limit 16..19 5:G 257 | printf "base = 0x%08x", $arg0 | ($arg1<<16) | ($arg2<<24) 258 | printf " limit = 0x" 259 | if $arg5 == 0 260 | printf "%08x", $arg3 | ($arg4<<16) 261 | else 262 | printf "%08x", (($arg3 | ($arg4<<16)) << 12) | 0xFFF 263 | end 264 | end 265 | 266 | define printdescs 267 | if $argc < 1 || $argc > 2 268 | echo Usage: printdescs expr [count] 269 | else 270 | if $argc == 1 271 | _printdescs ($arg0) (sizeof($arg0)/sizeof(($arg0)[0])) 272 | else 273 | _printdescs ($arg0) ($arg1) 274 | end 275 | end 276 | end 277 | 278 | document printdescs 279 | Print an array of x86 segment or gate descriptors. 280 | printdescs EXPR [COUNT] 281 | EXPR must evaluate to an array of descriptors. 282 | end 283 | 284 | define _printdescs 285 | set $i = 0 286 | while $i < $arg1 287 | printf "[%d] ", $i 288 | printdesc $arg0[$i] 289 | set $i = $i + 1 290 | end 291 | end 292 | -------------------------------------------------------------------------------- /grep.c: -------------------------------------------------------------------------------- 1 | // Simple grep. Only supports ^ . * $ operators. 2 | 3 | #include "types.h" 4 | #include "stat.h" 5 | #include "user.h" 6 | 7 | char buf[1024]; 8 | int match(char*, char*); 9 | 10 | void 11 | grep(char *pattern, int fd) 12 | { 13 | int n, m; 14 | char *p, *q; 15 | 16 | m = 0; 17 | while((n = read(fd, buf+m, sizeof(buf)-m)) > 0){ 18 | m += n; 19 | p = buf; 20 | while((q = strchr(p, '\n')) != 0){ 21 | *q = 0; 22 | if(match(pattern, p)){ 23 | *q = '\n'; 24 | write(1, p, q+1 - p); 25 | } 26 | p = q+1; 27 | } 28 | if(p == buf) 29 | m = 0; 30 | if(m > 0){ 31 | m -= p - buf; 32 | memmove(buf, p, m); 33 | } 34 | } 35 | } 36 | 37 | int 38 | main(int argc, char *argv[]) 39 | { 40 | int fd, i; 41 | char *pattern; 42 | 43 | if(argc <= 1){ 44 | printf(2, "usage: grep pattern [file ...]\n"); 45 | exit(); 46 | } 47 | pattern = argv[1]; 48 | 49 | if(argc <= 2){ 50 | grep(pattern, 0); 51 | exit(); 52 | } 53 | 54 | for(i = 2; i < argc; i++){ 55 | if((fd = open(argv[i], 0)) < 0){ 56 | printf(1, "grep: cannot open %s\n", argv[i]); 57 | exit(); 58 | } 59 | grep(pattern, fd); 60 | close(fd); 61 | } 62 | exit(); 63 | } 64 | 65 | // Regexp matcher from Kernighan & Pike, 66 | // The Practice of Programming, Chapter 9. 67 | 68 | int matchhere(char*, char*); 69 | int matchstar(int, char*, char*); 70 | 71 | int 72 | match(char *re, char *text) 73 | { 74 | if(re[0] == '^') 75 | return matchhere(re+1, text); 76 | do{ // must look at empty string 77 | if(matchhere(re, text)) 78 | return 1; 79 | }while(*text++ != '\0'); 80 | return 0; 81 | } 82 | 83 | // matchhere: search for re at beginning of text 84 | int matchhere(char *re, char *text) 85 | { 86 | if(re[0] == '\0') 87 | return 1; 88 | if(re[1] == '*') 89 | return matchstar(re[0], re+2, text); 90 | if(re[0] == '$' && re[1] == '\0') 91 | return *text == '\0'; 92 | if(*text!='\0' && (re[0]=='.' || re[0]==*text)) 93 | return matchhere(re+1, text+1); 94 | return 0; 95 | } 96 | 97 | // matchstar: search for c*re at beginning of text 98 | int matchstar(int c, char *re, char *text) 99 | { 100 | do{ // a * matches zero or more instances 101 | if(matchhere(re, text)) 102 | return 1; 103 | }while(*text!='\0' && (*text++==c || c=='.')); 104 | return 0; 105 | } 106 | 107 | -------------------------------------------------------------------------------- /ide.c: -------------------------------------------------------------------------------- 1 | // Simple PIO-based (non-DMA) IDE driver code. 2 | 3 | #include "types.h" 4 | #include "defs.h" 5 | #include "param.h" 6 | #include "memlayout.h" 7 | #include "mmu.h" 8 | #include "proc.h" 9 | #include "x86.h" 10 | #include "traps.h" 11 | #include "spinlock.h" 12 | #include "buf.h" 13 | 14 | #define IDE_BSY 0x80 15 | #define IDE_DRDY 0x40 16 | #define IDE_DF 0x20 17 | #define IDE_ERR 0x01 18 | 19 | #define IDE_CMD_READ 0x20 20 | #define IDE_CMD_WRITE 0x30 21 | 22 | // idequeue points to the buf now being read/written to the disk. 23 | // idequeue->qnext points to the next buf to be processed. 24 | // You must hold idelock while manipulating queue. 25 | 26 | static struct spinlock idelock; 27 | static struct buf *idequeue; 28 | 29 | static int havedisk1; 30 | static void idestart(struct buf*); 31 | 32 | // Wait for IDE disk to become ready. 33 | static int 34 | idewait(int checkerr) 35 | { 36 | int r; 37 | 38 | while(((r = inb(0x1f7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY) 39 | ; 40 | if(checkerr && (r & (IDE_DF|IDE_ERR)) != 0) 41 | return -1; 42 | return 0; 43 | } 44 | 45 | void 46 | ideinit(void) 47 | { 48 | int i; 49 | 50 | initlock(&idelock, "ide"); 51 | picenable(IRQ_IDE); 52 | ioapicenable(IRQ_IDE, ncpu - 1); 53 | idewait(0); 54 | 55 | // Check if disk 1 is present 56 | outb(0x1f6, 0xe0 | (1<<4)); 57 | for(i=0; i<1000; i++){ 58 | if(inb(0x1f7) != 0){ 59 | havedisk1 = 1; 60 | break; 61 | } 62 | } 63 | 64 | // Switch back to disk 0. 65 | outb(0x1f6, 0xe0 | (0<<4)); 66 | } 67 | 68 | // Start the request for b. Caller must hold idelock. 69 | static void 70 | idestart(struct buf *b) 71 | { 72 | if(b == 0) 73 | panic("idestart"); 74 | 75 | idewait(0); 76 | outb(0x3f6, 0); // generate interrupt 77 | outb(0x1f2, 1); // number of sectors 78 | outb(0x1f3, b->sector & 0xff); 79 | outb(0x1f4, (b->sector >> 8) & 0xff); 80 | outb(0x1f5, (b->sector >> 16) & 0xff); 81 | outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((b->sector>>24)&0x0f)); 82 | if(b->flags & B_DIRTY){ 83 | outb(0x1f7, IDE_CMD_WRITE); 84 | outsl(0x1f0, b->data, 512/4); 85 | } else { 86 | outb(0x1f7, IDE_CMD_READ); 87 | } 88 | } 89 | 90 | // Interrupt handler. 91 | void 92 | ideintr(void) 93 | { 94 | struct buf *b; 95 | 96 | // First queued buffer is the active request. 97 | acquire(&idelock); 98 | if((b = idequeue) == 0){ 99 | release(&idelock); 100 | // cprintf("spurious IDE interrupt\n"); 101 | return; 102 | } 103 | idequeue = b->qnext; 104 | 105 | // Read data if needed. 106 | if(!(b->flags & B_DIRTY) && idewait(1) >= 0) 107 | insl(0x1f0, b->data, 512/4); 108 | 109 | // Wake process waiting for this buf. 110 | b->flags |= B_VALID; 111 | b->flags &= ~B_DIRTY; 112 | wakeup(b); 113 | 114 | // Start disk on next buf in queue. 115 | if(idequeue != 0) 116 | idestart(idequeue); 117 | 118 | release(&idelock); 119 | } 120 | 121 | //PAGEBREAK! 122 | // Sync buf with disk. 123 | // If B_DIRTY is set, write buf to disk, clear B_DIRTY, set B_VALID. 124 | // Else if B_VALID is not set, read buf from disk, set B_VALID. 125 | void 126 | iderw(struct buf *b) 127 | { 128 | struct buf **pp; 129 | 130 | if(!(b->flags & B_BUSY)) 131 | panic("iderw: buf not busy"); 132 | if((b->flags & (B_VALID|B_DIRTY)) == B_VALID) 133 | panic("iderw: nothing to do"); 134 | if(b->dev != 0 && !havedisk1) 135 | panic("iderw: ide disk 1 not present"); 136 | 137 | acquire(&idelock); //DOC:acquire-lock 138 | 139 | // Append b to idequeue. 140 | b->qnext = 0; 141 | for(pp=&idequeue; *pp; pp=&(*pp)->qnext) //DOC:insert-queue 142 | ; 143 | *pp = b; 144 | 145 | // Start disk if necessary. 146 | if(idequeue == b) 147 | idestart(b); 148 | 149 | // Wait for request to finish. 150 | while((b->flags & (B_VALID|B_DIRTY)) != B_VALID){ 151 | sleep(b, &idelock); 152 | } 153 | 154 | release(&idelock); 155 | } 156 | -------------------------------------------------------------------------------- /init.c: -------------------------------------------------------------------------------- 1 | // init: The initial user-level program 2 | 3 | #include "types.h" 4 | #include "stat.h" 5 | #include "user.h" 6 | #include "fcntl.h" 7 | 8 | char *argv[] = { "sh", 0 }; 9 | 10 | int 11 | main(void) 12 | { 13 | int pid, wpid; 14 | 15 | if(open("console", O_RDWR) < 0){ 16 | mknod("console", 1, 1); 17 | open("console", O_RDWR); 18 | } 19 | dup(0); // stdout 20 | dup(0); // stderr 21 | 22 | for(;;){ 23 | printf(1, "init: starting sh\n"); 24 | pid = fork(); 25 | if(pid < 0){ 26 | printf(1, "init: fork failed\n"); 27 | exit(); 28 | } 29 | if(pid == 0){ 30 | exec("sh", argv); 31 | printf(1, "init: exec sh failed\n"); 32 | exit(); 33 | } 34 | while((wpid=wait()) >= 0 && wpid != pid) 35 | printf(1, "zombie!\n"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /initcode.S: -------------------------------------------------------------------------------- 1 | # Initial process execs /init. 2 | 3 | #include "syscall.h" 4 | #include "traps.h" 5 | 6 | 7 | # exec(init, argv) 8 | .globl start 9 | start: 10 | pushl $argv 11 | pushl $init 12 | pushl $0 // where caller pc would be 13 | movl $SYS_exec, %eax 14 | int $T_SYSCALL 15 | 16 | # for(;;) exit(); 17 | exit: 18 | movl $SYS_exit, %eax 19 | int $T_SYSCALL 20 | jmp exit 21 | 22 | # char init[] = "/init\0"; 23 | init: 24 | .string "/init\0" 25 | 26 | # char *argv[] = { init, 0 }; 27 | .p2align 2 28 | argv: 29 | .long init 30 | .long 0 31 | 32 | -------------------------------------------------------------------------------- /ioapic.c: -------------------------------------------------------------------------------- 1 | // The I/O APIC manages hardware interrupts for an SMP system. 2 | // http://www.intel.com/design/chipsets/datashts/29056601.pdf 3 | // See also picirq.c. 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "traps.h" 8 | 9 | #define IOAPIC 0xFEC00000 // Default physical address of IO APIC 10 | 11 | #define REG_ID 0x00 // Register index: ID 12 | #define REG_VER 0x01 // Register index: version 13 | #define REG_TABLE 0x10 // Redirection table base 14 | 15 | // The redirection table starts at REG_TABLE and uses 16 | // two registers to configure each interrupt. 17 | // The first (low) register in a pair contains configuration bits. 18 | // The second (high) register contains a bitmask telling which 19 | // CPUs can serve that interrupt. 20 | #define INT_DISABLED 0x00010000 // Interrupt disabled 21 | #define INT_LEVEL 0x00008000 // Level-triggered (vs edge-) 22 | #define INT_ACTIVELOW 0x00002000 // Active low (vs high) 23 | #define INT_LOGICAL 0x00000800 // Destination is CPU id (vs APIC ID) 24 | 25 | volatile struct ioapic *ioapic; 26 | 27 | // IO APIC MMIO structure: write reg, then read or write data. 28 | struct ioapic { 29 | uint reg; 30 | uint pad[3]; 31 | uint data; 32 | }; 33 | 34 | static uint 35 | ioapicread(int reg) 36 | { 37 | ioapic->reg = reg; 38 | return ioapic->data; 39 | } 40 | 41 | static void 42 | ioapicwrite(int reg, uint data) 43 | { 44 | ioapic->reg = reg; 45 | ioapic->data = data; 46 | } 47 | 48 | void 49 | ioapicinit(void) 50 | { 51 | int i, id, maxintr; 52 | 53 | if(!ismp) 54 | return; 55 | 56 | ioapic = (volatile struct ioapic*)IOAPIC; 57 | maxintr = (ioapicread(REG_VER) >> 16) & 0xFF; 58 | id = ioapicread(REG_ID) >> 24; 59 | if(id != ioapicid) 60 | cprintf("ioapicinit: id isn't equal to ioapicid; not a MP\n"); 61 | 62 | // Mark all interrupts edge-triggered, active high, disabled, 63 | // and not routed to any CPUs. 64 | for(i = 0; i <= maxintr; i++){ 65 | ioapicwrite(REG_TABLE+2*i, INT_DISABLED | (T_IRQ0 + i)); 66 | ioapicwrite(REG_TABLE+2*i+1, 0); 67 | } 68 | } 69 | 70 | void 71 | ioapicenable(int irq, int cpunum) 72 | { 73 | if(!ismp) 74 | return; 75 | 76 | // Mark interrupt edge-triggered, active high, 77 | // enabled, and routed to the given cpunum, 78 | // which happens to be that cpu's APIC ID. 79 | ioapicwrite(REG_TABLE+2*irq, T_IRQ0 + irq); 80 | ioapicwrite(REG_TABLE+2*irq+1, cpunum << 24); 81 | } 82 | -------------------------------------------------------------------------------- /kalloc.c: -------------------------------------------------------------------------------- 1 | // Physical memory allocator, intended to allocate 2 | // memory for user processes, kernel stacks, page table pages, 3 | // and pipe buffers. Allocates 4096-byte pages. 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "param.h" 8 | #include "memlayout.h" 9 | #include "mmu.h" 10 | #include "spinlock.h" 11 | 12 | void freerange(void *vstart, void *vend); 13 | extern char end[]; // first address after kernel loaded from ELF file 14 | 15 | struct run { 16 | struct run *next; 17 | }; 18 | 19 | struct { 20 | struct spinlock lock; 21 | int use_lock; 22 | struct run *freelist; 23 | } kmem; 24 | 25 | // Initialization happens in two phases. 26 | // 1. main() calls kinit1() while still using entrypgdir to place just 27 | // the pages mapped by entrypgdir on free list. 28 | // 2. main() calls kinit2() with the rest of the physical pages 29 | // after installing a full page table that maps them on all cores. 30 | void 31 | kinit1(void *vstart, void *vend) 32 | { 33 | initlock(&kmem.lock, "kmem"); 34 | kmem.use_lock = 0; 35 | freerange(vstart, vend); 36 | } 37 | 38 | void 39 | kinit2(void *vstart, void *vend) 40 | { 41 | freerange(vstart, vend); 42 | kmem.use_lock = 1; 43 | } 44 | 45 | void 46 | freerange(void *vstart, void *vend) 47 | { 48 | char *p; 49 | p = (char*)PGROUNDUP((uint)vstart); 50 | for(; p + PGSIZE <= (char*)vend; p += PGSIZE) 51 | kfree(p); 52 | } 53 | 54 | //PAGEBREAK: 21 55 | // Free the page of physical memory pointed at by v, 56 | // which normally should have been returned by a 57 | // call to kalloc(). (The exception is when 58 | // initializing the allocator; see kinit above.) 59 | void 60 | kfree(char *v) 61 | { 62 | struct run *r; 63 | 64 | if((uint)v % PGSIZE || v < end || v2p(v) >= PHYSTOP) 65 | panic("kfree"); 66 | 67 | // Fill with junk to catch dangling refs. 68 | memset(v, 1, PGSIZE); 69 | 70 | if(kmem.use_lock) 71 | acquire(&kmem.lock); 72 | r = (struct run*)v; 73 | r->next = kmem.freelist; 74 | kmem.freelist = r; 75 | if(kmem.use_lock) 76 | release(&kmem.lock); 77 | } 78 | 79 | // Allocate one 4096-byte page of physical memory. 80 | // Returns a pointer that the kernel can use. 81 | // Returns 0 if the memory cannot be allocated. 82 | char* 83 | kalloc(void) 84 | { 85 | struct run *r; 86 | 87 | if(kmem.use_lock) 88 | acquire(&kmem.lock); 89 | r = kmem.freelist; 90 | if(r) 91 | kmem.freelist = r->next; 92 | if(kmem.use_lock) 93 | release(&kmem.lock); 94 | return (char*)r; 95 | } 96 | 97 | -------------------------------------------------------------------------------- /kbd.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "x86.h" 3 | #include "defs.h" 4 | #include "kbd.h" 5 | 6 | int 7 | kbdgetc(void) 8 | { 9 | static uint shift; 10 | static uchar *charcode[4] = { 11 | normalmap, shiftmap, ctlmap, ctlmap 12 | }; 13 | uint st, data, c; 14 | 15 | st = inb(KBSTATP); 16 | if((st & KBS_DIB) == 0) 17 | return -1; 18 | data = inb(KBDATAP); 19 | 20 | if(data == 0xE0){ 21 | shift |= E0ESC; 22 | return 0; 23 | } else if(data & 0x80){ 24 | // Key released 25 | data = (shift & E0ESC ? data : data & 0x7F); 26 | shift &= ~(shiftcode[data] | E0ESC); 27 | return 0; 28 | } else if(shift & E0ESC){ 29 | // Last character was an E0 escape; or with 0x80 30 | data |= 0x80; 31 | shift &= ~E0ESC; 32 | } 33 | 34 | shift |= shiftcode[data]; 35 | shift ^= togglecode[data]; 36 | c = charcode[shift & (CTL | SHIFT)][data]; 37 | if(shift & CAPSLOCK){ 38 | if('a' <= c && c <= 'z') 39 | c += 'A' - 'a'; 40 | else if('A' <= c && c <= 'Z') 41 | c += 'a' - 'A'; 42 | } 43 | return c; 44 | } 45 | 46 | void 47 | kbdintr(void) 48 | { 49 | consoleintr(kbdgetc); 50 | } 51 | -------------------------------------------------------------------------------- /kbd.h: -------------------------------------------------------------------------------- 1 | // PC keyboard interface constants 2 | 3 | #define KBSTATP 0x64 // kbd controller status port(I) 4 | #define KBS_DIB 0x01 // kbd data in buffer 5 | #define KBDATAP 0x60 // kbd data port(I) 6 | 7 | #define NO 0 8 | 9 | #define SHIFT (1<<0) 10 | #define CTL (1<<1) 11 | #define ALT (1<<2) 12 | 13 | #define CAPSLOCK (1<<3) 14 | #define NUMLOCK (1<<4) 15 | #define SCROLLLOCK (1<<5) 16 | 17 | #define E0ESC (1<<6) 18 | 19 | // Special keycodes 20 | #define KEY_HOME 0xE0 21 | #define KEY_END 0xE1 22 | #define KEY_UP 0xE2 23 | #define KEY_DN 0xE3 24 | #define KEY_LF 0xE4 25 | #define KEY_RT 0xE5 26 | #define KEY_PGUP 0xE6 27 | #define KEY_PGDN 0xE7 28 | #define KEY_INS 0xE8 29 | #define KEY_DEL 0xE9 30 | 31 | // C('A') == Control-A 32 | #define C(x) (x - '@') 33 | 34 | static uchar shiftcode[256] = 35 | { 36 | [0x1D] CTL, 37 | [0x2A] SHIFT, 38 | [0x36] SHIFT, 39 | [0x38] ALT, 40 | [0x9D] CTL, 41 | [0xB8] ALT 42 | }; 43 | 44 | static uchar togglecode[256] = 45 | { 46 | [0x3A] CAPSLOCK, 47 | [0x45] NUMLOCK, 48 | [0x46] SCROLLLOCK 49 | }; 50 | 51 | static uchar normalmap[256] = 52 | { 53 | NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00 54 | '7', '8', '9', '0', '-', '=', '\b', '\t', 55 | 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10 56 | 'o', 'p', '[', ']', '\n', NO, 'a', 's', 57 | 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20 58 | '\'', '`', NO, '\\', 'z', 'x', 'c', 'v', 59 | 'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30 60 | NO, ' ', NO, NO, NO, NO, NO, NO, 61 | NO, NO, NO, NO, NO, NO, NO, '7', // 0x40 62 | '8', '9', '-', '4', '5', '6', '+', '1', 63 | '2', '3', '0', '.', NO, NO, NO, NO, // 0x50 64 | [0x9C] '\n', // KP_Enter 65 | [0xB5] '/', // KP_Div 66 | [0xC8] KEY_UP, [0xD0] KEY_DN, 67 | [0xC9] KEY_PGUP, [0xD1] KEY_PGDN, 68 | [0xCB] KEY_LF, [0xCD] KEY_RT, 69 | [0x97] KEY_HOME, [0xCF] KEY_END, 70 | [0xD2] KEY_INS, [0xD3] KEY_DEL 71 | }; 72 | 73 | static uchar shiftmap[256] = 74 | { 75 | NO, 033, '!', '@', '#', '$', '%', '^', // 0x00 76 | '&', '*', '(', ')', '_', '+', '\b', '\t', 77 | 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10 78 | 'O', 'P', '{', '}', '\n', NO, 'A', 'S', 79 | 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20 80 | '"', '~', NO, '|', 'Z', 'X', 'C', 'V', 81 | 'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30 82 | NO, ' ', NO, NO, NO, NO, NO, NO, 83 | NO, NO, NO, NO, NO, NO, NO, '7', // 0x40 84 | '8', '9', '-', '4', '5', '6', '+', '1', 85 | '2', '3', '0', '.', NO, NO, NO, NO, // 0x50 86 | [0x9C] '\n', // KP_Enter 87 | [0xB5] '/', // KP_Div 88 | [0xC8] KEY_UP, [0xD0] KEY_DN, 89 | [0xC9] KEY_PGUP, [0xD1] KEY_PGDN, 90 | [0xCB] KEY_LF, [0xCD] KEY_RT, 91 | [0x97] KEY_HOME, [0xCF] KEY_END, 92 | [0xD2] KEY_INS, [0xD3] KEY_DEL 93 | }; 94 | 95 | static uchar ctlmap[256] = 96 | { 97 | NO, NO, NO, NO, NO, NO, NO, NO, 98 | NO, NO, NO, NO, NO, NO, NO, NO, 99 | C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'), 100 | C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'), 101 | C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO, 102 | NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'), 103 | C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO, 104 | [0x9C] '\r', // KP_Enter 105 | [0xB5] C('/'), // KP_Div 106 | [0xC8] KEY_UP, [0xD0] KEY_DN, 107 | [0xC9] KEY_PGUP, [0xD1] KEY_PGDN, 108 | [0xCB] KEY_LF, [0xCD] KEY_RT, 109 | [0x97] KEY_HOME, [0xCF] KEY_END, 110 | [0xD2] KEY_INS, [0xD3] KEY_DEL 111 | }; 112 | 113 | -------------------------------------------------------------------------------- /kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | /* Must be equal to KERNLINK */ 12 | . = 0x80100000; 13 | 14 | .text : AT(0x100000) { 15 | *(.text .stub .text.* .gnu.linkonce.t.*) 16 | } 17 | 18 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 19 | 20 | .rodata : { 21 | *(.rodata .rodata.* .gnu.linkonce.r.*) 22 | } 23 | 24 | /* Include debugging information in kernel memory */ 25 | .stab : { 26 | PROVIDE(__STAB_BEGIN__ = .); 27 | *(.stab); 28 | PROVIDE(__STAB_END__ = .); 29 | BYTE(0) /* Force the linker to allocate space 30 | for this section */ 31 | } 32 | 33 | .stabstr : { 34 | PROVIDE(__STABSTR_BEGIN__ = .); 35 | *(.stabstr); 36 | PROVIDE(__STABSTR_END__ = .); 37 | BYTE(0) /* Force the linker to allocate space 38 | for this section */ 39 | } 40 | 41 | /* Adjust the address for the data segment to the next page */ 42 | . = ALIGN(0x1000); 43 | 44 | /* Conventionally, Unix linkers provide pseudo-symbols 45 | * etext, edata, and end, at the end of the text, data, and bss. 46 | * For the kernel mapping, we need the address at the beginning 47 | * of the data section, but that's not one of the conventional 48 | * symbols, because the convention started before there was a 49 | * read-only rodata section between text and data. */ 50 | PROVIDE(data = .); 51 | 52 | /* The data segment */ 53 | .data : { 54 | *(.data) 55 | } 56 | 57 | PROVIDE(edata = .); 58 | 59 | .bss : { 60 | *(.bss) 61 | } 62 | 63 | PROVIDE(end = .); 64 | 65 | /DISCARD/ : { 66 | *(.eh_frame .note.GNU-stack) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /kill.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | int 6 | main(int argc, char **argv) 7 | { 8 | int i; 9 | 10 | if(argc < 1){ 11 | printf(2, "usage: kill pid...\n"); 12 | exit(); 13 | } 14 | for(i=1; i>16) & 0xFF) >= 4) 76 | lapicw(PCINT, MASKED); 77 | 78 | // Map error interrupt to IRQ_ERROR. 79 | lapicw(ERROR, T_IRQ0 + IRQ_ERROR); 80 | 81 | // Clear error status register (requires back-to-back writes). 82 | lapicw(ESR, 0); 83 | lapicw(ESR, 0); 84 | 85 | // Ack any outstanding interrupts. 86 | lapicw(EOI, 0); 87 | 88 | // Send an Init Level De-Assert to synchronise arbitration ID's. 89 | lapicw(ICRHI, 0); 90 | lapicw(ICRLO, BCAST | INIT | LEVEL); 91 | while(lapic[ICRLO] & DELIVS) 92 | ; 93 | 94 | // Enable interrupts on the APIC (but not on the processor). 95 | lapicw(TPR, 0); 96 | } 97 | 98 | int 99 | cpunum(void) 100 | { 101 | // Cannot call cpu when interrupts are enabled: 102 | // result not guaranteed to last long enough to be used! 103 | // Would prefer to panic but even printing is chancy here: 104 | // almost everything, including cprintf and panic, calls cpu, 105 | // often indirectly through acquire and release. 106 | if(readeflags()&FL_IF){ 107 | static int n; 108 | if(n++ == 0) 109 | cprintf("cpu called from %x with interrupts enabled\n", 110 | __builtin_return_address(0)); 111 | } 112 | 113 | if(lapic) 114 | return lapic[ID]>>24; 115 | return 0; 116 | } 117 | 118 | // Acknowledge interrupt. 119 | void 120 | lapiceoi(void) 121 | { 122 | if(lapic) 123 | lapicw(EOI, 0); 124 | } 125 | 126 | // Spin for a given number of microseconds. 127 | // On real hardware would want to tune this dynamically. 128 | void 129 | microdelay(int us) 130 | { 131 | } 132 | 133 | #define IO_RTC 0x70 134 | 135 | // Start additional processor running entry code at addr. 136 | // See Appendix B of MultiProcessor Specification. 137 | void 138 | lapicstartap(uchar apicid, uint addr) 139 | { 140 | int i; 141 | ushort *wrv; 142 | 143 | // "The BSP must initialize CMOS shutdown code to 0AH 144 | // and the warm reset vector (DWORD based at 40:67) to point at 145 | // the AP startup code prior to the [universal startup algorithm]." 146 | outb(IO_RTC, 0xF); // offset 0xF is shutdown code 147 | outb(IO_RTC+1, 0x0A); 148 | wrv = (ushort*)P2V((0x40<<4 | 0x67)); // Warm reset vector 149 | wrv[0] = 0; 150 | wrv[1] = addr >> 4; 151 | 152 | // "Universal startup algorithm." 153 | // Send INIT (level-triggered) interrupt to reset other CPU. 154 | lapicw(ICRHI, apicid<<24); 155 | lapicw(ICRLO, INIT | LEVEL | ASSERT); 156 | microdelay(200); 157 | lapicw(ICRLO, INIT | LEVEL); 158 | microdelay(100); // should be 10ms, but too slow in Bochs! 159 | 160 | // Send startup IPI (twice!) to enter code. 161 | // Regular hardware is supposed to only accept a STARTUP 162 | // when it is in the halted state due to an INIT. So the second 163 | // should be ignored, but it is part of the official Intel algorithm. 164 | // Bochs complains about the second one. Too bad for Bochs. 165 | for(i = 0; i < 2; i++){ 166 | lapicw(ICRHI, apicid<<24); 167 | lapicw(ICRLO, STARTUP | (addr>>12)); 168 | microdelay(200); 169 | } 170 | } 171 | 172 | 173 | -------------------------------------------------------------------------------- /ln.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | if(argc != 3){ 9 | printf(2, "Usage: ln old new\n"); 10 | exit(); 11 | } 12 | if(link(argv[1], argv[2]) < 0) 13 | printf(2, "link %s %s: failed\n", argv[1], argv[2]); 14 | exit(); 15 | } 16 | -------------------------------------------------------------------------------- /log.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "defs.h" 3 | #include "param.h" 4 | #include "spinlock.h" 5 | #include "fs.h" 6 | #include "buf.h" 7 | 8 | // Simple logging. Each system call that might write the file system 9 | // should be surrounded with begin_trans() and commit_trans() calls. 10 | // 11 | // The log holds at most one transaction at a time. Commit forces 12 | // the log (with commit record) to disk, then installs the affected 13 | // blocks to disk, then erases the log. begin_trans() ensures that 14 | // only one system call can be in a transaction; others must wait. 15 | // 16 | // Allowing only one transaction at a time means that the file 17 | // system code doesn't have to worry about the possibility of 18 | // one transaction reading a block that another one has modified, 19 | // for example an i-node block. 20 | // 21 | // Read-only system calls don't need to use transactions, though 22 | // this means that they may observe uncommitted data. I-node and 23 | // buffer locks prevent read-only calls from seeing inconsistent data. 24 | // 25 | // The log is a physical re-do log containing disk blocks. 26 | // The on-disk log format: 27 | // header block, containing sector #s for block A, B, C, ... 28 | // block A 29 | // block B 30 | // block C 31 | // ... 32 | // Log appends are synchronous. 33 | 34 | // Contents of the header block, used for both the on-disk header block 35 | // and to keep track in memory of logged sector #s before commit. 36 | struct logheader { 37 | int n; 38 | int sector[LOGSIZE]; 39 | }; 40 | 41 | struct log { 42 | struct spinlock lock; 43 | int start; 44 | int size; 45 | int busy; // a transaction is active 46 | int dev; 47 | struct logheader lh; 48 | }; 49 | struct log log; 50 | 51 | static void recover_from_log(void); 52 | 53 | void 54 | initlog(void) 55 | { 56 | if (sizeof(struct logheader) >= BSIZE) 57 | panic("initlog: too big logheader"); 58 | 59 | struct superblock sb; 60 | initlock(&log.lock, "log"); 61 | readsb(ROOTDEV, &sb); 62 | log.start = sb.size - sb.nlog; 63 | log.size = sb.nlog; 64 | log.dev = ROOTDEV; 65 | recover_from_log(); 66 | } 67 | 68 | // Copy committed blocks from log to their home location 69 | static void 70 | install_trans(void) 71 | { 72 | int tail; 73 | 74 | for (tail = 0; tail < log.lh.n; tail++) { 75 | struct buf *lbuf = bread(log.dev, log.start+tail+1); // read log block 76 | struct buf *dbuf = bread(log.dev, log.lh.sector[tail]); // read dst 77 | memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst 78 | bwrite(dbuf); // write dst to disk 79 | brelse(lbuf); 80 | brelse(dbuf); 81 | } 82 | } 83 | 84 | // Read the log header from disk into the in-memory log header 85 | static void 86 | read_head(void) 87 | { 88 | struct buf *buf = bread(log.dev, log.start); 89 | struct logheader *lh = (struct logheader *) (buf->data); 90 | int i; 91 | log.lh.n = lh->n; 92 | for (i = 0; i < log.lh.n; i++) { 93 | log.lh.sector[i] = lh->sector[i]; 94 | } 95 | brelse(buf); 96 | } 97 | 98 | // Write in-memory log header to disk. 99 | // This is the true point at which the 100 | // current transaction commits. 101 | static void 102 | write_head(void) 103 | { 104 | struct buf *buf = bread(log.dev, log.start); 105 | struct logheader *hb = (struct logheader *) (buf->data); 106 | int i; 107 | hb->n = log.lh.n; 108 | for (i = 0; i < log.lh.n; i++) { 109 | hb->sector[i] = log.lh.sector[i]; 110 | } 111 | bwrite(buf); 112 | brelse(buf); 113 | } 114 | 115 | static void 116 | recover_from_log(void) 117 | { 118 | read_head(); 119 | install_trans(); // if committed, copy from log to disk 120 | log.lh.n = 0; 121 | write_head(); // clear the log 122 | } 123 | 124 | void 125 | begin_trans(void) 126 | { 127 | acquire(&log.lock); 128 | while (log.busy) { 129 | sleep(&log, &log.lock); 130 | } 131 | log.busy = 1; 132 | release(&log.lock); 133 | } 134 | 135 | void 136 | commit_trans(void) 137 | { 138 | if (log.lh.n > 0) { 139 | write_head(); // Write header to disk -- the real commit 140 | install_trans(); // Now install writes to home locations 141 | log.lh.n = 0; 142 | write_head(); // Erase the transaction from the log 143 | } 144 | 145 | acquire(&log.lock); 146 | log.busy = 0; 147 | wakeup(&log); 148 | release(&log.lock); 149 | } 150 | 151 | // Caller has modified b->data and is done with the buffer. 152 | // Append the block to the log and record the block number, 153 | // but don't write the log header (which would commit the write). 154 | // log_write() replaces bwrite(); a typical use is: 155 | // bp = bread(...) 156 | // modify bp->data[] 157 | // log_write(bp) 158 | // brelse(bp) 159 | void 160 | log_write(struct buf *b) 161 | { 162 | int i; 163 | 164 | if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1) 165 | panic("too big a transaction"); 166 | if (!log.busy) 167 | panic("write outside of trans"); 168 | 169 | for (i = 0; i < log.lh.n; i++) { 170 | if (log.lh.sector[i] == b->sector) // log absorbtion? 171 | break; 172 | } 173 | log.lh.sector[i] = b->sector; 174 | struct buf *lbuf = bread(b->dev, log.start+i+1); 175 | memmove(lbuf->data, b->data, BSIZE); 176 | bwrite(lbuf); 177 | brelse(lbuf); 178 | if (i == log.lh.n) 179 | log.lh.n++; 180 | b->flags |= B_DIRTY; // XXX prevent eviction 181 | } 182 | 183 | //PAGEBREAK! 184 | // Blank page. 185 | 186 | -------------------------------------------------------------------------------- /ls.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | #include "fs.h" 5 | 6 | char* 7 | fmtname(char *path) 8 | { 9 | static char buf[DIRSIZ+1]; 10 | char *p; 11 | 12 | // Find first character after last slash. 13 | for(p=path+strlen(path); p >= path && *p != '/'; p--) 14 | ; 15 | p++; 16 | 17 | // Return blank-padded name. 18 | if(strlen(p) >= DIRSIZ) 19 | return p; 20 | memmove(buf, p, strlen(p)); 21 | memset(buf+strlen(p), ' ', DIRSIZ-strlen(p)); 22 | return buf; 23 | } 24 | 25 | void 26 | ls(char *path) 27 | { 28 | char buf[512], *p; 29 | int fd; 30 | struct dirent de; 31 | struct stat st; 32 | 33 | if((fd = open(path, 0)) < 0){ 34 | printf(2, "ls: cannot open %s\n", path); 35 | return; 36 | } 37 | 38 | if(fstat(fd, &st) < 0){ 39 | printf(2, "ls: cannot stat %s\n", path); 40 | close(fd); 41 | return; 42 | } 43 | 44 | switch(st.type){ 45 | case T_FILE: 46 | printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size); 47 | break; 48 | 49 | case T_DIR: 50 | if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ 51 | printf(1, "ls: path too long\n"); 52 | break; 53 | } 54 | strcpy(buf, path); 55 | p = buf+strlen(buf); 56 | *p++ = '/'; 57 | while(read(fd, &de, sizeof(de)) == sizeof(de)){ 58 | if(de.inum == 0) 59 | continue; 60 | memmove(p, de.name, DIRSIZ); 61 | p[DIRSIZ] = 0; 62 | if(stat(buf, &st) < 0){ 63 | printf(1, "ls: cannot stat %s\n", buf); 64 | continue; 65 | } 66 | printf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size); 67 | } 68 | break; 69 | } 70 | close(fd); 71 | } 72 | 73 | int 74 | main(int argc, char *argv[]) 75 | { 76 | int i; 77 | 78 | if(argc < 2){ 79 | ls("."); 80 | exit(); 81 | } 82 | for(i=1; iid); 26 | picinit(); // interrupt controller 27 | ioapicinit(); // another interrupt controller 28 | consoleinit(); // I/O devices & their interrupts 29 | uartinit(); // serial port 30 | pinit(); // process table 31 | tvinit(); // trap vectors 32 | binit(); // buffer cache 33 | fileinit(); // file table 34 | iinit(); // inode cache 35 | ideinit(); // disk 36 | if(!ismp) 37 | timerinit(); // uniprocessor timer 38 | startothers(); // start other processors 39 | kinit2(P2V(4*1024*1024), P2V(PHYSTOP)); // must come after startothers() 40 | userinit(); // first user process 41 | // Finish setting up this processor in mpmain. 42 | mpmain(); 43 | } 44 | 45 | // Other CPUs jump here from entryother.S. 46 | static void 47 | mpenter(void) 48 | { 49 | switchkvm(); 50 | seginit(); 51 | lapicinit(); 52 | mpmain(); 53 | } 54 | 55 | // Common CPU setup code. 56 | static void 57 | mpmain(void) 58 | { 59 | cprintf("cpu%d: starting\n", cpu->id); 60 | idtinit(); // load idt register 61 | xchg(&cpu->started, 1); // tell startothers() we're up 62 | scheduler(); // start running processes 63 | } 64 | 65 | pde_t entrypgdir[]; // For entry.S 66 | 67 | // Start the non-boot (AP) processors. 68 | static void 69 | startothers(void) 70 | { 71 | extern uchar _binary_entryother_start[], _binary_entryother_size[]; 72 | uchar *code; 73 | struct cpu *c; 74 | char *stack; 75 | 76 | // Write entry code to unused memory at 0x7000. 77 | // The linker has placed the image of entryother.S in 78 | // _binary_entryother_start. 79 | code = p2v(0x7000); 80 | memmove(code, _binary_entryother_start, (uint)_binary_entryother_size); 81 | 82 | for(c = cpus; c < cpus+ncpu; c++){ 83 | if(c == cpus+cpunum()) // We've started already. 84 | continue; 85 | 86 | // Tell entryother.S what stack to use, where to enter, and what 87 | // pgdir to use. We cannot use kpgdir yet, because the AP processor 88 | // is running in low memory, so we use entrypgdir for the APs too. 89 | stack = kalloc(); 90 | *(void**)(code-4) = stack + KSTACKSIZE; 91 | *(void**)(code-8) = mpenter; 92 | *(int**)(code-12) = (void *) v2p(entrypgdir); 93 | 94 | lapicstartap(c->id, v2p(code)); 95 | 96 | // wait for cpu to finish mpmain() 97 | while(c->started == 0) 98 | ; 99 | } 100 | } 101 | 102 | // Boot page table used in entry.S and entryother.S. 103 | // Page directories (and page tables), must start on a page boundary, 104 | // hence the "__aligned__" attribute. 105 | // Use PTE_PS in page directory entry to enable 4Mbyte pages. 106 | __attribute__((__aligned__(PGSIZE))) 107 | pde_t entrypgdir[NPDENTRIES] = { 108 | // Map VA's [0, 4MB) to PA's [0, 4MB) 109 | [0] = (0) | PTE_P | PTE_W | PTE_PS, 110 | // Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB) 111 | [KERNBASE>>PDXSHIFT] = (0) | PTE_P | PTE_W | PTE_PS, 112 | }; 113 | 114 | //PAGEBREAK! 115 | // Blank page. 116 | 117 | -------------------------------------------------------------------------------- /memide.c: -------------------------------------------------------------------------------- 1 | // Fake IDE disk; stores blocks in memory. 2 | // Useful for running kernel without scratch disk. 3 | 4 | #include "types.h" 5 | #include "defs.h" 6 | #include "param.h" 7 | #include "mmu.h" 8 | #include "proc.h" 9 | #include "x86.h" 10 | #include "traps.h" 11 | #include "spinlock.h" 12 | #include "buf.h" 13 | 14 | extern uchar _binary_fs_img_start[], _binary_fs_img_size[]; 15 | 16 | static int disksize; 17 | static uchar *memdisk; 18 | 19 | void 20 | ideinit(void) 21 | { 22 | memdisk = _binary_fs_img_start; 23 | disksize = (uint)_binary_fs_img_size/512; 24 | } 25 | 26 | // Interrupt handler. 27 | void 28 | ideintr(void) 29 | { 30 | // no-op 31 | } 32 | 33 | // Sync buf with disk. 34 | // If B_DIRTY is set, write buf to disk, clear B_DIRTY, set B_VALID. 35 | // Else if B_VALID is not set, read buf from disk, set B_VALID. 36 | void 37 | iderw(struct buf *b) 38 | { 39 | uchar *p; 40 | 41 | if(!(b->flags & B_BUSY)) 42 | panic("iderw: buf not busy"); 43 | if((b->flags & (B_VALID|B_DIRTY)) == B_VALID) 44 | panic("iderw: nothing to do"); 45 | if(b->dev != 1) 46 | panic("iderw: request not for disk 1"); 47 | if(b->sector >= disksize) 48 | panic("iderw: sector out of range"); 49 | 50 | p = memdisk + b->sector*512; 51 | 52 | if(b->flags & B_DIRTY){ 53 | b->flags &= ~B_DIRTY; 54 | memmove(p, b->data, 512); 55 | } else 56 | memmove(b->data, p, 512); 57 | b->flags |= B_VALID; 58 | } 59 | -------------------------------------------------------------------------------- /memlayout.h: -------------------------------------------------------------------------------- 1 | // Memory layout 2 | 3 | #define EXTMEM 0x100000 // Start of extended memory 4 | #define PHYSTOP 0xE000000 // Top physical memory 5 | #define DEVSPACE 0xFE000000 // Other devices are at high addresses 6 | 7 | // Key addresses for address space layout (see kmap in vm.c for layout) 8 | #define KERNBASE 0x80000000 // First kernel virtual address 9 | #define KERNLINK (KERNBASE+EXTMEM) // Address where kernel is linked 10 | 11 | #ifndef __ASSEMBLER__ 12 | 13 | static inline uint v2p(void *a) { return ((uint) (a)) - KERNBASE; } 14 | static inline void *p2v(uint a) { return (void *) ((a) + KERNBASE); } 15 | 16 | #endif 17 | 18 | #define V2P(a) (((uint) (a)) - KERNBASE) 19 | #define P2V(a) (((void *) (a)) + KERNBASE) 20 | 21 | #define V2P_WO(x) ((x) - KERNBASE) // same as V2P, but without casts 22 | #define P2V_WO(x) ((x) + KERNBASE) // same as V2P, but without casts 23 | -------------------------------------------------------------------------------- /mkdir.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | int i; 9 | 10 | if(argc < 2){ 11 | printf(2, "Usage: mkdir files...\n"); 12 | exit(); 13 | } 14 | 15 | for(i = 1; i < argc; i++){ 16 | if(mkdir(argv[i]) < 0){ 17 | printf(2, "mkdir: %s failed to create\n", argv[i]); 18 | break; 19 | } 20 | } 21 | 22 | exit(); 23 | } 24 | -------------------------------------------------------------------------------- /mkfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define stat xv6_stat // avoid clash with host struct stat 9 | #include "types.h" 10 | #include "fs.h" 11 | #include "stat.h" 12 | #include "param.h" 13 | 14 | #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) 15 | 16 | int nblocks = 985; 17 | int nlog = LOGSIZE; 18 | int ninodes = 200; 19 | int size = 1024; 20 | 21 | int fsfd; 22 | struct superblock sb; 23 | char zeroes[512]; 24 | uint freeblock; 25 | uint usedblocks; 26 | uint bitblocks; 27 | uint freeinode = 1; 28 | 29 | void balloc(int); 30 | void wsect(uint, void*); 31 | void winode(uint, struct dinode*); 32 | void rinode(uint inum, struct dinode *ip); 33 | void rsect(uint sec, void *buf); 34 | uint ialloc(ushort type); 35 | void iappend(uint inum, void *p, int n); 36 | 37 | // convert to intel byte order 38 | ushort 39 | xshort(ushort x) 40 | { 41 | ushort y; 42 | uchar *a = (uchar*)&y; 43 | a[0] = x; 44 | a[1] = x >> 8; 45 | return y; 46 | } 47 | 48 | uint 49 | xint(uint x) 50 | { 51 | uint y; 52 | uchar *a = (uchar*)&y; 53 | a[0] = x; 54 | a[1] = x >> 8; 55 | a[2] = x >> 16; 56 | a[3] = x >> 24; 57 | return y; 58 | } 59 | 60 | int 61 | main(int argc, char *argv[]) 62 | { 63 | int i, cc, fd; 64 | uint rootino, inum, off; 65 | struct dirent de; 66 | char buf[512]; 67 | struct dinode din; 68 | 69 | 70 | static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); 71 | 72 | if(argc < 2){ 73 | fprintf(stderr, "Usage: mkfs fs.img files...\n"); 74 | exit(1); 75 | } 76 | 77 | assert((512 % sizeof(struct dinode)) == 0); 78 | assert((512 % sizeof(struct dirent)) == 0); 79 | 80 | fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); 81 | if(fsfd < 0){ 82 | perror(argv[1]); 83 | exit(1); 84 | } 85 | 86 | sb.size = xint(size); 87 | sb.nblocks = xint(nblocks); // so whole disk is size sectors 88 | sb.ninodes = xint(ninodes); 89 | sb.nlog = xint(nlog); 90 | 91 | bitblocks = size/(512*8) + 1; 92 | usedblocks = ninodes / IPB + 3 + bitblocks; 93 | freeblock = usedblocks; 94 | 95 | printf("used %d (bit %d ninode %zu) free %u log %u total %d\n", usedblocks, 96 | bitblocks, ninodes/IPB + 1, freeblock, nlog, nblocks+usedblocks+nlog); 97 | 98 | assert(nblocks + usedblocks + nlog == size); 99 | 100 | for(i = 0; i < nblocks + usedblocks + nlog; i++) 101 | wsect(i, zeroes); 102 | 103 | memset(buf, 0, sizeof(buf)); 104 | memmove(buf, &sb, sizeof(sb)); 105 | wsect(1, buf); 106 | 107 | rootino = ialloc(T_DIR); 108 | assert(rootino == ROOTINO); 109 | 110 | bzero(&de, sizeof(de)); 111 | de.inum = xshort(rootino); 112 | strcpy(de.name, "."); 113 | iappend(rootino, &de, sizeof(de)); 114 | 115 | bzero(&de, sizeof(de)); 116 | de.inum = xshort(rootino); 117 | strcpy(de.name, ".."); 118 | iappend(rootino, &de, sizeof(de)); 119 | 120 | for(i = 2; i < argc; i++){ 121 | assert(index(argv[i], '/') == 0); 122 | 123 | if((fd = open(argv[i], 0)) < 0){ 124 | perror(argv[i]); 125 | exit(1); 126 | } 127 | 128 | // Skip leading _ in name when writing to file system. 129 | // The binaries are named _rm, _cat, etc. to keep the 130 | // build operating system from trying to execute them 131 | // in place of system binaries like rm and cat. 132 | if(argv[i][0] == '_') 133 | ++argv[i]; 134 | 135 | inum = ialloc(T_FILE); 136 | 137 | bzero(&de, sizeof(de)); 138 | de.inum = xshort(inum); 139 | strncpy(de.name, argv[i], DIRSIZ); 140 | iappend(rootino, &de, sizeof(de)); 141 | 142 | while((cc = read(fd, buf, sizeof(buf))) > 0) 143 | iappend(inum, buf, cc); 144 | 145 | close(fd); 146 | } 147 | 148 | // fix size of root inode dir 149 | rinode(rootino, &din); 150 | off = xint(din.size); 151 | off = ((off/BSIZE) + 1) * BSIZE; 152 | din.size = xint(off); 153 | winode(rootino, &din); 154 | 155 | balloc(usedblocks); 156 | 157 | exit(0); 158 | } 159 | 160 | void 161 | wsect(uint sec, void *buf) 162 | { 163 | if(lseek(fsfd, sec * 512L, 0) != sec * 512L){ 164 | perror("lseek"); 165 | exit(1); 166 | } 167 | if(write(fsfd, buf, 512) != 512){ 168 | perror("write"); 169 | exit(1); 170 | } 171 | } 172 | 173 | uint 174 | i2b(uint inum) 175 | { 176 | return (inum / IPB) + 2; 177 | } 178 | 179 | void 180 | winode(uint inum, struct dinode *ip) 181 | { 182 | char buf[512]; 183 | uint bn; 184 | struct dinode *dip; 185 | 186 | bn = i2b(inum); 187 | rsect(bn, buf); 188 | dip = ((struct dinode*)buf) + (inum % IPB); 189 | *dip = *ip; 190 | wsect(bn, buf); 191 | } 192 | 193 | void 194 | rinode(uint inum, struct dinode *ip) 195 | { 196 | char buf[512]; 197 | uint bn; 198 | struct dinode *dip; 199 | 200 | bn = i2b(inum); 201 | rsect(bn, buf); 202 | dip = ((struct dinode*)buf) + (inum % IPB); 203 | *ip = *dip; 204 | } 205 | 206 | void 207 | rsect(uint sec, void *buf) 208 | { 209 | if(lseek(fsfd, sec * 512L, 0) != sec * 512L){ 210 | perror("lseek"); 211 | exit(1); 212 | } 213 | if(read(fsfd, buf, 512) != 512){ 214 | perror("read"); 215 | exit(1); 216 | } 217 | } 218 | 219 | uint 220 | ialloc(ushort type) 221 | { 222 | uint inum = freeinode++; 223 | struct dinode din; 224 | 225 | bzero(&din, sizeof(din)); 226 | din.type = xshort(type); 227 | din.nlink = xshort(1); 228 | din.size = xint(0); 229 | winode(inum, &din); 230 | return inum; 231 | } 232 | 233 | void 234 | balloc(int used) 235 | { 236 | uchar buf[512]; 237 | int i; 238 | 239 | printf("balloc: first %d blocks have been allocated\n", used); 240 | assert(used < 512*8); 241 | bzero(buf, 512); 242 | for(i = 0; i < used; i++){ 243 | buf[i/8] = buf[i/8] | (0x1 << (i%8)); 244 | } 245 | printf("balloc: write bitmap block at sector %zu\n", ninodes/IPB + 3); 246 | wsect(ninodes / IPB + 3, buf); 247 | } 248 | 249 | #define min(a, b) ((a) < (b) ? (a) : (b)) 250 | 251 | void 252 | iappend(uint inum, void *xp, int n) 253 | { 254 | char *p = (char*)xp; 255 | uint fbn, off, n1; 256 | struct dinode din; 257 | char buf[512]; 258 | uint indirect[NINDIRECT]; 259 | uint x; 260 | 261 | rinode(inum, &din); 262 | 263 | off = xint(din.size); 264 | while(n > 0){ 265 | fbn = off / 512; 266 | assert(fbn < MAXFILE); 267 | if(fbn < NDIRECT){ 268 | if(xint(din.addrs[fbn]) == 0){ 269 | din.addrs[fbn] = xint(freeblock++); 270 | usedblocks++; 271 | } 272 | x = xint(din.addrs[fbn]); 273 | } else { 274 | if(xint(din.addrs[NDIRECT]) == 0){ 275 | // printf("allocate indirect block\n"); 276 | din.addrs[NDIRECT] = xint(freeblock++); 277 | usedblocks++; 278 | } 279 | // printf("read indirect block\n"); 280 | rsect(xint(din.addrs[NDIRECT]), (char*)indirect); 281 | if(indirect[fbn - NDIRECT] == 0){ 282 | indirect[fbn - NDIRECT] = xint(freeblock++); 283 | usedblocks++; 284 | wsect(xint(din.addrs[NDIRECT]), (char*)indirect); 285 | } 286 | x = xint(indirect[fbn-NDIRECT]); 287 | } 288 | n1 = min(n, (fbn + 1) * 512 - off); 289 | rsect(x, buf); 290 | bcopy(p, buf + off - (fbn * 512), n1); 291 | wsect(x, buf); 292 | n -= n1; 293 | off += n1; 294 | p += n1; 295 | } 296 | din.size = xint(off); 297 | winode(inum, &din); 298 | } 299 | -------------------------------------------------------------------------------- /mp.c: -------------------------------------------------------------------------------- 1 | // Multiprocessor support 2 | // Search memory for MP description structures. 3 | // http://developer.intel.com/design/pentium/datashts/24201606.pdf 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "param.h" 8 | #include "memlayout.h" 9 | #include "mp.h" 10 | #include "x86.h" 11 | #include "mmu.h" 12 | #include "proc.h" 13 | 14 | struct cpu cpus[NCPU]; 15 | static struct cpu *bcpu; 16 | int ismp; 17 | int ncpu; 18 | uchar ioapicid; 19 | 20 | int 21 | mpbcpu(void) 22 | { 23 | return bcpu-cpus; 24 | } 25 | 26 | static uchar 27 | sum(uchar *addr, int len) 28 | { 29 | int i, sum; 30 | 31 | sum = 0; 32 | for(i=0; iphysaddr == 0) 87 | return 0; 88 | conf = (struct mpconf*) p2v((uint) mp->physaddr); 89 | if(memcmp(conf, "PCMP", 4) != 0) 90 | return 0; 91 | if(conf->version != 1 && conf->version != 4) 92 | return 0; 93 | if(sum((uchar*)conf, conf->length) != 0) 94 | return 0; 95 | *pmp = mp; 96 | return conf; 97 | } 98 | 99 | void 100 | mpinit(void) 101 | { 102 | uchar *p, *e; 103 | struct mp *mp; 104 | struct mpconf *conf; 105 | struct mpproc *proc; 106 | struct mpioapic *ioapic; 107 | 108 | bcpu = &cpus[0]; 109 | if((conf = mpconfig(&mp)) == 0) 110 | return; 111 | ismp = 1; 112 | lapic = (uint*)conf->lapicaddr; 113 | for(p=(uchar*)(conf+1), e=(uchar*)conf+conf->length; papicid){ 118 | cprintf("mpinit: ncpu=%d apicid=%d\n", ncpu, proc->apicid); 119 | ismp = 0; 120 | } 121 | if(proc->flags & MPBOOT) 122 | bcpu = &cpus[ncpu]; 123 | cpus[ncpu].id = ncpu; 124 | ncpu++; 125 | p += sizeof(struct mpproc); 126 | continue; 127 | case MPIOAPIC: 128 | ioapic = (struct mpioapic*)p; 129 | ioapicid = ioapic->apicno; 130 | p += sizeof(struct mpioapic); 131 | continue; 132 | case MPBUS: 133 | case MPIOINTR: 134 | case MPLINTR: 135 | p += 8; 136 | continue; 137 | default: 138 | cprintf("mpinit: unknown config type %x\n", *p); 139 | ismp = 0; 140 | } 141 | } 142 | if(!ismp){ 143 | // Didn't like what we found; fall back to no MP. 144 | ncpu = 1; 145 | lapic = 0; 146 | ioapicid = 0; 147 | return; 148 | } 149 | 150 | if(mp->imcrp){ 151 | // Bochs doesn't support IMCR, so this doesn't run on Bochs. 152 | // But it would on real hardware. 153 | outb(0x22, 0x70); // Select IMCR 154 | outb(0x23, inb(0x23) | 1); // Mask external interrupts. 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /mp.h: -------------------------------------------------------------------------------- 1 | // See MultiProcessor Specification Version 1.[14] 2 | 3 | struct mp { // floating pointer 4 | uchar signature[4]; // "_MP_" 5 | void *physaddr; // phys addr of MP config table 6 | uchar length; // 1 7 | uchar specrev; // [14] 8 | uchar checksum; // all bytes must add up to 0 9 | uchar type; // MP system config type 10 | uchar imcrp; 11 | uchar reserved[3]; 12 | }; 13 | 14 | struct mpconf { // configuration table header 15 | uchar signature[4]; // "PCMP" 16 | ushort length; // total table length 17 | uchar version; // [14] 18 | uchar checksum; // all bytes must add up to 0 19 | uchar product[20]; // product id 20 | uint *oemtable; // OEM table pointer 21 | ushort oemlength; // OEM table length 22 | ushort entry; // entry count 23 | uint *lapicaddr; // address of local APIC 24 | ushort xlength; // extended table length 25 | uchar xchecksum; // extended table checksum 26 | uchar reserved; 27 | }; 28 | 29 | struct mpproc { // processor table entry 30 | uchar type; // entry type (0) 31 | uchar apicid; // local APIC id 32 | uchar version; // local APIC verison 33 | uchar flags; // CPU flags 34 | #define MPBOOT 0x02 // This proc is the bootstrap processor. 35 | uchar signature[4]; // CPU signature 36 | uint feature; // feature flags from CPUID instruction 37 | uchar reserved[8]; 38 | }; 39 | 40 | struct mpioapic { // I/O APIC table entry 41 | uchar type; // entry type (2) 42 | uchar apicno; // I/O APIC id 43 | uchar version; // I/O APIC version 44 | uchar flags; // I/O APIC flags 45 | uint *addr; // I/O APIC address 46 | }; 47 | 48 | // Table entry types 49 | #define MPPROC 0x00 // One per processor 50 | #define MPBUS 0x01 // One per bus 51 | #define MPIOAPIC 0x02 // One per I/O APIC 52 | #define MPIOINTR 0x03 // One per bus interrupt source 53 | #define MPLINTR 0x04 // One per system interrupt source 54 | 55 | -------------------------------------------------------------------------------- /param.h: -------------------------------------------------------------------------------- 1 | #define NPROC 64 // maximum number of processes 2 | #define KSTACKSIZE 4096 // size of per-process kernel stack 3 | #define NCPU 8 // maximum number of CPUs 4 | #define NOFILE 16 // open files per process 5 | #define NFILE 100 // open files per system 6 | #define NBUF 10 // size of disk block cache 7 | #define NINODE 50 // maximum number of active i-nodes 8 | #define NDEV 10 // maximum major device number 9 | #define ROOTDEV 1 // device number of file system root disk 10 | #define MAXARG 32 // max exec arguments 11 | #define LOGSIZE 10 // max data sectors in on-disk log 12 | 13 | -------------------------------------------------------------------------------- /picirq.c: -------------------------------------------------------------------------------- 1 | // Intel 8259A programmable interrupt controllers. 2 | 3 | #include "types.h" 4 | #include "x86.h" 5 | #include "traps.h" 6 | 7 | // I/O Addresses of the two programmable interrupt controllers 8 | #define IO_PIC1 0x20 // Master (IRQs 0-7) 9 | #define IO_PIC2 0xA0 // Slave (IRQs 8-15) 10 | 11 | #define IRQ_SLAVE 2 // IRQ at which slave connects to master 12 | 13 | // Current IRQ mask. 14 | // Initial IRQ mask has interrupt 2 enabled (for slave 8259A). 15 | static ushort irqmask = 0xFFFF & ~(1<> 8); 23 | } 24 | 25 | void 26 | picenable(int irq) 27 | { 28 | picsetmask(irqmask & ~(1<readopen = 1; 33 | p->writeopen = 1; 34 | p->nwrite = 0; 35 | p->nread = 0; 36 | initlock(&p->lock, "pipe"); 37 | (*f0)->type = FD_PIPE; 38 | (*f0)->readable = 1; 39 | (*f0)->writable = 0; 40 | (*f0)->pipe = p; 41 | (*f1)->type = FD_PIPE; 42 | (*f1)->readable = 0; 43 | (*f1)->writable = 1; 44 | (*f1)->pipe = p; 45 | return 0; 46 | 47 | //PAGEBREAK: 20 48 | bad: 49 | if(p) 50 | kfree((char*)p); 51 | if(*f0) 52 | fileclose(*f0); 53 | if(*f1) 54 | fileclose(*f1); 55 | return -1; 56 | } 57 | 58 | void 59 | pipeclose(struct pipe *p, int writable) 60 | { 61 | acquire(&p->lock); 62 | if(writable){ 63 | p->writeopen = 0; 64 | wakeup(&p->nread); 65 | } else { 66 | p->readopen = 0; 67 | wakeup(&p->nwrite); 68 | } 69 | if(p->readopen == 0 && p->writeopen == 0){ 70 | release(&p->lock); 71 | kfree((char*)p); 72 | } else 73 | release(&p->lock); 74 | } 75 | 76 | //PAGEBREAK: 40 77 | int 78 | pipewrite(struct pipe *p, char *addr, int n) 79 | { 80 | int i; 81 | 82 | acquire(&p->lock); 83 | for(i = 0; i < n; i++){ 84 | while(p->nwrite == p->nread + PIPESIZE){ //DOC: pipewrite-full 85 | if(p->readopen == 0 || proc->killed){ 86 | release(&p->lock); 87 | return -1; 88 | } 89 | wakeup(&p->nread); 90 | sleep(&p->nwrite, &p->lock); //DOC: pipewrite-sleep 91 | } 92 | p->data[p->nwrite++ % PIPESIZE] = addr[i]; 93 | } 94 | wakeup(&p->nread); //DOC: pipewrite-wakeup1 95 | release(&p->lock); 96 | return n; 97 | } 98 | 99 | int 100 | piperead(struct pipe *p, char *addr, int n) 101 | { 102 | int i; 103 | 104 | acquire(&p->lock); 105 | while(p->nread == p->nwrite && p->writeopen){ //DOC: pipe-empty 106 | if(proc->killed){ 107 | release(&p->lock); 108 | return -1; 109 | } 110 | sleep(&p->nread, &p->lock); //DOC: piperead-sleep 111 | } 112 | for(i = 0; i < n; i++){ //DOC: piperead-copy 113 | if(p->nread == p->nwrite) 114 | break; 115 | addr[i] = p->data[p->nread++ % PIPESIZE]; 116 | } 117 | wakeup(&p->nwrite); //DOC: piperead-wakeup 118 | release(&p->lock); 119 | return i; 120 | } 121 | -------------------------------------------------------------------------------- /pr.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use POSIX qw(strftime); 4 | 5 | if($ARGV[0] eq "-h"){ 6 | shift @ARGV; 7 | $h = $ARGV[0]; 8 | shift @ARGV; 9 | }else{ 10 | $h = $ARGV[0]; 11 | } 12 | 13 | $page = 0; 14 | $now = strftime "%b %e %H:%M %Y", localtime; 15 | 16 | @lines = <>; 17 | for($i=0; $i<@lines; $i+=50){ 18 | print "\n\n"; 19 | ++$page; 20 | print "$now $h Page $page\n"; 21 | print "\n\n"; 22 | for($j=$i; $j<@lines && $j<$i +50; $j++){ 23 | $lines[$j] =~ s!//DOC.*!!; 24 | print $lines[$j]; 25 | } 26 | for(; $j<$i+50; $j++){ 27 | print "\n"; 28 | } 29 | $sheet = ""; 30 | if($lines[$i] =~ /^([0-9][0-9])[0-9][0-9] /){ 31 | $sheet = "Sheet $1"; 32 | } 33 | print "\n\n"; 34 | print "$sheet\n"; 35 | print "\n\n"; 36 | } 37 | -------------------------------------------------------------------------------- /printf.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | static void 6 | putc(int fd, char c) 7 | { 8 | write(fd, &c, 1); 9 | } 10 | 11 | static void 12 | printint(int fd, int xx, int base, int sgn) 13 | { 14 | static char digits[] = "0123456789ABCDEF"; 15 | char buf[16]; 16 | int i, neg; 17 | uint x; 18 | 19 | neg = 0; 20 | if(sgn && xx < 0){ 21 | neg = 1; 22 | x = -xx; 23 | } else { 24 | x = xx; 25 | } 26 | 27 | i = 0; 28 | do{ 29 | buf[i++] = digits[x % base]; 30 | }while((x /= base) != 0); 31 | if(neg) 32 | buf[i++] = '-'; 33 | 34 | while(--i >= 0) 35 | putc(fd, buf[i]); 36 | } 37 | 38 | // Print to the given fd. Only understands %d, %x, %p, %s. 39 | void 40 | printf(int fd, char *fmt, ...) 41 | { 42 | char *s; 43 | int c, i, state; 44 | uint *ap; 45 | 46 | state = 0; 47 | ap = (uint*)(void*)&fmt + 1; 48 | for(i = 0; fmt[i]; i++){ 49 | c = fmt[i] & 0xff; 50 | if(state == 0){ 51 | if(c == '%'){ 52 | state = '%'; 53 | } else { 54 | putc(fd, c); 55 | } 56 | } else if(state == '%'){ 57 | if(c == 'd'){ 58 | printint(fd, *ap, 10, 1); 59 | ap++; 60 | } else if(c == 'x' || c == 'p'){ 61 | printint(fd, *ap, 16, 0); 62 | ap++; 63 | } else if(c == 's'){ 64 | s = (char*)*ap; 65 | ap++; 66 | if(s == 0) 67 | s = "(null)"; 68 | while(*s != 0){ 69 | putc(fd, *s); 70 | s++; 71 | } 72 | } else if(c == 'c'){ 73 | putc(fd, *ap); 74 | ap++; 75 | } else if(c == '%'){ 76 | putc(fd, c); 77 | } else { 78 | // Unknown % sequence. Print it to draw attention. 79 | putc(fd, '%'); 80 | putc(fd, c); 81 | } 82 | state = 0; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /printpcs: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Decode the symbols from a panic EIP list 4 | 5 | # Find a working addr2line 6 | for p in i386-jos-elf-addr2line addr2line; do 7 | if which $p 2>&1 >/dev/null && \ 8 | $p -h 2>&1 | grep -q '\belf32-i386\b'; then 9 | break 10 | fi 11 | done 12 | 13 | # Enable as much pretty-printing as this addr2line can do 14 | $p $($p -h | grep ' -[aipsf] ' | awk '{print $1}') -e kernel "$@" 15 | -------------------------------------------------------------------------------- /proc.h: -------------------------------------------------------------------------------- 1 | // Segments in proc->gdt. 2 | #define NSEGS 7 3 | 4 | // Per-CPU state 5 | struct cpu { 6 | uchar id; // Local APIC ID; index into cpus[] below 7 | struct context *scheduler; // swtch() here to enter scheduler 8 | struct taskstate ts; // Used by x86 to find stack for interrupt 9 | struct segdesc gdt[NSEGS]; // x86 global descriptor table 10 | volatile uint started; // Has the CPU started? 11 | int ncli; // Depth of pushcli nesting. 12 | int intena; // Were interrupts enabled before pushcli? 13 | 14 | // Cpu-local storage variables; see below 15 | struct cpu *cpu; 16 | struct proc *proc; // The currently-running process. 17 | }; 18 | 19 | extern struct cpu cpus[NCPU]; 20 | extern int ncpu; 21 | 22 | // Per-CPU variables, holding pointers to the 23 | // current cpu and to the current process. 24 | // The asm suffix tells gcc to use "%gs:0" to refer to cpu 25 | // and "%gs:4" to refer to proc. seginit sets up the 26 | // %gs segment register so that %gs refers to the memory 27 | // holding those two variables in the local cpu's struct cpu. 28 | // This is similar to how thread-local variables are implemented 29 | // in thread libraries such as Linux pthreads. 30 | extern struct cpu *cpu asm("%gs:0"); // &cpus[cpunum()] 31 | extern struct proc *proc asm("%gs:4"); // cpus[cpunum()].proc 32 | 33 | //PAGEBREAK: 17 34 | // Saved registers for kernel context switches. 35 | // Don't need to save all the segment registers (%cs, etc), 36 | // because they are constant across kernel contexts. 37 | // Don't need to save %eax, %ecx, %edx, because the 38 | // x86 convention is that the caller has saved them. 39 | // Contexts are stored at the bottom of the stack they 40 | // describe; the stack pointer is the address of the context. 41 | // The layout of the context matches the layout of the stack in swtch.S 42 | // at the "Switch stacks" comment. Switch doesn't save eip explicitly, 43 | // but it is on the stack and allocproc() manipulates it. 44 | struct context { 45 | uint edi; 46 | uint esi; 47 | uint ebx; 48 | uint ebp; 49 | uint eip; 50 | }; 51 | 52 | enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; 53 | 54 | // Per-process state 55 | struct proc { 56 | uint sz; // Size of process memory (bytes) 57 | pde_t* pgdir; // Page table 58 | char *kstack; // Bottom of kernel stack for this process 59 | enum procstate state; // Process state 60 | volatile int pid; // Process ID 61 | struct proc *parent; // Parent process 62 | struct trapframe *tf; // Trap frame for current syscall 63 | struct context *context; // swtch() here to run process 64 | void *chan; // If non-zero, sleeping on chan 65 | int killed; // If non-zero, have been killed 66 | struct file *ofile[NOFILE]; // Open files 67 | struct inode *cwd; // Current directory 68 | char name[16]; // Process name (debugging) 69 | }; 70 | 71 | // Process memory is laid out contiguously, low addresses first: 72 | // text 73 | // original data and bss 74 | // fixed-size stack 75 | // expandable heap 76 | -------------------------------------------------------------------------------- /rm.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | int i; 9 | 10 | if(argc < 2){ 11 | printf(2, "Usage: rm files...\n"); 12 | exit(); 13 | } 14 | 15 | for(i = 1; i < argc; i++){ 16 | if(unlink(argv[i]) < 0){ 17 | printf(2, "rm: %s failed to delete\n", argv[i]); 18 | break; 19 | } 20 | } 21 | 22 | exit(); 23 | } 24 | -------------------------------------------------------------------------------- /runoff: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo This script takes a minute to run. Be patient. 1>&2 4 | 5 | LC_CTYPE=C export LC_CTYPE 6 | 7 | # pad stdin to multiple of 120 lines 8 | pad() 9 | { 10 | awk '{print} END{for(; NR%120!=0; NR++) print ""}' 11 | } 12 | 13 | # create formatted (numbered) files 14 | mkdir -p fmt 15 | rm -f fmt/* 16 | cp README fmt 17 | files=`grep -v '^#' runoff.list | awk '{print $1}'` 18 | n=99 19 | for i in $files 20 | do 21 | ./runoff1 -n $n $i >fmt/$i 22 | nn=`tail -1 fmt/$i | sed 's/ .*//; s/^0*//'` 23 | if [ "x$nn" != x ]; then 24 | n=$nn 25 | fi 26 | done 27 | 28 | # create table of contents 29 | cat toc.hdr >fmt/toc 30 | pr -e8 -t runoff.list | awk ' 31 | /^[a-z0-9]/ { 32 | s=$0 33 | f="fmt/"$1 34 | getline"fmt/tocdata" 39 | next 40 | } 41 | { 42 | print 43 | }' | pr -3 -t >>fmt/toc 44 | cat toc.ftr >>fmt/toc 45 | 46 | # check for bad alignments 47 | perl -e ' 48 | $leftwarn = 0; 49 | while(<>){ 50 | chomp; 51 | s!#.*!!; 52 | s!\s+! !g; 53 | s! +$!!; 54 | next if /^$/; 55 | 56 | if(/TOC: (\d+) (.*)/){ 57 | $toc{$2} = $1; 58 | next; 59 | } 60 | 61 | if(/sheet1: (left|right)$/){ 62 | print STDERR "assuming that sheet 1 is a $1 page. double-check!\n"; 63 | $left = $1 eq "left" ? "13579" : "02468"; 64 | $right = $1 eq "left" ? "02468" : "13579"; 65 | next; 66 | } 67 | 68 | if(/even: (.*)/){ 69 | $file = $1; 70 | if(!defined($toc{$file})){ 71 | print STDERR "Have no toc for $file\n"; 72 | next; 73 | } 74 | if($toc{$file} =~ /^\d\d[^0]/){ 75 | print STDERR "$file does not start on a fresh page.\n"; 76 | } 77 | next; 78 | } 79 | 80 | if(/odd: (.*)/){ 81 | $file = $1; 82 | if(!defined($toc{$file})){ 83 | print STDERR "Have no toc for $file\n"; 84 | next; 85 | } 86 | if($toc{$file} !~ /^\d\d5/){ 87 | print STDERR "$file does not start on a second half page.\n"; 88 | } 89 | next; 90 | } 91 | 92 | if(/(left|right): (.*)/){ 93 | $what = $1; 94 | $file = $2; 95 | if(!defined($toc{$file})){ 96 | print STDERR "Have no toc for $file\n"; 97 | next; 98 | } 99 | if($what eq "left" && !($toc{$file} =~ /^\d[$left][05]/)){ 100 | print STDERR "$file does not start on a left page [$toc{$file}]\n"; 101 | } 102 | # why does this not work if I inline $x in the if? 103 | $x = ($toc{$file} =~ /^\d[$right][05]/); 104 | if($what eq "right" && !$x){ 105 | print STDERR "$file does not start on a right page [$toc{$file}] [$x]\n"; 106 | } 107 | next; 108 | } 109 | 110 | print STDERR "Unknown spec: $_\n"; 111 | } 112 | ' fmt/tocdata runoff.spec 113 | 114 | # make definition list 115 | cd fmt 116 | perl -e ' 117 | while(<>) { 118 | chomp; 119 | 120 | s!//.*!!; 121 | s!/\*([^*]|[*][^/])*\*/!!g; 122 | s!\s! !g; 123 | s! +$!!; 124 | 125 | # look for declarations like char* x; 126 | if (/^[0-9]+ typedef .* u(int|short|long|char);/) { 127 | next; 128 | } 129 | if (/^[0-9]+ extern/) { 130 | next; 131 | } 132 | if (/^[0-9]+ struct [a-zA-Z0-9_]+;/) { 133 | next; 134 | } 135 | if (/^([0-9]+) #define +([A-za-z0-9_]+) +?\(.*/) { 136 | print "$1 $2\n" 137 | } 138 | elsif (/^([0-9]+) #define +([A-Za-z0-9_]+) +([^ ]+)/) { 139 | print "$1 $2 $3\n"; 140 | } 141 | elsif (/^([0-9]+) #define +([A-Za-z0-9_]+)/) { 142 | print "$1 $2\n"; 143 | } 144 | 145 | if(/^^([0-9]+) \.globl ([a-zA-Z0-9_]+)/){ 146 | $isglobl{$2} = 1; 147 | } 148 | if(/^^([0-9]+) ([a-zA-Z0-9_]+):$/ && $isglobl{$2}){ 149 | print "$1 $2\n"; 150 | } 151 | 152 | if (/\(/) { 153 | next; 154 | } 155 | 156 | if (/^([0-9]+) (((static|struct|extern|union|enum) +)*([A-Za-z0-9_]+))( .*)? +([A-Za-z_][A-Za-z0-9_]*)(,|;|=| =)/) { 157 | print "$1 $7\n"; 158 | } 159 | 160 | elsif(/^([0-9]+) (enum|struct|union) +([A-Za-z0-9_]+) +{/){ 161 | print "$1 $3\n"; 162 | } 163 | # TODO: enum members 164 | } 165 | ' $files >defs 166 | 167 | (for i in $files 168 | do 169 | case "$i" in 170 | *.S) 171 | cat $i | sed 's;#.*;;; s;//.*;;;' 172 | ;; 173 | *) 174 | cat $i | sed 's;//.*;;; s;"([^"\\]|\\.)*";;;' 175 | esac 176 | done 177 | ) >alltext 178 | 179 | perl -n -e 'print if s/^([0-9]+ [a-zA-Z0-9_]+)\(.*$/\1/;' alltext | 180 | egrep -v ' (STUB|usage|main|if|for)$' >>defs 181 | #perl -n -e 'print if s/^([0-9]+) STUB\(([a-zA-Z0-9_]+)\)$/\1 \2/;' alltext \ 182 | # >>defs 183 | ( 184 | >s.defs 185 | 186 | # make reference list 187 | for i in `awk '{print $2}' defs | sort -f | uniq` 188 | do 189 | defs=`egrep '^[0-9]+ '$i'( |$)' defs | awk '{print $1}'` 190 | echo $i $defs >>s.defs 191 | uses=`egrep -h '([^a-zA-Z_0-9])'$i'($|[^a-zA-Z_0-9])' alltext | awk '{print $1}'` 192 | if [ "x$defs" != "x$uses" ]; then 193 | echo $i $defs 194 | echo $uses |fmt -29 | sed 's/^/ /' 195 | # else 196 | # echo $i defined but not used >&2 197 | fi 198 | done 199 | ) >refs 200 | 201 | # build defs list 202 | awk ' 203 | { 204 | printf("%04d %s\n", $2, $1); 205 | for(i=3; i<=NF; i++) 206 | printf("%04d \" \n", $i); 207 | } 208 | ' s.defs > t.defs 209 | 210 | # format the whole thing 211 | ( 212 | ../pr.pl README 213 | ../pr.pl -h "table of contents" toc 214 | # pr -t -2 t.defs | ../pr.pl -h "definitions" | pad 215 | pr -t -l50 -2 refs | ../pr.pl -h "cross-references" | pad 216 | # pr.pl -h "definitions" -2 t.defs | pad 217 | # pr.pl -h "cross-references" -2 refs | pad 218 | for i in $files 219 | do 220 | ../pr.pl -h "xv6/$i" $i 221 | done 222 | ) | mpage -m50t50b -o -bLetter -T -t -2 -FCourier -L60 >all.ps 223 | grep Pages: all.ps 224 | 225 | # if we have the nice font, use it 226 | nicefont=LucidaSans-Typewriter83 227 | if [ ! -f ../$nicefont ] 228 | then 229 | if git cat-file blob font:$nicefont > ../$nicefont~; then 230 | mv ../$nicefont~ ../$nicefont 231 | fi 232 | fi 233 | if [ -f ../$nicefont ] 234 | then 235 | echo nicefont 236 | (sed 1q all.ps; cat ../$nicefont; sed "1d; s/Courier/$nicefont/" all.ps) >allf.ps 237 | else 238 | echo ugly font! 239 | cp all.ps allf.ps 240 | fi 241 | ps2pdf allf.ps ../xv6.pdf 242 | cd .. 243 | pdftops xv6.pdf xv6.ps 244 | -------------------------------------------------------------------------------- /runoff.list: -------------------------------------------------------------------------------- 1 | # basic headers 2 | types.h 3 | param.h 4 | memlayout.h 5 | defs.h 6 | x86.h 7 | asm.h 8 | mmu.h 9 | elf.h 10 | 11 | # entering xv6 12 | entry.S 13 | entryother.S 14 | main.c 15 | 16 | # locks 17 | spinlock.h 18 | spinlock.c 19 | 20 | # processes 21 | vm.c 22 | proc.h 23 | proc.c 24 | swtch.S 25 | kalloc.c 26 | 27 | # system calls 28 | traps.h 29 | vectors.pl 30 | trapasm.S 31 | trap.c 32 | syscall.h 33 | syscall.c 34 | sysproc.c 35 | 36 | # file system 37 | buf.h 38 | fcntl.h 39 | stat.h 40 | fs.h 41 | file.h 42 | ide.c 43 | bio.c 44 | log.c 45 | fs.c 46 | file.c 47 | sysfile.c 48 | exec.c 49 | 50 | # pipes 51 | pipe.c 52 | 53 | # string operations 54 | string.c 55 | 56 | # low-level hardware 57 | mp.h 58 | mp.c 59 | lapic.c 60 | ioapic.c 61 | picirq.c 62 | kbd.h 63 | kbd.c 64 | console.c 65 | timer.c 66 | uart.c 67 | 68 | # user-level 69 | initcode.S 70 | usys.S 71 | init.c 72 | sh.c 73 | 74 | # bootloader 75 | bootasm.S 76 | bootmain.c 77 | 78 | -------------------------------------------------------------------------------- /runoff.spec: -------------------------------------------------------------------------------- 1 | # Is sheet 01 (after the TOC) a left sheet or a right sheet? 2 | sheet1: right 3 | 4 | # "left" and "right" specify which page of a two-page spread a file 5 | # must start on. "left" means that a file must start on the first of 6 | # the two pages. "right" means it must start on the second of the two 7 | # pages. The file may start in either column. 8 | # 9 | # "even" and "odd" specify which column a file must start on. "even" 10 | # means it must start in the left of the two columns (00). "odd" means it 11 | # must start in the right of the two columns (50). 12 | # 13 | # You'd think these would be the other way around. 14 | 15 | # types.h either 16 | # param.h either 17 | # defs.h either 18 | # x86.h either 19 | # asm.h either 20 | # mmu.h either 21 | # elf.h either 22 | # mp.h either 23 | 24 | even: entry.S # mild preference 25 | even: entryother.S # mild preference 26 | even: main.c 27 | # mp.c don't care at all 28 | # even: initcode.S 29 | # odd: init.c 30 | 31 | left: spinlock.h 32 | even: spinlock.h 33 | 34 | # This gets struct proc and allocproc on the same spread 35 | left: proc.h 36 | even: proc.h 37 | 38 | # goal is to have two action-packed 2-page spreads, 39 | # one with 40 | # userinit growproc fork exit wait 41 | # and another with 42 | # scheduler sched yield forkret sleep wakeup1 wakeup 43 | right: proc.c # VERY important 44 | even: proc.c # VERY important 45 | 46 | # A few more action packed spreads 47 | # page table creation and process loading 48 | # walkpgdir mappages setupkvm switch[ku]vm inituvm (loaduvm) 49 | # process memory management 50 | # allocuvm deallocuvm freevm 51 | left: vm.c 52 | 53 | even: kalloc.c # mild preference 54 | 55 | # syscall.h either 56 | # trapasm.S either 57 | # traps.h either 58 | # even: trap.c 59 | # vectors.pl either 60 | # syscall.c either 61 | # sysproc.c either 62 | 63 | # buf.h either 64 | # dev.h either 65 | # fcntl.h either 66 | # stat.h either 67 | # file.h either 68 | # fs.h either 69 | # fsvar.h either 70 | # left: ide.c # mild preference 71 | even: ide.c 72 | # odd: bio.c 73 | 74 | # log.c fits nicely in a spread 75 | even: log.c 76 | left: log.c 77 | 78 | # with fs.c starting on 2nd column of a left page, we get these 2-page spreads: 79 | # ialloc iupdate iget idup ilock iunlock iput iunlockput 80 | # bmap itrunc stati readi writei 81 | # namecmp dirlookup dirlink skipelem namex namei 82 | # fileinit filealloc filedup fileclose filestat fileread filewrite 83 | # starting on 2nd column of a right page is not terrible either 84 | odd: fs.c # VERY important 85 | left: fs.c # mild preference 86 | # file.c either 87 | # exec.c either 88 | # sysfile.c either 89 | 90 | # Mild preference, but makes spreads of mp.c, lapic.c, and ioapic.c+picirq.c 91 | even: mp.c 92 | left: mp.c 93 | 94 | # even: pipe.c # mild preference 95 | # string.c either 96 | # left: kbd.h # mild preference 97 | even: kbd.h 98 | even: console.c 99 | odd: sh.c 100 | 101 | even: bootasm.S # mild preference 102 | even: bootmain.c # mild preference 103 | -------------------------------------------------------------------------------- /runoff1: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | $n = 0; 4 | $v = 0; 5 | if($ARGV[0] eq "-v") { 6 | $v = 1; 7 | shift @ARGV; 8 | } 9 | if($ARGV[0] eq "-n") { 10 | $n = $ARGV[1]; 11 | shift @ARGV; 12 | shift @ARGV; 13 | } 14 | $n = int(($n+49)/50)*50 - 1; 15 | 16 | $file = $ARGV[0]; 17 | @lines = <>; 18 | $linenum = 0; 19 | foreach (@lines) { 20 | $linenum++; 21 | chomp; 22 | s/\s+$//; 23 | if(length() >= 75){ 24 | print STDERR "$file:$linenum: line too long\n"; 25 | } 26 | } 27 | @outlines = (); 28 | $nextout = 0; 29 | 30 | for($i=0; $i<@lines; ){ 31 | # Skip leading blank lines. 32 | $i++ while $i<@lines && $lines[$i] =~ /^$/; 33 | last if $i>=@lines; 34 | 35 | # If the rest of the file fits, use the whole thing. 36 | if(@lines <= $i+50 && !grep { /PAGEBREAK/ } @lines){ 37 | $breakbefore = @lines; 38 | }else{ 39 | # Find a good next page break; 40 | # Hope for end of function. 41 | # but settle for a blank line (but not first blank line 42 | # in function, which comes after variable declarations). 43 | $breakbefore = $i; 44 | $lastblank = $i; 45 | $sawbrace = 0; 46 | $breaksize = 15; # 15 lines to get to function 47 | for($j=$i; $j<$i+50 && $j < @lines; $j++){ 48 | if($lines[$j] =~ /PAGEBREAK!/){ 49 | $lines[$j] = ""; 50 | $breakbefore = $j; 51 | $breaksize = 100; 52 | last; 53 | } 54 | if($lines[$j] =~ /PAGEBREAK:\s*([0-9]+)/){ 55 | $breaksize = $1; 56 | $breakbefore = $j; 57 | $lines[$j] = ""; 58 | } 59 | if($lines[$j] =~ /^};?$/){ 60 | $breakbefore = $j+1; 61 | $breaksize = 15; 62 | } 63 | if($lines[$j] =~ /^{$/){ 64 | $sawbrace = 1; 65 | } 66 | if($lines[$j] =~ /^$/){ 67 | if($sawbrace){ 68 | $sawbrace = 0; 69 | }else{ 70 | $lastblank = $j; 71 | } 72 | } 73 | } 74 | if($j<@lines && $lines[$j] =~ /^$/){ 75 | $lastblank = $j; 76 | } 77 | 78 | # If we are not putting enough on a page, try a blank line. 79 | if($breakbefore - $i < 50 - $breaksize && $lastblank > $breakbefore && $lastblank >= $i+50 - 5){ 80 | if($v){ 81 | print STDERR "breakbefore $breakbefore i $i breaksize $breaksize\n"; 82 | } 83 | $breakbefore = $lastblank; 84 | $breaksize = 5; # only 5 lines to get to blank line 85 | } 86 | 87 | # If we are not putting enough on a page, force a full page. 88 | if($breakbefore - $i < 50 - $breaksize && $breakbefore != @lines){ 89 | $breakbefore = $i + 50; 90 | $breakbefore = @lines if @lines < $breakbefore; 91 | } 92 | 93 | if($breakbefore < $i+2){ 94 | $breakbefore = $i+2; 95 | } 96 | } 97 | 98 | # Emit the page. 99 | $i50 = $i + 50; 100 | for(; $i<$breakbefore; $i++){ 101 | printf "%04d %s\n", ++$n, $lines[$i]; 102 | } 103 | 104 | # Finish page 105 | for($j=$i; $j<$i50; $j++){ 106 | printf "%04d \n", ++$n; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /show1: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | runoff1 "$@" | pr.pl -h "xv6/$@" | mpage -m50t50b -o -bLetter -T -t -2 -FLucidaSans-Typewriter83 -L60 >x.ps; gv --swap x.ps 4 | -------------------------------------------------------------------------------- /sign.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!"; 4 | 5 | $n = sysread(SIG, $buf, 1000); 6 | 7 | if($n > 510){ 8 | print STDERR "boot block too large: $n bytes (max 510)\n"; 9 | exit 1; 10 | } 11 | 12 | print STDERR "boot block is $n bytes (max 510)\n"; 13 | 14 | $buf .= "\0" x (510-$n); 15 | $buf .= "\x55\xAA"; 16 | 17 | open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!"; 18 | print SIG $buf; 19 | close SIG; 20 | -------------------------------------------------------------------------------- /sleep1.p: -------------------------------------------------------------------------------- 1 | /* 2 | This file defines a Promela model for xv6's 3 | acquire, release, sleep, and wakeup, along with 4 | a model of a simple producer/consumer queue. 5 | 6 | To run: 7 | spinp sleep1.p 8 | 9 | (You may need to install Spin, available at http://spinroot.com/.) 10 | 11 | After a successful run spin prints something like: 12 | 13 | unreached in proctype consumer 14 | (0 of 37 states) 15 | unreached in proctype producer 16 | (0 of 23 states) 17 | 18 | After an unsuccessful run, the spinp script prints 19 | an execution trace that causes a deadlock. 20 | 21 | The safe body of producer reads: 22 | 23 | acquire(lk); 24 | x = value; value = x + 1; x = 0; 25 | wakeup(0); 26 | release(lk); 27 | i = i + 1; 28 | 29 | If this is changed to: 30 | 31 | x = value; value = x + 1; x = 0; 32 | acquire(lk); 33 | wakeup(0); 34 | release(lk); 35 | i = i + 1; 36 | 37 | then a deadlock can happen, because the non-atomic 38 | increment of value conflicts with the non-atomic 39 | decrement in consumer, causing value to have a bad value. 40 | Try this. 41 | 42 | If it is changed to: 43 | 44 | acquire(lk); 45 | x = value; value = x + 1; x = 0; 46 | release(lk); 47 | wakeup(0); 48 | i = i + 1; 49 | 50 | then nothing bad happens: it is okay to wakeup after release 51 | instead of before, although it seems morally wrong. 52 | */ 53 | 54 | #define ITER 4 55 | #define N 2 56 | 57 | bit lk; 58 | byte value; 59 | bit sleeping[N]; 60 | 61 | inline acquire(x) 62 | { 63 | atomic { x == 0; x = 1 } 64 | } 65 | 66 | inline release(x) 67 | { 68 | assert x==1; 69 | x = 0 70 | } 71 | 72 | inline sleep(cond, lk) 73 | { 74 | assert !sleeping[_pid]; 75 | if 76 | :: cond -> 77 | skip 78 | :: else -> 79 | atomic { release(lk); sleeping[_pid] = 1 }; 80 | sleeping[_pid] == 0; 81 | acquire(lk) 82 | fi 83 | } 84 | 85 | inline wakeup() 86 | { 87 | w = 0; 88 | do 89 | :: w < N -> 90 | sleeping[w] = 0; 91 | w = w + 1 92 | :: else -> 93 | break 94 | od 95 | } 96 | 97 | active[N] proctype consumer() 98 | { 99 | byte i, x; 100 | 101 | i = 0; 102 | do 103 | :: i < ITER -> 104 | acquire(lk); 105 | sleep(value > 0, lk); 106 | x = value; value = x - 1; x = 0; 107 | release(lk); 108 | i = i + 1; 109 | :: else -> 110 | break 111 | od; 112 | i = 0; 113 | skip 114 | } 115 | 116 | active[N] proctype producer() 117 | { 118 | byte i, x, w; 119 | 120 | i = 0; 121 | do 122 | :: i < ITER -> 123 | acquire(lk); 124 | x = value; value = x + 1; x = 0; 125 | release(lk); 126 | wakeup(); 127 | i = i + 1; 128 | :: else -> 129 | break 130 | od; 131 | i = 0; 132 | skip 133 | } 134 | 135 | -------------------------------------------------------------------------------- /spinlock.c: -------------------------------------------------------------------------------- 1 | // Mutual exclusion spin locks. 2 | 3 | #include "types.h" 4 | #include "defs.h" 5 | #include "param.h" 6 | #include "x86.h" 7 | #include "memlayout.h" 8 | #include "mmu.h" 9 | #include "proc.h" 10 | #include "spinlock.h" 11 | 12 | void 13 | initlock(struct spinlock *lk, char *name) 14 | { 15 | lk->name = name; 16 | lk->locked = 0; 17 | lk->cpu = 0; 18 | } 19 | 20 | // Acquire the lock. 21 | // Loops (spins) until the lock is acquired. 22 | // Holding a lock for a long time may cause 23 | // other CPUs to waste time spinning to acquire it. 24 | void 25 | acquire(struct spinlock *lk) 26 | { 27 | pushcli(); // disable interrupts to avoid deadlock. 28 | if(holding(lk)) 29 | panic("acquire"); 30 | 31 | // The xchg is atomic. 32 | // It also serializes, so that reads after acquire are not 33 | // reordered before it. 34 | while(xchg(&lk->locked, 1) != 0) 35 | ; 36 | 37 | // Record info about lock acquisition for debugging. 38 | lk->cpu = cpu; 39 | getcallerpcs(&lk, lk->pcs); 40 | } 41 | 42 | // Release the lock. 43 | void 44 | release(struct spinlock *lk) 45 | { 46 | if(!holding(lk)) 47 | panic("release"); 48 | 49 | lk->pcs[0] = 0; 50 | lk->cpu = 0; 51 | 52 | // The xchg serializes, so that reads before release are 53 | // not reordered after it. The 1996 PentiumPro manual (Volume 3, 54 | // 7.2) says reads can be carried out speculatively and in 55 | // any order, which implies we need to serialize here. 56 | // But the 2007 Intel 64 Architecture Memory Ordering White 57 | // Paper says that Intel 64 and IA-32 will not move a load 58 | // after a store. So lock->locked = 0 would work here. 59 | // The xchg being asm volatile ensures gcc emits it after 60 | // the above assignments (and after the critical section). 61 | xchg(&lk->locked, 0); 62 | 63 | popcli(); 64 | } 65 | 66 | // Record the current call stack in pcs[] by following the %ebp chain. 67 | void 68 | getcallerpcs(void *v, uint pcs[]) 69 | { 70 | uint *ebp; 71 | int i; 72 | 73 | ebp = (uint*)v - 2; 74 | for(i = 0; i < 10; i++){ 75 | if(ebp == 0 || ebp < (uint*)KERNBASE || ebp == (uint*)0xffffffff) 76 | break; 77 | pcs[i] = ebp[1]; // saved %eip 78 | ebp = (uint*)ebp[0]; // saved %ebp 79 | } 80 | for(; i < 10; i++) 81 | pcs[i] = 0; 82 | } 83 | 84 | // Check whether this cpu is holding the lock. 85 | int 86 | holding(struct spinlock *lock) 87 | { 88 | return lock->locked && lock->cpu == cpu; 89 | } 90 | 91 | 92 | // Pushcli/popcli are like cli/sti except that they are matched: 93 | // it takes two popcli to undo two pushcli. Also, if interrupts 94 | // are off, then pushcli, popcli leaves them off. 95 | 96 | void 97 | pushcli(void) 98 | { 99 | int eflags; 100 | 101 | eflags = readeflags(); 102 | cli(); 103 | if(cpu->ncli++ == 0) 104 | cpu->intena = eflags & FL_IF; 105 | } 106 | 107 | void 108 | popcli(void) 109 | { 110 | if(readeflags()&FL_IF) 111 | panic("popcli - interruptible"); 112 | if(--cpu->ncli < 0) 113 | panic("popcli"); 114 | if(cpu->ncli == 0 && cpu->intena) 115 | sti(); 116 | } 117 | 118 | -------------------------------------------------------------------------------- /spinlock.h: -------------------------------------------------------------------------------- 1 | // Mutual exclusion lock. 2 | struct spinlock { 3 | uint locked; // Is the lock held? 4 | 5 | // For debugging: 6 | char *name; // Name of lock. 7 | struct cpu *cpu; // The cpu holding the lock. 8 | uint pcs[10]; // The call stack (an array of program counters) 9 | // that locked the lock. 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /spinp: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ $# != 1 ] || [ ! -f "$1" ]; then 4 | echo 'usage: spinp file.p' 1>&2 5 | exit 1 6 | fi 7 | 8 | rm -f $1.trail 9 | spin -a $1 || exit 1 10 | cc -DSAFETY -DREACH -DMEMLIM=500 -o pan pan.c 11 | pan -i 12 | rm pan.* pan 13 | if [ -f $1.trail ]; then 14 | spin -t -p $1 15 | fi 16 | 17 | -------------------------------------------------------------------------------- /stat.h: -------------------------------------------------------------------------------- 1 | #define T_DIR 1 // Directory 2 | #define T_FILE 2 // File 3 | #define T_DEV 3 // Device 4 | 5 | struct stat { 6 | short type; // Type of file 7 | int dev; // File system's disk device 8 | uint ino; // Inode number 9 | short nlink; // Number of links to file 10 | uint size; // Size of file in bytes 11 | }; 12 | -------------------------------------------------------------------------------- /stressfs.c: -------------------------------------------------------------------------------- 1 | // Demonstrate that moving the "acquire" in iderw after the loop that 2 | // appends to the idequeue results in a race. 3 | 4 | // For this to work, you should also add a spin within iderw's 5 | // idequeue traversal loop. Adding the following demonstrated a panic 6 | // after about 5 runs of stressfs in QEMU on a 2.1GHz CPU: 7 | // for (i = 0; i < 40000; i++) 8 | // asm volatile(""); 9 | 10 | #include "types.h" 11 | #include "stat.h" 12 | #include "user.h" 13 | #include "fs.h" 14 | #include "fcntl.h" 15 | 16 | int 17 | main(int argc, char *argv[]) 18 | { 19 | int fd, i; 20 | char path[] = "stressfs0"; 21 | char data[512]; 22 | 23 | printf(1, "stressfs starting\n"); 24 | memset(data, 'a', sizeof(data)); 25 | 26 | for(i = 0; i < 4; i++) 27 | if(fork() > 0) 28 | break; 29 | 30 | printf(1, "write %d\n", i); 31 | 32 | path[8] += i; 33 | fd = open(path, O_CREATE | O_RDWR); 34 | for(i = 0; i < 20; i++) 35 | // printf(fd, "%d\n", i); 36 | write(fd, data, sizeof(data)); 37 | close(fd); 38 | 39 | printf(1, "read\n"); 40 | 41 | fd = open(path, O_RDONLY); 42 | for (i = 0; i < 20; i++) 43 | read(fd, data, sizeof(data)); 44 | close(fd); 45 | 46 | wait(); 47 | 48 | exit(); 49 | } 50 | -------------------------------------------------------------------------------- /string.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "x86.h" 3 | 4 | void* 5 | memset(void *dst, int c, uint n) 6 | { 7 | if ((int)dst%4 == 0 && n%4 == 0){ 8 | c &= 0xFF; 9 | stosl(dst, (c<<24)|(c<<16)|(c<<8)|c, n/4); 10 | } else 11 | stosb(dst, c, n); 12 | return dst; 13 | } 14 | 15 | int 16 | memcmp(const void *v1, const void *v2, uint n) 17 | { 18 | const uchar *s1, *s2; 19 | 20 | s1 = v1; 21 | s2 = v2; 22 | while(n-- > 0){ 23 | if(*s1 != *s2) 24 | return *s1 - *s2; 25 | s1++, s2++; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | void* 32 | memmove(void *dst, const void *src, uint n) 33 | { 34 | const char *s; 35 | char *d; 36 | 37 | s = src; 38 | d = dst; 39 | if(s < d && s + n > d){ 40 | s += n; 41 | d += n; 42 | while(n-- > 0) 43 | *--d = *--s; 44 | } else 45 | while(n-- > 0) 46 | *d++ = *s++; 47 | 48 | return dst; 49 | } 50 | 51 | // memcpy exists to placate GCC. Use memmove. 52 | void* 53 | memcpy(void *dst, const void *src, uint n) 54 | { 55 | return memmove(dst, src, n); 56 | } 57 | 58 | int 59 | strncmp(const char *p, const char *q, uint n) 60 | { 61 | while(n > 0 && *p && *p == *q) 62 | n--, p++, q++; 63 | if(n == 0) 64 | return 0; 65 | return (uchar)*p - (uchar)*q; 66 | } 67 | 68 | char* 69 | strncpy(char *s, const char *t, int n) 70 | { 71 | char *os; 72 | 73 | os = s; 74 | while(n-- > 0 && (*s++ = *t++) != 0) 75 | ; 76 | while(n-- > 0) 77 | *s++ = 0; 78 | return os; 79 | } 80 | 81 | // Like strncpy but guaranteed to NUL-terminate. 82 | char* 83 | safestrcpy(char *s, const char *t, int n) 84 | { 85 | char *os; 86 | 87 | os = s; 88 | if(n <= 0) 89 | return os; 90 | while(--n > 0 && (*s++ = *t++) != 0) 91 | ; 92 | *s = 0; 93 | return os; 94 | } 95 | 96 | int 97 | strlen(const char *s) 98 | { 99 | int n; 100 | 101 | for(n = 0; s[n]; n++) 102 | ; 103 | return n; 104 | } 105 | 106 | -------------------------------------------------------------------------------- /swtch.S: -------------------------------------------------------------------------------- 1 | # Context switch 2 | # 3 | # void swtch(struct context **old, struct context *new); 4 | # 5 | # Save current register context in old 6 | # and then load register context from new. 7 | 8 | .globl swtch 9 | swtch: 10 | movl 4(%esp), %eax 11 | movl 8(%esp), %edx 12 | 13 | # Save old callee-save registers 14 | pushl %ebp 15 | pushl %ebx 16 | pushl %esi 17 | pushl %edi 18 | 19 | # Switch stacks 20 | movl %esp, (%eax) 21 | movl %edx, %esp 22 | 23 | # Load new callee-save registers 24 | popl %edi 25 | popl %esi 26 | popl %ebx 27 | popl %ebp 28 | ret 29 | -------------------------------------------------------------------------------- /symlink.patch: -------------------------------------------------------------------------------- 1 | diff -r f8a4e40ab1d6 fs.c 2 | --- a/fs.c Thu Aug 30 14:32:06 2007 -0400 3 | +++ b/fs.c Thu Aug 30 14:29:02 2007 -0400 4 | @@ -577,12 +577,18 @@ skipelem(char *path, char *name) 5 | // If parent != 0, return the inode for the parent and copy the final 6 | // path element into name, which must have room for DIRSIZ bytes. 7 | static struct inode* 8 | -_namei(char *path, int parent, char *name) 9 | +_namei(struct inode *root, char *path, int parent, char *name, int depth) 10 | { 11 | struct inode *ip, *next; 12 | + char buf[100], tname[DIRSIZ]; 13 | + 14 | + if(depth > 5) 15 | + return 0; 16 | 17 | if(*path == '/') 18 | ip = iget(ROOTDEV, 1); 19 | + else if(root) 20 | + ip = idup(root); 21 | else 22 | ip = idup(cp->cwd); 23 | 24 | @@ -598,10 +604,24 @@ _namei(char *path, int parent, char *nam 25 | return ip; 26 | } 27 | if((next = dirlookup(ip, name, 0)) == 0){ 28 | + cprintf("did not find %s\n", name); 29 | iunlockput(ip); 30 | return 0; 31 | } 32 | - iunlockput(ip); 33 | + iunlock(ip); 34 | + ilock(next); 35 | + if(next->type == T_SYMLINK){ 36 | + if(next->size >= sizeof(buf) || readi(next, buf, 0, next->size) != next->size){ 37 | + iunlockput(next); 38 | + iput(ip); 39 | + return 0; 40 | + } 41 | + buf[next->size] = 0; 42 | + iunlockput(next); 43 | + next = _namei(ip, buf, 0, tname, depth+1); 44 | + }else 45 | + iunlock(next); 46 | + iput(ip); 47 | ip = next; 48 | } 49 | if(parent){ 50 | @@ -615,11 +635,11 @@ namei(char *path) 51 | namei(char *path) 52 | { 53 | char name[DIRSIZ]; 54 | - return _namei(path, 0, name); 55 | + return _namei(0, path, 0, name, 0); 56 | } 57 | 58 | struct inode* 59 | nameiparent(char *path, char *name) 60 | { 61 | - return _namei(path, 1, name); 62 | -} 63 | + return _namei(0, path, 1, name, 0); 64 | +} 65 | diff -r f8a4e40ab1d6 fs.h 66 | --- a/fs.h Thu Aug 30 14:32:06 2007 -0400 67 | +++ b/fs.h Thu Aug 30 13:05:43 2007 -0400 68 | @@ -33,6 +33,7 @@ struct dinode { 69 | #define T_DIR 1 // Directory 70 | #define T_FILE 2 // File 71 | #define T_DEV 3 // Special device 72 | +#define T_SYMLINK 4 // Symlink 73 | 74 | // Inodes per block. 75 | #define IPB (BSIZE / sizeof(struct dinode)) 76 | diff -r f8a4e40ab1d6 syscall.c 77 | --- a/syscall.c Thu Aug 30 14:32:06 2007 -0400 78 | +++ b/syscall.c Thu Aug 30 13:05:29 2007 -0400 79 | @@ -96,6 +96,7 @@ extern int sys_unlink(void); 80 | extern int sys_unlink(void); 81 | extern int sys_wait(void); 82 | extern int sys_write(void); 83 | +extern int sys_symlink(void); 84 | 85 | static int (*syscalls[])(void) = { 86 | [SYS_chdir] sys_chdir, 87 | @@ -118,6 +119,7 @@ static int (*syscalls[])(void) = { 88 | [SYS_unlink] sys_unlink, 89 | [SYS_wait] sys_wait, 90 | [SYS_write] sys_write, 91 | +[SYS_symlink] sys_symlink, 92 | }; 93 | 94 | void 95 | diff -r f8a4e40ab1d6 syscall.h 96 | --- a/syscall.h Thu Aug 30 14:32:06 2007 -0400 97 | +++ b/syscall.h Thu Aug 30 13:02:48 2007 -0400 98 | @@ -19,3 +19,4 @@ 99 | #define SYS_getpid 18 100 | #define SYS_sbrk 19 101 | #define SYS_sleep 20 102 | +#define SYS_symlink 21 103 | diff -r f8a4e40ab1d6 sysfile.c 104 | --- a/sysfile.c Thu Aug 30 14:32:06 2007 -0400 105 | +++ b/sysfile.c Thu Aug 30 13:10:31 2007 -0400 106 | @@ -257,6 +257,21 @@ create(char *path, int canexist, short t 107 | } 108 | 109 | int 110 | +sys_symlink(void) 111 | +{ 112 | + char *old, *new; 113 | + struct inode *ip; 114 | + 115 | + if(argstr(0, &old) < 0 || argstr(1, &new) < 0) 116 | + return -1; 117 | + if((ip = create(new, 0, T_SYMLINK, 0, 0)) == 0) 118 | + return -1; 119 | + writei(ip, old, 0, strlen(old)); 120 | + iunlockput(ip); 121 | + return 0; 122 | +} 123 | + 124 | +int 125 | sys_open(void) 126 | { 127 | char *path; 128 | @@ -393,3 +408,4 @@ sys_pipe(void) 129 | fd[1] = fd1; 130 | return 0; 131 | } 132 | + 133 | diff -r f8a4e40ab1d6 user.h 134 | --- a/user.h Thu Aug 30 14:32:06 2007 -0400 135 | +++ b/user.h Thu Aug 30 13:02:34 2007 -0400 136 | @@ -21,6 +21,7 @@ int getpid(); 137 | int getpid(); 138 | char* sbrk(int); 139 | int sleep(int); 140 | +int symlink(int); 141 | 142 | // ulib.c 143 | int stat(char*, struct stat*); 144 | diff -r f8a4e40ab1d6 usys.S 145 | --- a/usys.S Thu Aug 30 14:32:06 2007 -0400 146 | +++ b/usys.S Thu Aug 30 13:05:54 2007 -0400 147 | @@ -28,3 +28,4 @@ STUB(getpid) 148 | STUB(getpid) 149 | STUB(sbrk) 150 | STUB(sleep) 151 | +STUB(symlink) 152 | -------------------------------------------------------------------------------- /syscall.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "defs.h" 3 | #include "param.h" 4 | #include "memlayout.h" 5 | #include "mmu.h" 6 | #include "proc.h" 7 | #include "x86.h" 8 | #include "syscall.h" 9 | 10 | // User code makes a system call with INT T_SYSCALL. 11 | // System call number in %eax. 12 | // Arguments on the stack, from the user call to the C 13 | // library system call function. The saved user %esp points 14 | // to a saved program counter, and then the first argument. 15 | 16 | // Fetch the int at addr from the current process. 17 | int 18 | fetchint(uint addr, int *ip) 19 | { 20 | if(addr >= proc->sz || addr+4 > proc->sz) 21 | return -1; 22 | *ip = *(int*)(addr); 23 | return 0; 24 | } 25 | 26 | // Fetch the nul-terminated string at addr from the current process. 27 | // Doesn't actually copy the string - just sets *pp to point at it. 28 | // Returns length of string, not including nul. 29 | int 30 | fetchstr(uint addr, char **pp) 31 | { 32 | char *s, *ep; 33 | 34 | if(addr >= proc->sz) 35 | return -1; 36 | *pp = (char*)addr; 37 | ep = (char*)proc->sz; 38 | for(s = *pp; s < ep; s++) 39 | if(*s == 0) 40 | return s - *pp; 41 | return -1; 42 | } 43 | 44 | // Fetch the nth 32-bit system call argument. 45 | int 46 | argint(int n, int *ip) 47 | { 48 | return fetchint(proc->tf->esp + 4 + 4*n, ip); 49 | } 50 | 51 | // Fetch the nth word-sized system call argument as a pointer 52 | // to a block of memory of size n bytes. Check that the pointer 53 | // lies within the process address space. 54 | int 55 | argptr(int n, char **pp, int size) 56 | { 57 | int i; 58 | 59 | if(argint(n, &i) < 0) 60 | return -1; 61 | if((uint)i >= proc->sz || (uint)i+size > proc->sz) 62 | return -1; 63 | *pp = (char*)i; 64 | return 0; 65 | } 66 | 67 | // Fetch the nth word-sized system call argument as a string pointer. 68 | // Check that the pointer is valid and the string is nul-terminated. 69 | // (There is no shared writable memory, so the string can't change 70 | // between this check and being used by the kernel.) 71 | int 72 | argstr(int n, char **pp) 73 | { 74 | int addr; 75 | if(argint(n, &addr) < 0) 76 | return -1; 77 | return fetchstr(addr, pp); 78 | } 79 | 80 | extern int sys_chdir(void); 81 | extern int sys_close(void); 82 | extern int sys_dup(void); 83 | extern int sys_exec(void); 84 | extern int sys_exit(void); 85 | extern int sys_fork(void); 86 | extern int sys_fstat(void); 87 | extern int sys_getpid(void); 88 | extern int sys_kill(void); 89 | extern int sys_link(void); 90 | extern int sys_mkdir(void); 91 | extern int sys_mknod(void); 92 | extern int sys_open(void); 93 | extern int sys_pipe(void); 94 | extern int sys_read(void); 95 | extern int sys_sbrk(void); 96 | extern int sys_sleep(void); 97 | extern int sys_unlink(void); 98 | extern int sys_wait(void); 99 | extern int sys_write(void); 100 | extern int sys_uptime(void); 101 | 102 | static int (*syscalls[])(void) = { 103 | [SYS_fork] sys_fork, 104 | [SYS_exit] sys_exit, 105 | [SYS_wait] sys_wait, 106 | [SYS_pipe] sys_pipe, 107 | [SYS_read] sys_read, 108 | [SYS_kill] sys_kill, 109 | [SYS_exec] sys_exec, 110 | [SYS_fstat] sys_fstat, 111 | [SYS_chdir] sys_chdir, 112 | [SYS_dup] sys_dup, 113 | [SYS_getpid] sys_getpid, 114 | [SYS_sbrk] sys_sbrk, 115 | [SYS_sleep] sys_sleep, 116 | [SYS_uptime] sys_uptime, 117 | [SYS_open] sys_open, 118 | [SYS_write] sys_write, 119 | [SYS_mknod] sys_mknod, 120 | [SYS_unlink] sys_unlink, 121 | [SYS_link] sys_link, 122 | [SYS_mkdir] sys_mkdir, 123 | [SYS_close] sys_close, 124 | }; 125 | 126 | void 127 | syscall(void) 128 | { 129 | int num; 130 | 131 | num = proc->tf->eax; 132 | if(num > 0 && num < NELEM(syscalls) && syscalls[num]) { 133 | proc->tf->eax = syscalls[num](); 134 | } else { 135 | cprintf("%d %s: unknown sys call %d\n", 136 | proc->pid, proc->name, num); 137 | proc->tf->eax = -1; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /syscall.h: -------------------------------------------------------------------------------- 1 | // System call numbers 2 | #define SYS_fork 1 3 | #define SYS_exit 2 4 | #define SYS_wait 3 5 | #define SYS_pipe 4 6 | #define SYS_read 5 7 | #define SYS_kill 6 8 | #define SYS_exec 7 9 | #define SYS_fstat 8 10 | #define SYS_chdir 9 11 | #define SYS_dup 10 12 | #define SYS_getpid 11 13 | #define SYS_sbrk 12 14 | #define SYS_sleep 13 15 | #define SYS_uptime 14 16 | #define SYS_open 15 17 | #define SYS_write 16 18 | #define SYS_mknod 17 19 | #define SYS_unlink 18 20 | #define SYS_link 19 21 | #define SYS_mkdir 20 22 | #define SYS_close 21 23 | -------------------------------------------------------------------------------- /sysfile.c: -------------------------------------------------------------------------------- 1 | // 2 | // File-system system calls. 3 | // Mostly argument checking, since we don't trust 4 | // user code, and calls into file.c and fs.c. 5 | // 6 | 7 | #include "types.h" 8 | #include "defs.h" 9 | #include "param.h" 10 | #include "stat.h" 11 | #include "mmu.h" 12 | #include "proc.h" 13 | #include "fs.h" 14 | #include "file.h" 15 | #include "fcntl.h" 16 | 17 | // Fetch the nth word-sized system call argument as a file descriptor 18 | // and return both the descriptor and the corresponding struct file. 19 | static int 20 | argfd(int n, int *pfd, struct file **pf) 21 | { 22 | int fd; 23 | struct file *f; 24 | 25 | if(argint(n, &fd) < 0) 26 | return -1; 27 | if(fd < 0 || fd >= NOFILE || (f=proc->ofile[fd]) == 0) 28 | return -1; 29 | if(pfd) 30 | *pfd = fd; 31 | if(pf) 32 | *pf = f; 33 | return 0; 34 | } 35 | 36 | // Allocate a file descriptor for the given file. 37 | // Takes over file reference from caller on success. 38 | static int 39 | fdalloc(struct file *f) 40 | { 41 | int fd; 42 | 43 | for(fd = 0; fd < NOFILE; fd++){ 44 | if(proc->ofile[fd] == 0){ 45 | proc->ofile[fd] = f; 46 | return fd; 47 | } 48 | } 49 | return -1; 50 | } 51 | 52 | int 53 | sys_dup(void) 54 | { 55 | struct file *f; 56 | int fd; 57 | 58 | if(argfd(0, 0, &f) < 0) 59 | return -1; 60 | if((fd=fdalloc(f)) < 0) 61 | return -1; 62 | filedup(f); 63 | return fd; 64 | } 65 | 66 | int 67 | sys_read(void) 68 | { 69 | struct file *f; 70 | int n; 71 | char *p; 72 | 73 | if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) 74 | return -1; 75 | return fileread(f, p, n); 76 | } 77 | 78 | int 79 | sys_write(void) 80 | { 81 | struct file *f; 82 | int n; 83 | char *p; 84 | 85 | if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) 86 | return -1; 87 | return filewrite(f, p, n); 88 | } 89 | 90 | int 91 | sys_close(void) 92 | { 93 | int fd; 94 | struct file *f; 95 | 96 | if(argfd(0, &fd, &f) < 0) 97 | return -1; 98 | proc->ofile[fd] = 0; 99 | fileclose(f); 100 | return 0; 101 | } 102 | 103 | int 104 | sys_fstat(void) 105 | { 106 | struct file *f; 107 | struct stat *st; 108 | 109 | if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0) 110 | return -1; 111 | return filestat(f, st); 112 | } 113 | 114 | // Create the path new as a link to the same inode as old. 115 | int 116 | sys_link(void) 117 | { 118 | char name[DIRSIZ], *new, *old; 119 | struct inode *dp, *ip; 120 | 121 | if(argstr(0, &old) < 0 || argstr(1, &new) < 0) 122 | return -1; 123 | if((ip = namei(old)) == 0) 124 | return -1; 125 | 126 | begin_trans(); 127 | 128 | ilock(ip); 129 | if(ip->type == T_DIR){ 130 | iunlockput(ip); 131 | commit_trans(); 132 | return -1; 133 | } 134 | 135 | ip->nlink++; 136 | iupdate(ip); 137 | iunlock(ip); 138 | 139 | if((dp = nameiparent(new, name)) == 0) 140 | goto bad; 141 | ilock(dp); 142 | if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){ 143 | iunlockput(dp); 144 | goto bad; 145 | } 146 | iunlockput(dp); 147 | iput(ip); 148 | 149 | commit_trans(); 150 | 151 | return 0; 152 | 153 | bad: 154 | ilock(ip); 155 | ip->nlink--; 156 | iupdate(ip); 157 | iunlockput(ip); 158 | commit_trans(); 159 | return -1; 160 | } 161 | 162 | // Is the directory dp empty except for "." and ".." ? 163 | static int 164 | isdirempty(struct inode *dp) 165 | { 166 | int off; 167 | struct dirent de; 168 | 169 | for(off=2*sizeof(de); offsize; off+=sizeof(de)){ 170 | if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 171 | panic("isdirempty: readi"); 172 | if(de.inum != 0) 173 | return 0; 174 | } 175 | return 1; 176 | } 177 | 178 | //PAGEBREAK! 179 | int 180 | sys_unlink(void) 181 | { 182 | struct inode *ip, *dp; 183 | struct dirent de; 184 | char name[DIRSIZ], *path; 185 | uint off; 186 | 187 | if(argstr(0, &path) < 0) 188 | return -1; 189 | if((dp = nameiparent(path, name)) == 0) 190 | return -1; 191 | 192 | begin_trans(); 193 | 194 | ilock(dp); 195 | 196 | // Cannot unlink "." or "..". 197 | if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0) 198 | goto bad; 199 | 200 | if((ip = dirlookup(dp, name, &off)) == 0) 201 | goto bad; 202 | ilock(ip); 203 | 204 | if(ip->nlink < 1) 205 | panic("unlink: nlink < 1"); 206 | if(ip->type == T_DIR && !isdirempty(ip)){ 207 | iunlockput(ip); 208 | goto bad; 209 | } 210 | 211 | memset(&de, 0, sizeof(de)); 212 | if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) 213 | panic("unlink: writei"); 214 | if(ip->type == T_DIR){ 215 | dp->nlink--; 216 | iupdate(dp); 217 | } 218 | iunlockput(dp); 219 | 220 | ip->nlink--; 221 | iupdate(ip); 222 | iunlockput(ip); 223 | 224 | commit_trans(); 225 | 226 | return 0; 227 | 228 | bad: 229 | iunlockput(dp); 230 | commit_trans(); 231 | return -1; 232 | } 233 | 234 | static struct inode* 235 | create(char *path, short type, short major, short minor) 236 | { 237 | uint off; 238 | struct inode *ip, *dp; 239 | char name[DIRSIZ]; 240 | 241 | if((dp = nameiparent(path, name)) == 0) 242 | return 0; 243 | ilock(dp); 244 | 245 | if((ip = dirlookup(dp, name, &off)) != 0){ 246 | iunlockput(dp); 247 | ilock(ip); 248 | if(type == T_FILE && ip->type == T_FILE) 249 | return ip; 250 | iunlockput(ip); 251 | return 0; 252 | } 253 | 254 | if((ip = ialloc(dp->dev, type)) == 0) 255 | panic("create: ialloc"); 256 | 257 | ilock(ip); 258 | ip->major = major; 259 | ip->minor = minor; 260 | ip->nlink = 1; 261 | iupdate(ip); 262 | 263 | if(type == T_DIR){ // Create . and .. entries. 264 | dp->nlink++; // for ".." 265 | iupdate(dp); 266 | // No ip->nlink++ for ".": avoid cyclic ref count. 267 | if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0) 268 | panic("create dots"); 269 | } 270 | 271 | if(dirlink(dp, name, ip->inum) < 0) 272 | panic("create: dirlink"); 273 | 274 | iunlockput(dp); 275 | 276 | return ip; 277 | } 278 | 279 | int 280 | sys_open(void) 281 | { 282 | char *path; 283 | int fd, omode; 284 | struct file *f; 285 | struct inode *ip; 286 | 287 | if(argstr(0, &path) < 0 || argint(1, &omode) < 0) 288 | return -1; 289 | if(omode & O_CREATE){ 290 | begin_trans(); 291 | ip = create(path, T_FILE, 0, 0); 292 | commit_trans(); 293 | if(ip == 0) 294 | return -1; 295 | } else { 296 | if((ip = namei(path)) == 0) 297 | return -1; 298 | ilock(ip); 299 | if(ip->type == T_DIR && omode != O_RDONLY){ 300 | iunlockput(ip); 301 | return -1; 302 | } 303 | } 304 | 305 | if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){ 306 | if(f) 307 | fileclose(f); 308 | iunlockput(ip); 309 | return -1; 310 | } 311 | iunlock(ip); 312 | 313 | f->type = FD_INODE; 314 | f->ip = ip; 315 | f->off = 0; 316 | f->readable = !(omode & O_WRONLY); 317 | f->writable = (omode & O_WRONLY) || (omode & O_RDWR); 318 | return fd; 319 | } 320 | 321 | int 322 | sys_mkdir(void) 323 | { 324 | char *path; 325 | struct inode *ip; 326 | 327 | begin_trans(); 328 | if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){ 329 | commit_trans(); 330 | return -1; 331 | } 332 | iunlockput(ip); 333 | commit_trans(); 334 | return 0; 335 | } 336 | 337 | int 338 | sys_mknod(void) 339 | { 340 | struct inode *ip; 341 | char *path; 342 | int len; 343 | int major, minor; 344 | 345 | begin_trans(); 346 | if((len=argstr(0, &path)) < 0 || 347 | argint(1, &major) < 0 || 348 | argint(2, &minor) < 0 || 349 | (ip = create(path, T_DEV, major, minor)) == 0){ 350 | commit_trans(); 351 | return -1; 352 | } 353 | iunlockput(ip); 354 | commit_trans(); 355 | return 0; 356 | } 357 | 358 | int 359 | sys_chdir(void) 360 | { 361 | char *path; 362 | struct inode *ip; 363 | 364 | if(argstr(0, &path) < 0 || (ip = namei(path)) == 0) 365 | return -1; 366 | ilock(ip); 367 | if(ip->type != T_DIR){ 368 | iunlockput(ip); 369 | return -1; 370 | } 371 | iunlock(ip); 372 | iput(proc->cwd); 373 | proc->cwd = ip; 374 | return 0; 375 | } 376 | 377 | int 378 | sys_exec(void) 379 | { 380 | char *path, *argv[MAXARG]; 381 | int i; 382 | uint uargv, uarg; 383 | 384 | if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0){ 385 | return -1; 386 | } 387 | memset(argv, 0, sizeof(argv)); 388 | for(i=0;; i++){ 389 | if(i >= NELEM(argv)) 390 | return -1; 391 | if(fetchint(uargv+4*i, (int*)&uarg) < 0) 392 | return -1; 393 | if(uarg == 0){ 394 | argv[i] = 0; 395 | break; 396 | } 397 | if(fetchstr(uarg, &argv[i]) < 0) 398 | return -1; 399 | } 400 | return exec(path, argv); 401 | } 402 | 403 | int 404 | sys_pipe(void) 405 | { 406 | int *fd; 407 | struct file *rf, *wf; 408 | int fd0, fd1; 409 | 410 | if(argptr(0, (void*)&fd, 2*sizeof(fd[0])) < 0) 411 | return -1; 412 | if(pipealloc(&rf, &wf) < 0) 413 | return -1; 414 | fd0 = -1; 415 | if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){ 416 | if(fd0 >= 0) 417 | proc->ofile[fd0] = 0; 418 | fileclose(rf); 419 | fileclose(wf); 420 | return -1; 421 | } 422 | fd[0] = fd0; 423 | fd[1] = fd1; 424 | return 0; 425 | } 426 | -------------------------------------------------------------------------------- /sysproc.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "x86.h" 3 | #include "defs.h" 4 | #include "param.h" 5 | #include "memlayout.h" 6 | #include "mmu.h" 7 | #include "proc.h" 8 | 9 | int 10 | sys_fork(void) 11 | { 12 | return fork(); 13 | } 14 | 15 | int 16 | sys_exit(void) 17 | { 18 | exit(); 19 | return 0; // not reached 20 | } 21 | 22 | int 23 | sys_wait(void) 24 | { 25 | return wait(); 26 | } 27 | 28 | int 29 | sys_kill(void) 30 | { 31 | int pid; 32 | 33 | if(argint(0, &pid) < 0) 34 | return -1; 35 | return kill(pid); 36 | } 37 | 38 | int 39 | sys_getpid(void) 40 | { 41 | return proc->pid; 42 | } 43 | 44 | int 45 | sys_sbrk(void) 46 | { 47 | int addr; 48 | int n; 49 | 50 | if(argint(0, &n) < 0) 51 | return -1; 52 | addr = proc->sz; 53 | if(growproc(n) < 0) 54 | return -1; 55 | return addr; 56 | } 57 | 58 | int 59 | sys_sleep(void) 60 | { 61 | int n; 62 | uint ticks0; 63 | 64 | if(argint(0, &n) < 0) 65 | return -1; 66 | acquire(&tickslock); 67 | ticks0 = ticks; 68 | while(ticks - ticks0 < n){ 69 | if(proc->killed){ 70 | release(&tickslock); 71 | return -1; 72 | } 73 | sleep(&ticks, &tickslock); 74 | } 75 | release(&tickslock); 76 | return 0; 77 | } 78 | 79 | // return how many clock tick interrupts have occurred 80 | // since start. 81 | int 82 | sys_uptime(void) 83 | { 84 | uint xticks; 85 | 86 | acquire(&tickslock); 87 | xticks = ticks; 88 | release(&tickslock); 89 | return xticks; 90 | } 91 | -------------------------------------------------------------------------------- /timer.c: -------------------------------------------------------------------------------- 1 | // Intel 8253/8254/82C54 Programmable Interval Timer (PIT). 2 | // Only used on uniprocessors; 3 | // SMP machines use the local APIC timer. 4 | 5 | #include "types.h" 6 | #include "defs.h" 7 | #include "traps.h" 8 | #include "x86.h" 9 | 10 | #define IO_TIMER1 0x040 // 8253 Timer #1 11 | 12 | // Frequency of all three count-down timers; 13 | // (TIMER_FREQ/freq) is the appropriate count 14 | // to generate a frequency of freq Hz. 15 | 16 | #define TIMER_FREQ 1193182 17 | #define TIMER_DIV(x) ((TIMER_FREQ+(x)/2)/(x)) 18 | 19 | #define TIMER_MODE (IO_TIMER1 + 3) // timer mode port 20 | #define TIMER_SEL0 0x00 // select counter 0 21 | #define TIMER_RATEGEN 0x04 // mode 2, rate generator 22 | #define TIMER_16BIT 0x30 // r/w counter 16 bits, LSB first 23 | 24 | void 25 | timerinit(void) 26 | { 27 | // Interrupt 100 times/sec. 28 | outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 29 | outb(IO_TIMER1, TIMER_DIV(100) % 256); 30 | outb(IO_TIMER1, TIMER_DIV(100) / 256); 31 | picenable(IRQ_TIMER); 32 | } 33 | -------------------------------------------------------------------------------- /toc.ftr: -------------------------------------------------------------------------------- 1 | 2 | 3 | The source listing is preceded by a cross-reference that lists every defined 4 | constant, struct, global variable, and function in xv6. Each entry gives, 5 | on the same line as the name, the line number (or, in a few cases, numbers) 6 | where the name is defined. Successive lines in an entry list the line 7 | numbers where the name is used. For example, this entry: 8 | 9 | swtch 2658 10 | 0374 2428 2466 2657 2658 11 | 12 | indicates that swtch is defined on line 2658 and is mentioned on five lines 13 | on sheets 03, 24, and 26. 14 | -------------------------------------------------------------------------------- /toc.hdr: -------------------------------------------------------------------------------- 1 | The numbers to the left of the file names in the table are sheet numbers. 2 | The source code has been printed in a double column format with fifty 3 | lines per column, giving one hundred lines per sheet (or page). 4 | Thus there is a convenient relationship between line numbers and sheet numbers. 5 | 6 | 7 | -------------------------------------------------------------------------------- /trap.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "defs.h" 3 | #include "param.h" 4 | #include "memlayout.h" 5 | #include "mmu.h" 6 | #include "proc.h" 7 | #include "x86.h" 8 | #include "traps.h" 9 | #include "spinlock.h" 10 | 11 | // Interrupt descriptor table (shared by all CPUs). 12 | struct gatedesc idt[256]; 13 | extern uint vectors[]; // in vectors.S: array of 256 entry pointers 14 | struct spinlock tickslock; 15 | uint ticks; 16 | 17 | void 18 | tvinit(void) 19 | { 20 | int i; 21 | 22 | for(i = 0; i < 256; i++) 23 | SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0); 24 | SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); 25 | 26 | initlock(&tickslock, "time"); 27 | } 28 | 29 | void 30 | idtinit(void) 31 | { 32 | lidt(idt, sizeof(idt)); 33 | } 34 | 35 | //PAGEBREAK: 41 36 | void 37 | trap(struct trapframe *tf) 38 | { 39 | if(tf->trapno == T_SYSCALL){ 40 | if(proc->killed) 41 | exit(); 42 | proc->tf = tf; 43 | syscall(); 44 | if(proc->killed) 45 | exit(); 46 | return; 47 | } 48 | 49 | switch(tf->trapno){ 50 | case T_IRQ0 + IRQ_TIMER: 51 | if(cpu->id == 0){ 52 | acquire(&tickslock); 53 | ticks++; 54 | wakeup(&ticks); 55 | release(&tickslock); 56 | } 57 | lapiceoi(); 58 | break; 59 | case T_IRQ0 + IRQ_IDE: 60 | ideintr(); 61 | lapiceoi(); 62 | break; 63 | case T_IRQ0 + IRQ_IDE+1: 64 | // Bochs generates spurious IDE1 interrupts. 65 | break; 66 | case T_IRQ0 + IRQ_KBD: 67 | kbdintr(); 68 | lapiceoi(); 69 | break; 70 | case T_IRQ0 + IRQ_COM1: 71 | uartintr(); 72 | lapiceoi(); 73 | break; 74 | case T_IRQ0 + 7: 75 | case T_IRQ0 + IRQ_SPURIOUS: 76 | cprintf("cpu%d: spurious interrupt at %x:%x\n", 77 | cpu->id, tf->cs, tf->eip); 78 | lapiceoi(); 79 | break; 80 | 81 | //PAGEBREAK: 13 82 | default: 83 | if(proc == 0 || (tf->cs&3) == 0){ 84 | // In kernel, it must be our mistake. 85 | cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n", 86 | tf->trapno, cpu->id, tf->eip, rcr2()); 87 | panic("trap"); 88 | } 89 | // In user space, assume process misbehaved. 90 | cprintf("pid %d %s: trap %d err %d on cpu %d " 91 | "eip 0x%x addr 0x%x--kill proc\n", 92 | proc->pid, proc->name, tf->trapno, tf->err, cpu->id, tf->eip, 93 | rcr2()); 94 | proc->killed = 1; 95 | } 96 | 97 | // Force process exit if it has been killed and is in user space. 98 | // (If it is still executing in the kernel, let it keep running 99 | // until it gets to the regular system call return.) 100 | if(proc && proc->killed && (tf->cs&3) == DPL_USER) 101 | exit(); 102 | 103 | // Force process to give up CPU on clock tick. 104 | // If interrupts were on while locks held, would need to check nlock. 105 | if(proc && proc->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER) 106 | yield(); 107 | 108 | // Check if the process has been killed since we yielded 109 | if(proc && proc->killed && (tf->cs&3) == DPL_USER) 110 | exit(); 111 | } 112 | -------------------------------------------------------------------------------- /trapasm.S: -------------------------------------------------------------------------------- 1 | #include "mmu.h" 2 | 3 | # vectors.S sends all traps here. 4 | .globl alltraps 5 | alltraps: 6 | # Build trap frame. 7 | pushl %ds 8 | pushl %es 9 | pushl %fs 10 | pushl %gs 11 | pushal 12 | 13 | # Set up data and per-cpu segments. 14 | movw $(SEG_KDATA<<3), %ax 15 | movw %ax, %ds 16 | movw %ax, %es 17 | movw $(SEG_KCPU<<3), %ax 18 | movw %ax, %fs 19 | movw %ax, %gs 20 | 21 | # Call trap(tf), where tf=%esp 22 | pushl %esp 23 | call trap 24 | addl $4, %esp 25 | 26 | # Return falls through to trapret... 27 | .globl trapret 28 | trapret: 29 | popal 30 | popl %gs 31 | popl %fs 32 | popl %es 33 | popl %ds 34 | addl $0x8, %esp # trapno and errcode 35 | iret 36 | -------------------------------------------------------------------------------- /traps.h: -------------------------------------------------------------------------------- 1 | // x86 trap and interrupt constants. 2 | 3 | // Processor-defined: 4 | #define T_DIVIDE 0 // divide error 5 | #define T_DEBUG 1 // debug exception 6 | #define T_NMI 2 // non-maskable interrupt 7 | #define T_BRKPT 3 // breakpoint 8 | #define T_OFLOW 4 // overflow 9 | #define T_BOUND 5 // bounds check 10 | #define T_ILLOP 6 // illegal opcode 11 | #define T_DEVICE 7 // device not available 12 | #define T_DBLFLT 8 // double fault 13 | // #define T_COPROC 9 // reserved (not used since 486) 14 | #define T_TSS 10 // invalid task switch segment 15 | #define T_SEGNP 11 // segment not present 16 | #define T_STACK 12 // stack exception 17 | #define T_GPFLT 13 // general protection fault 18 | #define T_PGFLT 14 // page fault 19 | // #define T_RES 15 // reserved 20 | #define T_FPERR 16 // floating point error 21 | #define T_ALIGN 17 // aligment check 22 | #define T_MCHK 18 // machine check 23 | #define T_SIMDERR 19 // SIMD floating point error 24 | 25 | // These are arbitrarily chosen, but with care not to overlap 26 | // processor defined exceptions or interrupt vectors. 27 | #define T_SYSCALL 64 // system call 28 | #define T_DEFAULT 500 // catchall 29 | 30 | #define T_IRQ0 32 // IRQ 0 corresponds to int T_IRQ 31 | 32 | #define IRQ_TIMER 0 33 | #define IRQ_KBD 1 34 | #define IRQ_COM1 4 35 | #define IRQ_IDE 14 36 | #define IRQ_ERROR 19 37 | #define IRQ_SPURIOUS 31 38 | 39 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | typedef unsigned int uint; 2 | typedef unsigned short ushort; 3 | typedef unsigned char uchar; 4 | typedef uint pde_t; 5 | -------------------------------------------------------------------------------- /uart.c: -------------------------------------------------------------------------------- 1 | // Intel 8250 serial port (UART). 2 | 3 | #include "types.h" 4 | #include "defs.h" 5 | #include "param.h" 6 | #include "traps.h" 7 | #include "spinlock.h" 8 | #include "fs.h" 9 | #include "file.h" 10 | #include "mmu.h" 11 | #include "proc.h" 12 | #include "x86.h" 13 | 14 | #define COM1 0x3f8 15 | 16 | static int uart; // is there a uart? 17 | 18 | void 19 | uartinit(void) 20 | { 21 | char *p; 22 | 23 | // Turn off the FIFO 24 | outb(COM1+2, 0); 25 | 26 | // 9600 baud, 8 data bits, 1 stop bit, parity off. 27 | outb(COM1+3, 0x80); // Unlock divisor 28 | outb(COM1+0, 115200/9600); 29 | outb(COM1+1, 0); 30 | outb(COM1+3, 0x03); // Lock divisor, 8 data bits. 31 | outb(COM1+4, 0); 32 | outb(COM1+1, 0x01); // Enable receive interrupts. 33 | 34 | // If status is 0xFF, no serial port. 35 | if(inb(COM1+5) == 0xFF) 36 | return; 37 | uart = 1; 38 | 39 | // Acknowledge pre-existing interrupt conditions; 40 | // enable interrupts. 41 | inb(COM1+2); 42 | inb(COM1+0); 43 | picenable(IRQ_COM1); 44 | ioapicenable(IRQ_COM1, 0); 45 | 46 | // Announce that we're here. 47 | for(p="xv6...\n"; *p; p++) 48 | uartputc(*p); 49 | } 50 | 51 | void 52 | uartputc(int c) 53 | { 54 | int i; 55 | 56 | if(!uart) 57 | return; 58 | for(i = 0; i < 128 && !(inb(COM1+5) & 0x20); i++) 59 | microdelay(10); 60 | outb(COM1+0, c); 61 | } 62 | 63 | static int 64 | uartgetc(void) 65 | { 66 | if(!uart) 67 | return -1; 68 | if(!(inb(COM1+5) & 0x01)) 69 | return -1; 70 | return inb(COM1+0); 71 | } 72 | 73 | void 74 | uartintr(void) 75 | { 76 | consoleintr(uartgetc); 77 | } 78 | -------------------------------------------------------------------------------- /ulib.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "fcntl.h" 4 | #include "user.h" 5 | #include "x86.h" 6 | 7 | char* 8 | strcpy(char *s, char *t) 9 | { 10 | char *os; 11 | 12 | os = s; 13 | while((*s++ = *t++) != 0) 14 | ; 15 | return os; 16 | } 17 | 18 | int 19 | strcmp(const char *p, const char *q) 20 | { 21 | while(*p && *p == *q) 22 | p++, q++; 23 | return (uchar)*p - (uchar)*q; 24 | } 25 | 26 | uint 27 | strlen(char *s) 28 | { 29 | int n; 30 | 31 | for(n = 0; s[n]; n++) 32 | ; 33 | return n; 34 | } 35 | 36 | void* 37 | memset(void *dst, int c, uint n) 38 | { 39 | stosb(dst, c, n); 40 | return dst; 41 | } 42 | 43 | char* 44 | strchr(const char *s, char c) 45 | { 46 | for(; *s; s++) 47 | if(*s == c) 48 | return (char*)s; 49 | return 0; 50 | } 51 | 52 | char* 53 | gets(char *buf, int max) 54 | { 55 | int i, cc; 56 | char c; 57 | 58 | for(i=0; i+1 < max; ){ 59 | cc = read(0, &c, 1); 60 | if(cc < 1) 61 | break; 62 | buf[i++] = c; 63 | if(c == '\n' || c == '\r') 64 | break; 65 | } 66 | buf[i] = '\0'; 67 | return buf; 68 | } 69 | 70 | int 71 | stat(char *n, struct stat *st) 72 | { 73 | int fd; 74 | int r; 75 | 76 | fd = open(n, O_RDONLY); 77 | if(fd < 0) 78 | return -1; 79 | r = fstat(fd, st); 80 | close(fd); 81 | return r; 82 | } 83 | 84 | int 85 | atoi(const char *s) 86 | { 87 | int n; 88 | 89 | n = 0; 90 | while('0' <= *s && *s <= '9') 91 | n = n*10 + *s++ - '0'; 92 | return n; 93 | } 94 | 95 | void* 96 | memmove(void *vdst, void *vsrc, int n) 97 | { 98 | char *dst, *src; 99 | 100 | dst = vdst; 101 | src = vsrc; 102 | while(n-- > 0) 103 | *dst++ = *src++; 104 | return vdst; 105 | } 106 | -------------------------------------------------------------------------------- /umalloc.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | #include "param.h" 5 | 6 | // Memory allocator by Kernighan and Ritchie, 7 | // The C programming Language, 2nd ed. Section 8.7. 8 | 9 | typedef long Align; 10 | 11 | union header { 12 | struct { 13 | union header *ptr; 14 | uint size; 15 | } s; 16 | Align x; 17 | }; 18 | 19 | typedef union header Header; 20 | 21 | static Header base; 22 | static Header *freep; 23 | 24 | void 25 | free(void *ap) 26 | { 27 | Header *bp, *p; 28 | 29 | bp = (Header*)ap - 1; 30 | for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) 31 | if(p >= p->s.ptr && (bp > p || bp < p->s.ptr)) 32 | break; 33 | if(bp + bp->s.size == p->s.ptr){ 34 | bp->s.size += p->s.ptr->s.size; 35 | bp->s.ptr = p->s.ptr->s.ptr; 36 | } else 37 | bp->s.ptr = p->s.ptr; 38 | if(p + p->s.size == bp){ 39 | p->s.size += bp->s.size; 40 | p->s.ptr = bp->s.ptr; 41 | } else 42 | p->s.ptr = bp; 43 | freep = p; 44 | } 45 | 46 | static Header* 47 | morecore(uint nu) 48 | { 49 | char *p; 50 | Header *hp; 51 | 52 | if(nu < 4096) 53 | nu = 4096; 54 | p = sbrk(nu * sizeof(Header)); 55 | if(p == (char*)-1) 56 | return 0; 57 | hp = (Header*)p; 58 | hp->s.size = nu; 59 | free((void*)(hp + 1)); 60 | return freep; 61 | } 62 | 63 | void* 64 | malloc(uint nbytes) 65 | { 66 | Header *p, *prevp; 67 | uint nunits; 68 | 69 | nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1; 70 | if((prevp = freep) == 0){ 71 | base.s.ptr = freep = prevp = &base; 72 | base.s.size = 0; 73 | } 74 | for(p = prevp->s.ptr; ; prevp = p, p = p->s.ptr){ 75 | if(p->s.size >= nunits){ 76 | if(p->s.size == nunits) 77 | prevp->s.ptr = p->s.ptr; 78 | else { 79 | p->s.size -= nunits; 80 | p += p->s.size; 81 | p->s.size = nunits; 82 | } 83 | freep = prevp; 84 | return (void*)(p + 1); 85 | } 86 | if(p == freep) 87 | if((p = morecore(nunits)) == 0) 88 | return 0; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /user.h: -------------------------------------------------------------------------------- 1 | struct stat; 2 | 3 | // system calls 4 | int fork(void); 5 | int exit(void) __attribute__((noreturn)); 6 | int wait(void); 7 | int pipe(int*); 8 | int write(int, void*, int); 9 | int read(int, void*, int); 10 | int close(int); 11 | int kill(int); 12 | int exec(char*, char**); 13 | int open(char*, int); 14 | int mknod(char*, short, short); 15 | int unlink(char*); 16 | int fstat(int fd, struct stat*); 17 | int link(char*, char*); 18 | int mkdir(char*); 19 | int chdir(char*); 20 | int dup(int); 21 | int getpid(void); 22 | char* sbrk(int); 23 | int sleep(int); 24 | int uptime(void); 25 | 26 | // ulib.c 27 | int stat(char*, struct stat*); 28 | char* strcpy(char*, char*); 29 | void *memmove(void*, void*, int); 30 | char* strchr(const char*, char c); 31 | int strcmp(const char*, const char*); 32 | void printf(int, char*, ...); 33 | char* gets(char*, int max); 34 | uint strlen(char*); 35 | void* memset(void*, int, uint); 36 | void* malloc(uint); 37 | void free(void*); 38 | int atoi(const char*); 39 | -------------------------------------------------------------------------------- /usys.S: -------------------------------------------------------------------------------- 1 | #include "syscall.h" 2 | #include "traps.h" 3 | 4 | #define SYSCALL(name) \ 5 | .globl name; \ 6 | name: \ 7 | movl $SYS_ ## name, %eax; \ 8 | int $T_SYSCALL; \ 9 | ret 10 | 11 | SYSCALL(fork) 12 | SYSCALL(exit) 13 | SYSCALL(wait) 14 | SYSCALL(pipe) 15 | SYSCALL(read) 16 | SYSCALL(write) 17 | SYSCALL(close) 18 | SYSCALL(kill) 19 | SYSCALL(exec) 20 | SYSCALL(open) 21 | SYSCALL(mknod) 22 | SYSCALL(unlink) 23 | SYSCALL(fstat) 24 | SYSCALL(link) 25 | SYSCALL(mkdir) 26 | SYSCALL(chdir) 27 | SYSCALL(dup) 28 | SYSCALL(getpid) 29 | SYSCALL(sbrk) 30 | SYSCALL(sleep) 31 | SYSCALL(uptime) 32 | -------------------------------------------------------------------------------- /vectors.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # Generate vectors.S, the trap/interrupt entry points. 4 | # There has to be one entry point per interrupt number 5 | # since otherwise there's no way for trap() to discover 6 | # the interrupt number. 7 | 8 | print "# generated by vectors.pl - do not edit\n"; 9 | print "# handlers\n"; 10 | print ".globl alltraps\n"; 11 | for(my $i = 0; $i < 256; $i++){ 12 | print ".globl vector$i\n"; 13 | print "vector$i:\n"; 14 | if(!($i == 8 || ($i >= 10 && $i <= 14) || $i == 17)){ 15 | print " pushl \$0\n"; 16 | } 17 | print " pushl \$$i\n"; 18 | print " jmp alltraps\n"; 19 | } 20 | 21 | print "\n# vector table\n"; 22 | print ".data\n"; 23 | print ".globl vectors\n"; 24 | print "vectors:\n"; 25 | for(my $i = 0; $i < 256; $i++){ 26 | print " .long vector$i\n"; 27 | } 28 | 29 | # sample output: 30 | # # handlers 31 | # .globl alltraps 32 | # .globl vector0 33 | # vector0: 34 | # pushl $0 35 | # pushl $0 36 | # jmp alltraps 37 | # ... 38 | # 39 | # # vector table 40 | # .data 41 | # .globl vectors 42 | # vectors: 43 | # .long vector0 44 | # .long vector1 45 | # .long vector2 46 | # ... 47 | 48 | -------------------------------------------------------------------------------- /wc.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | char buf[512]; 6 | 7 | void 8 | wc(int fd, char *name) 9 | { 10 | int i, n; 11 | int l, w, c, inword; 12 | 13 | l = w = c = 0; 14 | inword = 0; 15 | while((n = read(fd, buf, sizeof(buf))) > 0){ 16 | for(i=0; i> 16; 70 | 71 | asm volatile("lgdt (%0)" : : "r" (pd)); 72 | } 73 | 74 | struct gatedesc; 75 | 76 | static inline void 77 | lidt(struct gatedesc *p, int size) 78 | { 79 | volatile ushort pd[3]; 80 | 81 | pd[0] = size-1; 82 | pd[1] = (uint)p; 83 | pd[2] = (uint)p >> 16; 84 | 85 | asm volatile("lidt (%0)" : : "r" (pd)); 86 | } 87 | 88 | static inline void 89 | ltr(ushort sel) 90 | { 91 | asm volatile("ltr %0" : : "r" (sel)); 92 | } 93 | 94 | static inline uint 95 | readeflags(void) 96 | { 97 | uint eflags; 98 | asm volatile("pushfl; popl %0" : "=r" (eflags)); 99 | return eflags; 100 | } 101 | 102 | static inline void 103 | loadgs(ushort v) 104 | { 105 | asm volatile("movw %0, %%gs" : : "r" (v)); 106 | } 107 | 108 | static inline void 109 | cli(void) 110 | { 111 | asm volatile("cli"); 112 | } 113 | 114 | static inline void 115 | sti(void) 116 | { 117 | asm volatile("sti"); 118 | } 119 | 120 | static inline uint 121 | xchg(volatile uint *addr, uint newval) 122 | { 123 | uint result; 124 | 125 | // The + in "+m" denotes a read-modify-write operand. 126 | asm volatile("lock; xchgl %0, %1" : 127 | "+m" (*addr), "=a" (result) : 128 | "1" (newval) : 129 | "cc"); 130 | return result; 131 | } 132 | 133 | static inline uint 134 | rcr2(void) 135 | { 136 | uint val; 137 | asm volatile("movl %%cr2,%0" : "=r" (val)); 138 | return val; 139 | } 140 | 141 | static inline void 142 | lcr3(uint val) 143 | { 144 | asm volatile("movl %0,%%cr3" : : "r" (val)); 145 | } 146 | 147 | //PAGEBREAK: 36 148 | // Layout of the trap frame built on the stack by the 149 | // hardware and by trapasm.S, and passed to trap(). 150 | struct trapframe { 151 | // registers as pushed by pusha 152 | uint edi; 153 | uint esi; 154 | uint ebp; 155 | uint oesp; // useless & ignored 156 | uint ebx; 157 | uint edx; 158 | uint ecx; 159 | uint eax; 160 | 161 | // rest of trap frame 162 | ushort gs; 163 | ushort padding1; 164 | ushort fs; 165 | ushort padding2; 166 | ushort es; 167 | ushort padding3; 168 | ushort ds; 169 | ushort padding4; 170 | uint trapno; 171 | 172 | // below here defined by x86 hardware 173 | uint err; 174 | uint eip; 175 | ushort cs; 176 | ushort padding5; 177 | uint eflags; 178 | 179 | // below here only when crossing rings, such as from user to kernel 180 | uint esp; 181 | ushort ss; 182 | ushort padding6; 183 | }; 184 | -------------------------------------------------------------------------------- /zombie.c: -------------------------------------------------------------------------------- 1 | // Create a zombie process that 2 | // must be reparented at exit. 3 | 4 | #include "types.h" 5 | #include "stat.h" 6 | #include "user.h" 7 | 8 | int 9 | main(void) 10 | { 11 | if(fork() > 0) 12 | sleep(5); // Let child exit before parent. 13 | exit(); 14 | } 15 | --------------------------------------------------------------------------------