Skip to content

Commit bed5857

Browse files
authored
Merge pull request #47 from torfmaster/feature/update-tock
Update dependencies
2 parents 7036a8c + 658d267 commit bed5857

12 files changed

+76
-76
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "tock"]
22
path = tock
3-
url = https://github.com/helena-project/tock
3+
url = https://github.com/tock/tock

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"editor.formatOnSave": true,
3-
"rust-client.channel": "nightly-2018-04-12",
3+
"rust-client.channel": "nightly-2018-04-19",
44
"rust.target": "thumbv7em-none-eabi",
5-
}
5+
}

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors = ["Tock Project Developers <[email protected]>"]
55
license = "MIT/Apache-2.0"
66

77
[dependencies]
8-
linked_list_allocator = "0.5.0"
8+
linked_list_allocator = "0.6.0"
99

1010
[dev-dependencies]
1111
corepack = { version = "0.3.1", default-features = false, features = ["alloc"] }

layout.ld

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ MEMORY {
2020
}
2121

2222
SECTIONS {
23-
/* Text section, Code! */
23+
/* Text section, Code! */
2424
.text :
2525
{
2626
_text = .;
@@ -38,6 +38,7 @@ SECTIONS {
3838
* uint32_t bss_start;
3939
* uint32_t bss_size;
4040
* uint32_t reldata_start;
41+
* uint32_t stack_size;
4142
* };
4243
*/
4344
/* Offset of GOT symbols in flash */
@@ -59,6 +60,8 @@ SECTIONS {
5960
/* First address offset after program flash, where elf2tbf places
6061
* .rel.data section */
6162
LONG(LOADADDR(.endsec) - _text);
63+
/* The size of the stack requested by this application */
64+
LONG(STACK_SIZE);
6265

6366
KEEP (*(.start))
6467
*(.text*)
@@ -70,42 +73,42 @@ SECTIONS {
7073
} > FLASH =0xFF
7174

7275

73-
/* App state section. Used for persistent app data. */
76+
/* App state section. Used for persistent app data. */
7477
.app_state :
7578
{
7679
KEEP (*(.app_state))
7780
} > FLASH =0xFF
7881

79-
/* Global Offset Table */
82+
/* Global Offset Table */
8083
.got :
8184
{
8285
_got = .;
8386
*(.got*)
8487
*(.got.plt*)
8588
} > SRAM AT > FLASH
8689

87-
/* Data section, static initialized variables
88-
* Note: This is placed in Flash after the text section, but needs to be
89-
* moved to SRAM at runtime
90-
*/
90+
/* Data section, static initialized variables
91+
* Note: This is placed in Flash after the text section, but needs to be
92+
* moved to SRAM at runtime
93+
*/
9194
.data :
9295
{
9396
_data = .;
9497
KEEP(*(.data*))
9598
} > SRAM AT > FLASH
9699

97-
/* BSS section, static uninitialized variables */
100+
/* BSS section, static uninitialized variables */
98101
.bss :
99102
{
100103
_bss = .;
101104
KEEP(*(.bss*))
102105
*(COMMON)
103106
} > SRAM
104107

105-
/*
108+
/*
106109
* __NOTE__: The following symbols are used only to hint to elf2tbf how much
107110
* total memory to request from the OS.
108-
*/
111+
*/
109112
.stack :
110113
{
111114
. += STACK_SIZE;
@@ -126,19 +129,19 @@ SECTIONS {
126129
{
127130
} > FLASH
128131

129-
/* ARM Exception support
130-
*
131-
* This contains compiler-generated support for unwinding the stack,
132-
* consisting of key-value pairs of function addresses and information on
133-
* how to unwind stack frames.
134-
* https://wiki.linaro.org/KenWerner/Sandbox/libunwind?action=AttachFile&do=get&target=libunwind-LDS.pdf
135-
*
136-
* .ARM.exidx is sorted, so has to go in its own output section.
132+
/* ARM Exception support
133+
*
134+
* This contains compiler-generated support for unwinding the stack,
135+
* consisting of key-value pairs of function addresses and information on
136+
* how to unwind stack frames.
137+
* https://wiki.linaro.org/KenWerner/Sandbox/libunwind?action=AttachFile&do=get&target=libunwind-LDS.pdf
138+
*
139+
* .ARM.exidx is sorted, so has to go in its own output section.
137140
*
138141
* __NOTE__: It's at the end because we currently don't actually serialize
139142
* it to the binary in elf2tbf. If it was before the RAM sections, it would
140143
* through off our calculations of the header.
141-
*/
144+
*/
142145
PROVIDE_HIDDEN (__exidx_start = .);
143146
.ARM.exidx :
144147
{
@@ -147,5 +150,4 @@ SECTIONS {
147150
} > FLASH
148151
PROVIDE_HIDDEN (__exidx_end = .);
149152

150-
151153
}

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2018-04-12
1+
nightly-2018-04-19

rustfmt.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
reorder_imports = true
2-
reorder_imported_names = true
32
reorder_modules = true
43
use_field_init_shorthand = true
54
use_try_shorthand = true

src/entry_point.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
extern crate linked_list_allocator;
22

33
use self::linked_list_allocator::Heap;
4-
use alloc::allocator::Alloc;
5-
use alloc::allocator::AllocErr;
6-
use alloc::allocator::Layout;
7-
use alloc::string::String;
8-
use console::Console;
4+
use core::alloc::Alloc;
5+
use core::alloc::GlobalAlloc;
6+
use core::alloc::Layout;
7+
use core::alloc::Opaque;
98
use core::mem;
109
use core::ptr;
11-
use fmt;
10+
use core::ptr::NonNull;
1211
use syscalls;
1312

14-
const HEAP_SIZE: usize = 0x200;
15-
const STACK_SIZE: usize = 0x400;
13+
const HEAP_SIZE: usize = 0x400;
1614

1715
// None-threaded heap wrapper based on `r9` register instead of global variable
18-
pub(crate) struct StackOwnedHeap;
16+
pub(crate) struct TockAllocator;
1917

20-
impl StackOwnedHeap {
18+
impl TockAllocator {
2119
unsafe fn heap(&self) -> &mut Heap {
2220
let heap: *mut Heap;
2321
asm!("mov $0, r9" : "=r"(heap) : : : "volatile");
@@ -28,26 +26,25 @@ impl StackOwnedHeap {
2826
///
2927
/// # Unsafety
3028
///
31-
/// This function must be called at most once. The heap_buffer must remain valid until the end of the process.
29+
/// This function must be called at most once. The memory between [`heap_location`] and [`heap_top`] must not overlap with any other memory section.
3230
#[inline(never)]
33-
unsafe fn init(&mut self, heap_buffer: &mut [u8]) {
34-
let heap_location = heap_buffer.as_ptr() as usize;
35-
asm!("mov r9, $0" : : "r"(heap_location) : : "volatile");
36-
let heap = heap_location as *mut Heap;
31+
unsafe fn init(&mut self, heap_bottom: usize, heap_top: usize) {
32+
asm!("mov r9, $0" : : "r"(heap_bottom) : : "volatile");
3733

38-
let heap_bottom = heap_location + mem::size_of::<Heap>();
39-
let heap_top = heap_location + heap_buffer.len();
40-
*heap = Heap::new(heap_bottom, heap_top - heap_bottom);
34+
let effective_heap_bottom = heap_bottom + mem::size_of::<Heap>();
35+
36+
let heap = heap_bottom as *mut Heap;
37+
*heap = Heap::new(effective_heap_bottom, heap_top - effective_heap_bottom);
4138
}
4239
}
4340

44-
unsafe impl<'a> Alloc for &'a StackOwnedHeap {
45-
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
46-
self.heap().alloc(layout)
41+
unsafe impl GlobalAlloc for TockAllocator {
42+
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
43+
self.heap().alloc(layout).unwrap().as_ptr()
4744
}
4845

49-
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
50-
self.heap().dealloc(ptr, layout)
46+
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
47+
self.heap().dealloc(NonNull::new_unchecked(ptr), layout)
5148
}
5249
}
5350

@@ -56,35 +53,24 @@ unsafe impl<'a> Alloc for &'a StackOwnedHeap {
5653
#[no_mangle]
5754
#[naked]
5855
#[link_section = ".start"]
59-
pub extern "C" fn _start(
60-
_text_start: usize,
56+
pub unsafe extern "C" fn _start(
57+
text_start: usize,
6158
mem_start: usize,
6259
_memory_len: usize,
6360
_app_heap_break: usize,
6461
) -> ! {
65-
unsafe {
66-
asm!("mov sp, $0" : : "r"(mem_start + HEAP_SIZE + STACK_SIZE) : "memory" : "volatile" );
67-
run_with_new_stack()
68-
}
69-
}
70-
71-
#[naked]
72-
#[inline(never)]
73-
unsafe fn run_with_new_stack() -> ! {
7462
extern "C" {
7563
// This function is created internally by`rustc`. See `src/lang_items.rs` for more details.
7664
fn main(argc: isize, argv: *const *const u8) -> isize;
7765
}
78-
let mut heap: [u8; HEAP_SIZE as usize] = [0; HEAP_SIZE as usize];
7966

80-
StackOwnedHeap.init(&mut heap);
67+
let stack_size = *(text_start as *const usize).offset(9);
68+
let real_stack_top = mem_start + stack_size;
69+
// Set the effective stack top below the real stack stop and use the space in between for the heap
70+
let effective_stack_top = real_stack_top - HEAP_SIZE;
71+
TockAllocator.init(effective_stack_top, real_stack_top);
8172

82-
let mut console = Console::new();
83-
console.write(String::from(
84-
"\nProcess started\n===============\nHeap position: ",
85-
));
86-
console.write(fmt::u32_as_hex(heap.as_ptr() as u32));
87-
console.write(String::from("\n\n"));
73+
asm!("mov sp, $0" : : "r"(effective_stack_top) : "memory" : "volatile" );
8874

8975
main(0, ptr::null());
9076

src/ipc/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl<CB> IpcServerCallback<CB> {
2222

2323
impl<CB: FnMut(usize, usize, &mut [u8])> SubscribableCallback for IpcServerCallback<CB> {
2424
fn call_rust(&mut self, arg0: usize, arg1: usize, arg2: usize) {
25+
// FIXME: This is unsafe because IpcServerCallback can be subscribed on any driver
2526
let mut v = unsafe { slice::from_raw_parts_mut(arg2 as *mut u8, arg1) };
2627
(self.callback)(arg0, arg1, &mut v);
2728
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ mod syscalls;
3333

3434
#[cfg(target_arch = "arm")]
3535
#[global_allocator]
36-
static ALLOCATOR: entry_point::StackOwnedHeap = entry_point::StackOwnedHeap;
36+
static ALLOCATOR: entry_point::TockAllocator = entry_point::TockAllocator;

src/shared_memory.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ use core::ptr;
22
use syscalls;
33

44
pub struct SharedMemory<'a> {
5-
pub driver_number: usize,
6-
pub allow_number: usize,
7-
pub buffer_to_share: &'a mut [u8],
5+
driver_number: usize,
6+
allow_number: usize,
7+
buffer_to_share: &'a mut [u8],
88
}
99

1010
impl<'a> SharedMemory<'a> {
11+
pub fn new(
12+
driver_number: usize,
13+
allow_number: usize,
14+
buffer_to_share: &'a mut [u8],
15+
) -> SharedMemory<'a> {
16+
SharedMemory {
17+
driver_number,
18+
allow_number,
19+
buffer_to_share,
20+
}
21+
}
22+
1123
pub fn read_bytes(&self, destination: &mut [u8]) {
1224
safe_copy(self.buffer_to_share, destination);
1325
}

src/syscalls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ pub fn allow(
110110
)
111111
};
112112
if return_code == 0 {
113-
Ok(SharedMemory {
113+
Ok(SharedMemory::new(
114114
driver_number,
115115
allow_number,
116116
buffer_to_share,
117-
})
117+
))
118118
} else {
119119
Err(return_code)
120120
}
@@ -123,7 +123,7 @@ pub fn allow(
123123
pub unsafe fn allow_ptr(major: usize, minor: usize, slice: *mut u8, len: usize) -> isize {
124124
let res;
125125
asm!("svc 3" : "={r0}"(res)
126-
: "{r0}"(major) "{r1}"(minor) "{r2}"(slice as *mut u8) "{r3}"(len)
126+
: "{r0}"(major) "{r1}"(minor) "{r2}"(slice) "{r3}"(len)
127127
: "memory"
128128
: "volatile");
129129
res

tock

Submodule tock updated 238 files

0 commit comments

Comments
 (0)