From b977f1c0674022bfeaf70e317318d6b8d3856a3d Mon Sep 17 00:00:00 2001 From: bzt Date: Sun, 17 Jan 2021 18:20:46 +0100 Subject: [PATCH] Tidy up --- mykernel/c/Makefile | 10 ++-- mykernel/go/Makefile | 10 ++-- mykernel/pas/Makefile | 10 ++-- mykernel/rust/Cargo.toml | 2 +- mykernel/rust/Makefile | 31 ++++++++++- mykernel/rust/link.ld | 71 ++++++++++++++++++++++++ mykernel/rust/src/main.rs | 64 ++++++++++++++++----- mykernel/rust/triplets/mykernel-x86.json | 2 +- mykernel/rust/triplets/mykernel.ld | 41 -------------- 9 files changed, 172 insertions(+), 69 deletions(-) create mode 100644 mykernel/rust/link.ld delete mode 100644 mykernel/rust/triplets/mykernel.ld diff --git a/mykernel/c/Makefile b/mykernel/c/Makefile index f0ef0e6..a6ed0df 100644 --- a/mykernel/c/Makefile +++ b/mykernel/c/Makefile @@ -29,21 +29,23 @@ # CFLAGS = -Wall -fpic -ffreestanding -fno-stack-protector -nostdinc -nostdlib -I../../dist/ +LDFLAGS = -nostdlib -nostartfiles -T link.ld +STRIPFLAGS = -s -K mmio -K fb -K bootboot -K environment all: mykernel.x86_64.elf mykernel.aarch64.elf mykernel.x86_64.elf: kernel.c x86_64-elf-gcc $(CFLAGS) -mno-red-zone -c kernel.c -o kernel.o x86_64-elf-ld -r -b binary -o font.o font.psf - x86_64-elf-ld -nostdlib -nostartfiles -T link.ld kernel.o font.o -o mykernel.x86_64.elf - x86_64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.x86_64.elf + x86_64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.x86_64.elf + x86_64-elf-strip $(STRIPFLAGS) mykernel.x86_64.elf x86_64-elf-readelf -hls mykernel.x86_64.elf >mykernel.x86_64.txt mykernel.aarch64.elf: kernel.c aarch64-elf-gcc $(CFLAGS) -c kernel.c -o kernel.o aarch64-elf-ld -r -b binary -o font.o font.psf - aarch64-elf-ld -nostdlib -nostartfiles -T link.ld kernel.o font.o -o mykernel.aarch64.elf - aarch64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.aarch64.elf + aarch64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.aarch64.elf + aarch64-elf-strip $(STRIPFLAGS) mykernel.aarch64.elf aarch64-elf-readelf -hls mykernel.aarch64.elf >mykernel.aarch64.txt clean: diff --git a/mykernel/go/Makefile b/mykernel/go/Makefile index 20b1ec5..9a3246c 100644 --- a/mykernel/go/Makefile +++ b/mykernel/go/Makefile @@ -29,21 +29,23 @@ # GOFLAGS = -Wall -fpic -fno-stack-protector -static -nostdinc -nostdlib -nostartfiles -nodefaultlibs +LDFLAGS = -nostdlib -nostartfiles -T link.ld +STRIPFLAGS = -s -K mmio -K fb -K bootboot -K environment all: mykernel.x86_64.elf mykernel.aarch64.elf mykernel.x86_64.elf: go_rt0.s kernel.go x86_64-elf-gccgo $(GOFLAGS) -c kernel.go -o kernel.o x86_64-elf-as -c go_rt0.s -o go_rt0.o - x86_64-elf-ld -nostdlib -nostartfiles -T link.ld go_rt0.o kernel.o -o mykernel.x86_64.elf - x86_64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.x86_64.elf + x86_64-elf-ld $(LDFLAGS) go_rt0.o kernel.o -o mykernel.x86_64.elf + x86_64-elf-strip $(STRIPFLAGS) mykernel.x86_64.elf x86_64-elf-readelf -hls mykernel.x86_64.elf >mykernel.x86_64.txt mykernel.aarch64.elf: go_rt0.s kernel.go aarch64-elf-gccgo $(GOFLAGS) -c kernel.go -o kernel.o aarch64-elf-as -c go_rt0.s -o go_rt0.o - aarch64-elf-ld -nostdlib -nostartfiles -T link.ld go_rt0.o kernel.o -o mykernel.aarch64.elf - aarch64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.aarch64.elf + aarch64-elf-ld $(LDFLAGS) go_rt0.o kernel.o -o mykernel.aarch64.elf + aarch64-elf-strip $(STRIPFLAGS) mykernel.aarch64.elf aarch64-elf-readelf -hls mykernel.aarch64.elf >mykernel.aarch64.txt clean: diff --git a/mykernel/pas/Makefile b/mykernel/pas/Makefile index 17167c7..3ce7b60 100644 --- a/mykernel/pas/Makefile +++ b/mykernel/pas/Makefile @@ -29,21 +29,23 @@ # PASFLAGS = -Aelf -n -O3 -Xd -CX -XXs -Tlinux +LDFLAGS = -nostdlib -nostartfiles -T link.ld +STRIPFLAGS = -s -K mmio -K fb -K bootboot -K environment all: mykernel.x86_64.elf mykernel.aarch64.elf mykernel.x86_64.elf: kernel.pas fpc -Px86_64 $(PASFLAGS) kernel.pas x86_64-elf-ld -r -b binary -o font.o font.psf - x86_64-elf-ld -nostdlib -nostartfiles -T link.ld kernel.o font.o -o mykernel.x86_64.elf - x86_64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.x86_64.elf + x86_64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.x86_64.elf + x86_64-elf-strip $(STRIPFLAGS) mykernel.x86_64.elf x86_64-elf-readelf -hls mykernel.x86_64.elf >mykernel.x86_64.txt mykernel.aarch64.elf: kernel.pas fpc -Paarch64 $(PASFLAGS) kernel.pas aarch64-elf-ld -r -b binary -o font.o font.psf - aarch64-elf-ld -nostdlib -nostartfiles -T link.ld kernel.o font.o -o mykernel.aarch64.elf - aarch64-elf-strip -s -K mmio -K fb -K bootboot -K environment mykernel.aarch64.elf + aarch64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.aarch64.elf + aarch64-elf-strip $(STRIPFLAGS) mykernel.aarch64.elf aarch64-elf-readelf -hls mykernel.aarch64.elf >mykernel.aarch64.txt clean: diff --git a/mykernel/rust/Cargo.toml b/mykernel/rust/Cargo.toml index f8f5a19..3d1862c 100755 --- a/mykernel/rust/Cargo.toml +++ b/mykernel/rust/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "mykernel-rust" version = "0.0.1" -authors = ["Author "] +authors = ["Vinay Chandra"] edition = "2018" [dependencies] diff --git a/mykernel/rust/Makefile b/mykernel/rust/Makefile index ceb9f77..4e9bd72 100644 --- a/mykernel/rust/Makefile +++ b/mykernel/rust/Makefile @@ -1,3 +1,32 @@ +# +# mykernel/rust/Makefile +# +# Copyright (C) 2017 - 2021 Vinay Chandra +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +# This file is part of the BOOTBOOT Protocol package. +# @brief An example Makefile for sample kernel +# +# all: mykernel.x86_64.elf @@ -7,4 +36,4 @@ mykernel.x86_64.elf: src/** cargo xbuild --target ./triplets/mykernel-x86.json # Using this causes the kernel to exceed the 2 MB limit # cargo build -Z build-std=core,alloc --target ./triplets/$mykernel-x86.json - cp ./target/mykernel-x86/debug/mykernel-rust mykernel.x86_64.elf \ No newline at end of file + cp ./target/mykernel-x86/debug/mykernel-rust mykernel.x86_64.elf diff --git a/mykernel/rust/link.ld b/mykernel/rust/link.ld new file mode 100644 index 0000000..5bcc4e9 --- /dev/null +++ b/mykernel/rust/link.ld @@ -0,0 +1,71 @@ +/* + * mykernel/rust/link.ld + * + * Copyright (C) 2017 - 2021 Vinay Chandra + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This file is part of the BOOTBOOT Protocol package. + * @brief An example linker script for sample kernel + * + */ + +KERNEL_OFFSET = 0xfffffffff8000000; /* these are configurable for level 2 loaders */ + +PHDRS +{ + boot PT_LOAD FILEHDR PHDRS; /* one single loadable segment */ +} +SECTIONS +{ + . = KERNEL_OFFSET; + mmio = .; . += 0x4000000; + fb = .; . += 0x3E00000; + bootboot = .; . += 4096; + environment = .; . += 4096; + + .text . + SIZEOF_HEADERS : AT(ADDR(.text) - KERNEL_OFFSET + SIZEOF_HEADERS) { + __text_start = .; + KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) /* code */ + . = ALIGN(4096); + __text_end = .; + } :boot + + .rodata : AT(ADDR(.rodata) - KERNEL_OFFSET) { + __rodata_start = .; + *(.rodata*) + . = ALIGN(4096); + __rodata_end = .; + } :boot + + .data : AT(ADDR(.data) - KERNEL_OFFSET) { + __data_start = .; + *(.data*) + . = ALIGN(4096); + __data_end = .; + __bss_start = .; + *(.bss*) + . = ALIGN(4096); + __bss_end = .; + } :boot + + /DISCARD/ : { *(.eh_frame) *(.comment) } +} diff --git a/mykernel/rust/src/main.rs b/mykernel/rust/src/main.rs index 9cd1d84..7ba2344 100644 --- a/mykernel/rust/src/main.rs +++ b/mykernel/rust/src/main.rs @@ -1,3 +1,34 @@ +/* + * mykernel/rust/src/main.rs + * + * Copyright (C) 2017 - 2021 Vinay Chandra + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This file is part of the BOOTBOOT Protocol package. + * @brief A sample BOOTBOOT compatible kernel + * + */ + +// configure Rust compiler #![no_std] #![no_main] @@ -12,16 +43,16 @@ mod bootboot; // Required for -Z build-std flag. extern crate rlibc; -/// Entry point for the Operating System. +/****************************************** + * Entry point, called by BOOTBOOT Loader * + ******************************************/ #[no_mangle] // don't mangle the name of this function fn _start() -> ! { - // int x, y, s=bootboot.fb_scanline, w=bootboot.fb_width, h=bootboot.fb_height; - // for(y=0;y ! { *(addr as *mut u64) = 0x00FFFFFF; } - // RGB boxes in order + // red, green, blue boxes in order for y in 0..20 { for x in 0..20 { let addr = fb @@ -61,18 +92,16 @@ fn _start() -> ! { } } + // say hello puts("Hello from a simple BOOTBOOT kernel"); + // hang for now loop {} } -/// This function is called on panic. -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} -} - +/************************** + * Display text on screen * + **************************/ fn puts(string: &'static str) { use bootboot::*; unsafe { @@ -113,3 +142,12 @@ fn puts(string: &'static str) { } } } + +/************************************* + * This function is called on panic. * + *************************************/ +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/mykernel/rust/triplets/mykernel-x86.json b/mykernel/rust/triplets/mykernel-x86.json index ddcda71..5663e5b 100644 --- a/mykernel/rust/triplets/mykernel-x86.json +++ b/mykernel/rust/triplets/mykernel-x86.json @@ -23,6 +23,6 @@ "position-independent-executables": false, "has-elf-tls": true, "pre-link-args": { - "ld.lld": ["--script=./triplets/mykernel.ld", "./font.o"] + "ld.lld": ["--script=./link.ld", "./font.o"] } } diff --git a/mykernel/rust/triplets/mykernel.ld b/mykernel/rust/triplets/mykernel.ld deleted file mode 100644 index 67f33d3..0000000 --- a/mykernel/rust/triplets/mykernel.ld +++ /dev/null @@ -1,41 +0,0 @@ -KERNEL_OFFSET = 0xfffffffff8000000; - -PHDRS -{ - boot PT_LOAD FILEHDR PHDRS; /* one single loadable segment */ -} -SECTIONS -{ - . = KERNEL_OFFSET; - mmio = .; . += 0x4000000; - fb = .; . += 0x3E00000; - bootboot = .; . += 4096; - environment = .; . += 4096; - - .text . + SIZEOF_HEADERS : AT(ADDR(.text) - KERNEL_OFFSET + SIZEOF_HEADERS) { - __text_start = .; - KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) /* code */ - . = ALIGN(4096); - __text_end = .; - } :boot - - .rodata : AT(ADDR(.rodata) - KERNEL_OFFSET) { - __rodata_start = .; - *(.rodata*) - . = ALIGN(4096); - __rodata_end = .; - } :boot - - .data : AT(ADDR(.data) - KERNEL_OFFSET) { - __data_start = .; - *(.data*) - . = ALIGN(4096); - __data_end = .; - __bss_start = .; - *(.bss*) - . = ALIGN(4096); - __bss_end = .; - } :boot - - /DISCARD/ : { *(.eh_frame) *(.comment) } -}