apps/rust: start of sdk runtime support for Rust apps

Rust application runtime support + equivalents of hello & fibnoacci.

Change-Id: Ica9b0d181387f159169cbe5f219d26c96540a56d
GitOrigin-RevId: 0a14b67ddd9b166a8ba5c13bac37a30204deb3b0
This commit is contained in:
Sam Leffler
2022-09-16 18:33:10 +00:00
parent d0d46c89e1
commit 95f8965986
15 changed files with 561 additions and 86 deletions

View File

@@ -0,0 +1,25 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[package]
name = "libkata"
version = "0.1.0"
edition = "2021"
[lib]
path = "lib.rs"
[dependencies]
sel4-sys = { path = "../../system/components/kata-os-common/src/sel4-sys", default-features = false }
static_assertions = "1.1"

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.section .text._start
.align 2
.globl _start
.type _start, @function
_start:
.option push
.option norelax
la gp, __global_pointer$
la x4, _tls
/* Setup __sel4_ipc_buffer */
lui t1, 0
add t1, t1, tp
sw a0, 0(t1)
/* Setup SDKRuntime RPC framework */
/* seL4_CPtr to SDKRuntime Endpoint */
la t1, KATA_SDK_ENDPOINT
sw a1, 0(t1)
/* seL4_CPtr to KATA_SDK_PARAMS Frame object */
la t1, KATA_SDK_FRAME
sw a2, 0(t1)
/* virtual address of KATA_SDK_PARAMS */
la t1, KATA_SDK_PARAMS
sw a3, 0(t1)
/* XXX included only for testing */
addi sp, sp, -16
sw a0, 12(sp)
sw a1, 8(sp)
sw a2, 4(sp)
sw a3, 0(sp)
.option pop
j main
.section .bss
.align 12
.globl _tls
.type _tls, tls_object
_tls:
.ds.b 4096
.align 2
.global KATA_SDK_ENDPOINT
KATA_SDK_ENDPOINT:
.ds.b 4
.align 2
.global KATA_SDK_FRAME
KATA_SDK_FRAME:
.ds.b 4
.align 2
.global KATA_SDK_PARAMS
KATA_SDK_PARAMS:
.ds.b 4

54
apps/rust/libkata/lib.rs Normal file
View File

@@ -0,0 +1,54 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![no_std]
#![allow(non_upper_case_globals)]
#![feature(global_asm)]
#![feature(thread_local)]
use core::arch::global_asm;
use sel4_sys::seL4_IPCBuffer;
use static_assertions::*;
// NB: this mimics the logic in build.rs
assert_cfg!(any(
all(target_arch = "arm", target_pointer_width = "32"),
all(target_arch = "aarch64"),
all(target_arch = "riscv32"),
all(target_arch = "riscv64"),
all(target_arch = "x86"),
all(target_arch = "x86_64"),
));
#[cfg(target_arch = "x86")]
global_asm!(include_str!("arch/x86/crt0.S"));
#[cfg(target_arch = "x86_64")]
global_asm!(include_str!("arch/x86_64/crt0.S"));
#[cfg(all(target_arch = "arm", target_pointer_width = "32"))]
global_asm!(include_str!("arch/aarch32/crt0.S"));
#[cfg(target_arch = "aarch64")]
global_asm!(include_str!("arch/aarch64/crt0.S"));
#[cfg(target_arch = "riscv32")]
global_asm!(include_str!("arch/riscv32/crt0.S"));
#[cfg(target_arch = "riscv64")]
global_asm!(include_str!("arch/riscv64/crt0.S"));
#[no_mangle]
#[thread_local]
static mut __sel4_ipc_buffer: *mut seL4_IPCBuffer = 0 as _;

View File

@@ -0,0 +1,35 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
MYDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
include $(MYDIR)/common.mk
BUILD_DIR := $(BUILD_ROOT)/$(APPNAME)
INTERMEDIATES := ${BUILD_DIR}/lib${APPNAME}.a
$(BUILD_DIR)/$(APPNAME).elf: lib${APPNAME} ${LIB_LIBC} | $(BUILD_DIR)
$(LD) $(LDFLAGS) -o $(BUILD_DIR)/$(APPNAME).elf $(INTERMEDIATES) $(LIB_LIBC)
lib${APPNAME}:
SEL4_OUT_DIR=${OUT_KATA}/kernel \
${CARGO} build ${CARGO_OPTS} --target-dir ${BUILD_DIR} --out-dir ${BUILD_DIR}
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
clean:
rm -rf $(BUILD_DIR)
.PHONY: clean lib${APPNAME}

View File

@@ -0,0 +1,20 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
BASE_ARCH_NAME := riscv
ARCH_BITS := 32
FULL_ARCH_NAME := riscv32imac
ARCH_PREFIX := riscv32-unknown-elf
ARCH := rv32imac
ABI := ilp32

View File

@@ -0,0 +1,56 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
MYDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
BUILD_TYPE ?= debug
BUILD_ARCH ?= riscv32
include $(MYDIR)/arch/$(BUILD_ARCH).mk
include $(MYDIR)/sel4.mk
ifeq ($(BUILD_TYPE),debug)
DEBUG := -g
OPT := -O0
CARGO_OPTS :=
else
DEBUG :=
OPT := -O0 # TODO(jtgans): Actually optimize in a release build
CARGO_OPTS := --release
endif
ROOTDIR ?= $(MYDIR)
BUILD_ROOT ?= $(ROOTDIR)/out/kata/$(ARCH_PREFIX)/$(BUILD_TYPE)/apps/rust
CC := $(ARCH_PREFIX)-gcc
AS := $(ARCH_PREFIX)-as
AR := $(ARCH_PREFIX)-ar
LD := $(ARCH_PREFIX)-gcc
KATA_RUST_VERSION ?= nightly-2021-11-05
CARGO := cargo +${KATA_RUST_VERSION}
CFLAGS := $(DEBUG) $(OPT) $(INCLUDES)
CFLAGS += -march=$(ARCH) -mabi=$(ABI)
CFLAGS += -std=gnu11 -nostdlib
CFLAGS += -ftls-model=${TLS_MODEL}
ASFLAGS := -march=$(ARCH) -mabi=$(ABI)
LDFLAGS := $(DEBUG) -nostartfiles -static -nostdlib
CARGO_OPTS += -Z unstable-options
CARGO_OPTS += -Z avoid-dev-deps
# XXX RUSTFLAGS is the only way to pass tls-model but seems to work w/o
#CARGO_OPTS += -Z tls-model=${TLS_MODEL}
CARGO_OPTS += --target ${FULL_ARCH_NAME}-unknown-none-elf

View File

@@ -0,0 +1,16 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
OUT_KATA ?= $(OUT)/kata/$(ARCH_PREFIX)/$(BUILD_TYPE)
TLS_MODEL := local-exec