Create a os kernel using limine
- User
Create a os kernel using limine
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create a os kernel using limine
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a basic OS kernel using Limine bootloader. This example will create a minimal kernel that boots and displays some text on the screen.
First, let's set up the project structure:
my_kernel/
├── src/
│ ├── kernel.c
│ └── linker.ld
├── Makefile
└── limine.cfg
src/kernel.c):#include <stdint.h>
#include <stddef.h>
#include <limine.h>
// Limine requests
static volatile struct limine_terminal_request terminal_request = {
.id = LIMINE_TERMINAL_REQUEST,
.revision = 0
};
// Halt function
static void hlt(void) {
for (;;) {
__asm__("hlt");
}
}
// Kernel entry point
void _start(void) {
// Ensure we got a terminal
if (terminal_request.response == NULL
|| terminal_request.response->terminal_count < 1) {
hlt();
}
// Get the first terminal
struct limine_terminal *terminal = terminal_request.response->terminals[0];
// Print a message
terminal_request.response->write(terminal, "Hello from my kernel!", 20);
// Halt the CPU
hlt();
}
src/linker.ld):OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
PHDRS
{
text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */
rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */
data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */
}
SECTIONS
{
. = 0xffffffff80000000;
.text : {
*(.text .text.*)
} :text
. = ALIGN(4K);
.rodata : {
*(.rodata .rodata.*)
} :rodata
. = ALIGN(4K);
.data : {
*(.data .data.*)
} :data
.bss : {
*(COMMON)
*(.bss .bss.*)
} :data
}
limine.cfg):TIMEOUT=0
DEFAULT_ENTRY=1
:My Kernel
PROTOCOL=limine
KERNEL_PATH=boot:///kernel.elf
# Compiler and linker
CC = gcc
LD = ld
# Flags
CFLAGS = -Wall -Wextra -O2 -pipe -fno-stack-protector -fno-pic \
-mno-80387 -mno-mmx -mno-3dnow -mno-sse -mno-sse2 \
-fno-pie -fno-pic -m64 -march=x86-64 -mabi=sysv \
-mcmodel=kernel -ffreestanding -fno-common
LDFLAGS = -nostdlib -static -m elf_x86_64 -z max-page-size=0x1000 -T src/linker.ld
# Directories
BUILD_DIR = build
SRC_DIR = src
# Source files
CSOURCES = $(shell find $(SRC_DIR) -name '*.c')
OBJ = $(CSOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
# Target
.PHONY: all clean
all: $(BUILD_DIR)/kernel.elf
# Create kernel ELF
$(BUILD_DIR)/kernel.elf: $(OBJ)
@mkdir -p $(@D)
$(LD) $(OBJ) $(LDFLAGS) -o $@
# Compile C sources
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@
# Clean build directory
clean:
rm -rf $(BUILD_DIR)
Here's how to build and run:
git clone https://github.com/limine-bootloader/limine.git --branch=v4.x-branch-binary --depth=1
make -C limine
# Create directories
mkdir -p iso_root/boot
# Copy kernel and Limine files
cp build/kernel.elf iso_root/boot/
cp limine.cfg iso_root/boot/
cp limine/limine.sys iso_root/boot/
cp limine/limine-cd.bin iso_root/boot/
cp limine/limine-cd-efi.bin iso_root/boot/
# Create the ISO
xorriso -as mkisofs -b boot/limine-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
--efi-boot boot/limine-cd-efi.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o kernel.iso
# Deploy Limine bootloader
./limine/limine-deploy kernel.iso
qemu-system-x86_64 -cdrom kernel.iso -M q35 -m 2G
This will create a basic kernel that boots and displays "Hello from my kernel!" on the screen. The kernel uses the Limine terminal to display text and then halts the CPU.
To expand this kernel, you could add:
Remember that this is a very basic example. A full OS kernel would need many more features and components to be useful.