Skip to content

Commit 59af83b

Browse files
committed
feat: upgrade polyhal version and refactor deno scripts
1 parent d8b5ec8 commit 59af83b

File tree

6 files changed

+159
-123
lines changed

6 files changed

+159
-123
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/src/panic.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// use backtrace::backtrace;
22
use core::panic::PanicInfo;
33

4-
use polyhal::instruction::Instruction;
4+
use polyhal::instruction::shutdown;
55

66
#[inline]
77
fn hart_id() -> usize {
@@ -27,5 +27,6 @@ fn panic_handler(info: &PanicInfo) -> ! {
2727
// backtrace();
2828
println!("!TEST FINISH!");
2929
// loop {}
30-
Instruction::shutdown()
30+
31+
shutdown();
3132
}

kernel/src/tasks/initproc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use fs::{
1212
};
1313
use log::debug;
1414
use logging::get_char;
15-
use polyhal::{debug_console::DebugConsole, instruction::Instruction};
15+
use polyhal::{debug_console::DebugConsole, instruction::shutdown};
1616
use vfscore::INodeInterface;
1717

1818
use crate::tasks::add_user_task;
@@ -407,6 +407,6 @@ pub async fn initproc() {
407407
})
408408
.is_none()
409409
{
410-
Instruction::shutdown();
410+
shutdown();
411411
}
412412
}

scripts/cli-build.ts

+4-63
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,15 @@
11
import { Command, CommandOptions } from "https://deno.land/x/[email protected]/command/mod.ts";
22
import { globalArgType } from "./cli-types.ts";
3+
import { KernelBuilder } from "./kernel.ts";
34

4-
const targetMap: Record<string, string> = {
5-
"riscv64": 'riscv64gc-unknown-none-elf',
6-
"x86_64": 'x86_64-unknown-none',
7-
"aarch64": 'aarch64-unknown-none-softfloat',
8-
"loongarch64": 'loongarch64-unknown-none'
9-
};
10-
11-
/**
12-
* Get the path of the kernel elf file.
13-
* @param arch the architecture
14-
* @returns path to the file
15-
*/
16-
export function getKernelElf(arch: string): string {
17-
return `${Deno.cwd()}/target/${targetMap[arch]}/release/kernel`;
18-
}
19-
20-
/**
21-
* Get the path of the kernel Binary file.
22-
* @param arch the architecture
23-
* @returns path to the file
24-
*/
25-
export function getKernelBin(arch: string): string {
26-
return `${getKernelElf(arch)}.bin`;
27-
}
285

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

31-
const rustflags = [
32-
"-Cforce-frame-pointers=yes",
33-
"-Clink-arg=-no-pie",
34-
"-Ztls-model=local-exec",
35-
`--cfg=root_fs="ext4_rs"`
36-
];
37-
38-
const buildProc = new Deno.Command("cargo", {
39-
args: [
40-
"build",
41-
"--release",
42-
"--target",
43-
targetMap[options.arch],
44-
],
45-
env: {
46-
ROOT_MANIFEST_DIR: Deno.cwd() + "/",
47-
MOUNT_IMG_PATH: "mount.img",
48-
HEAP_SIZE: "0x0180_0000",
49-
RUSTFLAGS: (Deno.env.get("RUSTFLAGS") || "") + ' ' + rustflags.join(' ')
50-
},
51-
});
52-
const code = await buildProc.spawn().status;
53-
if(!code.success) {
54-
console.error("Failed to build the kernel");
55-
Deno.exit(1);
56-
}
57-
58-
const objcopyProc = new Deno.Command("rust-objcopy", {
59-
args: [
60-
`--binary-architecture=${options.arch}`,
61-
getKernelElf(options.arch),
62-
"--strip-all",
63-
"-O",
64-
"binary",
65-
getKernelBin(options.arch)
66-
]
67-
});
68-
await objcopyProc.spawn().status;
8+
const builder = new KernelBuilder(options.arch);
9+
await builder.buildElf();
10+
await builder.convertBin();
6911

7012
console.log("options", options);
71-
console.log("code: ", code);
7213
}
7314

7415
export const cliCommand = new Command<globalArgType>()

scripts/cli-qemu.ts

+72-50
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,81 @@
11
import { Command, CommandOptions } from "https://deno.land/x/[email protected]/command/mod.ts";
22
import { globalArgType } from "./cli-types.ts";
3-
import { cargoBuild, getKernelBin, getKernelElf } from "./cli-build.ts";
3+
import { KernelBuilder } from "./kernel.ts";
44

5-
async function runQemu(options: CommandOptions<globalArgType>) {
6-
await cargoBuild(options);
5+
class QemuRunner {
6+
arch: string;
7+
bus: string = "device";
8+
builder: KernelBuilder;
9+
10+
constructor(options: CommandOptions<globalArgType>, builder: KernelBuilder) {
11+
this.arch = options.arch;
12+
this.builder = builder;
13+
if(this.arch == "x86_64" || this.arch == "loongarch64")
14+
this.bus = "pci";
15+
}
716

8-
const qemuExecArch = {
9-
x86_64: [
10-
"-machine",
11-
"q35",
12-
"-kernel",
13-
getKernelElf(options.arch),
14-
"-cpu",
15-
"IvyBridge-v2"
16-
],
17-
riscv64: [
18-
"-machine",
19-
"virt",
20-
"-kernel",
21-
getKernelBin(options.arch)
22-
],
23-
aarch64: [
24-
"-cpu",
25-
"cortex-a72",
26-
"-machine",
27-
"virt",
28-
"-kernel",
29-
getKernelBin(options.arch)
30-
],
31-
loongarch64: [
32-
"-kernel",
33-
getKernelElf(options.arch)
34-
]
35-
};
17+
getQemuArchExec(): string[] {
18+
return {
19+
x86_64: [
20+
"-machine",
21+
"q35",
22+
"-kernel",
23+
this.builder.elfPath,
24+
"-cpu",
25+
"IvyBridge-v2"
26+
],
27+
riscv64: [
28+
"-machine",
29+
"virt",
30+
"-kernel",
31+
this.builder.binPath
32+
],
33+
aarch64: [
34+
"-cpu",
35+
"cortex-a72",
36+
"-machine",
37+
"virt",
38+
"-kernel",
39+
this.builder.binPath
40+
],
41+
loongarch64: [
42+
"-kernel",
43+
this.builder.elfPath
44+
]
45+
}[this.arch] ?? [];
46+
}
3647

37-
const qemuCommand = new Deno.Command(`qemu-system-${options.arch}`, {
38-
args: [
39-
...qemuExecArch[options.arch],
40-
"-m",
41-
"1G",
42-
"-nographic",
43-
"-smp",
44-
"1",
45-
"-D",
46-
"qemu.log",
47-
"-d",
48-
"in_asm,int,pcall,cpu_reset,guest_errors",
48+
async run() {
49+
const qemuCommand = new Deno.Command(`qemu-system-${this.arch}`, {
50+
args: [
51+
...this.getQemuArchExec(),
52+
"-m",
53+
"1G",
54+
"-nographic",
55+
"-smp",
56+
"1",
57+
"-D",
58+
"qemu.log",
59+
"-d",
60+
"in_asm,int,pcall,cpu_reset,guest_errors",
61+
62+
"-drive",
63+
"file=mount.img,if=none,format=raw,id=x0",
64+
"-device",
65+
"virtio-blk-device,drive=x0"
66+
]
67+
});
68+
await qemuCommand.spawn().status;
69+
}
70+
}
71+
72+
async function runQemu(options: CommandOptions<globalArgType>) {
73+
const builder = new KernelBuilder(options.arch);
74+
await builder.buildElf();
75+
await builder.convertBin();
4976

50-
"-drive",
51-
"file=mount.img,if=none,format=raw,id=x0",
52-
"-device",
53-
"virtio-blk-device,drive=x0"
54-
]
55-
});
56-
await qemuCommand.spawn().status;
77+
const runner = new QemuRunner(options, builder);
78+
await runner.run();
5779
}
5880

5981
export const cliCommand = new Command<globalArgType>()

scripts/kernel.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const targetMap: Record<string, string> = {
2+
"riscv64": 'riscv64gc-unknown-none-elf',
3+
"x86_64": 'x86_64-unknown-none',
4+
"aarch64": 'aarch64-unknown-none-softfloat',
5+
"loongarch64": 'loongarch64-unknown-none'
6+
};
7+
8+
export class KernelBuilder {
9+
arch: string;
10+
elfPath: string;
11+
binPath: string;
12+
rustflags: string;
13+
14+
constructor(arch: string) {
15+
this.arch = arch;
16+
this.elfPath = `${Deno.cwd()}/target/${targetMap[arch]}/release/kernel`;
17+
this.binPath = `${this.elfPath}.bin`;
18+
19+
this.rustflags = Deno.env.get('rustflags') || "";
20+
}
21+
22+
buildFlags() {
23+
const rustflags = [
24+
"-Cforce-frame-pointers=yes",
25+
"-Clink-arg=-no-pie",
26+
"-Ztls-model=local-exec",
27+
`--cfg=root_fs="ext4_rs"`,
28+
'--cfg=board="qemu"'
29+
];
30+
31+
this.rustflags += rustflags.join(" ");
32+
}
33+
34+
async buildElf() {
35+
this.buildFlags();
36+
37+
const buildProc = new Deno.Command("cargo", {
38+
args: [
39+
"build",
40+
"--release",
41+
"--target",
42+
targetMap[this.arch],
43+
],
44+
env: {
45+
ROOT_MANIFEST_DIR: Deno.cwd() + "/",
46+
MOUNT_IMG_PATH: "mount.img",
47+
HEAP_SIZE: "0x0180_0000",
48+
BOARD: "qemu",
49+
RUSTFLAGS: this.rustflags
50+
},
51+
});
52+
const code = await buildProc.spawn().status;
53+
if(!code.success) {
54+
console.error("Failed to build the kernel");
55+
Deno.exit(1);
56+
}
57+
}
58+
59+
async convertBin() {
60+
const objcopyProc = new Deno.Command("rust-objcopy", {
61+
args: [
62+
`--binary-architecture=${this.arch}`,
63+
this.elfPath,
64+
"--strip-all",
65+
"-O",
66+
"binary",
67+
this.binPath
68+
]
69+
});
70+
await objcopyProc.spawn().status;
71+
}
72+
}

0 commit comments

Comments
 (0)