Skip to content

Commit

Permalink
feat: upgrade polyhal version and refactor deno scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Nov 1, 2024
1 parent d8b5ec8 commit 59af83b
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 123 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions kernel/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// use backtrace::backtrace;
use core::panic::PanicInfo;

use polyhal::instruction::Instruction;
use polyhal::instruction::shutdown;

#[inline]
fn hart_id() -> usize {
Expand All @@ -27,5 +27,6 @@ fn panic_handler(info: &PanicInfo) -> ! {
// backtrace();
println!("!TEST FINISH!");
// loop {}
Instruction::shutdown()

shutdown();
}
4 changes: 2 additions & 2 deletions kernel/src/tasks/initproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fs::{
};
use log::debug;
use logging::get_char;
use polyhal::{debug_console::DebugConsole, instruction::Instruction};
use polyhal::{debug_console::DebugConsole, instruction::shutdown};
use vfscore::INodeInterface;

use crate::tasks::add_user_task;
Expand Down Expand Up @@ -407,6 +407,6 @@ pub async fn initproc() {
})
.is_none()
{
Instruction::shutdown();
shutdown();
}
}
67 changes: 4 additions & 63 deletions scripts/cli-build.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,15 @@
import { Command, CommandOptions } from "https://deno.land/x/[email protected]/command/mod.ts";
import { globalArgType } from "./cli-types.ts";
import { KernelBuilder } from "./kernel.ts";

const targetMap: Record<string, string> = {
"riscv64": 'riscv64gc-unknown-none-elf',
"x86_64": 'x86_64-unknown-none',
"aarch64": 'aarch64-unknown-none-softfloat',
"loongarch64": 'loongarch64-unknown-none'
};

/**
* Get the path of the kernel elf file.
* @param arch the architecture
* @returns path to the file
*/
export function getKernelElf(arch: string): string {
return `${Deno.cwd()}/target/${targetMap[arch]}/release/kernel`;
}

/**
* Get the path of the kernel Binary file.
* @param arch the architecture
* @returns path to the file
*/
export function getKernelBin(arch: string): string {
return `${getKernelElf(arch)}.bin`;
}

export const cargoBuild = async function(options: CommandOptions<globalArgType>) {

const rustflags = [
"-Cforce-frame-pointers=yes",
"-Clink-arg=-no-pie",
"-Ztls-model=local-exec",
`--cfg=root_fs="ext4_rs"`
];

const buildProc = new Deno.Command("cargo", {
args: [
"build",
"--release",
"--target",
targetMap[options.arch],
],
env: {
ROOT_MANIFEST_DIR: Deno.cwd() + "/",
MOUNT_IMG_PATH: "mount.img",
HEAP_SIZE: "0x0180_0000",
RUSTFLAGS: (Deno.env.get("RUSTFLAGS") || "") + ' ' + rustflags.join(' ')
},
});
const code = await buildProc.spawn().status;
if(!code.success) {
console.error("Failed to build the kernel");
Deno.exit(1);
}

const objcopyProc = new Deno.Command("rust-objcopy", {
args: [
`--binary-architecture=${options.arch}`,
getKernelElf(options.arch),
"--strip-all",
"-O",
"binary",
getKernelBin(options.arch)
]
});
await objcopyProc.spawn().status;
const builder = new KernelBuilder(options.arch);
await builder.buildElf();
await builder.convertBin();

console.log("options", options);
console.log("code: ", code);
}

export const cliCommand = new Command<globalArgType>()
Expand Down
122 changes: 72 additions & 50 deletions scripts/cli-qemu.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,81 @@
import { Command, CommandOptions } from "https://deno.land/x/[email protected]/command/mod.ts";
import { globalArgType } from "./cli-types.ts";
import { cargoBuild, getKernelBin, getKernelElf } from "./cli-build.ts";
import { KernelBuilder } from "./kernel.ts";

async function runQemu(options: CommandOptions<globalArgType>) {
await cargoBuild(options);
class QemuRunner {
arch: string;
bus: string = "device";
builder: KernelBuilder;

constructor(options: CommandOptions<globalArgType>, builder: KernelBuilder) {
this.arch = options.arch;
this.builder = builder;
if(this.arch == "x86_64" || this.arch == "loongarch64")
this.bus = "pci";
}

const qemuExecArch = {
x86_64: [
"-machine",
"q35",
"-kernel",
getKernelElf(options.arch),
"-cpu",
"IvyBridge-v2"
],
riscv64: [
"-machine",
"virt",
"-kernel",
getKernelBin(options.arch)
],
aarch64: [
"-cpu",
"cortex-a72",
"-machine",
"virt",
"-kernel",
getKernelBin(options.arch)
],
loongarch64: [
"-kernel",
getKernelElf(options.arch)
]
};
getQemuArchExec(): string[] {
return {
x86_64: [
"-machine",
"q35",
"-kernel",
this.builder.elfPath,
"-cpu",
"IvyBridge-v2"
],
riscv64: [
"-machine",
"virt",
"-kernel",
this.builder.binPath
],
aarch64: [
"-cpu",
"cortex-a72",
"-machine",
"virt",
"-kernel",
this.builder.binPath
],
loongarch64: [
"-kernel",
this.builder.elfPath
]
}[this.arch] ?? [];
}

const qemuCommand = new Deno.Command(`qemu-system-${options.arch}`, {
args: [
...qemuExecArch[options.arch],
"-m",
"1G",
"-nographic",
"-smp",
"1",
"-D",
"qemu.log",
"-d",
"in_asm,int,pcall,cpu_reset,guest_errors",
async run() {
const qemuCommand = new Deno.Command(`qemu-system-${this.arch}`, {
args: [
...this.getQemuArchExec(),
"-m",
"1G",
"-nographic",
"-smp",
"1",
"-D",
"qemu.log",
"-d",
"in_asm,int,pcall,cpu_reset,guest_errors",

"-drive",
"file=mount.img,if=none,format=raw,id=x0",
"-device",
"virtio-blk-device,drive=x0"
]
});
await qemuCommand.spawn().status;
}
}

async function runQemu(options: CommandOptions<globalArgType>) {
const builder = new KernelBuilder(options.arch);
await builder.buildElf();
await builder.convertBin();

"-drive",
"file=mount.img,if=none,format=raw,id=x0",
"-device",
"virtio-blk-device,drive=x0"
]
});
await qemuCommand.spawn().status;
const runner = new QemuRunner(options, builder);
await runner.run();
}

export const cliCommand = new Command<globalArgType>()
Expand Down
72 changes: 72 additions & 0 deletions scripts/kernel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const targetMap: Record<string, string> = {
"riscv64": 'riscv64gc-unknown-none-elf',
"x86_64": 'x86_64-unknown-none',
"aarch64": 'aarch64-unknown-none-softfloat',
"loongarch64": 'loongarch64-unknown-none'
};

export class KernelBuilder {
arch: string;
elfPath: string;
binPath: string;
rustflags: string;

constructor(arch: string) {
this.arch = arch;
this.elfPath = `${Deno.cwd()}/target/${targetMap[arch]}/release/kernel`;
this.binPath = `${this.elfPath}.bin`;

this.rustflags = Deno.env.get('rustflags') || "";
}

buildFlags() {
const rustflags = [
"-Cforce-frame-pointers=yes",
"-Clink-arg=-no-pie",
"-Ztls-model=local-exec",
`--cfg=root_fs="ext4_rs"`,
'--cfg=board="qemu"'
];

this.rustflags += rustflags.join(" ");
}

async buildElf() {
this.buildFlags();

const buildProc = new Deno.Command("cargo", {
args: [
"build",
"--release",
"--target",
targetMap[this.arch],
],
env: {
ROOT_MANIFEST_DIR: Deno.cwd() + "/",
MOUNT_IMG_PATH: "mount.img",
HEAP_SIZE: "0x0180_0000",
BOARD: "qemu",
RUSTFLAGS: this.rustflags
},
});
const code = await buildProc.spawn().status;
if(!code.success) {
console.error("Failed to build the kernel");
Deno.exit(1);
}
}

async convertBin() {
const objcopyProc = new Deno.Command("rust-objcopy", {
args: [
`--binary-architecture=${this.arch}`,
this.elfPath,
"--strip-all",
"-O",
"binary",
this.binPath
]
});
await objcopyProc.spawn().status;
}
}

0 comments on commit 59af83b

Please sign in to comment.