Skip to content

Commit c436ffe

Browse files
Johnathan Van Whyalistair23
Johnathan Van Why
authored andcommitted
Update the Rust toolchain to 2021-03-25.
This includes rust-lang/rust#82141, which is needed in order to add ARM support to libtock_runtime. It also allows us to use the unsafe_op_in_unsafe_fn lint.
1 parent d620f96 commit c436ffe

File tree

14 files changed

+75
-89
lines changed

14 files changed

+75
-89
lines changed

core/platform/src/command_return_tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn failure_u64() {
130130
assert_eq!(command_return.get_failure_2_u32(), None);
131131
assert_eq!(
132132
command_return.get_failure_u64(),
133-
Some((ErrorCode::Busy, 0x00001003_00001002))
133+
Some((ErrorCode::Busy, 0x0000_1003_0000_1002))
134134
);
135135
assert_eq!(command_return.get_success_u32(), None);
136136
assert_eq!(command_return.get_success_2_u32(), None);
@@ -240,7 +240,10 @@ fn success_u64() {
240240
assert_eq!(command_return.get_failure_u64(), None);
241241
assert_eq!(command_return.get_success_u32(), None);
242242
assert_eq!(command_return.get_success_2_u32(), None);
243-
assert_eq!(command_return.get_success_u64(), Some(0x00001002_00001001));
243+
assert_eq!(
244+
command_return.get_success_u64(),
245+
Some(0x0000_1002_0000_1001)
246+
);
244247
assert_eq!(command_return.get_success_3_u32(), None);
245248
assert_eq!(command_return.get_success_u32_u64(), None);
246249
assert_eq!(command_return.return_variant(), return_variant::SUCCESS_U64);
@@ -299,7 +302,7 @@ fn success_u32_u64() {
299302
assert_eq!(command_return.get_success_3_u32(), None);
300303
assert_eq!(
301304
command_return.get_success_u32_u64(),
302-
Some((1001, 0x00001003_00001002))
305+
Some((1001, 0x0000_1003_0000_1002))
303306
);
304307
assert_eq!(
305308
command_return.return_variant(),

core/platform/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![no_std]
2+
#![warn(unsafe_op_in_unsafe_fn)]
23

34
mod async_traits;
45
mod command_return;

core/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
#![feature(asm)]
2222
#![no_std]
23+
#![warn(unsafe_op_in_unsafe_fn)]
2324

2425
mod startup;
2526

core/runtime/src/syscalls_impl_riscv.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ impl RawSyscalls for crate::TockSyscalls {
4141
mut r3: usize,
4242
class: u8,
4343
) -> (u32, usize, usize, usize) {
44-
asm!("ecall",
45-
inlateout("a0") r0,
46-
inlateout("a1") r1,
47-
inlateout("a2") r2,
48-
inlateout("a3") r3,
49-
in("a4") class,
50-
options(preserves_flags, nostack),
51-
);
44+
unsafe {
45+
asm!("ecall",
46+
inlateout("a0") r0,
47+
inlateout("a1") r1,
48+
inlateout("a2") r2,
49+
inlateout("a3") r3,
50+
in("a4") class,
51+
options(preserves_flags, nostack),
52+
);
53+
}
5254
(r0, r1 as usize, r2, r3)
5355
}
5456

core/src/entry_point/start_item_arm.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
use core::hint;
2-
31
/// Tock programs' entry point. Called by the kernel at program start. Sets up
42
/// the stack then calls rust_start() for the remainder of setup.
53
#[doc(hidden)]
64
#[no_mangle]
75
#[naked]
86
#[link_section = ".start"]
9-
pub unsafe extern "C" fn _start(
10-
app_start: usize,
11-
mem_start: usize,
12-
_memory_len: usize,
13-
app_heap_break: usize,
14-
) -> ! {
15-
llvm_asm!("
7+
pub unsafe extern "C" fn _start() -> ! {
8+
asm!(
9+
"
1610
// Because ROPI-RWPI support in LLVM/rustc is incomplete, Rust
1711
// applications must be statically linked. An offset between the
1812
// location the program is linked at and its actual location in flash
@@ -102,12 +96,8 @@ pub unsafe extern "C" fn _start(
10296
mov r2, r8
10397
10498
// Call rust_start
105-
bl rust_start"
106-
: // No output operands
107-
: "{r0}"(app_start), "{r1}"(mem_start), "{r3}"(app_heap_break) // Input operands
108-
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r12",
109-
"cc", "memory" // Clobbers
110-
: "volatile" // Options
111-
);
112-
hint::unreachable_unchecked()
99+
bl rust_start",
100+
// No clobbers because we don't return.
101+
options(noreturn),
102+
)
113103
}

core/src/entry_point/start_item_riscv32.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::hint;
2-
31
/// Tock programs' entry point. Called by the kernel at program start. Sets up
42
/// the stack then calls rust_start() for the remainder of setup.
53
#[doc(hidden)]
@@ -14,12 +12,12 @@ use core::hint;
1412
// Due to Rust issue: https://github.com/rust-lang/rust/issues/42779 we can't have
1513
// args to the function
1614
pub unsafe extern "C" fn _start() -> ! {
17-
llvm_asm!(
18-
// Compute the stack top.
19-
//
20-
// struct hdr* myhdr = (struct hdr*) app_start;
21-
// uint32_t stacktop = (((uint32_t) mem_start + myhdr->stack_size + 7) & 0xfffffff8);
22-
"lw t0, 36(a0) // t0 = myhdr->stack_size
15+
asm!(
16+
// Compute the stack top.
17+
//
18+
// struct hdr* myhdr = (struct hdr*) app_start;
19+
// uint32_t stacktop = (((uint32_t) mem_start + myhdr->stack_size + 7) & 0xfffffff8);
20+
"lw t0, 36(a0) // t0 = myhdr->stack_size
2321
addi t0, t0, 7 // t0 = myhdr->stack_size + 7
2422
add t0, t0, a1 // t0 = mem_start + myhdr->stack_size + 7
2523
li t1, 7 // t1 = 7
@@ -89,14 +87,10 @@ pub unsafe extern "C" fn _start() -> ! {
8987
mv s0, sp // Set the frame pointer to sp.
9088
mv a1, s1 // second arg is stacktop
9189
mv a2, t1 // third arg is app_heap_break that we told the kernel
92-
jal rust_start"
93-
: // No output operands
94-
:
95-
: "memory", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17",
96-
"x5", "x6", "x7", "x28", "x29", "x30", "x31", "x1" // Clobbers
97-
: "volatile" // Options
98-
);
99-
hint::unreachable_unchecked();
90+
jal rust_start",
91+
// No clobbers needed for a noreturn asm! block.
92+
options(noreturn),
93+
)
10094
}
10195

10296
/// Ensure an abort symbol exists.

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(lang_items, llvm_asm, naked_functions)]
1+
#![feature(asm, lang_items, llvm_asm, naked_functions)]
22
#![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)]
33
#![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
44

layout_generic.ld

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ SECTIONS {
2424
/* Section for just the app crt0 header.
2525
* This must be first so that the app can find it.
2626
*/
27-
.crt0_header :
27+
/* Runtime setup logic. The crt0_header symbol is used by the entry point
28+
* assembly. We have to include start here rather than .text because
29+
* otherwise elf2tab fails to recognize that the process binary's flash
30+
* region should start at the beginning of .start.
31+
*/
32+
.start :
2833
{
2934
_beginning = .; /* Start of the app in flash. */
35+
crt0_header = .;
3036
/**
3137
* Populate the header expected by `crt0`:
3238
*
@@ -68,14 +74,15 @@ SECTIONS {
6874
* between the header and subsequent .data section. It's unclear why,
6975
* but LLD is aligning sections to a multiple of 32 bytes. */
7076
. = ALIGN(32);
77+
78+
*(.start)
7179
} > FLASH =0xFF
7280

7381
/* Text section, Code! */
7482
.text :
7583
{
7684
. = ALIGN(4);
7785
_text = .;
78-
KEEP (*(.start))
7986
*(.text*)
8087
*(.rodata*)
8188
KEEP (*(.syscalls))
@@ -140,30 +147,10 @@ SECTIONS {
140147
{
141148
} > FLASH
142149

143-
/* ARM Exception support
144-
*
145-
* This contains compiler-generated support for unwinding the stack,
146-
* consisting of key-value pairs of function addresses and information on
147-
* how to unwind stack frames.
148-
* https://wiki.linaro.org/KenWerner/Sandbox/libunwind?action=AttachFile&do=get&target=libunwind-LDS.pdf
149-
*
150-
* .ARM.exidx is sorted, so has to go in its own output section.
151-
*
152-
* __NOTE__: It's at the end because we currently don't actually serialize
153-
* it to the binary in elf2tbf. If it was before the RAM sections, it would
154-
* through off our calculations of the header.
155-
*/
156-
PROVIDE_HIDDEN (__exidx_start = .);
157-
.ARM.exidx :
158-
{
159-
/* (C++) Index entries for section unwinding */
160-
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
161-
} > FLASH
162-
PROVIDE_HIDDEN (__exidx_end = .);
163-
150+
/* Sections we do not need. */
164151
/DISCARD/ :
165152
{
166-
*(.eh_frame)
153+
*(.ARM.exidx .eh_frame)
167154
}
168155
}
169156

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[toolchain]
22
# See https://rust-lang.github.io/rustup-components-history/ for a list of
33
# recently nightlies and what components are available for them.
4-
channel = "nightly-2020-08-20"
4+
channel = "nightly-2021-03-25"
55
components = ["clippy", "miri", "rustfmt"]
66
targets = ["thumbv7em-none-eabi",
77
"riscv32imac-unknown-none-elf",

src/ble_composer.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct BlePayload {
1515
}
1616

1717
impl BlePayload {
18-
pub fn add(&mut self, kind: u8, content: &[u8]) -> Result<(), ()> {
18+
pub fn add(&mut self, kind: u8, content: &[u8]) -> Result<(), Overflow> {
1919
self.check_can_write_num_bytes(content.len() + 2)?;
2020

2121
self.bytes[self.occupied] = (content.len() + 1) as u8;
@@ -26,7 +26,7 @@ impl BlePayload {
2626
Ok(())
2727
}
2828

29-
pub fn add_flag(&mut self, flag: u8) -> Result<(), ()> {
29+
pub fn add_flag(&mut self, flag: u8) -> Result<(), Overflow> {
3030
self.check_can_write_num_bytes(3)?;
3131

3232
self.bytes[self.occupied] = 2;
@@ -36,7 +36,7 @@ impl BlePayload {
3636
Ok(())
3737
}
3838

39-
pub fn add_service_payload(&mut self, uuid: [u8; 2], content: &[u8]) -> Result<(), ()> {
39+
pub fn add_service_payload(&mut self, uuid: [u8; 2], content: &[u8]) -> Result<(), Overflow> {
4040
self.check_can_write_num_bytes(4 + content.len())?;
4141
self.bytes[self.occupied] = (content.len() + 3) as u8;
4242
self.bytes[self.occupied + 1] = gap_types::SERVICE_DATA;
@@ -49,11 +49,11 @@ impl BlePayload {
4949
Ok(())
5050
}
5151

52-
fn check_can_write_num_bytes(&self, number: usize) -> Result<(), ()> {
52+
fn check_can_write_num_bytes(&self, number: usize) -> Result<(), Overflow> {
5353
if self.occupied + number <= self.bytes.len() {
5454
Ok(())
5555
} else {
56-
Err(())
56+
Err(Overflow)
5757
}
5858
}
5959
}
@@ -72,6 +72,10 @@ impl AsRef<[u8]> for BlePayload {
7272
}
7373
}
7474

75+
// Error type returned when the buffer is too full to perform an operation.
76+
#[derive(Debug)]
77+
pub struct Overflow;
78+
7579
#[cfg(test)]
7680
mod test {
7781
use super::*;

src/debug/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub unsafe fn dump_address(address: *const usize) {
2828
write_as_hex(&mut buffer[12..22], *address);
2929
for index in 0..4 {
3030
let byte = *(address as *const u8).offset(index);
31-
let byte_is_printable_char = byte >= 0x20 && byte < 0x80;
31+
let byte_is_printable_char = (0x20..0x80).contains(&byte);
3232
if byte_is_printable_char {
3333
buffer[23 + index as usize] = byte;
3434
}

src/sensors/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ macro_rules! single_value_sensor {
4646
}
4747
}
4848

49-
impl Into<i32> for $type_name {
50-
fn into(self) -> i32 {
51-
self.value
49+
impl From<$type_name> for i32 {
50+
fn from(sensor: $type_name) -> i32 {
51+
sensor.value
5252
}
5353
}
5454

test_runner/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ fn process_output(stdout: ChildStdout) -> Result<(), Box<dyn std::error::Error>>
5353
}
5454

5555
fn test_succeeded(input: String, failed_tests: &mut Vec<String>) -> Option<bool> {
56-
let success = input.find("[ OK ]").is_some();
57-
let failure = input.find("[ FAILURE ]").is_some();
56+
let success = input.contains("[ OK ]");
57+
let failure = input.contains("[ FAILURE ]");
5858
let input = input.replace("[ OK ]", "");
5959
let input = input.replace("[ FAILURE ]", "");
6060
let input = input.trim();

tools/print_sizes/src/main.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,29 @@ fn main() {
117117
.expect("No examples found");
118118
let section_width = 7;
119119

120-
// TODO: We do not currently print out .rodata's size. Currently, the linker
121-
// script embeds .rodata in .text, so we don't see it as a separate section
122-
// here. We should modify the linker script to put .rodata in its own
123-
// section. Until that is done, .rodata's size will be counted as part of
124-
// .text, so we'll just print .text's size for now.
125120
println!(
126-
"{0:1$} {2:3$} {4:>7$} {5:>7$} {6:>7$}",
127-
"Example", name_width, "Architecture", arch_width, ".bss", ".data", ".text", section_width
121+
"{0:1$} {2:3$} {4:>8$} {5:>8$} {6:>8$} {7:>8$}",
122+
"Example",
123+
name_width,
124+
"Architecture",
125+
arch_width,
126+
".bss",
127+
".data",
128+
".text",
129+
".rodata",
130+
section_width
128131
);
129132
for data in example_data {
130133
println!(
131-
"{0:1$} {2:3$} {4:7$} {5:7$} {6:7$}",
134+
"{0:1$} {2:3$} {4:8$} {5:8$} {6:8$} {7:8$}",
132135
data.name,
133136
name_width,
134137
data.arch,
135138
arch_width,
136139
data.sizes.bss,
137140
data.sizes.data,
138141
data.sizes.text,
142+
data.sizes.rodata,
139143
section_width
140144
);
141145
}

0 commit comments

Comments
 (0)