Skip to content

Rewrite - Arch subcrates #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build]
target = "i8086-bootloader.json"

[alias]
xbuild = "build -Zbuild-std=core"
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ jobs:

- name: 'Build Bootloader'
run: cargo xbuild --release
working-directory: src/stage_2

- name: 'Convert Bootloader ELF to Binary'
run: cargo objcopy -- -I elf64-i386 -O binary --binary-architecture=i386:x86-64 target/x86_64-real_mode/release/stage_2 target/x86_64-real_mode/release/image.bin
run: cargo objcopy -- -I elf64-i386 -O binary --binary-architecture=i386:x86-64 target/i8086-bootloader/release/bootloader target/i8086-bootloader/release/image.bin

# # install QEMU
# - name: Install QEMU (Linux)
Expand Down
113 changes: 8 additions & 105 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 10 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
[workspace]
members = [
"src/shared",
"src/v86",
"src/bootsector",
"src/stage_2",
"example-kernel",
"test-kernel",
]
[package]
name = "bootloader"
version = "0.1.0"
authors = ["Ryland Morgan <ryland.hugo@gmail.com>"]
edition = "2018"
build = "build.rs"

[profile.release]
opt-level = "z"
panic = "abort"
lto = true
[dependencies]
shared = { path = "src/shared" }

[profile.release.package.bootsector]
opt-level = "s"
codegen-units = 1
[build-dependencies]
llvm-tools = "0.1"
123 changes: 123 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// This build script compiles our bootloader. Because of architecture differences we can't use the standard Rust dependency resolution. To get around this (and add some more seperation between different areas) we compile all of the subcrates as static libraries and link them like we would a C dependency

// TODO - Reuse compilation artifacts so core isn't compiled so many times
use llvm_tools::{exe, LlvmTools};
use std::env;
use std::path::Path;
use std::process::Command;

fn main() {
// Read environment variables set by cargo
let out_dir_path = env::var("OUT_DIR").expect("Missing OUT_DIR environment variable");
let out_dir = Path::new(&out_dir_path);

let cargo_path = env::var("CARGO").expect("Missing CARGO environment variable");
let cargo = Path::new(&cargo_path);

let manifest_dir_path =
env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR environment variable");
let manifest_dir = Path::new(&manifest_dir_path);

// Find the objcopy binary
let llvm_tools = LlvmTools::new().expect("LLVM tools not found");
let objcopy = llvm_tools
.tool(&exe("llvm-objcopy"))
.expect("llvm-objcopy not found");

// Build the bootsector
build_subproject(
Path::new("src/real/bootsector"),
&[
"_start",
"real_mode_println",
"no_int13h_extensions",
"dap_load_failed",
],
"../i8086-real_mode.json",
&out_dir,
&objcopy,
&cargo,
);

// Build stage 2
build_subproject(
Path::new("src/real/stage_2"),
&["second_stage"],
"../i8086-real_mode.json",
&out_dir,
&objcopy,
&cargo,
);
}

fn build_subproject(
subproject_dir: &Path,
global_symbols: &[&str],
target_file_path: &str,
root_out_dir: &Path,
objcopy: &Path,
cargo: &Path,
) {
let subproject_name = subproject_dir
.file_stem()
.expect("Couldn't get subproject name")
.to_str()
.expect("Subproject Name is not valid UTF-8");
let target_file = Path::new(&target_file_path)
.file_stem()
.expect("Couldn't get target file stem");
let target_dir = root_out_dir.join("target").join(&subproject_name);

// We have to export at least 1 symbol
assert!(
global_symbols.len() > 0,
"must have at least one global symbol"
);

// Use cargo in CARGO environment variable (set on build)
let mut build_cmd = Command::new(cargo);

// Build inside the subproject
build_cmd.current_dir(&subproject_dir);

// Build in release mode
build_cmd.arg("build").arg("--release");

// Cross-compile core (cargo-xbuild no longer needed)
build_cmd.arg("-Zbuild-std=core");

// Use calculated target directory
build_cmd.arg(format!("--target-dir={}", &target_dir.display()));

// Use the passed target
build_cmd.arg("--target").arg(target_file_path);

// Run the command and make sure it succeeds
let build_status = build_cmd.status().expect("Subcrate build failed!");
assert!(build_status.success(), "Subcrate build failed!");

// Compute the path to the binary
let binary_dir = target_dir.join(&target_file).join("release");
let binary_path = binary_dir.join(format!("lib{}.a", &subproject_name));

// Use passed objcopy
let mut objcopy_cmd = Command::new(objcopy);

// Localize all symbols except those passed
for symbol in global_symbols {
objcopy_cmd.arg("-G").arg(symbol);
}

// Pass the binary as argument
objcopy_cmd.arg(binary_path);

// Run the command and make sure it succeeds
let objcopy_status = objcopy_cmd.status().expect("Objcopy failed!");
assert!(objcopy_status.success(), "Objcopy failed!");

// Emit flags to the linker
//
// Staticlibs can't be used as normal dependencies, they have to be linked by a build script
println!("cargo:rustc-link-search=native={}", &binary_dir.display());
println!("cargo:rustc-link-lib=static={}", &subproject_name);
}
26 changes: 26 additions & 0 deletions i8086-bootloader.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"arch": "x86",
"cpu": "i386",
"data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128",
"dynamic-linking": false,
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"llvm-target": "i386-unknown-none-code16",
"max-atomic-width": 64,
"position-independent-executables": false,
"disable-redzone": true,
"target-c-int-width": "32",
"target-pointer-width": "32",
"target-endian": "little",
"panic-strategy": "abort",
"os": "none",
"vendor": "unknown",
"relocation_model": "static",
"eliminate_frame_pointer": true,
"pre-link-args": {
"ld.lld": [
"--script=src/real/linker.ld"
]
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![no_std]
#![no_main]

mod panic;

#[no_mangle]
fn bootloader_no_optimize() {}
File renamed without changes.
2 changes: 0 additions & 2 deletions src/v86/Cargo.toml → src/protected/v86/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ authors = ["Ryland Morgan <ryland.hugo@gmail.com>"]
edition = "2018"

[dependencies]
bitflags = "1.2.1"
bit_field = "0.10.0"
8 changes: 2 additions & 6 deletions src/v86/src/lib.rs → src/protected/v86/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#![feature(abi_x86_interrupt)]
#![feature(const_fn)]
#![feature(llvm_asm)]
#![no_std]

// FIXME
#![allow(dead_code, unused_imports)]

use core::slice;

pub mod gdt;
pub mod idt;

const EFLAG_IF: u32 = 0x00000200;
const EFLAG_VM: u32 = 0x00020000;

/*
pub struct V86 {}

Expand Down
Loading