Skip to content

Commit 592cdbe

Browse files
authored
Merge pull request #68 from G3bE/master
Improve docs on creating a simple kernel
2 parents 21fafea + a8bd937 commit 592cdbe

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

docs/README.md

+62-2
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,74 @@ void _start(void) {
6464
}
6565
```
6666

67+
The kernel needs a linker script to be loaded to 1 MiB, replacing NASMs `ORG` instruction.
68+
The file can be called `kernel.ld`.
69+
70+
```
71+
OUTPUT_FORMAT("binary")
72+
OUTPUT_ARCH("i386:x86-64")
73+
74+
SECTIONS
75+
{
76+
. = 0x100000;
77+
.text : {
78+
*(.text)
79+
}
80+
.data : {
81+
*(.data)
82+
}
83+
.rodata : {
84+
*(.rodata)
85+
}
86+
.bss : {
87+
*(.bss)
88+
}
89+
}
90+
91+
```
92+
6793
Compile is like this:
6894

6995
```
70-
gcc kernel.c -o kernel -mno-red-zone -fno-stack-protector -fomit-frame-pointer
96+
gcc -c kernel.c -o kernel.o -mno-red-zone -fno-stack-protector -fomit-frame-pointer
97+
ld -T kernel.ld -o kernel.bin kernel.o
98+
```
99+
100+
The flags added to the first command are there to help GCC produce could that will run in kernel space.
101+
The second command simply takes kernel.o and orders it as the linker script tells it to.
102+
103+
## Contributors note
104+
105+
The `_start` symbol must always appear first within flat binaries as Pure64 will call the start of the file so it must contain executable code. Function definitions (such as inline ones) in header files could interfere with the placement of the `_start` symbol. The best solution is to put the entry point in a separate file that calls the main function. Such a file could be called `start.c`.
106+
```
107+
extern int main(void);
108+
109+
void _start(void)
110+
{
111+
main();
112+
}
113+
```
114+
This file would **always** have to be linked in front of everything else. For the above example that would mean the linker command above would have to become:
115+
```
116+
ld -T kernel.ld -o kernel.bin start.o kernel.o
71117
```
72118

73-
The flags added to the command are there to help GCC produce could that will run in kernel space.
119+
## Creating a bootable image
120+
121+
After creating a kernel this is a possible routine to create a bootable image.
122+
The commands require Pure64 to be build and `pure64.sys` and `mbr.sys` to be in the same directory
123+
as your kernel with the name `kernel.bin`
124+
125+
```
126+
dd if=/dev/zero of=disk.img count=128 bs=1048576
127+
cat pure64.sys kernel.bin > software.sys
128+
129+
dd if=mbr.sys of=disk.img conv=notrunc
130+
dd if=software.sys of=disk.img bs=512 seek=16 conv=notrunc
131+
```
74132

133+
After creating a bootable image it can be tested using qemu:
134+
`qemu-system-x86_64 -drive format=raw,file=disk.img`
75135

76136
## Memory Map
77137

0 commit comments

Comments
 (0)