From 4cd2466900323c27937d2994b3b702266f840e46 Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Tue, 30 Apr 2024 00:23:04 +0300 Subject: [PATCH] First successful rust library call --- Makefile | 5 ++--- kernel/main.c | 6 +++++- rust/foo/Cargo.toml | 6 ++++++ rust/foo/src/lib.rs | 12 ++++++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 7516a3c..e4383e7 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ U=user R=rust RTARGET = riscv64gc-unknown-none-elf -RENV = debug OBJS = \ $K/entry.o \ @@ -35,7 +34,7 @@ OBJS = \ $K/virtio_disk.o RLIBS = \ - $R/foo/target/$(RTARGET)/$(RENV)/libfoo.rlib + $R/foo/target/$(RTARGET)/release/libfoo.a # riscv64-unknown-elf- or riscv64-linux-gnu- # perhaps in /opt/riscv/bin @@ -146,7 +145,7 @@ fs.img: mkfs/mkfs README $(UPROGS) -include kernel/*.d user/*.d rustlibs: - cargo build --manifest-path $R/foo/Cargo.toml + cargo build -r --manifest-path $R/foo/Cargo.toml clean: rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ diff --git a/kernel/main.c b/kernel/main.c index d2642ad..feb3ef8 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -3,6 +3,9 @@ #include "memlayout.h" #include "riscv.h" #include "defs.h" +#include + +extern int32_t add(int32_t right, int32_t left); volatile static int started = 0; @@ -13,7 +16,8 @@ void main() { printfinit(); printf("\n"); printf("xv6 kernel is booting\n"); - // printf("rustlib: add(1, 2) = %d\n", add(1, 2)); + printf("\n"); + printf("rust library call: add(1, 2) = %d\n", add(1, 2)); printf("\n"); kinit(); // physical page allocator kvminit(); // create kernel page table diff --git a/rust/foo/Cargo.toml b/rust/foo/Cargo.toml index 1d9cfe3..fe36c4f 100644 --- a/rust/foo/Cargo.toml +++ b/rust/foo/Cargo.toml @@ -3,6 +3,12 @@ name = "foo" version = "0.1.0" edition = "2021" +[lib] +crate-type = [ "staticlib" ] + +[profile.release] +panic = "abort" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/rust/foo/src/lib.rs b/rust/foo/src/lib.rs index 0e4f091..d635dc8 100644 --- a/rust/foo/src/lib.rs +++ b/rust/foo/src/lib.rs @@ -1,5 +1,13 @@ #![no_std] +#![crate_type = "staticlib"] -pub fn add(left: usize, right: usize) -> usize { - left + right + 5 +#[no_mangle] +pub extern "C" fn add(left: i32, right: i32) -> i32 { + left + right +} + +#[panic_handler] +#[no_mangle] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} }