《操作系统》的实验代码。
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

343 linhas
9.1 KiB

12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
  1. PROJ := 5
  2. EMPTY :=
  3. SPACE := $(EMPTY) $(EMPTY)
  4. SLASH := /
  5. V := @
  6. #need llvm/cang-3.5+
  7. #USELLVM := 1
  8. # try to infer the correct GCCPREFX
  9. ifndef GCCPREFIX
  10. GCCPREFIX := $(shell if i386-ucore-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
  11. then echo 'i386-ucore-elf-'; \
  12. elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
  13. then echo ''; \
  14. else echo "***" 1>&2; \
  15. echo "*** Error: Couldn't find an i386-ucore-elf version of GCC/binutils." 1>&2; \
  16. echo "*** Is the directory with i386-ucore-elf-gcc in your PATH?" 1>&2; \
  17. echo "*** If your i386-ucore-elf toolchain is installed with a command" 1>&2; \
  18. echo "*** prefix other than 'i386-ucore-elf-', set your GCCPREFIX" 1>&2; \
  19. echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
  20. echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
  21. echo "***" 1>&2; exit 1; fi)
  22. endif
  23. # try to infer the correct QEMU
  24. ifndef QEMU
  25. QEMU := $(shell if which qemu-system-i386 > /dev/null; \
  26. then echo 'qemu-system-i386'; exit; \
  27. elif which i386-elf-qemu > /dev/null; \
  28. then echo 'i386-elf-qemu'; exit; \
  29. elif which qemu > /dev/null; \
  30. then echo 'qemu'; exit; \
  31. else \
  32. echo "***" 1>&2; \
  33. echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
  34. echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
  35. echo "***" 1>&2; exit 1; fi)
  36. endif
  37. # eliminate default suffix rules
  38. .SUFFIXES: .c .S .h
  39. # delete target files if there is an error (or make is interrupted)
  40. .DELETE_ON_ERROR:
  41. # define compiler and flags
  42. ifndef USELLVM
  43. HOSTCC := gcc
  44. HOSTCFLAGS := -g -Wall -O2
  45. CC := $(GCCPREFIX)gcc
  46. CFLAGS := -fno-builtin -Wall -ggdb -m32 -gstabs -nostdinc $(DEFS)
  47. CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
  48. else
  49. HOSTCC := clang
  50. HOSTCFLAGS := -g -Wall -O2
  51. CC := clang
  52. CFLAGS := -fno-builtin -Wall -g -m32 -mno-sse -nostdinc $(DEFS)
  53. CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
  54. endif
  55. GDB := $(GCCPREFIX)gdb
  56. CTYPE := c S
  57. LD := $(GCCPREFIX)ld
  58. LDFLAGS := -m $(shell $(LD) -V | grep elf_i386 2>/dev/null)
  59. LDFLAGS += -nostdlib
  60. OBJCOPY := $(GCCPREFIX)objcopy
  61. OBJDUMP := $(GCCPREFIX)objdump
  62. COPY := cp
  63. MKDIR := mkdir -p
  64. MV := mv
  65. RM := rm -f
  66. AWK := awk
  67. SED := sed
  68. SH := sh
  69. TR := tr
  70. TOUCH := touch -c
  71. OBJDIR := obj
  72. BINDIR := bin
  73. ALLOBJS :=
  74. ALLDEPS :=
  75. TARGETS :=
  76. include tools/function.mk
  77. listf_cc = $(call listf,$(1),$(CTYPE))
  78. USER_PREFIX := __user_
  79. # for cc
  80. add_files_cc = $(call add_files,$(1),$(CC),$(CFLAGS) $(3),$(2),$(4))
  81. create_target_cc = $(call create_target,$(1),$(2),$(3),$(CC),$(CFLAGS))
  82. # for hostcc
  83. add_files_host = $(call add_files,$(1),$(HOSTCC),$(HOSTCFLAGS),$(2),$(3))
  84. create_target_host = $(call create_target,$(1),$(2),$(3),$(HOSTCC),$(HOSTCFLAGS))
  85. cgtype = $(patsubst %.$(2),%.$(3),$(1))
  86. objfile = $(call toobj,$(1))
  87. asmfile = $(call cgtype,$(call toobj,$(1)),o,asm)
  88. outfile = $(call cgtype,$(call toobj,$(1)),o,out)
  89. symfile = $(call cgtype,$(call toobj,$(1)),o,sym)
  90. filename = $(basename $(notdir $(1)))
  91. ubinfile = $(call outfile,$(addprefix $(USER_PREFIX),$(call filename,$(1))))
  92. # for match pattern
  93. match = $(shell echo $(2) | $(AWK) '{for(i=1;i<=NF;i++){if(match("$(1)","^"$$(i)"$$")){exit 1;}}}'; echo $$?)
  94. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  95. # include kernel/user
  96. INCLUDE += libs/
  97. CFLAGS += $(addprefix -I,$(INCLUDE))
  98. LIBDIR += libs
  99. $(call add_files_cc,$(call listf_cc,$(LIBDIR)),libs,)
  100. # -------------------------------------------------------------------
  101. # user programs
  102. UINCLUDE += user/include/ \
  103. user/libs/
  104. USRCDIR += user
  105. ULIBDIR += user/libs
  106. UCFLAGS += $(addprefix -I,$(UINCLUDE))
  107. USER_BINS :=
  108. $(call add_files_cc,$(call listf_cc,$(ULIBDIR)),ulibs,$(UCFLAGS))
  109. $(call add_files_cc,$(call listf_cc,$(USRCDIR)),uprog,$(UCFLAGS))
  110. UOBJS := $(call read_packet,ulibs libs)
  111. define uprog_ld
  112. __user_bin__ := $$(call ubinfile,$(1))
  113. USER_BINS += $$(__user_bin__)
  114. $$(__user_bin__): tools/user.ld
  115. $$(__user_bin__): $$(UOBJS)
  116. $$(__user_bin__): $(1) | $$$$(dir $$$$@)
  117. $(V)$(LD) $(LDFLAGS) -T tools/user.ld -o $$@ $$(UOBJS) $(1)
  118. @$(OBJDUMP) -S $$@ > $$(call cgtype,$$<,o,asm)
  119. @$(OBJDUMP) -t $$@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$$$/d' > $$(call cgtype,$$<,o,sym)
  120. endef
  121. $(foreach p,$(call read_packet,uprog),$(eval $(call uprog_ld,$(p))))
  122. # -------------------------------------------------------------------
  123. # kernel
  124. KINCLUDE += kern/debug/ \
  125. kern/driver/ \
  126. kern/trap/ \
  127. kern/mm/ \
  128. kern/libs/ \
  129. kern/sync/ \
  130. kern/fs/ \
  131. kern/process \
  132. kern/schedule \
  133. kern/syscall
  134. KSRCDIR += kern/init \
  135. kern/libs \
  136. kern/debug \
  137. kern/driver \
  138. kern/trap \
  139. kern/mm \
  140. kern/sync \
  141. kern/fs \
  142. kern/process \
  143. kern/schedule \
  144. kern/syscall
  145. KCFLAGS += $(addprefix -I,$(KINCLUDE))
  146. $(call add_files_cc,$(call listf_cc,$(KSRCDIR)),kernel,$(KCFLAGS))
  147. KOBJS = $(call read_packet,kernel libs)
  148. # create kernel target
  149. kernel = $(call totarget,kernel)
  150. $(kernel): tools/kernel.ld
  151. $(kernel): $(KOBJS) $(USER_BINS)
  152. @echo + ld $@
  153. $(V)$(LD) $(LDFLAGS) -T tools/kernel.ld -o $@ $(KOBJS) -b binary $(USER_BINS)
  154. @$(OBJDUMP) -S $@ > $(call asmfile,kernel)
  155. @$(OBJDUMP) -t $@ | $(SED) '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(call symfile,kernel)
  156. $(call create_target,kernel)
  157. # -------------------------------------------------------------------
  158. # create bootblock
  159. bootfiles = $(call listf_cc,boot)
  160. $(foreach f,$(bootfiles),$(call cc_compile,$(f),$(CC),$(CFLAGS) -Os -nostdinc))
  161. bootblock = $(call totarget,bootblock)
  162. $(bootblock): $(call toobj,boot/bootasm.S) $(call toobj,$(bootfiles)) | $(call totarget,sign)
  163. @echo + ld $@
  164. $(V)$(LD) $(LDFLAGS) -N -T tools/boot.ld $^ -o $(call toobj,bootblock)
  165. @$(OBJDUMP) -S $(call objfile,bootblock) > $(call asmfile,bootblock)
  166. @$(OBJCOPY) -S -O binary $(call objfile,bootblock) $(call outfile,bootblock)
  167. @$(call totarget,sign) $(call outfile,bootblock) $(bootblock)
  168. $(call create_target,bootblock)
  169. # -------------------------------------------------------------------
  170. # create 'sign' tools
  171. $(call add_files_host,tools/sign.c,sign,sign)
  172. $(call create_target_host,sign,sign)
  173. # -------------------------------------------------------------------
  174. # create ucore.img
  175. UCOREIMG := $(call totarget,ucore.img)
  176. $(UCOREIMG): $(kernel) $(bootblock)
  177. $(V)dd if=/dev/zero of=$@ count=10000
  178. $(V)dd if=$(bootblock) of=$@ conv=notrunc
  179. $(V)dd if=$(kernel) of=$@ seek=1 conv=notrunc
  180. $(call create_target,ucore.img)
  181. # -------------------------------------------------------------------
  182. # create swap.img
  183. SWAPIMG := $(call totarget,swap.img)
  184. $(SWAPIMG):
  185. $(V)dd if=/dev/zero of=$@ bs=1M count=128
  186. $(call create_target,swap.img)
  187. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  188. $(call finish_all)
  189. IGNORE_ALLDEPS = clean \
  190. dist-clean \
  191. grade \
  192. touch \
  193. print-.+ \
  194. run-.+ \
  195. build-.+ \
  196. handin
  197. ifeq ($(call match,$(MAKECMDGOALS),$(IGNORE_ALLDEPS)),0)
  198. -include $(ALLDEPS)
  199. endif
  200. # files for grade script
  201. TARGETS: $(TARGETS)
  202. .DEFAULT_GOAL := TARGETS
  203. QEMUOPTS = -hda $(UCOREIMG) -drive file=$(SWAPIMG),media=disk,cache=writeback
  204. .PHONY: qemu qemu-nox debug debug-nox
  205. qemu-mon: $(UCOREIMG) $(SWAPIMG)
  206. $(V)$(QEMU) -no-reboot -monitor stdio $(QEMUOPTS) -serial null
  207. qemu: $(UCOREIMG) $(SWAPIMG)
  208. $(V)$(QEMU) -no-reboot -parallel stdio $(QEMUOPTS) -serial null
  209. qemu-nox: $(UCOREIMG) $(SWAPIMG)
  210. $(V)$(QEMU) -no-reboot -serial mon:stdio $(QEMUOPTS) -nographic
  211. TERMINAL := gnome-terminal
  212. debug: $(UCOREIMG) $(SWAPIMG)
  213. $(V)$(QEMU) -S -s -parallel stdio $(QEMUOPTS) -serial null &
  214. $(V)sleep 2
  215. $(V)$(TERMINAL) -e "$(GDB) -q -x tools/gdbinit"
  216. debug-nox: $(UCOREIMG) $(SWAPIMG)
  217. $(V)$(QEMU) -S -s -serial mon:stdio $(QEMUOPTS) -nographic &
  218. $(V)sleep 2
  219. $(V)$(TERMINAL) -e "$(GDB) -q -x tools/gdbinit"
  220. RUN_PREFIX := _binary_$(OBJDIR)_$(USER_PREFIX)
  221. MAKEOPTS := --quiet --no-print-directory
  222. run-%: build-%
  223. $(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null
  224. run-nox-%: build-%
  225. $(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic
  226. build-%: touch
  227. $(V)$(MAKE) $(MAKEOPTS) "DEFS+=-DTEST=$* -DTESTSTART=$(RUN_PREFIX)$*_out_start -DTESTSIZE=$(RUN_PREFIX)$*_out_size"
  228. .PHONY: grade touch
  229. GRADE_GDB_IN := .gdb.in
  230. GRADE_QEMU_OUT := .qemu.out
  231. HANDIN := proj$(PROJ)-handin.tar.gz
  232. TOUCH_FILES := kern/process/proc.c
  233. MAKEOPTS := --quiet --no-print-directory
  234. grade:
  235. $(V)$(MAKE) $(MAKEOPTS) clean
  236. $(V)$(SH) tools/grade.sh
  237. touch:
  238. $(V)$(foreach f,$(TOUCH_FILES),$(TOUCH) $(f))
  239. print-%:
  240. @echo $($(shell echo $(patsubst print-%,%,$@) | $(TR) [a-z] [A-Z]))
  241. .PHONY: clean dist-clean handin packall tags
  242. clean:
  243. $(V)$(RM) $(GRADE_GDB_IN) $(GRADE_QEMU_OUT) cscope* tags
  244. -$(RM) -r $(OBJDIR) $(BINDIR)
  245. dist-clean: clean
  246. -$(RM) $(HANDIN)
  247. handin: packall
  248. @echo Please visit http://learn.tsinghua.edu.cn and upload $(HANDIN). Thanks!
  249. packall: clean
  250. @$(RM) -f $(HANDIN)
  251. @tar -czf $(HANDIN) `find . -type f -o -type d | grep -v '^\.*$$' | grep -vF '$(HANDIN)'`
  252. tags:
  253. @echo TAGS ALL
  254. $(V)rm -f cscope.files cscope.in.out cscope.out cscope.po.out tags
  255. $(V)find . -type f -name "*.[chS]" >cscope.files
  256. $(V)cscope -bq
  257. $(V)ctags -L cscope.files