1 # Makefile for GeekOS kernel, userspace, and tools
2 # Copyright (c) 2004,2005 David H. Hovemeyer <daveho@cs.umd.edu>
5 # This is free software. You are permitted to use,
6 # redistribute, and modify it as specified in the file "COPYING".
8 # Required software to build GeekOS:
9 # - GNU Make (http://www.gnu.org/software/make)
10 # - gcc 2.95.2 generating code for target (i386/ELF) and host platforms
11 # - nasm (http://nasm.sourceforge.net)
12 # - Perl5, AWK (any version), egrep
14 # Cygwin (http://cygwin.com) may be used to build GeekOS.
15 # Make sure that gcc, binutils, nasm, and perl are installed.
18 # - This makefile has been written carefully to work correctly
19 # with the -j (parallel make) option. I regularly use "make -j 2"
20 # to speed the build process on 2 processor systems.
23 # Base address of kernel
25 # Note: at top of memory minus three pages (GDT/TSS/IDT)
29 # Note that the code will initially load at 0x10000
31 # The setup code needs to copy it up to this address and jump there
33 KERNEL_BASE_ADDR := 0x00010000
35 # Kernel entry point function
36 KERNEL_ENTRY = $(SYM_PFX)Main
40 VPATH := $(PROJECT_ROOT)/src
42 #when -DNDEBUG is set the kassert functions are disabled
44 JRLDEBUG= -DSERIAL_PRINT_DEBUG=1 -DSERIAL_PRINT_DEBUG_LEVEL=10 -DSERIAL_PRINT=1
48 #Peter's compile flags
51 # Figure out if we're compiling with cygwin, http://cygwin.com
52 SYSTEM_NAME := $(shell uname -s)
53 ifeq ($(findstring CYGWIN,$(SYSTEM_NAME)),CYGWIN)
55 EXTRA_C_OPTS := -DNEED_UNDERSCORE -DGNU_WIN32
56 EXTRA_NASM_OPTS := -DNEED_UNDERSCORE
58 EXTRA_CC_USER_OPTS := -Dmain=geekos_main
64 # ----------------------------------------------------------------------
66 # Various options specifying how GeekOS should be built,
67 # what source files to build, which user programs to build,
68 # etc. This is generally the only section of the makefile
69 # that will need to be modified.
70 # ----------------------------------------------------------------------
72 # List of targets to build by default.
73 # These targets encompass everything needed to boot
75 ALL_TARGETS := fd.img vm_kernel
79 KERNEL_C_SRCS := idt.c int.c trap.c irq.c io.c \
81 keyboard.c screen.c timer.c \
83 gdt.c tss.c segment.c \
88 svm.c svm_handler.c vmm.c vmm_util.c vmm_stubs.c svm_ctrl_regs.c \
89 vmcb.c vmm_mem.c vmm_paging.c vmm_io.c vmm_debug.c svm_io.c \
90 vmm_shadow_paging.c vm_guest_mem.c \
91 debug.c vmx.c vmcs_gen.c vmcs.c\
94 # Kernel object files built from C source files
95 KERNEL_C_OBJS := $(KERNEL_C_SRCS:%.c=geekos/%.o)
97 # Kernel assembly files
98 KERNEL_ASM_SRCS := lowlevel.asm vmx_lowlevel.asm svm_lowlevel.asm
100 KERNEL_GAS_SRCS := testvm.s
102 # Kernel object files build from assembler source files
103 KERNEL_ASM_OBJS := $(KERNEL_ASM_SRCS:%.asm=geekos/%.o)
105 KERNEL_GAS_OBJS := $(KERNEL_GAS_SRCS:%.s=geekos/%.o)
108 # All kernel object files
109 KERNEL_OBJS := $(KERNEL_C_OBJS) \
110 $(KERNEL_ASM_OBJS) $(KERNEL_GAS_OBJS)
112 # Common library source files.
113 # This library is linked into both the kernel and user programs.
114 # It provides string functions and generic printf()-style
116 COMMON_C_SRCS := fmtout.c string.c memmove.c
118 # Common library object files.
119 COMMON_C_OBJS := $(COMMON_C_SRCS:%.c=common/%.o)
124 # ----------------------------------------------------------------------
126 # This section defines programs that are used to build GeekOS.
127 # ----------------------------------------------------------------------
129 # Uncomment if cross compiling
130 TARGET_CC_PREFIX := $(PROJECT_ROOT)/../devtools/i386/bin/i386-elf-
131 #TARGET_CC_PREFIX := i386-elf-
133 # Target C compiler. gcc 2.95.2 or later should work.
134 TARGET_CC := $(TARGET_CC_PREFIX)gcc
135 #TARGET_CC := $(TARGET_CC_PREFIX)gcc34 -m32
137 # Host C compiler. This is used to compile programs to execute on
138 # the host platform, not the target (x86) platform. On x86/ELF
139 # systems, such as Linux and FreeBSD, it can generally be the same
140 # as the target C compiler.
143 # Target linker. GNU ld is probably to only one that will work.
144 TARGET_LD := $(TARGET_CC_PREFIX)ld -melf_i386
147 TARGET_AR := $(TARGET_CC_PREFIX)ar
150 TARGET_RANLIB := $(TARGET_CC_PREFIX)ranlib
153 TARGET_NM := $(TARGET_CC_PREFIX)nm
156 TARGET_OBJCOPY := $(TARGET_CC_PREFIX)objcopy
158 # Nasm (http://nasm.sourceforge.net)
159 NASM := $(PROJECT_ROOT)/../devtools/bin/nasm
160 #NASM := /opt/vmm-tools/bin/nasm
164 # Tool to build PFAT filesystem images.
165 BUILDFAT := tools/builtFat.exe
170 # Pad a file so its size is a multiple of some unit (i.e., sector size)
171 PAD := $(PERL) $(PROJECT_ROOT)/scripts/pad
173 # Create a file filled with zeroes.
174 ZEROFILE := $(PERL) $(PROJECT_ROOT)/scripts/zerofile
176 # Calculate size of file in sectors
177 NUMSECS := $(PERL) $(PROJECT_ROOT)/scripts/numsecs
180 # ----------------------------------------------------------------------
182 # Options passed to the tools.
183 # ----------------------------------------------------------------------
185 # Flags used for all C source files
186 GENERAL_OPTS := -O -Wall $(EXTRA_C_OPTS) $(JRLDEBUG) $(PADFLAGS) -fPIC
187 CC_GENERAL_OPTS := $(GENERAL_OPTS) -Werror
189 # Flags used for kernel C source files
190 CC_KERNEL_OPTS := -g -DGEEKOS -I$(PROJECT_ROOT)/include
192 # Flags user for kernel assembly files
193 NASM_KERNEL_OPTS := -I$(PROJECT_ROOT)/src/geekos/ -f elf $(EXTRA_NASM_OPTS)
195 # Flags used for common library and libc source files
196 CC_USER_OPTS := -I$(PROJECT_ROOT)/include -I$(PROJECT_ROOT)/include/libc \
197 $(EXTRA_CC_USER_OPTS)
199 # Flags passed to objcopy program (strip unnecessary sections from kernel.exe)
200 OBJCOPY_FLAGS := -R .dynamic -R .note -R .comment
202 # ----------------------------------------------------------------------
204 # Describes how to compile the source files.
205 # ----------------------------------------------------------------------
207 # Compilation of kernel C source files
209 geekos/%.o : geekos/%.c
210 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o
213 # Compilation of kernel assembly source files
214 geekos/%.o : geekos/%.asm
215 $(NASM) $(NASM_KERNEL_OPTS) $< -o geekos/$*.o
217 # Compilation of test VM
218 geekos/%.o : geekos/%.s
219 $(AS) $< -o geekos/$*.o
221 geekos/%.o : geekos/%.S
222 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o
224 # Compilation of common library C source files
225 common/%.o : common/%.c
226 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_USER_OPTS) $< -o common/$*.o
228 # ----------------------------------------------------------------------
230 # Specifies files to be built
231 # ----------------------------------------------------------------------
233 # Default target - see definition of ALL_TARGETS in Configuration section
237 geekos/vmx_lowlevel.o: $(PROJECT_ROOT)/src/geekos/vmx_lowlevel.asm
240 -I$(PROJECT_ROOT)/src/geekos/ \
241 $(PROJECT_ROOT)/src/geekos/vmx_lowlevel.asm \
245 geekos/test: geekos/test.o geekos/vmcs.o geekos/vmx_lowlevel.o
246 $(CC) geekos/test.o geekos/vmcs.o geekos/vmx_lowlevel.o -o geekos/test
248 # Standard floppy image - just boots the kernel
249 fd.img : geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin vm_kernel
250 cat geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin > _temp
252 cat _temp vm_kernel > $@
254 # make ready to boot over PXE
256 cp fd.img /tftpboot/vmm.img
257 $(PAD) /tftpboot/vmm.img 1474560
261 # Floppy boot sector (first stage boot loader).
262 geekos/fd_boot.bin : geekos/setup.bin geekos/kernel.bin $(PROJECT_ROOT)/src/geekos/fd_boot.asm vm_kernel
264 -I$(PROJECT_ROOT)/src/geekos/ \
265 -DNUM_SETUP_SECTORS=`$(NUMSECS) geekos/setup.bin` \
266 -DNUM_KERN_SECTORS=`$(NUMSECS) geekos/kernel.bin` \
267 -DNUM_VM_KERNEL_SECTORS=`$(NUMSECS) vm_kernel` \
268 $(PROJECT_ROOT)/src/geekos/fd_boot.asm \
271 # Setup program (second stage boot loader).
272 geekos/setup.bin : geekos/kernel.bin $(PROJECT_ROOT)/src/geekos/setup.asm
274 -I$(PROJECT_ROOT)/src/geekos/ \
275 -DENTRY_POINT=0x`egrep 'Main$$' geekos/kernel.syms |awk '{print $$1}'` \
276 -DVMM_SIZE=`$(NUMSECS) geekos/kernel.bin` \
277 -DGUEST_SIZE=`$(NUMSECS) vm_kernel` \
278 $(PROJECT_ROOT)/src/geekos/setup.asm \
282 # Loadable (flat) kernel image.
283 geekos/kernel.bin : geekos/kernel.exe
284 $(TARGET_OBJCOPY) $(OBJCOPY_FLAGS) -S -O binary geekos/kernel.exe geekos/kernel.bin
287 # The kernel executable and symbol map.
288 geekos/kernel.exe : $(KERNEL_OBJS) $(COMMON_C_OBJS)
289 $(TARGET_LD) -o geekos/kernel.exe -Ttext $(KERNEL_BASE_ADDR) -e $(KERNEL_ENTRY) \
290 $(KERNEL_OBJS) $(COMMON_C_OBJS)
291 $(TARGET_NM) geekos/kernel.exe > geekos/kernel.syms
299 @echo "VM kernel lives at 0x100000 and is" `$(NUMSECS) vm_kernel` "sectors long"
303 vmm_mem_test: geekos/vmm_mem.c
304 $(HOST_CC) -m32 -o mem_test -DVMM_MEM_TEST -I../include ../src/geekos/vmm_mem.c
307 # Clean build directories of generated files
309 for d in geekos common libc user tools; do \
310 (cd $$d && rm -f *); \
314 # Build header file dependencies, so source files are recompiled when
315 # header files they depend on are modified.
316 depend : $(GENERATED_LIBC_SRCS)
317 $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) \
318 $(KERNEL_C_SRCS:%.c=$(PROJECT_ROOT)/src/geekos/%.c) \
319 | $(PERL) -n -e 's,^(\S),geekos/$$1,;print' \
321 $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_USER_OPTS) \
322 $(COMMON_C_SRCS:%.c=$(PROJECT_ROOT)/src/common/%.c) \
323 | $(PERL) -n -e 's,^(\S),common/$$1,;print' \
326 # By default, there are no header file dependencies.