Skip to content

Commit b5b72a3

Browse files
use the rust-toolchain
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 021d8f1 commit b5b72a3

File tree

31 files changed

+202
-434
lines changed

31 files changed

+202
-434
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ base-files/doom1.wad
2222
# Wayland test client and server executables: (todo: remove these)
2323
base-files/client
2424
base-files/server
25+
26+
userland/.cargo/config.toml

.vscode/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"./userland/Cargo.toml"
55
],
66
"rust-analyzer.checkOnSave.allTargets": false,
7-
"rust-analyzer.checkOnSave.extraArgs": [
8-
"-Zbuild-std=core,alloc"
9-
],
7+
// "rust-analyzer.checkOnSave.extraArgs": [
8+
// "-Zbuild-std=core,alloc"
9+
// ],
1010
"editor.formatOnSave": true,
1111
"files.associations": {
1212
"stdio.h": "c",

aero.py

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,54 @@
7171
"""
7272

7373

74+
def log_info(msg):
75+
"""
76+
Logs a message with info log level.
77+
"""
78+
print(f"\033[1m\033[92minfo\033[0m: {msg}")
79+
80+
81+
def download_userland_host_rust():
82+
out_file = os.path.join(BUNDLED_DIR, "host-rust-prebuilt.tar.gz")
83+
84+
# we have already cloned the toolchain
85+
if os.path.exists(out_file):
86+
return
87+
88+
log_info("downloading prebuilt userland host rust toolchain")
89+
90+
cmd = r"""
91+
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=FILE_HASH" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FILE_HASH" -O OUTPUT_FILE && rm -rf /tmp/cookies.txt
92+
""".replace("FILE_HASH", "1TTC9qa1z-KdLaQkhgMCYxLE5nuKg4gcx").replace("OUTPUT_FILE", out_file)
93+
94+
subprocess.run(cmd, shell=True)
95+
96+
log_info("extracting prebuilt userland host rust toolchain")
97+
98+
# the toolchain is compressed, so we need to extract it
99+
file = tarfile.open(out_file)
100+
file.extractall(os.path.join(BUNDLED_DIR, "host-rust-prebuilt"))
101+
file.close()
102+
103+
104+
def get_userland_tool():
105+
toolchain = os.path.join(SYSROOT_DIR, "tools")
106+
107+
if os.path.exists(toolchain):
108+
return toolchain
109+
110+
return os.path.join(BUNDLED_DIR, "host-rust-prebuilt/aero")
111+
112+
113+
def get_userland_package():
114+
toolchain = os.path.join(SYSROOT_DIR, "packages")
115+
116+
if os.path.exists(toolchain):
117+
return toolchain
118+
119+
return os.path.join(BUNDLED_DIR, "host-rust-prebuilt/aero")
120+
121+
74122
def remove_prefix(string: str, prefix: str):
75123
if string.startswith(prefix):
76124
return string[len(prefix):]
@@ -180,6 +228,9 @@ def download_bundled():
180228
run_command(['git', 'clone', '--branch', 'v3.0-branch-binary',
181229
'--depth', '1', LIMINE_URL, limine_path])
182230

231+
if not os.path.exists(SYSROOT_DIR):
232+
download_userland_host_rust()
233+
183234

184235
def extract_artifacts(stdout):
185236
result = []
@@ -195,13 +246,13 @@ def extract_artifacts(stdout):
195246
return result
196247

197248

198-
def build_cargo_workspace(cwd, command, args):
199-
code, _, _ = run_command(['cargo', command, *args], cwd=cwd)
249+
def build_cargo_workspace(cwd, command, args, cargo="cargo"):
250+
code, _, _ = run_command([cargo, command, *args], cwd=cwd)
200251

201252
if code != 0:
202253
return None
203254

204-
_, stdout, _ = run_command(['cargo', command, *args, '--message-format=json'],
255+
_, stdout, _ = run_command([cargo, command, *args, '--message-format=json'],
205256
stdout=subprocess.PIPE,
206257
stderr=subprocess.DEVNULL,
207258
cwd=cwd)
@@ -292,8 +343,31 @@ def build_userland_sysroot(args):
292343

293344

294345
def build_userland(args):
346+
HOST_CARGO = "host-cargo/bin/cargo"
347+
HOST_RUST = "host-rust/bin/rustc"
348+
HOST_GCC = "host-gcc/bin/x86_64-aero-gcc"
349+
HOST_BINUTILS = "host-binutils/x86_64-aero/bin"
350+
PACKAGE_MLIBC = "mlibc"
351+
352+
tool_dir = get_userland_tool()
353+
pkg_dir = get_userland_package()
354+
355+
def get_cargo(): return os.path.join('..', tool_dir, HOST_CARGO)
356+
def get_rustc(): return os.path.join('..', tool_dir, HOST_RUST)
357+
def get_gcc(): return os.path.join('..', tool_dir, HOST_GCC)
358+
def get_binutils(): return os.path.join("..", tool_dir, HOST_BINUTILS)
359+
def get_mlibc(): return os.path.join("..", pkg_dir, PACKAGE_MLIBC)
360+
295361
command = 'build'
296-
cmd_args = []
362+
cmd_args = ["--target", "x86_64-unknown-aero-system",
363+
364+
# cargo config
365+
"--config", f"build.rustc = '{get_rustc()}'",
366+
"--config", "build.target = 'x86_64-unknown-aero-system'",
367+
"--config", f"build.rustflags = ['-C', 'link-args=-no-pie -B {get_binutils()} --sysroot {get_mlibc()}', '-lc']",
368+
"--config", f"target.x86_64-unknown-aero-system.linker = '{get_gcc()}'",
369+
370+
"-Z", "unstable-options"]
297371

298372
if not args.debug:
299373
cmd_args += ['--release']
@@ -302,9 +376,9 @@ def build_userland(args):
302376
command = 'check'
303377

304378
if args.test:
305-
return build_cargo_workspace('userland', 'build', ['--package', 'utest', *cmd_args])
379+
return build_cargo_workspace('userland', 'build', ['--package', 'utest', *cmd_args], get_cargo())
306380
else:
307-
return build_cargo_workspace('userland', command, cmd_args)
381+
return build_cargo_workspace('userland', command, cmd_args, get_cargo())
308382

309383
# TODO: Userland check
310384
# elif args.check:
@@ -378,6 +452,15 @@ def cp(src, dest):
378452
os.makedirs(os.path.dirname(dest_file), exist_ok=True)
379453
shutil.copy(file, dest_file)
380454

455+
# dynamic linker (ld.so)
456+
mlibc = os.path.join(get_userland_package(), "mlibc")
457+
# gcc libraries required for rust programs
458+
gcc = os.path.join(get_userland_package(), "gcc")
459+
460+
if "host-rust-prebuilt" in str(mlibc):
461+
cp(mlibc, initramfs_root)
462+
cp(gcc, initramfs_root)
463+
381464
cp(BASE_FILES_DIR, initramfs_root)
382465

383466
for file in user_bins:

patches/mlibc/mlibc.patch

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 989da4c4aa01ce3ad7a52986f0492a6b565add28 Mon Sep 17 00:00:00 2001
1+
From 7483398f8048d7b25eae2015a6d67fc1feb061e6 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
33
Date: Thu, 10 Feb 2022 19:12:25 +1100
44
Subject: [PATCH] yes
@@ -7,12 +7,12 @@ Signed-off-by: Andy-Python-Programmer <[email protected]>
77
---
88
.gitignore | 3 +
99
options/rtdl/generic/linker.cpp | 2 +-
10-
sysdeps/aero/generic/aero.cpp | 12 +++-
10+
sysdeps/aero/generic/aero.cpp | 26 ++++++--
1111
sysdeps/aero/generic/filesystem.cpp | 97 ++++++++++++++++++++++++++---
1212
sysdeps/aero/generic/signals.cpp | 8 ++-
1313
sysdeps/aero/generic/sockets.cpp | 59 ++++++++++++++++++
14-
sysdeps/aero/include/aero/syscall.h | 11 ++++
15-
7 files changed, 179 insertions(+), 13 deletions(-)
14+
sysdeps/aero/include/aero/syscall.h | 12 ++++
15+
7 files changed, 191 insertions(+), 16 deletions(-)
1616

1717
diff --git a/.gitignore b/.gitignore
1818
index dbb35e8b..20c8d4c3 100644
@@ -39,10 +39,34 @@ index 53264175..38ba8c9f 100644
3939

4040
#if defined(__x86_64__)
4141
diff --git a/sysdeps/aero/generic/aero.cpp b/sysdeps/aero/generic/aero.cpp
42-
index 7de909f5..4281beb9 100644
42+
index 7de909f5..c8d076a5 100644
4343
--- a/sysdeps/aero/generic/aero.cpp
4444
+++ b/sysdeps/aero/generic/aero.cpp
45-
@@ -173,7 +173,15 @@ int sys_getcwd(char *buffer, size_t size) {
45+
@@ -131,12 +131,20 @@ void sys_exit(int status) {
46+
pid_t sys_getpid() {
47+
auto result = syscall(SYS_GETPID);
48+
49+
- // SAFETY: getpid does not return any errors (ie. the call is always
50+
- // successful).
51+
- __ensure(result >= 0);
52+
+ __ensure(result >= 0); // getpid() cannot fail.
53+
return result;
54+
}
55+
56+
+int sys_kill(int pid, int sig) {
57+
+ auto result = syscall(SYS_KILL, pid, sig);
58+
+
59+
+ if (result < 0) {
60+
+ return -result;
61+
+ }
62+
+
63+
+ return 0;
64+
+}
65+
+
66+
pid_t sys_getpgid(pid_t pid, pid_t *pgid) {
67+
mlibc::infoLogger() << "sys_getpgid() is unimplemented" << frg::endlog;
68+
*pgid = 0;
69+
@@ -173,7 +181,15 @@ int sys_getcwd(char *buffer, size_t size) {
4670
return 0;
4771
}
4872

@@ -59,7 +83,7 @@ index 7de909f5..4281beb9 100644
5983

6084
int sys_gethostname(char *buffer, size_t bufsize) {
6185
auto result = syscall(SYS_GETHOSTNAME, buffer, bufsize);
62-
@@ -286,7 +294,7 @@ int sys_execve(const char *path, char *const argv[], char *const envp[]) {
86+
@@ -286,7 +302,7 @@ int sys_execve(const char *path, char *const argv[], char *const envp[]) {
6387
__builtin_unreachable();
6488
}
6589

@@ -292,10 +316,10 @@ index e69de29b..b6b18fe7 100644
292316
+}
293317
+} // namespace mlibc
294318
diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h
295-
index 07b1b51b..6487cd85 100644
319+
index 07b1b51b..b6763b63 100644
296320
--- a/sysdeps/aero/include/aero/syscall.h
297321
+++ b/sysdeps/aero/include/aero/syscall.h
298-
@@ -49,6 +49,17 @@
322+
@@ -49,6 +49,18 @@
299323
#define SYS_DUP 42
300324
#define SYS_FCNTL 43
301325
#define SYS_DUP2 44
@@ -310,6 +334,7 @@ index 07b1b51b..6487cd85 100644
310334
+#define SYS_EPOLL_PWAIT 53
311335
+#define SYS_EPOLL_CTL 54
312336
+#define SYS_EVENT_FD 55
337+
+#define SYS_KILL 56
313338

314339
// Invalid syscall used to trigger a log error in the kernel (as a hint)
315340
// so, that we can implement the syscall in the kernel.

src/aero_kernel/src/arch/x86_64/interrupts/exceptions.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
168168

169169
let task = scheduler::get_scheduler().current_task();
170170
task.signal(aero_syscall::signal::SIGSEGV);
171-
172-
return;
173171
} else if !signal {
174172
} else {
175173
return;

src/aero_kernel/src/arch/x86_64/syscall.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::arch::gdt::GdtEntryType;
55
use crate::mem::paging::VirtAddr;
66
use crate::userland::scheduler;
77
use crate::utils::io;
8+
use crate::utils::sync::IrqGuard;
89

910
use super::interrupts::InterruptErrorStack;
1011

@@ -21,6 +22,8 @@ const ARCH_GET_GS: usize = 0x1004;
2122
fn arch_prctl(command: usize, address: usize) -> Result<usize, AeroSyscallError> {
2223
match command {
2324
ARCH_SET_FS => unsafe {
25+
let _guard = IrqGuard::new();
26+
2427
scheduler::get_scheduler()
2528
.current_task()
2629
.arch_task_mut()
@@ -36,6 +39,8 @@ fn arch_prctl(command: usize, address: usize) -> Result<usize, AeroSyscallError>
3639
.as_u64() as usize),
3740

3841
ARCH_SET_GS => unsafe {
42+
let _guard = IrqGuard::new();
43+
3944
scheduler::get_scheduler()
4045
.current_task()
4146
.arch_task_mut()

src/aero_kernel/src/arch/x86_64/task.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,12 @@ pub fn arch_task_spinup(from: &mut ArchTask, to: &ArchTask) {
443443
super::gdt::get_task_state_segement().rsp[0] = kstackp;
444444
io::wrmsr(io::IA32_SYSENTER_ESP, kstackp);
445445

446-
task_spinup(&mut from.context, to.context.as_ref());
447-
448-
// make a restore point for the current FS base.
449-
from.fs_base = VirtAddr::new(io::rdmsr(io::IA32_FS_BASE));
450446
// switch to the new FS base.
451447
io::wrmsr(io::IA32_FS_BASE, to.fs_base.as_u64());
452448

453-
// make a restore point for the current GS base.
454-
from.gs_base = VirtAddr::new(io::rdmsr(io::IA32_KERNEL_GSBASE));
455449
// update the swap GS target to point to the new GS base.
456450
io::wrmsr(io::IA32_KERNEL_GSBASE, to.gs_base.as_u64());
451+
452+
task_spinup(&mut from.context, to.context.as_ref());
457453
}
458454
}

src/aero_kernel/src/syscall/fs.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,17 @@ pub fn ioctl(fd: usize, command: usize, argument: usize) -> Result<usize, AeroSy
228228
.get_handle(fd)
229229
.ok_or(AeroSyscallError::EBADFD)?;
230230

231-
Ok(handle.inode().ioctl(command, argument)?)
231+
match command {
232+
// Sets the close-on-exec file descriptor flag. This is equivalent
233+
// to `fcntl(fd, F_SETFD, FD_CLOEXEC)`
234+
FIOCLEX => {
235+
handle.fd_flags.lock().insert(FdFlags::CLOEXEC);
236+
return Ok(0x00);
237+
}
238+
239+
// Handle file specific ioctl:
240+
_ => Ok(handle.inode().ioctl(command, argument)?),
241+
}
232242
}
233243

234244
#[syscall]

src/aero_kernel/src/syscall/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub fn generic_do_syscall(
181181
SYS_SIGACTION => process::sigaction(b, c, d, e),
182182
SYS_SIGPROCMASK => process::sigprocmask(b, c, d),
183183
SYS_CLONE => process::clone(b, c),
184+
SYS_KILL => process::kill(b, c),
184185

185186
SYS_READ => fs::read(b, c, d),
186187
SYS_OPEN => fs::open(b, c, d, e),

src/aero_kernel/src/syscall/process.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::fs::Path;
2929
use crate::mem::paging::VirtAddr;
3030
use crate::userland::scheduler;
3131
use crate::userland::signals::SignalEntry;
32+
use crate::userland::task::TaskId;
3233
use crate::utils::sync::IrqGuard;
3334

3435
static HOSTNAME: Once<Mutex<String>> = Once::new();
@@ -98,6 +99,21 @@ pub fn clone(entry: usize, stack: usize) -> Result<usize, AeroSyscallError> {
9899
Ok(cloned.pid().as_usize())
99100
}
100101

102+
#[syscall]
103+
pub fn kill(pid: usize, signal: usize) -> Result<usize, AeroSyscallError> {
104+
// If pid is positive, then signal is sent to the process with that pid.
105+
if pid > 0 {
106+
let task = scheduler::get_scheduler()
107+
.find_task(TaskId::new(pid))
108+
.ok_or(AeroSyscallError::ESRCH)?;
109+
110+
task.signal(signal);
111+
Ok(0)
112+
} else {
113+
unimplemented!()
114+
}
115+
}
116+
101117
#[syscall]
102118
pub fn exec(
103119
path: &Path,

0 commit comments

Comments
 (0)