Skip to content

Commit c04d286

Browse files
authored
APS updates (#2528)
These are some minor updates from walking through the session myself. * Add some context to the `entry.S` slide, which is otherwise a bit terrifying for someone who does not speak ARM assembly. * Include a simple, fake example of MMIO. * Add a "Using It" section to the minimal UART segment, parallel to the better UART * Better explanation of the `unwrap` calls in the logging example. Unwrap is never "unsafe", so remove that word. * Allow dead code in some `.rs` files. * Remove redundant warning about use of memory before MMU setup. * Rephase text about buddy-system * Fix lint warning in spin slide.
1 parent 3764569 commit c04d286

File tree

12 files changed

+53
-28
lines changed

12 files changed

+53
-28
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
- [MMIO](bare-metal/aps/mmio.md)
337337
- [Let's Write a UART Driver](bare-metal/aps/uart.md)
338338
- [More Traits](bare-metal/aps/uart/traits.md)
339+
- [Using It](bare-metal/aps/uart/using.md)
339340
- [A Better UART Driver](bare-metal/aps/better-uart.md)
340341
- [Bitflags](bare-metal/aps/better-uart/bitflags.md)
341342
- [Multiple Registers](bare-metal/aps/better-uart/registers.md)

src/bare-metal/aps/better-uart/using.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ echo incoming bytes.
99

1010
<details>
1111

12-
- As in the [inline assembly](../inline-assembly.md) example, this `main`
13-
function is called from our entry point code in `entry.S`. See the speaker
14-
notes there for details.
1512
- Run the example in QEMU with `make qemu` under `src/bare-metal/aps/examples`.
1613

1714
</details>

src/bare-metal/aps/entry-point.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Before we can start running Rust code, we need to do some initialisation.
88

99
<details>
1010

11+
This code is in `src/bare-metal/aps/examples/entry.S`. It's not necessary to
12+
understand this in detail -- the takeaway is that typically some low-level setup
13+
is needed to meet Rust's expectations of the system.
14+
1115
- This is the same as it would be for C: initialising the processor state,
1216
zeroing the BSS, and setting up the stack pointer.
1317
- The BSS (block starting symbol, for historical reasons) is the part of the

src/bare-metal/aps/examples/src/pl011.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#![allow(dead_code)]
1415

1516
use core::fmt::{self, Write};
1617

src/bare-metal/aps/logging.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ We can do this by implementing the `Log` trait.
99

1010
<details>
1111

12-
- The unwrap in `log` is safe because we initialise `LOGGER` before calling
13-
`set_logger`.
12+
- The first unwrap in `log` will succeed because we initialise `LOGGER` before
13+
calling `set_logger`. The second will succeed because `Uart::write_str` always
14+
returns `Ok`.
1415

1516
</details>
1617

src/bare-metal/aps/mmio.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
- Use `&raw` to get fields of structs without creating an intermediate
66
reference.
77

8+
```rust,editable,ignore
9+
const SOME_DEVICE_REGISTER: *mut u64 = 0x800_0000 as _;
10+
// SAFETY: Some device is mapped at this address.
11+
unsafe {
12+
SOME_DEVICE_REGISTER.write_volatile(0xff);
13+
SOME_DEVICE_REGISTER.write_volatile(0x80);
14+
assert_eq!(SOME_DEVICE_REGISTER.read_volatile(), 0xaa);
15+
}
16+
```
17+
818
[`pointer::read_volatile`]: https://doc.rust-lang.org/stable/core/primitive.pointer.html#method.read_volatile
919
[`pointer::write_volatile`]: https://doc.rust-lang.org/stable/core/primitive.pointer.html#method.write_volatile
1020
[`addr_of!`]: https://doc.rust-lang.org/stable/core/ptr/macro.addr_of.html

src/bare-metal/aps/other-projects.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,8 @@
1515
<details>
1616

1717
- The RaspberryPi OS tutorial runs Rust code before the MMU and caches are
18-
enabled. This will read and write memory (e.g. the stack). However:
19-
- Without the MMU and cache, unaligned accesses will fault. It builds with
20-
`aarch64-unknown-none` which sets `+strict-align` to prevent the compiler
21-
generating unaligned accesses so it should be alright, but this is not
22-
necessarily the case in general.
23-
- If it were running in a VM, this can lead to cache coherency issues. The
24-
problem is that the VM is accessing memory directly with the cache disabled,
25-
while the host has cacheable aliases to the same memory. Even if the host
26-
doesn't explicitly access the memory, speculative accesses can lead to cache
27-
fills, and then changes from one or the other will get lost. Again this is
28-
alright in this particular case (running directly on the hardware with no
29-
hypervisor), but isn't a good pattern in general.
18+
enabled. This will read and write memory (e.g. the stack). However, this has
19+
the problems mentioned at the beginning of this session regarding unaligned
20+
access and cache coherency.
3021

3122
</details>

src/bare-metal/aps/uart/traits.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ too.
1111

1212
- Implementing `Write` lets us use the `write!` and `writeln!` macros with our
1313
`Uart` type.
14-
- Run the example in QEMU with `make qemu_minimal` under
15-
`src/bare-metal/aps/examples`.
14+
15+
- `Send` is an auto-trait, but not implemented automatically because it is not
16+
implemented for pointers.
1617

1718
</details>

src/bare-metal/aps/uart/using.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Using it
2+
3+
Let's write a small program using our driver to write to the serial console.
4+
5+
```rust,editable,compile_fail
6+
{{#include ../examples/src/main_minimal.rs:main}}
7+
```
8+
9+
<details>
10+
11+
- As in the [inline assembly](../inline-assembly.md) example, this `main`
12+
function is called from our entry point code in `entry.S`. See the speaker
13+
notes there for details.
14+
- Run the example in QEMU with `make qemu_minimal` under
15+
`src/bare-metal/aps/examples`.
16+
17+
</details>

src/bare-metal/useful-crates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Useful crates
22

3-
We'll go over a few crates which solve some common problems in bare-metal
3+
We'll look at a few crates which solve some common problems in bare-metal
44
programming.

src/bare-metal/useful-crates/buddy_system_allocator.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# `buddy_system_allocator`
22

33
[`buddy_system_allocator`][1] is a crate implementing a basic buddy system
4-
allocator. It can be used both for [`LockedHeap`][2] implementing
5-
[`GlobalAlloc`][3] so you can use the standard `alloc` crate (as we saw
6-
[before][4]), or for allocating other address space. For example, we might want
7-
to allocate MMIO space for PCI BARs:
4+
allocator. It can be used both to implement [`GlobalAlloc`][3] (using
5+
[`LockedHeap`][2]) so you can use the standard `alloc` crate (as we saw
6+
[before][4]), or for allocating other address space (using
7+
[`FrameAllocator`][5]) . For example, we might want to allocate MMIO space for
8+
PCI BARs:
89

910
<!-- mdbook-xgettext: skip -->
1011

@@ -25,3 +26,4 @@ to allocate MMIO space for PCI BARs:
2526
[2]: https://docs.rs/buddy_system_allocator/0.9.0/buddy_system_allocator/struct.LockedHeap.html
2627
[3]: https://doc.rust-lang.org/core/alloc/trait.GlobalAlloc.html
2728
[4]: ../alloc.md
29+
[5]: https://docs.rs/buddy_system_allocator/0.9.0/buddy_system_allocator/struct.FrameAllocator.html

src/bare-metal/useful-crates/spin.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ primitives.
1212
```rust,editable,compile_fail
1313
use spin::mutex::SpinMutex;
1414
15-
static counter: SpinMutex<u32> = SpinMutex::new(0);
15+
static COUNTER: SpinMutex<u32> = SpinMutex::new(0);
1616
1717
fn main() {
18-
println!("count: {}", counter.lock());
19-
*counter.lock() += 2;
20-
println!("count: {}", counter.lock());
18+
println!("count: {}", COUNTER.lock());
19+
*COUNTER.lock() += 2;
20+
println!("count: {}", COUNTER.lock());
2121
}
2222
```
2323

0 commit comments

Comments
 (0)