Skip to content

Commit

Permalink
Build rust userspace using stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed May 17, 2024
1 parent 45a633a commit e076d93
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 221 deletions.
31 changes: 10 additions & 21 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = ["syscall-defs", "syscall-user", "user-alloc", "userspace", "acpica", "cykusz-rs"]
exclude = ["rust-test"]
members = ["syscall-defs", "syscall-user", "user-alloc", "acpica", "cykusz-rs"]
exclude = ["userspace", "sysroot/src/rust"]
resolver = "2"

[profile.release]
Expand Down
24 changes: 16 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ assembly_object_files := $(patsubst cykusz-rs/src/arch/$(arch)/asm/%.asm, \
build/arch/$(arch)/asm/%.o, $(assembly_source_files))

target ?= $(arch)-cykusz_os
target_user ?= $(arch)-unknown-cykusz
ifdef dev
rust_os := target/$(target)/debug/libcykusz_rs.a
rust_shell := target/$(target)/debug/shell
rust_init := target/$(target)/debug/init
rust_shell := userspace/target/$(target)/debug/shell
rust_init := userspace/target/$(target)/debug/init
kernel := build/kernel-$(arch)-g.bin
else
rust_os := target/$(target)/release/libcykusz_rs.a
rust_shell := target/$(target)/release/shell
rust_init := target/$(target)/release/init
rust_shell := userspace/target/$(target_user)/release/shell
rust_init := userspace/target/$(target_user)/release/init
kernel := build/kernel-$(arch).bin
endif
cross_c := sysroot/cross/bin/x86_64-cykusz-gcc
Expand Down Expand Up @@ -85,17 +86,24 @@ $(vdi): $(disk)
disk-scripts/make_vdi.sh
disk-scripts/attach_vdi.sh

$(kernel): cargo $(rust_os) $(assembly_object_files) $(linker_script)
$(kernel): cargo_kernel cargo_user $(rust_os) $(assembly_object_files) $(linker_script)
ld -n --whole-archive --gc-sections -T $(linker_script) -o $(kernel) $(assembly_object_files) $(rust_os)

usb: $(kernel)
sudo disk-scripts/install_usb.sh $(usb_dev)

cargo:
cargo_kernel:
ifdef dev
cargo build --workspace --verbose
cd cykusz-rs && cargo build --verbose && cd ../
else
cargo build --workspace --release --verbose
cd cykusz-rs && cargo build --release --verbose && cd ../
endif

cargo_user:
ifdef dev
CARGO_HOME=sysroot/cargo_home cd userspace && cargo build --verbose && cd ../
else
CARGO_HOME=sysroot/cargo_home cd userspace && cargo build --release --verbose && cd ../
endif

toolchain: $(cross_cpp)
Expand Down
File renamed without changes.
38 changes: 0 additions & 38 deletions cykusz-rs/src/arch/x86_64/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,44 +398,6 @@ impl Task {
)
}

#[cfg(disabled)]
pub fn new_user(entry: VirtAddr, hdr: &ElfHeader, vm: &VM, tls_vm: Option<TlsVmInfo>) -> Task {
logln!("entry point: {}", entry);

let p_table = prepare_p4();

vm.mmap_vm(
Some(VirtAddr(0x8000_0000_0000 - USER_STACK_SIZE)),
USER_STACK_SIZE,
MMapProt::PROT_WRITE | MMapProt::PROT_READ,
MMapFlags::MAP_FIXED | MMapFlags::MAP_PRIVATE | MMapFlags::MAP_ANONYOMUS,
None,
0,
);

let tls_ptr = if let Some(tls) = &tls_vm {
prepare_tls(vm, p_table, tls)
} else {
VirtAddr(0)
};

let f = unsafe { ::core::mem::transmute::<usize, fn()>(entry.0) };

let mut t = Task::new(
f as usize,
gdt::ring3_cs(),
gdt::ring3_ds(),
true,
p_table.phys_addr(),
Some(0x8000_0000_0000),
0,
);

t.user_fs_base = tls_ptr.0;

t
}

pub fn fork(&self) -> Task {
let orig_p4 = P4Table::new_mut_at_phys(PhysAddr(self.cr3));

Expand Down
18 changes: 0 additions & 18 deletions cykusz-rs/src/kernel/sched/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,6 @@ impl Scheduler {
task
}

#[cfg(disabled)]
fn create_user_task(&self, exe: DirEntryItem) -> Arc<Task> {
let task = Task::new_user(exe);

self.tasks.register_task(task.clone());

sessions().register_process(task.clone());

self.sched.queue_task(task.clone());

task
}

pub fn as_impl<T: SchedulerInterface>(&self) -> &T {
match self.sched.downcast_ref::<T>() {
Some(e) => e,
Expand Down Expand Up @@ -400,11 +387,6 @@ pub fn create_param_task(fun: usize, val: usize) -> Arc<Task> {
scheduler().create_param_task(fun, val)
}

#[cfg(disabled)]
pub fn create_user_task(exe: DirEntryItem) -> Arc<Task> {
scheduler().create_user_task(exe)
}

pub fn sleep(time_ns: Option<usize>, flags: SleepFlags) -> SignalResult<()> {
scheduler().sleep(time_ns, flags)
}
Expand Down
15 changes: 0 additions & 15 deletions cykusz-rs/src/kernel/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,6 @@ impl Task {
Self::make_ptr(task)
}

#[cfg(disabled)]
pub fn new_user(exe: DirEntryItem) -> Arc<Task> {
let mut task = Task::new();

let vm = task.vm();

if let Some((_base_addr, entry, elf_hdr, tls_vm)) = vm.load_bin(exe) {
task.arch_task = UnsafeCell::new(ArchTask::new_user(entry, &elf_hdr, vm, tls_vm));

Self::make_ptr(task)
} else {
panic!("Failed to exec task")
}
}

pub fn fork(&self) -> Arc<Task> {
let mut task = Task::new();

Expand Down
7 changes: 7 additions & 0 deletions cykusz-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern crate intrusive_collections;
extern crate lazy_static;

use core::arch::asm;
use syscall_defs::OpenFlags;
use crate::kernel::fs::{lookup_by_path, LookupMode};
use crate::kernel::fs::path::Path;

use crate::kernel::mm::VirtAddr;
use crate::kernel::sched::current_task_ref;
Expand Down Expand Up @@ -146,6 +149,10 @@ fn init_task() {

println!("[ OK ] Futexes Initialized");

current_task_ref().open_file(lookup_by_path(&Path::new("/dev/tty"), LookupMode::None).unwrap(), OpenFlags::WRONLY).expect("Failed to open tty");
current_task_ref().open_file(lookup_by_path(&Path::new("/dev/tty"), LookupMode::None).unwrap(), OpenFlags::WRONLY).expect("Failed to open tty");
current_task_ref().open_file(lookup_by_path(&Path::new("/dev/tty"), LookupMode::None).unwrap(), OpenFlags::WRONLY).expect("Failed to open tty");

// Start shell on this cpu
crate::kernel::init::exec();
}
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions disk-scripts/install_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ PROGS="test testcpp hello stack nyancat ttytest fork forktest poweroff stat fbdo

mount /dev/loop0p2 mnt
mkdir -p mnt/bin
cp -f target/x86_64-cykusz_os/release/init mnt/bin/init
cp -f target/x86_64-cykusz_os/release/shell mnt/bin/shell
cp -f userspace/target/x86_64-unknown-cykusz/release/init mnt/bin/init
cp -f userspace/target/x86_64-unknown-cykusz/release/shell mnt/bin/shell

for prog in $PROGS; do
cp -f sysroot/build/$prog mnt/bin/$prog
Expand Down
11 changes: 0 additions & 11 deletions rust-test/Cargo.lock

This file was deleted.

6 changes: 0 additions & 6 deletions rust-test/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions rust-test/src/main.rs

This file was deleted.

12 changes: 7 additions & 5 deletions sysroot/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CYKUSZ_DIR=$(realpath $SPATH/..)
SRC_DIR=$CYKUSZ_DIR/sysroot/src
BINUTILS_SRC_DIR=$SRC_DIR/binutils-gdb
GCC_SRC_DIR=$SRC_DIR/gcc
RUST_SRC_DIR=$HOME/sysroot/src/rust
RUST_SRC_DIR=$SRC_DIR/rust
LIBTOOL_SRC_DIR=$SRC_DIR/libtool
MLIBC_SRC_DIR=$SRC_DIR/mlibc
NYANCAT_SRC_DIR=$SRC_DIR/nyancat
Expand Down Expand Up @@ -64,7 +64,7 @@ LIBIDN2_CYKUSZ_BUILD_DIR=$BUILD_DIR/cykusz-libidn2
LIBFFI_CYKUSZ_BUILD_DIR=$BUILD_DIR/cykusz-libffi
LIBEXPAT_CYKUSZ_BUILD_DIR=$BUILD_DIR/cykusz-libexpat
GCC_BUILD_DIR=$BUILD_DIR/gcc
RUST_BUILD_DIR=$HOME/sysroot/build/rust
RUST_BUILD_DIR=$BUILD_DIR/rust
MLIBC_BUILD_DIR=$BUILD_DIR/mlibc

SYSROOT=$CYKUSZ_DIR/sysroot/cykusz
Expand Down Expand Up @@ -507,6 +507,8 @@ function _rust {
mkdir -p $CROSS/lib/rustlib/src
cd $CROSS/lib/rustlib/src
ln -sf $SRC_DIR/rust ./rust
cd $CROSS/bin
ln -sf $(which cargo)

popd
}
Expand Down Expand Up @@ -1054,9 +1056,9 @@ function _cykusz_apps {
_cykusz_nyancat

pushd .
cd $CYKUSZ_DIR/rust-test
CARGO_HOME=$SPATH/cargo_home cargo build --target x86_64-unknown-cykusz
cp target/x86_64-unknown-cykusz/debug/rust-test $BUILD_DIR/rust-test
cd $CYKUSZ_DIR/userspace
CARGO_HOME=$SPATH/cargo_home cargo build --release
cp target/x86_64-unknown-cykusz/release/init $BUILD_DIR/init
popd
}

Expand Down
1 change: 0 additions & 1 deletion sysroot/cfg/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 12
USER ck

RUN mkdir -p /home/ck/code/cykusz-rs
RUN mkdir -p /home/ck/sysroot
WORKDIR /home/ck/code/cykusz-rs/sysroot

RUN curl https://sh.rustup.rs -sSf | \
Expand Down
2 changes: 2 additions & 0 deletions sysroot/cfg/rust/host-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[patch.crates-io]
libc = { git = "https://github.com/rafalmiel/libc.git", branch = "cykusz" }
2 changes: 1 addition & 1 deletion sysroot/dockit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
SPATH=$(dirname $(readlink -f "$0"))
CYKUSZ_DIR=$(realpath $SPATH/..)

docker run -u $(id -u ${USER}):$(id -g ${USER}) -v $CYKUSZ_DIR:/home/ck/code/cykusz-rs -v $SPATH:/home/ck/sysroot cykusz-build:latest $*
docker run -u $(id -u ${USER}):$(id -g ${USER}) -v $CYKUSZ_DIR:/home/ck/code/cykusz-rs cykusz-build:latest $*
Loading

0 comments on commit e076d93

Please sign in to comment.