FLATBOOT Barebones:在 S/390 上开发操作系统的引导与基础环境

来源:OSDev Wiki· 阅读原文 ↗

S/390 架构是内核领域最有趣的架构之一,因为它涉及标准小型计算机架构中未见的概念,例如专用的 I/O 处理器或易出错的检查。

先决条件

源代码

Flatboot 引导加载程序是在 S/390 上开始操作系统开发的一个简单起点。它部分实现了 stivale 协议(但并非完全兼容)。同时支持 ELF 加载和 BIN 加载。一般来说;在大型机世界中,拥有一个通用引导加载程序并不实用,因为大多数 IPL 引导加载程序都依赖于它们所加载的设备。Flatboot 浪费了宝贵的内存(64KB),这在 S/360 和 S/370 上是很大的量。但它为新手提供了一个简单的起点。

kernel.c

系统的核心控制部分(在任何 S/390 或 z/Arch 系统的 LPAR 中运行):

// Source: https://github.com/limine-bootloader/limine-barebones/blob/master/stivale2-barebones/kernel/kernel.c
#include <stdint.h>
#include <stddef.h>

#define _STIVALE2_SPLIT_64_FORCE 1
#include <stivale2.h>

static uint8_t stack[8192];
static struct stivale2_header_tag_terminal terminal_hdr_tag = {
    .tag = {
        .identifier = STIVALE2_HEADER_TAG_TERMINAL_ID,
        .next = 0
    },
    .flags = 0
};

__attribute__((section(".stivale2hdr"), used))
static struct stivale2_header stivale_hdr = {
    .entry_point = 0,
    .stack = (uintptr_t)stack + sizeof(stack),
    .flags = (1 << 1) | (1 << 2),
    .tags = (uintptr_t)&terminal_hdr_tag
};

// We will now write a helper function which will allow us to scan for tags
// that we want FROM the bootloader (structure tags).
void *stivale2_get_tag(struct stivale2_struct *stivale2_struct, uint64_t id) {
    struct stivale2_tag *current_tag = (void *)stivale2_struct->tags;
    for (;;) {
        // If the tag pointer is NULL (end of linked list), we did not find
        // the tag. Return NULL to signal this.
        if (current_tag == NULL) {
            return NULL;
        }

        // Check whether the identifier matches. If it does, return a pointer
        // to the matching tag.
        if (current_tag->identifier == id) {
            return current_tag;
        }

        // Get a pointer to the next tag in the linked list and repeat.
        current_tag = (void *)current_tag->next;
    }
}

// The following will be our kernel's entry point.
void _start(struct stivale2_struct *stivale2_struct) {
    // Let's get the terminal structure tag from the bootloader.
    struct stivale2_struct_tag_terminal *term_str_tag;
    term_str_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_TERMINAL_ID);

    // Check if the tag was actually found.
    if (term_str_tag == NULL) {
        // It wasn't found, just hang...
        for (;;);
    }

    // Let's get the address of the terminal write function.
    void *term_write_ptr = (void *)term_str_tag->term_write;

    // Now, let's assign this pointer to a function pointer which
    // matches the prototype described in the stivale2 specification for
    // the stivale2_term_write function.
    void (*term_write)(const char *string, size_t length) = term_write_ptr;
    term_write("Hello World\n", 13);
    for (;;);
}

linker.ld

由于 Flatboot 对内核有严格的禁止低 64K 规则,内核需要加载到 0x20000 以上。

ENTRY(_start);
SECTIONS {
    . = 0x20000;

    .text : {
        *(.text);
        *(.text.*);
        *(.stivale2hdr*);
    }
    
    .rodata : {
        *(.rodata);
        *(.rodata.*);
    }

    .data : {
        *(.data);
        *(.data.*);
    }

    .bss : ALIGN(4K) {
        *(COMMON);
        *(.bss);
        *(.bss.*);
    }
}

hercules.cnf

Hercules 的配置文件,启用诊断消息,将架构模式设置为 ESA S/390,并使用 IBM-1047 代码页(EBCDIC)

MAINSIZE             32
ARCHMODE             ESA/390
CPUSERIAL            000611
CPUMODEL             4381
DIAG8CMD             ENABLE
NUMCPU               1 
PANRATE              MEDIUM
PGMPRDOS             RESTRICTED
CODEPAGE             819/1047

# This is the IPL disk and can be any address
01b9      3390       flat00.cckd

dasd.txt

描述 DASD 磁盘中的文件,这是必需的,因为 ZDSFS 有一种独特的数据集(文件)排序方式——包括对文本文件(70 列)或二进制文件的支持。

FLAT00       3390-1 * stage1.txt
STAGE2.BIN   SEQ stage2.bin          TRK 10 1 0 PS FB 1 18452
SYSVTOC      VTOC                    CYL  2
KERNEL.ELF   SEQ kernel              CYL  1 1 0 PS FB 1 18452

运行大型机

#!/usr/bin/bash

# Download headers required for the stivale2 structs
rm stivale2.h
wget https://raw.githubusercontent.com/stivale/stivale/master/stivale2.h

# Download the disk-dependant IPL
wget https://github.com/udos-project/flatboot/releases/download/0.1.2/stage1_S390.txt
mv stage1_S390.txt stage1.txt

# Download the second stage bootloader
wget https://github.com/udos-project/flatboot/releases/download/0.1.2/stage2_S390.bin
mv stage2_S390.bin stage2.bin

CC=s390-linux-gcc
CFLAGS="-fexec-charset=IBM-1047 -fno-stack-protector -pipe -m31 -ffreestanding -mpacked-stack -I. -O2 -g"
LDFLAGS="-Wl,-static,--no-dynamic-linker,-ztext -nostdlib -z max-page-size=0x1000 -static -fno-pic -fno-pie -g"

$CC $CFLAGS -c kernel.c -o kernel.o
$CC $LDFLAGS $CFLAGS -Tlinker.ld kernel.o -o kernel

# Create the DASD disk
dasdload -bz2 dasd.txt flat00.cckd

# Fire up the emulator and concat to a log file
hercules -f hercules.cnf >hercules.log

一旦 hercules 屏幕出现,可以用以下方式加载内核:

IPL 1B9

下一步是什么?

  • 在 hercules 中使用 MVS 3.8/tk4 来了解大型机如何运行
  • 阅读此内容
  • 捕获程序异常
  • 获取 CSS 以便能够对设备执行读/写操作
  • 没有可言的帧缓冲区,没有 GUI,只有终端——所以实现一个 3270(图形终端)驱动程序或 2703(Telnet)
  • 实现多处理

来源与翻译说明

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

原文:FLATBOOT Barebones;离线修订号:30280。对应原文版本

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

评论

暂无评论