Skip to content

Commit

Permalink
Writing C in Protected Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
taikiy committed Feb 21, 2023
1 parent 7d9e728 commit 2d58737
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.code-workspace
*.bin
bin/*
build/*
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FILES = ./build/kernel.asm.o
FILES = ./build/kernel.asm.o ./build/kernel.o
INCLUDES = -I./src
FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parammeter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc

all: ./bin/boot.bin ./bin/kernel.bin
rm -rf ./bin/os.bin
Expand All @@ -11,11 +13,14 @@ all: ./bin/boot.bin ./bin/kernel.bin

./bin/kernel.bin: $(FILES)
i686-elf-ld -g -relocatable $(FILES) -o ./build/kernelfull.o
i686-elf-gcc -T ./src/linker.ld -o ./bin/kernel.bin -ffreestanding -O0 -nostdlib ./build/kernelfull.o
i686-elf-gcc $(FLAGS) -T ./src/linker.ld -o ./bin/kernel.bin ./build/kernelfull.o

./build/kernel.asm.o: ./src/kernel.asm
nasm -f elf -g ./src/kernel.asm -o ./build/kernel.asm.o

./build/kernel.o: ./src/kernel.c
i686-elf-gcc $(INCLUDES) $(FLAGS) -std=gnu99 -c ./src/kernel.c -o ./build/kernel.o

run:
qemu-system-x86_64 -hda ./bin/os.bin

Expand Down
2 changes: 1 addition & 1 deletion doc/protected_mode_development.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ general:

---

## Notes
## References

- [GDB to LLDB command map](https://lldb.llvm.org/use/map.html#examining-thread-state)
- [Entering Protected Mode tutorial](http://www.osdever.net/tutorials/view/the-world-of-protected-mode)
4 changes: 4 additions & 0 deletions doc/protected_mode_development_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ Quick summary of how our code works with memory.
- Note that aligning `boot.asm` to 512 bytes is unrelated to the memory alignment issue we talk about here. The bootloader must have the boot signature 0x55AA at 511 and 512 byte.
- Make sure that `kernel.asm.o` is the first file to be linked. That ensures `kernel.asm.o` is located in the `.text` section ([`linker.ld`](../src/linker.ld)) when linked, and always starts at 0x100000. `kernel.asm.o` is 512 bytes long, so any other C object files linked after that are automatically aligned.
- In other kernel assembly files, specify `.asm` section so that they are linked at the end of the object file. If the assembled code is not a multiple of 4 bytes, that's okay because those files are at the end.

---

[continue](./protected_mode_development_3.md)
7 changes: 7 additions & 0 deletions doc/protected_mode_development_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Protected Mode Development - Part 3

## Writing C program in Protected Mode

Now, we start writing our kernel in C. [commit]()

Debugging steps are the same as what we did in the [previous note](./protected_mode_development_2.md) "5. Build, run, and debug".
4 changes: 4 additions & 0 deletions src/kernel.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

global _start

extern kernel_main

CODE_SEG equ 0x08
DATA_SEG equ 0x10

Expand All @@ -20,6 +22,8 @@ _start:
or al, 2
out 0x92, al ; Write to the bus line 92

call kernel_main

cld ; Clears direction flag
cli ; Disables interrupts
hlt ; This hangs the computer
Expand Down
5 changes: 5 additions & 0 deletions src/kernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "kernel.h"

void kernel_main()
{
}
6 changes: 6 additions & 0 deletions src/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef KERNEL_H
#define KERNEL_H

void kernel_main();

#endif

0 comments on commit 2d58737

Please sign in to comment.