Rave 精简版:用 Rave 编程语言编写可引导内核的教程

来源:OSDev Wiki· 阅读原文 ↗
文章封面

本条目或本节的事实准确性 存在争议。
请参阅 讨论页。

本教程需要解释代码的作用,因为教程不应只是复制粘贴。你可以通过 编辑 本页面来补充更多关于代码作用的上下文。

等等!你是否已经阅读了 入门指南、初学者常见错误,以及一些相关的 操作系统理论?

难度级别
Difficulty 1.png
初级
内核设计
模型
其他概念

在本教程中,我们将使用 Rave 编程语言编写一个可引导的内核。

构建该项目所需的工具:

  • NASM
  • Rave
  • binutils (ld)
  • QEMU(可选;用于测试)


概述

在本教程中,我们将创建一个简单的 NASM 加载器和一个能够输出所需文本的内核——在本例中,我们将输出 "Hello, Rave!"。

我们的最终项目将包含以下文件:

  • boot.asm
  • kernel.rave
  • linker.ld

boot.asm

MBALIGN  equ  1 << 0            ; align loaded modules on page boundaries
MEMINFO  equ  1 << 1            ; provide memory map
MBFLAGS  equ  MBALIGN | MEMINFO ; this is the Multiboot 'flag' field
MAGIC    equ  0x1BADB002        ; 'magic number' lets bootloader find the header
CHECKSUM equ -(MAGIC + MBFLAGS) ; checksum of above, to prove we are multiboot
                                ; CHECKSUM + MAGIC + MBFLAGS should be Zero (0)
section .multiboot
align 4
	dd MAGIC
	dd MBFLAGS
	dd CHECKSUM

section .bss
align 16
stack_bottom:
resb 16384 ; 16 KiB is reserved for stack
stack_top:

section .text
global _start:function (_start.end - _start)
_start:
	mov esp, stack_top

	extern kmain
	call kmain
	cli
.hang:	hlt
	jmp .hang
.end:

使用以下命令进行汇编:

nasm -felf32 boot.asm -o boot.o

kernel.rave

namespace terminal {
    (volatile) ushort* memory = 0xB8000;

    alias columns = 80;
    alias rows = 25;

    usize row = 0;
    usize column = 0;

    void clear {
        for (uint y=0; y<terminal::rows; y++) {
            for (uint x=0; x<terminal::columns; x++) terminal::memory[(y * terminal::rows) + x] = 0;
        }
    }

    void putchar(uchar symbol) {
        if (symbol == '\n') {
            terminal::column = 0;
            terminal::row += 1;
        }
        else {
            terminal::memory[(terminal::row * terminal::columns) + terminal::column] = (cast(ushort*)(&[symbol, cast(uchar)0x0F]))[0];
            terminal::column += 1;
        }

        if (terminal::column == terminal::columns) {
            terminal::column = 0;
            terminal::row += 1;

            if (terminal::row == terminal::rows) terminal::row = 0;
        }
    }

    void print(char* cstr) {
        usize i = 0;
        while (cstr[i] != '\0') { terminal::putchar(cstr[i]); i += 1; }
    }
}
 
(C) void kmain {
    terminal::clear();
    terminal::print("Hello, Rave!");

	while (true) {}
}

使用以下命令进行编译:

rave kernel.rave -o kernel.o -c --noStd --noEntry --noChecks -ffreestanding -t i686-linux

linker.ld

OUTPUT_FORMAT(elf32-i386)

/* The bootloader will look at this image and start execution at the symbol
   designated as the entry point. */
ENTRY(_start)

/* Tell where the various sections of the object files will be put in the final
   kernel image. */
SECTIONS
{
	/* It used to be universally recommended to use 1M as a start offset,
	   as it was effectively guaranteed to be available under BIOS systems.
	   However, UEFI has made things more complicated, and experimental data
	   strongly suggests that 2M is a safer place to load. In 2016, a new
	   feature was introduced to the multiboot2 spec to inform bootloaders
	   that a kernel can be loaded anywhere within a range of addresses and
	   will be able to relocate itself to run from such a loader-selected
	   address, in order to give the loader freedom in selecting a span of
	   memory which is verified to be available by the firmware, in order to
	   work around this issue. This does not use that feature, so 2M was
	   chosen as a safer option than the traditional 1M. */
	. = 2M;

	/* First put the multiboot header, as it is required to be put very early
	   in the image or the bootloader won't recognize the file format.
	   Next we'll put the .text section. */
	.text BLOCK(4K) : ALIGN(4K)
	{
		*(.multiboot)
		*(.text)
	}

	/* Read-only data. */
	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	/* Read-write data (initialized) */
	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.data)
	}

	/* Read-write data (uninitialized) and stack */
	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
	}

	/* The compiler may produce other sections, by default it will put them in
	   a segment with the same name. Simply add stuff here as needed. */
}

之后,你可以使用以下命令将它们全部链接起来:

ld -melf_i386 -T linker.ld -o myos.bin boot.o kernel.o

你的内核现在命名为 myos.bin。你可以尝试使用 QEMU 引导它:

qemu-system-i386 -kernel myos.bin

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:Rave Bare Bones;离线修订号:30335。对应原文版本

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论