summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gdbinit1
-rw-r--r--Makefile55
-rw-r--r--Makefile.common3
-rw-r--r--README.md2
-rw-r--r--asm/kernel_entry.asm4
-rw-r--r--asm/multiboot.s77
-rw-r--r--kernel/config.h2
-rw-r--r--kernel/kernel.c19
-rw-r--r--kernel/syscalls.c1
-rw-r--r--linker.ld19
10 files changed, 149 insertions, 34 deletions
diff --git a/.gdbinit b/.gdbinit
new file mode 100644
index 0000000..24efe1e
--- /dev/null
+++ b/.gdbinit
@@ -0,0 +1 @@
+target remote localhost:1234
diff --git a/Makefile b/Makefile
index c090265..94d4c7a 100644
--- a/Makefile
+++ b/Makefile
@@ -14,24 +14,23 @@
############ some constants ############
+FOOLOS=FoolOS.img
+FOOLOS_VDI=FoolOS.vdi
#submodules
SUBDIRS=boot1 boot2 font userspace
-FOOLOS=FoolOS.img
-FOOLOS_VDI=FoolOS.vdi
-
#take care to set this properly!
USB_STICK=/dev/sdf
#use our cross compiler
-CC=i686-foolos-gcc
+CC=i686-elf-gcc
############ compiler flags ############
CFLAGS=
-CFLAGS+=-fstack-protector-all
-CFLAGS+=-Werror-implicit-function-declaration
-CFLAGS+=-w
+## CFLAGS+=-fstack-protector-all
+## CFLAGS+=-Werror-implicit-function-declaration
+## CFLAGS+=-w
CFLAGS+=-ffreestanding
CFLATS+=-Wall
CFLAGS+=-Wextra
@@ -41,6 +40,7 @@ CFLAGS+=-Wextra
CFLAGS+=-std=gnu11
CFLAGS+=-I.
CFLAGS+=-I/home/miguel/temp/fool-os-stuff/newlib-2.1.0/newlib/libc/include
+CFLAGS+=-gstabs
#CFLAGS+=-lgcc
#CFLAGS+=-fno-zero-initialized-in-bss
#CFLAGS+= -O4
@@ -78,18 +78,19 @@ DEPS=$(patsubst %.c, %.d, $(SOURCES))
ASM_SOURCES=$(wildcard ./asm/*.asm)
ASM_OBJECTS=$(patsubst %.asm, %.o, $(ASM_SOURCES))
-KERNEL_ENTRY=./asm/kernel_entry.o
-
+ASM_MULTIBOOT=$(wildcard ./asm/*.s)
+ASM_MULTIBOOT_OBJ=$(patsubst %.s, %.o, $(ASM_MULTIBOOT))
############ Final Targets ############
-all: FoolOS.vdi FoolOS.img
+all: $(FOOLOS) #$(FOOLOS_VDI)
new: clean all
+run: run-qemu
+newrun: clean run
-
-########### INCLUDES ###################3
+########### INCLUDES ###################
include Makefile.common
-include $(DEPS)
@@ -97,6 +98,7 @@ include Makefile.common
#### BINARIES AND SUBMODULES ########
+KERNEL_IMG=foolos.img
BIN_KERNEL=kernel.bin
BIN_MBR=./boot1/mbr.bin
BIN_STAGE2=./boot2/stage2.bin
@@ -124,21 +126,23 @@ $(CLEANDIRS):
############ linking kernel binary ############
-# the kernel_entry.o needs to be FIRST!!
-$(BIN_KERNEL): $(KERNEL_ENTRY) $(ASM_OBJECTS) $(OBJECTS)
+$(KERNEL_IMG): $(ASM_MULTIBOOT_OBJ) $(ASM_OBJECTS) $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -T linker.ld -o $@ $^
############ assembling of final image ############
# master boot record, kernel binary and fool-font
-$(FOOLOS): $(BIN_KERNEL) $(SUBDIRS)
- dd if=$(BIN_MBR) of=$@ bs=512 seek=0 conv=notrunc
- dd if=$(BIN_STAGE2) of=$@ bs=512 seek=1 conv=notrunc
- # kernel will start at 0x100000 in ram (this is what the booloader starts loading secotr: 10)
- dd if=$(BIN_KERNEL) of=$@ bs=512 seek=10 conv=notrunc # kernel can be about 128KB (incl bss)
- # the data will starts at 0x120000 in ram
- dd if=$(IMG_USERSPACE) of=$@ bs=512 seek=266 conv=notrunc
+$(FOOLOS): $(KERNEL_IMG) # $(SUBDIRS)
+ sudo cp $(KERNEL_IMG) /mnt/vdi/boot
+ sync
+
+# dd if=$(BIN_MBR) of=$@ bs=512 seek=0 conv=notrunc
+# dd if=$(BIN_STAGE2) of=$@ bs=512 seek=1 conv=notrunc
+# # kernel will start at 0x100000 in ram (this is what the booloader starts loading secotr: 10)
+# dd if=$(BIN_KERNEL) of=$@ bs=512 seek=10 conv=notrunc # kernel can be about 128KB (incl bss)
+# # the data will starts at 0x120000 in ram
+# dd if=$(IMG_USERSPACE) of=$@ bs=512 seek=266 conv=notrunc
############ virtual machines stuff ############
@@ -152,15 +156,12 @@ $(FOOLOS_VDI): $(FOOLOS)
run-bochs: all
~/opt/bochs-2.6.6/bochs -q -f bochs/bochsrc -rc bochs/bochsdebug
+# run in qemu
run-qemu: all
qemu-system-i386 $(FOOLOS)
run-qemu-debug: all
- qemu-system-i386 -s -S $(FOOLOS)
-
-run: run-bochs
-
-newrun: clean run
+ qemu-system-i386 -s -S ~/temp/FoolOs/disk.img
############ create bootable usb image ############
@@ -172,4 +173,4 @@ stick: $(FOOLOS)
############ cleanup ############
clean: $(CLEANDIRS)
- -rm *.bin $(FOOLOS) $(FOOLOS_VDI) $(KERNEL_ENTRY) $(ASM_OBJECTS) $(OBJECTS) $(IMG_FILLUP) $(BIN_MBR) $(BIN_MP) $(BIN_STAGE2) $(DEPS) bochs.out bochs.log ne2k-tx.log ne2k-txdump.txt
+ -rm *.bin $(FOOLOS) $(FOOLOS_VDI) $(KERNEL_ENTRY) $(ASM_MULTIBOOT_OBJ) $(KERNEL_IMG) $(ASM_OBJECTS) $(OBJECTS) $(IMG_FILLUP) $(BIN_MBR) $(BIN_MP) $(BIN_STAGE2) $(DEPS) bochs.out bochs.log ne2k-tx.log ne2k-txdump.txt
diff --git a/Makefile.common b/Makefile.common
index 4cd411d..5436c87 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -4,6 +4,9 @@
%.bin: %.asm
nasm -f bin $*.asm -o $@
+
+%.o: %.s
+ i686-elf-as $*.s -o $@
%.o: %.c
$(CC) -c $(CFLAGS) $*.c -o $*.o
diff --git a/README.md b/README.md
index ec7cc6f..a887b66 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ as well as VESA and a couple of other things.
![Screenshot of FoolOS](/screenshots/foolos.png?raw=true "FoolOs Kernel")
-Copyright M.Idziorek 2014 <m.i@gmx.at> unless stated otherwise!
+Copyright M.Idziorek 2014-2015 <m.i@gmx.at> unless stated otherwise!
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
diff --git a/asm/kernel_entry.asm b/asm/kernel_entry.asm
index 53f63ad..7dacee3 100644
--- a/asm/kernel_entry.asm
+++ b/asm/kernel_entry.asm
@@ -6,9 +6,13 @@
; to simplify the entrance!
;
;
+global kernel_start
+
[bits 32]
[extern kernel_main]
+kernel_start:
+
push 0x1
cmp eax,1
diff --git a/asm/multiboot.s b/asm/multiboot.s
new file mode 100644
index 0000000..08cc87e
--- /dev/null
+++ b/asm/multiboot.s
@@ -0,0 +1,77 @@
+# Declare constants used for creating a multiboot header.
+.set ALIGN, 1<<0 # align loaded modules on page boundaries
+.set MEMINFO, 1<<1 # provide memory map
+.set FLAGS, ALIGN | MEMINFO # this is the Multiboot 'flag' field
+.set MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header
+.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot
+
+# Declare a header as in the Multiboot Standard. We put this into a special
+# section so we can force the header to be in the start of the final program.
+# You don't need to understand all these details as it is just magic values that
+# is documented in the multiboot standard. The bootloader will search for this
+# magic sequence and recognize us as a multiboot kernel.
+.section .multiboot
+.align 4
+.long MAGIC
+.long FLAGS
+.long CHECKSUM
+
+# Currently the stack pointer register (esp) points at anything and using it may
+# cause massive harm. Instead, we'll provide our own stack. We will allocate
+# room for a small temporary stack by creating a symbol at the bottom of it,
+# then allocating 16384 bytes for it, and finally creating a symbol at the top.
+.section .bootstrap_stack, "aw", @nobits
+stack_bottom:
+.skip 16384 # 16 KiB
+stack_top:
+
+# The linker script specifies _start as the entry point to the kernel and the
+# bootloader will jump to this position once the kernel has been loaded. It
+# doesn't make sense to return from this function as the bootloader is gone.
+.section .text
+.global _start
+.type _start, @function
+_start:
+ # Welcome to kernel mode! We now have sufficient code for the bootloader to
+ # load and run our operating system. It doesn't do anything interesting yet.
+ # Perhaps we would like to call printf("Hello, World\n"). You should now
+ # realize one of the profound truths about kernel mode: There is nothing
+ # there unless you provide it yourself. There is no printf function. There
+ # is no <stdio.h> header. If you want a function, you will have to code it
+ # yourself. And that is one of the best things about kernel development:
+ # you get to make the entire system yourself. You have absolute and complete
+ # power over the machine, there are no security restrictions, no safe
+ # guards, no debugging mechanisms, there is nothing but what you build.
+
+ # By now, you are perhaps tired of assembly language. You realize some
+ # things simply cannot be done in C, such as making the multiboot header in
+ # the right section and setting up the stack. However, you would like to
+ # write the operating system in a higher level language, such as C or C++.
+ # To that end, the next task is preparing the processor for execution of
+ # such code. C doesn't expect much at this point and we only need to set up
+ # a stack. Note that the processor is not fully initialized yet and stuff
+ # such as floating point instructions are not available yet.
+
+ # To set up a stack, we simply set the esp register to point to the top of
+ # our stack (as it grows downwards).
+ movl $stack_top, %esp
+
+ # We are now ready to actually execute C code. We cannot embed that in an
+ # assembly file, so we'll create a kernel.c file in a moment. In that file,
+ # we'll create a C entry point called kernel_main and call it here.
+ call kernel_start
+
+ # In case the function returns, we'll want to put the computer into an
+ # infinite loop. To do that, we use the clear interrupt ('cli') instruction
+ # to disable interrupts, the halt instruction ('hlt') to stop the CPU until
+ # the next interrupt arrives, and jumping to the halt instruction if it ever
+ # continues execution, just to be safe. We will create a local label rather
+ # than real symbol and jump to there endlessly.
+ cli
+ hlt
+.Lhang:
+ jmp .Lhang
+
+# Set the size of the _start symbol to the current location '.' minus its start.
+# This is useful when debugging or when you implement call tracing.
+.size _start, . - _start
diff --git a/kernel/config.h b/kernel/config.h
index 26d056d..216641c 100644
--- a/kernel/config.h
+++ b/kernel/config.h
@@ -9,7 +9,7 @@
#define FOOLOS_CONSOLE_AUTOBREAK // add newline automatically at end of line
//#define FOOLOS_LOG_OFF // do not log anything
-//#define FOOLOS_CONSOLE // otherwise VESA will be used!
+#define FOOLOS_CONSOLE // otherwise VESA will be used!
#define FOOLSOS_SHOW_VESAMODES
#define MEM_PRINT_MEMORYMAP
#define LOG_BUF_SIZE 4069
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 401204a..8bd60ca 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -16,6 +16,7 @@
#include "console.h"
+#include <stddef.h>
// for built-in shell
#include "lib/buffer/ringbuffer.h"
@@ -25,6 +26,22 @@
// mp informs us if this if this is the main processor
void kernel_main(uint32_t initial_stack, int mp)
{
+
+/*
+ int i=0;
+ while(true)
+ {
+ size_t *terminal_buffer = (uint16_t*) 0xB8000;
+ uint16_t c16='X';
+ i++;
+ if (i%2)c16='_';
+
+ c16 = c16 | 4 << 8;
+ terminal_buffer[2] = c16;
+ }
+ */
+
+
log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initial_stack: 0x%08X",initial_stack);
//
// Configuring the PIT timer.
@@ -55,7 +72,7 @@ void kernel_main(uint32_t initial_stack, int mp)
// log buffered messages to console
log_log();
- while(1);
+ //while(1);
//
// Setup Interrupts (code segment: 0x08)
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index 63c3ce5..6d46f1a 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -8,6 +8,7 @@
#include "kernel/config.h"
#include <sys/stat.h>
#include <stdbool.h>
+#include <stddef.h>
int syscall_unhandled(int nr)
diff --git a/linker.ld b/linker.ld
index 52a32ef..92ca00f 100644
--- a/linker.ld
+++ b/linker.ld
@@ -1,24 +1,35 @@
-OUTPUT_FORMAT(binary)
+ENTRY(_start)
SECTIONS
{
- . = 0x100000;
+ . = 2M;
.text BLOCK(4K) : ALIGN(4K)
{
+ *(.multiboot)
*(.text)
}
- .bss BLOCK(4K) : ALIGN(4K)
+ /* Read-only data. */
+ .rodata BLOCK(4K) : ALIGN(4K)
{
- *(.bss)
+ *(.rodata)
}
+ /* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
+ /* Read-write data (uninitialized) and stack */
+ .bss BLOCK(4K) : ALIGN(4K)
+ {
+ *(COMMON)
+ *(.bss)
+ *(.bootstrap_stack)
+ }
+
kernel_end = .;
}