Skip to content

Commit a973d13

Browse files
authored
Merge pull request #490 from Linaro/rust-x86
Initial support for Rust on x86_64
2 parents 56cf15f + 3189c6d commit a973d13

File tree

9 files changed

+78
-12
lines changed

9 files changed

+78
-12
lines changed

.github/workflows/github-ci-rust.yml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
# Currently, this is hard-coded here, but should probably be gathered
2525
# from the source.
2626
- qemu-virt-arm64-test
27+
- pc-x86-64-test
2728
env:
2829
PROJECT: ${{ matrix.project }}
2930
TOOLCHAIN_VER: ${{ matrix.toolchain-ver }}
@@ -64,16 +65,6 @@ jobs:
6465
echo "LIBGCC=" >> $GITHUB_ENV
6566
cat "$GITHUB_ENV"
6667
67-
# Install Rust
68-
# Note that rustup is already included in the image, and we just need to add our target.
69-
- name: install rust
70-
shell: bash
71-
run: |
72-
rustup install nightly
73-
rustup component add rust-src --toolchain nightly
74-
rustup target add aarch64-unknown-linux-gnu
75-
rustup target add aarch64-unknown-none
76-
7768
# build it
7869
- name: build
7970
shell: bash

arch/x86/64/x86_64-llvm.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"arch": "x86_64",
3+
"rustc-abi": "x86-softfloat",
4+
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
5+
"features": "-mmx,+soft-float,-retpoline-external-thunk,-retpoline-indirect-branches,-retpoline-indirect-calls,+harden-sls-ijmp,+harden-sls-ret",
6+
"llvm-target": "x86_64-linux-gnu",
7+
"supported-sanitizers": ["kcfi","kernel-address"],
8+
"target-pointer-width": 64,
9+
"emit-debug-gdb-scripts": false,
10+
"frame-pointer": "may-omit",
11+
"stack-probes": {"kind": "none"}
12+
}

lib/libc/string/bcmp.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3+
** Distributed under the terms of the NewOS License.
4+
*/
5+
/*
6+
* Copyright (c) 2008 Travis Geiselbrecht
7+
*
8+
* Use of this source code is governed by a MIT-style
9+
* license that can be found in the LICENSE file or at
10+
* https://opensource.org/licenses/MIT
11+
*/
12+
#include <string.h>
13+
#include <sys/types.h>
14+
15+
int
16+
bcmp(const void *cs, const void *ct, size_t count) {
17+
return memcmp(cs, ct, count);
18+
}

lib/libc/string/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
LOCAL_DIR := $(GET_LOCAL_DIR)
22

33
C_STRING_OPS := \
4+
bcmp \
45
bcopy \
56
bzero \
67
memchr \

lib/rust_support/Cargo.toml.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Rust meta crate
22

3+
cargo-features = ["panic-immediate-abort"]
4+
35
# This crate brings in the other dependencies needed so that cargo builds all of the Rust code that
46
# we need.
57

@@ -22,9 +24,11 @@ debug-assertions = true
2224
overflow-checks = true
2325
debug = true
2426
codegen-units = 1
27+
panic = "immediate-abort"
2528

2629
[profile.dev]
2730
opt-level = 2
2831
codegen-units = 1
32+
panic = "immediate-abort"
2933

3034
@DEPCRATES@

lib/rust_support/rules.mk

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ MODULE_SRCDIR := $(MODULE)
2121
MODULE_BUILDDIR := $(call TOBUILDDIR,$(MODULE_SRCDIR))
2222

2323
MODULE_SRCS := \
24+
$(MODULE_SRCDIR)/rust-toolchain.toml \
2425
$(MODULE_SRCDIR)/Cargo.toml \
2526
$(MODULE_SRCDIR)/src/lib.rs
2627
MODULE_OBJS := $(foreach d,$(MODULE_SRCS),$(call TOBUILDDIR,$(d)))
@@ -45,10 +46,19 @@ ifeq ($(RUST_TARGET_PATH),)
4546
RUST_TARGET_PATH := $(RUST_TARGET)
4647
endif
4748

49+
ifndef RUST_CFLAGS
50+
RUST_CFLAGS :=
51+
endif
52+
4853
define TOML_ESC
4954
$(subst \,\\,$(subst ",\",$1))
5055
endef
5156

57+
# Expand a make "list" (space separated) into a quoted version appropriate for inserting into TOML.
58+
define TOML_ARRAY
59+
[$(foreach w,$(1),\"$(w)\",)]
60+
endef
61+
5262
CARGO_CONFIG := $(MODULE_BUILDDIR)/.cargo/config.toml
5363

5464
# TODO: Allow debug/release builds
@@ -62,7 +72,7 @@ $(MODULE_OBJECT): MODULE_BUILDDIR:=$(MODULE_BUILDDIR)
6272
# Override with module local values for the build rule.
6373
$(MODULE_OBJECT): $(MODULE_OBJECT).phony $(MODULE_OBJS) $(CARGO_CONFIG)
6474
cd $(MODULE_BUILDDIR); \
65-
cargo +nightly build
75+
cargo build
6676

6777
EXTRA_OBJS := $(EXTRA_OBJS) $(MODULE_OBJECT)
6878

@@ -96,6 +106,9 @@ $(CARGO_CONFIG): $(CARGO_CONFIG).phony
96106
echo "[env]"; \
97107
echo 'GLOBAL_INCLUDES = { value = "$(call TOML_ESC,$(GLOBAL_INCLUDES))", force = true }'; \
98108
echo; \
109+
echo "[target.'cfg(all())']"; \
110+
echo "rustflags = $(call TOML_ARRAY,$(RUST_CFLAGS))"; \
111+
echo; \
99112
echo "[unstable]"; \
100113
echo 'build-std = ["core", "alloc"]'; \
101114
} > "$$tmp"; \
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "nightly-2025-12-04"
3+
components = ["rust-src"]

platform/pc/rules.mk

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,22 @@ LK_HEAP_IMPLEMENTATION ?= dlmalloc
3636
GLOBAL_DEFINES += \
3737
PLATFORM_HAS_DYNAMIC_TIMER=1
3838

39-
include make/module.mk
39+
# TODO: This probably needs to be different for 32 and 64 bit.
40+
RUST_TARGET := x86_64-llvm
41+
RUST_TARGET_PATH := $(abspath $(BUILDROOT))/arch/x86/64/x86_64-llvm.json
42+
43+
RUST_CFLAGS := \
44+
-Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 \
45+
-Ctarget-cpu=x86-64 \
46+
-Ztune-cpu=generic \
47+
-Cno-redzone=y \
48+
-Copt-level=2 \
49+
-Cdebug-assertions=n \
50+
-Coverflow-checks=y \
51+
-Ccode-model=kernel \
52+
-Cforce-frame-pointers=y
53+
# -Zdwarf-version=5 -Cdebuginfo=2
54+
# -Zfunction-return=thunk-extern
55+
# -Zpatchable-function-entry=16,16
4056

57+
include make/module.mk

project/pc-x86-64-test.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ include project/target/pc.mk
77
include project/virtual/fs.mk
88
include project/virtual/minip.mk
99
include project/virtual/test.mk
10+
11+
USE_RUST ?= 0
12+
13+
ifeq ($(call TOBOOL,$(USE_RUST)),true)
14+
$(info "Including rust support")
15+
include project/virtual/rust.mk
16+
endif

0 commit comments

Comments
 (0)