Restructure and include basic Rust

This commit is contained in:
Garen Tyler 2023-10-14 16:21:21 -06:00
parent f5b93ef12f
commit 79ecd97c76
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
22 changed files with 65 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
*~
_*
*.o
*.a
*.d
*.asm
*.sym

View File

@ -1,5 +1,6 @@
K=kernel
U=user
P=programs
OBJS = \
$K/entry.o \
@ -11,6 +12,7 @@ OBJS = \
$K/spinlock.o \
$K/string.o \
$K/main.o \
$K/rust.a \
$K/vm.o \
$K/proc.o \
$K/swtch.o \
@ -63,6 +65,10 @@ CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
RUSTC = rustc
TARGET_TRIPLE = riscv64gc-unknown-none-elf
RUSTFLAGS = -C soft-float=n --target $(TARGET_TRIPLE)
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
CFLAGS += -fno-pie -no-pie
@ -89,6 +95,12 @@ tags: $(OBJS) _init
ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
%.a: %.rs
$(RUSTC) $(RUSTFLAGS) --crate-type staticlib -o $@ -L $K $^
%.o: %.c *.h
$(CC) $(CFLAGS) -c $^
_%: %.o $(ULIB)
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
$(OBJDUMP) -S $@ > $*.asm
@ -116,22 +128,22 @@ mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h
.PRECIOUS: %.o
UPROGS=\
$U/_cat\
$U/_echo\
$U/_forktest\
$U/_grep\
$U/_init\
$U/_kill\
$U/_ln\
$U/_ls\
$U/_mkdir\
$U/_rm\
$U/_sh\
$U/_stressfs\
$U/_usertests\
$U/_grind\
$U/_wc\
$U/_zombie\
$P/_cat\
$P/_echo\
$P/_forktest\
$P/_grep\
$P/_grind\
$P/_init\
$P/_kill\
$P/_ln\
$P/_ls\
$P/_mkdir\
$P/_rm\
$P/_sh\
$P/_stressfs\
$P/_usertests\
$P/_wc\
$P/_zombie\
fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
@ -140,7 +152,7 @@ fs.img: mkfs/mkfs README $(UPROGS)
clean:
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*/*.o */*.d */*.asm */*.sym \
*/*.o */*.a */*.d */*.asm */*.sym \
$U/initcode $U/initcode.out $K/kernel fs.img \
mkfs/mkfs .gdbinit \
$U/usys.S \

View File

@ -6,6 +6,8 @@
volatile static int started = 0;
void rust_main();
// start() jumps here in supervisor mode on all CPUs.
void
main()
@ -16,6 +18,7 @@ main()
printf("\n");
printf("xv6 kernel is booting\n");
printf("\n");
rust_main();
kinit(); // physical page allocator
kvminit(); // create kernel page table
kvminithart(); // turn on paging

View File

@ -59,6 +59,10 @@ printptr(uint64 x)
consputc(digits[x >> (sizeof(uint64) * 8 - 4)]);
}
void print(char *raw) {
printf("%s", raw);
}
// Print to the console. only understands %d, %x, %p, %s.
void
printf(char *fmt, ...)

26
kernel/rust.rs Normal file
View File

@ -0,0 +1,26 @@
#![no_std]
#![no_main]
use core::ffi::{c_char, CStr};
extern "C" {
fn print(message: *const c_char);
fn panic(panic_message: *const c_char) -> !;
}
#[no_mangle]
pub extern "C" fn rust_main() {
unsafe {
print(
CStr::from_bytes_with_nul(b"Hello from Rust!\0")
.unwrap()
.as_ptr(),
);
}
}
#[panic_handler]
unsafe fn panic_wrapper(_panic_info: &core::panic::PanicInfo) -> ! {
panic(CStr::from_bytes_with_nul(b"panic from rust\0").unwrap_or_default().as_ptr())
}

View File

@ -132,6 +132,8 @@ main(int argc, char *argv[])
char *shortname;
if(strncmp(argv[i], "user/", 5) == 0)
shortname = argv[i] + 5;
else if(strncmp(argv[i], "programs/", 9) == 0)
shortname = argv[i] + 9;
else
shortname = argv[i];