-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade polyhal version and refactor deno scripts
- Loading branch information
Showing
6 changed files
with
159 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |