Browse files

qemu: configuration to use qemu debug console

QEMU provides a debug console for x86 CPUs which could be binded on
standard output. It really easy to use so it could be an easy way to
debug when display is not available yet. The support for this console is
now available in boot process as a callback from print library.

A flexible way to define kernel configuration is now available in build
chain: it is possible to define a configuration in kernel-configs.mk
which modify compilation flags and so on to fit new requirements when
the target is called.

New target are available in global makefile to directly one or another
emulator depending on which functionnality you need.
  • Loading branch information...
1 parent 8e49595 commit a24859dff4443bc73d79590d4748f368fd723fc1 @ddejean committed Feb 20, 2012
Showing with 155 additions and 25 deletions.
  1. +2 −2 .gitignore
  2. +27 −10 Makefile
  3. +27 −13 kernel/Makefile
  4. +29 −0 kernel/boot/qemu.c
  5. +22 −0 kernel/boot/qemu.h
  6. +7 −0 kernel/boot/stage1.c
  7. +22 −0 kernel/build/kernel-configs.mk
  8. +19 −0 kernel/build/utils.mk
View
4 .gitignore
@@ -1,5 +1,5 @@
disk.img
disk
-kernel.bin
-build
+*.bin
+out
*.swp
View
37 Makefile
@@ -3,28 +3,45 @@
# by Damien Dejean <djod4556@yahoo.fr>  #
#########################################
-all: kernel.bin disk
+# Kernel configurations
+include kernel/build/kernel-configs.mk
+.DEFAULT_GOAL := $(KERNEL_DEFAULT)
+
+# Copy the kernel binary to a disk image an run bochs
+bochs: update-disk
+ bochs
+
+# Boot the kernel with qemu multiboot loader
+qemu: $(KERNEL_DEFAULT)
+ qemu -kernel kernel/$<
+
+# Run qemu with a debug console using embedded multiboot loader
+qemu-debug: $(KERNEL_QEMU_DEBUG)
+ qemu -kernel kernel/$< -debugcon stdio
+
+# Run qemu with a debug console using embedded multiboot loader
+qemu-disk: update-disk
+ qemu -hda disk.img
+
+# Update disk image with new compiled kernel
+.PHONY: update-disk
+update-disk: disk $(KERNEL_DEFAULT)
@echo "### This target will require root access to mont disk image ! ###"
sudo losetup -o 1048576 /dev/loop0 disk.img
sudo mount -t ext2 /dev/loop0 disk/
- sudo cp kernel.bin disk/
+ sudo cp kernel/$(KERNEL_DEFAULT) disk/
sync
sudo umount disk/
sudo losetup -d /dev/loop0
- rm -f kernel.bin
disk:
mkdir disk/
-kernel.bin: kernel/kernel.bin
- cp kernel/kernel.bin .
-
-.PHONY: kernel/kernel.bin
-kernel/kernel.bin:
- make -C kernel/ kernel.bin
+.PHONY: $(KERNEL_CONFIGS)
+$(KERNEL_CONFIGS):
+ make -C kernel/ $@
.PHONY: clean
clean:
make -C kernel/ clean
- rm -f kernel.bin
View
40 kernel/Makefile
@@ -10,11 +10,15 @@ CPP := g++
AS := gcc
LD := ld
-# Build parameters
-FLAGS := -m32 -Wall -Wextra -Werror -g -gstabs -pipe
-CFLAGS := $(FLAGS) -std=c99 -nostdinc -fno-stack-protector
-CXXFLAGS := $(FLAGS) -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
-LDFLAGS := -melf_i386
+# Include util functions
+include build/utils.mk
+
+### Build parameters ###
+# Recursively expanded variables to allow flag redefinition by kernel-configs.mk
+FLAGS = -m32 -Wall -Wextra -Werror -g -gstabs -pipe
+CFLAGS = $(FLAGS) -std=c99 -nostdinc -fno-stack-protector
+CXXFLAGS = $(FLAGS) -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
+LDFLAGS = -melf_i386
# Build sources
DIRS := . boot lib
@@ -33,13 +37,23 @@ vpath %.c $(DIRS)
vpath %.cpp $(DIRS)
vpath %.s $(DIRS)
-# Build products
-OUTPUT := build
-OBJS := $(strip crt0.o $(filter-out crt0.o,$(notdir $(patsubst %.s,%.o,$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(FILES)))))))
-OBJS := $(addprefix $(OUTPUT)/, $(OBJS))
+# Output configuration
+OUTPUT_BASE := out
+
+# Include all kernel configs:
+# defines different targets, flags and output directories
+include build/kernel-configs.mk
+
+### Create list of output files ###
+# We want a list of obj files
+OBJS := $(call targetize, $(FILES))
+# The bootstrap file must be linked first
+OBJS := $(call first-and-unique, crt0.o, $(OBJS))
+# Set the output directory
+OBJS := $(addprefix $(OUTPUT)/, $(OBJS))
-# Build rules
-kernel.bin: kernel.lds $(OBJS)
+### Build rules ###
+$(KERNEL_CONFIGS): kernel.lds $(OBJS)
$(LD) $(LDFLAGS) -e entry -T $< -o $@ $(filter-out kernel.lds, $^)
$(OUTPUT)/%.o: %.s $(OUTPUT)
@@ -56,6 +70,6 @@ $(OUTPUT):
.PHONY: clean
clean:
- rm -f kernel.bin
- rm -rf build/
+ rm -f $(KERNEL_CONFIGS)
+ rm -rf out/
View
29 kernel/boot/qemu.c
@@ -0,0 +1,29 @@
+/*
+ * qemu.h
+ *
+ * Copyright (C) 2012 Simple Object Kernel project
+ * by Damien Dejean <djod4556@yahoo.fr>
+ *
+ * QEMU debug console support. Every char written on the QEMU port
+ * is printed on the qemu debug console. Use -debugcon flag to enable
+ * it on QEMU run.
+ */
+
+#ifndef _QEMU_H_
+#define _QEMU_H_
+
+#include "cpu.h"
+
+#define QEMU_PORT 0xE9
+
+void qemu_putbytes(const char *str, int len)
+{
+ int i;
+
+ for (i = 0; i < len && *str != '\0'; i++, str++) {
+ outb(*str, QEMU_PORT);
+ }
+}
+
+#endif
+
View
22 kernel/boot/qemu.h
@@ -0,0 +1,22 @@
+/*
+ * qemu.h
+ *
+ * Copyright (C) 2012 Simple Object Kernel project
+ * by Damien Dejean <djod4556@yahoo.fr>
+ *
+ * QEMU debug console support.
+ */
+
+#ifndef _QEMU_H_
+#define _QEMU_H_
+
+/**
+ * Writes the string <str> on QEMU debug console.
+ *
+ * @param str the string to put on the console
+ * @param len the length of the <str> string
+ */
+void qemu_putbytes(const char *str, int len);
+
+#endif
+
View
7 kernel/boot/stage1.c
@@ -7,6 +7,8 @@
*/
#include "vga.h"
+#include "qemu.h"
+#include "cpu.h"
#include "putbytes.h"
#include "stdio.h"
@@ -16,7 +18,12 @@ void stage1_main(unsigned int multiboot_magic, void *multiboot_info)
(void) multiboot_info;
/* Prepare the display for this stage1 */
+#ifdef QEMU_DEBUG
+ putbytes_callback(qemu_putbytes);
+#else
putbytes_callback(vga_putbytes);
+#endif
+ printf("vga_clear");
vga_clear();
/* Display a stage1 message */
View
22 kernel/build/kernel-configs.mk
@@ -0,0 +1,22 @@
+##################################################################
+# Simple Object Kernel project Makefile #
+# by Damien Dejean <djod4556@yahoo.fr>  #
+# Provides definitions of different kernel build configurations. #
+##################################################################
+
+# Default kernel configuration, no specific debug options
+KERNEL_DEFAULT := kernel-default.bin
+ifeq ($(MAKECMDGOALS),$(KERNEL_DEFAULT))
+ OUTPUT := $(OUTPUT_BASE)/default
+endif
+
+# Kernel with qemu debug console defined as default output in stage1
+KERNEL_QEMU_DEBUG := kernel-qemu-debug.bin
+$(KERNEL_QEMU_DEBUG): FLAGS += -DQEMU_DEBUG
+ifeq ($(MAKECMDGOALS),$(KERNEL_QEMU_DEBUG))
+ OUTPUT := $(OUTPUT_BASE)/qemu-debug
+endif
+
+# Summary of all kernel configs
+KERNEL_CONFIGS := $(KERNEL_DEFAULT) $(KERNEL_QEMU_DEBUG) $(KERNEL_BIDULE)
+
View
19 kernel/build/utils.mk
@@ -0,0 +1,19 @@
+#########################################################
+# Simple Object Kernel project Makefile #
+# by Damien Dejean <djod4556@yahoo.fr>  #
+# Provides functions to simplify main Makefile content. #
+#########################################################
+
+# targetize:
+# Creates compilation target files from source files
+define targetize
+$(notdir $(patsubst %.s,%.o,$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(1)))))
+endef
+
+# first-and-unique:
+# Ensure that the provided file is the first one and the unique instance of the list
+# Prototype: $(first-and-unique file, file-list)
+define first-and-unique
+$(strip $(1) $(filter-out $(1), $(2)))
+endef
+

0 comments on commit a24859d

Please sign in to comment.