Skip to content

Commit 7eb9f8a

Browse files
Viktor EngelmannViktor Engelmann
Viktor Engelmann
authored and
Viktor Engelmann
committedJul 4, 2016
1 parent 37e2927 commit 7eb9f8a

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
 

‎kernel.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
void printf(char* str)
3+
{
4+
static unsigned short* VideoMemory = (unsigned short*)0xb8000;
5+
6+
for(int i = 0; str[i] != '\0'; ++i)
7+
VideoMemory[i] = (VideoMemory[i] & 0xFF00) | str[i];
8+
}
9+
10+
11+
12+
typedef void (*constructor)();
13+
extern "C" constructor start_ctors;
14+
extern "C" constructor end_ctors;
15+
extern "C" void callConstructors()
16+
{
17+
for(constructor* i = &start_ctors; i != &end_ctors; i++)
18+
(*i)();
19+
}
20+
21+
22+
23+
extern "C" void kernelMain(const void* multiboot_structure, unsigned int /*multiboot_magic*/)
24+
{
25+
printf("Hello World! --- http://www.AlgorithMan.de");
26+
27+
while(1);
28+
}

‎linker.ld

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
ENTRY(loader)
2+
OUTPUT_FORMAT(elf32-i386)
3+
OUTPUT_ARCH(i386:i386)
4+
5+
SECTIONS
6+
{
7+
. = 0x0100000;
8+
9+
.text :
10+
{
11+
*(.multiboot)
12+
*(.text*)
13+
*(.rodata)
14+
}
15+
16+
.data :
17+
{
18+
start_ctors = .;
19+
KEEP(*( .init_array ));
20+
KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* )));
21+
end_ctors = .;
22+
23+
*(.data)
24+
}
25+
26+
.bss :
27+
{
28+
*(.bss)
29+
}
30+
31+
/DISCARD/ : { *(.fini_array*) *(.comment) }
32+
}

‎loader.s

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.set MAGIC, 0x1badb002
2+
.set FLAGS, (1<<0 | 1<<1)
3+
.set CHECKSUM, -(MAGIC + FLAGS)
4+
5+
.section .multiboot
6+
.long MAGIC
7+
.long FLAGS
8+
.long CHECKSUM
9+
10+
11+
.section .text
12+
.extern kernelMain
13+
.extern callConstructors
14+
.global loader
15+
16+
17+
loader:
18+
mov $kernel_stack, %esp
19+
call callConstructors
20+
push %eax
21+
push %ebx
22+
call kernelMain
23+
24+
25+
_stop:
26+
cli
27+
hlt
28+
jmp _stop
29+
30+
31+
.section .bss
32+
.space 2*1024*1024; # 2 MiB
33+
kernel_stack:
34+

‎makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# sudo apt-get install g++ binutils libc6-dev-i386
3+
4+
GCCPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore
5+
ASPARAMS = --32
6+
LDPARAMS = -melf_i386
7+
8+
objects = loader.o kernel.o
9+
10+
11+
12+
%.o: %.cpp
13+
gcc $(GCCPARAMS) -c -o $@ $<
14+
15+
%.o: %.s
16+
as $(ASPARAMS) -o $@ $<
17+
18+
mykernel.bin: linker.ld $(objects)
19+
ld $(LDPARAMS) -T $< -o $@ $(objects)
20+
21+
install: mykernel.bin
22+
sudo cp $< /boot/mykernel.bin
23+

0 commit comments

Comments
 (0)
Please sign in to comment.